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
[SPARK-25121][SQL] Supports multi-part table names for broadcast hint resolution #22198
Closed
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
24de799
Fix
maropu a6e4e40
Fix
maropu f021770
Add test cases
dongjoon-hyun d434ba7
Fix
maropu c138b81
Fix
maropu 6a202f2
Fix
maropu 545148b
fix
dongjoon-hyun bc29a11
Fix
maropu 59e60d4
Fix
maropu 5b2b272
Fix
maropu b6b9f65
Fix
maropu File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 |
---|---|---|
|
@@ -17,8 +17,11 @@ | |
|
||
package org.apache.spark.sql | ||
|
||
import org.apache.spark.sql.catalyst.TableIdentifier | ||
import org.apache.spark.sql.catalyst.plans.{Inner, LeftOuter, RightOuter} | ||
import org.apache.spark.sql.catalyst.plans.logical.Join | ||
import org.apache.spark.sql.catalyst.plans.logical.{Join, JoinHint} | ||
import org.apache.spark.sql.execution.FileSourceScanExec | ||
import org.apache.spark.sql.execution.exchange.BroadcastExchangeExec | ||
import org.apache.spark.sql.execution.joins.BroadcastHashJoinExec | ||
import org.apache.spark.sql.functions._ | ||
import org.apache.spark.sql.internal.SQLConf | ||
|
@@ -191,6 +194,83 @@ class DataFrameJoinSuite extends QueryTest with SharedSQLContext { | |
assert(plan2.collect { case p: BroadcastHashJoinExec => p }.size == 1) | ||
} | ||
|
||
test("SPARK-25121 supports multi-part names for broadcast hint resolution") { | ||
val (table1Name, table2Name) = ("t1", "t2") | ||
|
||
withTempDatabase { dbName => | ||
withTable(table1Name, table2Name) { | ||
withSQLConf(SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1") { | ||
spark.range(50).write.saveAsTable(s"$dbName.$table1Name") | ||
spark.range(100).write.saveAsTable(s"$dbName.$table2Name") | ||
|
||
// First, makes sure a join is not broadcastable | ||
val plan = sql(s"SELECT * FROM $dbName.$table1Name, $dbName.$table2Name " + | ||
s"WHERE $table1Name.id = $table2Name.id") | ||
.queryExecution.executedPlan | ||
assert(plan.collect { case p: BroadcastHashJoinExec => p }.isEmpty) | ||
|
||
def checkIfHintApplied(tableName: String, hintTableName: String): Unit = { | ||
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.
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. yea, I'll fix. |
||
val p = sql(s"SELECT /*+ BROADCASTJOIN($hintTableName) */ * " + | ||
s"FROM $tableName, $dbName.$table2Name " + | ||
s"WHERE $tableName.id = $table2Name.id") | ||
.queryExecution.executedPlan | ||
val broadcastHashJoins = p.collect { case p: BroadcastHashJoinExec => p } | ||
assert(broadcastHashJoins.size == 1) | ||
val broadcastExchanges = broadcastHashJoins.head.collect { | ||
case p: BroadcastExchangeExec => p | ||
} | ||
assert(broadcastExchanges.size == 1) | ||
val tables = broadcastExchanges.head.collect { | ||
case FileSourceScanExec(_, _, _, _, _, _, Some(tableIdent)) => tableIdent | ||
} | ||
assert(tables.size == 1) | ||
assert(tables.head === TableIdentifier(table1Name, Some(dbName))) | ||
} | ||
|
||
def checkIfHintNotApplied(tableName: String, hintTableName: String): Unit = { | ||
val p = sql(s"SELECT /*+ BROADCASTJOIN($hintTableName) */ * " + | ||
s"FROM $tableName, $dbName.$table2Name " + | ||
s"WHERE $tableName.id = $table2Name.id") | ||
.queryExecution.executedPlan | ||
val broadcastHashJoins = p.collect { case p: BroadcastHashJoinExec => p } | ||
assert(broadcastHashJoins.isEmpty) | ||
} | ||
|
||
sql(s"USE $dbName") | ||
checkIfHintApplied(table1Name, table1Name) | ||
checkIfHintApplied(s"$dbName.$table1Name", s"$dbName.$table1Name") | ||
checkIfHintApplied(s"$dbName.$table1Name", table1Name) | ||
checkIfHintNotApplied(table1Name, s"$dbName.$table1Name") | ||
checkIfHintNotApplied(s"$dbName.$table1Name", s"$dbName.$table1Name.id") | ||
} | ||
} | ||
} | ||
} | ||
|
||
test("SPARK-25121 the same table name exists in two databases for broadcast hint resolution") { | ||
val (db1Name, db2Name) = ("db1", "db2") | ||
|
||
withDatabase(db1Name, db2Name) { | ||
withTable("t") { | ||
withSQLConf(SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1") { | ||
sql(s"CREATE DATABASE $db1Name") | ||
sql(s"CREATE DATABASE $db2Name") | ||
spark.range(1).write.saveAsTable(s"$db1Name.t") | ||
spark.range(1).write.saveAsTable(s"$db2Name.t") | ||
|
||
// Checks if a broadcast hint applied in both sides | ||
val statement = s"SELECT /*+ BROADCASTJOIN(t) */ * FROM $db1Name.t, $db2Name.t " + | ||
s"WHERE $db1Name.t.id = $db2Name.t.id" | ||
sql(statement).queryExecution.optimizedPlan match { | ||
case Join(_, _, _, _, JoinHint(Some(leftHint), Some(rightHint))) => | ||
assert(leftHint.broadcast && rightHint.broadcast) | ||
case _ => fail("broadcast hint not found in both tables") | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
test("join - outer join conversion") { | ||
val df = Seq((1, 2, "1"), (3, 4, "3")).toDF("int", "int2", "str").as("a") | ||
val df2 = Seq((1, 3, "1"), (5, 6, "5")).toDF("int", "int2", "str").as("b") | ||
|
This file contains 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 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
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.
Accordingly, we can use String instead of SessionCatalog.
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 think we can't use
String
there becausecurrentDatabase
might be updatable by others?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.
We can instead use
getCurrentDatabase: () => String
?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.
Ya. Right, please ignore this. We need
catalog
to lookupglobal_temp
, too.