Java Code Examples for org.apache.calcite.rel.core.JoinRelType#RIGHT

The following examples show how to use org.apache.calcite.rel.core.JoinRelType#RIGHT . 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: JoinNode.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Execution of the join action, returns the matched rows for the outer source row.
 */
private List<Row> doJoin(Row outerRow, List<Row> innerRows,
    JoinRelType joinRelType) throws InterruptedException {
  boolean outerRowOnLeft = joinRelType != JoinRelType.RIGHT;
  copyToContext(outerRow, outerRowOnLeft);
  List<Row> matchInnerRows = new ArrayList<>();
  for (Row innerRow: innerRows) {
    copyToContext(innerRow, !outerRowOnLeft);
    final Boolean execute = (Boolean) condition.execute(context);
    if (execute != null && execute) {
      matchInnerRows.add(innerRow);
    }
  }
  doSend(outerRow, matchInnerRows, joinRelType);
  return matchInnerRows;
}
 
Example 2
Source File: MergeJoinComparatorTemplate.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@Override
public boolean finishNonMatching() {
  Preconditions.checkArgument(!leftIterator.hasNext() || !rightIterator.hasNext());

  // allocate new buffer for new batch
  if (outputRecordsCounter == 0) {
    outgoing.allocateNew();
  }

  if (joinType == JoinRelType.RIGHT) {
    return projectRightNonMatching();
  } else if (joinType == JoinRelType.LEFT) {
    return projectLeftNonMatching();
  } else if (joinType == JoinRelType.FULL) {
    if (leftIterator.hasNext()) {
      return projectLeftNonMatching();
    } else {
      return projectRightNonMatching();
    }
  } else {
    throw new IllegalStateException("Reach unexpected state");
  }
}
 
Example 3
Source File: HBTQueryConvertor.java    From Mycat2 with GNU General Public License v3.0 6 votes vote down vote up
private JoinRelType joinOp(HBTOp op) {
    switch (op) {
        case INNER_JOIN:
            return JoinRelType.INNER;
        case LEFT_JOIN:
            return JoinRelType.LEFT;
        case RIGHT_JOIN:
            return JoinRelType.RIGHT;
        case FULL_JOIN:
            return JoinRelType.FULL;
        case SEMI_JOIN:
            return JoinRelType.SEMI;
        case ANTI_JOIN:
            return JoinRelType.ANTI;
        case CORRELATE_INNER_JOIN:
            return JoinRelType.INNER;
        case CORRELATE_LEFT_JOIN:
            return JoinRelType.LEFT;
        default:
            throw new UnsupportedOperationException();
    }
}
 
Example 4
Source File: QueryOperationConverter.java    From flink with Apache License 2.0 5 votes vote down vote up
private JoinRelType convertJoinType(JoinType joinType) {
	switch (joinType) {
		case INNER:
			return JoinRelType.INNER;
		case LEFT_OUTER:
			return JoinRelType.LEFT;
		case RIGHT_OUTER:
			return JoinRelType.RIGHT;
		case FULL_OUTER:
			return JoinRelType.FULL;
		default:
			throw new TableException("Unknown join type: " + joinType);
	}
}
 
Example 5
Source File: QueryOperationConverter.java    From flink with Apache License 2.0 5 votes vote down vote up
private JoinRelType convertJoinType(JoinType joinType) {
	switch (joinType) {
		case INNER:
			return JoinRelType.INNER;
		case LEFT_OUTER:
			return JoinRelType.LEFT;
		case RIGHT_OUTER:
			return JoinRelType.RIGHT;
		case FULL_OUTER:
			return JoinRelType.FULL;
		default:
			throw new TableException("Unknown join type: " + joinType);
	}
}
 
Example 6
Source File: HashJoinOperator.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public void noMoreToConsumeLeft() throws Exception {
  state.is(State.CAN_CONSUME_L);

  finishedProbe = true;
  if(joinType == JoinRelType.FULL || joinType == JoinRelType.RIGHT){
    // if we need to project build records that didn't match, make sure we do so.
    state = State.CAN_PRODUCE;
  } else {
    state = State.DONE;
  }
}
 
Example 7
Source File: VectorizedHashJoinOperator.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public void noMoreToConsumeLeft() throws Exception {
  state.is(State.CAN_CONSUME_L);

  finishedProbe = true;
  if(joinType == JoinRelType.FULL || joinType == JoinRelType.RIGHT){
    // if we need to project build records that didn't match, make sure we do so.
    state = State.CAN_PRODUCE;
  } else {
    state = State.DONE;
  }
}
 
Example 8
Source File: FlinkJoinToMultiJoinRule.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Combines the join filters from the left and right inputs (if they are
 * MultiJoinRels) with the join filter in the joinrel into a single AND'd
 * join filter, unless the inputs correspond to null generating inputs in an
 * outer join.
 *
 * @param joinRel join rel
 * @param left    left child of the join
 * @param right   right child of the join
 * @return combined join filters AND-ed together
 */
private List<RexNode> combineJoinFilters(
		Join joinRel,
		RelNode left,
		RelNode right) {
	JoinRelType joinType = joinRel.getJoinType();

	// AND the join condition if this isn't a left or right outer join;
	// in those cases, the outer join condition is already tracked
	// separately
	final List<RexNode> filters = new ArrayList<>();
	if ((joinType != JoinRelType.LEFT) && (joinType != JoinRelType.RIGHT)) {
		filters.add(joinRel.getCondition());
	}
	if (canCombine(left, joinType.generatesNullsOnLeft())) {
		filters.add(((MultiJoin) left).getJoinFilter());
	}
	// Need to adjust the RexInputs of the right child, since
	// those need to shift over to the right
	if (canCombine(right, joinType.generatesNullsOnRight())) {
		MultiJoin multiJoin = (MultiJoin) right;
		filters.add(
				shiftRightFilter(joinRel, left, multiJoin,
						multiJoin.getJoinFilter()));
	}

	return filters;
}
 
Example 9
Source File: QueryOperationConverter.java    From flink with Apache License 2.0 5 votes vote down vote up
private JoinRelType convertJoinType(JoinType joinType) {
	switch (joinType) {
		case INNER:
			return JoinRelType.INNER;
		case LEFT_OUTER:
			return JoinRelType.LEFT;
		case RIGHT_OUTER:
			return JoinRelType.RIGHT;
		case FULL_OUTER:
			return JoinRelType.FULL;
		default:
			throw new TableException("Unknown join type: " + joinType);
	}
}
 
Example 10
Source File: QueryOperationConverter.java    From flink with Apache License 2.0 5 votes vote down vote up
private JoinRelType convertJoinType(JoinType joinType) {
	switch (joinType) {
		case INNER:
			return JoinRelType.INNER;
		case LEFT_OUTER:
			return JoinRelType.LEFT;
		case RIGHT_OUTER:
			return JoinRelType.RIGHT;
		case FULL_OUTER:
			return JoinRelType.FULL;
		default:
			throw new TableException("Unknown join type: " + joinType);
	}
}
 
Example 11
Source File: EnumerableBatchNestedLoopJoin.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public DeriveMode getDeriveMode() {
  if (joinType == JoinRelType.FULL || joinType == JoinRelType.RIGHT) {
    return DeriveMode.PROHIBITED;
  }

  return DeriveMode.LEFT_FIRST;
}
 
Example 12
Source File: PigRelOpVisitor.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Decides the join type from the inner types of both relation.
 *
 * @param leftInner  true if the left requires inner
 * @param rightInner true if the right requires inner
 * @return The join type, either INNER, LEFT, RIGHT, or FULL
 */
private static JoinRelType getJoinType(boolean leftInner, boolean rightInner) {
  if (leftInner && rightInner) {
    return JoinRelType.INNER;
  } else if (leftInner) {
    return JoinRelType.LEFT;
  } else if (rightInner) {
    return JoinRelType.RIGHT;
  } else {
    return JoinRelType.FULL;
  }
}
 
Example 13
Source File: HashJoinProbeTemplate.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public void changeToFinalProbeState() {
  // We are done with the (left) probe phase.
  // If it's a RIGHT or a FULL join then need to get the unmatched indexes from the build side
  probeState =
    (joinType == JoinRelType.RIGHT || joinType == JoinRelType.FULL) ? ProbeState.PROJECT_RIGHT :
      ProbeState.DONE; // else we're done
}
 
Example 14
Source File: HashJoinPOP.java    From Bats with Apache License 2.0 5 votes vote down vote up
public HashJoinPOP flipIfRight() {
    if (joinType == JoinRelType.RIGHT) {
      List<JoinCondition> flippedConditions = Lists.newArrayList();
      for (JoinCondition c : conditions) {
        flippedConditions.add(c.flip());
      }
      return new HashJoinPOP(right, left, flippedConditions, JoinRelType.LEFT, semiJoin, runtimeFilterDef, isRowKeyJoin, joinControl);
    } else {
      return this;
    }
}
 
Example 15
Source File: JoinToMultiJoinRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Combines the join filters from the left and right inputs (if they are
 * MultiJoinRels) with the join filter in the joinrel into a single AND'd
 * join filter, unless the inputs correspond to null generating inputs in an
 * outer join
 *
 * @param joinRel join rel
 * @param left    left child of the join
 * @param right   right child of the join
 * @return combined join filters AND-ed together
 */
private List<RexNode> combineJoinFilters(
    Join joinRel,
    RelNode left,
    RelNode right) {
  JoinRelType joinType = joinRel.getJoinType();

  // AND the join condition if this isn't a left or right outer join;
  // in those cases, the outer join condition is already tracked
  // separately
  final List<RexNode> filters = new ArrayList<>();
  if ((joinType != JoinRelType.LEFT) && (joinType != JoinRelType.RIGHT)) {
    filters.add(joinRel.getCondition());
  }
  if (canCombine(left, joinType.generatesNullsOnLeft())) {
    filters.add(((MultiJoin) left).getJoinFilter());
  }
  // Need to adjust the RexInputs of the right child, since
  // those need to shift over to the right
  if (canCombine(right, joinType.generatesNullsOnRight())) {
    MultiJoin multiJoin = (MultiJoin) right;
    filters.add(
        shiftRightFilter(joinRel, left, multiJoin,
            multiJoin.getJoinFilter()));
  }

  return filters;
}
 
Example 16
Source File: OLAPJoinRel.java    From kylin-on-parquet-v2 with Apache License 2.0 4 votes vote down vote up
@Override
    public void implementOLAP(OLAPImplementor implementor) {

        // create context for root join
        if (!(implementor.getParentNode() instanceof OLAPJoinRel) && !isParentMerelyPermutation(implementor)) {
            implementor.allocateContext();
        }
        //parent context
        this.context = implementor.getContext();
        this.context.allOlapJoins.add(this);
        this.isTopJoin = !this.context.hasJoin;
        this.context.hasJoin = true;

        boolean leftHasSubquery = false;
        boolean rightHasSubquery = false;

        // as we keep the first table as fact table, we need to visit from left to right
        implementor.fixSharedOlapTableScanOnTheLeft(this);
        implementor.visitChild(this.left, this);

        //current  has another context
        if (this.context != implementor.getContext() || ((OLAPRel) this.left).hasSubQuery()) {
            this.hasSubQuery = true;
            leftHasSubquery = true;
            // if child is also an OLAPJoin, then the context has already been popped
            if (this.context != implementor.getContext()) {
                implementor.freeContext();
            }
        }

        if (leftHasSubquery) {
            // After KYLIN-2579, leftHasSubquery means right side have to be separate olap context 
            implementor.setNewOLAPContextRequired(true);
        }

        implementor.fixSharedOlapTableScanOnTheRight(this);
        implementor.visitChild(this.right, this);
        if (this.context != implementor.getContext() || ((OLAPRel) this.right).hasSubQuery()) {
            this.hasSubQuery = true;
            rightHasSubquery = true;
            // if child is also an OLAPJoin, then the context has already been popped

            if (leftHasSubquery) {
                Preconditions.checkState(!implementor.isNewOLAPContextRequired());//should have been satisfied
                Preconditions.checkState(this.context != implementor.getContext(), "missing a new olapcontext");
            }

            if (this.context != implementor.getContext()) {
                implementor.freeContext();
            }
        }

        this.columnRowType = buildColumnRowType();

        if (isTopJoin) {
            this.context.afterJoin = true;
        }

        if (!this.hasSubQuery) {
//            this.context.allColumns.clear();

            // build JoinDesc
            Preconditions.checkState(this.getCondition() instanceof RexCall, "Cartesian Join is not supported.");

            RexCall condition = (RexCall) this.getCondition();
            JoinDesc join = buildJoin(condition);

            JoinRelType joinRelType = this.getJoinType();
            String joinType = joinRelType == JoinRelType.INNER ? "INNER"
                    : joinRelType == JoinRelType.LEFT ? "LEFT" : joinRelType == JoinRelType.RIGHT ? "RIGHT" : "FULL";
            join.setType(joinType);

            this.context.joins.add(join);
        } else {
            //When join contains subquery, the join-condition fields of fact_table will add into context.
            Multimap<TblColRef, TblColRef> joinCol = HashMultimap.create();
            translateJoinColumn(this.getCondition(), joinCol);

            for (Map.Entry<TblColRef, TblColRef> columnPair : joinCol.entries()) {
                TblColRef fromCol = (rightHasSubquery ? columnPair.getKey() : columnPair.getValue());
                this.context.subqueryJoinParticipants.add(fromCol);
            }
            joinCol.clear();
        }
    }
 
Example 17
Source File: RelMdRowCount.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
public static double estimateRowCount(Join rel, RelMetadataQuery mq) {
  double rightJoinFactor = 1.0;

  RexNode condition = rel.getCondition();
  if (condition.isAlwaysTrue()) {
    // Cartesian join is only supported for NLJ. If join type is right, make it more expensive
    if (rel.getJoinType() == JoinRelType.RIGHT) {
      rightJoinFactor = 2.0;
    }
    return RelMdUtil.getJoinRowCount(mq, rel, condition) * rightJoinFactor;
  }

  final PlannerSettings plannerSettings = PrelUtil.getPlannerSettings(rel.getCluster().getPlanner());
  double filterMinSelectivityEstimateFactor = plannerSettings == null ?
    PlannerSettings.DEFAULT_FILTER_MIN_SELECTIVITY_ESTIMATE_FACTOR :
    plannerSettings.getFilterMinSelectivityEstimateFactor();
  double filterMaxSelectivityEstimateFactor = plannerSettings == null ?
    PlannerSettings.DEFAULT_FILTER_MAX_SELECTIVITY_ESTIMATE_FACTOR :
    plannerSettings.getFilterMaxSelectivityEstimateFactor();

  final RexNode remaining;
  if (rel instanceof JoinRelBase) {
    remaining = ((JoinRelBase) rel).getRemaining();
  } else {
    remaining = RelOptUtil.splitJoinCondition(rel.getLeft(), rel.getRight(), condition, new ArrayList<>(), new ArrayList<>(), new ArrayList<>());
  }

  double selectivity = mq.getSelectivity(rel, remaining);
  if (!remaining.isAlwaysFalse()) {
    // Cap selectivity at filterMinSelectivityEstimateFactor unless it is always FALSE
    if (selectivity < filterMinSelectivityEstimateFactor) {
      selectivity = filterMinSelectivityEstimateFactor;
    }
  }

  if (!remaining.isAlwaysTrue()) {
    // Cap selectivity at filterMaxSelectivityEstimateFactor unless it is always TRUE
    if (selectivity > filterMaxSelectivityEstimateFactor) {
      selectivity = filterMaxSelectivityEstimateFactor;
    }
    // Make right join more expensive for inequality join condition (logical phase)
    if (rel.getJoinType() == JoinRelType.RIGHT) {
      rightJoinFactor = 2.0;
    }
  }

  return selectivity * Math.max(mq.getRowCount(rel.getLeft()), mq.getRowCount(rel.getRight())) * rightJoinFactor;
}
 
Example 18
Source File: VectorizedProbe.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
public void setup(
    BufferAllocator allocator,
    final ExpandableHyperContainer buildBatch,
    final VectorAccessible probeBatch,
    // Contains all vectors in probe side output
    final List<FieldVector> probeOutputs,
    /* Contains only carry over vectors in build side output for VECTORIZED_GENERIC
     * Contains all field vectors in build side output for VECTORIZED_BIGINT
     */
    final List<FieldVector> buildOutputs,
    /* Contains the key field vectors in incoming probe side batch for VECTORIZED_GENERIC
     * Only for VECTORIZED_GENERIC
     */
    final List<FieldVector> probeIncomingKeys,
    /* Contains the key field vectors in build side output for VECTORIZED_GENERIC
     * Only for VECTORIZED_GENERIC
     */
    final List<FieldVector> buildOutputKeys,
    VectorizedHashJoinOperator.Mode mode,
    JoinRelType joinRelType,
    List<BuildInfo> buildInfos,
    List<ArrowBuf> startIndices,
    List<MatchBitSet> keyMatchBitVectors,
    int maxHashTableIndex,
    JoinTable table,
    // Used to pivot the keys in incoming build batch into hash table
    PivotDef pivot,
    // Used to unpivot the keys in hash table to build side output
    PivotDef buildUnpivot,
    int targetRecordsPerBatch,
    final NullComparator nullMask){

  this.nullMask = nullMask;
  this.pivot = pivot;
  this.buildUnpivot = buildUnpivot;
  this.allocator = allocator;
  this.table = table;
  this.links = new ArrowBuf[buildInfos.size()];

  for (int i =0; i < links.length; i++) {
    links[i] = buildInfos.get(i).getLinks();
  }

  this.starts = new ArrowBuf[startIndices.size()];
  this.keyMatches = new MatchBitSet[keyMatchBitVectors.size()];

  if (startIndices.size() > 0) {
    this.maxOffsetForLastBatch = maxHashTableIndex - (startIndices.size() - 1) * HashTable.BATCH_SIZE;
  } else {
    this.maxOffsetForLastBatch = -1;
  }
  this.maxHashTableIndex = maxHashTableIndex;
  for (int i = 0; i < starts.length; i++) {
    starts[i] = startIndices.get(i);
    keyMatches[i] = keyMatchBitVectors.get(i);
  }

  this.projectUnmatchedBuild = joinRelType == JoinRelType.RIGHT || joinRelType == JoinRelType.FULL;
  this.projectUnmatchedProbe = joinRelType == JoinRelType.LEFT || joinRelType == JoinRelType.FULL;
  this.targetRecordsPerBatch = targetRecordsPerBatch;
  this.projectProbeSv2 = allocator.buffer(targetRecordsPerBatch * BATCH_OFFSET_SIZE);
  this.probeSv2Addr = projectProbeSv2.memoryAddress();
  // first 4 bytes (int) are for batch index and rest 2 bytes are offset within the batch
  this.projectBuildOffsetBuf = allocator.buffer(targetRecordsPerBatch * BUILD_RECORD_LINK_SIZE);
  this.projectBuildOffsetAddr = projectBuildOffsetBuf.memoryAddress();
  this.projectBuildKeyOffsetBuf = allocator.buffer(targetRecordsPerBatch * ORDINAL_SIZE);
  this.projectBuildKeyOffsetAddr = projectBuildKeyOffsetBuf.memoryAddress();

  this.mode = mode;

  this.buildOutputs = buildOutputs;
  if (table.size() > 0) {
    this.buildCopiers = projectUnmatchedProbe  ?
      ConditionalFieldBufferCopier6.getFourByteCopiers(VectorContainer.getHyperFieldVectors(buildBatch), buildOutputs) :
      FieldBufferCopier6.getFourByteCopiers(VectorContainer.getHyperFieldVectors(buildBatch), buildOutputs);
  } else {
    this.buildCopiers = Collections.emptyList();
  }

  /* For VECTORIZED_GENERIC, we don't keep the key vectors in hyper container,
   * and then we need to copy keys from probe batch to build side in output for matched and non matched records,
   * otherwise eight byte hash table is used, we keep the key vector in hyper container,
   * and then we don't need to copy keys from probe batch to build side in output for matched and non matched records.
   */
  if (this.mode == VectorizedHashJoinOperator.Mode.VECTORIZED_GENERIC) {
    // create copier for copying keys from probe batch to build side output
    if (probeIncomingKeys.size() > 0) {
      this.keysCopiers = FieldBufferCopier.getCopiers(probeIncomingKeys, buildOutputKeys);
    } else {
      this.keysCopiers = Collections.emptyList();
    }

    this.projectNullKeyOffset = allocator.buffer(targetRecordsPerBatch * BATCH_OFFSET_SIZE);
    this.projectNullKeyOffsetAddr = projectNullKeyOffset.memoryAddress();
  } else {
    this.projectNullKeyOffsetAddr = 0;
    this.keysCopiers = null;
  }

  this.probeCopiers = FieldBufferCopier.getCopiers(VectorContainer.getFieldVectors(probeBatch), probeOutputs);
}
 
Example 19
Source File: HashJoinProbeTemplate.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
@Override
public void setupHashJoinProbe(
    FunctionContext functionContext,
    VectorAccessible buildBatch,
    VectorAccessible probeBatch,
    VectorAccessible outgoing,
    HashTable hashTable,
    JoinRelType joinRelType,
    List<BuildInfo> buildInfos,
    List<ArrowBuf> startIndices,
    List<BitSet> keyMatchBitVectors,
    int maxHashTableIndex,
    int targetRecordsPerBatch) {

  links = new ArrowBuf[buildInfos.size()];

  for (int i =0; i < links.length; i++) {
    links[i] = buildInfos.get(i).getLinks();
  }

  starts = new ArrowBuf[startIndices.size()];
  this.keyMatches = new BitSet[keyMatchBitVectors.size()];

  if (startIndices.size() > 0) {
    this.maxOffsetForLastBatch = maxHashTableIndex - (startIndices.size() - 1) * HashTable.BATCH_SIZE;
  } else {
    this.maxOffsetForLastBatch = -1;
  }
  for (int i = 0; i < starts.length; i++) {
    starts[i] = startIndices.get(i);
    keyMatches[i] = keyMatchBitVectors.get(i);
  }

  this.probeBatch = probeBatch;
  this.projectUnmatchedProbe = joinRelType == JoinRelType.LEFT || joinRelType == JoinRelType.FULL;
  this.projectUnmatchedBuild = joinRelType == JoinRelType.RIGHT || joinRelType == JoinRelType.FULL;
  this.hashTable = hashTable;
  this.targetRecordsPerBatch = targetRecordsPerBatch;

  doSetup(functionContext, buildBatch, probeBatch, outgoing);
}
 
Example 20
Source File: JoinNode.java    From calcite with Apache License 2.0 4 votes vote down vote up
public void run() throws InterruptedException {

    final int fieldCount = rel.getLeft().getRowType().getFieldCount()
        + rel.getRight().getRowType().getFieldCount();
    context.values = new Object[fieldCount];

    // source for the outer relation of nested loop
    Source outerSource = leftSource;
    // source for the inner relation of nested loop
    Source innerSource = rightSource;
    if (rel.getJoinType() == JoinRelType.RIGHT) {
      outerSource = rightSource;
      innerSource = leftSource;
    }

    // row from outer source
    Row outerRow = null;
    // rows from inner source
    List<Row> innerRows = null;
    Set<Row> matchRowSet = new HashSet<>();
    while ((outerRow = outerSource.receive()) != null) {
      if (innerRows == null) {
        innerRows = new ArrayList<Row>();
        Row innerRow = null;
        while ((innerRow = innerSource.receive()) != null) {
          innerRows.add(innerRow);
        }
      }
      matchRowSet.addAll(doJoin(outerRow, innerRows, rel.getJoinType()));
    }
    if (rel.getJoinType() == JoinRelType.FULL) {
      // send un-match rows for full join on right source
      List<Row> empty = new ArrayList<>();
      for (Row row: innerRows) {
        if (matchRowSet.contains(row)) {
          continue;
        }
        doSend(row, empty, JoinRelType.RIGHT);
      }
    }
  }