Repository navigation
feat(postgres): typed jsonb paths, @> and jsonb_set from the column schema - #115
Conversation
…the column schema
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 43 minutes. View limit details
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. |
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Brought up to date for merge (head
Verified locally: CI run 41473731791: 26.8 passed. On 26.2 the only failure was "generated fixtures: query results agree (clickhouse-join-nulls)", the same "Division by zero" flake fixed in #131. It is unrelated to this PR, which touches no ClickHouse code. Left for the maintainer: |
A jsonb column knows its document schema, but a query cannot read, filter or write one field of it with types. Today a path read is untyped text (
jsonText), and a containment filter or a one-path update needs a rawCH.sqlfragment.Closes #84.
Part of #52.
Changes
PG.jsonPath(column, ...keys)reads a path with->. The keys and the result type come from the column's TypeScript type, and the decoder comes from the column's Schema:jsonPath($.payload, "profile", "city")is anExpr<string | null>. A number key indexes an array. A missing key reads asnull..eq("Kyiv")matches the JSON string"Kyiv".PG.jsonPathText(column, ...keys)reads a path as text with->>, forlikeand text comparisons.PG.jsonContains(expr, part)writes(expr @> $n::jsonb).partis a deep partial of the document type. It also takes ajsonPath.PG.jsonSet(expr, keys, value)writesjsonb_set(expr, ARRAY[...], $n::jsonb). Use it in an UPDATEsetto write one path without a read-modify-write. The value has the path's type and is encoded by the field's schema. Nest calls to set more than one path. RETURNING decodes the full document.src/ch/json-path.ts: the path types (JsonPath,JsonPathValue,JsonContained,JsonKeyOf,JsonStep,JsonKey) andjsonPathSchema.jsonPathSchemawalks the Schema AST through structs, records, arrays, tuples, unions,NullOr, optional keys andsuspend. Only the SQL is insrc/pg/functions.ts. These are exported from/postgresfor helpers.QueryBuilderDefectat compile. A path through a node whose stored form differs from its type (Schema.encodeKeys, a class) is refused, because the stored keys can differ. A value with no JSON form (a bigint) fails withQueryBuilderErrorInvalidLiteral, not a throw.(payload -> 'profile')matches.docs/postgres.mdwith a PGlite example thatcheck-doc-examplesruns,docs/reference.md, adesign/dialects.mdnote, and the P0 row indesign/gap-review.md.minorchangeset.How SQLite and MySQL plug in: a SQLite or MySQL catalog adds its own
jsonPath/jsonSet/jsonContains. They reuse the same path types andjsonPathSchema, and writejson_extract(x, '$.a[0]')andjson_set(x, '$.a', json(?))on SQLite, orJSON_EXTRACT,JSON_UNQUOTE,JSON_CONTAINSandJSON_SETon MySQL. The only new piece is a function that writes the keys as a$.a[0]path string.Left out
?,?|,?&key-exists operators, and the array operators&&andANY. They are the other part of the gap-review P0 row. A follow-up can add them on the same base.$.payload.profile.city, as in effect-qb). A proxy on a column ref collides withExprmethod names (eq,get,in_) and with document keys that have those names. Variadic keys give the same types and autocompletion without that risk.jsonContainsdoes not encode its value through the schema, because a partial document does not satisfy a full schema. The value is written as given (the stored form). This is documented.jsonContains/jsonSet.CH.sqlstill covers that case.jsonb_set'screate_missingargument,#>/#>>, andjsonb_insert.Validation
bun run build: pass.bun run typecheck: pass (includes the newsrc/pg/json.test-d.ts).bunx vitest run --testTimeout=60000 --hookTimeout=60000with the live ClickHouse 26.8.2.7 env set: 61 files passed, 1046 tests passed, 0 skipped. The newsrc/pg/json.test.tsruns every query on PGlite. The postgres coverage manifest has ajson-pathscase.node scripts/check-doc-citations.mjs: ok.node scripts/check-exports-documented.mjs: ok (217 /postgres exports).node scripts/check-doc-examples.mjs: ok (22 files type-checked; the new example runs on PGlite).bun scripts/check-package.ts: ok.Need help on this PR? Tag
@codesmith-botwith what you need.