Changeset: 646114a1d3f7 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/646114a1d3f7
Modified Files:
        gdk/gdk_cand.c
        sql/backends/monet5/generator/generator.c
Branch: r57
Log Message:

Code deduplication: thetaselect can be implemented using select.


diffs (truncated from 986 to 300 lines):

diff --git a/gdk/gdk_cand.c b/gdk/gdk_cand.c
--- a/gdk/gdk_cand.c
+++ b/gdk/gdk_cand.c
@@ -400,6 +400,7 @@ count_mask_bits(const struct canditer *c
 }
 
 /* initialize a candidate iterator */
+ATTR_ACCESS(write_only, 1)
 static void
 canditer_init_counts(struct canditer *ci, oid hseq, BUN batcount, BAT *s)
 {
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
@@ -65,16 +65,17 @@ VLTgenerator_noop(Client cntxt, MalBlkPt
                break;
        default:
                if (tpe == TYPE_date) {
-                       /* with date, step is of SQL type "interval month or 
day",
-                        * i.e., MAL / C type "int" or "lng" */
+                       /* with date, step is of SQL type "interval
+                        * month or day", i.e., MAL / C type "int" or
+                        * "lng" */
                        int steptpe = pci->argc == 4 ? getArgType(mb, pci, 3) : 
0;
                        if (steptpe == TYPE_int)
                                VLTnoop(int);
                        else /* default interval days */
                                VLTnoop(lng);
                } else if (tpe == TYPE_timestamp) {
-                       /* with timestamp, step is of SQL type "interval 
seconds",
-                        * i.e., MAL / C type "lng" */
+                       /* with timestamp, step is of SQL type "interval
+                        * seconds", i.e., MAL / C type "lng" */
                        VLTnoop(lng);
                } else {
                        throw(MAL, "generator.noop",
@@ -265,8 +266,8 @@ VLTgenerator_table_(BAT **result, Client
                        if (VARcalccmp(&ret, &stk->stk[pci->argv[1]],
                                       &stk->stk[pci->argv[2]]) != GDK_SUCCEED)
                                throw(MAL, "generator.table",
-                                     SQLSTATE(
-                                             42000) "Illegal generator 
expression range");
+                                     SQLSTATE(42000)
+                                     "Illegal generator expression range");
                        f = *getArgReference_TYPE(stk, pci, 1, date);
                        l = *getArgReference_TYPE(stk, pci, 2, date);
                        s = *getArgReference_int(stk, pci, 3);
@@ -276,8 +277,8 @@ VLTgenerator_table_(BAT **result, Client
                            is_date_nil(f) ||
                            is_date_nil(l))
                                throw(MAL, "generator.table",
-                                     SQLSTATE(
-                                             42000) "Illegal generator range");
+                                     SQLSTATE(42000)
+                                     "Illegal generator range");
                        if (s > GDK_int_max / 28)
                                n = 0;
                        else
@@ -310,8 +311,8 @@ VLTgenerator_table_(BAT **result, Client
                        if (VARcalccmp(&ret, &stk->stk[pci->argv[1]],
                                       &stk->stk[pci->argv[2]]) != GDK_SUCCEED)
                                throw(MAL, "generator.table",
-                                     SQLSTATE(
-                                             42000) "Illegal generator 
expression range");
+                                     SQLSTATE(42000)
+                                     "Illegal generator expression range");
                        f = *getArgReference_TYPE(stk, pci, 1, date);
                        l = *getArgReference_TYPE(stk, pci, 2, date);
                        s = *getArgReference_lng(stk, pci, 3);
@@ -322,8 +323,8 @@ VLTgenerator_table_(BAT **result, Client
                            is_date_nil(f) ||
                            is_date_nil(l))
                                throw(MAL, "generator.table",
-                                     SQLSTATE(
-                                             42000) "Illegal generator range");
+                                     SQLSTATE(42000)
+                                     "Illegal generator range");
                        s /= 24 * 60 * 60 * 1000;
                        if (s > (lng) GDK_int_max)
                                s = (lng) GDK_int_max;
@@ -356,8 +357,8 @@ VLTgenerator_table_(BAT **result, Client
                        if (VARcalccmp(&ret, &stk->stk[pci->argv[1]],
                                       &stk->stk[pci->argv[2]]) != GDK_SUCCEED)
                                throw(MAL, "generator.table",
-                                     SQLSTATE(
-                                             42000) "Illegal generator 
expression range");
+                                     SQLSTATE(42000)
+                                     "Illegal generator expression range");
                        f = *getArgReference_TYPE(stk, pci, 1, timestamp);
                        l = *getArgReference_TYPE(stk, pci, 2, timestamp);
                        if (pci->argc == 3)
@@ -370,8 +371,8 @@ VLTgenerator_table_(BAT **result, Client
                            is_timestamp_nil(f) ||
                            is_timestamp_nil(l))
                                throw(MAL, "generator.table",
-                                     SQLSTATE(
-                                             42000) "Illegal generator range");
+                                     SQLSTATE(42000)
+                                     "Illegal generator range");
                        /* casting one value to lng causes the whole
                         * computation to be done as lng, reducing the
                         * risk of overflow */
@@ -428,10 +429,11 @@ VLTgenerator_table(Client cntxt, MalBlkP
 }
 
 /*
- * Selection over the generator table does not require a materialization of 
the table
- * An optimizer can replace the subselect directly into a generator specific 
one.
- * The target to look for is generator.series(A1,A2,A3)
- * We need the generator parameters, which are injected to replace the target 
column
+ * Selection over the generator table does not require a materialization
+ * of the table An optimizer can replace the subselect directly into a
+ * generator specific one.  The target to look for is
+ * generator.series(A1,A2,A3) We need the generator parameters, which
+ * are injected to replace the target column
  */
 static InstrPtr
 findGeneratorDefinition(MalBlkPtr mb, InstrPtr pci, int target)
@@ -467,7 +469,6 @@ findGeneratorDefinition(MalBlkPtr mb, In
                    (s < 0 && f < l) ||                                 \
                    is_##TPE##_nil(f) ||                                \
                    is_##TPE##_nil(l)) {                                \
-                       BBPreclaim(cand);                               \
                        throw(MAL, "generator.select",                  \
                              SQLSTATE(42000) "Illegal generator range"); \
                }                                                       \
@@ -475,8 +476,8 @@ findGeneratorDefinition(MalBlkPtr mb, In
                if ((TPE) (n * s + f) != l)                             \
                        n++;                                            \
                                                                        \
-               low = *getArgReference_##TPE(stk, pci, i);              \
-               hgh = *getArgReference_##TPE(stk, pci, i + 1);          \
+               low = *(const TPE *) lowp;                              \
+               hgh = *(const TPE *) hghp;                              \
                                                                        \
                if (!is_##TPE##_nil(low) && low == hgh)                 \
                        hi = li;                                        \
@@ -537,41 +538,18 @@ findGeneratorDefinition(MalBlkPtr mb, In
        } while (0)
 
 static str
-VLTgenerator_subselect(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
+do_generator_select(BAT **bnp, InstrPtr p, MalBlkPtr mb, MalStkPtr stk,
+                   BAT *cand, bool li, bool hi, bool anti,
+                   int tpe, const void *lowp, const void *hghp)
 {
-       bit li, hi, anti;
-       int i;
        oid o1, o2;
        BUN n = 0;
-       BAT *bn, *cand = NULL;
-       struct canditer ci = {.tpe = cand_dense};
-       InstrPtr p;
-       int tpe;
-
-       (void) cntxt;
-       p = findGeneratorDefinition(mb, pci, pci->argv[1]);
-       if (p == NULL)
-               throw(MAL, "generator.select",
-                     SQLSTATE(42000) "Could not locate definition for object");
+       BAT *bn;
+       struct canditer ci;
 
-       if (pci->argc == 8) { /* candidate list included */
-               bat candid = *getArgReference_bat(stk, pci, 2);
-               if (candid) {
-                       cand = BATdescriptor(candid);
-                       if (cand == NULL)
-                               throw(MAL, "generator.select",
-                                     SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
-                       canditer_init(&ci, NULL, cand);
-               }
-               i = 3;
-       } else
-               i = 2;
+       canditer_init(&ci, NULL, cand);
 
-       li = *getArgReference_bit(stk, pci, i + 2);
-       hi = *getArgReference_bit(stk, pci, i + 3);
-       anti = *getArgReference_bit(stk, pci, i + 4);
-
-       switch (tpe = getArgType(mb, pci, i)) {
+       switch (tpe) {
        case TYPE_bte:
                calculate_range(bte, sht);
                break;
@@ -601,7 +579,6 @@ VLTgenerator_subselect(Client cntxt, Mal
                break;
        default:
                if (p->argc == 3) {
-                       BBPreclaim(cand);
                        throw(MAL, "generator.table",
                              SQLSTATE(42000) "Date step missing");
                }
@@ -620,14 +597,13 @@ VLTgenerator_subselect(Client cntxt, Mal
                            is_date_nil(tsl) ||
                            (tss > 0 && tsf > tsl) ||
                            (tss < 0 && tsf < tsl)) {
-                               BBPreclaim(cand);
                                throw(MAL, "generator.select",
                                      SQLSTATE(
                                              42000) "Illegal generator range");
                        }
 
-                       tlow = *getArgReference_TYPE(stk, pci, i, date);
-                       thgh = *getArgReference_TYPE(stk, pci, i + 1, date);
+                       tlow = *(const date *) lowp;
+                       thgh = *(const date *) hghp;
 
                        if (!is_date_nil(tlow) && tlow == thgh)
                                hi = li;
@@ -650,28 +626,27 @@ VLTgenerator_subselect(Client cntxt, Mal
                                                      SQLSTATE(HY013)
                                                              MAL_MALLOC_FAIL);
 
-                                       *getArgReference_bat(stk, pci, 0) =
-                                               bn->batCacheid;
-                                       BBPkeepref(bn);
-                                       return MAL_SUCCEED;
+                                       *bnp = bn;
                                }
                        }
 
                        o2 = (BUN) (date_diff(tsl, tsf) / (tss * 28));
                        bn = COLnew(0, TYPE_oid, o2 + 1, TRANSIENT);
                        if (bn == NULL) {
-                               BBPreclaim(cand);
                                throw(MAL, "generator.select",
                                      SQLSTATE(HY013) MAL_MALLOC_FAIL);
                        }
 
-                       // simply enumerate the sequence and filter it by 
predicate and candidate list
+                       // simply enumerate the sequence and filter it
+                       // by predicate and candidate list
                        ol = (oid *) Tloc(bn, 0);
                        for (o1 = 0; o1 <= o2; o1++) {
                                if (((is_date_nil(tlow) || tsf >= tlow) &&
                                     (is_date_nil(thgh) || tsf < thgh)) !=
                                    anti) {
-                                       /* could be improved when no candidate 
list is available into a void/void BAT */
+                                       /* could be improved when no
+                                        * candidate list is available
+                                        * into a void/void BAT */
                                        if (cand == NULL ||
                                            canditer_contains(&ci, o1)) {
                                                *ol++ = o1;
@@ -682,15 +657,13 @@ VLTgenerator_subselect(Client cntxt, Mal
                                if (is_date_nil(tsf))
                                        break;
                        }
-                       BBPreclaim(cand);
                        BATsetcount(bn, n);
                        bn->tsorted = true;
                        bn->trevsorted = BATcount(bn) <= 1;
                        bn->tkey = true;
                        bn->tnil = false;
                        bn->tnonil = true;
-                       *getArgReference_bat(stk, pci, 0) = bn->batCacheid;
-                       BBPkeepref(bn);
+                       *bnp = bn;
                        return MAL_SUCCEED;
                } else if (tpe == TYPE_date) { /* days */
                        date tsf, tsl;
@@ -706,17 +679,17 @@ VLTgenerator_subselect(Client cntxt, Mal
                            is_date_nil(tsl) ||
                            (tss > 0 && tsf > tsl) ||
                            (tss < 0 && tsf < tsl) ||
-                           tss % (24 * 60 * 60 * 1000) != 0 /* only whole days 
*/
+                           /* only whole days: */
+                           tss % (24 * 60 * 60 * 1000) != 0
                        ) {
-                               BBPreclaim(cand);
                                throw(MAL, "generator.select",
-                                     SQLSTATE(
-                                             42000) "Illegal generator range");
+                                     SQLSTATE(42000)
+                                     "Illegal generator range");
                        }
                        tss /= 24 * 60 * 60 * 1000;
 
-                       tlow = *getArgReference_TYPE(stk, pci, i, date);
-                       thgh = *getArgReference_TYPE(stk, pci, i + 1, date);
+                       tlow = *(const date *) lowp;
+                       thgh = *(const date *) hghp;
 
                        if (!is_date_nil(tlow) && tlow == thgh)
                                hi = li;
@@ -738,9 +711,7 @@ VLTgenerator_subselect(Client cntxt, Mal
                                                throw(MAL, "generator.select",
                                                      SQLSTATE(HY013)
                                                              MAL_MALLOC_FAIL);
-                                       *getArgReference_bat(stk, pci, 0) =
-                                               bn->batCacheid;
-                                       BBPkeepref(bn);
+                                       *bnp = bn;
                                        return MAL_SUCCEED;
                                }
                        }
@@ -748,18 +719,20 @@ VLTgenerator_subselect(Client cntxt, Mal
                        o2 = (BUN) (date_diff(tsl, tsf) / tss) + 1;
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to