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

executor: ignore foreign key error in UPDATE/INSERT/DELETE ignore #56682

Merged
merged 1 commit into from
Oct 28, 2024

Conversation

YangKeao
Copy link
Member

What problem does this PR solve?

Issue Number: close #56678, close #56681, close #39712

Problem Summary:

In the following cases, the foreign key error should be omitted and placed in the warning:

  1. INSERT IGNORE ... ON DUPLICATE UPDATE ...
  2. DELETE IGNORE ...
  3. UPDATE IGNORE ...

What changed and how does it work?

Introduce a function checkFKIgnoreErr to check rows explicitly, and use this function to check each rows in these three cases.

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No need to test
    • I checked and no code files have been changed.

Release note

Please refer to Release Notes Language Style Guide to write a quality release note.

None

@ti-chi-bot ti-chi-bot bot added release-note-none Denotes a PR that doesn't merit a release note. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. sig/planner SIG: Planner labels Oct 16, 2024
@YangKeao YangKeao requested review from hawkingrei and removed request for lcwangchao October 16, 2024 08:26
Copy link

codecov bot commented Oct 16, 2024

Codecov Report

Attention: Patch coverage is 69.62025% with 24 lines in your changes missing coverage. Please review.

Project coverage is 56.6417%. Comparing base (73584bb) to head (40ef448).
Report is 46 commits behind head on master.

Additional details and impacted files
@@                Coverage Diff                @@
##             master     #56682         +/-   ##
=================================================
- Coverage   73.3422%   56.6417%   -16.7005%     
=================================================
  Files          1630       1761        +131     
  Lines        450364     640407     +190043     
=================================================
+ Hits         330307     362738      +32431     
- Misses        99779     253438     +153659     
- Partials      20278      24231       +3953     
Flag Coverage Δ
integration 37.0785% <69.6202%> (?)
unit 72.4935% <62.0253%> (+0.0242%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
dumpling 52.9478% <ø> (ø)
parser ∅ <ø> (∅)
br 52.4485% <ø> (+6.8890%) ⬆️

Comment on lines 135 to 145
if e.ignoreErr {
fkToBeCheckedRows[0] = toBeCheckedRow{row: datumRow, ignored: false}
err := checkFKIgnoreErr(ctx, e.Ctx(), e.fkChecks[tbl.Meta().ID], fkToBeCheckedRows)
if err != nil {
return err
}

// meets an error, skip this row.
if fkToBeCheckedRows[0].ignored {
datumRow = datumRow[:0]
continue
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

How about move this into removeRow?
this is duplicate with #251~#263

Copy link
Member Author

@YangKeao YangKeao Oct 21, 2024

Choose a reason for hiding this comment

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

I prefer to keep it outside. If we move it inside the removeRow, we'll need to handle the error carefully to distinguish the error, the ignored error and success. (And also, these logic are also duplicated in insert / update, and it's also not easy to have an easy-to-understand abstration for all of them).

Instead, I change the declaration / implementation of checkFKIgnoreErr to integrate the declaration and usage of the fkToBeCheckedRows[0] usage, and make it easier to understand / use:

func checkFKIgnoreErr(ctx context.Context, sctx sessionctx.Context, fkChecks []*FKCheckExec, row []types.Datum) (bool, error)

Now, only the row is needed, and it'll return whether an error has been ignored, or there is an unignorable error. The usage can be simpler:

			if e.ignoreErr {
				ignored, err := checkFKIgnoreErr(ctx, e.Ctx(), e.fkChecks[tbl.Meta().ID], datumRow)
				if err != nil {
					return err
				}

				// meets an error, skip this row.
				if ignored {
					datumRow = datumRow[:0]
					continue
				}
			}

if e.ignoreErr {
ignored, err := checkFKIgnoreErr(ctx, e.Ctx(), e.fkChecks[id], val.handleVal)
if err != nil {
return false
Copy link
Contributor

Choose a reason for hiding this comment

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

If return from here, will ignore the error?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes. It's expected. See the implementation of checkFKIgnoreErr and fkc.checkRows. If the row breaks the foreign key constraint, a warning will be appended and the function checkFKIgnoreErr will return true, nil. If the row has a wrong format, or it meets some network issues (or other errors not related to the foreign key), it'll return the error as false, err.

Previously, we already have an existing implementation of INSERT ignore. This PR keeps the same behavior with it.

pkg/executor/delete.go Outdated Show resolved Hide resolved
Signed-off-by: Yang Keao <yangkeao@chunibyo.icu>
@ti-chi-bot ti-chi-bot bot added approved needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Oct 28, 2024
@YangKeao
Copy link
Member Author

/retest

@YangKeao YangKeao requested a review from crazycs520 October 28, 2024 07:26
Copy link

ti-chi-bot bot commented Oct 28, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: crazycs520, hawkingrei

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@ti-chi-bot ti-chi-bot bot added lgtm and removed needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Oct 28, 2024
Copy link

ti-chi-bot bot commented Oct 28, 2024

[LGTM Timeline notifier]

Timeline:

  • 2024-10-28 06:35:25.764892181 +0000 UTC m=+245238.604047724: ☑️ agreed by hawkingrei.
  • 2024-10-28 08:49:16.512901962 +0000 UTC m=+253269.352057508: ☑️ agreed by crazycs520.

@ti-chi-bot ti-chi-bot bot merged commit 2de388b into pingcap:master Oct 28, 2024
18 of 23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved lgtm release-note-none Denotes a PR that doesn't merit a release note. sig/planner SIG: Planner size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.
Projects
None yet
3 participants