Search in sources :

Example 66 with RelCollation

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelCollation in project flink by apache.

the class BatchPhysicalPythonAggregateRule method convert.

@Override
public RelNode convert(RelNode relNode) {
    FlinkLogicalAggregate agg = (FlinkLogicalAggregate) relNode;
    RelNode input = agg.getInput();
    int[] groupSet = agg.getGroupSet().toArray();
    RelTraitSet traitSet = relNode.getTraitSet().replace(FlinkConventions.BATCH_PHYSICAL());
    Tuple2<int[], Seq<AggregateCall>> auxGroupSetAndCallsTuple = AggregateUtil.checkAndSplitAggCalls(agg);
    int[] auxGroupSet = auxGroupSetAndCallsTuple._1;
    Seq<AggregateCall> aggCallsWithoutAuxGroupCalls = auxGroupSetAndCallsTuple._2;
    Tuple3<int[][], DataType[][], UserDefinedFunction[]> aggBufferTypesAndFunctions = AggregateUtil.transformToBatchAggregateFunctions(FlinkTypeFactory.toLogicalRowType(input.getRowType()), aggCallsWithoutAuxGroupCalls, null);
    UserDefinedFunction[] aggFunctions = aggBufferTypesAndFunctions._3();
    RelTraitSet requiredTraitSet = input.getTraitSet().replace(FlinkConventions.BATCH_PHYSICAL());
    if (groupSet.length != 0) {
        FlinkRelDistribution requiredDistribution = FlinkRelDistribution.hash(groupSet, false);
        requiredTraitSet = requiredTraitSet.replace(requiredDistribution);
        RelCollation sortCollation = createRelCollation(groupSet);
        requiredTraitSet = requiredTraitSet.replace(sortCollation);
    } else {
        requiredTraitSet = requiredTraitSet.replace(FlinkRelDistribution.SINGLETON());
    }
    RelNode convInput = RelOptRule.convert(input, requiredTraitSet);
    return new BatchPhysicalPythonGroupAggregate(relNode.getCluster(), traitSet, convInput, agg.getRowType(), convInput.getRowType(), convInput.getRowType(), groupSet, auxGroupSet, aggCallsWithoutAuxGroupCalls, aggFunctions);
}
Also used : UserDefinedFunction(org.apache.flink.table.functions.UserDefinedFunction) RelTraitSet(org.apache.calcite.plan.RelTraitSet) AggregateCall(org.apache.calcite.rel.core.AggregateCall) FlinkRelDistribution(org.apache.flink.table.planner.plan.trait.FlinkRelDistribution) RelCollation(org.apache.calcite.rel.RelCollation) RelNode(org.apache.calcite.rel.RelNode) BatchPhysicalPythonGroupAggregate(org.apache.flink.table.planner.plan.nodes.physical.batch.BatchPhysicalPythonGroupAggregate) FlinkLogicalAggregate(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalAggregate) DataType(org.apache.flink.table.types.DataType) Seq(scala.collection.Seq)

Example 67 with RelCollation

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelCollation in project flink by apache.

the class BatchPhysicalPythonWindowAggregateRule method onMatch.

