Skip to content

Commit 5f91f87

Browse files
xingchaozhGitHub Enterprise
authored andcommitted
[CARMEL-6553] Backport [SPARK-35673][SQL] Fix user-defined hint and unrecognized hint in subquery (#1236)
1 parent d8b4399 commit 5f91f87

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/CheckAnalysis.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ trait CheckAnalysis extends PredicateHelper {
109109
case u: UnresolvedRelation =>
110110
u.failAnalysis(s"Table or view not found: ${u.multipartIdentifier.quoted}")
111111

112+
case u: UnresolvedHint =>
113+
u.failAnalysis(s"Hint not found: ${u.name}")
114+
112115
case InsertIntoStatement(u: UnresolvedRelation, _, _, _, _, _) =>
113116
failAnalysis(s"Table not found: ${u.multipartIdentifier.quoted}")
114117

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/hints.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import org.apache.spark.sql.catalyst.expressions.Attribute
3030
case class UnresolvedHint(name: String, parameters: Seq[Any], child: LogicalPlan)
3131
extends UnaryNode {
3232

33-
override lazy val resolved: Boolean = false
33+
override lazy val resolved: Boolean = child.resolved
3434
override def output: Seq[Attribute] = child.output
3535
}
3636

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/AnalysisErrorSuite.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,4 +683,20 @@ class AnalysisErrorSuite extends AnalysisTest {
683683
UnresolvedRelation(TableIdentifier("t", Option("nonexist")))))))
684684
assertAnalysisError(plan, "Table or view not found:" :: Nil)
685685
}
686+
687+
test("SPARK-35673: fail if the plan still contains UnresolvedHint after analysis") {
688+
val hintName = "some_random_hint_that_does_not_exist"
689+
val plan = UnresolvedHint(hintName, Seq.empty,
690+
Project(Alias(Literal(1), "x")() :: Nil, OneRowRelation())
691+
)
692+
assert(plan.resolved)
693+
694+
val error = intercept[AnalysisException] {
695+
SimpleAnalyzer.checkAnalysis(plan)
696+
}
697+
assert(error.message.contains(s"Hint not found: ${hintName}"))
698+
699+
// UnresolvedHint be removed by batch `Remove Unresolved Hints`
700+
assertAnalysisSuccess(plan, true)
701+
}
686702
}

sql/core/src/test/scala/org/apache/spark/sql/SparkSessionExtensionSuite.scala

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,32 @@ class SparkSessionExtensionSuite extends SparkFunSuite {
355355
stop(session)
356356
}
357357
}
358+
359+
test("SPARK-35673: user-defined hint and unrecognized hint in subquery") {
360+
withSession(Seq(_.injectPostHocResolutionRule(MyHintRule))) { session =>
361+
// unrecognized hint
362+
QueryTest.checkAnswer(
363+
session.sql(
364+
"""
365+
|SELECT *
366+
|FROM (
367+
| SELECT /*+ some_random_hint_that_does_not_exist */ 42
368+
|)
369+
|""".stripMargin),
370+
Row(42) :: Nil)
371+
372+
// user-defined hint
373+
QueryTest.checkAnswer(
374+
session.sql(
375+
"""
376+
|SELECT *
377+
|FROM (
378+
| SELECT /*+ CONVERT_TO_EMPTY */ 42
379+
|)
380+
|""".stripMargin),
381+
Nil)
382+
}
383+
}
358384
}
359385

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

0 commit comments

Comments
 (0)