Update of /cvsroot/monetdb/MonetDB5/src/optimizer
In directory sc8-pr-cvs16.sourceforge.net:/tmp/cvs-serv28759/src/optimizer

Modified Files:
      Tag: GDK-2
        opt_accumulators.mx opt_emptySet.mx opt_factorize.mx 
        opt_garbageCollector.mx opt_joinselect.mx opt_macro.mx 
        opt_mergetable.mx opt_multiplex.mx opt_partitions.mx 
        opt_peephole.mx opt_pushranges.mx opt_remap.mx 
        opt_remoteQueries.mx opt_singleton.mx opt_support.mx 
Log Message:
propagated changes of Friday Aug 17 2007
from the development trunk to the GDK-2 branch


Index: opt_pushranges.mx
===================================================================
RCS file: /cvsroot/monetdb/MonetDB5/src/optimizer/opt_pushranges.mx,v
retrieving revision 1.9
retrieving revision 1.9.2.1
diff -u -d -r1.9 -r1.9.2.1
--- opt_pushranges.mx   16 Jul 2007 10:52:54 -0000      1.9
+++ opt_pushranges.mx   17 Aug 2007 15:38:17 -0000      1.9.2.1
@@ -221,9 +221,9 @@
                                        p->argc=1;
                                        getModuleId(p)= batRef;
                                        getFunctionId(p)= newRef;
-                                       pushArgument(mb,p, 
typ=newTypeVariable(mb, getHeadType(x)));
+                                       p= pushArgument(mb,p, 
typ=newTypeVariable(mb, getHeadType(x)));
                                        isConstant(mb,typ)= TRUE;
-                                       pushArgument(mb,p, 
typ=newTypeVariable(mb, getTailType(x)));
+                                       p= pushArgument(mb,p, 
typ=newTypeVariable(mb, getTailType(x)));
                                        isConstant(mb,typ)= TRUE;
                                        actions++;
                                }

Index: opt_remoteQueries.mx
===================================================================
RCS file: /cvsroot/monetdb/MonetDB5/src/optimizer/opt_remoteQueries.mx,v
retrieving revision 1.12
retrieving revision 1.12.2.1
diff -u -d -r1.12 -r1.12.2.1
--- opt_remoteQueries.mx        16 Jul 2007 10:53:01 -0000      1.12
+++ opt_remoteQueries.mx        17 Aug 2007 15:38:21 -0000      1.12.2.1
@@ -201,7 +201,7 @@
                        getModuleId(r)= mserverRef;
                        getFunctionId(r)= lookupRef;
                        j= getArg(r,0)= newTmpVariable(mb, TYPE_int);
-                       pushArgument(mb,r, getArg(p,@1));
+                       r= pushArgument(mb,r, getArg(p,@1));
                        pushInstruction(mb,r);
                        dbalias[dbtop].dbhdl= j;
                        dbalias[dbtop++].dbname= db;
@@ -214,20 +214,20 @@
        getModuleId(r)= mserverRef;
        getFunctionId(r)= rpcRef;
        getArg(r,0)= newTmpVariable(mb, @1);
-       pushArgument(mb,r,j);
+       r= pushArgument(mb,r,j);
 
 @= putRemoteVariables
        for(j=p->retc; j<p->argc; j++)
        if( location[getArg(p,j)] == 0 && !isConstant(mb,getArg(p,j)) ){
                q= newStmt(mb,mserverRef,putRef);
                getArg(q,0)= newTmpVariable(mb, TYPE_void);
-               pushArgument(mb,q,location[getArg(p,j)]);
-               pushStr(mb,q, getRefName(mb,getArg(p,j)));
-               pushArgument(mb,q,getArg(p,j));
+               q= pushArgument(mb,q,location[getArg(p,j)]);
+               q= pushStr(mb,q, getRefName(mb,getArg(p,j)));
+               q= pushArgument(mb,q,getArg(p,j));
        }
 @= remoteAction
        s= RQcall2str(mb,p);
-       pushStr(mb,r,s+1);
+       r= pushStr(mb,r,s+1);
        GDKfree(s);
        pushInstruction(mb,r);
        freeInstruction(p);
@@ -317,7 +317,7 @@
                                @:prepareRemote(TYPE_void)@
 
                                s= RQcall2str(mb,p);
-                               pushStr(mb,r,s+1);
+                               r= pushStr(mb,r,s+1);
                                GDKfree(s);
                                pushInstruction(mb,r);
                                freeInstruction(p);
@@ -401,10 +401,10 @@
                                if( location[getArg(p,j)]){
                                        q= newStmt(mb,mserverRef,rpcRef);
                                        getArg(q,0)= getArg(p,j);
-                                       
pushArgument(mb,q,location[getArg(p,j)]);
+                                       q= 
pushArgument(mb,q,location[getArg(p,j)]);
                                        snprintf(buf,BUFSIZ,"io.print(%s);",
                                                getRefName(mb,getArg(p,j)) );
-                                       pushStr(mb,q,buf);
+                                       q= pushStr(mb,q,buf);
                                }
                                pushInstruction(mb,p);
                                /* as of now all the targets are also local */
@@ -417,19 +417,19 @@
                                getModuleId(r)= mserverRef;
                                getFunctionId(r)= rpcRef;
                                getArg(r,0)= newTmpVariable(mb, TYPE_void);
-                               pushArgument(mb, r, remoteSite);
+                               r= pushArgument(mb, r, remoteSite);
 
                                for(j=p->retc; j<p->argc; j++)
                                if( location[getArg(p,j)] == 0 && 
!isConstant(mb,getArg(p,j)) ){
                                        q= newStmt(mb,mserverRef,putRef);
                                        getArg(q,0)= newTmpVariable(mb, 
TYPE_void);
-                                       pushArgument(mb, q, remoteSite);
-                                       pushStr(mb,q, 
getRefName(mb,getArg(p,j)));
-                                       pushArgument(mb, q, getArg(p,j));
+                                       q= pushArgument(mb, q, remoteSite);
+                                       q= pushStr(mb,q, 
getRefName(mb,getArg(p,j)));
+                                       q= pushArgument(mb, q, getArg(p,j));
                                }
                                s= RQcall2str(mb, p);
                                pushInstruction(mb,r);
-                               pushStr(mb,r,s+1);
+                               r= pushStr(mb,r,s+1);
                                GDKfree(s);
                                for(j=0; j<p->retc; j++)
                                        location[getArg(p,j)]= remoteSite;

Index: opt_emptySet.mx
===================================================================
RCS file: /cvsroot/monetdb/MonetDB5/src/optimizer/opt_emptySet.mx,v
retrieving revision 1.39
retrieving revision 1.39.2.1
diff -u -d -r1.39 -r1.39.2.1
--- opt_emptySet.mx     16 Jul 2007 10:52:37 -0000      1.39
+++ opt_emptySet.mx     17 Aug 2007 15:37:58 -0000      1.39.2.1
@@ -202,7 +202,7 @@
                                                setFunctionId(p, NULL);
                                                p->argc = 1;
                                                p->token = ASSIGNsymbol;
-                                               pushBit(mb, p, FALSE);
+                                               p= pushBit(mb, p, FALSE);
                                                actions++;
                                                break;
                                        } 
@@ -258,9 +258,9 @@
                                        p->token = ASSIGNsymbol;
                                        p->typechk= TYPE_UNKNOWN;
                                        p->fcn = NULL;
-                                       pushArgument(mb, p, 
+                                       p= pushArgument(mb, p, 
                                                newTypeVariable(mb, 
getHeadType(tpe)));
-                                       pushArgument(mb, p, 
+                                       p= pushArgument(mb, p, 
                                                newTypeVariable(mb, 
getTailType(tpe)));
                                        actions++;
                                        break;

Index: opt_factorize.mx
===================================================================
RCS file: /cvsroot/monetdb/MonetDB5/src/optimizer/opt_factorize.mx,v
retrieving revision 1.11
retrieving revision 1.11.2.1
diff -u -d -r1.11 -r1.11.2.1
--- opt_factorize.mx    16 Jul 2007 10:52:39 -0000      1.11
+++ opt_factorize.mx    17 Aug 2007 15:38:00 -0000      1.11.2.1
@@ -210,8 +210,8 @@
        /* added control block */
        v = newVariable(mb, GDKstrdup("always"), TYPE_bit);
        p = newInstruction(NULL,BARRIERsymbol);
-       pushReturn(mb, p, v);
-       pushBit(mb,p,TRUE);
+       p= pushReturn(mb, p, v);
+       p= pushBit(mb,p,TRUE);
        mb->stmt[k++] = p;
 
        for (i = 0; i < sk - 1; i++)
@@ -220,15 +220,15 @@
        /* finalize the factory */
        if (returnseen == 0) {
                p = newInstruction(NULL,YIELDsymbol);
-               pushReturn(mb, p, findVariable(mb, getFcnName(mb)));
+               p= pushReturn(mb, p, findVariable(mb, getFcnName(mb)));
                mb->stmt[k++] = p;
        }
        p = newInstruction(NULL,REDOsymbol);
-       pushReturn(mb, p, v);
+       p= pushReturn(mb, p, v);
        mb->stmt[k++] = p;
 
        p = newInstruction(NULL,EXITsymbol);
-       pushReturn(mb, p, v);
+       p= pushReturn(mb, p, v);
        mb->stmt[k++] = p;
 
        /* add END statement */

Index: opt_support.mx
===================================================================
RCS file: /cvsroot/monetdb/MonetDB5/src/optimizer/opt_support.mx,v
retrieving revision 1.46
retrieving revision 1.46.2.1
diff -u -d -r1.46 -r1.46.2.1
--- opt_support.mx      8 Aug 2007 12:58:27 -0000       1.46
+++ opt_support.mx      17 Aug 2007 15:38:24 -0000      1.46.2.1
@@ -385,8 +385,10 @@
 @item 
 @tab    Garbage Collector.
 @end multitable
[EMAIL PROTECTED]
-Alias removal can be applied after each other optimization step.
+The interaction also excludes combinations. For example,
+the Accumulator should be used after the Partition
+optimizer.
+
 @node Building Blocks, Framework, Dependencies, The MAL Optimizer
 @- Optimizer Building Blocks
 
@@ -433,7 +435,7 @@
 version of a MAL function while it is already referenced by or
 when polymorphic typechecking is required afterwards.
 
[EMAIL PROTECTED] Building your optimizer
[EMAIL PROTECTED] Building Your Own Optimizer
 Implementation of your own MAL-MAL optimizer can best be started
 from refinement of one of the examples included in the code base.
 Beware that only those used in the critical path of SQL execution
@@ -447,7 +449,7 @@
 
 A tally on major events should be retained, because it gives
 valuable insight in the effectiveness of your optimizer.
-The effects of all optimizers is collected in a system catalog.[todo]
+The effects of all optimizers is collected in a system catalog.
 
 Each optimizer ends with a strong defense line, @code{optimizerCheck()}
 It performs a complete type and data flow analysis before returning.
@@ -455,6 +457,11 @@
 plan produced for inspection. Studying the differences between
 optimizer steps provide valuable information to improve your code.
 
+The functionality of the optimizer should be clearly delineated.
+The guiding policy is that it is always safe to not apply an
+optimizer step. 
+This helps to keep the optimizers as independent as possible.
+
 It really helps if you start with a few tiny examples to test
 your optimizer. They should be added to the Tests directory
 and administered in Tests/All.
@@ -462,6 +469,16 @@
 Breaking up the optimizer into different components and
 grouping them together in arbitrary sequences calls for
 careful programming.
+
+One of the major hurdles is to test interference of the
+optimizer. The test set is a good starting point, but does
+not garantee that all cases have been covered.
+
+In principle, any subset of optimizers should work flawlessly.
+With a few tens of optimizers this amounts to potential millions
+of runs. Adherence to a partial order reduces the problem, but
+still is likely to be too resource consumptive to test continously.
+
 @{
 The optimizers defined here are registered to the optimizer module.
 @mal
@@ -1132,20 +1149,30 @@
                        }
        }
 }
-
[EMAIL PROTECTED]
+Used in the merge table optimizer. It is build incrementally
+and should be conservative.
[EMAIL PROTECTED]
 int isFragmentGroup(InstrPtr p){
        return
-                       (getModuleId(p)== algebraRef && getFunctionId(p)== 
selectRef) ||
-                       (getModuleId(p)== algebraRef && 
getFunctionId(p)==uselectRef) ||
-                       (getModuleId(p)== algebraRef && 
getFunctionId(p)==likeselectRef) ||
-                       (getModuleId(p)== batRef && 
getFunctionId(p)==reverseRef) ||
-                       (getModuleId(p)== batRef && 
getFunctionId(p)==mirrorRef) ||
-                       (getModuleId(p)== algebraRef && 
getFunctionId(p)==markTRef) ||
-                       (getModuleId(p)== algebraRef && 
getFunctionId(p)==kdifferenceRef) ||
-                       (getModuleId(p)== algebraRef && getFunctionId(p)== 
joinRef) ||
-                       (getModuleId(p)== algebraRef && getFunctionId(p)== 
semijoinRef) ||
-                       (getModuleId(p)== batRef && getFunctionId(p)== 
setAccessRef) ||
-                       (getModuleId(p)== batRef && getFunctionId(p)== 
appendRef)  ||
-                       (getModuleId(p)== batRef && getFunctionId(p)== 
deleteRef);
+                       (getModuleId(p)== constraintsRef && 
+                               getFunctionId(p)== getName("emptySet",8)) ||
+                       (getModuleId(p)== algebraRef && (
+                               getFunctionId(p)== selectRef ||
+                               getFunctionId(p)==uselectRef ||
+                               getFunctionId(p)==likeselectRef ||
+                               getFunctionId(p)==markTRef ||
+                               getFunctionId(p)==kdifferenceRef ||
+                               getFunctionId(p)== joinRef ||
+                               getFunctionId(p)== semijoinRef
+                       )       )  ||
+                       (getModuleId(p)== batRef && (
+                               getFunctionId(p)==reverseRef ||
+                               getFunctionId(p)==mirrorRef ||
+                               getFunctionId(p)== setAccessRef ||
+                               getFunctionId(p)== setWriteModeRef ||
+                               getFunctionId(p)== appendRef  ||
+                               getFunctionId(p)== deleteRef
+                       ) );
 }
 @}

Index: opt_singleton.mx
===================================================================
RCS file: /cvsroot/monetdb/MonetDB5/src/optimizer/opt_singleton.mx,v
retrieving revision 1.13
retrieving revision 1.13.2.1
diff -u -d -r1.13 -r1.13.2.1
--- opt_singleton.mx    16 Jul 2007 10:53:08 -0000      1.13
+++ opt_singleton.mx    17 Aug 2007 15:38:23 -0000      1.13.2.1
@@ -120,7 +120,7 @@
        setArgument(mb, p, 1, tuples[varid].tidx);
        setVarUsed(mb,getArg(p,1),TRUE);
        p->retc++;
-       pushArgument(mb, p, tuples[varid].vidx);
+       p= pushArgument(mb, p, tuples[varid].vidx);
        tuples[varid].packed = 0;
 }
 
@@ -133,8 +133,8 @@
        getArg(p, 0) = tuples[varid].vidx;
        p->retc = 1;
        p->argc = 1;
-       pushArgument(mb, p, tuples[varid].hidx);
-       pushArgument(mb, p, tuples[varid].tidx);
+       p= pushArgument(mb, p, tuples[varid].hidx);
+       p= pushArgument(mb, p, tuples[varid].tidx);
 }
 
 static int
@@ -177,8 +177,8 @@
                                setModuleId(p, NULL);
                                setFunctionId(p, NULL);
                                p->retc = p->argc = 2;
-                               pushArgument(mb, p, tuples[k].hidx);
-                               pushArgument(mb, p, tuples[k].tidx);
+                               p= pushArgument(mb, p, tuples[k].hidx);
+                               p= pushArgument(mb, p, tuples[k].tidx);
                                k = STnewTuple(mb, getArg(p, 0), &top, tuples);
                                p->argv[0] = tuples[k].hidx;
                                p->argv[1] = tuples[k].tidx;
@@ -192,7 +192,7 @@
                                (k = STalias(getArg(p, 1), top, tuples)) >= 0) {
                                if (tuples[k].packed == 0) {
                                        getArg(p, 1) = tuples[k].hidx;
-                                       pushArgument(mb, p, tuples[k].tidx);
+                                       p= pushArgument(mb, p, tuples[k].tidx);
                                } else
                                        goto STrepack;
                        } else
@@ -204,8 +204,8 @@
                                setModuleId(p, NULL);
                                setFunctionId(p, NULL);
                                p->retc = p->argc = 2;
-                               pushOid(mb, p,0);
-                               pushArgument(mb, p, tuples[k].tidx);
+                               p= pushOid(mb, p,0);
+                               p= pushArgument(mb, p, tuples[k].tidx);
                                k = STnewTuple(mb, getArg(p, 0), &top, tuples);
                                tuples[k].packed = 0;
                                p->argv[0] = tuples[k].hidx;
@@ -221,8 +221,8 @@
                                setModuleId(p, NULL);
                                setFunctionId(p, NULL);
                                p->retc = p->argc = 2;
-                               pushArgument(mb, p, tuples[k].hidx);
-                               pushOid(mb, p,0);
+                               p= pushArgument(mb, p, tuples[k].hidx);
+                               p= pushOid(mb, p,0);
                                k = STnewTuple(mb, getArg(p, 0), &top, tuples);
                                tuples[k].packed = 0;
                                p->argv[0] = tuples[k].hidx;
@@ -245,11 +245,11 @@
                                                setFunctionId(p, NULL);
                                                STnewTuple(mb, getArg(p, 0), 
&top, tuples);
                                                p->argc = 0;
-                                               pushReturn(mb, p, tuples[top - 
1].hidx);
-                                               pushReturn(mb, p, tuples[top - 
1].tidx);
+                                               p= pushReturn(mb, p, tuples[top 
- 1].hidx);
+                                               p= pushReturn(mb, p, tuples[top 
- 1].tidx);
                                                p->retc = 2;
-                                               pushArgument(mb, p, 
tuples[k1].hidx);
-                                               pushArgument(mb, p, 
tuples[k2].tidx);
+                                               p= pushArgument(mb, p, 
tuples[k1].hidx);
+                                               p= pushArgument(mb, p, 
tuples[k2].tidx);
                                                p->token = ASSIGNsymbol;
                                                p->fcn = NULL;
                                        }
@@ -279,7 +279,7 @@
                                setFunctionId(p, NULL);
                                p->argc = 1;
                                p->token = ASSIGNsymbol;
-                               pushLng(mb,p,1);
+                               p= pushLng(mb,p,1);
                        } else {
                        STrepack:
                                /* pack everything you need */

Index: opt_macro.mx
===================================================================
RCS file: /cvsroot/monetdb/MonetDB5/src/optimizer/opt_macro.mx,v
retrieving revision 1.19
retrieving revision 1.19.2.1
diff -u -d -r1.19 -r1.19.2.1
--- opt_macro.mx        12 Jun 2007 17:21:56 -0000      1.19
+++ opt_macro.mx        17 Aug 2007 15:38:07 -0000      1.19.2.1
@@ -282,8 +282,8 @@
        for (n = 0; n < p->retc; n++) {
                q = ns[k++] = newInstruction(NULL, ASSIGNsymbol);
                q->argc = q->retc = 0;
-               pushArgument(mb, q, getArg(p, n)); q->retc++;
-               pushNil(mb, q, getArgType(mb, p, n));
+               q= pushArgument(mb, q, getArg(p, n)); q->retc++;
+               q= pushNil(mb, q, getArgType(mb, p, n));
        }
 
        /* copying stops at the first return statement */
@@ -407,6 +407,7 @@
                q = getInstrPtr(mc, k);
                for (j = 0; j < p->argc; j++)
                        cvar[ctop++] = getArg(q, j);
+               assert(ctop < mc->vtop *MAXARG);
 
                for (j = 0; j < p->argc; j++)
                        mvar[mtop++] = getArg(p, j);

Index: opt_garbageCollector.mx
===================================================================
RCS file: /cvsroot/monetdb/MonetDB5/src/optimizer/opt_garbageCollector.mx,v
retrieving revision 1.25
retrieving revision 1.25.2.1
diff -u -d -r1.25 -r1.25.2.1
--- opt_garbageCollector.mx     16 Jul 2007 10:52:40 -0000      1.25
+++ opt_garbageCollector.mx     17 Aug 2007 15:38:02 -0000      1.25.2.1
@@ -107,7 +107,7 @@
 @= releaseBAT
 {      q= newInstruction(NULL,ASSIGNsymbol);
        getArg(q,0) = getArg(p,j);
-       pushNil(mb,q, TYPE_bat);
+       q= pushNil(mb,q, TYPE_bat);
        pushInstruction(mb,q);
        typeChecker(s,mb,q,TRUE);
 }
@@ -116,7 +116,7 @@
        q= newInstruction(NULL,ASSIGNsymbol);
        setModuleId(q,batRef);
        setFunctionId(q,putName("flush",5));
-       pushArgument(mb,q,getArg(p,j));
+       q= pushArgument(mb,q,getArg(p,j));
        getArg(q,0) = newTmpVariable(mb,TYPE_any);
        pushInstruction(mb,q);
        typeChecker(s,mb,q,TRUE);
@@ -140,7 +140,7 @@
        q= newInstruction(NULL,ASSIGNsymbol);
        setModuleId(q,batRef);
        setFunctionId(q,putName("reduce",6));
-       pushArgument(mb,q,getArg(p,j));
+       q= pushArgument(mb,q,getArg(p,j));
        getArg(q,0) = getArg(p,j);
        pushInstruction(mb,q);
        typeChecker(s,mb,q,TRUE);

Index: opt_joinselect.mx
===================================================================
RCS file: /cvsroot/monetdb/MonetDB5/src/optimizer/opt_joinselect.mx,v
retrieving revision 1.11
retrieving revision 1.11.2.1
diff -u -d -r1.11 -r1.11.2.1
--- opt_joinselect.mx   16 Jul 2007 21:54:53 -0000      1.11
+++ opt_joinselect.mx   17 Aug 2007 15:38:05 -0000      1.11.2.1
@@ -164,8 +164,8 @@
                ps+=xx; 
                qs = old[*(int*)ps];
                setModuleId(qs, putName("crackers", 8));
-                       setFunctionId(qs, putName("joinuselect", 11));
-                       pushArgument(mb, qs, getArg(p, 0));
+               setFunctionId(qs, putName("joinuselect", 11));
+               qs= pushArgument(mb, qs, getArg(p, 0));
                pushInstruction(mb, qs);
                i++;    
        }
@@ -180,8 +180,8 @@
                pi+=xx; 
                qi = old[*(int*)pi];
                setModuleId(qi, putName("crackers", 8));
-                       setFunctionId(qi, putName("joinuselect", 11));
-                       pushArgument(mb, qi, getArg(p, 0));
+               setFunctionId(qi, putName("joinuselect", 11));
+               qi= pushArgument(mb, qi, getArg(p, 0));
                pushInstruction(mb, qi);
                i++;    
        }
@@ -191,8 +191,8 @@
        getArg(r,0) = newTmpVariable(mb, newBatType(TYPE_void, TYPE_void));
        setModuleId(r, putName("algebra", 7));
         setFunctionId(r, putName("kunion", 6));        
-        pushArgument(mb, r, getArg(qs, 0));
-        pushArgument(mb, r, getArg(qi, 0));
+        r= pushArgument(mb, r, getArg(qs, 0));
+        r= pushArgument(mb, r, getArg(qi, 0));
        pushInstruction(mb, r);
 
 
@@ -200,7 +200,7 @@
         getArg(d,0) = newTmpVariable(mb, newBatType(TYPE_oid, TYPE_void));
         setModuleId(d, putName("bat", 3));
         setFunctionId(d, putName("reverse", 7));
-        pushArgument(mb, d, getArg(r, 0));
+        d= pushArgument(mb, d, getArg(r, 0));
         pushInstruction(mb, d);
        r=d;
 */
@@ -213,9 +213,9 @@
                k = newInstruction(mb,ASSIGNsymbol);
                getArg(k,0) = newTmpVariable(mb, newBatType(TYPE_oid, 
TYPE_void));
                setModuleId(k, putName("algebra", 7));
-                       setFunctionId(k, putName("kdifference", 11));
-                       pushArgument(mb, k, getArg(r, 0));
-                       pushArgument(mb, k, getArg(d, 1));
+               setFunctionId(k, putName("kdifference", 11));
+               k= pushArgument(mb, k, getArg(r, 0));
+               k= pushArgument(mb, k, getArg(d, 1));
                pushInstruction(mb, k);
                pu+=xx;
                r = k; 
@@ -232,8 +232,8 @@
                pu+=xx; 
                qu = old[*(int*)pu];
                setModuleId(qu, putName("crackers", 8));
-                       setFunctionId(qu, putName("joinuselect", 11));
-                       pushArgument(mb, qu, getArg(p, 0));
+               setFunctionId(qu, putName("joinuselect", 11));
+               qu= pushArgument(mb, qu, getArg(p, 0));
                pushInstruction(mb, qu);
                i++;    
        }
@@ -242,9 +242,9 @@
        p = newInstruction(mb,ASSIGNsymbol);
        getArg(p,0) = newTmpVariable(mb, getArgType(mb,r,0));
        setModuleId(p, putName("algebra", 7));
-        setFunctionId(p, putName("kunion", 6));        
-        pushArgument(mb, p, getArg(r, 0));
-        pushArgument(mb, p, getArg(qu, 0));
+       setFunctionId(p, putName("kunion", 6));        
+       p= pushArgument(mb, p, getArg(r, 0));
+       p= pushArgument(mb, p, getArg(qu, 0));
        pushInstruction(mb, p);
 
        /*remove any bun that is deleted*/
@@ -256,9 +256,9 @@
                k = newInstruction(mb,ASSIGNsymbol);
                getArg(k,0) = newTmpVariable(mb, newBatType(TYPE_oid, 
TYPE_void));
                setModuleId(k, putName("algebra", 7));
-                       setFunctionId(k, putName("kdifference", 11));
-                       pushArgument(mb, k, getArg(p, 0));
-                       pushArgument(mb, k, getArg(qd, 0));
+               setFunctionId(k, putName("kdifference", 11));
+               k= pushArgument(mb, k, getArg(p, 0));
+               k= pushArgument(mb, k, getArg(qd, 0));
                pushInstruction(mb, k);
                pd += xx;
                p = k; 

Index: opt_remap.mx
===================================================================
RCS file: /cvsroot/monetdb/MonetDB5/src/optimizer/opt_remap.mx,v
retrieving revision 1.12
retrieving revision 1.12.2.1
diff -u -d -r1.12 -r1.12.2.1
--- opt_remap.mx        8 Aug 2007 12:58:26 -0000       1.12
+++ opt_remap.mx        17 Aug 2007 15:38:19 -0000      1.12.2.1
@@ -101,7 +101,7 @@
                getArg(p,0)= getArg(pci,i);
        p->retc= p->argc= pci->retc;
        for(i= pci->retc+2; i<pci->argc; i++)
-               pushArgument(mb,p,getArg(pci,i));
+               p= pushArgument(mb,p,getArg(pci,i));
 #ifdef DEBUG_OPT_REMAP
        printInstruction(GDKout,mb,p,LIST_MAL_ALL);
 #endif

Index: opt_partitions.mx
===================================================================
RCS file: /cvsroot/monetdb/MonetDB5/src/optimizer/opt_partitions.mx,v
retrieving revision 1.34
retrieving revision 1.34.2.1
diff -u -d -r1.34 -r1.34.2.1
--- opt_partitions.mx   8 Aug 2007 12:58:26 -0000       1.34
+++ opt_partitions.mx   17 Aug 2007 15:38:13 -0000      1.34.2.1
@@ -90,6 +90,7 @@
 An operation should be replace by an iterator if
 any of its arguments is partitioned. This is detected
 by an alias function, which is set to the alternative.
+
 @c
 static int 
 OPTuseIterator(MalBlkPtr mb, InstrPtr p, int *alias){
@@ -101,13 +102,11 @@
 }
 
 static void
-OPTbarrierList(MalBlkPtr mb, InstrPtr p, int *alias, int offset){
+OPTbarrierList(MalBlkPtr mb, InstrPtr p, int *alias){
        int j;
        InstrPtr q;
        /* dump the iterators */
-       if( p->retc+offset >= p->argc)
-               offset= 0;
-       for(j=p->retc+offset; j<p->argc; j++)
+       for(j=p->retc; j<p->argc; j++)
        if( isaBatType(getArgType(mb,p,j))  && alias[getArg(p,j)]>0){
                q = newStmt(mb, bpmRef, "newIterator");
                q->barrier= BARRIERsymbol;
@@ -115,7 +114,7 @@
 #ifdef DEBUG_OPT_PARTITIONED
                printf("barrier %d ->%d\n",getArg(p,j), alias[getArg(p,j)]);
 #endif
-               pushArgument(mb, q, 
+               q= pushArgument(mb, q, 
                        (alias[getArg(p,j)]?alias[getArg(p,j)]:getArg(p,j)) );
        }
 }
@@ -129,22 +128,20 @@
        return cnt;
 }
 static void
-OPTredoList(MalBlkPtr mb, InstrPtr p, int *alias, int offset){
+OPTredoList(MalBlkPtr mb, InstrPtr p, int *alias){
        int j;
        InstrPtr q;
-       if( p->retc+offset >= p->argc)
-               offset= 0;
        /* finalize iterator structure */
-       for(j=p->argc-1; j>= p->retc+offset; j--)
+       for(j=p->argc-1; j>= p->retc; j--)
        if( isaBatType(getArgType(mb,p,j))  && alias[getArg(p,j)]>0){
                q = newStmt(mb, bpmRef, "hasMoreElements");
                q->barrier= REDOsymbol;
                q->argv[0]= getArg(p,j);
-               pushArgument(mb, q, 
+               q= pushArgument(mb, q, 
                        (alias[getArg(p,j)]?alias[getArg(p,j)]:getArg(p,j)) );
        }
 
-       for(j=p->argc-1; j>=p->retc+offset; j--)
+       for(j=p->argc-1; j>=p->retc; j--)
        if( isaBatType(getArgType(mb,p,j))  && alias[getArg(p,j)]>0){
                q = newInstruction(mb,EXITsymbol);
                q->barrier= EXITsymbol;
@@ -164,8 +161,8 @@
 #endif
        anchor= q = newStmt(mb,"bpm","new");
        tpe= getArgType(mb,p,0);
-       pushArgument(mb,anchor,newTypeVariable(mb,getHeadType(tpe)));
-       pushArgument(mb,anchor,newTypeVariable(mb,getTailType(tpe)));
+       anchor= pushArgument(mb,anchor,newTypeVariable(mb,getHeadType(tpe)));
+       anchor= pushArgument(mb,anchor,newTypeVariable(mb,getTailType(tpe)));
 #ifdef DEBUG_OPT_PARTITIONED
        printf("alias[%d]=%d\n",getArg(p,0),getArg(q,0));
 #endif
@@ -173,17 +170,17 @@
        garbage[getArg(q,0)]= 1;
 
        /* dump the iterators */
-       OPTbarrierList(mb,p,alias, p->argc>3 && getModuleId(p)==batcalcRef);
+       OPTbarrierList(mb,p,alias);
 
        /* run the partitioned operation */
        pushInstruction(mb,p);
 
        /* save the temporary partitions */
        q = newStmt(mb,"bpm","addPartition");
-       pushArgument(mb,q,getArg(anchor,0));
-       pushArgument(mb,q,getArg(p,0));
+       q= pushArgument(mb,q,getArg(anchor,0));
+       q= pushArgument(mb,q,getArg(p,0));
 
-       OPTredoList(mb,p,alias, p->argc>3 && getModuleId(p)==batcalcRef);
+       OPTredoList(mb,p,alias);
 }
 @-
 Be aware the the SQL produces sql.bind(-,-,-,0) and sql.bind(-,-,-,3)
@@ -195,8 +192,27 @@
        v= &getConstant(mb,getArg(p,4));
        return v->val.ival>0;
 }
[EMAIL PROTECTED]
+Garbage collection of partitioned BATs is tricky when the
+sql.bind() operations are nested inside BARRIER-EXIT blocks.
+For the time being we ignore plans that rely on this.
+The test below is rather coarse grained.
[EMAIL PROTECTED]
+static int
+OPTisSimple(MalBlkPtr mb){
+       int i,nested=0;
+       InstrPtr p;
 
-static void
+       for(i=0; i<mb->stop; i++){
+               p= getInstrPtr(mb,i);
+               nested +=  p->barrier== BARRIERsymbol || p->barrier== 
CATCHsymbol;
+               if( getModuleId(p)== sqlRef && getFunctionId(p)== bindRef && 
nested)
+                       return FALSE;
+       }
+       return TRUE;
+}
+
+static int
 OPTnaiveIterations(MalBlkPtr mb){
        int i,j,k,limit,vlimit= mb->vsize + mb->ssize*4; /* expanded*/
        InstrPtr *old,p=0,q;
@@ -204,6 +220,8 @@
        int *garbage= (int*) alloca(sizeof(int)*vlimit);
        /* char name[1024];*/
 
+       if( !OPTisSimple(mb))
+               return 0;
        memset((char*) alias, 0, sizeof(int)*vlimit);
        memset((char*) garbage, 0, sizeof(int)*vlimit);
 
@@ -230,12 +248,12 @@
 #endif
 /*
                        snprintf(name,1024,"%s_%s_%s",
-                               getConstant(mb, getArg(p,1)).val.sval,
-                               getConstant(mb, getArg(p,2)).val.sval,
-                               getConstant(mb, getArg(p,3)).val.sval );
-                       pushStr(mb, q,name);
+                       getConstant(mb, getArg(p,1)).val.sval,
+                       getConstant(mb, getArg(p,2)).val.sval,
+                       getConstant(mb, getArg(p,3)).val.sval );
+                       q= pushStr(mb, q,name);
 */
-                       pushArgument(mb,q,getArg(p,0));
+                       q= pushArgument(mb,q,getArg(p,0));
                        garbage[getArg(q,0)]=1;
                        /* printf("unfold  %d %d\n",getArg(q,0),getArg(q,1));*/
                        continue;
@@ -244,8 +262,18 @@
 The instruction should be broken into an interator
 if any of its arguments is already partitioned.
 The exceptions are 1) there exists a fast direct
-implementation in the bpm for it, 2) it is part
-of the sql repository
+implementation in the bpm for it, 2) it has
+side-effects, 3) it involves a blocking instruction
[EMAIL PROTECTED]
+Caveats.
+The batcalc module uses a merge-like operation
+to scan through multiple BATs. A naive iterator expansion approach would
+lead to a cartesian product. This means the opt_accumulator
+can not be used in this context or a complex backwards
+analysis is needed. At this stage we consider them blocking,
+later a correct partitioned solution can be made.
+
+The bakeIterator currently supports a single result variable.
 @c
                if( OPTuseIterator(mb, p, alias) == 0 )
                        goto jump2;
@@ -258,21 +286,23 @@
                }
 
                if( !hasSideEffects(p,TRUE) && p->retc == 1 &&
-                       getModuleId(p)!= aggrRef &&
-                   getModuleId(p)!= sqlRef &&
+                       !isBlocking(p) &&
+                       getModuleId(p) != batcalcRef &&
                        OPTbarrierListCnt(mb, p, alias) > 0
                ){
                        OPTbakeIterator(mb, p, alias, garbage);
                        continue;
                }
[EMAIL PROTECTED]
+At this stage we first undo the partitioning.
[EMAIL PROTECTED]
        jump2:
                for(j= p->retc; j<p->argc; j++)
                if( (alias[getArg(p,j)] && alias[getArg(p,j)]!= getArg(p,j)) ){
                        q = newStmt(mb,"bpm","fold");
                        q->argv[0]= getArg(p,j);
-                       pushArgument(mb,q,alias[getArg(p,j)]);
+                       q= pushArgument(mb,q,alias[getArg(p,j)]);
                        garbage[getArg(q,1)]=0;
-                       /* printf("fold %d  %d\n",getArg(q,0),getArg(q,1));*/
                        alias[getArg(p,j)]= 0;
 #ifdef DEBUG_OPT_PARTITIONED
                        printf("alias[%d]=0\n",getArg(p,j));
@@ -296,7 +326,7 @@
        if( garbage[k]){
                q=newStmt(mb,bpmRef,"garbage");
                setVarType(mb,getArg(q,0),TYPE_void);
-               pushArgument(mb,q,k);
+               q= pushArgument(mb,q,k);
        }
        q= newAssignment(mb);
        getArg(q,0)= j;
@@ -310,23 +340,19 @@
        if( garbage[k]){
                q=newStmt(mb,bpmRef,"garbage");
                setVarType(mb,getArg(q,0),TYPE_void);
-               pushArgument(mb,q,k);
+               q= pushArgument(mb,q,k);
        }
        for(;i< limit; i++)
                pushInstruction(mb,old[i]);
+       return 1;
 }
 
 static int
 OPTpartitionsImplementation(MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
 {
-       OPTnaiveIterations(mb);
-#ifdef DEBUG_OPT_PARTITIONED
-       stream_printf(GDKout,"partition optimizer Strategy\n");
-       printFunction(GDKout,mb,0);
-#endif
        (void) stk;
        (void) pci;
-       return 1;
+       return OPTnaiveIterations(mb);
 }
 
 @-

Index: opt_multiplex.mx
===================================================================
RCS file: /cvsroot/monetdb/MonetDB5/src/optimizer/opt_multiplex.mx,v
retrieving revision 1.25
retrieving revision 1.25.2.1
diff -u -d -r1.25 -r1.25.2.1
--- opt_multiplex.mx    8 Aug 2007 12:58:26 -0000       1.25
+++ opt_multiplex.mx    17 Aug 2007 15:38:11 -0000      1.25.2.1
@@ -119,11 +119,12 @@
        int i = 2, k, mloop, resB, iter = 0, cr;
        int hvar, tvar;
        str mod, fcn;
-       int alias[MAXARG];
+       int *alias;
        InstrPtr q;
        int ht, tt;
 
        (void) stk;
+       alias= (int*) alloca(sizeof(int) * pci->maxarg);
        mod = VALget(&getVar(mb, getArg(pci, 1))->value);
        mod = putName(mod,strlen(mod));
        fcn = VALget(&getVar(mb, getArg(pci, 2))->value);
@@ -172,10 +173,10 @@
        q->barrier = BARRIERsymbol;
        getArg(q, 0) = mloop = newTmpVariable(mb, TYPE_lng);
        hvar = newTmpVariable(mb, TYPE_any);
-       pushReturn(mb, q, hvar);
+       q= pushReturn(mb, q, hvar);
        tvar = newTmpVariable(mb, TYPE_any);
-       pushReturn(mb, q, tvar);
-       pushArgument(mb, q, iter);
+       q= pushReturn(mb, q, tvar);
+       q= pushArgument(mb, q, iter);
 
        /* $1:= bat.find(Ai,h) or constant */
        alias[i] = tvar;
@@ -185,8 +186,8 @@
                        q = newFcnCall(mb, algebraRef, "find");
                        alias[i] = newTmpVariable(mb, 
getTailType(getArgType(mb, pci, i)));
                        getArg(q, 0) = alias[i];
-                       pushArgument(mb, q, getArg(pci, i));
-                       pushArgument(mb, q, hvar);
+                       q= pushArgument(mb, q, getArg(pci, i));
+                       q= pushArgument(mb, q, hvar);
                }
 
        /* cr:= mod.CMD($1,...,$n); */
