dongjoon-hyun opened a new pull request, #60702:
URL: https://github.com/apache/spark/pull/60702

   ### What changes were proposed in this pull request?
   
   This PR aims to analyze the existence default values once per `initBatch` 
call in the vectorized Parquet and ORC readers, instead of once per column.
   
   - `VectorizedParquetRecordReader.initBatch` computes 
`ResolveDefaultColumns.existenceDefaultValues(sparkRequestedSchema)` once 
before the per-column loop and indexes into the array.
   - `OrcColumnarBatchReader.initBatch` computes 
`ResolveDefaultColumns.existenceDefaultValues(requiredSchema)` at most once, 
when it meets the first missing column, and indexes into the array.
   
   ### Why are the changes needed?
   
   `ResolveDefaultColumns.existenceDefaultValues(schema)` parses and analyzes 
the `EXISTS_DEFAULT` metadata of every field of the schema on each call. Its 
documentation already says the caller should avoid using it in a loop.
   
   
https://github.com/apache/spark/blob/d157ec95b9b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/ResolveDefaultColumnsUtil.scala#L544-L549
   
   However, both vectorized readers call it inside a per-column loop and use 
only the `i`-th element. So, each file split analyzes `(number of columns) x 
(number of columns with an existence default)` default values.
   
   - The Parquet reader calls it for every top-level column.
   - The ORC reader calls it for every missing column.
   
   This is a performance regression since SPARK-44271 (Apache Spark 3.5.0). 
Before that, `StructType.existenceDefaultValues` was a `lazy val`, so the same 
loops analyzed the default values once per schema. SPARK-44271 moved these 
helpers into `ResolveDefaultColumns` without the caching, while these loops 
kept the per-column calls.
   
   - [v3.4.0 
(2023-04-07)](https://github.com/apache/spark/blob/v3.4.0/sql/catalyst/src/main/scala/org/apache/spark/sql/types/StructType.scala#L522):
 `private[sql] lazy val existenceDefaultValues: Array[Any] = 
getExistenceDefaultValues(this)`
   - [v3.5.0 
(2023-09-09)](https://github.com/apache/spark/blob/v3.5.0/sql/core/src/main/java/org/apache/spark/sql/execution/datasources/parquet/VectorizedParquetRecordReader.java#L284):
 `defaultValue = 
ResolveDefaultColumns.existenceDefaultValues(sparkRequestedSchema)[i];`
   
   ### Does this PR introduce _any_ user-facing change?
   
   No. The query results are the same. Only the time to initialize each file 
split is reduced when a table has columns with existence default values, e.g., 
columns added by `ALTER TABLE ... ADD COLUMNS ... DEFAULT ...`.
   
   ### How was this patch tested?
   
   Pass the CIs with the existing tests, e.g., `InsertSuite` (SPARK-38811, 
SPARK-39557), `OrcColumnarBatchReaderSuite`, and `ParquetStorageFilterSuite` 
(existence DEFAULT tests).
   
   In addition, I measured it manually. Each table has one `INT` column written 
as 200 files, and then 50 `INT` columns are added by `ALTER TABLE ... ADD 
COLUMNS`, with or without `DEFAULT`. The query reads all columns of the old 
files via the `noop` data source. Each number is the median of 7 runs after 3 
warm-up runs (Apple M3 Max, JDK 21, `local[2]`).
   
   ```scala
   sql("CREATE TABLE t(id INT) USING parquet")
   spark.range(200).selectExpr("CAST(id AS 
INT)").repartition(200).write.insertInto("t")
   sql(s"ALTER TABLE t ADD COLUMNS (${(1 to 50).map(i => s"c$i INT DEFAULT 
$i").mkString(", ")})")
   spark.table("t").write.format("noop").mode("overwrite").save()
   ```
   
   | Format | Added columns | Before (ms) | After (ms) |
   | - | - | -: | -: |
   | Parquet | without `DEFAULT` | 97 | 98 |
   | Parquet | with `DEFAULT` | 489 | 96 |
   | ORC | without `DEFAULT` | 127 | 128 |
   | ORC | with `DEFAULT` | 497 | 123 |
   
   After this PR, reading the columns with `DEFAULT` takes about the same time 
as reading the columns without it.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Opus 5.5
   


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