Search in sources :

Example 36 with JavaTypeFactory

use of org.apache.calcite.adapter.java.JavaTypeFactory in project calcite by apache.

the class EnumerableWindow method implement.

public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
    final JavaTypeFactory typeFactory = implementor.getTypeFactory();
    final EnumerableRel child = (EnumerableRel) getInput();
    final BlockBuilder builder = new BlockBuilder();
    final Result result = implementor.visitChild(this, 0, child, pref);
    Expression source_ = builder.append("source", result.block);
    final List<Expression> translatedConstants = new ArrayList<Expression>(constants.size());
    for (RexLiteral constant : constants) {
        translatedConstants.add(RexToLixTranslator.translateLiteral(constant, constant.getType(), typeFactory, RexImpTable.NullAs.NULL));
    }
    PhysType inputPhysType = result.physType;
    final int w = implementor.windowCount++;
    ParameterExpression prevStart = Expressions.parameter(int.class, builder.newName("prevStart" + w));
    ParameterExpression prevEnd = Expressions.parameter(int.class, builder.newName("prevEnd" + w));
    builder.add(Expressions.declare(0, prevStart, null));
    builder.add(Expressions.declare(0, prevEnd, null));
    for (int windowIdx = 0; windowIdx < groups.size(); windowIdx++) {
        Group group = groups.get(windowIdx);
        // Comparator:
        // final Comparator<JdbcTest.Employee> comparator =
        // new Comparator<JdbcTest.Employee>() {
        // public int compare(JdbcTest.Employee o1,
        // JdbcTest.Employee o2) {
        // return Integer.compare(o1.empid, o2.empid);
        // }
        // };
        final Expression comparator_ = builder.append("comparator", inputPhysType.generateComparator(group.collation()));
        Pair<Expression, Expression> partitionIterator = getPartitionIterator(builder, source_, inputPhysType, group, comparator_);
        final Expression collectionExpr = partitionIterator.left;
        final Expression iterator_ = partitionIterator.right;
        List<AggImpState> aggs = new ArrayList<AggImpState>();
        List<AggregateCall> aggregateCalls = group.getAggregateCalls(this);
        for (int aggIdx = 0; aggIdx < aggregateCalls.size(); aggIdx++) {
            AggregateCall call = aggregateCalls.get(aggIdx);
            aggs.add(new AggImpState(aggIdx, call, true));
        }
        // The output from this stage is the input plus the aggregate functions.
        final RelDataTypeFactory.Builder typeBuilder = typeFactory.builder();
        typeBuilder.addAll(inputPhysType.getRowType().getFieldList());
        for (AggImpState agg : aggs) {
            typeBuilder.add(agg.call.name, agg.call.type);
        }
        RelDataType outputRowType = typeBuilder.build();
        final PhysType outputPhysType = PhysTypeImpl.of(typeFactory, outputRowType, pref.prefer(result.format));
        final Expression list_ = builder.append("list", Expressions.new_(ArrayList.class, Expressions.call(collectionExpr, BuiltInMethod.COLLECTION_SIZE.method)), false);
        Pair<Expression, Expression> collationKey = getRowCollationKey(builder, inputPhysType, group, windowIdx);
        Expression keySelector = collationKey.left;
        Expression keyComparator = collationKey.right;
        final BlockBuilder builder3 = new BlockBuilder();
        final Expression rows_ = builder3.append("rows", Expressions.convert_(Expressions.call(iterator_, BuiltInMethod.ITERATOR_NEXT.method), Object[].class), false);
        builder3.add(Expressions.statement(Expressions.assign(prevStart, Expressions.constant(-1))));
        builder3.add(Expressions.statement(Expressions.assign(prevEnd, Expressions.constant(Integer.MAX_VALUE))));
        final BlockBuilder builder4 = new BlockBuilder();
        final ParameterExpression i_ = Expressions.parameter(int.class, builder4.newName("i"));
        final Expression row_ = builder4.append("row", RexToLixTranslator.convert(Expressions.arrayIndex(rows_, i_), inputPhysType.getJavaRowType()));
        final RexToLixTranslator.InputGetter inputGetter = new WindowRelInputGetter(row_, inputPhysType, result.physType.getRowType().getFieldCount(), translatedConstants);
        final RexToLixTranslator translator = RexToLixTranslator.forAggregation(typeFactory, builder4, inputGetter);
        final List<Expression> outputRow = new ArrayList<Expression>();
        int fieldCountWithAggResults = inputPhysType.getRowType().getFieldCount();
        for (int i = 0; i < fieldCountWithAggResults; i++) {
            outputRow.add(inputPhysType.fieldReference(row_, i, outputPhysType.getJavaFieldType(i)));
        }
        declareAndResetState(typeFactory, builder, result, windowIdx, aggs, outputPhysType, outputRow);
        // There are assumptions that minX==0. If ever change this, look for
        // frameRowCount, bounds checking, etc
        final Expression minX = Expressions.constant(0);
        final Expression partitionRowCount = builder3.append("partRows", Expressions.field(rows_, "length"));
        final Expression maxX = builder3.append("maxX", Expressions.subtract(partitionRowCount, Expressions.constant(1)));
        final Expression startUnchecked = builder4.append("start", translateBound(translator, i_, row_, minX, maxX, rows_, group, true, inputPhysType, comparator_, keySelector, keyComparator));
        final Expression endUnchecked = builder4.append("end", translateBound(translator, i_, row_, minX, maxX, rows_, group, false, inputPhysType, comparator_, keySelector, keyComparator));
        final Expression startX;
        final Expression endX;
        final Expression hasRows;
        if (group.isAlwaysNonEmpty()) {
            startX = startUnchecked;
            endX = endUnchecked;
            hasRows = Expressions.constant(true);
        } else {
            Expression startTmp = group.lowerBound.isUnbounded() || startUnchecked == i_ ? startUnchecked : builder4.append("startTmp", Expressions.call(null, BuiltInMethod.MATH_MAX.method, startUnchecked, minX));
            Expression endTmp = group.upperBound.isUnbounded() || endUnchecked == i_ ? endUnchecked : builder4.append("endTmp", Expressions.call(null, BuiltInMethod.MATH_MIN.method, endUnchecked, maxX));
            ParameterExpression startPe = Expressions.parameter(0, int.class, builder4.newName("startChecked"));
            ParameterExpression endPe = Expressions.parameter(0, int.class, builder4.newName("endChecked"));
            builder4.add(Expressions.declare(Modifier.FINAL, startPe, null));
            builder4.add(Expressions.declare(Modifier.FINAL, endPe, null));
            hasRows = builder4.append("hasRows", Expressions.lessThanOrEqual(startTmp, endTmp));
            builder4.add(Expressions.ifThenElse(hasRows, Expressions.block(Expressions.statement(Expressions.assign(startPe, startTmp)), Expressions.statement(Expressions.assign(endPe, endTmp))), Expressions.block(Expressions.statement(Expressions.assign(startPe, Expressions.constant(-1))), Expressions.statement(Expressions.assign(endPe, Expressions.constant(-1))))));
            startX = startPe;
            endX = endPe;
        }
        final BlockBuilder builder5 = new BlockBuilder(true, builder4);
        BinaryExpression rowCountWhenNonEmpty = Expressions.add(startX == minX ? endX : Expressions.subtract(endX, startX), Expressions.constant(1));
        final Expression frameRowCount;
        if (hasRows.equals(Expressions.constant(true))) {
            frameRowCount = builder4.append("totalRows", rowCountWhenNonEmpty);
        } else {
            frameRowCount = builder4.append("totalRows", Expressions.condition(hasRows, rowCountWhenNonEmpty, Expressions.constant(0)));
        }
        ParameterExpression actualStart = Expressions.parameter(0, int.class, builder5.newName("actualStart"));
        final BlockBuilder builder6 = new BlockBuilder(true, builder5);
        builder6.add(Expressions.statement(Expressions.assign(actualStart, startX)));
        for (final AggImpState agg : aggs) {
            agg.implementor.implementReset(agg.context, new WinAggResetContextImpl(builder6, agg.state, i_, startX, endX, hasRows, partitionRowCount, frameRowCount));
        }
        Expression lowerBoundCanChange = group.lowerBound.isUnbounded() && group.lowerBound.isPreceding() ? Expressions.constant(false) : Expressions.notEqual(startX, prevStart);
        Expression needRecomputeWindow = Expressions.orElse(lowerBoundCanChange, Expressions.lessThan(endX, prevEnd));
        BlockStatement resetWindowState = builder6.toBlock();
        if (resetWindowState.statements.size() == 1) {
            builder5.add(Expressions.declare(0, actualStart, Expressions.condition(needRecomputeWindow, startX, Expressions.add(prevEnd, Expressions.constant(1)))));
        } else {
            builder5.add(Expressions.declare(0, actualStart, null));
            builder5.add(Expressions.ifThenElse(needRecomputeWindow, resetWindowState, Expressions.statement(Expressions.assign(actualStart, Expressions.add(prevEnd, Expressions.constant(1))))));
        }
        if (lowerBoundCanChange instanceof BinaryExpression) {
            builder5.add(Expressions.statement(Expressions.assign(prevStart, startX)));
        }
        builder5.add(Expressions.statement(Expressions.assign(prevEnd, endX)));
        final BlockBuilder builder7 = new BlockBuilder(true, builder5);
        final DeclarationStatement jDecl = Expressions.declare(0, "j", actualStart);
        final PhysType inputPhysTypeFinal = inputPhysType;
        final Function<BlockBuilder, WinAggFrameResultContext> resultContextBuilder = getBlockBuilderWinAggFrameResultContextFunction(typeFactory, result, translatedConstants, comparator_, rows_, i_, startX, endX, minX, maxX, hasRows, frameRowCount, partitionRowCount, jDecl, inputPhysTypeFinal);
        final Function<AggImpState, List<RexNode>> rexArguments = new Function<AggImpState, List<RexNode>>() {

            public List<RexNode> apply(AggImpState agg) {
                List<Integer> argList = agg.call.getArgList();
                List<RelDataType> inputTypes = EnumUtils.fieldRowTypes(result.physType.getRowType(), constants, argList);
                List<RexNode> args = new ArrayList<RexNode>(inputTypes.size());
                for (int i = 0; i < argList.size(); i++) {
                    Integer idx = argList.get(i);
                    args.add(new RexInputRef(idx, inputTypes.get(i)));
                }
                return args;
            }
        };
        implementAdd(aggs, builder7, resultContextBuilder, rexArguments, jDecl);
        BlockStatement forBlock = builder7.toBlock();
        if (!forBlock.statements.isEmpty()) {
            // For instance, row_number does not use for loop to compute the value
            Statement forAggLoop = Expressions.for_(Arrays.asList(jDecl), Expressions.lessThanOrEqual(jDecl.parameter, endX), Expressions.preIncrementAssign(jDecl.parameter), forBlock);
            if (!hasRows.equals(Expressions.constant(true))) {
                forAggLoop = Expressions.ifThen(hasRows, forAggLoop);
            }
            builder5.add(forAggLoop);
        }
        if (implementResult(aggs, builder5, resultContextBuilder, rexArguments, true)) {
            builder4.add(Expressions.ifThen(Expressions.orElse(lowerBoundCanChange, Expressions.notEqual(endX, prevEnd)), builder5.toBlock()));
        }
        implementResult(aggs, builder4, resultContextBuilder, rexArguments, false);
        builder4.add(Expressions.statement(Expressions.call(list_, BuiltInMethod.COLLECTION_ADD.method, outputPhysType.record(outputRow))));
        builder3.add(Expressions.for_(Expressions.declare(0, i_, Expressions.constant(0)), Expressions.lessThan(i_, Expressions.field(rows_, "length")), Expressions.preIncrementAssign(i_), builder4.toBlock()));
        builder.add(Expressions.while_(Expressions.call(iterator_, BuiltInMethod.ITERATOR_HAS_NEXT.method), builder3.toBlock()));
        builder.add(Expressions.statement(Expressions.call(collectionExpr, BuiltInMethod.MAP_CLEAR.method)));
        // We're not assigning to "source". For each group, create a new
        // final variable called "source" or "sourceN".
        source_ = builder.append("source", Expressions.call(BuiltInMethod.AS_ENUMERABLE.method, list_));
        inputPhysType = outputPhysType;
    }
    // return Linq4j.asEnumerable(list);
    builder.add(Expressions.return_(null, source_));
    return implementor.result(inputPhysType, builder.toBlock());
}
Also used : WinAggResetContextImpl(org.apache.calcite.adapter.enumerable.impl.WinAggResetContextImpl) RexLiteral(org.apache.calcite.rex.RexLiteral) ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType) BlockStatement(org.apache.calcite.linq4j.tree.BlockStatement) Function(com.google.common.base.Function) SqlAggFunction(org.apache.calcite.sql.SqlAggFunction) BinaryExpression(org.apache.calcite.linq4j.tree.BinaryExpression) JavaTypeFactory(org.apache.calcite.adapter.java.JavaTypeFactory) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder) DeclarationStatement(org.apache.calcite.linq4j.tree.DeclarationStatement) Statement(org.apache.calcite.linq4j.tree.Statement) DeclarationStatement(org.apache.calcite.linq4j.tree.DeclarationStatement) BlockStatement(org.apache.calcite.linq4j.tree.BlockStatement) AggregateCall(org.apache.calcite.rel.core.AggregateCall) BinaryExpression(org.apache.calcite.linq4j.tree.BinaryExpression) Expression(org.apache.calcite.linq4j.tree.Expression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) RexInputRef(org.apache.calcite.rex.RexInputRef) RexNode(org.apache.calcite.rex.RexNode)

Example 37 with JavaTypeFactory

use of org.apache.calcite.adapter.java.JavaTypeFactory in project calcite by apache.

the class JdbcTable method scan.

public Enumerable<Object[]> scan(DataContext root) {
    final JavaTypeFactory typeFactory = root.getTypeFactory();
    final SqlString sql = generateSql();
    return ResultSetEnumerable.of(jdbcSchema.getDataSource(), sql.getSql(), JdbcUtils.ObjectArrayRowBuilder.factory(fieldClasses(typeFactory)));
}
Also used : JavaTypeFactory(org.apache.calcite.adapter.java.JavaTypeFactory) SqlString(org.apache.calcite.sql.util.SqlString)

Example 38 with JavaTypeFactory

use of org.apache.calcite.adapter.java.JavaTypeFactory in project calcite by apache.

the class CalcitePrepareImpl method simplePrepare.

/**
 * Quickly prepares a simple SQL statement, circumventing the usual
 * preparation process.
 */
private <T> CalciteSignature<T> simplePrepare(Context context, String sql) {
    final JavaTypeFactory typeFactory = context.getTypeFactory();
    final RelDataType x = typeFactory.builder().add(SqlUtil.deriveAliasFromOrdinal(0), SqlTypeName.INTEGER).build();
    @SuppressWarnings("unchecked") final List<T> list = (List) ImmutableList.of(1);
    final List<String> origin = null;
    final List<List<String>> origins = Collections.nCopies(x.getFieldCount(), origin);
    final List<ColumnMetaData> columns = getColumnMetaDataList(typeFactory, x, x, origins);
    final Meta.CursorFactory cursorFactory = Meta.CursorFactory.deduce(columns, null);
    return new CalciteSignature<>(sql, ImmutableList.<AvaticaParameter>of(), ImmutableMap.<String, Object>of(), x, columns, cursorFactory, context.getRootSchema(), ImmutableList.<RelCollation>of(), -1, new Bindable<T>() {

        public Enumerable<T> bind(DataContext dataContext) {
            return Linq4j.asEnumerable(list);
        }
    }, Meta.StatementType.SELECT);
}
Also used : Meta(org.apache.calcite.avatica.Meta) RelDataType(org.apache.calcite.rel.type.RelDataType) DataContext(org.apache.calcite.DataContext) JavaTypeFactory(org.apache.calcite.adapter.java.JavaTypeFactory) Enumerable(org.apache.calcite.linq4j.Enumerable) ArrayList(java.util.ArrayList) ImmutableIntList(org.apache.calcite.util.ImmutableIntList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) ColumnMetaData(org.apache.calcite.avatica.ColumnMetaData)

