Changeset: fa1eacd50d7a for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/fa1eacd50d7a
Added Files:
        sql/backends/monet5/copy.c
Modified Files:
        sql/backends/monet5/CMakeLists.txt
        sql/backends/monet5/rel_bin.c
Branch: copyparpipe
Log Message:

Emit some code, any code


diffs (193 lines):

diff --git a/sql/backends/monet5/CMakeLists.txt 
b/sql/backends/monet5/CMakeLists.txt
--- a/sql/backends/monet5/CMakeLists.txt
+++ b/sql/backends/monet5/CMakeLists.txt
@@ -155,6 +155,7 @@ target_sources(sql
   opt_backend.h
   for.c for.h
   dict.c dict.h
+  copy.c
   ${MONETDB_CURRENT_SQL_SOURCES}
   PUBLIC
   ${sql_public_headers})
diff --git a/sql/backends/monet5/copy.c b/sql/backends/monet5/copy.c
new file mode 100644
--- /dev/null
+++ b/sql/backends/monet5/copy.c
@@ -0,0 +1,75 @@
+/*
+ * 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 - 2022 MonetDB B.V.
+ */
+
+#include "monetdb_config.h"
+#include "gdk.h"
+#include "streams.h"
+#include "mal.h"
+#include "mal_errors.h"
+// #include "mal_client.h"
+// #include "mal_instruction.h"
+#include "mal_exception.h"
+// #include "mal_interpreter.h"
+
+
+#define bailout(f, ...) do { \
+               msg = createException(SQL, f, SQLSTATE(42000) __VA_ARGS__); \
+               goto end; \
+       } while (0)
+
+
+static str
+COPYread(lng *ret_nread, Stream *stream_arg, lng *block_size_arg, bat 
*block_bat_arg)
+{
+       str msg = MAL_SUCCEED;
+       stream *s = *stream_arg;
+       lng block_size = *block_size_arg;
+       bat b = *block_bat_arg;
+       BAT *bat = NULL;
+       lng nread;
+
+       bat = BATdescriptor(b);
+       if (bat->batRole != TRANSIENT) {
+               bailout("copy.read", "can only read into transient BAT");
+       }
+       BATclear(bat, true);
+
+       if (BATcapacity(bat) < (BUN)block_size && BATextend(bat, block_size) != 
GDK_SUCCEED) {
+               bailout("copy.read", "%s", GDK_EXCEPTION);
+       }
+
+       nread = mnstr_read(s, Tloc(bat, 0), 1, block_size);
+       if (nread < 0) {
+               bailout("copy.read", "%s", mnstr_peek_error(s));
+       }
+
+       BATsetcount(bat, nread);
+       *ret_nread = nread;
+end:
+       if (bat != NULL)
+               BBPunfix(bat->batCacheid);
+       return msg;
+}
+
+
+#include "mel.h"
+static mel_func copy_init_funcs[] = {
+ command("copy", "read", COPYread, true, "Clear the BAT and read 'block_size' 
bytes into it from 's'",
+       args(1, 4,
+               arg("",lng),
+               arg("stream", streams), arg("block_size", lng), batarg("block", 
bte)
+ )),
+ { .imp=NULL }
+};
+#include "mal_import.h"
+#ifdef _MSC_VER
+#undef read
+#pragma section(".CRT$XCU",read)
+#endif
+LIB_STARTUP_FUNC(init_json_mal)
+{ mal_module("copy", NULL, copy_init_funcs); }
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
@@ -4439,6 +4439,77 @@ can_use_directappend(sql_rel *rel)
        return copy_from;
 }
 
