Search in sources :

Example 81 with RelNode

use of org.apache.calcite.rel.RelNode in project drill by apache.

the class StreamAggPrule method createTransformRequest.

private void createTransformRequest(RelOptRuleCall call, DrillAggregateRel aggregate, RelNode input, RelTraitSet traits) throws InvalidRelException {
    final RelNode convertedInput = convert(input, traits);
    StreamAggPrel newAgg = new StreamAggPrel(aggregate.getCluster(), traits, convertedInput, aggregate.indicator, aggregate.getGroupSet(), aggregate.getGroupSets(), aggregate.getAggCallList(), OperatorPhase.PHASE_1of1);
    call.transformTo(newAgg);
}
Also used : RelNode(org.apache.calcite.rel.RelNode)

Example 82 with RelNode

use of org.apache.calcite.rel.RelNode in project drill by apache.

the class LimitUnionExchangeTransposeRule method onMatch.

@Override
public void onMatch(RelOptRuleCall call) {
    final LimitPrel limit = (LimitPrel) call.rel(0);
    final UnionExchangePrel unionExchangePrel = (UnionExchangePrel) call.rel(1);
    RelNode child = unionExchangePrel.getInput();
    final int offset = limit.getOffset() != null ? Math.max(0, RexLiteral.intValue(limit.getOffset())) : 0;
    final int fetch = Math.max(0, RexLiteral.intValue(limit.getFetch()));
    // child Limit uses conservative approach:  use offset 0 and fetch = parent limit offset + parent limit fetch.
    final RexNode childFetch = limit.getCluster().getRexBuilder().makeExactLiteral(BigDecimal.valueOf(offset + fetch));
    final RelNode limitUnderExchange = new LimitPrel(child.getCluster(), child.getTraitSet(), child, null, childFetch);
    final RelNode newUnionExch = new UnionExchangePrel(unionExchangePrel.getCluster(), unionExchangePrel.getTraitSet(), limitUnderExchange);
    final RelNode limitAboveExchange = new LimitPrel(limit.getCluster(), limit.getTraitSet(), newUnionExch, limit.getOffset(), limit.getFetch(), true);
    call.transformTo(limitAboveExchange);
}
Also used : RelNode(org.apache.calcite.rel.RelNode) RexNode(org.apache.calcite.rex.RexNode)

Example 83 with RelNode

use of org.apache.calcite.rel.RelNode in project drill by apache.

the class SqlConverter method toRel.

public RelNode toRel(final SqlNode validatedNode) {
    final RexBuilder rexBuilder = new DrillRexBuilder(typeFactory);
    if (planner == null) {
        planner = new VolcanoPlanner(costFactory, settings);
        planner.setExecutor(new DrillConstExecutor(functions, util, settings));
        planner.clearRelTraitDefs();
        planner.addRelTraitDef(ConventionTraitDef.INSTANCE);
        planner.addRelTraitDef(DrillDistributionTraitDef.INSTANCE);
        planner.addRelTraitDef(RelCollationTraitDef.INSTANCE);
    }
    final RelOptCluster cluster = RelOptCluster.create(planner, rexBuilder);
    final SqlToRelConverter sqlToRelConverter = new SqlToRelConverter(new Expander(), validator, catalog, cluster, DrillConvertletTable.INSTANCE, sqlToRelConverterConfig);
    final RelNode rel = sqlToRelConverter.convertQuery(validatedNode, false, !isInnerQuery);
    final RelNode rel2 = sqlToRelConverter.flattenTypes(rel, true);
    final RelNode rel3 = RelDecorrelator.decorrelateQuery(rel2);
    return rel3;
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) SqlToRelConverter(org.apache.calcite.sql2rel.SqlToRelConverter) RelNode(org.apache.calcite.rel.RelNode) RexBuilder(org.apache.calcite.rex.RexBuilder) VolcanoPlanner(org.apache.calcite.plan.volcano.VolcanoPlanner) DrillConstExecutor(org.apache.drill.exec.planner.logical.DrillConstExecutor)

Example 84 with RelNode

use of org.apache.calcite.rel.RelNode in project drill by apache.

the class DefaultSqlHandler method convertToDrel.

/**
   *  Given a relNode tree for SELECT statement, convert to Drill Logical RelNode tree.
   * @param relNode
   * @return
   * @throws SqlUnsupportedException
   * @throws RelConversionException
   */
protected DrillRel convertToDrel(final RelNode relNode) throws SqlUnsupportedException, RelConversionException {
    if (context.getOptions().getOption(ExecConstants.EARLY_LIMIT0_OPT) && context.getPlannerSettings().isTypeInferenceEnabled() && FindLimit0Visitor.containsLimit0(relNode)) {
        // if the schema is known, return the schema directly
        final DrillRel shorterPlan;
        if ((shorterPlan = FindLimit0Visitor.getDirectScanRelIfFullySchemaed(relNode)) != null) {
            return shorterPlan;
        }
        if (FindHardDistributionScans.canForceSingleMode(relNode)) {
            // disable distributed mode
            context.getPlannerSettings().forceSingleMode();
        }
    }
    try {
        final RelNode convertedRelNode;
        // HEP Directory pruning .
        final RelNode pruned = transform(PlannerType.HEP_BOTTOM_UP, PlannerPhase.DIRECTORY_PRUNING, relNode);
        final RelTraitSet logicalTraits = pruned.getTraitSet().plus(DrillRel.DRILL_LOGICAL);
        if (!context.getPlannerSettings().isHepOptEnabled()) {
            // hep is disabled, use volcano
            convertedRelNode = transform(PlannerType.VOLCANO, PlannerPhase.LOGICAL_PRUNE_AND_JOIN, pruned, logicalTraits);
        } else {
            final RelNode intermediateNode2;
            if (context.getPlannerSettings().isHepPartitionPruningEnabled()) {
                // hep is enabled and hep pruning is enabled.
                final RelNode intermediateNode = transform(PlannerType.VOLCANO, PlannerPhase.LOGICAL, pruned, logicalTraits);
                intermediateNode2 = transform(PlannerType.HEP_BOTTOM_UP, PlannerPhase.PARTITION_PRUNING, intermediateNode);
            } else {
                // Only hep is enabled
                intermediateNode2 = transform(PlannerType.VOLCANO, PlannerPhase.LOGICAL_PRUNE, pruned, logicalTraits);
            }
            // Do Join Planning.
            convertedRelNode = transform(PlannerType.HEP_BOTTOM_UP, PlannerPhase.JOIN_PLANNING, intermediateNode2);
        }
        // Convert SUM to $SUM0
        final RelNode convertedRelNodeWithSum0 = transform(PlannerType.HEP_BOTTOM_UP, PlannerPhase.SUM_CONVERSION, convertedRelNode);
        final DrillRel drillRel = (DrillRel) convertedRelNodeWithSum0;
        if (drillRel instanceof DrillStoreRel) {
            throw new UnsupportedOperationException();
        } else {
            // If the query contains a limit 0 clause, disable distributed mode since it is overkill for determining schema.
            if (FindLimit0Visitor.containsLimit0(convertedRelNodeWithSum0) && FindHardDistributionScans.canForceSingleMode(convertedRelNodeWithSum0)) {
                context.getPlannerSettings().forceSingleMode();
            }
            return drillRel;
        }
    } catch (RelOptPlanner.CannotPlanException ex) {
        logger.error(ex.getMessage());
        if (JoinUtils.checkCartesianJoin(relNode, new ArrayList<Integer>(), new ArrayList<Integer>(), new ArrayList<Boolean>())) {
            throw new UnsupportedRelOperatorException("This query cannot be planned possibly due to either a cartesian join or an inequality join");
        } else {
            throw ex;
        }
    }
}
Also used : RelNode(org.apache.calcite.rel.RelNode) UnsupportedRelOperatorException(org.apache.drill.exec.work.foreman.UnsupportedRelOperatorException) DrillStoreRel(org.apache.drill.exec.planner.logical.DrillStoreRel) ArrayList(java.util.ArrayList) DrillRel(org.apache.drill.exec.planner.logical.DrillRel) RelTraitSet(org.apache.calcite.plan.RelTraitSet) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner)

Example 85 with RelNode

use of org.apache.calcite.rel.RelNode in project drill by apache.

the class DefaultSqlHandler method convertToRel.

private RelNode convertToRel(SqlNode node) throws RelConversionException {
    final RelNode convertedNode = config.getConverter().toRel(node);
    log("INITIAL", convertedNode, logger, null);
    return transform(PlannerType.HEP, PlannerPhase.WINDOW_REWRITE, convertedNode);
}
Also used : RelNode(org.apache.calcite.rel.RelNode)

Aggregations

RelNode (org.apache.calcite.rel.RelNode)219 RexNode (org.apache.calcite.rex.RexNode)75 ArrayList (java.util.ArrayList)50 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)30 RelDataType (org.apache.calcite.rel.type.RelDataType)27 RelTraitSet (org.apache.calcite.plan.RelTraitSet)25 HashMap (java.util.HashMap)24 RexBuilder (org.apache.calcite.rex.RexBuilder)24 RexInputRef (org.apache.calcite.rex.RexInputRef)21 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)20 Prel (org.apache.drill.exec.planner.physical.Prel)20 AggregateCall (org.apache.calcite.rel.core.AggregateCall)16 Pair (org.apache.calcite.util.Pair)16 ImmutableList (com.google.common.collect.ImmutableList)15 Project (org.apache.calcite.rel.core.Project)14 RelOptCluster (org.apache.calcite.plan.RelOptCluster)13 HiveProject (org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveProject)13 TreeMap (java.util.TreeMap)11 HashSet (java.util.HashSet)10 RelCollation (org.apache.calcite.rel.RelCollation)10