Example 39 with JavaTypeFactory

use of org.apache.calcite.adapter.java.JavaTypeFactory in project calcite by apache.

the class CalcitePrepareImpl method convert_.

private ParseResult convert_(Context context, String sql, boolean analyze, boolean fail, CalciteCatalogReader catalogReader, SqlValidator validator, SqlNode sqlNode1) {
    final JavaTypeFactory typeFactory = context.getTypeFactory();
    final Convention resultConvention = enableBindable ? BindableConvention.INSTANCE : EnumerableConvention.INSTANCE;
    final HepPlanner planner = new HepPlanner(new HepProgramBuilder().build());
    planner.addRelTraitDef(ConventionTraitDef.INSTANCE);
    final SqlToRelConverter.ConfigBuilder configBuilder = SqlToRelConverter.configBuilder().withTrimUnusedFields(true);
    if (analyze) {
        configBuilder.withConvertTableAccess(false);
    }
    final CalcitePreparingStmt preparingStmt = new CalcitePreparingStmt(this, context, catalogReader, typeFactory, context.getRootSchema(), null, planner, resultConvention, createConvertletTable());
    final SqlToRelConverter converter = preparingStmt.getSqlToRelConverter(validator, catalogReader, configBuilder.build());
    final RelRoot root = converter.convertQuery(sqlNode1, false, true);
    if (analyze) {
        return analyze_(validator, sql, sqlNode1, root, fail);
    }
    return new ConvertResult(this, validator, sql, sqlNode1, validator.getValidatedNodeType(sqlNode1), root);
}
Also used : BindableConvention(org.apache.calcite.interpreter.BindableConvention) Convention(org.apache.calcite.plan.Convention) EnumerableConvention(org.apache.calcite.adapter.enumerable.EnumerableConvention) SqlToRelConverter(org.apache.calcite.sql2rel.SqlToRelConverter) JavaTypeFactory(org.apache.calcite.adapter.java.JavaTypeFactory) HepProgramBuilder(org.apache.calcite.plan.hep.HepProgramBuilder) RelRoot(org.apache.calcite.rel.RelRoot) HepPlanner(org.apache.calcite.plan.hep.HepPlanner)

Example 40 with JavaTypeFactory

use of org.apache.calcite.adapter.java.JavaTypeFactory in project calcite by apache.

the class Prepare method optimize.

/**
 * Optimizes a query plan.
 *
 * @param root Root of relational expression tree
 * @param materializations Tables known to be populated with a given query
 * @param lattices Lattices
 * @return an equivalent optimized relational expression
 */
protected RelRoot optimize(RelRoot root, final List<Materialization> materializations, final List<CalciteSchema.LatticeEntry> lattices) {
    final RelOptPlanner planner = root.rel.getCluster().getPlanner();
    final DataContext dataContext = context.getDataContext();
    planner.setExecutor(new RexExecutorImpl(dataContext));
    final List<RelOptMaterialization> materializationList = new ArrayList<>();
    for (Materialization materialization : materializations) {
        List<String> qualifiedTableName = materialization.materializedTable.path();
        materializationList.add(new RelOptMaterialization(materialization.tableRel, materialization.queryRel, materialization.starRelOptTable, qualifiedTableName));
    }
    final List<RelOptLattice> latticeList = new ArrayList<>();
    for (CalciteSchema.LatticeEntry lattice : lattices) {
        final CalciteSchema.TableEntry starTable = lattice.getStarTable();
        final JavaTypeFactory typeFactory = context.getTypeFactory();
        final RelOptTableImpl starRelOptTable = RelOptTableImpl.create(catalogReader, starTable.getTable().getRowType(typeFactory), starTable, null);
        latticeList.add(new RelOptLattice(lattice.getLattice(), starRelOptTable));
    }
    final RelTraitSet desiredTraits = getDesiredRootTraitSet(root);
    // Work around
    // [CALCITE-1774] Allow rules to be registered during planning process
    // by briefly creating each kind of physical table to let it register its
    // rules. The problem occurs when plans are created via RelBuilder, not
    // the usual process (SQL and SqlToRelConverter.Config.isConvertTableAccess
    // = true).
    final RelVisitor visitor = new RelVisitor() {

        @Override
        public void visit(RelNode node, int ordinal, RelNode parent) {
            if (node instanceof TableScan) {
                final RelOptCluster cluster = node.getCluster();
                final RelOptTable.ToRelContext context = RelOptUtil.getContext(cluster);
                final RelNode r = node.getTable().toRel(context);
                planner.registerClass(r);
            }
            super.visit(node, ordinal, parent);
        }
    };
    visitor.go(root.rel);
    final Program program = getProgram();
    final RelNode rootRel4 = program.run(planner, root.rel, desiredTraits, materializationList, latticeList);
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Plan after physical tweaks: {}", RelOptUtil.toString(rootRel4, SqlExplainLevel.ALL_ATTRIBUTES));
    }
    return root.withRel(rootRel4);
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) TableScan(org.apache.calcite.rel.core.TableScan) Program(org.apache.calcite.tools.Program) ArrayList(java.util.ArrayList) RelTraitSet(org.apache.calcite.plan.RelTraitSet) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) LatticeEntry(org.apache.calcite.jdbc.CalciteSchema.LatticeEntry) RexExecutorImpl(org.apache.calcite.rex.RexExecutorImpl) RelOptMaterialization(org.apache.calcite.plan.RelOptMaterialization) DataContext(org.apache.calcite.DataContext) RelNode(org.apache.calcite.rel.RelNode) RelOptMaterialization(org.apache.calcite.plan.RelOptMaterialization) CalciteSchema(org.apache.calcite.jdbc.CalciteSchema) JavaTypeFactory(org.apache.calcite.adapter.java.JavaTypeFactory) RelOptLattice(org.apache.calcite.plan.RelOptLattice) RelVisitor(org.apache.calcite.rel.RelVisitor) RelOptTable(org.apache.calcite.plan.RelOptTable)

Aggregations

JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)43 JavaTypeFactoryImpl (org.apache.calcite.jdbc.JavaTypeFactoryImpl)18 ArrayList (java.util.ArrayList)15 SchemaPlus (org.apache.calcite.schema.SchemaPlus)15 StreamableTable (org.apache.calcite.schema.StreamableTable)12 Table (org.apache.calcite.schema.Table)12 RelDataType (org.apache.calcite.rel.type.RelDataType)10 Expression (org.apache.calcite.linq4j.tree.Expression)9 BlockBuilder (org.apache.calcite.linq4j.tree.BlockBuilder)8 SqlNode (org.apache.calcite.sql.SqlNode)7 Type (java.lang.reflect.Type)6 ParameterExpression (org.apache.calcite.linq4j.tree.ParameterExpression)6 FrameworkConfig (org.apache.calcite.tools.FrameworkConfig)6 CompilerUtil (org.apache.storm.sql.compiler.CompilerUtil)6 CalciteCatalogReader (org.apache.calcite.prepare.CalciteCatalogReader)5 RelNode (org.apache.calcite.rel.RelNode)5 SqlOperatorTable (org.apache.calcite.sql.SqlOperatorTable)5 ChainedSqlOperatorTable (org.apache.calcite.sql.util.ChainedSqlOperatorTable)5 ImmutableList (com.google.common.collect.ImmutableList)4 CalcitePrepare (org.apache.calcite.jdbc.CalcitePrepare)4