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 @@ -122,6 +122,9 @@ trait CheckAnalysis extends PredicateHelper with LookupCatalog {
case u: UnresolvedRelation =>
u.failAnalysis(s"Table or view not found: ${u.multipartIdentifier.quoted}")

case u: UnresolvedHint =>
u.failAnalysis(s"Hint not found: ${u.name}")

case InsertIntoStatement(u: UnresolvedRelation, _, _, _, _, _) =>
u.failAnalysis(s"Table not found: ${u.multipartIdentifier.quoted}")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ import org.apache.spark.sql.catalyst.trees.TreePattern.{TreePattern, UNRESOLVED_
case class UnresolvedHint(name: String, parameters: Seq[Any], child: LogicalPlan)
extends UnaryNode {

override lazy val resolved: Boolean = false
// we need it to be resolved so that the analyzer can continue to analyze the rest of the query
// plan.
override lazy val resolved: Boolean = child.resolved
Copy link
Contributor

@cloud-fan cloud-fan Jun 9, 2021

Choose a reason for hiding this comment

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

Let's add some comments to explain the reason: we need it to be resolved so that the analyzer can continue to analyze the rest of the query plan.

Copy link
Contributor

Choose a reason for hiding this comment

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

To make up for this change, let's add a check in CheckAnalysis and fail if the plan still contains UnresolvedHint after analysis.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's ok for me to add this check.
And note that in the build-in analyzer, all of UnresolvedHint will be removed by batch Remove Unresolved Hints.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yea, it's just for sanity check, to make sure UnresolvedHint shouldn't exist after analysis.


override def output: Seq[Attribute] = child.output
final override val nodePatterns: Seq[TreePattern] = Seq(UNRESOLVED_HINT)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -791,4 +791,20 @@ class AnalysisErrorSuite extends AnalysisTest {
assertAnalysisError(plan, s"Correlated column is not allowed in predicate ($msg)" :: Nil)
}
}

test("SPARK-35673: fail if the plan still contains UnresolvedHint after analysis") {
val hintName = "some_random_hint_that_does_not_exist"
val plan = UnresolvedHint(hintName, Seq.empty,
Project(Alias(Literal(1), "x")() :: Nil, OneRowRelation())
)
assert(plan.resolved)

val error = intercept[AnalysisException] {
SimpleAnalyzer.checkAnalysis(plan)
}
assert(error.message.contains(s"Hint not found: ${hintName}"))

// UnresolvedHint be removed by batch `Remove Unresolved Hints`
assertAnalysisSuccess(plan, true)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,32 @@ class SparkSessionExtensionSuite extends SparkFunSuite {
}
}
}

test("SPARK-35673: user-defined hint and unrecognized hint in subquery") {
withSession(Seq(_.injectPostHocResolutionRule(MyHintRule))) { session =>
// unrecognized hint
QueryTest.checkAnswer(
session.sql(
"""
|SELECT *
|FROM (
| SELECT /*+ some_random_hint_that_does_not_exist */ 42
|)
|""".stripMargin),
Row(42) :: Nil)

// user-defined hint
QueryTest.checkAnswer(
session.sql(
"""
|SELECT *
|FROM (
| SELECT /*+ CONVERT_TO_EMPTY */ 42
|)
|""".stripMargin),
Nil)
}
}
}

case class MyRule(spark: SparkSession) extends Rule[LogicalPlan] {
Expand Down