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

builtin: add format built-in function #2883

Merged
merged 15 commits into from
Mar 31, 2017
Merged

Conversation

ariesdevil
Copy link

No description provided.

@coocood coocood added the contribution This PR is from a community contributor. label Mar 20, 2017
return d, errFunctionNotExists.GenByArgs("format")
args, err := b.evalArgs(row)
if err != nil {
return types.Datum{}, errors.Trace(err)
Copy link
Member

Choose a reason for hiding this comment

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

return d, errors.Trace(err)

var arg2 string

// TODO: Neet to be moved away.
supportedFormat := map[string]formatFunc{
Copy link
Member

Choose a reason for hiding this comment

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

You can define some consts in mysql/const.go.

}

d.SetString(supportedFormat[arg2](number, arg1))
return d, errors.Trace(err)
Copy link
Member

Choose a reason for hiding this comment

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

return d, nil

}

// TODO: Move those functions away
func _formatENUS(number float64, precision int64) string {
Copy link
Member

Choose a reason for hiding this comment

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

The function name is not in golang style. Rename it to formatENUS.

}

func _formatZHCN(number float64, precision int64) string {
// TODO
Copy link
Member

Choose a reason for hiding this comment

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

Should we return error here?

// TODO: Neet to be moved away.
supportedFormat := map[string]formatFunc{
"en-US": _formatENUS,
"zh-CN": _formatZHCN,
Copy link
Contributor

Choose a reason for hiding this comment

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

_ is seldom used in golang for naming

if !exist {
arg2 = "en-US"
}
number, err := arg0.ToFloat64()
Copy link
Contributor

Choose a reason for hiding this comment

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

Will this operation loss precision? SELECT FORMAT(12332.1234567890123456789012345678901, 22);
PTAL @coocood

Copy link
Member

Choose a reason for hiding this comment

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

Yes.

number = 0 - number
}
comma := []byte{','}
parts := strings.Split(strconv.FormatFloat(number, 'f', -1, 64), ".")
Copy link
Contributor

Choose a reason for hiding this comment

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

check len(parts) == 2 ?
if you strconv.format(1), maybe no decimal part there, I guess...

Copy link
Contributor

Choose a reason for hiding this comment

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

ok, I see line 1652...

{-12332.123456, 4, "-12,332.1234"},
{-12332.123456, 0, "-12,332"},
{-12332.123456, -4, "-12,332"},
{"-12332.123456", "4", "-12,332.1234"},
Copy link
Contributor

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

Copy link
Contributor

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

@ariesdevil
Copy link
Author

@tiancaiamao @shenli @coocood please take a look again.

{-12332.123456, 0, "-12,332"},
{-12332.123456, -4, "-12,332"},
{"-12332.123456", "4", "-12,332.1234"},
{"12332.1234567890123456789012345678901", 22, "12,332.1234567890123456789012"},
Copy link
Contributor

Choose a reason for hiding this comment

The 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:

mysql> select format("abcd", 32);
ERROR 2013 (HY000): Lost connection to MySQL server during query

Copy link
Author

Choose a reason for hiding this comment

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

@ariesdevil
Copy link
Author

@tiancaiamao Add more tests and please take a look again.

{[]interface{}{"Quadratic", 3, -1, "What"}, "QuWhat"},
{[]interface{}{"Quadratic", 3, 1, "What"}, "QuWhatdratic"},

{[]interface{}{"我叫小雨呀", 3, 2, "王雨叶"}, "我叫王雨叶呀"},
Copy link
Contributor

Choose a reason for hiding this comment

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

Who is "王雨叶" ? Foreigners will feel 干(yi)的(lian)漂(meng)亮(bi) when they see this.
TiDB is a global project, it should not use those meaningless string.

Copy link
Author

Choose a reason for hiding this comment

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

@tiancaiamao It's not my code. That's TestInsert, not TestFormat.

Copy link
Author

Choose a reason for hiding this comment

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

@tiancaiamao The Insert function was implemented by @hiwjd , PR: #2863

@tiancaiamao
Copy link
Contributor

LGTM
@shenli @coocood

@tiancaiamao tiancaiamao added the status/LGT1 Indicates that a PR has LGTM 1. label Mar 27, 2017
Copy link
Member

@coocood coocood left a comment

Choose a reason for hiding this comment

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

LGTM

@coocood coocood merged commit ff3abdf into pingcap:master Mar 31, 2017
@ariesdevil ariesdevil deleted the format branch March 31, 2017 06:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contribution This PR is from a community contributor. status/LGT1 Indicates that a PR has LGTM 1.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants