Re: BUG #17450: SUBSTRING function extracting lesser characters than specified

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Pavel Borisov <pashkin(dot)elfe(at)gmail(dot)com>
Cc: depesz(at)depesz(dot)com, ganguly(dot)04(at)gmail(dot)com, PostgreSQL mailing lists <pgsql-bugs(at)lists(dot)postgresql(dot)org>
Subject: Re: BUG #17450: SUBSTRING function extracting lesser characters than specified
Date: 2022-03-28 13:55:21
Message-ID: 703080.1648475721@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Pavel Borisov <pashkin(dot)elfe(at)gmail(dot)com> writes:
> Yes, the behavior expected in a report seems to be right. I've attached a
> very small fix for that issue. Probably it should be backpatched into all
> versions having 4bd3fad80e5c i.e. since v11.

The current calculation matches the SQL standard; this makes
it not match the standard. From SQL99:

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

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

b) 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.

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

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

e) 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.

Perhaps there's something to do here documentation-wise,
but there is no bug.

regards, tom lane

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Japin Li 2022-03-28 14:00:06 Re: BUG #17450: SUBSTRING function extracting lesser characters than specified
Previous Message Pavel Borisov 2022-03-28 12:35:30 Re: BUG #17450: SUBSTRING function extracting lesser characters than specified