From 1eae0a0ba493ab97b80a027833fc74571d056b6a Mon Sep 17 00:00:00 2001 From: lxian2shell Date: Mon, 23 Mar 2020 11:06:31 +0800 Subject: [PATCH] #63 SqlValidatorImpl - correct sql select rowType deduction (#64) * SqlValidatorImpl - correct sql select rowType deduction * r28 --- cassandra/pom.xml | 4 +-- core/pom.xml | 4 +-- .../sql/validate/SqlValidatorImpl.java | 35 ++++++++++++++++++- druid/pom.xml | 4 +-- elasticsearch2/pom.xml | 4 +-- elasticsearch5/pom.xml | 4 +-- example/csv/pom.xml | 4 +-- example/function/pom.xml | 4 +-- example/pom.xml | 4 +-- file/pom.xml | 4 +-- geode/pom.xml | 4 +-- linq4j/pom.xml | 4 +-- mongodb/pom.xml | 4 +-- pig/pom.xml | 4 +-- piglet/pom.xml | 4 +-- plus/pom.xml | 4 +-- pom.xml | 2 +- server/pom.xml | 4 +-- spark/pom.xml | 4 +-- splunk/pom.xml | 4 +-- ubenchmark/pom.xml | 2 +- 21 files changed, 72 insertions(+), 39 deletions(-) diff --git a/cassandra/pom.xml b/cassandra/pom.xml index 611572e7ad1f..d5378c2109c7 100644 --- a/cassandra/pom.xml +++ b/cassandra/pom.xml @@ -20,12 +20,12 @@ limitations under the License. org.apache.calcite calcite - 1.16.0-kylin-4.x-r27 + 1.16.0-kylin-4.x-r28 calcite-cassandra jar -1.16.0-kylin-4.x-r27 +1.16.0-kylin-4.x-r28 Calcite Cassandra Cassandra adapter for Calcite diff --git a/core/pom.xml b/core/pom.xml index 6c8a7eff7acd..d639567c5a83 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -20,12 +20,12 @@ limitations under the License. org.apache.calcite calcite - 1.16.0-kylin-4.x-r27 + 1.16.0-kylin-4.x-r28 calcite-core jar -1.16.0-kylin-4.x-r27 +1.16.0-kylin-4.x-r28 Calcite Core Core Calcite APIs and engine. 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 a9ceb706397f..6fd39395aea5 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 @@ -129,6 +129,7 @@ import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Objects; import java.util.Set; import java.util.TreeSet; @@ -431,10 +432,42 @@ private boolean expandSelectItem( // parentheses-free functions such as LOCALTIME into explicit function // calls. SqlNode expanded = expand(selectItem, scope); - final String alias = + String alias = deriveAlias( selectItem, aliases.size()); + // if the alias derived is found in already generated aliases set + // then there is a chance that two selectItem(SqlIdentifier) appear to have the same name + // In that case, we need to derive a different name for such selectItem + // eg. + // SELECT * FROM ( + // SELECT order_id, test_kylin_fact.cal_dt, edw.test_cal_dt.cal_dt + // FROM test_kylin_fact LEFT JOIN edw.test_cal_dt + // ON test_kylin_fact.order_id = test_cal_dt.DAY_OF_CAL_ID + // ) T where T.order_id = 4752 + if (aliases.contains(alias) && selectItem instanceof SqlIdentifier) { + boolean foundEqualSelectNode = false; + int sameNameIdentifierCount = -1; + for (SqlNode item : selectItems) { + if (item instanceof SqlIdentifier) { + // if same selectItem appears before, then it is okay for them to share the same alias + if (item.equalsDeep(selectItem, Litmus.IGNORE)) { + foundEqualSelectNode = true; + break; + } + // count the same name selectItem + if (Objects.equals( + Util.last(((SqlIdentifier) item).names), + Util.last(((SqlIdentifier) selectItem).names))) { + sameNameIdentifierCount++; + } + } + } + if (!foundEqualSelectNode) { + Preconditions.checkArgument(sameNameIdentifierCount > -1); + alias += sameNameIdentifierCount; + } + } // If expansion has altered the natural alias, supply an explicit 'AS'. final SqlValidatorScope selectScope = getSelectScope(select); diff --git a/druid/pom.xml b/druid/pom.xml index 537567b9cd4c..dcf1ba271035 100644 --- a/druid/pom.xml +++ b/druid/pom.xml @@ -20,12 +20,12 @@ limitations under the License. org.apache.calcite calcite - 1.16.0-kylin-4.x-r27 + 1.16.0-kylin-4.x-r28 calcite-druid jar -1.16.0-kylin-4.x-r27 +1.16.0-kylin-4.x-r28 Calcite Druid Druid adapter for Calcite diff --git a/elasticsearch2/pom.xml b/elasticsearch2/pom.xml index cb05d5e7d112..2df2487582ce 100644 --- a/elasticsearch2/pom.xml +++ b/elasticsearch2/pom.xml @@ -21,12 +21,12 @@ limitations under the License. org.apache.calcite calcite - 1.16.0-kylin-4.x-r27 + 1.16.0-kylin-4.x-r28 calcite-elasticsearch2 jar -1.16.0-kylin-4.x-r27 +1.16.0-kylin-4.x-r28 Calcite Elasticsearch Elasticsearch adapter for Calcite diff --git a/elasticsearch5/pom.xml b/elasticsearch5/pom.xml index bb0c12927b51..7f95480a06e0 100644 --- a/elasticsearch5/pom.xml +++ b/elasticsearch5/pom.xml @@ -21,12 +21,12 @@ limitations under the License. org.apache.calcite calcite - 1.16.0-kylin-4.x-r27 + 1.16.0-kylin-4.x-r28 calcite-elasticsearch5 jar -1.16.0-kylin-4.x-r27 +1.16.0-kylin-4.x-r28 Calcite Elasticsearch5 Elasticsearch5 adapter for Calcite diff --git a/example/csv/pom.xml b/example/csv/pom.xml index 0739ecf8b3ed..142255e70f3e 100644 --- a/example/csv/pom.xml +++ b/example/csv/pom.xml @@ -20,12 +20,12 @@ limitations under the License. org.apache.calcite calcite-example - 1.16.0-kylin-4.x-r27 + 1.16.0-kylin-4.x-r28 calcite-example-csv jar -1.16.0-kylin-4.x-r27 +1.16.0-kylin-4.x-r28 Calcite Example CSV An example Calcite provider that reads CSV files diff --git a/example/function/pom.xml b/example/function/pom.xml index 7990588c5e67..16e67f710562 100644 --- a/example/function/pom.xml +++ b/example/function/pom.xml @@ -20,12 +20,12 @@ limitations under the License. org.apache.calcite calcite-example - 1.16.0-kylin-4.x-r27 + 1.16.0-kylin-4.x-r28 calcite-example-function jar -1.16.0-kylin-4.x-r27 +1.16.0-kylin-4.x-r28 Calcite Example Function Examples of user-defined Calcite functions diff --git a/example/pom.xml b/example/pom.xml index e317350ca795..45853518f466 100644 --- a/example/pom.xml +++ b/example/pom.xml @@ -20,13 +20,13 @@ limitations under the License. org.apache.calcite calcite - 1.16.0-kylin-4.x-r27 + 1.16.0-kylin-4.x-r28 calcite-example pom -1.16.0-kylin-4.x-r27 +1.16.0-kylin-4.x-r28 Calcite Examples Calcite examples diff --git a/file/pom.xml b/file/pom.xml index e81507dc4acf..8854017a2e4a 100644 --- a/file/pom.xml +++ b/file/pom.xml @@ -19,13 +19,13 @@ limitations under the License. org.apache.calcite calcite - 1.16.0-kylin-4.x-r27 + 1.16.0-kylin-4.x-r28 calcite-file jar -1.16.0-kylin-4.x-r27 +1.16.0-kylin-4.x-r28 Calcite File Calcite provider that reads files and URIs diff --git a/geode/pom.xml b/geode/pom.xml index 634823163ee1..992b595c00f1 100644 --- a/geode/pom.xml +++ b/geode/pom.xml @@ -20,12 +20,12 @@ limitations under the License. org.apache.calcite calcite - 1.16.0-kylin-4.x-r27 + 1.16.0-kylin-4.x-r28 calcite-geode jar -1.16.0-kylin-4.x-r27 +1.16.0-kylin-4.x-r28 Calcite Geode Geode adapter for Calcite diff --git a/linq4j/pom.xml b/linq4j/pom.xml index 24741fbefc9b..7f8e4139f284 100644 --- a/linq4j/pom.xml +++ b/linq4j/pom.xml @@ -20,12 +20,12 @@ limitations under the License. org.apache.calcite calcite - 1.16.0-kylin-4.x-r27 + 1.16.0-kylin-4.x-r28 calcite-linq4j jar -1.16.0-kylin-4.x-r27 +1.16.0-kylin-4.x-r28 Calcite Linq4j Calcite APIs for LINQ (Language-Integrated Query) in Java diff --git a/mongodb/pom.xml b/mongodb/pom.xml index efa4b4c0c6fd..af3a6f2ce94c 100644 --- a/mongodb/pom.xml +++ b/mongodb/pom.xml @@ -20,12 +20,12 @@ limitations under the License. org.apache.calcite calcite - 1.16.0-kylin-4.x-r27 + 1.16.0-kylin-4.x-r28 calcite-mongodb jar -1.16.0-kylin-4.x-r27 +1.16.0-kylin-4.x-r28 Calcite MongoDB MongoDB adapter for Calcite diff --git a/pig/pom.xml b/pig/pom.xml index bda108cec7be..9400d03e40ab 100644 --- a/pig/pom.xml +++ b/pig/pom.xml @@ -20,12 +20,12 @@ limitations under the License. org.apache.calcite calcite - 1.16.0-kylin-4.x-r27 + 1.16.0-kylin-4.x-r28 calcite-pig jar -1.16.0-kylin-4.x-r27 +1.16.0-kylin-4.x-r28 Calcite Pig Pig adapter for Calcite diff --git a/piglet/pom.xml b/piglet/pom.xml index 3b54e9f7d652..60fc76384aef 100644 --- a/piglet/pom.xml +++ b/piglet/pom.xml @@ -20,12 +20,12 @@ limitations under the License. org.apache.calcite calcite - 1.16.0-kylin-4.x-r27 + 1.16.0-kylin-4.x-r28 calcite-piglet jar -1.16.0-kylin-4.x-r27 +1.16.0-kylin-4.x-r28 Calcite Piglet Pig-like language built on top of Calcite algebra diff --git a/plus/pom.xml b/plus/pom.xml index 6b9bf8ed29f1..ca6962ee778d 100644 --- a/plus/pom.xml +++ b/plus/pom.xml @@ -20,12 +20,12 @@ limitations under the License. org.apache.calcite calcite - 1.16.0-kylin-4.x-r27 + 1.16.0-kylin-4.x-r28 calcite-plus jar -1.16.0-kylin-4.x-r27 +1.16.0-kylin-4.x-r28 Calcite Plus Miscellaneous extras for Calcite diff --git a/pom.xml b/pom.xml index 79c8a683c7da..3ac0515f209a 100644 --- a/pom.xml +++ b/pom.xml @@ -27,7 +27,7 @@ limitations under the License. org.apache.calcite calcite pom -1.16.0-kylin-4.x-r27 +1.16.0-kylin-4.x-r28 Calcite diff --git a/server/pom.xml b/server/pom.xml index ea0c7d936e9c..f5a35785e0c1 100644 --- a/server/pom.xml +++ b/server/pom.xml @@ -20,12 +20,12 @@ limitations under the License. org.apache.calcite calcite - 1.16.0-kylin-4.x-r27 + 1.16.0-kylin-4.x-r28 calcite-server jar -1.16.0-kylin-4.x-r27 +1.16.0-kylin-4.x-r28 Calcite Server Calcite Server diff --git a/spark/pom.xml b/spark/pom.xml index 640e880bc82f..4e60d6fe1d0d 100644 --- a/spark/pom.xml +++ b/spark/pom.xml @@ -20,12 +20,12 @@ limitations under the License. org.apache.calcite calcite - 1.16.0-kylin-4.x-r27 + 1.16.0-kylin-4.x-r28 calcite-spark jar -1.16.0-kylin-4.x-r27 +1.16.0-kylin-4.x-r28 Calcite Spark diff --git a/splunk/pom.xml b/splunk/pom.xml index 4c123ff90a16..71b93a2f9798 100644 --- a/splunk/pom.xml +++ b/splunk/pom.xml @@ -20,12 +20,12 @@ limitations under the License. org.apache.calcite calcite - 1.16.0-kylin-4.x-r27 + 1.16.0-kylin-4.x-r28 calcite-splunk jar -1.16.0-kylin-4.x-r27 +1.16.0-kylin-4.x-r28 Calcite Splunk Splunk adapter for Calcite; also a JDBC driver for Splunk diff --git a/ubenchmark/pom.xml b/ubenchmark/pom.xml index 6c7e25e2b806..ce49095715c9 100644 --- a/ubenchmark/pom.xml +++ b/ubenchmark/pom.xml @@ -20,7 +20,7 @@ limitations under the License. org.apache.calcite calcite - 1.16.0-kylin-4.x-r27 + 1.16.0-kylin-4.x-r28