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 foreign key cascades in udfs #110396

Merged

Conversation

rharding6373
Copy link
Collaborator

This commit adds testing and makes some fixes to support foreign key cascades in UDFs.

Epic: CRDB-25388
Informs: #87289

Release note: none

@cockroach-teamcity
Copy link
Member

This change is Reviewable

@rharding6373 rharding6373 force-pushed the 20230607_udf_fk_cascade_87289 branch 3 times, most recently from 658fed9 to 41d81f0 Compare September 14, 2023 04:27
@rharding6373 rharding6373 marked this pull request as ready for review September 14, 2023 14:01
@rharding6373 rharding6373 requested a review from a team as a code owner September 14, 2023 14:01
Copy link
Member

@yuzefovich yuzefovich left a comment

Choose a reason for hiding this comment

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

Nice! I only have a few nits.

Reviewed 3 of 3 files at r1, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @DrewKimball, @mgartner, and @rharding6373)


pkg/sql/logictest/testdata/logic_test/udf_fk line 258 at r1 (raw file):


statement ok
SELECT f_fk_p_cascade(1, 2);

nit: since we use RETURNING * in our UDFs might as well as double-check what's being returned in all cases, and not only when multiple UDFs are invoked in a single stmt.


pkg/sql/logictest/testdata/logic_test/udf_fk line 424 at r1 (raw file):

query IT rowsort
SELECT
	(SELECT * FROM (VALUES ((SELECT x FROM (VALUES (1)) AS s (x)) + y))),

nit: there are tabs here (which I think we generally try to avoid (?)).


pkg/sql/logictest/testdata/logic_test/udf_fk line 446 at r1 (raw file):

12
13

nit: it might be interesting to a test case where the first invocation of mutating UDF succeeds but the second one fails, so the stmt errors out and ensure that the tables aren't modified.

It also might be good to have a test case where we have a cascade and a non-cascade FK check (e.g. we have parent_cascade and child_cascade linked by CASCADE, but also we have grandchild_fk which has FK with child_cascade but without CASCADE (or something along those lines)). WDYT?

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.

:LGTM: Nice! Cool to see that it just sort of worked with minimal changes after the other work you put into this. I left a few suggestions for some additional tests, if you agree they could be valuable.

Reviewed 3 of 3 files at r1, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @DrewKimball, @rharding6373, and @yuzefovich)


pkg/sql/logictest/testdata/logic_test/udf_fk line 446 at r1 (raw file):

Previously, yuzefovich (Yahor Yuzefovich) wrote…

nit: it might be interesting to a test case where the first invocation of mutating UDF succeeds but the second one fails, so the stmt errors out and ensure that the tables aren't modified.

It also might be good to have a test case where we have a cascade and a non-cascade FK check (e.g. we have parent_cascade and child_cascade linked by CASCADE, but also we have grandchild_fk which has FK with child_cascade but without CASCADE (or something along those lines)). WDYT?

To add to those suggestions, these tests might be interesting:

  • A UDF with multiple statements that cascade to the same table.
  • An update/delete that cascades to multiple rows in the same table (can add this to one of the setups above pretty easily I think)
  • A self-referencing FK cascade (a table with a foreign key on the same table)

Copy link
Collaborator

@DrewKimball DrewKimball left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewed 3 of 3 files at r1, all commit messages.
Reviewable status: :shipit: complete! 2 of 0 LGTMs obtained (waiting on @rharding6373 and @yuzefovich)


pkg/sql/apply_join.go line 341 at r1 (raw file):

		// when executing the outer query.
		defer func() {
			plan.subqueryPlans = nil

It's not obvious where subqueryPlans becomes non-nil since we check len(plan.subqueryPlans) != 0. Can you clarify that?

Copy link
Collaborator Author

@rharding6373 rharding6373 left a comment

Choose a reason for hiding this comment

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

TFTRs!

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 2 stale) (waiting on @DrewKimball, @mgartner, and @yuzefovich)


pkg/sql/logictest/testdata/logic_test/udf_fk line 258 at r1 (raw file):

Previously, yuzefovich (Yahor Yuzefovich) wrote…

nit: since we use RETURNING * in our UDFs might as well as double-check what's being returned in all cases, and not only when multiple UDFs are invoked in a single stmt.

Done.


pkg/sql/logictest/testdata/logic_test/udf_fk line 424 at r1 (raw file):

Previously, yuzefovich (Yahor Yuzefovich) wrote…

nit: there are tabs here (which I think we generally try to avoid (?)).

Fixed.


pkg/sql/logictest/testdata/logic_test/udf_fk line 446 at r1 (raw file):

Previously, mgartner (Marcus Gartner) wrote…

To add to those suggestions, these tests might be interesting:

  • A UDF with multiple statements that cascade to the same table.
  • An update/delete that cascades to multiple rows in the same table (can add this to one of the setups above pretty easily I think)
  • A self-referencing FK cascade (a table with a foreign key on the same table)

Thanks for the suggestions! Added these tests. Want to note (since Marcus and I were discussing this offline) that the self-referencing FK cascade test is correct -- I thought it wasn't compared to postgres, but I was accidentally omitting the cascades.

Copy link
Member

@yuzefovich yuzefovich left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewed 1 of 1 files at r2, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (and 2 stale) (waiting on @rharding6373)


pkg/sql/logictest/testdata/logic_test/udf_fk line 309 at r2 (raw file):


statement ok
DROP TABLE child_cascade;

nit: looks like we're recreating child_cascade table with the same schema, why not use TRUNCATE?

This commit adds testing and makes some fixes to support foreign key
cascades in UDFs.

Epic: CRDB-25388
Informs: cockroachdb#87289

Release note: none
Copy link
Collaborator Author

@rharding6373 rharding6373 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 (and 3 stale) (waiting on @yuzefovich)


pkg/sql/logictest/testdata/logic_test/udf_fk line 309 at r2 (raw file):

Previously, yuzefovich (Yahor Yuzefovich) wrote…

nit: looks like we're recreating child_cascade table with the same schema, why not use TRUNCATE?

They aren't actually the same. The first version doesn't have UNIQUE, allowing us to test cascading updates to multiple rows. We need UNIQUE when we cascade to the grandchild_cascade table. I added some comments to the test to help explain what each series of statements is designed to test in the hopes it will clarify this in the future.

Copy link
Member

@yuzefovich yuzefovich left a comment

Choose a reason for hiding this comment

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

Reviewed all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 3 stale) (waiting on @rharding6373)


pkg/sql/logictest/testdata/logic_test/udf_fk line 309 at r2 (raw file):

Previously, rharding6373 (Rachael Harding) wrote…

They aren't actually the same. The first version doesn't have UNIQUE, allowing us to test cascading updates to multiple rows. We need UNIQUE when we cascade to the grandchild_cascade table. I added some comments to the test to help explain what each series of statements is designed to test in the hopes it will clarify this in the future.

I see, thanks!

@rharding6373
Copy link
Collaborator Author

TFTRs!

bors r+

@craig
Copy link
Contributor

craig bot commented Sep 20, 2023

Build succeeded:

@craig craig bot merged commit 2a90855 into cockroachdb:master Sep 20, 2023
8 checks passed
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.

None yet

5 participants