Skip to content

Commit

Permalink
sql/parser: unreserve INDEX and NOTHING from the RHS of SET statements
Browse files Browse the repository at this point in the history
The SET statement in the pg dialect is special because it
auto-converts identifiers on its RHS to symbolic values or strings. In
particular it is meant to support a diversity of special keywords as
pseudo-values.

This patch ensures that INDEX and NOTHING are accepted on the RHS.

Release note (sql change): the names "index" and "nothing" are again
accepted in the right-hand-side of the assignment in SET statements,
for compatibility with PostgreSQL.
  • Loading branch information
knz committed Oct 23, 2018
1 parent ebad853 commit b18eeb1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
6 changes: 5 additions & 1 deletion docs/generated/sql/bnf/stmt_block.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -1052,7 +1052,7 @@ to_or_eq ::=

var_value ::=
a_expr
| 'ON'
| extra_var_value

opt_on_targets_roles ::=
'ON' targets_roles
Expand Down Expand Up @@ -1421,6 +1421,10 @@ offset_clause ::=
generic_set ::=
var_name to_or_eq var_list

extra_var_value ::=
'ON'
| cockroachdb_extra_reserved_keyword

targets_roles ::=
'ROLE' name_list
| targets
Expand Down
3 changes: 3 additions & 0 deletions pkg/sql/parser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1832,6 +1832,9 @@ func TestParse2(t *testing.T) {
{`ALTER TABLE a ALTER b TYPE INT`, `ALTER TABLE a ALTER COLUMN b SET DATA TYPE INT`},
{`EXPLAIN ANALYZE SELECT 1`, `EXPLAIN ANALYZE (DISTSQL) SELECT 1`},

{`SET a = INDEX`, `SET a = "index"`},
{`SET a = NOTHING`, `SET a = "nothing"`},

// Regression for #31589
{`CREATE TABLE FAMILY (x INT)`,
`CREATE TABLE "family" (x INT)`},
Expand Down
19 changes: 17 additions & 2 deletions pkg/sql/parser/sql.y
Original file line number Diff line number Diff line change
Expand Up @@ -921,7 +921,7 @@ func newNameFromStr(s string) *tree.Name {
%type <tree.Expr> string_or_placeholder_list

%type <str> unreserved_keyword type_func_name_keyword cockroachdb_extra_type_func_name_keyword
%type <str> col_name_keyword reserved_keyword cockroachdb_extra_reserved_keyword
%type <str> col_name_keyword reserved_keyword cockroachdb_extra_reserved_keyword extra_var_value

%type <tree.ConstraintTableDef> table_constraint constraint_elem
%type <tree.TableDef> index_def
Expand Down Expand Up @@ -2823,11 +2823,26 @@ attrs:

var_value:
a_expr
| ON
| extra_var_value
{
$$.val = tree.Expr(&tree.UnresolvedName{NumParts: 1, Parts: tree.NameParts{$1}})
}

// The RHS of a SET statement can contain any valid expression, which
// themselves can contain identifiers like TRUE, FALSE. These are parsed
// as column names (via a_expr) and later during semantic analysis
// assigned their special value.
//
// In addition, for compatibility with CockroachDB we need to support
// the reserved keyword ON (to go along OFF, which is a valid column name).
//
// Finally, in PostgreSQL the CockroachDB-reserved words "index",
// "nothing", etc. are not special and are valid in SET. These need to
// be allowed here too.
extra_var_value:
ON
| cockroachdb_extra_reserved_keyword

var_list:
var_value
{
Expand Down

0 comments on commit b18eeb1

Please sign in to comment.