kylin-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From liy...@apache.org
Subject [2/4] kylin git commit: KYLIN-1971 refactor query module to use new TblColRef
Date Mon, 24 Oct 2016 07:14:56 GMT
KYLIN-1971 refactor query module to use new TblColRef


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

Branch: refs/heads/KYLIN-1971
Commit: 06841bfcdf1833b5bf7e3945d93969e17888f43a
Parents: eed5a5d
Author: Yang Li <liyang@apache.org>
Authored: Mon Oct 24 08:24:02 2016 +0800
Committer: Yang Li <liyang@apache.org>
Committed: Mon Oct 24 08:24:02 2016 +0800

----------------------------------------------------------------------
 .../apache/kylin/metadata/model/ColumnDesc.java |  2 ++
 .../kylin/metadata/model/DataModelDesc.java     |  3 +-
 .../apache/kylin/metadata/model/TableRef.java   | 15 ++++++---
 .../apache/kylin/metadata/model/TblColRef.java  | 30 +++++++++++++++++-
 .../kylin/query/relnode/OLAPAggregateRel.java   | 11 ++-----
 .../apache/kylin/query/relnode/OLAPContext.java | 21 ++++++++-----
 .../kylin/query/relnode/OLAPFilterRel.java      |  1 +
 .../apache/kylin/query/relnode/OLAPJoinRel.java |  1 +
 .../kylin/query/relnode/OLAPLimitRel.java       |  1 +
 .../kylin/query/relnode/OLAPProjectRel.java     |  1 +
 .../org/apache/kylin/query/relnode/OLAPRel.java | 30 ++++++++++++++++++
 .../apache/kylin/query/relnode/OLAPSortRel.java |  1 +
 .../kylin/query/relnode/OLAPTableScan.java      | 30 ++++++++++++------
 .../relnode/OLAPToEnumerableConverter.java      | 33 ++++++++++++++------
 .../kylin/query/relnode/OLAPUnionRel.java       |  1 +
 .../kylin/query/relnode/OLAPWindowRel.java      |  5 ++-
 .../apache/kylin/query/schema/OLAPTable.java    |  6 ----
 17 files changed, 142 insertions(+), 50 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/kylin/blob/06841bfc/core-metadata/src/main/java/org/apache/kylin/metadata/model/ColumnDesc.java
----------------------------------------------------------------------
diff --git a/core-metadata/src/main/java/org/apache/kylin/metadata/model/ColumnDesc.java b/core-metadata/src/main/java/org/apache/kylin/metadata/model/ColumnDesc.java
index 1b19b50..772297f 100644
--- a/core-metadata/src/main/java/org/apache/kylin/metadata/model/ColumnDesc.java
+++ b/core-metadata/src/main/java/org/apache/kylin/metadata/model/ColumnDesc.java
@@ -58,6 +58,8 @@ public class ColumnDesc implements Serializable {
     public ColumnDesc() { // default constructor for Jackson
     }
 
+    /** Use TableRef.getColumn() instead */
+    @Deprecated
     public TblColRef getRef() {
         if (ref == null) {
             ref = new TblColRef(this);

http://git-wip-us.apache.org/repos/asf/kylin/blob/06841bfc/core-metadata/src/main/java/org/apache/kylin/metadata/model/DataModelDesc.java
----------------------------------------------------------------------
diff --git a/core-metadata/src/main/java/org/apache/kylin/metadata/model/DataModelDesc.java
b/core-metadata/src/main/java/org/apache/kylin/metadata/model/DataModelDesc.java
index f23e1be..a7734f1 100644
--- a/core-metadata/src/main/java/org/apache/kylin/metadata/model/DataModelDesc.java
+++ b/core-metadata/src/main/java/org/apache/kylin/metadata/model/DataModelDesc.java
@@ -287,7 +287,8 @@ public class DataModelDesc extends RootPersistentEntity {
         if (tables.containsKey(factTable) == false)
             throw new IllegalStateException("Fact table does not exist:" + factTable);
         
-        factTableRef = new TableRef(this, "FACT", tables.get(factTable));
+        TableDesc factDesc = tables.get(factTable);
+        factTableRef = new TableRef(this, factDesc.getName(), factDesc);
         addAlias(factTableRef);
         
         for (LookupDesc lookup : lookups) {

http://git-wip-us.apache.org/repos/asf/kylin/blob/06841bfc/core-metadata/src/main/java/org/apache/kylin/metadata/model/TableRef.java
----------------------------------------------------------------------
diff --git a/core-metadata/src/main/java/org/apache/kylin/metadata/model/TableRef.java b/core-metadata/src/main/java/org/apache/kylin/metadata/model/TableRef.java
index 975094c..186ff35 100644
--- a/core-metadata/src/main/java/org/apache/kylin/metadata/model/TableRef.java
+++ b/core-metadata/src/main/java/org/apache/kylin/metadata/model/TableRef.java
@@ -26,10 +26,10 @@ import com.google.common.collect.Maps;
 
 public class TableRef {
 
-    final DataModelDesc model;
-    final String alias;
-    final TableDesc table;
-    final Map<String, TblColRef> columns;
+    final private DataModelDesc model;
+    final private String alias;
+    final private TableDesc table;
+    final private Map<String, TblColRef> columns;
 
     TableRef(DataModelDesc model, String alias, TableDesc table) {
         this.model = model;
@@ -70,6 +70,13 @@ public class TableRef {
         return Collections.unmodifiableCollection(columns.values());
     }
     
+    public TblColRef makeFakeColumn(String name) {
+        ColumnDesc colDesc = new ColumnDesc();
+        colDesc.setName(name);
+        colDesc.setTable(table);
+        return new TblColRef(this, colDesc);
+    }
+    
     @Override
     public boolean equals(Object o) {
         if (this == o)

http://git-wip-us.apache.org/repos/asf/kylin/blob/06841bfc/core-metadata/src/main/java/org/apache/kylin/metadata/model/TblColRef.java
----------------------------------------------------------------------
diff --git a/core-metadata/src/main/java/org/apache/kylin/metadata/model/TblColRef.java b/core-metadata/src/main/java/org/apache/kylin/metadata/model/TblColRef.java
index 5f0fe94..2d45e35 100644
--- a/core-metadata/src/main/java/org/apache/kylin/metadata/model/TblColRef.java
+++ b/core-metadata/src/main/java/org/apache/kylin/metadata/model/TblColRef.java
@@ -23,6 +23,8 @@ import java.io.Serializable;
 import org.apache.commons.lang.StringUtils;
 import org.apache.kylin.metadata.datatype.DataType;
 
+import static com.google.common.base.Preconditions.*;
+
 /**
  */
 @SuppressWarnings("serial")
@@ -60,6 +62,24 @@ public class TblColRef implements Serializable {
         colRef.markInnerColumn(dataType);
         return colRef;
     }
+    
+    private static final DataModelDesc UNKNOWN_MODEL = new DataModelDesc();
+    
+    public static TableRef tableForUnknownModel(String tempTableAlias, TableDesc table) {
+        return new TableRef(UNKNOWN_MODEL, tempTableAlias, table);
+    }
+    
+    public static TblColRef columnForUnknownModel(TableRef table, ColumnDesc colDesc) {
+        checkArgument(table.getModel() == UNKNOWN_MODEL);
+        return new TblColRef(table, colDesc);
+    }
+    
+    public static void fixUnknownModel(DataModelDesc model, String alias, TblColRef col)
{
+        checkArgument(col.table.getModel() == UNKNOWN_MODEL || col.table.getModel() == model);
+        TableRef tableRef = model.findTable(alias);
+        checkArgument(tableRef.getTableDesc() == col.column.getTable());
+        col.table = tableRef;
+    }
 
     // for test only
     public static TblColRef mockup(TableDesc table, int oneBasedColumnIndex, String name,
String datatype) {
@@ -82,6 +102,7 @@ public class TblColRef implements Serializable {
     }
     
     TblColRef(TableRef table, ColumnDesc column) {
+        checkArgument(table.getTableDesc() == column.getTable());
         this.table = table;
         this.column = column;
     }
@@ -143,6 +164,8 @@ public class TblColRef implements Serializable {
 
     @Override
     public int hashCode() {
+        // NOTE: tableRef MUST NOT participate in hashCode().
+        // Because fixUnknownModel() can change tableRef while TblColRef is held as set/map
keys.
         final int prime = 31;
         int result = 1;
 
@@ -169,6 +192,11 @@ public class TblColRef implements Serializable {
 
     @Override
     public String toString() {
-        return (column.getTable() == null ? null : column.getTable().getIdentity()) + "."
+ column.getName();
+        String alias = table == null ? "UNKNOWN-MODEL" : table.getAlias();
+        if (alias.equals(column.getTable().getName())) {
+            return column.getTable().getIdentity() + "." + column.getName();
+        } else {
+            return alias + ":" + column.getTable().getIdentity() + "." + column.getName();
+        }
     }
 }

http://git-wip-us.apache.org/repos/asf/kylin/blob/06841bfc/query/src/main/java/org/apache/kylin/query/relnode/OLAPAggregateRel.java
----------------------------------------------------------------------
diff --git a/query/src/main/java/org/apache/kylin/query/relnode/OLAPAggregateRel.java b/query/src/main/java/org/apache/kylin/query/relnode/OLAPAggregateRel.java
index 9c4f287..d73206a 100644
--- a/query/src/main/java/org/apache/kylin/query/relnode/OLAPAggregateRel.java
+++ b/query/src/main/java/org/apache/kylin/query/relnode/OLAPAggregateRel.java
@@ -56,11 +56,9 @@ import org.apache.calcite.sql.validate.SqlUserDefinedAggFunction;
 import org.apache.calcite.util.ImmutableBitSet;
 import org.apache.calcite.util.Util;
 import org.apache.kylin.measure.MeasureTypeFactory;
-import org.apache.kylin.metadata.model.ColumnDesc;
 import org.apache.kylin.metadata.model.FunctionDesc;
 import org.apache.kylin.metadata.model.MeasureDesc;
 import org.apache.kylin.metadata.model.ParameterDesc;
-import org.apache.kylin.metadata.model.TableDesc;
 import org.apache.kylin.metadata.model.TblColRef;
 import org.apache.kylin.metadata.realization.SQLDigest.SQLCall;
 import org.apache.kylin.query.schema.OLAPTable;
@@ -147,7 +145,7 @@ public class OLAPAggregateRel extends Aggregate implements OLAPRel {
 
     @Override
     public void implementOLAP(OLAPImplementor implementor) {
-
+        implementor.fixSharedOlapTableScan(this);
         implementor.visitChild(getInput(), this);
 
         this.context = implementor.getContext();
@@ -213,11 +211,8 @@ public class OLAPAggregateRel extends Aggregate implements OLAPRel {
     private TblColRef buildRewriteColumn(FunctionDesc aggFunc) {
         TblColRef colRef;
         if (aggFunc.needRewriteField()) {
-            ColumnDesc column = new ColumnDesc();
-            column.setName(aggFunc.getRewriteFieldName());
-            TableDesc table = this.context.firstTableScan.getOlapTable().getSourceTable();
-            column.setTable(table);
-            colRef = column.getRef();
+            String colName = aggFunc.getRewriteFieldName();
+            colRef = this.context.firstTableScan.makeRewriteColumn(colName);
         } else {
             throw new IllegalStateException("buildRewriteColumn on a aggrFunc that does not
need rewrite " + aggFunc);
         }

http://git-wip-us.apache.org/repos/asf/kylin/blob/06841bfc/query/src/main/java/org/apache/kylin/query/relnode/OLAPContext.java
----------------------------------------------------------------------
diff --git a/query/src/main/java/org/apache/kylin/query/relnode/OLAPContext.java b/query/src/main/java/org/apache/kylin/query/relnode/OLAPContext.java
index b368b5d..d7f68ac 100644
--- a/query/src/main/java/org/apache/kylin/query/relnode/OLAPContext.java
+++ b/query/src/main/java/org/apache/kylin/query/relnode/OLAPContext.java
@@ -108,6 +108,7 @@ public class OLAPContext {
     // query info
     public OLAPSchema olapSchema = null;
     public OLAPTableScan firstTableScan = null; // to be fact table scan except "select *
from lookupTable"
+    public Set<OLAPTableScan> allTableScans = new HashSet<>();
     public TupleInfo returnTupleInfo = null;
     public boolean afterAggregate = false;
     public boolean afterSkippedFilter = false;
@@ -117,19 +118,19 @@ public class OLAPContext {
     // cube metadata
     public IRealization realization;
 
-    public Set<TblColRef> allColumns = new HashSet<TblColRef>();
-    public List<TblColRef> groupByColumns = new ArrayList<TblColRef>();
-    public Set<TblColRef> metricsColumns = new HashSet<TblColRef>();
-    public List<FunctionDesc> aggregations = new ArrayList<FunctionDesc>(); //
storage level measure type, on top of which various sql aggr function may apply
-    public List<SQLCall> aggrSqlCalls = new ArrayList<SQLCall>(); // sql level
aggregation function call
-    public Set<TblColRef> filterColumns = new HashSet<TblColRef>();
+    public Set<TblColRef> allColumns = new HashSet<>();
+    public List<TblColRef> groupByColumns = new ArrayList<>();
+    public Set<TblColRef> metricsColumns = new HashSet<>();
+    public List<FunctionDesc> aggregations = new ArrayList<>(); // storage level
measure type, on top of which various sql aggr function may apply
+    public List<SQLCall> aggrSqlCalls = new ArrayList<>(); // sql level aggregation
function call
+    public Set<TblColRef> filterColumns = new HashSet<>();
     public TupleFilter filter;
-    public List<JoinDesc> joins = new LinkedList<JoinDesc>();
+    public List<JoinDesc> joins = new LinkedList<>();
     private List<TblColRef> sortColumns;
     private List<SQLDigest.OrderEnum> sortOrders;
 
     // rewrite info
-    public Map<String, RelDataType> rewriteFields = new HashMap<String, RelDataType>();
+    public Map<String, RelDataType> rewriteFields = new HashMap<>();
 
     public int limit;
 
@@ -175,6 +176,10 @@ public class OLAPContext {
             sortOrders.add(order);
         }
     }
+    
+    // ============================================================================
+    
+    
 
     public interface IAccessController {
         /*

http://git-wip-us.apache.org/repos/asf/kylin/blob/06841bfc/query/src/main/java/org/apache/kylin/query/relnode/OLAPFilterRel.java
----------------------------------------------------------------------
diff --git a/query/src/main/java/org/apache/kylin/query/relnode/OLAPFilterRel.java b/query/src/main/java/org/apache/kylin/query/relnode/OLAPFilterRel.java
index b34cc6f..8b2035b 100644
--- a/query/src/main/java/org/apache/kylin/query/relnode/OLAPFilterRel.java
+++ b/query/src/main/java/org/apache/kylin/query/relnode/OLAPFilterRel.java
@@ -289,6 +289,7 @@ public class OLAPFilterRel extends Filter implements OLAPRel {
 
     @Override
     public void implementOLAP(OLAPImplementor implementor) {
+        implementor.fixSharedOlapTableScan(this);
         implementor.visitChild(getInput(), this);
 
         this.columnRowType = buildColumnRowType();

http://git-wip-us.apache.org/repos/asf/kylin/blob/06841bfc/query/src/main/java/org/apache/kylin/query/relnode/OLAPJoinRel.java
----------------------------------------------------------------------
diff --git a/query/src/main/java/org/apache/kylin/query/relnode/OLAPJoinRel.java b/query/src/main/java/org/apache/kylin/query/relnode/OLAPJoinRel.java
index 2a143fb..be7d263 100644
--- a/query/src/main/java/org/apache/kylin/query/relnode/OLAPJoinRel.java
+++ b/query/src/main/java/org/apache/kylin/query/relnode/OLAPJoinRel.java
@@ -121,6 +121,7 @@ public class OLAPJoinRel extends EnumerableJoin implements OLAPRel {
         boolean rightHasSubquery = false;
 
         // as we keep the first table as fact table, we need to visit from left to right
+        implementor.fixSharedOlapTableScan(this);
         implementor.visitChild(this.left, this);
         if (this.context != implementor.getContext() || ((OLAPRel) this.left).hasSubQuery())
{
             this.hasSubQuery = true;

http://git-wip-us.apache.org/repos/asf/kylin/blob/06841bfc/query/src/main/java/org/apache/kylin/query/relnode/OLAPLimitRel.java
----------------------------------------------------------------------
diff --git a/query/src/main/java/org/apache/kylin/query/relnode/OLAPLimitRel.java b/query/src/main/java/org/apache/kylin/query/relnode/OLAPLimitRel.java
index 988d1fa..d6569f6 100644
--- a/query/src/main/java/org/apache/kylin/query/relnode/OLAPLimitRel.java
+++ b/query/src/main/java/org/apache/kylin/query/relnode/OLAPLimitRel.java
@@ -71,6 +71,7 @@ public class OLAPLimitRel extends SingleRel implements OLAPRel {
 
     @Override
     public void implementOLAP(OLAPImplementor implementor) {
+        implementor.fixSharedOlapTableScan(this);
         implementor.visitChild(getInput(), this);
 
         this.columnRowType = buildColumnRowType();

http://git-wip-us.apache.org/repos/asf/kylin/blob/06841bfc/query/src/main/java/org/apache/kylin/query/relnode/OLAPProjectRel.java
----------------------------------------------------------------------
diff --git a/query/src/main/java/org/apache/kylin/query/relnode/OLAPProjectRel.java b/query/src/main/java/org/apache/kylin/query/relnode/OLAPProjectRel.java
index db6ec2d..03b9ddd 100644
--- a/query/src/main/java/org/apache/kylin/query/relnode/OLAPProjectRel.java
+++ b/query/src/main/java/org/apache/kylin/query/relnode/OLAPProjectRel.java
@@ -107,6 +107,7 @@ public class OLAPProjectRel extends Project implements OLAPRel {
 
     @Override
     public void implementOLAP(OLAPImplementor implementor) {
+        implementor.fixSharedOlapTableScan(this);
         implementor.visitChild(getInput(), this);
 
         this.context = implementor.getContext();

http://git-wip-us.apache.org/repos/asf/kylin/blob/06841bfc/query/src/main/java/org/apache/kylin/query/relnode/OLAPRel.java
----------------------------------------------------------------------
diff --git a/query/src/main/java/org/apache/kylin/query/relnode/OLAPRel.java b/query/src/main/java/org/apache/kylin/query/relnode/OLAPRel.java
index f2b52a7..a24b447 100644
--- a/query/src/main/java/org/apache/kylin/query/relnode/OLAPRel.java
+++ b/query/src/main/java/org/apache/kylin/query/relnode/OLAPRel.java
@@ -29,7 +29,9 @@ import org.apache.calcite.adapter.enumerable.EnumerableRelImplementor;
 import org.apache.calcite.plan.Convention;
 import org.apache.calcite.plan.RelTrait;
 import org.apache.calcite.plan.RelTraitSet;
+import org.apache.calcite.rel.BiRel;
 import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.SingleRel;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -101,6 +103,34 @@ public interface OLAPRel extends RelNode {
             ctxStack.push(context);
             OLAPContext.registerContext(context);
         }
+
+        public void fixSharedOlapTableScan(OLAPRel parent) {
+            if (parent instanceof SingleRel) {
+                SingleRel single = (SingleRel) parent;
+                OLAPTableScan copy = copyTableScanIfNeeded(single.getInput());
+                if (copy != null)
+                    single.replaceInput(0, copy);
+            } else if (parent instanceof BiRel) {
+                BiRel bi = (BiRel) parent;
+                OLAPTableScan copyLeft = copyTableScanIfNeeded(bi.getLeft());
+                if (copyLeft != null)
+                    bi.replaceInput(0, copyLeft);
+                OLAPTableScan copyRight = copyTableScanIfNeeded(bi.getRight());
+                if (copyRight != null)
+                    bi.replaceInput(1, copyRight);
+            }
+        }
+
+        private OLAPTableScan copyTableScanIfNeeded(RelNode input) {
+            if (input instanceof OLAPTableScan) {
+                OLAPTableScan tableScan = (OLAPTableScan) input;
+                if (tableScan.getColumnRowType() != null) { // implementedOLAP() was done,
meaning it is shared
+                    OLAPTableScan copy = (OLAPTableScan) tableScan.copy(tableScan.getTraitSet(),
tableScan.getInputs());
+                    return copy;
+                }
+            }
+            return null;
+        }
     }
 
     public void implementOLAP(OLAPImplementor implementor);

http://git-wip-us.apache.org/repos/asf/kylin/blob/06841bfc/query/src/main/java/org/apache/kylin/query/relnode/OLAPSortRel.java
----------------------------------------------------------------------
diff --git a/query/src/main/java/org/apache/kylin/query/relnode/OLAPSortRel.java b/query/src/main/java/org/apache/kylin/query/relnode/OLAPSortRel.java
index 70fc3e3..03ba9c5 100644
--- a/query/src/main/java/org/apache/kylin/query/relnode/OLAPSortRel.java
+++ b/query/src/main/java/org/apache/kylin/query/relnode/OLAPSortRel.java
@@ -65,6 +65,7 @@ public class OLAPSortRel extends Sort implements OLAPRel {
 
     @Override
     public void implementOLAP(OLAPImplementor implementor) {
+        implementor.fixSharedOlapTableScan(this);
         implementor.visitChild(getInput(), this);
 
         this.context = implementor.getContext();

http://git-wip-us.apache.org/repos/asf/kylin/blob/06841bfc/query/src/main/java/org/apache/kylin/query/relnode/OLAPTableScan.java
----------------------------------------------------------------------
diff --git a/query/src/main/java/org/apache/kylin/query/relnode/OLAPTableScan.java b/query/src/main/java/org/apache/kylin/query/relnode/OLAPTableScan.java
index 381ced4..aa70dbc 100644
--- a/query/src/main/java/org/apache/kylin/query/relnode/OLAPTableScan.java
+++ b/query/src/main/java/org/apache/kylin/query/relnode/OLAPTableScan.java
@@ -58,6 +58,8 @@ import org.apache.calcite.rel.type.RelDataType;
 import org.apache.calcite.rel.type.RelDataTypeFactory;
 import org.apache.calcite.rel.type.RelDataTypeField;
 import org.apache.kylin.metadata.model.ColumnDesc;
+import org.apache.kylin.metadata.model.DataModelDesc;
+import org.apache.kylin.metadata.model.TableRef;
 import org.apache.kylin.metadata.model.TblColRef;
 import org.apache.kylin.query.optrule.AggregateMultipleExpandRule;
 import org.apache.kylin.query.optrule.AggregateProjectReduceRule;
@@ -199,12 +201,15 @@ public class OLAPTableScan extends TableScan implements OLAPRel, EnumerableRel
{
 
     @Override
     public void implementOLAP(OLAPImplementor implementor) {
+        Preconditions.checkState(columnRowType == null, "OLAPTableScan MUST NOT be shared
by more than one prent");
+        
         // create context in case of non-join
         if (implementor.getContext() == null || !(implementor.getParentNode() instanceof
OLAPJoinRel)) {
             implementor.allocateContext();
         }
         columnRowType = buildColumnRowType();
         context = implementor.getContext();
+        context.allTableScans.add(this);
 
         if (context.olapSchema == null) {
             OLAPSchema schema = olapTable.getSchema();
@@ -218,13 +223,28 @@ public class OLAPTableScan extends TableScan implements OLAPRel, EnumerableRel
{
     }
 
     private ColumnRowType buildColumnRowType() {
+        String tmpAlias = Integer.toHexString(System.identityHashCode(this));
+        TableRef tableRef = TblColRef.tableForUnknownModel(tmpAlias, olapTable.getSourceTable());
+        
         List<TblColRef> columns = new ArrayList<TblColRef>();
         for (ColumnDesc sourceColumn : olapTable.getExposedColumns()) {
-            TblColRef colRef = sourceColumn.getRef();
+            TblColRef colRef = TblColRef.columnForUnknownModel(tableRef, sourceColumn);
             columns.add(colRef);
         }
         return new ColumnRowType(columns);
     }
+    
+    public TblColRef makeRewriteColumn(String name) {
+        TableRef tableRef = columnRowType.getColumnByIndex(0).getTableRef();
+        return tableRef.makeFakeColumn(name);
+    }
+    
+    public void fixColumnRowTypeWithModel(DataModelDesc model) {
+        TableRef tableRef = model.findFirstTable(olapTable.getTableName());
+        for (TblColRef col : columnRowType.getAllColumns()) {
+            TblColRef.fixUnknownModel(model, tableRef.getAlias(), col);
+        }
+    }
 
     @Override
     public EnumerableRel implementEnumerable(List<EnumerableRel> inputs) {
@@ -257,17 +277,9 @@ public class OLAPTableScan extends TableScan implements OLAPRel, EnumerableRel
{
         return columnRowType;
     }
 
-    /**
-     * Because OLAPTableScan is reused for the same table, we can't use
-     * this.context and have to use parent context
-     */
     @Override
     public void implementRewrite(RewriteImplementor implementor) {
         Map<String, RelDataType> rewriteFields = this.context.rewriteFields;
-        if (implementor.getParentContext() != null) {
-            rewriteFields = implementor.getParentContext().rewriteFields;
-        }
-
         for (Map.Entry<String, RelDataType> rewriteField : rewriteFields.entrySet())
{
             String fieldName = rewriteField.getKey();
             RelDataTypeField field = rowType.getField(fieldName, true, false);

http://git-wip-us.apache.org/repos/asf/kylin/blob/06841bfc/query/src/main/java/org/apache/kylin/query/relnode/OLAPToEnumerableConverter.java
----------------------------------------------------------------------
diff --git a/query/src/main/java/org/apache/kylin/query/relnode/OLAPToEnumerableConverter.java
b/query/src/main/java/org/apache/kylin/query/relnode/OLAPToEnumerableConverter.java
index 55c27d3..aa01086 100644
--- a/query/src/main/java/org/apache/kylin/query/relnode/OLAPToEnumerableConverter.java
+++ b/query/src/main/java/org/apache/kylin/query/relnode/OLAPToEnumerableConverter.java
@@ -45,6 +45,7 @@ import org.apache.kylin.metadata.filter.ColumnTupleFilter;
 import org.apache.kylin.metadata.filter.LogicalTupleFilter;
 import org.apache.kylin.metadata.filter.TupleFilter;
 import org.apache.kylin.metadata.filter.TupleFilter.FilterOperatorEnum;
+import org.apache.kylin.metadata.model.DataModelDesc;
 import org.apache.kylin.metadata.model.TblColRef;
 import org.apache.kylin.metadata.realization.IRealization;
 import org.apache.kylin.query.routing.NoRealizationFoundException;
@@ -91,16 +92,8 @@ public class OLAPToEnumerableConverter extends ConverterImpl implements
Enumerab
                 IRealization realization = QueryRouter.selectRealization(context);
                 context.realization = realization;
 
-                String controllerCls = KylinConfig.getInstanceFromEnv().getQueryAccessController();
-                if (null != controllerCls && !controllerCls.isEmpty()) {
-                    OLAPContext.IAccessController accessController = (OLAPContext.IAccessController)
ClassUtil.newInstance(controllerCls);
-                    TupleFilter tupleFilter = accessController.check(context.olapAuthen,
context.allColumns, context.realization);
-                    if (null != tupleFilter) {
-                        context.filterColumns.addAll(collectColumns(tupleFilter));
-                        context.allColumns.addAll(collectColumns(tupleFilter));
-                        context.filter = and(context.filter, tupleFilter);
-                    }
-                }
+                fixModel(context);
+                doAccessControl(context);
             }
         } catch (NoRealizationFoundException e) {
             OLAPContext ctx0 = (OLAPContext) OLAPContext.getThreadLocalContexts().toArray()[0];
@@ -130,6 +123,26 @@ public class OLAPToEnumerableConverter extends ConverterImpl implements
Enumerab
         return impl.visitChild(this, 0, inputAsEnum, pref);
     }
 
+    private void fixModel(OLAPContext context) {
+        DataModelDesc model = context.realization.getDataModelDesc();
+        for (OLAPTableScan tableScan : context.allTableScans) {
+            tableScan.fixColumnRowTypeWithModel(model);
+        }
+    }
+
+    private void doAccessControl(OLAPContext context) {
+        String controllerCls = KylinConfig.getInstanceFromEnv().getQueryAccessController();
+        if (null != controllerCls && !controllerCls.isEmpty()) {
+            OLAPContext.IAccessController accessController = (OLAPContext.IAccessController)
ClassUtil.newInstance(controllerCls);
+            TupleFilter tupleFilter = accessController.check(context.olapAuthen, context.allColumns,
context.realization);
+            if (null != tupleFilter) {
+                context.filterColumns.addAll(collectColumns(tupleFilter));
+                context.allColumns.addAll(collectColumns(tupleFilter));
+                context.filter = and(context.filter, tupleFilter);
+            }
+        }
+    }
+
     private TupleFilter and(TupleFilter f1, TupleFilter f2) {
         if (f1 == null)
             return f2;

http://git-wip-us.apache.org/repos/asf/kylin/blob/06841bfc/query/src/main/java/org/apache/kylin/query/relnode/OLAPUnionRel.java
----------------------------------------------------------------------
diff --git a/query/src/main/java/org/apache/kylin/query/relnode/OLAPUnionRel.java b/query/src/main/java/org/apache/kylin/query/relnode/OLAPUnionRel.java
index 02c3967..adcf27f 100644
--- a/query/src/main/java/org/apache/kylin/query/relnode/OLAPUnionRel.java
+++ b/query/src/main/java/org/apache/kylin/query/relnode/OLAPUnionRel.java
@@ -71,6 +71,7 @@ public class OLAPUnionRel extends Union implements OLAPRel {
 
     @Override
     public void implementOLAP(OLAPImplementor implementor) {
+        implementor.fixSharedOlapTableScan(this);
         for (RelNode child : getInputs()) {
             implementor.visitChild(child, this);
         }

http://git-wip-us.apache.org/repos/asf/kylin/blob/06841bfc/query/src/main/java/org/apache/kylin/query/relnode/OLAPWindowRel.java
----------------------------------------------------------------------
diff --git a/query/src/main/java/org/apache/kylin/query/relnode/OLAPWindowRel.java b/query/src/main/java/org/apache/kylin/query/relnode/OLAPWindowRel.java
index 1ee0348..c2ea4e2 100644
--- a/query/src/main/java/org/apache/kylin/query/relnode/OLAPWindowRel.java
+++ b/query/src/main/java/org/apache/kylin/query/relnode/OLAPWindowRel.java
@@ -72,9 +72,8 @@ public class OLAPWindowRel extends Window implements OLAPRel {
 
     @Override
     public void implementOLAP(OLAPImplementor implementor) {
-        for (RelNode child : getInputs()) {
-            implementor.visitChild(child, this);
-        }
+        implementor.fixSharedOlapTableScan(this);
+        implementor.visitChild(getInput(), this);
 
         this.columnRowType = buildColumnRowType();
         this.context = implementor.getContext();

http://git-wip-us.apache.org/repos/asf/kylin/blob/06841bfc/query/src/main/java/org/apache/kylin/query/schema/OLAPTable.java
----------------------------------------------------------------------
diff --git a/query/src/main/java/org/apache/kylin/query/schema/OLAPTable.java b/query/src/main/java/org/apache/kylin/query/schema/OLAPTable.java
index b9d2fd0..4994433 100644
--- a/query/src/main/java/org/apache/kylin/query/schema/OLAPTable.java
+++ b/query/src/main/java/org/apache/kylin/query/schema/OLAPTable.java
@@ -80,12 +80,6 @@ public class OLAPTable extends AbstractQueryableTable implements TranslatableTab
         SQLTYPE_MAPPING.put("time", SqlTypeName.TIME);
         SQLTYPE_MAPPING.put("timestamp", SqlTypeName.TIMESTAMP);
         SQLTYPE_MAPPING.put("any", SqlTypeName.ANY);
-
-        // try {
-        // Class.forName("org.apache.hive.jdbc.HiveDriver");
-        // } catch (ClassNotFoundException e) {
-        // e.printStackTrace();
-        // }
     }
 
     private final OLAPSchema olapSchema;


Mime
View raw message