-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Add money_format function #1064
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
Merged
Merged
Changes from all commits
Commits
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,132 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| #include "exprs/string_functions.h" | ||
| #include "util/logging.h" | ||
| #include "exprs/anyval_util.h" | ||
| #include <iostream> | ||
| #include <string> | ||
|
|
||
| #include <gtest/gtest.h> | ||
|
|
||
| namespace doris { | ||
|
|
||
| class StringFunctionsTest : public testing::Test { | ||
| public: | ||
| StringFunctionsTest() { | ||
| } | ||
| }; | ||
|
|
||
| TEST_F(StringFunctionsTest, money_format_bigint) { | ||
| doris_udf::FunctionContext* context = new doris_udf::FunctionContext(); | ||
|
|
||
| StringVal result = StringFunctions::money_format(context, doris_udf::BigIntVal(123456)); | ||
| StringVal expected = AnyValUtil::from_string_temp(context, std::string("123,456.00")); | ||
| ASSERT_EQ(expected, result); | ||
|
|
||
| result = StringFunctions::money_format(context, doris_udf::BigIntVal(-123456)); | ||
| expected = AnyValUtil::from_string_temp(context, std::string("-123,456.00")); | ||
| ASSERT_EQ(expected, result); | ||
|
|
||
| result = StringFunctions::money_format(context, doris_udf::BigIntVal(9223372036854775807)); | ||
| expected = AnyValUtil::from_string_temp(context, std::string("9,223,372,036,854,775,807.00")); | ||
| ASSERT_EQ(expected, result); | ||
| } | ||
|
|
||
| TEST_F(StringFunctionsTest, money_format_large_int) { | ||
| doris_udf::FunctionContext* context = new doris_udf::FunctionContext(); | ||
|
|
||
| std::string str("170141183460469231731687303715884105727"); | ||
| std::stringstream ss; | ||
| ss << str; | ||
| __int128 value; | ||
| ss >> value; | ||
|
|
||
| std::cout << "value: " << value << std::endl; | ||
|
|
||
| StringVal result = StringFunctions::money_format(context, doris_udf::LargeIntVal(value)); | ||
| StringVal expected = AnyValUtil::from_string_temp(context, std::string("170,141,183,460,469,231,731,687,303,715,884,105,727.00")); | ||
| ASSERT_EQ(expected, result); | ||
| } | ||
|
|
||
| TEST_F(StringFunctionsTest, money_format_double) { | ||
| doris_udf::FunctionContext* context = new doris_udf::FunctionContext(); | ||
|
|
||
| StringVal result = StringFunctions::money_format(context, doris_udf::DoubleVal(1234.456)); | ||
| StringVal expected = AnyValUtil::from_string_temp(context, std::string("1,234.46")); | ||
| ASSERT_EQ(expected, result); | ||
|
|
||
| result = StringFunctions::money_format(context, doris_udf::DoubleVal(1234.45)); | ||
| expected = AnyValUtil::from_string_temp(context, std::string("1,234.45")); | ||
| ASSERT_EQ(expected, result); | ||
|
|
||
| result = StringFunctions::money_format(context, doris_udf::DoubleVal(1234.4)); | ||
| expected = AnyValUtil::from_string_temp(context, std::string("1,234.40")); | ||
| ASSERT_EQ(expected, result); | ||
|
|
||
| result = StringFunctions::money_format(context, doris_udf::DoubleVal(1234.454)); | ||
| expected = AnyValUtil::from_string_temp(context, std::string("1,234.45")); | ||
| ASSERT_EQ(expected, result); | ||
| } | ||
|
|
||
| TEST_F(StringFunctionsTest, money_format_decimal) { | ||
| doris_udf::FunctionContext* context = new doris_udf::FunctionContext(); | ||
|
|
||
| DecimalValue dv1(std::string("3333333333.2222222222")); | ||
| DecimalVal value1; | ||
| dv1.to_decimal_val(&value1); | ||
|
|
||
| StringVal result = StringFunctions::money_format(context, value1); | ||
| StringVal expected = AnyValUtil::from_string_temp(context, std::string("3,333,333,333.22")); | ||
| ASSERT_EQ(expected, result); | ||
|
|
||
| DecimalValue dv2(std::string("-7407407406790123456.71604938271975308642")); | ||
| DecimalVal value2; | ||
| dv2.to_decimal_val(&value2); | ||
|
|
||
| result = StringFunctions::money_format(context, value2); | ||
| expected = AnyValUtil::from_string_temp(context, std::string("-7,407,407,406,790,123,456.72")); | ||
| ASSERT_EQ(expected, result); | ||
| } | ||
|
|
||
| TEST_F(StringFunctionsTest, money_format_decimal_v2) { | ||
| doris_udf::FunctionContext* context = new doris_udf::FunctionContext(); | ||
|
|
||
| DecimalV2Value dv1(std::string("3333333333.2222222222")); | ||
| DecimalV2Val value1; | ||
| dv1.to_decimal_val(&value1); | ||
|
|
||
| StringVal result = StringFunctions::money_format(context, value1); | ||
| StringVal expected = AnyValUtil::from_string_temp(context, std::string("3,333,333,333.22")); | ||
| ASSERT_EQ(expected, result); | ||
|
|
||
| DecimalV2Value dv2(std::string("-740740740.71604938271975308642")); | ||
| DecimalV2Val value2; | ||
| dv2.to_decimal_val(&value2); | ||
|
|
||
| result = StringFunctions::money_format(context, value2); | ||
| expected = AnyValUtil::from_string_temp(context, std::string("-740,740,740.72")); | ||
| ASSERT_EQ(expected, result); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| int main(int argc, char** argv) { | ||
| doris::init_glog("be-test"); | ||
| ::testing::InitGoogleTest(&argc, argv); | ||
| return RUN_ALL_TESTS(); | ||
| } | ||
34 changes: 34 additions & 0 deletions
34
docs/documentation/cn/sql-reference/sql-functions/string-functions/money_format.md
This file contains hidden or 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,34 @@ | ||
| # money_format | ||
|
|
||
| ## Syntax | ||
|
|
||
| VARCHAR money_format(Number) | ||
|
|
||
| ## Description | ||
|
|
||
| 将数字按照货币格式输出,整数部分每隔3位用逗号分隔,小数部分保留2位 | ||
|
|
||
| ## Examples | ||
|
|
||
| ``` | ||
| mysql> select money_format(17014116); | ||
| +------------------------+ | ||
| | money_format(17014116) | | ||
| +------------------------+ | ||
| | 17,014,116.00 | | ||
| +------------------------+ | ||
|
|
||
| mysql> select money_format(1123.456); | ||
| +------------------------+ | ||
| | money_format(1123.456) | | ||
| +------------------------+ | ||
| | 1,123.46 | | ||
| +------------------------+ | ||
|
|
||
| mysql> select money_format(1123.4); | ||
| +----------------------+ | ||
| | money_format(1123.4) | | ||
| +----------------------+ | ||
| | 1,123.40 | | ||
| +----------------------+ | ||
| ``` |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.