@@ -195,7 +196,7 @@
 
        for (i = 3; i < pci->argc; i++)
                if (isaBatType(getArgType(mb, pci, i))) {
-                       pushArgument(mb, q, alias[i]);
+                       q= pushArgument(mb, q, alias[i]);
                } else {
                        q = pushArgument(mb, q, getArg(pci, i));
                }
@@ -204,23 +205,23 @@
           not append(resB, cr); the head type (oid) may dynamically change */
        
        q = newFcnCall(mb, batRef, insertRef);
-       pushArgument(mb, q, resB);
-       pushArgument(mb, q, hvar);
-       pushArgument(mb, q, cr);
+       q= pushArgument(mb, q, resB);
+       q= pushArgument(mb, q, hvar);
+       q= pushArgument(mb, q, cr);
 
 /* redo (mloop,h,r):= hasMoreElements(refBat); */
        q = newFcnCall(mb, batRef, "hasMoreElements");
        q->barrier = REDOsymbol;
        getArg(q, 0) = mloop;
-       pushReturn(mb, q, hvar);
-       pushReturn(mb, q, tvar);
-       pushArgument(mb, q, iter);
+       q= pushReturn(mb, q, hvar);
+       q= pushReturn(mb, q, tvar);
+       q= pushArgument(mb, q, iter);
 
 /* catch   MALException and ignore them  and replace value with nil */
        q = newInstruction(mb,CATCHsymbol);
        k = newVariable(mb, GDKstrdup(exceptionToString(MAL)), TYPE_str);
        getVar(mb,k)->isudftype = 1;
-       pushReturn(mb, q, k);
+       q= pushReturn(mb, q, k);
        pushInstruction(mb, q);
 
 /* redo (mloop,h,r):= hasMoreElements(refBat); */
@@ -228,24 +229,24 @@
        q = newFcnCall(mb, batRef, "hasMoreElements");
        q->barrier = REDOsymbol;
        getArg(q, 0) = mloop;
-       pushReturn(mb, q, hvar);
-       pushReturn(mb, q, tvar);
-       pushArgument(mb, q, iter);
+       q= pushReturn(mb, q, hvar);
+       q= pushReturn(mb, q, tvar);
+       q= pushArgument(mb, q, iter);
 
 /* finalized the catch block */
        q= newInstruction(mb,EXITsymbol);
-       pushReturn(mb,q,k);
+       q= pushReturn(mb,q,k);
        pushInstruction(mb,q);
 
        q = newAssignment(mb);
        q->barrier = EXITsymbol;
        getArg(q, 0) = mloop;
-       pushReturn(mb, q, hvar);
-       pushReturn(mb, q, tvar);
+       q= pushReturn(mb, q, hvar);
+       q= pushReturn(mb, q, tvar);
 
        q = newAssignment(mb);
        getArg(q, 0) = getArg(pci, 0);
-       pushArgument(mb, q, resB);
+       q= pushArgument(mb, q, resB);
        return MAL_SUCCEED;
 }
 

Index: opt_mergetable.mx
===================================================================
RCS file: /cvsroot/monetdb/MonetDB5/src/optimizer/opt_mergetable.mx,v
retrieving revision 1.12
retrieving revision 1.12.2.1
diff -u -d -r1.12 -r1.12.2.1
--- opt_mergetable.mx   8 Aug 2007 12:58:26 -0000       1.12
+++ opt_mergetable.mx   17 Aug 2007 15:38:09 -0000      1.12.2.1
@@ -19,18 +19,18 @@
 @- Merge Tables 
 A merge association table (MAT) descriptor defines an ordered collection 
 of type compatible BATs, whose union represents a single (virtual) BAT.
-The MAT may represent a partitioned BAT (see BPM), but could also be an 
arbitrary
-collection of temporary BATs within a program fragment.
+The MAT may represent a partitioned BAT (see BPM), but could also 
+be an arbitrary collection of temporary BATs within a program fragment.
 
 The MAT definition lives within the scope of a single block.
 The MAT optimizer simply expands the plan to
-deal with its components on an individual basis.
+deal with its components on an instruction basis.
 Only when a blocking operator is encounted, the underlying BAT is materialized.
 
 The MAT object can not be passed as an argument to any function
 without first being materialized. Simply because
 the MAT is not known by the type system and none of the lower
-level operations is currently aware of their existence.
+level operations is aware of their existence.
 
 In the first approach of the MAT optimizer we assume that the 
 last BAT in the MAT sequence is used as an accumulator.
@@ -141,19 +141,35 @@
 }
 @-
 Packing the BATs into a single one is handled here
+Note that MATs can be defined recursively.
 @c
-InstrPtr MATpackAll(MalBlkPtr mb, InstrPtr  r, int m, InstrPtr *mat, int 
*mvar, int mtop){
-       int l,k;
+static InstrPtr 
+MATpackAll(MalBlkPtr mb, InstrPtr  r, int m, InstrPtr *mat, int *mvar, int 
*mtop){
+       int j,l,k;
+       if( r== NULL){
+               r = newInstruction(mb, ASSIGNsymbol);
+               setModuleId(r,matRef);
+               setFunctionId(r,packRef);
+               getArg(r,0)= getArg(mat[m],0);
+       }
        for(l=mat[m]->retc; l< mat[m]->argc; l++){
-               k= isMATalias( getArg(mat[m],l),mvar,mtop);
+               k= isMATalias( getArg(mat[m],l),mvar,*mtop);
                if( k< 0)
                        r= pushArgument(mb,r, getArg(mat[m],l));
                else
                        r= MATpackAll(mb,r, k, mat,mvar,mtop);
        }
+       for(j=m; j < *mtop-1; j++){
+               mat[j]= mat[j+1];
+               mvar[j]= mvar[j+1];
+       }
+       *mtop = *mtop-1; 
+       pushInstruction(mb,r);
        return r;
 }
-void MATcollect(int m, InstrPtr *mat, int *mvar, int mtop, int *newvar, int 
*n){
+
+static void 
+MATcollect(int m, InstrPtr *mat, int *mvar, int mtop, int *newvar, int *n){
        int i,j,k;
        for(i= mat[m]->retc; i<mat[m]->argc; i++)
                if( (j= isMATalias( getArg(mat[m],i), mvar, mtop)) >= 0){
@@ -163,10 +179,20 @@
                } else { newvar[*n]=  getArg(mat[m],i); *n = *n+1;}
 }
 
-int
+static int
+MATcount(InstrPtr p, int *mvar, int mtop){
+       int j,cnt=0;
+       for(j=p->retc; j<p->argc; j++)
+       if(  isMATalias(getArg(p,j),mvar,mtop) >= 0) {
+               cnt++;
+       }
+       return cnt;
+}
+
+static int
 OPTmergetableImplementation(MalBlkPtr mb, MalStkPtr stk, InstrPtr p){
        InstrPtr *old=0, q,r, mat[256];
-       int oldtop,i,j,k,m,mtop=0, mvar[256],tpe;
+       int oldtop,fm,i,k,m,mtop=0, mvar[256],tpe;
        int size,match,actions=0;
 #ifdef DEBUG_OPT_MERGETABLE
        stream_printf(GDKout,"Start MAT optimizer\n");
@@ -192,162 +218,145 @@
                        continue;
                }
 @-
-A sql.bind() operation is trapped to inspect the partition catalog
-for definitions.
+A sql.bind() operation should be trapped to inspect the partition catalog
+for definitions. For now we simulate a partitioned table, comprised
+of the original base and an empty one.
 @c
                if( (getModuleId(p)== sqlRef && getFunctionId(p)==bindRef)){
-                       int i;
+                       InstrPtr p2;
+                       int v= getArg(p,0);
+
+                       freezeVarType(mb,getArg(p,0));
+#ifdef DEBUG_OPT_MERGETABLE
+                       stream_printf(GDKout,"found bind\n");
+                       printInstruction(GDKout,mb,p,0);
+#endif
                        pushInstruction(mb,p);
-                       i= mb->stop;
-                       BPMexpand(mb,getArg(p,0));
+
+                       p2= newStmt(mb,batRef,newRef);
+                       setVarType(mb,getArg(p2,0),getVarType(mb,v));
+                       p2= pushArgument(mb,p2,v);
+
                        q= newStmt(mb,matRef,newRef);
-                       getArg(q,0)= getArg(p,0);
-                       for(; i<mb->stop; i++){
-                               mat[mtop]= getInstrPtr(mb,i);
-                               mvar[mtop] = getArg(mat[mtop],0);
-                               pushArgument(mb,q,mvar[mtop]);
-                               setVarType(mb,getArg(q,0), 
getVarType(mb,getArg(mat[mtop],0)));
-                               mtop++;
-                       }
+                       setVarType(mb,getArg(q,0),getVarType(mb,v));
+
+                       getArg(p,0)= getArg(q,0);
+                       getArg(p2,1)= getArg(p,0);
+                       getArg(q,0)= v;
+                       q= pushArgument(mb,q,getArg(p,0));
+                       q= pushArgument(mb,q,getArg(p2,0));
+#ifdef DEBUG_OPT_MERGETABLE
+                       stream_printf(GDKout,"new block\n");
+                       printInstruction(GDKout,mb,p,0);
+                       printInstruction(GDKout,mb,p2,0);
+                       printInstruction(GDKout,mb,q,0);
+#endif
+                       mat[mtop]= q;
+                       mvar[mtop] = v;
+                       mtop++;
                        actions++;
                        continue;
                }
 @-
-If the instruction does not contain MAT references it can be added.
+If the instruction does not contain MAT references it can simply be added.
 Otherwise we have to decide on either packing them or replacement.
 @c
-               match=0;
-               for(j=p->retc; j<p->argc; j++)
-               if(  isMATalias(getArg(p,j),mvar,mtop) >= 0) {
-                       m=i; match++;
-               }
+               match= MATcount(p,mvar,mtop);
                if( match== 0 ){
                        pushInstruction(mb,p);
+#ifdef DEBUG_OPT_MERGETABLE
+                       stream_printf(GDKout,"simply move\n");
+                       printInstruction(GDKout,mb,p,0);
+#endif
                        continue;
                }
 @-
-Pack all but one MAT argument together.
-Remove their definitions afterwards.
+Pack MAT arguments, except one, to limit plan explosion.
+The preferred partitioned one is the first argment as it
+often reflects a base table.
 @c
-               if( match>1 ){
-                       for( k= p->retc; k<p->argc && match>1; k++, match--)
-                       if(     (m=isMATalias(getArg(p,k),mvar,mtop)) >= 0){
+               for( fm= p->argc-1; fm>p->retc ; fm--)
+               if(     (m=isMATalias(getArg(p,fm),mvar,mtop)) >= 0)
+                       break;
+
+               for( k=fm-1; match > 1 && k >= p->retc ; k--)
+               if(     (m=isMATalias(getArg(p,k),mvar,mtop)) >= 0){
 #ifdef DEBUG_OPT_MERGETABLE
-                               stream_printf(GDKout,"Dependency resolution 
k=%d\n",k);
-                               printInstruction(GDKout,mb,p,0);
+                       stream_printf(GDKout,"Pack k=%d\n",k);
+                       printInstruction(GDKout,mb,p,0);
 #endif
-                               r = newInstruction(mb, ASSIGNsymbol);
-                               setModuleId(r,matRef);
-                               setFunctionId(r,packRef);
-                               getArg(r,0)= getArg(mat[m],0);
-                               r= MATpackAll(mb, r, m, mat, mvar, mtop);
-                               pushInstruction(mb,r);
-                               getArg(p,k)= getArg(r,0);
-                               for(j=m; j<mtop-1; j++){
-                                       mat[j]= mat[j+1];
-                                       mvar[j]= mvar[j+1];
-                               }
-                               mtop--; 
-                       }
-                       if( match== 0)
-                               pushInstruction(mb,p);
+                       r= MATpackAll(mb, NULL, m, mat, mvar, &mtop);
+                       getArg(p,k)= getArg(r,0);
+                       match--;
                }
 @-
 Look at the depth of the MAT definition to limit the explosion.
[EMAIL PROTECTED]
-               if( MATdepth(m,mat,mvar,mtop)>1){
-               }
-Left with an instruction with at most one MAT we can replace it.
+               
+Left with an instruction with one MAT we can replace it.
 Not all instructions can be replaced by the sequence. We have to
 group them and check for them individually.
-At first we look for the first argument. Others are dealt with
-through the default case.
 @c
-               if( isFragmentGroup(p) && (m=isMATalias(getArg(p,1),mvar,mtop)) 
>= 0){
-                       r = newInstruction(mb, ASSIGNsymbol);
-                       setModuleId(r,matRef);
-                       setFunctionId(r,newRef);
-                       getArg(r,0)= getArg(p,0);
-                       tpe= getArgType(mb,p,0);
+               if( isFragmentGroup(p) && 
+                       (m=isMATalias(getArg(p,fm),mvar,mtop)) >= 0){
+#ifdef DEBUG_OPT_MERGETABLE
+                       stream_printf(GDKout,"isFragment resolution 
fm=%d\n",fm);
+                       printInstruction(GDKout,mb,p,0);
+#endif
+                       if( isaBatType(getVarType(mb,getArg(p,0))) ){
+                               r = newInstruction(mb, ASSIGNsymbol);
+                               setModuleId(r,matRef);
+                               setFunctionId(r,newRef);
+                               getArg(r,0)= getArg(p,0);
+                               tpe= getArgType(mb,p,0);
+                       } else {
+                                       tpe= TYPE_any;
+                                       r=0;
+                       }
 
                        for(k=1; k< mat[m]->argc; k++){
-                               if( k<mat[m]->argc-1 &&  (
-                                       getFunctionId(p)== insertRef ||
-                                       getFunctionId(p)== appendRef ))
-                                       continue;
                                q= copyInstruction(p);
-                               getArg(q,1) = getArg(mat[m],k);
+                               getArg(q,fm) = getArg(mat[m],k);
                                getArg(q,0) = newTmpVariable(mb, tpe);
                                pushInstruction(mb,q);
-                               r= pushArgument(mb,r,getArg(q,0));
-                       }
-                       if( getFunctionId(p)== insertRef ||
-                               getFunctionId(p)== appendRef ||
-                               getFunctionId(p)== deleteRef )
-                                       match=1;
-                       else {
-                               match =0;
-                               for(j=0;j<mtop; j++)
-                                       if( mvar[j]== getArg(p,0)) match++;
-                               if( match == 0){
-                                       mvar[mtop] = getArg(p,0);
-                                       mat[mtop++]= r;
-                                       pushInstruction(mb,r);
-                               }
+                               if( r)
+                                       r= pushArgument(mb,r,getArg(q,0));
                        }
-                       if( match)
-                               freeInstruction(r);
                        actions++;
+                       if(r){
+                               mat[mtop]= r;
+                               mvar[mtop] = getArg(r,0);
+                               mtop++;
+                               pushInstruction(mb,r);
+                       }
+#ifdef DEBUG_OPT_MERGETABLE
+                       stream_printf(GDKout,"Fragment result \n");
+                       if(r) printInstruction(GDKout,mb,r,0);
+#endif
                        continue;
                } 
 @-
 The insertions are sent to the last component of the MAT.
 Selection of the proper componetn based on range descriptors will follow.
 @c
-               if(getModuleId(p)== batRef && getFunctionId(p)== insertRef &&
-               (m=isMATalias(getArg(p,1),mvar,mtop)) >= 0){
+               if(     getModuleId(p)== batRef && getFunctionId(p)== insertRef 
&&
+                       (m=isMATalias(getArg(p,1),mvar,mtop)) >= 0){
                        getArg(p,1) = getArg(mat[m],mat[m]->argc-1);
                        pushInstruction(mb,p);
                        continue;
                } 
 @-
-The MAT argument could also occur as the second one.
[EMAIL PROTECTED]
-               if( getModuleId(p)==algebraRef && getFunctionId(p)== joinRef &&
-                       (m=isMATalias(getArg(p,2),mvar,mtop)) >= 0){
-                       r = newInstruction(mb, ASSIGNsymbol);
-                       setModuleId(r,matRef);
-                       setFunctionId(r,newRef);
-                       getArg(r,0)= getArg(p,0);
-                       tpe= getArgType(mb,p,0);
-
-                       for(k=1; k< mat[m]->argc; k++){
-                               q= copyInstruction(p);
-                               getArg(q,2) = getArg(mat[m],k);
-                               getArg(q,0) = newTmpVariable(mb, tpe);
-                               pushInstruction(mb,q);
-                               r= pushArgument(mb,r,getArg(q,0));
-                       }
-                       match =0;
-                       for(j=0;j<mtop; j++)
-                       if( mvar[j]== getArg(p,0)) match++;
-                       if( match == 0){
-                               mvar[mtop] = getArg(p,0);
-                               mat[mtop++]= r;
-                               pushInstruction(mb,r);
-                       }
-                       actions++;
-                       continue;
-               } 
[EMAIL PROTECTED]
 Handle the rewrite v:=aggr.count(b) and sum()
 @c
                if( ((getModuleId(p)==aggrRef && getFunctionId(p)== countRef) 
|| 
                        (getModuleId(p)==aggrRef && getFunctionId(p)==sumRef && 
p->argc==2)) &&
                        (m=isMATalias(getArg(p,1),mvar,mtop)) >= 0){
+#ifdef DEBUG_OPT_MERGETABLE
+                       stream_printf(GDKout,"count/sum \n");
+                       printInstruction(GDKout,mb,p,0);
+#endif
                        r = newInstruction(mb,ASSIGNsymbol);
                        getArg(r,0)= getArg(p,0);
-                       pushInt(mb,r,0);
+                       r= pushInt(mb,r,0);
                        pushInstruction(mb,r);
                        for(k=1; k< mat[m]->argc; k++){
                                int v= newTmpVariable(mb,TYPE_int);
@@ -365,6 +374,9 @@
                                q= pushArgument(mb,q,getArg(r,0));
                                q= pushArgument(mb,q,v);
                                pushInstruction(mb,q);
+#ifdef DEBUG_OPT_MERGETABLE
+                               printInstruction(GDKout,mb,q,0);
+#endif
                        }
                        continue;
                } 
@@ -377,7 +389,7 @@
                        (m=isMATalias(getArg(p,1),mvar,mtop)) >= 0){
                        r = newInstruction(mb,ASSIGNsymbol);
                        getArg(r,0)= getArg(p,0);
-                       pushNil(mb,r,getArgType(mb,p,0));
+                       r= pushNil(mb,r,getArgType(mb,p,0));
                        pushInstruction(mb,r);
                        for(k=1; k< mat[m]->argc; k++){
                                int v= newTmpVariable(mb,TYPE_int);
@@ -406,27 +418,17 @@
                for( k= p->retc; k<p->argc; k++)
                if(     (m=isMATalias(getArg(p,k),mvar,mtop)) >= 0){
 #ifdef DEBUG_OPT_MERGETABLE
-                       stream_printf(GDKout,"Dependency resolution k=%d\n",k);
+                       stream_printf(GDKout,"pack resolution \n");
                        printInstruction(GDKout,mb,p,0);
 #endif
-                       r = newInstruction(mb, ASSIGNsymbol);
-                       setModuleId(r,matRef);
-                       setFunctionId(r,packRef);
-                       getArg(r,0)= getArg(mat[m],0);
-                       r= MATpackAll(mb, r, m, mat, mvar, mtop);
-                       if(     !( getModuleId(p) == matRef && 
getFunctionId(p)== newRef) )
-                               pushInstruction(mb,r);
-                       else freeInstruction(r);
-
-                       for(j=m; j<mtop-1; j++){
-                               mat[j]= mat[j+1];
-                               mvar[j]= mvar[j+1];
-                       }
-                       mtop--; 
+                       r= MATpackAll(mb, NULL, m, mat, mvar, &mtop);
                        actions++;
                        break;
                }
-
+#ifdef DEBUG_OPT_MERGETABLE
+                       stream_printf(GDKout,"last catc\n");
+                       printInstruction(GDKout,mb,p,0);
+#endif
                pushInstruction(mb,p);
        }
        GDKfree(old);
@@ -438,7 +440,8 @@
        (void) stk; 
 #ifdef DEBUG_OPT_MERGETABLE
        stream_printf(GDKout,"Result of multi table optimizer\n");
-       printFunction(GDKout, mb, 0);
+       optimizerCheck(mb,"merge test",1,0,0);
+       printFunction(GDKout, mb, LIST_MAL_ALL);
 #endif
 
        return actions;

Index: opt_peephole.mx
===================================================================
RCS file: /cvsroot/monetdb/MonetDB5/src/optimizer/opt_peephole.mx,v
retrieving revision 1.11
retrieving revision 1.11.2.1
diff -u -d -r1.11 -r1.11.2.1
--- opt_peephole.mx     16 Jul 2007 10:52:52 -0000      1.11
+++ opt_peephole.mx     17 Aug 2007 15:38:15 -0000      1.11.2.1
@@ -117,7 +117,7 @@
 #endif
                        r = newInstruction(mb,ASSIGNsymbol);
                        getArg(r, 0) = getArg(q, 0);
-                       pushArgument(mb, r, getArg(p, 1));
+                       r= pushArgument(mb, r, getArg(p, 1));
                        freeInstruction(p);
                        freeInstruction(q);
                        pushInstruction(mb, r);

Index: opt_accumulators.mx
===================================================================
RCS file: /cvsroot/monetdb/MonetDB5/src/optimizer/opt_accumulators.mx,v
retrieving revision 1.18
retrieving revision 1.18.2.1
diff -u -d -r1.18 -r1.18.2.1
--- opt_accumulators.mx 16 Jul 2007 10:52:22 -0000      1.18
+++ opt_accumulators.mx 17 Aug 2007 15:37:56 -0000      1.18.2.1
@@ -101,7 +101,7 @@
                freeInstruction(q);
                if(available[a]==0){
                        q= newFcnCall(mb,"algebra","reuse");
-                       pushArgument(mb,q,a);
+                       q= pushArgument(mb,q,a);
                        available[getArg(p,1)]=1;
                        getArg(p,1)=getArg(q,0);
                } else {
@@ -170,7 +170,7 @@
 @c
 
                        q= copyInstruction(p);
-                       pushArgument(mb, p, getArg(p,2));
+                       p= pushArgument(mb, p, getArg(p,2));
                        setArg(p,2,getArg(p,1));
                        setArg(p,1,getArg(p,0));
                        @:patchInstruction@
@@ -189,7 +189,7 @@
                        printInstruction(GDKout, mb, p, LIST_MAL_ALL);
 #endif
                        q= copyInstruction(p);
-                       pushArgument(mb, p, a);
+                       p= pushArgument(mb, p, a);
                        setArg(p,1,getArg(p,0));
                        @:patchInstruction@
                } 


-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >>  http://get.splunk.com/
_______________________________________________
Monetdb-checkins mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/monetdb-checkins

Reply via email to