Changeset: 8af5f7874e7d for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/8af5f7874e7d
Modified Files:
        monetdb5/optimizer/opt_evaluate.c
        monetdb5/optimizer/opt_fastpath.c
        monetdb5/optimizer/opt_oltp.c
        monetdb5/optimizer/opt_pushselect.c
        monetdb5/optimizer/opt_querylog.c
        monetdb5/optimizer/opt_strimps.c
        monetdb5/optimizer/opt_wlc.c
        sql/test/SQLancer/Tests/sqlancer17.test
Branch: Jan2022
Log Message:

Make sure every optimizer adds number of actions taken, plus avoid 
reallocations on fast paths


diffs (300 lines):

diff --git a/monetdb5/optimizer/opt_evaluate.c 
b/monetdb5/optimizer/opt_evaluate.c
--- a/monetdb5/optimizer/opt_evaluate.c
+++ b/monetdb5/optimizer/opt_evaluate.c
@@ -127,7 +127,7 @@ OPTevaluateImplementation(Client cntxt, 
        (void)stk;
 
        if ( mb->inlineProp )
-               return MAL_SUCCEED;
+               goto wrapup;
 
        cntxt->itrace = 0;
 
diff --git a/monetdb5/optimizer/opt_fastpath.c 
b/monetdb5/optimizer/opt_fastpath.c
--- a/monetdb5/optimizer/opt_fastpath.c
+++ b/monetdb5/optimizer/opt_fastpath.c
@@ -47,20 +47,25 @@
 #include "mal_profiler.h"
 #include "opt_prelude.h"
 
+#define optcall(TEST, OPT) \
+       do { \
+               if (TEST) { \
+                       if ((msg = OPT(cntxt, mb, stk, pci)) != MAL_SUCCEED) \
+                               goto bailout; \
+                       actions += *(int*)getVarValue(mb, getArg(pci, pci->argc 
- 1)); \
+                       pci->argc--; /* keep number of argc low, so 'pci' is 
not reallocated */ \
+               } \
+       } while (0)
+
 str
 OPTminimalfastImplementation(Client cntxt, MalBlkPtr mb, MalStkPtr stk, 
InstrPtr pci)
 {
-       int actions = 0;
        str msg = MAL_SUCCEED;
-       InstrPtr q;
-       int i, bincopy = 0, generator = 0, multiplex = 0;
-
-       (void)cntxt;
-       (void)stk;
+       int bincopy = 0, generator = 0, multiplex = 0, actions = 0;
 
        /* perform a single scan through the plan to determine which optimizer 
steps to skip */
-       for( i=0; i<mb->stop; i++){
-               q = getInstrPtr(mb,i);
+       for( int i=0; i<mb->stop; i++){
+               InstrPtr q = getInstrPtr(mb,i);
                if (q->modname == sqlRef && q->fcnname == importTableRef) 
                        bincopy= 1;
                if( getModuleId(q) == generatorRef)
@@ -69,18 +74,19 @@ OPTminimalfastImplementation(Client cntx
                        multiplex = 1;
        }
 
-       msg = OPTinlineImplementation(cntxt, mb, stk, pci);
-       if( msg == MAL_SUCCEED) msg = OPTremapImplementation(cntxt, mb, stk, 
pci);
-       if( bincopy && msg == MAL_SUCCEED) msg = 
OPTbincopyfromImplementation(cntxt, mb, stk, pci);
-       if( msg == MAL_SUCCEED) msg = OPTdeadcodeImplementation(cntxt, mb, stk, 
pci);
-       if( multiplex && msg == MAL_SUCCEED) msg = 
OPTmultiplexImplementation(cntxt, mb, stk, pci);
-       if( generator &&  msg == MAL_SUCCEED) msg = 
OPTgeneratorImplementation(cntxt, mb, stk, pci);
-       if( malProfileMode &&  msg == MAL_SUCCEED) msg = 
OPTprofilerImplementation(cntxt, mb, stk, pci);
-       if( malProfileMode &&  msg == MAL_SUCCEED) msg = 
OPTcandidatesImplementation(cntxt, mb, stk, pci);
-       if( msg == MAL_SUCCEED) msg = OPTgarbageCollectorImplementation(cntxt, 
mb, stk, pci);
+       optcall(true, OPTinlineImplementation);
+       optcall(true, OPTremapImplementation);
+       optcall(bincopy, OPTbincopyfromImplementation);
+       optcall(true, OPTdeadcodeImplementation);
+       optcall(multiplex, OPTmultiplexImplementation);
+       optcall(generator, OPTgeneratorImplementation);
+       optcall(malProfileMode, OPTprofilerImplementation);
+       optcall(malProfileMode, OPTcandidatesImplementation);
+       optcall(true, OPTgarbageCollectorImplementation);
 
        /* Defense line against incorrect plans  handled by optimizer steps */
        /* keep actions taken as a fake argument*/
+bailout:
        (void) pushInt(mb, pci, actions);
        return msg;
 }
@@ -88,18 +94,12 @@ OPTminimalfastImplementation(Client cntx
 str
 OPTdefaultfastImplementation(Client cntxt, MalBlkPtr mb, MalStkPtr stk, 
InstrPtr pci)
 {
-       int actions = 0;
        str msg = MAL_SUCCEED;
-       InstrPtr q;
-       int i, bincopy = 0, generator = 0, multiplex = 0;
-
-       (void)cntxt;
-       (void)stk;
-
+       int bincopy = 0, generator = 0, multiplex = 0, actions = 0;
 
        /* perform a single scan through the plan to determine which optimizer 
steps to skip */
-       for( i=0; i<mb->stop; i++){
-               q = getInstrPtr(mb,i);
+       for( int i=0; i<mb->stop; i++){
+               InstrPtr q = getInstrPtr(mb,i);
                if (q->modname == sqlRef && q->fcnname == importTableRef) 
                        bincopy= 1;
                if( getModuleId(q) == generatorRef)
@@ -107,40 +107,42 @@ OPTdefaultfastImplementation(Client cntx
                if ( getFunctionId(q) == multiplexRef)
                        multiplex = 1;
        }
-       msg = OPTinlineImplementation(cntxt, mb, stk, pci);
-       if( msg == MAL_SUCCEED) msg = OPTremapImplementation(cntxt, mb, stk, 
pci);
-       if( msg == MAL_SUCCEED) msg = OPTcostModelImplementation(cntxt, mb, 
stk, pci);
-       if( msg == MAL_SUCCEED) msg = OPTcoercionImplementation(cntxt, mb, stk, 
pci);
-       if( msg == MAL_SUCCEED) msg = OPTaliasesImplementation(cntxt, mb, stk, 
pci);
-       if( msg == MAL_SUCCEED) msg = OPTevaluateImplementation(cntxt, mb, stk, 
pci);
-       if( msg == MAL_SUCCEED) msg = OPTemptybindImplementation(cntxt, mb, 
stk, pci);
-       if( msg == MAL_SUCCEED) msg = OPTdeadcodeImplementation(cntxt, mb, stk, 
pci);
-       if( msg == MAL_SUCCEED) msg = OPTpushselectImplementation(cntxt, mb, 
stk, pci);
-       if( msg == MAL_SUCCEED) msg = OPTaliasesImplementation(cntxt, mb, stk, 
pci);
-       if( msg == MAL_SUCCEED) msg = OPTmitosisImplementation(cntxt, mb, stk, 
pci);
-       if( msg == MAL_SUCCEED) msg = OPTmergetableImplementation(cntxt, mb, 
stk, pci);
-       if( bincopy && msg == MAL_SUCCEED) msg = 
OPTbincopyfromImplementation(cntxt, mb, stk, pci);
-       if( msg == MAL_SUCCEED) msg = OPTaliasesImplementation(cntxt, mb, stk, 
pci);
-       if( msg == MAL_SUCCEED) msg = OPTconstantsImplementation(cntxt, mb, 
stk, pci);
-       if( msg == MAL_SUCCEED) msg = OPTcommonTermsImplementation(cntxt, mb, 
stk, pci);
-       if( msg == MAL_SUCCEED) msg = OPTprojectionpathImplementation(cntxt, 
mb, stk, pci);
-       if( msg == MAL_SUCCEED) msg = OPTdeadcodeImplementation(cntxt, mb, stk, 
pci);
-       if( msg == MAL_SUCCEED) msg = OPTreorderImplementation(cntxt, mb, stk, 
pci);
-       if( msg == MAL_SUCCEED) msg = OPTmatpackImplementation(cntxt, mb, stk, 
pci);
-       if( msg == MAL_SUCCEED) msg = OPTdataflowImplementation(cntxt, mb, stk, 
pci);
-       if( msg == MAL_SUCCEED) msg = OPTquerylogImplementation(cntxt, mb, stk, 
pci);
-       if( multiplex && msg == MAL_SUCCEED) msg = 
OPTmultiplexImplementation(cntxt, mb, stk, pci);
-       if( generator && msg == MAL_SUCCEED) msg = 
OPTgeneratorImplementation(cntxt, mb, stk, pci);
-       if( malProfileMode &&  msg == MAL_SUCCEED) msg = 
OPTprofilerImplementation(cntxt, mb, stk, pci);
-       if( malProfileMode &&  msg == MAL_SUCCEED) msg = 
OPTcandidatesImplementation(cntxt, mb, stk, pci);
-       if( msg == MAL_SUCCEED) msg = OPTdeadcodeImplementation(cntxt, mb, stk, 
pci);
-       if( msg == MAL_SUCCEED) msg = OPTpostfixImplementation(cntxt, mb, stk, 
pci);
-       // if( msg == MAL_SUCCEED) msg = OPTjitImplementation(cntxt, mb, stk, 
pci);
-       if( msg == MAL_SUCCEED) msg = OPTwlcImplementation(cntxt, mb, stk, pci);
-       if( msg == MAL_SUCCEED) msg = OPTgarbageCollectorImplementation(cntxt, 
mb, stk, pci);
+
+       optcall(true, OPTinlineImplementation);
+       optcall(true, OPTremapImplementation);
+       optcall(true, OPTcostModelImplementation);
+       optcall(true, OPTcoercionImplementation);
+       optcall(true, OPTaliasesImplementation);
+       optcall(true, OPTevaluateImplementation);
+       optcall(true, OPTemptybindImplementation);
+       optcall(true, OPTdeadcodeImplementation);
+       optcall(true, OPTpushselectImplementation);
+       optcall(true, OPTaliasesImplementation);
+       optcall(true, OPTmitosisImplementation);
+       optcall(true, OPTmergetableImplementation);
+       optcall(bincopy, OPTbincopyfromImplementation);
+       optcall(true, OPTaliasesImplementation);
+       optcall(true, OPTconstantsImplementation);
+       optcall(true, OPTcommonTermsImplementation);
+       optcall(true, OPTprojectionpathImplementation);
+       optcall(true, OPTdeadcodeImplementation);
+       optcall(true, OPTreorderImplementation);
+       optcall(true, OPTmatpackImplementation);
+       optcall(true, OPTdataflowImplementation);
+       optcall(true, OPTquerylogImplementation);
+       optcall(multiplex, OPTmultiplexImplementation);
+       optcall(generator, OPTgeneratorImplementation);
+       optcall(malProfileMode, OPTprofilerImplementation);
+       optcall(malProfileMode, OPTcandidatesImplementation);
+       optcall(true, OPTdeadcodeImplementation);
+       optcall(true, OPTpostfixImplementation);
+       // optcall(true, OPTjitImplementation);
+       optcall(true, OPTwlcImplementation);
+       optcall(true, OPTgarbageCollectorImplementation);
 
        /* Defense line against incorrect plans  handled by optimizer steps */
        /* keep actions taken as a fake argument*/
+bailout:
        (void) pushInt(mb, pci, actions);
        return msg;
 }
diff --git a/monetdb5/optimizer/opt_oltp.c b/monetdb5/optimizer/opt_oltp.c
--- a/monetdb5/optimizer/opt_oltp.c
+++ b/monetdb5/optimizer/opt_oltp.c
@@ -74,7 +74,7 @@ OPToltpImplementation(Client cntxt, MalB
        }
 
        if( updates == 0)
-               return 0;
+               goto wrapup;
 
        // Get a free instruction, don't get it from mb
        lcks= newInstruction(0, oltpRef,lockRef);
@@ -89,13 +89,13 @@ OPToltpImplementation(Client cntxt, MalB
 
        if( lcks->argc == 1 ){
                freeInstruction(lcks);
-               return MAL_SUCCEED;
+               goto wrapup;
        }
 
        // Now optimize the code
        if ( newMalBlkStmt(mb,mb->ssize + 6) < 0) {
                freeInstruction(lcks);
-               return 0;
+               throw(MAL,"optimizer.oltp", SQLSTATE(HY013) MAL_MALLOC_FAIL);
        }
        pushInstruction(mb,old[0]);
        pushInstruction(mb,lcks);
@@ -133,6 +133,7 @@ OPToltpImplementation(Client cntxt, MalB
        //      msg = chkDeclarations(mb);
 
        /* keep actions taken as a fake argument*/
+wrapup:
        (void) pushInt(mb, pci, actions);
        return msg;
 }
diff --git a/monetdb5/optimizer/opt_pushselect.c 
b/monetdb5/optimizer/opt_pushselect.c
--- a/monetdb5/optimizer/opt_pushselect.c
+++ b/monetdb5/optimizer/opt_pushselect.c
@@ -145,7 +145,7 @@ OPTpushselectImplementation(Client cntxt
 
        subselects = (subselect_t) {0};
        if( mb->errors)
-               return MAL_SUCCEED;
+               throw(MAL, "optimizer.pushselect", "%s", mb->errors);
 
        no_mito = !isOptimizerEnabled(mb, mitosisRef);
        (void) stk;
diff --git a/monetdb5/optimizer/opt_querylog.c 
b/monetdb5/optimizer/opt_querylog.c
--- a/monetdb5/optimizer/opt_querylog.c
+++ b/monetdb5/optimizer/opt_querylog.c
@@ -24,7 +24,7 @@ OPTquerylogImplementation(Client cntxt, 
 
        // query log needed?
        if ( !QLOGisset() )
-               return MAL_SUCCEED;
+               goto wrapup;
 
        (void) stk;             /* to fool compilers */
        (void) cntxt;
@@ -38,7 +38,7 @@ OPTquerylogImplementation(Client cntxt, 
        }
        if ( defineQuery == NULL)
                /* nothing to do */
-               return MAL_SUCCEED;
+               goto wrapup;
 
        actions++;
        limit= mb->stop;
@@ -195,6 +195,7 @@ OPTquerylogImplementation(Client cntxt, 
        if (!msg)
                msg = chkDeclarations(mb);
        /* keep actions taken as a fake argument*/
+wrapup:
        (void) pushInt(mb, pci, actions);
        return msg;
 }
diff --git a/monetdb5/optimizer/opt_strimps.c b/monetdb5/optimizer/opt_strimps.c
--- a/monetdb5/optimizer/opt_strimps.c
+++ b/monetdb5/optimizer/opt_strimps.c
@@ -37,7 +37,7 @@ OPTstrimpsImplementation(Client cntxt, M
        limit= mb->stop;
 
        if ( mb->inlineProp )
-               return MAL_SUCCEED;
+               goto bailout;
 
        for(i=0; i < limit; i++) {
                p = old[i];
diff --git a/monetdb5/optimizer/opt_wlc.c b/monetdb5/optimizer/opt_wlc.c
--- a/monetdb5/optimizer/opt_wlc.c
+++ b/monetdb5/optimizer/opt_wlc.c
@@ -53,7 +53,7 @@ OPTwlcImplementation(Client cntxt, MalBl
        def = 0;
 
        if(query) // nothing to log
-               return MAL_SUCCEED;
+               goto wrapup;
 
        // We use a fake collection of objects to speed up the checking later.
 
diff --git a/sql/test/SQLancer/Tests/sqlancer17.test 
b/sql/test/SQLancer/Tests/sqlancer17.test
--- a/sql/test/SQLancer/Tests/sqlancer17.test
+++ b/sql/test/SQLancer/Tests/sqlancer17.test
@@ -416,6 +416,21 @@ statement ok
 ROLLBACK
 
 statement ok
+CREATE TABLE t1(c0 boolean)
+
+statement ok
+SET "optimizer"='default_fast'
+
+statement ok rowcount 0
+DELETE FROM t1
+
+statement ok
+SET "optimizer"='default_pipe'
+
+statement ok
+DROP TABLE t1
+
+statement ok
 CREATE TABLE t2(c0 INTERVAL DAY)
 
 statement ok rowcount 1
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to