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 correlated subqueries in UPDATE #33641

Closed
konung opened this issue Jan 10, 2019 · 4 comments
Closed

sql: support correlated subqueries in UPDATE #33641

konung opened this issue Jan 10, 2019 · 4 comments
Labels
A-sql-mutations Mutation statements: UPDATE/INSERT/UPSERT/DELETE. A-sql-optimizer SQL logical planning and optimizations. C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) O-community Originated from the community

Comments

@konung
Copy link

konung commented Jan 10, 2019

Summary

Correlated subqueries in update statements don't work.
Filing this example as requested here: https://www.cockroachlabs.com/docs/stable/subqueries.html#correlated-subqueries (in the Note section)
Related to #3288

Use Case

I need to create effecient counter cache on the tables with millions of row. It's possible to do this within application itself, but highly ineffecient and slow. Running a simple updates statement ( as listed below) is way faster.
This is common pattern in migrations in Rails/Sinatra for creating counter cache on related models.

Steps to reproduce

  1. Using sample Startrek data set.
  2. Add a new column to episodes: quotes_count
  3. Update said column from subquery result
ALTER TABLE public.episodes ADD quotes_count int NULL;
CREATE INDEX episodes_quotes_count_idx ON public.episodes (quotes_count);

Actual statement in question

UPDATE
  episodes
SET
  quotes_count =
  (SELECT
    COUNT(quotes.quote) AS quotes_count
  FROM
    quotes
  WHERE quotes.episode = episodes.id)

Expected Result

This works in MariaDB, MySQL and Postgres.

image

Actual Result

Doesn't seem to be supported yet?

Returns this error


SQL Error [42P01]: ERROR: no data source matches prefix: episodes
  ERROR: no data source matches prefix: episodes
  ERROR: no data source matches prefix: episodes

I'm just trying out CDB coming from MariaDB/PostgreSQL, and I might be doing it wrong in CDb context, if so, what the supported SQL in Cockroach DB?

Thank you

@konung konung changed the title Correlated subque Correlated subquery Jan 11, 2019
@knz
Copy link
Contributor

knz commented Jan 12, 2019

Thank you, we are aware of this and are working on it.

@knz knz added C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) O-community Originated from the community A-sql-optimizer SQL logical planning and optimizations. A-sql-mutations Mutation statements: UPDATE/INSERT/UPSERT/DELETE. labels Jan 12, 2019
@knz knz added this to To do in BACKLOG, NO NEW ISSUES: SQL Optimizer via automation Jan 12, 2019
@knz
Copy link
Contributor

knz commented Jan 12, 2019

cc @andy-kimball you may want to add this to your test suite.

@knz knz changed the title Correlated subquery sql: support correlated subqueries in UPDATE Jan 12, 2019
@konung
Copy link
Author

konung commented Jan 12, 2019

@knz Yep, I figured as much. I just reported it , because in the docs there is a request for more examples. Hope this helps :)

@andy-kimball
Copy link
Contributor

Correlated subqueries in UPDATE is now working by default in the latest master branch, after merging #34522.

BACKLOG, NO NEW ISSUES: SQL Optimizer automation moved this from To do to Done Feb 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-sql-mutations Mutation statements: UPDATE/INSERT/UPSERT/DELETE. A-sql-optimizer SQL logical planning and optimizations. C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) O-community Originated from the community
Projects
None yet
Development

No branches or pull requests

3 participants