Skip to content

[SPARK-59056][SQL] Pre-size HashSets in ParquetFilters IN/InSet pushdown - #58349

Open
david-mollitor-db wants to merge 1 commit into
apache:masterfrom
david-mollitor-db:SPARK-59056
Open

[SPARK-59056][SQL] Pre-size HashSets in ParquetFilters IN/InSet pushdown#58349
david-mollitor-db wants to merge 1 commit into
apache:masterfrom
david-mollitor-db:SPARK-59056

Conversation

@david-mollitor-db

Copy link
Copy Markdown

What changes were proposed in this pull request?

ParquetFilters.makeInPredicate builds a java.util.HashSet for each IN
predicate pushed down to Parquet, then adds exactly values.length elements to
it. The sets are created without an initial capacity, so they start at the
default capacity (16) and rehash as they grow.

This PR pre-sizes each of those 12 sets from the known element count using
Guava's Sets.newHashSetWithExpectedSize(values.length), so they hold all IN
values without rehashing or reallocating the internal bucket array.

The Guava helper is used rather than new HashSet(values.length) because
java.util.HashSet's int constructor treats the argument as bucket capacity,
not expected size, so the naive form would still rehash once the set passes
~75% load. This mirrors the existing Maps.newHashMapWithExpectedSize usage in
sql/catalyst.

Why are the changes needed?

makeInPredicate is only reached when the IN list size exceeds
spark.sql.parquet.pushdown.inFilterThreshold (default 10); smaller lists take
a per-element equality OR-chain and never build a HashSet. So by construction
the set always holds more than the threshold's worth of values, and for larger
IN lists (dozens to thousands) the default-capacity set rehashes several times
per pushed-down predicate, allocating and discarding bucket arrays, once per
Parquet file split scanned. Pre-sizing removes that avoidable work. The change
is capacity-only and behavior-preserving.

Does this PR introduce any user-facing change?

No.

How was this patch tested?

Existing ParquetV1FilterSuite, which covers IN/InSet predicate pushdown,
passes. The change is capacity-only, so predicate semantics are unchanged.

Was this patch authored or co-authored using generative AI tooling?

Generated-by: Claude Opus 4.8

`ParquetFilters.makeInPredicate` builds a `java.util.HashSet` for each IN
predicate pushed down to Parquet and then adds exactly `values.length`
elements. The sets start at the default capacity (16) and rehash as they grow.
Pre-size each of the 12 sets from the known element count with Guava's
`Sets.newHashSetWithExpectedSize(values.length)` so they hold all IN values
without rehashing or reallocating the internal bucket array.

The Guava helper is used rather than `new HashSet(values.length)` because
`java.util.HashSet`'s int constructor treats the argument as bucket capacity,
not expected size, so the naive form would still rehash past ~75% load. This
mirrors the existing `Maps.newHashMapWithExpectedSize` usage in sql/catalyst.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant