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

expression: fix tikv crash when bool like cast(bit as char) #57484

Merged
merged 4 commits into from
Nov 20, 2024

Conversation

lcwangchao
Copy link
Collaborator

What problem does this PR solve?

Issue Number: close #56494

What changed and how does it work?

When casting the bit type to a non binary string, this PR first convert bits to binary string first. This can ensure HandleBinaryLiteral can be used in the following code and convert binary to the specified collation with right behavior.

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.

Side effects

  • Performance regression: Consumes more CPU
  • Performance regression: Consumes more Memory
  • Breaking backward compatibility

Documentation

  • Affects user behaviors
  • Contains syntax changes
  • Contains variable changes
  • Contains experimental features
  • Changes MySQL compatibility

Release note

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

fix tikv crash when `bool like cast(bit as char)`

@ti-chi-bot ti-chi-bot bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Nov 19, 2024
Copy link

tiprow bot commented Nov 19, 2024

Hi @lcwangchao. Thanks for your PR.

PRs from untrusted users cannot be marked as trusted with /ok-to-test in this repo meaning untrusted PR authors can never trigger tests themselves. Collaborators can still trigger tests on the PR using /test all.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

Copy link

codecov bot commented Nov 19, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 73.3407%. Comparing base (a22b842) to head (561c9d1).
Report is 516 commits behind head on master.

Additional details and impacted files
@@               Coverage Diff                @@
##             master     #57484        +/-   ##
================================================
+ Coverage   72.8488%   73.3407%   +0.4918%     
================================================
  Files          1672       1676         +4     
  Lines        462666     470868      +8202     
================================================
+ Hits         337047     345338      +8291     
+ Misses       104821     104711       -110     
- Partials      20798      20819        +21     
Flag Coverage Δ
integration 43.5485% <100.0000%> (?)
unit 72.4417% <100.0000%> (+0.2200%) ⬆️

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

Components Coverage Δ
dumpling 52.7673% <ø> (ø)
parser ∅ <ø> (∅)
br 43.3494% <ø> (-1.7732%) ⬇️

@lcwangchao
Copy link
Collaborator Author

/retest

Copy link

tiprow bot commented Nov 19, 2024

@lcwangchao: Cannot trigger testing until a trusted user reviews the PR and leaves an /ok-to-test message.

In response to this:

/retest

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@ti-chi-bot ti-chi-bot bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Nov 19, 2024
@lcwangchao
Copy link
Collaborator Author

/retest

Copy link

tiprow bot commented Nov 19, 2024

@lcwangchao: Cannot trigger testing until a trusted user reviews the PR and leaves an /ok-to-test message.

In response to this:

/retest

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@ti-chi-bot ti-chi-bot bot added the sig/planner SIG: Planner label Nov 19, 2024
@ti-chi-bot ti-chi-bot bot added the needs-1-more-lgtm Indicates a PR needs 1 more LGTM. label Nov 20, 2024
Copy link
Contributor

@qw4990 qw4990 left a comment

Choose a reason for hiding this comment

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

LGTM for the optimizer part.

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

ti-chi-bot bot commented Nov 20, 2024

[LGTM Timeline notifier]

Timeline:

  • 2024-11-20 02:02:59.991861831 +0000 UTC m=+1012942.182730827: ☑️ agreed by Defined2014.
  • 2024-11-20 02:11:54.169677522 +0000 UTC m=+1013476.360546519: ☑️ agreed by qw4990.

sig.setPbCode(tipb.ScalarFuncSig_CastStringAsString)
return sig, nil
if ft := args[0].GetType(ctx.GetEvalCtx()); ft.Hybrid() {
castBitAsUnBinary := ft.GetType() == mysql.TypeBit && c.tp.GetCharset() != charset.CharsetBin
Copy link
Contributor

Choose a reason for hiding this comment

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

For other type that store binary string like Blob, will TiDB meet the same problem?

Copy link
Collaborator Author

@lcwangchao lcwangchao Nov 20, 2024

Choose a reason for hiding this comment

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

No, only the "hybrid" types will address this issue. Their "EvalType" may return ETInt, but when converting them to strings, we should be recognized as bytes instead.

I did not handle other hybrid types like enum and set in this PR, because in most time their should have a legal encoding and another reason is to introduce less logic change in this fix.

castTp := types.NewFieldType(mysql.TypeString)
castTp.SetCharset(charset.CharsetBin)
castTp.SetCollate(charset.CollationBin)
castByteAsStringFunc, err := NewFunction(mock.NewContext(), ast.Cast, castTp, byteColumn)
Copy link
Contributor

Choose a reason for hiding this comment

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

This function can not be push down to TiKV because from_binary can not be push down to TiKV?

Copy link
Collaborator Author

@lcwangchao lcwangchao Nov 20, 2024

Choose a reason for hiding this comment

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

no, I think it's the issue: #51877 The tidb have some special logic for function conv. When converting a BIT type, it uses conv(cast(bit as char)) which uses cast to cast bit to string, but conv should also handles the string as a binary number instead of string...

See:

if x.FuncName.L == ast.Cast {
arg0 := x.GetArgs()[0]
if arg0.GetType(ctx).Hybrid() || IsBinaryLiteral(arg0) {
str, isNull, err = arg0.EvalString(ctx, row)
if isNull || err != nil {
return str, isNull, err
}
d := types.NewStringDatum(str)
str = d.GetBinaryLiteral().ToBitLiteralString(true)
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I added a comment in this test

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This test is not related with this issue, but we should update it to set to casted type as bin collations to make the test pass.

Copy link
Member

@YangKeao YangKeao left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link

tiprow bot commented Nov 20, 2024

@lcwangchao: The following test failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
fast_test_tiprow df5cace link true /test fast_test_tiprow

Full PR test history. Your PR dashboard.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here.

@ti-chi-bot
Copy link
Member

In response to a cherrypick label: new pull request created to branch release-7.5: #59290.
But this PR has conflicts, please resolve them!

@lcwangchao lcwangchao added the needs-cherry-pick-release-8.1 Should cherry pick this PR to release-8.1 branch. label Feb 7, 2025
@ti-chi-bot
Copy link
Member

In response to a cherrypick label: new pull request created to branch release-8.1: #59291.
But this PR has conflicts, please resolve them!

ti-chi-bot pushed a commit to ti-chi-bot/tidb that referenced this pull request Feb 7, 2025
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
@lcwangchao
Copy link
Collaborator Author

/cherry-pick release-7.5

ti-chi-bot pushed a commit to ti-chi-bot/tidb that referenced this pull request Feb 7, 2025
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
@ti-chi-bot
Copy link
Member

@lcwangchao: new pull request created to branch release-7.5: #59314.
But this PR has conflicts, please resolve them!

In response to this:

/cherry-pick release-7.5

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the ti-community-infra/tichi repository.

@lcwangchao
Copy link
Collaborator Author

/cherry-pick release-8.1

@ti-chi-bot
Copy link
Member

@lcwangchao: new pull request created to branch release-8.1: #59315.
But this PR has conflicts, please resolve them!

In response to this:

/cherry-pick release-8.1

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the ti-community-infra/tichi repository.

ti-chi-bot pushed a commit to ti-chi-bot/tidb that referenced this pull request Feb 7, 2025
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
@lcwangchao
Copy link
Collaborator Author

/cherry-pick release-7.1

@ti-chi-bot
Copy link
Member

@lcwangchao: new pull request created to branch release-7.1: #59316.
But this PR has conflicts, please resolve them!

In response to this:

/cherry-pick release-7.1

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the ti-community-infra/tichi repository.

ti-chi-bot pushed a commit to ti-chi-bot/tidb that referenced this pull request Feb 7, 2025
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
@lcwangchao
Copy link
Collaborator Author

/cherry-pick release-8.1

ti-chi-bot pushed a commit to ti-chi-bot/tidb that referenced this pull request Feb 7, 2025
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
@ti-chi-bot
Copy link
Member

@lcwangchao: new pull request created to branch release-8.1: #59321.
But this PR has conflicts, please resolve them!

In response to this:

/cherry-pick release-8.1

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the ti-community-infra/tichi repository.

@lcwangchao
Copy link
Collaborator Author

/cherry-pick release-6.5

ti-chi-bot pushed a commit to ti-chi-bot/tidb that referenced this pull request Feb 7, 2025
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
@ti-chi-bot
Copy link
Member

@lcwangchao: new pull request created to branch release-6.5: #59324.
But this PR has conflicts, please resolve them!

In response to this:

/cherry-pick release-6.5

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the ti-community-infra/tichi repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved lgtm needs-cherry-pick-release-6.5 Should cherry pick this PR to release-6.5 branch. needs-cherry-pick-release-7.5 Should cherry pick this PR to release-7.5 branch. needs-cherry-pick-release-8.1 Should cherry pick this PR to release-8.1 branch. needs-cherry-pick-release-8.5 Should cherry pick this PR to release-8.5 branch. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/planner SIG: Planner size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[FATAL] [lib.rs:478] ["range end index 4 out of range for slice of length 3"]
6 participants