pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Ashutosh Sharma <ashu.coek88@gmail.com>
Cc: bvimalvictor@gmail.com, PostgreSQL mailing lists <pgsql-bugs@lists.postgresql.org>
Subject: Re: BUG #15971: Behaviour of SUBSTR function depending on its arguments
Date: Wed, 21 Aug 2019 09:45:13 -0400
Message-ID: <29036.1566395113@sss.pgh.pa.us> (raw)
In-Reply-To: <CAE9k0PmKOMkeABqF88vrac3hFmBpkUD=mV=xw0Y1NnhxDyaB4Q@mail.gmail.com>
References: <15971-965406c5a667c01a@postgresql.org>
	<CAE9k0PmKOMkeABqF88vrac3hFmBpkUD=mV=xw0Y1NnhxDyaB4Q@mail.gmail.com>

Ashutosh Sharma <ashu.coek88@gmail.com> writes:
> I don't think it's a bug. I think it's just that the SUBSTR in
> PostgreSQL is more SQL compliant than any other databases that you
> mentioned.  As per the SQL standard, if the start position is zero or
> negative number, it should be adjusted to the start of the string and
> not to the end of the string and that way I feel the behaviour of
> SUBSTR in PostgreSQL is more SQL compliant than other databases.

Yes, the standard provides no wiggle room here.  The behavior of
substring() with integer parameters, as specified in SQL:2008
6.29 <string value function> general rule 3, is

3) If <character substring function> is specified, then:

  a) If the character encoding form of <character value expression> is
  UTF8, UTF16, or UTF32, then, in the remainder of this General Rule, the
  term “character” shall be taken to mean “unit specified by <char length
  units>”.

  b) Let C be the value of the <character value expression>, let LC be the
  length in characters of C, and let S be the value of the <start
  position>.

  c) If <string length> is specified, then let L be the value of <string
  length> and let E be S+L. Otherwise, let E be the larger of LC + 1 and
  S.

  d) If either C, S, or L is the null value, then the result of the
  <character substring function> is the null value.

  e) If E is less than S, then an exception condition is raised: data
  exception — substring error.

  f) Case:

    i) If S is greater than LC or if E is less than 1 (one), then the
    result of the <character substring function> is a zero-length string.

    ii) Otherwise,

      1) Let S1 be the larger of S and 1 (one). Let E1 be the smaller of E
      and LC+1. Let L1 be E1–S1.

      2) The result of the <character substring function> is a character
      string containing the L1 characters of C starting at character
      number S1 in the same order that the characters appear in C.

I believe our implementation does this exactly.

Even if it were true that Oracle's behavior is more common than the
spec's definition, it's quite unlikely that we could be talked into
abandoning spec-compliant behavior to match Oracle.

			regards, tom lane





view thread (6+ messages)  latest in thread

Message-ID: <29036.1566395113@sss.pgh.pa.us>
Permalink:  ../29036.1566395113@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/29036.1566395113@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: tgl@sss.pgh.pa.us, ashu.coek88@gmail.com
  Subject: Re: BUG #15971: Behaviour of SUBSTR function depending on its arguments
  In-Reply-To: <29036.1566395113@sss.pgh.pa.us>
  References: <15971-965406c5a667c01a@postgresql.org> <CAE9k0PmKOMkeABqF88vrac3hFmBpkUD=mV=xw0Y1NnhxDyaB4Q@mail.gmail.com> <29036.1566395113@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox