You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by li...@apache.org on 2022/10/09 06:43:41 UTC

[calcite] branch main updated: [CALCITE-5296] In a query with ROLLUP, validator wrongly infers that a column is NOT NULL

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

libenchao 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 5bbbd18563 [CALCITE-5296] In a query with ROLLUP, validator wrongly infers that a column is NOT NULL
5bbbd18563 is described below

commit 5bbbd18563c11a80a4d41d02e749f94c9f7f57bb
Author: xurenhe <xu...@gmail.com>
AuthorDate: Fri Sep 23 11:16:21 2022 +0800

    [CALCITE-5296] In a query with ROLLUP, validator wrongly infers that a column is NOT NULL
    
    This closes #2920
---
 .../apache/calcite/sql/validate/SqlValidatorImpl.java  |  5 ++---
 .../java/org/apache/calcite/test/SqlValidatorTest.java |  7 +++++++
 core/src/test/resources/sql/agg.iq                     | 18 ++++++++++++++++++
 3 files changed, 27 insertions(+), 3 deletions(-)

diff --git a/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java b/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java
index c1a479d038..8fbf3a2415 100644
--- a/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java
+++ b/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java
@@ -458,9 +458,8 @@ public class SqlValidatorImpl implements SqlValidatorWithHints {
     selectItems.add(expanded);
     aliases.add(alias);
 
-    if (expanded != null) {
-      inferUnknownTypes(targetType, scope, expanded);
-    }
+    inferUnknownTypes(targetType, selectScope, expanded);
+
     RelDataType type = deriveType(selectScope, expanded);
     // Re-derive SELECT ITEM's data type that may be nullable in AggregatingSelectScope when it
     // appears in advanced grouping elements such as CUBE, ROLLUP , GROUPING SETS.
diff --git a/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java b/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
index be8d40ecc6..81ab2ccc3b 100644
--- a/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
+++ b/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
@@ -7336,6 +7336,13 @@ public class SqlValidatorTest extends SqlValidatorTestCase {
         + "group by rollup(empno), deptno")
         .ok()
         .type("RecordType(INTEGER NOT NULL DEPTNO, INTEGER EMPNO) NOT NULL");
+
+    // empno becomes NULL because it is rolled up, and so does empno + 1.
+    sql("select empno, empno + 1 as e1\n"
+        + "from emp\n"
+        + "group by rollup(empno)")
+        .ok()
+        .type("RecordType(INTEGER EMPNO, INTEGER E1) NOT NULL");
   }
 
   @Test void testGroupByCorrelatedColumn() {
diff --git a/core/src/test/resources/sql/agg.iq b/core/src/test/resources/sql/agg.iq
index 4adc4fd5f9..8718313948 100644
--- a/core/src/test/resources/sql/agg.iq
+++ b/core/src/test/resources/sql/agg.iq
@@ -566,6 +566,24 @@ having count(*) > 3;
 
 !ok
 
+# ROLLUP column used in expression; see [CALCITE-5296]
+# In a query with ROLLUP, validator wrongly infers that a column is NOT NULL
+select deptno, deptno + 1 as d1 from emp group by rollup(deptno);
++--------+----+
+| DEPTNO | D1 |
++--------+----+
+|     10 | 11 |
+|     20 | 21 |
+|     30 | 31 |
+|     50 | 51 |
+|     60 | 61 |
+|        |    |
+|        |    |
++--------+----+
+(7 rows)
+
+!ok
+
 # CUBE and DISTINCT
 select distinct count(*) from emp group by cube(deptno, gender);
 +--------+