Skip to content

Commit eb1e6ad

Browse files
committed
[SPARK-46388][SQL] HiveAnalysis misses the pattern guard of query.resolved
### What changes were proposed in this pull request? This PR adds `query.resolved` as a pattern guard when HiveAnalysis converts InsertIntoStatement to InsertIntoHiveTable. ### Why are the changes needed? Due to https://github.com/apache/spark/pull/41262/files#diff-ed19f376a63eba52eea59ca71f3355d4495fad4fad4db9a3324aade0d4986a47R1080, the `table` field is resolved regardless of the query field. Before, it never got a chance to be resolved as `HiveTableRelation` and then match any rule of HiveAnalysis. But now, it gets the chance always and results in a spark-kernel bug - `Invalid call to toAttribute on unresolved object.` ``` insert into t2 select cast(a as short) from t where b=1; Invalid call to toAttribute on unresolved object ``` ### Does this PR introduce _any_ user-facing change? no, bugfix for 3.5 and later ### How was this patch tested? added new test ### Was this patch authored or co-authored using generative AI tooling? no Closes #44326 from yaooqinn/SPARK-46388. Authored-by: Kent Yao <[email protected]> Signed-off-by: Kent Yao <[email protected]> (cherry picked from commit ccc436d) Signed-off-by: Kent Yao <[email protected]>
1 parent ac031d6 commit eb1e6ad

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveStrategies.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ object HiveAnalysis extends Rule[LogicalPlan] {
161161
override def apply(plan: LogicalPlan): LogicalPlan = plan resolveOperators {
162162
case InsertIntoStatement(
163163
r: HiveTableRelation, partSpec, _, query, overwrite, ifPartitionNotExists, _)
164-
if DDLUtils.isHiveTable(r.tableMeta) =>
164+
if DDLUtils.isHiveTable(r.tableMeta) && query.resolved =>
165165
InsertIntoHiveTable(r.tableMeta, partSpec, query, overwrite,
166166
ifPartitionNotExists, query.output.map(_.name))
167167

sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/SQLQuerySuite.scala

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2660,6 +2660,32 @@ abstract class SQLQuerySuiteBase extends QueryTest with SQLTestUtils with TestHi
26602660
checkAnswer(df, Seq.empty[Row])
26612661
}
26622662
}
2663+
2664+
test("SPARK-46388: HiveAnalysis convert InsertIntoStatement to InsertIntoHiveTable " +
2665+
"iff child resolved") {
2666+
withTable("t") {
2667+
sql("CREATE TABLE t (a STRING)")
2668+
checkError(
2669+
exception = intercept[AnalysisException](sql("INSERT INTO t SELECT a*2 FROM t where b=1")),
2670+
errorClass = "UNRESOLVED_COLUMN.WITH_SUGGESTION",
2671+
sqlState = None,
2672+
parameters = Map("objectName" -> "`b`", "proposal" -> "`a`"),
2673+
context = ExpectedContext(
2674+
fragment = "b",
2675+
start = 38,
2676+
stop = 38) )
2677+
checkError(
2678+
exception = intercept[AnalysisException](
2679+
sql("INSERT INTO t SELECT cast(a as short) FROM t where b=1")),
2680+
errorClass = "UNRESOLVED_COLUMN.WITH_SUGGESTION",
2681+
sqlState = None,
2682+
parameters = Map("objectName" -> "`b`", "proposal" -> "`a`"),
2683+
context = ExpectedContext(
2684+
fragment = "b",
2685+
start = 51,
2686+
stop = 51))
2687+
}
2688+
}
26632689
}
26642690

26652691
@SlowHiveTest

0 commit comments

Comments
 (0)