Repository navigation
fix(types): replace pointer-address Ord with discriminant Ord; fix count-distinct O(n²) - #73
Merged
Merged
Conversation
…or Value Value::Ord's cross-variant fallthrough compared stack-frame pointer addresses, which is non-deterministic across call contexts and violates Rust's Ord contract (BTreeMap/BTreeSet require a consistent total order). This caused incorrect aggregation grouping (PR #71) and wrong OR/OrJoin deduplication (PR #68). Fix: use a stable u8 discriminant per variant for cross-variant ordering. Also fix the O(n²) Vec::contains deduplication in count-distinct and sum-distinct (issue #50, which PR #66 claimed to fix but did not): replace with BTreeSet, which is now safe because Value::Ord is a correct total order. Adds two regression tests: - value_ord_cross_variant_is_antisymmetric: proves the old code violated antisymmetry (cmp(a,b) must equal reverse(cmp(b,a))) - value_ord_cross_variant_is_stable: cross-variant ordering must depend only on the variant type, not the inner value Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #73 +/- ##
==========================================
+ Coverage 84.70% 84.90% +0.20%
==========================================
Files 25 25
Lines 4484 4478 -6
==========================================
+ Hits 3798 3802 +4
+ Misses 686 676 -10
🚀 New features to boost your workflow:
|
Closed
9 tasks
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.
Summary
Value::Ord's cross-variant fallthrough compared stack-frame pointer addresses (self as *const Value as usize), which is non-deterministic and violates Rust'sOrdcontract.BTreeMapandBTreeSetrequire a consistent total order; violating this produces wrong aggregation groups (PR perf(query): fix O(n²) aggregation grouping (issue #31) #71) and incorrect OR/OrJoin deduplication (PR perf(query): fix O(n²) Or/OrJoin deduplication (issue #35) #68). Fixed by using a stableu8discriminant per variant.count-distinctandsum-distinctinapply_builtin_aggregatestill usedVec::containsfor deduplication (O(n²)), even though PR perf(query): optimize CountDistinct/SumDistinct from O(n²) to O(n log n) #66 claimed to fix issue CountDistinct/SumDistinct is O(n²) #50. Now usesBTreeSet(O(n log n)), which is correct becauseValue::Ordis now a proper total order.Changes
src/graph/types.rs: replace pointer-address fallthrough inOrd::cmpwith deterministic discriminant comparison; add two regression testssrc/query/datalog/functions.rs: replaceVec::containsdedup incount-distinctandsum-distinctwithBTreeSetTest plan
value_ord_cross_variant_is_antisymmetric— proves the old code violated antisymmetry; passes with the fixvalue_ord_cross_variant_is_stable— ordering depends only on variant type, not inner value🤖 Generated with Claude Code