Java Code Examples for org.apache.calcite.rel.core.Join#getRowType()

The following examples show how to use org.apache.calcite.rel.core.Join#getRowType() . 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: ApexRelNode.java    From attic-apex-malhar with Apache License 2.0 5 votes vote down vote up
private RelInfo addJoinFilter(Join join, RexNode remaining, RelInfo relInfo, RelContext context)
{
  FilterTransformOperator operator = context.dag
      .addOperator(OperatorUtils.getUniqueOperatorName(join.getRelTypeName() + "_Filter"),
      FilterTransformOperator.class);
  ExpressionCompiler compiler = new ExpressionCompiler(new RexBuilder(join.getCluster().getTypeFactory()));
  String expression = compiler.getExpression(remaining, join.getRowType(), join.getRowType());

  Map<String, String> expMap = new HashMap<>();
  for (Pair<RelDataTypeField, RelDataTypeField> pair : Pair.zip(join.getRowType().getFieldList(),
      join.getRowType().getFieldList())) {
    String leftName = OperatorUtils.getFieldName(pair.left);
    String rightName = OperatorUtils.getFieldName(pair.right);
    expMap.put(leftName, rightName);
  }
  operator.setExpressionMap(expMap);
  operator.setCondition(expression);

  String streamName = OperatorUtils.getUniqueStreamName(join.getRelTypeName() + "_Join", join.getRelTypeName() +
      "_Filter");
  Class schema = TupleSchemaRegistry.getSchemaForRelDataType(context.schemaRegistry, streamName,
      relInfo.getOutRelDataType());
  context.dag.setOutputPortAttribute(relInfo.getOutPort(), Context.PortContext.TUPLE_CLASS, schema);
  context.dag.setInputPortAttribute(operator.input, Context.PortContext.TUPLE_CLASS, schema);
  context.dag.addStream(streamName, relInfo.getOutPort(), operator.input);

  return new RelInfo("Join", relInfo.getInputPorts(), operator, operator.output, join.getRowType());
}
 
Example 2
Source File: JoinToMultiJoinRule.java    From Bats with Apache License 2.0 4 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  final Join origJoin = call.rel(0);
  final RelNode left = call.rel(1);
  final RelNode right = call.rel(2);

  // combine the children MultiJoin inputs into an array of inputs
  // for the new MultiJoin
  final List<ImmutableBitSet> projFieldsList = new ArrayList<>();
  final List<int[]> joinFieldRefCountsList = new ArrayList<>();
  final List<RelNode> newInputs =
      combineInputs(
          origJoin,
          left,
          right,
          projFieldsList,
          joinFieldRefCountsList);

  // combine the outer join information from the left and right
  // inputs, and include the outer join information from the current
  // join, if it's a left/right outer join
  final List<Pair<JoinRelType, RexNode>> joinSpecs = new ArrayList<>();
  combineOuterJoins(
      origJoin,
      newInputs,
      left,
      right,
      joinSpecs);

  // pull up the join filters from the children MultiJoinRels and
  // combine them with the join filter associated with this LogicalJoin to
  // form the join filter for the new MultiJoin
  List<RexNode> newJoinFilters = combineJoinFilters(origJoin, left, right);

  // add on the join field reference counts for the join condition
  // associated with this LogicalJoin
  final ImmutableMap<Integer, ImmutableIntList> newJoinFieldRefCountsMap =
      addOnJoinFieldRefCounts(newInputs,
          origJoin.getRowType().getFieldCount(),
          origJoin.getCondition(),
          joinFieldRefCountsList);

  List<RexNode> newPostJoinFilters =
      combinePostJoinFilters(origJoin, left, right);

  final RexBuilder rexBuilder = origJoin.getCluster().getRexBuilder();
  RelNode multiJoin =
      new MultiJoin(
          origJoin.getCluster(),
          newInputs,
          RexUtil.composeConjunction(rexBuilder, newJoinFilters),
          origJoin.getRowType(),
          origJoin.getJoinType() == JoinRelType.FULL,
          Pair.right(joinSpecs),
          Pair.left(joinSpecs),
          projFieldsList,
          newJoinFieldRefCountsMap,
          RexUtil.composeConjunction(rexBuilder, newPostJoinFilters, true));

  call.transformTo(multiJoin);
}
 
Example 3
Source File: FlinkJoinToMultiJoinRule.java    From flink with Apache License 2.0 4 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
	final Join origJoin = call.rel(0);
	final RelNode left = call.rel(1);
	final RelNode right = call.rel(2);

	// combine the children MultiJoin inputs into an array of inputs
	// for the new MultiJoin
	final List<ImmutableBitSet> projFieldsList = new ArrayList<>();
	final List<int[]> joinFieldRefCountsList = new ArrayList<>();
	final List<RelNode> newInputs =
			combineInputs(
					origJoin,
					left,
					right,
					projFieldsList,
					joinFieldRefCountsList);

	// combine the outer join information from the left and right
	// inputs, and include the outer join information from the current
	// join, if it's a left/right outer join
	final List<Pair<JoinRelType, RexNode>> joinSpecs = new ArrayList<>();
	combineOuterJoins(
			origJoin,
			newInputs,
			left,
			right,
			joinSpecs);

	// pull up the join filters from the children MultiJoinRels and
	// combine them with the join filter associated with this LogicalJoin to
	// form the join filter for the new MultiJoin
	List<RexNode> newJoinFilters = combineJoinFilters(origJoin, left, right);

	// add on the join field reference counts for the join condition
	// associated with this LogicalJoin
	final com.google.common.collect.ImmutableMap<Integer, ImmutableIntList> newJoinFieldRefCountsMap =
			addOnJoinFieldRefCounts(newInputs,
					origJoin.getRowType().getFieldCount(),
					origJoin.getCondition(),
					joinFieldRefCountsList);

	List<RexNode> newPostJoinFilters =
			combinePostJoinFilters(origJoin, left, right);

	final RexBuilder rexBuilder = origJoin.getCluster().getRexBuilder();
	RelNode multiJoin =
			new MultiJoin(
					origJoin.getCluster(),
					newInputs,
					RexUtil.composeConjunction(rexBuilder, newJoinFilters),
					origJoin.getRowType(),
					origJoin.getJoinType() == JoinRelType.FULL,
					Pair.right(joinSpecs),
					Pair.left(joinSpecs),
					projFieldsList,
					newJoinFieldRefCountsMap,
					RexUtil.composeConjunction(rexBuilder, newPostJoinFilters, true));

	call.transformTo(multiJoin);
}
 
Example 4
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 5
Source File: JoinToMultiJoinRule.java    From calcite with Apache License 2.0 4 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  final Join origJoin = call.rel(0);
  final RelNode left = call.rel(1);
  final RelNode right = call.rel(2);

  // combine the children MultiJoin inputs into an array of inputs
  // for the new MultiJoin
  final List<ImmutableBitSet> projFieldsList = new ArrayList<>();
  final List<int[]> joinFieldRefCountsList = new ArrayList<>();
  final List<RelNode> newInputs =
      combineInputs(
          origJoin,
          left,
          right,
          projFieldsList,
          joinFieldRefCountsList);

  // combine the outer join information from the left and right
  // inputs, and include the outer join information from the current
  // join, if it's a left/right outer join
  final List<Pair<JoinRelType, RexNode>> joinSpecs = new ArrayList<>();
  combineOuterJoins(
      origJoin,
      newInputs,
      left,
      right,
      joinSpecs);

  // pull up the join filters from the children MultiJoinRels and
  // combine them with the join filter associated with this LogicalJoin to
  // form the join filter for the new MultiJoin
  List<RexNode> newJoinFilters = combineJoinFilters(origJoin, left, right);

  // add on the join field reference counts for the join condition
  // associated with this LogicalJoin
  final ImmutableMap<Integer, ImmutableIntList> newJoinFieldRefCountsMap =
      addOnJoinFieldRefCounts(newInputs,
          origJoin.getRowType().getFieldCount(),
          origJoin.getCondition(),
          joinFieldRefCountsList);

  List<RexNode> newPostJoinFilters =
      combinePostJoinFilters(origJoin, left, right);

  final RexBuilder rexBuilder = origJoin.getCluster().getRexBuilder();
  RelNode multiJoin =
      new MultiJoin(
          origJoin.getCluster(),
          newInputs,
          RexUtil.composeConjunction(rexBuilder, newJoinFilters),
          origJoin.getRowType(),
          origJoin.getJoinType() == JoinRelType.FULL,
          Pair.right(joinSpecs),
          Pair.left(joinSpecs),
          projFieldsList,
          newJoinFieldRefCountsMap,
          RexUtil.composeConjunction(rexBuilder, newPostJoinFilters, true));

  call.transformTo(multiJoin);
}