Changeset: 63cc5dda9632 for MonetDB URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=63cc5dda9632 Modified Files: monetdb5/optimizer/opt_pipes.c monetdb5/optimizer/opt_prelude.c monetdb5/optimizer/opt_prelude.h monetdb5/optimizer/opt_projection2candidate.c Branch: projection2candidate Log Message:
first version of a "showcase" MAL optimize to eliminate algrebra.projection() calls by making subsequent MAL operations use the respective candidate list directly. For now, all code is disabled by compiler directives ("#if 0 ... #endif") because most of the code does not work, yet; seemingly, the available MAL operations with candidates might not yet work as assumed, or they get used wrongly. Comments in the code detail the problems that occur with TPC-H. diffs (truncated from 461 to 300 lines): 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 @@ -91,7 +91,10 @@ static struct PIPELINES { "optimizer.candidates();" "optimizer.postfix();" "optimizer.deadcode();" -// "optimizer.projection2candidate();" awaiting the new batcalc api + "optimizer.projection2candidate();" + "optimizer.deadcode();" + "optimizer.projectionpath();" + "optimizer.deadcode();" // "optimizer.oltp();"awaiting the autocommit front-end changes "optimizer.wlc();" "optimizer.garbageCollector();", @@ -128,7 +131,10 @@ static struct PIPELINES { "optimizer.candidates();" "optimizer.postfix();" "optimizer.deadcode();" -// "optimizer.projection2candidate();" awaiting the new batcalc api + "optimizer.projection2candidate();" + "optimizer.deadcode();" + "optimizer.projectionpath();" + "optimizer.deadcode();" // "optimizer.oltp();"awaiting the autocommit front-end changes "optimizer.wlc();" "optimizer.garbageCollector();", @@ -170,7 +176,10 @@ static struct PIPELINES { "optimizer.candidates();" "optimizer.postfix();" "optimizer.deadcode();" -// "optimizer.projection2candidate();" awaiting the new batcalc api + "optimizer.projection2candidate();" + "optimizer.deadcode();" + "optimizer.projectionpath();" + "optimizer.deadcode();" // "optimizer.oltp();"awaiting the autocommit front-end changes "optimizer.wlc();" "optimizer.garbageCollector();", @@ -199,7 +208,7 @@ static struct PIPELINES { "optimizer.aliases();" "optimizer.constants();" "optimizer.commonTerms();" - "optimizer.projectionpath();" + //"optimizer.projectionpath();" "optimizer.reorder();" "optimizer.deadcode();" // "optimizer.reduce();" deprecated @@ -211,7 +220,10 @@ static struct PIPELINES { "optimizer.candidates();" "optimizer.postfix();" "optimizer.deadcode();" -// "optimizer.projection2candidate();" awaiting the new batcalc api + "optimizer.projection2candidate();" + "optimizer.deadcode();" + "optimizer.projectionpath();" + "optimizer.deadcode();" // "optimizer.oltp();"awaiting the autocommit front-end changes "optimizer.wlc();" "optimizer.garbageCollector();", diff --git a/monetdb5/optimizer/opt_prelude.c b/monetdb5/optimizer/opt_prelude.c --- a/monetdb5/optimizer/opt_prelude.c +++ b/monetdb5/optimizer/opt_prelude.c @@ -145,6 +145,7 @@ str hashRef; str hgeRef; str identityRef; str ifthenelseRef; +str ilikejoinRef; str ilikeRef; str ilikeselectRef; str ilikethetaselectRef; @@ -158,6 +159,7 @@ str joinRef; str jsonRef; str languageRef; str leftjoinRef; +str likejoinRef; str likeRef; str likeselectRef; str likethetaselectRef; @@ -196,6 +198,7 @@ str oidRef; str oltpRef; str openRef; str optimizerRef; +str outerjoinRef; str pack2Ref; str packIncrementRef; str packRef; @@ -442,6 +445,7 @@ void optimizerInit(void) hgeRef = putName("hge"); identityRef = putName("identity"); ifthenelseRef = putName("ifthenelse"); + ilikejoinRef = putName("ilikejoin"); ilikeRef = putName("ilike"); ilikeselectRef = putName("ilikeselect"); ilikethetaselectRef = putName("ilikethetaselect"); @@ -455,6 +459,7 @@ void optimizerInit(void) jsonRef = putName("json"); languageRef= putName("language"); leftjoinRef = putName("leftjoin"); + likejoinRef = putName("likejoin"); likeRef = putName("like"); likeselectRef = putName("likeselect"); likethetaselectRef = putName("likethetaselect"); @@ -493,6 +498,7 @@ void optimizerInit(void) oltpRef = putName("oltp"); openRef = putName("open"); optimizerRef = putName("optimizer"); + outerjoinRef = putName("outerjoin"); pack2Ref = putName("pack2"); packIncrementRef = putName("packIncrement"); packRef = putName("pack"); diff --git a/monetdb5/optimizer/opt_prelude.h b/monetdb5/optimizer/opt_prelude.h --- a/monetdb5/optimizer/opt_prelude.h +++ b/monetdb5/optimizer/opt_prelude.h @@ -146,6 +146,7 @@ mal_export str hashRef; mal_export str hgeRef; mal_export str identityRef; mal_export str ifthenelseRef; +mal_export str ilikejoinRef; mal_export str ilikeRef; mal_export str ilikeselectRef; mal_export str ilikethetaselectRef; @@ -160,6 +161,7 @@ mal_export str joinRef; mal_export str jsonRef; mal_export str languageRef; mal_export str leftjoinRef; +mal_export str likejoinRef; mal_export str likeRef; mal_export str likeselectRef; mal_export str likethetaselectRef; @@ -199,6 +201,7 @@ mal_export str oidRef; mal_export str oltpRef; mal_export str openRef; mal_export str optimizerRef; +mal_export str outerjoinRef; mal_export str pack2Ref; mal_export str packIncrementRef; mal_export str packRef; diff --git a/monetdb5/optimizer/opt_projection2candidate.c b/monetdb5/optimizer/opt_projection2candidate.c --- a/monetdb5/optimizer/opt_projection2candidate.c +++ b/monetdb5/optimizer/opt_projection2candidate.c @@ -6,18 +6,28 @@ * Copyright 1997 - July 2008 CWI, August 2008 - 2018 MonetDB B.V. */ -/* author M.Kersten - * This optimizer can be used for JIT optimization and moves - * candidate lists into MAL operations where possible. - * It should be ran after the candidates optimizer. - * Specific snippets to be replaced - * C_1:bat[:oid] := sql.tid(X_0,"sys","t"); - * X_4:bat[:int] := sql.bind(X_0,"sys","t","i",0); - * X_13 := algebra.projection(C_1,X_4); - * projection can be avoided +/* authors M. Kersten ("projection2candidate"), S. Manegold ("projection2candidate") * - * A candidate list can be pushed into the calculations + * This optimizer is intended to eliminate projections (algebra.projection()) + * by using the candidate list (first argument of algebra.projection() + * directly in the MAL operation that consumes the projection result, + * i.e., replace pattern like + * X_20 := algebra.projection(C_10, X_10); + * (..) := mod.func(.., X_20, ..); + * by + * (..) := mod.func(.., X_10, .., C_10, ..); + * when/where ever the variant of mod.func() with candidate list exists. + * + * In fact, for actual elimination of no longer used algebra.projection() + * calls, this optimizer should be followed by the "deadcode" optimizer! + * + * This optimizer might merely be a proof-of-concept prototype + * that eventually gets replaced by having the SQL-2-MAL code generator + * no longer emit algebra.projection() calls, but rather mod.func() calls + * that directly use the candidate list (and perform the implicite + * projection on-the-fly). */ + #include "monetdb_config.h" #include "mal_builder.h" #include "opt_projection2candidate.h" @@ -33,7 +43,7 @@ OPTprojection2candidateImplementation(Cl { int i,actions = 0; int limit = mb->stop; - InstrPtr p, q, *old = mb->stmt; + InstrPtr p, *old = mb->stmt; char buf[256]; lng usec = GDKusec(); str msg = MAL_SUCCEED; @@ -61,23 +71,270 @@ OPTprojection2candidateImplementation(Cl pushInstruction(mb,old[i]); break; } +#if 0 +// Problems / errors: +// gdk/gdk_calc.c:5561: BATcalccstsub: Assertion `cand < candend' failed. +// with TPCH q14 +// inputs not the same size. +// with TPCH q01, q03, q05, q07, q15, q19 /* case 1 * X_527 := algebra.projection(C_353, X_329); * X_535 := batcalc.-(100:lng, X_527); */ + /* UNCHNAGED LEGACY CODE (from former "projection2candidate" optimizer): + * - too strict in matching only batcalc.-(const-scalar,COL) + * - batcalc.*() implementation with candidate list for shrinking result + * not yet implemented --- well, possibly partly in batcalc-candidates + * branch ... + */ if( getModuleId(p) == batcalcRef && *getFunctionId(p) == '-' && p->argc == 3 && isVarConstant(mb, getArg(p,1)) ){ - q= getInstrPtr(mb, getVar(mb,getArg(p,2))->updated); + InstrPtr q= getInstrPtr(mb, getVar(mb,getArg(p,2))->updated); if ( q == 0) q= getInstrPtr(mb, getVar(mb,getArg(p,2))->declared); if( q && getArg(q,0) == getArg(p,2) && getModuleId(q) == algebraRef && getFunctionId(q) == projectionRef ){ getArg(p,2)= getArg(q,2); p= pushArgument(mb,p, getArg(q,1)); + actions++; OPTDEBUGprojection2candidate( fprintf(stderr, "#Optimize projection2candidate case 1\n"); fprintInstruction(stderr, mb,0,p,LIST_MAL_DEBUG); ) } } +#endif +#if 0 + /* case 2: + * replace + * X_20 := algebra.projection(C_10, X_10); + * (..) := group.group[done](X_20); + * by + * (..) := group.group[done](X_10, C_10); + * or + * X_20 := algebra.projection(C_10, X_10); + * (..) := group.subgroup[done](X_20, X_21 [,X_22, X_23]); + * by + * (..) := group.subgroup[done](X_10, C_10, X_21 [,X_22, X_23])); + * + * the latter fails for now, seemling resulting in an endless loop ... + */ + if ( getModuleId(p) == groupRef && ( FALSE +# if 0 +// Problems / errors: +// gdk/gdk_group.c:521: BATgroup_internal: Assertion `cand < candend' failed. +// with TPCH q09, q17, q21 +// GDK reported error: BATgroupmin: b and g must be aligned +// with TPCH q02 +// Can not create object +// with TPCH q11, q1, q21 +// wrong result +// with TPCH q04, q05 + || ( ( getFunctionId(p) == groupRef || getFunctionId(p) == groupdoneRef ) && p->argc == p->retc + 1 ) +# endif +# if 0 +// Problems / errors: +// gdk/gdk_group.c:521: BATgroup_internal: Assertion `cand < candend' failed. +// with TPCH q10 +// endless loop +// with TPCH q01, q03 +// GDK reported error: BATproject: does not match always +// with TPCH q16 +// Can not create object +// with TPCH q18 + || ( ( getFunctionId(p) == subgroupRef || getFunctionId(p) == subgroupdoneRef ) && ( p->argc == p->retc + 2 || p->argc == p->retc + 4 ) ) +# endif + ) ) + { + /* found group.[sub]group[done]() */ + /* find statement the creates the first argument of group.[sub]group[done] */ + InstrPtr q= getInstrPtr(mb, getVar(mb,getArg(p,p->retc))->updated); + if ( q == 0) + q= getInstrPtr(mb, getVar(mb,getArg(p,p->retc))->declared); + /* if found, check whether it is algebra.projection() */ + if( q && q->argc == 3 && getArg(q,0) == getArg(p,p->retc) && getModuleId(q) == algebraRef && getFunctionId(q) == projectionRef ){ + if (p->argc == p->retc + 1) { + /* group.group[done](): + * - simply add candiate list as second argument + */ + p= pushArgument(mb,p, getArg(q,1)); /* cand */ + } else { + /* group.subgroup[done](): + * - add new argument + * - shift all but the first argument by one + * - add candiate list as second argument + */ + int n = p->argc - 1; + p= pushArgument(mb,p, getArg(p,n)); + for (--n; n > p->retc + 1; n--) + getArg(p,n)= getArg(p,n-1); + getArg(p,p->retc + 1) = getArg(q,1); /* cand */ + } + /* replace first argument of group.[sub]group[done() + * by original column, i.e., second argument of + * algebra.projection() + */ + getArg(p,p->retc) = getArg(q,2); /* org COL */ + actions++; + OPTDEBUGprojection2candidate( + fprintf(stderr, "#Optimize projection2candidate case 2\n"); + fprintInstruction(stderr, mb,0,p,LIST_MAL_DEBUG); _______________________________________________ checkin-list mailing list checkin-list@monetdb.org https://www.monetdb.org/mailman/listinfo/checkin-list