-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-16475][SQL] Broadcast hint for SQL Queries #16925
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 all commits
539782d
318bc03
c702e3e
a095df3
617f8a2
51a73d5
0d42978
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 |
|---|---|---|
|
|
@@ -365,7 +365,7 @@ querySpecification | |
| (RECORDREADER recordReader=STRING)? | ||
| fromClause? | ||
| (WHERE where=booleanExpression)?) | ||
| | ((kind=SELECT setQuantifier? namedExpressionSeq fromClause? | ||
| | ((kind=SELECT hint? setQuantifier? namedExpressionSeq fromClause? | ||
| | fromClause (kind=SELECT setQuantifier? namedExpressionSeq)?) | ||
| lateralView* | ||
| (WHERE where=booleanExpression)? | ||
|
|
@@ -374,6 +374,16 @@ querySpecification | |
| windows?) | ||
| ; | ||
|
|
||
| hint | ||
| : '/*+' hintStatement '*/' | ||
| ; | ||
|
|
||
| hintStatement | ||
| : hintName=identifier | ||
| | hintName=identifier '(' parameters+=identifier parameters+=identifier ')' | ||
|
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 rule doesn't support like Is it intentional?
Contributor
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. What does it support? ? I think ti's a bit weird to support space as the delimiter.
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. But we support
Contributor
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. do we? we should disallow it. want to submit a pr?
Contributor
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. btw this was written by @dongjoon-hyun. I didn't change it :)
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. ok. let me prepare a tiny one. |
||
| | hintName=identifier '(' parameters+=identifier (',' parameters+=identifier)* ')' | ||
| ; | ||
|
|
||
| fromClause | ||
| : FROM relation (',' relation)* lateralView* | ||
| ; | ||
|
|
@@ -1002,8 +1012,12 @@ SIMPLE_COMMENT | |
| : '--' ~[\r\n]* '\r'? '\n'? -> channel(HIDDEN) | ||
| ; | ||
|
|
||
| BRACKETED_EMPTY_COMMENT | ||
| : '/**/' -> channel(HIDDEN) | ||
| ; | ||
|
|
||
| BRACKETED_COMMENT | ||
| : '/*' .*? '*/' -> channel(HIDDEN) | ||
| : '/*' ~[+] .*? '*/' -> channel(HIDDEN) | ||
|
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. what does
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. It means
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.
|
||
| ; | ||
|
|
||
| WS | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.sql.catalyst.analysis | ||
|
|
||
| import org.apache.spark.sql.catalyst.CatalystConf | ||
| import org.apache.spark.sql.catalyst.plans.logical._ | ||
| import org.apache.spark.sql.catalyst.rules.Rule | ||
| import org.apache.spark.sql.catalyst.trees.CurrentOrigin | ||
|
|
||
|
|
||
| /** | ||
| * Collection of rules related to hints. The only hint currently available is broadcast join hint. | ||
| * | ||
| * Note that this is separatedly into two rules because in the future we might introduce new hint | ||
| * rules that have different ordering requirements from broadcast. | ||
| */ | ||
| object SubstituteHints { | ||
|
|
||
| /** | ||
| * Substitute Hints. | ||
| * | ||
| * The only hint currently available is broadcast join hint. | ||
| * | ||
| * For broadcast hint, we accept "BROADCAST", "BROADCASTJOIN", and "MAPJOIN", and a sequence of | ||
| * relation aliases can be specified in the hint. A broadcast hint plan node will be inserted | ||
| * on top of any relation (that is not aliased differently), subquery, or common table expression | ||
| * that match the specified name. | ||
| * | ||
| * The hint resolution works by recursively traversing down the query plan to find a relation or | ||
| * subquery that matches one of the specified broadcast aliases. The traversal does not go past | ||
| * beyond any existing broadcast hints, subquery aliases. | ||
| * | ||
| * This rule must happen before common table expressions. | ||
| */ | ||
| class SubstituteBroadcastHints(conf: CatalystConf) extends Rule[LogicalPlan] { | ||
| private val BROADCAST_HINT_NAMES = Set("BROADCAST", "BROADCASTJOIN", "MAPJOIN") | ||
|
|
||
| def resolver: Resolver = conf.resolver | ||
|
|
||
| private def applyBroadcastHint(plan: LogicalPlan, toBroadcast: Set[String]): LogicalPlan = { | ||
| // Whether to continue recursing down the tree | ||
| var recurse = true | ||
|
|
||
| val newNode = CurrentOrigin.withOrigin(plan.origin) { | ||
| plan match { | ||
| case r: UnresolvedRelation => | ||
| val alias = r.alias.getOrElse(r.tableIdentifier.table) | ||
| if (toBroadcast.exists(resolver(_, alias))) BroadcastHint(plan) else plan | ||
| case r: SubqueryAlias => | ||
| if (toBroadcast.exists(resolver(_, r.alias))) { | ||
| BroadcastHint(plan) | ||
| } else { | ||
| // Don't recurse down subquery aliases if there are no match. | ||
| recurse = false | ||
| plan | ||
| } | ||
| case _: BroadcastHint => | ||
| // Found a broadcast hint; don't change the plan but also don't recurse down. | ||
| recurse = false | ||
| plan | ||
| case _ => | ||
| plan | ||
| } | ||
| } | ||
|
|
||
| if ((plan fastEquals newNode) && recurse) { | ||
| newNode.mapChildren(child => applyBroadcastHint(child, toBroadcast)) | ||
|
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. in the case of self-join, we may broadcast both side, is it expected?
Contributor
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. That's fine. Both being broadcastable doesn't mean we broadcast both. |
||
| } else { | ||
| newNode | ||
| } | ||
| } | ||
|
|
||
| def apply(plan: LogicalPlan): LogicalPlan = plan transformUp { | ||
| case h: Hint if BROADCAST_HINT_NAMES.contains(h.name.toUpperCase) => | ||
| applyBroadcastHint(h.child, h.parameters.toSet) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Removes all the hints, used to remove invalid hints provided by the user. | ||
| * This must be executed after all the other hint rules are executed. | ||
| */ | ||
| object RemoveAllHints extends Rule[LogicalPlan] { | ||
| def apply(plan: LogicalPlan): LogicalPlan = plan transformUp { | ||
| case h: Hint => h.child | ||
| } | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.sql.catalyst.analysis | ||
|
|
||
| import org.apache.spark.sql.catalyst.TableIdentifier | ||
| import org.apache.spark.sql.catalyst.dsl.expressions._ | ||
| import org.apache.spark.sql.catalyst.dsl.plans._ | ||
| import org.apache.spark.sql.catalyst.parser.CatalystSqlParser | ||
| import org.apache.spark.sql.catalyst.plans.Inner | ||
| import org.apache.spark.sql.catalyst.plans.logical._ | ||
|
|
||
| class SubstituteHintsSuite extends AnalysisTest { | ||
| import org.apache.spark.sql.catalyst.analysis.TestRelations._ | ||
|
|
||
| test("invalid hints should be ignored") { | ||
| checkAnalysis( | ||
| Hint("some_random_hint_that_does_not_exist", Seq("TaBlE"), table("TaBlE")), | ||
| testRelation, | ||
| caseSensitive = false) | ||
| } | ||
|
|
||
| test("case-sensitive or insensitive parameters") { | ||
| checkAnalysis( | ||
| Hint("MAPJOIN", Seq("TaBlE"), table("TaBlE")), | ||
| BroadcastHint(testRelation), | ||
| caseSensitive = false) | ||
|
|
||
| checkAnalysis( | ||
| Hint("MAPJOIN", Seq("table"), table("TaBlE")), | ||
| BroadcastHint(testRelation), | ||
| caseSensitive = false) | ||
|
|
||
| checkAnalysis( | ||
| Hint("MAPJOIN", Seq("TaBlE"), table("TaBlE")), | ||
| BroadcastHint(testRelation), | ||
| caseSensitive = true) | ||
|
|
||
| checkAnalysis( | ||
| Hint("MAPJOIN", Seq("table"), table("TaBlE")), | ||
| testRelation, | ||
| caseSensitive = true) | ||
| } | ||
|
|
||
| test("multiple broadcast hint aliases") { | ||
| checkAnalysis( | ||
| Hint("MAPJOIN", Seq("table", "table2"), table("table").join(table("table2"))), | ||
| Join(BroadcastHint(testRelation), BroadcastHint(testRelation2), Inner, None), | ||
| caseSensitive = false) | ||
| } | ||
|
|
||
| test("do not traverse past existing broadcast hints") { | ||
| checkAnalysis( | ||
| Hint("MAPJOIN", Seq("table"), BroadcastHint(table("table").where('a > 1))), | ||
| BroadcastHint(testRelation.where('a > 1)).analyze, | ||
| caseSensitive = false) | ||
| } | ||
|
|
||
| test("should work for subqueries") { | ||
| checkAnalysis( | ||
| Hint("MAPJOIN", Seq("tableAlias"), table("table").as("tableAlias")), | ||
| BroadcastHint(testRelation), | ||
| caseSensitive = false) | ||
|
|
||
| checkAnalysis( | ||
| Hint("MAPJOIN", Seq("tableAlias"), table("table").subquery('tableAlias)), | ||
| BroadcastHint(testRelation), | ||
| caseSensitive = false) | ||
|
|
||
| // Negative case: if the alias doesn't match, don't match the original table name. | ||
| checkAnalysis( | ||
| Hint("MAPJOIN", Seq("table"), table("table").as("tableAlias")), | ||
| testRelation, | ||
| caseSensitive = false) | ||
| } | ||
|
|
||
| test("do not traverse past subquery alias") { | ||
| checkAnalysis( | ||
| Hint("MAPJOIN", Seq("table"), table("table").where('a > 1).subquery('tableAlias)), | ||
| testRelation.where('a > 1).analyze, | ||
| caseSensitive = false) | ||
| } | ||
|
|
||
| test("should work for CTE") { | ||
| checkAnalysis( | ||
| CatalystSqlParser.parsePlan( | ||
| """ | ||
| |WITH ctetable AS (SELECT * FROM table WHERE a > 1) | ||
| |SELECT /*+ BROADCAST(ctetable) */ * FROM ctetable | ||
| """.stripMargin | ||
| ), | ||
| BroadcastHint(testRelation.where('a > 1).select('a)).select('a).analyze, | ||
| caseSensitive = false) | ||
| } | ||
|
|
||
| test("should not traverse down CTE") { | ||
| checkAnalysis( | ||
| CatalystSqlParser.parsePlan( | ||
| """ | ||
| |WITH ctetable AS (SELECT * FROM table WHERE a > 1) | ||
| |SELECT /*+ BROADCAST(table) */ * FROM ctetable | ||
| """.stripMargin | ||
| ), | ||
| testRelation.where('a > 1).select('a).select('a).analyze, | ||
| caseSensitive = false) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -493,4 +493,46 @@ class PlanParserSuite extends PlanTest { | |
| assertEqual("select a, b from db.c where x !> 1", | ||
| table("db", "c").where('x <= 1).select('a, 'b)) | ||
| } | ||
|
|
||
| test("select hint syntax") { | ||
| // Hive compatibility: Missing parameter raises ParseException. | ||
| val m = intercept[ParseException] { | ||
| parsePlan("SELECT /*+ HINT() */ * FROM t") | ||
| }.getMessage | ||
| assert(m.contains("no viable alternative at input")) | ||
|
|
||
| // Hive compatibility: No database. | ||
| val m2 = intercept[ParseException] { | ||
| parsePlan("SELECT /*+ MAPJOIN(default.t) */ * from default.t") | ||
| }.getMessage | ||
| assert(m2.contains("no viable alternative at input")) | ||
|
|
||
| comparePlans( | ||
| parsePlan("SELECT /*+ HINT */ * FROM t"), | ||
| Hint("HINT", Seq.empty, table("t").select(star()))) | ||
|
|
||
| comparePlans( | ||
| parsePlan("SELECT /*+ BROADCASTJOIN(u) */ * FROM t"), | ||
| Hint("BROADCASTJOIN", Seq("u"), table("t").select(star()))) | ||
|
|
||
| comparePlans( | ||
| parsePlan("SELECT /*+ MAPJOIN(u) */ * FROM t"), | ||
| Hint("MAPJOIN", Seq("u"), table("t").select(star()))) | ||
|
|
||
| comparePlans( | ||
| parsePlan("SELECT /*+ STREAMTABLE(a,b,c) */ * FROM t"), | ||
| Hint("STREAMTABLE", Seq("a", "b", "c"), table("t").select(star()))) | ||
|
|
||
| comparePlans( | ||
| parsePlan("SELECT /*+ INDEX(t emp_job_ix) */ * FROM t"), | ||
|
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. what if users writing
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. Yes. Both are considered equally now.
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. @rxin Looks like we already support space as delimiter? |
||
| Hint("INDEX", Seq("t", "emp_job_ix"), table("t").select(star()))) | ||
|
|
||
| comparePlans( | ||
| parsePlan("SELECT /*+ MAPJOIN(`default.t`) */ * from `default.t`"), | ||
| Hint("MAPJOIN", Seq("default.t"), table("default.t").select(star()))) | ||
|
|
||
| comparePlans( | ||
| parsePlan("SELECT /*+ MAPJOIN(t) */ a from t where true group by a order by a"), | ||
| Hint("MAPJOIN", Seq("t"), table("t").where(Literal(true)).groupBy('a)('a)).orderBy('a.asc)) | ||
| } | ||
| } | ||
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.
why do we need this?
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.
The purpose of this is to allow
SqlBase.g4accept more general rule for the future.So, possibly, it's for reducing the change in
SqlBase.g4in order to add new hint rules.