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: fix trigger interaction with computed columns #133328

Merged
merged 1 commit into from
Nov 6, 2024

Conversation

DrewKimball
Copy link
Collaborator

This commit makes the following changes to the way row-level BEFORE triggers interact with computed columns:

  • BEFORE triggers now observe NULL values in place of computed columns.
  • Modifications to computed columns by BEFORE triggers are ignored.
  • Computed columns are re-computed after row-level BEFORE triggers execute.

These changes make the behavior consistent with that of Postgres.

Fixes #132979

Release note: None

@DrewKimball DrewKimball requested a review from a team October 24, 2024 06:07
@DrewKimball DrewKimball requested a review from a team as a code owner October 24, 2024 06:07
@DrewKimball DrewKimball requested review from mw5h and removed request for a team October 24, 2024 06:07
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@DrewKimball DrewKimball requested a review from mgartner October 24, 2024 06:08
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 all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @DrewKimball and @mw5h)


pkg/sql/opt/optbuilder/trigger.go line 136 at r1 (raw file):

	// the computed columns.
	if eventType == tree.TriggerEventInsert || eventType == tree.TriggerEventUpdate {
		mb.recomputeComputedColsForTrigger(eventType)

I think an optbiulder test or two for this would be useful to show how this recomputation is planned.


pkg/sql/opt/optbuilder/trigger.go line 830 at r1 (raw file):

// hasRowLevelTriggers returns true if the table has any row-level triggers that
// match the given action time and event type.
func (mb *mutationBuilder) hasRowLevelTriggers(

This function is not used in this PR - remove it?


pkg/ccl/logictestccl/testdata/logic_test/triggers line 2601 at r1 (raw file):

# see NULLs. In addition, while a BEFORE trigger can modify a computed column,
# the changed value is ignored. In contrast, the computed column *does* show
# the effect of modifications to the columns it depends on.

Does a WHEN expression see NULLs for the computed column, or the pre-trigger value of the column? Can you add some tests for this case?

Copy link
Collaborator Author

@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.

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


pkg/sql/opt/optbuilder/trigger.go line 136 at r1 (raw file):

Previously, mgartner (Marcus Gartner) wrote…

I think an optbiulder test or two for this would be useful to show how this recomputation is planned.

Good idea, Done.


pkg/sql/opt/optbuilder/trigger.go line 830 at r1 (raw file):

Previously, mgartner (Marcus Gartner) wrote…

This function is not used in this PR - remove it?

🤦 Oops, removed it.


pkg/ccl/logictestccl/testdata/logic_test/triggers line 2601 at r1 (raw file):

Previously, mgartner (Marcus Gartner) wrote…

Does a WHEN expression see NULLs for the computed column, or the pre-trigger value of the column? Can you add some tests for this case?

It sees NULLs, I'll add some tests.

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: Nicely done!

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

@DrewKimball
Copy link
Collaborator Author

TFTR!

bors r+

@DrewKimball DrewKimball added the backport-24.3.x Flags PRs that need to be backported to 24.3 label Nov 6, 2024
@craig
Copy link
Contributor

craig bot commented Nov 6, 2024

Build failed (retrying...):

@DrewKimball
Copy link
Collaborator Author

bors r-

@craig
Copy link
Contributor

craig bot commented Nov 6, 2024

Canceled.

@DrewKimball
Copy link
Collaborator Author

Needed a rebase.

This commit makes the following changes to the way row-level BEFORE
triggers interact with computed columns:
* BEFORE triggers now observe NULL values in place of computed columns.
* Modifications to computed columns by BEFORE triggers are ignored.
* Computed columns are re-computed after row-level BEFORE triggers
  execute.

These changes make the behavior consistent with that of Postgres.

Fixes cockroachdb#132979

Release note: None
@mgartner
Copy link
Collaborator

mgartner commented Nov 6, 2024

bors r+

@craig craig bot merged commit f5f5cbc into cockroachdb:master Nov 6, 2024
23 checks passed
Copy link

blathers-crl bot commented Nov 6, 2024

Based on the specified backports for this PR, I applied new labels to the following linked issue(s). Please adjust the labels as needed to match the branches actually affected by the issue(s), including adding any known older branches.


Issue #132979: branch-release-24.3.


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

Copy link

blathers-crl bot commented Nov 6, 2024

Encountered an error creating backports. Some common things that can go wrong:

  1. The backport branch might have already existed.
  2. There was a merge conflict.
  3. The backport branch contained merge commits.

You might need to create your backport manually using the backport tool.


error creating merge commit from 43d6d62 to blathers/backport-release-24.3-133328: POST https://api.github.com/repos/cockroachdb/cockroach/merges: 409 Merge conflict []

you may need to manually resolve merge conflicts with the backport tool.

Backport to branch 24.3.x failed. See errors above.


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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-24.3.x Flags PRs that need to be backported to 24.3
Projects
None yet
Development

Successfully merging this pull request may close these issues.

sql: incorrect handling for computed columns in row-level BEFORE triggers
3 participants