raviston commented on code in PR #60084:
URL: https://github.com/apache/spark/pull/60084#discussion_r4545716676
##########
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
+ // position among the top-level fields is not its position among the
column chunks.
+ Seq(((100, 200), Seq(7, 8), Map(9 -> 10), Some(1), 2, true),
+ ((300, 400), Seq(11), Map(12 -> 13), None, 6, false),
+ ((500, 600), Seq.empty[Int], Map.empty[Int, Int], Some(5), 4,
false))
+ .toDF("s", "arr", "m", "a", "b", "c")
+ .coalesce(1).write.parquet(path)
+ withTempView("t") {
+ spark.read.parquet(path).createOrReplaceTempView("t")
+ withSQLConf(
+ aggPushDownEnabledKey -> "true",
+ vectorizedReaderEnabledKey -> enableVectorizedReader) {
+ val df = sql("SELECT max(a), min(b), count(a), max(c), min(c) FROM
t")
+ checkPushedInfo(df, "PushedAggregation: [MAX(a), MIN(b), COUNT(a),
MAX(c), MIN(c)]")
+ checkAnswer(df, Row(5, 2, 2, true, false))
+ }
+ }
+ }
+ }
+ }
+
+ test("SPARK-60598: COUNT of a nested column is not pushed down") {
+ Seq("false", "true").foreach { enableVectorizedReader =>
+ withTempPath { dir =>
+ val path = dir.getCanonicalPath
+ // The footer only has statistics for the leaf columns. A non-null
struct whose first
+ // field is null, and an empty array, are both null in the first leaf
column, so its
+ // null count is not the number of null values of the column.
+ sql("SELECT named_struct('x', CAST(NULL AS INT), 'y', 1) AS s, array()
AS arr, " +
+ "map(1, 2) AS m " +
+ "UNION ALL SELECT NULL, NULL, NULL " +
+ "UNION ALL SELECT named_struct('x', 3, 'y', 4), array(5), map()")
+ .coalesce(1).write.parquet(path)
+ withTempView("t") {
+ spark.read.parquet(path).createOrReplaceTempView("t")
+ withSQLConf(
+ aggPushDownEnabledKey -> "true",
+ vectorizedReaderEnabledKey -> enableVectorizedReader) {
+ val df = sql("SELECT count(s), count(arr), count(m) FROM t")
+ checkPushedInfo(df, "PushedAggregation: []")
+ checkAnswer(df, Row(2, 2, 2))
+ }
+ }
+ }
+ }
+ }
+
+ test("SPARK-60598: aggregate push-down with a user schema that differs from
the file schema") {
+ Seq("false", "true").foreach { enableVectorizedReader =>
+ withTempPath { dir =>
+ val path = dir.getCanonicalPath
+ Seq((1, 10L, 100), (2, 20L, 200)).toDF("x", "a",
"b").coalesce(1).write.parquet(path)
+ withTempView("t") {
+ // The data schema lists `b` (as `B`) before `a`, leaves out `x`,
and adds `z`, which
+ // the file does not have.
+ spark.read.schema("B INT, a LONG, z
INT").parquet(path).createOrReplaceTempView("t")
+ withSQLConf(
+ aggPushDownEnabledKey -> "true",
+ vectorizedReaderEnabledKey -> enableVectorizedReader) {
+ val df = sql("SELECT max(a), min(B), count(B), count(z) FROM t")
+ checkPushedInfo(df, "PushedAggregation: [MAX(a), MIN(B), COUNT(B),
COUNT(z)]")
+ checkAnswer(df, Row(20L, 100, 2, 0))
+ // A column the file does not have is all null, so it has no min
or max either.
+ Seq("min", "max").foreach { agg =>
+ checkErrorMatchPVals(
+ exception = interceptAggPushDownError(s"SELECT $agg(z) FROM
t"),
+ condition =
"PARQUET_AGGREGATE_PUSH_DOWN_UNSUPPORTED.NO_MIN_MAX",
+ parameters = Map("filePath" -> ".*", "config" ->
aggPushDownEnabledKey))
+ }
+ }
+ }
+ }
+ }
+ }
+
+ test("SPARK-60598: aggregate push-down fails on an ambiguous
case-insensitive column match") {
+ withTempPath { dir =>
+ val path = dir.getCanonicalPath
+ withSQLConf(SQLConf.CASE_SENSITIVE.key -> "true") {
+ spark.range(3).selectExpr("id AS a", "id * 2 AS
A").coalesce(1).write.parquet(path)
+ }
+ withTempView("t") {
+ spark.read.schema("a LONG").parquet(path).createOrReplaceTempView("t")
+ withSQLConf(aggPushDownEnabledKey -> "true") {
+ // The reader fails in the same way for this file (see
`ParquetReadSupport`).
+ val e = interceptCause[SparkRuntimeException]("SELECT max(a) FROM t")
Review Comment:
Done. The test now runs `checkPushedInfo(sql("SELECT max(a) FROM t"),
"PushedAggregation: [MAX(a)]")` before checking the error. So does the new
group-column test.
##########
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
+ // position among the top-level fields is not its position among the
column chunks.
+ Seq(((100, 200), Seq(7, 8), Map(9 -> 10), Some(1), 2, true),
+ ((300, 400), Seq(11), Map(12 -> 13), None, 6, false),
+ ((500, 600), Seq.empty[Int], Map.empty[Int, Int], Some(5), 4,
false))
+ .toDF("s", "arr", "m", "a", "b", "c")
+ .coalesce(1).write.parquet(path)
+ withTempView("t") {
+ spark.read.parquet(path).createOrReplaceTempView("t")
+ withSQLConf(
+ aggPushDownEnabledKey -> "true",
+ vectorizedReaderEnabledKey -> enableVectorizedReader) {
+ val df = sql("SELECT max(a), min(b), count(a), max(c), min(c) FROM
t")
+ checkPushedInfo(df, "PushedAggregation: [MAX(a), MIN(b), COUNT(a),
MAX(c), MIN(c)]")
+ checkAnswer(df, Row(5, 2, 2, true, false))
+ }
+ }
+ }
+ }
+ }
+
+ test("SPARK-60598: COUNT of a nested column is not pushed down") {
+ Seq("false", "true").foreach { enableVectorizedReader =>
+ withTempPath { dir =>
+ val path = dir.getCanonicalPath
+ // The footer only has statistics for the leaf columns. A non-null
struct whose first
+ // field is null, and an empty array, are both null in the first leaf
column, so its
+ // null count is not the number of null values of the column.
+ sql("SELECT named_struct('x', CAST(NULL AS INT), 'y', 1) AS s, array()
AS arr, " +
+ "map(1, 2) AS m " +
+ "UNION ALL SELECT NULL, NULL, NULL " +
+ "UNION ALL SELECT named_struct('x', 3, 'y', 4), array(5), map()")
+ .coalesce(1).write.parquet(path)
+ withTempView("t") {
+ spark.read.parquet(path).createOrReplaceTempView("t")
+ withSQLConf(
+ aggPushDownEnabledKey -> "true",
+ vectorizedReaderEnabledKey -> enableVectorizedReader) {
+ val df = sql("SELECT count(s), count(arr), count(m) FROM t")
+ checkPushedInfo(df, "PushedAggregation: []")
+ checkAnswer(df, Row(2, 2, 2))
+ }
+ }
+ }
+ }
+ }
+
+ test("SPARK-60598: aggregate push-down with a user schema that differs from
the file schema") {
+ Seq("false", "true").foreach { enableVectorizedReader =>
+ withTempPath { dir =>
+ val path = dir.getCanonicalPath
+ Seq((1, 10L, 100), (2, 20L, 200)).toDF("x", "a",
"b").coalesce(1).write.parquet(path)
+ withTempView("t") {
+ // The data schema lists `b` (as `B`) before `a`, leaves out `x`,
and adds `z`, which
+ // the file does not have.
+ spark.read.schema("B INT, a LONG, z
INT").parquet(path).createOrReplaceTempView("t")
+ withSQLConf(
+ aggPushDownEnabledKey -> "true",
+ vectorizedReaderEnabledKey -> enableVectorizedReader) {
+ val df = sql("SELECT max(a), min(B), count(B), count(z) FROM t")
+ checkPushedInfo(df, "PushedAggregation: [MAX(a), MIN(B), COUNT(B),
COUNT(z)]")
+ checkAnswer(df, Row(20L, 100, 2, 0))
+ // A column the file does not have is all null, so it has no min
or max either.
+ Seq("min", "max").foreach { agg =>
+ checkErrorMatchPVals(
+ exception = interceptAggPushDownError(s"SELECT $agg(z) FROM
t"),
+ condition =
"PARQUET_AGGREGATE_PUSH_DOWN_UNSUPPORTED.NO_MIN_MAX",
+ parameters = Map("filePath" -> ".*", "config" ->
aggPushDownEnabledKey))
+ }
+ }
+ }
+ }
+ }
+ }
+
+ test("SPARK-60598: aggregate push-down fails on an ambiguous
case-insensitive column match") {
+ withTempPath { dir =>
+ val path = dir.getCanonicalPath
+ withSQLConf(SQLConf.CASE_SENSITIVE.key -> "true") {
Review Comment:
Added `SPARK-60598: aggregate push-down in case-sensitive mode`. A file with
`a`, `A` and `b`, read with `spark.sql.caseSensitive=true` and the schema `a
LONG, A LONG, B LONG`, gives `max(a) = 2`, `max(A) = 10`, `count(A) = 1` and
`count(B) = 0` with `[MAX(a), MAX(A), COUNT(A), COUNT(B)]` pushed. It fails if
`ParquetPartitionReaderFactory` passes `false` instead of `isCaseSensitive`,
with the duplicate-field error.
##########
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
+ // position among the top-level fields is not its position among the
column chunks.
+ Seq(((100, 200), Seq(7, 8), Map(9 -> 10), Some(1), 2, true),
+ ((300, 400), Seq(11), Map(12 -> 13), None, 6, false),
+ ((500, 600), Seq.empty[Int], Map.empty[Int, Int], Some(5), 4,
false))
+ .toDF("s", "arr", "m", "a", "b", "c")
+ .coalesce(1).write.parquet(path)
+ withTempView("t") {
+ spark.read.parquet(path).createOrReplaceTempView("t")
+ withSQLConf(
+ aggPushDownEnabledKey -> "true",
+ vectorizedReaderEnabledKey -> enableVectorizedReader) {
+ val df = sql("SELECT max(a), min(b), count(a), max(c), min(c) FROM
t")
+ checkPushedInfo(df, "PushedAggregation: [MAX(a), MIN(b), COUNT(a),
MAX(c), MIN(c)]")
+ checkAnswer(df, Row(5, 2, 2, true, false))
Review Comment:
Done, in both places:
- The nested fixture now also has `count(b) = 3`. The chunk at `b`'s
top-level position, `m.key_value.value`, has a null from the empty map, while
`b` has none.
- In the user-schema test, `b` now has a null, so `count(B)` is 1. The chunk
at `B`'s data-schema position is `x`, which has no null and would give 2.
##########
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
+ // position among the top-level fields is not its position among the
column chunks.
+ Seq(((100, 200), Seq(7, 8), Map(9 -> 10), Some(1), 2, true),
+ ((300, 400), Seq(11), Map(12 -> 13), None, 6, false),
+ ((500, 600), Seq.empty[Int], Map.empty[Int, Int], Some(5), 4,
false))
+ .toDF("s", "arr", "m", "a", "b", "c")
+ .coalesce(1).write.parquet(path)
+ withTempView("t") {
+ spark.read.parquet(path).createOrReplaceTempView("t")
+ withSQLConf(
+ aggPushDownEnabledKey -> "true",
+ vectorizedReaderEnabledKey -> enableVectorizedReader) {
+ val df = sql("SELECT max(a), min(b), count(a), max(c), min(c) FROM
t")
+ checkPushedInfo(df, "PushedAggregation: [MAX(a), MIN(b), COUNT(a),
MAX(c), MIN(c)]")
+ checkAnswer(df, Row(5, 2, 2, true, false))
+ }
+ }
+ }
+ }
+ }
+
+ test("SPARK-60598: COUNT of a nested column is not pushed down") {
+ Seq("false", "true").foreach { enableVectorizedReader =>
+ withTempPath { dir =>
+ val path = dir.getCanonicalPath
+ // The footer only has statistics for the leaf columns. A non-null
struct whose first
+ // field is null, and an empty array, are both null in the first leaf
column, so its
+ // null count is not the number of null values of the column.
+ sql("SELECT named_struct('x', CAST(NULL AS INT), 'y', 1) AS s, array()
AS arr, " +
+ "map(1, 2) AS m " +
+ "UNION ALL SELECT NULL, NULL, NULL " +
+ "UNION ALL SELECT named_struct('x', 3, 'y', 4), array(5), map()")
+ .coalesce(1).write.parquet(path)
+ withTempView("t") {
+ spark.read.parquet(path).createOrReplaceTempView("t")
+ withSQLConf(
+ aggPushDownEnabledKey -> "true",
+ vectorizedReaderEnabledKey -> enableVectorizedReader) {
+ val df = sql("SELECT count(s), count(arr), count(m) FROM t")
Review Comment:
Done. The test now runs one query per column, `count(s)`, `count(arr)`,
`count(m)`, `count(v)` (a `parse_json` variant) and `count(u)`
(`TestUDT.MyDenseVectorUDT`, over an array), and each checks
`PushedAggregation: []` and the count. I checked that it fails when
`isNestedType` returns false for each of `ArrayType`, `MapType`, `VariantType`
and `UserDefinedType` in turn.
##########
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)) {
Review Comment:
Thanks, you're right. The guard now reads the flag from `hadoopConf` of the
scan builder, which `newHadoopConfWithOptions` builds from the session confs
and the options of the read, the same configuration that
`ParquetReadSupport.getRequestedSchema` reads it from. Your repro (`b BIGINT`
with ID 5, read as `a BIGINT` with ID 5 and the option, with the session flag
off) is in `SPARK-60598: aggregate push-down is not used with field ID
reading`. It returns `count(a) = 2`, `max(a) = 200` and `min(a) = 100` without
push-down, and fails on the previous head. (The test session has
`spark.sql.parquet.fieldId.read.enabled=true`, so the test turns it off
explicitly for this case.)
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/parquet/ParquetUtils.scala:
##########
@@ -368,9 +387,11 @@ object ParquetUtils extends Logging {
}
isCount = true
if (!isPartitionCol) {
- index = dataSchema.getFieldIndex(colName).getOrElse(-1)
- // Count(*) includes the null values, but Count(colName) doesn't.
- rowCount -= getNumNulls(filePath, blockMetaData, index)
+ index = columnIndex(colName)
+ // Count(*) includes the null values, but Count(colName)
doesn't. A column the
+ // file does not have is all null.
+ rowCount -= (if (index < 0) block.getRowCount
Review Comment:
Thanks for catching this. `pushAggregation` now declines when an aggregated
column (MIN, MAX or COUNT) has an existence default, since the reader returns
that default for a file without the column (`VectorizedParquetRecordReader`
passes `existenceDefaultValues(sparkRequestedSchema)` to `ParquetColumnVector`,
and `ParquetRowConverter` does the same) and the footer can't know it. Your
repro is `SPARK-60598: aggregate push-down is not used for a column with an
existence default`: `count(z) = 2` and `max(z) = 7`, with no push-down. The
docs of `spark.sql.parquet.aggregatePushdown` mention it.
##########
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
Review Comment:
Done. The lookup no longer calls `getColumns`. It uses `getFields` and
counts the leaves of each top-level field, and the map is `lazy`, so `COUNT(*)`
doesn't build it.
--
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]