pg.ddx.io pgsql-odbc@postgresql.org mailing list archivehelp / color / mirror / Atom feed
BUG #11767: ODBC driver bug when fetching constant string columns 5+ messages / 5 participants [nested] [flat]
* BUG #11767: ODBC driver bug when fetching constant string columns @ 2014-10-22 21:22 keyurgovande@gmail.com 0 siblings, 1 reply; 5+ messages in thread From: keyurgovande@gmail.com @ 2014-10-22 21:22 UTC (permalink / raw) To: pgsql-bugs@postgresql.org The following bug has been logged on the website: Bug reference: 11767 Logged by: Keyur Govande Email address: keyurgovande@gmail.com PostgreSQL version: 9.0.3 Operating system: Centos 6.5 Description: Hello, This was reported to the PHP project (https://bugs.php.net/bug.php?id=68087) but it seems like a Postgres ODBC driver bug. 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. 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. A temporary workaround is to use a cast thusly: SELECT id, varchar_col, date_col, varchar 'random string' as random; -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs ^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: [BUGS] BUG #11767: ODBC driver bug when fetching constant string columns @ 2014-10-24 13:36 Michael Paquier <michael.paquier@gmail.com> parent: keyurgovande@gmail.com 0 siblings, 1 reply; 5+ messages in thread From: Michael Paquier @ 2014-10-24 13:36 UTC (permalink / raw) To: keyurgovande@gmail.com; +Cc: pgsql-odbc On Wed, Oct 22, 2014 at 11:22 PM, <keyurgovande@gmail.com> wrote: > The following bug has been logged on the website: > > Bug reference: 11767 > Logged by: Keyur Govande > Email address: keyurgovande@gmail.com > PostgreSQL version: 9.0.3 You should update to 9.0.18, you are 3.5 years of bug fixes. > 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. > > 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. > > A temporary workaround is to use a cast thusly: SELECT id, varchar_col, > date_col, varchar 'random string' as random; > This was reported to the PHP project (https://bugs.php.net/bug.php?id=68087) > but it seems like a Postgres ODBC driver bug. pgsql-odbc is more adapted for a bug report on the ODBC driver. I am moving the thread there. -- Michael -- Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-odbc ^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: [BUGS] BUG #11767: ODBC driver bug when fetching constant string columns @ 2014-10-24 16:10 Keyur Govande <keyurgovande@gmail.com> parent: Michael Paquier <michael.paquier@gmail.com> 0 siblings, 0 replies; 5+ messages in thread From: Keyur Govande @ 2014-10-24 16:10 UTC (permalink / raw) To: Michael Paquier <michael.paquier@gmail.com>; +Cc: pgsql-odbc On Fri, Oct 24, 2014 at 9:36 AM, Michael Paquier <michael.paquier@gmail.com> wrote: > On Wed, Oct 22, 2014 at 11:22 PM, <keyurgovande@gmail.com> wrote: > > The following bug has been logged on the website: > > > > Bug reference: 11767 > > Logged by: Keyur Govande > > Email address: keyurgovande@gmail.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. > > > 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. > > > > 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. > > > > A temporary workaround is to use a cast thusly: SELECT id, varchar_col, > > date_col, varchar 'random string' as random; > > This was reported to the PHP project ( > https://bugs.php.net/bug.php?id=68087) > > but it seems like a Postgres ODBC driver bug. > pgsql-odbc is more adapted for a bug report on the ODBC driver. I am > moving the thread there. > -- > Michael > ^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: [BUGS] BUG #11767: ODBC driver bug when fetching constant string columns @ 2014-12-02 12:28 Faith, Jeremy <jfaith@tycoint.com> 0 siblings, 1 reply; 5+ messages in thread From: Faith, Jeremy @ 2014-12-02 12:28 UTC (permalink / raw) To: pgsql-odbc > 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. > > > > > > 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. > > > > > > 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. > > > > > > A temporary workaround is to use a cast thusly: SELECT id, varchar_col, > > > date_col, varchar 'random string' as random; > > > This was reported to the PHP project ( > > https://bugs.php.net/bug.php?id=68087) > > > but it seems like a Postgres ODBC driver bug. > > pgsql-odbc is more adapted for a bug report on the ODBC driver. I am > > moving the thread there. > > -- > > Michael > > Jeremy Faith Tyco Safety Products/CEM Systems Ltd. ________________________________ This e-mail contains privileged and confidential information intended for the use of the addressees named above. If you are not the intended recipient of this e-mail, you are hereby notified that you must not disseminate, copy or take any action in respect of any information contained in it. If you have received this e-mail in error, please notify the sender immediately by e-mail and immediately destroy this e-mail and its attachments. -- Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-odbc ^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: Re: [BUGS] BUG #11767: ODBC driver bug when fetching constant string columns @ 2014-12-02 14:02 Heikki Linnakangas <hlinnakangas@vmware.com> parent: Faith, Jeremy <jfaith@tycoint.com> 0 siblings, 0 replies; 5+ messages in thread From: Heikki Linnakangas @ 2014-12-02 14:02 UTC (permalink / raw) To: Faith, Jeremy <jfaith@tycoint.com>; pgsql-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:^ permalink raw reply [nested|flat] 5+ messages in threadfix-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 \
end of thread, other threads:[~2014-12-02 14:02 UTC | newest] Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed) -- links below jump to the message on this page -- 2014-10-22 21:22 BUG #11767: ODBC driver bug when fetching constant string columns keyurgovande@gmail.com 2014-10-24 13:36 ` Michael Paquier <michael.paquier@gmail.com> 2014-10-24 16:10 ` Keyur Govande <keyurgovande@gmail.com> 2014-12-02 12:28 Re: [BUGS] BUG #11767: ODBC driver bug when fetching constant string columns Faith, Jeremy <jfaith@tycoint.com> 2014-12-02 14:02 ` Heikki Linnakangas <hlinnakangas@vmware.com>
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