kylin-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From liy...@apache.org
Subject [8/8] kylin git commit: KYILN-2277 add test case
Date Wed, 14 Dec 2016 10:31:30 GMT
KYILN-2277 add test case


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

Branch: refs/heads/KYLIN-2277
Commit: e73b4f09754321baedba1ef5db9fb92e1ca0c15a
Parents: cb7c0ad
Author: Li Yang <liyang@apache.org>
Authored: Wed Dec 14 18:05:36 2016 +0800
Committer: Li Yang <liyang@apache.org>
Committed: Wed Dec 14 18:05:36 2016 +0800

----------------------------------------------------------------------
 .../calcite/sql2rel/SqlToRelConverter.java      | 38 ++++++++++++++------
 .../apache/kylin/query/ITKylinQueryTest.java    |  6 ++++
 .../org/apache/kylin/query/KylinTestBase.java   | 21 +++++++++++
 3 files changed, 54 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/kylin/blob/e73b4f09/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 f1e4074..cf36f61 100644
--- a/atopcalcite/src/main/java/org/apache/calcite/sql2rel/SqlToRelConverter.java
+++ b/atopcalcite/src/main/java/org/apache/calcite/sql2rel/SqlToRelConverter.java
@@ -531,6 +531,8 @@ public class SqlToRelConverter {
      *                        the query will be part of a view.
      */
     public RelRoot convertQuery(SqlNode query, final boolean needsValidation, final boolean
top) {
+        SqlNode origQuery = query; /* OVERRIDE POINT */
+        
         if (needsValidation) {
             query = validator.validate(query);
         }
@@ -555,15 +557,29 @@ public class SqlToRelConverter {
         }
 
         final RelDataType validatedRowType = validator.getValidatedNodeType(query);
-        return hackSelectStar(query, RelRoot.of(result, validatedRowType, query.getKind()).withCollation(collation));
+        return hackSelectStar(origQuery, RelRoot.of(result, validatedRowType, query.getKind()).withCollation(collation));
     }
 
     /* OVERRIDE POINT */
     private RelRoot hackSelectStar(SqlNode query, RelRoot root) {
-        if (!(root.rel instanceof LogicalProject))
+        /*
+         * Rel tree is like:
+         * 
+         *   LogicalSort (optional)
+         *    |- LogicalProject
+         *        |- OLAPTableScan
+         */
+        LogicalProject rootPrj = null;
+        LogicalSort rootSort = null;
+        if (root.rel instanceof LogicalProject) {
+            rootPrj = (LogicalProject) root.rel;
+        } else if (root.rel instanceof LogicalSort && root.rel.getInput(0) instanceof
LogicalProject) {
+            rootPrj = (LogicalProject) root.rel.getInput(0);
+            rootSort = (LogicalSort) root.rel;
+        } else {
             return root;
+        }
         
-        LogicalProject rootPrj = (LogicalProject) root.rel;
         if (!rootPrj.getInput().getClass().getSimpleName().equals("OLAPTableScan"))
             return root;
 
@@ -571,30 +587,30 @@ public class SqlToRelConverter {
         if (rootPrj.getRowType().getFieldCount() < scan.getRowType().getFieldCount())
             return root;
         
-        RelDataType inType = scan.getRowType();
+        RelDataType inType = rootPrj.getRowType();
         List<String> inFields = inType.getFieldNames();
         List<RexNode> projExp = new ArrayList<>();
         List<Pair<Integer, String>> projFields = new ArrayList<>();
         FieldInfoBuilder projTypeBuilder = getCluster().getTypeFactory().builder();
         FieldInfoBuilder validTypeBuilder = getCluster().getTypeFactory().builder();
-        SqlNodeList selectList = ((SqlSelect) query).getSelectList();
-        SqlNodeList newSelectList = new SqlNodeList(selectList.getParserPosition());
         for (int i = 0; i < inFields.size(); i++) {
             if (!inFields.get(i).startsWith("_KY_")) {
-                RexBuilder rexBuilder = getCluster().getRexBuilder();
-                projExp.add(rexBuilder.makeInputRef(scan, i));
+                projExp.add(rootPrj.getProjects().get(i));
                 projFields.add(Pair.of(projFields.size(), inFields.get(i)));
                 projTypeBuilder.add(inType.getFieldList().get(i));
                 validTypeBuilder.add(root.validatedRowType.getFieldList().get(i));
-                newSelectList.add(selectList.get(i));
             }
         }
 
         RelDataType projRowType = getCluster().getTypeFactory().createStructType(projTypeBuilder);
+        rootPrj = LogicalProject.create(scan, projExp, projRowType);
+        if (rootSort != null) {
+            rootSort = (LogicalSort) rootSort.copy(rootSort.getTraitSet(), rootPrj, rootSort.collation,
rootSort.offset, rootSort.fetch);
+        }
+        
         RelDataType validRowType = getCluster().getTypeFactory().createStructType(validTypeBuilder);
-        root = new RelRoot(LogicalProject.create(scan, projExp, projRowType), validRowType,
root.kind, projFields, root.collation);
+        root = new RelRoot(rootSort == null ? rootPrj : rootSort, validRowType, root.kind,
projFields, root.collation);
         
-        ((SqlSelect) query).setSelectList(newSelectList);
         validator.setValidatedNodeType(query, validRowType);
         
         return root;

http://git-wip-us.apache.org/repos/asf/kylin/blob/e73b4f09/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 d485955..7f04bbb 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
@@ -379,4 +379,10 @@ public class ITKylinQueryTest extends KylinTestBase {
         // compare the result
         Assert.assertEquals(expectVersion, queriedVersion);
     }
+    
+    @Test
+    public void testSelectStarColumnCount() throws Exception {
+        execAndCompColumnCount("select * from test_kylin_fact limit 10", 9);
+        execAndCompColumnCount("select * from test_kylin_fact", 9);
+    }
 }

http://git-wip-us.apache.org/repos/asf/kylin/blob/e73b4f09/kylin-it/src/test/java/org/apache/kylin/query/KylinTestBase.java
----------------------------------------------------------------------
diff --git a/kylin-it/src/test/java/org/apache/kylin/query/KylinTestBase.java b/kylin-it/src/test/java/org/apache/kylin/query/KylinTestBase.java
index bcf55e5..ddb996c 100644
--- a/kylin-it/src/test/java/org/apache/kylin/query/KylinTestBase.java
+++ b/kylin-it/src/test/java/org/apache/kylin/query/KylinTestBase.java
@@ -67,6 +67,7 @@ import org.dbunit.ext.h2.H2Connection;
 import org.dbunit.ext.h2.H2DataTypeFactory;
 import org.junit.Assert;
 
+import com.google.common.collect.ImmutableSet;
 import com.google.common.io.Files;
 
 /**
@@ -422,6 +423,26 @@ public class KylinTestBase {
         }
     }
 
+    protected void execAndCompColumnCount(String input, int expectedColumnCount) throws Exception
{
+        printInfo("---------- test column count: " + input);
+        Set<String> sqlSet = ImmutableSet.of(input);
+        
+        for (String sql : sqlSet) {
+            // execute Kylin
+            printInfo("Query Result from Kylin - " + sql);
+            IDatabaseConnection kylinConn = new DatabaseConnection(cubeConnection);
+            ITable kylinTable = executeQuery(kylinConn, sql, sql, false);
+            
+            try {
+                // compare the result
+                Assert.assertEquals(expectedColumnCount, kylinTable.getTableMetaData().getColumns().length);
+            } catch (Throwable t) {
+                printInfo("execAndCompColumnCount failed on: " + sql);
+                throw t;
+            }
+        }
+    }
+    
     protected void execLimitAndValidate(String queryFolder) throws Exception {
         printInfo("---------- test folder: " + new File(queryFolder).getAbsolutePath());
 


Mime
View raw message