spark-test commented on code in PR #60132:
URL: https://github.com/apache/spark/pull/60132#discussion_r4543296188
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/parquet/ParquetFilters.scala:
##########
@@ -1242,9 +1250,11 @@ class ParquetFilters(
}.reduceLeftOption(FilterApi.or)
} else if (canPartialPushDownConjuncts) {
if (values.contains(null)) {
- Seq(makeEq.lift(fieldType).map(_(fieldNames, null)),
- makeInPredicate.lift(fieldType).map(_(fieldNames,
values.filter(_ != null)))
- ).flatten.reduceLeftOption(FilterApi.or)
+ // Push both disjuncts or neither: either one alone is stricter
than the IN.
+ for {
+ eqNull <- makeEq.lift(fieldType).map(_(fieldNames, null))
+ in <- makeInPredicate.lift(fieldType).map(_(fieldNames,
values.filter(_ != null)))
Review Comment:
**Non-blocking (P2):** When every entry of the list is NULL,
`values.filter(_ != null)` is empty, and the new BOOLEAN case passes that empty
set to `FilterApi.in`, whose `SetColumnFilterPredicate` constructor in
parquet-java 1.18.1 throws `IllegalArgumentException("values in
SetColumnFilterPredicate shouldn't be empty!")`. OptimizeIn can be excluded
through `spark.sql.optimizer.excludedRules`, and once it is, nothing upstream
removes such a list: NullPropagation and ReplaceNullWithFalseInPredicate leave
`b IN (NULL, NULL)` alone, DataSourceStrategy keeps the duplicates, and
`canMakeFilterOn` accepts NULL values.
So, reading the code, with `spark.sql.parquet.pushdown.inFilterThreshold=1`
that query on a BOOLEAN column now fails, while planning in V2
(`ParquetScanBuilder.pushDataFilters` through `convertibleFilters`) and in the
scan task in V1, where the base branch pushed `eq(b, null)` alone and returned
the correct empty result. Every other type `makeInPredicate` covers already
fails this way on the base branch, and giving BOOLEAN a case extends it to
BOOLEAN. An all-NULL list matches no row, so pushing `eq(col, null)` alone when
no non-null value remains, as the below-threshold branch does for the same
list, avoids the failure for every type.
**Verification:**
- **Regression:** With OptimizeIn excluded and a threshold below the list
length, check that an IN list of only NULLs on a BOOLEAN column returns no rows
through V1 and V2 instead of throwing, and that the predicate built for it is
the IS NULL test alone.
- **Behavior:** Check that the same all-NULL list on a column of another
type, such as INT, builds the IS NULL test alone instead of throwing.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/parquet/ParquetFilters.scala:
##########
@@ -793,6 +793,14 @@ class ParquetFilters(
private val makeInPredicate:
PartialFunction[ParquetSchemaType, (Array[String], Array[Any]) =>
FilterPredicate] = {
+ case ParquetBooleanType =>
+ (n: Array[String], values: Array[Any]) =>
+ val set = new HashSet[JBoolean]()
+ for (value <- values) {
+ set.add(value.asInstanceOf[JBoolean])
+ }
+ FilterApi.in(booleanColumn(n), set)
Review Comment:
**Non-blocking (P2):** This case hands parquet-java a BOOLEAN `in`, and
parquet-java 1.18.1's bloom-filter row-group level cannot evaluate one.
`BloomFilterImpl.visit(In)` calls `bloomFilter.hash(value)` with no try/catch,
unlike `visit(Eq)`, which catches the failure and answers might-match, and
`BlockSplitBloomFilter.hash(Object)` throws `RuntimeException("Parquet Bloom
filter: Not supported type")` for a Boolean. A BOOLEAN column chunk carries a
bloom filter whenever bloom filters are enabled for that column at write time
(`parquet.bloom.filter.enabled`, globally or as
`parquet.bloom.filter.enabled#b`): BOOLEAN is never dictionary-encoded, so the
writer keeps the empty filter, and readers run the bloom level by default.
So, reading the code, `b IN (true, false)` with
`spark.sql.parquet.pushdown.inFilterThreshold=1` on such a file fails the scan
in both V1 and V2, where the base branch pushed nothing for this conjunct and
returned the matching rows. The same holds for any IN list without NULL that is
longer than the threshold on a BOOLEAN field at top level, inside a struct, or
on a shredded-variant field read as BOOLEAN in a DSv1 scan. Lists with NULL,
which this PR fixes, are not affected: the bloom check evaluates the `eq(b,
null)` side of the `or` first and stops there. None of the new tests writes a
bloom filter, so they cannot catch this.
**Recommended change:** Build the BOOLEAN case of the Parquet IN predicate
builder as the OR of one `FilterApi.eq(booleanColumn(n), value)` per distinct
value, in list order, instead of `FilterApi.in(booleanColumn(n), set)`, and add
a comment there saying that parquet-java's bloom-filter level cannot hash a
Boolean in an `in` but falls back safely for `eq`. A BOOLEAN value set holds at
most two values, so the OR is the same filter, and it is the form the
below-threshold branch already pushes for the same list. Update the new
predicate-shape test to expect the equality form for `IN (true, false)` and `IN
(true, NULL)`, and add Parquet filter pushdown tests that read a BOOLEAN
column, at top level and as a field inside a struct, written with bloom filters
enabled, with the IN filter threshold at 1.
**Why this works:** Every route that pushes a BOOLEAN IN above the threshold
takes its predicate from this case, so after the change parquet-java receives
only `eq` leaves on a BOOLEAN column. Its bloom-filter level returns
might-match when hashing a Boolean fails for `eq`, its statistics, column-index
and record-level filters evaluate `eq` on BOOLEAN as they already do for `b =
true`, and an OR of equalities over at most two values keeps or skips a row
group or page exactly when an `in` over the same values would, so pruning stays
the same while the bloom-filter failure goes away.
**Scope:** Build the BOOLEAN case of the Parquet IN predicate builder from
equalities instead of a native `in`, with tests.
**Compatibility:** Results and pruning for every BOOLEAN IN list, including
the corrected handling of lists that mix NULL with a non-null value, stay as
the PR makes them, except that lists without NULL no longer fail on files whose
BOOLEAN column carries a bloom filter, and every other type keeps its native
`in`.
**Risks:** The Parquet predicate pushed for a BOOLEAN IN above the threshold
becomes an OR of `eq` leaves instead of an `in`; results and pruning are
unchanged at every filter level, so only code or tests that inspect the
predicate's class, such as the new predicate-shape test, see the difference. On
a BOOLEAN column chunk that carries a bloom filter, parquet-java logs a warning
for each row group whose `eq` value it cannot hash, as it already does for `b =
true`. No rollback flag is needed: apart from no longer failing on such files,
the change alters only the form of the pushed filter, not which rows, pages or
row groups it keeps.
**Constraints:** The case must still return one predicate for a non-empty
value set; its callers never pass an empty one once the NULL branch stops
building the `in` part from a list with no non-null value. Every route that
reaches the case, including the DSv1 shredded-variant IN conversion, must keep
taking its BOOLEAN predicate from it rather than building `FilterApi.in` itself.
**Exclusions:**
- An IN list of only NULLs, whose `in` part has no value to build from, is
fixed separately in the NULL branch of the IN conversion.
- Other types keep their native `in`, whose values parquet-java's bloom
filter can hash.
**Success:** No route in `ParquetFilters` hands parquet-java a Parquet `in`
on a BOOLEAN column. `b IN (true, false)` on a BOOLEAN column, and the same IN
on a BOOLEAN field inside a struct, written with
`parquet.bloom.filter.enabled=true` and read with
`spark.sql.parquet.pushdown.inFilterThreshold=1` returns the true and false
rows in V1 and V2, with both the vectorized and the parquet-mr reader, where it
failed before. BOOLEAN IN lists above the threshold, with or without NULL, are
still pushed, and `b IN (true, NULL)` and `b IN (true, false, NULL)` still
return the matching rows. For a shredded-variant field read as BOOLEAN, an IN
list above the threshold pushes an equality-based leaf predicate rather than an
`in`.
**Verification:**
- **Regression:** Write a BOOLEAN column, at top level and as a field inside
a struct, with Parquet bloom filters enabled, set the IN filter threshold to 1,
and check that an IN list without NULL over both values returns the right rows
through V1 and V2 and through both the vectorized and the parquet-mr reader;
before the change this fails with the bloom filter's unsupported-type error.
- **Behavior:** Check that the predicate built for a BOOLEAN IN list above
the threshold is the OR of per-value equalities, with the IS NULL disjunct
added for a list with NULL, and that lists with NULL still return the matching
rows end to end.
- **Regression:** For a shredded-variant field read as BOOLEAN with an IN
list above the threshold, check that the pushed leaf predicate is built from
equalities rather than a Parquet `in`.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/parquet/ParquetFilters.scala:
##########
@@ -1242,9 +1250,11 @@ class ParquetFilters(
}.reduceLeftOption(FilterApi.or)
} else if (canPartialPushDownConjuncts) {
if (values.contains(null)) {
- Seq(makeEq.lift(fieldType).map(_(fieldNames, null)),
- makeInPredicate.lift(fieldType).map(_(fieldNames,
values.filter(_ != null)))
- ).flatten.reduceLeftOption(FilterApi.or)
+ // Push both disjuncts or neither: either one alone is stricter
than the IN.
Review Comment:
**Nit (P3):** Only one of the two disjuncts is stricter than the IN on its
own. A NULL never satisfies `col IN (..., NULL)`, so the IN keeps exactly the
rows whose value is a non-null list entry: `in(col, non-null values)` alone
keeps those same rows, while `eq(col, null)` (IS NULL) alone keeps only NULL
rows, which is what made Parquet skip the matching row groups. The PR's own
end-to-end test expects exactly this, with `b IN (true, NULL)` returning only
`Row(true)`. The new test comment in `ParquetFilterSuite.scala` (line 1912) has
the same gap: it calls the pushed `eq(b, null)` `b = NULL`, which in SQL is
never true and which `ParquetFilters` elsewhere glosses as IS NULL, and its
reason shows only that the `in` part must be kept, not both parts. The code
itself is safe, since `makeEq` and `makeInPredicate` now cover the same types;
only the explanation is wrong. Suggested wording here: the IS NULL disjunct
alone would be stricter than the IN whenever the list has a non-null val
ue, so it is pushed only together with the `in` disjunct, and pushing nothing
is always safe. In the test, say the filter must not be `eq(b, null)` (IS NULL)
alone.
**Verification:**
- **Inspection:** Read both comments and check that each says an IN with
NULL never keeps a NULL row, that only the IS NULL disjunct alone is stricter
than the IN, and that neither spells Parquet's IS NULL test as `= NULL`.
##########
sql/core/src/test/scala/org/apache/spark/sql/execution/datasources/parquet/ParquetFilterSuite.scala:
##########
@@ -1896,6 +1896,45 @@ abstract class ParquetFilterSuite extends ParquetTest
with SharedSparkSession {
}
}
+ test("SPARK-60916: Pushdown IN predicate on a BOOLEAN column") {
Review Comment:
**Nit (P3):** The two new SPARK-60916 tests check the predicate
`createFilter` builds and an end-to-end read through the default vectorized
reader with Spark's Filter still applied, so they only notice row groups or
pages skipped wrongly. The new BOOLEAN IN conversion also feeds parquet-mr's
record-level filter, struct-nested BOOLEAN fields and the V2 scan's
pushed-filter list, and none of these is exercised: a per-record evaluation
that dropped matching rows, or a nested BOOLEAN IN that stopped being pushed,
would leave both tests green. Every other `makeInPredicate` case gets those
failure signals from an IN threshold loop in its per-type test, which runs
through `checkFilterPredicate` with the vectorized reader off and Spark's
Filter stripped, but `filter pushdown - boolean` has no IN case. Adding the
same loop there, for a list with and without NULL at thresholds on both sides
of the list length and expecting the predicate class the conversion builds at
each, would close the g
ap.
**Verification:**
- **Behavior:** Run the BOOLEAN IN cases through the per-type harness with
the vectorized reader off and record-level filtering on, for top-level and
struct-nested columns in V1 and V2, and check that the rows kept with Spark's
Filter stripped are the matching ones and that the expected predicate is pushed
at thresholds below and above the list length.
--
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]