walterddr commented on code in PR #9907:
URL: https://github.com/apache/pinot/pull/9907#discussion_r1051004699
##########
pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/operator/HashJoinOperator.java:
##########
@@ -152,44 +179,87 @@ private TransferableBlock
buildJoinedDataBlock(TransferableBlock leftBlock)
if (leftBlock.isErrorBlock()) {
_upstreamErrorBlock = leftBlock;
return _upstreamErrorBlock;
- } else if (TransferableBlockUtils.isNoOpBlock(leftBlock) ||
TransferableBlockUtils.isEndOfStream(leftBlock)) {
+ }
+ if (leftBlock.isNoOpBlock() || (leftBlock.isSuccessfulEndOfStreamBlock()
&& !needUnmatchedRightRows())) {
return leftBlock;
}
+ // TODO: Moved to a different function.
+ if (leftBlock.isSuccessfulEndOfStreamBlock() && needUnmatchedRightRows()) {
+ // Return remaining non-matched rows for non-inner join.
+ List<Object[]> returnRows = new ArrayList<>();
+ for (Map.Entry<Key, List<Object[]>> entry :
_broadcastRightTable.entrySet()) {
+ Set<Integer> matchedIdx =
_matchedRightRows.getOrDefault(entry.getKey(), new HashSet<>());
+ List<Object[]> rightRows = entry.getValue();
+ if (rightRows.size() == matchedIdx.size()) {
+ continue;
+ }
+ for (int i = 0; i < rightRows.size(); i++) {
+ if (!matchedIdx.contains(i)) {
+ returnRows.add(joinRow(null, rightRows.get(i)));
+ }
+ }
+ }
+ _isTerminated = true;
+ return new TransferableBlock(returnRows, _resultSchema,
DataBlock.Type.ROW);
+ }
List<Object[]> rows = new ArrayList<>();
List<Object[]> container = leftBlock.isEndOfStreamBlock() ? new
ArrayList<>() : leftBlock.getContainer();
for (Object[] leftRow : container) {
+ Key key = new Key(_leftKeySelector.getKey(leftRow));
// NOTE: Empty key selector will always give same hash code.
- List<Object[]> hashCollection =
- _broadcastHashTable.getOrDefault(new
Key(_leftKeySelector.getKey(leftRow)), Collections.emptyList());
- // If it is a left join and right table is empty, we return left rows.
- if (hashCollection.isEmpty() && _joinType == JoinRelType.LEFT) {
- rows.add(joinRow(leftRow, null));
- } else {
- // If it is other type of join.
- for (Object[] rightRow : hashCollection) {
- // TODO: Optimize this to avoid unnecessary object copy.
- Object[] resultRow = joinRow(leftRow, rightRow);
- if (_joinClauseEvaluators.isEmpty() ||
_joinClauseEvaluators.stream().allMatch(evaluator ->
- (Boolean)
FunctionInvokeUtils.convert(evaluator.apply(resultRow),
DataSchema.ColumnDataType.BOOLEAN))) {
- rows.add(resultRow);
+ List<Object[]> matchedRightRows = _broadcastRightTable.getOrDefault(key,
null);
+ if (matchedRightRows == null) {
+ if (needUnmatchedLeftRows()) {
+ rows.add(joinRow(leftRow, null));
+ }
+ continue;
+ }
+ boolean hasMatchForLeftRow = false;
+ for (int i = 0; i < matchedRightRows.size(); i++) {
+ Object[] rightRow = matchedRightRows.get(i);
+ // TODO: Optimize this to avoid unnecessary object copy.
+ Object[] resultRow = joinRow(leftRow, rightRow);
+ if (_joinClauseEvaluators.isEmpty() ||
_joinClauseEvaluators.stream().allMatch(
+ evaluator -> (Boolean)
FunctionInvokeUtils.convert(evaluator.apply(resultRow),
+ DataSchema.ColumnDataType.BOOLEAN))) {
+ rows.add(resultRow);
+ hasMatchForLeftRow = true;
+ if (_matchedRightRows != null) {
+ HashSet<Integer> matchedRows =
_matchedRightRows.computeIfAbsent(key, k -> new HashSet<>());
+ matchedRows.add(i);
}
}
}
+ if (!hasMatchForLeftRow && needUnmatchedLeftRows()) {
+ rows.add(joinRow(leftRow, null));
+ }
}
return new TransferableBlock(rows, _resultSchema, DataBlock.Type.ROW);
}
- private Object[] joinRow(Object[] leftRow, @Nullable Object[] rightRow) {
+ private Object[] joinRow(@Nullable Object[] leftRow, @Nullable Object[]
rightRow) {
Object[] resultRow = new Object[_resultRowSize];
int idx = 0;
- for (Object obj : leftRow) {
- resultRow[idx++] = obj;
+ if (leftRow != null) {
+ for (Object obj : leftRow) {
+ resultRow[idx++] = obj;
+ }
}
+ // This is needed since left row can be null and we need to advance the
idx to the beginning of right row.
+ idx = _leftRowSize;
if (rightRow != null) {
Review Comment:
suggest making this into 3 utils. but can be done in follow up
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]