Java Code Examples for org.apache.calcite.rel.logical.LogicalProject#copy()

The following examples show how to use org.apache.calcite.rel.logical.LogicalProject#copy() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SubstitutionVisitor.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override
public void onMatch(RelOptRuleCall call) {
    final LogicalFilter filter = call.rel(0);
    final LogicalProject project = call.rel(1);

    final List<RexNode> newProjects = new ArrayList<>(project.getProjects());
    newProjects.add(filter.getCondition());

    final RelOptCluster cluster = filter.getCluster();
    RelDataType newRowType = cluster.getTypeFactory().builder().addAll(project.getRowType().getFieldList())
            .add("condition", Util.last(newProjects).getType()).build();
    final RelNode newProject = project.copy(project.getTraitSet(), project.getInput(), newProjects, newRowType);

    final RexInputRef newCondition = cluster.getRexBuilder().makeInputRef(newProject, newProjects.size() - 1);

    call.transformTo(LogicalFilter.create(newProject, newCondition));
}
 
Example 2
Source File: PreProcessRel.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public RelNode visit(LogicalProject project) {
  final RenameConvertToConvertFromVisitor renameVisitor = new RenameConvertToConvertFromVisitor(project.getCluster().getRexBuilder(), table);
  final List<RexNode> projExpr = Lists.newArrayList();
  for(RexNode rexNode : project.getChildExps()) {
    projExpr.add(rexNode.accept(unwrappingExpressionVisitor));
  }

  project =  project.copy(project.getTraitSet(),
      project.getInput(),
      projExpr,
      project.getRowType());

  List<RexNode> exprList = new ArrayList<>();
  boolean rewrite = false;

  for (RexNode rex : project.getChildExps()) {
    RexNode newExpr = rex.accept(renameVisitor);
    if (newExpr != rex) {
      rewrite = true;
    }
    exprList.add(newExpr);
  }

  if (rewrite == true) {
    LogicalProject newProject = project.copy(project.getTraitSet(), project.getInput(0), exprList, project.getRowType());
    return visitChild(newProject, 0, project.getInput());
  }

  return visitChild(project, 0, project.getInput());
}
 
Example 3
Source File: PushProjectIntoTableSourceScanRule.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public void onMatch(RelOptRuleCall call) {
	LogicalProject project = call.rel(0);
	LogicalTableScan scan = call.rel(1);

	int[] usedFields = RexNodeExtractor.extractRefInputFields(project.getProjects());
	// if no fields can be projected, we keep the original plan.
	if (scan.getRowType().getFieldCount() == usedFields.length) {
		return;
	}

	TableSourceTable oldTableSourceTable = scan.getTable().unwrap(TableSourceTable.class);
	DynamicTableSource newTableSource = oldTableSourceTable.tableSource().copy();

	int[][] projectedFields = new int[usedFields.length][];
	List<String> fieldNames = new ArrayList<>();
	for (int i = 0; i < usedFields.length; ++i) {
		int usedField = usedFields[i];
		projectedFields[i] = new int[] { usedField };
		fieldNames.add(scan.getRowType().getFieldNames().get(usedField));
	}
	((SupportsProjectionPushDown) newTableSource).applyProjection(projectedFields);
	FlinkTypeFactory flinkTypeFactory = (FlinkTypeFactory) oldTableSourceTable.getRelOptSchema().getTypeFactory();
	RelDataType newRowType = flinkTypeFactory.projectStructType(oldTableSourceTable.getRowType(), usedFields);

	// project push down does not change the statistic, we can reuse origin statistic
	TableSourceTable newTableSourceTable = oldTableSourceTable.copy(
			newTableSource, newRowType, new String[] { ("project=[" + String.join(", ", fieldNames) + "]") });

	LogicalTableScan newScan = new LogicalTableScan(
			scan.getCluster(), scan.getTraitSet(), scan.getHints(), newTableSourceTable);
	// rewrite input field in projections
	List<RexNode> newProjects = RexNodeRewriter.rewriteWithNewFieldInput(project.getProjects(), usedFields);
	LogicalProject newProject = project.copy(
			project.getTraitSet(),
			newScan,
			newProjects,
			project.getRowType());

	if (ProjectRemoveRule.isTrivial(newProject)) {
		// drop project if the transformed program merely returns its input
		call.transformTo(newScan);
	} else {
		call.transformTo(newProject);
	}
}