dtenedor commented on code in PR #60629:
URL: https://github.com/apache/spark/pull/60629#discussion_r4605626638


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala:
##########
@@ -8853,6 +8853,18 @@ object SQLConf {
       .booleanConf
       .createWithDefault(true)
 
+  val PIPE_SET_ADDS_MISSING_COLUMNS_ENABLED =
+    buildConf("spark.sql.pipeSetAddsMissingColumns.enabled")
+      .internal()
+      .doc("When true (the default), a pipe |> SET assignment to a column that 
does not exist " +
+        "appends the column after the input columns, as |> EXTEND does. When 
false, such an " +
+        "assignment fails with UNRESOLVED_COLUMN, as it did before Spark 5.0. 
Either way, an " +
+        "assignment to an existing column replaces it in place.")
+      .version("5.0.0")
+      .withBindingPolicy(ConfigBindingPolicy.SESSION)

Review Comment:
   Should this be `PERSISTED` rather than `SESSION`? The `ConfigBindingPolicy` 
guide says to use PERSISTED when a view or SQL function should keep computing 
the result it had at creation, no matter who calls it later. Its counterexample 
is a bug-fix flag, where views shouldn't freeze the buggy behavior. This flag 
gates a feature. The precedent in the description, 
`spark.sql.legacy.allowAliasedPipeAggregateOrdinals`, gates a correctness fix.
   
   With SESSION, setting the flag to `false`, as the migration guide suggests 
for restoring the old behavior, makes every view that relies on the new 
behavior fail with `UNRESOLVED_COLUMN`. The new golden output shows this for 
the temp view.
   
   Because `AstBuilder` reads the flag at parse time, SESSION also only applies 
where a body is parsed under the caller's settings:
   - views: `SessionCatalog.fromCatalogTable` parses under 
`View.effectiveSQLConf`.
   - SQL table functions: `ResolveSQLTableFunctions` passes 
`applySessionOverrides = true`.
   
   Scalar SQL function bodies are parsed in 
`SessionCatalog.makeSQLFunctionPlan` under `buildSQLFunctionConf(..., 
applySessionOverrides = false)`. If I'm reading it right, a caller's `false` 
has no effect there.
   
   Suggestions:
   - Switch to PERSISTED. If SESSION is intended, say in the migration guide 
that turning the flag off breaks views that use the new behavior.
   - Either way, add a persistent view and a SQL function to the SESSION-config 
`gridTest` in `AnalysisConfOverrideSuite`. Only a temp view is tested now.



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/unresolved.scala:
##########
@@ -800,6 +808,32 @@ case class UnresolvedStarExceptOrReplace(
 
     filterColumns(expandedCols, exceptPairs)
   }
+
+  /**
+   * Expands the star, replacing the columns that match an EXCEPT name and 
appending the
+   * replacements of the EXCEPT names that match no column, in EXCEPT list 
order.
+   */
+  private def expandStarAppendingMissingReplacements(
+      parameters: ExpandStarParameters): Seq[NamedExpression] = {
+    val replacementList = replacements match {
+      case Some(list) if list.size == excepts.size => list
+      case _ => throw SparkException.internalError(
+        "Appending missing replacements requires one replacement per except")
+    }
+    val expandedAttributes = 
AttributeSeq(super.expandStar(parameters).map(_.toAttribute))
+    val (presentExceptsAndReplacements, missingExceptsAndReplacements) =
+      excepts.zip(replacementList).partition { case (exceptParts, _) =>
+        expandedAttributes.resolve(exceptParts, parameters.resolver).isDefined
+      }
+    // The regular expansion replaces the present names. It resolves them a 
second time, which
+    // keeps the replacement logic in one place.
+    val expandedWithPresentColumnsReplaced = copy(
+      excepts = presentExceptsAndReplacements.map(_._1),
+      replacements = Some(presentExceptsAndReplacements.map(_._2)),
+      appendMissingReplacements = false
+    ).expandStar(parameters)
+    expandedWithPresentColumnsReplaced ++ 
missingExceptsAndReplacements.map(_._2)

Review Comment:
   This looks like it changes one query shape that worked before, which the PR 
description says shouldn't happen.
   
   When star expansion throws inside a subquery, `ResolveReferences.expand` in 
`Analyzer.scala` retries against the outer query's input and wraps the results 
in `OuterReference`. For example:
   
   ```sql
   -- o has a column z; t has a column a but no z.
   SELECT (TABLE t |> SET z = 1 |> LIMIT 1) FROM o;
   ```
   
   Before this change, expanding against `t` failed, so the fallback expanded 
`o`'s columns with `z` replaced, and the subquery returned the single column 
`z`. Now the expansion against `t` succeeds by appending `z`. The subquery 
returns `a, z` and fails the scalar subquery's one-column check.
   
   The shape is narrow. If the SET value references `t`'s columns, 
`wrapOuterReference` rewrites those references too, so I'd expect such queries 
to have failed before as well. The new behavior is arguably the right one.
   
   I haven't run this. Could you add a test with `|> SET` inside a subquery to 
pin the behavior? If it does change, please also update the PR description's 
"queries that worked before return the same results" line.



-- 
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]

Reply via email to