Skip to content
Closed
Show file tree
Hide file tree
Changes from 10 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 @@ -371,7 +371,7 @@ querySpecification
(RECORDREADER recordReader=STRING)?
fromClause?
(WHERE where=booleanExpression)?)
| ((kind=SELECT hint? setQuantifier? namedExpressionSeq fromClause?
| ((kind=SELECT (hints+=hint)* setQuantifier? namedExpressionSeq fromClause?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

In Hive and Oracle, multiple hints are put in the same /*+ */.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This patch supports both.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@gatorsmile does hive support multiple /*+ */?

@gatorsmile gatorsmile May 30, 2017

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nope. Hive does not support multiple /*+ */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It does not hurt anything if we support more hint styles, as long as they are user-friendly.

| fromClause (kind=SELECT setQuantifier? namedExpressionSeq)?)
lateralView*
(WHERE where=booleanExpression)?
Expand All @@ -381,12 +381,12 @@ querySpecification
;

hint
: '/*+' hintStatement '*/'
: '/*+' hintStatements+=hintStatement (hintStatements+=hintStatement)* '*/'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

In the same block /*+ */, multiple hints are separated by commas in Hive. However, in Oracle, it is separated by spaces.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I added support for optional comma

;

hintStatement
: hintName=identifier
| hintName=identifier '(' parameters+=identifier (',' parameters+=identifier)* ')'
| hintName=identifier '(' parameters+=primaryExpression (',' parameters+=primaryExpression)* ')'
;

fromClause
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package org.apache.spark.sql.catalyst.analysis

import java.util.Locale

import org.apache.spark.sql.AnalysisException
import org.apache.spark.sql.catalyst.plans.logical._
import org.apache.spark.sql.catalyst.rules.Rule
import org.apache.spark.sql.catalyst.trees.CurrentOrigin
Expand Down Expand Up @@ -91,7 +92,12 @@ object ResolveHints {
ResolvedHint(h.child, HintInfo(isBroadcastable = Option(true)))
} else {
// Otherwise, find within the subtree query plans that should be broadcasted.
applyBroadcastHint(h.child, h.parameters.toSet)
applyBroadcastHint(h.child, h.parameters.map {
case tableName: String => tableName
case tableId: UnresolvedAttribute => tableId.name
case unsupported => throw new AnalysisException("Broadcast hint parameter should be " +
s" identifier or string but was $unsupported (${unsupported.getClass}")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nit: s" identifier or string -> s"an identifier or string

}.toSet)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ class AstBuilder(conf: SQLConf) extends SqlBaseBaseVisitor[AnyRef] with Logging
val withWindow = withDistinct.optionalMap(windows)(withWindows)

// Hint
withWindow.optionalMap(hint)(withHints)
hints.asScala.foldRight(withWindow)(withHints)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

why we construct the hint from right to left?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

so that select /*+ hint1() /* /*+ hint2() */produces Hint1(Hint2(plan)) and not Hint2(Hint1(plan)). withHints adds a Hint on top so the last one folded is the top most.

}
}

Expand Down Expand Up @@ -533,13 +533,16 @@ class AstBuilder(conf: SQLConf) extends SqlBaseBaseVisitor[AnyRef] with Logging
}

/**
* Add a [[UnresolvedHint]] to a logical plan.
* Add a [[UnresolvedHint]]s to a logical plan.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

remove a

*/
private def withHints(
ctx: HintContext,
query: LogicalPlan): LogicalPlan = withOrigin(ctx) {
val stmt = ctx.hintStatement
UnresolvedHint(stmt.hintName.getText, stmt.parameters.asScala.map(_.getText), query)
var plan = query

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

using foldLeft instead of having a var?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Honestly I think foldLeft is almost always a bad idea ...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I used foldRight somewhere too. Why is it a bad idea?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

i always find a loop simpler to reason about ...

ctx.hintStatements.asScala.foreach { case stmt =>
plan = UnresolvedHint(stmt.hintName.getText, stmt.parameters.asScala.map(expression), plan)
}
plan
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import org.apache.spark.sql.internal.SQLConf
* should be removed This node will be eliminated post analysis.
* A pair of (name, parameters).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This needs an update.

*/
case class UnresolvedHint(name: String, parameters: Seq[String], child: LogicalPlan)
case class UnresolvedHint(name: String, parameters: Seq[Any], child: LogicalPlan)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

shall we use Expression as type?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

If we use Expression then either:

  • Dataset.hint parameters should be Expression too, in which case you can't do df.hint("hint", 1, 2, "c") you'd have to do df.hint("hint", Literal(1), Literal(2), Literal("c")) or a shortcut if there is
  • Dataset.hint accepts Any but then has to convert Any to Expressions. One problem here is that Seq(1,2,3) can't be converted to Literal. So you have to use df.hint("hint", Array(1,2,3))

The disadvantage of have Any in UnresolvedHint is that to resolve the hint you have to check both for String and Literal(String) but the API is easier to use.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

we can keep Any in the API(df.hint(xxx)), but use Expression in UnresolvedHint, what do you think?

@bogdanrdc bogdanrdc May 29, 2017

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

One useful hint parameter is a list of columns.
Something like df.hint("hint", $"table", Seq($"col1", $"col2", $"col3"))

In this case UnresolvedHint could be called like this:
UnresolvedHint(name: String, parameters: Seq(Expression, Seq[Expression]), child)

But if UnresolvedHint.parameters is Seq[Expression] then it's not possible to have this kind of hint.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

To support multiple parameters in hint, does it make sense to do it like df.hint("hint", "1, 2, c")? We can use our Parser to parse this parameter string.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think that could be something extra. The DF API should accept scala expressions too: function calls (df.hint("hint", getInterestingValues()))

extends UnaryNode {

override lazy val resolved: Boolean = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package org.apache.spark.sql.catalyst.parser

import org.apache.spark.sql.catalyst.FunctionIdentifier
import org.apache.spark.sql.catalyst.analysis.{UnresolvedGenerator, UnresolvedInlineTable, UnresolvedTableValuedFunction}
import org.apache.spark.sql.catalyst.analysis.{UnresolvedAttribute, UnresolvedFunction, UnresolvedGenerator, UnresolvedInlineTable, UnresolvedTableValuedFunction}
import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.catalyst.plans._
import org.apache.spark.sql.catalyst.plans.logical._
Expand Down Expand Up @@ -518,47 +518,97 @@ class PlanParserSuite extends PlanTest {
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("mismatched input '.' expecting {')', ','}"))
assert(m.contains("mismatched input"))

// Disallow space as the delimiter.
val m3 = intercept[ParseException] {
parsePlan("SELECT /*+ INDEX(a b c) */ * from default.t")
}.getMessage
assert(m3.contains("mismatched input 'b' expecting {')', ','}"))
assert(m3.contains("mismatched input 'b' expecting"))

comparePlans(
parsePlan("SELECT /*+ HINT */ * FROM t"),
UnresolvedHint("HINT", Seq.empty, table("t").select(star())))

comparePlans(
parsePlan("SELECT /*+ BROADCASTJOIN(u) */ * FROM t"),
UnresolvedHint("BROADCASTJOIN", Seq("u"), table("t").select(star())))
UnresolvedHint("BROADCASTJOIN", Seq($"u"), table("t").select(star())))

comparePlans(
parsePlan("SELECT /*+ MAPJOIN(u) */ * FROM t"),
UnresolvedHint("MAPJOIN", Seq("u"), table("t").select(star())))
UnresolvedHint("MAPJOIN", Seq($"u"), table("t").select(star())))

comparePlans(
parsePlan("SELECT /*+ STREAMTABLE(a,b,c) */ * FROM t"),
UnresolvedHint("STREAMTABLE", Seq("a", "b", "c"), table("t").select(star())))
UnresolvedHint("STREAMTABLE", Seq($"a", $"b", $"c"), table("t").select(star())))

comparePlans(
parsePlan("SELECT /*+ INDEX(t, emp_job_ix) */ * FROM t"),
UnresolvedHint("INDEX", Seq("t", "emp_job_ix"), table("t").select(star())))
UnresolvedHint("INDEX", Seq($"t", $"emp_job_ix"), table("t").select(star())))

comparePlans(
parsePlan("SELECT /*+ MAPJOIN(`default.t`) */ * from `default.t`"),
UnresolvedHint("MAPJOIN", Seq("default.t"), table("default.t").select(star())))
UnresolvedHint("MAPJOIN", Seq(UnresolvedAttribute.quoted("default.t")),
table("default.t").select(star())))

comparePlans(
parsePlan("SELECT /*+ MAPJOIN(t) */ a from t where true group by a order by a"),
UnresolvedHint("MAPJOIN", Seq("t"),
UnresolvedHint("MAPJOIN", Seq($"t"),
table("t").where(Literal(true)).groupBy('a)('a)).orderBy('a.asc))
}

test("SPARK-20854: select hint syntax with expressions") {
comparePlans(
parsePlan("SELECT /*+ HINT1(a, array(1, 2, 3)) */ * from t"),
UnresolvedHint("HINT1", Seq($"a",
UnresolvedFunction("array", Literal(1) :: Literal(2) :: Literal(3) :: Nil, false)),
table("t").select(star())
)
)

comparePlans(
parsePlan("SELECT /*+ HINT1(a, array(1, 2, 3)) */ * from t"),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is this test case redundant?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

yea, @bogdanrdc can you send a follow-up PR to clean it up?

UnresolvedHint("HINT1", Seq($"a",
UnresolvedFunction("array", Literal(1) :: Literal(2) :: Literal(3) :: Nil, false)),
table("t").select(star())
)
)

comparePlans(
parsePlan("SELECT /*+ HINT1(a, 5, 'a', b) */ * from t"),
UnresolvedHint("HINT1", Seq($"a", Literal(5), Literal("a"), $"b"),
table("t").select(star())
)
)

comparePlans(
parsePlan("SELECT /*+ HINT1('a', (b, c), (1, 2)) */ * from t"),
UnresolvedHint("HINT1",
Seq(Literal("a"),
CreateStruct($"b" :: $"c" :: Nil),
CreateStruct(Literal(1) :: Literal(2) :: Nil)),
table("t").select(star())
)
)
}

test("SPARK-20854: multiple hints") {
comparePlans(
parsePlan("SELECT /*+ HINT1(a, 1) hint2(b, 2) */ * from t"),
UnresolvedHint("hint2", Seq($"b", Literal(2)),
UnresolvedHint("HINT1", Seq($"a", Literal(1)),
table("t").select(star())

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nit: Indent

)
)
)

comparePlans(
parsePlan("SELECT /*+ HINT1(a, 1) */ /*+ hint2(b, 2) */ * from t"),
UnresolvedHint("HINT1", Seq($"a", Literal(1)),
UnresolvedHint("hint2", Seq($"b", Literal(2)),
table("t").select(star())
)
)
)
}
}
2 changes: 1 addition & 1 deletion sql/core/src/main/scala/org/apache/spark/sql/Dataset.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1176,7 +1176,7 @@ class Dataset[T] private[sql](
* @since 2.2.0
*/
@scala.annotation.varargs
def hint(name: String, parameters: String*): Dataset[T] = withTypedPlan {
def hint(name: String, parameters: Any*): Dataset[T] = withTypedPlan {
UnresolvedHint(name, parameters, planWithBarrier)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Normally, we move such a test suite to org.apache.spark.sql.catalyst. We just need to add hint into org.apache.spark.sql.catalyst.dsl.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I added a new test for dsl. I also want a test that calls df.hint


import org.apache.spark.sql.catalyst.expressions.Literal
import org.apache.spark.sql.catalyst.plans.PlanTest
import org.apache.spark.sql.catalyst.plans.logical.UnresolvedHint
import org.apache.spark.sql.test.SharedSQLContext

class DataFrameHintSuite extends PlanTest with SharedSQLContext {
import testImplicits._
lazy val df = spark.range(10)

test("various hint parameters") {
comparePlans(
df.hint("hint1").queryExecution.logical,
UnresolvedHint("hint1", Seq(),
df.logicalPlan
)
)

comparePlans(
df.hint("hint1", 1, "a").queryExecution.logical,
UnresolvedHint("hint1", Seq(1, "a"), df.logicalPlan)
)

comparePlans(
df.hint("hint1", 1, $"a").queryExecution.logical,
UnresolvedHint("hint1", Seq(1, $"a"),
df.logicalPlan
)
)

comparePlans(
df.hint("hint1", Seq(1, 2, 3), Seq($"a", $"b", $"c")).queryExecution.logical,
UnresolvedHint("hint1", Seq(Seq(1, 2, 3), Seq($"a", $"b", $"c")),
df.logicalPlan
)
)
}
}