org.apache.kylin.query.schema.OLAPTable Java Examples

The following examples show how to use org.apache.kylin.query.schema.OLAPTable. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TupleExpressionVisitor.java    From kylin with Apache License 2.0 6 votes vote down vote up
@Override
public TupleExpression visitLiteral(RexLiteral literal) {
    TupleExpression tuple;
    Object value = literal.getValue();

    DataType dataType = DataType.getType(OLAPTable.DATATYPE_MAPPING.get(literal.getType().getSqlTypeName()));
    if (value instanceof Number) {
        tuple = new ConstantTupleExpression(dataType, value);
    } else {
        if (value == null) {
            tuple = new ConstantTupleExpression(dataType, null);
        } else if (value instanceof NlsString) {
            tuple = new ConstantTupleExpression(dataType, ((NlsString) value).getValue());
        } else {
            tuple = new ConstantTupleExpression(dataType, value.toString());
        }
    }
    tuple.setDigest(literal.toString());
    return tuple;
}
 
Example #2
Source File: OLAPTableScan.java    From Kylin with Apache License 2.0 6 votes vote down vote up
@Override
public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
    if (!(implementor instanceof JavaImplementor))
        throw new IllegalStateException("implementor is not JavaImplementor");
    JavaImplementor javaImplementor = (JavaImplementor) implementor;

    int ctxId = this.context.id;
    if (javaImplementor.getParentContext() != null) {
        ctxId = javaImplementor.getParentContext().id;
    }

    PhysType physType = PhysTypeImpl.of(javaImplementor.getTypeFactory(), this.rowType, pref.preferArray());

    String execFunction = genExecFunc();

    return javaImplementor.result(physType, Blocks.toBlock(Expressions.call(table.getExpression(OLAPTable.class), execFunction, javaImplementor.getRootExpression(), Expressions.constant(ctxId))));
}
 
Example #3
Source File: LookupTableEnumerator.java    From Kylin with Apache License 2.0 6 votes vote down vote up
public LookupTableEnumerator(OLAPContext olapContext) {

        //TODO: assuming LookupTableEnumerator is handled by a cube
        CubeInstance cube = (CubeInstance) olapContext.realization;

        String lookupTableName = olapContext.firstTableScan.getTableName();
        DimensionDesc dim = cube.getDescriptor().findDimensionByTable(lookupTableName);
        if (dim == null)
            throw new IllegalStateException("No dimension with derived columns found for lookup table " + lookupTableName + ", cube desc " + cube.getDescriptor());

        CubeManager cubeMgr = CubeManager.getInstance(cube.getConfig());
        LookupStringTable table = cubeMgr.getLookupTable(cube.getLatestReadySegment(), dim);
        this.allRows = table.getAllRows();

        OLAPTable olapTable = (OLAPTable) olapContext.firstTableScan.getOlapTable();
        this.colDescs = olapTable.getExposedColumns();
        this.current = new Object[colDescs.size()];

        reset();
    }
 
Example #4
Source File: OLAPTableScan.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
@Override
public Result implement(EnumerableRelImplementor implementor, Prefer pref) {

    context.setReturnTupleInfo(rowType, columnRowType);
    String execFunction = genExecFunc();

    PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), getRowType(), JavaRowFormat.ARRAY);
    MethodCallExpression exprCall = Expressions.call(table.getExpression(OLAPTable.class), execFunction,
            implementor.getRootExpression(), Expressions.constant(context.id));
    return implementor.result(physType, Blocks.toBlock(exprCall));
}
 
Example #5
Source File: OLAPJoinRel.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
@Override
public Result implement(EnumerableRelImplementor implementor, Prefer pref) {

    context.setReturnTupleInfo(rowType, columnRowType);

    PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), getRowType(), pref.preferArray());
    RelOptTable factTable = context.firstTableScan.getTable();
    MethodCallExpression exprCall = Expressions.call(factTable.getExpression(OLAPTable.class), "executeOLAPQuery",
            implementor.getRootExpression(), Expressions.constant(context.id));
    return implementor.result(physType, Blocks.toBlock(exprCall));
}
 
Example #6
Source File: OLAPTableScan.java    From Kylin with Apache License 2.0 5 votes vote down vote up
public OLAPTableScan(RelOptCluster cluster, RelOptTable table, OLAPTable olapTable, int[] fields) {
    super(cluster, cluster.traitSetOf(OLAPRel.CONVENTION), table);
    this.olapTable = olapTable;
    this.fields = fields;
    this.tableName = olapTable.getTableName();
    this.rowType = getRowType();
}
 
Example #7
Source File: OLAPToEnumerableConverter.java    From Kylin with Apache License 2.0 5 votes vote down vote up
private Result buildHiveResult(EnumerableRelImplementor enumImplementor, Prefer pref, OLAPContext context) {
    RelDataType hiveRowType = getRowType();

    context.olapRowType = hiveRowType;
    PhysType physType = PhysTypeImpl.of(enumImplementor.getTypeFactory(), hiveRowType, pref.preferArray());

    RelOptTable factTable = context.firstTableScan.getTable();
    Result result = enumImplementor.result(physType, Blocks.toBlock(Expressions.call(factTable.getExpression(OLAPTable.class), "executeHiveQuery", enumImplementor.getRootExpression())));
    return result;
}
 
Example #8
Source File: OLAPJoinRel.java    From Kylin with Apache License 2.0 5 votes vote down vote up
@Override
public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
    Result result = null;
    if (this.hasSubQuery) {
        result = super.implement(implementor, pref);
    } else {
        PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), getRowType(), pref.preferArray());

        RelOptTable factTable = context.firstTableScan.getTable();
        result = implementor.result(physType, Blocks.toBlock(Expressions.call(factTable.getExpression(OLAPTable.class), "executeIndexQuery", implementor.getRootExpression(), Expressions.constant(context.id))));
    }

    return result;
}
 
Example #9
Source File: OLAPTableScan.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
public OLAPTableScan(RelOptCluster cluster, RelOptTable table, OLAPTable olapTable, int[] fields) {
    super(cluster, cluster.traitSetOf(OLAPRel.CONVENTION), table);
    this.olapTable = olapTable;
    this.fields = fields;
    this.tableName = olapTable.getTableName();
    this.rowType = getRowType();
    this.kylinConfig = KylinConfig.getInstanceFromEnv();
}
 
Example #10
Source File: KylinRelDataTypeSystemTest.java    From kylin with Apache License 2.0 5 votes vote down vote up
@Test
public void testIllegalDecimalType() {
    RelDataTypeSystem typeSystem = new KylinRelDataTypeSystem();
    RelDataTypeFactory typeFactory = new SqlTypeFactoryImpl(typeSystem);
    
    DataType dataType = DataType.getType("decimal(40, 10)");
    RelDataType relDataType = OLAPTable.createSqlType(typeFactory, dataType, true);
    
    Assert.assertTrue(relDataType instanceof BasicSqlType);
    Assert.assertEquals(relDataType.getSqlTypeName(), SqlTypeName.DECIMAL);
    Assert.assertTrue(typeSystem.getMaxNumericPrecision() < 40);
    Assert.assertEquals(relDataType.getPrecision(), typeSystem.getMaxNumericPrecision());
    Assert.assertEquals(relDataType.getScale(), 10);
    Assert.assertTrue(relDataType.getScale() <= typeSystem.getMaxNumericScale());
}
 
Example #11
Source File: KylinRelDataTypeSystemTest.java    From kylin with Apache License 2.0 5 votes vote down vote up
@Test
public void testLegalDecimalType() {
    RelDataTypeSystem typeSystem = new KylinRelDataTypeSystem();
    RelDataTypeFactory typeFactory = new SqlTypeFactoryImpl(typeSystem);
    
    DataType dataType = DataType.getType("decimal(30, 10)");
    RelDataType relDataType = OLAPTable.createSqlType(typeFactory, dataType, true);
    
    Assert.assertTrue(relDataType instanceof BasicSqlType);
    Assert.assertEquals(relDataType.getSqlTypeName(), SqlTypeName.DECIMAL);
    Assert.assertEquals(relDataType.getPrecision(), 30);
    Assert.assertTrue(relDataType.getPrecision() <= typeSystem.getMaxNumericPrecision());
    Assert.assertEquals(relDataType.getScale(), 10);
    Assert.assertTrue(relDataType.getScale() <= typeSystem.getMaxNumericScale());
}
 
Example #12
Source File: OLAPTableScan.java    From kylin with Apache License 2.0 5 votes vote down vote up
@Override
public Result implement(EnumerableRelImplementor implementor, Prefer pref) {

    context.setReturnTupleInfo(rowType, columnRowType);
    String execFunction = genExecFunc();

    PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), getRowType(), JavaRowFormat.ARRAY);
    MethodCallExpression exprCall = Expressions.call(table.getExpression(OLAPTable.class), execFunction,
            implementor.getRootExpression(), Expressions.constant(context.id));
    return implementor.result(physType, Blocks.toBlock(exprCall));
}
 
Example #13
Source File: OLAPTableScan.java    From kylin with Apache License 2.0 5 votes vote down vote up
public OLAPTableScan(RelOptCluster cluster, RelOptTable table, OLAPTable olapTable, int[] fields) {
    super(cluster, cluster.traitSetOf(OLAPRel.CONVENTION), table);
    this.olapTable = olapTable;
    this.fields = fields;
    this.tableName = olapTable.getTableName();
    this.rowType = getRowType();
    this.kylinConfig = KylinConfig.getInstanceFromEnv();
}
 
Example #14
Source File: TupleExpressionVisitor.java    From kylin with Apache License 2.0 5 votes vote down vote up
private RexCallTupleExpression getRexCallTupleExpression(RexCall call) {
    List<TupleExpression> children = Lists.newArrayListWithExpectedSize(call.getOperands().size());
    for (RexNode rexNode : call.operands) {
        children.add(rexNode.accept(this));
    }

    DataType dataType = DataType.getType(OLAPTable.DATATYPE_MAPPING.get(call.type.getSqlTypeName()));
    RexCallTupleExpression tuple = new RexCallTupleExpression(dataType, children);
    tuple.setDigest(call.toString());
    return tuple;
}
 
Example #15
Source File: TupleExpressionVisitor.java    From kylin with Apache License 2.0 5 votes vote down vote up
private CaseTupleExpression getCaseTupleExpression(RexCall call) {
    List<Pair<TupleFilter, TupleExpression>> whenList = Lists
            .newArrayListWithExpectedSize(call.operands.size() / 2);
    TupleExpression elseExpr = null;

    TupleFilterVisitor filterVistor = new TupleFilterVisitor(inputRowType);
    for (int i = 0; i < call.operands.size() - 1; i += 2) {
        if (call.operands.get(i) instanceof RexCall) {
            RexCall whenCall = (RexCall) call.operands.get(i);
            CompareTupleFilter.CompareResultType compareResultType = RexUtil.getCompareResultType(whenCall);
            if (compareResultType == CompareTupleFilter.CompareResultType.AlwaysTrue) {
                elseExpr = call.operands.get(i + 1).accept(this);
                break;
            } else if (compareResultType == CompareTupleFilter.CompareResultType.AlwaysFalse) {
                continue;
            }
            TupleFilter whenFilter = whenCall.accept(filterVistor);
            whenFilter = new FilterOptimizeTransformer().transform(whenFilter);

            TupleExpression thenExpr = call.operands.get(i + 1).accept(this);
            whenList.add(new Pair<>(whenFilter, thenExpr));
        }
    }
    if (elseExpr == null && call.operands.size() % 2 == 1) {
        RexNode elseNode = call.operands.get(call.operands.size() - 1);
        if (!(elseNode instanceof RexLiteral && ((RexLiteral) elseNode).getValue() == null)) {
            elseExpr = elseNode.accept(this);
        }
    }

    DataType dataType = DataType.getType(OLAPTable.DATATYPE_MAPPING.get(call.type.getSqlTypeName()));
    CaseTupleExpression tuple = new CaseTupleExpression(dataType, whenList, elseExpr);
    tuple.setDigest(call.toString());
    return tuple;
}
 
Example #16
Source File: OLAPJoinRel.java    From kylin with Apache License 2.0 5 votes vote down vote up
@Override
public Result implement(EnumerableRelImplementor implementor, Prefer pref) {

    context.setReturnTupleInfo(rowType, columnRowType);

    PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), getRowType(), pref.preferArray());
    RelOptTable factTable = context.firstTableScan.getTable();
    MethodCallExpression exprCall = Expressions.call(factTable.getExpression(OLAPTable.class), "executeOLAPQuery",
            implementor.getRootExpression(), Expressions.constant(context.id));
    return implementor.result(physType, Blocks.toBlock(exprCall));
}
 
Example #17
Source File: KylinRelDataTypeSystemTest.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testIllegalDecimalType() {
    RelDataTypeSystem typeSystem = new KylinRelDataTypeSystem();
    RelDataTypeFactory typeFactory = new SqlTypeFactoryImpl(typeSystem);
    
    DataType dataType = DataType.getType("decimal(40, 10)");
    RelDataType relDataType = OLAPTable.createSqlType(typeFactory, dataType, true);
    
    Assert.assertTrue(relDataType instanceof BasicSqlType);
    Assert.assertEquals(relDataType.getSqlTypeName(), SqlTypeName.DECIMAL);
    Assert.assertTrue(typeSystem.getMaxNumericPrecision() < 40);
    Assert.assertEquals(relDataType.getPrecision(), typeSystem.getMaxNumericPrecision());
    Assert.assertEquals(relDataType.getScale(), 10);
    Assert.assertTrue(relDataType.getScale() <= typeSystem.getMaxNumericScale());
}
 
Example #18
Source File: KylinRelDataTypeSystemTest.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testLegalDecimalType() {
    RelDataTypeSystem typeSystem = new KylinRelDataTypeSystem();
    RelDataTypeFactory typeFactory = new SqlTypeFactoryImpl(typeSystem);
    
    DataType dataType = DataType.getType("decimal(30, 10)");
    RelDataType relDataType = OLAPTable.createSqlType(typeFactory, dataType, true);
    
    Assert.assertTrue(relDataType instanceof BasicSqlType);
    Assert.assertEquals(relDataType.getSqlTypeName(), SqlTypeName.DECIMAL);
    Assert.assertEquals(relDataType.getPrecision(), 30);
    Assert.assertTrue(relDataType.getPrecision() <= typeSystem.getMaxNumericPrecision());
    Assert.assertEquals(relDataType.getScale(), 10);
    Assert.assertTrue(relDataType.getScale() <= typeSystem.getMaxNumericScale());
}
 
Example #19
Source File: OLAPTableScan.java    From kylin with Apache License 2.0 4 votes vote down vote up
public OLAPTable getOlapTable() {
    return olapTable;
}
 
Example #20
Source File: OLAPProjectRel.java    From kylin-on-parquet-v2 with Apache License 2.0 4 votes vote down vote up
ColumnRowType buildColumnRowType() {
    List<TblColRef> columns = Lists.newArrayList();
    List<TupleExpression> sourceColumns = Lists.newArrayList();

    OLAPRel olapChild = (OLAPRel) getInput();
    ColumnRowType inputColumnRowType = olapChild.getColumnRowType();
    boolean ifVerify = !hasSubQuery() && !afterAggregate;
    TupleExpressionVisitor visitor = new TupleExpressionVisitor(inputColumnRowType, ifVerify);
    for (int i = 0; i < this.rewriteProjects.size(); i++) {
        RexNode rex = this.rewriteProjects.get(i);
        RelDataTypeField columnField = this.rowType.getFieldList().get(i);
        String fieldName = columnField.getName();

        TupleExpression tupleExpr = rex.accept(visitor);
        TblColRef column = translateRexNode(rex, inputColumnRowType, tupleExpr, fieldName);
        if (!this.rewriting && !this.afterAggregate && !isMerelyPermutation) {
            Set<TblColRef> srcCols = ExpressionColCollector.collectColumns(tupleExpr);
            // remove cols not belonging to context tables
            Iterator<TblColRef> srcColIter = srcCols.iterator();
            while (srcColIter.hasNext()) {
                if (!context.belongToContextTables(srcColIter.next())) {
                    srcColIter.remove();
                }
            }
            this.context.allColumns.addAll(srcCols);

            if (this.context.isDynamicColumnEnabled() && tupleExpr.ifForDynamicColumn()) {
                SqlTypeName fSqlType = columnField.getType().getSqlTypeName();
                String dataType = OLAPTable.DATATYPE_MAPPING.get(fSqlType);
                // upgrade data type for number columns
                if (DataType.isNumberFamily(dataType)) {
                    dataType = "decimal";
                }
                column.getColumnDesc().setDatatype(dataType);
                this.context.dynamicFields.put(column, columnField.getType());
            }
        } else {
            tupleExpr = new NoneTupleExpression();
        }

        columns.add(column);
        sourceColumns.add(tupleExpr);
    }
    return new ColumnRowType(columns, sourceColumns);
}
 
Example #21
Source File: OLAPProjectRel.java    From kylin with Apache License 2.0 4 votes vote down vote up
ColumnRowType buildColumnRowType() {
    List<TblColRef> columns = Lists.newArrayList();
    List<TupleExpression> sourceColumns = Lists.newArrayList();

    OLAPRel olapChild = (OLAPRel) getInput();
    ColumnRowType inputColumnRowType = olapChild.getColumnRowType();
    boolean ifVerify = !hasSubQuery() && !afterAggregate;
    TupleExpressionVisitor visitor = new TupleExpressionVisitor(inputColumnRowType, ifVerify);
    for (int i = 0; i < this.rewriteProjects.size(); i++) {
        RexNode rex = this.rewriteProjects.get(i);
        RelDataTypeField columnField = this.rowType.getFieldList().get(i);
        String fieldName = columnField.getName();

        TupleExpression tupleExpr = rex.accept(visitor);
        TblColRef column = translateRexNode(rex, inputColumnRowType, tupleExpr, fieldName);
        if (!this.rewriting && !this.afterAggregate && !isMerelyPermutation) {
            Set<TblColRef> srcCols = ExpressionColCollector.collectColumns(tupleExpr);
            // remove cols not belonging to context tables
            Iterator<TblColRef> srcColIter = srcCols.iterator();
            while (srcColIter.hasNext()) {
                if (!context.belongToContextTables(srcColIter.next())) {
                    srcColIter.remove();
                }
            }
            this.context.allColumns.addAll(srcCols);

            if (this.context.isDynamicColumnEnabled() && tupleExpr.ifForDynamicColumn()) {
                SqlTypeName fSqlType = columnField.getType().getSqlTypeName();
                String dataType = OLAPTable.DATATYPE_MAPPING.get(fSqlType);
                column.getColumnDesc().setDatatype(dataType);
                this.context.dynamicFields.put(column, columnField.getType());
            }
        } else {
            tupleExpr = new NoneTupleExpression();
        }

        columns.add(column);
        sourceColumns.add(tupleExpr);
    }
    return new ColumnRowType(columns, sourceColumns);
}
 
Example #22
Source File: OLAPTableScan.java    From Kylin with Apache License 2.0 4 votes vote down vote up
public OLAPTable getOlapTable() {
    return olapTable;
}
 
Example #23
Source File: OLAPTableScan.java    From kylin-on-parquet-v2 with Apache License 2.0 4 votes vote down vote up
public OLAPTable getOlapTable() {
    return olapTable;
}