Skip to content

Planner: most-common-values and t-digest per property from the stats kernel #1797

Open

Description

@bplatz

Originated in the review of #1789 (fluree-db-stats kernel and on-demand column profiling), which asked whether the new sketches could serve query planning. Nothing in that PR runs at index time; this issue is the follow-up that would.

Problem

The planner reasons from count, ndv_values and ndv_subjects per (graph, property), and two estimates are known to be wrong in a way those three cannot fix:

  • Bound object is count / ndv_values, a uniform assumption (fluree-db-query/src/planner.rs, estimate_triple_row_count, PatternType::BoundObject). hash_join.rs documents the BSBM case: country=US is estimated at ~28K rows against an actual 61.8K, and HASH_JOIN_MAX_SCAN_RATIO was loosened to 1024× so the join this operator exists for is not rejected on that error.
  • FILTER selectivity is a fixed 50% (fluree-db-query/src/filter.rs). BSBM Q1, Q3 and Q5 carry numeric range filters.

Proposal

Persist two more sketches per (graph, property) in the indexer's HllSketchBlob, computed in the same IdStatsHook::apply_record pass that feeds the HLL today:

  1. Most common values: fluree_db_stats::HeavyHitters (Misra–Gries, 64 counters) keyed on the o_hash the hook already computes. For the planner the display sample is unnecessary; a (hash, count) pair per counter is enough. Consumption: hash the constant object the same way, use the counter when present, otherwise (count − Σ mcv) / (ndv − |mcv|), the Postgres formula. Exact for low-cardinality properties, bounded for popular values elsewhere.
  2. Value distribution: fluree_db_stats::TDigest per numeric and temporal property. Consumption: cdf(K) for FILTER(?v > K) and the like; min/max for a zero-result short-circuit.

Both merge, so the incremental refresh path folds novelty sketches into the persisted ones the way it folds the HLL. Both are insert-only, which matches the HLL's existing semantics: retractions do not decrement, a full rebuild resets. For planning that means stale-high under deletes, which is conservative.

Constraints

  • Bench first. The MCV adds a hash-map lookup per flake plus an O(64) sweep when the table is full and the value is unseen; the digest adds a buffered push and a sort of ~600 every 500 values. That roughly doubles the stats hook's per-flake work, and the hook has stalled imports before. Gate it behind a flag and measure on the DBLP import before it is on by default.
  • Persistence is a HllSketchBlob version bump with serde(default) on the new fields; a few kilobytes per property.
  • assemble_fast_stats in fluree-db-novelty merges novelty deltas into the counts at query time; the MCV can be left base-only within the novelty window.

Acceptance

  • The hash_join_cost_wins BI-1 case estimates country=US within the sketch's error bar, and the 1024× cap can be tightened back toward the measured break-even.
  • A FILTER-selectivity test on a skewed numeric property where the 50% constant misorders a two-pattern join and the digest does not.
  • Import bench shows the per-flake cost of the hook with the flag on, against the flag off.

Activity

  1. added
    enhancement
    P2
    area:query
    on Sep 5, 2026
  2. modified the milestone: 5.0.0 on Nov 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2area:queryenhancement

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions