maropu commented on a change in pull request #27935: [SPARK-25121][SQL] 
Supports multi-part table names for broadcast hint resolution
URL: https://github.com/apache/spark/pull/27935#discussion_r395357554
 
 

 ##########
 File path: 
sql/core/src/test/scala/org/apache/spark/sql/DataFrameJoinSuite.scala
 ##########
 @@ -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)
+
+          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"))
+
+          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"))
 
 Review comment:
   Yea, but I think a query with a hint having a non-existent relation 
identifier should work?;
   ```
   
   scala> sql("create table t1 (id int)")
   scala> sql("create table t2 (id int)")
   scala> sql("create temporary view tv as select * from t1")
   scala> sql("SELECT /*+ BROADCASTJOIN(default.non_exist) */ * FROM tv, t2 
WHERE tv.id = t2.id").explain(true)
   20/03/20 07:34:02 WARN HintErrorLogger: Count not find relation 
'default.non_exist' specified in hint 'BROADCASTJOIN(default.non_exist)'.
   == Parsed Logical Plan ==
   'UnresolvedHint BROADCASTJOIN, ['default.non_exist]
   +- 'Project [*]
      +- 'Filter ('tv.id = 't2.id)
         +- 'Join Inner
            :- 'UnresolvedRelation [tv]
            +- 'UnresolvedRelation [t2]
   
   == Analyzed Logical Plan ==
   id: int, id: int
   Project [id#0, id#1]
   +- Filter (id#0 = id#1)
      +- Join Inner
         :- SubqueryAlias tv
         :  +- Project [id#0]
         :     +- SubqueryAlias spark_catalog.default.t1
         :        +- Relation[id#0] parquet
         +- SubqueryAlias spark_catalog.default.t2
            +- Relation[id#1] parquet
   ```

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to