-
Notifications
You must be signed in to change notification settings - Fork 29.2k
[SPARK-44060][SQL] Code-gen for build side outer shuffled hash join #41614
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
szehon-ho
wants to merge
8
commits into
apache:master
from
szehon-ho:same_side_outer_join_codegen_master
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c090dbf
[SPARK-44060][SQL] Code-gen for build side outer shuffled hash join
szehon-ho 769266b
Small fixups
szehon-ho efbae53
Add explicit config in WholeStageCodegenSuite new test
szehon-ho 2162131
Test both codegen and non-codegen in JoinSuite
szehon-ho 221c1a3
Review comments
szehon-ho f0eeca2
Fix error
szehon-ho 0adbfc6
remove unnecessary change
szehon-ho f9dccff
consumeFullOuterJoinRow => consumeOuterJoinRow
szehon-ho File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -340,8 +340,10 @@ case class ShuffledHashJoinExec( | |
|
|
||
| override def supportCodegen: Boolean = joinType match { | ||
| case FullOuter => conf.getConf(SQLConf.ENABLE_FULL_OUTER_SHUFFLED_HASH_JOIN_CODEGEN) | ||
| case LeftOuter if buildSide == BuildLeft => false | ||
| case RightOuter if buildSide == BuildRight => false | ||
| case LeftOuter if buildSide == BuildLeft => | ||
| conf.getConf(SQLConf.ENABLE_BUILD_SIDE_OUTER_SHUFFLED_HASH_JOIN_CODEGEN) | ||
| case RightOuter if buildSide == BuildRight => | ||
| conf.getConf(SQLConf.ENABLE_BUILD_SIDE_OUTER_SHUFFLED_HASH_JOIN_CODEGEN) | ||
| case _ => true | ||
| } | ||
|
|
||
|
|
@@ -362,9 +364,15 @@ case class ShuffledHashJoinExec( | |
| } | ||
|
|
||
| override def doProduce(ctx: CodegenContext): String = { | ||
| // Specialize `doProduce` code for full outer join, because full outer join needs to | ||
| // iterate streamed and build side separately. | ||
| if (joinType != FullOuter) { | ||
| // Specialize `doProduce` code for full outer join and build-side outer join, | ||
| // because we need to iterate streamed and build side separately. | ||
| val specializedProduce = joinType match { | ||
| case FullOuter => true | ||
| case LeftOuter if buildSide == BuildLeft => true | ||
| case RightOuter if buildSide == BuildRight => true | ||
| case _ => false | ||
| } | ||
| if (!specializedProduce) { | ||
| return super.doProduce(ctx) | ||
| } | ||
|
|
||
|
|
@@ -407,21 +415,24 @@ case class ShuffledHashJoinExec( | |
| case BuildLeft => buildResultVars ++ streamedResultVars | ||
| case BuildRight => streamedResultVars ++ buildResultVars | ||
| } | ||
| val consumeFullOuterJoinRow = ctx.freshName("consumeFullOuterJoinRow") | ||
| ctx.addNewFunction(consumeFullOuterJoinRow, | ||
| val consumeOuterJoinRow = ctx.freshName("consumeOuterJoinRow") | ||
| ctx.addNewFunction(consumeOuterJoinRow, | ||
| s""" | ||
| |private void $consumeFullOuterJoinRow() throws java.io.IOException { | ||
| |private void $consumeOuterJoinRow() throws java.io.IOException { | ||
| | ${metricTerm(ctx, "numOutputRows")}.add(1); | ||
| | ${consume(ctx, resultVars)} | ||
| |} | ||
| """.stripMargin) | ||
|
|
||
| val joinWithUniqueKey = codegenFullOuterJoinWithUniqueKey( | ||
| val isFullOuterJoin = joinType == FullOuter | ||
| val joinWithUniqueKey = codegenBuildSideOrFullOuterJoinWithUniqueKey( | ||
| ctx, (streamedRow, buildRow), (streamedInput, buildInput), streamedKeyEv, streamedKeyAnyNull, | ||
| streamedKeyExprCode.value, relationTerm, conditionCheck, consumeFullOuterJoinRow) | ||
| val joinWithNonUniqueKey = codegenFullOuterJoinWithNonUniqueKey( | ||
| streamedKeyExprCode.value, relationTerm, conditionCheck, consumeOuterJoinRow, | ||
| isFullOuterJoin) | ||
| val joinWithNonUniqueKey = codegenBuildSideOrFullOuterJoinNonUniqueKey( | ||
| ctx, (streamedRow, buildRow), (streamedInput, buildInput), streamedKeyEv, streamedKeyAnyNull, | ||
| streamedKeyExprCode.value, relationTerm, conditionCheck, consumeFullOuterJoinRow) | ||
| streamedKeyExprCode.value, relationTerm, conditionCheck, consumeOuterJoinRow, | ||
| isFullOuterJoin) | ||
|
|
||
| s""" | ||
| |if ($keyIsUnique) { | ||
|
|
@@ -433,10 +444,10 @@ case class ShuffledHashJoinExec( | |
| } | ||
|
|
||
| /** | ||
| * Generates the code for full outer join with unique join keys. | ||
| * This is code-gen version of `fullOuterJoinWithUniqueKey()`. | ||
| * Generates the code for build-side or full outer join with unique join keys. | ||
| * This is code-gen version of `buildSideOrFullOuterJoinUniqueKey()`. | ||
| */ | ||
| private def codegenFullOuterJoinWithUniqueKey( | ||
| private def codegenBuildSideOrFullOuterJoinWithUniqueKey( | ||
| ctx: CodegenContext, | ||
| rows: (String, String), | ||
| inputs: (String, String), | ||
|
|
@@ -445,7 +456,8 @@ case class ShuffledHashJoinExec( | |
| streamedKeyValue: ExprValue, | ||
| relationTerm: String, | ||
| conditionCheck: String, | ||
| consumeFullOuterJoinRow: String): String = { | ||
| consumeOuterJoinRow: String, | ||
| isFullOuterJoin: Boolean): String = { | ||
| // Inline mutable state since not many join operations in a task | ||
| val matchedKeySetClsName = classOf[BitSet].getName | ||
| val matchedKeySet = ctx.addMutableState(matchedKeySetClsName, "matchedKeySet", | ||
|
|
@@ -484,7 +496,10 @@ case class ShuffledHashJoinExec( | |
| | } | ||
| | } | ||
| | | ||
| | $consumeFullOuterJoinRow(); | ||
| | if ($foundMatch || $isFullOuterJoin) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: we can statically remove or simply the condition:
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks , let me make some follow ups a bit later |
||
| | $consumeOuterJoinRow(); | ||
| | } | ||
| | | ||
| | if (shouldStop()) return; | ||
| |} | ||
| """.stripMargin | ||
|
|
@@ -500,7 +515,7 @@ case class ShuffledHashJoinExec( | |
| | // check if key index is not in matched keys set | ||
| | if (!$matchedKeySet.get($rowWithIndex.getKeyIndex())) { | ||
| | $buildRow = $rowWithIndex.getValue(); | ||
| | $consumeFullOuterJoinRow(); | ||
| | $consumeOuterJoinRow(); | ||
| | } | ||
| | | ||
| | if (shouldStop()) return; | ||
|
|
@@ -514,10 +529,10 @@ case class ShuffledHashJoinExec( | |
| } | ||
|
|
||
| /** | ||
| * Generates the code for full outer join with non-unique join keys. | ||
| * This is code-gen version of `fullOuterJoinWithNonUniqueKey()`. | ||
| * Generates the code for build-side or full outer join with non-unique join keys. | ||
| * This is code-gen version of `buildSideOrFullOuterJoinNonUniqueKey()`. | ||
| */ | ||
| private def codegenFullOuterJoinWithNonUniqueKey( | ||
| private def codegenBuildSideOrFullOuterJoinNonUniqueKey( | ||
| ctx: CodegenContext, | ||
| rows: (String, String), | ||
| inputs: (String, String), | ||
|
|
@@ -526,7 +541,8 @@ case class ShuffledHashJoinExec( | |
| streamedKeyValue: ExprValue, | ||
| relationTerm: String, | ||
| conditionCheck: String, | ||
| consumeFullOuterJoinRow: String): String = { | ||
| consumeOuterJoinRow: String, | ||
| isFullOuterJoin: Boolean): String = { | ||
| // Inline mutable state since not many join operations in a task | ||
| val matchedRowSetClsName = classOf[OpenHashSet[_]].getName | ||
| val matchedRowSet = ctx.addMutableState(matchedRowSetClsName, "matchedRowSet", | ||
|
|
@@ -572,13 +588,15 @@ case class ShuffledHashJoinExec( | |
| | // set row index in matched row set | ||
| | $matchedRowSet.add($rowIndex); | ||
| | $foundMatch = true; | ||
| | $consumeFullOuterJoinRow(); | ||
| | $consumeOuterJoinRow(); | ||
| | } | ||
| | } | ||
| | | ||
| | if (!$foundMatch) { | ||
| | $buildRow = null; | ||
| | $consumeFullOuterJoinRow(); | ||
| | if ($isFullOuterJoin) { | ||
| | $consumeOuterJoinRow(); | ||
| | } | ||
| | } | ||
| | | ||
| | if (shouldStop()) return; | ||
|
|
@@ -603,7 +621,7 @@ case class ShuffledHashJoinExec( | |
| | // check if row index is not in matched row set | ||
| | if (!$matchedRowSet.contains($rowIndex)) { | ||
| | $buildRow = $rowWithIndex.getValue(); | ||
| | $consumeFullOuterJoinRow(); | ||
| | $consumeOuterJoinRow(); | ||
| | } | ||
| | | ||
| | if (shouldStop()) return; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wasn't entirely sure if this is the right approach, but the existing flag was a bit explicit in FULL OUTER JOIN, and in the discussion like #41398 (comment) build-side outer join is taken to mean, if only one side is OUTER.