Skip to content

Add estimated cardinality support for table functions#324

Merged
Giorgi merged 2 commits intoGiorgi:developfrom
jdaigle:DuckDBBindSetCardinality
Apr 12, 2026
Merged

Add estimated cardinality support for table functions#324
Giorgi merged 2 commits intoGiorgi:developfrom
jdaigle:DuckDBBindSetCardinality

Conversation

@jdaigle
Copy link
Copy Markdown
Contributor

@jdaigle jdaigle commented Apr 11, 2026

Adds an optional EstimatedCardinality property to the TableFunction record. When set, the bind callback calls duckdb_bind_set_cardinality to hint the query optimizer. Currently only available through the low-level API where the caller constructs the TableFunction directly. Only estimated (non-exact) cardinality is supported.

Implements #322

Adds an optional EstimatedCardinality property to the TableFunction record.
When set, the bind callback calls duckdb_bind_set_cardinality to hint the
query optimizer. Currently only available through the low-level API where
the caller constructs the TableFunction directly. Only estimated (non-exact)
cardinality is supported.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@coveralls
Copy link
Copy Markdown

coveralls commented Apr 12, 2026

Coverage Report for CI Build 24316052415

Coverage increased (+0.01%) to 89.607%

Details

  • Coverage increased (+0.01%) from the base build.
  • Patch coverage: 4 of 4 lines across 1 file are fully covered (100%).
  • No coverage regressions found.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 2910
Covered Lines: 2661
Line Coverage: 91.44%
Relevant Branches: 1487
Covered Branches: 1279
Branch Coverage: 86.01%
Branches in Coverage %: Yes
Coverage Strength: 451168.6 hits per line

💛 - Coveralls

@Giorgi
Copy link
Copy Markdown
Owner

Giorgi commented Apr 12, 2026

Thanks @jdaigle Why did you decide to limit it to estimate only? Why not allow callers pass exact number too?

Is it ok to set both for a table function at the same time?

@jdaigle
Copy link
Copy Markdown
Contributor Author

jdaigle commented Apr 12, 2026

@Giorgi I personally didn't have a use case for "exact". But I'm happy to improve this to support that use case.

Although I'm not entirely sure how it is supposed to work with "exact". Are we supposed to call duckdb_bind_set_cardinality just once passing true instead of false, or if we have both an "estimate" and "exact" are we supposed to call it twice?

@Giorgi
Copy link
Copy Markdown
Owner

Giorgi commented Apr 12, 2026

Is setting both useful? If we know an exact number, why set an estimate too?

Setting it twice will overwrite the previous value: https://github.com/duckdb/duckdb/blob/main/src%2Fmain%2Fcapi%2Ftable_function-c.cpp#L386-L396

Replaces the raw `ulong? EstimatedCardinality` parameter with a
`CardinalityHint` record that pairs the value with an `IsExact` flag,
addressing PR feedback from @Giorgi. The `IsExact` flag is now passed
through to `duckdb_bind_set_cardinality` instead of being hardcoded
to `false`.

Tests consolidated into parameterized `[Theory]` methods covering both
estimated and exact cases.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@jdaigle
Copy link
Copy Markdown
Contributor Author

jdaigle commented Apr 12, 2026

@Giorgi oh I see now, I didn't think to check the source code. I've updated to use a record type for both the value and boolean. Thanks!

@Giorgi Giorgi merged commit 7794d61 into Giorgi:develop Apr 12, 2026
13 of 15 checks passed
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.

3 participants