+static stmt *
+rel2bin_copyparpipe(backend *be, sql_rel *rel, list *refs, sql_exp *copyfrom)
+{
+       (void)rel;
+       (void)refs;
+
+       InstrPtr q;
+       MalBlkPtr mb = be->mb;
+       // mvc *mvc = be->mvc;
+       // sql_allocator *sa = mvc->sa;
+
+       // Extract arguments
+       list *copyfrom_args = copyfrom->l;
+       node *fname_node = copyfrom_args->h->next->next->next->next->next;
+       sql_exp *fname_exp = fname_node->data;
+       atom *fname_atom = fname_exp->l;
+       const char *fname = fname_atom->data.val.sval;
+
+       int streams_type = ATOMindex("streams");
+
+       q = newStmtArgs(mb, "streams", "openRead", 1);
+       setDestType(mb, q, streams_type);
+       q = pushStr(mb, q, fname);
+       int var_s = getDestVar(q);
+
+       q = newStmtArgs(mb, "bat", "new", 3);
+       setDestType(mb, q, newBatType(TYPE_bte));
+       q = pushNil(mb, q, TYPE_bte);
+       q = pushLng(mb, q, 300);
+       q = pushBit(mb, q, false);
+       int var_block = getDestVar(q);
+
+       q = newStmtArgs(mb, "copy", "read", 3);
+       setDestType(mb, q, TYPE_lng);
+       q = pushArgument(mb, q, var_s);
+       q = pushLng(mb, q, 200);
+       q = pushArgument(mb, q, var_block);
+       int var_nread = getDestVar(q);
+
+
+
+
+       add_to_rowcount_accumulator(be, var_nread);
+
+       stmt *dummy_stmt = stmt_none(be);
+       // dummy_stmt->nr = getDestVar(q);
+
+       return dummy_stmt;
+
+
+
+       // sql_subtype *str_type = sql_bind_localtype("str");
+       // sql_subtype *streams_type = sql_bind_localtype("streams");
+
+       // //  
batarg("",oid),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))),
+
+       // // Emit the filename
+       // node *fname_node = copyfrom_args->h->next->next->next->next->next;
+       // sql_exp *fname_exp = fname_node->data;
+       // atom *fname_atom = fname_exp->l;
+       // const char *fname = fname_atom->data.val.sval;
+       // stmt *fname_stmt = stmt_atom_string(be, sa_strdup(sa, fname));
+
+       // // sql_subfunc *f = sql_bind_func(mvc, "streams", "openRead", 
str_type, NULL, F_FUNC);
+       // sql_subfunc *f = sql_bind_func_result(mvc, "streams", "openRead", 
F_FUNC, streams_type, 1, str_type);
+       // stmt *openRead_stmt = stmt_unop(be, fname_stmt, NULL, f);
+
+       // snprintf(be->mvc->errstr, sizeof(be->mvc->errstr), "banana");
+       // return openRead_stmt;
+}
+
 // Temporarily emit the MAL to call to sql.copy_from and aggr.count directly
 // from rel2bin_insert. This needs to move to rel2bin_exp.
 static stmt *
@@ -4451,11 +4522,19 @@ rel2bin_directappend(backend *be, sql_re
        mvc *mvc = be->mvc;
        MalBlkPtr mb = be->mb;
 
+       list *args = copyfrom->l;
+       // X_27:bat[:oid] := sql.append_from(0x7f727c15e310:ptr, "|":str, 
"\n":str, nil:str, "null":str, "/tmp/joeri":str, -1:lng, 0:lng, 0:int, nil:str, 
0:int, 1:int); |
+       node *fname_node = args->h->next->next->next->next->next;
+       sql_exp *fname_exp = fname_node->data;
+       atom *fname_atom = fname_exp->l;
+       const char *fname = fname_atom->data.val.sval;
+       if (strstr(fname, "banana") != NULL)
+               return rel2bin_copyparpipe(be, rel, refs, copyfrom);
+
        // We're about to emit a custom sql.copy_from invocation.
        // Temporarily, until we learn how to do that properly.
 
        // First emit statements for all copyfrom's arguments.
-       list *args = copyfrom->l;
        list *l = sa_list(mvc->sa);
        for (node *n = args->h; n; n = n->next) {
                sql_exp *arg = n->data;
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to