Changeset: 2540349f9d3f for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=2540349f9d3f
Added Files:
        monetdb5/optimizer/Tests/BCexample.sql
        monetdb5/optimizer/opt_batcalc.c
        monetdb5/optimizer/opt_batcalc.h
Modified Files:
        monetdb5/optimizer/Makefile.ag
        monetdb5/optimizer/opt_pipes.c
        monetdb5/optimizer/opt_support.c
        monetdb5/optimizer/opt_wrapper.c
        monetdb5/optimizer/optimizer.mal
Branch: batcalc-candidates
Log Message:

Add the framework for the batcalc optimizer
A peephole optimizer infrastructure to use candidate list
See opt_batcalc.c for example

Code is currently disabled in opt_batcalc.c, because
additional changes are needed in the batcal interface.


diffs (263 lines):

diff --git a/monetdb5/optimizer/Makefile.ag b/monetdb5/optimizer/Makefile.ag
--- a/monetdb5/optimizer/Makefile.ag
+++ b/monetdb5/optimizer/Makefile.ag
@@ -19,6 +19,7 @@ lib_optimizer = {
        SOURCES = \
                optimizer.c optimizer.h optimizer_private.h \
                opt_aliases.c opt_aliases.h \
+               opt_batcalc.c opt_batcalc.h \
                opt_coercion.c opt_coercion.h \
                opt_commonTerms.c opt_commonTerms.h \
                opt_candidates.c opt_candidates.h \
diff --git a/monetdb5/optimizer/Tests/BCexample.sql 
b/monetdb5/optimizer/Tests/BCexample.sql
new file mode 100644
--- /dev/null
+++ b/monetdb5/optimizer/Tests/BCexample.sql
@@ -0,0 +1,6 @@
+create table t(i int);
+insert into t values(1),(2),(3),(4);
+
+explain select 100+i from t where i<2;
+
+drop table t;
diff --git a/monetdb5/optimizer/opt_batcalc.c b/monetdb5/optimizer/opt_batcalc.c
new file mode 100644
--- /dev/null
+++ b/monetdb5/optimizer/opt_batcalc.c
@@ -0,0 +1,118 @@
+/*
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0.  If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * Copyright 1997 - July 2008 CWI, August 2008 - 2017 MonetDB B.V.
+ */
+
+/*
+ * Push the candidate list arithmetic operations
+ */
+
+#include "monetdb_config.h"
+#include "mal_instruction.h"
+#include "opt_batcalc.h"
+
+str
+OPTbatcalcImplementation(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr 
pci)
+{
+       int i, j, limit, slimit, actions=0;
+       int *vars = 0;
+       InstrPtr p,q,*old = 0;
+       lng usec = GDKusec();
+       str msg= MAL_SUCCEED;
+       char buf[256];
+
+       (void) stk;
+       (void) pci;
+       (void) cntxt;
+
+       // For now it is disabled, awaiting modified batcalc signatures
+       return MAL_SUCCEED;
+
+       limit = mb->stop;
+        slimit = mb->ssize;
+       vars= (int*) GDKzalloc(sizeof(int)* mb->vtop);
+       if (vars == NULL)
+               throw(MAL,"optimizer.batcalc", SQLSTATE(HY001) MAL_MALLOC_FAIL);
+
+       old= mb->stmt;
+        if (newMalBlkStmt(mb, mb->ssize) < 0) {
+               GDKfree(vars);
+                throw(MAL,"optimizer.batcalc", SQLSTATE(HY001) 
MAL_MALLOC_FAIL);
+        }
+
+       /* collect where variables are assigned last */
+       for ( i = 1; i < limit; i++){
+               p= old[i];
+               for( j = 0; j< p->retc; j++)
+                       vars[getArg(p,j)]= i;
+       }
+
+       /* replace the combinations:
+        * X_840 := algebra.projection(C_701,X_666);
+        * X_841 := batcalc.lng(X_840);
+        *
+        * into
+        * X_841 := batcalc.lng(X_666,C_701);
+        *
+        * and combination:
+        * X_850 := algebra.projection(C_701,X_667);
+        * X_875 := algebra.projection(C_701,X_678);
+        * X_883 := batcalc.-(100:lng,X_875);
+        * X_942 := batcalc.*(X_850,X_883);
+        *
+        * into
+        * X_883 := batcalc.-(100:lng, X_678, nil:bat, C_701)
+        * X_999 := batcalc.*(X_667, X_883, C_701, nil:bat)
+        *
+        */
+       for( i=0; i< limit; i++){
+               p = old[i];
+               if( getModuleId(p) == batcalcRef ){
+                       actions++;
+                       if( isVarConstant(mb, getArg(p,1)) ){
+                               p = pushNil(mb,p,TYPE_bat);
+                       } else {
+                               q= old[vars[getArg(p,1)]];
+                               if( getModuleId(q) == algebraRef && 
getFunctionId(q) == projectionRef && isVarCList(mb, getArg(q,1)) ){
+                                       p = pushArgument(mb,p, getArg(q,1));
+                                       getArg(p,1) = getArg(q,2);
+                               } else
+                                       p = pushNil(mb,p,TYPE_bat);
+                       }
+                       if ( p->argc >3){
+                               if( isVarConstant(mb, getArg(p,2)) ){
+                                       p = pushNil(mb,p,TYPE_bat);
+                               } else {
+                                       q= old[vars[getArg(p,2)]];
+                                       if( getModuleId(q) == algebraRef && 
getFunctionId(q) == projectionRef && isVarCList(mb, getArg(q,1)) ){
+                                               p = pushArgument(mb,p, 
getArg(q,1));
+                                               getArg(p,2) = getArg(q,2);
+                                       } else
+                                               p = pushNil(mb,p,TYPE_bat);
+                               }
+                       }
+               }
+               pushInstruction(mb,p);
+       }
+       for(; i < slimit; i++)
+               if( old[i])
+                       freeInstruction(old[i]);
+
+       /* Defense line against incorrect plans */
+       /* Plan is unaffected */
+       chkTypes(cntxt->usermodule, mb, FALSE);
+       chkFlow(mb);
+       chkDeclarations(mb);
+       /* keep all actions taken as a post block comment * and update stats */
+       usec= GDKusec() - usec;
+       snprintf(buf,256,"%-20s actions=%2d time=" LLFMT " 
usec","batcalc",actions,usec);
+       newComment(mb,buf);
+       if( actions >= 0)
+               addtoMalBlkHistory(mb);
+       GDKfree(vars);
+       GDKfree(old);
+       return msg;
+}
diff --git a/monetdb5/optimizer/opt_batcalc.h b/monetdb5/optimizer/opt_batcalc.h
new file mode 100644
--- /dev/null
+++ b/monetdb5/optimizer/opt_batcalc.h
@@ -0,0 +1,17 @@
+/*
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0.  If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * Copyright 1997 - July 2008 CWI, August 2008 - 2017 MonetDB B.V.
+ */
+
+#ifndef _OPT_BATCALC_
+#define _OPT_BATCALC_
+#include "opt_prelude.h"
+#include "opt_support.h"
+#include "mal_exception.h"
+
+mal_export str OPTbatcalcImplementation(Client cntxt, MalBlkPtr mb, MalStkPtr 
stk, InstrPtr p);
+
+#endif
diff --git a/monetdb5/optimizer/opt_pipes.c b/monetdb5/optimizer/opt_pipes.c
--- a/monetdb5/optimizer/opt_pipes.c
+++ b/monetdb5/optimizer/opt_pipes.c
@@ -89,6 +89,8 @@ static struct PIPELINES {
         "optimizer.generator();"
         "optimizer.profiler();"
         "optimizer.candidates();"
+        "optimizer.batcalc();"
+        "optimizer.deadcode();"  // remove what is left after batcalc
 //      "optimizer.jit();" awaiting the new batcalc api
 //      "optimizer.oltp();"awaiting the autocommit front-end changes
         "optimizer.wlc();"
@@ -124,6 +126,8 @@ static struct PIPELINES {
         "optimizer.volcano();"
         "optimizer.profiler();"
         "optimizer.candidates();"
+        "optimizer.batcalc();"
+        "optimizer.deadcode();"  // remove what is left after batcalc
 //      "optimizer.jit();" awaiting the new batcalc api
 //      "optimizer.oltp();"awaiting the autocommit front-end changes
         "optimizer.wlc();"
@@ -164,6 +168,8 @@ static struct PIPELINES {
         "optimizer.generator();"
         "optimizer.profiler();"
         "optimizer.candidates();"
+        "optimizer.batcalc();"
+        "optimizer.deadcode();"  // remove what is left after batcalc
 //      "optimizer.jit();" awaiting the new batcalc api
 //      "optimizer.oltp();"awaiting the autocommit front-end changes
         "optimizer.wlc();"
@@ -203,6 +209,8 @@ static struct PIPELINES {
         "optimizer.generator();"
         "optimizer.profiler();"
         "optimizer.candidates();"
+        "optimizer.batcalc();"
+        "optimizer.deadcode();"  // remove what is left after batcalc
 //      "optimizer.jit();" awaiting the new batcalc api
 //      "optimizer.oltp();"awaiting the autocommit front-end changes
         "optimizer.wlc();"
diff --git a/monetdb5/optimizer/opt_support.c b/monetdb5/optimizer/opt_support.c
--- a/monetdb5/optimizer/opt_support.c
+++ b/monetdb5/optimizer/opt_support.c
@@ -28,6 +28,7 @@ struct OPTcatalog {
        int actions;
 } optcatalog[]= {
 {"aliases",            0,      0,      0},
+{"batcalc",            0,      0,      0},
 {"coercions",  0,      0,      0},
 {"commonTerms",        0,      0,      0},
 {"constants",  0,      0,      0},
diff --git a/monetdb5/optimizer/opt_wrapper.c b/monetdb5/optimizer/opt_wrapper.c
--- a/monetdb5/optimizer/opt_wrapper.c
+++ b/monetdb5/optimizer/opt_wrapper.c
@@ -21,6 +21,7 @@
  * The optimizer used so far
 */
 #include "opt_aliases.h"
+#include "opt_batcalc.h"
 #include "opt_coercion.h"
 #include "opt_commonTerms.h"
 #include "opt_candidates.h"
@@ -58,6 +59,7 @@ struct{
        lng timing;
 } codes[] = {
        {"aliases", &OPTaliasesImplementation,0,0},
+       {"batcalc", &OPTbatcalcImplementation,0,0},
        {"candidates", &OPTcandidatesImplementation,0,0},
        {"coercions", &OPTcoercionImplementation,0,0},
        {"commonTerms", &OPTcommonTermsImplementation,0,0},
@@ -150,7 +152,7 @@ str OPTwrapper (Client cntxt, MalBlkPtr 
                        break;  
                }
        if (codes[i].nme == 0)
-               throw(MAL, optimizer, "Optimizer implementation '%s' missing", 
fcnnme);
+               throw(MAL, optimizer, "Optimizer implementation '%s' missing", 
optimizer);
 
        OPTIMIZERDEBUG {
                fprintf(stderr,"=FINISHED %s  %d\n",optimizer, actions);
diff --git a/monetdb5/optimizer/optimizer.mal b/monetdb5/optimizer/optimizer.mal
--- a/monetdb5/optimizer/optimizer.mal
+++ b/monetdb5/optimizer/optimizer.mal
@@ -22,6 +22,14 @@ pattern optimizer.coercions(mod:str, fcn
 address OPTwrapper
 comment "Handle simple type coercions";
 
+#opt_coercions.mal
+
+pattern optimizer.batcalc():str
+address OPTwrapper;
+pattern optimizer.batcalc(mod:str, fcn:str):str
+address OPTwrapper
+comment "Merge projections into the arithmetic";
+
 #opt_commonTerms.mal
 
 pattern optimizer.commonTerms():str
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to