generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 133
Fixed some defect on STR function #4104
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
Open
kelepcera
wants to merge
8
commits into
babelfish-for-postgresql:BABEL_5_X_DEV
Choose a base branch
from
kelepcera:fix/str_function
base: BABEL_5_X_DEV
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3ab4856
Fixed some defect on STR function
7242c9d
Added regression test
aef42b2
Added more test cases
8d77a9e
Fix wrong names
342959d
Extended and updated test cases to cover new results
0ac6ce9
Reverted unnecessay test cases
84cfed7
Quick fix
bc346b3
Merge branch 'babelfish-for-postgresql:BABEL_5_X_DEV' into fix/str_fu…
kelepcera 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -543,7 +543,7 @@ tsql_varbinary_substr(PG_FUNCTION_ARGS) | |
| /* | ||
| * Returns character data converted from numeric data. The character data | ||
| * is right-justified, with a specified length and decimal precision. | ||
| */ | ||
| */ | ||
| Datum | ||
| float_str(PG_FUNCTION_ARGS) | ||
| { | ||
|
|
@@ -557,7 +557,7 @@ float_str(PG_FUNCTION_ARGS) | |
| char *ptr; | ||
| int input_deci_digits; | ||
| int has_neg_sign = 0; | ||
| int input_deci_point = 0; | ||
| int input_deci_point = 0; /* NOTE: this is a flag (0/1), not an index */ | ||
| int has_deci_point = 0; | ||
| int num_spaces = 0; | ||
| int int_digits = 0; | ||
|
|
@@ -643,6 +643,8 @@ float_str(PG_FUNCTION_ARGS) | |
| /* allocate buffer for putting result together */ | ||
| buf = palloc(size); | ||
| memset(buf, 0, size); | ||
|
|
||
| /* account for sign and decimal point in available length */ | ||
| if (has_neg_sign) | ||
| length--; | ||
| if (decimal > 0 && length > int_digits) | ||
|
|
@@ -680,15 +682,15 @@ float_str(PG_FUNCTION_ARGS) | |
|
|
||
| num_spaces = length - int_digits - deci_digits; | ||
|
|
||
| /* comp space for the decimal point */ | ||
| /* if we planned a decimal point but deci_digits=0, remove point and reclaim 1 space */ | ||
| if (has_deci_point && !deci_digits) | ||
| { | ||
| /* | ||
| * no enough space for decimal part, remove the decimal point and add | ||
| * one space. STR(1.234, 2, 1) returns " 1" | ||
| */ | ||
| num_spaces++; | ||
| has_deci_point--; | ||
| has_deci_point = 0; | ||
| } | ||
|
|
||
| /* | ||
|
|
@@ -749,6 +751,7 @@ float_str(PG_FUNCTION_ARGS) | |
| */ | ||
| memset(buf + num_spaces - 1, '-', 1); | ||
| num_spaces++; | ||
| ++float_char; /* skip '-' in source */ | ||
| } | ||
| memset(buf + num_spaces - 1, '1', 1); | ||
| memset(float_char, '0', 1); | ||
|
|
@@ -774,7 +777,6 @@ float_str(PG_FUNCTION_ARGS) | |
| } | ||
| } | ||
|
|
||
|
|
||
| /* copy the actual number to the buffer after preceding spaces */ | ||
| strncpy(buf + num_spaces, float_char, size - 1 - num_spaces); | ||
|
|
||
|
|
@@ -801,47 +803,50 @@ float_str(PG_FUNCTION_ARGS) | |
| } | ||
| } | ||
|
|
||
| return return_varchar_pointer(buf, size); | ||
| /* return VARCHAR with actual content length (no NUL) */ | ||
| return return_varchar_pointer(buf, (int)strlen(buf)); | ||
|
Comment on lines
+806
to
+807
Contributor
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. Similar to how its done in earlier return statements, for example following case can we use size-1 instead of computing the length again using strlen, as |
||
| } | ||
|
|
||
| /* | ||
| * Find the rounding position of the float_char input using the constraints | ||
| * returns the rounding position | ||
| */ | ||
| */ | ||
| static int | ||
| find_round_pos(char *float_char, int has_neg_sign, int int_digits, int deci_digits, int input_deci_digits, int input_deci_point, int deci_sig) | ||
| { | ||
| int round_pos = 0; | ||
| int curr_digit; | ||
| int look_idx; | ||
|
|
||
| if (int_digits + input_deci_digits > 17) | ||
| { | ||
| /* | ||
| * exceeds the max precision, need to round to 17th digit(excluding - | ||
| * and .) | ||
| */ | ||
| /* Case 1: 18th significant digit is in integer part */ | ||
| if (int_digits > 17) | ||
| { | ||
| /* round in int part */ | ||
| /* STR(12345678901234567890, 20) returns "1234567890123456800" */ | ||
| curr_digit = float_char[17 + has_neg_sign] - '0'; | ||
| /* look at the 18th significant digit (skip '-') */ | ||
| look_idx = 17 + has_neg_sign; | ||
| curr_digit = float_char[look_idx] - '0'; | ||
|
|
||
| if (curr_digit >= 5) | ||
| round_pos = 16 + has_neg_sign; | ||
| round_pos = look_idx - 1; /* round the 17th significant digit */ | ||
| } | ||
| else | ||
| { | ||
| /* round in decimal part */ | ||
|
|
||
| /* | ||
| * STR(1234567890.1234567890, 22, 20) returns | ||
| * "1234567890.12345670000" | ||
| * Case 2: 18th significant digit is in decimal part. | ||
| * We still "walk" 17 significant digits from start (skipping '-' | ||
| * and not counting '.'), so index is: | ||
| * has_neg_sign + input_deci_point + 17 | ||
| * (int_digits cancels out: int_digits + (17 - int_digits) = 17) | ||
| */ | ||
| curr_digit = float_char[17 + has_neg_sign + input_deci_point] - '0'; | ||
| look_idx = 17 + has_neg_sign + input_deci_point; | ||
| curr_digit = float_char[look_idx] - '0'; | ||
|
|
||
| if (curr_digit >= 5) | ||
| round_pos = 16 + has_neg_sign + input_deci_point; | ||
| round_pos = look_idx - 1; /* round the 17th significant digit */ | ||
| } | ||
| } | ||
| /* Otherwise, if we have more input decimals than we will print, round to last printed decimal */ | ||
| else if (deci_digits && input_deci_digits > deci_sig) | ||
| { | ||
| /* input decimal digits > needed, round to last output decimal digit */ | ||
|
|
@@ -850,6 +855,7 @@ find_round_pos(char *float_char, int has_neg_sign, int int_digits, int deci_digi | |
| if (curr_digit >= 5) | ||
| round_pos = has_neg_sign + int_digits + input_deci_point + deci_sig - 1; | ||
| } | ||
| /* If no decimal will be printed but input had decimals, round to integer */ | ||
| else if (!deci_sig && input_deci_digits) | ||
| { | ||
| /* int part == length and has deci digit input, round to integer */ | ||
|
|
@@ -865,14 +871,14 @@ find_round_pos(char *float_char, int has_neg_sign, int int_digits, int deci_digi | |
|
|
||
| /* | ||
| * Inplace round the float_char to the digit at round_pos, returns the final carried over digit | ||
| */ | ||
| */ | ||
| static int | ||
| round_float_char(char *float_char, int round_pos, int has_neg_sign) | ||
| { | ||
| int curr_digit; | ||
| int carry = 1; | ||
|
|
||
| while (round_pos > (0 + has_neg_sign) && carry) | ||
| while (round_pos >= (0 + has_neg_sign) && carry) | ||
| { | ||
| if (float_char[round_pos] == '.') | ||
| { | ||
|
|
||
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.
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.
can we evaluate whether we need this or not ?