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

Cannot combine DELETE .. USING .. ORDER BY .. LIMIT #123565

Closed
lukaseder opened this issue May 3, 2024 · 5 comments
Closed

Cannot combine DELETE .. USING .. ORDER BY .. LIMIT #123565

lukaseder opened this issue May 3, 2024 · 5 comments
Labels
A-sql-optimizer SQL logical planning and optimizations. C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. O-community Originated from the community T-sql-queries SQL Queries Team

Comments

@lukaseder
Copy link

lukaseder commented May 3, 2024

Describe the problem

I tried running a DELETE .. USING .. ORDER BY .. LIMIT query but it resulted in an error that seems to hint at an internal transformation flaw for the syntax combination:

SQL Error [42P10]: ERROR: SELECT DISTINCT ON expressions must match initial ORDER BY expressions

To Reproduce

create table t (i int);
create table u (i int);

delete from t
using u
where t.i = u.i
order by t.i
limit 1;

Remove the ORDER BY clause, and the problem goes away

Expected behavior

The query should run normally.

If applicable, add screenshots to help explain your problem.

Environment:

Jira issue: CRDB-38401

@lukaseder lukaseder added the C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. label May 3, 2024
Copy link

blathers-crl bot commented May 3, 2024

Hello, I am Blathers. I am here to help you get the issue triaged.

Hoot - a bug! Though bugs are the bane of my existence, rest assured the wretched thing will get the best of care here.

I was unable to automatically find someone to ping.

If we have not gotten back to your issue within a few business days, you can try the following:

  • Join our community slack channel and ask on #cockroachdb.
  • Try find someone from here if you know they worked closely on the area and CC them.

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

@blathers-crl blathers-crl bot added O-community Originated from the community X-blathers-untriaged blathers was unable to find an owner labels May 3, 2024
@lukaseder
Copy link
Author

I've reverse engineered this and this seems to work:

delete from t
using u
where t.i = u.i
order by t.rowid, t.i
limit 1;

But this risks to implement a different semantics, I think?

@lukaseder
Copy link
Author

Also, the problem doesn't appear with explicit primary keys. This works:

create table t (i int primary key);
create table u (i int primary key);

delete from t
using u
where t.i = u.i
order by t.i
limit 1;

@yuzefovich yuzefovich added T-sql-queries SQL Queries Team and removed X-blathers-untriaged blathers was unable to find an owner labels May 4, 2024
@michae2
Copy link
Collaborator

michae2 commented May 6, 2024

Thank you for filing the issue, @lukaseder.

I've reverse engineered this and this seems to work:

delete from t
using u
where t.i = u.i
order by t.rowid, t.i
limit 1;

But this risks to implement a different semantics, I think?

Yes, this has different semantics. This would delete the row with the lowest rowid rather than the row with the lowest i.

I think a rewrite like this could be a workaround:

DELETE FROM t WHERE t.rowid IN (
  SELECT t.rowid FROM t JOIN u ON t.i = u.i ORDER BY t.i LIMIT 1
);

@michae2 michae2 added the A-sql-optimizer SQL logical planning and optimizations. label May 6, 2024
@DrewKimball
Copy link
Collaborator

Closing as a dupe of #89817.

@michae2 michae2 added the X-duplicate Closed as a duplicate of another issue. label May 21, 2024
@exalate-issue-sync exalate-issue-sync bot removed the X-duplicate Closed as a duplicate of another issue. label May 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-sql-optimizer SQL logical planning and optimizations. C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. O-community Originated from the community T-sql-queries SQL Queries Team
Projects
Archived in project
Development

No branches or pull requests

4 participants