Skip to content

perf(parquet): count distinct values from the dictionary while a chunk keeps one - #12416

Open
shanielh wants to merge 1 commit into
apache:mainfrom
shanielh:dict-array-distinct-count
Open

shanielh wants to merge 1 commit into
apache:mainfrom
shanielh:dict-array-distinct-count

Conversation

@shanielh

@shanielh shanielh commented Nov 23, 2026 •

Copy link
Copy Markdown

Which issue does this PR close?

None. Follows up the performance note in #10654, which added the option and measured it at 1.4x to 8.8x slower writes.

Rationale for this change

With set_write_row_group_number_distinct_values(true), the ArrowWriter counts each column chunk's distinct values by hashing every non-null value into a set. While a chunk is dictionary encoded, its dictionary already holds exactly those values: the encoder interns each written value once, compared by its bytes. So for most low-cardinality columns the hashing repeats work the dictionary has already done, and it is most of the cost of the option.

What changes are included in this PR?

The count moves from ArrowColumnWriter into the column value encoders:

  • While a chunk keeps its dictionary, the count is the dictionary's size, and nothing is hashed.
  • Values written without a dictionary (dictionary encoding disabled, or after the chunk falls back once its dictionary page reaches the size limit) are hashed into a set as before. At fallback, the set is seeded with the dictionary's values, so values written on both sides count once.
  • GenericColumnWriter::close takes the count before writing the dictionary page, so a chunk that keeps its dictionary never hashes it.
  • Counting is skipped for columns with statistics disabled, where the count is not written.

This replaces the Arrow-side hashing, its per-type value access and the crate-private override that passed the count to the column writer.

Are these changes tested?

The existing distinct-count tests pass unchanged. New tests:

  • test_number_distinct_values_with_and_without_dictionary: string, string dictionary and Int64 columns written with a dictionary kept, with one that falls back after the first mini-batch, and with dictionary encoding disabled. Some values appear only before the fallback and others on both sides, so a missing seed or a double count fails it (removing the seed reports 1041 instead of 1050).
  • test_column_writer_counts_distinct_values: the row-based column writer writes the count.

Benchmarks

Targeted bench: arrow_writer, the */number_distinct_values cases.

Measured by retired instructions and cycles on Apple M5 Pro (aarch64, macOS): a small fixed-work program builds the same batches as the string_dictionary* benches (same generators, seed and 1M-row size), plus random-key, large-dictionary and plain-string variants, and writes each one 10 times to an empty sink; the cost per write is the count of 10 writes minus that of 0, divided by 10. Median of five alternating rounds; instruction counts repeat within about ±0.3%.

Batch, number_distinct_values Instructions, main PR Change Cycles, main PR Change Option's cost, main Option's cost, PR
string_dictionary_low_cardinality_100 cast to Utf8 527.9 M 288.5 M -45.3% 97.1 M 49.5 M -49.0% +83% +0.08%
string_dictionary_low_cardinality_20 423.3 M 145.6 M -65.6% 89.7 M 34.5 M -61.6% +191% 0.00%
string_dictionary_low_cardinality_100 419.1 M 141.3 M -66.3% 86.9 M 33.1 M -62.0% +197% 0.00%
string_dictionary_low_cardinality_400 416.9 M 138.8 M -66.7% 95.5 M 39.8 M -58.3% +200% 0.00%
400 values, random keys 417.7 M 139.6 M -66.6% 128.8 M 43.8 M -66.0% +199% 0.00%
string_dictionary_1pct_cardinality 375.0 M 163.3 M -56.4% 74.0 M 30.2 M -59.2% +130% +0.01%
string_dictionary (every value unique) 799.1 M 781.0 M -2.3% 278.3 M 280.0 M +0.6% +146% +140%
1M values, random keys 627.6 M 597.2 M -4.8% 554.7 M 557.2 M +0.5% +213% +197%
2M values, random keys 630.8 M 599.7 M -4.9% 762.8 M 649.4 M -14.9% +216% +200%

The option's cost is the extra instructions with the option on, relative to the same tree with it off. While a chunk keeps its dictionary the option now costs nothing: the count is read from the dictionary once, at close. The last three cases outgrow the 1 MiB dictionary page and fall back, so they keep hashing every row; they gain a little from hashing each value's bytes directly instead of through Arrow's per-type value access.

Slower inputs: none measured. A chunk that falls back hashes its dictionary's values once more when it does; the string_dictionary and large-dictionary cases above include that cost. Their cycle changes are within their round-to-round spread (up to 15%).

With the option off the new code does not run and instruction counts are unchanged, but this build measured 1-9% more cycles on some cases. Building both trees with RUSTFLAGS="-C llvm-args=-align-all-functions=6" brought every case within ±1.4%, so it is a code-alignment effect of the rebuild rather than added work.

Are there any significant user-facing changes?

Yes:

  • SerializedFileWriter and SerializedRowGroupWriter now honor set_write_row_group_number_distinct_values; its docs said they ignored it. With the option on, the computed count takes precedence over one passed to write_batch_with_statistics, as it already did for the ArrowWriter.
  • The count covers the values as stored. It differs from before only where the conversion to Parquet merges Arrow values, for example Date64 written with set_coerce_types(true).
  • No public API changes.

AI usage disclosure

An AI coding agent (Claude Code) wrote the change and its tests, extended the measurement program, ran the measurements, including the alignment check, and drafted this description, which the submitter reviewed. The approach of counting from the dictionary was the submitter's suggestion.

🤖 Generated with Claude Code

@boring-cyborg boring-cyborg Bot added the parquet parquet crate label Nov 23, 2026
…k keeps one

With `write_row_group_number_distinct_values`, the `ArrowWriter` hashed every
non-null value into a set. While a column chunk is dictionary encoded, its
dictionary already holds exactly its distinct values.

Count them in the column value encoders instead: the dictionary's size while
the chunk keeps it, and a set of hashes only for values written without one,
seeded with the dictionary's values when the encoder falls back. The column
writer takes the count before writing the dictionary page, so a chunk that
keeps its dictionary is counted without hashing.

Because the encoders do the counting, `SerializedFileWriter` now honors the
option too, and the count covers the values as stored.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@shanielh shanielh changed the title perf(parquet): hash each referenced dictionary value once for the distinct count perf(parquet): count distinct values from the dictionary while a chunk keeps one Nov 23, 2026
@shanielh
shanielh force-pushed the dict-array-distinct-count branch from 43293ff to 2d58636 Compare November 23, 2026 09:54
@shanielh

Copy link
Copy Markdown
Author

Related to #12401

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

parquet parquet crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant