Changeset: 8d13ef8c7dbc for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=8d13ef8c7dbc
Modified Files:
        monetdb5/modules/mal/mal_weld.c
        monetdb5/modules/mal/mal_weld.h
        monetdb5/modules/mal/mal_weld.mal
        monetdb5/modules/mal/mal_weld.mal.sh
        monetdb5/optimizer/opt_prelude.c
        monetdb5/optimizer/opt_prelude.h
        monetdb5/optimizer/opt_weld.c
Branch: mal-weld
Log Message:

weld impl for str thetaselect and bat.mergecand


diffs (285 lines):

diff --git a/monetdb5/modules/mal/mal_weld.c b/monetdb5/modules/mal/mal_weld.c
--- a/monetdb5/modules/mal/mal_weld.c
+++ b/monetdb5/modules/mal/mal_weld.c
@@ -120,6 +120,20 @@ static str getWeldCandList(int sid, bat 
        return candList;
 }
 
+/* Candidate lists can be dense so we have to replace them with a rangeiter */
+static str getWeldCandListFull(int sid, bat s) {
+       static char candList[STR_SIZE_INC];
+       BAT *list = is_bat_nil(s) ? NULL : BATdescriptor(s);
+       if (list == NULL || !BATtdense(list)) {
+               sprintf(candList, "v%d", sid);
+       } else {
+               sprintf(candList,
+                               "result(for(rangeiter(%ldL, %ldL, 1L), 
appender[?], |b, i, x| merge(b, x)))",
+                               list->tseqbase, list->tseqbase + 
list->batCount);
+       }
+       return candList;
+}
+
 static void dumpProgram(weldState *wstate, MalBlkPtr mb) {
        FILE *f = fopen(tmpnam(NULL), "w");
        int i;
@@ -558,20 +572,38 @@ WeldAlgebraThetaselect1(Client cntxt, Ma
        int ret = getArg(pci, 0);                                               
           /* bat[:oid] */
        int bid = getArg(pci, 1);                                               
           /* bat[:any_1] */
        int val = getArg(pci, 2);                                               
           /* any_1 */
+       int valType = getArgType(mb, pci, 2);
        str op = *getArgReference_str(stk, pci, 3);                        /* 
has value */
        weldState *wstate = *getArgReference_ptr(stk, pci, 4); /* has value */
        char weldStmt[STR_SIZE_INC];
-       sprintf(weldStmt,
-       "let v%d = result("
-       "       for (v%d, appender[i64], |b, i, x|"
-       "               if (x %s v%d,"
-       "                       merge(b, i + v%dhseqbase),"
-       "                       b"
-       "               )"
-       "       )"
-       ");"
-       "let v%dhseqbase = 0L;",
-       ret, bid, op, val, bid, ret);
+       if (valType == TYPE_str) {
+               if (strcmp(op, "==") != 0) {
+                       throw(MAL, "weld.algebrathetaselect", PROGRAM_NYI": str 
thetaselect only supports ==");
+               }
+               sprintf(weldStmt,
+               "let v%d = result("
+               "       for (v%d, appender[i64], |b, i, x|"
+               "               if (strslice(v%dstr, i64(x) + v%dstroffset) %s 
v%d,"
+               "                       merge(b, i + v%dhseqbase),"
+               "                       b"
+               "               )"
+               "       )"
+               ");"
+               "let v%dhseqbase = 0L;",
+               ret, bid, bid, bid, op, val, bid, ret);
+       } else {
+               sprintf(weldStmt,
+               "let v%d = result("
+               "       for (v%d, appender[i64], |b, i, x|"
+               "               if (x %s v%d,"
+               "                       merge(b, i + v%dhseqbase),"
+               "                       b"
+               "               )"
+               "       )"
+               ");"
+               "let v%dhseqbase = 0L;",
+               ret, bid, op, val, bid, ret);
+       }
        appendWeldStmt(wstate, weldStmt);
        return MAL_SUCCEED;
 }
@@ -587,20 +619,39 @@ WeldAlgebraThetaselect2(Client cntxt, Ma
        int sid = getArg(pci, 2);                                               
           /* bat[:oid] */
        bat s = *getArgReference_bat(stk, pci, 2);                         /* 
might have value */
        int val = getArg(pci, 3);                                               
           /* any_1 */
+       int valType = getArgType(mb, pci, 3);
        str op = *getArgReference_str(stk, pci, 4);                        /* 
has value */
        weldState *wstate = *getArgReference_ptr(stk, pci, 5); /* has value */
        char weldStmt[STR_SIZE_INC];
-       sprintf(weldStmt,
-       "let v%d = result("
-       "       for (%s, appender[i64], |b: appender[i64], i: i64, oid: i64|"
-       "               if (lookup(v%d, oid - v%dhseqbase) %s v%d,"
-       "                       merge(b, oid),"
-       "                       b"
-       "               )"
-       "       )"
-       ");"
-       "let v%dhseqbase = 0L;",
-       ret, getWeldCandList(sid, s), bid, bid, op, val, ret);
+       if (valType == TYPE_str) {
+               if (strcmp(op, "==") != 0) {
+                       throw(MAL, "weld.algebrathetaselect", PROGRAM_NYI": str 
thetaselect only supports ==");
+               }
+               sprintf(weldStmt,
+               "let v%d = result("
+               "       for (%s, appender[i64], |b, i, oid|"
+               "               let offset = lookup(v%d, oid - v%dhseqbase);"
+               "               if (strslice(v%dstr, i64(offset) + 
v%dstroffset) %s v%d,"
+               "                       merge(b, oid),"
+               "                       b"
+               "               )"
+               "       )"
+               ");"
+               "let v%dhseqbase = 0L;",
+               ret, getWeldCandList(sid, s), bid, bid, bid, bid, op, val, ret);
+       } else {
+               sprintf(weldStmt,
+               "let v%d = result("
+               "       for (%s, appender[i64], |b: appender[i64], i: i64, oid: 
i64|"
+               "               if (lookup(v%d, oid - v%dhseqbase) %s v%d,"
+               "                       merge(b, oid),"
+               "                       b"
+               "               )"
+               "       )"
+               ");"
+               "let v%dhseqbase = 0L;",
+               ret, getWeldCandList(sid, s), bid, bid, op, val, ret);
+       }
        appendWeldStmt(wstate, weldStmt);
        return MAL_SUCCEED;
 }
@@ -1001,6 +1052,53 @@ WeldBatMtimeYear(Client cntxt, MalBlkPtr
 }
 
 str
+WeldBatMergeCand(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
+{
+       (void) cntxt;
+       (void) mb;
+       int ret = getArg(pci, 0);                                               
                                   /* bat[:oid] */
+       int left = getArg(pci, 1);                                              
                                   /* bat[:oid] */
+       bat leftBat = *getArgReference_bat(stk, pci, 1);                        
           /* might have value */
+       int right = getArg(pci, 2);                                             
                                   /* bat[:oid] */
+       bat rightBat = *getArgReference_bat(stk, pci, 2);                       
           /* might have value */
+       weldState *wstate = *getArgReference_ptr(stk, pci, pci->argc - 1); /* 
has value */
+
+       char *leftWeld = strdup(getWeldCandListFull(left, leftBat));
+       char *rightWeld = strdup(getWeldCandListFull(right, rightBat));
+       char weldStmt[STR_SIZE_INC];
+       sprintf(weldStmt,
+       "let left = %s;"
+       "let right = %s;"
+       "let iter_res = if(len(left) > 0L && len(right) > 0L,"
+       "       iterate({0L, 0L, appender[i64]}, |x|"
+       "               let leftIdx = x.$0;"
+       "               let rightIdx = x.$1;"
+       "               let b = x.$2;"
+       "               let leftVal = lookup(left, leftIdx);"
+       "               let rightVal = lookup(right, rightIdx);"
+       "               let output = "
+       "               if(leftVal < rightVal,"
+       "                       {leftIdx + 1L, rightIdx, merge(b, leftVal)},"
+       "                       if (leftVal > rightVal,"
+       "                               {leftIdx, rightIdx + 1L, merge(b, 
rightVal)},"
+       "                               {leftIdx + 1L, rightIdx + 1L, merge(b, 
leftVal)}"
+       "                       )"
+       "               );"     
+       "               {output, output.$0 < len(left) && output.$1 < 
len(right)}),"
+       "       {0L, 0L, appender[i64]});"
+       "let res = iter_res.$2;"
+       "let res = for(rangeiter(iter_res.$0, len(left), 1L), res, |b, i, x| 
merge(b, lookup(left, x)));"
+       "let res = for(rangeiter(iter_res.$1, len(right), 1L), res, |b, i, x| 
merge(b, lookup(right, x)));"
+       "let v%d = result(res);"
+       "let v%dhseqbase = 0L;",
+       leftWeld, rightWeld, ret, ret);
+       appendWeldStmt(wstate, weldStmt);
+       free(leftWeld);
+       free(rightWeld);
+       return MAL_SUCCEED;
+}
+
+str
 WeldLanguagePass(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
 {
        (void) cntxt;
diff --git a/monetdb5/modules/mal/mal_weld.h b/monetdb5/modules/mal/mal_weld.h
--- a/monetdb5/modules/mal/mal_weld.h
+++ b/monetdb5/modules/mal/mal_weld.h
@@ -43,6 +43,7 @@ mal_export str WeldAggrSubProd(Client cn
 mal_export str WeldAggrSubMin(Client cntxt, MalBlkPtr mb, MalStkPtr stk, 
InstrPtr pci);
 mal_export str WeldAggrSubMax(Client cntxt, MalBlkPtr mb, MalStkPtr stk, 
InstrPtr pci);
 mal_export str WeldBatMtimeYear(Client cntxt, MalBlkPtr mb, MalStkPtr stk, 
InstrPtr pci);
+mal_export str WeldBatMergeCand(Client cntxt, MalBlkPtr mb, MalStkPtr stk, 
InstrPtr pci);
 mal_export str WeldLanguagePass(Client cntxt, MalBlkPtr mb, MalStkPtr stk, 
InstrPtr pci);
 
 #endif
diff --git a/monetdb5/modules/mal/mal_weld.mal 
b/monetdb5/modules/mal/mal_weld.mal
--- a/monetdb5/modules/mal/mal_weld.mal
+++ b/monetdb5/modules/mal/mal_weld.mal
@@ -89,6 +89,10 @@ pattern batmtimeyear(d:bat[:date], wstat
 address WeldBatMtimeYear
 comment "batmtime.year";
 
+pattern batmergecand(a:bat[:oid], b:bat[:oid], wstate:ptr):bat[:oid]
+address WeldBatMergeCand
+comment "bat.mergecand";
+
 pattern aggrsum(b:bat[:bte], wstate:ptr):bte
 address WeldAggrSum
 comment "aggr.sum";
diff --git a/monetdb5/modules/mal/mal_weld.mal.sh 
b/monetdb5/modules/mal/mal_weld.mal.sh
--- a/monetdb5/modules/mal/mal_weld.mal.sh
+++ b/monetdb5/modules/mal/mal_weld.mal.sh
@@ -99,6 +99,10 @@ pattern batmtimeyear(d:bat[:date], wstat
 address WeldBatMtimeYear
 comment "batmtime.year";
 
+pattern batmergecand(a:bat[:oid], b:bat[:oid], wstate:ptr):bat[:oid]
+address WeldBatMergeCand
+comment "bat.mergecand";
+
 EOF
 
 for tp in ${numeric[@]}; do
diff --git a/monetdb5/optimizer/opt_prelude.c b/monetdb5/optimizer/opt_prelude.c
--- a/monetdb5/optimizer/opt_prelude.c
+++ b/monetdb5/optimizer/opt_prelude.c
@@ -324,6 +324,7 @@ str weldBatcalcLeRef;
 str weldBatcalcGtRef;
 str weldBatcalcGeRef;
 str weldBatcalcNeRef;
+str weldBatMergeCandRef;
 str weldBatMtimeYearRef;
 str weldGetResultRef;
 str weldGroupRef;
@@ -643,6 +644,7 @@ void optimizerInit(void)
        weldBatcalcGtRef = putName("batcalcgt");
        weldBatcalcGeRef = putName("batcalcge");
        weldBatcalcNeRef = putName("batcalcne");
+       weldBatMergeCandRef = putName("batmergecand");
        weldBatMtimeYearRef = putName("batmtimeyear");
        weldGetResultRef = putName("getresult");
        weldGroupRef = putName("groupgroup");
diff --git a/monetdb5/optimizer/opt_prelude.h b/monetdb5/optimizer/opt_prelude.h
--- a/monetdb5/optimizer/opt_prelude.h
+++ b/monetdb5/optimizer/opt_prelude.h
@@ -332,6 +332,7 @@ mal_export  str weldBatcalcLeRef;
 mal_export  str weldBatcalcGtRef;
 mal_export  str weldBatcalcGeRef;
 mal_export  str weldBatcalcNeRef;
+mal_export  str weldBatMergeCandRef;
 mal_export  str weldBatMtimeYearRef;
 mal_export  str weldGetResultRef;
 mal_export  str weldGroupRef;
diff --git a/monetdb5/optimizer/opt_weld.c b/monetdb5/optimizer/opt_weld.c
--- a/monetdb5/optimizer/opt_weld.c
+++ b/monetdb5/optimizer/opt_weld.c
@@ -18,7 +18,7 @@
 #include "mal_instruction.h"
 #include "opt_weld.h"
 
-#define NUM_WELD_INSTR 26
+#define NUM_WELD_INSTR 27
 #define UNMARKED 0
 #define TEMP_MARK 1
 #define PERM_MARK 2
@@ -64,6 +64,7 @@ static void initWeldInstrs(void) {
        addWeldInstr(batcalcRef, gtRef, weldBatcalcGtRef);                      
                 /* batcalc.> */
        addWeldInstr(batcalcRef, geRef, weldBatcalcGeRef);                      
                 /* batcalc.>= */
        addWeldInstr(batcalcRef, neRef, weldBatcalcNeRef);                      
                 /* batcalc.!= */
+       addWeldInstr(batRef, mergecandRef, weldBatMergeCandRef);                
         /* bat.mergecand */
        addWeldInstr(batmtimeRef, yearRef, weldBatMtimeYearRef);                
         /* batmtime.year */
        addWeldInstr(languageRef, passRef, weldLanguagePassRef);                
         /* language.pass */
        addWeldInstr(groupRef, groupRef, weldGroupRef);                         
                 /* group.group*/
@@ -74,15 +75,8 @@ static void initWeldInstrs(void) {
 }
 
 static str getWeldRef(MalBlkPtr mb, InstrPtr instr) {
+       (void) mb;
        int i;
-       for (i = instr->retc; i < instr->argc; i++) {
-               int argType = getArgType(mb, instr, i);
-               if (isaBatType(argType) && getBatType(argType) == TYPE_str &&
-                       getModuleId(instr) != groupRef && getFunctionId(instr) 
!= projectionRef) {
-                       /* TODO For now only allow string columns for group.* 
and algebra.projection*/
-                       return NULL;
-               }
-       }
        for (i = 0; i < NUM_WELD_INSTR; i++) {
                if (getModuleId(instr) == weldInstrs[i][0] && 
getFunctionId(instr) == weldInstrs[i][1]) {
                        return weldInstrs[i][2];
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to