Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WuhHW-0000ic-95 for pgsql-odbc@arkaria.postgresql.org; Wed, 11 Jun 2014 12:05:30 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1WuhHV-0007US-4M for pgsql-odbc@arkaria.postgresql.org; Wed, 11 Jun 2014 12:05:29 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1WuhHU-0007UL-4q for pgsql-odbc@postgresql.org; Wed, 11 Jun 2014 12:05:28 +0000 Received: from sd.tpf.co.jp ([210.161.239.34]) by makus.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1WuhHP-00053L-U5 for pgsql-odbc@postgresql.org; Wed, 11 Jun 2014 12:05:26 +0000 Received: (qmail 903 invoked by uid 512); 11 Jun 2014 12:05:22 -0000 X-Authentication: inoue was authenticated by sd.tpf.co.jp at 11 Jun 2014 12:05:22 -0000 Received: from unknown (HELO ?192.168.1.4?) (183.76.63.174) by sd.tpf.co.jp with SMTP; 11 Jun 2014 12:05:22 -0000 Message-ID: <539845FE.30107@tpf.co.jp> Date: Wed, 11 Jun 2014 17:50:18 +0545 From: Hiroshi Inoue User-Agent: Mozilla/5.0 (Windows NT 6.0; rv:24.0) Gecko/20100101 Thunderbird/24.5.0 MIME-Version: 1.0 To: Heikki Linnakangas , Walter Couto , "pgsql-odbc@postgresql.org" Subject: Re: Problem retrieving a numeric(38,0) value as SQL_NUMERIC_STRUCT if value needs to use all 16 SQLCHAR elements of the val array References: <7FA4F9E5512F214C801E596ECD8790E066A79839@ETNAMAIL01.embarcadero.com>, <538D44EC.10201@tpf.co.jp> <538E9137.1000308@tpf.co.jp> <7FA4F9E5512F214C801E596ECD8790E066A7FCE4@ETNAMAIL01.embarcadero.com> <5396F0CF.4040107@vmware.com> In-Reply-To: <5396F0CF.4040107@vmware.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-odbc Precedence: bulk Sender: pgsql-odbc-owner@postgresql.org Thanks for your investigation. I don't object to the change. Probably few people used SQL_C_NUMERIC extensively. regards, Hiroshi Inoue (2014/06/10 17:34), Heikki Linnakangas wrote: > Ugh, the decimal->binary conversion algorithm is really ugly even before > this patch, and the patch doesn't make it any prettier. It took me quite > a while to understand how it works: it repeatedly divides the base-10 > representation by two, and outputs the reminder bit. There are much > better and faster ways to convert from decimal to binary, and they're > not really any more complicated either. > > The binary->decimal conversion in ResolveNumericParam is even more ugly. > To be honest, I still don't understand the algorithm behind it, but it > looks complicated and slow. There's a fast-path for small numbers that > fit in an UInt4, which doesn't make it any simpler. > > Aside from ugly and slow, the binary->decimal conversion is also buggy: > > 1.The following input creates a core dump with "Floating point exception": > > precision=3, scale=50, val=0x02 0x03 > > 2. Building the final output string is broken if scale is large, e.g. > this produces garbage output: > > precision=25, scale=80 > > ERROR: invalid input syntax for type numeric: > "p.000`000H0000000000000000000000aT000000000000000000000000000000000000000000000770"; > > > 3. The param_string buffer allocated for the result of the > binary->decimal conversion is too small. The buffer is 128 bytes, but a > numeric can have a scale as large as 127. That won't fit, as the sign > and decimal dot use two more bytes. > > I propose the attached patch to fix all of the above, rewriting the > binary->decimal and decimal->binary conversions altogether. It fixes all > of the known issues, the ones listed above and the one Walter reported. > It also adds a regression test for all of them. > > Can anyone come up with some more special cases that this patched or the > old conversion might handle incorrectly, for adding to the regression > suite? > > > One more thing: The precision works in a funny way, if you pass a "val" > with more bits than fit in precision. I filled the val-bytes with 0x9F > 0x86 0x01, which corresponds to 99999 in decimal. I passed that as a > param with different precisions: > > sign 1 prec 1 scale 0 val 9F8601: > 159 > sign 1 prec 2 scale 0 val 9F8601: > 159 > sign 1 prec 3 scale 0 val 9F8601: > 34463 > sign 1 prec 4 scale 0 val 9F8601: > 34463 > sign 1 prec 5 scale 0 val 9F8601: > 99999 > sign 1 prec 6 scale 0 val 9F8601: > 99999 > > The conversion algorithm seems to assume that there are no extra bits in > the val-array that don't fit within the precision. Perhaps that's a > reasonable assumption, but I think it would be more robust to ignore any > extra bits if they are there, rather than sometimes take some of them > into account. (Or, always parse all the bits, ignoring the precision.) I > did that in the attached patch. > > However, according to this article I found from Microsoft: > https://support.microsoft.com/kb/222831 > > "The precision and scale fields of the numeric structure are never used > for input from an application, only for output from the driver to the > application." > > So we probably should be ignoring them completely, and get the precision > and scale from elsewhere. > > > PS. ResolveNumericParam always returns TRUE. That's fortunate, because > the caller would incorrectly fall through the switch-case if it didn't.. > When the code was added, it fell through to the "default" case, but the > interval-cases were later added in between, breaking it. But it's not a > live bug, since ResolveNumericParam always returns TRUE. Fixed that too > in the attached patch. > > - Heikki > -- Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-odbc