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

   ### What changes were proposed in this pull request?
   
   This PR aims to analyze the existence default values at most once per JSON 
struct converter, `ParquetRowConverter` and `OrcDeserializer`, instead of two 
to four times per row.
   
   - `ResolveDefaultColumns.resetExistenceDefaultsBitmask` and 
`applyExistenceDefaultValuesToRow` take the result of 
`existenceDefaultValues(schema)` instead of the schema.
   - `ResolveDefaultColumns.getExistenceDefaultsBitmask` uses `schema.length` 
instead of analyzing the default values only to count them.
   - `JacksonParser` computes `existenceDefaultValues(st)` in a `lazy val` next 
to `fieldConverters` of each struct converter, and passes it to 
`convertObject`. So, it is still computed only when the first JSON object of 
that struct is parsed.
   - `ParquetRowConverter` and `OrcDeserializer` keep 
`existenceDefaultValues(schema)` in a field. Their constructors already 
computed it.
   - `ResolveDefaultColumns.hasExistenceDefaultValues` is removed because it 
has no caller anymore.
   
   ### 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-L553
   
   However, the helpers for applying the existence default values to a row call 
it on each call: twice in `applyExistenceDefaultValuesToRow` (directly and via 
`hasExistenceDefaultValues`), and once in each of 
`resetExistenceDefaultsBitmask` and `getExistenceDefaultsBitmask`.
   
   
https://github.com/apache/spark/blob/d157ec95b9b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/ResolveDefaultColumnsUtil.scala#L491-L519
   
   And, their callers run per row.
   
   - `JacksonParser.convertObject` calls all three for each JSON object, i.e., 
4 analyses of every existence default value per object. This is used by the 
JSON data source and `from_json`.
     
https://github.com/apache/spark/blob/d157ec95b9b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/json/JacksonParser.scala#L603-L604
     
https://github.com/apache/spark/blob/d157ec95b9b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/json/JacksonParser.scala#L633
   - `ParquetRowConverter.currentRecord` and `OrcDeserializer.deserialize` call 
`applyExistenceDefaultValuesToRow` for each row, i.e., 2 analyses per row.
     
https://github.com/apache/spark/blob/d157ec95b9b/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/parquet/ParquetRowConverter.scala#L198-L199
     
https://github.com/apache/spark/blob/d157ec95b9b/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/orc/OrcDeserializer.scala#L88
     
https://github.com/apache/spark/blob/d157ec95b9b/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/orc/OrcDeserializer.scala#L105
   
   So, for a table with `N` columns with an existence default, every row pays 
`4 x N` (JSON) or `2 x N` (Parquet and ORC) analyses. This happens for every 
file, including the files that already contain all columns. Parquet and ORC use 
these row-based readers when the vectorized reader is disabled, or when the 
query reads more than `spark.sql.codegen.maxFields` (default: 100) fields.
   
   This is a performance regression since SPARK-44271 (Apache Spark 3.5.0). 
Before that, `StructType` cached the existence default values in `lazy val`s, 
and these helpers used them. SPARK-44271 moved these helpers into 
`ResolveDefaultColumns` without the caching.
   
   - [v3.4.0 
(2023-04-07)](https://github.com/apache/spark/blob/v3.4.0/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/ResolveDefaultColumnsUtil.scala#L282-L299):
 `schema.existenceDefaultValues` and `schema.hasExistenceDefaultValues` with 
[`private[sql] lazy val existenceDefaultValues: Array[Any] = 
getExistenceDefaultValues(this)`](https://github.com/apache/spark/blob/v3.4.0/sql/catalyst/src/main/scala/org/apache/spark/sql/types/StructType.scala#L522-L524)
   - [v3.5.0 
(2023-09-09)](https://github.com/apache/spark/blob/v3.5.0/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/ResolveDefaultColumnsUtil.scala#L384-L404):
 `existenceDefaultValues(schema)` and `hasExistenceDefaultValues(schema)` on 
each call
   
   ### Does this PR introduce _any_ user-facing change?
   
   No. The query results are the same. Only reading JSON tables, and Parquet 
and ORC tables with the row-based readers, becomes faster when the table has 
columns with existence default values.
   
   ### How was this patch tested?
   
   Pass the CIs with the existing tests. `InsertSuite` (`INSERT rows, ALTER 
TABLE ADD COLUMNS with DEFAULTs, then SELECT them`) reads JSON, ORC and Parquet 
tables, with and without the vectorized readers, after `ALTER TABLE ... ADD 
COLUMNS ... DEFAULT ...`. It checks the existence default values of many data 
types, and the explicit `NULL` values that must not be replaced by the 
defaults. So, it covers all changed per-row paths with `DEFAULT` values. I also 
confirmed that this test fails for each of `JacksonParser`, 
`ParquetRowConverter` and `OrcDeserializer` when its per-row call gets an array 
of nulls instead of the cached default values.
   
   In addition, I measured it manually. Each table is written as 4 files of 
2,500 rows each.
   - Old files: the table has one `INT` column when it is written, and then 50 
`INT` columns are added by `ALTER TABLE ... ADD COLUMNS`, with or without 
`DEFAULT`.
   - New files: the table is created with all 51 columns, with or without 
`DEFAULT`, and the files contain all columns.
   
   The query reads all columns via the `noop` data source. Parquet and ORC use 
the row-based readers (`spark.sql.parquet.enableVectorizedReader=false` and 
`spark.sql.orc.enableVectorizedReader=false`). 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(c0 INT) USING json")
   sql("INSERT INTO t SELECT /*+ REPARTITION(4) */ CAST(id AS INT) FROM 
range(10000)")
   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 | Files | Without `DEFAULT`, before -> after (ms) | With `DEFAULT`, 
before -> after (ms) |
   | - | - | -: | -: |
   | JSON | old | 41 -> 36 | 1,586 -> 26 |
   | JSON | new | 43 -> 39 | 1,624 -> 37 |
   | Parquet (row-based) | old | 33 -> 25 | 808 -> 24 |
   | Parquet (row-based) | new | 38 -> 32 | 821 -> 30 |
   | ORC (row-based) | old | 27 -> 22 | 816 -> 31 |
   | ORC (row-based) | new | 32 -> 41 | 880 -> 27 |
   
   After this PR, the tables with `DEFAULT` columns are read as fast as the 
tables without them. The numbers without `DEFAULT` are within the noise.
   
   ### 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