Changeset: 8b2fe42d9242 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/8b2fe42d9242
Modified Files:
        clients/Tests/exports.stable.out
        gdk/gdk.h
        sql/server/rel_select.c
        sql/server/rel_updates.c
        sql/test/prepare/Tests/sqlancer_prepare.stable.err
        sql/test/prepare/Tests/sqlancer_prepare.stable.err.int128
Branch: resource_management
Log Message:

merge with default


diffs (truncated from 10045 to 300 lines):

diff --git a/clients/Tests/MAL-signatures-hge.test 
b/clients/Tests/MAL-signatures-hge.test
--- a/clients/Tests/MAL-signatures-hge.test
+++ b/clients/Tests/MAL-signatures-hge.test
@@ -3444,6 +3444,11 @@ command algebra.groupby(X_0:bat[:oid], X
 ALGgroupby;
 Produces a new BAT with groups identified by the head column. The result 
contains tail times the head value, ie the tail contains the result group sizes.
 algebra
+groupedfirstn
+pattern algebra.groupedfirstn(X_0:lng, X_1:bat[:oid], X_2:bat[:oid], 
X_3:any...):bat[:oid]
+ALGgroupedfirstn;
+Grouped firstn
+algebra
 intersect
 command algebra.intersect(X_0:bat[:any_1], X_1:bat[:any_1], X_2:bat[:oid], 
X_3:bat[:oid], X_4:bit, X_5:bit, X_6:lng):bat[:oid]
 ALGintersect;
diff --git a/clients/Tests/MAL-signatures.test 
b/clients/Tests/MAL-signatures.test
--- a/clients/Tests/MAL-signatures.test
+++ b/clients/Tests/MAL-signatures.test
@@ -2879,6 +2879,11 @@ command algebra.groupby(X_0:bat[:oid], X
 ALGgroupby;
 Produces a new BAT with groups identified by the head column. The result 
contains tail times the head value, ie the tail contains the result group sizes.
 algebra
+groupedfirstn
+pattern algebra.groupedfirstn(X_0:lng, X_1:bat[:oid], X_2:bat[:oid], 
X_3:any...):bat[:oid]
+ALGgroupedfirstn;
+Grouped firstn
+algebra
 intersect
 command algebra.intersect(X_0:bat[:any_1], X_1:bat[:any_1], X_2:bat[:oid], 
X_3:bat[:oid], X_4:bit, X_5:bit, X_6:lng):bat[:oid]
 ALGintersect;
diff --git a/clients/Tests/exports.stable.out b/clients/Tests/exports.stable.out
--- a/clients/Tests/exports.stable.out
+++ b/clients/Tests/exports.stable.out
@@ -135,6 +135,7 @@ BAT *BATgroupcorrelation(BAT *b1, BAT *b
 BAT *BATgroupcount(BAT *b, BAT *g, BAT *e, BAT *s, int tp, bool skip_nils);
 BAT *BATgroupcovariance_population(BAT *b1, BAT *b2, BAT *g, BAT *e, BAT *s, 
int tp, bool skip_nils);
 BAT *BATgroupcovariance_sample(BAT *b1, BAT *b2, BAT *g, BAT *e, BAT *s, int 
tp, bool skip_nils);
+BAT *BATgroupedfirstn(BUN n, BAT *s, BAT *g, int nbats, BAT **bats, bool *asc, 
bool *nilslast) __attribute__((__warn_unused_result__));
 BAT *BATgroupmax(BAT *b, BAT *g, BAT *e, BAT *s, int tp, bool skip_nils);
 BAT *BATgroupmedian(BAT *b, BAT *g, BAT *e, BAT *s, int tp, bool skip_nils);
 BAT *BATgroupmedian_avg(BAT *b, BAT *g, BAT *e, BAT *s, int tp, bool 
skip_nils);
diff --git a/gdk/gdk.h b/gdk/gdk.h
--- a/gdk/gdk.h
+++ b/gdk/gdk.h
@@ -2470,6 +2470,8 @@ gdk_export gdk_return BATfirstn(BAT **to
        __attribute__((__access__(write_only, 1)))
        __attribute__((__access__(write_only, 2)))
        __attribute__((__warn_unused_result__));
+gdk_export BAT *BATgroupedfirstn(BUN n, BAT *s, BAT *g, int nbats, BAT **bats, 
bool *asc, bool *nilslast)
+       __attribute__((__warn_unused_result__));
 
 #include "gdk_calc.h"
 
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;
_______________________________________________
checkin-list mailing list -- checkin-list@monetdb.org
To unsubscribe send an email to checkin-list-le...@monetdb.org

Reply via email to