raviston opened a new pull request, #60494:
URL: https://github.com/apache/spark/pull/60494

   ### What changes were proposed in this pull request?
   
   ORC aggregate push-down (`spark.sql.orc.aggregatePushdown=true`, DSv2 scan) 
answers MIN, MAX and COUNT from the column statistics in the file footer. 
`OrcUtils.createAggInternalRowFromFooter` looked up the statistics of a column 
at the column's position in the data schema, but the statistics follow the 
columns of the ORC file's own schema. The ORC reader instead finds a column 
with `OrcUtils.requestedColumnIds`, by name or by position.
   
   This PR makes the footer lookup find the column the way the reader does:
   - `createAggInternalRowFromFooter` calls `requestedColumnIds` for the 
aggregated data columns. So it fails in the same cases as the reader, e.g. on 
an ambiguous case-insensitive match.
   - **By position**: for a file with `_colN` names (written by an old version 
of Hive) or with `orc.force.positional.evolution`, it uses the index that 
`requestedColumnIds` returns, which is the column's position in the file schema.
   - **By name**: otherwise, it finds the file column by its exact name in 
case-sensitive mode, and by its name lower-cased with `Locale.ROOT` otherwise, 
as `requestedColumnIds` does.
   - **Missing column**: a column that the file does not have is all null, so 
its COUNT is 0 and its MIN and MAX are null.
   - The lookup is built lazily, so `COUNT(*)` does not use it.
   
   `OrcScanBuilder.pushAggregation` also no longer pushes down aggregates of a 
column with an existence default. For a file without the column, the reader 
returns the default (see `OrcDeserializer`), which the footer does not know 
about. The docs of `spark.sql.orc.aggregatePushdown` now say so.
   
   ### Why are the changes needed?
   
   Push-down returned the statistics of another column when the data schema 
orders or names its columns differently from the file:
   
   ```python
   spark.conf.set("spark.sql.sources.useV1SourceList", "")
   spark.sql("SELECT * FROM VALUES (1, 10, 100), (2, 20, 200) AS t(x, a, 
b)").coalesce(1).write.orc("/tmp/t")
   
   spark.conf.set("spark.sql.orc.aggregatePushdown", "true")
   df = spark.read.schema("b INT, a INT, z INT").orc("/tmp/t")
   df.selectExpr("min(b)").collect()    # 1, expected 100 (from x)
   df.selectExpr("count(z)").collect()  # 2, expected 0 (from b; z is not in 
the file)
   df.selectExpr("max(z)").collect()    # 200, expected NULL
   ```
   
   With `spark.sql.orc.aggregatePushdown=false`, the queries return `100`, `0` 
and `NULL`. A data-schema position past the file's last column failed with 
`IndexOutOfBoundsException`. The same positional lookup was fixed for Parquet 
in SPARK-60598.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, with `spark.sql.orc.aggregatePushdown=true` and the DSv2 ORC source:
   - MIN, MAX and COUNT now return the same results as without push-down when 
the data schema orders or names columns differently from the file, or has 
columns that the file does not have. Before, they could return the statistics 
of another column, or fail with `IndexOutOfBoundsException`.
   - An ambiguous case-insensitive match now fails with the reader's error, 
`_LEGACY_ERROR_TEMP_2093`.
   - Aggregates of a column with an existence default are no longer pushed down.
   
   The same lookup is in branch-3.5 and later. ORC aggregate push-down was 
added in 3.3.0.
   
   ### How was this patch tested?
   
   New tests in `OrcV2AggregatePushDownSuite`. Most run each query both with 
and without push-down, and check that it is pushed down and that both give the 
same answer:
   - `SPARK-61499: aggregate push-down with a user schema that differs from the 
file schema`: reordered columns, and a column that the file does not have, past 
its last column.
   - `SPARK-61499: aggregate push-down matches columns by position like the 
reader`: a `_colN` file, and `orc.force.positional.evolution` set to true and 
to false.
   - `SPARK-61499: aggregate push-down matches column names like the reader`: a 
case-insensitive match, and `id` against `ıd` (dotless i), which 
`String.equalsIgnoreCase` treats as equal but lower-casing with `Locale.ROOT` 
does not.
   - `SPARK-61499: aggregate push-down in case-sensitive mode`.
   - `SPARK-61499: aggregate push-down fails on an ambiguous case-insensitive 
column match`: fails in the same way with and without push-down.
   - `SPARK-61499: aggregate push-down is not used for a column with an 
existence default`.
   
   All six fail on master. I also changed each branch of the new code 
separately (always match by name, always by position, `equalsIgnoreCase`, 
ignore case-sensitive mode, a non-zero COUNT for a missing column, keep 
partition columns in the lookup, case-sensitive `requestedColumnIds`, keep 
pushing down with an existence default), and each change fails at least one 
test.
   
   These pass locally (JDK 21):
   - `build/sbt 'sql/testOnly *OrcV1AggregatePushDownSuite 
*OrcV2AggregatePushDownSuite *ParquetV1AggregatePushDownSuite 
*ParquetV2AggregatePushDownSuite *OrcV1QuerySuite *OrcV2QuerySuite 
*OrcSourceV1Suite *OrcSourceV2Suite *OrcPartitionReaderFactorySuite 
*OrcReadSchemaSuite *OrcV1SchemaPruningSuite *OrcV2SchemaPruningSuite 
*OrcColumnarBatchReaderSuite *OrcV1FilterSuite *OrcFilterSuite *SQLConfSuite'`
   - `sql/scalastyle`, `sql/Test/scalastyle` and `catalyst/scalastyle`.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Code (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