cloud-fan commented on code in PR #58831:
URL: https://github.com/apache/spark/pull/58831#discussion_r4608100692
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala:
##########
@@ -6508,6 +6508,16 @@ object SQLConf {
.booleanConf
.createWithDefault(true)
+ val INSERT_COLUMN_LIST_DIFFERENT_ARITY_STRUCTS_RESOLVE_BY_POSITION =
+
buildConf("spark.sql.insertInto.columnList.differentArityStructsResolveByPosition.enabled")
+ .doc("When true, INSERT column lists resolve input and target structs
with different field " +
+ "counts by position.")
+ .internal()
+ .version("4.4.0")
Review Comment:
**Nit (P3):** `branch-4.4` was cut on 2026-11-16, after the previous review,
and `branch-4.x` now builds 4.5.0-SNAPSHOT (SPARK-61832). A PR against master
normally lands on master and `branch-4.x`, so this config would first ship in
4.5.0 while its metadata names 4.4.0. Unless a committer also lands the change
on `branch-4.4`, the version should be 4.5.0:
```suggestion
.version("4.5.0")
```
**Verification:**
- **Inspection:** The new config entry's version matches the release branch
it first lands on: 4.5.0 for master and branch-4.x, whose pom version is
4.5.0-SNAPSHOT.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/InsertionResolution.scala:
##########
@@ -154,40 +161,131 @@ private[sql] object InsertionResolution {
def resolveByNameColumn(
queryOutputColumn: Attribute,
targetColumn: NamedExpression,
- newAlias: (Expression, String) => Alias): Alias = {
+ newAlias: (Expression, String) => Alias,
+ tableName: String,
+ resolver: => Resolver,
+ resolveDifferentArityByPosition: => Boolean): Alias = {
+ val colPath = Seq(targetColumn.name)
(queryOutputColumn.dataType, targetColumn.dataType) match {
case (input: StructType, expected: StructType) =>
- newAlias(Cast(queryOutputColumn, renameFieldsInStruct(input,
expected)), targetColumn.name)
+ val renamedType = renameFieldsInStruct(
+ input,
+ expected,
+ tableName,
+ colPath,
+ resolver,
+ resolveDifferentArityByPosition)
+ newAlias(Cast(queryOutputColumn, renamedType), targetColumn.name)
case (input: ArrayType, expected: ArrayType) =>
- newAlias(Cast(queryOutputColumn, renameFieldsInType(input, expected)),
targetColumn.name)
+ val renamedType = renameFieldsInType(
+ input,
+ expected,
+ tableName,
+ colPath,
+ resolver,
+ resolveDifferentArityByPosition)
+ newAlias(Cast(queryOutputColumn, renamedType), targetColumn.name)
case (input: MapType, expected: MapType) =>
- newAlias(Cast(queryOutputColumn, renameFieldsInType(input, expected)),
targetColumn.name)
+ val renamedType = renameFieldsInType(
+ input,
+ expected,
+ tableName,
+ colPath,
+ resolver,
+ resolveDifferentArityByPosition)
+ newAlias(Cast(queryOutputColumn, renamedType), targetColumn.name)
case _ =>
newAlias(queryOutputColumn, targetColumn.name)
}
}
- private def renameFieldsInStruct(input: StructType, expected: StructType):
StructType = {
- if (input.length == expected.length) {
- val newFields = input.zip(expected).map { case (f1, f2) =>
- f1.copy(name = f2.name, dataType = renameFieldsInType(f1.dataType,
f2.dataType))
+ private def renameFieldsInStruct(
+ input: StructType,
+ expected: StructType,
+ tableName: String,
+ colPath: Seq[String],
+ resolver: => Resolver,
+ resolveDifferentArityByPosition: => Boolean): StructType = {
+ if (input.length == expected.length || resolveDifferentArityByPosition) {
Review Comment:
**Nit (P3):** This condition now also renames structs whose field count
differs from the target's, so the projection keeps a `Cast` whose type differs
from its input only in field names. `SimplifyCasts` removes only same-type
casts, and neither `CollapseProject` nor `SimplifyExtractValueOps` folds
through a `Cast`, so, reading those rules, a struct built inline with
`named_struct(...)` is built and then rebuilt per row, also when its names
differ from the target only by case.
Deferring that to a separate optimizer follow-up, as you said in the review
thread, is fine for this PR, but nothing in the code says so. A TODO here that
names the deferred simplification would tell later readers that the extra
per-row work is known and what should remove it, for example:
```suggestion
// TODO: A rename makes the projection's Cast differ from its input only
in field names, so
// SimplifyCasts keeps it and the optimizer cannot fold through it: a
struct built inline is
// built and then rebuilt per row, also when names differ from the
target only by case.
// Remove this cost with an optimizer rule that simplifies casts that
only rename fields.
if (input.length == expected.length || resolveDifferentArityByPosition) {
```
This extends an [existing
discussion](https://github.com/apache/spark/pull/58831#discussion_r4483367229).
**Verification:**
- **Inspection:** The comment next to the different-arity rename in
InsertionResolution.scala (line 209 at this head) states that the
field-renaming cast keeps an extra per-row struct rebuild for inline-built
structs, including names that differ from the target only by case, and that
removing it is deferred to an optimizer follow-up that simplifies such casts.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala:
##########
@@ -6508,6 +6508,16 @@ object SQLConf {
.booleanConf
.createWithDefault(true)
+ val INSERT_COLUMN_LIST_DIFFERENT_ARITY_STRUCTS_RESOLVE_BY_POSITION =
+
buildConf("spark.sql.insertInto.columnList.differentArityStructsResolveByPosition.enabled")
Review Comment:
**Nit (P3):** This key opens the namespaces `spark.sql.insertInto.` and
`spark.sql.insertInto.columnList.`, which no other config uses. The naming
guideline in
`common/utils/src/main/scala/org/apache/spark/internal/config/ConfigEntry.scala`
says to create a namespace only when it is meaningful and shared by several
configs, and the neighboring INSERT configs use flat names such as
`spark.sql.insertIntoReplaceWhereColumnList.enabled` and
`spark.sql.insertNestedTypeCoercion.enabled`. The key is new, so renaming it
costs nothing now; after a release, sessions that set it would need the old
name kept as an alternative. A flat name such as
`spark.sql.insertIntoColumnListDifferentArityByPosition.enabled` would follow
the guideline, and the constant, default and behavior can stay as they are.
**Verification:**
- **Inspection:** No reference to the string
spark.sql.insertInto.columnList.differentArityStructsResolveByPosition.enabled
remains in the repository, and the new key does not start a namespace that no
other config uses.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveInsertionBase.scala:
##########
@@ -51,39 +53,79 @@ abstract class ResolveInsertionBase extends
Rule[LogicalPlan] {
(queryOutputCol.dataType, resolvedCol.dataType) match {
case (input: StructType, expected: StructType) =>
// Rename inner fields of the input column to pass the by-name
INSERT analysis.
- Alias(Cast(queryOutputCol, renameFieldsInStruct(input, expected)),
resolvedCol.name)()
+ val renamedType = renameFieldsInStruct(tblName, input, expected,
Seq(resolvedCol.name))
+ Alias(Cast(queryOutputCol, renamedType), resolvedCol.name)()
case (input: ArrayType, expected: ArrayType) =>
- Alias(Cast(queryOutputCol, renameFieldsInType(input, expected)),
resolvedCol.name)()
+ val renamedType = renameFieldsInType(tblName, input, expected,
Seq(resolvedCol.name))
+ Alias(Cast(queryOutputCol, renamedType), resolvedCol.name)()
case (input: MapType, expected: MapType) =>
- Alias(Cast(queryOutputCol, renameFieldsInType(input, expected)),
resolvedCol.name)()
+ val renamedType = renameFieldsInType(tblName, input, expected,
Seq(resolvedCol.name))
+ Alias(Cast(queryOutputCol, renamedType), resolvedCol.name)()
case _ =>
Alias(queryOutputCol, resolvedCol.name)()
}
}
Project(projectByName, i.query)
}
- private def renameFieldsInStruct(input: StructType, expected: StructType):
StructType = {
- if (input.length == expected.length) {
- val newFields = input.zip(expected).map { case (f1, f2) =>
- f1.copy(name = f2.name, dataType = renameFieldsInType(f1.dataType,
f2.dataType))
+ private def renameFieldsInStruct(
+ tableName: String,
+ input: StructType,
+ expected: StructType,
+ colPath: Seq[String]): StructType = {
+ if (input.length == expected.length || resolveDifferentArityByPosition) {
+ val renamedFields = input.zip(expected).map { case (f1, f2) =>
+ f1.copy(
+ name = f2.name,
Review Comment:
Thanks, deferring the cast simplification to a separate optimizer follow-up
is fine for this PR, since the equal-arity path already has the same cast
shape. Because the deferral is recorded only in this thread, this review asks
for a TODO in the code that names the deferred simplification, so later readers
know the extra per-row work is known and what should remove it.
<!-- SPARK_DEV_REVIEW_REPLY
{"feedback_id":"inline:4483367229","thread_id":"inline:4483367229","verdict_sha256":"f56c24f1c1a6e8aa77c60a46224ac97df84ef0d453d0734a361d65a1db2b752c"}
-->
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]