Changeset: f69ea2e3922b for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/f69ea2e3922b
Modified Files:
        gdk/gdk_firstn.c
        sql/server/rel_updates.c
Branch: default
Log Message:

Merge with Aug2024 branch.


diffs (truncated from 2035 to 300 lines):

diff --git a/gdk/gdk_firstn.c b/gdk/gdk_firstn.c
--- a/gdk/gdk_firstn.c
+++ b/gdk/gdk_firstn.c
@@ -187,8 +187,7 @@
        do {                                                            \
                const TYPE *restrict vals = (const TYPE *) bi->base;    \
                heapify(OP##fix, SWAP1);                                \
-               while (cnt > 0) {                                       \
-                       cnt--;                                          \
+               TIMEOUT_LOOP(ci.ncand - n, qry_ctx) {                   \
                        i = canditer_next(&ci);                         \
                        if (OP(vals[i - hseq],                          \
                               vals[oids[0] - hseq])) {                 \
@@ -214,7 +213,7 @@ BATfirstn_unique(BATiter *bi, BAT *s, BU
        BAT *bn;
        oid *restrict oids;
        oid hseq = bi->b->hseqbase;
-       BUN i, cnt;
+       BUN i;
        struct canditer ci;
        int tpe = bi->type;
        int (*cmp)(const void *, const void *);
@@ -225,9 +224,8 @@ BATfirstn_unique(BATiter *bi, BAT *s, BU
 
        MT_thread_setalgorithm(__func__);
        canditer_init(&ci, bi->b, s);
-       cnt = ci.ncand;
 
-       if (n >= cnt) {
+       if (n >= ci.ncand) {
                /* trivial: return all candidates */
                bn = canditer_slice(&ci, 0, ci.ncand);
                if (bn && lastp)
@@ -258,7 +256,7 @@ BATfirstn_unique(BATiter *bi, BAT *s, BU
                }
                /* return the last part of the candidate list or of
                 * the BAT itself */
-               bn = canditer_slice(&ci, cnt - n, cnt);
+               bn = canditer_slice(&ci, ci.ncand - n, ci.ncand);
                if (bn && lastp)
                        *lastp = BUNtoid(bn, 0);
                TRC_DEBUG(ALGO, "b=" ALGOBATFMT ",s=" ALGOOPTBATFMT
@@ -281,18 +279,18 @@ BATfirstn_unique(BATiter *bi, BAT *s, BU
                if (nilslast == asc && !bi->nonil) {
                        pos = SORTfndlast(bi->b, ATOMnilptr(tpe));
                        pos = canditer_search(&ci, hseq + pos, true);
-                       /* 0 <= pos <= cnt
-                        * 0 < n < cnt
+                       /* 0 <= pos <= ci.ncand
+                        * 0 < n < ci.ncand
                         */
                        if (bi->sorted) {
                                /* [0..pos) -- nil
-                                * [pos..cnt) -- non-nil <<<
+                                * [pos..ci.ncand) -- non-nil <<<
                                 */
                                if (asc) { /* i.e. nilslast */
                                        /* prefer non-nil and
                                         * smallest */
-                                       if (cnt - pos < n) {
-                                               bn = canditer_slice(&ci, cnt - 
n, cnt);
+                                       if (ci.ncand - pos < n) {
+                                               bn = canditer_slice(&ci, 
ci.ncand - n, ci.ncand);
                                                pos = 0;
                                        } else {
                                                bn = canditer_slice(&ci, pos, 
pos + n);
@@ -301,7 +299,7 @@ BATfirstn_unique(BATiter *bi, BAT *s, BU
                                } else { /* i.e. !asc, !nilslast */
                                        /* prefer nil and largest */
                                        if (pos < n) {
-                                               bn = canditer_slice2(&ci, 0, 
pos, cnt - (n - pos), cnt);
+                                               bn = canditer_slice2(&ci, 0, 
pos, ci.ncand - (n - pos), ci.ncand);
                                                /* pos = pos; */
                                        } else {
                                                bn = canditer_slice(&ci, 0, n);
@@ -310,7 +308,7 @@ BATfirstn_unique(BATiter *bi, BAT *s, BU
                                }
                        } else { /* i.e. trevsorted */
                                /* [0..pos) -- non-nil >>>
-                                * [pos..cnt) -- nil
+                                * [pos..ci.ncand) -- nil
                                 */
                                if (asc) { /* i.e. nilslast */
                                        /* prefer non-nil and
@@ -324,9 +322,9 @@ BATfirstn_unique(BATiter *bi, BAT *s, BU
                                        }
                                } else { /* i.e. !asc, !nilslast */
                                        /* prefer nil and largest */
-                                       if (cnt - pos < n) {
-                                               bn = canditer_slice2(&ci, 0, n 
- (cnt - pos), pos, cnt);
-                                               pos = n - (cnt - pos) - 1;
+                                       if (ci.ncand - pos < n) {
+                                               bn = canditer_slice2(&ci, 0, n 
- (ci.ncand - pos), pos, ci.ncand);
+                                               pos = n - (ci.ncand - pos) - 1;
                                        } else {
                                                bn = canditer_slice(&ci, pos, 
pos + n);
                                                pos = 0;
@@ -345,7 +343,7 @@ BATfirstn_unique(BATiter *bi, BAT *s, BU
                        } else {
                                /* return copy of last part of
                                 * candidate list */
-                               bn = canditer_slice(&ci, cnt - n, cnt);
+                               bn = canditer_slice(&ci, ci.ncand - n, 
ci.ncand);
                                pos = 0;
                        }
                }
@@ -382,14 +380,15 @@ BATfirstn_unique(BATiter *bi, BAT *s, BU
                for (i = 0; i < n; i++)
                        oids[i] = canditer_next(&ci);
        } else {
-               item = canditer_idx(&ci, cnt - n);
-               ci.next = cnt - n;
-               ci.add = item - ci.seq - (cnt - n);
+               item = canditer_idx(&ci, ci.ncand - n);
+               ci.next = ci.ncand - n;
+               ci.add = item - ci.seq - (ci.ncand - n);
                for (i = n; i > 0; i--)
                        oids[i - 1] = canditer_next(&ci);
                canditer_reset(&ci);
        }
-       cnt -= n;
+
+       QryCtx *qry_ctx = MT_thread_get_qry_ctx();
 
        if (asc) {
                if (nilslast && !bi->nonil) {
@@ -419,8 +418,7 @@ BATfirstn_unique(BATiter *bi, BAT *s, BU
                                break;
                        default:
                                heapify(nLTany, SWAP1);
-                               while (cnt > 0) {
-                                       cnt--;
+                               TIMEOUT_LOOP(ci.ncand - n, qry_ctx) {
                                        i = canditer_next(&ci);
                                        if (cmp(BUNtail(*bi, i - hseq), nil) != 0
                                            && (cmp(BUNtail(*bi, oids[0] - 
hseq), nil) == 0
@@ -459,8 +457,7 @@ BATfirstn_unique(BATiter *bi, BAT *s, BU
                                break;
                        default:
                                heapify(LTany, SWAP1);
-                               while (cnt > 0) {
-                                       cnt--;
+                               TIMEOUT_LOOP(ci.ncand - n, qry_ctx) {
                                        i = canditer_next(&ci);
                                        if (cmp(BUNtail(*bi, i - hseq),
                                                BUNtail(*bi, oids[0] - hseq)) < 
0) {
@@ -499,8 +496,7 @@ BATfirstn_unique(BATiter *bi, BAT *s, BU
                                break;
                        default:
                                heapify(GTany, SWAP1);
-                               while (cnt > 0) {
-                                       cnt--;
+                               TIMEOUT_LOOP(ci.ncand - n, qry_ctx) {
                                        i = canditer_next(&ci);
                                        if (cmp(BUNtail(*bi, i - hseq),
                                                BUNtail(*bi, oids[0] - hseq)) > 
0) {
@@ -537,8 +533,7 @@ BATfirstn_unique(BATiter *bi, BAT *s, BU
                                break;
                        default:
                                heapify(nGTany, SWAP1);
-                               while (cnt > 0) {
-                                       cnt--;
+                               TIMEOUT_LOOP(ci.ncand - n, qry_ctx) {
                                        i = canditer_next(&ci);
                                        if (cmp(BUNtail(*bi, oids[0] - hseq), 
nil) != 0
                                            && (cmp(BUNtail(*bi, i - hseq), 
nil) == 0
@@ -552,6 +547,7 @@ BATfirstn_unique(BATiter *bi, BAT *s, BU
                        }
                }
        }
+       TIMEOUT_CHECK(qry_ctx, GOTO_LABEL_TIMEOUT_HANDLER(bailout, qry_ctx));
        if (lastp)
                *lastp = oids[0]; /* store id of largest value */
        /* output must be sorted since it's a candidate list */
@@ -569,6 +565,10 @@ BATfirstn_unique(BATiter *bi, BAT *s, BU
                  ALGOBATPAR(bi->b), ALGOOPTBATPAR(s), n,
                  ALGOOPTBATPAR(bn), GDKusec() - t0);
        return bn;
+
+  bailout:
+       BBPreclaim(bn);
+       return NULL;
 }
 
 #define LTfixgrp(p1, p2)                       \
@@ -687,8 +687,7 @@ BATfirstn_unique(BATiter *bi, BAT *s, BU
        do {                                                            \
                const TYPE *restrict vals = (const TYPE *) bi->base;    \
                heapify(OP##fixgrp, SWAP2);                             \
-               while (cnt > 0) {                                       \
-                       cnt--;                                          \
+               TIMEOUT_LOOP(ci.ncand - n, qry_ctx) {                   \
                        i = canditer_next(&ci);                         \
                        if (gv[j] < goids[0] ||                         \
                            (gv[j] == goids[0] &&                       \
@@ -720,7 +719,7 @@ BATfirstn_unique_with_groups(BATiter *bi
        oid *restrict oids, *restrict goids;
        oid hseq = bi->b->hseqbase;
        const oid *restrict gv;
-       BUN i, j, cnt;
+       BUN i, j;
        struct canditer ci;
        int tpe = bi->type;
        int (*cmp)(const void *, const void *);
@@ -731,10 +730,9 @@ BATfirstn_unique_with_groups(BATiter *bi
 
        MT_thread_setalgorithm(__func__);
        canditer_init(&ci, bi->b, s);
-       cnt = ci.ncand;
 
-       if (n > cnt)
-               n = cnt;
+       if (n > ci.ncand)
+               n = ci.ncand;
 
        if (n == 0) {
                /* candidate list might refer only to values outside
@@ -790,14 +788,14 @@ BATfirstn_unique_with_groups(BATiter *bi
                oids[i] = canditer_next(&ci);
                goids[i] = gv[j++];
        }
-       cnt -= n;
+
+       QryCtx *qry_ctx = MT_thread_get_qry_ctx();
 
        if (BATtvoid(bi->b)) {
                /* nilslast doesn't make a difference (all nil, or no nil) */
                if (asc) {
                        heapify(LTvoidgrp, SWAP2);
-                       while (cnt > 0) {
-                               cnt--;
+                       TIMEOUT_LOOP(ci.ncand - n, qry_ctx) {
                                i = canditer_next(&ci);
                                if (gv[j] < goids[0]
                                    /* || (gv[j] == goids[0]
@@ -810,8 +808,7 @@ BATfirstn_unique_with_groups(BATiter *bi
                        }
                } else {
                        heapify(GTvoidgrp, SWAP2);
-                       while (cnt > 0) {
-                               cnt--;
+                       TIMEOUT_LOOP(ci.ncand - n, qry_ctx) {
                                i = canditer_next(&ci);
                                if (gv[j] < goids[0]
                                    || (gv[j] == goids[0]
@@ -851,8 +848,7 @@ BATfirstn_unique_with_groups(BATiter *bi
                                break;
                        default:
                                heapify(nLTanygrp, SWAP2);
-                               while (cnt > 0) {
-                                       cnt--;
+                               TIMEOUT_LOOP(ci.ncand - n, qry_ctx) {
                                        i = canditer_next(&ci);
                                        if (gv[j] < goids[0]
                                            || (gv[j] == goids[0]
@@ -895,8 +891,7 @@ BATfirstn_unique_with_groups(BATiter *bi
                                break;
                        default:
                                heapify(LTanygrp, SWAP2);
-                               while (cnt > 0) {
-                                       cnt--;
+                               TIMEOUT_LOOP(ci.ncand - n, qry_ctx) {
                                        i = canditer_next(&ci);
                                        if (gv[j] < goids[0] ||
                                            (gv[j] == goids[0] &&
@@ -938,8 +933,7 @@ BATfirstn_unique_with_groups(BATiter *bi
                        break;
                default:
                        heapify(GTanygrp, SWAP2);
-                       while (cnt > 0) {
-                               cnt--;
+                       TIMEOUT_LOOP(ci.ncand - n, qry_ctx) {
                                i = canditer_next(&ci);
                                if (gv[j] < goids[0] ||
                                    (gv[j] == goids[0] &&
@@ -980,8 +974,7 @@ BATfirstn_unique_with_groups(BATiter *bi
                        break;
                default:
                        heapify(nGTanygrp, SWAP2);
-                       while (cnt > 0) {
-                               cnt--;
+                       TIMEOUT_LOOP(ci.ncand - n, qry_ctx) {
                                i = canditer_next(&ci);
                                if (gv[j] < goids[0]
                                    || (gv[j] == goids[0]
@@ -998,6 +991,7 @@ BATfirstn_unique_with_groups(BATiter *bi
                        break;
                }
        }
+       TIMEOUT_CHECK(qry_ctx, GOTO_LABEL_TIMEOUT_HANDLER(bailout, qry_ctx));
        if (lastp)
                *lastp = oids[0];
        if (lastgp)
@@ -1017,6 +1011,11 @@ BATfirstn_unique_with_groups(BATiter *bi
                  ALGOBATPAR(bi->b), ALGOOPTBATPAR(s), ALGOBATPAR(g), n,
                  ALGOOPTBATPAR(bn), GDKusec() - t0);
        return bn;
+
+  bailout:
+       GDKfree(goids);
_______________________________________________
checkin-list mailing list -- checkin-list@monetdb.org
To unsubscribe send an email to checkin-list-le...@monetdb.org

Reply via email to