steveyzzz opened a new pull request, #60629:
URL: https://github.com/apache/spark/pull/60629
### What changes were proposed in this pull request?
This PR lets the SQL pipe operator `|> SET` add columns. An assignment to a
column that does not exist in the input table now appends the column after the
existing columns, as `|> EXTEND` does, instead of failing with
`UNRESOLVED_COLUMN`. Everything else about `SET` stays the same: an assignment
to an existing column replaces it in place, each assignment can refer to the
result of earlier ones, a name that matches more than one column still fails
with `AMBIGUOUS_REFERENCE`, and aggregate functions are still rejected.
```sql
-- t has the columns x INT and y STRING.
TABLE t
|> SET z = x + length(y), x = x * 10, zz = z + x;
-- Output columns: x, y, z, zz. x is replaced in place; z and zz are
appended.
```
- `UnresolvedStarExceptOrReplace` gets an `appendMissingReplacements` field,
default `false`. When it is set, star expansion appends the replacement of each
EXCEPT name that matches no column, in EXCEPT list order, instead of failing.
Names that match a column go through the existing replace logic. The
fixed-point analyzer and the single-pass resolver both expand stars through
`expandStar`, so both get the new behavior.
- `AstBuilder.visitOperatorPipeSet` sets the field unless the new internal
config `spark.sql.legacy.pipeSetRequiresExistingColumns` (default `false`) is
`true`. `|> DROP` and `SELECT * EXCEPT` don't set it.
- The config uses `ConfigBindingPolicy.SESSION`, like
`spark.sql.legacy.allowAliasedPipeAggregateOrdinals`: a view's definition is
resolved with the reading session's value.
- `docs/sql-pipe-syntax.md` describes the new behavior. The SET section
showed the same example twice; the second copy now shows an assignment that
adds a column.
- `docs/sql-migration-guide.md` describes the change and how to restore the
previous behavior.
### Why are the changes needed?
Today `|> SET c = ...` fails when the input has no column `c`, and `|>
EXTEND ... AS c` adds a second `c` when the input already has one. A query that
sets the same column on inputs with different schemas, such as when normalizing
data from several sources, therefore has to know each input's schema. With this
change one `SET` works on both: it replaces `c` where it exists and adds it
where it doesn't.
### Does this PR introduce _any_ user-facing change?
Yes. Previously, a `|> SET` assignment to a column that does not exist
failed:
```
TABLE t |> SET z = 1;
[UNRESOLVED_COLUMN.WITH_SUGGESTION] A column, variable, or function
parameter with name `z` cannot be resolved. Did you mean one of the following?
[`x`, `y`]. SQLSTATE: 42703
```
Now it appends the column:
```
TABLE t |> SET z = 1;
0 abc 1
1 def 1
```
Queries that worked before return the same results. Setting
`spark.sql.legacy.pipeSetRequiresExistingColumns` to `true` restores the
previous error, and `docs/sql-migration-guide.md` describes the change.
### How was this patch tested?
- New cases in `pipe-operators.sql`: appending one and several columns (in
assignment order), replacing and appending in one `SET` with references to
both, assigning a new column twice, case-insensitive and case-sensitive
matching between a new column and a later assignment, a backquoted name, values
from a struct field, a scalar subquery and a window function, a table alias
staying visible, the hidden `_metadata` column, the example from the docs, and
a temporary view, which fails with `UNRESOLVED_COLUMN` when read with the
legacy config on. The existing missing-column case now runs with the legacy
config on, and new cases cover the errors that remain (ambiguous name,
aggregate function). Regenerated `results/` and `analyzer-results/`.
- New `UnresolvedStarExceptOrReplaceSuite` expands one node that holds
several EXCEPT names, some matching columns and some not. `|> SET` builds one
node per assignment, so the SQL tests only reach the single-name case.
- Ran `SQLQueryTestSuite` (`pipe-operators.sql`),
`UnresolvedStarExceptOrReplaceSuite`, `StarTreePatternSuite`,
`RetainedResolutionConfigsSuite`, `SparkSqlParserSuite` ("Operator pipe SQL
syntax"), `SQLConfSuite`, `SQLConfEntrySuite`, `AnalysisConfOverrideSuite`,
`SparkConfigBindingPolicySuite`, and scalastyle for `catalyst` and `sql`.
- The single-pass resolver doesn't support pipe operators yet
(`ResolverGuard` rejects `PipeExpression`), so these queries run only on the
fixed-point analyzer. The new logic is in
`UnresolvedStarExceptOrReplace.expandStar`, which the single-pass resolver also
uses.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code, Opus 5.5
This pull request and its description were written by Isaac.
--
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]