dtenedor opened a new pull request, #60679:
URL: https://github.com/apache/spark/pull/60679
### What changes were proposed in this pull request?
JIRA: SPARK-NNNNN
When a vectorized Parquet or ORC scan reads a file written before `ALTER
TABLE ... ADD COLUMN ... DEFAULT ...`, it fills the missing column with its
existence default via `WritableColumnVector.appendObjects`. Two kinds of
default made that method fail, and the query failed with `FAILED_READ_FILE`:
1. **A decimal whose unscaled value does not fit in a long** (for example
`DECIMAL(38, 0) DEFAULT 99999999999999999999999999999999999999`), at the top
level or inside a struct or array. `appendObjects` called
`decimal.toUnscaledLong()` before checking the precision. That call throws
`ArithmeticException: BigInteger out of long range`, so the byte-array branch
for wide decimals added in SPARK-46056 never ran for values that overflow a
long.
2. **A NULL inside a struct or array default** (for example
`named_struct('tier', 1, 'nickname', CAST(NULL AS STRING))` or `array(1, NULL,
3)`). `appendObjects` had no branch for `null`, so it returned
`Optional.empty()`. Both callers (`ParquetColumnVector` and
`OrcColumnarBatchReader`) then threw `Cannot assign default column value ...`.
This PR:
- Calls `toUnscaledLong()` only on the int and long branches.
- Adds a `null` branch that appends NULLs, using `appendStruct(true)` for
struct-like child types. A small `isStructLike` helper, shared with
`appendStruct`, decides which types those are.
- Adds an internal legacy conf,
`spark.sql.legacy.vectorizedReader.failOnNullFieldOrWideDecimalDefault`
(default `false`). Setting it to `true` restores the old failure. The conf is
checked only when the default contains a NULL or a decimal with precision > 18,
so other defaults take the same path as before.
Map keys and values keep the existing behavior. The map branch of
`appendObjects` has a separate problem: it does not give each row its own
entries. That is tracked in SPARK-60940 (#60143), and extending this fix to
maps should wait for it.
### Why are the changes needed?
The row-based readers already return these defaults, so only the vectorized
readers fail. Any table that adds a column with such a default can no longer be
read by default, until the old files are rewritten. Repro:
```sql
CREATE TABLE ledger (id INT) USING PARQUET;
INSERT INTO ledger VALUES (1);
ALTER TABLE ledger ADD COLUMN cap DECIMAL(38,0) DEFAULT
99999999999999999999999999999999999999;
SELECT id, cap FROM ledger;
-- before: [FAILED_READ_FILE.NO_HINT] ... ArithmeticException: BigInteger
out of long range
-- after: [1, 99999999999999999999999999999999999999]
CREATE TABLE prefs (id INT) USING ORC;
INSERT INTO prefs VALUES (1);
ALTER TABLE prefs ADD COLUMN p STRUCT<tier: INT, nickname: STRING>
DEFAULT named_struct('tier', 1, 'nickname', CAST(NULL AS STRING));
SELECT * FROM prefs;
-- before: FAILED_READ_FILE ... Cannot assign default column value to result
column batch in vectorized Orc reader
-- after: [1, {1, null}]
```
### Does this PR introduce _any_ user-facing change?
Yes. A vectorized Parquet or ORC scan of a missing column whose default
holds, outside any map, a decimal that overflows a long (at the top level or
nested in a struct or array) or a NULL nested in a struct or array now returns
the default value instead of failing with `FAILED_READ_FILE`. Only queries that
used to fail change. The bug has been present since vectorized scans with
DEFAULT values were added in Spark 3.4 (SPARK-39265). The conf
`spark.sql.legacy.vectorizedReader.failOnNullFieldOrWideDecimalDefault=true`
restores the old behavior, and `docs/sql-migration-guide.md` has a new entry
under 4.4 to 5.0.
### How was this patch tested?
Added two tests to `InsertSuite`, each run for Parquet and ORC:
- `vectorized readers fill existence defaults with NULL fields and wide
decimals`: reads the default for a NULL struct field, a NULL array element, a
NULL nested struct, and decimals that overflow a long (positive, negative,
scale 30, and inside a struct and an array). As controls, it reads decimals
that fit in an int or a long, including a precision-38 decimal whose value fits
in a long. It also checks that map defaults, at the top level and inside a
struct, still fail with the old error.
- `... (legacy flag reproduces the old failure)`: with the legacy conf on,
every affected column fails with the old error and the control columns still
read correctly.
`build/sbt "sql/testOnly org.apache.spark.sql.sources.InsertSuite"` passes
all 90 tests, including the existing DEFAULT-value tests such as SPARK-39557.
With only the `WritableColumnVector` change reverted, the first new test fails
with `FAILED_READ_FILE.NO_HINT`. Scalastyle passes for `catalyst` and `sql`.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Isaac (Claude 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]