Skip to content

Commit

Permalink
[SPARK-46388][SQL] HiveAnalysis misses the pattern guard of `query.re…
Browse files Browse the repository at this point in the history
…solved`

### 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]>
  • Loading branch information
yaooqinn committed Dec 13, 2023
1 parent b3398e6 commit ccc436d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ object HiveAnalysis extends Rule[LogicalPlan] {
override def apply(plan: LogicalPlan): LogicalPlan = plan resolveOperators {
case InsertIntoStatement(
r: HiveTableRelation, partSpec, _, query, overwrite, ifPartitionNotExists, _)
if DDLUtils.isHiveTable(r.tableMeta) =>
if DDLUtils.isHiveTable(r.tableMeta) && query.resolved =>
InsertIntoHiveTable(r.tableMeta, partSpec, query, overwrite,
ifPartitionNotExists, query.output.map(_.name))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2669,6 +2669,32 @@ abstract class SQLQuerySuiteBase extends QueryTest with SQLTestUtils with TestHi
checkAnswer(df, Seq.empty[Row])
}
}

test("SPARK-46388: HiveAnalysis convert InsertIntoStatement to InsertIntoHiveTable " +
"iff child resolved") {
withTable("t") {
sql("CREATE TABLE t (a STRING)")
checkError(
exception = intercept[AnalysisException](sql("INSERT INTO t SELECT a*2 FROM t where b=1")),
errorClass = "UNRESOLVED_COLUMN.WITH_SUGGESTION",
sqlState = None,
parameters = Map("objectName" -> "`b`", "proposal" -> "`a`"),
context = ExpectedContext(
fragment = "b",
start = 38,
stop = 38) )
checkError(
exception = intercept[AnalysisException](
sql("INSERT INTO t SELECT cast(a as short) FROM t where b=1")),
errorClass = "UNRESOLVED_COLUMN.WITH_SUGGESTION",
sqlState = None,
parameters = Map("objectName" -> "`b`", "proposal" -> "`a`"),
context = ExpectedContext(
fragment = "b",
start = 51,
stop = 51))
}
}
}

@SlowHiveTest
Expand Down

0 comments on commit ccc436d

Please sign in to comment.