pg.ddx.io  pgsql-odbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Heikki Linnakangas <hlinnakangas@vmware.com>
To: Faith, Jeremy <jfaith@tycoint.com>
To: pgsql-odbc@postgresql.org <pgsql-odbc@postgresql.org>
Subject: Re: Re: [BUGS] BUG #11767: ODBC driver bug when fetching constant string columns
Date: Tue, 2 Dec 2014 16:02:16 +0200
Message-ID: <547DC668.2000106@vmware.com> (raw)
In-Reply-To: <55BAADA01D8C504993894EAEA7517CF80BA6C3@003FCH1MPN2-001.003f.mgd2.msft.net>
References: <55BAADA01D8C504993894EAEA7517CF80BA6C3@003FCH1MPN2-001.003f.mgd2.msft.net>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-odbc>

On 12/02/2014 02:28 PM, Faith, Jeremy wrote:
>> On Fri, Oct 24, 2014 at 9:36 AM, Michael Paquier <michael(dot)paquier(at)gmail(dot)com>
>> wrote:
>>
>>> On Wed, Oct 22, 2014 at 11:22 PM,  <keyurgovande(at)gmail(dot)com> wrote:
>>>> The following bug has been logged on the website:
>>>>
>>>> Bug reference:      11767
>>>> Logged by:          Keyur Govande
>>>> Email address:      keyurgovande(at)gmail(dot)com
>>>> PostgreSQL version: 9.0.3
>>> You should update to 9.0.18, you are 3.5 years of bug fixes.
>>>
>>
>> Ah I apologize, I chose the wrong version in the bug report dropdown. This
>> bug was observed with the latest driver: psqlodbc-09.03.0300 connecting to
>> a Postgres server 9.1.14.
>>
> I can confirm that this bug exists in psqlodbc-09.03.0300 connecting to PostgreSQL 9.3.5.

Ok, thanks for the testing.

>>>> If we run a SELECT statement like: SELECT id, varchar_col, date_col,
>>> 'random
>>>> string' as random;
>>>> via ODBC, then the last column comes back as junk into PHP.
>>>>
>>>> In PHP we use the SQL_DESC_OCTET_LENGTH to figure out how many bytes to
>>>> allocate. But for that column, SQL_DESC_OCTET_LENGTH returns 0 and we
>>> don't
>>>> allocate enough space.

(https://bugs.php.net/bug.php?id=68087)

Hmm. Why does that cause junk data to be returned, though? Even if you 
allocate a single byte of storage, that's enough for the 
null-terminator, and you should just end up with an empty string, not junk.

>>>> The driver issue seems to be that when SQLColAttributes() is called with
>>>> SQL_COLUMN_TYPE for column 'random', it returns SQL_VARCHAR even though
>>> the
>>>> PG type is PG_TYPE_UNKNOWN. The promotion is happening here:
>>>>
>>> http://git.postgresql.org/gitweb/?p=psqlodbc.git;a=blob;f=pgtypes.c;h=4ce7636b69d371d9f511d492c68013...
>>>>
>>>> I think the same promotion needs to happen when fetching octet-length
>>> here:
>>>>
>>> http://git.postgresql.org/gitweb/?p=psqlodbc.git;a=blob;f=pgtypes.c;h=4ce7636b69d371d9f511d492c68013...
>>>>
>>>> Thoughts? This is not a problem when using the Vertica ODBC driver for
>>>> example.

Yeah, we should fix that in psqlodbc. We can't really determine a sane 
max size for the column, but we should do the same for unknown that we 
do for text or varchar columns. I believe the attached patch would fix 
this. The fix itself is a single line, but it includes a test case.

Note that a text (or unknown) column can hold up to 1 GB of text. By the 
letter of the spec, I think SQL_DESC_OCTET_LENGTH should return 1GB for 
a text column (or an unknown-type column), but that would be 
impractical. So the driver just makes a guess. The data will be 
truncated, if the value in the column is long enough. It would be good 
to check for SQLSTATE=01004 result from SQLFetch, and perhaps resize the 
buffers or give a warning or error, if truncation happens.

- Heikki

-- 
Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-odbc

Attachments:

fix-octet-length-for-unknown-1.patch (text/x-diff, 5.7K) download diff --git a/Makefile.am b/Makefile.am index 797bee4..4b06c8d 100644 --- a/Makefile.am +++ b/Makefile.am @@ -93,6 +93,7 @@ EXTRA_DIST = license.txt readme.txt readme_winbuild.txt \ test/expected/bookmark.out \ test/expected/boolsaschar.out \ test/expected/catalogfunctions.out \ + test/expected/colattribute.out \ test/expected/commands.out \ test/expected/connect.out \ test/expected/cte.out \ @@ -135,6 +136,7 @@ EXTRA_DIST = license.txt readme.txt readme_winbuild.txt \ test/src/bookmark-test.c \ test/src/boolsaschar-test.c \ test/src/catalogfunctions-test.c \ + test/src/colattribute-test.c \ test/src/commands-test.c \ test/src/common.c \ test/src/common.h \ diff --git a/pgtypes.c b/pgtypes.c index 60a94bb..05191ef 100644 --- a/pgtypes.c +++ b/pgtypes.c @@ -1198,11 +1198,13 @@ pgtype_attr_transfer_octet_length(const ConnectionClass *conn, OID type, int att { int coef = 1; Int4 maxvarc, column_size; + switch (type) { case PG_TYPE_VARCHAR: case PG_TYPE_BPCHAR: case PG_TYPE_TEXT: + case PG_TYPE_UNKNOWN: column_size = pgtype_attr_column_size(conn, type, atttypmod, PG_UNSPECIFIED, handle_unknown_size_as); if (SQL_NO_TOTAL == column_size) return column_size; diff --git a/test/expected/colattribute.out b/test/expected/colattribute.out new file mode 100644 index 0000000..e9661b2 --- /dev/null +++ b/test/expected/colattribute.out @@ -0,0 +1,96 @@ +\! "./src/colattribute-test" +Running tests with UnknownSizes=-1;MaxVarcharSize=100... +connected +Testing SQLColAttribute... + +-- Column 1: intcol -- +SQL_DESC_OCTET_LENGTH: 0 + +-- Column 2: textcol -- +SQL_DESC_OCTET_LENGTH: 49140 + +-- Column 3: unknowncol -- +SQL_DESC_OCTET_LENGTH: 100 + +-- Column 4: varcharcol -- +SQL_DESC_OCTET_LENGTH: 100 + +-- Column 5: empty_varchar_col -- +SQL_DESC_OCTET_LENGTH: 100 +disconnecting +Running tests with UnknownSizes=0;MaxVarcharSize=100... +connected +Testing SQLColAttribute... + +-- Column 1: intcol -- +SQL_DESC_OCTET_LENGTH: 0 + +-- Column 2: textcol -- +SQL_DESC_OCTET_LENGTH: 49140 + +-- Column 3: unknowncol -- +SQL_DESC_OCTET_LENGTH: 100 + +-- Column 4: varcharcol -- +SQL_DESC_OCTET_LENGTH: 100 + +-- Column 5: empty_varchar_col -- +SQL_DESC_OCTET_LENGTH: 100 +disconnecting +Running tests with UnknownSizes=1;MaxVarcharSize=100... +connected +Testing SQLColAttribute... + +-- Column 1: intcol -- +SQL_DESC_OCTET_LENGTH: 0 + +-- Column 2: textcol -- +SQL_DESC_OCTET_LENGTH: 49140 + +-- Column 3: unknowncol -- +SQL_DESC_OCTET_LENGTH: 100 + +-- Column 4: varcharcol -- +SQL_DESC_OCTET_LENGTH: 100 + +-- Column 5: empty_varchar_col -- +SQL_DESC_OCTET_LENGTH: 100 +disconnecting +Running tests with UnknownSizes=2;MaxVarcharSize=100... +connected +Testing SQLColAttribute... + +-- Column 1: intcol -- +SQL_DESC_OCTET_LENGTH: 0 + +-- Column 2: textcol -- +SQL_DESC_OCTET_LENGTH: 36 + +-- Column 3: unknowncol -- +SQL_DESC_OCTET_LENGTH: 84 + +-- Column 4: varcharcol -- +SQL_DESC_OCTET_LENGTH: 84 + +-- Column 5: empty_varchar_col -- +SQL_DESC_OCTET_LENGTH: 100 +disconnecting +Running tests with UnknownSizes=100;MaxVarcharSize=100... +connected +Testing SQLColAttribute... + +-- Column 1: intcol -- +SQL_DESC_OCTET_LENGTH: 0 + +-- Column 2: textcol -- +SQL_DESC_OCTET_LENGTH: 49140 + +-- Column 3: unknowncol -- +SQL_DESC_OCTET_LENGTH: 100 + +-- Column 4: varcharcol -- +SQL_DESC_OCTET_LENGTH: 100 + +-- Column 5: empty_varchar_col -- +SQL_DESC_OCTET_LENGTH: 100 +disconnecting diff --git a/test/src/colattribute-test.c b/test/src/colattribute-test.c new file mode 100644 index 0000000..b6ed51d --- /dev/null +++ b/test/src/colattribute-test.c @@ -0,0 +1,70 @@ +/* + * Test SQLColAttribute + */ + +#include <string.h> +#include <stdio.h> +#include <stdlib.h> + +#include "common.h" + +static void +runtest(char *extra_conn_options) +{ + int rc; + HSTMT hstmt = SQL_NULL_HSTMT; + SQLUSMALLINT i; + + printf("Running tests with %s...\n", extra_conn_options); + + /* The behavior of these tests depend on the UnknownSizes parameter */ + test_connect_ext(extra_conn_options); + + rc = SQLAllocHandle(SQL_HANDLE_STMT, conn, &hstmt); + if (!SQL_SUCCEEDED(rc)) + { + print_diag("failed to allocate stmt handle", SQL_HANDLE_DBC, conn); + exit(1); + } + + /* + * Get column attributes of a simple query. + */ + printf("Testing SQLColAttribute...\n"); + rc = SQLExecDirect(hstmt, + (SQLCHAR *) "SELECT '1'::int AS intcol, 'foobar'::text AS textcol, 'unknown string' AS unknowncol, 'varchar string' as varcharcol, '' as empty_varchar_col", + SQL_NTS); + CHECK_STMT_RESULT(rc, "SQLExecDirect failed", hstmt); + for (i = 1 ; i <= 5; i++) + { + char buffer[64]; + SQLLEN number; + + rc = SQLColAttribute(hstmt, i, SQL_DESC_LABEL, buffer, 64, NULL, NULL); + CHECK_STMT_RESULT(rc, "SQLColAttribute failed", hstmt); + printf("\n-- Column %d: %s --\n", i, buffer); + + rc = SQLColAttribute(hstmt, i, SQL_DESC_OCTET_LENGTH, NULL, SQL_IS_INTEGER, NULL, &number); + CHECK_STMT_RESULT(rc, "SQLColAttribute failed", hstmt); + printf("SQL_DESC_OCTET_LENGTH: %d\n", (int) number); + } + + /* Clean up */ + test_disconnect(); +} + +int main(int argc, char **argv) +{ + + /* + * The output of these tests depend on the UnknownSizes and + * MaxVarcharSize parameters + */ + runtest("UnknownSizes=-1;MaxVarcharSize=100"); + runtest("UnknownSizes=0;MaxVarcharSize=100"); + runtest("UnknownSizes=1;MaxVarcharSize=100"); + runtest("UnknownSizes=2;MaxVarcharSize=100"); + runtest("UnknownSizes=100;MaxVarcharSize=100"); + + return 0; +} diff --git a/test/tests b/test/tests index bf1a2c1..49fcd95 100644 --- a/test/tests +++ b/test/tests @@ -14,6 +14,7 @@ TESTBINS = src/connect-test \ src/commands-test \ src/multistmt-test \ src/getresult-test \ + src/colattribute-test \ src/result-conversions-test \ src/prepare-test \ src/params-test \

view thread (5+ messages)

Message-ID: <547DC668.2000106@vmware.com>
Permalink:  ../547DC668.2000106@vmware.com/
Also on:    postgresql.org/message-id/547DC668.2000106@vmware.com

 ·  · 

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-odbc@postgresql.org
  Cc: hlinnakangas@vmware.com, jfaith@tycoint.com
  Subject: Re: [BUGS] BUG #11767: ODBC driver bug when fetching constant string columns
  In-Reply-To: <547DC668.2000106@vmware.com>
  References: <55BAADA01D8C504993894EAEA7517CF80BA6C3@003FCH1MPN2-001.003f.mgd2.msft.net> <547DC668.2000106@vmware.com>

* 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