Skip to content

Commit

Permalink
expression: Fix warnings for FORMAT() in CTE (#56198)
Browse files Browse the repository at this point in the history
close #56193
  • Loading branch information
dveeden authored Sep 26, 2024
1 parent badf610 commit f399e91
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 2 deletions.
12 changes: 10 additions & 2 deletions pkg/expression/builtin_string_vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2978,7 +2978,11 @@ func formatDecimal(ctx EvalContext, xBuf *chunk.Column, dInt64s []int64, result
} else if !strings.EqualFold(localeBuf.GetString(i), "en_US") {
// TODO: support other locales.
tc := typeCtx(ctx)
tc.AppendWarning(errUnknownLocale.FastGenByArgs(localeBuf.GetString(i)))

// force copy of the string
// https://github.com/pingcap/tidb/issues/56193
locale := strings.Clone(localeBuf.GetString(i))
tc.AppendWarning(errUnknownLocale.FastGenByArgs(locale))
}

xStr := roundFormatArgs(x.String(), int(d))
Expand Down Expand Up @@ -3020,7 +3024,11 @@ func formatReal(ctx EvalContext, xBuf *chunk.Column, dInt64s []int64, result *ch
} else if !strings.EqualFold(localeBuf.GetString(i), "en_US") {
// TODO: support other locales.
tc := typeCtx(ctx)
tc.AppendWarning(errUnknownLocale.FastGenByArgs(localeBuf.GetString(i)))

// force copy of the string
// https://github.com/pingcap/tidb/issues/56193
locale := strings.Clone(localeBuf.GetString(i))
tc.AppendWarning(errUnknownLocale.FastGenByArgs(locale))
}

xStr := roundFormatArgs(strconv.FormatFloat(x, 'f', -1, 64), int(d))
Expand Down
52 changes: 52 additions & 0 deletions tests/integrationtest/r/expression/format.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
SELECT FORMAT(1.234,0,'en_US');
FORMAT(1.234,0,'en_US')
1
SELECT FORMAT(1.234,1,'en_US');
FORMAT(1.234,1,'en_US')
1.2
SELECT FORMAT(1.234,2,'en_US');
FORMAT(1.234,2,'en_US')
1.23
SELECT FORMAT(1.234,3,'en_US');
FORMAT(1.234,3,'en_US')
1.234
SELECT FORMAT(1.234,4,'en_US');
FORMAT(1.234,4,'en_US')
1.2340
SELECT FORMAT(1.234e-2,4,'en_US');
FORMAT(1.234e-2,4,'en_US')
0.0123
SELECT FORMAT(-1.234,4,'en_US');
FORMAT(-1.234,4,'en_US')
-1.2340
WITH RECURSIVE cte(n) AS (
SELECT 1 n
UNION ALL
SELECT 100+n FROM cte WHERE n<1000
)
SELECT FORMAT(n+0.01,3,'foo_bar') FROM cte;
FORMAT(n+0.01,3,'foo_bar')
1.010
101.010
201.010
301.010
401.010
501.010
601.010
701.010
801.010
901.010
1,001.010
SHOW WARNINGS;
Level Code Message
Warning 1649 Unknown locale: 'foo_bar'
Warning 1649 Unknown locale: 'foo_bar'
Warning 1649 Unknown locale: 'foo_bar'
Warning 1649 Unknown locale: 'foo_bar'
Warning 1649 Unknown locale: 'foo_bar'
Warning 1649 Unknown locale: 'foo_bar'
Warning 1649 Unknown locale: 'foo_bar'
Warning 1649 Unknown locale: 'foo_bar'
Warning 1649 Unknown locale: 'foo_bar'
Warning 1649 Unknown locale: 'foo_bar'
Warning 1649 Unknown locale: 'foo_bar'
15 changes: 15 additions & 0 deletions tests/integrationtest/t/expression/format.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
SELECT FORMAT(1.234,0,'en_US');
SELECT FORMAT(1.234,1,'en_US');
SELECT FORMAT(1.234,2,'en_US');
SELECT FORMAT(1.234,3,'en_US');
SELECT FORMAT(1.234,4,'en_US');
SELECT FORMAT(1.234e-2,4,'en_US');
SELECT FORMAT(-1.234,4,'en_US');

WITH RECURSIVE cte(n) AS (
SELECT 1 n
UNION ALL
SELECT 100+n FROM cte WHERE n<1000
)
SELECT FORMAT(n+0.01,3,'foo_bar') FROM cte;
SHOW WARNINGS;

0 comments on commit f399e91

Please sign in to comment.