Re: psql can't subtract

Lists: pgsql-general
From: Rob Sargent <robjsargent(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: psql can't subtract
Date: 2011-03-25 15:29:57
Message-ID: 4D8CB4F5.4010302@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Running 9.0.3 (client and server)

Seems I cannot subtract 1 from the result of position.

select distinct
substring( substring(xml_text,1,300),
position( 'xmlns=' in substring(xml_text,1,300)) +
length('xmlns="'),
position( '"' in (substring(substring(xml_text,1,300),
position( 'xmlns=' in
substring(xml_text,1,300)) + length('xmlns="'),
100)))
) as namespace
from elements;

gives out put as below:

+-----------------------------------------------------+
| namespace |
+-----------------------------------------------------+
| |
| http://amirsys.com/ns/acres/anatomy/breast-mri/1.0" |
| http://amirsys.com/ns/acres/anatomy/intro/1.0" |
| http://amirsys.com/ns/acres/anatomymodule/1.0" |
| http://amirsys.com/ns/acres/calculator/1.0" |
...
| http://amirsys.com/ns/acres/table/1.0" |
| http://amirsys.com/ns/acres/tableintro/1.0" |
| http://amirsys.com/ns/acres/tsm/1.4" |
+-----------------------------------------------------+
(41 rows)

and I want to drop the last char, the double quote mark but

select distinct
substring( substring(xml_text,1,300),
position( 'xmlns=' in substring(xml_text,1,300)) +
length('xmlns="'),
position( '"' in (substring(substring(xml_text,1,300),
position( 'xmlns=' in
substring(xml_text,1,300)) + length('xmlns="'),
100))) -1
) as namespace
from elements;

gives
ERROR: negative substring length not allowed.

But adding one to position() works, as you can see with the addition of
the right angle bracket on some of the lines (there's an added space on
the others).

select distinct
substring( substring(xml_text,1,300),
position( 'xmlns=' in substring(xml_text,1,300)) +
length('xmlns="'),
position( '"' in (substring(substring(xml_text,1,300),
position( 'xmlns=' in
substring(xml_text,1,300)) + length('xmlns="'),
100))) + 1
) as namespace
from elements;

+------------------------------------------------------+
| namespace |
+------------------------------------------------------+
| http://amirsys.com/ns/acres/anatomy/breast-mri/1.0"> |
| http://amirsys.com/ns/acres/anatomy/intro/1.0"> |
| http://amirsys.com/ns/acres/anatomymodule/1.0"> |
| http://amirsys.com/ns/acres/calculator/1.0"> |
....
| http://amirsys.com/ns/acres/casechallenge/1.0"> |
| http://amirsys.com/ns/acres/tsm/1.4"> |
| http://amirsys.com/ns/acres/tsm/1.4" |
| l |
+------------------------------------------------------+
(63 rows)

What I did to solve the issue was change the search string in the last
position() to "mlns=", but that of course is an egregious hack...

pg_config gives
BINDIR = /opt/PostgreSQL/9.0.3/bin
DOCDIR = /opt/PostgreSQL/9.0.3/share/doc/postgresql
HTMLDIR = /opt/PostgreSQL/9.0.3/share/doc/postgresql
INCLUDEDIR = /opt/PostgreSQL/9.0.3/include
PKGINCLUDEDIR = /opt/PostgreSQL/9.0.3/include/postgresql
INCLUDEDIR-SERVER = /opt/PostgreSQL/9.0.3/include/postgresql/server
LIBDIR = /opt/PostgreSQL/9.0.3/lib
PKGLIBDIR = /opt/PostgreSQL/9.0.3/lib/postgresql
LOCALEDIR = /opt/PostgreSQL/9.0.3/share/locale
MANDIR = /opt/PostgreSQL/9.0.3/share/man
SHAREDIR = /opt/PostgreSQL/9.0.3/share/postgresql
SYSCONFDIR = /opt/PostgreSQL/9.0.3/etc/postgresql
PGXS = /opt/PostgreSQL/9.0.3/lib/postgresql/pgxs/src/makefiles/pgxs.mk
CONFIGURE = '--prefix=/opt/PostgreSQL/9.0.3' '--with-openssl'
'--with-ldap' '--with-libxml' '--with-ossp-uuid' '--with-pgport=5431'
'--with-python'
CC = gcc
CPPFLAGS = -D_GNU_SOURCE -I/usr/include/libxml2
CFLAGS = -O2 -Wall -Wmissing-prototypes -Wpointer-arith
-Wdeclaration-after-statement -Wendif-labels -fno-strict-aliasing -fwrapv
CFLAGS_SL = -fpic
LDFLAGS = -Wl,--as-needed
-Wl,-rpath,'/opt/PostgreSQL/9.0.3/lib',--enable-new-dtags
LDFLAGS_EX =
LDFLAGS_SL =
LIBS = -lpgport -lxml2 -lssl -lcrypto -lz -lreadline -lcrypt -ldl -lm
VERSION = PostgreSQL 9.0.3


From: Richard Huxton <dev(at)archonet(dot)com>
To: Rob Sargent <robjsargent(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: psql can't subtract
Date: 2011-03-25 15:57:24
Message-ID: 4D8CBB64.1060408@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: Postg토토 사이트 순위SQL

On 25/03/11 15:29, Rob Sargent wrote:
> Running 9.0.3 (client and server)

> select distinct
> substring(
...
> position( 'xmlns=' in substring(xml_text,1,300)) + length('xmlns="'),
> 100))) -1
...
> gives
> ERROR: negative substring length not allowed.

