Java Code Examples for org.apache.calcite.rel.logical.LogicalJoin#getLeft()

The following examples show how to use org.apache.calcite.rel.logical.LogicalJoin#getLeft() . 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: RelDecorrelator.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Pulls project above the join from its RHS input. Enforces nullability
 * for join output.
 *
 * @param join          Join
 * @param project       Original project as the right-hand input of the join
 * @param nullIndicatorPos Position of null indicator
 * @return the subtree with the new Project at the root
 */
private RelNode projectJoinOutputWithNullability(LogicalJoin join, LogicalProject project, int nullIndicatorPos) {
    final RelDataTypeFactory typeFactory = join.getCluster().getTypeFactory();
    final RelNode left = join.getLeft();
    final JoinRelType joinType = join.getJoinType();

    RexInputRef nullIndicator = RexBuilder.getRexFactory().makeInputRef(nullIndicatorPos, typeFactory
            .createTypeWithNullability(join.getRowType().getFieldList().get(nullIndicatorPos).getType(), true));

    // now create the new project
    List<Pair<RexNode, String>> newProjExprs = new ArrayList<>();

    // project everything from the LHS and then those from the original
    // projRel
    List<RelDataTypeField> leftInputFields = left.getRowType().getFieldList();

    for (int i = 0; i < leftInputFields.size(); i++) {
        newProjExprs.add(RexInputRef.of2(i, leftInputFields));
    }

    // Marked where the projected expr is coming from so that the types will
    // become nullable for the original projections which are now coming out
    // of the nullable side of the OJ.
    boolean projectPulledAboveLeftCorrelator = joinType.generatesNullsOnRight();

    for (Pair<RexNode, String> pair : project.getNamedProjects()) {
        RexNode newProjExpr = removeCorrelationExpr(pair.left, projectPulledAboveLeftCorrelator, nullIndicator);

        newProjExprs.add(Pair.of(newProjExpr, pair.right));
    }

    return relBuilder.push(join).projectNamed(Pair.left(newProjExprs), Pair.right(newProjExprs), true).build();
}
 
Example 2
Source File: RelDecorrelator.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Pulls project above the join from its RHS input. Enforces nullability
 * for join output.
 *
 * @param join          Join
 * @param project       Original project as the right-hand input of the join
 * @param nullIndicatorPos Position of null indicator
 * @return the subtree with the new Project at the root
 */
private RelNode projectJoinOutputWithNullability(
    LogicalJoin join,
    LogicalProject project,
    int nullIndicatorPos) {
  final RelDataTypeFactory typeFactory = join.getCluster().getTypeFactory();
  final RelNode left = join.getLeft();
  final JoinRelType joinType = join.getJoinType();

  RexInputRef nullIndicator =
      new RexInputRef(
          nullIndicatorPos,
          typeFactory.createTypeWithNullability(
              join.getRowType().getFieldList().get(nullIndicatorPos)
                  .getType(),
              true));

  // now create the new project
  List<Pair<RexNode, String>> newProjExprs = new ArrayList<>();

  // project everything from the LHS and then those from the original
  // projRel
  List<RelDataTypeField> leftInputFields =
      left.getRowType().getFieldList();

  for (int i = 0; i < leftInputFields.size(); i++) {
    newProjExprs.add(RexInputRef.of2(i, leftInputFields));
  }

  // Marked where the projected expr is coming from so that the types will
  // become nullable for the original projections which are now coming out
  // of the nullable side of the OJ.
  boolean projectPulledAboveLeftCorrelator =
      joinType.generatesNullsOnRight();

  for (Pair<RexNode, String> pair : project.getNamedProjects()) {
    RexNode newProjExpr =
        removeCorrelationExpr(
            pair.left,
            projectPulledAboveLeftCorrelator,
            nullIndicator);

    newProjExprs.add(Pair.of(newProjExpr, pair.right));
  }

  return relBuilder.push(join)
      .projectNamed(Pair.left(newProjExprs), Pair.right(newProjExprs), true)
      .build();
}
 
Example 3
Source File: RelDecorrelator.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Pulls project above the join from its RHS input. Enforces nullability
 * for join output.
 *
 * @param join          Join
 * @param project       Original project as the right-hand input of the join
 * @param nullIndicatorPos Position of null indicator
 * @return the subtree with the new Project at the root
 */
private RelNode projectJoinOutputWithNullability(
    LogicalJoin join,
    LogicalProject project,
    int nullIndicatorPos) {
  final RelDataTypeFactory typeFactory = join.getCluster().getTypeFactory();
  final RelNode left = join.getLeft();
  final JoinRelType joinType = join.getJoinType();

  RexInputRef nullIndicator =
      new RexInputRef(
          nullIndicatorPos,
          typeFactory.createTypeWithNullability(
              join.getRowType().getFieldList().get(nullIndicatorPos)
                  .getType(),
              true));

  // now create the new project
  List<Pair<RexNode, String>> newProjExprs = new ArrayList<>();

  // project everything from the LHS and then those from the original
  // projRel
  List<RelDataTypeField> leftInputFields =
      left.getRowType().getFieldList();

  for (int i = 0; i < leftInputFields.size(); i++) {
    newProjExprs.add(RexInputRef.of2(i, leftInputFields));
  }

  // Marked where the projected expr is coming from so that the types will
  // become nullable for the original projections which are now coming out
  // of the nullable side of the OJ.
  boolean projectPulledAboveLeftCorrelator =
      joinType.generatesNullsOnRight();

  for (Pair<RexNode, String> pair : project.getNamedProjects()) {
    RexNode newProjExpr =
        removeCorrelationExpr(
            pair.left,
            projectPulledAboveLeftCorrelator,
            nullIndicator);

    newProjExprs.add(Pair.of(newProjExpr, pair.right));
  }

  return relBuilder.push(join)
      .projectNamed(Pair.left(newProjExprs), Pair.right(newProjExprs), true)
      .build();
}
 
Example 4
Source File: JoinTranslator.java    From samza with Apache License 2.0 4 votes vote down vote up
void translate(final LogicalJoin join, final TranslatorContext translatorContext) {
  JoinInputNode.InputType inputTypeOnLeft = getInputType(join.getLeft(), translatorContext);
  JoinInputNode.InputType inputTypeOnRight = getInputType(join.getRight(), translatorContext);

  // Do the validation of join query
  validateJoinQuery(join, inputTypeOnLeft, inputTypeOnRight);

  // At this point, one of the sides is a table. Let's figure out if it is on left or right side.
  boolean isTablePosOnRight = inputTypeOnRight != JoinInputNode.InputType.STREAM;

  // stream and table keyIds are used to extract the join condition field (key) names and values out of the stream
  // and table records.
  List<Integer> streamKeyIds = new LinkedList<>();
  List<Integer> tableKeyIds = new LinkedList<>();

  // Fetch the stream and table indices corresponding to the fields given in the join condition.

  final int leftSideSize = join.getLeft().getRowType().getFieldCount();
  final int tableStartIdx = isTablePosOnRight ? leftSideSize : 0;
  final int streamStartIdx = isTablePosOnRight ? 0 : leftSideSize;
  final int tableEndIdx = isTablePosOnRight ? join.getRowType().getFieldCount() : leftSideSize;
  join.getCondition().accept(new RexShuttle() {
    @Override
    public RexNode visitInputRef(RexInputRef inputRef) {
      validateJoinKeyType(inputRef); // Validate the type of the input ref.
      int index = inputRef.getIndex();
      if (index >= tableStartIdx && index < tableEndIdx) {
        tableKeyIds.add(index - tableStartIdx);
      } else {
        streamKeyIds.add(index - streamStartIdx);
      }
      return inputRef;
    }
  });
  Collections.sort(tableKeyIds);
  Collections.sort(streamKeyIds);

  // Get the two input nodes (stream and table nodes) for the join.
  JoinInputNode streamNode = new JoinInputNode(isTablePosOnRight ? join.getLeft() : join.getRight(), streamKeyIds,
      isTablePosOnRight ? inputTypeOnLeft : inputTypeOnRight, !isTablePosOnRight);
  JoinInputNode tableNode = new JoinInputNode(isTablePosOnRight ? join.getRight() : join.getLeft(), tableKeyIds,
      isTablePosOnRight ? inputTypeOnRight : inputTypeOnLeft, isTablePosOnRight);

  MessageStream<SamzaSqlRelMessage> inputStream = translatorContext.getMessageStream(streamNode.getRelNode().getId());
  Table table = getTable(tableNode, translatorContext);

  MessageStream<SamzaSqlRelMessage> outputStream =
      joinStreamWithTable(inputStream, table, streamNode, tableNode, join, translatorContext);

  translatorContext.registerMessageStream(join.getId(), outputStream);

  outputStream.map(outputMetricsMF);
}
 
Example 5
Source File: PigRules.java    From calcite with Apache License 2.0 4 votes vote down vote up
public RelNode convert(RelNode rel) {
  final LogicalJoin join = (LogicalJoin) rel;
  final RelTraitSet traitSet = join.getTraitSet().replace(PigRel.CONVENTION);
  return new PigJoin(join.getCluster(), traitSet, join.getLeft(), join.getRight(),
      join.getCondition(), join.getJoinType());
}