Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Xvo2p-0003AD-6z for pgsql-odbc@arkaria.postgresql.org; Tue, 02 Dec 2014 14:03:11 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Xvo2o-000591-Nv for pgsql-odbc@arkaria.postgresql.org; Tue, 02 Dec 2014 14:03:10 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1Xvo2o-00058v-46 for pgsql-odbc@postgresql.org; Tue, 02 Dec 2014 14:03:10 +0000 Received: from smtp-outbound-2.vmware.com ([208.91.2.13]) by magus.postgresql.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1Xvo2g-0006gT-Ge for pgsql-odbc@postgresql.org; Tue, 02 Dec 2014 14:03:07 +0000 Received: from sc9-mailhost3.vmware.com (sc9-mailhost3.vmware.com [10.113.161.73]) by smtp-outbound-2.vmware.com (Postfix) with ESMTP id 417B62813B; Tue, 2 Dec 2014 06:02:57 -0800 (PST) Received: from EX13-CAS-009.vmware.com (EX13-CAS-009.vmware.com [10.113.191.61]) by sc9-mailhost3.vmware.com (Postfix) with ESMTP id 3C47F40FCE; Tue, 2 Dec 2014 06:02:57 -0800 (PST) Received: from EX13-MBX-024.vmware.com (10.113.191.44) by EX13-MBX-005.vmware.com (10.113.191.25) with Microsoft SMTP Server (TLS) id 15.0.913.22; Tue, 2 Dec 2014 06:02:56 -0800 Received: from [192.168.1.90] (10.113.160.246) by EX13-MBX-024.vmware.com (10.113.191.44) with Microsoft SMTP Server (TLS) id 15.0.913.22; Tue, 2 Dec 2014 06:02:38 -0800 Message-ID: <547DC668.2000106@vmware.com> Date: Tue, 2 Dec 2014 16:02:16 +0200 From: Heikki Linnakangas Organization: VMware User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Icedove/31.2.0 MIME-Version: 1.0 To: "Faith, Jeremy" , "pgsql-odbc@postgresql.org" Subject: Re: Re: [BUGS] BUG #11767: ODBC driver bug when fetching constant string columns References: <55BAADA01D8C504993894EAEA7517CF80BA6C3@003FCH1MPN2-001.003f.mgd2.msft.net> In-Reply-To: <55BAADA01D8C504993894EAEA7517CF80BA6C3@003FCH1MPN2-001.003f.mgd2.msft.net> Content-Type: multipart/mixed; boundary="------------050901020808050603070704" X-Originating-IP: [10.113.160.246] X-ClientProxiedBy: EX13-CAS-013.vmware.com (10.113.191.65) To EX13-MBX-024.vmware.com (10.113.191.44) X-Pg-Spam-Score: -6.9 (------) 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 --------------050901020808050603070704 Content-Type: text/plain; charset="windows-1252"; format=flowed Content-Transfer-Encoding: 7bit On 12/02/2014 02:28 PM, Faith, Jeremy wrote: >> On Fri, Oct 24, 2014 at 9:36 AM, Michael Paquier >> wrote: >> >>> On Wed, Oct 22, 2014 at 11:22 PM, 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=4ce7636b69d371d9f511d492c68013f070e3b32a;hb=HEAD#l665 >>>> >>>> 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=4ce7636b69d371d9f511d492c68013f070e3b32a;hb=HEAD#l1275 >>>> >>>> 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 --------------050901020808050603070704 Content-Type: text/x-diff; name="fix-octet-length-for-unknown-1.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="fix-octet-length-for-unknown-1.patch" 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 +#include +#include + +#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 \ --------------050901020808050603070704 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-odbc --------------050901020808050603070704--