Changeset: c9f7f9e711f1 for MonetDB URL: https://dev.monetdb.org/hg/MonetDB/rev/c9f7f9e711f1 Branch: default Log Message:
merged diffs (289 lines): diff --git a/ChangeLog b/ChangeLog --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,23 @@ # ChangeLog file for devel # This file is updated with Maddlog +* Wed Nov 18 2026 Martin van Dinther <[email protected]> +- Queries to a proto_loader('odbc:...') function with a limit-clause + will now push the limit value down to the ODBC driver via ODBC call + SQLSetStmtAttr(stmt, SQL_ATTR_MAX_ROWS, limit, 0). This reduces + the amount of rows retrieved from the connected ODBC Data Source. Example: + SELECT * FROM proto_loader('odbc:DRIVER=/usr/lib64/libsqlite3odbc.so; + Database=/home/user1/sqlite_dbs/chinook.db; + QUERY=SELECT * FROM tracks') LIMIT 10; + Note: this limit push down is only possible when there are no other + clauses between the FROM-clause and the LIMIT-OFFSET-clause or + OFFSET-FETCHFIRST-clause. If you need extra clauses specify + them in the QUERY= part of the string literal, example: + SELECT * FROM proto_loader('odbc:DRIVER=/usr/lib64/libsqlite3odbc.so; + Database=/home/user1/sqlite_dbs/chinook.db; + QUERY=SELECT * FROM tracks WHERE Name LIKE ''%Love%'' ORDER BY Name') + LIMIT 10; + * Wed Nov 18 2026 Ying Zhang <[email protected]> - Added support for tuple matching at projections, so now one can run queries like 'SELECT (1, 2) IN (SELECT 1, 2)' and 'SELECT (1, 2) NOT IN 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 @@ -1591,11 +1591,11 @@ exp2bin_proto_loader(backend *be, sql_ex assert(is_atom(fexp->type)); atom *fa = fexp->l; assert(fa->data.vtype == TYPE_str); - char *filename = fa->data.val.sval; + char *uri = fa->data.val.sval; sql_exp *topn = NULL; if (list_length(arg_list) == 3) topn = list_fetch(arg_list, 2); - return (stmt*)pl->load(be, f, filename, topn); + return (stmt*)pl->load(be, f, uri, topn); } stmt * diff --git a/sql/backends/monet5/vaults/odbc/odbc_loader.c b/sql/backends/monet5/vaults/odbc/odbc_loader.c --- a/sql/backends/monet5/vaults/odbc/odbc_loader.c +++ b/sql/backends/monet5/vaults/odbc/odbc_loader.c @@ -357,7 +357,7 @@ odbc_cleanup(SQLHANDLE env, SQLHANDLE db } } -/* copied from monetdb5/modules/mal/tablet.c */ +/* copied from monetdb5/modules/mal/tablet.c but with calling BATnegateprops(b); */ static BAT * bat_create(int adt, BUN nr) { @@ -371,16 +371,7 @@ bat_create(int adt, BUN nr) } /* disable all properties here */ - b->tsorted = false; - b->trevsorted = false; - b->tnosorted = 0; - b->tnorevsorted = 0; - b->tseqbase = oid_nil; - b->tkey = false; - b->tnokey[0] = 0; - b->tnokey[1] = 0; - b->tnil = false; - b->tnonil = false; + BATnegateprops(b); return b; } @@ -544,12 +535,14 @@ fraction2msec(SQLUINTEGER fraction, SQLS * the caller argument is ODBC_RELATION when called from odbc_relation and ODBC_LOADER when called from ODBCloader */ static str -odbc_query(int caller, mvc *sql, sql_subfunc *f, char *url, list *res_exps, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci) +odbc_query(int caller, mvc *sql, sql_subfunc *f, char *url, list *res_exps, lng nrows, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci) { + assert(caller == ODBC_RELATION || caller == ODBC_LOADER); + if (sql == NULL) - return "Missing mvc value."; + return "Missing mvc value"; if (f == NULL) - return "Missing sql_subfunc value."; + return "Missing sql_subfunc value"; /* check received url and extract the ODBC connection string and the SQL query */ if (!url || (url && strncasecmp("odbc:", url, 5) != 0)) @@ -668,6 +661,13 @@ odbc_query(int caller, mvc *sql, sql_sub } #endif + if (caller == ODBC_RELATION || (caller == ODBC_LOADER && nrows >= 0 && nrows <= 2147483647)) { + SQLULEN max_rows = (caller == ODBC_RELATION) ? 1 : (SQLULEN) nrows; + /* instruct connected DBMS to return max_rows */ + ret = SQLSetStmtAttr(stmt, SQL_ATTR_MAX_ROWS, (SQLPOINTER) (uintptr_t) max_rows, 0); + TRC_DEBUG(LOADER, "SQLSetStmtAttr(stmt, SQL_ATTR_MAX_ROWS, %lu, 0) returned %s\n", max_rows, nameOfRetCode(ret)); + } + uint16_t * query_Wstr = utf8toutf16(query); if (query_Wstr != NULL) { ret = SQLExecDirectW(stmt, (SQLWCHAR *) query_Wstr, SQL_NTS); @@ -768,6 +768,9 @@ odbc_query(int caller, mvc *sql, sql_sub /* when called from ODBCloader() */ if (caller == ODBC_LOADER) { + assert(mb); + assert(pci); + rescol_t * colmetadata = (rescol_t *) GDKzalloc(nr_cols * sizeof(rescol_t)); if (colmetadata == NULL) { errmsg = "GDKzalloc colmetadata[nr_cols] failed."; @@ -799,6 +802,7 @@ odbc_query(int caller, mvc *sql, sql_sub SQLULEN largestStringSize = 0; bool hasBlobCols = false; SQLULEN largestBlobSize = 0; + BUN init_size = (nrows >= 0 ? (BUN)nrows : 4096); /* make bats with right atom type */ for (SQLUSMALLINT col = 0; col < (SQLUSMALLINT) nr_cols; col++) { @@ -1015,13 +1019,13 @@ odbc_query(int caller, mvc *sql, sql_sub TRC_INFO(LOADER, "ResCol %u, name: %s, type %d (%s), size %u, decdigits %d, battype %d\n", (unsigned) col+1, cname, dataType, nameofSQLtype(dataType), (unsigned int)columnSize, decimalDigits, battype); - TRC_DEBUG(LOADER, "Before create BAT %d type %d\n", col+1, battype); - b = bat_create(battype, 0); + b = bat_create(battype, init_size); if (b) { colmetadata[col].bat = b; - TRC_DEBUG(LOADER, "Created BAT %d\n", col+1); + TRC_DEBUG(LOADER, "Created bat %d type %d size " BUNFMT "\n", col+1, battype, init_size); } else { errmsg = "Failed to create bat."; + TRC_ERROR(LOADER, "Failed to create bat %d type %d size " BUNFMT "\n", col+1, battype, init_size); /* cleanup already created bats */ while (col > 0) { col--; @@ -1083,6 +1087,10 @@ odbc_query(int caller, mvc *sql, sql_sub unsigned long row = 0; ret = SQLFetch(stmt); // TODO optimisation: use SQLExtendedFetch() to pull data array wise and use BUNappendmulti() while (ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO) { + /* when nrows is set, stop reading when nrows is reached */ + if (nrows >= 0 && row >= (unsigned long)nrows) + break; + row++; TRC_DEBUG(LOADER, "Fetched row %lu\n", row); @@ -1558,22 +1566,25 @@ static str odbc_relation(mvc *sql, sql_subfunc *f, char *url, list *res_exps, char *aname) { (void) aname; - return odbc_query(ODBC_RELATION, sql, f, url, res_exps, NULL, NULL, NULL); + return odbc_query(ODBC_RELATION, sql, f, url, res_exps, -1, NULL, NULL, NULL); } static void * odbc_load(void *BE, sql_subfunc *f, char *url, sql_exp *topn) { backend *be = (backend*)BE; + lng nrows = -1; + if (!f) return NULL; - (void)topn; - InstrPtr q = newStmtArgs(be->mb, "odbc", "loader", list_length(f->coltypes) + 2); if (q == NULL) return NULL; + if (topn && topn->type == e_atom) + nrows = ((atom*)topn->l)->data.val.lval; + int col = 0; list *l = sa_list(be->mvc->sa); for (node *n = f->coltypes->h, *nn = f->colnames->h; n && nn; col++, n = n->next, nn = nn->next) { @@ -1592,6 +1603,7 @@ odbc_load(void *BE, sql_subfunc *f, char } q = pushStr(be->mb, q, url); q = pushPtr(be->mb, q, f); + q = pushLng(be->mb, q, nrows); pushInstruction(be->mb, q); return stmt_list(be, l); } @@ -1600,19 +1612,22 @@ static str ODBCloader(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci) { backend *be = NULL; - str msg; - if ((msg = getBackendContext(cntxt, &be)) != NULL) + str msg = getBackendContext(cntxt, &be); + if (msg != NULL) return msg; str uri = *getArgReference_str(stk, pci, pci->retc); sql_subfunc *f = *(sql_subfunc**)getArgReference_ptr(stk, pci, pci->retc+1); - - return odbc_query(ODBC_LOADER, be->mvc, f, uri, NULL, mb, stk, pci); + lng nrows = *getArgReference_lng(stk, pci, pci->retc +2); + return odbc_query(ODBC_LOADER, be->mvc, f, uri, NULL, nrows, mb, stk, pci); } static str ODBCprelude(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci) { - (void)cntxt; (void)mb; (void)stk; (void)pci; + (void)cntxt; + (void)mb; + (void)stk; + (void)pci; pl_register("odbc", &odbc_relation, &odbc_load); return MAL_SUCCEED; } @@ -1620,7 +1635,7 @@ ODBCprelude(Client cntxt, MalBlkPtr mb, static str ODBCepilogue(Client cntxt, void *ret) { - (void) cntxt; + (void)cntxt; (void)ret; pl_unregister("odbc"); return MAL_SUCCEED; @@ -1632,7 +1647,7 @@ ODBCepilogue(Client cntxt, void *ret) static mel_func odbc_init_funcs[] = { pattern("odbc", "prelude", ODBCprelude, false, "", noargs), command("odbc", "epilogue", ODBCepilogue, false, "", noargs), - pattern("odbc", "loader", ODBCloader, true, "Import a query result via the odbc uri", args(1,3, batvarargany("",0),arg("uri",str),arg("func",ptr))), + pattern("odbc", "loader", ODBCloader, true, "Import a query result via the odbc uri", args(1, 4, batvarargany("",0), arg("uri",str), arg("func",ptr), arg("nrows", lng))), { .imp=NULL } }; diff --git a/sql/test/proto_loader/odbc/Tests/monetodbc.test b/sql/test/proto_loader/odbc/Tests/monetodbc.test --- a/sql/test/proto_loader/odbc/Tests/monetodbc.test +++ b/sql/test/proto_loader/odbc/Tests/monetodbc.test @@ -37,3 +37,17 @@ select * from 'odbc:DSN=MonetDB-Test;QUE ---- ƜMȸẞʬͶΘΩπ҈ڞ௵ᐂ‡‰⁂ℼ∜〈┌├┬┼ +query T nosort +select name from 'odbc:DSN=MonetDB-Test;QUERY=select distinct name from sys.tables order by name' LIMIT 4 +---- +_columns +_tables +args +auths + +query T nosort +select name from 'odbc:DSN=MonetDB-Test;QUERY=select distinct name from sys.tables order by name' OFFSET 2 ROWS FETCH FIRST 2 ROWS ONLY +---- +args +auths + diff --git a/sql/test/proto_loader/odbc/Tests/sqlite3odbc.test.in b/sql/test/proto_loader/odbc/Tests/sqlite3odbc.test.in --- a/sql/test/proto_loader/odbc/Tests/sqlite3odbc.test.in +++ b/sql/test/proto_loader/odbc/Tests/sqlite3odbc.test.in @@ -431,6 +431,29 @@ Heavy Metal Classic 18 On-The-Go 1 +query IT nosort +select * from proto_loader('odbc:DRIVER=/usr/lib64/libsqlite3odbc.so;Database=$QTSTDATAPATH/sqlite3odbc/chinook.db;QUERY=SELECT * FROM playlists') + OFFSET 3 ROWS FETCH FIRST 9 ROWS ONLY +---- +4 +Audiobooks +5 +90’s Music +6 +Audiobooks +7 +Movies +8 +Music +9 +Music Videos +10 +TV Shows +11 +Brazilian Music +12 +Classical + query ITIIITIIR nosort select * from proto_loader('odbc:DRIVER=/usr/lib64/libsqlite3odbc.so;Database=$QTSTDATAPATH/sqlite3odbc/chinook.db;QUERY=SELECT * FROM tracks') ---- _______________________________________________ checkin-list mailing list -- [email protected] To unsubscribe send an email to [email protected]
