Search in sources :

Example 41 with RelOptCluster

use of org.apache.calcite.plan.RelOptCluster in project flink by apache.

the class LogicalDistribution method create.

public static LogicalDistribution create(RelNode input, RelCollation collation, List<Integer> distKeys) {
    RelOptCluster cluster = input.getCluster();
    collation = RelCollationTraitDef.INSTANCE.canonize(collation);
    RelTraitSet traitSet = input.getTraitSet().replace(Convention.NONE).replace(collation);
    return new LogicalDistribution(cluster, traitSet, input, collation, distKeys);
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) RelTraitSet(org.apache.calcite.plan.RelTraitSet)

Example 42 with RelOptCluster

use of org.apache.calcite.plan.RelOptCluster in project flink by apache.

the class SubQueryDecorrelator method decorrelateQuery.

/**
 * Decorrelates a subquery.
 *
 * <p>This is the main entry point to {@code SubQueryDecorrelator}.
 *
 * @param rootRel The node which has SubQuery.
 * @return Decorrelate result.
 */
public static Result decorrelateQuery(RelNode rootRel) {
    int maxCnfNodeCount = FlinkRelOptUtil.getMaxCnfNodeCount(rootRel);
    final CorelMapBuilder builder = new CorelMapBuilder(maxCnfNodeCount);
    final CorelMap corelMap = builder.build(rootRel);
    if (builder.hasNestedCorScope || builder.hasUnsupportedCorCondition) {
        return null;
    }
    if (!corelMap.hasCorrelation()) {
        return Result.EMPTY;
    }
    RelOptCluster cluster = rootRel.getCluster();
    RelBuilder relBuilder = new FlinkRelBuilder(cluster.getPlanner().getContext(), cluster, null);
    RexBuilder rexBuilder = cluster.getRexBuilder();
    final SubQueryDecorrelator decorrelator = new SubQueryDecorrelator(new SubQueryRelDecorrelator(corelMap, relBuilder, rexBuilder, maxCnfNodeCount), relBuilder);
    rootRel.accept(decorrelator);
    return new Result(decorrelator.subQueryMap);
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) RelBuilder(org.apache.calcite.tools.RelBuilder) FlinkRelBuilder(org.apache.flink.table.planner.calcite.FlinkRelBuilder) FlinkRelBuilder(org.apache.flink.table.planner.calcite.FlinkRelBuilder) RexBuilder(org.apache.calcite.rex.RexBuilder)

Example 43 with RelOptCluster

use of org.apache.calcite.plan.RelOptCluster in project flink by apache.

the class StreamPhysicalPythonGroupWindowAggregateRule method convert.

@Override
public RelNode convert(RelNode rel) {
    FlinkLogicalWindowAggregate agg = (FlinkLogicalWindowAggregate) rel;
    LogicalWindow window = agg.getWindow();
    List<AggregateCall> aggCalls = agg.getAggCallList();
    boolean isPandasPythonUDAF = aggCalls.stream().anyMatch(x -> PythonUtil.isPythonAggregate(x, PythonFunctionKind.PANDAS));
    if (isPandasPythonUDAF && window instanceof SessionGroupWindow) {
        throw new TableException("Session Group Window is currently not supported for Pandas UDAF.");
    }
    RelNode input = agg.getInput();
    RelOptCluster cluster = rel.getCluster();
    FlinkRelDistribution requiredDistribution;
    if (agg.getGroupCount() != 0) {
        requiredDistribution = FlinkRelDistribution.hash(agg.getGroupSet().asList(), true);
    } else {
        requiredDistribution = FlinkRelDistribution.SINGLETON();
    }
    RelTraitSet requiredTraitSet = input.getTraitSet().replace(FlinkConventions.STREAM_PHYSICAL()).replace(requiredDistribution);
    RelTraitSet providedTraitSet = rel.getTraitSet().replace(FlinkConventions.STREAM_PHYSICAL());
    RelNode newInput = RelOptRule.convert(input, requiredTraitSet);
    ReadableConfig config = ShortcutUtils.unwrapTableConfig(rel);
    WindowEmitStrategy emitStrategy = WindowEmitStrategy.apply(config, agg.getWindow());
    if (emitStrategy.produceUpdates()) {
        throw new TableException("Python Group Window Aggregate Function is currently not supported for early fired or lately fired.");
    }
    return new StreamPhysicalPythonGroupWindowAggregate(cluster, providedTraitSet, newInput, rel.getRowType(), agg.getGroupSet().toArray(), JavaScalaConversionUtil.toScala(aggCalls), agg.getWindow(), agg.getNamedProperties(), emitStrategy);
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) StreamPhysicalPythonGroupWindowAggregate(org.apache.flink.table.planner.plan.nodes.physical.stream.StreamPhysicalPythonGroupWindowAggregate) TableException(org.apache.flink.table.api.TableException) RelTraitSet(org.apache.calcite.plan.RelTraitSet) AggregateCall(org.apache.calcite.rel.core.AggregateCall) FlinkRelDistribution(org.apache.flink.table.planner.plan.trait.FlinkRelDistribution) ReadableConfig(org.apache.flink.configuration.ReadableConfig) LogicalWindow(org.apache.flink.table.planner.plan.logical.LogicalWindow) RelNode(org.apache.calcite.rel.RelNode) WindowEmitStrategy(org.apache.flink.table.planner.plan.utils.WindowEmitStrategy) FlinkLogicalWindowAggregate(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalWindowAggregate) SessionGroupWindow(org.apache.flink.table.planner.plan.logical.SessionGroupWindow)

