Skip to content

fix(encoding): filter out NULLs in encodeContainingArrayInvertedIndexSpans (#173046) - #173089

Closed
waterWang wants to merge 1 commit into
cockroachdb:masterfrom
waterWang:fix-inverted-index-null-173046
Closed

fix(encoding): filter out NULLs in encodeContainingArrayInvertedIndexSpans (#173046)#173089
waterWang wants to merge 1 commit into
cockroachdb:masterfrom
waterWang:fix-inverted-index-null-173046

Conversation

@waterWang

Copy link
Copy Markdown

Fixes #173046

Problem: When encodeContainingArrayInvertedIndexSpans encounters an array with NULL elements (e.g., ARRAY[NULL::TIMETZ]), it previously returned an empty SpanExpression with no spans. This caused the optimizer to generate an inverted index scan with no constraint, which the execution engine rejected with:

internal error: expected inverted index scan to have a constraint

Root cause: The HasNulls() check in encodeContainingArrayInvertedIndexSpans was too aggressive: it returned an empty SpanExpression for ALL arrays with NULLs, even those that also contain non-NULL elements. The empty SpanExpression caused the optimizer to generate an unconstrained inverted scan.

Fix: When the array has NULLs, filter them out using excludeNulls=true when calling encodeArrayInvertedIndexTableKeys, and generate spans for the remaining non-NULL elements. If all elements are NULL, return nil to let the optimizer fall back to a full table scan (which correctly returns no rows since NULL comparisons in SQL are unknown).

Reproduction:

CREATE TABLE t (a TIMETZ[]);
CREATE INVERTED INDEX t_a_inverted ON t (a);
SELECT 1 FROM t WHERE a @> ARRAY[NULL::TIMETZ]::TIMETZ[];

This previously errored with "expected inverted index scan to have a constraint". After the fix, it returns an empty result set without error.

The <@ (contained by) variant already worked correctly by using excludeNulls=true.

…Spans (cockroachdb#173046)

When encodeContainingArrayInvertedIndexSpans encounters an array with NULL
elements, it previously returned an empty SpanExpression with no spans.
This caused the optimizer to generate an inverted index scan with no
constraint, which the execution engine rejected with:
'expected inverted index scan to have a constraint'.

Fix: filter out NULL elements using excludeNulls=true, and generate spans
for the remaining non-NULL elements. If all elements are NULL, return nil
to let the optimizer fall back to a full table scan.

Fixes cockroachdb#173046
@waterWang
waterWang requested a review from a team as a code owner August 4, 2026 05:47
@waterWang
waterWang requested review from ZhouXing19 and removed request for a team August 4, 2026 05:47
@blathers-crl

blathers-crl Bot commented Aug 4, 2026

Copy link
Copy Markdown

It looks like your PR touches production code but doesn't add or edit any test code. Did you consider adding tests to your PR?

Thank you for contributing to CockroachDB. Please ensure you have followed the guidelines for creating a PR.

Before a member of our team reviews your PR, I have some potential action items for you:

  • Please ensure your git commit message contains a release note.
  • When CI has completed, please ensure no errors have appeared.

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

@cockroachlabs-cla-agent

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@ZhouXing19

Copy link
Copy Markdown
Collaborator

Thanks for working on this!
We're already working on the fix internally, so closing this PR.

@ZhouXing19 ZhouXing19 closed this Aug 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

O-community Originated from the community

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sql: Internal error "expected inverted index scan to have a constraint" for TIMETZ[] inverted index with NULL-containing containment query

2 participants