Skip to content

feat: Extend single-value NDV optimization to string types #21109

Closed

Description

@asolimando

Is your feature request related to a problem or challenge?

In #21077, we set distinct_count to Exact(1) when a filter predicate collapses a column interval to a single value. This works for numeric types (Int32, Float64, etc.) but not for strings (Utf8, Utf8View, LargeUtf8).

The root cause is that next_value_helper() in interval_arithmetic.rs has no implementation for string types - it returns the value unchanged, which prevents the constraint propagation solver from collapsing the interval.

For example, WHERE name = 'hello' should produce NDV=Exact(1) but currently stays at the original Inexact NDV.

Describe the solution you'd like

Detect equality predicates directly in collect_new_statistics (pattern-match BinaryExpr with Operator::Eq + Column + Literal), bypassing interval analysis for the NDV computation.

Describe alternatives you've considered

Implement next_value for strings in interval_arithmetic.rs. This is non-trivial since string ordering depends on locale/collation (see PostgreSQL's collation support for reference), making a universal "next string" ill-defined. That said, it would benefit other interval-based optimizations as well.

Additional context

Activity

  1. buraksenn commented on Mar 23, 2026

    @buraksenn
    Contributor

    I would like to work on this if you're not already working

  2. asolimando commented on Mar 23, 2026

    @asolimando
    MemberAuthor

    I would like to work on this if you're not already working

    Thanks @buraksenn for your interest, please go ahead!

    FYI: I have filed #21111 too, similar in spirit

  3. changed the title [-]feat: Extend single-value NDV optimization to string and non-numeric types[/-] [+]feat: Extend single-value NDV optimization to string types[/+] on Mar 23, 2026
  4. buraksenn commented on Mar 23, 2026

    @buraksenn
    Contributor

    Then I'm also taking #21111 since they should follow similar approach

  5. buraksenn commented on Mar 23, 2026

    @buraksenn
    Contributor

    take

  6. buraksenn commented on Apr 6, 2026

    @buraksenn
    Contributor

    I'm waiting on #21077 to be merged btw @asolimando to not have some conflicts. I think I'm pretty close with both issues ( #21111 and this) will open PRs when your PR is merged. Just wanted to give update

  7. asolimando commented on Apr 7, 2026

    @asolimando
    MemberAuthor

    I'm waiting on #21077 to be merged btw @asolimando to not have some conflicts. I think I'm pretty close with both issues ( #21111 and this) will open PRs when your PR is merged. Just wanted to give update

    Thanks for the update @buraksenn, makes sense, FYI #21077 has just been merged

  8. buraksenn commented on Apr 8, 2026

    @buraksenn
    Contributor

    Thanks @asolimando for the heads up, I've opened the PR just now. Btw I want to mention that I can take a look to any follow-ups to these kind of issues I've enjoyed working on these and would love to help. Feel free to assign me or mention me for the ones you don't have bandwidth for. I would really appreciate it

  9. asolimando commented on Apr 8, 2026

    @asolimando
    MemberAuthor

    Thanks @asolimando for the heads up, I've opened the PR just now. Btw I want to mention that I can take a look to any follow-ups to these kind of issues I've enjoyed working on these and would love to help. Feel free to assign me or mention me for the ones you don't have bandwidth for. I would really appreciate it

    Thanks, I will make sure to loop you in, happy to keep working on improving statistics too (code or reviews), feel free to ping me where you think I can help!

  10. added a commit that references this issue on Apr 17, 2026
    2f0ca3d
  11. added 2 commits that reference this issue on Apr 21, 2026
    516f1f4
    a29234c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions