Changeset: 990de495ab37 for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=990de495ab37
Modified Files:
        sql/backends/monet5/sql_execute.c
        sql/backends/monet5/sql_optimizer.c
        sql/backends/monet5/sql_optimizer.h
        sql/backends/monet5/sql_scenario.c
Branch: jit
Log Message:

Baseline for just-in-time optimizer execution
The optimizer pipeline is moved from the parsing phase to
the execution phase. This potentially gives more opportunities
for optimizers, e.g. identical argument handling to find common
subexpressions, dealing with BATs without insertion/deletions,
more opportunities for candidate lists in arithmetic...


diffs (228 lines):

diff --git a/sql/backends/monet5/sql_execute.c 
b/sql/backends/monet5/sql_execute.c
--- a/sql/backends/monet5/sql_execute.c
+++ b/sql/backends/monet5/sql_execute.c
@@ -38,6 +38,7 @@
 #include "mal_debugger.h"
 #include <mtime.h>
 #include "optimizer.h"
+#include "opt_inline.h"
 #include <unistd.h>
 
 /*
@@ -189,24 +190,58 @@ SQLsetTrace(Client cntxt)
 }
 
 static str
-SQLrun(Client c, mvc *m){
+SQLrun(Client c, backend *be, mvc *m){
        str msg= MAL_SUCCEED;
-       MalBlkPtr old;
+       MalBlkPtr mc = 0, mb=c->curprg->def, old=mb;
+       InstrPtr p=0;
+       int i,j, retc;
+       ValPtr val;
                        
-       // first consider running in debug mode
+       // locate and inline the query template instruction
+       mb = copyMalBlk(c->curprg->def);
+       for ( i= 1; i < mb->stop;i++){
+               p=getInstrPtr(mb,i);
+               if( (p->token == FCNcall || p->token == FUNCTIONsymbol) && 
p->blk) {
+                       mc= copyMalBlk(p->blk);
+                       retc =p->retc;
+                       freeMalBlk(mb);
+                       mb= mc;
+                       // declare the argument values as a constant
+                       // We use the knowledge that the arguments are first on 
the stack
+                       for (j = 0; j < m->argc; j++) {
+                               sql_subtype *pt = be->q->params + j;
+                               atom *arg = m->args[j];
+                               
+                               if (!atom_cast(arg, pt)) {
+                                       throw(SQL, "sql.prepare", "07001!EXEC: 
wrong type for argument %d of " "query template : %s, expected %s", i + 1, 
atom_type(arg)->type->sqlname, pt->type->sqlname);
+                               }
+                               val= (ValPtr) &arg->data;
+                               VALcopy(&mb->var[j+retc]->value, val);
+                               setVarConstant(mb, j+retc);
+                               setVarFixed(mb, j+retc);
+                       }
+                       mb->stmt[0]->argc = 1;
+                       break;
+               }
+       }
+       // JIT optimize the SQL query using all current information
+       // This include template constants, BAT sizes.
+       optimizeQuery(c,mb);
+
        if( m->emod & mod_debug)
-               msg = runMALDebugger(c, c->curprg->def);
+               msg = runMALDebugger(c, mb);
         else{
                if( m->emod & mod_trace){
-                       c->curprg->def = copyMalBlk(old = c->curprg->def);
                        SQLsetTrace(c);
-                       msg = runMAL(c, c->curprg->def, 0, 0);
+                       msg = runMAL(c, mb, 0, 0);
                        stopTrace(0);
-                       freeMalBlk(c->curprg->def);
-                       c->curprg->def = old;
                } else
-                       msg = runMAL(c, c->curprg->def, 0, 0);
+                       msg = runMAL(c, mb, 0, 0);
        }
+
+       // release the resources
+       freeMalBlk(mb);
+       mb = old;
        return msg;
 }
 
@@ -369,7 +404,7 @@ SQLstatementIntern(Client c, str *expr, 
                if (!output)
                        sql->out = NULL;        /* no output stream */
                if (execute)
-                       msg = SQLrun(c,m);
+                       msg = SQLrun(c,be,m);
 
                MSresetInstructions(c->curprg->def, oldstop);
                freeVariables(c, c->curprg->def, NULL, oldvtop);
@@ -590,7 +625,7 @@ SQLengineIntern(Client c, backend *be)
        if (MALcommentsOnly(c->curprg->def)) 
                msg = MAL_SUCCEED;
        else 
-               msg = SQLrun(c,m);
+               msg = SQLrun(c,be,m);
 
 cleanup_engine:
        if (m->type == Q_SCHEMA)
@@ -668,7 +703,7 @@ RAstatement(Client c, MalBlkPtr mb, MalS
                        msg = createException(SQL,"RAstatement","Program 
contains errors");
                else 
                        addQueryToCache(c);
-                       SQLrun(c,m);
+                       SQLrun(c,b,m);
                if (!msg) {
                        resetMalBlk(c->curprg->def, oldstop);
                        freeVariables(c, c->curprg->def, NULL, oldvtop);
diff --git a/sql/backends/monet5/sql_optimizer.c 
b/sql/backends/monet5/sql_optimizer.c
--- a/sql/backends/monet5/sql_optimizer.c
+++ b/sql/backends/monet5/sql_optimizer.c
@@ -128,7 +128,7 @@ addOptimizers(Client c, MalBlkPtr mb, ch
                addtoMalBlkHistory(mb, "getStatistics");
 }
 
-static str
+str
 sqlJIToptimizer(Client c, MalBlkPtr mb, backend *be)
 {
        str msg;
@@ -150,9 +150,8 @@ sqlJIToptimizer(Client c, MalBlkPtr mb, 
 }
 
 str
-optimizeQuery(Client c)
+optimizeQuery(Client c, MalBlkPtr mb)
 {
-       MalBlkPtr mb;
        backend *be;
        str msg = 0;
 
@@ -161,7 +160,6 @@ optimizeQuery(Client c)
 
        trimMalBlk(c->curprg->def);
        c->blkmode = 0;
-       mb = c->curprg->def;
        chkProgram(c->fdout, c->nspace, mb);
 #ifdef _SQL_OPTIMIZER_DEBUG
        mnstr_printf(GDKout, "Optimize query\n");
@@ -190,14 +188,16 @@ optimizeQuery(Client c)
 void
 addQueryToCache(Client c)
 {
-       str msg = NULL;
+       //str msg = NULL;
 
        insertSymbol(c->nspace, c->curprg);
-       msg = optimizeQuery(c);
+       /*
+       msg = optimizeQuery(c,c->curprg);
        if (msg != MAL_SUCCEED) {
                showScriptException(c->fdout, c->curprg->def, 0, MAL, "%s", 
msg);
                GDKfree(msg);
        }
+       */
 }
 
 /*
diff --git a/sql/backends/monet5/sql_optimizer.h 
b/sql/backends/monet5/sql_optimizer.h
--- a/sql/backends/monet5/sql_optimizer.h
+++ b/sql/backends/monet5/sql_optimizer.h
@@ -12,9 +12,10 @@
 
 //#define _SQL_OPTIMIZER_DEBUG
 
-sql5_export str optimizeQuery(Client c);
+sql5_export str optimizeQuery(Client c,MalBlkPtr mb);
 sql5_export void addQueryToCache(Client c);
 sql5_export str SQLoptimizer(Client c);
+sql5_export str sqlJIToptimizer(Client c, MalBlkPtr mb, backend *be);
 sql5_export void SQLsetAccessMode(Client c);
 sql5_export str getSQLoptimizer(mvc *m);
 sql5_export void addOptimizers(Client c, MalBlkPtr mb, char *pipe);
diff --git a/sql/backends/monet5/sql_scenario.c 
b/sql/backends/monet5/sql_scenario.c
--- a/sql/backends/monet5/sql_scenario.c
+++ b/sql/backends/monet5/sql_scenario.c
@@ -1063,8 +1063,10 @@ SQLparser(Client c)
                scanner_query_processed(&(m->scanner));
        } else if (caching(m) && cachable(m, NULL) && m->emode != m_prepare && 
(be->q = qc_match(m->qc, m->sym, m->args, m->argc, m->scanner.key ^ 
m->session->schema->base.id)) != NULL) {
                /* query template was found in the query cache */
+       /*
                if (!(m->emod & (mod_explain | mod_debug | mod_trace )))
                        m->emode = m_inplace;
+       */
                scanner_query_processed(&(m->scanner));
        } else {
                sql_rel *r;
@@ -1114,9 +1116,10 @@ SQLparser(Client c)
 
                        /* register name in the namespace */
                        be->q->name = putName(be->q->name, strlen(be->q->name));
-                       /* unless a query modifier has been set, we directly 
call the cached plan */
+                       /* unless a query modifier has been set, we directly 
call the cached plan 
                        if (m->emode == m_normal && m->emod == mod_none)
                                m->emode = m_inplace;
+                       */
                }
        }
        if (err)
@@ -1124,10 +1127,10 @@ SQLparser(Client c)
        if (err == 0) {
                /* no parsing error encountered, finalize the code of the query 
wrapper */
                if (be->q) {
-                       if (m->emode == m_prepare)
+                       if (m->emode == m_prepare){
                                /* For prepared queries, return a table with 
result set structure*/
                                err = mvc_export_prepare(m, c->fdout, be->q, 
"");
-                       else if (m->emode == m_inplace) {
+                       } else if (m->emode == m_inplace) {
                                /* everything ready for a fast call */
                        } else if( m->emode == m_execute || m->emode == 
m_normal || m->emode == m_plan){
                                /* call procedure generation (only in cache 
mode) */
@@ -1136,13 +1139,15 @@ SQLparser(Client c)
                }
 
                pushEndInstruction(c->curprg->def);
+               // Prepared query plans should be optimized
+               if(err == 0 && m->emode == m_prepare ) 
sqlJIToptimizer(c,be->q->code,be);
 
                /* check the query wrapper for errors */
                chkTypes(c->fdout, c->nspace, c->curprg->def, TRUE);
 
                /* in case we had produced a non-cachable plan, the optimizer 
should be called */
-               if (opt && !c->curprg->def->errors ) {
-                       str msg = optimizeQuery(c);
+               if (opt ) {
+                       str msg = optimizeQuery(c, c->curprg->def);
 
                        if (msg != MAL_SUCCEED) {
                                sqlcleanup(m, err);
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to