Search in sources :

Example 36 with LogicalProject

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

the class Lattice method populate.

private static boolean populate(List<RelNode> nodes, List<int[][]> tempLinks, RelNode rel) {
    if (nodes.isEmpty() && rel instanceof LogicalProject) {
        return populate(nodes, tempLinks, ((LogicalProject) rel).getInput());
    }
    if (rel instanceof TableScan) {
        nodes.add(rel);
        return true;
    }
    if (rel instanceof LogicalJoin) {
        LogicalJoin join = (LogicalJoin) rel;
        if (join.getJoinType() != JoinRelType.INNER) {
            throw new RuntimeException("only inner join allowed, but got " + join.getJoinType());
        }
        populate(nodes, tempLinks, join.getLeft());
        populate(nodes, tempLinks, join.getRight());
        for (RexNode rex : RelOptUtil.conjunctions(join.getCondition())) {
            tempLinks.add(grab(nodes, rex));
        }
        return true;
    }
    throw new RuntimeException("Invalid node type " + rel.getClass().getSimpleName() + " in lattice query");
}
Also used : TableScan(org.apache.calcite.rel.core.TableScan) LogicalJoin(org.apache.calcite.rel.logical.LogicalJoin) LogicalProject(org.apache.calcite.rel.logical.LogicalProject) RexNode(org.apache.calcite.rex.RexNode)

Example 37 with LogicalProject

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

the class CsvProjectTableScanRule method onMatch.

@Override
public void onMatch(RelOptRuleCall call) {
    final LogicalProject project = call.rel(0);
    final CsvTableScan scan = call.rel(1);
    int[] fields = getProjectFields(project.getProjects());
    if (fields == null) {
        // Project contains expressions more complex than just field references.
        return;
    }
    call.transformTo(new CsvTableScan(scan.getCluster(), scan.getTable(), scan.csvTable, fields));
}
Also used : LogicalProject(org.apache.calcite.rel.logical.LogicalProject)

Example 38 with LogicalProject

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

the class SplunkPushDownRule method onMatch.

// ~ Methods --------------------------------------------------------------
public void onMatch(RelOptRuleCall call) {
    LOGGER.debug(description);
    int relLength = call.rels.length;
    SplunkTableScan splunkRel = (SplunkTableScan) call.rels[relLength - 1];
    LogicalFilter filter;
    LogicalProject topProj = null;
    LogicalProject bottomProj = null;
    RelDataType topRow = splunkRel.getRowType();
    int filterIdx = 2;
    if (call.rels[relLength - 2] instanceof LogicalProject) {
        bottomProj = (LogicalProject) call.rels[relLength - 2];
        filterIdx = 3;
        // bottom projection will change the field count/order
        topRow = bottomProj.getRowType();
    }
    String filterString;
    if (filterIdx <= relLength && call.rels[relLength - filterIdx] instanceof LogicalFilter) {
        filter = (LogicalFilter) call.rels[relLength - filterIdx];
        int topProjIdx = filterIdx + 1;
        if (topProjIdx <= relLength && call.rels[relLength - topProjIdx] instanceof LogicalProject) {
            topProj = (LogicalProject) call.rels[relLength - topProjIdx];
        }
        RexCall filterCall = (RexCall) filter.getCondition();
        SqlOperator op = filterCall.getOperator();
        List<RexNode> operands = filterCall.getOperands();
        LOGGER.debug("fieldNames: {}", getFieldsString(topRow));
        final StringBuilder buf = new StringBuilder();
        if (getFilter(op, operands, buf, topRow.getFieldNames())) {
            filterString = buf.toString();
        } else {
            // can't handle
            return;
        }
    } else {
        filterString = "";
    }
    // top projection will change the field count/order
    if (topProj != null) {
        topRow = topProj.getRowType();
    }
    LOGGER.debug("pre transformTo fieldNames: {}", getFieldsString(topRow));
    call.transformTo(appendSearchString(filterString, splunkRel, topProj, bottomProj, topRow, null));
}
Also used : RexCall(org.apache.calcite.rex.RexCall) LogicalFilter(org.apache.calcite.rel.logical.LogicalFilter) SqlOperator(org.apache.calcite.sql.SqlOperator) RelDataType(org.apache.calcite.rel.type.RelDataType) LogicalProject(org.apache.calcite.rel.logical.LogicalProject) NlsString(org.apache.calcite.util.NlsString) RexNode(org.apache.calcite.rex.RexNode)

Example 39 with LogicalProject

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

the class ProjectWatermarkAssignerTransposeRule method onMatch.

@Override
public void onMatch(RelOptRuleCall call) {
    LogicalProject project = call.rel(0);
    LogicalWatermarkAssigner watermarkAssigner = call.rel(1);
    // NOTES: DON'T use the nestedSchema datatype to build the transposed project.
    NestedSchema nestedSchema = getUsedFieldsInTopLevelProjectAndWatermarkAssigner(project, watermarkAssigner);
    FlinkRelBuilder builder = (FlinkRelBuilder) call.builder().push(watermarkAssigner.getInput());
    List<RexInputRef> transposedProjects = new LinkedList<>();
    List<String> usedNames = new LinkedList<>();
    // add the used column RexInputRef and names into list
    for (NestedColumn column : nestedSchema.columns().values()) {
        // mark by hand
        column.setIndexOfLeafInNewSchema(transposedProjects.size());
        column.markLeaf();
        usedNames.add(column.name());
        transposedProjects.add(builder.field(column.indexInOriginSchema()));
    }
    // get the rowtime field index in the transposed project
    String rowTimeName = watermarkAssigner.getRowType().getFieldNames().get(watermarkAssigner.rowtimeFieldIndex());
    int indexOfRowTimeInTransposedProject;
    if (nestedSchema.columns().get(rowTimeName) == null) {
        // push the RexInputRef of the rowtime into the list
        int rowTimeIndexInInput = watermarkAssigner.rowtimeFieldIndex();
        indexOfRowTimeInTransposedProject = transposedProjects.size();
        transposedProjects.add(builder.field(rowTimeIndexInInput));
        usedNames.add(rowTimeName);
    } else {
        // find rowtime ref in the list and mark the location
        indexOfRowTimeInTransposedProject = nestedSchema.columns().get(rowTimeName).indexOfLeafInNewSchema();
    }
    // the rowtime column has no rowtime indicator
    builder.project(transposedProjects, usedNames);
    // rewrite the top level field reference
    RexNode newWatermarkExpr = watermarkAssigner.watermarkExpr().accept(new RexShuttle() {

        @Override
        public RexNode visitInputRef(RexInputRef inputRef) {
            String fieldName = watermarkAssigner.getRowType().getFieldNames().get(inputRef.getIndex());
            return builder.field(nestedSchema.columns().get(fieldName).indexOfLeafInNewSchema());
        }
    });
    builder.watermark(indexOfRowTimeInTransposedProject, newWatermarkExpr);
    List<RexNode> newProjects = NestedProjectionUtil.rewrite(project.getProjects(), nestedSchema, call.builder().getRexBuilder());
    RelNode newProject = builder.project(newProjects, project.getRowType().getFieldNames()).build();
    call.transformTo(newProject);
}
Also used : RexShuttle(org.apache.calcite.rex.RexShuttle) NestedColumn(org.apache.flink.table.planner.plan.utils.NestedColumn) LinkedList(java.util.LinkedList) RelNode(org.apache.calcite.rel.RelNode) LogicalWatermarkAssigner(org.apache.flink.table.planner.plan.nodes.calcite.LogicalWatermarkAssigner) FlinkRelBuilder(org.apache.flink.table.planner.calcite.FlinkRelBuilder) RexInputRef(org.apache.calcite.rex.RexInputRef) LogicalProject(org.apache.calcite.rel.logical.LogicalProject) NestedSchema(org.apache.flink.table.planner.plan.utils.NestedSchema) RexNode(org.apache.calcite.rex.RexNode)

Example 40 with LogicalProject

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

the class ProjectWindowTableFunctionTransposeRule method onMatch.

@Override
public void onMatch(RelOptRuleCall call) {
    LogicalProject project = call.rel(0);
    LogicalTableFunctionScan scan = call.rel(1);
    RelNode scanInput = scan.getInput(0);
    TimeAttributeWindowingStrategy windowingStrategy = WindowUtil.convertToWindowingStrategy((RexCall) scan.getCall(), scanInput.getRowType());
    // 1. get fields to push down
    ImmutableBitSet projectFields = RelOptUtil.InputFinder.bits(project.getProjects(), null);
    int scanInputFieldCount = scanInput.getRowType().getFieldCount();
    ImmutableBitSet toPushFields = ImmutableBitSet.range(0, scanInputFieldCount).intersect(projectFields).set(windowingStrategy.getTimeAttributeIndex());
    if (toPushFields.cardinality() == scanInputFieldCount) {
        return;
    }
    // 2. create new input of window table function scan
    RelBuilder relBuilder = call.builder();
    RelNode newScanInput = createInnerProject(relBuilder, scanInput, toPushFields);
    // mapping origin field index to new field index, used to rewrite WindowTableFunction and
    // top project
    Map<Integer, Integer> mapping = getFieldMapping(scan.getRowType().getFieldCount(), scanInputFieldCount, toPushFields);
    // 3. create new window table function scan
    LogicalTableFunctionScan newScan = createNewTableFunctionScan(relBuilder, scan, windowingStrategy.getTimeAttributeType(), newScanInput, mapping);
    // 4. create top project
    RelNode topProject = createTopProject(relBuilder, project, newScan, mapping);
    call.transformTo(topProject);
}
Also used : LogicalTableFunctionScan(org.apache.calcite.rel.logical.LogicalTableFunctionScan) RelBuilder(org.apache.calcite.tools.RelBuilder) RelNode(org.apache.calcite.rel.RelNode) ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) TimeAttributeWindowingStrategy(org.apache.flink.table.planner.plan.logical.TimeAttributeWindowingStrategy) LogicalProject(org.apache.calcite.rel.logical.LogicalProject)

Aggregations

LogicalProject (org.apache.calcite.rel.logical.LogicalProject)38 RexNode (org.apache.calcite.rex.RexNode)20 ArrayList (java.util.ArrayList)14 RelNode (org.apache.calcite.rel.RelNode)12 LogicalJoin (org.apache.calcite.rel.logical.LogicalJoin)11 LogicalFilter (org.apache.calcite.rel.logical.LogicalFilter)9 RelDataType (org.apache.calcite.rel.type.RelDataType)8 LogicalTableScan (org.apache.calcite.rel.logical.LogicalTableScan)6 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)6 RexBuilder (org.apache.calcite.rex.RexBuilder)6 Test (org.junit.Test)6 RexInputRef (org.apache.calcite.rex.RexInputRef)5 RelBuilder (org.apache.calcite.tools.RelBuilder)4 RexNode (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode)3 TableScan (org.apache.calcite.rel.core.TableScan)3 RexCall (org.apache.calcite.rex.RexCall)3 SqlOperator (org.apache.calcite.sql.SqlOperator)3 NlsString (org.apache.calcite.util.NlsString)3 ImmutableList (com.google.common.collect.ImmutableList)2 ResolvedComputedColumn (com.google.zetasql.resolvedast.ResolvedNodes.ResolvedComputedColumn)2