dongjoon-hyun opened a new pull request, #60703: URL: https://github.com/apache/spark/pull/60703
### What changes were proposed in this pull request? This PR aims to analyze the existence default values at most once per `UnivocityParser` instance, instead of once per missing or malformed CSV token. `UnivocityParser` keeps `ResolveDefaultColumns.existenceDefaultValues(requiredSchema)` in a `lazy val`, and the per-token `catch` block of `convert` indexes into it. ### 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, `UnivocityParser.convert` calls it inside the per-token `catch` block and uses only the `i`-th element. https://github.com/apache/spark/blob/d157ec95b9b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/csv/UnivocityParser.scala#L438 This block runs for every row of the CSV files written before `ALTER TABLE ... ADD COLUMNS (... DEFAULT ...)`. Such a file has no token for the added columns, so `getToken` throws `ArrayIndexOutOfBoundsException` once per added column. So, each row of an old file analyzes `(number of missing columns) x (number of columns with an existence default)` default values, e.g., 2,500 analyses per row for 50 added columns with `DEFAULT`. This is a performance regression since SPARK-44271 (Apache Spark 3.5.0). Before that, `StructType.existenceDefaultValues` was a `lazy val`, so the same `catch` block analyzed the default values once per schema. SPARK-44271 moved these helpers into `ResolveDefaultColumns` without the caching, while this `catch` block kept the per-token call. - [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/csv/UnivocityParser.scala#L356): `row.update(i, requiredSchema.existenceDefaultValues(i))` 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) - [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/csv/UnivocityParser.scala#L356): `row.update(i, ResolveDefaultColumns.existenceDefaultValues(requiredSchema)(i))` ### Does this PR introduce _any_ user-facing change? No. The query results are the same. Only reading the CSV files that lack columns with existence default values, e.g., the files written before `ALTER TABLE ... ADD COLUMNS ... DEFAULT ...`, becomes faster. ### 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 old CSV files with and without CSV column pruning, and checks the existence default values of many data types. So, it covers this `catch` block with `DEFAULT` values. I also confirmed that this test fails when the `catch` block reads a wrong index of the cached array. In addition, I measured it manually. Each table has one `INT` column written as 4 files of 2,500 rows each, 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 csv") spark.range(10000).selectExpr("CAST(id AS INT)").repartition(4).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() ``` | Added columns | Before (ms) | After (ms) | | - | -: | -: | | without `DEFAULT` | 81 | 35 | | with `DEFAULT` | 20,775 | 63 | After this PR, reading the old files with `DEFAULT` columns takes tens of milliseconds, like reading them without `DEFAULT`. The case without `DEFAULT` also gets faster because the `catch` block no longer builds an array over all fields for each missing token. ### 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]
