Fix non-deterministic iteration in SessionStateBuilder#21262
Open
shehab-ali wants to merge 1 commit intoapache:mainfrom
Open
Fix non-deterministic iteration in SessionStateBuilder#21262shehab-ali wants to merge 1 commit intoapache:mainfrom
shehab-ali wants to merge 1 commit intoapache:mainfrom
Conversation
a9333c6 to
97e37c6
Compare
| /// one [`Arc`] per logical function. | ||
| fn dedup_function_registry_by_canonical_name<T>( | ||
| map: &HashMap<String, Arc<T>>, | ||
| canonical_name: impl Fn(&T) -> &str, |
Contributor
There was a problem hiding this comment.
Do we need canonical_name as it's own function if all the uses are the same function?
Author
There was a problem hiding this comment.
those function have different types though: WindowUDF, ScalarUDF, AggregateUDF
Contributor
There was a problem hiding this comment.
ah I see now, my bad.
97e37c6 to
936c4bd
Compare
ahmed-mez
approved these changes
Apr 8, 2026
Contributor
ahmed-mez
left a comment
There was a problem hiding this comment.
Thank you for fixing this! I hope the maintainers review this soon.
| /// matches the canonical name. The session stores one hash map entry per alias | ||
| /// plus the canonical name; filtering to canonical-name entries yields exactly | ||
| /// one [`Arc`] per logical function. | ||
| fn dedup_function_registry_by_canonical_name<T>( |
Contributor
There was a problem hiding this comment.
minor: I think the function could consume the map instead of borrowing it, to avoid some arc clones
fn dedup_function_registry_by_canonical_name<T>(
map: HashMap<String, Arc<T>>,
canonical_name: impl Fn(&T) -> &str,
) -> Vec<Arc<T>> {
map.into_iter()
.filter(|(key, udf)| key.as_str() == canonical_name(udf.as_ref()))
.map(|(_, udf)| udf) // no Arc::clone needed
.collect()
}
Contributor
There was a problem hiding this comment.
Alternatively, it might be more robust to dedup by identity
fn dedup_by_identity<T>(map: HashMap<String, Arc<T>>) -> Vec<Arc<T>> {
let mut seen = HashSet::new();
map.into_values()
.filter(|arc| seen.insert(Arc::as_ptr(arc)))
.collect()
}
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?
Problem:
SessionStateBuilder::new_from_existing()previously usedHashMap::into_values().collect_vec()for scalar, aggregate, and window functions. Since the HashMap stores entries for both the canonical name and each alias (all pointing to the sameArc<UDF>),into_values()produced duplicate entries in arbitrary order. Whenbuild()re-registered them, the last-writer-wins behavior on shared alias keys was nondeterministic.Fix: Introduced
dedup_function_registry_by_canonical_name()which:Iterates HashMap keys in sorted order (deterministic)
Keeps only one
Arc<UDF>per unique canonical name (no duplicates)This ensures build() re-registers each function exactly once, making the round-trip through
new_from_existing()→build()deterministic and alias-preserving.Are these changes tested?
Added unit test
Are there any user-facing changes?
No