You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kylin.apache.org by li...@apache.org on 2017/05/12 05:22:23 UTC

[08/23] kylin git commit: KYLIN-2599 select * in subquery fail due to bug in hackSelectStar

KYLIN-2599 select * in subquery fail due to bug in hackSelectStar


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

Branch: refs/heads/2.0.x-hbase0.98
Commit: f982a27c9307aa902d79c8757904c9c154e4c914
Parents: cb47216
Author: Hongbin Ma <ma...@apache.org>
Authored: Wed May 10 15:50:49 2017 +0800
Committer: Hongbin Ma <ma...@apache.org>
Committed: Wed May 10 15:50:49 2017 +0800

----------------------------------------------------------------------
 .../calcite/sql2rel/SqlToRelConverter.java      | 36 ++++++++++++++++++--
 .../apache/kylin/query/ITKylinQueryTest.java    |  1 +
 .../src/test/resources/query/sql/query105.sql   | 26 ++++++++++++++
 .../resources/query/sql_verifyCount/query09.sql | 34 ++++++++++++++++++
 .../query/sql_verifyCount/query09.sql.expected  |  2 ++
 .../resources/query/sql_verifyCount/query10.sql | 34 ++++++++++++++++++
 .../query/sql_verifyCount/query10.sql.expected  |  2 ++
 .../resources/query/sql_verifyCount/query11.sql | 36 ++++++++++++++++++++
 .../query/sql_verifyCount/query11.sql.expected  |  2 ++
 9 files changed, 170 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/kylin/blob/f982a27c/atopcalcite/src/main/java/org/apache/calcite/sql2rel/SqlToRelConverter.java
----------------------------------------------------------------------
diff --git a/atopcalcite/src/main/java/org/apache/calcite/sql2rel/SqlToRelConverter.java b/atopcalcite/src/main/java/org/apache/calcite/sql2rel/SqlToRelConverter.java
index c199c31..8085a70 100644
--- a/atopcalcite/src/main/java/org/apache/calcite/sql2rel/SqlToRelConverter.java
+++ b/atopcalcite/src/main/java/org/apache/calcite/sql2rel/SqlToRelConverter.java
@@ -29,6 +29,7 @@ import org.apache.calcite.plan.RelTraitSet;
 import org.apache.calcite.prepare.Prepare;
 import org.apache.calcite.prepare.RelOptTableImpl;
 import org.apache.calcite.rel.RelCollation;
+import org.apache.calcite.rel.RelCollationImpl;
 import org.apache.calcite.rel.RelCollationTraitDef;
 import org.apache.calcite.rel.RelCollations;
 import org.apache.calcite.rel.RelFieldCollation;
@@ -47,6 +48,7 @@ import org.apache.calcite.rel.core.Project;
 import org.apache.calcite.rel.core.RelFactories;
 import org.apache.calcite.rel.core.Sample;
 import org.apache.calcite.rel.core.Sort;
+import org.apache.calcite.rel.core.TableScan;
 import org.apache.calcite.rel.core.Uncollect;
 import org.apache.calcite.rel.logical.LogicalAggregate;
 import org.apache.calcite.rel.logical.LogicalCorrelate;
@@ -611,6 +613,7 @@ public class SqlToRelConverter {
         } else {
             return root;
         }
+
         //
         RelNode input = rootPrj.getInput();
         //        if (!(//
