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: add support for DELETE FROM ... USING #88974

Merged
merged 3 commits into from
Oct 17, 2022

Conversation

faizaanmadhani
Copy link
Contributor

@faizaanmadhani faizaanmadhani commented Sep 28, 2022

See commit messages for details.

Resolves: #40963

@cockroach-teamcity
Copy link
Member

This change is Reviewable

@faizaanmadhani faizaanmadhani force-pushed the faizaan/delete-from-using branch 2 times, most recently from d25f22e to 7d401f1 Compare September 29, 2022 15:18
@faizaanmadhani faizaanmadhani changed the title Faizaan/delete from using sql: add support for DELETE FROM ... USING Sep 29, 2022
@faizaanmadhani faizaanmadhani force-pushed the faizaan/delete-from-using branch 10 times, most recently from 1823ff3 to c129904 Compare October 6, 2022 14:42
@faizaanmadhani faizaanmadhani force-pushed the faizaan/delete-from-using branch 9 times, most recently from 1589799 to 222eb09 Compare October 10, 2022 19:25
@faizaanmadhani faizaanmadhani marked this pull request as ready for review October 10, 2022 19:26
@faizaanmadhani faizaanmadhani requested review from a team as code owners October 10, 2022 19:26
@faizaanmadhani faizaanmadhani force-pushed the faizaan/delete-from-using branch 3 times, most recently from 84f2746 to 54176eb Compare October 10, 2022 23:24
@faizaanmadhani faizaanmadhani force-pushed the faizaan/delete-from-using branch 2 times, most recently from 4fa4fd9 to 9c19507 Compare October 12, 2022 15:54
Copy link
Contributor Author

@faizaanmadhani faizaanmadhani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @mgartner)


pkg/sql/opt/optbuilder/mutation_builder.go line 446 at r1 (raw file):

Previously, mgartner (Marcus Gartner) wrote…

I'm alarmed that prior to this commit we called setFetchColIDs with some potential non-fetch columns... I guess that didn't cause any problems. Regardless, moving setFetchColIDs up looks more correct to me. 👍

Done.


pkg/sql/opt/optbuilder/testdata/delete line 490 at r2 (raw file):

Previously, mgartner (Marcus Gartner) wrote…

Some tests to add:

  • test the validateJoinTableNames path by putting the same table in the USING clause multiple times

Done. This test is added below (second last test in file).

@faizaanmadhani faizaanmadhani force-pushed the faizaan/delete-from-using branch from 9c19507 to 4947e95 Compare October 12, 2022 17:14
@mgartner mgartner requested a review from a team October 13, 2022 17:39
Previously, the statement `DELETE FROM .. USING` would
return an unimplemented error. This commit adds
production rules in the parser to handle the `USING`
clause in a `DELETE` statement, however usage will
return an error as support has not been implemented
in `optbuilder`.

Release note: None
@faizaanmadhani faizaanmadhani force-pushed the faizaan/delete-from-using branch from 4947e95 to 7c63889 Compare October 13, 2022 17:40
Copy link
Collaborator

@mgartner mgartner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 15 of 15 files at r4, 17 of 17 files at r9, 5 of 5 files at r10, 10 of 10 files at r11, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @faizaanmadhani and @michae2)


-- commits line 34 at r11:
nit: cockroachdb => CockroachDB


pkg/sql/delete.go line 165 at r11 (raw file):

	var pm row.PartialIndexUpdateHelper
	if n := len(d.run.td.tableDesc().PartialIndexes()); n > 0 {
		offset := d.run.partialIndexDelValsOffset + d.run.numPassthrough

To keep d.run.partialIndexDelValsOffset true to its name, I think we should update it's value here instead:

partialIndexDelValsOffset: len(rd.FetchCols),

And the comment for it here shuld be updated too:

// partialIndexDelValsOffset is the offset of partial index delete
// indicators in the source values. It is equal to the number of fetched
// columns.


pkg/sql/opt/optbuilder/mutation_builder.go line 487 at r10 (raw file):

		}

		if !pkCols.Empty() {

nit: A table must have PK columns, so this check is unnecessary.


pkg/sql/logictest/testdata/logic_test/delete line 338 at r11 (raw file):

5

# Test that USING works

nit: add period to the end of this comment and the ones below


pkg/sql/logictest/testdata/logic_test/delete line 345 at r11 (raw file):

    b STRING,
    c INT
);