@Override
public void onMatch(RelOptRuleCall call) {
    FlinkLogicalWindowAggregate agg = call.rel(0);
    RelNode input = agg.getInput();
    LogicalWindow window = agg.getWindow();
    if (!(window instanceof TumblingGroupWindow && AggregateUtil.hasTimeIntervalType(((TumblingGroupWindow) window).size()) || window instanceof SlidingGroupWindow && AggregateUtil.hasTimeIntervalType(((SlidingGroupWindow) window).size()) || window instanceof SessionGroupWindow)) {
        // sliding & tumbling count window and session window not supported
        throw new TableException("Window " + window + " is not supported right now.");
    }
    int[] groupSet = agg.getGroupSet().toArray();
    RelTraitSet traitSet = agg.getTraitSet().replace(FlinkConventions.BATCH_PHYSICAL());
    Tuple2<int[], Seq<AggregateCall>> auxGroupSetAndCallsTuple = AggregateUtil.checkAndSplitAggCalls(agg);
    int[] auxGroupSet = auxGroupSetAndCallsTuple._1;
    Seq<AggregateCall> aggCallsWithoutAuxGroupCalls = auxGroupSetAndCallsTuple._2;
    Tuple3<int[][], DataType[][], UserDefinedFunction[]> aggBufferTypesAndFunctions = AggregateUtil.transformToBatchAggregateFunctions(FlinkTypeFactory.toLogicalRowType(input.getRowType()), aggCallsWithoutAuxGroupCalls, null);
    UserDefinedFunction[] aggFunctions = aggBufferTypesAndFunctions._3();
    int inputTimeFieldIndex = AggregateUtil.timeFieldIndex(input.getRowType(), call.builder(), window.timeAttribute());
    RelDataType inputTimeFieldType = input.getRowType().getFieldList().get(inputTimeFieldIndex).getType();
    boolean inputTimeIsDate = inputTimeFieldType.getSqlTypeName() == SqlTypeName.DATE;
    RelTraitSet requiredTraitSet = agg.getTraitSet().replace(FlinkConventions.BATCH_PHYSICAL());
    if (groupSet.length != 0) {
        FlinkRelDistribution requiredDistribution = FlinkRelDistribution.hash(groupSet, false);
        requiredTraitSet = requiredTraitSet.replace(requiredDistribution);
    } else {
        requiredTraitSet = requiredTraitSet.replace(FlinkRelDistribution.SINGLETON());
    }
    RelCollation sortCollation = createRelCollation(groupSet, inputTimeFieldIndex);
    requiredTraitSet = requiredTraitSet.replace(sortCollation);
    RelNode newInput = RelOptRule.convert(input, requiredTraitSet);
    BatchPhysicalPythonGroupWindowAggregate windowAgg = new BatchPhysicalPythonGroupWindowAggregate(agg.getCluster(), traitSet, newInput, agg.getRowType(), newInput.getRowType(), groupSet, auxGroupSet, aggCallsWithoutAuxGroupCalls, aggFunctions, window, inputTimeFieldIndex, inputTimeIsDate, agg.getNamedProperties());
    call.transformTo(windowAgg);
}
Also used : TableException(org.apache.flink.table.api.TableException) UserDefinedFunction(org.apache.flink.table.functions.UserDefinedFunction) BatchPhysicalPythonGroupWindowAggregate(org.apache.flink.table.planner.plan.nodes.physical.batch.BatchPhysicalPythonGroupWindowAggregate) RelDataType(org.apache.calcite.rel.type.RelDataType) RelTraitSet(org.apache.calcite.plan.RelTraitSet) AggregateCall(org.apache.calcite.rel.core.AggregateCall) FlinkRelDistribution(org.apache.flink.table.planner.plan.trait.FlinkRelDistribution) RelCollation(org.apache.calcite.rel.RelCollation) LogicalWindow(org.apache.flink.table.planner.plan.logical.LogicalWindow) TumblingGroupWindow(org.apache.flink.table.planner.plan.logical.TumblingGroupWindow) RelNode(org.apache.calcite.rel.RelNode) FlinkLogicalWindowAggregate(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalWindowAggregate) DataType(org.apache.flink.table.types.DataType) RelDataType(org.apache.calcite.rel.type.RelDataType) SlidingGroupWindow(org.apache.flink.table.planner.plan.logical.SlidingGroupWindow) SessionGroupWindow(org.apache.flink.table.planner.plan.logical.SessionGroupWindow) Seq(scala.collection.Seq)

Example 68 with RelCollation

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelCollation in project calcite by apache.

the class LogicalCalc method create.

public static LogicalCalc create(final RelNode input, final RexProgram program) {
    final RelOptCluster cluster = input.getCluster();
    final RelMetadataQuery mq = cluster.getMetadataQuery();
    final RelTraitSet traitSet = cluster.traitSet().replace(Convention.NONE).replaceIfs(RelCollationTraitDef.INSTANCE, new Supplier<List<RelCollation>>() {

        public List<RelCollation> get() {
            return RelMdCollation.calc(mq, input, program);
        }
    }).replaceIf(RelDistributionTraitDef.INSTANCE, new Supplier<RelDistribution>() {

        public RelDistribution get() {
            return RelMdDistribution.calc(mq, input, program);
        }
    });
    return new LogicalCalc(cluster, traitSet, input, program);
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) RelMetadataQuery(org.apache.calcite.rel.metadata.RelMetadataQuery) RelCollation(org.apache.calcite.rel.RelCollation) Supplier(com.google.common.base.Supplier) RelTraitSet(org.apache.calcite.plan.RelTraitSet) RelDistribution(org.apache.calcite.rel.RelDistribution)

Example 69 with RelCollation

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelCollation in project calcite by apache.

the class LogicalFilter method create.

/**
 * Creates a LogicalFilter.
 */
