summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPavan Deolasee2016-12-20 05:35:08 +0000
committerPavan Deolasee2017-05-05 04:59:33 +0000
commitdf13f58003d119162d9a1450ced06048e5992b67 (patch)
treed156c087800e41fd1d7089befb2f3dbdb94108e0
parent97b8a00f0be9d6dad4a209b7979358ce4a53a3ca (diff)
Handle multi-command SQL strings correctly even when there are 'null' sql
commands. Reported by Krzysztof Nienartowicz
-rw-r--r--src/backend/parser/gram.y40
-rw-r--r--src/test/regress/input/misc.source11
-rw-r--r--src/test/regress/output/misc.source26
3 files changed, 66 insertions, 11 deletions
diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y
index 796292537f..00744a4980 100644
--- a/src/backend/parser/gram.y
+++ b/src/backend/parser/gram.y
@@ -788,17 +788,35 @@ stmtmulti: stmtmulti ';' stmt
* the ';' token, add '\0' at the corresponding offset
* to get a separated command.
*/
- last = list_tail($1->queries);
- query = palloc(@2 - $1->offset + 1);
- memcpy(query, lfirst(last), @2 - $1->offset);
- query[@2 - $1->offset] = '\0';
-
- lfirst(last) = query;
- query = scanner_get_query(@3, -1, yyscanner);
- $1->offset = @2;
- $1->parsetrees = lappend($1->parsetrees, $3);
- $1->queries = lappend($1->queries, query);
- $$ = $1;
+ if ($1 != NULL)
+ {
+ last = list_tail($1->queries);
+ query = palloc(@2 - $1->offset + 1);
+ memcpy(query, lfirst(last), @2 - $1->offset);
+ query[@2 - $1->offset] = '\0';
+ lfirst(last) = query;
+
+ query = scanner_get_query(@3, -1, yyscanner);
+ $1->offset = @2;
+ $1->parsetrees = lappend($1->parsetrees, $3);
+ $1->queries = lappend($1->queries, query);
+ $$ = $1;
+ }
+ /*
+ *
+ * If the earlier statements were all null, then we
+ * must initialise the StmtMulti structure and make
+ * singleton lists
+ */
+ else
+ {
+ StmtMulti *n = (StmtMulti *) palloc0(sizeof (StmtMulti));
+ query = scanner_get_query(@3, -1, yyscanner);
+ n->offset = @2;
+ n->parsetrees = list_make1($3);
+ n->queries = list_make1(query);
+ $$ = n;
+ }
}
else
$$ = $1;
diff --git a/src/test/regress/input/misc.source b/src/test/regress/input/misc.source
index c46223bedf..9be4a61618 100644
--- a/src/test/regress/input/misc.source
+++ b/src/test/regress/input/misc.source
@@ -276,3 +276,14 @@ drop table oldstyle_test;
--
-- rewrite rules
--
+
+--
+-- multi-statement cases
+--
+create table testtab_multi (a int, b int); create index testtab_multi_indx on testtab_multi(a); drop table testtab_multi;
+;create table testtab_multi (a int, b int); create index testtab_multi_indx on testtab_multi(a); drop table testtab_multi;
+;;select 1;;create table testtab_multi (a int, b int);;;;; create index testtab_multi_indx on testtab_multi(a);;; drop table testtab_multi;
+create table testtab_multi (a int, b int);;;;; create index testtab_multi_indx on testtab_multi(a);;insert into testtab_multi values (1, 2), (3,4); select * from testtab_multi order by a;drop table testtab_multi;
+;create table testtab_multi (a int, b int);;;;; create index testtab_multi_indx on testtab_multi(a);;insert into testtab_multi values (1, 2), (3,4); select * from testtab_multi_nonexist order by a;drop table testtab_multi;
+;;create table testtab_multi (a int, b int);;;;; create index testtab_multi_indx on testtab_multi(a);;insert into testtab_multi values (1, 2), (3,4); select * from testtab_multi_nonexist order by a;drop table testtab_multi;
+
diff --git a/src/test/regress/output/misc.source b/src/test/regress/output/misc.source
index 696c3645f5..fc092bbf98 100644
--- a/src/test/regress/output/misc.source
+++ b/src/test/regress/output/misc.source
@@ -854,3 +854,29 @@ drop table oldstyle_test;
--
-- rewrite rules
--
+--
+-- multi-statement cases
+--
+create table testtab_multi (a int, b int); create index testtab_multi_indx on testtab_multi(a); drop table testtab_multi;
+;create table testtab_multi (a int, b int); create index testtab_multi_indx on testtab_multi(a); drop table testtab_multi;
+;;select 1;;create table testtab_multi (a int, b int);;;;; create index testtab_multi_indx on testtab_multi(a);;; drop table testtab_multi;
+ ?column?
+----------
+ 1
+(1 row)
+
+create table testtab_multi (a int, b int);;;;; create index testtab_multi_indx on testtab_multi(a);;insert into testtab_multi values (1, 2), (3,4); select * from testtab_multi order by a;drop table testtab_multi;
+ a | b
+---+---
+ 1 | 2
+ 3 | 4
+(2 rows)
+
+;create table testtab_multi (a int, b int);;;;; create index testtab_multi_indx on testtab_multi(a);;insert into testtab_multi values (1, 2), (3,4); select * from testtab_multi_nonexist order by a;drop table testtab_multi;
+ERROR: relation "testtab_multi_nonexist" does not exist
+LINE 1: select * from testtab_multi_nonexist order by a;
+ ^
+;;create table testtab_multi (a int, b int);;;;; create index testtab_multi_indx on testtab_multi(a);;insert into testtab_multi values (1, 2), (3,4); select * from testtab_multi_nonexist order by a;drop table testtab_multi;
+ERROR: relation "testtab_multi_nonexist" does not exist
+LINE 1: select * from testtab_multi_nonexist order by a;
+ ^