nit: for single-statements in statement ok, there is no need for the ; at the end. I don't think we are terribly consistent, so it's up to you, but I think for the most part we leave it off.


pkg/sql/logictest/testdata/logic_test/delete line 366 at r11 (raw file):

);

statement count 4

nit: statement ok is fine here and below IMO - you're not testing INSERT so you don't need additional assertions


pkg/sql/logictest/testdata/logic_test/delete line 376 at r11 (raw file):


query ITI rowsort
SELECT * FROM u_a;

nit: you can remove this SELECT test


pkg/sql/logictest/testdata/logic_test/delete line 383 at r11 (raw file):

4  d  40

#Test a join with a filter

nit: space after #


pkg/sql/logictest/testdata/logic_test/delete line 462 at r11 (raw file):

# Test aliased table names, ORDER BY and LIMIT where ORDER BY references the target
# table.
query ITIIT rowsort

nit: remove rowsort since you have the ORDER BY in the query


pkg/sql/logictest/testdata/logic_test/delete line 483 at r11 (raw file):


query ITI rowsort
SELECT * FROM u_a;

nit: this looks redundant


pkg/sql/logictest/testdata/logic_test/delete line 528 at r11 (raw file):

CREATE TABLE pindex (
  a DECIMAL (10, 2),
  CHECK (a > 0)

There's no partial index here, and no need for the check constraint.


pkg/sql/opt/exec/explain/testdata/gists line 746 at r11 (raw file):

opt
DELETE FROM foo WHERE a = 1
----

nit: this looks like left-over from testing that you can remove


pkg/sql/opt/optbuilder/testdata/delete line 754 at r2 (raw file):

Previously, faizaanmadhani (Faizaan Madhani) wrote…

Done. Test case maintained but comment changed and test to check partial indexes added below.

CHECK constraints aren't checked during deletes, so you can remove the CHECK, add the parital index directly into this CREATE TABLE, and remove the test directly below this one. I'd update the table name to match too.


pkg/sql/opt/optbuilder/testdata/delete line 800 at r10 (raw file):

DELETE FROM abcde AS foo USING xyz AS bar WHERE bar.y > 0 ORDER BY foo.a DESC LIMIT 5;
----
error (42P10): SELECT DISTINCT ON expressions must match initial ORDER BY expressions

This should succeed, shouldn't it?


pkg/sql/opt/optbuilder/testdata/delete line 1106 at r10 (raw file):

      │              └── column2:14
      └── projections
           └── h:9 > 3 [as=partial_index_del1:15]

It'd be good to add a test where the target table has a compound primary key (more than 1 PK column, e.g. CREATE TABLE (h INT, i INT, j INT, PRIMARY KEY (h, i))) to ensure that the distinct-on groups by all the PK columns.

@faizaanmadhani
Copy link
Contributor Author

pkg/sql/opt/optbuilder/testdata/delete line 800 at r10 (raw file):

Previously, mgartner (Marcus Gartner) wrote…

This should succeed, shouldn't it?

(I think) It should. We have a similar logictest that also uses the target table in the ORDER BY that passes, so this is confusing.

After some digging, I figured out that this only happens if we're ordering on the non-primary key column of the target table while having a USING. Existing functionality does allow to do an ORDER BY with a non-primary key column in DELETE statements without USING clauses.

Similarly, if we check UPDATE ... FROM doing an ORDER BY with a column of a table that is not a primary key, this works so this test should pass.

@faizaanmadhani faizaanmadhani force-pushed the faizaan/delete-from-using branch 2 times, most recently from 301c6f3 to 45a9d39 Compare October 14, 2022 15:34
Previously, the optbuilder would return an error when
given sql statements of the form `DELETE FROM USING`.
This commit adds support to the Optbuilder to build query
plans for statements of the form `DELETE FROM ... USING`.

Release note: None
@faizaanmadhani faizaanmadhani force-pushed the faizaan/delete-from-using branch from 45a9d39 to ad2e1c6 Compare October 14, 2022 16:04
Copy link
Contributor Author

@faizaanmadhani faizaanmadhani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @mgartner and @michae2)


pkg/sql/delete.go line 165 at r11 (raw file):

Previously, mgartner (Marcus Gartner) wrote…

To keep d.run.partialIndexDelValsOffset true to its name, I think we should update it's value here instead:

partialIndexDelValsOffset: len(rd.FetchCols),

And the comment for it here shuld be updated too:

// partialIndexDelValsOffset is the offset of partial index delete
// indicators in the source values. It is equal to the number of fetched
// columns.

Done.


pkg/sql/logictest/testdata/logic_test/delete line 528 at r11 (raw file):

Previously, mgartner (Marcus Gartner) wrote…

There's no partial index here, and no need for the check constraint.

Yup. I changed it to a partial index and modified the test for it


pkg/sql/opt/optbuilder/testdata/delete line 754 at r2 (raw file):

Previously, mgartner (Marcus Gartner) wrote…

CHECK constraints aren't checked during deletes, so you can remove the CHECK, add the parital index directly into this CREATE TABLE, and remove the test directly below this one. I'd update the table name to match too.

Done. Removed the check constraint test below and added a partial index test using table name pindex


pkg/sql/opt/optbuilder/testdata/delete line 800 at r10 (raw file):

Previously, faizaanmadhani (Faizaan Madhani) wrote…

(I think) It should. We have a similar logictest that also uses the target table in the ORDER BY that passes, so this is confusing.

After some digging, I figured out that this only happens if we're ordering on the non-primary key column of the target table while having a USING. Existing functionality does allow to do an ORDER BY with a non-primary key column in DELETE statements without USING clauses.

Similarly, if we check UPDATE ... FROM doing an ORDER BY with a column of a table that is not a primary key, this works so this test should pass.

Resolved offline. While this should ideally succeed this failure happens when we do ORDER BY on non-primary key columns in a USING in tables when primary keys are present . This is related to this issue that's also present in UPDATE ... FROM: #89817

@faizaanmadhani faizaanmadhani force-pushed the faizaan/delete-from-using branch 2 times, most recently from a8a767c to 62634ff Compare October 14, 2022 17:08
@faizaanmadhani faizaanmadhani requested review from a team and rytaft October 17, 2022 13:44
Copy link
Collaborator

@mgartner mgartner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! :lgtm: just left one test suggestion.

Reviewed 12 of 12 files at r12, 9 of 10 files at r13, 1 of 1 files at r14, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @faizaanmadhani, @michae2, and @rytaft)


pkg/sql/logictest/testdata/logic_test/delete line 527 at r14 (raw file):

3.00
4.00
8.00

nit: add a test for SELECT a FROM pindex@pindex_a_idx WHERE a > 3 to verify that scanning the partial index yields the same results as the full table scan here.

Previously, while the optimizer would generate query plans
for sql statements of the form `DELETE FROM ... USING`
executing the statement in an instance of CockroachDB
may return errors, particularly with statements that
included `RETURNING` clauses. This commit adds support to
the execbuilder to execute statements of the form
`DELETE FROM ... USING`.

Release note (sql change): CockroachDB now supports
executing statements of the form `DELETE FROM ... USING`.
@faizaanmadhani faizaanmadhani force-pushed the faizaan/delete-from-using branch from 62634ff to 74ed574 Compare October 17, 2022 20:25
@faizaanmadhani
Copy link
Contributor Author

TFTR!!!! 🎉

bors r+

@craig
Copy link
Contributor

craig bot commented Oct 17, 2022

Build succeeded:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

sql: support DELETE FROM ... USING
3 participants