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: extend locale support for format() #56168

Draft
wants to merge 17 commits into
base: master
Choose a base branch
from

Conversation

dveeden
Copy link
Contributor

@dveeden dveeden commented Sep 19, 2024

What problem does this PR solve?

Issue Number: close #56167

Problem Summary:

What changed and how does it work?

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.

The FORMAT() function now supports more locales

@dveeden dveeden requested a review from lcwangchao September 19, 2024 15:35
Copy link

ti-chi-bot bot commented Sep 19, 2024

Skipping CI for Draft Pull Request.
If you want CI signal for your change, please convert it to an actual PR.
You can still manually trigger a test run with /test all

@ti-chi-bot ti-chi-bot bot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. 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 Sep 19, 2024
Copy link

tiprow bot commented Sep 19, 2024

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

@dveeden
Copy link
Contributor Author

dveeden commented Sep 19, 2024

/ok-to-test

@ti-chi-bot ti-chi-bot bot added the ok-to-test Indicates a PR is ready to be tested. label Sep 19, 2024
Copy link

codecov bot commented Sep 19, 2024

Codecov Report

Attention: Patch coverage is 58.82353% with 21 lines in your changes missing coverage. Please review.

Project coverage is 75.4794%. Comparing base (f399e91) to head (e4c693e).
Report is 354 commits behind head on master.

Additional details and impacted files
@@               Coverage Diff                @@
##             master     #56168        +/-   ##
================================================
+ Coverage   73.4247%   75.4794%   +2.0546%     
================================================
  Files          1619       1628         +9     
  Lines        446765     463235     +16470     
================================================
+ Hits         328036     349647     +21611     
+ Misses        98618      92892      -5726     
- Partials      20111      20696       +585     
Flag Coverage Δ
integration 48.8034% <56.8627%> (?)

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

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

@kennytm
Copy link
Contributor

kennytm commented Sep 19, 2024

👀 i'm afraid the test cases is too simple, since format() is supposed to handle thousands separators as well so you should at least test

mysql> select format('12345678.9', 3, 'en_US');
12,345,678.900
mysql> select format('12345678.9', 3, 'nl_NL');
12345678,900
mysql> select format('12345678.9', 3, 'id_ID');
12.345.678,900
mysql> select format('12345678.9', 3, 'it_CH'); -- Switzerland
12'345'678,900
mysql> select format('12345678.9', 3, 'de_CH'); -- Switzerland
12'345'678.900
mysql> select format('12345678.9', 3, 'en_IN'); -- India
1,23,45,678.900

return "", false, err
}
p := message.NewPrinter(lang)
xint, _ := strconv.ParseFloat(x, 64)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why ignore error here? I think it's better to handle error even if we suppose it should not happen

@dveeden dveeden changed the title expression: extend locale support or format() expression: extend locale support for format() Sep 20, 2024
@dveeden dveeden marked this pull request as ready for review September 24, 2024 10:55
@ti-chi-bot ti-chi-bot bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Sep 24, 2024
@dveeden dveeden requested a review from YangKeao September 24, 2024 10:56
Copy link

ti-chi-bot bot commented Sep 24, 2024

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign d3hunter, xuhuaiyu for approval, ensuring that each of them provides their approval before proceeding. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found 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 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 Sep 24, 2024
@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 Sep 25, 2024
@dveeden dveeden marked this pull request as draft September 26, 2024 09:12
@ti-chi-bot ti-chi-bot bot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Sep 26, 2024
@dveeden
Copy link
Contributor Author

dveeden commented Sep 26, 2024

/test all

@dveeden dveeden marked this pull request as ready for review September 26, 2024 09:39
@ti-chi-bot ti-chi-bot bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Sep 26, 2024
12,345,678.9000
SELECT FORMAT(12345678.9,999999999999999999,'de_CH');
FORMAT(12345678.9,999999999999999999,'de_CH')
12’345’678.900000000372529029846191406250
Copy link
Contributor

Choose a reason for hiding this comment

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

why will it end with ....372529029846191406250? the input should be a decimal number with exact decimal representation 🤔

mysql> select FORMAT(12345678.9, 99999999999999999, 'de_CH');
+------------------------------------------------+
| FORMAT(12345678.9, 99999999999999999, 'de_CH') |
+------------------------------------------------+
| 12'345'678.900000000000000000000000000000      |
+------------------------------------------------+
1 row in set (0.00 sec)

also why is it separated using U+2019 RIGHT SINGLE QUOTATION MARK rather than ' U+0027 APOSTROPHE? is this OS-dependent?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Go

package main

import (
	"golang.org/x/text/language"
	"golang.org/x/text/message"
	"golang.org/x/text/number"
)

func main() {
	p := message.NewPrinter(language.MustParse("de_CH"))
	p.Printf("%f", number.Decimal(12345678.9, number.Scale(1)))
}

https://go.dev/play/p/7Iv3SA0R-bR

output:

12’345’678.9

C with ICU

#include <stdio.h>
#include <unicode/unum.h>
#include <unicode/uloc.h>
#include <unicode/ustring.h>
#include <unicode/ustdio.h>

int main() {
    double number = 12345678.9;
    const char* locale = "de_CH";
    UChar result[100];

    UErrorCode status = U_ZERO_ERROR;
    UNumberFormat* fmt = unum_open(UNUM_DECIMAL, NULL, 0, locale, NULL, &status);
    int32_t resultLength = sizeof(result) / sizeof(result[0]);
    resultLength = unum_formatDouble(fmt, number, result, resultLength, NULL, &status);
    char resultUTF8[100];
    u_strToUTF8(resultUTF8, sizeof(resultUTF8), NULL, result, resultLength, &status);
    printf("%s\n", resultUTF8);
    unum_close(fmt);
    return 0;
}

output:

12’345’678.9

Python

#!/bin/python3
import locale

locale.setlocale(locale.LC_NUMERIC, "de_CH")
print(locale.format_string("%.2f", 12345678.9, grouping=True))

output:

12'345'678.90

Conclusion

Depending on the OS, programming language and libraries either or ' are used. Looks like both are acceptable.

Note that this results in minor differences between TiDB and MySQL (tested with 9.0.1):

locale TiDB MySQL Difference?
nl_BE 12.345.678,9000 12345678,9000 Yes
fr_BE 12 345 678,9000 12345678,9000 Yes
en_IN 1,23,45,678.9000 1,23,45,678.9000 No
de_CH 12’345’678.9000 12'345'678.9000 Yes
no_NO 12,345,678.9000 12.345.678,9000 Yes (big difference)
bg_BG 12 345 678,9000 12 345 678,9000 No
ar_SA 12,345,678.9000 12345678.9000 Yes
es_MX 12,345,678.9000 12,345,678.9000 No

Copy link
Contributor Author

@dveeden dveeden Sep 27, 2024

Choose a reason for hiding this comment

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

The no_NO difference is probably not a real issue as it should have been nb_NO or nn_NO instead of no_NO. See also https://bugzilla.redhat.com/show_bug.cgi?id=532487#c0

mysql-9.0.1> \W
Show warnings enabled.
mysql-9.0.1> SELECT FORMAT(12345678.9,4,'nb_NO');
+------------------------------+
| FORMAT(12345678.9,4,'nb_NO') |
+------------------------------+
| 12.345.678,9000              |
+------------------------------+
1 row in set (0.00 sec)

mysql-9.0.1> SELECT FORMAT(12345678.9,4,'nn_NO');
+------------------------------+
| FORMAT(12345678.9,4,'nn_NO') |
+------------------------------+
| 12,345,678.9000              |
+------------------------------+
1 row in set, 1 warning (0.00 sec)

Warning (Code 1649): Unknown locale: 'nn_NO'
mysql-8.0.11-TiDB-v8.4.0-alpha-295-g5f8ee5d509> \W
Show warnings enabled.
mysql-8.0.11-TiDB-v8.4.0-alpha-295-g5f8ee5d509> SELECT FORMAT(12345678.9,4,'nb_NO');
+------------------------------+
| FORMAT(12345678.9,4,'nb_NO') |
+------------------------------+
| 12 345 678,9000              |
+------------------------------+
1 row in set (0.01 sec)

mysql-8.0.11-TiDB-v8.4.0-alpha-295-g5f8ee5d509> SELECT FORMAT(12345678.9,4,'nn_NO');
+------------------------------+
| FORMAT(12345678.9,4,'nn_NO') |
+------------------------------+
| 12 345 678,9000              |
+------------------------------+
1 row in set (0.01 sec)

@dveeden dveeden marked this pull request as draft September 27, 2024 13:46
@ti-chi-bot ti-chi-bot bot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Sep 27, 2024
@dveeden
Copy link
Contributor Author

dveeden commented Sep 30, 2024

/test all

Copy link

tiprow bot commented Sep 30, 2024

@dveeden: 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 e4c693e 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.

Copy link

ti-chi-bot bot commented Sep 30, 2024

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

Test name Commit Details Required Rerun command
idc-jenkins-ci-tidb/mysql-test e4c693e link true /test mysql-test
idc-jenkins-ci-tidb/unit-test e4c693e link true /test unit-test

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/test-infra repository. I understand the commands that are listed here.

@dveeden
Copy link
Contributor Author

dveeden commented Oct 2, 2024

I'm going to part this pull request for now. The reason for this is that golang.org/x/text can do nice formatting for a large number of locales, but it only accepts integer and float types, which cause incorrect results due to how floats are handled.

I've filed a proposal to allow direct conversion from a string instead of relying on first converting the value to a float:
golang/go#69740

The options we have now are:

  1. Wait for golang.org/x/text improvements
  2. Do the locale formatting without an external library. This is quite a bit of work to do it correctly. However the benefit is that we more easily follow the output of MySQL more closely.
  3. Find another library that does this

cc @kennytm @lcwangchao @YangKeao

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. ok-to-test Indicates a PR is ready to be tested. release-note Denotes a PR that will be considered when it comes time to generate release notes. 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.

Support more locales for FORMAT()
3 participants