Changeset: 2ed49be07b74 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/2ed49be07b74
Modified Files:
        monetdb5/optimizer/opt_prelude.c
        monetdb5/optimizer/opt_prelude.h
        sql/backends/monet5/rel_bin.c
        sql/backends/monet5/sql.c
        sql/backends/monet5/sql_statement.c
        sql/backends/monet5/sql_statement.h
Branch: copyintobinary
Log Message:

Emit a MAL plan

The underlying mal operator is still a stub


diffs (212 lines):

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
@@ -135,6 +135,7 @@ const char *evalRef;
 const char *execRef;
 const char *expandRef;
 const char *exportOperationRef;
+const char *export_bin_columnRef;
 const char *export_tableRef;
 const char *fetchRef;
 const char *findRef;
@@ -450,6 +451,7 @@ void optimizerInit(void)
        execRef = putName("exec");
        expandRef = putName("expand");
        exportOperationRef = putName("exportOperation");
+       export_bin_columnRef = "export_bin_column";
        export_tableRef = putName("export_table");
        fetchRef = putName("fetch");
        findRef = putName("find");
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
@@ -132,6 +132,7 @@ mal_export  const char *evalRef;
 mal_export  const char *execRef;
 mal_export  const char *expandRef;
 mal_export  const char *exportOperationRef;
+mal_export  const char *export_bin_columnRef;
 mal_export  const char *export_tableRef;
 mal_export  const char *fetchRef;
 mal_export  const char *findRef;
diff --git a/sql/backends/monet5/rel_bin.c b/sql/backends/monet5/rel_bin.c
--- a/sql/backends/monet5/rel_bin.c
+++ b/sql/backends/monet5/rel_bin.c
@@ -5784,43 +5784,78 @@ rel2bin_truncate(backend *be, sql_rel *r
        return truncate;
 }
 
+static ValPtr take_atom_arg(node **n, int expected_type) {
+       sql_exp *e = (*n)->data;
+       atom *a = e->l;
+       assert(a->tpe.type->localtype == expected_type);
+       assert(!a->isnull);
+       *n = (*n)->next;
+       return &a->data;
+}
+
 static stmt *
 rel2bin_output(backend *be, sql_rel *rel, list *refs)
 {
        mvc *sql = be->mvc;
-       node *n;
-       const char *tsep, *rsep, *ssep, *ns, *fn = NULL;
-       atom *tatom, *ratom, *satom, *natom;
-       int onclient = 0;
-       stmt *s = NULL, *fns = NULL, *res = NULL;
+       stmt *sub = NULL, *fns = NULL, *res = NULL;
        list *slist = sa_list(sql->sa);
 
        if (rel->l)  /* first construct the sub relation */
-               s = subrel_bin(be, rel->l, refs);
-       s = subrel_project(be, s, refs, rel->l);
-       if (!s)
+               sub = subrel_bin(be, rel->l, refs);
+       sub = subrel_project(be, sub, refs, rel->l);
+       if (!sub)
                return NULL;
 
        if (!rel->exps)
-               return s;
-       n = rel->exps->h;
-       tatom = ((sql_exp*) n->data)->l;
-       tsep  = sa_strdup(sql->sa, tatom->isnull ? "" : tatom->data.val.sval);
-       ratom = ((sql_exp*) n->next->data)->l;
-       rsep  = sa_strdup(sql->sa, ratom->isnull ? "" : ratom->data.val.sval);
-       satom = ((sql_exp*) n->next->next->data)->l;
-       ssep  = sa_strdup(sql->sa, satom->isnull ? "" : satom->data.val.sval);
-       natom = ((sql_exp*) n->next->next->next->data)->l;
-       ns    = sa_strdup(sql->sa, natom->isnull ? "" : natom->data.val.sval);
-
-       if (n->next->next->next->next) {
-               fn = E_ATOM_STRING(n->next->next->next->next->data);
-               fns = stmt_atom_string(be, sa_strdup(sql->sa, fn));
-               onclient = E_ATOM_INT(n->next->next->next->next->next->data);
-       }
-       list_append(slist, stmt_export(be, s, tsep, rsep, ssep, ns, onclient, 
fns));
-       if (s->type == st_list && ((stmt*)s->op4.lval->h->data)->nrcols != 0) {
-               res = stmt_aggr(be, s->op4.lval->h->data, NULL, NULL, 
sql_bind_func(sql, "sys", "count", sql_bind_localtype("void"), NULL, F_AGGR, 
true), 1, 0, 1);
+               return sub;
+
+       list *arglist = rel->exps;
+       node *argnode = arglist->h;
+       atom *a = ((sql_exp*)argnode->data)->l;
+       int tpe = a->tpe.type->localtype;
+
+       // With regular COPY INTO <file>, the first argument is a string.
+       // With COPY INTO BINARY, it is an int.
+       if (tpe == TYPE_str) {
+               atom *tatom = ((sql_exp*) argnode->data)->l;
+               const char *tsep  = sa_strdup(sql->sa, tatom->isnull ? "" : 
tatom->data.val.sval);
+               atom *ratom = ((sql_exp*) argnode->next->data)->l;
+               const char *rsep  = sa_strdup(sql->sa, ratom->isnull ? "" : 
ratom->data.val.sval);
+               atom *satom = ((sql_exp*) argnode->next->next->data)->l;
+               const char *ssep  = sa_strdup(sql->sa, satom->isnull ? "" : 
satom->data.val.sval);
+               atom *natom = ((sql_exp*) argnode->next->next->next->data)->l;
+               const char *ns = sa_strdup(sql->sa, natom->isnull ? "" : 
natom->data.val.sval);
+
+               const char *fn = NULL;
+               int onclient = 0;
+               if (argnode->next->next->next->next) {
+                       fn = 
E_ATOM_STRING(argnode->next->next->next->next->data);
+                       fns = stmt_atom_string(be, sa_strdup(sql->sa, fn));
+                       onclient = 
E_ATOM_INT(argnode->next->next->next->next->next->data);
+               }
+               stmt *export = stmt_export(be, sub, tsep, rsep, ssep, ns, 
onclient, fns);
+               list_append(slist, export);
+       } else if (tpe == TYPE_int) {
+               endianness endian = take_atom_arg(&argnode, TYPE_int)->val.ival;
+               int on_client = take_atom_arg(&argnode, TYPE_int)->val.ival;
+               assert(sub->type == st_list);
+               list *collist = sub->op4.lval;
+               for (node *colnode = collist->h; colnode; colnode = 
colnode->next) {
+                       stmt *colstmt = colnode->data;
+                       assert(argnode != NULL);
+                       const char *filename = take_atom_arg(&argnode, 
TYPE_str)->val.sval;
+                       stmt *export = stmt_export_bin(be, colstmt, endian, 
filename, on_client);
+                       list_append(slist, export);
+               }
+               assert(argnode == NULL);
+
+       } else {
+               assert(0 && "unimplemented export statement type");
+               return sub;
+       }
+
+       if (sub->type == st_list && ((stmt*)sub->op4.lval->h->data)->nrcols != 
0) {
+               res = stmt_aggr(be, sub->op4.lval->h->data, NULL, NULL, 
sql_bind_func(sql, "sys", "count", sql_bind_localtype("void"), NULL, F_AGGR, 
true), 1, 0, 1);
        } else {
                res = stmt_atom_lng(be, 1);
        }
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
@@ -3084,6 +3084,18 @@ bat2return(MalStkPtr stk, InstrPtr pci, 
 static const char fwftsep[2] = {STREAM_FWF_FIELD_SEP, '\0'};
 static const char fwfrsep[2] = {STREAM_FWF_RECORD_SEP, '\0'};
 
+static str
+mvc_export_bin_column_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr 
pci)
+{
+       (void)cntxt;
+       (void)mb;
+       (void)stk;
+       (void)pci;
+
+       throw(SQL, "sql.export_bin_column", SQLSTATE(42000) "not implemented");
+}
+
+
 /* str mvc_import_table_wrap(int *res, sql_table **t, unsigned char* *T, 
unsigned char* *R, unsigned char* *S, unsigned char* *N, str *fname, lng *sz, 
lng *offset, int *besteffort, str *fixed_width, int *onclient, int *escape); */
 str
 mvc_import_table_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
@@ -5189,6 +5201,8 @@ static mel_func sql_init_funcs[] = {
  pattern("sql", "exportChunk", mvc_export_chunk_wrap, true, "Export a chunk of 
the result set (in order) to stream s", args(1,3, 
arg("",void),arg("s",streams),arg("res_id",int))),
  pattern("sql", "exportChunk", mvc_export_chunk_wrap, true, "Export a chunk of 
the result set (in order) to stream s", args(1,5, 
arg("",void),arg("s",streams),arg("res_id",int),arg("offset",int),arg("nr",int))),
  pattern("sql", "exportOperation", mvc_export_operation_wrap, true, "Export 
result of schema/transaction queries", args(1,1, arg("",void))),
+ pattern("sql", "export_bin_column", mvc_export_bin_column_wrap, true, "export 
column as binary", args(1, 5, arg("", lng), batargany("col", 1), arg("endian", 
int), arg("filename", str), arg("onclient", int))),
+ pattern("sql", "export_bin_column", mvc_export_bin_column_wrap, true, "export 
column as binary", args(1, 5, arg("", lng), argany("val", 1), arg("endian", 
int), arg("filename", str), arg("onclient", int))),
  pattern("sql", "affectedRows", mvc_affected_rows_wrap, true, "export the 
number of affected rows by the current query", args(1,3, 
arg("",int),arg("mvc",int),arg("nr",lng))),
  pattern("sql", "copy_from", mvc_import_table_wrap, true, "Import a table from 
bstream s with the \ngiven tuple and seperators (sep/rsep)", args(1,13, 
batvarargany("",0),arg("t",ptr),arg("sep",str),arg("rsep",str),arg("ssep",str),arg("ns",str),arg("fname",str),arg("nr",lng),arg("offset",lng),arg("best",int),arg("fwf",str),arg("onclient",int),arg("escape",int))),
  //we use bat.single now
diff --git a/sql/backends/monet5/sql_statement.c 
b/sql/backends/monet5/sql_statement.c
--- a/sql/backends/monet5/sql_statement.c
+++ b/sql/backends/monet5/sql_statement.c
@@ -2610,6 +2610,27 @@ stmt_export(backend *be, stmt *t, const 
 }
 
 stmt *
+stmt_export_bin(backend *be, stmt *colstmt, endianness endian, const char 
*filename, int on_client)
+{
+       MalBlkPtr mb = be->mb;
+       InstrPtr q;
+
+
+       q = newStmt(mb, sqlRef, export_bin_columnRef);
+       pushArgument(mb, q, colstmt->nr);
+       pushInt(mb, q, endian);
+       pushStr(mb, q, filename);
+       pushInt(mb, q, on_client);
+
+       stmt *s = stmt_create(be->mvc->sa, st_export);
+       if (!s)
+               return NULL;
+
+       s->q = q;
+       return s;
+}
+
+stmt *
 stmt_trans(backend *be, int type, stmt *chain, stmt *name)
 {
        MalBlkPtr mb = be->mb;
diff --git a/sql/backends/monet5/sql_statement.h 
b/sql/backends/monet5/sql_statement.h
--- a/sql/backends/monet5/sql_statement.h
+++ b/sql/backends/monet5/sql_statement.h
@@ -172,6 +172,7 @@ extern stmt *stmt_replace(backend *be, s
 extern stmt *stmt_table_clear(backend *be, sql_table *t, int 
restart_sequences);
 
 extern stmt *stmt_export(backend *be, stmt *t, const char *sep, const char 
*rsep, const char *ssep, const char *null_string, int onclient, stmt *file);
+extern stmt *stmt_export_bin(backend *be, stmt *colstmt, endianness endian, 
const char *filename, int on_client);
 extern stmt *stmt_trans(backend *b, int type, stmt *chain, stmt *name);
 extern stmt *stmt_catalog(backend *be, int type, stmt *args);
 
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to