diff options
author | Michael Paquier | 2012-10-15 00:14:58 +0000 |
---|---|---|
committer | Michael Paquier | 2012-10-15 00:14:58 +0000 |
commit | 945f83fc5d5b2ef1d51438a7dd48dd9532c9d936 (patch) | |
tree | 5e932ba3c4d0e4dd2e90ba4764e2d919de9d8061 | |
parent | d165a5c2458e97cbf943135cf1bb534b8f8af807 (diff) |
Fix for COPY query generation with quoted column names
Report and patch by Nikhil Sontakke, completed with esthetic changes.
-rw-r--r-- | src/backend/pgxc/copy/remotecopy.c | 56 | ||||
-rw-r--r-- | src/test/regress/input/xc_copy.source | 18 | ||||
-rw-r--r-- | src/test/regress/output/xc_copy.source | 16 |
3 files changed, 34 insertions, 56 deletions
diff --git a/src/backend/pgxc/copy/remotecopy.c b/src/backend/pgxc/copy/remotecopy.c index 23db6ecdd7..5ac20dc60a 100644 --- a/src/backend/pgxc/copy/remotecopy.c +++ b/src/backend/pgxc/copy/remotecopy.c @@ -25,7 +25,6 @@ #include "utils/rel.h" static void RemoteCopy_QuoteStr(StringInfo query_buf, char *value); -static void RemoteCopy_QuoteIdentifier(StringInfo query_buf, char *value); /* * RemoteCopy_GetRelationLoc @@ -132,7 +131,8 @@ RemoteCopy_BuildStatement(RemoteCopyData *state, { if (prev) appendStringInfoString(&state->query_buf, ", "); - RemoteCopy_QuoteIdentifier(&state->query_buf, strVal(lfirst(cell))); + appendStringInfoString(&state->query_buf, + quote_identifier(strVal(lfirst(cell)))); prev = cell; } @@ -156,8 +156,8 @@ RemoteCopy_BuildStatement(RemoteCopyData *state, !pgxc_is_expr_shippable(expression_planner(defexpr), NULL)) { appendStringInfoString(&state->query_buf, ", "); - RemoteCopy_QuoteIdentifier(&state->query_buf, - NameStr(tupDesc->attrs[attnum - 1]->attname)); + appendStringInfoString(&state->query_buf, + quote_identifier(NameStr(tupDesc->attrs[attnum - 1]->attname))); } } } @@ -225,7 +225,8 @@ RemoteCopy_BuildStatement(RemoteCopyData *state, { if (prev) appendStringInfoString(&state->query_buf, ", "); - RemoteCopy_QuoteIdentifier(&state->query_buf, strVal(lfirst(cell))); + appendStringInfoString(&state->query_buf, + quote_identifier(strVal(lfirst(cell)))); prev = cell; } } @@ -239,7 +240,8 @@ RemoteCopy_BuildStatement(RemoteCopyData *state, { if (prev) appendStringInfoString(&state->query_buf, ", "); - RemoteCopy_QuoteIdentifier(&state->query_buf, strVal(lfirst(cell))); + appendStringInfoString(&state->query_buf, + quote_identifier(strVal(lfirst(cell)))); prev = cell; } } @@ -362,45 +364,3 @@ RemoteCopy_QuoteStr(StringInfo query_buf, char *value) APPENDSOFAR(query_buf, start, current); appendStringInfoChar(query_buf, '\''); } - -/* - * RemoteCopy_QuoteIdentifier - * Determine if identifier needs to be quoted and surround it with double quotes - */ -static void -RemoteCopy_QuoteIdentifier(StringInfo query_buf, char *value) -{ - char *start = value; - char *current = value; - char c; - int len = strlen(value); - bool need_quote = (strspn(value, "_abcdefghijklmnopqrstuvwxyz0123456789") < len) || - (strchr("_abcdefghijklmnopqrstuvwxyz", value[0]) == NULL); - - if (need_quote) - { - appendStringInfoChar(query_buf, '"'); - - while ((c = *current) != '\0') - { - switch (c) - { - case '"': - APPENDSOFAR(query_buf, start, current); - /* Double current */ - appendStringInfoChar(query_buf, c); - /* Second current will be appended next time */ - start = current; - /* fallthru */ - default: - current++; - } - } - APPENDSOFAR(query_buf, start, current); - appendStringInfoChar(query_buf, '"'); - } - else - { - appendBinaryStringInfo(query_buf, value, len); - } -} diff --git a/src/test/regress/input/xc_copy.source b/src/test/regress/input/xc_copy.source index cf08efe74d..2f238420ac 100644 --- a/src/test/regress/input/xc_copy.source +++ b/src/test/regress/input/xc_copy.source @@ -100,7 +100,7 @@ INSERT INTO xc_copy_1 VALUES (1,23),(34,5),(9,11); COPY xc_copy_1 TO STDOUT; DROP TABLE xc_copy_1; --- Quoted table +-- Quoted table name -- check for correct remote query generation CREATE TABLE "Xc_copy_2" (a int, b int); COPY "Xc_copy_2" FROM STDIN DELIMITER ','; @@ -110,7 +110,17 @@ COPY "Xc_copy_2" FROM STDIN DELIMITER ','; COPY "Xc_copy_2" TO STDOUT; DROP TABLE "Xc_copy_2"; --- Table with no locator data -CREATE TABLE xc_copy_3 (c1 int) DISTRIBUTE BY HASH(c1); -COPY (SELECT pclocatortype,pcattnum,pchashalgorithm,pchashbuckets FROM pgxc_class WHERE pgxc_class.pcrelid = 'xc_copy_3'::regclass) TO stdout; +-- Quoted column name +-- check for correct remote query generation +CREATE TABLE xc_copy_3(a int, "user" int); +COPY xc_copy_3 (a, "user") FROM STDIN (DELIMITER ','); +1,2 +3,4 +\. +COPY xc_copy_3 TO STDOUT; DROP TABLE xc_copy_3; + +-- Table with no locator data +CREATE TABLE xc_copy_4 (c1 int) DISTRIBUTE BY HASH(c1); +COPY (SELECT pclocatortype,pcattnum,pchashalgorithm,pchashbuckets FROM pgxc_class WHERE pgxc_class.pcrelid = 'xc_copy_4'::regclass) TO stdout; +DROP TABLE xc_copy_4; diff --git a/src/test/regress/output/xc_copy.source b/src/test/regress/output/xc_copy.source index 1defee54d2..1805516178 100644 --- a/src/test/regress/output/xc_copy.source +++ b/src/test/regress/output/xc_copy.source @@ -100,7 +100,7 @@ COPY xc_copy_1 TO STDOUT; 34 5 9 11 DROP TABLE xc_copy_1; --- Quoted table +-- Quoted table name -- check for correct remote query generation CREATE TABLE "Xc_copy_2" (a int, b int); COPY "Xc_copy_2" FROM STDIN DELIMITER ','; @@ -108,8 +108,16 @@ COPY "Xc_copy_2" TO STDOUT; 1 2 3 4 DROP TABLE "Xc_copy_2"; +-- Quoted column name +-- check for correct remote query generation +CREATE TABLE xc_copy_3(a int, "user" int); +COPY xc_copy_3 (a, "user") FROM STDIN (DELIMITER ','); +COPY xc_copy_3 TO STDOUT; +1 2 +3 4 +DROP TABLE xc_copy_3; -- Table with no locator data -CREATE TABLE xc_copy_3 (c1 int) DISTRIBUTE BY HASH(c1); -COPY (SELECT pclocatortype,pcattnum,pchashalgorithm,pchashbuckets FROM pgxc_class WHERE pgxc_class.pcrelid = 'xc_copy_3'::regclass) TO stdout; +CREATE TABLE xc_copy_4 (c1 int) DISTRIBUTE BY HASH(c1); +COPY (SELECT pclocatortype,pcattnum,pchashalgorithm,pchashbuckets FROM pgxc_class WHERE pgxc_class.pcrelid = 'xc_copy_4'::regclass) TO stdout; H 1 1 4096 -DROP TABLE xc_copy_3; +DROP TABLE xc_copy_4; |