raviston commented on code in PR #60084:
URL: https://github.com/apache/spark/pull/60084#discussion_r4545715626
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/parquet/ParquetUtils.scala:
##########
@@ -323,12 +324,30 @@ object ParquetUtils extends Logging {
private[sql] def getPushedDownAggResult(
footer: ParquetMetadata,
filePath: String,
- dataSchema: StructType,
+ isCaseSensitive: Boolean,
partitionSchema: StructType,
aggregation: Aggregation)
: (Array[PrimitiveType], Array[Any]) = {
- val footerFileMetaData = footer.getFileMetaData
- val fields = footerFileMetaData.getSchema.getFields
+ // The column chunks of a row group follow the leaf columns of the file
schema, and a nested
+ // column has one chunk per leaf, so a column's position among the
top-level fields is not
+ // the index of its chunk. The file may also order its columns differently
from the data
+ // schema. Aggregates are only pushed down for top-level primitive
columns, so find the
+ // chunk index by matching the column name against the top-level leaves of
the file schema.
+ // Returns -1 if the file does not have the column.
+ val leafColumns = footer.getFileMetaData.getSchema.getColumns
+ val columnIndexes = mutable.HashMap.empty[String, Int]
+ def columnIndex(colName: String): Int =
columnIndexes.getOrElseUpdate(colName, {
+ val matches = (0 until leafColumns.size).filter { i =>
+ val path = leafColumns.get(i).getPath
+ path.length == 1 &&
+ (if (isCaseSensitive) path.head == colName else
path.head.equalsIgnoreCase(colName))
Review Comment:
Thanks, confirmed. The lookup now keys the top-level fields of the file
schema the way `ParquetReadSupport.clipParquetGroupFields` does: the exact name
in case-sensitive mode, and `getName.toLowerCase(Locale.ROOT)` otherwise, with
the same `_LEGACY_ERROR_TEMP_2093` when more than one field matches.
The new test `SPARK-60598: aggregate push-down matches column names in the
same way as the reader` covers the three cases:
- A file Spark wrote with `id` and `ıd` gives `max(id) = 2` and `max(ıd) =
4` with push-down.
- A file column `i̇d` read as `İd` gives `count = 2`, `min = 1` and `max =
3`.
- A file column `ſum` read as `sum` counts no rows and fails MIN/MAX with
`NO_MIN_MAX`, as for any missing column.
On the previous head, the test fails at the first case with the
duplicate-field error. By reading the code, it also treats `i̇d` as missing and
matches `ſum` to `sum`.
The test builds the names from code points, so the source stays ASCII. All
three cases run with both readers.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/parquet/ParquetUtils.scala:
##########
@@ -323,12 +324,30 @@ object ParquetUtils extends Logging {
private[sql] def getPushedDownAggResult(
footer: ParquetMetadata,
filePath: String,
- dataSchema: StructType,
+ isCaseSensitive: Boolean,
partitionSchema: StructType,
aggregation: Aggregation)
: (Array[PrimitiveType], Array[Any]) = {
- val footerFileMetaData = footer.getFileMetaData
- val fields = footerFileMetaData.getSchema.getFields
+ // The column chunks of a row group follow the leaf columns of the file
schema, and a nested
+ // column has one chunk per leaf, so a column's position among the
top-level fields is not
+ // the index of its chunk. The file may also order its columns differently
from the data
+ // schema. Aggregates are only pushed down for top-level primitive
columns, so find the
+ // chunk index by matching the column name against the top-level leaves of
the file schema.
+ // Returns -1 if the file does not have the column.
+ val leafColumns = footer.getFileMetaData.getSchema.getColumns
+ val columnIndexes = mutable.HashMap.empty[String, Int]
+ def columnIndex(colName: String): Int =
columnIndexes.getOrElseUpdate(colName, {
+ val matches = (0 until leafColumns.size).filter { i =>
+ val path = leafColumns.get(i).getPath
+ path.length == 1 &&
Review Comment:
Done. Groups are now part of the match, as in the reader:
- A file with a primitive `a` and a struct `A`, read as `a LONG` in a
case-insensitive session, fails with `_LEGACY_ERROR_TEMP_2093` (`[a, A]`). The
read fails with the same error.
- A file whose `a` is a struct, read as `a LONG`, fails with
`FAILED_READ_FILE.PARQUET_COLUMN_DATA_TYPE_MISMATCH` (`Data type mismatches
when reading Parquet column [a]. Expected Spark type bigint, actual Parquet
type required group a { required int64 x; }`) for both COUNT and MAX. The
footer code throws the reader's `SchemaColumnConvertNotSupportedException`,
which `FileDataSourceV2.attachFilePath` turns into that error with the file
path. That is not a `PARQUET_AGGREGATE_PUSH_DOWN_UNSUPPORTED` subclass, so it
doesn't suggest disabling push-down. (The ordinary read of that file currently
fails with a `ClassCastException`.)
Both cases are in `SPARK-60598: aggregate push-down with a group column of
the same name`, which checks that the aggregates are pushed before checking the
error.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/parquet/ParquetUtils.scala:
##########
@@ -323,12 +324,30 @@ object ParquetUtils extends Logging {
private[sql] def getPushedDownAggResult(
footer: ParquetMetadata,
filePath: String,
- dataSchema: StructType,
+ isCaseSensitive: Boolean,
partitionSchema: StructType,
aggregation: Aggregation)
: (Array[PrimitiveType], Array[Any]) = {
- val footerFileMetaData = footer.getFileMetaData
- val fields = footerFileMetaData.getSchema.getFields
+ // The column chunks of a row group follow the leaf columns of the file
schema, and a nested
+ // column has one chunk per leaf, so a column's position among the
top-level fields is not
+ // the index of its chunk. The file may also order its columns differently
from the data
+ // schema. Aggregates are only pushed down for top-level primitive
columns, so find the
+ // chunk index by matching the column name against the top-level leaves of
the file schema.
+ // Returns -1 if the file does not have the column.
+ val leafColumns = footer.getFileMetaData.getSchema.getColumns
+ val columnIndexes = mutable.HashMap.empty[String, Int]
+ def columnIndex(colName: String): Int =
columnIndexes.getOrElseUpdate(colName, {
+ val matches = (0 until leafColumns.size).filter { i =>
Review Comment:
Done. One map of the file's top-level fields
(`fileFields.indices.groupBy(lookupKey)`) and the first leaf index of each
field (`scanLeft` over the leaf counts) are built once per file. Each
aggregated column is then a single map lookup. Both are `lazy`, so `COUNT(*)`
doesn't build them, as sunchao suggested.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/parquet/ParquetScanBuilder.scala:
##########
@@ -95,7 +95,29 @@ case class ParquetScanBuilder(
}
override def pushAggregation(aggregation: Aggregation): Boolean = {
- if (!sparkSession.sessionState.conf.parquetAggregatePushDown) {
+ val sqlConf = sparkSession.sessionState.conf
+ if (!sqlConf.parquetAggregatePushDown) {
+ return false
+ }
+ // With field ID reading, the reader matches columns by field ID rather
than by name (see
+ // `ParquetReadSupport.clipParquetGroupFields`), but the footer statistics
are looked up by
+ // name, so the two could pick different columns.
+ if (sqlConf.parquetFieldIdReadEnabled &&
ParquetUtils.hasFieldIds(dataSchema)) {
+ return false
+ }
+ // The footer only has statistics for leaf columns, and the null count of
a leaf of a nested
+ // column is not the number of nulls of the column itself: a non-null
struct can have a null
+ // field, and an empty array or map has no leaf value. So COUNT of a
nested column cannot be
+ // answered from the footer.
+ val countsNestedColumn = aggregation.aggregateExpressions.exists {
+ case count: Count => V2ColumnUtils.extractV2Column(count.column).exists
{ colName =>
+ // `colName` is the name of the resolved column, so match it exactly:
`getFieldIndex` is
+ // case-insensitive and could pick another column that differs only in
case.
Review Comment:
You're right, `getFieldIndex` is exact. The comment now says: "`colName` is
the name of the resolved column, as it appears in the table schema, so match it
exactly." The same lookup is now also used for the new existence-default check.
##########
sql/core/src/test/scala/org/apache/spark/sql/execution/datasources/FileSourceAggregatePushDownSuite.scala:
##########
@@ -773,17 +779,145 @@ class ParquetV2AggregatePushDownSuite extends
ParquetAggregatePushDownSuite {
}
}
+ test("SPARK-60598: aggregate push-down reads the right column after a nested
column") {
+ Seq("false", "true").foreach { enableVectorizedReader =>
+ withTempPath { dir =>
+ val path = dir.getCanonicalPath
+ // `s`, `arr` and `m` have several leaf column chunks each, so a later
column's
Review Comment:
Fixed: "`s` and `m` have two leaf column chunks each (and `arr` one), so a
later column's position among the top-level fields is not its position among
the column chunks."
--
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]