Skip to content

Conversation

YangKeao
Copy link
Member

@YangKeao YangKeao commented Jun 3, 2025

What problem does this PR solve?

Issue Number: close #61350

Problem Summary:

The length returned by TiDB is not compatible with MySQL. Sometimes it's too small so that the client may fail to allocate big enough buffer and cause panic/security issue. This PR modifies the logic of calculating the length, and make it compatible with MySQL for most of the cases.

What changed and how does it work?

  1. Change the logic of calculating length for casting many types to string.

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.

Run the SQLs provided in #61350 and check the results are the same.

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 the issue that the length returned by `cast` function is not compatible with MySQL.

@ti-chi-bot ti-chi-bot bot added release-note-none Denotes a PR that doesn't merit a release note. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. release-note Denotes a PR that will be considered when it comes time to generate release notes. and removed release-note-none Denotes a PR that doesn't merit a release note. labels Jun 3, 2025
@YangKeao
Copy link
Member Author

YangKeao commented Jun 4, 2025

Oops. I found it also affects the behavior. For example:

select binary(123)

Will return different values with/without this PR. I should be more careful.

/hold

@ti-chi-bot ti-chi-bot bot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Jun 4, 2025
@YangKeao YangKeao force-pushed the fix-61350 branch 3 times, most recently from dc73f67 to 9a5c067 Compare July 2, 2025 09:00
@ti-chi-bot ti-chi-bot bot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jul 2, 2025
@YangKeao YangKeao force-pushed the fix-61350 branch 2 times, most recently from f0876f2 to be36271 Compare July 2, 2025 09:07

// Only estimate the length for variable length string types, because different length for fixed
// length string types will have different behaviors and may cause compatibility issues.
if retFt.GetType() == mysql.TypeString {
Copy link
Member Author

Choose a reason for hiding this comment

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

select binary(123) will use cast to return TypeString. However, it's also not compatible with MySQL because MySQL also returns TypeVarString. Maybe we should modify it to TypeVarString in parser later.

BTW, binary function is deprecated by MySQL.

@YangKeao YangKeao force-pushed the fix-61350 branch 4 times, most recently from 4f70a7f to 7207764 Compare July 2, 2025 12:15
@YangKeao
Copy link
Member Author

YangKeao commented Jul 2, 2025

/retest

@YangKeao YangKeao force-pushed the fix-61350 branch 2 times, most recently from 794f99d to d781589 Compare July 2, 2025 13:23
@YangKeao YangKeao changed the title expression: fix the length of casting from INT/REAL/DECIMAL/.... to string expression: fix the length of casting from INT/REAL/DECIMAL/.... to string | tidb-test=pr/2549 Jul 2, 2025
@YangKeao YangKeao force-pushed the fix-61350 branch 4 times, most recently from 6d1629a to dd91f59 Compare July 4, 2025 03:18
@ti-chi-bot ti-chi-bot bot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. and removed size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Jul 4, 2025
@ti-chi-bot ti-chi-bot bot added lgtm and removed needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Jul 9, 2025
Copy link

ti-chi-bot bot commented Jul 9, 2025

[LGTM Timeline notifier]

Timeline:

  • 2025-07-09 06:28:07.310503324 +0000 UTC m=+2067540.033682307: ☑️ agreed by windtalker.
  • 2025-07-09 07:18:27.870877992 +0000 UTC m=+2070560.594056974: ☑️ agreed by xhebox.

@YangKeao YangKeao requested review from AilinKid and bb7133 July 9, 2025 07:40
Copy link

ti-chi-bot bot commented Jul 9, 2025

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: AilinKid, windtalker, xhebox

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 the approved label Jul 9, 2025
@YangKeao
Copy link
Member Author

YangKeao commented Jul 9, 2025

/retest

// decimal representation. It's not necessary to keep all decimals. Ref:
// - https://github.com/ulfjack/ryu
// - https://dl.acm.org/doi/10.1145/93548.93559
// So maybe 48/327 is enough for float/double, but we still set 87/370 for safety.
Copy link
Contributor

Choose a reason for hiding this comment

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

nice explaination

@YangKeao
Copy link
Member Author

YangKeao commented Jul 9, 2025

/retest

2 similar comments
@purelind
Copy link
Contributor

purelind commented Jul 9, 2025

/retest

@purelind
Copy link
Contributor

purelind commented Jul 9, 2025

/retest

@ti-chi-bot ti-chi-bot bot merged commit 3c2dc46 into pingcap:master Jul 9, 2025
27 checks passed
ti-chi-bot pushed a commit to ti-chi-bot/tidb that referenced this pull request Jul 9, 2025
@ti-chi-bot
Copy link
Member

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

@hawkingrei
Copy link
Member

/cherrypick release-8.1
/cherrypick release-7.5

ti-chi-bot pushed a commit to ti-chi-bot/tidb that referenced this pull request Jul 23, 2025
@ti-chi-bot
Copy link
Member

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

In response to this:

/cherrypick release-8.1
/cherrypick 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.

@ti-chi-bot
Copy link
Member

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

In response to this:

/cherrypick release-8.1
/cherrypick 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.

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-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/XL Denotes a PR that changes 500-999 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

incompatible with mysql in column-type-info when use cast()
7 participants