Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UapZY-0000xi-0o for pgsql-bugs@arkaria.postgresql.org; Fri, 10 May 2013 15:49:28 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1UapZX-0005k3-Cr for pgsql-bugs@arkaria.postgresql.org; Fri, 10 May 2013 15:49:27 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UapZW-0005jw-8S for pgsql-bugs@postgresql.org; Fri, 10 May 2013 15:49:26 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UapZQ-0001hD-VJ for pgsql-bugs@postgresql.org; Fri, 10 May 2013 15:49:25 +0000 Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.5/8.14.5) with ESMTP id r4AFnIts008416; Fri, 10 May 2013 11:49:19 -0400 (EDT) From: Tom Lane To: Patryk Kordylewski cc: pgsql-bugs@postgresql.org Subject: Re: Inconsistency between TO_CHAR() and TO_NUMBER() In-reply-to: <5187C2FE.9000305@fooby.de> References: <51878292.6080003@fooby.de> <14272.1367850378@sss.pgh.pa.us> <5187C2FE.9000305@fooby.de> Comments: In-reply-to Patryk Kordylewski message dated "Mon, 06 May 2013 16:49:34 +0200" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <8191.1368200826.0@sss.pgh.pa.us> Date: Fri, 10 May 2013 11:49:18 -0400 Message-ID: <8415.1368200958@sss.pgh.pa.us> X-Pg-Spam-Score: -3.2 (---) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <8191.1368200826.1@sss.pgh.pa.us> Patryk Kordylewski writes: > SET lc_numeric TO 'de_DE.UTF-8'; > SET > SELECT > TO_CHAR(123456789.123, 'FM99G999G999G999G999G999G999D000'), > TO_NUMBER(TO_CHAR(123456789.123, 'FM99G999G999G999G999G999G999D000'), > 'FM99G999G999G999G999G999G999D000'); > to_char | to_number > -----------------+----------- > 123.456.789,123 | 123.456 > (1 row) I looked into this, and find that the reason it misbehaves is that NUM_numpart_from_char() will treat a '.' as being a decimal point *without any regard to locale considerations*. So even if we have a locale-dependent format string and a locale that says '.' is a thousands separator, it does the wrong thing. It's a bit surprising nobody's complained of this before. I propose the attached patch. I'm slightly worried though about whether this might break any existing applications that are (incorrectly) depending on a D format specifier being able to match '.' regardless of locale. Perhaps we should only apply this to HEAD and not back-patch? regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-patch; charset="us-ascii" Content-ID: <8191.1368200826.2@sss.pgh.pa.us> Content-Description: fix-to-number-locale-thousands.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/utils/adt/formatting.c b/src/backend/utils/adt/for= matting.c index db5dfca51d477d3e9b33b8d2c264495b3b2ec433..81e3329ef60ce4f835fedba5020= 8b8d0f4b19d63 100644 *** a/src/backend/utils/adt/formatting.c --- b/src/backend/utils/adt/formatting.c *************** NUM_numpart_from_char(NUMProc *Np, int i *** 4131,4137 **** #endif =20=20 /* ! * read digit */ if (isdigit((unsigned char) *Np->inout_p)) { --- 4131,4137 ---- #endif =20=20 /* ! * read digit or decimal point */ if (isdigit((unsigned char) *Np->inout_p)) { *************** NUM_numpart_from_char(NUMProc *Np, int i *** 4151,4190 **** #ifdef DEBUG_TO_FROM_CHAR elog(DEBUG_elog_output, "Read digit (%c)", *Np->inout_p); #endif -=20 - /* - * read decimal point - */ } else if (IS_DECIMAL(Np->Num) && Np->read_dec =3D=3D FALSE) { #ifdef DEBUG_TO_FROM_CHAR ! elog(DEBUG_elog_output, "Try read decimal point (%c)", *Np->inout_p); #endif ! if (*Np->inout_p =3D=3D '.') { *Np->number_p =3D '.'; Np->number_p++; Np->read_dec =3D TRUE; isread =3D TRUE; } - else - { - int x =3D strlen(Np->decimal); -=20 - #ifdef DEBUG_TO_FROM_CHAR - elog(DEBUG_elog_output, "Try read locale point (%c)", - *Np->inout_p); - #endif - if (x && AMOUNT_TEST(x) && strncmp(Np->inout_p, Np->decimal, x) =3D=3D= 0) - { - Np->inout_p +=3D x - 1; - *Np->number_p =3D '.'; - Np->number_p++; - Np->read_dec =3D TRUE; - isread =3D TRUE; - } - } } =20=20 if (OVERLOAD_TEST) --- 4151,4178 ---- #ifdef DEBUG_TO_FROM_CHAR elog(DEBUG_elog_output, "Read digit (%c)", *Np->inout_p); #endif } else if (IS_DECIMAL(Np->Num) && Np->read_dec =3D=3D FALSE) { + /* + * We need not test IS_LDECIMAL(Np->Num) explicitly here, because + * Np->decimal is always just "." if we don't have a D format token. + * So we just unconditionally match to Np->decimal. + */ + int x =3D strlen(Np->decimal); +=20 #ifdef DEBUG_TO_FROM_CHAR ! elog(DEBUG_elog_output, "Try read decimal point (%c)", ! *Np->inout_p); #endif ! if (x && AMOUNT_TEST(x) && strncmp(Np->inout_p, Np->decimal, x) =3D=3D = 0) { + Np->inout_p +=3D x - 1; *Np->number_p =3D '.'; Np->number_p++; Np->read_dec =3D TRUE; isread =3D TRUE; } } =20=20 if (OVERLOAD_TEST) ------- =_aaaaaaaaaa0 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs ------- =_aaaaaaaaaa0--