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: mutations in statement sources don't work properly with limits #20732

Closed
jordanlewis opened this issue Dec 14, 2017 · 4 comments · Fixed by #23824
Closed

sql: mutations in statement sources don't work properly with limits #20732

jordanlewis opened this issue Dec 14, 2017 · 4 comments · Fixed by #23824
Assignees
Labels
C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior.
Milestone

Comments

@jordanlewis
Copy link
Member

root@:26257/test> create table a (a int);
CREATE TABLE
root@:26257/test> insert into a values(1),(2);
INSERT 2
root@:26257/test> select * from [delete from a returning a] limit 1;
+---+
| a |
+---+
| 1 |
+---+
(1 row)
root@:26257/test> select * from [delete from a returning a] limit 2;
+---+
| a |
+---+
| 1 |
| 2 |
+---+
(2 rows)
root@:26257/test> select * from a;
+---+
| a |
+---+
| 1 |
| 2 |
+---+
(2 rows)

As you can see, the inner delete never gets executed.

Without a limit, or with an offset, this seems to work properly:

root@:26257/test> select * from [delete from a returning a] offset 1;
+---+
| a |
+---+
| 2 |
+---+
(1 row)
root@:26257/test> select * from a;
+---+
| a |
+---+
+---+
(0 rows)

Same problem exists with insert:

root@:26257/test> select * from [insert into a values(1) returning a] limit 1;
+---+
| a |
+---+
| 1 |
+---+
(1 row)
root@:26257/test> select * from a;
+---+
| a |
+---+
+---+
(0 rows)

cc @knz

@jordanlewis jordanlewis added the C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. label Dec 14, 2017
@jordanlewis jordanlewis self-assigned this Dec 14, 2017
@jordanlewis
Copy link
Member Author

The problem is that we don't flush the result when the plan is closed. The result is only flushed currently when the DML's datasource is exhausted. I think this is an okay change to make. I'll send it out for review.

@jordanlewis
Copy link
Member Author

So I tried fixing this but it wasn't as straightforward as I was hoping. cc @knz do you expect an inner [] plan to be fully consumed despite outer limits?

If so, and I think we should keep that expectation, we'll need to add a consume plan step when a plan is closed. I tried going down this path, but it's not straightforward - the right place for this seems like it should be in Close(), but Close() doesn't return an error and is perhaps not intended for these kinds of heavy-duty cleanups.

The resolution of this is a blocker for #20359, since common table expressions have the same problem - DML CTEs should be fully consumed in a plan.

@bdarnell
Copy link
Contributor

bdarnell commented Feb 8, 2018

Terminology nit: SELECT is a DML statement too. DML is defined in contrast to DDL (schema definitions); it doesn't have anything to do with read-only vs read/write statements.

@knz knz added this to the 2.0 milestone Feb 27, 2018
@knz
Copy link
Contributor

knz commented Feb 27, 2018

will followup in #23156

@jordanlewis jordanlewis assigned knz and unassigned jordanlewis Mar 13, 2018
@knz knz changed the title sql: DMLs in statement sources don't work properly with limits sql: mutations in statement sources don't work properly with limits Mar 15, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants