Java Code Examples for org.apache.calcite.rex.RexProgramBuilder#addIdentity()

The following examples show how to use org.apache.calcite.rex.RexProgramBuilder#addIdentity() . 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: EnumerableFilterToCalcRule.java    From calcite with Apache License 2.0 6 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  final EnumerableFilter filter = call.rel(0);
  final RelNode input = filter.getInput();

  // Create a program containing a filter.
  final RexBuilder rexBuilder = filter.getCluster().getRexBuilder();
  final RelDataType inputRowType = input.getRowType();
  final RexProgramBuilder programBuilder =
      new RexProgramBuilder(inputRowType, rexBuilder);
  programBuilder.addIdentity();
  programBuilder.addCondition(filter.getCondition());
  final RexProgram program = programBuilder.getProgram();

  final EnumerableCalc calc = EnumerableCalc.create(input, program);
  call.transformTo(calc);
}
 
Example 2
Source File: FilterMergeRule.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a RexProgram corresponding to a LogicalFilter
 *
 * @param filterRel the LogicalFilter
 * @return created RexProgram
 */
private RexProgram createProgram(Filter filterRel) {
  RexProgramBuilder programBuilder =
      new RexProgramBuilder(
          filterRel.getRowType(),
          filterRel.getCluster().getRexBuilder());
  programBuilder.addIdentity();
  programBuilder.addCondition(filterRel.getCondition());
  return programBuilder.getProgram();
}
 
Example 3
Source File: FilterCalcMergeRule.java    From Bats with Apache License 2.0 5 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  final LogicalFilter filter = call.rel(0);
  final LogicalCalc calc = call.rel(1);

  // Don't merge a filter onto a calc which contains windowed aggregates.
  // That would effectively be pushing a multiset down through a filter.
  // We'll have chance to merge later, when the over is expanded.
  if (calc.getProgram().containsAggs()) {
    return;
  }

  // Create a program containing the filter.
  final RexBuilder rexBuilder = filter.getCluster().getRexBuilder();
  final RexProgramBuilder progBuilder =
      new RexProgramBuilder(
          calc.getRowType(),
          rexBuilder);
  progBuilder.addIdentity();
  progBuilder.addCondition(filter.getCondition());
  RexProgram topProgram = progBuilder.getProgram();
  RexProgram bottomProgram = calc.getProgram();

  // Merge the programs together.
  RexProgram mergedProgram =
      RexProgramBuilder.mergePrograms(
          topProgram,
          bottomProgram,
          rexBuilder);
  final LogicalCalc newCalc =
      LogicalCalc.create(calc.getInput(), mergedProgram);
  call.transformTo(newCalc);
}
 
Example 4
Source File: DrillMergeFilterRule.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a RexProgram corresponding to a LogicalFilter
 *
 * @param filterRel the LogicalFilter
 * @return created RexProgram
 */
private RexProgram createProgram(Filter filterRel) {
  RexProgramBuilder programBuilder =
      new RexProgramBuilder(
          filterRel.getRowType(),
          filterRel.getCluster().getRexBuilder());
  programBuilder.addIdentity();
  programBuilder.addCondition(filterRel.getCondition());
  return programBuilder.getProgram();
}
 
Example 5
Source File: OLAPFilterRel.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
@Override
public EnumerableRel implementEnumerable(List<EnumerableRel> inputs) {
    // keep it for having clause
    RexBuilder rexBuilder = getCluster().getRexBuilder();
    RelDataType inputRowType = getInput().getRowType();
    RexProgramBuilder programBuilder = new RexProgramBuilder(inputRowType, rexBuilder);
    programBuilder.addIdentity();
    programBuilder.addCondition(this.condition);
    RexProgram program = programBuilder.getProgram();

    return new EnumerableCalc(getCluster(), getCluster().traitSetOf(EnumerableConvention.INSTANCE), //
            sole(inputs), program);
}
 
Example 6
Source File: FilterMergeCrule.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a RexProgram corresponding to a LogicalFilter
 *
 * @param filterRel
 *          the LogicalFilter
 * @return created RexProgram
 */
private RexProgram createProgram(Filter filterRel) {
  RexProgramBuilder programBuilder = new RexProgramBuilder(filterRel.getRowType(),
      filterRel.getCluster().getRexBuilder());
  programBuilder.addIdentity();
  programBuilder.addCondition(filterRel.getCondition());
  return programBuilder.getProgram();
}
 
Example 7
Source File: OLAPFilterRel.java    From kylin with Apache License 2.0 5 votes vote down vote up
@Override
public EnumerableRel implementEnumerable(List<EnumerableRel> inputs) {
    // keep it for having clause
    RexBuilder rexBuilder = getCluster().getRexBuilder();
    RelDataType inputRowType = getInput().getRowType();
    RexProgramBuilder programBuilder = new RexProgramBuilder(inputRowType, rexBuilder);
    programBuilder.addIdentity();
    programBuilder.addCondition(this.condition);
    RexProgram program = programBuilder.getProgram();

    return new EnumerableCalc(getCluster(), getCluster().traitSetOf(EnumerableConvention.INSTANCE), //
            sole(inputs), program);
}
 
Example 8
Source File: FilterCalcMergeRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  final LogicalFilter filter = call.rel(0);
  final LogicalCalc calc = call.rel(1);

  // Don't merge a filter onto a calc which contains windowed aggregates.
  // That would effectively be pushing a multiset down through a filter.
  // We'll have chance to merge later, when the over is expanded.
  if (calc.getProgram().containsAggs()) {
    return;
  }

  // Create a program containing the filter.
  final RexBuilder rexBuilder = filter.getCluster().getRexBuilder();
  final RexProgramBuilder progBuilder =
      new RexProgramBuilder(
          calc.getRowType(),
          rexBuilder);
  progBuilder.addIdentity();
  progBuilder.addCondition(filter.getCondition());
  RexProgram topProgram = progBuilder.getProgram();
  RexProgram bottomProgram = calc.getProgram();

  // Merge the programs together.
  RexProgram mergedProgram =
      RexProgramBuilder.mergePrograms(
          topProgram,
          bottomProgram,
          rexBuilder);
  final LogicalCalc newCalc =
      LogicalCalc.create(calc.getInput(), mergedProgram);
  call.transformTo(newCalc);
}
 
Example 9
Source File: RelWriterTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testCalc() {
  final FrameworkConfig config = RelBuilderTest.config().build();
  final RelBuilder builder = RelBuilder.create(config);
  final RexBuilder rexBuilder = builder.getRexBuilder();
  final LogicalTableScan scan = (LogicalTableScan) builder.scan("EMP").build();
  final RexProgramBuilder programBuilder =
      new RexProgramBuilder(scan.getRowType(), rexBuilder);
  final RelDataTypeField field = scan.getRowType().getField("SAL", false, false);
  programBuilder.addIdentity();
  programBuilder.addCondition(
      rexBuilder.makeCall(SqlStdOperatorTable.GREATER_THAN,
          new RexInputRef(field.getIndex(), field.getType()),
          builder.literal(10)));
  final LogicalCalc calc = LogicalCalc.create(scan, programBuilder.getProgram());
  String relJson = RelOptUtil.dumpPlan("", calc,
      SqlExplainFormat.JSON, SqlExplainLevel.EXPPLAN_ATTRIBUTES);
  String s =
      Frameworks.withPlanner((cluster, relOptSchema, rootSchema) -> {
        final RelJsonReader reader = new RelJsonReader(
            cluster, getSchema(calc), rootSchema);
        RelNode node;
        try {
          node = reader.read(relJson);
        } catch (IOException e) {
          throw TestUtil.rethrow(e);
        }
        return RelOptUtil.dumpPlan("", node, SqlExplainFormat.TEXT,
            SqlExplainLevel.EXPPLAN_ATTRIBUTES);
      });
  final String expected =
      "LogicalCalc(expr#0..7=[{inputs}], expr#8=[10], expr#9=[>($t5, $t8)],"
          + " proj#0..7=[{exprs}], $condition=[$t9])\n"
          + "  LogicalTableScan(table=[[scott, EMP]])\n";
  assertThat(s, isLinux(expected));
}
 
Example 10
Source File: SemiJoinProjectTransposeRule.java    From Bats with Apache License 2.0 4 votes vote down vote up
/**
 * Pulls the project above the semijoin and returns the resulting semijoin
 * condition. As a result, the semijoin condition should be modified such
 * that references to the LHS of a semijoin should now reference the
 * children of the project that's on the LHS.
 *
 * @param project  LogicalProject on the LHS of the semijoin
 * @param semiJoin the semijoin
 * @return the modified semijoin condition
 */
private RexNode adjustCondition(LogicalProject project, SemiJoin semiJoin) {
  // create two RexPrograms -- the bottom one representing a
  // concatenation of the project and the RHS of the semijoin and the
  // top one representing the semijoin condition

  RexBuilder rexBuilder = project.getCluster().getRexBuilder();
  RelDataTypeFactory typeFactory = rexBuilder.getTypeFactory();
  RelNode rightChild = semiJoin.getRight();

  // for the bottom RexProgram, the input is a concatenation of the
  // child of the project and the RHS of the semijoin
  RelDataType bottomInputRowType =
      SqlValidatorUtil.deriveJoinRowType(
          project.getInput().getRowType(),
          rightChild.getRowType(),
          JoinRelType.INNER,
          typeFactory,
          null,
          semiJoin.getSystemFieldList());
  RexProgramBuilder bottomProgramBuilder =
      new RexProgramBuilder(bottomInputRowType, rexBuilder);

  // add the project expressions, then add input references for the RHS
  // of the semijoin
  for (Pair<RexNode, String> pair : project.getNamedProjects()) {
    bottomProgramBuilder.addProject(pair.left, pair.right);
  }
  int nLeftFields = project.getInput().getRowType().getFieldCount();
  List<RelDataTypeField> rightFields =
      rightChild.getRowType().getFieldList();
  int nRightFields = rightFields.size();
  for (int i = 0; i < nRightFields; i++) {
    final RelDataTypeField field = rightFields.get(i);
    RexNode inputRef =
        rexBuilder.makeInputRef(
            field.getType(), i + nLeftFields);
    bottomProgramBuilder.addProject(inputRef, field.getName());
  }
  RexProgram bottomProgram = bottomProgramBuilder.getProgram();

  // input rowtype into the top program is the concatenation of the
  // project and the RHS of the semijoin
  RelDataType topInputRowType =
      SqlValidatorUtil.deriveJoinRowType(
          project.getRowType(),
          rightChild.getRowType(),
          JoinRelType.INNER,
          typeFactory,
          null,
          semiJoin.getSystemFieldList());
  RexProgramBuilder topProgramBuilder =
      new RexProgramBuilder(
          topInputRowType,
          rexBuilder);
  topProgramBuilder.addIdentity();
  topProgramBuilder.addCondition(semiJoin.getCondition());
  RexProgram topProgram = topProgramBuilder.getProgram();

  // merge the programs and expand out the local references to form
  // the new semijoin condition; it now references a concatenation of
  // the project's child and the RHS of the semijoin
  RexProgram mergedProgram =
      RexProgramBuilder.mergePrograms(
          topProgram,
          bottomProgram,
          rexBuilder);

  return mergedProgram.expandLocalRef(
      mergedProgram.getCondition());
}
 
Example 11
Source File: FlinkSemiAntiJoinProjectTransposeRule.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Pulls the project above the semi/anti join and returns the resulting semi/anti join
 * condition. As a result, the semi/anti join condition should be modified such
 * that references to the LHS of a semi/anti join should now reference the
 * children of the project that's on the LHS.
 *
 * @param project LogicalProject on the LHS of the semi/anti join
 * @param join the semi/anti join
 * @return the modified semi/anti join condition
 */
private RexNode adjustCondition(LogicalProject project, Join join) {
	// create two RexPrograms -- the bottom one representing a
	// concatenation of the project and the RHS of the semi/anti join and the
	// top one representing the semi/anti join condition

	RexBuilder rexBuilder = project.getCluster().getRexBuilder();
	RelDataTypeFactory typeFactory = rexBuilder.getTypeFactory();
	RelNode rightChild = join.getRight();

	// for the bottom RexProgram, the input is a concatenation of the
	// child of the project and the RHS of the semi/anti join
	RelDataType bottomInputRowType =
			SqlValidatorUtil.deriveJoinRowType(
					project.getInput().getRowType(),
					rightChild.getRowType(),
					JoinRelType.INNER,
					typeFactory,
					null,
					join.getSystemFieldList());
	RexProgramBuilder bottomProgramBuilder =
			new RexProgramBuilder(bottomInputRowType, rexBuilder);

	// add the project expressions, then add input references for the RHS
	// of the semi/anti join
	for (Pair<RexNode, String> pair : project.getNamedProjects()) {
		bottomProgramBuilder.addProject(pair.left, pair.right);
	}
	int nLeftFields = project.getInput().getRowType().getFieldCount();
	List<RelDataTypeField> rightFields =
			rightChild.getRowType().getFieldList();
	int nRightFields = rightFields.size();
	for (int i = 0; i < nRightFields; i++) {
		final RelDataTypeField field = rightFields.get(i);
		RexNode inputRef =
				rexBuilder.makeInputRef(
						field.getType(), i + nLeftFields);
		bottomProgramBuilder.addProject(inputRef, field.getName());
	}
	RexProgram bottomProgram = bottomProgramBuilder.getProgram();

	// input rowtype into the top program is the concatenation of the
	// project and the RHS of the semi/anti join
	RelDataType topInputRowType =
			SqlValidatorUtil.deriveJoinRowType(
					project.getRowType(),
					rightChild.getRowType(),
					JoinRelType.INNER,
					typeFactory,
					null,
					join.getSystemFieldList());
	RexProgramBuilder topProgramBuilder =
			new RexProgramBuilder(
					topInputRowType,
					rexBuilder);
	topProgramBuilder.addIdentity();
	topProgramBuilder.addCondition(join.getCondition());
	RexProgram topProgram = topProgramBuilder.getProgram();

	// merge the programs and expand out the local references to form
	// the new semi/anti join condition; it now references a concatenation of
	// the project's child and the RHS of the semi/anti join
	RexProgram mergedProgram =
			RexProgramBuilder.mergePrograms(
					topProgram,
					bottomProgram,
					rexBuilder);

	return mergedProgram.expandLocalRef(
			mergedProgram.getCondition());
}
 
Example 12
Source File: JoinCalcTransposeRule.java    From quark with Apache License 2.0 4 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  final Join joinRel = call.rel(0);
  final RelNode otherNode;
  final Calc calc;

  final RelNode leftJoinChild;
  final RelNode rightJoinChild;

  if (call.rel(1) instanceof Calc) {
    otherNode = call.rel(2);
    calc = call.rel(1);
    rightJoinChild = otherNode;
    leftJoinChild = calc.getInput();
  } else {
    otherNode = call.rel(1);
    calc = call.rel(2);
    rightJoinChild = calc.getInput();
    leftJoinChild = otherNode;
  }
  /**
   * Currently not supporting calc which doesnot
   * project star (all the columns of input)
   * or has aggregates.
   */
  if (!isStar(calc.getProgram())
      || calc.getProgram().containsAggs()) {
    return;
  }

  final List<RelDataTypeField> origFields =
      calc.getRowType().getFieldList();
  final int[] adjustments = new int[calc.getProgram().getExprCount()];
  if (rightJoinChild == calc.getInput()) {
    int offset = leftJoinChild.getRowType().getFieldList().size();
    for (int i = 0; i < origFields.size(); i++) {
      adjustments[i] = offset;
    }
  }
  Join newJoinRel =
      joinRel.copy(joinRel.getTraitSet(), joinRel.getCondition(),
          leftJoinChild, rightJoinChild, joinRel.getJoinType(),
          joinRel.isSemiJoinDone());

  RexProgramBuilder topProgramBuilder =
      new RexProgramBuilder(
          joinRel.getRowType(),
          joinRel.getCluster().getRexBuilder());
  topProgramBuilder.addIdentity();
  final RelOptUtil.RexInputConverter rexInputConverter =
      new RelOptUtil.RexInputConverter(calc.getCluster().getRexBuilder(),
          origFields,
          joinRel.getRowType().getFieldList(),
          adjustments);
  if (calc.getProgram().getCondition() != null) {
    RexNode cond =
        calc.getProgram().expandLocalRef(calc.getProgram().getCondition());
    final RexLocalRef rexLocalRef =
        topProgramBuilder.addExpr(cond.accept(rexInputConverter));
    topProgramBuilder.addCondition(rexLocalRef);
  }
  Calc newCalcRel =
      calc.copy(calc.getTraitSet(), newJoinRel, topProgramBuilder.getProgram());

  call.transformTo(newCalcRel);
}
 
Example 13
Source File: FlinkSemiAntiJoinProjectTransposeRule.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Pulls the project above the semi/anti join and returns the resulting semi/anti join
 * condition. As a result, the semi/anti join condition should be modified such
 * that references to the LHS of a semi/anti join should now reference the
 * children of the project that's on the LHS.
 *
 * @param project LogicalProject on the LHS of the semi/anti join
 * @param join the semi/anti join
 * @return the modified semi/anti join condition
 */
private RexNode adjustCondition(LogicalProject project, Join join) {
	// create two RexPrograms -- the bottom one representing a
	// concatenation of the project and the RHS of the semi/anti join and the
	// top one representing the semi/anti join condition

	RexBuilder rexBuilder = project.getCluster().getRexBuilder();
	RelDataTypeFactory typeFactory = rexBuilder.getTypeFactory();
	RelNode rightChild = join.getRight();

	// for the bottom RexProgram, the input is a concatenation of the
	// child of the project and the RHS of the semi/anti join
	RelDataType bottomInputRowType =
			SqlValidatorUtil.deriveJoinRowType(
					project.getInput().getRowType(),
					rightChild.getRowType(),
					JoinRelType.INNER,
					typeFactory,
					null,
					join.getSystemFieldList());
	RexProgramBuilder bottomProgramBuilder =
			new RexProgramBuilder(bottomInputRowType, rexBuilder);

	// add the project expressions, then add input references for the RHS
	// of the semi/anti join
	for (Pair<RexNode, String> pair : project.getNamedProjects()) {
		bottomProgramBuilder.addProject(pair.left, pair.right);
	}
	int nLeftFields = project.getInput().getRowType().getFieldCount();
	List<RelDataTypeField> rightFields =
			rightChild.getRowType().getFieldList();
	int nRightFields = rightFields.size();
	for (int i = 0; i < nRightFields; i++) {
		final RelDataTypeField field = rightFields.get(i);
		RexNode inputRef =
				rexBuilder.makeInputRef(
						field.getType(), i + nLeftFields);
		bottomProgramBuilder.addProject(inputRef, field.getName());
	}
	RexProgram bottomProgram = bottomProgramBuilder.getProgram();

	// input rowtype into the top program is the concatenation of the
	// project and the RHS of the semi/anti join
	RelDataType topInputRowType =
			SqlValidatorUtil.deriveJoinRowType(
					project.getRowType(),
					rightChild.getRowType(),
					JoinRelType.INNER,
					typeFactory,
					null,
					join.getSystemFieldList());
	RexProgramBuilder topProgramBuilder =
			new RexProgramBuilder(
					topInputRowType,
					rexBuilder);
	topProgramBuilder.addIdentity();
	topProgramBuilder.addCondition(join.getCondition());
	RexProgram topProgram = topProgramBuilder.getProgram();

	// merge the programs and expand out the local references to form
	// the new semi/anti join condition; it now references a concatenation of
	// the project's child and the RHS of the semi/anti join
	RexProgram mergedProgram =
			RexProgramBuilder.mergePrograms(
					topProgram,
					bottomProgram,
					rexBuilder);

	return mergedProgram.expandLocalRef(
			mergedProgram.getCondition());
}
 
Example 14
Source File: SemiJoinProjectTransposeRule.java    From calcite with Apache License 2.0 4 votes vote down vote up
/**
 * Pulls the project above the semijoin and returns the resulting semijoin
 * condition. As a result, the semijoin condition should be modified such
 * that references to the LHS of a semijoin should now reference the
 * children of the project that's on the LHS.
 *
 * @param project  LogicalProject on the LHS of the semijoin
 * @param semiJoin the semijoin
 * @return the modified semijoin condition
 */
private RexNode adjustCondition(LogicalProject project, LogicalJoin semiJoin) {
  // create two RexPrograms -- the bottom one representing a
  // concatenation of the project and the RHS of the semijoin and the
  // top one representing the semijoin condition

  RexBuilder rexBuilder = project.getCluster().getRexBuilder();
  RelDataTypeFactory typeFactory = rexBuilder.getTypeFactory();
  RelNode rightChild = semiJoin.getRight();

  // for the bottom RexProgram, the input is a concatenation of the
  // child of the project and the RHS of the semijoin
  RelDataType bottomInputRowType =
      SqlValidatorUtil.deriveJoinRowType(
          project.getInput().getRowType(),
          rightChild.getRowType(),
          JoinRelType.INNER,
          typeFactory,
          null,
          semiJoin.getSystemFieldList());
  RexProgramBuilder bottomProgramBuilder =
      new RexProgramBuilder(bottomInputRowType, rexBuilder);

  // add the project expressions, then add input references for the RHS
  // of the semijoin
  for (Pair<RexNode, String> pair : project.getNamedProjects()) {
    bottomProgramBuilder.addProject(pair.left, pair.right);
  }
  int nLeftFields = project.getInput().getRowType().getFieldCount();
  List<RelDataTypeField> rightFields =
      rightChild.getRowType().getFieldList();
  int nRightFields = rightFields.size();
  for (int i = 0; i < nRightFields; i++) {
    final RelDataTypeField field = rightFields.get(i);
    RexNode inputRef =
        rexBuilder.makeInputRef(
            field.getType(), i + nLeftFields);
    bottomProgramBuilder.addProject(inputRef, field.getName());
  }
  RexProgram bottomProgram = bottomProgramBuilder.getProgram();

  // input rowtype into the top program is the concatenation of the
  // project and the RHS of the semijoin
  RelDataType topInputRowType =
      SqlValidatorUtil.deriveJoinRowType(
          project.getRowType(),
          rightChild.getRowType(),
          JoinRelType.INNER,
          typeFactory,
          null,
          semiJoin.getSystemFieldList());
  RexProgramBuilder topProgramBuilder =
      new RexProgramBuilder(
          topInputRowType,
          rexBuilder);
  topProgramBuilder.addIdentity();
  topProgramBuilder.addCondition(semiJoin.getCondition());
  RexProgram topProgram = topProgramBuilder.getProgram();

  // merge the programs and expand out the local references to form
  // the new semijoin condition; it now references a concatenation of
  // the project's child and the RHS of the semijoin
  RexProgram mergedProgram =
      RexProgramBuilder.mergePrograms(
          topProgram,
          bottomProgram,
          rexBuilder);

  return mergedProgram.expandLocalRef(
      mergedProgram.getCondition());
}