Repository: calcite
Updated Branches:
  refs/heads/master bd0e14002 -> 41a067718


[CALCITE-2422] Query with unnest of column from nested subquery fails when 
dynamic table is used

Close apache/calcite#768


Project: http://git-wip-us.apache.org/repos/asf/calcite/repo
Commit: http://git-wip-us.apache.org/repos/asf/calcite/commit/41a06771
Tree: http://git-wip-us.apache.org/repos/asf/calcite/tree/41a06771
Diff: http://git-wip-us.apache.org/repos/asf/calcite/diff/41a06771

Branch: refs/heads/master
Commit: 41a06771876374474d46ef8d3a14886d742c66e9
Parents: bd0e140
Author: Volodymyr Vysotskyi <[email protected]>
Authored: Thu Jul 26 15:44:44 2018 +0300
Committer: Volodymyr Vysotskyi <[email protected]>
Committed: Mon Jul 30 17:20:49 2018 +0300

----------------------------------------------------------------------
 .../calcite/sql/validate/SqlValidatorImpl.java    | 11 +++++++++++
 .../calcite/test/SqlToRelConverterTest.java       |  8 ++++++++
 .../apache/calcite/test/SqlToRelConverterTest.xml | 18 ++++++++++++++++++
 3 files changed, 37 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/calcite/blob/41a06771/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java
----------------------------------------------------------------------
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 2d14bd5..7edcd7b 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
@@ -3025,6 +3025,9 @@ public class SqlValidatorImpl implements 
SqlValidatorWithHints {
     case OVER:
       validateOver((SqlCall) node, scope);
       break;
+    case UNNEST:
+      validateUnnest((SqlCall) node, scope, targetRowType);
+      break;
     default:
       validateQuery(node, scope, targetRowType);
       break;
@@ -3039,6 +3042,14 @@ public class SqlValidatorImpl implements 
SqlValidatorWithHints {
     throw new AssertionError("OVER unexpected in this context");
   }
 
+  protected void validateUnnest(SqlCall call, SqlValidatorScope scope, 
RelDataType targetRowType) {
+    for (int i = 0; i < call.operandCount(); i++) {
+      SqlNode expandedItem = expand(call.operand(i), scope);
+      call.setOperand(i, expandedItem);
+    }
+    validateQuery(call, scope, targetRowType);
+  }
+
   private void checkRollUpInUsing(SqlIdentifier identifier, SqlNode 
leftOrRight) {
     leftOrRight = stripAs(leftOrRight);
     // if it's not a SqlIdentifier then that's fine, it'll be validated 
somewhere else.

http://git-wip-us.apache.org/repos/asf/calcite/blob/41a06771/core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java
----------------------------------------------------------------------
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 2fd4dbe..6cc02de 100644
--- a/core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java
+++ b/core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java
@@ -2519,6 +2519,14 @@ public class SqlToRelConverterTest extends 
SqlToRelTestBase {
         + "unnest(t1.fake_col) as t2";
     sql(sql3).with(getTesterWithDynamicTable()).ok();
   }
+
+  @Test public void testStarDynamicSchemaUnnestNestedSubquery() {
+    String sql3 = "select t2.c1\n"
+        + "from (select * from SALES.CUSTOMER) as t1,\n"
+        + "unnest(t1.fake_col) as t2(c1)";
+    sql(sql3).with(getTesterWithDynamicTable()).ok();
+  }
+
   /**
    * Test case for Dynamic Table / Dynamic Star support
    * <a 
href="https://issues.apache.org/jira/browse/CALCITE-1150";>[CALCITE-1150]</a>

http://git-wip-us.apache.org/repos/asf/calcite/blob/41a06771/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml
----------------------------------------------------------------------
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 51d6855..0867b8d 100644
--- a/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml
+++ b/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml
@@ -5292,6 +5292,24 @@ LogicalProject(**=[$1], $unnest=[$2])
 ]]>
         </Resource>
     </TestCase>
+    <TestCase name="testStarDynamicSchemaUnnestNestedSubquery">
+        <Resource name="sql">
+            <![CDATA[select t2.c1
+            from (select * from SALES.CUSTOMER) as t1, unnest(t1.fake_col) as 
t2(c1)]]>
+        </Resource>
+        <Resource name="plan">
+            <![CDATA[
+LogicalProject(C1=[$1])
+  LogicalCorrelate(correlation=[$cor0], joinType=[inner], 
requiredColumns=[{0}])
+    LogicalProject(**=[$0])
+      LogicalTableScan(table=[[CATALOG, SALES, CUSTOMER]])
+    LogicalProject(C1=[$0])
+      Uncollect
+        LogicalProject(EXPR$0=[ITEM($cor0.**, 'FAKE_COL')])
+          LogicalValues(tuples=[[{ 0 }]])
+]]>
+        </Resource>
+    </TestCase>
     <TestCase name="testAnyValueAggregateFunctionNoGroupBy">
         <Resource name="sql">
             <![CDATA[SELECT any_value(empno) as anyempno FROM emp AS e]]>

Reply via email to