-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
builtin: add format built-in function #2883
Merged
Merged
Changes from 14 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
705c989
builtin: add format built-in function
c4f1bb0
fix conflict
feca101
Merge remote-tracking branch 'upstream/master' into format
fdc3b8d
modify todo
a6679ce
fix and add more tests
f12d7ee
fix and add more tests
de55887
fix and add more tests
0d3f9de
fix and add more tests
d1b8079
fix and add more tests
5b4732e
fix conflict
7aa0817
Merge remote-tracking branch 'upstream/master' into format
1a07e34
fix conflict
d295c2c
Merge branch 'master' into format
a112d94
Merge branch 'master' into format
7fe422a
Merge branch 'master' into format
coocood File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1099,6 +1099,93 @@ func (s *testEvaluatorSuite) TestOct(c *C) { | |
c.Assert(r.IsNull(), IsTrue) | ||
} | ||
|
||
func (s *testEvaluatorSuite) TestFormat(c *C) { | ||
defer testleak.AfterTest(c)() | ||
formatCases := []struct { | ||
number interface{} | ||
precision interface{} | ||
locale string | ||
ret interface{} | ||
}{ | ||
{12332.1234561111111111111111111111111111111111111, 4, "en_US", "12,332.1234"}, | ||
{nil, 22, "en_US", nil}, | ||
} | ||
formatCases1 := []struct { | ||
number interface{} | ||
precision interface{} | ||
ret interface{} | ||
}{ | ||
{12332.123456, 4, "12,332.1234"}, | ||
{12332.123456, 0, "12,332"}, | ||
{12332.123456, -4, "12,332"}, | ||
{-12332.123456, 4, "-12,332.1234"}, | ||
{-12332.123456, 0, "-12,332"}, | ||
{-12332.123456, -4, "-12,332"}, | ||
{"12332.123456", "4", "12,332.1234"}, | ||
{"12332.123456A", "4", "12,332.1234"}, | ||
{"-12332.123456", "4", "-12,332.1234"}, | ||
{"-12332.123456A", "4", "-12,332.1234"}, | ||
{"A123345", "4", "0.0000"}, | ||
{"-A123345", "4", "0.0000"}, | ||
{"-12332.123456", "A", "-12,332"}, | ||
{"12332.123456", "A", "12,332"}, | ||
{"-12332.123456", "4A", "-12,332.1234"}, | ||
{"12332.123456", "4A", "12,332.1234"}, | ||
{"-A12332.123456", "A", "0"}, | ||
{"A12332.123456", "A", "0"}, | ||
{"-A12332.123456", "4A", "0.0000"}, | ||
{"A12332.123456", "4A", "0.0000"}, | ||
{"-.12332.123456", "4A", "-0.1233"}, | ||
{".12332.123456", "4A", "0.1233"}, | ||
{"12332.1234567890123456789012345678901", 22, "12,332.1234567890123456789012"}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi, @ariesdevil I mean you should also handle this case in your branch:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tiancaiamao soon |
||
{nil, 22, nil}, | ||
} | ||
formatCases2 := struct { | ||
number interface{} | ||
precision interface{} | ||
locale string | ||
ret interface{} | ||
}{-12332.123456, -4, "zh_CN", nil} | ||
formatCases3 := struct { | ||
number interface{} | ||
precision interface{} | ||
locale string | ||
ret interface{} | ||
}{"-12332.123456", "4", "de_GE", nil} | ||
|
||
for _, t := range formatCases { | ||
fc := funcs[ast.Format] | ||
f, err := fc.getFunction(datumsToConstants(types.MakeDatums(t.number, t.precision, t.locale)), s.ctx) | ||
c.Assert(err, IsNil) | ||
r, err := f.eval(nil) | ||
c.Assert(err, IsNil) | ||
c.Assert(r, testutil.DatumEquals, types.NewDatum(t.ret)) | ||
} | ||
|
||
for _, t := range formatCases1 { | ||
fc := funcs[ast.Format] | ||
f, err := fc.getFunction(datumsToConstants(types.MakeDatums(t.number, t.precision)), s.ctx) | ||
c.Assert(err, IsNil) | ||
r, err := f.eval(nil) | ||
c.Assert(err, IsNil) | ||
c.Assert(r, testutil.DatumEquals, types.NewDatum(t.ret)) | ||
} | ||
|
||
fc2 := funcs[ast.Format] | ||
f2, err := fc2.getFunction(datumsToConstants(types.MakeDatums(formatCases2.number, formatCases2.precision, formatCases2.locale)), s.ctx) | ||
c.Assert(err, IsNil) | ||
r2, err := f2.eval(nil) | ||
c.Assert(types.NewDatum(err), testutil.DatumEquals, types.NewDatum(errors.New("not implemented"))) | ||
c.Assert(r2, testutil.DatumEquals, types.NewDatum(formatCases2.ret)) | ||
|
||
fc3 := funcs[ast.Format] | ||
f3, err := fc3.getFunction(datumsToConstants(types.MakeDatums(formatCases3.number, formatCases3.precision, formatCases3.locale)), s.ctx) | ||
c.Assert(err, IsNil) | ||
r3, err := f3.eval(nil) | ||
c.Assert(types.NewDatum(err), testutil.DatumEquals, types.NewDatum(errors.New("not support for the specific locale"))) | ||
c.Assert(r3, testutil.DatumEquals, types.NewDatum(formatCases3.ret)) | ||
} | ||
|
||
func (s *testEvaluatorSuite) TestInsert(c *C) { | ||
tests := []struct { | ||
args []interface{} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package mysql | ||
|
||
import ( | ||
"bytes" | ||
"strconv" | ||
"strings" | ||
"unicode" | ||
|
||
"github.com/juju/errors" | ||
) | ||
|
||
func formatENUS(number string, precision string) (string, error) { | ||
var buffer bytes.Buffer | ||
if unicode.IsDigit(rune(precision[0])) { | ||
for i, v := range precision { | ||
if unicode.IsDigit(v) { | ||
continue | ||
} else { | ||
precision = precision[:i] | ||
break | ||
} | ||
} | ||
} else { | ||
precision = "0" | ||
} | ||
if number[0] == '-' && number[1] == '.' { | ||
number = strings.Replace(number, "-", "-0", 1) | ||
} else if number[0] == '.' { | ||
number = strings.Replace(number, ".", "0.", 1) | ||
} | ||
|
||
if (number[:1] == "-" && !unicode.IsDigit(rune(number[1]))) || | ||
(!unicode.IsDigit(rune(number[0])) && number[:1] != "-") { | ||
buffer.Write([]byte{'0'}) | ||
position, err := strconv.ParseUint(precision, 10, 64) | ||
if err == nil && position > 0 { | ||
buffer.Write([]byte{'.'}) | ||
buffer.WriteString(strings.Repeat("0", int(position))) | ||
} | ||
return buffer.String(), nil | ||
} else if number[:1] == "-" { | ||
buffer.Write([]byte{'-'}) | ||
number = number[1:] | ||
} | ||
|
||
for i, v := range number { | ||
if unicode.IsDigit(v) { | ||
continue | ||
} else if i == 1 && number[1] == '.' { | ||
continue | ||
} else if v == '.' && number[1] != '.' { | ||
continue | ||
} else { | ||
number = number[:i] | ||
break | ||
} | ||
} | ||
|
||
comma := []byte{','} | ||
parts := strings.Split(number, ".") | ||
pos := 0 | ||
if len(parts[0])%3 != 0 { | ||
pos += len(parts[0]) % 3 | ||
buffer.WriteString(parts[0][:pos]) | ||
buffer.Write(comma) | ||
} | ||
for ; pos < len(parts[0]); pos += 3 { | ||
buffer.WriteString(parts[0][pos : pos+3]) | ||
buffer.Write(comma) | ||
} | ||
buffer.Truncate(buffer.Len() - 1) | ||
|
||
position, err := strconv.ParseUint(precision, 10, 64) | ||
if err == nil { | ||
if position > 0 { | ||
buffer.Write([]byte{'.'}) | ||
if len(parts) == 2 { | ||
if uint64(len(parts[1])) >= position { | ||
buffer.WriteString(parts[1][:position]) | ||
} else { | ||
buffer.WriteString(parts[1]) | ||
buffer.WriteString(strings.Repeat("0", int(position)-len(parts[1]))) | ||
} | ||
} else { | ||
buffer.WriteString(strings.Repeat("0", int(position))) | ||
} | ||
} | ||
} | ||
|
||
return buffer.String(), nil | ||
} | ||
|
||
func formatZHCN(number string, precision string) (string, error) { | ||
return "", errors.New("not implemented") | ||
} | ||
|
||
func formatNotSupport(number string, precision string) (string, error) { | ||
return "", errors.New("not support for the specific locale") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add case that decimal part is very long
add case no decimal part
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and test the invalid input such as NULL or string and other type, check mysql behavior to confirm