Changeset: f9d6a5193140 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/f9d6a5193140
Added Files:
        monetdb5/modules/mal/properties.c
        monetdb5/modules/mal/properties.h
        monetdb5/optimizer/opt_properties.c
        monetdb5/optimizer/opt_properties.h
Modified Files:
        sql/backends/monet5/sql_optimizer.c
Branch: types
Log Message:

Gather some basic properties from the BATs during optimizer call.


diffs (294 lines):

diff --git a/monetdb5/modules/mal/properties.c 
b/monetdb5/modules/mal/properties.c
new file mode 100644
--- /dev/null
+++ b/monetdb5/modules/mal/properties.c
@@ -0,0 +1,63 @@
+/*
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0.  If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * Copyright 1997 - July 2008 CWI, August 2008 - 2021 MonetDB B.V.
+ */
+
+/*
+ *  Martin Kersten
+ * The support routines for opt_properties
+ */
+
+#include "monetdb_config.h"
+#include "properties.h"
+#include "gdk_calc.h"
+#include "mal_exception.h"
+
+
+// dummy procedures to monitor the generation of the properties code.
+str
+PROPget(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
+{
+       (void) cntxt;
+       (void) mb;
+       (void) stk;
+       (void) pci;
+
+       return MAL_SUCCEED;
+}
+
+str
+PROPbind(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
+{
+       // beware the bind() returns a view on the original BAT
+       //bat orig = *getArgReference_bat(stk, pci, 1);
+       //bat bid = (bat) *getArgReference_int(stk, pci, 2);
+       (void) cntxt;
+       (void) mb;
+       (void) stk;
+       (void) pci;
+
+       //if( orig != bid)
+               //throw(MAL,"properties.bind"," Incompatible BAT references ");
+       return MAL_SUCCEED;
+}
+
+#include "mel.h"
+mel_func properties_init_funcs[] = {
+ pattern("properties", "bind", PROPbind, false, "Retrieve actual properties 
from a BAT", args(1,3,  arg("",void),batargany("",1),arg("min",int))),
+ pattern("properties", "get", PROPget, false, "Retrieve actual properties from 
a BAT", args(1,5,  
arg("",void),batargany("",1),arg("min",bte),arg("max",bte),arg("nils",lng) )),
+ pattern("properties", "get", PROPget, false, "Retrieve actual properties from 
a BAT", args(1,5,  
arg("",void),batargany("",1),arg("min",sht),arg("max",sht),arg("nils",lng) )),
+ pattern("properties", "get", PROPget, false, "Retrieve actual properties from 
a BAT", args(1,5,  
arg("",void),batargany("",1),arg("min",int),arg("max",int),arg("nils",lng) )),
+ pattern("properties", "get", PROPget, false, "Retrieve actual properties from 
a BAT", args(1,5,  
arg("",void),batargany("",1),arg("min",lng),arg("max",lng),arg("nils",lng) )),
+ { .imp=NULL }
+};
+#include "mal_import.h"
+#ifdef _MSC_VER
+#undef read
+#pragma section(".CRT$XCU",read)
+#endif
+LIB_STARTUP_FUNC(init_querylog_mal)
+{ mal_module("properties", NULL, properties_init_funcs); }
diff --git a/monetdb5/modules/mal/properties.h 
b/monetdb5/modules/mal/properties.h
new file mode 100644
--- /dev/null
+++ b/monetdb5/modules/mal/properties.h
@@ -0,0 +1,16 @@
+/*
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0.  If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * Copyright 1997 - July 2008 CWI, August 2008 - 2021 MonetDB B.V.
+ */
+
+#ifndef _PROPS_H
+#define _PROPS_H
+#include "mal.h"
+#include "mal_interpreter.h"
+
+mal_export str PROPget(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr 
pci);
+mal_export str PROPbind(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr 
pci);
+#endif /* _PROPS_H */
diff --git a/monetdb5/optimizer/opt_properties.c 
b/monetdb5/optimizer/opt_properties.c
new file mode 100644
--- /dev/null
+++ b/monetdb5/optimizer/opt_properties.c
@@ -0,0 +1,134 @@
+/*
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0.  If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * Copyright 1997 - July 2008 CWI, August 2008 - 2021 MonetDB B.V.
+ */
+
+/* author M.Kersten
+ * This optimizer is used to scale-down columns when properties
+ * of the underlying BATs permits it.
+ * The effect should be a smaller footprint.
+ */
+#include "monetdb_config.h"
+#include "mal_builder.h"
+#include "mal_interpreter.h"
+#include "opt_properties.h"
+
+#define properties(TPE) \
+       val =  BATmax_skipnil(bn, (void*) (&vmax.val.TPE), 1);\
+       if( val != &vmax.val.TPE){\
+               /* error */\
+       }\
+       val =  BATmin_skipnil(bn, (void*) (&vmin.val.TPE), 1);\
+       if( val != &vmin.val.TPE){\
+               /* error */\
+       }
+
+
+#define keeparound(TPE)\
+       setArgType(mb, q, 0, TYPE_void);\
+       q = pushArgument(mb, q, getArg(pci,1));\
+       q = pushInt(mb, q, vmin.val.TPE);\
+       q = pushInt(mb, q, vmax.val.TPE);\
+       q = pushLng(mb, q, nils);
+
+
+static str
+PROPstatistics(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
+{
+       InstrPtr q;
+       bat bid;
+       BAT *b, *bn;
+       int tpe = TYPE_int, basetype= TYPE_any;
+       ValRecord vmin, vmax;
+       lng nils=5;
+       ptr val;
+
+    (void) cntxt;
+    (void) mb;
+    (void) stk;
+
+       bid = getVarConstant(mb, getArg(pci, 2)).val.ival;
+       basetype = getBatType(getArgType(mb, pci,1));
+
+       switch(basetype){
+               case TYPE_bte: case TYPE_sht: case TYPE_int: case TYPE_lng: 
case TYPE_hge: break;
+               default: return MAL_SUCCEED;
+       }
+
+    if ((b = BATdescriptor(bid)) == NULL){
+        throw(MAL, "algebra.max", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
+       }
+       bn = VIEWcreate(b->hseqbase, b);
+
+    // Access a BAT and derive the properties 
+       switch(basetype){
+       case TYPE_bte: properties(btval); break;
+       case TYPE_int: properties(ival); break;
+       }
+    // Consolidate the type, looking for the minimum type needed for the 
min/max value;
+
+       // Leave the properties behind in the plan
+       q = newStmt(mb, propertiesRef, getRef);
+       switch(tpe){
+       case TYPE_int: keeparound(ival); break;
+       }
+       BBPunfix(bn->batCacheid);
+       BBPunfix(b->batCacheid);
+    return MAL_SUCCEED;
+}
+
+str
+OPTpropertiesImplementation(Client cntxt, MalBlkPtr mb, MalStkPtr stk, 
InstrPtr pci)
+{
+       int i, actions = 0, *originaltype= NULL;
+       int limit = mb->stop;
+       InstrPtr p, *old = mb->stmt;
+       char buf[256];
+       lng usec = GDKusec();
+       str msg = MAL_SUCCEED;
+
+       (void) stk;
+       (void) cntxt;
+       (void) pci;
+
+       setVariableScope(mb);
+       if ( newMalBlkStmt(mb, mb->ssize) < 0)
+               throw(MAL,"optimizer.properties", SQLSTATE(HY013) 
MAL_MALLOC_FAIL);
+
+       /* Collect properties */
+       for (i = 0; i < limit; i++) {
+               p = old[i];
+
+               if (p->token == ENDsymbol){
+                       for(; i<limit; i++)
+                               if (old[i])
+                                       pushInstruction(mb,old[i]);
+                       break;
+               }
+               pushInstruction(mb,p);
+
+               // Phase I, add the properties of the BATs to the plan
+               if( getModuleId(p) == propertiesRef && ( getFunctionId(p) == 
bindRef || getFunctionId(p) == bindidxRef)){
+                       (void) PROPstatistics(cntxt, mb, stk, p);
+               }
+       }
+
+       GDKfree(old);
+       GDKfree(originaltype);
+    /* Defense line against incorrect plans */
+       msg = chkTypes(cntxt->usermodule, mb, FALSE);
+       if (!msg)
+               msg = chkFlow(mb);
+       if (!msg)
+               msg = chkDeclarations(mb);
+    /* keep all actions taken as a post block comment */
+       usec = GDKusec()- usec;
+    snprintf(buf,256,"%-20s actions=%2d time=" LLFMT " 
usec","properties",actions, usec);
+    newComment(mb,buf);
+       if( actions > 0)
+               addtoMalBlkHistory(mb);
+       return msg;
+}
diff --git a/monetdb5/optimizer/opt_properties.h 
b/monetdb5/optimizer/opt_properties.h
new file mode 100644
--- /dev/null
+++ b/monetdb5/optimizer/opt_properties.h
@@ -0,0 +1,16 @@
+/*
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0.  If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * Copyright 1997 - July 2008 CWI, August 2008 - 2021 MonetDB B.V.
+ */
+
+#ifndef _MAL_PROPS_
+#define _MAL_PROPS_
+#include "opt_prelude.h"
+#include "opt_support.h"
+
+extern str OPTpropertiesImplementation(Client cntxt, MalBlkPtr mb, MalStkPtr 
stk, InstrPtr p);
+
+#endif
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
@@ -58,9 +58,18 @@ SQLgetSpace(mvc *m, MalBlkPtr mb, int pr
        sql_trans *tr = m->session->tr;
        lng size,space = 0, i;
        str lasttable = 0;
+       InstrPtr *old, q;
+       int limit, extend = 1;
+       
+       limit = mb->stop;
+       old = mb->stmt;
+    if ( newMalBlkStmt(mb, mb->ssize) < 0){
+        extend = 0;
+       }
 
-       for (i = 0; i < mb->stop; i++) {
-               InstrPtr p = mb->stmt[i];
+       for (i = 0; i < limit; i++) {
+               InstrPtr p = old[i];
+               pushInstruction(mb, p);
 
                /* now deal with the update binds, it is only necessary to 
identify that there are updats
                 * The actual size is not that important */
@@ -72,6 +81,7 @@ SQLgetSpace(mvc *m, MalBlkPtr mb, int pr
                        sql_schema *s = mvc_bind_schema(m, sname);
                        sql_table *t = 0;
                        sql_column *c = 0;
+                       int bid = 0;
 
                        if (!s)
                                continue;
@@ -82,6 +92,12 @@ SQLgetSpace(mvc *m, MalBlkPtr mb, int pr
                        if (!c)
                                continue;
 
+                       bid = ((sql_delta *)c->data)->cs.bid;
+                       if( extend == 1){
+                               q = newStmt(mb, propertiesRef,  bindRef);
+                               q = pushArgument(mb, q, getArg(p,0));
+                               q = pushInt(mb, q, bid);
+                       }
                        /* we have to sum the cost of all three components of a 
BAT */
                        if (c && isTable(c->t) && (lasttable == 0 || 
strcmp(lasttable,tname)==0)) {
                                size = SQLgetColumnSize(tr, c, access);
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to