Skip to content

Conversation

@pkolaczk
Copy link

@pkolaczk pkolaczk commented Jan 7, 2026

What is the issue

When a memory index contains very few rows and is split into
many shards, we can expect a lot of variance in the number of rows
between the shards. Hence, if we took only one
shard to estimate the number of matched rows, and
extrapolate that on all shards to compute the estimated matching rows
from the whole index, we risk making a huge estimation error.

This turned out to be a problem when testing the query planner metrics
in #2130, when the planner estimated 0 rows and didn't bump up
the estimated row count metric.

What does this PR fix and why was it fixed

This commit changes the algorithm to take as many shards as needed
to collect enough returned or indexed rows. For very
tiny datasets is it's likely to use all shards for estimation.
For big datasets, one shard will likely be enough, speeding up
estimation.

This change also allows to remove one estimation method.
We no longer need to manually choose between the estimation
from the first shard and from all shards.

@github-actions
Copy link

github-actions bot commented Jan 7, 2026

Checklist before you submit for review

  • This PR adheres to the Definition of Done
  • Make sure there is a PR in the CNDB project updating the Converged Cassandra version
  • Use NoSpamLogger for log lines that may appear frequently in the logs
  • Verify test results on Butler
  • Test coverage for new/modified code is > 80%
  • Proper code formatting
  • Proper title for each commit staring with the project-issue number, like CNDB-1234
  • Each commit has a meaningful description
  • Each commit is not very long and contains related changes
  • Renames, moves and reformatting are in distinct commits
  • All new files should contain the DataStax copyright header instead of the Apache License one

@pkolaczk pkolaczk requested a review from k-rus January 7, 2026 17:08
@k-rus
Copy link
Member

k-rus commented Jan 7, 2026

@pkolaczk can you add to the PR description, which issue is going to be fixed by this PR?

@pkolaczk
Copy link
Author

pkolaczk commented Jan 8, 2026

@pkolaczk can you add to the PR description, which issue is going to be fixed by this PR?

Linked. https://github.com/riptano/cndb/issues/16363

@k-rus
Copy link
Member

k-rus commented Jan 8, 2026

It would be great to update the PR description with motivation for the work from the issue and that it's blocking for another work.

@pkolaczk pkolaczk force-pushed the c16363-memtable-index-estimates branch from 2dbcb8e to 876dc37 Compare January 9, 2026 11:53
@pkolaczk pkolaczk requested review from adelapena and removed request for k-rus January 9, 2026 11:59
@pkolaczk pkolaczk force-pushed the c16363-memtable-index-estimates branch from 876dc37 to 23c2479 Compare January 9, 2026 12:58
@pkolaczk
Copy link
Author

test this please

@pkolaczk pkolaczk force-pushed the c16363-memtable-index-estimates branch from 23c2479 to 56197c0 Compare January 12, 2026 11:21
Copy link

@scottfines scottfines left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm new, and just trying to learn. But for what its worth, the logic looks sound to me

Copy link

@adelapena adelapena left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes look good to me. I have just left a few nits that can be addressed before merging. I think the CNDB PR will need an update. Maybe IndexQueryMetricsTest.testIndexQueryMetrics will need some adapting in that PR too.

@cassci-bot
Copy link

❌ Build ds-cassandra-pr-gate/PR-2188 rejected by Butler


3 regressions found
See build details here


Found 3 new test failures

Test Explanation Runs Upstream
o.a.c.index.sai.cql.VectorCompaction100dTest.testOneToManyCompaction[dc true] REGRESSION 🔴 0 / 21
o.a.c.index.sai.cql.VectorSiftSmallTest.testSiftSmall[db false] REGRESSION 🔴 0 / 21
o.a.c.index.sai.plan.PlanTest.prettyPrint (compression) REGRESSION 🔵🔴 0 / 21

Found 3 known test failures

When a memory index contains very few rows and is split into
many shards, we can expect a lot of variance in the number of rows
between the shards. Hence, if we took only one
shard to estimate the number of matched rows, and
extrapolate that on all shards to compute the estimated matching rows
from the whole index, we risk making a huge estimation error.

This commit changes the algorithm to take as many shards as needed
to collect enough returned or indexed rows. For very
tiny datasets is it's likely to use all shards for estimation.
For big datasets, one shard will likely be enough, speeding up
estimation.

This change also allows to remove one estimation method.
We no longer need to manually choose between the estimation
from the first shard and from all shards.

Additionally, the accuracy of estimating of NOT_EQ rows has been
improved by letting the planner know the union generated by NOT_EQ
is disjoint so the result set cardinality is the sum of cardinalities
of the subplans.

The commit contains also a fix for a bug that caused some
non-hybrid queries be counted as hybrid by the query metrics.

Unused keyRange parameters have been removed from the methods
for estimating row counts in the index classes.
@pkolaczk pkolaczk force-pushed the c16363-memtable-index-estimates branch from 325bad7 to 2386ac7 Compare January 14, 2026 17:20
@sonarqubecloud
Copy link

@pkolaczk pkolaczk merged commit c7ae969 into main Jan 16, 2026
485 of 499 checks passed
@pkolaczk pkolaczk deleted the c16363-memtable-index-estimates branch January 16, 2026 13:44
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.

6 participants