This is an automated email from the ASF dual-hosted git repository.
jhyde pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/calcite.git
The following commit(s) were added to refs/heads/main by this push:
new ae7c378716 [CALCITE-5393] Allow VALUE as a synonym for VALUES (enabled
in MySQL conformance)
ae7c378716 is described below
commit ae7c37871659c7a151b02c88da68716cc6858b18
Author: duanzhengqiang <[email protected]>
AuthorDate: Mon Nov 28 20:11:41 2022 +0800
[CALCITE-5393] Allow VALUE as a synonym for VALUES (enabled in MySQL
conformance)
Adjust LeafQueryOrExpr syntax order to ensure that LeafQuery
is processed first. (Seems to be necessary in the Babel
parser only.)
Add isValueAllowed() in BABEL, LENIENT and MYSQL_5 conformance.
Close apache/calcite#2985
---
core/src/main/codegen/templates/Parser.jj | 16 +++++++++++++---
.../org/apache/calcite/runtime/CalciteResource.java | 3 +++
.../calcite/sql/validate/SqlAbstractConformance.java | 4 ++++
.../apache/calcite/sql/validate/SqlConformance.java | 12 ++++++++++++
.../calcite/sql/validate/SqlConformanceEnum.java | 11 +++++++++++
.../sql/validate/SqlDelegatingConformance.java | 4 ++++
.../apache/calcite/runtime/CalciteResource.properties | 1 +
site/_docs/reference.md | 6 +++++-
.../org/apache/calcite/sql/parser/SqlParserTest.java | 19 ++++++++++++++++++-
9 files changed, 71 insertions(+), 5 deletions(-)
diff --git a/core/src/main/codegen/templates/Parser.jj
b/core/src/main/codegen/templates/Parser.jj
index 206f7929b2..09eabf64d3 100644
--- a/core/src/main/codegen/templates/Parser.jj
+++ b/core/src/main/codegen/templates/Parser.jj
@@ -2444,7 +2444,17 @@ SqlNode TableConstructor() :
final Span s;
}
{
- <VALUES> { s = span(); }
+ (
+ <VALUES> { s = span(); }
+ |
+ <VALUE>
+ {
+ s = span();
+ if (!this.conformance.isValueAllowed()) {
+ throw SqlUtil.newContextException(getPos(),
RESOURCE.valueNotAllowed());
+ }
+ }
+ )
AddRowConstructor(list)
(
LOOKAHEAD(2)
@@ -3492,9 +3502,9 @@ SqlNode LeafQueryOrExpr(ExprContext exprContext) :
SqlNode e;
}
{
- e = Expression(exprContext) { return e; }
-|
e = LeafQuery(exprContext) { return e; }
+|
+ e = Expression(exprContext) { return e; }
}
/** As {@link #Expression} but appends to a list. */
diff --git a/core/src/main/java/org/apache/calcite/runtime/CalciteResource.java
b/core/src/main/java/org/apache/calcite/runtime/CalciteResource.java
index 47150f5503..3a6b5ffbb5 100644
--- a/core/src/main/java/org/apache/calcite/runtime/CalciteResource.java
+++ b/core/src/main/java/org/apache/calcite/runtime/CalciteResource.java
@@ -48,6 +48,9 @@ public interface CalciteResource {
@BaseMessage("APPLY operator is not allowed under the current SQL
conformance level")
ExInst<CalciteException> applyNotAllowed();
+ @BaseMessage("VALUE is not allowed under the current SQL conformance level")
+ ExInst<CalciteException> valueNotAllowed();
+
@BaseMessage("Illegal {0} literal ''{1}'': {2}")
ExInst<CalciteException> illegalLiteral(String a0, String a1, String a2);
diff --git
a/core/src/main/java/org/apache/calcite/sql/validate/SqlAbstractConformance.java
b/core/src/main/java/org/apache/calcite/sql/validate/SqlAbstractConformance.java
index 5fd1855a1f..c4e456dedf 100644
---
a/core/src/main/java/org/apache/calcite/sql/validate/SqlAbstractConformance.java
+++
b/core/src/main/java/org/apache/calcite/sql/validate/SqlAbstractConformance.java
@@ -133,6 +133,10 @@ public abstract class SqlAbstractConformance implements
SqlConformance {
return SqlConformanceEnum.DEFAULT.allowAliasUnnestItems();
}
+ @Override public boolean isValueAllowed() {
+ return SqlConformanceEnum.DEFAULT.isValueAllowed();
+ }
+
@Override public SqlLibrary semantics() {
return SqlConformanceEnum.DEFAULT.semantics();
}
diff --git
a/core/src/main/java/org/apache/calcite/sql/validate/SqlConformance.java
b/core/src/main/java/org/apache/calcite/sql/validate/SqlConformance.java
index a0a7dceefa..eef63adefa 100644
--- a/core/src/main/java/org/apache/calcite/sql/validate/SqlConformance.java
+++ b/core/src/main/java/org/apache/calcite/sql/validate/SqlConformance.java
@@ -524,6 +524,18 @@ public interface SqlConformance {
*/
boolean allowQualifyingCommonColumn();
+ /**
+ * Whether {@code VALUE} is allowed as an alternative to {@code VALUES} in
+ * the parser.
+ *
+ * <p>Among the built-in conformance levels, true in
+ * {@link SqlConformanceEnum#BABEL},
+ * {@link SqlConformanceEnum#LENIENT},
+ * {@link SqlConformanceEnum#MYSQL_5};
+ * false otherwise.
+ */
+ boolean isValueAllowed();
+
/**
* Controls the behavior of operators that are part of Standard SQL but
* nevertheless have different behavior in different databases.
diff --git
a/core/src/main/java/org/apache/calcite/sql/validate/SqlConformanceEnum.java
b/core/src/main/java/org/apache/calcite/sql/validate/SqlConformanceEnum.java
index 911f9e675a..993cfcfc70 100644
--- a/core/src/main/java/org/apache/calcite/sql/validate/SqlConformanceEnum.java
+++ b/core/src/main/java/org/apache/calcite/sql/validate/SqlConformanceEnum.java
@@ -404,6 +404,17 @@ public enum SqlConformanceEnum implements SqlConformance {
}
}
+ @Override public boolean isValueAllowed() {
+ switch (this) {
+ case BABEL:
+ case LENIENT:
+ case MYSQL_5:
+ return true;
+ default:
+ return false;
+ }
+ }
+
@Override public SqlLibrary semantics() {
switch (this) {
case BIG_QUERY:
diff --git
a/core/src/main/java/org/apache/calcite/sql/validate/SqlDelegatingConformance.java
b/core/src/main/java/org/apache/calcite/sql/validate/SqlDelegatingConformance.java
index 8cc42555f3..3b55d083ba 100644
---
a/core/src/main/java/org/apache/calcite/sql/validate/SqlDelegatingConformance.java
+++
b/core/src/main/java/org/apache/calcite/sql/validate/SqlDelegatingConformance.java
@@ -79,6 +79,10 @@ public class SqlDelegatingConformance extends
SqlAbstractConformance {
return delegate.allowAliasUnnestItems();
}
+ @Override public boolean isValueAllowed() {
+ return delegate.isValueAllowed();
+ }
+
@Override public SqlLibrary semantics() {
return delegate.semantics();
}
diff --git
a/core/src/main/resources/org/apache/calcite/runtime/CalciteResource.properties
b/core/src/main/resources/org/apache/calcite/runtime/CalciteResource.properties
index d8992d57af..77d74c10b2 100644
---
a/core/src/main/resources/org/apache/calcite/runtime/CalciteResource.properties
+++
b/core/src/main/resources/org/apache/calcite/runtime/CalciteResource.properties
@@ -24,6 +24,7 @@ PercentRemainderNotAllowed=Percent remainder ''%'' is not
allowed under the curr
LimitStartCountNotAllowed=''LIMIT start, count'' is not allowed under the
current SQL conformance level
OffsetLimitNotAllowed=''OFFSET start LIMIT count'' is not allowed under the
current SQL conformance level
ApplyNotAllowed=APPLY operator is not allowed under the current SQL
conformance level
+ValueNotAllowed=VALUE is not allowed under the current SQL conformance level
IllegalLiteral=Illegal {0} literal ''{1}'': {2}
IdentifierTooLong=Length of identifier ''{0}'' must be less than or equal to
{1,number,#} characters
BadFormat=not in format ''{0}''
diff --git a/site/_docs/reference.md b/site/_docs/reference.md
index a0c759d370..74724ef866 100644
--- a/site/_docs/reference.md
+++ b/site/_docs/reference.md
@@ -311,7 +311,7 @@ unpivotValue:
| '(' column [, column ]* ')' [ AS '(' literal [, literal ]* ')' ]
values:
- VALUES expression [, expression ]*
+ { VALUES | VALUE } expression [, expression ]*
groupItem:
expression
@@ -400,6 +400,10 @@ but is only allowed in certain
"OFFSET start" may occur before "LIMIT count" in certain
[conformance levels]({{ site.apiRoot
}}/org/apache/calcite/sql/validate/SqlConformance.html#isOffsetLimitAllowed--).
+VALUE is equivalent to VALUES,
+but is not standard SQL and is only allowed in certain
+[conformance levels]({{ site.apiRoot
}}/org/apache/calcite/sql/validate/SqlConformance.html#isValueAllowed--).
+
## Keywords
The following is a list of SQL keywords.
diff --git
a/testkit/src/main/java/org/apache/calcite/sql/parser/SqlParserTest.java
b/testkit/src/main/java/org/apache/calcite/sql/parser/SqlParserTest.java
index 8237b97bec..eab5658b34 100644
--- a/testkit/src/main/java/org/apache/calcite/sql/parser/SqlParserTest.java
+++ b/testkit/src/main/java/org/apache/calcite/sql/parser/SqlParserTest.java
@@ -4073,8 +4073,17 @@ public class SqlParserTest {
}
@Test void testValues() {
+ final String expected = "VALUES (ROW(1, 'two'))";
+ final String pattern =
+ "VALUE is not allowed under the current SQL conformance level";
sql("values(1,'two')")
- .ok("VALUES (ROW(1, 'two'))");
+ .ok(expected);
+ sql("value(1,'two')")
+ .withConformance(SqlConformanceEnum.MYSQL_5)
+ .ok(expected)
+ .node(not(isDdl()));
+ sql("^value^(1,'two')")
+ .fails(pattern);
}
@Test void testValuesExplicitRow() {
@@ -4589,9 +4598,17 @@ public class SqlParserTest {
@Test void testInsertValues() {
final String expected = "INSERT INTO `EMPS`\n"
+ "VALUES (ROW(1, 'Fredkin'))";
+ final String pattern =
+ "VALUE is not allowed under the current SQL conformance level";
sql("insert into emps values (1,'Fredkin')")
.ok(expected)
.node(not(isDdl()));
+ sql("insert into emps value (1, 'Fredkin')")
+ .withConformance(SqlConformanceEnum.MYSQL_5)
+ .ok(expected)
+ .node(not(isDdl()));
+ sql("insert into emps ^value^ (1, 'Fredkin')")
+ .fails(pattern);
}
@Test void testInsertValuesDefault() {