You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by xi...@apache.org on 2021/12/14 22:21:28 UTC

[calcite] branch master updated: [CALCITE-4906] Wrong result for scalar subquery from empty input

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

xiong pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/calcite.git


The following commit(s) were added to refs/heads/master by this push:
     new 250dfb7  [CALCITE-4906] Wrong result for scalar subquery from empty input
250dfb7 is described below

commit 250dfb7a378fd4f8068b63fc9defde8995bbd9a7
Author: Aleksey Plekhanov <pl...@gmail.com>
AuthorDate: Sun Nov 28 17:30:34 2021 +0300

    [CALCITE-4906] Wrong result for scalar subquery from empty input
---
 .../calcite/sql/fun/SqlSingleValueAggFunction.java |  2 +-
 .../apache/calcite/test/SqlToRelConverterTest.java |  5 +++++
 .../org/apache/calcite/test/TableFunctionTest.java |  2 +-
 .../apache/calcite/test/SqlToRelConverterTest.xml  | 15 +++++++++++++
 core/src/test/resources/sql/scalar.iq              | 26 ++++++++++++++++++++++
 5 files changed, 48 insertions(+), 2 deletions(-)

diff --git a/core/src/main/java/org/apache/calcite/sql/fun/SqlSingleValueAggFunction.java b/core/src/main/java/org/apache/calcite/sql/fun/SqlSingleValueAggFunction.java
index d547174..a186fe4 100644
--- a/core/src/main/java/org/apache/calcite/sql/fun/SqlSingleValueAggFunction.java
+++ b/core/src/main/java/org/apache/calcite/sql/fun/SqlSingleValueAggFunction.java
@@ -50,7 +50,7 @@ public class SqlSingleValueAggFunction extends SqlAggFunction {
         "SINGLE_VALUE",
         null,
         SqlKind.SINGLE_VALUE,
-        ReturnTypes.ARG0,
+        ReturnTypes.ARG0_NULLABLE_IF_EMPTY,
         null,
         OperandTypes.ANY,
         SqlFunctionCategory.SYSTEM,
diff --git a/core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java b/core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java
index 8f3188c..07ab07e 100644
--- a/core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java
+++ b/core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java
@@ -2938,6 +2938,11 @@ class SqlToRelConverterTest extends SqlToRelTestBase {
     sql(sql).ok();
   }
 
+  @Test void testSubQueryNoExpand() {
+    final String sql = "select (select empno from EMP where 1 = 0)";
+    sql(sql).expand(false).ok();
+  }
+
   /**
    * Test case for
    * <a href="https://issues.apache.org/jira/browse/CALCITE-695">[CALCITE-695]
diff --git a/core/src/test/java/org/apache/calcite/test/TableFunctionTest.java b/core/src/test/java/org/apache/calcite/test/TableFunctionTest.java
index 4a7713b..36d3229 100644
--- a/core/src/test/java/org/apache/calcite/test/TableFunctionTest.java
+++ b/core/src/test/java/org/apache/calcite/test/TableFunctionTest.java
@@ -122,7 +122,7 @@ class TableFunctionTest {
           + "from (values (2), (4)) as t (x)";
       ResultSet resultSet = connection.createStatement().executeQuery(sql);
       assertThat(CalciteAssert.toString(resultSet),
-          equalTo("X=2; EXPR$1=0\nX=4; EXPR$1=0\n"));
+          equalTo("X=2; EXPR$1=null\nX=4; EXPR$1=null\n"));
     }
   }
 
diff --git a/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml b/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml
index 04bc535..06adac4 100644
--- a/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml
+++ b/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml
@@ -6182,6 +6182,21 @@ LogicalProject(DEPTNO=[$7])
 ]]>
     </Resource>
   </TestCase>
+  <TestCase name="testSubQueryNoExpand">
+    <Resource name="sql">
+      <![CDATA[select (select empno from EMP where 1 = 0)]]>
+    </Resource>
+    <Resource name="plan">
+      <![CDATA[
+LogicalProject(EXPR$0=[$SCALAR_QUERY({
+LogicalProject(EMPNO=[$0])
+  LogicalFilter(condition=[=(1, 0)])
+    LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+})])
+  LogicalValues(tuples=[[{ 0 }]])
+]]>
+    </Resource>
+  </TestCase>
   <TestCase name="testSubQueryOr">
     <Resource name="sql">
       <![CDATA[select * from emp where deptno = 10 or deptno in (
diff --git a/core/src/test/resources/sql/scalar.iq b/core/src/test/resources/sql/scalar.iq
index 283f5c3..365c90e 100644
--- a/core/src/test/resources/sql/scalar.iq
+++ b/core/src/test/resources/sql/scalar.iq
@@ -110,6 +110,32 @@ select deptno, (select sum(empno) from "scott".emp where 1 = 0) as x from "scott
 
 !ok
 
+select deptno, (select empno from "scott".emp where 1 = 0) as x from "scott".dept;
++--------+---+
+| DEPTNO | X |
++--------+---+
+|     10 |   |
+|     20 |   |
+|     30 |   |
+|     40 |   |
++--------+---+
+(4 rows)
+
+!ok
+
+select deptno, (select empno from "scott".emp where emp.deptno = dept.deptno and job = 'PRESIDENT') as x from "scott".dept;
++--------+------+
+| DEPTNO | X    |
++--------+------+
+|     10 | 7839 |
+|     20 |      |
+|     30 |      |
+|     40 |      |
++--------+------+
+(4 rows)
+
+!ok
+
 select deptno, (select sum(empno) from "scott".emp where 1 = 0 group by ()) as x from "scott".dept;
 +--------+---+
 | DEPTNO | X |