You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by mb...@apache.org on 2025/04/26 00:14:30 UTC
(calcite) branch issue6986 created (now 490d04c198)
This is an automated email from the ASF dual-hosted git repository.
mbudiu pushed a change to branch issue6986
in repository https://gitbox.apache.org/repos/asf/calcite.git
at 490d04c198 [CALCITE-6986] Parser rejects SQL sources that produce an empty statement list
This branch includes the following new commits:
new 490d04c198 [CALCITE-6986] Parser rejects SQL sources that produce an empty statement list
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
(calcite) 01/01: [CALCITE-6986] Parser rejects SQL sources that produce an empty statement list
Posted by mb...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
mbudiu pushed a commit to branch issue6986
in repository https://gitbox.apache.org/repos/asf/calcite.git
commit 490d04c19863625883105fcb14e6dc2bce891d92
Author: Mihai Budiu <mb...@feldera.com>
AuthorDate: Fri Apr 25 17:13:47 2025 -0700
[CALCITE-6986] Parser rejects SQL sources that produce an empty statement list
Signed-off-by: Mihai Budiu <mb...@feldera.com>
---
core/src/main/codegen/templates/Parser.jj | 22 ++++++++++++----------
.../apache/calcite/sql/parser/SqlParserPos.java | 2 +-
.../apache/calcite/sql/parser/SqlParserTest.java | 8 ++++++++
.../apache/calcite/sql/test/SqlTestFactory.java | 6 ++++++
4 files changed, 27 insertions(+), 11 deletions(-)
diff --git a/core/src/main/codegen/templates/Parser.jj b/core/src/main/codegen/templates/Parser.jj
index c02de6cb93..fa7593c194 100644
--- a/core/src/main/codegen/templates/Parser.jj
+++ b/core/src/main/codegen/templates/Parser.jj
@@ -1115,17 +1115,19 @@ SqlNodeList SqlStmtList() :
SqlNode stmt;
}
{
- stmt = SqlStmt() {
- stmtList.add(stmt);
- }
(
- <SEMICOLON>
- [
- stmt = SqlStmt() {
- stmtList.add(stmt);
- }
- ]
- )*
+ stmt = SqlStmt() {
+ stmtList.add(stmt);
+ }
+ (
+ <SEMICOLON>
+ [
+ stmt = SqlStmt() {
+ stmtList.add(stmt);
+ }
+ ]
+ )*
+ )?
<EOF>
{
return new SqlNodeList(stmtList, Span.of(stmtList).pos());
diff --git a/core/src/main/java/org/apache/calcite/sql/parser/SqlParserPos.java b/core/src/main/java/org/apache/calcite/sql/parser/SqlParserPos.java
index 53e62346a3..97fb72cac0 100644
--- a/core/src/main/java/org/apache/calcite/sql/parser/SqlParserPos.java
+++ b/core/src/main/java/org/apache/calcite/sql/parser/SqlParserPos.java
@@ -229,7 +229,7 @@ public static SqlParserPos sum(Iterable<SqlParserPos> poses) {
? (List<SqlParserPos>) poses
: Lists.newArrayList(poses);
if (list.isEmpty()) {
- throw new AssertionError();
+ return SqlParserPos.ZERO;
}
final SqlParserPos pos0 = list.get(0);
if (list.size() == 1) {
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 a01a8c77d1..0939a594fc 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
@@ -2111,6 +2111,14 @@ void checkPeriodPredicate(Checker checker) {
+ "FROM `DEPT`))) AND (3 = 4))");
}
+ /** Test case for <a href="https://issues.apache.org/jira/browse/CALCITE-6986">[CALCITE-6986]
+ * Parser rejects SQL sources that produce an empty statement list</a>. */
+ @Test public void testEmpty() {
+ sql("").list().ok();
+ sql(" ").list().ok();
+ sql("-- comment").list().ok();
+ }
+
@Test void testUnique() {
sql("select * from dept where unique (select 1 from emp where emp.deptno = dept.deptno)")
.ok("SELECT *\n"
diff --git a/testkit/src/main/java/org/apache/calcite/sql/test/SqlTestFactory.java b/testkit/src/main/java/org/apache/calcite/sql/test/SqlTestFactory.java
index 86476b5ef6..9ecc8429c4 100644
--- a/testkit/src/main/java/org/apache/calcite/sql/test/SqlTestFactory.java
+++ b/testkit/src/main/java/org/apache/calcite/sql/test/SqlTestFactory.java
@@ -130,6 +130,12 @@ protected SqlTestFactory(CatalogReaderFactory catalogReaderFactory,
/** Creates a parser. */
public SqlParser createParser(String sql) {
+ if (sql.isEmpty()) {
+ // I could not figure out how to convince the grammar to accept an empty
+ // string. Without this change I get an exception in the token reader in code
+ // generated by JavaCC
+ sql = " ";
+ }
SqlParser.Config parserConfig = parserConfig();
return SqlParser.create(new SourceStringReader(sql), parserConfig);
}