Repository: calcite Updated Branches: refs/heads/master 5e765bd1a -> d12549530
[CALCITE-2514] Add SqlIdentifier conversion to ITEM operator for dynamic tables in ExtendedExpander (Arina Ielchiieva) Close #814 Project: http://git-wip-us.apache.org/repos/asf/calcite/repo Commit: http://git-wip-us.apache.org/repos/asf/calcite/commit/d1254953 Tree: http://git-wip-us.apache.org/repos/asf/calcite/tree/d1254953 Diff: http://git-wip-us.apache.org/repos/asf/calcite/diff/d1254953 Branch: refs/heads/master Commit: d12549530c0aeeedbd6b03f5473c15029d93566f Parents: 5e765bd Author: Arina Ielchiieva <[email protected]> Authored: Fri Aug 31 14:57:15 2018 +0300 Committer: Volodymyr Vysotskyi <[email protected]> Committed: Mon Sep 3 14:29:53 2018 +0300 ---------------------------------------------------------------------- .../calcite/sql/validate/SqlValidatorImpl.java | 41 +++++++++++--------- .../calcite/test/SqlToRelConverterTest.java | 15 +++++++ .../calcite/test/SqlToRelConverterTest.xml | 15 +++++++ 3 files changed, 52 insertions(+), 19 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/calcite/blob/d1254953/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 da64cf8..d425b95 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 @@ -5631,24 +5631,7 @@ public class SqlValidatorImpl implements SqlValidatorWithHints { return call.accept(this); } final SqlIdentifier fqId = getScope().fullyQualify(id).identifier; - SqlNode expandedExpr = fqId; - // Convert a column ref into ITEM(*, 'col_name'). - // select col_name from (select * from dynTable) - // SqlIdentifier "col_name" would be resolved to a dynamic star field in dynTable's rowType. - // Expand such SqlIdentifier to ITEM operator. - if (DynamicRecordType.isDynamicStarColName(Util.last(fqId.names)) - && !DynamicRecordType.isDynamicStarColName(Util.last(id.names))) { - SqlNode[] inputs = new SqlNode[2]; - inputs[0] = fqId; - inputs[1] = SqlLiteral.createCharString( - Util.last(id.names), - id.getParserPosition()); - SqlBasicCall item_call = new SqlBasicCall( - SqlStdOperatorTable.ITEM, - inputs, - id.getParserPosition()); - expandedExpr = item_call; - } + SqlNode expandedExpr = expandDynamicStar(id, fqId); validator.setOriginal(expandedExpr, id); return expandedExpr; } @@ -5670,6 +5653,24 @@ public class SqlValidatorImpl implements SqlValidatorWithHints { validator.setOriginal(result, call); return result; } + + protected SqlNode expandDynamicStar(SqlIdentifier id, SqlIdentifier fqId) { + if (DynamicRecordType.isDynamicStarColName(Util.last(fqId.names)) + && !DynamicRecordType.isDynamicStarColName(Util.last(id.names))) { + // Convert a column ref into ITEM(*, 'col_name') + // for a dynamic star field in dynTable's rowType. + SqlNode[] inputs = new SqlNode[2]; + inputs[0] = fqId; + inputs[1] = SqlLiteral.createCharString( + Util.last(id.names), + id.getParserPosition()); + return new SqlBasicCall( + SqlStdOperatorTable.ITEM, + inputs, + id.getParserPosition()); + } + return fqId; + } } /** @@ -5819,7 +5820,9 @@ public class SqlValidatorImpl implements SqlValidatorWithHints { } expr = stripAs(expr); if (expr instanceof SqlIdentifier) { - expr = getScope().fullyQualify((SqlIdentifier) expr).identifier; + SqlIdentifier sid = (SqlIdentifier) expr; + final SqlIdentifier fqId = getScope().fullyQualify(sid).identifier; + expr = expandDynamicStar(sid, fqId); } return expr; } http://git-wip-us.apache.org/repos/asf/calcite/blob/d1254953/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 cafe24a..dc88b36 100644 --- a/core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java +++ b/core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java @@ -29,6 +29,7 @@ import org.apache.calcite.rel.externalize.RelXmlWriter; import org.apache.calcite.sql.SqlExplainLevel; import org.apache.calcite.sql.validate.SqlConformance; import org.apache.calcite.sql.validate.SqlConformanceEnum; +import org.apache.calcite.sql.validate.SqlDelegatingConformance; import org.apache.calcite.sql2rel.SqlToRelConverter; import org.apache.calcite.test.catalog.MockCatalogReaderExtended; import org.apache.calcite.util.Bug; @@ -2623,6 +2624,20 @@ public class SqlToRelConverterTest extends SqlToRelTestBase { sql(sql).with(getTesterWithDynamicTable()).ok(); } + @Test public void testWindowAndGroupByWithDynamicStar() { + final String sql = "SELECT\n" + + "n_regionkey,\n" + + "MAX(MIN(n_nationkey)) OVER (PARTITION BY n_regionkey)\n" + + "FROM (SELECT * FROM SALES.NATION)\n" + + "GROUP BY n_regionkey"; + + sql(sql).conformance(new SqlDelegatingConformance(SqlConformanceEnum.DEFAULT) { + @Override public boolean isGroupByAlias() { + return true; + } + }).with(getTesterWithDynamicTable()).ok(); + } + /** Test case for * <a href="https://issues.apache.org/jira/browse/CALCITE-2366">[CALCITE-2366] * Add support for ANY_VALUE aggregate function</a>. */ http://git-wip-us.apache.org/repos/asf/calcite/blob/d1254953/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 277b5e0..c605bef 100644 --- a/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml +++ b/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml @@ -5142,6 +5142,21 @@ LogicalProject(EXPR$0=[CASE(>(COUNT(ITEM($0, 'N_NATIONKEY')) OVER (PARTITION BY ]]> </Resource> </TestCase> + <TestCase name="testWindowAndGroupByWithDynamicStar"> + <Resource name="sql"> + <![CDATA[SELECT +n_regionkey, MAX(MIN(n_nationkey)) OVER (PARTITION BY n_regionkey) +FROM (SELECT * FROM SALES.NATION) GROUP BY n_regionkey)]]> + </Resource> + <Resource name="plan"> + <![CDATA[ +LogicalProject(N_REGIONKEY=[$0], EXPR$1=[MAX($1) OVER (PARTITION BY $0 RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)]) + LogicalAggregate(group=[{0}], agg#0=[MIN($1)]) + LogicalProject(N_REGIONKEY=[ITEM($0, 'N_REGIONKEY')], $f1=[ITEM($0, 'N_NATIONKEY')]) + LogicalTableScan(table=[[CATALOG, SALES, NATION]]) +]]> + </Resource> + </TestCase> <TestCase name="testWithExists"> <Resource name="sql"> <