Skip to content

Commit

Permalink
copy: fix copy grammar to match PG
Browse files Browse the repository at this point in the history
Previously COPY would allow a wide range of syntax in the COPY
TO substatement.  Now like PG we limit it to a few things.

PG grammar is:
```
PreparableStmt:
			SelectStmt
			| InsertStmt
			| UpdateStmt
			| DeleteStmt
			| MergeStmt
```

And now we do something similar. This prevents the wheels from coming
off when RSG generates EXPLAIN's in the substatement for instance.

Release note: none
Epic: none
  • Loading branch information
cucaroach committed Mar 20, 2023
1 parent 110d430 commit 5a19c66
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 8 deletions.
1 change: 1 addition & 0 deletions docs/generated/sql/bnf/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ FILES = [
"comment",
"commit_transaction",
"copy_stmt",
"copy_to_stmt",
"create_as_col_qual_list",
"create_as_constraint_def",
"create_changefeed_stmt",
Expand Down
10 changes: 5 additions & 5 deletions docs/generated/sql/bnf/copy_stmt.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ copy_stmt ::=
| 'COPY' table_name opt_column_list 'TO' 'STDOUT' 'WITH' '(' copy_generic_options_list ')'
| 'COPY' table_name opt_column_list 'TO' 'STDOUT' '(' copy_generic_options_list ')'
| 'COPY' table_name opt_column_list 'TO' 'STDOUT'
| 'COPY' '(' preparable_stmt ')' 'TO' 'STDOUT' 'WITH' copy_options ( ( copy_options ) )*
| 'COPY' '(' preparable_stmt ')' 'TO' 'STDOUT' copy_options ( ( copy_options ) )*
| 'COPY' '(' preparable_stmt ')' 'TO' 'STDOUT' 'WITH' '(' copy_generic_options_list ')'
| 'COPY' '(' preparable_stmt ')' 'TO' 'STDOUT' '(' copy_generic_options_list ')'
| 'COPY' '(' preparable_stmt ')' 'TO' 'STDOUT'
| 'COPY' '(' copy_to_stmt ')' 'TO' 'STDOUT' 'WITH' copy_options ( ( copy_options ) )*
| 'COPY' '(' copy_to_stmt ')' 'TO' 'STDOUT' copy_options ( ( copy_options ) )*
| 'COPY' '(' copy_to_stmt ')' 'TO' 'STDOUT' 'WITH' '(' copy_generic_options_list ')'
| 'COPY' '(' copy_to_stmt ')' 'TO' 'STDOUT' '(' copy_generic_options_list ')'
| 'COPY' '(' copy_to_stmt ')' 'TO' 'STDOUT'
6 changes: 6 additions & 0 deletions docs/generated/sql/bnf/copy_to_stmt.bnf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
copy_to_stmt ::=
delete_stmt
| insert_stmt
| select_stmt
| update_stmt
| upsert_stmt
9 changes: 8 additions & 1 deletion docs/generated/sql/bnf/stmt_block.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ analyze_stmt ::=
copy_stmt ::=
'COPY' table_name opt_column_list 'FROM' 'STDIN' opt_with_copy_options opt_where_clause
| 'COPY' table_name opt_column_list 'TO' 'STDOUT' opt_with_copy_options
| 'COPY' '(' preparable_stmt ')' 'TO' 'STDOUT' opt_with_copy_options
| 'COPY' '(' copy_to_stmt ')' 'TO' 'STDOUT' opt_with_copy_options

comment_stmt ::=
'COMMENT' 'ON' 'DATABASE' database_name 'IS' comment_text
Expand Down Expand Up @@ -332,6 +332,13 @@ opt_where_clause ::=
where_clause
|

copy_to_stmt ::=
delete_stmt
| insert_stmt
| select_stmt
| update_stmt
| upsert_stmt

database_name ::=
name

Expand Down
1 change: 1 addition & 0 deletions pkg/gen/bnf.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ BNF_SRCS = [
"//docs/generated/sql/bnf:comment.bnf",
"//docs/generated/sql/bnf:commit_transaction.bnf",
"//docs/generated/sql/bnf:copy_stmt.bnf",
"//docs/generated/sql/bnf:copy_to_stmt.bnf",
"//docs/generated/sql/bnf:create_as_col_qual_list.bnf",
"//docs/generated/sql/bnf:create_as_constraint_def.bnf",
"//docs/generated/sql/bnf:create_changefeed_stmt.bnf",
Expand Down
1 change: 1 addition & 0 deletions pkg/gen/diagrams.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ DIAGRAMS_SRCS = [
"//docs/generated/sql/bnf:comment.html",
"//docs/generated/sql/bnf:commit_transaction.html",
"//docs/generated/sql/bnf:copy.html",
"//docs/generated/sql/bnf:copy_to.html",
"//docs/generated/sql/bnf:create.html",
"//docs/generated/sql/bnf:create_as_col_qual_list.html",
"//docs/generated/sql/bnf:create_as_constraint_def.html",
Expand Down
1 change: 1 addition & 0 deletions pkg/gen/docs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ DOCS_SRCS = [
"//docs/generated/sql/bnf:comment.bnf",
"//docs/generated/sql/bnf:commit_transaction.bnf",
"//docs/generated/sql/bnf:copy_stmt.bnf",
"//docs/generated/sql/bnf:copy_to_stmt.bnf",
"//docs/generated/sql/bnf:create_as_col_qual_list.bnf",
"//docs/generated/sql/bnf:create_as_constraint_def.bnf",
"//docs/generated/sql/bnf:create_changefeed_stmt.bnf",
Expand Down
15 changes: 13 additions & 2 deletions pkg/sql/parser/sql.y
Original file line number Diff line number Diff line change
Expand Up @@ -1207,6 +1207,7 @@ func (u *sqlSymUnion) showCreateFormatOption() tree.ShowCreateFormatOption {
%type <tree.Statement> preparable_stmt
%type <tree.Statement> explainable_stmt
%type <tree.Statement> row_source_extension_stmt
%type <tree.Statement> copy_to_stmt
%type <tree.Statement> export_stmt
%type <tree.Statement> execute_stmt
%type <tree.Statement> deallocate_stmt
Expand Down Expand Up @@ -3977,15 +3978,15 @@ copy_stmt:
{
return unimplementedWithIssue(sqllex, 97181)
}
| COPY '(' preparable_stmt ')' TO STDOUT opt_with_copy_options
| COPY '(' copy_to_stmt ')' TO STDOUT opt_with_copy_options
{
/* FORCE DOC */
$$.val = &tree.CopyTo{
Statement: $3.stmt(),
Options: *$7.copyOptions(),
}
}
| COPY '(' preparable_stmt ')' TO error
| COPY '(' copy_to_stmt ')' TO error
{
return unimplementedWithIssue(sqllex, 96590)
}
Expand Down Expand Up @@ -5680,6 +5681,16 @@ row_source_extension_stmt:
| update_stmt // EXTEND WITH HELP: UPDATE
| upsert_stmt // EXTEND WITH HELP: UPSERT

copy_to_stmt:
delete_stmt // EXTEND WITH HELP: DELETE
| insert_stmt // EXTEND WITH HELP: INSERT
| select_stmt // help texts in sub-rule
{
$$.val = $1.slct()
}
| update_stmt // EXTEND WITH HELP: UPDATE
| upsert_stmt // EXTEND WITH HELP: UPSERT

explain_option_list:
explain_option_name
{
Expand Down
8 changes: 8 additions & 0 deletions pkg/sql/parser/testdata/copy
Original file line number Diff line number Diff line change
Expand Up @@ -380,3 +380,11 @@ COPY (SELECT * FROM t) TO STDOUT (HEADER, OIDS)
^
HINT: You have attempted to use a feature that is not yet implemented.
See: https://go.crdb.dev/issue-v/41608/

error
COPY (EXPLAIN SELECT * FROM t) TO STDOUT
----
at or near "explain": syntax error
DETAIL: source SQL:
COPY (EXPLAIN SELECT * FROM t) TO STDOUT
^

0 comments on commit 5a19c66

Please sign in to comment.