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 UPDATE ... FROM #7841

Open
mjibson opened this Issue Jul 14, 2016 · 10 comments

Comments

@mjibson
Copy link
Member

mjibson commented Jul 14, 2016

(edit @knz 2018-05-19)

UPDATE X SET ... = Y FROM Z
https://www.postgresql.org/docs/10/static/sql-update.html

performs (I think) a lateral join of X with Z and then makes the values computed by Z available for the scalar expressions in Y, to serve as values to update.

Full support for the feature needs support for correlated subqueries #3288 and lateral joins #24560.

Reduced support when the relational expression Z is uncorrelated (a common case) could perhaps be added with a simple join, although additional execution machinery is still needed to propagate the PK prefixes for the X operand to the join through up to the point UPDATE can pick them up.

As to which rows get updated:

  • Rows of table X that don't match in the join are not updated.
  • Rows that match in X with the join on Z are updated. If there is just 1 matching row (or if there is a cross join but the right operand has just 1 row), then the situation is clear. I am not exactly sure what happens if there are multiple rows.

The work to be done is twofold:

  • construct (and properly optimize) the data source with a join when FROM is specified
  • plumb the PK values properly so that UPDATE still has PKs to work with.
@mjibson

This comment has been minimized.

Copy link
Member

mjibson commented Jul 14, 2016

See #7247 when fixing this issue.

@aitjcize

This comment has been minimized.

Copy link

aitjcize commented Nov 8, 2017

any updates on when this will be supported?

@dianasaur323

This comment has been minimized.

Copy link
Collaborator

dianasaur323 commented Nov 9, 2017

@awoods187

This comment has been minimized.

Copy link
Contributor

awoods187 commented Nov 9, 2017

@aitjcize we will not be including this in our upcoming April release. I'd love to hear more about your use case to understand your need for this feature. This will help us prioritize for future roadmap inclusion

@aitjcize

This comment has been minimized.

Copy link

aitjcize commented Nov 9, 2017

For our specific use case, we simply want to save as much query as possible. We are using UPDATE table SET ... FROM table old_table WHERE ... RETURNING old_table.X to return pre-update value after update.

@jviksne

This comment has been minimized.

Copy link

jviksne commented Jan 14, 2018

I would also like to vote for this to be implemented. I need to do some conditional operations on a sum of values from child records. In PostgreSQL I can do it in a single query, while I guess in CockroachDB I will need to split it into multiple separate ones.

The query looks something like this (with simplified conditions here):
UPDATE my_table SET child_cost=
CASE WHEN subq.child_cost_sum < cost THEN subq.child_cost_sum ELSE cost END
FROM (SELECT sum(cost) AS child_cost_sum FROM my_table WHERE parent_id=123) AS subq;

@jordanlewis jordanlewis added this to Backlog in SQL Front-end, Lang & Semantics via automation Apr 26, 2018

@knz knz moved this from Triage to Feature requests / pie-in-the-skie in SQL Front-end, Lang & Semantics May 3, 2018

@knz knz added A-sql-pgcompat and removed E-starter labels May 19, 2018

@knz

This comment has been minimized.

Copy link
Member

knz commented May 19, 2018

I have extended the top description of the issue to detail the work that needs to be done. It's not super simple but it's not fundamentally difficult either. Probably reasonable to table for CRDB 2.2. (edit 2018-10-19 I don't know actually)

@awoods187

This comment has been minimized.

Copy link
Contributor

awoods187 commented Oct 30, 2018

top requested feature based on telemetry data

@knz

This comment has been minimized.

Copy link
Member

knz commented Nov 16, 2018

Discussed this with @andy-kimball yesterday - Andy confirmed that once the opt framework knows about mutations, planning update...from will be a natural extension of the support for correlated subqueries and lateral joins.

(Of course ensuring the statement is recognized in the first place will still require some extra work)

@knz knz added this to To do in SQL Planning via automation Nov 16, 2018

@knz

This comment has been minimized.

Copy link
Member

knz commented Nov 30, 2018

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