Search in sources :

Example 86 with Project

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.core.Project in project drill by axbaretto.

the class DrillPushFilterPastProjectRule method onMatch.

// ~ Methods ----------------------------------------------------------------
// implement RelOptRule
public void onMatch(RelOptRuleCall call) {
    Filter filterRel = call.rel(0);
    Project projRel = call.rel(1);
    // get a conjunctions of the filter condition. For each conjunction, if it refers to ITEM or FLATTEN expression
    // then we could not pushed down. Otherwise, it's qualified to be pushed down.
    final List<RexNode> predList = RelOptUtil.conjunctions(filterRel.getCondition());
    final List<RexNode> qualifiedPredList = Lists.newArrayList();
    final List<RexNode> unqualifiedPredList = Lists.newArrayList();
    for (final RexNode pred : predList) {
        if (DrillRelOptUtil.findOperators(pred, projRel.getProjects(), BANNED_OPERATORS) == null) {
            qualifiedPredList.add(pred);
        } else {
            unqualifiedPredList.add(pred);
        }
    }
    final RexNode qualifedPred = RexUtil.composeConjunction(filterRel.getCluster().getRexBuilder(), qualifiedPredList, true);
    if (qualifedPred == null) {
        return;
    }
    // convert the filter to one that references the child of the project
    RexNode newCondition = RelOptUtil.pushPastProject(qualifedPred, projRel);
    Filter newFilterRel = LogicalFilter.create(projRel.getInput(), newCondition);
    Project newProjRel = (Project) RelOptUtil.createProject(newFilterRel, Pair.left(projRel.getNamedProjects()), Pair.right(projRel.getNamedProjects()), false, relBuilderFactory.create(projRel.getCluster(), null));
    final RexNode unqualifiedPred = RexUtil.composeConjunction(filterRel.getCluster().getRexBuilder(), unqualifiedPredList, true);
    if (unqualifiedPred == null) {
        call.transformTo(newProjRel);
    } else {
        // if there are filters not qualified to be pushed down, then we have to put those filters on top of
        // the new Project operator.
        // Filter -- unqualified filters
        // \
        // Project
        // \
        // Filter  -- qualified filters
        Filter filterNotPushed = LogicalFilter.create(newProjRel, unqualifiedPred);
        call.transformTo(filterNotPushed);
    }
}
Also used : Project(org.apache.calcite.rel.core.Project) LogicalProject(org.apache.calcite.rel.logical.LogicalProject) LogicalFilter(org.apache.calcite.rel.logical.LogicalFilter) Filter(org.apache.calcite.rel.core.Filter) RexNode(org.apache.calcite.rex.RexNode)

Example 87 with Project

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.core.Project in project drill by axbaretto.

the class DrillPushProjIntoScan method onMatch.

@Override
public void onMatch(RelOptRuleCall call) {
    final Project proj = call.rel(0);
    final TableScan scan = call.rel(1);
    try {
        ProjectPushInfo columnInfo = PrelUtil.getColumns(scan.getRowType(), proj.getProjects());
        // get DrillTable, either wrapped in RelOptTable, or DrillTranslatableTable.
        DrillTable table = scan.getTable().unwrap(DrillTable.class);
        if (table == null) {
            table = scan.getTable().unwrap(DrillTranslatableTable.class).getDrillTable();
        }
        if (// 
        columnInfo == null || columnInfo.isStarQuery() || !// 
        table.getGroupScan().canPushdownProjects(columnInfo.columns)) {
            return;
        }
        final DrillScanRel newScan = new DrillScanRel(scan.getCluster(), scan.getTraitSet().plus(DrillRel.DRILL_LOGICAL), scan.getTable(), columnInfo.createNewRowType(proj.getInput().getCluster().getTypeFactory()), columnInfo.columns);
        List<RexNode> newProjects = Lists.newArrayList();
        for (RexNode n : proj.getChildExps()) {
            newProjects.add(n.accept(columnInfo.getInputRewriter()));
        }
        final DrillProjectRel newProj = new DrillProjectRel(proj.getCluster(), proj.getTraitSet().plus(DrillRel.DRILL_LOGICAL), newScan, newProjects, proj.getRowType());
        if (ProjectRemoveRule.isTrivial(newProj)) {
            call.transformTo(newScan);
        } else {
            call.transformTo(newProj);
        }
    } catch (IOException e) {
        throw new DrillRuntimeException(e);
    }
}
Also used : Project(org.apache.calcite.rel.core.Project) LogicalProject(org.apache.calcite.rel.logical.LogicalProject) TableScan(org.apache.calcite.rel.core.TableScan) EnumerableTableScan(org.apache.calcite.adapter.enumerable.EnumerableTableScan) ProjectPushInfo(org.apache.drill.exec.planner.physical.PrelUtil.ProjectPushInfo) IOException(java.io.IOException) DrillRuntimeException(org.apache.drill.common.exceptions.DrillRuntimeException) RexNode(org.apache.calcite.rex.RexNode)

Example 88 with Project

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.core.Project in project drill by axbaretto.

the class DrillProjectRule method onMatch.

@Override
public void onMatch(RelOptRuleCall call) {
    final Project project = call.rel(0);
    final RelNode input = project.getInput();
    final RelTraitSet traits = project.getTraitSet().plus(DrillRel.DRILL_LOGICAL);
    final RelNode convertedInput = convert(input, input.getTraitSet().plus(DrillRel.DRILL_LOGICAL).simplify());
    call.transformTo(new DrillProjectRel(project.getCluster(), traits, convertedInput, project.getProjects(), project.getRowType()));
}
Also used : Project(org.apache.calcite.rel.core.Project) LogicalProject(org.apache.calcite.rel.logical.LogicalProject) RelNode(org.apache.calcite.rel.RelNode) RelTraitSet(org.apache.calcite.plan.RelTraitSet)

Example 89 with Project

use of org.apache.beam.vendor.calcite.v1_28_0.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 90 with Project

use of org.apache.beam.vendor.calcite.v1_28_0.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)51 LogicalProject (org.apache.calcite.rel.logical.LogicalProject)35 RexBuilder (org.apache.calcite.rex.RexBuilder)28 RelDataType (org.apache.calcite.rel.type.RelDataType)26 Test (org.junit.Test)23 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)20 RexLiteral (org.apache.calcite.rex.RexLiteral)19 AggregateCall (org.apache.calcite.rel.core.AggregateCall)18 Sort (org.apache.calcite.rel.core.Sort)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