Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nYpqE-0006v4-Cv for pgsql-bugs@arkaria.postgresql.org; Mon, 28 Mar 2022 13:55:30 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nYpqD-0001bR-5J for pgsql-bugs@arkaria.postgresql.org; Mon, 28 Mar 2022 13:55:29 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nYpqC-0001bI-TQ for pgsql-bugs@lists.postgresql.org; Mon, 28 Mar 2022 13:55:28 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nYpqA-00032n-Pd for pgsql-bugs@lists.postgresql.org; Mon, 28 Mar 2022 13:55:28 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 22SDtLJs703081; Mon, 28 Mar 2022 09:55:21 -0400 From: Tom Lane To: Pavel Borisov cc: depesz@depesz.com, ganguly.04@gmail.com, PostgreSQL mailing lists Subject: Re: BUG #17450: SUBSTRING function extracting lesser characters than specified In-reply-to: References: <17450-69df48455c28fbea@postgresql.org> <20220328110119.GA28018@depesz.com> Comments: In-reply-to Pavel Borisov message dated "Mon, 28 Mar 2022 16:35:30 +0400" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <703079.1648475721.1@sss.pgh.pa.us> Date: Mon, 28 Mar 2022 09:55:21 -0400 Message-ID: <703080.1648475721@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Pavel Borisov 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 is specified, then: a) Let C be the value of the , let LC be the length of C, and let S be the value of the . b) If is specified, then let L be the value of 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 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 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 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