public static LogicalFilter create(final RelNode input, RexNode condition, ImmutableSet<CorrelationId> variablesSet) {
    final RelOptCluster cluster = input.getCluster();
    final RelMetadataQuery mq = cluster.getMetadataQuery();
    final RelTraitSet traitSet = cluster.traitSetOf(Convention.NONE).replaceIfs(RelCollationTraitDef.INSTANCE, new Supplier<List<RelCollation>>() {

        public List<RelCollation> get() {
            return RelMdCollation.filter(mq, input);
        }
    }).replaceIf(RelDistributionTraitDef.INSTANCE, new Supplier<RelDistribution>() {

        public RelDistribution get() {
            return RelMdDistribution.filter(mq, input);
        }
    });
    return new LogicalFilter(cluster, traitSet, input, condition, variablesSet);
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) RelMetadataQuery(org.apache.calcite.rel.metadata.RelMetadataQuery) RelCollation(org.apache.calcite.rel.RelCollation) Supplier(com.google.common.base.Supplier) RelTraitSet(org.apache.calcite.plan.RelTraitSet) RelDistribution(org.apache.calcite.rel.RelDistribution)

Example 70 with RelCollation

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelCollation in project calcite by apache.

the class SqlToRelConverter method trimUnusedFields.

/**
 * Walks over a tree of relational expressions, replacing each
 * {@link RelNode} with a 'slimmed down' relational expression that projects
 * only the fields required by its consumer.
 *
 * <p>This may make things easier for the optimizer, by removing crud that
 * would expand the search space, but is difficult for the optimizer itself
 * to do it, because optimizer rules must preserve the number and type of
 * fields. Hence, this transform that operates on the entire tree, similar
 * to the {@link RelStructuredTypeFlattener type-flattening transform}.
 *
 * <p>Currently this functionality is disabled in farrago/luciddb; the
 * default implementation of this method does nothing.
 *
 * @param ordered Whether the relational expression must produce results in
 * a particular order (typically because it has an ORDER BY at top level)
 * @param rootRel Relational expression that is at the root of the tree
 * @return Trimmed relational expression
 */
public RelNode trimUnusedFields(boolean ordered, RelNode rootRel) {
    // Trim fields that are not used by their consumer.
    if (isTrimUnusedFields()) {
        final RelFieldTrimmer trimmer = newFieldTrimmer();
        final List<RelCollation> collations = rootRel.getTraitSet().getTraits(RelCollationTraitDef.INSTANCE);
        rootRel = trimmer.trim(rootRel);
        if (!ordered && collations != null && !collations.isEmpty() && !collations.equals(ImmutableList.of(RelCollations.EMPTY))) {
            final RelTraitSet traitSet = rootRel.getTraitSet().replace(RelCollationTraitDef.INSTANCE, collations);
            rootRel = rootRel.copy(traitSet, rootRel.getInputs());
        }
        if (SQL2REL_LOGGER.isDebugEnabled()) {
            SQL2REL_LOGGER.debug(RelOptUtil.dumpPlan("Plan after trimming unused fields", rootRel, SqlExplainFormat.TEXT, SqlExplainLevel.EXPPLAN_ATTRIBUTES));
        }
    }
    return rootRel;
}
Also used : RelCollation(org.apache.calcite.rel.RelCollation) RelTraitSet(org.apache.calcite.plan.RelTraitSet)

Aggregations

RelCollation (org.apache.calcite.rel.RelCollation)83 RelNode (org.apache.calcite.rel.RelNode)40 RelTraitSet (org.apache.calcite.plan.RelTraitSet)37 RelFieldCollation (org.apache.calcite.rel.RelFieldCollation)33 RexNode (org.apache.calcite.rex.RexNode)24 ArrayList (java.util.ArrayList)19 RelDataType (org.apache.calcite.rel.type.RelDataType)17 RelOptCluster (org.apache.calcite.plan.RelOptCluster)15 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)13 RelMetadataQuery (org.apache.calcite.rel.metadata.RelMetadataQuery)12 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)12 List (java.util.List)11 Sort (org.apache.calcite.rel.core.Sort)11 ImmutableList (com.google.common.collect.ImmutableList)8 RelDistribution (org.apache.calcite.rel.RelDistribution)8 RexInputRef (org.apache.calcite.rex.RexInputRef)8 Map (java.util.Map)7 Mappings (org.apache.calcite.util.mapping.Mappings)7 Supplier (com.google.common.base.Supplier)6 HashMap (java.util.HashMap)6