Skip to content

Add string-backed JSON path pushdown - #353

Open
k-bx wants to merge 1 commit into
ClickHouse:mainfrom
k-bx:codex/string-json-path-pushdown
Open

k-bx wants to merge 1 commit into
ClickHouse:mainfrom
k-bx:codex/string-json-path-pushdown

Conversation

@k-bx

@k-bx k-bx commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

Compatibility views often expose a ClickHouse String as PostgreSQL json/jsonb through json[b]_in(text::cstring). This teaches json/jsonb extract_path_text pushdown to recognize that exact wrapper and deparse constant non-numeric paths with ClickHouse JSON functions.

The mapping preserves SQL NULL, missing keys, JSON null, empty strings, scalars, objects, nested paths, and invalid-JSON errors. Numeric and empty path components, non-text extraction, and non-constant paths remain local where semantics cannot be mapped safely.

Regression coverage includes Cyrillic ILIKE pushdown and json/jsonb result semantics.

Tested with PostgreSQL 18 and ClickHouse 26.3.12.3:

  • full installcheck suite: 39/39 passed
  • PostgreSQL 14 compilation
  • all pre-commit hooks

@theory theory added pushdown Improvements to query pushdown data types Improve data type support labels Sep 14, 2026
@theory
theory force-pushed the codex/string-json-path-pushdown branch from d50b717 to 7363408 Compare September 14, 2026 22:35

@theory theory 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.

This looks very useful, and simpler than the earlier PRs, thank you!

I rebased and made some tweaks, but have a few more questions about the code and some changes to make.

I also tried using body::jsonb instead of jsonb_in(body::cstring); shouldn't that work, as well? Also CAST(body AS jsonb).

Comment thread src/deparse.c

document = serialized_json_document((Expr*)linitial(fe->args));
if (document != NULL) {
if (cdef->cf_type != CF_JSON_EXTRACT_PATH_TEXT ||

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.

Why not allow CF_JSON_EXTRACT_PATH, too?

Comment thread src/deparse.c
}
input = (FuncExpr*)expr;
if ((input->funcid != F_JSON_IN && input->funcid != F_JSONB_IN) ||
(input->funcresulttype != JSONOID && input->funcresulttype != JSONBOID) ||

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.

Don't json_in() and jsonb_in() return JSON and JSONB by definition? This line seems redundant.

Comment thread src/deparse.c
Comment on lines +933 to +935
if (!IsA(argument, CoerceViaIO) ||
((CoerceViaIO*)argument)->resulttype != CSTRINGOID) {
return NULL;

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.

json_in() and jsonb_in() take a cstring argument by definition, no? Again seems redundant.

Comment thread src/deparse.c
return NULL;
}
argument = ((CoerceViaIO*)argument)->arg;
return exprType((Node*)argument) == TEXTOID ? argument : NULL;

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.

We only ever allow TEXT to be converted to a cstring? What about bytea, character, or varchar?

Comment thread src/deparse.c
Comment on lines +959 to +960
static bool
serialized_json_path_is_shippable(Const* path) {

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.

Could use a comment to explain that a numeric value is not usable for ClickHouse JSON path expressions, so this function prevents them from being pushed down.

Comment thread src/deparse.c
}

static void
append_serialized_json_function(

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.

Please add doc comments for each new function. TIA!

Compatibility views often expose a ClickHouse String as PostgreSQL
json/jsonb through `json[b]_in(text::cstring)`. Teach json/jsonb
`extract_path_text` to recognize that exact wrapper and puh them down to
ClickHouse JSON functions when called with constant, non-numeric paths.

The mapping preserves SQL NULL, missing keys, JSON null, empty strings,
scalars, objects, nested paths, and invalid-JSON errors. Numeric and
empty path components, non-text extraction, and non-constant paths
remain local where semantics cannot be mapped safely.

Tests include Cyrillic `ILIKE` pushdown and json/jsonb result semantics.
@theory
theory force-pushed the codex/string-json-path-pushdown branch from 7363408 to 5bf1a1e Compare September 14, 2026 22:38
@k-bx

k-bx commented Nov 7, 2026

Copy link
Copy Markdown
Contributor Author

Sorry for the long silence on this one. To answer your question about body::jsonb: yes, it should work, but it needs a small change.

serialized_json_document() only recognizes json_in(body::cstring) and jsonb_in(body::cstring). A plain cast is a CoerceViaIO node, so it isn't detected. With this PR on current main (36c797a) and the test view using body::jsonb, a path deparses to CAST(body AS String).body, and ClickHouse rejects it:

First argument for function tupleElement must be Tuple, Nullable(Tuple), QBit, JSON, Nullable(JSON) or array of these. Actual String

Treating a CoerceViaIO from text to json or jsonb as the same document fixes that. I tested this locally on PostgreSQL 18 and ClickHouse 26.10: the path now deparses to the serialized JSON expression, and the row count matches the jsonb_in() form.

The test view now uses body::jsonb, so nothing covers jsonb_in() any more, and the expected output still shows the old form. I'd keep the view on jsonb_in() and add a second view for the cast, so both are tested.

None of this is pushed yet, and your six inline comments are still open. I'll address them in the same push.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

data types Improve data type support pushdown Improvements to query pushdown

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants