Line data Source code
1 : /*
2 : * SPDX-License-Identifier: MPL-2.0
3 : *
4 : * This Source Code Form is subject to the terms of the Mozilla Public
5 : * License, v. 2.0. If a copy of the MPL was not distributed with this
6 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 : *
8 : * Copyright 2024 MonetDB Foundation;
9 : * Copyright August 2008 - 2023 MonetDB B.V.;
10 : * Copyright 1997 - July 2008 CWI.
11 : */
12 :
13 : /*
14 : * M.L.Kersten
15 : * BAT Algebra Bitvector Extensions
16 : * The current primitives are focused on the use of vectors as Candidate lists
17 : */
18 : #include "monetdb_config.h"
19 : #include "mal_client.h"
20 : #include "mal_interpreter.h"
21 : #include "bat5.h"
22 : #include "gdk_time.h"
23 : #include "mal_exception.h"
24 :
25 : /*
26 : * BAT bitvector enhancements
27 : * The code to enhance the kernel.
28 : */
29 :
30 : static str
31 0 : MSKmask(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
32 : {
33 0 : BAT *b, *dst;
34 0 : bat *bid;
35 0 : int *ret;
36 :
37 0 : (void) mb;
38 0 : (void) cntxt;
39 :
40 0 : ret = getArgReference_bat(stk, pci, 0);
41 0 : bid = getArgReference_bat(stk, pci, 1);
42 0 : if ((b = BATdescriptor(*bid)) == NULL)
43 0 : throw(SQL, "bat.mask", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
44 0 : if (!b->tkey || !b->tsorted) {
45 0 : BBPunfix(b->batCacheid);
46 0 : throw(SQL, "bat.mask",
47 : SQLSTATE(HY002) "Input should be unique and in ascending order");
48 : }
49 0 : if (BATcount(b) == 0) {
50 0 : dst = COLnew(0, TYPE_msk, 0, TRANSIENT);
51 : } else {
52 0 : oid fst;
53 0 : BUN cap;
54 0 : BUN max = 0;
55 0 : if (b->tsorted) {
56 0 : fst = BUNtoid(b, 0);
57 0 : dst = COLnew(fst, TYPE_msk, BUNtoid(b, BATcount(b) - 1) + 1 - fst,
58 : TRANSIENT);
59 : } else {
60 0 : fst = 0;
61 0 : dst = COLnew(0, TYPE_msk, BATcount(b), TRANSIENT);
62 : }
63 0 : cap = BATcapacity(b);
64 0 : if (dst) {
65 0 : memset(Tloc(dst, 0), 0, dst->theap->size);
66 0 : for (BUN p = 0; p < BATcount(b); p++) {
67 0 : oid o = BUNtoid(b, p);
68 0 : if (is_oid_nil(o)) {
69 0 : BBPunfix(b->batCacheid);
70 0 : BBPreclaim(dst);
71 0 : throw(MAL, "mask.mask", "no NULL allowed");
72 : }
73 0 : o -= fst;
74 0 : if (o >= cap) {
75 0 : if (BATextend(dst, o + 1) != GDK_SUCCEED) {
76 0 : BBPunfix(b->batCacheid);
77 0 : BBPreclaim(dst);
78 0 : throw(MAL, "mask.mask", GDK_EXCEPTION);
79 : }
80 0 : cap = BATcapacity(dst);
81 : }
82 0 : mskSetVal(dst, o, true);
83 0 : if (o > max)
84 : max = o;
85 : }
86 0 : BATsetcount(dst, max + 1);
87 0 : dst->tsorted = dst->trevsorted = false;
88 0 : dst->tkey = false;
89 0 : dst->tnil = false;
90 0 : dst->tnonil = true;
91 : }
92 : }
93 0 : BBPunfix(b->batCacheid);
94 0 : if (dst == NULL)
95 0 : throw(MAL, "mask.mask", GDK_EXCEPTION);
96 :
97 0 : *ret = dst->batCacheid;
98 0 : BBPkeepref(dst);
99 0 : return MAL_SUCCEED;
100 : }
101 :
102 : static str
103 0 : MSKumask(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
104 : {
105 0 : BAT *b, *dst;
106 0 : bat *bid;
107 0 : int *ret;
108 :
109 0 : (void) mb;
110 0 : (void) cntxt;
111 :
112 0 : ret = getArgReference_bat(stk, pci, 0);
113 0 : bid = getArgReference_bat(stk, pci, 1);
114 0 : if ((b = BATdescriptor(*bid)) == NULL)
115 0 : throw(SQL, "bat.umask", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
116 0 : if (b->ttype != TYPE_msk && !mask_cand(b)) {
117 0 : BBPunfix(b->batCacheid);
118 0 : throw(MAL, "mask.umask", SQLSTATE(42000) "msk type input expected");
119 : }
120 0 : dst = BATunmask(b);
121 0 : BBPunfix(b->batCacheid);
122 0 : if (dst == NULL)
123 0 : throw(MAL, "mask.umask", GDK_EXCEPTION);
124 0 : *ret = dst->batCacheid;
125 0 : BBPkeepref(dst);
126 0 : return MAL_SUCCEED;
127 : }
128 :
129 :
130 : #include "mel.h"
131 : mel_func batMask_init_funcs[] = {
132 : pattern("mask", "mask", MSKmask, false, "", args(1,2, batarg("r", msk), batarg("b",oid))),
133 : pattern("mask", "umask", MSKumask, false, "", args(1,2, batarg("r", oid), batarg("b",msk))),
134 : { .imp=NULL }
135 : };
136 : #include "mal_import.h"
137 : #ifdef _MSC_VER
138 : #undef read
139 : #pragma section(".CRT$XCU",read)
140 : #endif
141 329 : LIB_STARTUP_FUNC(init_batMask_mal)
142 329 : { mal_module("batMask", NULL, batMask_init_funcs); }
|