Repository navigation
Conversation
…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
force-pushed
the
dict-array-distinct-count
branch
from
November 23, 2026 09:54
43293ff to
2d58636
Compare
Author
|
Related to #12401 |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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), theArrowWritercounts 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
ArrowColumnWriterinto the column value encoders:GenericColumnWriter::closetakes the count before writing the dictionary page, so a chunk that keeps its dictionary never hashes it.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 andInt64columns 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_valuescases.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%.number_distinct_valuesstring_dictionary_low_cardinality_100cast toUtf8string_dictionary_low_cardinality_20string_dictionary_low_cardinality_100string_dictionary_low_cardinality_400string_dictionary_1pct_cardinalitystring_dictionary(every value unique)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_dictionaryand 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:
SerializedFileWriterandSerializedRowGroupWriternow honorset_write_row_group_number_distinct_values; its docs said they ignored it. With the option on, the computed count takes precedence over one passed towrite_batch_with_statistics, as it already did for theArrowWriter.Date64written withset_coerce_types(true).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