Skip to content

perf(next): advanced cost-based join ordering with real in-memory index statistics - #644

Merged
remiceres merged 2 commits into
feature/corese-nextfrom
perf/optimizer-cbo-index-statistics
Oct 22, 2026
Merged

remiceres merged 2 commits into
feature/corese-nextfrom
perf/optimizer-cbo-index-statistics

Conversation

@remiceres

Copy link
Copy Markdown
Contributor

Summary

Resolves #637 by replacing the heuristic static reduction scales in CardinalityPatternOrdering with exact and sampled index statistics extracted directly from InMemoryStatementStore (distinctSubjects and distinctObjects).

Technical Changes

  1. Storage API & Model:

    • Added PredicateStatistics record (IRI predicate, long count, long distinctSubjects, long distinctObjects) in fr.inria.corese.core.next.storage.api.model.
    • Added default PredicateStatistics getPredicateStatistics(IRI predicate) to MetadataOperations.
  2. InMemoryStatementStore Implementation:

    • Implemented estimatePredicateStatistics(IRI, maxSample) in TripleIndex leveraging the POS index permutation ($\mathcal{O}(1)$ distinct objects directly from the branch root size, and bounded sampling for distinct subjects via PredicateSampleAccumulator).
    • Implemented getPredicateStatistics(IRI) in IndexedStatementStore aggregating across partitions.
  3. Query Engine SPI & Producer:

    • Added getPredicateStatistics(Query, Node, IRI) and getStorageStatistics() to QueryStatistics interface.
    • Implemented delegation and session caching in StorageManagerProducer with safety guards for mocked storages.
  4. Cost-Based Query Planner (CardinalityPatternOrdering):

    • Replaced fixed decimal scaling with dynamic selectivity factors:
      $\text{cost} = \frac{\text{baseCount}}{\text{distinctSubjects} \times \text{distinctObjects}}$
    • Encapsulated planning parameters inside PlanningContext to maintain strict compliance with SonarLint cognitive complexity and parameter count thresholds.
    • Preserved graceful fallback to original heuristic scales when statistics are unavailable.
  5. Unit Tests & Quality:

    • Added unit tests in CardinalityQueryPlannerTest verifying join ordering decisions driven by distinct subjects/objects, exact statistics extraction, and Cartesian explosion avoidance.
    • 0 SonarLint issues (cognitive complexity < 15, clean typing, Boy Scout compliance).
    • 100% unit tests passing in core.next.

…ex statistics

- Add PredicateStatistics record to storage API model
- Provide POS-index based distinct subjects and objects extraction in TripleIndex
- Expose predicate statistics via MetadataOperations and IndexedStatementStore
- Integrate index statistics into QueryStatistics and StorageManagerProducer
- Upgrade CardinalityPatternOrdering to use real selectivity ratios from index stats
- Refactor ordering methods with PlanningContext to ensure 0 Sonar alerts
- Add tests in CardinalityQueryPlannerTest covering index-driven join ordering

Closes #637
@remiceres remiceres self-assigned this Oct 22, 2026
@github-actions

Copy link
Copy Markdown
Overall Project 57.94% -0.03% 🍏
Files changed 84.72% 🍏

File Coverage
TripleIndex.java 97.19% -0.66% 🍏
IndexedStatementStore.java 95.32% -0.19% 🍏
CardinalityPatternOrdering.java 89.69% -9.83% 🍏
StorageManagerProducer.java 87.85% -1.32% 🍏
PredicateStatistics.java 86.11% -13.89% 🍏
QueryStatistics.java 20% -20% ❌
MetadataOperations.java 0% ❌

@github-actions

Copy link
Copy Markdown

Test Results

  416 files    416 suites   26s ⏱️
3 360 tests 3 360 ✅ 0 💤 0 ❌
3 394 runs  3 394 ✅ 0 💤 0 ❌

Results for commit 92e5c22.

…r cache

- Fix distinct subjects extrapolation formula in TripleIndex
- Guard against long arithmetic overflow using BigDecimal in CardinalityPatternOrdering
- Safely handle invalid IRI strings in toPredicateIri
- Make StorageManagerProducer thread-safe and cache missing predicate stats
- Handle StorageException gracefully during statistics inspection
@remiceres
remiceres merged commit de8d6b0 into feature/corese-next Oct 22, 2026
1 check passed
@remiceres
remiceres deleted the perf/optimizer-cbo-index-statistics branch October 22, 2026 14:03
@github-actions

Copy link
Copy Markdown
Overall Project 57.94% -0.03% 🍏
Files changed 84.51% 🍏

File Coverage
TripleIndex.java 97.19% -0.66% 🍏
IndexedStatementStore.java 95.32% -0.19% 🍏
CardinalityPatternOrdering.java 89.05% -10.48% 🍏
StorageManagerProducer.java 88.09% -1.52% 🍏
PredicateStatistics.java 86.11% -13.89% 🍏
QueryStatistics.java 20% -20% ❌
MetadataOperations.java 0% ❌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant