Fixed some defect on STR function#4104
Fixed some defect on STR function#4104kelepcera wants to merge 8 commits intobabelfish-for-postgresql:BABEL_5_X_DEVfrom
Conversation
Pull Request Test Coverage Report for Build 20502144051Details
💛 - Coveralls |
Yvinayak07
left a comment
There was a problem hiding this comment.
Let's add test cases also.
Hello, I have added several regression tests — you can check their results.
Because of this “approximate number” behavior, slight discrepancies (especially around .5 boundaries or values like 2.675) are expected and not considered bugs. The only remaining problem is with numbers that exceed 17 significant digits: SQL Server truncates them, while our code sometimes rounds. I attempted a fix, but it caused regressions elsewhere, so I left it as is for now. |
|
@Yvinayak07 Hello is there any update? |
|
Hi, I just wanted to kindly check if you’ve had a chance to look at the merge request I opened for the fix. It’s a very small change and you can check in your local env adding some break points. Thank you. |
There was a problem hiding this comment.
Hi, could you please add some test cases including functions, procedures and views calling this function STR.
There was a problem hiding this comment.
Yes, sure thank you!
There was a problem hiding this comment.
Hello again, it has already kind of test cases and I have just added more test cases to test new outputs and updated expected files.
| /* ========================= | ||
| 0) Smoke: basic rounding (1 d.p., 2 d.p.) | ||
| ========================= */ | ||
| SELECT STR(123.355, 10, 1) AS s_1dp_up; -- MSSQL: ' 123.4' |
There was a problem hiding this comment.
Kindly you the term "-- TSQL:" instead
There was a problem hiding this comment.
Thank you to inform me. I will fix them.
|
Hello, is there any update? :) @tanyagupta17 @Yvinayak07 @rohit01010 |
|
Hi there, |
There was a problem hiding this comment.
Hi @kelepcera could you please fix the failing test cases in this PR. Thanks!
I checked the test failures, could you please re-run your PR, these failures are fixed in recent times.
Hi @tanyagupta17 . I rebased my branch as a result of this the pipeline has been triggered. Every jobs has been finished successfully. And also thank you to take care of errors in my pipeline. |
|
Hello @tanyagupta17 , is there any update about bugs? |
|
Hello everybody, is there any update? It should be ready to be merged. |
| /* return VARCHAR with actual content length (no NUL) */ | ||
| return return_varchar_pointer(buf, (int)strlen(buf)); |
There was a problem hiding this comment.
Similar to how its done in earlier return statements, for example following case
/* not enough space for the carried_over digit, return *** */
/*
* the space limitation goes by the one set before the
* rounding & carried over
*/
/*
* STR(9999.998, 7, 2) returns "*******" but STR(10000.000, 7,
* 2) returns "10000.0"
*/
/*
* which means the max length constraint of integer part is
* still 4 after rounding
*/
memset(buf, '*', size - 1);
return return_varchar_pointer(buf, size - 1);
can we use size-1 instead of computing the length again using strlen, as (int)strlen(buf) will anyways be size-1 ?
| ++float_char; /* skip '-' in source */ | ||
| } | ||
| memset(buf + num_spaces - 1, '1', 1); | ||
| memset(float_char, '0', 1); |
There was a problem hiding this comment.
memset(float_char, '0', 1);
can we evaluate whether we need this or not ?
|
Thanks @kelepcera for your patience! I've completed my review and left some comments. Please check the comments when you get a chance. |
Description
This MR aligns STR() with SQL Server in several major correctness areas (carry propagation, decimal-point placement, and negative rounding with decimal = 0). It also documents the remaining compatibility gaps for (a) 17-significant-digits normalization and (b) float tie boundary literals.
Fixed: major correctness issues
Why this matters: the old behavior produced numerically wrong strings (-1090 instead of -1000, 10.000 instead of 2.000/4.000) and broke SQL Server parity in common edge cases.
Still different (not fixed in this MR)
SQL Server I guess normalizes to at most 17 significant digits (round at digit 17 and zero-fill beyond) before any length/decimal formatting. We don’t yet enforce that step:
What changed (implementation)
Corrected post-round carry propagation and decimal-point placement, including cases where carry expands the integer part (e.g., 1.9999 --> 2.000, 3.9999 --> 4.000) without misplacing the decimal or producing 10.000.
Fixed negative rounding with decimal = 0 so sign + carry produce the correct integer (e.g., -999.9 --> -1000) and align with SQL Server formatting.
Issues Resolved
[Issue Link]](#4103)
Test Scenarios Covered
Use case based -
Boundary conditions -
Arbitrary inputs -
Negative test cases -
Minor version upgrade tests -
Major version upgrade tests -
Performance tests -
Tooling impact -
Client tests -
Check List
By submitting this pull request, I confirm that my contribution is under the terms of the Apache 2.0 and PostgreSQL licenses, and grant any person obtaining a copy of the contribution permission to relicense all or a portion of my contribution to the PostgreSQL License solely to contribute all or a portion of my contribution to the PostgreSQL open source project.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.