Skip to content

Conversation

@ChangRui-Ryan
Copy link
Contributor

@ChangRui-Ryan ChangRui-Ryan commented Aug 25, 2025

What problem does this PR solve?

Issue Number: close #10365, close #3496, ref pingcap/tidb#63086

Problem Summary:

What is changed and how it works?


Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No code

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

None

@ti-chi-bot ti-chi-bot bot added release-note-none Denotes a PR that doesn't merit a release note. do-not-merge/needs-linked-issue size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Aug 25, 2025
@pingcap-cla-assistant
Copy link

pingcap-cla-assistant bot commented Aug 25, 2025

CLA assistant check
All committers have signed the CLA.

@ti-chi-bot ti-chi-bot bot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. do-not-merge/needs-triage-completed and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Aug 26, 2025
@ti-chi-bot ti-chi-bot bot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Aug 26, 2025
@ChangRui-Ryan ChangRui-Ryan force-pushed the decimal_floor branch 2 times, most recently from 799bf98 to 26fbbb9 Compare August 27, 2025 03:43
@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. do-not-merge/needs-triage-completed labels Aug 27, 2025
@ChangRui-Ryan ChangRui-Ryan changed the title Fix decimal floor/ceil (#63086) Fix decimal floor/ceil (#10365) Aug 27, 2025
using Op = IntegerRoundingComputation<NativeType, rounding_mode, ScaleMode::Negative>;
auto scale_factor = intExp10OfSize<NativeType>(decimal_scale);

if constexpr (std::is_same_v<T, OutputType>)
Copy link
Contributor

Choose a reason for hiding this comment

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

In which case will the output be Int64, and in which case will the output type the same as T?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For decimal(M, N), if M-N > 18, use T; otherwise use Int64. Because Int64 is sufficient to represent 18-digit integers. TiDB will make decision.


if constexpr (std::is_same_v<T, OutputType>)
{
Op::compute(&in->value, scale_factor, &out->value);
Copy link
Contributor

Choose a reason for hiding this comment

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

If the output type is decimal, will the return type of ceil/floor keep its input type's scale, or the result type's scale is always 0?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

will keep its input type's scale.

Copy link
Contributor

Choose a reason for hiding this comment

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

Are you sure of this? Because from TiDB's code, it seems if the return type is decimal, the scale is always 0:
https://github.com/pingcap/tidb/blob/ad52c8e05b49602888f08db4a81cfcffbb21e555/pkg/expression/builtin_math.go#L732-L735

Copy link
Contributor Author

Choose a reason for hiding this comment

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

From getReturnTypeImpl of FunctionRounding class, we can see that return type is just arguments[0], and I didn't change that in this pr. Maybe some other places change the scale to 0 after. Let me try to find out.

Copy link
Contributor

Choose a reason for hiding this comment

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

that is because after TiFlash's function, we will add extra cast to keep the return type the same as TiDB. But for this particular function, I think we can adopt TiDB's return type infer logic in TiFlash so we don't need to add extra cast after the function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, I will optimize that.

@ChangRui-Ryan ChangRui-Ryan force-pushed the decimal_floor branch 2 times, most recently from bf4b947 to fb9278c Compare August 29, 2025 07:28
// So, we only handle ScaleMode::Zero here.
if constexpr (scale_mode == ScaleMode::Zero)
{
auto scale_factor = intExp10OfSize<NativeType>(decimal_scale);
Copy link
Contributor

Choose a reason for hiding this comment

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

why not use scale_factor as the last parameter of compute, so we don't need calcuate scale_factor for every input row

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good suggestion, I will refactor this part.
Originally, I didn’t want to make major change, like modifying function interface.

@ChangRui-Ryan
Copy link
Contributor Author

/retest

Copy link
Contributor

@windtalker windtalker left a comment

Choose a reason for hiding this comment

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

lgtm

@ti-chi-bot ti-chi-bot bot added needs-1-more-lgtm Indicates a PR needs 1 more LGTM. approved labels Sep 3, 2025
@ti-chi-bot
Copy link
Contributor

ti-chi-bot bot commented Sep 5, 2025

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: windtalker, wshwsh12

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

The pull request process is described here

Details Needs approval from an approver in each of these files:
  • OWNERS [windtalker,wshwsh12]

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 Sep 5, 2025
@ti-chi-bot
Copy link
Contributor

ti-chi-bot bot commented Sep 5, 2025

[LGTM Timeline notifier]

Timeline:

  • 2025-09-03 07:45:54.673315145 +0000 UTC m=+944646.551146098: ☑️ agreed by windtalker.
  • 2025-09-05 03:15:19.023955617 +0000 UTC m=+149663.007053065: ☑️ agreed by wshwsh12.

@ti-chi-bot ti-chi-bot bot merged commit f5f30b8 into pingcap:master Sep 5, 2025
7 checks passed
@ti-chi-bot ti-chi-bot bot added needs-cherry-pick-release-8.5 Should cherry pick this PR to release-8.5 branch. needs-cherry-pick-release-8.1 Should cherry pick this PR to release-8.1 branch. needs-cherry-pick-release-7.5 Should cherry pick this PR to release-7.5 branch. needs-cherry-pick-release-7.1 Should cherry pick this PR to release-7.1 branch. needs-cherry-pick-release-6.5 Should cherry pick this PR to release-6.5 branch. labels Sep 8, 2025
@ti-chi-bot
Copy link
Member

In response to a cherrypick label: new pull request created to branch release-8.5: #10412.

@ti-chi-bot
Copy link
Member

In response to a cherrypick label: new pull request created to branch release-8.1: #10413.

@ti-chi-bot
Copy link
Member

In response to a cherrypick label: new pull request created to branch release-7.5: #10414.

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

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

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

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

ti-chi-bot bot pushed a commit that referenced this pull request Sep 10, 2025
close #3496, close #10365, ref pingcap/tidb#63086

Co-authored-by: ChangRui-Ryan <changrui82@gmail.com>
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.1 Should cherry pick this PR to release-7.1 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-none Denotes a PR that doesn't merit a release note. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect result of floor function Add unit test for Math function floor

5 participants