Example 44 with RelOptCluster

use of org.apache.calcite.plan.RelOptCluster in project flink by apache.

the class ProjectWindowTableFunctionTransposeRule method createNewTableFunctionScan.

private LogicalTableFunctionScan createNewTableFunctionScan(RelBuilder relBuilder, LogicalTableFunctionScan oldScan, LogicalType timeAttributeType, RelNode newInput, Map<Integer, Integer> mapping) {
    relBuilder.push(newInput);
    RexNode newCall = rewriteWindowCall((RexCall) oldScan.getCall(), mapping, relBuilder);
    RelOptCluster cluster = oldScan.getCluster();
    FlinkTypeFactory typeFactory = (FlinkTypeFactory) cluster.getTypeFactory();
    RelDataType newScanOutputType = SqlWindowTableFunction.inferRowType(typeFactory, newInput.getRowType(), typeFactory.createFieldTypeFromLogicalType(timeAttributeType));
    return LogicalTableFunctionScan.create(cluster, new ArrayList<>(Collections.singleton(newInput)), newCall, oldScan.getElementType(), newScanOutputType, oldScan.getColumnMappings());
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) FlinkTypeFactory(org.apache.flink.table.planner.calcite.FlinkTypeFactory) RelDataType(org.apache.calcite.rel.type.RelDataType) RexNode(org.apache.calcite.rex.RexNode)

Example 45 with RelOptCluster

use of org.apache.calcite.plan.RelOptCluster in project hive by apache.

the class HiveRelMdRuntimeRowCount method getRuntimeRowCount.

public Optional<Long> getRuntimeRowCount(RelNode rel) {
    RelOptCluster cluster = rel.getCluster();
    Context context = cluster.getPlanner().getContext();
    if (context instanceof HivePlannerContext) {
        StatsSource ss = ((HivePlannerContext) context).unwrap(StatsSource.class);
        if (ss.canProvideStatsFor(rel.getClass())) {
            Optional<OperatorStats> os = ss.lookup(RelTreeSignature.of(rel));
            if (os.isPresent()) {
                long outputRecords = os.get().getOutputRecords();
                return Optional.of(outputRecords);
            }
        }
    }
    return Optional.empty();
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) Context(org.apache.calcite.plan.Context) HivePlannerContext(org.apache.hadoop.hive.ql.optimizer.calcite.HivePlannerContext) HivePlannerContext(org.apache.hadoop.hive.ql.optimizer.calcite.HivePlannerContext) OperatorStats(org.apache.hadoop.hive.ql.stats.OperatorStats) StatsSource(org.apache.hadoop.hive.ql.plan.mapper.StatsSource)

Aggregations

RelOptCluster (org.apache.calcite.plan.RelOptCluster)117 RelNode (org.apache.calcite.rel.RelNode)63 RelTraitSet (org.apache.calcite.plan.RelTraitSet)36 RexBuilder (org.apache.calcite.rex.RexBuilder)35 RexNode (org.apache.calcite.rex.RexNode)31 ArrayList (java.util.ArrayList)25 RelDataType (org.apache.calcite.rel.type.RelDataType)23 Test (org.junit.Test)21 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)15 List (java.util.List)13 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)13 RelBuilder (org.apache.calcite.tools.RelBuilder)13 RelCollation (org.apache.calcite.rel.RelCollation)12 RelMetadataQuery (org.apache.calcite.rel.metadata.RelMetadataQuery)11 RelOptTable (org.apache.calcite.plan.RelOptTable)10 ImmutableList (com.google.common.collect.ImmutableList)9 HashMap (java.util.HashMap)9 RelOptPlanner (org.apache.calcite.plan.RelOptPlanner)9 Join (org.apache.calcite.rel.core.Join)9 LogicalJoin (org.apache.calcite.rel.logical.LogicalJoin)9