Search in sources :

Example 36 with LogicalProject

use of 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 37 with LogicalProject

use of 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 38 with LogicalProject

use of 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 39 with LogicalProject

use of 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)

Example 40 with LogicalProject

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

the class RelRoot method project.

/**
 * Returns the root relational expression as a {@link LogicalProject}.
 *
 * @param force Create a Project even if all fields are used
 */
public RelNode project(boolean force) {
    if (isRefTrivial() && (SqlKind.DML.contains(kind) || !force || rel instanceof LogicalProject)) {
        return rel;
    }
    final List<RexNode> projects = new ArrayList<>();
    final RexBuilder rexBuilder = rel.getCluster().getRexBuilder();
    for (Pair<Integer, String> field : fields) {
        projects.add(rexBuilder.makeInputRef(rel, field.left));
    }
    return LogicalProject.create(rel, projects, Pair.right(fields));
}
Also used : ArrayList(java.util.ArrayList) RexBuilder(org.apache.calcite.rex.RexBuilder) LogicalProject(org.apache.calcite.rel.logical.LogicalProject) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

LogicalProject (org.apache.calcite.rel.logical.LogicalProject)38 RexNode (org.apache.calcite.rex.RexNode)20 ArrayList (java.util.ArrayList)13 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 LogicalProject (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.logical.LogicalProject)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 PrintWriter (java.io.PrintWriter)2