-
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: add support for DELETE FROM ... USING
#88974
sql: add support for DELETE FROM ... USING
#88974
Conversation
d25f22e
to
7d401f1
Compare
DELETE FROM ... USING
1823ff3
to
c129904
Compare
1589799
to
222eb09
Compare
84f2746
to
54176eb
Compare
4fa4fd9
to
9c19507
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: 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, movingsetFetchColIDs
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).
9c19507
to
4947e95
Compare
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
4947e95
to
7c63889
Compare
There was a problem hiding this 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: 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:
cockroach/pkg/sql/opt_exec_factory.go
Line 1699 in 3f87f2f
partialIndexDelValsOffset: len(rd.FetchCols), |
And the comment for it here shuld be updated too:
Lines 52 to 54 in 961e66f
// 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.
Previously, mgartner (Marcus Gartner) 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 Similarly, if we check |
301c6f3
to
45a9d39
Compare
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
45a9d39
to
ad2e1c6
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: 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:cockroach/pkg/sql/opt_exec_factory.go
Line 1699 in 3f87f2f
partialIndexDelValsOffset: len(rd.FetchCols), And the comment for it here shuld be updated too:
Lines 52 to 54 in 961e66f
// 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 withoutUSING
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
a8a767c
to
62634ff
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work! 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: 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`.
62634ff
to
74ed574
Compare
TFTR!!!! 🎉 bors r+ |
Build succeeded: |
See commit messages for details.
Resolves: #40963