-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-16475][SQL] Broadcast Hint for SQL Queries #14132
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
9bd25da
eae3931
60a304e
5d81510
92de211
63b629c
3b25505
f029dac
4fce2ca
16ad3a1
8a407aa
6b20a44
0f3f53e
11e5f76
7ab0c19
9cf4d65
47c3733
61f7776
fccf9da
b0b9f61
39a3277
1b38e55
9bf6fa7
c8436fd
302b9d4
e7c7943
e6b2409
9f0b104
4023d97
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 |
|---|---|---|
|
|
@@ -338,7 +338,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)? | ||
|
|
@@ -347,6 +347,16 @@ querySpecification | |
| windows?) | ||
| ; | ||
|
|
||
| hint | ||
| : '/*+' hintStatement '*/' | ||
|
||
| ; | ||
|
|
||
| hintStatement | ||
| : hintName=identifier | ||
| | hintName=identifier '(' parameters+=identifier parameters+=identifier ')' | ||
|
||
| | hintName=identifier '(' parameters+=identifier (',' parameters+=identifier)* ')' | ||
| ; | ||
|
|
||
| fromClause | ||
| : FROM relation (',' relation)* lateralView* | ||
| ; | ||
|
|
@@ -945,8 +955,12 @@ SIMPLE_COMMENT | |
| : '--' ~[\r\n]* '\r'? '\n'? -> channel(HIDDEN) | ||
| ; | ||
|
|
||
| BRACKETED_EMPTY_COMMENT | ||
|
||
| : '/**/' -> channel(HIDDEN) | ||
| ; | ||
|
|
||
| BRACKETED_COMMENT | ||
| : '/*' .*? '*/' -> channel(HIDDEN) | ||
| : '/*' ~[+] .*? '*/' -> channel(HIDDEN) | ||
|
||
| ; | ||
|
|
||
| WS | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -84,7 +84,8 @@ class Analyzer( | |
| Batch("Substitution", fixedPoint, | ||
| CTESubstitution, | ||
| WindowsSubstitution, | ||
| EliminateUnions), | ||
| EliminateUnions, | ||
| SubstituteHints), | ||
| Batch("Resolution", fixedPoint, | ||
| ResolveRelations :: | ||
| ResolveReferences :: | ||
|
|
@@ -1786,6 +1787,63 @@ class Analyzer( | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Substitute Hints. | ||
| * - BROADCAST/BROADCASTJOIN/MAPJOIN match the closest table with the given name parameters. | ||
| * | ||
| * This rule substitutes `UnresolvedRelation`s in `Substitute` batch before `ResolveRelations` | ||
| * rule is applied. Here are two reasons. | ||
| * - To support `MetastoreRelation` in Hive module. | ||
| * - To reduce the effect of `Hint` on the other rules. | ||
| * | ||
| * After this rule, it is guaranteed that there exists no unknown `Hint` in the plan. | ||
| * All new `Hint`s should be transformed into concrete Hint classes `BroadcastHint` here. | ||
| */ | ||
| object SubstituteHints extends Rule[LogicalPlan] { | ||
|
||
| val BROADCAST_HINT_NAMES = Set("BROADCAST", "BROADCASTJOIN", "MAPJOIN") | ||
|
|
||
| import scala.collection.mutable.Set | ||
| private def appendAllDescendant(set: Set[LogicalPlan], plan: LogicalPlan): Unit = { | ||
| set += plan | ||
| plan.children.foreach { child => appendAllDescendant(set, child) } | ||
| } | ||
|
|
||
| def apply(plan: LogicalPlan): LogicalPlan = plan transform { | ||
| case logical: LogicalPlan => logical transformDown { | ||
| case h @ Hint(name, parameters, child) if BROADCAST_HINT_NAMES.contains(name.toUpperCase) => | ||
| var resolvedChild = child | ||
| for (table <- parameters) { | ||
| var stop = false | ||
| val skipNodeSet = scala.collection.mutable.Set.empty[LogicalPlan] | ||
| resolvedChild = resolvedChild.transformDown { | ||
| case n if skipNodeSet.contains(n) => | ||
| skipNodeSet -= n | ||
| n | ||
| case p @ Project(_, _) if p != resolvedChild => | ||
| appendAllDescendant(skipNodeSet, p) | ||
| skipNodeSet -= p | ||
| p | ||
| case r @ BroadcastHint(UnresolvedRelation(t, _)) | ||
| if !stop && resolver(t.table, table) => | ||
| stop = true | ||
| r | ||
| case r @ UnresolvedRelation(t, alias) if !stop && resolver(t.table, table) => | ||
| stop = true | ||
| if (alias.isDefined) { | ||
| SubqueryAlias(alias.get, BroadcastHint(r.copy(alias = None))) | ||
| } else { | ||
| BroadcastHint(r) | ||
| } | ||
| } | ||
| } | ||
| resolvedChild | ||
|
|
||
| // Remove unrecognized hints | ||
| case Hint(name, _, child) => child | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Check and add proper window frames for all window functions. | ||
| */ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| /* | ||
| * 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.dsl.expressions._ | ||
| import org.apache.spark.sql.catalyst.dsl.plans._ | ||
| import org.apache.spark.sql.catalyst.plans.logical._ | ||
|
|
||
| class SubstituteHintsSuite extends AnalysisTest { | ||
| import org.apache.spark.sql.catalyst.analysis.TestRelations._ | ||
|
|
||
| val a = testRelation.output(0) | ||
| val b = testRelation2.output(0) | ||
|
|
||
| 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)) | ||
|
|
||
| checkAnalysis( | ||
| Hint("MAPJOIN", Seq("table"), table("TaBlE")), | ||
| testRelation) | ||
| } | ||
|
|
||
| test("single hint") { | ||
| checkAnalysis( | ||
| Hint("MAPJOIN", Seq("TaBlE"), table("TaBlE").select(a)), | ||
| BroadcastHint(testRelation).select(a)) | ||
|
|
||
| checkAnalysis( | ||
| Hint("MAPJOIN", Seq("TaBlE"), table("TaBlE").as("t").join(table("TaBlE2").as("u")).select(a)), | ||
| BroadcastHint(testRelation).join(testRelation2).select(a)) | ||
|
|
||
| checkAnalysis( | ||
| Hint("MAPJOIN", Seq("TaBlE2"), | ||
| table("TaBlE").as("t").join(table("TaBlE2").as("u")).select(a)), | ||
| testRelation.join(BroadcastHint(testRelation2)).select(a)) | ||
| } | ||
|
|
||
| test("single hint with multiple parameters") { | ||
| checkAnalysis( | ||
| Hint("MAPJOIN", Seq("TaBlE", "TaBlE"), | ||
| table("TaBlE").as("t").join(table("TaBlE2").as("u")).select(a)), | ||
| BroadcastHint(testRelation).join(testRelation2).select(a)) | ||
|
|
||
| checkAnalysis( | ||
| Hint("MAPJOIN", Seq("TaBlE", "TaBlE2"), | ||
| table("TaBlE").as("t").join(table("TaBlE2").as("u")).select(a)), | ||
| BroadcastHint(testRelation).join(BroadcastHint(testRelation2)).select(a)) | ||
| } | ||
|
|
||
| test("duplicated nested hints are transformed into one") { | ||
| checkAnalysis( | ||
| Hint("MAPJOIN", Seq("TaBlE"), | ||
| Hint("MAPJOIN", Seq("TaBlE"), table("TaBlE").as("t").select('a)) | ||
| .join(table("TaBlE2").as("u")).select(a)), | ||
| BroadcastHint(testRelation).select(a).join(testRelation2).select(a)) | ||
|
|
||
| checkAnalysis( | ||
| Hint("MAPJOIN", Seq("TaBlE2"), | ||
| table("TaBlE").as("t").select(a) | ||
| .join(Hint("MAPJOIN", Seq("TaBlE2"), table("TaBlE2").as("u").select(b))).select(a)), | ||
| testRelation.select(a).join(BroadcastHint(testRelation2).select(b)).select(a)) | ||
| } | ||
|
|
||
| test("distinct nested two hints are handled separately") { | ||
| checkAnalysis( | ||
| Hint("MAPJOIN", Seq("TaBlE2"), | ||
| Hint("MAPJOIN", Seq("TaBlE"), table("TaBlE").as("t").select(a)) | ||
| .join(table("TaBlE2").as("u")).select(a)), | ||
| BroadcastHint(testRelation).select(a).join(BroadcastHint(testRelation2)).select(a)) | ||
|
|
||
| checkAnalysis( | ||
| Hint("MAPJOIN", Seq("TaBlE"), | ||
| table("TaBlE").as("t") | ||
| .join(Hint("MAPJOIN", Seq("TaBlE2"), table("TaBlE2").as("u").select(b))).select(a)), | ||
| BroadcastHint(testRelation).join(BroadcastHint(testRelation2).select(b)).select(a)) | ||
| } | ||
|
|
||
| test("deep self join") { | ||
| checkAnalysis( | ||
| Hint("MAPJOIN", Seq("TaBlE"), | ||
| table("TaBlE").join(table("TaBlE")).join(table("TaBlE")).join(table("TaBlE")).select(a)), | ||
| BroadcastHint(testRelation).join(testRelation).join(testRelation).join(testRelation) | ||
| .select(a)) | ||
| } | ||
|
|
||
| test("subquery should be ignored") { | ||
| checkAnalysis( | ||
| Hint("MAPJOIN", Seq("TaBlE"), | ||
| table("TaBlE").select(a).as("x").join(table("TaBlE")).select(a)), | ||
| testRelation.select(a).join(BroadcastHint(testRelation)).select(a)) | ||
|
|
||
| checkAnalysis( | ||
| Hint("MAPJOIN", Seq("TaBlE"), | ||
| table("TaBlE").as("t").select(a).as("x") | ||
| .join(table("TaBlE2").as("t2")).select(a)), | ||
| testRelation.select(a).join(testRelation2).select(a)) | ||
| } | ||
| } |
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.
How about this
SELECT? Should we also allow users useHinthere?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.
Do you want the following? It's already handled.
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.
uh... This grammar file kills my eye... You are right. So far, to support Broadcast Hint, it is good enough.