diff --git a/presto-docs/src/main/sphinx/language/reserved.rst b/presto-docs/src/main/sphinx/language/reserved.rst index 694ca537aa17c..14b769a996f6f 100644 --- a/presto-docs/src/main/sphinx/language/reserved.rst +++ b/presto-docs/src/main/sphinx/language/reserved.rst @@ -63,7 +63,6 @@ Keyword SQL:2016 SQL-92 ``ORDER`` reserved reserved ``OUTER`` reserved reserved ``PREPARE`` reserved reserved -``PRIMARY`` reserved reserved ``RECURSIVE`` reserved ``RIGHT`` reserved reserved ``ROLLUP`` reserved diff --git a/presto-parser/src/main/antlr4/com/facebook/presto/sql/parser/SqlBase.g4 b/presto-parser/src/main/antlr4/com/facebook/presto/sql/parser/SqlBase.g4 index 8ec51bd9117e1..6dd461c03538f 100644 --- a/presto-parser/src/main/antlr4/com/facebook/presto/sql/parser/SqlBase.g4 +++ b/presto-parser/src/main/antlr4/com/facebook/presto/sql/parser/SqlBase.g4 @@ -577,15 +577,7 @@ number ; constraintSpecification - : namedConstraintSpecification | unnamedConstraintSpecification - ; - -namedConstraintSpecification - : CONSTRAINT name=identifier unnamedConstraintSpecification - ; - -unnamedConstraintSpecification - : constraintType columnAliases constraintEnabled? constraintRely? constraintEnforced? + : (CONSTRAINT name=identifier)? constraintType columnAliases constraintEnabled? constraintRely? constraintEnforced? ; constraintType @@ -622,7 +614,7 @@ nonReserved | MAP | MATERIALIZED | MINUTE | MONTH | NAME | NFC | NFD | NFKC | NFKD | NO | NONE | NULLIF | NULLS | OF | OFFSET | ONLY | OPTION | ORDINALITY | OUTPUT | OVER - | PARTITION | PARTITIONS | POSITION | PRECEDING | PRIVILEGES | PROPERTIES + | PARTITION | PARTITIONS | POSITION | PRECEDING | PRIMARY | PRIVILEGES | PROPERTIES | RANGE | READ | REFRESH | RELY | RENAME | REPEATABLE | REPLACE | RESET | RESPECT | RESTRICT | RETURN | RETURNS | REVOKE | ROLE | ROLES | ROLLBACK | ROW | ROWS | SCHEMA | SCHEMAS | SECOND | SECURITY | SERIALIZABLE | SESSION | SET | SETS | SQL | SHOW | SOME | START | STATS | SUBSTRING | SYSTEM | SYSTEM_TIME | SYSTEM_VERSION diff --git a/presto-parser/src/main/java/com/facebook/presto/sql/parser/AstBuilder.java b/presto-parser/src/main/java/com/facebook/presto/sql/parser/AstBuilder.java index 357de56d940da..b03879f38e45f 100644 --- a/presto-parser/src/main/java/com/facebook/presto/sql/parser/AstBuilder.java +++ b/presto-parser/src/main/java/com/facebook/presto/sql/parser/AstBuilder.java @@ -213,6 +213,7 @@ import static com.google.common.collect.ImmutableList.toImmutableList; import static com.google.common.collect.Iterables.getOnlyElement; import static java.lang.String.format; +import static java.util.Locale.ENGLISH; import static java.util.Objects.requireNonNull; import static java.util.stream.Collectors.toList; @@ -519,27 +520,6 @@ public Node visitAddConstraint(SqlBaseParser.AddConstraintContext context) @Override public Node visitConstraintSpecification(SqlBaseParser.ConstraintSpecificationContext context) - { - return context.namedConstraintSpecification() != null ? - (ConstraintSpecification) visit(context.namedConstraintSpecification()) : - (ConstraintSpecification) visit(context.unnamedConstraintSpecification()); - } - - @Override - public Node visitNamedConstraintSpecification(SqlBaseParser.NamedConstraintSpecificationContext context) - { - ConstraintSpecification unnamedConstraint = (ConstraintSpecification) visit(context.unnamedConstraintSpecification()); - return new ConstraintSpecification(getLocation(context), - Optional.of(visit(context.name).toString()), - unnamedConstraint.getColumns(), - unnamedConstraint.getConstraintType(), - unnamedConstraint.isEnabled(), - unnamedConstraint.isRely(), - unnamedConstraint.isEnforced()); - } - - @Override - public Node visitUnnamedConstraintSpecification(SqlBaseParser.UnnamedConstraintSpecificationContext context) { List columnAliases = visit(context.columnAliases().identifier(), Identifier.class); @@ -547,6 +527,17 @@ public Node visitUnnamedConstraintSpecification(SqlBaseParser.UnnamedConstraintS boolean rely = context.constraintRely() == null || context.constraintRely().NOT() == null; boolean enforced = context.constraintEnforced() == null || context.constraintEnforced().NOT() == null; + // Named constraint + if (context.CONSTRAINT() != null) { + return new ConstraintSpecification(getLocation(context), + Optional.of(visit(context.name).toString()), + columnAliases.stream().map(Identifier::toString).collect(toImmutableList()), + getConstraintType((Token) context.constraintType().getChild(0).getPayload()), + enabled, + rely, + enforced); + } + // Unnammed constraint return new ConstraintSpecification(getLocation(context), Optional.empty(), columnAliases.stream().map(Identifier::toString).collect(toImmutableList()), diff --git a/presto-parser/src/test/java/com/facebook/presto/sql/parser/TestStatementBuilder.java b/presto-parser/src/test/java/com/facebook/presto/sql/parser/TestStatementBuilder.java index ce20bfde6ffce..f7afa0f821f32 100644 --- a/presto-parser/src/test/java/com/facebook/presto/sql/parser/TestStatementBuilder.java +++ b/presto-parser/src/test/java/com/facebook/presto/sql/parser/TestStatementBuilder.java @@ -277,6 +277,8 @@ public void testStatementBuilder() printStatement("create table t1 (c1 int, c2 varchar, c3 double, c4 int, constraint pk1 primary key (c1, c2))"); printStatement("create table t1 (c1 int, c2 varchar, c3 double, c4 int, constraint pk1 primary key (c1, c2), constraint uq1 unique (c4), unique (c3))"); printStatement("create table t1 (c1 int, c2 varchar, c3 double, c4 int, constraint pk1 primary key (c1, c2) disabled not rely enforced , constraint uq1 unique (c4) not rely enforced, unique (c3) disabled)"); + // primary is not a keyword and may be used as a column name + printStatement("select primary, secondary from foo a (primary, secondary)"); } @Test