-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-25121][SQL] Supports multi-part relation names for join strategy hint resolution #27935
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
Changes from 8 commits
52089fc
ac19ea1
70c994a
fbbe824
2444a20
f07613d
6528dad
c517c1f
61e4a95
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -241,4 +241,52 @@ class ResolveHintsSuite extends AnalysisTest { | |
| Project(testRelation.output, testRelation), | ||
| caseSensitive = false) | ||
| } | ||
|
|
||
| test("Supports multi-part table names for broadcast hint resolution") { | ||
| // local temp table (single-part identifier case) | ||
| checkAnalysis( | ||
| UnresolvedHint("MAPJOIN", Seq("table", "table2"), | ||
| table("table").join(table("table2"))), | ||
|
Member
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. The following will be better because this is a - table("table").join(table("table2"))),
+ table("TaBlE").join(table("TaBlE2"))),
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. Yea, yes! |
||
| Join( | ||
| ResolvedHint(testRelation, HintInfo(strategy = Some(BROADCAST))), | ||
| ResolvedHint(testRelation2, HintInfo(strategy = Some(BROADCAST))), | ||
| Inner, | ||
| None, | ||
| JoinHint.NONE), | ||
| caseSensitive = false) | ||
|
|
||
| checkAnalysis( | ||
| UnresolvedHint("MAPJOIN", Seq("TaBlE", "table2"), | ||
| table("TaBlE").join(table("TaBlE2"))), | ||
| Join( | ||
| ResolvedHint(testRelation, HintInfo(strategy = Some(BROADCAST))), | ||
| testRelation2, | ||
| Inner, | ||
| None, | ||
| JoinHint.NONE), | ||
| caseSensitive = true) | ||
|
|
||
| // global temp table (multi-part identifier case) | ||
| checkAnalysis( | ||
| UnresolvedHint("MAPJOIN", Seq("GlOBal_TeMP.table4", "table5"), | ||
| table("global_temp", "table4").join(table("global_temp", "table5"))), | ||
| Join( | ||
| ResolvedHint(testRelation4, HintInfo(strategy = Some(BROADCAST))), | ||
| ResolvedHint(testRelation5, HintInfo(strategy = Some(BROADCAST))), | ||
| Inner, | ||
| None, | ||
| JoinHint.NONE), | ||
| caseSensitive = false) | ||
|
|
||
| checkAnalysis( | ||
| UnresolvedHint("MAPJOIN", Seq("global_temp.TaBlE4", "table5"), | ||
| table("global_temp", "TaBlE4").join(table("global_temp", "TaBlE5"))), | ||
| Join( | ||
| ResolvedHint(testRelation4, HintInfo(strategy = Some(BROADCAST))), | ||
| testRelation5, | ||
| Inner, | ||
| None, | ||
| JoinHint.NONE), | ||
| caseSensitive = true) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,10 +17,13 @@ | |
|
|
||
| package org.apache.spark.sql | ||
|
|
||
| import org.apache.spark.sql.catalyst.TableIdentifier | ||
| import org.apache.spark.sql.catalyst.plans.{Inner, InnerLike, LeftOuter, RightOuter} | ||
| import org.apache.spark.sql.catalyst.plans.logical.{Filter, Join, LogicalPlan, Project} | ||
| import org.apache.spark.sql.catalyst.plans.logical.{BROADCAST, Filter, HintInfo, Join, JoinHint, LogicalPlan, Project} | ||
| import org.apache.spark.sql.execution.FileSourceScanExec | ||
| import org.apache.spark.sql.execution.adaptive.AdaptiveSparkPlanHelper | ||
| import org.apache.spark.sql.execution.datasources.LogicalRelation | ||
| 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 | ||
|
|
@@ -322,4 +325,99 @@ class DataFrameJoinSuite extends QueryTest | |
| } | ||
| } | ||
| } | ||
|
|
||
| test("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) | ||
|
Member
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.
- .queryExecution.executedPlan
- assert(plan.collect { case p: BroadcastHashJoinExec => p }.isEmpty)
+ checkIfHintNotApplied(plan)
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. Ah... I forgot to remove the old tests... I can remove it. Thanks! |
||
|
|
||
| def checkIfHintApplied(df: DataFrame): Unit = { | ||
| val sparkPlan = df.queryExecution.executedPlan | ||
| val broadcastHashJoins = sparkPlan.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(df: DataFrame): Unit = { | ||
| val sparkPlan = df.queryExecution.executedPlan | ||
| val broadcastHashJoins = sparkPlan.collect { case p: BroadcastHashJoinExec => p } | ||
| assert(broadcastHashJoins.isEmpty) | ||
| } | ||
|
|
||
| def sqlTemplate(tableName: String, hintTableName: String): DataFrame = { | ||
| sql(s"SELECT /*+ BROADCASTJOIN($hintTableName) */ * " + | ||
| s"FROM $tableName, $dbName.$table2Name " + | ||
| s"WHERE $tableName.id = $table2Name.id") | ||
| } | ||
|
|
||
| def dfTemplate(tableName: String, hintTableName: String): DataFrame = { | ||
| spark.table(tableName).join(spark.table(s"$dbName.$table2Name"), "id") | ||
| .hint("broadcast", hintTableName) | ||
| } | ||
|
|
||
| sql(s"USE $dbName") | ||
|
|
||
| checkIfHintApplied(sqlTemplate(table1Name, table1Name)) | ||
| checkIfHintApplied(sqlTemplate(s"$dbName.$table1Name", s"$dbName.$table1Name")) | ||
| checkIfHintApplied(sqlTemplate(s"$dbName.$table1Name", table1Name)) | ||
| checkIfHintNotApplied(sqlTemplate(table1Name, s"$dbName.$table1Name")) | ||
| checkIfHintNotApplied(sqlTemplate(s"$dbName.$table1Name", s"$dbName.$table1Name.id")) | ||
|
Member
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. Is
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. ok, I will modify |
||
|
|
||
| checkIfHintApplied(dfTemplate(table1Name, table1Name)) | ||
| checkIfHintApplied(dfTemplate(s"$dbName.$table1Name", s"$dbName.$table1Name")) | ||
| checkIfHintApplied(dfTemplate(s"$dbName.$table1Name", table1Name)) | ||
| checkIfHintApplied(dfTemplate(table1Name, s"$dbName.$table1Name")) | ||
| checkIfHintNotApplied(dfTemplate(s"$dbName.$table1Name", s"$dbName.$table1Name.id")) | ||
|
|
||
| withTempView("tv") { | ||
| sql(s"CREATE TEMPORARY VIEW tv AS SELECT * FROM $dbName.$table1Name") | ||
| checkIfHintApplied(sqlTemplate("tv", "tv")) | ||
| checkIfHintNotApplied(sqlTemplate("tv", "default.tv")) | ||
|
Member
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. This might be misleading. Technically, this is the same with scala> sql("select * from default.tv").show
org.apache.spark.sql.AnalysisException: Table or view not found: default.tv; line 1 pos 14;
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. Yea, but I think a query with a hint having a non-existent relation identifier should work?;
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. I will modify the tests a little. |
||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| test("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(HintInfo(Some(BROADCAST))), | ||
| Some(HintInfo(Some(BROADCAST))))) => | ||
| case _ => fail("broadcast hint not found in both tables") | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
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.