@@ -626,25 +629,52 @@ public class SqlToRelConverter {
         List<String> inFields = inType.getFieldNames();
         List<RexNode> projExp = new ArrayList<>();
         List<Pair<Integer, String>> projFields = new ArrayList<>();
+        Map<Integer,Integer> projFieldMapping = new HashMap<>();
         RelDataTypeFactory.FieldInfoBuilder projTypeBuilder = getCluster().getTypeFactory().builder();
         RelDataTypeFactory.FieldInfoBuilder validTypeBuilder = getCluster().getTypeFactory().builder();
+        
+        boolean hiddenColumnExists = false;
+        for (int i = 0; i < root.validatedRowType.getFieldList().size(); i++) {
+            if (root.validatedRowType.getFieldNames().get(i).startsWith("_KY_")) 
+                hiddenColumnExists = true;
+        }
+        if(!hiddenColumnExists) {
+            return root;
+        }
+        
         for (int i = 0; i < inFields.size(); i++) {
             if (!inFields.get(i).startsWith("_KY_")) {
                 projExp.add(rootPrj.getProjects().get(i));
+                projFieldMapping.put(i, projFields.size());
                 projFields.add(Pair.of(projFields.size(), inFields.get(i)));
                 projTypeBuilder.add(inType.getFieldList().get(i));
-                validTypeBuilder.add(root.validatedRowType.getFieldList().get(i));
+                
+                if (i < root.validatedRowType.getFieldList().size()) //for cases like kylin-it/src/test/resources/query/sql_verifyCount/query10.sql
+                    validTypeBuilder.add(root.validatedRowType.getFieldList().get(i));
             }
         }
 
         RelDataType projRowType = getCluster().getTypeFactory().createStructType(projTypeBuilder);
         rootPrj = LogicalProject.create(input, projExp, projRowType);
         if (rootSort != null) {
-            rootSort = (LogicalSort) rootSort.copy(rootSort.getTraitSet(), rootPrj, rootSort.collation, rootSort.offset, rootSort.fetch);
+            //for cases like kylin-it/src/test/resources/query/sql_verifyCount/query10.sql, original RelCollation is stale, need to fix its fieldIndex
+            RelCollation originalCollation = rootSort.collation;
+            RelCollation newCollation = null;
+            List<RelFieldCollation> fieldCollations = originalCollation.getFieldCollations();
+            ImmutableList.Builder<RelFieldCollation> newFieldCollations = ImmutableList.builder();
+            for (RelFieldCollation fieldCollation : fieldCollations) {
+                if(projFieldMapping.containsKey(fieldCollation.getFieldIndex())) { 
+                    newFieldCollations.add(fieldCollation.copy(projFieldMapping.get(fieldCollation.getFieldIndex())));
+                } else {
+                    newFieldCollations.add(fieldCollation);
+                }
+            }
+            newCollation = RelCollationImpl.of(newFieldCollations.build());
+            rootSort = LogicalSort.create(rootPrj, newCollation, rootSort.offset, rootSort.fetch);
         }
 
         RelDataType validRowType = getCluster().getTypeFactory().createStructType(validTypeBuilder);
-        root = new RelRoot(rootSort == null ? rootPrj : rootSort, validRowType, root.kind, projFields, root.collation);
+        root = new RelRoot(rootSort == null ? rootPrj : rootSort, validRowType, root.kind, projFields, rootSort == null ? root.collation : rootSort.getCollation());
 
         validator.setValidatedNodeType(query, validRowType);
 

http://git-wip-us.apache.org/repos/asf/kylin/blob/f982a27c/kylin-it/src/test/java/org/apache/kylin/query/ITKylinQueryTest.java
----------------------------------------------------------------------
diff --git a/kylin-it/src/test/java/org/apache/kylin/query/ITKylinQueryTest.java b/kylin-it/src/test/java/org/apache/kylin/query/ITKylinQueryTest.java
index cdaa10b..7a0af10 100644
--- a/kylin-it/src/test/java/org/apache/kylin/query/ITKylinQueryTest.java
+++ b/kylin-it/src/test/java/org/apache/kylin/query/ITKylinQueryTest.java
@@ -133,6 +133,7 @@ public class ITKylinQueryTest extends KylinTestBase {
 
     @Test
     public void testSingleRunQuery() throws Exception {
+        System.setProperty("log4j.configuration", "file:../build/conf/kylin-tools-log4j.properties");
 
         String queryFileName = getQueryFolderPrefix() + "src/test/resources/query/sql_verifyCount/query03.sql";
 

http://git-wip-us.apache.org/repos/asf/kylin/blob/f982a27c/kylin-it/src/test/resources/query/sql/query105.sql
----------------------------------------------------------------------
diff --git a/kylin-it/src/test/resources/query/sql/query105.sql b/kylin-it/src/test/resources/query/sql/query105.sql
new file mode 100644
index 0000000..a467b65
--- /dev/null
+++ b/kylin-it/src/test/resources/query/sql/query105.sql
@@ -0,0 +1,26 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one
+-- or more contributor license agreements.  See the NOTICE file
+-- distributed with this work for additional information
+-- regarding copyright ownership.  The ASF licenses this file
+-- to you under the Apache License, Version 2.0 (the
+-- "License"); you may not use this file except in compliance
+-- with the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+select fact.lstg_format_name from 
+ 
+ (select * from test_kylin_fact where cal_dt > date'2010-01-01' ) as fact
+ 
+ group by fact.lstg_format_name 
+ 
+ order by CASE WHEN fact.lstg_format_name IS NULL THEN 'sdf' ELSE fact.lstg_format_name END 
+ 

http://git-wip-us.apache.org/repos/asf/kylin/blob/f982a27c/kylin-it/src/test/resources/query/sql_verifyCount/query09.sql
----------------------------------------------------------------------
diff --git a/kylin-it/src/test/resources/query/sql_verifyCount/query09.sql b/kylin-it/src/test/resources/query/sql_verifyCount/query09.sql
new file mode 100644
index 0000000..74a8355
--- /dev/null
+++ b/kylin-it/src/test/resources/query/sql_verifyCount/query09.sql
@@ -0,0 +1,34 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one
+-- or more contributor license agreements.  See the NOTICE file
+-- distributed with this work for additional information
+-- regarding copyright ownership.  The ASF licenses this file
+-- to you under the Apache License, Version 2.0 (the
+-- "License"); you may not use this file except in compliance
+-- with the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+ 
+ select lstg_format_name from test_kylin_fact order by case
+                                              
+                                                           when 1=1  then
+                                              
+                                                             cal_dt
+                                              
+                                                          ELSE
+                                              
+                                                             seller_id
+                                              
+                                                         end 
+                                              
+ 
+ 
+ 

http://git-wip-us.apache.org/repos/asf/kylin/blob/f982a27c/kylin-it/src/test/resources/query/sql_verifyCount/query09.sql.expected
----------------------------------------------------------------------
diff --git a/kylin-it/src/test/resources/query/sql_verifyCount/query09.sql.expected b/kylin-it/src/test/resources/query/sql_verifyCount/query09.sql.expected
new file mode 100644
index 0000000..dbab0e0
--- /dev/null
+++ b/kylin-it/src/test/resources/query/sql_verifyCount/query09.sql.expected
@@ -0,0 +1,2 @@
+*
+1

http://git-wip-us.apache.org/repos/asf/kylin/blob/f982a27c/kylin-it/src/test/resources/query/sql_verifyCount/query10.sql
----------------------------------------------------------------------
diff --git a/kylin-it/src/test/resources/query/sql_verifyCount/query10.sql b/kylin-it/src/test/resources/query/sql_verifyCount/query10.sql
new file mode 100644
index 0000000..9435f6d
--- /dev/null
+++ b/kylin-it/src/test/resources/query/sql_verifyCount/query10.sql
@@ -0,0 +1,34 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one
+-- or more contributor license agreements.  See the NOTICE file
+-- distributed with this work for additional information
+-- regarding copyright ownership.  The ASF licenses this file
+-- to you under the Apache License, Version 2.0 (the
+-- "License"); you may not use this file except in compliance
+-- with the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+ 
+ select  * from test_kylin_fact order by case
+                                              
+                                                           when 1=1  then
+                                              
+                                                             cal_dt
+                                              
+                                                          ELSE
+                                              
+                                                             seller_id
+                                              
+                                                         end  desc
+                                              
+ 
+ 
+ 

http://git-wip-us.apache.org/repos/asf/kylin/blob/f982a27c/kylin-it/src/test/resources/query/sql_verifyCount/query10.sql.expected
----------------------------------------------------------------------
diff --git a/kylin-it/src/test/resources/query/sql_verifyCount/query10.sql.expected b/kylin-it/src/test/resources/query/sql_verifyCount/query10.sql.expected
new file mode 100644
index 0000000..11636e7
--- /dev/null
+++ b/kylin-it/src/test/resources/query/sql_verifyCount/query10.sql.expected
@@ -0,0 +1,2 @@
+*
+13

http://git-wip-us.apache.org/repos/asf/kylin/blob/f982a27c/kylin-it/src/test/resources/query/sql_verifyCount/query11.sql
----------------------------------------------------------------------
diff --git a/kylin-it/src/test/resources/query/sql_verifyCount/query11.sql b/kylin-it/src/test/resources/query/sql_verifyCount/query11.sql
new file mode 100644
index 0000000..84a6710
--- /dev/null
+++ b/kylin-it/src/test/resources/query/sql_verifyCount/query11.sql
@@ -0,0 +1,36 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one
+-- or more contributor license agreements.  See the NOTICE file
+-- distributed with this work for additional information
+-- regarding copyright ownership.  The ASF licenses this file
+-- to you under the Apache License, Version 2.0 (the
+-- "License"); you may not use this file except in compliance
+-- with the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+ 
+ select  * from test_kylin_fact where case
+                                              
+                                                           when cal_dt is null then
+                                              
+                                                             date'2010-01-01'
+                                              
+                                                          ELSE
+                                              
+                                                                cal_dt
+                                              
+                                                         end  
+                                                         
+                                                         < date '2013-01-01'
+                                              limit 10
+ 
+ 
+ 

http://git-wip-us.apache.org/repos/asf/kylin/blob/f982a27c/kylin-it/src/test/resources/query/sql_verifyCount/query11.sql.expected
----------------------------------------------------------------------
diff --git a/kylin-it/src/test/resources/query/sql_verifyCount/query11.sql.expected b/kylin-it/src/test/resources/query/sql_verifyCount/query11.sql.expected
new file mode 100644
index 0000000..093e327
--- /dev/null
+++ b/kylin-it/src/test/resources/query/sql_verifyCount/query11.sql.expected
@@ -0,0 +1,2 @@
+10
+13