If there is no match the initial position will be zero. Minus one will
give you a negative substring length.

--
Richard Huxton
Archonet Ltd


From: Rob Sargent <robjsargent(at)gmail(dot)com>
To: Richard Huxton <dev(at)archonet(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: psql can't subtract
Date: 2011-03-25 16:30:30
Message-ID: 4D8CC326.8070508@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On 03/25/2011 09:57 AM, Richard Huxton wrote:
> On 25/03/11 15:29, Rob Sargent wrote:
>> Running 9.0.3 (client and server)
>
>> select distinct
>> substring(
> ...
>> position( 'xmlns=' in substring(xml_text,1,300)) + length('xmlns="'),
>> 100))) -1
> ...
>> gives
>> ERROR: negative substring length not allowed.
>
> If there is no match the initial position will be zero. Minus one will
> give you a negative substring length.
>
Of course. One lousy malformed input record. The speed at which the
failure occurred lead me to think it was a parsing issue. Turns out the
bogus record was "near the front of the queue".

Thanks


From: hubert depesz lubaczewski <depesz(at)depesz(dot)com>
To: Rob Sargent <robjsargent(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: psql can't subtract
Date: 2011-03-25 16:59:00
Message-ID: 20110325165900.GA16890@depesz.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On Fri, Mar 25, 2011 at 09:29:57AM -0600, Rob Sargent wrote:
> Running 9.0.3 (client and server)
>
>
> Seems I cannot subtract 1 from the result of position.
>
> select distinct
> substring( substring(xml_text,1,300),
> position( 'xmlns=' in substring(xml_text,1,300)) +
> length('xmlns="'),
> position( '"' in (substring(substring(xml_text,1,300),
> position( 'xmlns=' in
> substring(xml_text,1,300)) + length('xmlns="'),
> 100)))
> ) as namespace
> from elements;

is there any particular reason why you're writing something that
complicated instead of using simple regexp?

select distinct regexp_replace( xml_text, '.*xmlns="([^"]+)".*', E'\\1') from elements;

Best regards,

depesz

--
The best thing about modern society is how easy it is to avoid contact with it.
http://depesz.com/


From: Rob Sargent <robjsargent(at)gmail(dot)com>
To: depesz(at)depesz(dot)com
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: psql can't subtract
Date: 2011-03-25 17:05:53
Message-ID: 4D8CCB71.6090506@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On 03/25/2011 10:59 AM, hubert depesz lubaczewski wrote:
> On Fri, Mar 25, 2011 at 09:29:57AM -0600, Rob Sargent wrote:
>> Running 9.0.3 (client and server)
>>
>>
>> Seems I cannot subtract 1 from the result of position.
>>
>> select distinct
>> substring( substring(xml_text,1,300),
>> position( 'xmlns=' in substring(xml_text,1,300)) +
>> length('xmlns="'),
>> position( '"' in (substring(substring(xml_text,1,300),
>> position( 'xmlns=' in
>> substring(xml_text,1,300)) + length('xmlns="'),
>> 100)))
>> ) as namespace
>> from elements;
>
> is there any particular reason why you're writing something that
> complicated instead of using simple regexp?
>
> select distinct regexp_replace( xml_text, '.*xmlns="([^"]+)".*', E'\\1') from elements;
>
> Best regards,
>
> depesz
>

Just gross incompetency with regexps. Much thanks.