Repository navigation
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 15 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. Comment |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.