Skip to content

Commit

Permalink
[CARMEL-6553] Backport [SPARK-35673][SQL] Fix user-defined hint and u…
Browse files Browse the repository at this point in the history
…nrecognized hint in subquery (#1236)
  • Loading branch information
xingchaozh authored and GitHub Enterprise committed Feb 16, 2023
1 parent d8b4399 commit 5f91f87
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ trait CheckAnalysis extends PredicateHelper {
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, _, _, _, _, _) =>
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,7 @@ import org.apache.spark.sql.catalyst.expressions.Attribute
case class UnresolvedHint(name: String, parameters: Seq[Any], child: LogicalPlan)
extends UnaryNode {

override lazy val resolved: Boolean = false
override lazy val resolved: Boolean = child.resolved
override def output: Seq[Attribute] = child.output
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -683,4 +683,20 @@ class AnalysisErrorSuite extends AnalysisTest {
UnresolvedRelation(TableIdentifier("t", Option("nonexist")))))))
assertAnalysisError(plan, "Table or view not found:" :: 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 @@ -355,6 +355,32 @@ class SparkSessionExtensionSuite extends SparkFunSuite {
stop(session)
}
}

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

0 comments on commit 5f91f87

Please sign in to comment.