Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)?
Expand All @@ -374,6 +374,16 @@ querySpecification
windows?)
;

hint
: '/*+' hintStatement '*/'
;

hintStatement
: hintName=identifier
| hintName=identifier '(' parameters+=identifier parameters+=identifier ')'
Copy link
Contributor

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?

Copy link
Member

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.g4 accept more general rule for the future.
So, possibly, it's for reducing the change in SqlBase.g4 in order to add new hint rules.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This rule doesn't support like SELECT /*+ INDEX(a b c) */ * FROM t. To support it, it should be:

| hintName=identifier '(' parameters+=identifier parameters+=identifier* ')'

Is it intentional?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does it support?

INDEX(a, b, c)

?

I think ti's a bit weird to support space as the delimiter.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But we support INDEX(a b)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we? we should disallow it. want to submit a pr?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw this was written by @dongjoon-hyun. I didn't change it :)

Copy link
Member

Choose a reason for hiding this comment

The 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*
;
Expand Down Expand Up @@ -1002,8 +1012,12 @@ SIMPLE_COMMENT
: '--' ~[\r\n]* '\r'? '\n'? -> channel(HIDDEN)
;

BRACKETED_EMPTY_COMMENT
: '/**/' -> channel(HIDDEN)
;

BRACKETED_COMMENT
: '/*' .*? '*/' -> channel(HIDDEN)
: '/*' ~[+] .*? '*/' -> channel(HIDDEN)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does ~[+] mean?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It means except +.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/*+ is used hint rule in line 377.

;

WS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ class Analyzer(
val postHocResolutionRules: Seq[Rule[LogicalPlan]] = Nil

lazy val batches: Seq[Batch] = Seq(
Batch("Hints", fixedPoint,
new SubstituteHints.SubstituteBroadcastHints(conf),
SubstituteHints.RemoveAllHints),
Batch("Substitution", fixedPoint,
CTESubstitution,
WindowsSubstitution,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,10 @@ trait CheckAnalysis extends PredicateHelper {
|in operator ${operator.simpleString}
""".stripMargin)

case _: Hint =>
throw new IllegalStateException(
"Internal error: logical hint operator should have been removed during analysis")

case _ => // Analysis successful!
}
}
Expand Down
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))
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
Expand Up @@ -380,7 +380,10 @@ class AstBuilder extends SqlBaseBaseVisitor[AnyRef] with Logging {
}

// Window
withDistinct.optionalMap(windows)(withWindows)
val withWindow = withDistinct.optionalMap(windows)(withWindows)

// Hint
withWindow.optionalMap(hint)(withHints)
}
}

Expand Down Expand Up @@ -505,6 +508,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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,15 @@ case class BroadcastHint(child: LogicalPlan) extends UnaryNode {
super.computeStats(conf).copy(isBroadcastable = true)
}

/**
* A general hint for the child. This node will be eliminated post analysis.
* A pair of (name, parameters).
*/
case class Hint(name: String, parameters: Seq[String], child: LogicalPlan) extends UnaryNode {
override lazy val resolved: Boolean = false
override def output: Seq[Attribute] = child.output
}

/**
* Insert some data into a table. Note that this plan is unresolved and has to be replaced by the
* concrete implementations during analysis.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ trait AnalysisTest extends PlanTest {
val conf = new SimpleCatalystConf(caseSensitive)
val catalog = new SessionCatalog(new InMemoryCatalog, EmptyFunctionRegistry, conf)
catalog.createTempView("TaBlE", TestRelations.testRelation, overrideIfExists = true)
catalog.createTempView("TaBlE2", TestRelations.testRelation2, overrideIfExists = true)
new Analyzer(catalog, conf) {
override val extendedResolutionRules = EliminateSubqueryAliases :: Nil
}
Expand Down
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
Expand Up @@ -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"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if users writing /*+ BROADCASTJOIN(a b) */? shall we treat it as /*+ BROADCASTJOIN(a, b) */?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Both are considered equally now.

Copy link
Member

Choose a reason for hiding this comment

The 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))
}
}
Loading