Changeset: 456916ac3041 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=456916ac3041
Modified Files:
        monetdb5/optimizer/opt_bincopyfrom.c
        sql/backends/monet5/sql.c
        sql/backends/monet5/sql.h
Branch: copybinary
Log Message:

Split sql.importTable into separate sql.importColumn calls


diffs (173 lines):

diff --git a/monetdb5/optimizer/opt_bincopyfrom.c 
b/monetdb5/optimizer/opt_bincopyfrom.c
--- a/monetdb5/optimizer/opt_bincopyfrom.c
+++ b/monetdb5/optimizer/opt_bincopyfrom.c
@@ -14,13 +14,124 @@
 #include "mal_builder.h"
 #include "opt_bincopyfrom.h"
 
+static str transform(MalBlkPtr mb, InstrPtr importTable);
+static int extract_column(MalBlkPtr mb, InstrPtr old, int idx, int count_var);
+
 str
 OPTbincopyfromImplementation(Client cntxt, MalBlkPtr mb, MalStkPtr stk, 
InstrPtr pci)
 {
+       str msg = MAL_SUCCEED;
+       InstrPtr *old_mb_stmt = NULL;
        (void)cntxt;
        (void)mb;
        (void)stk;
        (void)pci;
 
+       str importTableRef = putName("importTable");
+
+       int found_at = -1;
+       for (int i = 0; i < mb->stop; i++) {
+               InstrPtr p = getInstrPtr(mb, i);
+               if (p->modname == sqlRef && p->fcnname == importTableRef) {
+                       found_at = i;
+                       break;
+               }
+       }
+       if (found_at == -1)
+               return MAL_SUCCEED;
+
+       old_mb_stmt = mb->stmt;
+       int old_stop = mb->stop;
+       if (newMalBlkStmt(mb, mb->stop + getInstrPtr(mb, found_at)->argc) < 0) {
+               msg = createException(MAL, "optimizer.bincopyfrom", 
SQLSTATE(HY013) MAL_MALLOC_FAIL);
+               goto end;
+       }
+
+       for (int i = 0; i < old_stop; i++) {
+               InstrPtr p = old_mb_stmt[i];
+               if (p->modname != sqlRef || p->fcnname != importTableRef) {
+                       pushInstruction(mb, p);
+                       continue;
+               }
+               msg = transform(mb, p);
+               if (msg != MAL_SUCCEED)
+                       return msg;
+       }
+
+end:
+       if (old_mb_stmt)
+               GDKfree(old_mb_stmt);
+       return msg;
+}
+
+static str
+transform(MalBlkPtr mb, InstrPtr old)
+{
+       // prototype: (bat1, .., batN) := sql.importTable(schema, table, 
onclient, path1 , .. ,pathN);
+       int onclient = *(int*)getVarValue(mb, getArg(old, old->retc + 2));
+       fprintf(stderr, "onclient=%d\n", onclient);
+
+       // find the narrowest result column whose file is not nil, it is 
probably quickest to load
+       int best_idx = -1;
+       int best_type = TYPE_any;
+       for (int i = 0; i < old->retc; i++) {
+               int var = getArg(old, i);
+               int var_type = ATOMstorage(getVarType(mb, var) & 0xFFFF);  // 
<=== what's the proper way to do this?
+               if (var_type >= best_type)
+                       continue;
+               int path_idx = old->retc + 3 + i;
+               int path_var = getArg(old, path_idx);
+               if (VALisnil(&getVarConstant(mb, path_var)))
+                       continue;
+               // must be better
+               best_idx = i;
+               best_type = var_type;
+       }
+       if (best_idx < 0)
+               return createException(MAL, "optimizer.bincopyfrom", 
SQLSTATE(42000) "all paths are nil");
+
+       // First emit this "best" column so we can use its row count in later 
columns.
+       int row_count_var = extract_column(mb, old, best_idx, -1);
+       (void) row_count_var;
+
+       // Then emit the rest of the columns
+       for (int i = 0; i < old->retc; i++) {
+               if (i != best_idx) {
+                       int new_row_count_var = extract_column(mb, old, i, 
row_count_var);
+                       if (onclient)
+                               row_count_var = new_row_count_var;
+               }
+       }
+
        return MAL_SUCCEED;
 }
+
+static int
+extract_column(MalBlkPtr mb, InstrPtr old, int idx, int count_var)
+{
+       str importColumnRef = putName("importColumn");
+
+       int var = getArg(old, idx);
+       int var_type = getVarType(mb, var) & 0xFFFF;  // <=== what's the proper 
way to do this?
+       str var_type_name = ATOMname(var_type);
+
+       int onclient = *(int*)getVarValue(mb, getArg(old, old->retc + 2));
+
+       int path_idx = old->retc + 3 + idx;
+       int path_var = getArg(old, path_idx);
+       str path = (str)getVarValue(mb, path_var);
+
+       InstrPtr p = newFcnCall(mb, sqlRef, importColumnRef);
+       setReturnArgument(p, old->argv[idx]);
+       int row_count_var = newTmpVariable(mb, TYPE_lng);
+       pushReturn(mb, p, row_count_var);
+       pushStr(mb, p, var_type_name);
+       pushStr(mb, p, path);
+       pushInt(mb, p, onclient);
+       if (count_var < 0)
+               pushLng(mb, p, 0);
+       else
+               pushArgument(mb, p, count_var);
+
+       return row_count_var;
+}
diff --git a/sql/backends/monet5/sql.c b/sql/backends/monet5/sql.c
--- a/sql/backends/monet5/sql.c
+++ b/sql/backends/monet5/sql.c
@@ -3489,6 +3489,22 @@ end:
        }
 }
 
+str
+importColumn(bat *ret, lng *retcnt, const str *tpe, const str *path, const int 
*onclient, const lng *nrows)
+{
+       (void)ret;
+       (void)retcnt;
+       (void)tpe;
+       (void)path;
+       (void)onclient;
+       (void)nrows;
+
+       return createException(MAL, "sql.importColumn", SQLSTATE(42000) "COPY 
BINARY FROM not implemented for type '%s': path=%s onclient=%d nrows=%ld",
+               *tpe, *path, *onclient, *nrows);
+}
+
+
+
 /* str mvc_bin_import_table_wrap(.., str *sname, str *tname, str *fname..);
  * binary attachment only works for simple binary types.
  * Non-simple types require each line to contain a valid ascii representation
@@ -5793,6 +5809,7 @@ static mel_func sql_init_funcs[] = {
  //we use bat.single now
  //pattern("sql", "single", CMDBATsingle, false, "", args(1,2, 
batargany("",2),argany("x",2))),
  pattern("sql", "importTable", mvc_bin_import_table_wrap, true, "Import a 
table from the files (fname)", args(1,5, 
batvarargany("",0),arg("sname",str),arg("tname",str),arg("onclient",int),vararg("fname",str))),
+ command("sql", "importColumn", importColumn, true, "Import a column from the 
given file", args(2, 6, batargany("", 0),arg("", lng), 
arg("type",str),arg("path",str),arg("onclient",int),arg("nrows",lng))),
  command("aggr", "not_unique", not_unique, false, "check if the tail sorted 
bat b doesn't have unique tail values", args(1,2, arg("",bit),batarg("b",oid))),
  command("sql", "optimizers", getPipeCatalog, false, "", args(3,3, 
batarg("",str),batarg("",str),batarg("",str))),
  pattern("sql", "optimizer_updates", SQLoptimizersUpdate, false, "", noargs),
diff --git a/sql/backends/monet5/sql.h b/sql/backends/monet5/sql.h
--- a/sql/backends/monet5/sql.h
+++ b/sql/backends/monet5/sql.h
@@ -98,6 +98,7 @@ sql5_export str mvc_row_result_wrap(Clie
 sql5_export str mvc_export_row_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, 
InstrPtr pci);
 sql5_export str mvc_import_table_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr 
stk, InstrPtr pci);
 sql5_export str mvc_bin_import_table_wrap(Client cntxt, MalBlkPtr mb, 
MalStkPtr stk, InstrPtr pci);
+sql5_export str importColumn(bat *ret, lng *retcnt, const str *tpe, const str 
*path, const int *onclient, const lng *nrows);
 sql5_export str setVariable(Client cntxt, MalBlkPtr mb, MalStkPtr stk, 
InstrPtr pci);
 sql5_export str getVariable(Client cntxt, MalBlkPtr mb, MalStkPtr stk, 
InstrPtr pci);
 sql5_export str sql_variables(Client cntxt, MalBlkPtr mb, MalStkPtr stk, 
InstrPtr pci);
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to