Repository navigation
Conversation
18d62b8 to
b10d8cf
Compare
zhuqi-lucas
left a comment
There was a problem hiding this comment.
Thanks @rkrishn7, the lookup paths match intern for every impl as far as I can tell. A few comments inline.
…sions (apache#27501) ## Which issue does this PR close? - Related to apache#27498. Found during review of apache#27499. ## Rationale for this change Two `GroupValues::intern` paths can panic: - `GroupValuesColumn::emit(EmitTo::All)` replaces the column builders but keeps the hash map and collision lists. Interning a key seen before the emit finds a stale entry and indexes the empty builders: `index out of bounds: the len is 1 but the index is 2`. In-tree callers call `clear_shrink` after `emit(EmitTo::All)` or stop, so only code that reuses a `GroupValues` after a full emit hits this. - `ArrowBytesViewMap::insert_if_new` compares a long input with a stored entry that has the same hash and 4-byte prefix without checking the length. If the stored entry is inline, its bytes are read as a buffer index and offset: `range start index 1499027801 out of range for slice of length 0`. The values can't compare equal, so this never returns a wrong group, but it needs a 64-bit hash collision to happen and then panics. ## What changes are included in this PR? - `GroupValuesColumn::emit(EmitTo::All)` clears the hash map and collision lists along with the column builders. - `ArrowBytesViewMap::insert_if_new` skips entries whose length differs from the input, as `ArrowBytesMap` already does. ## What is the testing strategy for this PR? Two unit tests that fail on `main` and pass with this PR: - `intern_after_emit_all_starts_new_groups` interns, emits everything, interns again, and checks the new group ids in both streaming and vectorized modes. - `test_long_value_colliding_with_inline_value` interns an inline value and then a long value with the same prefix. Hashes only collide with the `force_hash_collisions` feature, which the extended CI job enables. ## Are there any user-facing changes? No.
af643ad to
b779104
Compare
## Which issue does this PR close? Backport of apache#27499 (approved) and apache#27501, which add `GroupValues::find` for apache#27498. Upstream split the fixes into apache#27501. This PR keeps them together. ## Rationale for this change fireflow's rolling aggregation will keep groups in several generations and needs to check older ones without growing them. `intern` inserts every key it doesn't find, so it can't be used for that. ## What changes are included in this PR? - Adds `GroupValues::find`: for each input row, the stored group id or `None`, without inserting and without changing stored values or ids. The default returns a not-implemented error, as upstream. - Implements it on all six interners: primitive, column, bytes, bytes_view, boolean and rows. Each mirrors its `intern` lookup without the insert path. - Adds a lookup-only `find` to `ArrowBytesMap` and `ArrowBytesViewMap`, with the same null, hash and equality handling as `insert_if_new`. `find` relies on four `intern` fixes, which this PR also includes: - `GroupValuesColumn::emit(EmitTo::All)` clears the hash map and collision lists. Without this, interning after a full emit panics on stale group ids (apache#27501). - `ArrowBytesViewMap::insert_if_new` checks the length before comparing a long value with a colliding inline entry. Without this, the inline bytes are read as a buffer index and offset (apache#27501). - `ArrowBytesMap::insert_if_new` does the same check for long values (upstream apache#25218, which also added the `force_hash_collisions` feature to `datafusion-physical-expr-common`). - `GroupValuesPrimitive::clear_shrink` resets the null group (upstream apache#25914). ## Are these changes tested? Yes: - **`find` behavior:** for each key type that `new_group_values` dispatches on, `find` returns the id `intern` assigned to each stored row, returns `None` for missing keys, and doesn't change `len()`. Tests also cover nulls, float normalization, and lookups after `emit` and `clear_shrink`. - **Bundled fixes:** each has a regression test that fails without it. The collision tests run with `--features force_hash_collisions`, as in the extended CI job. ## Are there any user-facing changes? No query behavior changes. `GroupValues::find`, `ArrowBytesMap::find` and `ArrowBytesViewMap::find` are new public API, with no breaking changes.
alamb
left a comment
There was a problem hiding this comment.
What is the usecase for this function? It seems like it is adding code that is not used by the core anywhere, so maybe it is unlikely to be used by other users?
If that is the case, I feel like maybe it is not a good thing to add to the core (as this will make extending / refactoring aggregators harder, for example) -- maybe we could find some extension API that is less invasive?
alamb
left a comment
There was a problem hiding this comment.
I wonder if there is some way to refactor the current implementation so it calls what you need (purge out some rows) as part of normal operation rather than adding new (effectively dead from this codebase) functions?
Yeah that's a fair point @alamb. Our implementation uses these aggregation APIs directly, rather than via the in-tree aggregation execution plans. Without getting into too much detail, we essentially keep multiple In my mind it comes down to whether we view these APIs as a public building block. If so, I think being able to probe a
I think I'm aligned in spirit. Conceptually |
If I'm understanding you correctly, I started with something similar in #24603. However, as mentioned in that issue, the accumulators' internal buffers aren't always well suited to evicting individual groups. |
I agree this is the key distinction and I think committing some pretty deep structure as a public API is potentially problematic in 2 ways:
Thus I would like to suggest either:
|
|
A public API that nothing calls +1 to this concern |
Both good points, agreed. I think this approach suffers from the fact that it adds a non-trivial amount of additional surface area, that to your point is new and can easily break in a future refactor. Thought a bit more about it and I think we can refactor existing Though, this doesn't mean the new API itself gets used in-tree. I think this might be a fine tradeoff, though? It greatly reduces the amount of new code to maintain while still adding a useful public API. I have a draft PR in #27664 for reference. Curious whether it seems like an acceptable tradeoff to you. |
|
@rkrishn7 I think a lookup API is acceptable even without an in-tree caller, and #27664 is the better shape for it. @alamb, does this work for you? Why a lookup API is OK without an in-tree caller (I +1'd that concern earlier):
Why #27664: Two asks for #27664:
|
yes thanks @jayzhan211 - I agree it looks good to me and I agree with the 2 items you asked about as well I think there is a danger than |
|
Appreciate the feedback @jayzhan211 and @alamb. Closing this and can move further discussion to #27664 |
Which issue does this PR close?
GroupValues::find#27498.Rationale for this change
See #27498.
What changes are included in this PR?
GroupValues::find, which writesSome(group_id)for each stored row andNoneotherwise, and never inserts. The default implementation returns a not-implemented error.GroupValues, mostly by reusing each implementation'sinternprobe path and skipping the insert:GroupValuesPrimitive,GroupValuesBoolean,GroupValuesBytes,GroupValuesBytesViewGroupValuesRows, which applies the same float normalization asinternbefore converting rowsGroupValuesColumn:vectorized_equal_topass per column.internalready did is moved into a helper that both methods call.GroupValuesClusteredhas no hash table, sofindsplits the input into runs of equal rows and scans the buffered groups, newest first.findmethods toArrowBytesMapandArrowBytesViewMap, which back the single-column bytes implementations.Depends on #27501:
findafteremit(EmitTo::All)relies on the hash map being cleared there.What is the testing strategy for this PR?
Unit tests check that, for every
GroupValuesimplementation,findreturns the idinternassigned to each stored row, returnsNonefor missing keys, and never inserts. They also cover nulls and lookups afteremitandclear_shrink. Lookups afteremit(EmitTo::All)intest_find_matches_intern_without_insertingfail until #27501 merges. The tests also run with theforce_hash_collisionsfeature, which sends every lookup through the collision handling.Are there any user-facing changes?
There are no changes to query behavior.
New public API, with no breaking changes:
GroupValues::find, with a default implementation that returns a not-implemented error.ArrowBytesMap::findandArrowBytesViewMap::findindatafusion-physical-expr-common.