-
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 1 commit
539782d
318bc03
c702e3e
a095df3
617f8a2
51a73d5
0d42978
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -364,7 +364,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)? | ||
|
|
@@ -373,6 +373,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* | ||
| ; | ||
|
|
@@ -996,8 +1006,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 |
|---|---|---|
|
|
@@ -387,7 +387,10 @@ class AstBuilder extends SqlBaseBaseVisitor[AnyRef] with Logging { | |
| } | ||
|
|
||
| // Window | ||
| withDistinct.optionalMap(windows)(withWindows) | ||
| val withWindow = withDistinct.optionalMap(windows)(withWindows) | ||
|
|
||
| // Hint | ||
| withWindow.optionalMap(ctx.hint)(withHints) | ||
|
||
| } | ||
| } | ||
|
|
||
|
|
@@ -527,6 +530,16 @@ class AstBuilder extends SqlBaseBaseVisitor[AnyRef] with Logging { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Add a Hint to a logical plan. | ||
| */ | ||
| private def withHints( | ||
| ctx: HintContext, | ||
| query: LogicalPlan): LogicalPlan = withOrigin(ctx) { | ||
| val stmt = ctx.hintStatement | ||
| Hint(stmt.hintName.getText, stmt.parameters.asScala.map(_.getText), query) | ||
| } | ||
|
|
||
| /** | ||
| * Add a [[Generate]] (Lateral View) to a logical plan. | ||
| */ | ||
|
|
||
| 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)) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -503,4 +503,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.