Changeset: 6b1b04edbf04 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/6b1b04edbf04
Modified Files:
        sql/server/rel_statistics.c
        sql/storage/bat/bat_storage.c
Branch: persistent_sketches
Log Message:

merged with default


diffs (truncated from 2884 to 300 lines):

diff --git a/clients/odbc/driver/SQLConnect.c b/clients/odbc/driver/SQLConnect.c
--- a/clients/odbc/driver/SQLConnect.c
+++ b/clients/odbc/driver/SQLConnect.c
@@ -134,7 +134,7 @@ makeNulTerminated(const SQLCHAR **argume
                *scratch = value;
        }
 
-       return value;
+       return value != NULL;
 }
 
 char*
diff --git a/gdk/gdk_analytic_bounds.c b/gdk/gdk_analytic_bounds.c
--- a/gdk/gdk_analytic_bounds.c
+++ b/gdk/gdk_analytic_bounds.c
@@ -375,14 +375,52 @@ GDKanalyticaldiff(BAT *b, BAT *p, const 
 #define date_sub_month(D, M)           date_add_month(D, -(M))
 #define timestamp_sub_month(T, M)      timestamp_add_month(T, -(M))
 
+#define DAY_MSEC                       (24 * 60 * 60 * 1000)
+
+static inline bool
+date_msec_overflow(lng ms)
+{
+       lng days = ms / DAY_MSEC;
+       return days < INT_MIN || days > INT_MAX;
+}
+
+static inline bool
+msec_overflow(lng ms)
+{
+       return ms < GDK_lng_min / 1000 || ms > GDK_lng_max / 1000;
+}
+
+static inline bool
+mtime_no_overflow(lng ms)
+{
+       (void) ms;
+       return false;
+}
+
+/* A millisecond limit is narrowed to an int day count for DATE and multiplied
+ * by 1000 for DAYTIME/TIMESTAMP.  Check before the arithmetic, since either
+ * operation would wrap and silently produce a much smaller bound.  A nil
+ * result from the arithmetic itself is not an overflow: it remains an
+ * unbounded frame, as it was before. */
 #define daytime_add_msec(D, M)         daytime_add_usec(D, 1000*(M))
 #define daytime_sub_msec(D, M)         daytime_add_usec(D, -1000*(M))
-#define date_add_msec(D, M)            date_add_day(D, (int) 
((M)/(24*60*60*1000)))
-#define date_sub_msec(D, M)            date_add_day(D, (int) 
-((M)/(24*60*60*1000)))
+#define date_add_msec(D, M)            date_add_day(D, (int) ((M)/DAY_MSEC))
+#define date_sub_msec(D, M)            date_add_day(D, (int) -((M)/DAY_MSEC))
 #define timestamp_add_msec(T, M)       timestamp_add_usec(T, (M)*1000)
 #define timestamp_sub_msec(T, M)       timestamp_add_usec(T, -(M)*1000)
 
-#define ANALYTICAL_WINDOW_BOUNDS_RANGE_MTIME_PRECEDING(TPE1, LIMIT, TPE2, SUB, 
ADD) \
+#define MTIME_BOUNDS(TPE, V, VMIN, VMAX, SUB, ADD, M, OVERFLOW, NIL_OVERFLOW) \
+       do {                                                    \
+               if (OVERFLOW(M))                                \
+                       goto calc_overflow;                     \
+               VMIN = SUB(V, M);                               \
+               VMAX = ADD(V, M);                               \
+               if (NIL_OVERFLOW &&                             \
+                   (is_##TPE##_nil(VMIN) || is_##TPE##_nil(VMAX))) \
+                       goto calc_overflow;                     \
+       } while (0)
+
+#define ANALYTICAL_WINDOW_BOUNDS_RANGE_MTIME_PRECEDING(TPE1, LIMIT, TPE2, SUB, 
ADD, OVERFLOW, NIL_OVERFLOW) \
        do {                                                            \
                BUN m = k;                                              \
                TPE1 v, vmin, vmax;                                     \
@@ -392,8 +430,7 @@ GDKanalyticaldiff(BAT *b, BAT *p, const 
                                if (is_##TPE1##_nil(rlimit) || rlimit < 0) \
                                        goto invalid_bound;             \
                                v = bp[k];                              \
-                               vmin = SUB(v, rlimit);                  \
-                               vmax = ADD(v, rlimit);                  \
+                               MTIME_BOUNDS(TPE1, v, vmin, vmax, SUB, ADD, 
rlimit, OVERFLOW, NIL_OVERFLOW); \
                                for (j=k; ; j--) {                      \
                                        if ((!is_##TPE1##_nil(vmin) && bp[j] < 
vmin) || (!is_##TPE1##_nil(vmax) && bp[j] > vmax)) { \
                                                j++;                    \
@@ -421,8 +458,7 @@ GDKanalyticaldiff(BAT *b, BAT *p, const 
                                        }                               \
                                        rb[k] = j;                      \
                                } else {                                \
-                                       vmin = SUB(v, rlimit);          \
-                                       vmax = ADD(v, rlimit);          \
+                                       MTIME_BOUNDS(TPE1, v, vmin, vmax, SUB, 
ADD, rlimit, OVERFLOW, NIL_OVERFLOW); \
                                        for (j=k; ; j--) {              \
                                                if (is_##TPE1##_nil(bp[j])) { \
                                                        j++;            \
@@ -441,7 +477,7 @@ GDKanalyticaldiff(BAT *b, BAT *p, const 
                }                                                       \
        } while(0)
 
-#define ANALYTICAL_WINDOW_BOUNDS_RANGE_MTIME_FOLLOWING(TPE1, LIMIT, TPE2, SUB, 
ADD) \
+#define ANALYTICAL_WINDOW_BOUNDS_RANGE_MTIME_FOLLOWING(TPE1, LIMIT, TPE2, SUB, 
ADD, OVERFLOW, NIL_OVERFLOW) \
        do {                                                            \
                TPE1 v, vmin, vmax;                                     \
                if (bi.nonil) {                                         \
@@ -450,8 +486,7 @@ GDKanalyticaldiff(BAT *b, BAT *p, const 
                                if (is_##TPE1##_nil(rlimit) || rlimit < 0) \
                                        goto invalid_bound;             \
                                v = bp[k];                              \
-                               vmin = SUB(v, rlimit);                  \
-                               vmax = ADD(v, rlimit);                  \
+                               MTIME_BOUNDS(TPE1, v, vmin, vmax, SUB, ADD, 
rlimit, OVERFLOW, NIL_OVERFLOW); \
                                for (j=k+1; j<i; j++)                   \
                                        if ((!is_##TPE1##_nil(vmin) && bp[j] < 
vmin) || (!is_##TPE1##_nil(vmax) && bp[j] > vmax)) \
                                                break;                  \
@@ -468,8 +503,7 @@ GDKanalyticaldiff(BAT *b, BAT *p, const 
                                                if (!is_##TPE1##_nil(bp[j])) \
                                                        break;          \
                                } else {                                \
-                                       vmin = SUB(v, rlimit);          \
-                                       vmax = ADD(v, rlimit);          \
+                                       MTIME_BOUNDS(TPE1, v, vmin, vmax, SUB, 
ADD, rlimit, OVERFLOW, NIL_OVERFLOW); \
                                        for (j=k+1; j<i; j++) {         \
                                                if (is_##TPE1##_nil(bp[j]) || 
(!is_##TPE1##_nil(vmin) && bp[j] < vmin) || (!is_##TPE1##_nil(vmax) && bp[j] > 
vmax)) \
                                                        break;          \
@@ -480,14 +514,14 @@ GDKanalyticaldiff(BAT *b, BAT *p, const 
                }                                                       \
        } while(0)
 
-#define ANALYTICAL_WINDOW_BOUNDS_CALC_MTIME(TPE1, IMP, CARD, LIMIT, TPE2, SUB, 
ADD) \
+#define ANALYTICAL_WINDOW_BOUNDS_CALC_MTIME(TPE1, IMP, CARD, LIMIT, TPE2, SUB, 
ADD, OVERFLOW, NIL_OVERFLOW) \
        do {                                                            \
                const TPE1 *restrict bp = (TPE1*)bi.base;               \
                if (p == NULL)                                          \
                        i = cnt;                                        \
                for (; i <= cnt; i++) {                                 \
                        if (i == cnt || np[i])                          \
-                               IMP(TPE1, LIMIT, TPE2, SUB, ADD);       \
+                               IMP(TPE1, LIMIT, TPE2, SUB, ADD, OVERFLOW, 
NIL_OVERFLOW); \
                }                                                       \
        } while(0)
 
@@ -495,10 +529,10 @@ GDKanalyticaldiff(BAT *b, BAT *p, const 
        do {                                                            \
                switch (tp1) {                                          \
                case TYPE_date:                                         \
-                       ANALYTICAL_WINDOW_BOUNDS_CALC_MTIME(date, 
ANALYTICAL_WINDOW_BOUNDS_RANGE_MTIME##IMP, CARD, LIMIT, int, date_sub_month, 
date_add_month); \
+                       ANALYTICAL_WINDOW_BOUNDS_CALC_MTIME(date, 
ANALYTICAL_WINDOW_BOUNDS_RANGE_MTIME##IMP, CARD, LIMIT, int, date_sub_month, 
date_add_month, mtime_no_overflow, false); \
                        break;                                          \
                case TYPE_timestamp:                                    \
-                       ANALYTICAL_WINDOW_BOUNDS_CALC_MTIME(timestamp, 
ANALYTICAL_WINDOW_BOUNDS_RANGE_MTIME##IMP, CARD, LIMIT, int, 
timestamp_sub_month, timestamp_add_month); \
+                       ANALYTICAL_WINDOW_BOUNDS_CALC_MTIME(timestamp, 
ANALYTICAL_WINDOW_BOUNDS_RANGE_MTIME##IMP, CARD, LIMIT, int, 
timestamp_sub_month, timestamp_add_month, mtime_no_overflow, false); \
                        break;                                          \
                default:                                                \
                        goto type_not_supported;                        \
@@ -509,13 +543,13 @@ GDKanalyticaldiff(BAT *b, BAT *p, const 
        do {                                                            \
                switch (tp1) {                                          \
                case TYPE_date:                                         \
-                       ANALYTICAL_WINDOW_BOUNDS_CALC_MTIME(date, 
ANALYTICAL_WINDOW_BOUNDS_RANGE_MTIME##IMP, CARD, LIMIT, lng, date_sub_msec, 
date_add_msec); \
+                       ANALYTICAL_WINDOW_BOUNDS_CALC_MTIME(date, 
ANALYTICAL_WINDOW_BOUNDS_RANGE_MTIME##IMP, CARD, LIMIT, lng, date_sub_msec, 
date_add_msec, date_msec_overflow, true); \
                        break;                                          \
                case TYPE_daytime:                                      \
-                       ANALYTICAL_WINDOW_BOUNDS_CALC_MTIME(daytime, 
ANALYTICAL_WINDOW_BOUNDS_RANGE_MTIME##IMP, CARD, LIMIT, lng, daytime_sub_msec, 
daytime_add_msec); \
+                       ANALYTICAL_WINDOW_BOUNDS_CALC_MTIME(daytime, 
ANALYTICAL_WINDOW_BOUNDS_RANGE_MTIME##IMP, CARD, LIMIT, lng, daytime_sub_msec, 
daytime_add_msec, msec_overflow, false); \
                        break;                                          \
                case TYPE_timestamp:                                    \
-                       ANALYTICAL_WINDOW_BOUNDS_CALC_MTIME(timestamp, 
ANALYTICAL_WINDOW_BOUNDS_RANGE_MTIME##IMP, CARD, LIMIT, lng, 
timestamp_sub_msec, timestamp_add_msec); \
+                       ANALYTICAL_WINDOW_BOUNDS_CALC_MTIME(timestamp, 
ANALYTICAL_WINDOW_BOUNDS_RANGE_MTIME##IMP, CARD, LIMIT, lng, 
timestamp_sub_msec, timestamp_add_msec, msec_overflow, true); \
                        break;                                          \
                default:                                                \
                        goto type_not_supported;                        \
diff --git a/monetdb5/mal/mal_session.c b/monetdb5/mal/mal_session.c
--- a/monetdb5/mal/mal_session.c
+++ b/monetdb5/mal/mal_session.c
@@ -328,7 +328,7 @@ MALparser(Client c)
        c->yycur = 0;
        c->qryctx.starttime = GDKusec();
        c->qryctx.endtime = c->querytimeout ? c->qryctx.starttime + 
c->querytimeout : 0;
-       if (c->qryctx.endtime == 0 || c->sessiontimeout < c->qryctx.endtime)
+       if (c->sessiontimeout && (c->qryctx.endtime == 0 || c->sessiontimeout < 
c->qryctx.endtime))
                c->qryctx.endtime = c->sessiontimeout;
 
        /* check for unfinished blocks */
diff --git a/monetdb5/modules/atoms/json.c b/monetdb5/modules/atoms/json.c
--- a/monetdb5/modules/atoms/json.c
+++ b/monetdb5/modules/atoms/json.c
@@ -12,6 +12,7 @@
  * (c) 2013 Martin Kersten
  */
 #include "monetdb_config.h"
+#include <errno.h>
 #include "gdk.h"
 #include "mal.h"
 #include "mal_client.h"
@@ -716,8 +717,9 @@ typedef struct {
        path_token token;                       /* Token kind */
        char *name;                                     /* specific key */
        size_t namelen;                         /* the size of the key */
-       int index;                                      /* if index == INT_MAX 
we got [*] */
-       int first, last;
+       bool any;                                       /* no explicit index, 
or [*] */
+       lng index;                                      /* explicit array index 
*/
+       lng first, last;
 } pattern;
 
 static str
@@ -729,9 +731,10 @@ JSONcompile(Client ctx, const char *expr
 
        for (s = expr; *s; s++) {
                terms[t].token = CHILD_STEP;
-               terms[t].index = INT_MAX;
-               terms[t].first = INT_MAX;
-               terms[t].last = INT_MAX;
+               terms[t].any = true;
+               terms[t].index = 0;
+               terms[t].first = 0;
+               terms[t].last = 0;
                if (*s == '$') {
                        if (t && terms[t - 1].token != END_STEP)
                                throw(MAL, "json.compile", "Root node must be 
first");
@@ -774,8 +777,14 @@ JSONcompile(Client ctx, const char *expr
                        if (*s != '*') {
                                /* TODO: Implement [start:step:end] indexing */
                                if (isdigit((unsigned char) *s)) {
-                                       terms[t].index = atoi(s);
-                                       terms[t].first = terms[t].last = 
atoi(s);
+                                       long long val;
+
+                                       errno = 0;
+                                       val = strtoll(s, NULL, 10);
+                                       if (errno == ERANGE || val < (long 
long) GDK_lng_min || val > (long long) GDK_lng_max)
+                                               throw(MAL, "json.path", "index 
out of range");
+                                       terms[t].any = false;
+                                       terms[t].index = terms[t].first = 
terms[t].last = (lng) val;
                                } else
                                        throw(MAL, "json.path", "'*' or digit 
expected");
                        }
@@ -875,7 +884,7 @@ JSONmatch(allocator *ma, JSON *jt, int j
                }
                cnt = 0;
                for (i = jt->elm[ji].next; i && cnt >= 0; i = jt->elm[i].next, 
cnt++) {
-                       if (terms[ti].index == INT_MAX
+                       if (terms[ti].any
                                || (cnt >= terms[ti].first && cnt <= 
terms[ti].last)) {
                                if (terms[ti].token == ANY_STEP) {
                                        if (jt->elm[i].child)
@@ -892,7 +901,7 @@ JSONmatch(allocator *ma, JSON *jt, int j
                                        if (r == NULL)
                                                r = (str) -1;
                                } else {
-                                       r = JSONmatch(ma, jt, jt->elm[i].child, 
terms, ti + 1, terms[ti].index == INT_MAX);
+                                       r = JSONmatch(ma, jt, jt->elm[i].child, 
terms, ti + 1, terms[ti].any);
                                }
                                if (r == (str) -1 || r == (str) -2) {
                                        return r;
@@ -909,7 +918,7 @@ JSONmatch(allocator *ma, JSON *jt, int j
                                 && strncmp(terms[ti].name, jt->elm[i].value,
                                                        terms[ti].namelen) == 
0) || terms[ti].name == 0
                                || terms[ti].name[0] == '*') {
-                               if (terms[ti].index == INT_MAX
+                               if (terms[ti].any
                                        || (cnt >= terms[ti].first && cnt <= 
terms[ti].last)) {
                                        if (ti + 1 == MAXTERMS)
                                                return NULL;
@@ -918,7 +927,7 @@ JSONmatch(allocator *ma, JSON *jt, int j
                                                if (r == NULL)
                                                        r = (str) -1;
                                        } else {
-                                               r = JSONmatch(ma, jt, 
jt->elm[i].child, terms, ti + 1, terms[ti].index == INT_MAX);
+                                               r = JSONmatch(ma, jt, 
jt->elm[i].child, terms, ti + 1, terms[ti].any);
                                        }
                                        if (r == (str) -1 || r == (str) -2) {
                                                return r;
@@ -1021,8 +1030,8 @@ JSONfilterInternal(Client ctx, json *ret
                // pattern contains the .. operator
                if (terms[i].token == ANY_STEP ||
                        // pattern contains the [*] operator
-                       (terms[i].token == CHILD_STEP && terms[i].index == 
INT_MAX && terms[i].name == NULL) ||
-                       (terms[i].token == CHILD_STEP && terms[i].index == 
INT_MAX && *terms[i].name == '*')) {
+                       (terms[i].token == CHILD_STEP && terms[i].any && 
terms[i].name == NULL) ||
+                       (terms[i].token == CHILD_STEP && terms[i].any && 
*terms[i].name == '*')) {
 
                        return_array = true;
                        break;
diff --git a/monetdb5/modules/atoms/mtime.h b/monetdb5/modules/atoms/mtime.h
--- a/monetdb5/modules/atoms/mtime.h
+++ b/monetdb5/modules/atoms/mtime.h
@@ -17,13 +17,15 @@
 #include "mal_interpreter.h"
 #include "mal_exception.h"
 
+#define DAY_MSEC       (24 * 60 * 60 * 1000)
+
 /* TODO change dayint again into an int instead of lng */
 ATTR_CONST
 static inline lng
 date_diff_imp(const date d1, const date d2)
 {
        int diff = date_diff(d1, d2);
-       return is_int_nil(diff) ? lng_nil : (lng) diff *(lng) (24 * 60 * 60 * 
1000);
+       return is_int_nil(diff) ? lng_nil : (lng) diff * (lng) DAY_MSEC;
 }
 
 ATTR_CONST
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to