Changeset: 5814c9e3b218 for MonetDB URL: https://dev.monetdb.org/hg/MonetDB/rev/5814c9e3b218 Added Files: sql/test/BugTracker-2026/Tests/8038-decimal-double-cast.test Modified Files: gdk/gdk_calc_convert.c sql/test/BugTracker-2026/Tests/All Branch: default Log Message:
Try to preserve as much precision as possible when casting decimal to float.
Fixes #8038.
diffs (126 lines):
diff --git a/gdk/gdk_calc_convert.c b/gdk/gdk_calc_convert.c
--- a/gdk/gdk_calc_convert.c
+++ b/gdk/gdk_calc_convert.c
@@ -115,8 +115,9 @@ convert_##TYPE1##_##TYPE2(const TYPE1 *s
if (is_##TYPE1##_nil(v)) { \
dst[i] = TYPE2##_nil; \
nils++; \
- } else \
+ } else { \
dst[i] = (TYPE2) v; \
+ } \
} \
} else { \
TIMEOUT_LOOP_IDX(i, ci->ncand, qry_ctx) { \
@@ -125,8 +126,25 @@ convert_##TYPE1##_##TYPE2(const TYPE1 *s
if (is_##TYPE1##_nil(v)) { \
dst[i] = TYPE2##_nil; \
nils++; \
- } else \
- dst[i] = (TYPE2) v / div; \
+ } else { \
+ TYPE1 d = div; \
+ while (d > 1 && v % 10 == 0) { \
+ d /= 10; \
+ v /= 10; \
+ } \
+ dst[i] = (TYPE2) v / d; \
+ } \
+ } \
+ } \
+ } else if (div == 1) { \
+ TIMEOUT_LOOP_IDX(i, ci->ncand, qry_ctx) { \
+ x = canditer_next(ci) - candoff; \
+ v = src[x]; \
+ if (is_##TYPE1##_nil(v)) { \
+ dst[i] = TYPE2##_nil; \
+ nils++; \
+ } else { \
+ dst[i] = (TYPE2) v; \
} \
} \
} else { \
@@ -136,8 +154,41 @@ convert_##TYPE1##_##TYPE2(const TYPE1 *s
if (is_##TYPE1##_nil(v)) { \
dst[i] = TYPE2##_nil; \
nils++; \
- } else \
- dst[i] = (TYPE2) v / div; \
+ } else { \
+ TYPE1 d = div; \
+ while (d > 1 && v % 10 == 0) { \
+ d /= 10; \
+ v /= 10; \
+ } \
+ dst[i] = (TYPE2) v / d; \
+ } \
+ } \
+ } \
+ TIMEOUT_CHECK(qry_ctx, TIMEOUT_HANDLER(BUN_NONE, qry_ctx)); \
+ return nils; \
+}
+
+#define convertimpl_float_float(TYPE1, TYPE2, MANT_DIG)
\
+static BUN \
+convert_##TYPE1##_##TYPE2(const TYPE1 *src, TYPE2 *restrict dst, \
+ struct canditer *restrict ci, \
+ oid candoff, uint8_t scale1, bool *reduce) \
+{ \
+ BUN i, nils = 0; \
+ TYPE1 v; \
+ oid x; \
+ const TYPE1 div = (TYPE1) scales[scale1]; \
+ QryCtx *qry_ctx = MT_thread_get_qry_ctx(); \
+ \
+ *reduce = 8 * sizeof(TYPE1) > MANT_DIG; \
+ TIMEOUT_LOOP_IDX(i, ci->ncand, qry_ctx) { \
+ x = canditer_next(ci) - candoff; \
+ v = src[x]; \
+ if (is_##TYPE1##_nil(v)) { \
+ dst[i] = TYPE2##_nil; \
+ nils++; \
+ } else { \
+ dst[i] = (TYPE2) v / div; \
} \
} \
TIMEOUT_CHECK(qry_ctx, TIMEOUT_HANDLER(BUN_NONE, qry_ctx)); \
@@ -618,8 +669,8 @@ convertimpl_reduce_float(flt, lng)
#ifdef HAVE_HGE
convertimpl_reduce_float(flt, hge)
#endif
-convertimpl_enlarge_float(flt, flt, 128)
-convertimpl_enlarge_float(flt, dbl, DBL_MANT_DIG)
+convertimpl_float_float(flt, flt, 128)
+convertimpl_float_float(flt, dbl, DBL_MANT_DIG)
convertimpl_reduce_float(dbl, bte)
convertimpl_reduce_float(dbl, sht)
@@ -633,7 +684,7 @@ convertimpl_reduce_float(dbl, hge)
/* no rounding here */
#define rounddbl(x) (x)
convertimpl_reduce_float(dbl, flt)
-convertimpl_enlarge_float(dbl, dbl, 128)
+convertimpl_float_float(dbl, dbl, 128)
convert2bit_impl(bte)
convert2bit_impl(sht)
diff --git a/sql/test/BugTracker-2026/Tests/8038-decimal-double-cast.test
b/sql/test/BugTracker-2026/Tests/8038-decimal-double-cast.test
new file mode 100644
--- /dev/null
+++ b/sql/test/BugTracker-2026/Tests/8038-decimal-double-cast.test
@@ -0,0 +1,7 @@
+query RR nosort
+SELECT
+ CAST(CAST(9007199254740991.0 AS DOUBLE) AS BIGINT) AS from_decimal_literal,
+ CAST(CAST(9007199254740991 AS DOUBLE) AS BIGINT) AS from_integer_literal
+----
+9007199254740991.000 9007199254740991.000
+
diff --git a/sql/test/BugTracker-2026/Tests/All
b/sql/test/BugTracker-2026/Tests/All
--- a/sql/test/BugTracker-2026/Tests/All
+++ b/sql/test/BugTracker-2026/Tests/All
@@ -254,3 +254,4 @@ KNOWNFAIL?8010-first_value-last_value-pa
8117-duplicate-cte
8118-between-dense
8127-date-overflow
+8038-decimal-double-cast
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]
