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

Loss of decimal digits when converting from REAL to DECIMAL #129

Open
wants to merge 1 commit into
base: BABEL_3_X_DEV__PG_15_X
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/backend/utils/adt/numeric.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include "utils/numeric.h"
#include "utils/pg_lsn.h"
#include "utils/sortsupport.h"
#include "common/shortest_dec.h"

/* ----------
* Uncomment the following to enable compilation of dump_numeric()
Expand Down Expand Up @@ -4535,7 +4536,15 @@ float4_numeric(PG_FUNCTION_ARGS)
PG_RETURN_NUMERIC(make_result(&const_pinf));
}

snprintf(buf, sizeof(buf), "%.*g", FLT_DIG, val);
/*
* REAL datatype can store upto 4B of floating point data;
* anything larger than that gets truncated unless
* buf has extra places to store those extra_float_digits
*/
if (sql_dialect == SQL_DIALECT_TSQL && extra_float_digits > 0)
float_to_shortest_decimal_buf(val, buf);
else
snprintf(buf, sizeof(buf), "%.*g", FLT_DIG, val);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

similar logic can be found in float4out(); however in float4_numeric() the same was missing.

Copy link
Contributor

Choose a reason for hiding this comment

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

we need more test cases to validate that this right check. This change implies that when dialect is TSQL, always try to use extra digits for REAL datatype when casting from float4 to numeric. Is it really the case for all possible casting scenario from float4 to numeric?

init_var(&result);

Expand Down