Skip to content

Don't use IN for empty array (ANY('{}'))#285

Merged
theory merged 1 commit into
mainfrom
empty-any
Jun 16, 2026
Merged

Don't use IN for empty array (ANY('{}'))#285
theory merged 1 commit into
mainfrom
empty-any

Conversation

@theory

@theory theory commented Jun 16, 2026

Copy link
Copy Markdown
Collaborator

Prior to ClickHouse 25, an IN expression with no values, e.g., WHERE x = IN(), fails with an error. An alternative is to use has([], x), which does not fail on earlier versions

Revise the code that determines when to use IN to detect when an ANY expression has passed an empty array, and if so, disqualify it for use in an IN() expression. This lets it continue with the has() function, instead. Add tests to validate this behavior, and include a note to replace the new function, is_ok_in_expr(), with the original, simpler IsA(expr, Const) expression.

Resolves #275.

@serprex

serprex commented Jun 16, 2026

Copy link
Copy Markdown
Member

seems like you could replace expression with false

@theory

theory commented Jun 16, 2026

Copy link
Copy Markdown
Collaborator Author

seems like you could replace expression with false

Yes, I tried that and it works, but I chatted with the ClickHouse developers who said that, in the absence of benchmarking, it's likely that IN() expressions will have been better optimized, given that IN() resolves to a different function than has(). So I elected to keep it as-is.

@serprex

serprex commented Jun 16, 2026

Copy link
Copy Markdown
Member

my point is that when we have x in () or has([], x), those could be collapsed to false

@theory

theory commented Jun 16, 2026

Copy link
Copy Markdown
Collaborator Author

my point is that when we have x in () or has([], x), those could be collapsed to false

Yeah, that was my first thought, too, though I've been leaving such optimizations to the underlying planner. Can confirm it returns false on both tho.

@theory

theory commented Jun 16, 2026

Copy link
Copy Markdown
Collaborator Author

And also I wanted to minimize the work we were doing, with an eye to rip it all out when we someday drop support for ClickHouse 24.

Prior to ClickHouse 25, an `IN` expression with no values, e.g.,
`WHERE x = IN()`, fails with an error. An alternative is to use
`has([], x)`, which does not fail on earlier versions

Revise the code that determines when to use `IN` to detect when an `ANY`
expression has passed an empty array, and if so, disqualify it for use
in an `IN()` expression. This lets it continue with the `has()`
function, instead. Add tests to validate this behavior, and include a
note to replace the new function, `is_ok_in_expr()`, with the original,
simpler `IsA(expr, Const)` expression.
@theory theory merged commit 4944fcf into main Jun 16, 2026
27 of 28 checks passed
@theory theory deleted the empty-any branch June 16, 2026 15:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Deparser emits empty IN () for = ANY / <> ALL over empty array constants — rejected by ClickHouse < 25.x

2 participants