This is an automated email from the ASF dual-hosted git repository.

mbudiu 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 1ff37a0097 [CALCITE-6986] Parser rejects SQL sources that produce an 
empty statement list
1ff37a0097 is described below

commit 1ff37a00977edb348bb7281443f4115190ab7086
Author: Mihai Budiu <[email protected]>
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 <[email protected]>
---
 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);
   }

Reply via email to