Search in sources :

Example 6 with RexShuttle

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexShuttle in project flink by apache.

the class WatermarkAssignerChangelogNormalizeTransposeRule method pushDownTransformedWatermark.

private RelNode pushDownTransformedWatermark(StreamPhysicalWatermarkAssigner watermark, StreamPhysicalCalc calc, StreamPhysicalChangelogNormalize changelogNormalize, StreamPhysicalExchange exchange, Mappings.TargetMapping calcMapping, RexBuilder rexBuilder) {
    Mappings.TargetMapping inversedMapping = calcMapping.inverse();
    final int newRowTimeFieldIndex = inversedMapping.getTargetOpt(watermark.rowtimeFieldIndex());
    // Updates watermark properties after push down before Calc
    // 1. rewrites watermark expression
    // 2. clears distribution
    // 3. updates row time field index
    RexNode newWatermarkExpr = watermark.watermarkExpr();
    if (watermark.watermarkExpr() != null) {
        newWatermarkExpr = RexUtil.apply(inversedMapping, watermark.watermarkExpr());
    }
    final RelNode newWatermark = watermark.copy(watermark.getTraitSet().plus(FlinkRelDistribution.DEFAULT()), exchange.getInput(), newRowTimeFieldIndex, newWatermarkExpr);
    final RelNode newChangelogNormalize = buildTreeInOrder(newWatermark, Tuple2.of(exchange, exchange.getTraitSet()), Tuple2.of(changelogNormalize, changelogNormalize.getTraitSet()));
    // Rewrites Calc program because the field type of row time
    // field is changed after watermark pushed down
    final RexProgram oldProgram = calc.getProgram();
    final RexProgramBuilder programBuilder = new RexProgramBuilder(newChangelogNormalize.getRowType(), rexBuilder);
    final Function<RexNode, RexNode> rexShuttle = e -> e.accept(new RexShuttle() {

        @Override
        public RexNode visitInputRef(RexInputRef inputRef) {
            if (inputRef.getIndex() == newRowTimeFieldIndex) {
                return RexInputRef.of(newRowTimeFieldIndex, newChangelogNormalize.getRowType());
            } else {
                return inputRef;
            }
        }
    });
    oldProgram.getNamedProjects().forEach(pair -> programBuilder.addProject(rexShuttle.apply(oldProgram.expandLocalRef(pair.left)), pair.right));
    if (oldProgram.getCondition() != null) {
        programBuilder.addCondition(rexShuttle.apply(oldProgram.expandLocalRef(oldProgram.getCondition())));
    }
    final RexProgram newProgram = programBuilder.getProgram();
    return calc.copy(calc.getTraitSet(), newChangelogNormalize, newProgram);
}
Also used : Mappings(org.apache.calcite.util.mapping.Mappings) RexProgram(org.apache.calcite.rex.RexProgram) StreamPhysicalExchange(org.apache.flink.table.planner.plan.nodes.physical.stream.StreamPhysicalExchange) Tuple2(org.apache.flink.api.java.tuple.Tuple2) HashMap(java.util.HashMap) FlinkRelDistribution(org.apache.flink.table.planner.plan.trait.FlinkRelDistribution) RelOptUtil(org.apache.calcite.plan.RelOptUtil) Function(java.util.function.Function) ArrayList(java.util.ArrayList) RexUtil(org.apache.calcite.rex.RexUtil) StreamPhysicalChangelogNormalize(org.apache.flink.table.planner.plan.nodes.physical.stream.StreamPhysicalChangelogNormalize) RexNode(org.apache.calcite.rex.RexNode) Map(java.util.Map) RelTraitSet(org.apache.calcite.plan.RelTraitSet) FlinkRelDistributionTraitDef(org.apache.flink.table.planner.plan.trait.FlinkRelDistributionTraitDef) RelDataType(org.apache.calcite.rel.type.RelDataType) RexBuilder(org.apache.calcite.rex.RexBuilder) StreamPhysicalCalc(org.apache.flink.table.planner.plan.nodes.physical.stream.StreamPhysicalCalc) RelRule(org.apache.calcite.plan.RelRule) RelNode(org.apache.calcite.rel.RelNode) RelOptRuleCall(org.apache.calcite.plan.RelOptRuleCall) RowTypeUtils(org.apache.flink.table.planner.typeutils.RowTypeUtils) RexInputRef(org.apache.calcite.rex.RexInputRef) RelOptRule(org.apache.calcite.plan.RelOptRule) RexProgramBuilder(org.apache.calcite.rex.RexProgramBuilder) List(java.util.List) Preconditions.checkArgument(org.apache.flink.util.Preconditions.checkArgument) RexLocalRef(org.apache.calcite.rex.RexLocalRef) RelDistribution(org.apache.calcite.rel.RelDistribution) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) StreamPhysicalWatermarkAssigner(org.apache.flink.table.planner.plan.nodes.physical.stream.StreamPhysicalWatermarkAssigner) RexShuttle(org.apache.calcite.rex.RexShuttle) Collections(java.util.Collections) RexShuttle(org.apache.calcite.rex.RexShuttle) Mappings(org.apache.calcite.util.mapping.Mappings) RelNode(org.apache.calcite.rel.RelNode) RexProgram(org.apache.calcite.rex.RexProgram) RexInputRef(org.apache.calcite.rex.RexInputRef) RexProgramBuilder(org.apache.calcite.rex.RexProgramBuilder) RexNode(org.apache.calcite.rex.RexNode)

Example 7 with RexShuttle

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexShuttle in project flink by apache.

the class RelTimeIndicatorConverter method visitJoin.

private RelNode visitJoin(FlinkLogicalJoin join) {
    RelNode newLeft = join.getLeft().accept(this);
    RelNode newRight = join.getRight().accept(this);
    int leftFieldCount = newLeft.getRowType().getFieldCount();
    // temporal table join
    if (TemporalJoinUtil.satisfyTemporalJoin(join, newLeft, newRight)) {
        RelNode rewrittenTemporalJoin = join.copy(join.getTraitSet(), join.getCondition(), newLeft, newRight, join.getJoinType(), join.isSemiJoinDone());
        // Materialize all of the time attributes from the right side of temporal join
        Set<Integer> rightIndices = IntStream.range(0, newRight.getRowType().getFieldCount()).mapToObj(startIdx -> leftFieldCount + startIdx).collect(Collectors.toSet());
        return createCalcToMaterializeTimeIndicators(rewrittenTemporalJoin, rightIndices);
    } else {
        if (JoinUtil.satisfyRegularJoin(join, newLeft, newRight)) {
            // materialize time attribute fields of regular join's inputs
            newLeft = materializeTimeIndicators(newLeft);
            newRight = materializeTimeIndicators(newRight);
        }
        List<RelDataTypeField> leftRightFields = new ArrayList<>();
        leftRightFields.addAll(newLeft.getRowType().getFieldList());
        leftRightFields.addAll(newRight.getRowType().getFieldList());
        RexNode newCondition = join.getCondition().accept(new RexShuttle() {

            @Override
            public RexNode visitInputRef(RexInputRef inputRef) {
                if (isTimeIndicatorType(inputRef.getType())) {
                    return RexInputRef.of(inputRef.getIndex(), leftRightFields);
                } else {
                    return super.visitInputRef(inputRef);
                }
            }
        });
        return FlinkLogicalJoin.create(newLeft, newRight, newCondition, join.getJoinType());
    }
}
Also used : WindowUtil.groupingContainsWindowStartEnd(org.apache.flink.table.planner.plan.utils.WindowUtil.groupingContainsWindowStartEnd) RexProgram(org.apache.calcite.rex.RexProgram) Tuple2(org.apache.flink.api.java.tuple.Tuple2) FlinkLogicalWindowAggregate(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalWindowAggregate) LogicalTableModify(org.apache.calcite.rel.logical.LogicalTableModify) FlinkLogicalLegacySink(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalLegacySink) FlinkLogicalDistribution(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalDistribution) FlinkLogicalIntersect(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalIntersect) TemporalJoinUtil(org.apache.flink.table.planner.plan.utils.TemporalJoinUtil) RexPatternFieldRef(org.apache.calcite.rex.RexPatternFieldRef) RexNode(org.apache.calcite.rex.RexNode) Map(java.util.Map) FlinkLogicalSnapshot(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalSnapshot) LocalZonedTimestampType(org.apache.flink.table.types.logical.LocalZonedTimestampType) FlinkLogicalMinus(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalMinus) Predicate(java.util.function.Predicate) FlinkLogicalTableFunctionScan(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalTableFunctionScan) FlinkLogicalRank(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalRank) Set(java.util.Set) FlinkLogicalJoin(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalJoin) Preconditions(org.apache.flink.util.Preconditions) Collectors(java.util.stream.Collectors) FlinkLogicalValues(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalValues) RexInputRef(org.apache.calcite.rex.RexInputRef) List(java.util.List) FlinkTypeFactory.isTimeIndicatorType(org.apache.flink.table.planner.calcite.FlinkTypeFactory.isTimeIndicatorType) LogicalType(org.apache.flink.table.types.logical.LogicalType) SqlStdOperatorTable(org.apache.calcite.sql.fun.SqlStdOperatorTable) FlinkLogicalSink(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalSink) RexLocalRef(org.apache.calcite.rex.RexLocalRef) ValidationException(org.apache.flink.table.api.ValidationException) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) FlinkTypeFactory.isRowtimeIndicatorType(org.apache.flink.table.planner.calcite.FlinkTypeFactory.isRowtimeIndicatorType) FlinkLogicalSort(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalSort) FlinkLogicalOverAggregate(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalOverAggregate) RexCall(org.apache.calcite.rex.RexCall) IntStream(java.util.stream.IntStream) TableScan(org.apache.calcite.rel.core.TableScan) LogicalCalc(org.apache.calcite.rel.logical.LogicalCalc) FlinkLogicalMatch(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalMatch) FlinkLogicalUnion(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalUnion) SetOp(org.apache.calcite.rel.core.SetOp) RelWindowProperties(org.apache.flink.table.planner.plan.trait.RelWindowProperties) RelOptUtil(org.apache.calcite.plan.RelOptUtil) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) MatchUtil.isFinalOnMatchTimeIndicator(org.apache.flink.table.planner.plan.utils.MatchUtil.isFinalOnMatchTimeIndicator) TimestampType(org.apache.flink.table.types.logical.TimestampType) Pair(org.apache.calcite.util.Pair) FlinkLogicalAggregate(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalAggregate) TimeIndicatorRelDataType(org.apache.flink.table.planner.plan.schema.TimeIndicatorRelDataType) SqlOperator(org.apache.calcite.sql.SqlOperator) SingleRel(org.apache.calcite.rel.SingleRel) RelCollations(org.apache.calcite.rel.RelCollations) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) FlinkRelMetadataQuery(org.apache.flink.table.planner.plan.metadata.FlinkRelMetadataQuery) RexBuilder(org.apache.calcite.rex.RexBuilder) FlinkTypeFactory.isProctimeIndicatorType(org.apache.flink.table.planner.calcite.FlinkTypeFactory.isProctimeIndicatorType) TableException(org.apache.flink.table.api.TableException) FlinkLogicalTableAggregate(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalTableAggregate) FlinkLogicalCalc(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalCalc) FlinkLogicalWindowTableAggregate(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalWindowTableAggregate) FlinkLogicalExpand(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalExpand) RelNode(org.apache.calcite.rel.RelNode) Aggregate(org.apache.calcite.rel.core.Aggregate) JoinUtil(org.apache.flink.table.planner.plan.utils.JoinUtil) RelHomogeneousShuttle(org.apache.calcite.rel.RelHomogeneousShuttle) RexProgramBuilder(org.apache.calcite.rex.RexProgramBuilder) FlinkSqlOperatorTable(org.apache.flink.table.planner.functions.sql.FlinkSqlOperatorTable) FlinkLogicalWatermarkAssigner(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalWatermarkAssigner) AggregateCall(org.apache.calcite.rel.core.AggregateCall) RexShuttle(org.apache.calcite.rex.RexShuttle) FlinkLogicalCorrelate(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalCorrelate) Collections(java.util.Collections) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RexShuttle(org.apache.calcite.rex.RexShuttle) RelNode(org.apache.calcite.rel.RelNode) ArrayList(java.util.ArrayList) RexInputRef(org.apache.calcite.rex.RexInputRef) RexNode(org.apache.calcite.rex.RexNode)

Example 8 with RexShuttle

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexShuttle in project druid by druid-io.

the class RelParameterizerShuttle method bindRel.

private RelNode bindRel(RelNode node, RexBuilder builder, RelDataTypeFactory typeFactory) {
    final RexShuttle binder = new RexShuttle() {

        @Override
        public RexNode visitDynamicParam(RexDynamicParam dynamicParam) {
            return bind(dynamicParam, builder, typeFactory);
        }
    };
    node = node.accept(binder);
    node.childrenAccept(new RelVisitor() {

        @Override
        public void visit(RelNode node, int ordinal, RelNode parent) {
            super.visit(node, ordinal, parent);
            RelNode transformed = node.accept(binder);
            if (!node.equals(transformed)) {
                parent.replaceInput(ordinal, transformed);
            }
        }
    });
    return node;
}
Also used : RexShuttle(org.apache.calcite.rex.RexShuttle) RelNode(org.apache.calcite.rel.RelNode) RelVisitor(org.apache.calcite.rel.RelVisitor) RexDynamicParam(org.apache.calcite.rex.RexDynamicParam)

Example 9 with RexShuttle

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexShuttle in project hazelcast by hazelcast.

the class JoinNestedLoopPhysicalRel method joinInfo.

public JetJoinInfo joinInfo(QueryParameterMetadata parameterMetadata) {
    List<Integer> leftKeys = analyzeCondition().leftKeys.toIntegerList();
    List<Integer> rightKeys = analyzeCondition().rightKeys.toIntegerList();
    HazelcastTable table = OptUtils.extractHazelcastTable(getRight());
    RexBuilder rexBuilder = getCluster().getRexBuilder();
    List<RexNode> additionalNonEquiConditions = new ArrayList<>();
    for (int i = 0; i < rightKeys.size(); i++) {
        Integer rightKeyIndex = rightKeys.get(i);
        RexNode rightExpr = table.getProjects().get(rightKeyIndex);
        if (rightExpr instanceof RexInputRef) {
            rightKeys.set(i, ((RexInputRef) rightExpr).getIndex());
        } else {
            // offset the indices in rightExp by the width of left row
            rightExpr = rightExpr.accept(new RexShuttle() {

                @Override
                public RexNode visitInputRef(RexInputRef inputRef) {
                    return rexBuilder.makeInputRef(inputRef.getType(), inputRef.getIndex() + getLeft().getRowType().getFieldCount());
                }
            });
            additionalNonEquiConditions.add(rexBuilder.makeCall(HazelcastSqlOperatorTable.EQUALS, rexBuilder.makeInputRef(getLeft(), leftKeys.get(i)), rightExpr));
            leftKeys.remove(i);
            rightKeys.remove(i);
            i--;
        }
    }
    Expression<Boolean> nonEquiCondition = filter(schema(parameterMetadata), RexUtil.composeConjunction(rexBuilder, asList(analyzeCondition().getRemaining(rexBuilder), RexUtil.composeConjunction(rexBuilder, additionalNonEquiConditions))), parameterMetadata);
    Expression<Boolean> condition = filter(schema(parameterMetadata), getCondition(), parameterMetadata);
    return new JetJoinInfo(getJoinType(), toIntArray(leftKeys), toIntArray(rightKeys), nonEquiCondition, condition);
}
Also used : RexShuttle(org.apache.calcite.rex.RexShuttle) ArrayList(java.util.ArrayList) JetJoinInfo(com.hazelcast.jet.sql.impl.JetJoinInfo) RexBuilder(org.apache.calcite.rex.RexBuilder) RexInputRef(org.apache.calcite.rex.RexInputRef) HazelcastTable(com.hazelcast.jet.sql.impl.schema.HazelcastTable) RexNode(org.apache.calcite.rex.RexNode)

Example 10 with RexShuttle

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexShuttle in project hive by apache.

the class HiveSubQueryRemoveRule method onMatch.

@Override
public void onMatch(RelOptRuleCall call) {
    final RelNode relNode = call.rel(0);
    final RelBuilder builder = call.builder();
    // if subquery is in FILTER
    if (relNode instanceof HiveFilter) {
        final HiveFilter filter = call.rel(0);
        final RexSubQuery e = RexUtil.SubQueryFinder.find(filter.getCondition());
        assert e != null;
        final RelOptUtil.Logic logic = LogicVisitor.find(RelOptUtil.Logic.TRUE, ImmutableList.of(filter.getCondition()), e);
        builder.push(filter.getInput());
        final int fieldCount = builder.peek().getRowType().getFieldCount();
        SubqueryConf subqueryConfig = filter.getCluster().getPlanner().getContext().unwrap(SubqueryConf.class);
        boolean isCorrScalarQuery = subqueryConfig.getCorrScalarRexSQWithAgg().contains(e.rel);
        final RexNode target = apply(call.getMetadataQuery(), e, HiveFilter.getVariablesSet(e), logic, builder, 1, fieldCount, isCorrScalarQuery);
        final RexShuttle shuttle = new ReplaceSubQueryShuttle(e, target);
        builder.filter(shuttle.apply(filter.getCondition()));
        builder.project(fields(builder, filter.getRowType().getFieldCount()));
        RelNode newRel = builder.build();
        call.transformTo(newRel);
    } else if (relNode instanceof HiveProject) {
        // if subquery is in PROJECT
        final HiveProject project = call.rel(0);
        final RexSubQuery e = RexUtil.SubQueryFinder.find(project.getProjects());
        assert e != null;
        final RelOptUtil.Logic logic = LogicVisitor.find(RelOptUtil.Logic.TRUE_FALSE_UNKNOWN, project.getProjects(), e);
        builder.push(project.getInput());
        final int fieldCount = builder.peek().getRowType().getFieldCount();
        SubqueryConf subqueryConfig = project.getCluster().getPlanner().getContext().unwrap(SubqueryConf.class);
        boolean isCorrScalarQuery = subqueryConfig.getCorrScalarRexSQWithAgg().contains(e.rel);
        final RexNode target = apply(call.getMetadataQuery(), e, HiveFilter.getVariablesSet(e), logic, builder, 1, fieldCount, isCorrScalarQuery);
        final RexShuttle shuttle = new ReplaceSubQueryShuttle(e, target);
        builder.project(shuttle.apply(project.getProjects()), project.getRowType().getFieldNames());
        call.transformTo(builder.build());
    }
}
Also used : RelBuilder(org.apache.calcite.tools.RelBuilder) RexShuttle(org.apache.calcite.rex.RexShuttle) RelOptUtil(org.apache.calcite.plan.RelOptUtil) SubqueryConf(org.apache.hadoop.hive.ql.optimizer.calcite.SubqueryConf) HiveFilter(org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveFilter) RelNode(org.apache.calcite.rel.RelNode) HiveProject(org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveProject) RexSubQuery(org.apache.calcite.rex.RexSubQuery) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

RexShuttle (org.apache.calcite.rex.RexShuttle)20 RexNode (org.apache.calcite.rex.RexNode)16 RexInputRef (org.apache.calcite.rex.RexInputRef)13 ArrayList (java.util.ArrayList)9 RelNode (org.apache.calcite.rel.RelNode)8 RexCall (org.apache.calcite.rex.RexCall)7 RexBuilder (org.apache.calcite.rex.RexBuilder)6 RelOptUtil (org.apache.calcite.plan.RelOptUtil)5 List (java.util.List)4 Map (java.util.Map)4 RexLiteral (org.apache.calcite.rex.RexLiteral)4 RexLocalRef (org.apache.calcite.rex.RexLocalRef)4 RexProgram (org.apache.calcite.rex.RexProgram)4 RexProgramBuilder (org.apache.calcite.rex.RexProgramBuilder)4 Collections (java.util.Collections)3 LinkedList (java.util.LinkedList)3 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)3 RelBuilder (org.apache.calcite.tools.RelBuilder)3 HashMap (java.util.HashMap)2 Function (java.util.function.Function)2