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));
}
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));
}
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);
}
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);
}
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));
}
Aggregations