Changeset: 7829c78d7140 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/7829c78d7140
Added Files:
        sql/test/BugTracker-2026/Tests/8127-date-overflow.test
Modified Files:
        sql/backends/monet5/generator/generator.c
        sql/test/BugTracker-2026/Tests/All
Branch: Dec2025
Log Message:

Check for overflow.
Fixes #8172.


diffs (124 lines):

diff --git a/sql/backends/monet5/generator/generator.c 
b/sql/backends/monet5/generator/generator.c
--- a/sql/backends/monet5/generator/generator.c
+++ b/sql/backends/monet5/generator/generator.c
@@ -242,32 +242,24 @@ VLTgenerator_table_(BAT **result, Client
                                is_date_nil(f) || is_date_nil(l))
                                throw(MAL, "generator.table",
                                      SQLSTATE(42000) "Illegal generator 
range");
-                       n = (BUN) (date_diff(l, f) / (s *28)); /* n maybe too 
large now */
+                       if (s > GDK_int_max / 28)
+                               n = 0;
+                       else
+                               n = (BUN) (date_diff(l, f) / (s * 28)); /* n 
maybe too large now */
                        bn = COLnew(0, TYPE_date, n + 1, TRANSIENT);
                        if (bn == NULL)
                                throw(MAL, "generator.table", SQLSTATE(HY013) 
MAL_MALLOC_FAIL);
                        v = (date *) Tloc(bn, 0);
-                       BUN c;
                        if (s < 0) {
-                               for (c = 0; f > l; c++) {
+                               for (; !is_date_nil(f) && f > l; f = 
date_add_month(f, s)) {
                                        *v++ = f;
-                                       f = date_add_month(f, s);
-                                       if (is_date_nil(f)) {
-                                               BBPreclaim(bn);
-                                               throw(MAL, "generator.table", 
SQLSTATE(22003) "overflow in calculation");
-                                       }
                                }
                        } else {
-                               for (c = 0; f < l; c++) {
+                               for (; !is_date_nil(f) && f < l; f = 
date_add_month(f, s)) {
                                        *v++ = f;
-                                       f = date_add_month(f, s);
-                                       if (is_date_nil(f)) {
-                                               BBPreclaim(bn);
-                                               throw(MAL, "generator.table", 
SQLSTATE(22003) "overflow in calculation");
-                                       }
                                }
                        }
-                       n = c;
+                       n = v - (date *) Tloc(bn, 0);
                        bn->tsorted = s > 0 || n <= 1;
                        bn->trevsorted = s < 0 || n <= 1;
                } else if (tpe == TYPE_date) { /* days */
@@ -294,27 +286,16 @@ VLTgenerator_table_(BAT **result, Client
                        if (bn == NULL)
                                throw(MAL, "generator.table", SQLSTATE(HY013) 
MAL_MALLOC_FAIL);
                        v = (date *) Tloc(bn, 0);
-                       BUN c;
                        if (s < 0) {
-                               for (c = 0; c < n && f > l; c++) {
+                               for (; !is_date_nil(f) && f > l; f = 
date_add_day(f, s)) {
                                        *v++ = f;
-                                       f = date_add_day(f, (int) s);
-                                       if (is_date_nil(f)) {
-                                               BBPreclaim(bn);
-                                               throw(MAL, "generator.table", 
SQLSTATE(22003) "overflow in calculation");
-                                       }
                                }
                        } else {
-                               for (c = 0; c < n && f < l; c++) {
+                               for (; !is_date_nil(f) && f < l; f = 
date_add_day(f, s)) {
                                        *v++ = f;
-                                       f = date_add_day(f, (int) s);
-                                       if (is_date_nil(f)) {
-                                               BBPreclaim(bn);
-                                               throw(MAL, "generator.table", 
SQLSTATE(22003) "overflow in calculation");
-                                       }
                                }
                        }
-                       n = c;
+                       n = v - (date *) Tloc(bn, 0);
                        bn->tsorted = s > 0 || n <= 1;
                        bn->trevsorted = s < 0 || n <= 1;
                } else if (tpe == TYPE_timestamp) {
@@ -345,18 +326,16 @@ VLTgenerator_table_(BAT **result, Client
                        if (bn == NULL)
                                throw(MAL, "generator.table", SQLSTATE(HY013) 
MAL_MALLOC_FAIL);
                        v = (timestamp *) Tloc(bn, 0);
-                       for (BUN c = 0; c < n; c++) {
-                               *v++ = f;
-                               f = timestamp_add_usec(f, s);
-                               if (is_timestamp_nil(f)) {
-                                       BBPreclaim(bn);
-                                       throw(MAL, "generator.table", 
SQLSTATE(22003) "overflow in calculation");
+                       if (s < 0) {
+                               for (; !is_timestamp_nil(f) && f > l; f = 
timestamp_add_usec(f, s)) {
+                                       *v++ = f;
+                               }
+                       } else {
+                               for (; !is_timestamp_nil(f) && f < l; f = 
timestamp_add_usec(f, s)) {
+                                       *v++ = f;
                                }
                        }
-                       if (f != l) {
-                               *v++ = f;
-                               n++;
-                       }
+                       n = v - (timestamp *) Tloc(bn, 0);
                        bn->tsorted = s > 0 || n <= 1;
                        bn->trevsorted = s < 0 || n <= 1;
                } else {
diff --git a/sql/test/BugTracker-2026/Tests/8127-date-overflow.test 
b/sql/test/BugTracker-2026/Tests/8127-date-overflow.test
new file mode 100644
--- /dev/null
+++ b/sql/test/BugTracker-2026/Tests/8127-date-overflow.test
@@ -0,0 +1,10 @@
+query ITT nosort
+SELECT count(*), min(value), max(value)
+FROM generate_series(
+    DATE '2000-01-01',
+    DATE '2000-01-02',
+    INTERVAL '1073741824' MONTH
+)
+----
+1      2000-01-01      2000-01-01
+
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
@@ -214,3 +214,4 @@ KNOWNFAIL?8030-inner-join-group-by-bug
 8107-select-generate-date
 8113-dict-compress
 8118-between-dense
+8127-date-overflow
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to