Search in sources :

Example 96 with Project

use of org.apache.calcite.rel.core.Project in project beam by apache.

the class BeamAggregationRule method updateWindow.

private static RelNode updateWindow(Aggregate aggregate, Project project) {
    ImmutableBitSet groupByFields = aggregate.getGroupSet();
    ArrayList<RexNode> projects = new ArrayList(project.getProjects());
    WindowFn windowFn = null;
    int windowFieldIndex = -1;
    for (int groupFieldIndex : groupByFields.asList()) {
        RexNode projNode = projects.get(groupFieldIndex);
        if (!(projNode instanceof RexCall)) {
            continue;
        }
        RexCall rexCall = (RexCall) projNode;
        WindowFn fn = createWindowFn(rexCall.getOperands(), rexCall.op.kind);
        if (fn != null) {
            windowFn = fn;
            windowFieldIndex = groupFieldIndex;
            projects.set(groupFieldIndex, rexCall.getOperands().get(0));
        }
    }
    if (windowFn == null) {
        return null;
    }
    final Project newProject = project.copy(project.getTraitSet(), project.getInput(), projects, project.getRowType());
    return new BeamAggregationRel(aggregate.getCluster(), aggregate.getTraitSet().replace(BeamLogicalConvention.INSTANCE), convert(newProject, newProject.getTraitSet().replace(BeamLogicalConvention.INSTANCE)), aggregate.getGroupSet(), aggregate.getGroupSets(), aggregate.getAggCallList(), windowFn, windowFieldIndex);
}
Also used : RexCall(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexCall) Project(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.core.Project) ImmutableBitSet(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.ImmutableBitSet) WindowFn(org.apache.beam.sdk.transforms.windowing.WindowFn) ArrayList(java.util.ArrayList) BeamAggregationRel(org.apache.beam.sdk.extensions.sql.impl.rel.BeamAggregationRel) RexNode(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode)

Example 97 with Project

use of org.apache.calcite.rel.core.Project in project beam by apache.

the class BeamAggregationRule method onMatch.

@Override
public void onMatch(RelOptRuleCall call) {
    final Aggregate aggregate = call.rel(0);
    final Project project = call.rel(1);
    if (aggregate.getGroupType() != Aggregate.Group.SIMPLE) {
        return;
    }
    RelNode x = updateWindow(aggregate, project);
    if (x == null) {
        // Non-windowed case should be handled by the BeamBasicAggregationRule
        return;
    }
    call.transformTo(x);
}
Also used : Project(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.core.Project) RelNode(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelNode) Aggregate(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.core.Aggregate)

Example 98 with Project

use of org.apache.calcite.rel.core.Project in project beam by apache.

the class BeamAggregateProjectMergeRule method getUnderlyingIO.

/**
 * Following scenarios are possible:<br>
 * 1) Aggregate <- Project <- IO.<br>
 * 2) Aggregate <- Project <- Chain of Project/Filter <- IO.<br>
 * 3) Aggregate <- Project <- Something else.<br>
 * 4) Aggregate <- Project <- Chain of Project/Filter <- Something else.
 *
 * @param parent project that matched this rule.
 * @return {@code BeamIOSourceRel} when it is present or null when some other {@code RelNode} is
 *     present.
 */
private BeamIOSourceRel getUnderlyingIO(Set<RelNode> visitedNodes, SingleRel parent) {
    // No need to look at the same node more than once.
    if (visitedNodes.contains(parent)) {
        return null;
    }
    visitedNodes.add(parent);
    List<RelNode> nodes = ((RelSubset) parent.getInput()).getRelList();
    for (RelNode node : nodes) {
        if (node instanceof Filter || node instanceof Project) {
            // Search node inputs for an IO.
            BeamIOSourceRel child = getUnderlyingIO(visitedNodes, (SingleRel) node);
            if (child != null) {
                return child;
            }
        } else if (node instanceof BeamIOSourceRel) {
            return (BeamIOSourceRel) node;
        }
    }
    return null;
}
Also used : Project(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.core.Project) RelNode(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelNode) Filter(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.core.Filter) BeamIOSourceRel(org.apache.beam.sdk.extensions.sql.impl.rel.BeamIOSourceRel) RelSubset(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.volcano.RelSubset)

Example 99 with Project

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

the class AggregateSlidingWindowPhysicalRule method onMatch.

@Override
public void onMatch(RelOptRuleCall call) {
    AggregateLogicalRel logicalAggregate = call.rel(0);
    assert logicalAggregate.getGroupType() == Group.SIMPLE;
    assert logicalAggregate.getGroupSets().size() == 1 && (logicalAggregate.getGroupSet() == null || logicalAggregate.getGroupSet().equals(logicalAggregate.getGroupSets().get(0)));
    List<RexNode> projections;
    RelDataType projectRowType;
    SlidingWindowLogicalRel windowRel;
    if (hasProject) {
        Project projectRel = call.rel(1);
        projections = new ArrayList<>(projectRel.getProjects());
        projectRowType = projectRel.getRowType();
        windowRel = call.rel(2);
    } else {
        windowRel = call.rel(1);
        // create an identity projection
        List<RelDataTypeField> fields = windowRel.getRowType().getFieldList();
        projections = new ArrayList<>(fields.size());
        for (int i = 0; i < fields.size(); i++) {
            RelDataTypeField field = fields.get(i);
            projections.add(call.builder().getRexBuilder().makeInputRef(field.getType(), i));
        }
        projectRowType = windowRel.getRowType();
    }
    // Our input hierarchy is, for example:
    // -Aggregate(group=[$0], EXPR$1=[AVG($1)])
    // --Project(rowType=[window_start, field1])
    // ---SlidingWindowRel(rowType=[field1, field2, timestamp, window_start, window_end])
    // 
    // We need to preserve the column used to generate window bounds and remove the window
    // bounds from the projection to get input projection such as this:
    // -SlidingWindowAggregatePhysicalRel(group=[$0], EXPR$1=[AVG($1)])
    // --Project(rowType=[timestamp, field1])
    // 
    // The group=[$0] we pass to SlidingWindowAggregatePhysicalRel' superclass isn't correct,
    // but it works for us for now - the superclass uses it only to calculate the output type.
    // And the timestamp and the window bound have the same type.
    int timestampIndex = windowRel.orderingFieldIndex();
    int windowStartIndex = windowRel.windowStartIndex();
    int windowEndIndex = windowRel.windowEndIndex();
    // Replace references to either window bound to timestamp in projection
    List<Integer> windowStartIndexes = new ArrayList<>();
    List<Integer> windowEndIndexes = new ArrayList<>();
    for (int i = 0; i < projections.size(); i++) {
        RexNode projection = projections.get(i);
        // we don't support any transformation of the window bound using an expression, it must be a direct input reference.
        if (projection instanceof RexInputRef) {
            int index = ((RexInputRef) projection).getIndex();
            if (index == windowStartIndex || index == windowEndIndex) {
                // todo [viliam] avoid multiple projections of the timestamp
                projection = call.builder().getRexBuilder().makeInputRef(projection.getType(), timestampIndex);
                projections.set(i, projection);
                if (index == windowStartIndex) {
                    windowStartIndexes.add(i);
                } else {
                    windowEndIndexes.add(i);
                }
            }
        } else if (hasInputRef(projection, windowStartIndex, windowEndIndex)) {
            throw QueryException.error(SqlErrorCode.PARSING, "In window aggregation, the window_start and window_end fields must be used" + " directly, without any transformation");
        }
    }
    RelNode input = windowRel.getInput();
    RelNode convertedInput = OptUtils.toPhysicalInput(input);
    Collection<RelNode> transformedInputs = OptUtils.extractPhysicalRelsFromSubset(convertedInput);
    for (RelNode transformedInput : transformedInputs) {
        // todo [viliam] change the name for window bound replaced with timestamps
        RelDataType newRowType = projectRowType;
        RelNode newProject = new ProjectPhysicalRel(transformedInput.getCluster(), transformedInput.getTraitSet(), transformedInput, projections, newRowType);
        RelNode transformedRel = transform(newProject, logicalAggregate, windowStartIndexes, windowEndIndexes, windowRel.windowPolicyProvider());
        if (transformedRel != null) {
            call.transformTo(transformedRel);
        }
    }
}
Also used : AggregateLogicalRel(com.hazelcast.jet.sql.impl.opt.logical.AggregateLogicalRel) ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType) Project(org.apache.calcite.rel.core.Project) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelNode(org.apache.calcite.rel.RelNode) SlidingWindowLogicalRel(com.hazelcast.jet.sql.impl.opt.logical.SlidingWindowLogicalRel) RexInputRef(org.apache.calcite.rex.RexInputRef) RexNode(org.apache.calcite.rex.RexNode)

Example 100 with Project

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

the class ProjectPhysicalRule method convert.

@Override
public RelNode convert(RelNode rel) {
    Project project = (Project) rel;
    RelNode transformedInput = RelOptRule.convert(project.getInput(), project.getInput().getTraitSet().replace(PHYSICAL));
    return new ProjectPhysicalRel(project.getCluster(), transformedInput.getTraitSet(), transformedInput, project.getProjects(), project.getRowType());
}
Also used : Project(org.apache.calcite.rel.core.Project) RelNode(org.apache.calcite.rel.RelNode)

Aggregations

Project (org.apache.calcite.rel.core.Project)143 RexNode (org.apache.calcite.rex.RexNode)77 RelNode (org.apache.calcite.rel.RelNode)71 ArrayList (java.util.ArrayList)46 LogicalProject (org.apache.calcite.rel.logical.LogicalProject)35 RexBuilder (org.apache.calcite.rex.RexBuilder)28 RelDataType (org.apache.calcite.rel.type.RelDataType)26 Aggregate (org.apache.calcite.rel.core.Aggregate)22 Filter (org.apache.calcite.rel.core.Filter)22 Join (org.apache.calcite.rel.core.Join)22 List (java.util.List)19 RexLiteral (org.apache.calcite.rex.RexLiteral)19 AggregateCall (org.apache.calcite.rel.core.AggregateCall)18 Sort (org.apache.calcite.rel.core.Sort)18 Test (org.junit.Test)18 RelBuilder (org.apache.calcite.tools.RelBuilder)17 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)16 HiveProject (org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveProject)16 Collectors (java.util.stream.Collectors)15 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)15