Changeset: 57e6e3c55e51 for MonetDB URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=57e6e3c55e51 Added Files: monetdb5/optimizer/opt_append.c monetdb5/optimizer/opt_append.h Modified Files: monetdb5/optimizer/CMakeLists.txt monetdb5/optimizer/opt_bincopyfrom.c sql/backends/monet5/sql.c Branch: copybinary Log Message:
Split sql.append into sql.append_prep and sql.append_exec diffs (truncated from 343 to 300 lines): diff --git a/monetdb5/optimizer/CMakeLists.txt b/monetdb5/optimizer/CMakeLists.txt --- a/monetdb5/optimizer/CMakeLists.txt +++ b/monetdb5/optimizer/CMakeLists.txt @@ -12,6 +12,8 @@ target_sources(optimizer PRIVATE optimizer.c optimizer.h optimizer_private.h opt_aliases.c opt_aliases.h + opt_append.c opt_append.h + opt_bincopyfrom.c opt_bincopyfrom.h opt_coercion.c opt_coercion.h opt_commonTerms.c opt_commonTerms.h opt_candidates.c opt_candidates.h @@ -31,7 +33,6 @@ target_sources(optimizer opt_matpack.c opt_matpack.h opt_json.c opt_json.h opt_mergetable.c opt_mergetable.h - opt_bincopyfrom.c opt_bincopyfrom.h opt_mitosis.c opt_mitosis.h opt_multiplex.c opt_multiplex.h opt_oltp.c opt_oltp.h diff --git a/monetdb5/optimizer/opt_append.c b/monetdb5/optimizer/opt_append.c new file mode 100644 --- /dev/null +++ b/monetdb5/optimizer/opt_append.c @@ -0,0 +1,107 @@ +/* + * 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 - 2020 MonetDB B.V. + */ + +/* author Joeri van Ruth + * This optimizer replaces calls to sql.importTable with a series of calls to + * sql.importColumn. + */ +#include "monetdb_config.h" +#include "mal_builder.h" +#include "opt_append.h" + +static str transform(MalBlkPtr mb, InstrPtr importTable); + +str +OPTparappendImplementation(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; + + int found_at = -1; + for (int i = 0; i < mb->stop; i++) { + InstrPtr p = getInstrPtr(mb, i); + if (p->modname == sqlRef && p->fcnname == appendRef) { + 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.parappend", 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 == appendRef && isaBatType(getArgType(mb, p, 5))) { + msg = transform(mb, p); + } else { + pushInstruction(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) +{ + + // take the old instruction apart + assert(old->retc == 1); + assert(old->argc == 1 + 5); + int chain_out_var = getArg(old, 0); + int chain_in_var = getArg(old, 1); + int sname_var = getArg(old, 2); + int tname_var = getArg(old, 3); + int cname_var = getArg(old, 4); + int data_var = getArg(old, 5); + + bool sname_constant = isVarConstant(mb, sname_var); + bool tname_constant = isVarConstant(mb, tname_var); + bool cname_constant = isVarConstant(mb, cname_var); + + if (!sname_constant || !tname_constant || !cname_constant) { + // cannot transform this + pushInstruction(mb, old); + return MAL_SUCCEED; + } + + int cookie_var = newTmpVariable(mb, TYPE_ptr); + + str append_prepRef = putName("append_prep"); + InstrPtr p = newFcnCall(mb, sqlRef, append_prepRef); + setReturnArgument(p, chain_out_var); + pushReturn(mb, p, cookie_var); + pushArgument(mb, p, chain_in_var); + pushArgument(mb, p, sname_var); + pushArgument(mb, p, tname_var); + pushArgument(mb, p, cname_var); + + str append_execRef = putName("append_exec"); + InstrPtr e = newFcnCall(mb, sqlRef, append_execRef); + pushArgument(mb, e, cookie_var); + pushArgument(mb, e, data_var); + + // fprintf(stderr, "TRIGGER\n"); + + return MAL_SUCCEED; +} diff --git a/monetdb5/optimizer/opt_append.h b/monetdb5/optimizer/opt_append.h new file mode 100644 --- /dev/null +++ b/monetdb5/optimizer/opt_append.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 - 2020 MonetDB B.V. + */ + +#ifndef _OPT_PARAPPEND_ +#define _OPT_PARAPPEND_ +#include "opt_prelude.h" +#include "opt_support.h" +#include "mal_exception.h" +mal_export str OPTparappendImplementation(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr p); + +#endif 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 @@ -13,6 +13,7 @@ #include "monetdb_config.h" #include "mal_builder.h" #include "opt_bincopyfrom.h" +#include "opt_append.h" static str transform(MalBlkPtr mb, InstrPtr importTable); static int extract_column(MalBlkPtr mb, InstrPtr old, int idx, int proto_bat_var, int count_var); @@ -28,7 +29,6 @@ OPTbincopyfromImplementation(Client cntx (void)pci; str importTableRef = putName("importTable"); - str append_batRef = putName("append_bat"); int found_at = -1; for (int i = 0; i < mb->stop; i++) { @@ -53,13 +53,8 @@ OPTbincopyfromImplementation(Client cntx for (int i = 0; i < old_stop; i++) { InstrPtr p = old_mb_stmt[i]; if (p->modname == sqlRef && p->fcnname == importTableRef) { + importTable_seen = true; msg = transform(mb, p); - importTable_seen = true; - } else if (p->modname == sqlRef && p->fcnname == appendRef && isaBatType(getArgType(mb, p, 5))) { - if (importTable_seen) { - setFunctionId(p, append_batRef); - } - pushInstruction(mb, p); } else { pushInstruction(mb, p); } @@ -67,6 +62,9 @@ OPTbincopyfromImplementation(Client cntx return msg; } + if (importTable_seen) + msg = OPTparappendImplementation(cntxt, mb, stk, pci); + end: if (old_mb_stmt) GDKfree(old_mb_stmt); 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 @@ -1764,21 +1764,25 @@ mvc_append_bat_wrap(Client cntxt, MalBlk return MAL_SUCCEED; } +// tmp_1, cookie_1 := sql.append_prep(chain_0, s, t, c_1); str mvc_append_prep_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci) { - int *res = getArgReference_int(stk, pci, 0); + int *chain_out = getArgReference_int(stk, pci, 0); + ptr *cookie_out = getArgReference_ptr(stk, pci, 1); + int chain_in = *getArgReference_int(stk, pci, 2); mvc *m = NULL; str msg; - const char *sname = *getArgReference_str(stk, pci, 2); - const char *tname = *getArgReference_str(stk, pci, 3); - const char *cname = *getArgReference_str(stk, pci, 4); - assert(isaBatType(getArgType(mb, pci, 5))); - bat batid = *getArgReference_bat(stk, pci, 5); + const char *sname = *getArgReference_str(stk, pci, 3); + const char *tname = *getArgReference_str(stk, pci, 4); + const char *cname = *getArgReference_str(stk, pci, 5); sql_schema *s; sql_table *t; sql_column *c; + *chain_out = chain_in; + *cookie_out = NULL; + if (strNil(sname)) throw(SQL, "sql.append_bat", SQLSTATE(42000) "sql.append_bat schema name is nil"); if (strNil(tname)) @@ -1789,7 +1793,6 @@ mvc_append_prep_wrap(Client cntxt, MalBl if (cname[0] == '%') throw(SQL, "sql.append_bat", SQLSTATE(42000) "sql.append_bat not intended for indices: %s.%s.%s", sname, tname, cname); - *res = 0; if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL) return msg; if ((msg = checkSQLContext(cntxt)) != NULL) @@ -1804,85 +1807,30 @@ mvc_append_prep_wrap(Client cntxt, MalBl if (c == NULL) throw(SQL, "sql.append_bat", SQLSTATE(42S02) "Column missing %s.%s.%s", sname, tname, cname); - fprintf(stderr, "WOOOOOPIE1\n"); void *cookie = store_funcs.append_col_prep(m->session->tr, c); - BAT *b = BATdescriptor(batid); - if (b == NULL) - throw(SQL, "sql.append_exec", SQLSTATE(HY005) "Cannot access column descriptor %s.%s.%s", - sname,tname,cname); - if( b && BATcount(b) > 4096 && !b->batTransient) - BATmsync(b); - - fprintf(stderr, "WOOOOOPIE2\n"); - int ret = store_funcs.append_col_exec(cookie, b); - - if (b) { - BBPunfix(b->batCacheid); - } - - if (ret != LOG_OK) - throw(SQL, "sql_append_bat_exec", GDK_EXCEPTION); - + *cookie_out = cookie; return MAL_SUCCEED; } +// sql.append_exec(cookie_1, bat_1); str mvc_append_exec_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci) { - int *res = getArgReference_int(stk, pci, 0); - mvc *m = NULL; - str msg; - const char *sname = *getArgReference_str(stk, pci, 2); - const char *tname = *getArgReference_str(stk, pci, 3); - const char *cname = *getArgReference_str(stk, pci, 4); - assert(isaBatType(getArgType(mb, pci, 5))); - bat batid = *getArgReference_bat(stk, pci, 5); - sql_schema *s; - sql_table *t; - sql_column *c; - - if (strNil(sname)) - throw(SQL, "sql.append_bat", SQLSTATE(42000) "sql.append_bat schema name is nil"); - if (strNil(tname)) - throw(SQL, "sql.append_bat", SQLSTATE(42000) "sql.append_bat table name is nil"); - if (strNil(cname)) - throw(SQL, "sql.append_bat", SQLSTATE(42000) "sql.append_bat column name is nil"); - - if (cname[0] == '%') - throw(SQL, "sql.append_bat", SQLSTATE(42000) "sql.append_bat not intended for indices: %s.%s.%s", sname, tname, cname); - - *res = 0; - if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL) - return msg; - if ((msg = checkSQLContext(cntxt)) != NULL) - return msg; - s = mvc_bind_schema(m, sname); _______________________________________________ checkin-list mailing list checkin-list@monetdb.org https://www.monetdb.org/mailman/listinfo/checkin-list