Search in sources :

Example 16 with TableScan

use of org.apache.calcite.rel.core.TableScan in project storm by apache.

the class StreamsScanRule method convert.

@Override
public RelNode convert(RelNode rel) {
    final TableScan scan = (TableScan) rel;
    int parallelismHint = DEFAULT_PARALLELISM_HINT;
    final ParallelTable parallelTable = scan.getTable().unwrap(ParallelTable.class);
    if (parallelTable != null && parallelTable.parallelismHint() != null) {
        parallelismHint = parallelTable.parallelismHint();
    }
    final Table table = scan.getTable().unwrap(Table.class);
    switch(table.getJdbcTableType()) {
        case STREAM:
            return new StreamsStreamScanRel(scan.getCluster(), scan.getTraitSet().replace(StreamsLogicalConvention.INSTANCE), scan.getTable(), parallelismHint);
        default:
            throw new IllegalArgumentException(String.format("Unsupported table type: %s", table.getJdbcTableType()));
    }
}
Also used : TableScan(org.apache.calcite.rel.core.TableScan) EnumerableTableScan(org.apache.calcite.adapter.enumerable.EnumerableTableScan) ParallelStreamableTable(org.apache.storm.sql.calcite.ParallelStreamableTable) Table(org.apache.calcite.schema.Table) ParallelTable(org.apache.storm.sql.calcite.ParallelTable) StreamsStreamScanRel(org.apache.storm.sql.planner.streams.rel.StreamsStreamScanRel) ParallelTable(org.apache.storm.sql.calcite.ParallelTable)

Example 17 with TableScan

use of org.apache.calcite.rel.core.TableScan in project hazelcast by hazelcast.

the class JoinPhysicalRule method onMatch.

@Override
public void onMatch(RelOptRuleCall call) {
    JoinLogicalRel logicalJoin = call.rel(0);
    JoinRelType joinType = logicalJoin.getJoinType();
    if (joinType != JoinRelType.INNER && joinType != JoinRelType.LEFT) {
        throw new RuntimeException("Unexpected joinType: " + joinType);
    }
    RelNode leftInput = call.rel(1);
    RelNode rightInput = call.rel(2);
    if (OptUtils.isUnbounded(rightInput)) {
        // can be on the left side.
        return;
    }
    RelNode leftInputConverted = RelRule.convert(leftInput, leftInput.getTraitSet().replace(PHYSICAL));
    RelNode rightInputConverted = RelRule.convert(rightInput, rightInput.getTraitSet().replace(PHYSICAL));
    // we don't use hash join for unbounded left input because it doesn't refresh the right side
    if (OptUtils.isBounded(leftInput)) {
        RelNode rel = new JoinHashPhysicalRel(logicalJoin.getCluster(), logicalJoin.getTraitSet().replace(PHYSICAL), leftInputConverted, rightInputConverted, logicalJoin.getCondition(), logicalJoin.getJoinType());
        call.transformTo(rel);
    }
    if (rightInput instanceof TableScan) {
        HazelcastTable rightHzTable = rightInput.getTable().unwrap(HazelcastTable.class);
        if (SqlConnectorUtil.getJetSqlConnector(rightHzTable.getTarget()).isNestedLoopReaderSupported()) {
            RelNode rel2 = new JoinNestedLoopPhysicalRel(logicalJoin.getCluster(), OptUtils.toPhysicalConvention(logicalJoin.getTraitSet()), leftInputConverted, rightInputConverted, logicalJoin.getCondition(), logicalJoin.getJoinType());
            call.transformTo(rel2);
        }
    }
}
Also used : JoinRelType(org.apache.calcite.rel.core.JoinRelType) TableScan(org.apache.calcite.rel.core.TableScan) RelNode(org.apache.calcite.rel.RelNode) JoinLogicalRel(com.hazelcast.jet.sql.impl.opt.logical.JoinLogicalRel) HazelcastTable(com.hazelcast.jet.sql.impl.schema.HazelcastTable)

Example 18 with TableScan

use of org.apache.calcite.rel.core.TableScan in project hive by apache.

the class ASTConverter method convertSource.

private QueryBlockInfo convertSource(RelNode r) throws CalciteSemanticException {
    Schema s = null;
    ASTNode ast = null;
    if (r instanceof TableScan) {
        TableScan f = (TableScan) r;
        s = new Schema(f);
        ast = ASTBuilder.table(f);
        planMapper.link(ast, f);
    } else if (r instanceof HiveJdbcConverter) {
        HiveJdbcConverter f = (HiveJdbcConverter) r;
        s = new Schema(f);
        ast = ASTBuilder.table(f);
    } else if (r instanceof DruidQuery) {
        DruidQuery f = (DruidQuery) r;
        s = new Schema(f);
        ast = ASTBuilder.table(f);
    } else if (r instanceof Join) {
        Join join = (Join) r;
        QueryBlockInfo left = convertSource(join.getLeft());
        QueryBlockInfo right = convertSource(join.getRight());
        s = new Schema(left.schema, right.schema);
        ASTNode cond = join.getCondition().accept(new RexVisitor(s, false, r.getCluster().getRexBuilder()));
        boolean semiJoin = join.isSemiJoin() || join.getJoinType() == JoinRelType.ANTI;
        if (join.getRight() instanceof Join && !semiJoin) {
            // should not be done for semijoin since it will change the semantics
            // Invert join inputs; this is done because otherwise the SemanticAnalyzer
            // methods to merge joins will not kick in
            JoinRelType type;
            if (join.getJoinType() == JoinRelType.LEFT) {
                type = JoinRelType.RIGHT;
            } else if (join.getJoinType() == JoinRelType.RIGHT) {
                type = JoinRelType.LEFT;
            } else {
                type = join.getJoinType();
            }
            ast = ASTBuilder.join(right.ast, left.ast, type, cond);
            addPkFkInfoToAST(ast, join, true);
        } else {
            ast = ASTBuilder.join(left.ast, right.ast, join.getJoinType(), cond);
            addPkFkInfoToAST(ast, join, false);
        }
        if (semiJoin) {
            s = left.schema;
        }
    } else if (r instanceof Union) {
        Union u = ((Union) r);
        ASTNode left = new ASTConverter(((Union) r).getInput(0), this.derivedTableCount, planMapper).convert();
        for (int ind = 1; ind < u.getInputs().size(); ind++) {
            left = getUnionAllAST(left, new ASTConverter(((Union) r).getInput(ind), this.derivedTableCount, planMapper).convert());
            String sqAlias = nextAlias();
            ast = ASTBuilder.subQuery(left, sqAlias);
            s = new Schema((Union) r, sqAlias);
        }
    } else {
        ASTConverter src = new ASTConverter(r, this.derivedTableCount, planMapper);
        ASTNode srcAST = src.convert();
        String sqAlias = nextAlias();
        s = src.getRowSchema(sqAlias);
        ast = ASTBuilder.subQuery(srcAST, sqAlias);
    }
    return new QueryBlockInfo(s, ast);
}
Also used : HiveTableScan(org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveTableScan) TableScan(org.apache.calcite.rel.core.TableScan) JdbcHiveTableScan(org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.jdbc.JdbcHiveTableScan) DruidQuery(org.apache.calcite.adapter.druid.DruidQuery) FieldSchema(org.apache.hadoop.hive.metastore.api.FieldSchema) Join(org.apache.calcite.rel.core.Join) Union(org.apache.calcite.rel.core.Union) JoinRelType(org.apache.calcite.rel.core.JoinRelType) ASTNode(org.apache.hadoop.hive.ql.parse.ASTNode) HiveJdbcConverter(org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.jdbc.HiveJdbcConverter)

Example 19 with TableScan

use of org.apache.calcite.rel.core.TableScan in project hive by apache.

the class MaterializedViewRewritingRelVisitor method check.

private void check(Union union) {
    // We found the Union
    if (union.getInputs().size() != 2) {
        // Bail out
        throw new ReturnedValue(false);
    }
    // First branch should have the query (with write ID filter conditions)
    new RelVisitor() {

        @Override
        public void visit(RelNode node, int ordinal, RelNode parent) {
            if (node instanceof TableScan || node instanceof Filter || node instanceof Project || node instanceof Join) {
                // We can continue
                super.visit(node, ordinal, parent);
            } else if (node instanceof Aggregate && containsAggregate) {
                Aggregate aggregate = (Aggregate) node;
                for (int i = 0; i < aggregate.getAggCallList().size(); ++i) {
                    AggregateCall aggregateCall = aggregate.getAggCallList().get(i);
                    if (aggregateCall.getAggregation().getKind() == SqlKind.COUNT && aggregateCall.getArgList().size() == 0) {
                        countIndex = i + aggregate.getGroupCount();
                        break;
                    }
                }
                // We can continue
                super.visit(node, ordinal, parent);
            } else {
                throw new ReturnedValue(false);
            }
        }
    }.go(union.getInput(0));
    // Second branch should only have the MV
    new RelVisitor() {

        @Override
        public void visit(RelNode node, int ordinal, RelNode parent) {
            if (node instanceof TableScan) {
                // We can continue
                // TODO: Need to check that this is the same MV that we are rebuilding
                RelOptHiveTable hiveTable = (RelOptHiveTable) node.getTable();
                if (!hiveTable.getHiveTableMD().isMaterializedView()) {
                    // If it is not a materialized view, we do not rewrite it
                    throw new ReturnedValue(false);
                }
                if (containsAggregate && !AcidUtils.isFullAcidTable(hiveTable.getHiveTableMD())) {
                    // we do not rewrite it (we need MERGE support)
                    throw new ReturnedValue(false);
                }
            } else if (node instanceof Project) {
                // We can continue
                super.visit(node, ordinal, parent);
            } else {
                throw new ReturnedValue(false);
            }
        }
    }.go(union.getInput(1));
    // We pass all the checks, we can rewrite
    throw new ReturnedValue(true);
}
Also used : AggregateCall(org.apache.calcite.rel.core.AggregateCall) TableScan(org.apache.calcite.rel.core.TableScan) Project(org.apache.calcite.rel.core.Project) RelOptHiveTable(org.apache.hadoop.hive.ql.optimizer.calcite.RelOptHiveTable) RelNode(org.apache.calcite.rel.RelNode) Filter(org.apache.calcite.rel.core.Filter) Join(org.apache.calcite.rel.core.Join) RelVisitor(org.apache.calcite.rel.RelVisitor) Aggregate(org.apache.calcite.rel.core.Aggregate)

Example 20 with TableScan

use of org.apache.calcite.rel.core.TableScan in project hive by apache.

the class HiveMaterializedViewUtils method augmentMaterializationWithTimeInformation.

/**
 * Method to enrich the materialization query contained in the input with
 * its invalidation.
 */
public static HiveRelOptMaterialization augmentMaterializationWithTimeInformation(HiveRelOptMaterialization materialization, String validTxnsList, ValidTxnWriteIdList materializationTxnList) throws LockException {
    // Extract tables used by the query which will in turn be used to generate
    // the corresponding txn write ids
    List<String> tablesUsed = new ArrayList<>();
    new RelVisitor() {

        @Override
        public void visit(RelNode node, int ordinal, RelNode parent) {
            if (node instanceof TableScan) {
                TableScan ts = (TableScan) node;
                tablesUsed.add(((RelOptHiveTable) ts.getTable()).getHiveTableMD().getFullyQualifiedName());
            }
            super.visit(node, ordinal, parent);
        }
    }.go(materialization.queryRel);
    ValidTxnWriteIdList currentTxnList = SessionState.get().getTxnMgr().getValidWriteIds(tablesUsed, validTxnsList);
    // Augment
    final RexBuilder rexBuilder = materialization.queryRel.getCluster().getRexBuilder();
    final HepProgramBuilder augmentMaterializationProgram = new HepProgramBuilder().addRuleInstance(new HiveAugmentMaterializationRule(rexBuilder, currentTxnList, materializationTxnList));
    final HepPlanner augmentMaterializationPlanner = new HepPlanner(augmentMaterializationProgram.build());
    augmentMaterializationPlanner.setRoot(materialization.queryRel);
    final RelNode modifiedQueryRel = augmentMaterializationPlanner.findBestExp();
    return new HiveRelOptMaterialization(materialization.tableRel, modifiedQueryRel, null, materialization.qualifiedTableName, materialization.getScope(), materialization.getRebuildMode());
}
Also used : HiveTableScan(org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveTableScan) TableScan(org.apache.calcite.rel.core.TableScan) ArrayList(java.util.ArrayList) HepProgramBuilder(org.apache.calcite.plan.hep.HepProgramBuilder) HepPlanner(org.apache.calcite.plan.hep.HepPlanner) HiveRelNode(org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveRelNode) RelNode(org.apache.calcite.rel.RelNode) RexBuilder(org.apache.calcite.rex.RexBuilder) RelVisitor(org.apache.calcite.rel.RelVisitor) ValidTxnWriteIdList(org.apache.hadoop.hive.common.ValidTxnWriteIdList) HiveRelOptMaterialization(org.apache.hadoop.hive.ql.metadata.HiveRelOptMaterialization)

Aggregations

TableScan (org.apache.calcite.rel.core.TableScan)51 RelNode (org.apache.calcite.rel.RelNode)19 ArrayList (java.util.ArrayList)13 PlannerSettings (org.apache.drill.exec.planner.physical.PlannerSettings)13 Project (org.apache.calcite.rel.core.Project)12 DrillScanRel (org.apache.drill.exec.planner.logical.DrillScanRel)11 Filter (org.apache.calcite.rel.core.Filter)10 IOException (java.io.IOException)9 RexNode (org.apache.calcite.rex.RexNode)9 GroupScan (org.apache.drill.exec.physical.base.GroupScan)9 RelOptRuleCall (org.apache.calcite.plan.RelOptRuleCall)8 DrillFilterRel (org.apache.drill.exec.planner.logical.DrillFilterRel)8 Aggregate (org.apache.calcite.rel.core.Aggregate)7 LogicalProject (org.apache.calcite.rel.logical.LogicalProject)7 RexBuilder (org.apache.calcite.rex.RexBuilder)6 RelOptCluster (org.apache.calcite.plan.RelOptCluster)5 RelShuttleImpl (org.apache.calcite.rel.RelShuttleImpl)5 LogicalJoin (org.apache.calcite.rel.logical.LogicalJoin)5 RelDataType (org.apache.calcite.rel.type.RelDataType)5 SchemaPath (org.apache.drill.common.expression.SchemaPath)5