Skip to content

sql: UPDATE ... FROM ... ORDER BY and DELETE ... USING returns errors when the columns in the ORDER BY are from the table in the FROM clause. #89817

@faizaanmadhani

Description

@faizaanmadhani

Describe the problem

In UPDATE ... FROM ... ORDER BY ... statements where the columns specified in the ORDER BY clause come from the FROM table, cockroach returns an error to the user which stating error (42P10): SELECT DISTINCT ON expressions must match initial ORDER BY expressions. This is possibly because columns in the FROM clause are not in scope of the ORDER BY clause of an UPDATE FROM statement.

To Reproduce

Currently, we do not test ORDER BY clauses in UPDATE ... FROM statements, though we support it as shown in the docs. Adding an optbuilder test to pkg/sql/opt/optbuilder/testdata/update_from: UPDATE abc SET b = other.d FROM dec AS other WHERE abc.a = other.k ORDER BY other.k DESC LIMIT 2 returns this error:

error (42P10): SELECT DISTINCT ON expressions must match initial ORDER BY expressions

Note that the tables used in this test are already present in the optbuilder test file, and are:

CREATE TABLE abc (a int primary key, b int, c int)
CREATE TABLE dec (k INT PRIMARY KEY, d DECIMAL(10, 2))

Expected behavior
UPDATE ... FROM should allow a user to ORDER BY by the contents of the FROM table.

Jira issue: CRDB-20455

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCode not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior.T-sql-queriesSQL Queries Team

    Type

    No type

    Projects

    Status

    Bugs to Fix

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions