Skip to content

Add pushdown for JSONB ? operator, with casting - #344

Open
theory wants to merge 1 commit into
mainfrom
k-bx-agent/jsonb-exists-pushdown
Open

Add pushdown for JSONB ? operator, with casting#344
theory wants to merge 1 commit into
mainfrom
k-bx-agent/jsonb-exists-pushdown

Conversation

@theory

@theory theory commented Aug 8, 2026

Copy link
Copy Markdown
Collaborator

Added pushdown for the JSONB ? existence operator. Evaluates not only ClickHouse JSON columns mapped to JSONB, but also ClickHouse String columns that contain JSON, thus working on both JSON objects and JSON arrays. This requires a bit of complicated ClickHouse function dispatch, but does the trick.

Test output varies on ClickHouse 24.3 and earlier because the "JSON" feature at that time required the same keys in every object in the column, so the tests do in fact find the key in every row.

Resolves #325

@theory
theory requested a review from serprex August 8, 2026 03:37
@theory theory self-assigned this Aug 8, 2026
@theory theory added pushdown Improvements to query pushdown operators Improve operator pushdown labels Aug 8, 2026
Comment thread CHANGELOG.md Outdated
Comment thread test/sql/jsonb_exists.sql Outdated
Comment on lines +102 to +107
ORDER BY id;
id
----
1
2
(2 rows)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

on postgres this would include "key" (6)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

david=# select '{"key": 1}'::jsonb ? '"key"';
 ?column? 
----------
 f

Comment thread src/deparse.c Outdated
deparseJsonbNonnullDocument(document, document_kind, context);
appendStringInfoString(
buf,
") = 'Array', arrayExists(jsonb_exists_element -> "

@serprex serprex Aug 8, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This will cause trouble if they have column named jsonb_exists_element

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I actually prefer x, but maybe it should be __x.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Renamed to elem and added a column named elem to the tests that doesn't conflict AFAICT.

@serprex serprex Aug 8, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

conflict is when you're doing col1 ? elem

@theory
theory force-pushed the k-bx-agent/jsonb-exists-pushdown branch 2 times, most recently from e9d1116 to 55f1436 Compare August 8, 2026 05:53
Added pushdown for the JSONB `?` existence operator. Evaluates not only
ClickHouse JSON columns mapped to JSONB, but also ClickHouse String
columns that contain JSON, thus working on both JSON objects and JSON
arrays. This requires a bit of complicated ClickHouse function dispatch,
but does the trick.

Test output varies on ClickHouse 24.3 and earlier because the "JSON"
feature at that time required the same keys in every object in the
column, so the tests do in fact find the key in every row.

Signed-off-by: David E. Wheeler <david.wheeler@clickhouse.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

operators Improve operator pushdown pushdown Improvements to query pushdown

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants