Skip to content

Support CARDINALITY() in indexes - #4100

Merged
alecgrieser merged 1 commit into
FoundationDB:mainfrom
robert-brunel:cardinality-index
May 15, 2026
Merged

alecgrieser merged 1 commit into
FoundationDB:mainfrom
robert-brunel:cardinality-index

Conversation

@robert-brunel

Copy link
Copy Markdown
Contributor
  • Introduce a CardinalityFunctionKeyExpression to make it possible to define indexes with a function("cardinality", …) key expression.
  • In MaterializedViewIndexGenerator, add the necessary logic to convert CardinalityValue into a key expression.

For a nullable ARRAY field, the key expression produced from a CARDINALITY(int_arr) SQL expression will look as follows:

function("cardinality",
         field("int_arr").nest(field("values", Concatenate)))

The use of the Concatenate fan-out type currently runs into some gaps in the code that we also fix with this PR.

Testing:

  • Integration tests in arrays_cardinality.yamsql.
  • Unit tests in IndexTest.java and KeyExpressionTest.java.

@robert-brunel robert-brunel self-assigned this Apr 21, 2026
@robert-brunel robert-brunel added the enhancement New feature or request label Apr 21, 2026
@normen662
normen662 self-requested a review April 22, 2026 07:34
@robert-brunel
robert-brunel force-pushed the cardinality-index branch 2 times, most recently from 233e334 to 7a80efd Compare May 13, 2026 14:45

@pengpeng-lu pengpeng-lu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM!

@robert-brunel
robert-brunel removed the request for review from g31pranjal May 14, 2026 10:58

@alecgrieser alecgrieser left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I have a few small thoughts. The big ones here are more observations for future work rather than change requests, per se. I did have a few remaining questions that may be best answered with additional comments

@robert-brunel
robert-brunel requested a review from alecgrieser May 14, 2026 16:04

@alecgrieser alecgrieser left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM. I think the issue in the comment isn't quite right, but that doesn't need to block this PR

* Introduce a `CardinalityFunctionKeyExpression` to make it possible to
  define indexes with a `function("cardinality", …)` key expression.
* In `MaterializedViewIndexGenerator`, add the necessary logic to
  convert `CardinalityValue` into a key expression.

For a nullable ARRAY field, the key expression produced from a
`CARDINALITY(int_arr)` SQL expression will look as follows:

  function("cardinality",
           field("int_arr").nest(field("values", Concatenate)))

The use of the Concatenate fan-out type currently runs into some gaps
in the code that we also fix with this PR.

Testing:
* Integration tests in `arrays_cardinality.yamsql`.
* Unit tests in `IndexTest.java` and `KeyExpressionTest.java`.
@github-actions

Copy link
Copy Markdown

📊 Metrics Diff Analysis Report

Summary

  • New queries: 17
  • Dropped queries: 0
  • Plan changed + metrics changed: 0
  • Plan unchanged + metrics changed: 0
ℹ️ About this analysis

This automated analysis compares query planner metrics between the base branch and this PR. It categorizes changes into:

  • New queries: Queries added in this PR
  • Dropped queries: Queries removed in this PR. These should be reviewed to ensure we are not losing coverage.
  • Plan changed + metrics changed: The query plan has changed along with planner metrics.
  • Metrics only changed: Same plan but different metrics

The last category in particular may indicate planner regressions that should be investigated.

New Queries

Count of new queries by file:

  • yaml-tests/src/test/resources/arrays-cardinality.metrics.yaml: 17

@alecgrieser
alecgrieser merged commit c6c9681 into FoundationDB:main May 15, 2026
14 checks passed
@robert-brunel
robert-brunel deleted the cardinality-index branch May 15, 2026 13:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants