Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: support PG14 JSON and ARRAY subscripting for UPDATE operations #77434

Open
steven-hubbard opened this issue Mar 7, 2022 · 2 comments
Open
Labels
A-sql-pgcompat Semantic compatibility with PostgreSQL A-tools-jooq C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) T-sql-queries SQL Queries Team

Comments

@steven-hubbard
Copy link
Contributor

steven-hubbard commented Mar 7, 2022

https://www.postgresql.org/docs/14/datatype-json.html#JSONB-SUBSCRIPTING

8.14.5. jsonb Subscripting
The jsonb data type supports array-style subscripting expressions to extract and modify elements. Nested values can be indicated by chaining subscripting expressions, following the same rules as the path argument in the jsonb_set function. If a jsonb value is an array, numeric subscripts start at zero, and negative integers count backwards from the last element of the array. Slice expressions are not supported. The result of a subscripting expression is always of the jsonb data type.

UPDATE statements may use subscripting in the SET clause to modify jsonb values. Subscript paths must be traversable for all affected values insofar as they exist. For instance, the path val['a']['b']['c'] can be traversed all the way to c if every val, val['a'], and val['a']['b'] is an object. If any val['a'] or val['a']['b'] is not defined, it will be created as an empty object and filled as necessary. However, if any val itself or one of the intermediary values is defined as a non-object such as a string, number, or jsonb null, traversal cannot proceed so an error is raised and the transaction aborted.

Jira issue: CRDB-13589

Epic CRDB-12464

@steven-hubbard steven-hubbard added the C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) label Mar 7, 2022
@blathers-crl blathers-crl bot added the T-sql-queries SQL Queries Team label May 24, 2022
@exalate-issue-sync exalate-issue-sync bot removed the T-sql-queries SQL Queries Team label May 24, 2022
@blathers-crl blathers-crl bot added the T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions) label May 24, 2022
@exalate-issue-sync exalate-issue-sync bot added T-sql-queries SQL Queries Team and removed T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions) labels May 24, 2022
@otan otan changed the title Make CRDB compatible with Postgres 14 syntax sql: add PG14 JSON subscripting types May 24, 2022
@otan otan changed the title sql: add PG14 JSON subscripting types sql: add PG14 JSON subscripting operations May 24, 2022
@mgartner
Copy link
Collaborator

cc @vy-ton for prioritization

@otan otan self-assigned this Jun 14, 2022
craig bot pushed a commit that referenced this issue Jun 29, 2022
82877: sql: support JSONB subscripting for SELECT cases r=mgartner a=otan

I've kept the `ArraySubscript` class names and references as PG does
this too. We can choose to change this in later iterations.

Refs [#77434](#77434)

Release note (sql change): Added support for JSONB subscripting in
SELECT-style cases, e.g. SELECT json_field['a'] ... WHERE
json_field['b'] = ...

83226: changefeedccl: block testfeed closure on canceled status r=samiskin a=samiskin

Resolves #82794 

TestChangefeedNemeses would sometimes flake on cloudstorage sink likely
due to the feed writing files during the tempdirectory closing.  Stress
testing it on cloudstorage sink failed at 160 executions.

This small change blocks feed.Close() on actually seeing the "cancelled"
job status to ensure the feed is completely stopped prior to Close
completing.  Running cloudstorage Nemeses did not fail after 500+ executions.

Release note: None

Co-authored-by: Oliver Tan <[email protected]>
Co-authored-by: Shiranka Miskin <[email protected]>
@otan
Copy link
Contributor

otan commented Jul 4, 2022

I've done SELECT support, but UPDATE is trickier and is deep into the weeds of the optimiser code.

In #83764, i've added support for single SET statements, e.g. UPDATE t SET k['a'] = '1', t[2] = '3'. This is done by replacing subscripts with a json_set clause. This in theory works for JSON, but should also work for other types that support this (e.g. hstore, which also supports subscripting). Happy to merge that if we're OK with partial support.

I imagine the following to be a small lift from that:

  • UPDATE t SET (k['a'], j['b']) = (1, 2)

The bigger lifts will from supporting statements like these:

  • UPDATE t SET k[0] = 1, k[1] = 2 -- note the same column is referenced multiple times. We currently do not allow this in the optimiser, and it'd be tricky-ish to shoehorn that in I'd imagine.
  • UPDATE t SET (k[0], k[1]) = SELECT 1, 2 FROM tbl, where results are extracted from a subquery.

The future of supporting these kinds of statements is something that extends way outside my current knowledge. Probably needs a wordy discussion too. I'm no longer on SQL Experience with migrations work and have little time to follow up if this is a priority.

Maybe a good intern / starter project for someone starting new on the queries team? Unassigning myself for now...

@otan otan removed their assignment Jul 4, 2022
@rafiss rafiss changed the title sql: add PG14 JSON subscripting operations sql: support PG14 JSON subscripting for UPDATE operations Oct 12, 2022
@mgartner mgartner changed the title sql: support PG14 JSON subscripting for UPDATE operations sql: support PG14 JSON and ARRAY subscripting for UPDATE operations Oct 18, 2022
@rafiss rafiss added the A-sql-pgcompat Semantic compatibility with PostgreSQL label Nov 17, 2022
@mgartner mgartner moved this to Backlog (DO NOT ADD NEW ISSUES) in SQL Queries Jul 24, 2023
@mgartner mgartner moved this from Backlog (DO NOT ADD NEW ISSUES) to New Backlog in SQL Queries Feb 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-sql-pgcompat Semantic compatibility with PostgreSQL A-tools-jooq C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) T-sql-queries SQL Queries Team
Projects
Status: Backlog
Development

No branches or pull requests

4 participants