-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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: UPDATE .. FROM
distinct-on should apply before the LIMIT
is applied
#89823
Comments
UPDATE FROM ... ORDER BY ...
returns unexpected output when an ORDER BY
clause is presentUPDATE .. FROM
distinct-on should apply before the LIMIT
is applied
The problem here is that the |
Maybe other database vendors disallow this syntax because it's confusing. An equivalent SELECT would be: Looking at this form, the ORDER BY clearly applies to the result of the join relation and the first two rows in the ordered join relation only contain the first row from table
I wouldn't mind doing away with support for LIMIT and ORDER BY in an UPDATE FROM statement to force users to write SQL which is easier to understand. If we didn't have to worry about Postgres compatibility, we could get rid of UPDATE FROM altogether. I didn't see it mentioned in the ANSI SQL spec. https://sqlserverfast.com/blog/hugo/2008/03/lets-deprecate-update-from/ |
I don't think we'll be able to get rid of |
The only major database I could find which supports ORDER BY and LIMIT in an UPDATE statement is MySQL. And they don't support the FROM clause, meaning the ORDER BY can only be applied to the columns of the target table of the UPDATE. A statement like this fails because the ORDER BY column is not in scope: UPDATE t1 set a = t1.a+1 WHERE t1.a IN (SELECT a FROM t2) ORDER BY t2.b LIMIT 1; So, either we should remove support for The original issue for supporting UPDATE ... FROM seems to be doing so for Postgres compatibility, so it may have just been an oversight to allow both |
I agree—we should disallow |
Describe the problem
When an
ORDER BY
clause is present in anUPDATE ... FROM
statement, all the rows that are expected to be updated are not updated.For example, in this logic test, it would be expected that the
UPDATE
statement would update both rows(1, 10)
and(2, 20)
to(1, 100)
and(2, 100)
respectively as both rows join on the first two rows of theVALUES v(i) table
, however only one of the rows are updated.Additional data
Here is the query plan for the statement.
Jira issue: CRDB-20457
The text was updated successfully, but these errors were encountered: