LCOV - code coverage report
Current view: top level - sql/backends/monet5 - sql.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 1695 3136 54.0 %
Date: 2021-10-13 02:24:04 Functions: 72 109 66.1 %

          Line data    Source code
       1             : /*
       2             :  * This Source Code Form is subject to the terms of the Mozilla Public
       3             :  * License, v. 2.0.  If a copy of the MPL was not distributed with this
       4             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       5             :  *
       6             :  * Copyright 1997 - July 2008 CWI, August 2008 - 2021 MonetDB B.V.
       7             :  */
       8             : 
       9             : /*
      10             :  * authors M Kersten, N Nes
      11             :  * SQL support implementation
      12             :  * This module contains the wrappers around the SQL
      13             :  * multi-version-catalog and support routines copied
      14             :  * from the Version 4 code base.
      15             :  */
      16             : #include "monetdb_config.h"
      17             : #include "sql.h"
      18             : #include "mapi_prompt.h"
      19             : #include "sql_result.h"
      20             : #include "sql_gencode.h"
      21             : #include "sql_storage.h"
      22             : #include "sql_scenario.h"
      23             : #include "store_sequence.h"
      24             : #include "sql_optimizer.h"
      25             : #include "sql_datetime.h"
      26             : #include "sql_partition.h"
      27             : #include "rel_unnest.h"
      28             : #include "rel_optimizer.h"
      29             : #include "rel_partition.h"
      30             : #include "rel_distribute.h"
      31             : #include "rel_select.h"
      32             : #include "rel_rel.h"
      33             : #include "rel_exp.h"
      34             : #include "rel_dump.h"
      35             : #include "rel_bin.h"
      36             : #include "mal.h"
      37             : #include "mal_client.h"
      38             : #include "mal_interpreter.h"
      39             : #include "mal_module.h"
      40             : #include "mal_session.h"
      41             : #include "mal_resolve.h"
      42             : #include "mal_client.h"
      43             : #include "mal_interpreter.h"
      44             : #include "mal_profiler.h"
      45             : #include "bat5.h"
      46             : #include "opt_pipes.h"
      47             : #include "orderidx.h"
      48             : #include "clients.h"
      49             : #include "mal_instruction.h"
      50             : #include "mal_resource.h"
      51             : #include "mal_authorize.h"
      52             : #include "gdk_cand.h"
      53             : 
      54             : static int
      55             : rel_is_table(sql_rel *rel)
      56             : {
      57       11914 :         if (!rel || is_base(rel->op))
      58             :                 return 1;
      59             :         return 0;
      60             : }
      61             : 
      62             : static int
      63        6638 : exp_is_point_select(sql_exp *e)
      64             : {
      65        6638 :         if (!e)
      66             :                 return 1;
      67        6638 :         if (e->type == e_cmp && !e->f && e->flag == (int) cmp_equal) {
      68        4349 :                 sql_exp *r = e->r;
      69        4349 :                 sql_exp *l = e->l;
      70             : 
      71        4349 :                 if (!is_func(l->type) && r->card <= CARD_AGGR)
      72        4248 :                         return 1;
      73             :         }
      74             :         return 0;
      75             : }
      76             : 
      77             : static int
      78      362172 : rel_no_mitosis(mvc *sql, sql_rel *rel)
      79             : {
      80             :         if (mvc_highwater(sql))
      81           0 :                 return 0;
      82      620581 :         if (!rel || is_basetable(rel->op))
      83             :                 return 1;
      84             :         /* use mitosis on order topn */
      85      524370 :         if (is_topn(rel->op)) {
      86         382 :                 sql_rel *l = rel->l;
      87         382 :                 if (l && is_simple_project(l->op) && l->r)
      88             :                         return 0;
      89             :         }
      90      524181 :         if (is_topn(rel->op) || is_sample(rel->op) || is_simple_project(rel->op))
      91      188609 :                 return rel_no_mitosis(sql, rel->l);
      92      335572 :         if ((is_delete(rel->op) || is_truncate(rel->op)) && rel->card <= CARD_AGGR)
      93             :                 return 1;
      94      335412 :         if ((is_insert(rel->op) || is_update(rel->op)) && rel->card <= CARD_AGGR)
      95       69800 :                 return rel_no_mitosis(sql, rel->r);
      96      272250 :         if (is_select(rel->op) && rel_is_table(rel->l) && !list_empty(rel->exps)) {
      97             :                 /* just one point expression makes this a point query */
      98        6638 :                 if (exp_is_point_select(rel->exps->h->data))
      99        4248 :                         return 1;
     100             :         }
     101             :         return 0;
     102             : }
     103             : 
     104             : static int
     105      261553 : rel_need_distinct_query(sql_rel *rel)
     106             : {
     107             :         int need_distinct = 0;
     108             : 
     109      306252 :         while (rel && is_simple_project(rel->op))
     110       44699 :                 rel = rel->l;
     111      261553 :         if (rel && is_groupby(rel->op) && !list_empty(rel->exps) && list_empty(rel->r)) {
     112        1837 :                 for (node *n = rel->exps->h; n && !need_distinct; n = n->next) {
     113         997 :                         sql_exp *e = n->data;
     114             : 
     115         997 :                         if (e->type == e_aggr && need_distinct(e))
     116             :                                 need_distinct = 1;
     117             :                 }
     118             :         }
     119      261553 :         return need_distinct;
     120             : }
     121             : 
     122             : sql_rel *
     123      364243 : sql_symbol2relation(backend *be, symbol *sym)
     124             : {
     125             :         sql_rel *rel;
     126      364243 :         sql_query *query = query_create(be->mvc);
     127             :         lng Tbegin;
     128      364243 :         int extra_opts = be->mvc->emode != m_prepare;
     129             : 
     130      364243 :         rel = rel_semantic(query, sym);
     131      364243 :         Tbegin = GDKusec();
     132      364243 :         if (rel)
     133      362181 :                 rel = sql_processrelation(be->mvc, rel, extra_opts, extra_opts);
     134      364243 :         if (rel)
     135      362173 :                 rel = rel_distribute(be->mvc, rel);
     136      364243 :         if (rel)
     137      362172 :                 rel = rel_partition(be->mvc, rel);
     138      364243 :         if (rel && (rel_no_mitosis(be->mvc, rel) || rel_need_distinct_query(rel)))
     139      100673 :                 be->no_mitosis = 1;
     140      364243 :         be->reloptimizer = GDKusec() - Tbegin;
     141      364243 :         return rel;
     142             : }
     143             : 
     144             : /*
     145             :  * After the SQL statement has been executed, its data structures
     146             :  * should be garbage collected. For successful actions we have to finish
     147             :  * the transaction as well, e.g. commit or rollback.
     148             :  */
     149             : int
     150      444342 : sqlcleanup(backend *be, int err)
     151             : {
     152      444342 :         sql_destroy_params(be->mvc);
     153             : 
     154             :         /* some statements dynamically disable caching */
     155      444342 :         be->mvc->sym = NULL;
     156      444342 :         if (be->mvc->ta)
     157      444342 :                 be->mvc->ta = sa_reset(be->mvc->ta);
     158      444342 :         if (be->mvc->sa)
     159      444032 :                 be->mvc->sa = sa_reset(be->mvc->sa);
     160      444342 :         if (err >0)
     161         435 :                 be->mvc->session->status = -err;
     162      444342 :         if (err <0)
     163        3025 :                 be->mvc->session->status = err;
     164      444342 :         be->mvc->label = 0;
     165      444342 :         be->no_mitosis = 0;
     166      444342 :         scanner_query_processed(&(be->mvc->scanner));
     167      444342 :         return err;
     168             : }
     169             : 
     170             : /*
     171             :  * The internal administration of the SQL compilation and execution state
     172             :  * is administered by a state descriptor accessible in each phase.
     173             :  * Failure to find the state descriptor aborts the session.
     174             :  */
     175             : 
     176             : str
     177     6716270 : checkSQLContext(Client cntxt)
     178             : {
     179             :         backend *be;
     180             : 
     181     6716270 :         if (cntxt == NULL)
     182           0 :                 throw(SQL, "mvc", SQLSTATE(42005) "No client record");
     183     6716270 :         if (cntxt->sqlcontext == NULL)
     184           0 :                 throw(SQL, "mvc", SQLSTATE(42006) "SQL module not initialized");
     185             :         be = (backend *) cntxt->sqlcontext;
     186     6716270 :         if (be->mvc == NULL)
     187           0 :                 throw(SQL, "mvc", SQLSTATE(42006) "SQL module not initialized, mvc struct missing");
     188             :         return MAL_SUCCEED;
     189             : }
     190             : 
     191             : str
     192       78580 : getBackendContext(Client cntxt, backend **be)
     193             : {
     194             :         str msg;
     195             : 
     196       78580 :         if ((msg = checkSQLContext(cntxt)) != MAL_SUCCEED)
     197             :                 return msg;
     198       78580 :         *be = (backend *) cntxt->sqlcontext;
     199       78580 :         return MAL_SUCCEED;
     200             : }
     201             : 
     202             : str
     203     3273738 : getSQLContext(Client cntxt, MalBlkPtr mb, mvc **c, backend **b)
     204             : {
     205             :         backend *be;
     206             :         (void) mb;
     207             :         str msg;
     208             : 
     209     3273738 :         if ((msg = checkSQLContext(cntxt)) != MAL_SUCCEED)
     210             :                 return msg;
     211     3273577 :         be = (backend *) cntxt->sqlcontext;
     212     3273577 :         if (c)
     213     3273577 :                 *c = be->mvc;
     214     3273577 :         if (b)
     215         280 :                 *b = be;
     216             :         return MAL_SUCCEED;
     217             : }
     218             : 
     219             : str
     220      739970 : SQLmvc(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
     221             : {
     222      739970 :         mvc *sql = NULL;
     223             :         str msg;
     224      739970 :         int *res = getArgReference_int(stk, pci, 0);
     225             : 
     226      739970 :         if ((msg = getSQLContext(cntxt, mb, &sql, NULL)) != NULL)
     227             :                 return msg;
     228      739970 :         if ((msg = checkSQLContext(cntxt)) != NULL)
     229             :                 return msg;
     230      739970 :         *res = 0;
     231      739970 :         return MAL_SUCCEED;
     232             : }
     233             : 
     234             : str
     235           0 : SQLcommit(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
     236             : {
     237           0 :         mvc *sql = NULL;
     238             :         str msg;
     239             :         (void) stk;
     240             :         (void) pci;
     241             : 
     242           0 :         if ((msg = getSQLContext(cntxt, mb, &sql, NULL)) != NULL)
     243             :                 return msg;
     244           0 :         if ((msg = checkSQLContext(cntxt)) != NULL)
     245             :                 return msg;
     246             : 
     247           0 :         if (sql->session->auto_commit != 0)
     248           0 :                 throw(SQL, "sql.trans", SQLSTATE(2DM30) "COMMIT not allowed in auto commit mode");
     249           0 :         return mvc_commit(sql, 0, 0, false);
     250             : }
     251             : 
     252             : str
     253           0 : SQLabort(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
     254             : {
     255           0 :         mvc *sql = NULL;
     256             :         str msg;
     257             :         (void) stk;
     258             :         (void) pci;
     259             : 
     260           0 :         if ((msg = getSQLContext(cntxt, mb, &sql, NULL)) != NULL)
     261             :                 return msg;
     262           0 :         if ((msg = checkSQLContext(cntxt)) != NULL)
     263             :                 return msg;
     264             : 
     265           0 :         if (sql->session->tr->active) {
     266           0 :                 msg = mvc_rollback(sql, 0, NULL, false);
     267             :         }
     268             :         return msg;
     269             : }
     270             : 
     271             : static str
     272           2 : SQLshutdown_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
     273             : {
     274             :         str msg;
     275             : 
     276           2 :         if ((msg = CLTshutdown(cntxt, mb, stk, pci)) == MAL_SUCCEED) {
     277             :                 /* administer the shutdown in the system log */
     278           2 :                 TRC_INFO(SQL_TRANS, "Shutdown: %s\n", *getArgReference_str(stk, pci, 0));
     279             :         }
     280           2 :         return msg;
     281             : }
     282             : 
     283             : static str
     284           1 : SQLset_protocol(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
     285             : {
     286           1 :         const int protocol = *getArgReference_int(stk, pci, 1);
     287             : 
     288             :         (void) mb;
     289             :         (void) stk;
     290             : 
     291           1 :         if (!(
     292           1 :                 protocol == PROTOCOL_AUTO ||
     293             :                 protocol == PROTOCOL_9 ||
     294           1 :                 protocol == PROTOCOL_COLUMNAR))
     295             :         {
     296           0 :                 return createException(SQL, "sql.set_protocol", "unknown protocol: %d", protocol);
     297             :         }
     298             : 
     299           1 :         *getArgReference_int(stk, pci, 0) = (cntxt->protocol = (protocol_version) protocol);
     300             : 
     301           1 :         return MAL_SUCCEED;
     302             : }
     303             : 
     304             : str
     305       24930 : create_table_or_view(mvc *sql, char *sname, char *tname, sql_table *t, int temp, int replace)
     306             : {
     307             :         sql_allocator *osa;
     308       24930 :         sql_schema *s = mvc_bind_schema(sql, sname);
     309       24930 :         sql_table *nt = NULL, *ot;
     310             :         node *n;
     311             :         int check = 0;
     312       24930 :         const char *action = (temp == SQL_DECLARED_TABLE) ? "DECLARE" : (replace ? "CREATE OR REPLACE" : "CREATE");
     313       24930 :         const char *obj = t->query ? "VIEW" : "TABLE";
     314             :         str msg = MAL_SUCCEED;
     315             : 
     316       24930 :         if (store_readonly(sql->session->tr->store))
     317           0 :                 throw(SQL, "sql.catalog", SQLSTATE(25006) "schema statements cannot be executed on a readonly database.");
     318             : 
     319       24930 :         if (!s)
     320           0 :                 throw(SQL, "sql.catalog", SQLSTATE(3F000) "%s %s: schema '%s' doesn't exist", action, obj, sname);
     321       24930 :         if (temp != SQL_DECLARED_TABLE && (!mvc_schema_privs(sql, s) && !(isTempSchema(s) && temp == SQL_LOCAL_TEMP)))
     322           0 :                 throw(SQL, "sql.catalog", SQLSTATE(42000) "%s %s: insufficient privileges for user '%s' in schema '%s'",
     323             :                                                  action, obj, get_string_global_var(sql, "current_user"), s->base.name);
     324       24930 :         if ((ot = mvc_bind_table(sql, s, t->base.name))) {
     325          15 :                 if (replace) {
     326          15 :                         if (ot->type != t->type)
     327           0 :                                 throw(SQL, "sql.catalog", SQLSTATE(42000) "%s %s: unable to drop %s '%s': is a %s",
     328           0 :                                                                  action, obj, obj, t->base.name, TABLE_TYPE_DESCRIPTION(ot->type, ot->properties));
     329          15 :                         if (ot->system)
     330           0 :                                 throw(SQL, "sql.catalog", SQLSTATE(42000) "%s %s: cannot replace system %s '%s'", action, obj, obj, t->base.name);
     331          15 :                         if (mvc_check_dependency(sql, ot->base.id, isView(ot) ? VIEW_DEPENDENCY : TABLE_DEPENDENCY, NULL))
     332           1 :                                 throw(SQL, "sql.catalog", SQLSTATE(42000) "%s %s: cannot replace %s '%s', there are database objects which depend on it",
     333             :                                                                  action, obj, obj, t->base.name);
     334          14 :                         if ((msg = mvc_drop_table(sql, s, ot, 0)) != MAL_SUCCEED)
     335             :                                 return msg;
     336             :                 } else {
     337           0 :                         throw(SQL, "sql.catalog", SQLSTATE(42S01) "%s %s: name '%s' already in use", action, obj, t->base.name);
     338             :                 }
     339             :         }
     340       24929 :         if (temp == SQL_DECLARED_TABLE && ol_length(t->keys))
     341           0 :                 throw(SQL, "sql.catalog", SQLSTATE(42000) "%s %s: '%s' cannot have constraints", action, obj, t->base.name);
     342             : 
     343       24929 :         switch (sql_trans_create_table(&nt, sql->session->tr, s, tname, t->query, t->type, t->system, temp, t->commit_action, t->sz, t->properties)) {
     344           0 :                 case -1:
     345           0 :                         throw(SQL, "sql.catalog", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     346           4 :                 case -2:
     347             :                 case -3:
     348           4 :                         throw(SQL, "sql.catalog", SQLSTATE(42000) "%s %s: '%s' name conflicts", action, obj, t->base.name);
     349             :                 default:
     350             :                         break;
     351             :         }
     352       24925 :         osa = sql->sa;
     353       24925 :         sql->sa = sql->ta;
     354             :         /* first check default values */
     355      180389 :         for (n = ol_first_node(t->columns); n; n = n->next) {
     356      155466 :                 sql_column *c = n->data;
     357             : 
     358      155466 :                 if (c->def) {
     359             :                         /* TODO please don't place an auto incremented sequence in the default value */
     360             :                         const char *next_value_for = "next value for \"sys\".\"seq_";
     361             :                         sql_rel *r = NULL;
     362             : 
     363        1291 :                         sql->sa = sql->ta;
     364        1291 :                         r = rel_parse(sql, s, sa_message(sql->ta, "select %s;", c->def), m_deps);
     365        2581 :                         if (!r || !is_project(r->op) || !r->exps || list_length(r->exps) != 1 ||
     366        1290 :                                 exp_check_type(sql, &c->type, r, r->exps->h->data, type_equal) == NULL) {
     367           2 :                                 if (r)
     368           1 :                                         rel_destroy(r);
     369           2 :                                 sa_reset(sql->ta);
     370           2 :                                 sql->sa = osa;
     371           2 :                                 if (strlen(sql->errstr) > 6 && sql->errstr[5] == '!')
     372           2 :                                         throw(SQL, "sql.catalog", "%s", sql->errstr);
     373             :                                 else
     374           0 :                                         throw(SQL, "sql.catalog", SQLSTATE(42000) "%s", sql->errstr);
     375             :                         }
     376             :                         /* For a self incremented column, it's sequence will get a BEDROPPED_DEPENDENCY,
     377             :                                 so no additional dependencies are needed */
     378        1289 :                         if (strncmp(c->def, next_value_for, strlen(next_value_for)) != 0) {
     379        1096 :                                 list *blist = rel_dependencies(sql, r);
     380        1096 :                                 if (mvc_create_dependencies(sql, blist, nt->base.id, FUNC_DEPENDENCY)) {
     381           0 :                                         rel_destroy(r);
     382           0 :                                         sa_reset(sql->sa);
     383           0 :                                         throw(SQL, "sql.catalog", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     384             :                                 }
     385             :                         }
     386        1289 :                         rel_destroy(r);
     387        1289 :                         sa_reset(sql->sa);
     388             :                 }
     389             :         }
     390             : 
     391      180387 :         for (n = ol_first_node(t->columns); n; n = n->next) {
     392      155464 :                 sql_column *c = n->data, *copied = NULL;
     393             : 
     394      155464 :                 switch (mvc_copy_column(sql, nt, c, &copied)) {
     395           0 :                         case -1:
     396           0 :                                 sa_reset(sql->ta);
     397           0 :                                 sql->sa = osa;
     398           0 :                                 throw(SQL, "sql.catalog", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     399           0 :                         case -2:
     400             :                         case -3:
     401           0 :                                 sa_reset(sql->ta);
     402           0 :                                 sql->sa = osa;
     403           0 :                                 throw(SQL, "sql.catalog", SQLSTATE(42000) "CREATE TABLE: %s_%s_%s conflicts", s->base.name, t->base.name, c->base.name);
     404             :                         default:
     405             :                                 break;
     406             :                 }
     407      155464 :                 if (isPartitionedByColumnTable(t) && c->base.id == t->part.pcol->base.id)
     408          67 :                         nt->part.pcol = copied;
     409             :         }
     410       24923 :         if (isPartitionedByExpressionTable(t)) {
     411             :                 char *err = NULL;
     412             : 
     413          22 :                 _DELETE(nt->part.pexp->exp);
     414          22 :                 nt->part.pexp->exp = SA_STRDUP(sql->session->tr->sa, t->part.pexp->exp);
     415          22 :                 err = bootstrap_partition_expression(sql, nt, 1);
     416          22 :                 sa_reset(sql->ta);
     417          22 :                 if (err) {
     418           3 :                         sql->sa = osa;
     419           3 :                         return err;
     420             :                 }
     421             :         }
     422       24920 :         check = sql_trans_set_partition_table(sql->session->tr, nt);
     423       24920 :         if (check == -4) {
     424           0 :                 sql->sa = osa;
     425           0 :                 throw(SQL, "sql.catalog", SQLSTATE(42000) "CREATE TABLE: %s_%s: the partition's expression is too long", s->base.name, t->base.name);
     426       24920 :         } else if (check) {
     427           0 :                 sql->sa = osa;
     428           0 :                 throw(SQL, "sql.catalog", SQLSTATE(42000) "CREATE TABLE: %s_%s: an internal error occurred", s->base.name, t->base.name);
     429             :         }
     430             : 
     431       24920 :         if (t->idxs) {
     432       29851 :                 for (n = ol_first_node(t->idxs); n; n = n->next) {
     433        4931 :                         sql_idx *i = n->data;
     434             : 
     435        4931 :                         switch (mvc_copy_idx(sql, nt, i, NULL)) {
     436           0 :                                 case -1:
     437           0 :                                         sql->sa = osa;
     438           0 :                                         throw(SQL, "sql.catalog", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     439           0 :                                 case -2:
     440             :                                 case -3:
     441           0 :                                         sql->sa = osa;
     442           0 :                                         throw(SQL, "sql.catalog", SQLSTATE(42000) "CREATE TABLE: %s_%s_%s index conflicts", s->base.name, t->base.name, i->base.name);
     443             :                                 default:
     444             :                                         break;
     445             :                         }
     446             :                 }
     447             :         }
     448       24920 :         if (t->keys) {
     449       29845 :                 for (n = ol_first_node(t->keys); n; n = n->next) {
     450        4931 :                         sql_key *k = n->data;
     451             :                         char *err = NULL;
     452             : 
     453        4931 :                         err = sql_partition_validate_key(sql, nt, k, "CREATE");
     454        4931 :                         sa_reset(sql->ta);
     455        4931 :                         if (err) {
     456           6 :                                 sql->sa = osa;
     457           6 :                                 return err;
     458             :                         }
     459        4925 :                         switch (mvc_copy_key(sql, nt, k, NULL)) {
     460           0 :                                 case -1:
     461           0 :                                         sql->sa = osa;
     462           0 :                                         throw(SQL, "sql.catalog", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     463           0 :                                 case -2:
     464             :                                 case -3:
     465           0 :                                         sql->sa = osa;
     466           0 :                                         throw(SQL, "sql.catalog", SQLSTATE(42000) "CREATE TABLE: %s_%s_%s constraint conflicts", s->base.name, t->base.name, k->base.name);
     467             :                                 default:
     468             :                                         break;
     469             :                         }
     470             :                 }
     471             :         }
     472       24914 :         if (t->triggers) {
     473       24914 :                 for (n = ol_first_node(t->triggers); n; n = n->next) {
     474           0 :                         sql_trigger *tr = n->data;
     475             : 
     476           0 :                         switch (mvc_copy_trigger(sql, nt, tr, NULL)) {
     477           0 :                                 case -1:
     478           0 :                                         sql->sa = osa;
     479           0 :                                         throw(SQL, "sql.catalog", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     480           0 :                                 case -2:
     481             :                                 case -3:
     482           0 :                                         sql->sa = osa;
     483           0 :                                         throw(SQL, "sql.catalog", SQLSTATE(42000) "CREATE TABLE: %s_%s_%s trigger conflicts", s->base.name, t->base.name, nt->base.name);
     484             :                                 default:
     485             :                                         break;
     486             :                         }
     487             :                 }
     488             :         }
     489             :         /* also create dependencies when not renaming */
     490       24914 :         if (nt->query && isView(nt)) {
     491             :                 sql_rel *r = NULL;
     492             : 
     493       16615 :                 r = rel_parse(sql, s, nt->query, m_deps);
     494       16615 :                 if (r)
     495       16615 :                         r = sql_processrelation(sql, r, 0, 0);
     496       16615 :                 if (r) {
     497       16615 :                         list *blist = rel_dependencies(sql, r);
     498       16615 :                         if (mvc_create_dependencies(sql, blist, nt->base.id, VIEW_DEPENDENCY)) {
     499           0 :                                 sa_reset(sql->ta);
     500           0 :                                 sql->sa = osa;
     501           0 :                                 throw(SQL, "sql.catalog", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     502             :                         }
     503             :                 }
     504       16615 :                 sa_reset(sql->ta);
     505       16615 :                 if (!r) {
     506           0 :                         sql->sa = osa;
     507           0 :                         if (strlen(sql->errstr) > 6 && sql->errstr[5] == '!')
     508           0 :                                 throw(SQL, "sql.catalog", "%s", sql->errstr);
     509             :                         else
     510           0 :                                 throw(SQL, "sql.catalog", SQLSTATE(42000) "%s", sql->errstr);
     511             :                 }
     512             :         }
     513       24914 :         sql->sa = osa;
     514       24914 :         return MAL_SUCCEED;
     515             : }
     516             : 
     517             : static int
     518             : mvc_claim_slots(sql_trans *tr, sql_table *t, size_t cnt, BUN *offset, BAT **pos)
     519             : {
     520       71080 :         sqlstore *store = tr->store;
     521       71080 :         return store->storage_api.claim_tab(tr, t, cnt, offset, pos);
     522             : }
     523             : 
     524             : str
     525       71067 : mvc_claim_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
     526             : {
     527       71067 :         BUN *offset = (BUN*)getArgReference_oid(stk, pci, 0);
     528       71067 :         bat *res = getArgReference_bat(stk, pci, 1);
     529       71067 :         mvc *m = NULL;
     530             :         str msg;
     531       71067 :         const char *sname = *getArgReference_str(stk, pci, 3);
     532       71067 :         const char *tname = *getArgReference_str(stk, pci, 4);
     533       71067 :         lng cnt = *(lng*)getArgReference_lng(stk, pci, 5);
     534       71067 :         BAT *pos = NULL;
     535             :         sql_schema *s;
     536             :         sql_table *t;
     537             : 
     538       71067 :         *res = 0;
     539       71067 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
     540             :                 return msg;
     541       71067 :         if ((msg = checkSQLContext(cntxt)) != NULL)
     542             :                 return msg;
     543             : 
     544       71067 :         s = mvc_bind_schema(m, sname);
     545       71067 :         if (s == NULL)
     546           0 :                 throw(SQL, "sql.claim", SQLSTATE(3F000) "Schema missing %s", sname);
     547       71067 :         t = mvc_bind_table(m, s, tname);
     548       71067 :         if (t == NULL)
     549           0 :                 throw(SQL, "sql.claim", SQLSTATE(42S02) "Table missing %s.%s", sname, tname);
     550       71067 :         if (!isTable(t))
     551           0 :                 throw(SQL, "sql.claim", SQLSTATE(42000) "%s '%s' is not persistent", TABLE_TYPE_DESCRIPTION(t->type, t->properties), t->base.name);
     552       71067 :         if (mvc_claim_slots(m->session->tr, t, (size_t)cnt, offset, &pos) == LOG_OK) {
     553       71067 :                 *res = bat_nil;
     554       71067 :                 if (pos)
     555           9 :                         BBPkeepref(*res = pos->batCacheid);
     556       71067 :                 return MAL_SUCCEED;
     557             :         }
     558           0 :         throw(SQL, "sql.claim", SQLSTATE(3F000) "Could not claim slots");
     559             : }
     560             : 
     561             : str
     562           1 : create_table_from_emit(Client cntxt, char *sname, char *tname, sql_emit_col *columns, size_t ncols)
     563             : {
     564             :         size_t i;
     565           1 :         sql_table *t = NULL;
     566             :         sql_schema *s = NULL;
     567           1 :         mvc *sql = NULL;
     568             :         str msg = MAL_SUCCEED;
     569             : 
     570           1 :         if ((msg = getSQLContext(cntxt, NULL, &sql, NULL)) != NULL)
     571             :                 return msg;
     572           1 :         if ((msg = checkSQLContext(cntxt)) != NULL)
     573             :                 return msg;
     574             : 
     575           1 :         if (!sname)
     576             :                 sname = "sys";
     577           1 :         if (!(s = mvc_bind_schema(sql, sname)))
     578           0 :                 throw(SQL, "sql.catalog", SQLSTATE(3F000) "CREATE TABLE: no such schema '%s'", sname);
     579           1 :         if (!mvc_schema_privs(sql, s))
     580           0 :                 throw(SQL, "sql.catalog", SQLSTATE(42000) "CREATE TABLE: Access denied for %s to schema '%s'", get_string_global_var(sql, "current_user"), s->base.name);
     581           1 :         switch (mvc_create_table(&t, sql, s, tname, tt_table, 0, SQL_DECLARED_TABLE, CA_COMMIT, -1, 0)) {
     582           0 :                 case -1:
     583           0 :                         throw(SQL, "sql.catalog", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     584           0 :                 case -2:
     585             :                 case -3:
     586           0 :                         throw(SQL, "sql.catalog", SQLSTATE(42000) "CREATE TABLE: transaction conflict detected");
     587             :                 default:
     588             :                         break;
     589             :         }
     590             : 
     591           3 :         for (i = 0; i < ncols; i++) {
     592           2 :                 BAT *b = columns[i].b;
     593           2 :                 str atoname = ATOMname(b->ttype);
     594             :                 sql_subtype tpe;
     595           2 :                 sql_column *col = NULL;
     596             : 
     597           2 :                 if (!strcmp(atoname, "str"))
     598           2 :                         sql_find_subtype(&tpe, "clob", 0, 0);
     599             :                 else {
     600           0 :                         sql_subtype *t = sql_bind_localtype(atoname);
     601           0 :                         if (!t)
     602           0 :                                 throw(SQL, "sql.catalog", SQLSTATE(3F000) "CREATE TABLE: could not find type for column");
     603           0 :                         tpe = *t;
     604             :                 }
     605             : 
     606           2 :                 if (columns[i].name && columns[i].name[0] == '%')
     607           0 :                         throw(SQL, "sql.catalog", SQLSTATE(42000) "CREATE TABLE: generated labels not allowed in column names, use an alias instead");
     608           2 :                 switch (mvc_create_column(&col, sql, t, columns[i].name, &tpe)) {
     609           0 :                         case -1:
     610           0 :                                 throw(SQL, "sql.catalog", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     611           0 :                         case -2:
     612             :                         case -3:
     613           0 :                                 throw(SQL, "sql.catalog", SQLSTATE(42000) "CREATE TABLE: transaction conflict detected");
     614             :                         default:
     615             :                                 break;
     616             :                 }
     617             :         }
     618           1 :         if ((msg = create_table_or_view(sql, sname, t->base.name, t, 0, 0)) != MAL_SUCCEED)
     619             :                 return msg;
     620           1 :         if (!(t = mvc_bind_table(sql, s, tname)))
     621           0 :                 throw(SQL, "sql.catalog", SQLSTATE(3F000) "CREATE TABLE: could not bind table %s", tname);
     622             :         BUN offset;
     623           1 :         BAT *pos = NULL;
     624           1 :         if (mvc_claim_slots(sql->session->tr, t, BATcount(columns[0].b), &offset, &pos) != LOG_OK)
     625           0 :                 throw(SQL, "sql.catalog", SQLSTATE(3F000) "CREATE TABLE: Could not insert data");
     626           3 :         for (i = 0; i < ncols; i++) {
     627           2 :                 BAT *b = columns[i].b;
     628             :                 sql_column *col = NULL;
     629             : 
     630           2 :                 if (!(col = mvc_bind_column(sql, t, columns[i].name))) {
     631           0 :                         bat_destroy(pos);
     632           0 :                         throw(SQL, "sql.catalog", SQLSTATE(3F000) "CREATE TABLE: could not bind column %s", columns[i].name);
     633             :                 }
     634           2 :                 if ((msg = mvc_append_column(sql->session->tr, col, offset, pos, b)) != MAL_SUCCEED) {
     635           0 :                         bat_destroy(pos);
     636           0 :                         return msg;
     637             :                 }
     638             :         }
     639           1 :         bat_destroy(pos);
     640           1 :         return msg;
     641             : }
     642             : 
     643             : str
     644          12 : append_to_table_from_emit(Client cntxt, char *sname, char *tname, sql_emit_col *columns, size_t ncols)
     645             : {
     646             :         size_t i;
     647             :         sql_table *t;
     648             :         sql_schema *s;
     649          12 :         mvc *sql = NULL;
     650             :         str msg = MAL_SUCCEED;
     651             : 
     652          12 :         if ((msg = getSQLContext(cntxt, NULL, &sql, NULL)) != NULL)
     653             :                 return msg;
     654          12 :         if ((msg = checkSQLContext(cntxt)) != NULL)
     655             :                 return msg;
     656             : 
     657          12 :         if (!sname)
     658             :                 sname = "sys";
     659          12 :         if (!(s = mvc_bind_schema(sql, sname)))
     660           0 :                 throw(SQL, "sql.catalog", SQLSTATE(3F000) "APPEND TABLE: no such schema '%s'", sname);
     661          12 :         if (!(t = mvc_bind_table(sql, s, tname)))
     662           0 :                 throw(SQL, "sql.catalog", SQLSTATE(3F000) "APPEND TABLE: could not bind table %s", tname);
     663          12 :         if (!isTable(t))
     664           0 :                 throw(SQL, "sql.catalog", SQLSTATE(42000) "APPEND TABLE: %s '%s' is not persistent", TABLE_TYPE_DESCRIPTION(t->type, t->properties), t->base.name);
     665             :         BUN offset;
     666          12 :         BAT *pos = NULL;
     667          12 :         if (mvc_claim_slots(sql->session->tr, t, BATcount(columns[0].b), &offset, &pos) != LOG_OK)
     668           0 :                 throw(SQL, "sql.catalog", SQLSTATE(3F000) "APPEND TABLE: Could not append data");
     669          62 :         for (i = 0; i < ncols; i++) {
     670          50 :                 BAT *b = columns[i].b;
     671             :                 sql_column *col = NULL;
     672             : 
     673          50 :                 if (!(col = mvc_bind_column(sql, t, columns[i].name))) {
     674           0 :                         bat_destroy(pos);
     675           0 :                         throw(SQL, "sql.catalog", SQLSTATE(3F000) "APPEND TABLE: could not bind column %s", columns[i].name);
     676             :                 }
     677          50 :                 if ((msg = mvc_append_column(sql->session->tr, col, offset, pos, b)) != MAL_SUCCEED) {
     678           0 :                         bat_destroy(pos);
     679           0 :                         return msg;
     680             :                 }
     681             :         }
     682          12 :         bat_destroy(pos);
     683          12 :         return msg;
     684             : }
     685             : 
     686             : BAT *
     687     1621350 : mvc_bind(mvc *m, const char *sname, const char *tname, const char *cname, int access)
     688             : {
     689     1621350 :         sql_trans *tr = m->session->tr;
     690             :         BAT *b = NULL;
     691             :         sql_schema *s = NULL;
     692             :         sql_table *t = NULL;
     693             :         sql_column *c = NULL;
     694             : 
     695     1621350 :         s = mvc_bind_schema(m, sname);
     696     1621343 :         if (s == NULL)
     697             :                 return NULL;
     698     1621343 :         t = mvc_bind_table(m, s, tname);
     699     1621229 :         if (t == NULL || !isTable(t))
     700             :                 return NULL;
     701     1621229 :         c = mvc_bind_column(m, t, cname);
     702     1621137 :         if (c == NULL)
     703             :                 return NULL;
     704             : 
     705     1621137 :         sqlstore *store = tr->store;
     706     1621137 :         b = store->storage_api.bind_col(tr, c, access);
     707     1620794 :         return b;
     708             : }
     709             : 
     710             : /* setVariable(int *ret, str *sname, str *name, any value) */
     711             : str
     712         411 : setVariable(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
     713             : {
     714         411 :         int *res = getArgReference_int(stk, pci, 0);
     715         411 :         mvc *m = NULL;
     716             :         str msg;
     717         411 :         const char *sname = *getArgReference_str(stk, pci, 2);
     718         411 :         const char *varname = *getArgReference_str(stk, pci, 3);
     719         411 :         int mtype = getArgType(mb, pci, 4);
     720             :         sql_schema *s;
     721             :         sql_var *var;
     722             : 
     723         411 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
     724             :                 return msg;
     725         411 :         if ((msg = checkSQLContext(cntxt)) != NULL)
     726             :                 return msg;
     727             : 
     728         411 :         if (!(s = mvc_bind_schema(m, sname)))
     729           0 :                 throw(SQL, "sql.setVariable", SQLSTATE(3F000) "Cannot find the schema '%s'", sname);
     730             : 
     731         411 :         *res = 0;
     732         411 :         if (mtype < 0 || mtype >= 255)
     733           0 :                 throw(SQL, "sql.setVariable", SQLSTATE(42100) "Variable type error");
     734             : 
     735         411 :         if ((var = find_global_var(m, s, varname))) {
     736         501 :                 if (!strcmp("sys", s->base.name) && !strcmp("optimizer", varname)) {
     737         117 :                         const char *newopt = *getArgReference_str(stk, pci, 4);
     738             :                         char buf[18];
     739             : 
     740         117 :                         if (strNil(newopt))
     741          27 :                                 throw(SQL, "sql.setVariable", SQLSTATE(42000) "Variable '%s.%s' cannot be NULL", sname, varname);
     742         117 :                         if (!isOptimizerPipe(newopt) && strchr(newopt, (int) ';') == 0)
     743           5 :                                 throw(SQL, "sql.setVariable", SQLSTATE(42100) "optimizer '%s' unknown", newopt);
     744         112 :                         (void) snprintf(buf, sizeof(buf), "user_%d", cntxt->idx); /* should always suffice */
     745         112 :                         if (!isOptimizerPipe(newopt) || strcmp(buf, newopt) == 0) {
     746          23 :                                 if ((msg = addPipeDefinition(cntxt, buf, newopt)))
     747             :                                         return msg;
     748           1 :                                 if (!sqlvar_set_string(find_global_var(m, s, varname), buf))
     749           0 :                                         throw(SQL, "sql.setVariable", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     750          89 :                         } else if (!sqlvar_set_string(find_global_var(m, s, varname), newopt))
     751           0 :                                 throw(SQL, "sql.setVariable", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     752             :                 } else {
     753         294 :                         ValPtr ptr = &stk->stk[getArg(pci, 4)];
     754             : 
     755         294 :                         if ((msg = sql_update_var(m, s, varname, ptr)))
     756             :                                 return msg;
     757         279 :                         if (!sqlvar_set(var, ptr))
     758           0 :                                 throw(SQL, "sql.setVariable", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     759             :                 }
     760         369 :                 return MAL_SUCCEED;
     761             :         }
     762           0 :         throw(SQL, "sql.setVariable", SQLSTATE(42100) "Variable '%s.%s' unknown", sname, varname);
     763             : }
     764             : 
     765             : /* getVariable(int *ret, str *name) */
     766             : str
     767         927 : getVariable(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
     768             : {
     769         927 :         int mtype = getArgType(mb, pci, 0);
     770         927 :         mvc *m = NULL;
     771             :         str msg;
     772         927 :         const char *sname = *getArgReference_str(stk, pci, 2);
     773         927 :         const char *varname = *getArgReference_str(stk, pci, 3);
     774             :         ValRecord *dst, *src;
     775             :         sql_schema *s;
     776             :         sql_var *var;
     777             : 
     778         927 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
     779             :                 return msg;
     780         927 :         if ((msg = checkSQLContext(cntxt)) != NULL)
     781             :                 return msg;
     782             : 
     783         927 :         if (!(s = mvc_bind_schema(m, sname)))
     784           0 :                 throw(SQL, "sql.getVariable", SQLSTATE(3F000) "Cannot find the schema '%s'", sname);
     785         927 :         if (mtype < 0 || mtype >= 255)
     786           0 :                 throw(SQL, "sql.getVariable", SQLSTATE(42100) "Variable type error");
     787         927 :         if (!(var = find_global_var(m, s, varname)))
     788           0 :                 throw(SQL, "sql.getVariable", SQLSTATE(42100) "Variable '%s.%s' unknown", sname, varname);
     789         927 :         src = &(var->var.data);
     790         927 :         dst = &stk->stk[getArg(pci, 0)];
     791         927 :         if (VALcopy(dst, src) == NULL)
     792           0 :                 throw(MAL, "sql.getVariable", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     793             :         return MAL_SUCCEED;
     794             : }
     795             : 
     796             : str
     797           1 : sql_variables(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
     798             : {
     799           1 :         mvc *m = NULL;
     800             :         BAT *schemas, *names, *types, *values;
     801             :         str msg = MAL_SUCCEED;
     802           1 :         bat *s = getArgReference_bat(stk,pci,0);
     803           1 :         bat *n = getArgReference_bat(stk,pci,1);
     804           1 :         bat *t = getArgReference_bat(stk,pci,2);
     805           1 :         bat *v = getArgReference_bat(stk,pci,3);
     806             :         int nvars;
     807             : 
     808           1 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
     809             :                 return msg;
     810           1 :         if ((msg = checkSQLContext(cntxt)) != NULL)
     811             :                 return msg;
     812             : 
     813           1 :         nvars = list_length(m->global_vars);
     814           1 :         schemas = COLnew(0, TYPE_str, nvars, TRANSIENT);
     815           1 :         names = COLnew(0, TYPE_str, nvars, TRANSIENT);
     816           1 :         types = COLnew(0, TYPE_str, nvars, TRANSIENT);
     817           1 :         values = COLnew(0, TYPE_str, nvars, TRANSIENT);
     818           1 :         if (!schemas || !names || !types || !values) {
     819           0 :                 msg = createException(SQL, "sql.variables", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     820           0 :                 goto bailout;
     821             :         }
     822             : 
     823           1 :         if (m->global_vars) {
     824           9 :                 for (node *n = m->global_vars->h; n ; n = n->next) {
     825           8 :                         sql_var *var = (sql_var*) n->data;
     826           8 :                         atom value = var->var;
     827             :                         ValPtr myptr = &(value.data);
     828           8 :                         ValRecord val = (ValRecord) {.vtype = TYPE_void,};
     829             :                         gdk_return res;
     830             : 
     831           8 :                         if (value.tpe.type->localtype != TYPE_str) {
     832           4 :                                 ptr ok = VALcopy(&val, myptr);
     833           4 :                                 if (ok)
     834           4 :                                         ok = VALconvert(TYPE_str, &val);
     835           4 :                                 if (!ok) {
     836           0 :                                         VALclear(&val);
     837           0 :                                         msg = createException(SQL, "sql.variables", SQLSTATE(HY013) "Failed to convert variable '%s.%s' into a string", var->sname, var->name);
     838           0 :                                         goto bailout;
     839             :                                 }
     840             :                                 myptr = &val;
     841             :                         }
     842           8 :                         res = BUNappend(values, VALget(myptr), false);
     843           8 :                         VALclear(&val);
     844           8 :                         if (res != GDK_SUCCEED) {
     845           0 :                                 msg = createException(SQL, "sql.variables", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     846           0 :                                 goto bailout;
     847             :                         }
     848           8 :                         if (BUNappend(schemas, var->sname, false) != GDK_SUCCEED) {
     849           0 :                                 msg = createException(SQL, "sql.variables", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     850           0 :                                 goto bailout;
     851             :                         }
     852           8 :                         if (BUNappend(names, var->name, false) != GDK_SUCCEED) {
     853           0 :                                 msg = createException(SQL, "sql.variables", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     854           0 :                                 goto bailout;
     855             :                         }
     856           8 :                         if (BUNappend(types, value.tpe.type->base.name, false) != GDK_SUCCEED) {
     857           0 :                                 msg = createException(SQL, "sql.variables", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     858           0 :                                 goto bailout;
     859             :                         }
     860             :                 }
     861             :         }
     862             : 
     863           1 : bailout:
     864           1 :         if (msg) {
     865           0 :                 BBPreclaim(schemas);
     866           0 :                 BBPreclaim(names);
     867           0 :                 BBPreclaim(types);
     868           0 :                 BBPreclaim(values);
     869             :         } else {
     870           1 :                 BBPkeepref(*s = schemas->batCacheid);
     871           1 :                 BBPkeepref(*n = names->batCacheid);
     872           1 :                 BBPkeepref(*t = types->batCacheid);
     873           1 :                 BBPkeepref(*v = values->batCacheid);
     874             :         }
     875             :         return msg;
     876             : }
     877             : 
     878             : /* str mvc_logfile(int *d, str *filename); */
     879             : str
     880           0 : mvc_logfile(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
     881             : {
     882           0 :         mvc *m = NULL;
     883             :         str msg;
     884           0 :         const char *filename = *getArgReference_str(stk, pci, 1);
     885             : 
     886           0 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
     887             :                 return msg;
     888           0 :         if ((msg = checkSQLContext(cntxt)) != NULL)
     889             :                 return msg;
     890           0 :         if (m->scanner.log) {
     891           0 :                 close_stream(m->scanner.log);
     892           0 :                 m->scanner.log = NULL;
     893             :         }
     894             : 
     895           0 :         if (!strNil(filename)) {
     896           0 :                 if((m->scanner.log = open_wastream(filename)) == NULL)
     897           0 :                         throw(SQL, "sql.logfile", SQLSTATE(HY013) "%s", mnstr_peek_error(NULL));
     898             :         }
     899             :         return MAL_SUCCEED;
     900             : }
     901             : 
     902             : /* str mvc_next_value(lng *res, str *sname, str *seqname); */
     903             : str
     904         906 : mvc_next_value(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
     905             : {
     906         906 :         backend *be = NULL;
     907             :         str msg;
     908             :         sql_schema *s;
     909             :         sql_sequence *seq;
     910         906 :         lng *res = getArgReference_lng(stk, pci, 0);
     911         906 :         const char *sname = *getArgReference_str(stk, pci, 1);
     912         906 :         const char *seqname = *getArgReference_str(stk, pci, 2);
     913             : 
     914             :         (void)mb;
     915         906 :         if ((msg = getBackendContext(cntxt, &be)) != NULL)
     916             :                 return msg;
     917         906 :         if (!(s = mvc_bind_schema(be->mvc, sname)))
     918           0 :                 throw(SQL, "sql.next_value", SQLSTATE(3F000) "Cannot find the schema %s", sname);
     919         906 :         if (!mvc_schema_privs(be->mvc, s))
     920           0 :                 throw(SQL, "sql.next_value", SQLSTATE(42000) "Access denied for %s to schema '%s'", get_string_global_var(be->mvc, "current_user"), s->base.name);
     921         906 :         if (!(seq = find_sql_sequence(be->mvc->session->tr, s, seqname)))
     922           0 :                 throw(SQL, "sql.next_value", SQLSTATE(HY050) "Cannot find the sequence %s.%s", sname, seqname);
     923             : 
     924         906 :         if (seq_next_value(be->mvc->session->tr->store, seq, res)) {
     925         905 :                 be->last_id = *res;
     926         905 :                 sqlvar_set_number(find_global_var(be->mvc, mvc_bind_schema(be->mvc, "sys"), "last_id"), be->last_id);
     927         905 :                 return MAL_SUCCEED;
     928             :         }
     929           1 :         throw(SQL, "sql.next_value", SQLSTATE(HY050) "Cannot generate next sequence value %s.%s", sname, seqname);
     930             : }
     931             : 
     932             : /* str mvc_get_value(lng *res, str *sname, str *seqname); */
     933             : str
     934          22 : mvc_get_value(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
     935             : {
     936          22 :         mvc *m = NULL;
     937             :         str msg;
     938             :         sql_schema *s;
     939             :         sql_sequence *seq;
     940          22 :         lng *res = getArgReference_lng(stk, pci, 0);
     941          22 :         const char *sname = *getArgReference_str(stk, pci, 1);
     942          22 :         const char *seqname = *getArgReference_str(stk, pci, 2);
     943             : 
     944          22 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
     945             :                 return msg;
     946          22 :         if ((msg = checkSQLContext(cntxt)) != NULL)
     947             :                 return msg;
     948          22 :         if (!(s = mvc_bind_schema(m, sname)))
     949           0 :                 throw(SQL, "sql.get_value", SQLSTATE(3F000) "Cannot find the schema %s", sname);
     950          22 :         if (!(seq = find_sql_sequence(m->session->tr, s, seqname)))
     951           0 :                 throw(SQL, "sql.get_value", SQLSTATE(HY050) "Cannot find the sequence %s.%s", sname, seqname);
     952             : 
     953          22 :         if (seq_get_value(m->session->tr->store, seq, res))
     954             :                 return MAL_SUCCEED;
     955           0 :         throw(SQL, "sql.get_value", SQLSTATE(HY050) "Cannot get sequence value %s.%s", sname, seqname);
     956             : }
     957             : 
     958             : static str
     959         222 : mvc_bat_next_get_value(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci, int (*bulk_func)(seqbulk *, lng *), const char *call, const char *action)
     960             : {
     961         222 :         mvc *m = NULL;
     962             :         str msg = MAL_SUCCEED, sname = NULL, seqname = NULL;
     963             :         BAT *b = NULL, *c = NULL, *r = NULL, *it;
     964             :         BUN p, q;
     965             :         sql_schema *s = NULL;
     966             :         sql_sequence *seq = NULL;
     967             :         seqbulk *sb = NULL;
     968         222 :         BATiter bi =  (BATiter) { .b = NULL }, ci =  (BATiter) { .b = NULL };
     969         222 :         bat *res = getArgReference_bat(stk, pci, 0);
     970             :         bat schid = 0, seqid = 0;
     971             : 
     972         222 :         if (isaBatType(getArgType(mb, pci, 1)))
     973         214 :                 schid = *getArgReference_bat(stk, pci, 1);
     974             :         else
     975           8 :                 sname = *getArgReference_str(stk, pci, 1);
     976         222 :         if (isaBatType(getArgType(mb, pci, 2)))
     977         135 :                 seqid = *getArgReference_bat(stk, pci, 2);
     978             :         else
     979          87 :                 seqname = *getArgReference_str(stk, pci, 2);
     980             : 
     981         222 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
     982             :                 return msg;
     983         222 :         if ((msg = checkSQLContext(cntxt)) != NULL)
     984             :                 return msg;
     985         222 :         sqlstore *store = m->session->tr->store;
     986             : 
     987         222 :         if (schid && !(b = BATdescriptor(schid))) {
     988           0 :                 msg = createException(SQL, call, SQLSTATE(HY005) "Cannot access column descriptor");
     989           0 :                 goto bailout;
     990             :         }
     991         221 :         if (seqid && !(c = BATdescriptor(seqid))) {
     992           0 :                 msg = createException(SQL, call, SQLSTATE(HY005) "Cannot access column descriptor");
     993           0 :                 goto bailout;
     994             :         }
     995         221 :         assert(b || c);
     996         221 :         it = b ? b : c; /* Either b or c must be set */
     997             : 
     998         221 :         if (!(r = COLnew(it->hseqbase, TYPE_lng, BATcount(it), TRANSIENT))) {
     999           0 :                 msg = createException(SQL, call, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1000           0 :                 goto bailout;
    1001             :         }
    1002             : 
    1003         221 :         if (!BATcount(it))
    1004         119 :                 goto bailout; /* Success case */
    1005             : 
    1006         102 :         bi = bat_iterator(b);
    1007         102 :         ci = bat_iterator(c);
    1008             : 
    1009       29382 :         BATloop(it, p, q) {
    1010             :                 str nsname, nseqname;
    1011             :                 lng l;
    1012             : 
    1013       29280 :                 if (b)
    1014       29276 :                         nsname = BUNtvar(bi, p);
    1015             :                 else
    1016             :                         nsname = sname;
    1017       29280 :                 if (c)
    1018          52 :                         nseqname = BUNtvar(ci, p);
    1019             :                 else
    1020             :                         nseqname = seqname;
    1021             : 
    1022       29280 :                 if (!s || strcmp(s->base.name, nsname) != 0 || !seq || strcmp(seq->base.name, nseqname) != 0) {
    1023         133 :                         if (sb) {
    1024          31 :                                 seqbulk_destroy(store, sb);
    1025             :                                 sb = NULL;
    1026             :                         }
    1027             :                         seq = NULL;
    1028         133 :                         if ((!s || strcmp(s->base.name, nsname) != 0) && !(s = mvc_bind_schema(m, nsname))) {
    1029           0 :                                 msg = createException(SQL, call, SQLSTATE(3F000) "Cannot find the schema %s", nsname);
    1030           0 :                                 goto bailout1;
    1031             :                         }
    1032         133 :                         if (bulk_func == seqbulk_next_value && !mvc_schema_privs(m, s)) {
    1033           0 :                                 msg = createException(SQL, call, SQLSTATE(42000) "Access denied for %s to schema '%s'", get_string_global_var(m, "current_user"), s->base.name);
    1034           0 :                                 goto bailout1;
    1035             :                         }
    1036         133 :                         if (!(seq = find_sql_sequence(m->session->tr, s, nseqname)) || !(sb = seqbulk_create(store, seq, BATcount(it)))) {
    1037           0 :                                 msg = createException(SQL, call, SQLSTATE(HY050) "Cannot find the sequence %s.%s", nsname, nseqname);
    1038           0 :                                 goto bailout1;
    1039             :                         }
    1040             :                 }
    1041       29280 :                 if (!bulk_func(sb, &l)) {
    1042           0 :                         msg = createException(SQL, call, SQLSTATE(HY050) "Cannot %s sequence value %s.%s", action, nsname, nseqname);
    1043           0 :                         goto bailout1;
    1044             :                 }
    1045       29280 :                 if (BUNappend(r, &l, false) != GDK_SUCCEED) {
    1046           0 :                         msg = createException(SQL, call, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1047           0 :                         goto bailout1;
    1048             :                 }
    1049             :         }
    1050         102 : bailout1:
    1051         102 :         bat_iterator_end(&bi);
    1052         102 :         bat_iterator_end(&ci);
    1053             : 
    1054         221 : bailout:
    1055         221 :         if (sb)
    1056         102 :                 seqbulk_destroy(store, sb);
    1057         221 :         if (b)
    1058         213 :                 BBPunfix(b->batCacheid);
    1059         222 :         if (c)
    1060         135 :                 BBPunfix(c->batCacheid);
    1061         222 :         if (msg)
    1062           0 :                 BBPreclaim(r);
    1063             :         else
    1064         222 :                 BBPkeepref(*res = r->batCacheid);
    1065             :         return msg;
    1066             : }
    1067             : 
    1068             : str
    1069          91 : mvc_bat_next_value(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    1070             : {
    1071          91 :         return mvc_bat_next_get_value(cntxt, mb, stk, pci, seqbulk_next_value, "sql.next_value", "generate next");
    1072             : }
    1073             : 
    1074             : str
    1075         131 : mvc_bat_get_value(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    1076             : {
    1077         131 :         return mvc_bat_next_get_value(cntxt, mb, stk, pci, seqbulk_get_value, "sql.get_value", "get");
    1078             : }
    1079             : 
    1080             : str
    1081           0 : mvc_getVersion(lng *version, const int *clientid)
    1082             : {
    1083           0 :         mvc *m = NULL;
    1084           0 :         Client cntxt = MCgetClient(*clientid);
    1085             :         str msg;
    1086             : 
    1087           0 :         if ((msg = getSQLContext(cntxt, NULL, &m, NULL)) != NULL)
    1088             :                 return msg;
    1089           0 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    1090             :                 return msg;
    1091           0 :         *version = -1;
    1092           0 :         if (m->session->tr)
    1093           0 :                 *version = (lng)m->session->tr->ts;
    1094             :         return MAL_SUCCEED;
    1095             : }
    1096             : 
    1097             : /* str mvc_restart_seq(lng *res, str *sname, str *seqname, lng *start); */
    1098             : str
    1099           0 : mvc_restart_seq(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    1100             : {
    1101           0 :         mvc *m = NULL;
    1102             :         str msg;
    1103             :         sql_schema *s;
    1104             :         sql_sequence *seq;
    1105           0 :         lng *res = getArgReference_lng(stk, pci, 0);
    1106           0 :         const char *sname = *getArgReference_str(stk, pci, 1);
    1107           0 :         const char *seqname = *getArgReference_str(stk, pci, 2);
    1108           0 :         lng start = *getArgReference_lng(stk, pci, 3);
    1109             : 
    1110           0 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    1111             :                 return msg;
    1112           0 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    1113             :                 return msg;
    1114           0 :         if (!(s = mvc_bind_schema(m, sname)))
    1115           0 :                 throw(SQL, "sql.restart", SQLSTATE(3F000) "Cannot find the schema %s", sname);
    1116           0 :         if (!mvc_schema_privs(m, s))
    1117           0 :                 throw(SQL, "sql.restart", SQLSTATE(42000) "Access denied for %s to schema '%s'", get_string_global_var(m, "current_user"), s->base.name);
    1118           0 :         if (!(seq = find_sql_sequence(m->session->tr, s, seqname)))
    1119           0 :                 throw(SQL, "sql.restart", SQLSTATE(HY050) "Failed to fetch sequence %s.%s", sname, seqname);
    1120           0 :         if (is_lng_nil(start))
    1121           0 :                 throw(SQL, "sql.restart", SQLSTATE(HY050) "Cannot (re)start sequence %s.%s with NULL", sname, seqname);
    1122           0 :         if (seq->minvalue && start < seq->minvalue)
    1123           0 :                 throw(SQL, "sql.restart", SQLSTATE(HY050) "Cannot set sequence %s.%s start to a value lesser than the minimum ("LLFMT" < "LLFMT")", sname, seqname, start, seq->minvalue);
    1124           0 :         if (seq->maxvalue && start > seq->maxvalue)
    1125           0 :                 throw(SQL, "sql.restart", SQLSTATE(HY050) "Cannot set sequence %s.%s start to a value higher than the maximum ("LLFMT" > "LLFMT")", sname, seqname, start, seq->maxvalue);
    1126           0 :         switch (sql_trans_sequence_restart(m->session->tr, seq, start)) {
    1127           0 :                 case -1:
    1128           0 :                         throw(SQL,"sql.restart",SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1129           0 :                 case -2:
    1130             :                 case -3:
    1131           0 :                         throw(SQL,"sql.restart",SQLSTATE(42000) "RESTART SEQUENCE: transaction conflict detected");
    1132           0 :                 case -4:
    1133           0 :                         throw(SQL,"sql.restart",SQLSTATE(HY050) "Cannot (re)start sequence %s.%s", sname, seqname);
    1134           0 :                 default:
    1135           0 :                         *res = start;
    1136             :         }
    1137           0 :         return MAL_SUCCEED;
    1138             : }
    1139             : 
    1140             : str
    1141           1 : mvc_bat_restart_seq(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    1142             : {
    1143           1 :         mvc *m = NULL;
    1144             :         str msg = MAL_SUCCEED, sname = NULL, seqname = NULL;
    1145             :         BAT *b = NULL, *c = NULL, *d = NULL, *r = NULL, *it;
    1146             :         BUN p, q;
    1147             :         sql_schema *s = NULL;
    1148             :         sql_sequence *seq = NULL;
    1149             :         seqbulk *sb = NULL;
    1150             :         BATiter bi, ci, di;
    1151           1 :         bat *res = getArgReference_bat(stk, pci, 0);
    1152             :         bat schid = 0, seqid = 0, startid = 0;
    1153             :         lng start = 0, *dptr = NULL;
    1154             : 
    1155           1 :         if (isaBatType(getArgType(mb, pci, 1)))
    1156           1 :                 schid = *getArgReference_bat(stk, pci, 1);
    1157             :         else
    1158           0 :                 sname = *getArgReference_str(stk, pci, 1);
    1159           1 :         if (isaBatType(getArgType(mb, pci, 2)))
    1160           1 :                 seqid = *getArgReference_bat(stk, pci, 2);
    1161             :         else
    1162           0 :                 seqname = *getArgReference_str(stk, pci, 2);
    1163           1 :         if (isaBatType(getArgType(mb, pci, 3)))
    1164           0 :                 startid = *getArgReference_bat(stk, pci, 3);
    1165             :         else
    1166           1 :                 start = *getArgReference_lng(stk, pci, 3);
    1167             : 
    1168           1 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    1169             :                 return msg;
    1170           1 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    1171             :                 return msg;
    1172             : 
    1173           1 :         sqlstore *store = m->session->tr->store;
    1174           1 :         if (schid && !(b = BATdescriptor(schid))) {
    1175           0 :                 msg = createException(SQL, "sql.restart", SQLSTATE(HY005) "Cannot access column descriptor");
    1176           0 :                 goto bailout;
    1177             :         }
    1178           1 :         if (seqid && !(c = BATdescriptor(seqid))) {
    1179           0 :                 msg = createException(SQL, "sql.restart", SQLSTATE(HY005) "Cannot access column descriptor");
    1180           0 :                 goto bailout;
    1181             :         }
    1182           1 :         if (startid && !(d = BATdescriptor(startid))) {
    1183           0 :                 msg = createException(SQL, "sql.restart", SQLSTATE(HY005) "Cannot access column descriptor");
    1184           0 :                 goto bailout;
    1185             :         }
    1186           1 :         assert(b || c || d);
    1187           1 :         it = b ? b : c ? c : d; /* Either b, c or d must be set */
    1188             : 
    1189           1 :         if (!(r = COLnew(it->hseqbase, TYPE_lng, BATcount(it), TRANSIENT))) {
    1190           0 :                 msg = createException(SQL, "sql.restart", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1191           0 :                 goto bailout;
    1192             :         }
    1193             : 
    1194           1 :         if (!BATcount(it))
    1195           0 :                 goto bailout; /* Success case */
    1196             : 
    1197           1 :         bi = bat_iterator(b);
    1198           1 :         ci = bat_iterator(c);
    1199           1 :         di = bat_iterator(d);
    1200           1 :         if (d)
    1201           0 :                 dptr = (lng *) di.base;
    1202             : 
    1203           3 :         BATloop(it, p, q) {
    1204             :                 str nsname, nseqname;
    1205             :                 lng nstart;
    1206             : 
    1207           2 :                 if (b)
    1208           2 :                         nsname = BUNtvar(bi, p);
    1209             :                 else
    1210             :                         nsname = sname;
    1211           2 :                 if (c)
    1212           2 :                         nseqname = BUNtvar(ci, p);
    1213             :                 else
    1214             :                         nseqname = seqname;
    1215           2 :                 if (dptr)
    1216           0 :                         nstart = dptr[p];
    1217             :                 else
    1218           2 :                         nstart = start;
    1219             : 
    1220           2 :                 if (!s || strcmp(s->base.name, nsname) != 0 || !seq || strcmp(seq->base.name, nseqname) != 0) {
    1221           2 :                         if (sb) {
    1222           1 :                                 seqbulk_destroy(store, sb);
    1223             :                                 sb = NULL;
    1224             :                         }
    1225             :                         seq = NULL;
    1226           2 :                         if ((!s || strcmp(s->base.name, nsname) != 0) && !(s = mvc_bind_schema(m, nsname))) {
    1227           0 :                                 msg = createException(SQL, "sql.restart", SQLSTATE(3F000) "Cannot find the schema %s", nsname);
    1228           0 :                                 goto bailout1;
    1229             :                         }
    1230           2 :                         if (!mvc_schema_privs(m, s)) {
    1231           0 :                                 msg = createException(SQL, "sql.restart", SQLSTATE(42000) "Access denied for %s to schema '%s'", get_string_global_var(m, "current_user"), s->base.name);
    1232           0 :                                 goto bailout1;
    1233             :                         }
    1234           2 :                         if (!(seq = find_sql_sequence(m->session->tr, s, nseqname)) || !(sb = seqbulk_create(store, seq, BATcount(it)))) {
    1235           0 :                                 msg = createException(SQL, "sql.restart", SQLSTATE(HY050) "Cannot find the sequence %s.%s", nsname, nseqname);
    1236           0 :                                 goto bailout1;
    1237             :                         }
    1238             :                 }
    1239           2 :                 if (is_lng_nil(nstart)) {
    1240           0 :                         msg = createException(SQL, "sql.restart", SQLSTATE(HY050) "Cannot (re)start sequence %s.%s with NULL", sname, seqname);
    1241           0 :                         goto bailout1;
    1242             :                 }
    1243           2 :                 if (seq->minvalue && nstart < seq->minvalue) {
    1244           0 :                         msg = createException(SQL, "sql.restart", SQLSTATE(HY050) "Cannot set sequence %s.%s start to a value lesser than the minimum ("LLFMT" < "LLFMT")", sname, seqname, start, seq->minvalue);
    1245           0 :                         goto bailout1;
    1246             :                 }
    1247           2 :                 if (seq->maxvalue && nstart > seq->maxvalue) {
    1248           0 :                         msg = createException(SQL, "sql.restart", SQLSTATE(HY050) "Cannot set sequence %s.%s start to a value higher than the maximum ("LLFMT" > "LLFMT")", sname, seqname, start, seq->maxvalue);
    1249           0 :                         goto bailout1;
    1250             :                 }
    1251           2 :                 switch (sql_trans_seqbulk_restart(m->session->tr, sb, nstart)) {
    1252           0 :                         case -1:
    1253           0 :                                 msg = createException(SQL,"sql.restart",SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1254           0 :                                 goto bailout1;
    1255           0 :                         case -2:
    1256             :                         case -3:
    1257           0 :                                 msg = createException(SQL,"sql.restart",SQLSTATE(42000) "RESTART SEQUENCE: transaction conflict detected");
    1258           0 :                                 goto bailout1;
    1259           0 :                         case -4:
    1260           0 :                                 msg = createException(SQL,"sql.restart",SQLSTATE(HY050) "Cannot restart sequence %s.%s", nsname, nseqname);
    1261           0 :                                 goto bailout1;
    1262             :                         default:
    1263             :                                 break;
    1264             :                 }
    1265           2 :                 if (BUNappend(r, &nstart, false) != GDK_SUCCEED) {
    1266           0 :                         msg = createException(SQL, "sql.restart", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1267           0 :                         goto bailout1;
    1268             :                 }
    1269             :         }
    1270           1 : bailout1:
    1271           1 :         bat_iterator_end(&bi);
    1272           1 :         bat_iterator_end(&ci);
    1273           1 :         bat_iterator_end(&di);
    1274             : 
    1275           1 : bailout:
    1276           1 :         if (sb)
    1277           1 :                 seqbulk_destroy(store, sb);
    1278           1 :         if (b)
    1279           1 :                 BBPunfix(b->batCacheid);
    1280           1 :         if (c)
    1281           1 :                 BBPunfix(c->batCacheid);
    1282           1 :         if (d)
    1283           0 :                 BBPunfix(d->batCacheid);
    1284           1 :         if (msg)
    1285           0 :                 BBPreclaim(r);
    1286             :         else
    1287           1 :                 BBPkeepref(*res = r->batCacheid);
    1288             :         return msg;
    1289             : }
    1290             : 
    1291             : BAT *
    1292        6039 : mvc_bind_idxbat(mvc *m, const char *sname, const char *tname, const char *iname, int access)
    1293             : {
    1294        6039 :         sql_trans *tr = m->session->tr;
    1295             :         BAT *b = NULL;
    1296             :         sql_schema *s = NULL;
    1297             :         sql_idx *i = NULL;
    1298             : 
    1299        6039 :         s = mvc_bind_schema(m, sname);
    1300        6038 :         if (s == NULL)
    1301             :                 return NULL;
    1302        6038 :         i = mvc_bind_idx(m, s, iname);
    1303        6038 :         if (i == NULL || !isTable(i->t))
    1304             :                 return NULL;
    1305             : 
    1306             :         (void) tname;
    1307        6038 :         sqlstore *store = tr->store;
    1308        6038 :         b = store->storage_api.bind_idx(tr, i, access);
    1309        6040 :         return b;
    1310             : }
    1311             : 
    1312             : /* str mvc_bind_wrap(int *bid, str *sname, str *tname, str *cname, int *access); */
    1313             : str
    1314     1455829 : mvc_bind_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    1315             : {
    1316     1455829 :         int upd = (pci->argc == 7 || pci->argc == 9);
    1317             :         BAT *b = NULL, *bn;
    1318     1455829 :         bat *bid = getArgReference_bat(stk, pci, 0);
    1319     1455829 :         int coltype = getBatType(getArgType(mb, pci, 0));
    1320     1455829 :         mvc *m = NULL;
    1321             :         str msg;
    1322     1455829 :         const char *sname = *getArgReference_str(stk, pci, 2 + upd);
    1323     1455829 :         const char *tname = *getArgReference_str(stk, pci, 3 + upd);
    1324     1455829 :         const char *cname = *getArgReference_str(stk, pci, 4 + upd);
    1325     1455829 :         int access = *getArgReference_int(stk, pci, 5 + upd);
    1326             : 
    1327             :         /* This doesn't work with quick access for now... */
    1328     1455829 :         assert(access != QUICK);
    1329     1455829 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    1330             :                 return msg;
    1331     1455612 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    1332             :                 return msg;
    1333     1455490 :         sqlstore *store = m->store;
    1334     1455490 :         sql_schema *s = mvc_bind_schema(m, sname);
    1335     1455567 :         sql_table *t = mvc_bind_table(m, s, tname);
    1336     1455391 :         if (t && !isTable(t))
    1337           0 :                 throw(SQL, "sql.bind", SQLSTATE(42000) "%s '%s' is not persistent",
    1338           0 :                           TABLE_TYPE_DESCRIPTION(t->type, t->properties), t->base.name);
    1339     1455391 :         sql_column *c = mvc_bind_column(m, t, cname);
    1340     1455686 :         b = mvc_bind(m, sname, tname, cname, access);
    1341     1455417 :         if (b && b->ttype && b->ttype != coltype) {
    1342           0 :                 BBPunfix(b->batCacheid);
    1343           0 :                 throw(SQL,"sql.bind",SQLSTATE(42000) "Column type mismatch %s.%s.%s",sname,tname,cname);
    1344             :         }
    1345     1455417 :         if (b) {
    1346     2427067 :                 if (pci->argc == (8 + upd) && getArgType(mb, pci, 6 + upd) == TYPE_int) {
    1347      971360 :                         BUN cnt = store->storage_api.count_col(m->session->tr, c, 0), psz;
    1348             :                         /* partitioned access */
    1349      971832 :                         int part_nr = *getArgReference_int(stk, pci, 6 + upd);
    1350      971832 :                         int nr_parts = *getArgReference_int(stk, pci, 7 + upd);
    1351             : 
    1352      971832 :                         if (access == 0) {
    1353             :                                 BUN l, h;
    1354      567744 :                                 psz = cnt ? (cnt / nr_parts) : 0;
    1355      567744 :                                 l = part_nr * psz;
    1356             :                                 if (l > cnt)
    1357             :                                         l = cnt;
    1358      567744 :                                 h = (part_nr + 1 == nr_parts) ? cnt : ((part_nr + 1) * psz);
    1359             :                                 if (h > cnt)
    1360             :                                         h = cnt;
    1361      567744 :                                 bn = BATslice(b, l, h);
    1362      567536 :                                 if(bn == NULL) {
    1363           0 :                                         BBPunfix(b->batCacheid);
    1364           0 :                                         throw(SQL, "sql.bind", GDK_EXCEPTION);
    1365             :                                 }
    1366      567536 :                                 BAThseqbase(bn, l);
    1367             :                         } else {
    1368             :                                 /* BAT b holds the UPD_ID bat */
    1369             :                                 oid l, h;
    1370      404088 :                                 cnt = store->storage_api.count_col(m->session->tr, c, 0);
    1371      404063 :                                 psz = cnt ? (cnt / nr_parts) : 0;
    1372      404063 :                                 l = part_nr * psz;
    1373      404063 :                                 if (l > cnt)
    1374           0 :                                         l = cnt;
    1375      404063 :                                 h = (part_nr + 1 == nr_parts) ? cnt : ((part_nr + 1) * psz);
    1376      404063 :                                 if (h > cnt)
    1377           0 :                                         h = cnt;
    1378      404063 :                                 h--;
    1379      404063 :                                 bn = BATselect(b, NULL, &l, &h, true, true, false);
    1380      404047 :                                 if(bn == NULL) {
    1381           0 :                                         BBPunfix(b->batCacheid);
    1382           0 :                                         throw(SQL, "sql.bind", GDK_EXCEPTION);
    1383             :                                 }
    1384             :                         }
    1385      971659 :                         BBPunfix(b->batCacheid);
    1386             :                         b = bn;
    1387      484057 :                 } else if (upd) {
    1388      165546 :                         BAT *uv = mvc_bind(m, sname, tname, cname, RD_UPD_VAL);
    1389      165550 :                         bat *uvl = getArgReference_bat(stk, pci, 1);
    1390             : 
    1391      165550 :                         if (uv == NULL) {
    1392           0 :                                 BBPunfix(b->batCacheid);
    1393           0 :                                 throw(SQL,"sql.bind",SQLSTATE(HY005) "Cannot access the update column %s.%s.%s",
    1394             :                                         sname,tname,cname);
    1395             :                         }
    1396      165550 :                         BBPkeepref(*bid = b->batCacheid);
    1397      165550 :                         BBPkeepref(*uvl = uv->batCacheid);
    1398      165547 :                         return MAL_SUCCEED;
    1399             :                 }
    1400     1290161 :                 if (upd) {
    1401      404070 :                         bat *uvl = getArgReference_bat(stk, pci, 1);
    1402             : 
    1403      404070 :                         if (BATcount(b)) {
    1404           3 :                                 BAT *uv = mvc_bind(m, sname, tname, cname, RD_UPD_VAL);
    1405           3 :                                 BAT *ui = mvc_bind(m, sname, tname, cname, RD_UPD_ID);
    1406             :                                 BAT *id;
    1407             :                                 BAT *vl;
    1408           3 :                                 if (ui == NULL || uv == NULL) {
    1409           0 :                                         bat_destroy(uv);
    1410           0 :                                         bat_destroy(ui);
    1411           0 :                                         BBPunfix(b->batCacheid);
    1412           0 :                                         throw(SQL,"sql.bind",SQLSTATE(HY005) "Cannot access the insert column %s.%s.%s",
    1413             :                                                 sname, tname, cname);
    1414             :                                 }
    1415           3 :                                 id = BATproject(b, ui);
    1416           3 :                                 vl = BATproject(b, uv);
    1417           3 :                                 bat_destroy(ui);
    1418           3 :                                 bat_destroy(uv);
    1419           3 :                                 if (id == NULL || vl == NULL) {
    1420           0 :                                         BBPunfix(b->batCacheid);
    1421           0 :                                         bat_destroy(id);
    1422           0 :                                         bat_destroy(vl);
    1423           0 :                                         throw(SQL, "sql.bind", GDK_EXCEPTION);
    1424             :                                 }
    1425           3 :                                 if ( BATcount(id) != BATcount(vl)){
    1426           0 :                                         BBPunfix(b->batCacheid);
    1427           0 :                                         bat_destroy(id);
    1428           0 :                                         bat_destroy(vl);
    1429           0 :                                         throw(SQL, "sql.bind", SQLSTATE(0000) "Inconsistent BAT count");
    1430             :                                 }
    1431           3 :                                 BBPkeepref(*bid = id->batCacheid);
    1432           3 :                                 BBPkeepref(*uvl = vl->batCacheid);
    1433             :                         } else {
    1434      404067 :                                 *bid = e_bat(TYPE_oid);
    1435      403910 :                                 *uvl = e_bat(c->type.type->localtype);
    1436      404050 :                                 if (*bid == BID_NIL || *uvl == BID_NIL) {
    1437           2 :                                         if (*bid)
    1438           0 :                                                 BBPunfix(*bid);
    1439           2 :                                         if (*uvl)
    1440           0 :                                                 BBPunfix(*uvl);
    1441           2 :                                         BBPunfix(b->batCacheid);
    1442           0 :                                         throw(SQL, "sql.bind", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1443             :                                 }
    1444             :                         }
    1445      404051 :                         BBPunfix(b->batCacheid);
    1446             :                 } else {
    1447      886091 :                         BBPkeepref(*bid = b->batCacheid);
    1448             :                 }
    1449     1289981 :                 return MAL_SUCCEED;
    1450             :         }
    1451           0 :         if (!strNil(sname))
    1452           0 :                 throw(SQL, "sql.bind", SQLSTATE(42000) "unable to find %s.%s(%s)", sname, tname, cname);
    1453           0 :         throw(SQL, "sql.bind", SQLSTATE(42000) "unable to find %s(%s)", tname, cname);
    1454             : }
    1455             : 
    1456             : /* The output of this function are 7 columns:
    1457             :  *  - The sqlid of the column
    1458             :  *  - Number of values of the column.
    1459             :  *  - Number of segments, indication of the fragmentation
    1460             :  *  - Number of inserted rows during the current transaction.
    1461             :  *  - Number of updated rows during the current transaction.
    1462             :  *  - Number of deletes of the column's table.
    1463             :  *  - the number in the transaction chain (.i.e for each savepoint a new transaction is added in the chain)
    1464             :  */
    1465             : 
    1466             : static str
    1467           0 : mvc_insert_delta_values(mvc *m, BAT *col1, BAT *col2, BAT *col3, BAT *col4, BAT *col5, BAT *col6, BAT *col7, sql_column *c, lng segments, lng deletes)
    1468             : {
    1469           0 :         int level = 0;
    1470           0 :         sqlstore *store = m->session->tr->store;
    1471             : 
    1472           0 :         lng inserted = (lng) store->storage_api.count_col(m->session->tr, c, 1);
    1473           0 :         lng all = (lng) store->storage_api.count_col(m->session->tr, c, 0);
    1474           0 :         lng updates = (lng) store->storage_api.count_col(m->session->tr, c, 2);
    1475             : 
    1476           0 :         if (BUNappend(col1, &c->base.id, false) != GDK_SUCCEED) {
    1477           0 :                 return createException(SQL,"sql.delta", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1478             :         }
    1479           0 :         if (BUNappend(col2, &segments, false) != GDK_SUCCEED) {
    1480           0 :                 return createException(SQL,"sql.delta", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1481             :         }
    1482           0 :         if (BUNappend(col3, &all, false) != GDK_SUCCEED) {
    1483           0 :                 return createException(SQL,"sql.delta", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1484             :         }
    1485           0 :         if (BUNappend(col4, &inserted, false) != GDK_SUCCEED) {
    1486           0 :                 return createException(SQL,"sql.delta", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1487             :         }
    1488           0 :         if (BUNappend(col5, &updates, false) != GDK_SUCCEED) {
    1489           0 :                 return createException(SQL,"sql.delta", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1490             :         }
    1491           0 :         if (BUNappend(col6, &deletes, false) != GDK_SUCCEED) {
    1492           0 :                 return createException(SQL,"sql.delta", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1493             :         }
    1494             :         /* compute level using global transaction */
    1495           0 :         if (c) {
    1496           0 :                 for(sql_delta *d = ATOMIC_PTR_GET(&c->data); d; d = d->next)
    1497           0 :                         level++;
    1498             :         }
    1499           0 :         if (BUNappend(col7, &level, false) != GDK_SUCCEED) {
    1500           0 :                 return createException(SQL,"sql.delta", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1501             :         }
    1502             :         return MAL_SUCCEED;
    1503             : }
    1504             : 
    1505             : str
    1506           0 : mvc_delta_values(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    1507             : {
    1508           0 :         const char *sname = *getArgReference_str(stk, pci, 7),
    1509           0 :                            *tname = (pci->argc > 8) ? *getArgReference_str(stk, pci, 8) : NULL,
    1510           0 :                            *cname = (pci->argc > 9) ? *getArgReference_str(stk, pci, 9) : NULL;
    1511             :         mvc *m;
    1512             :         str msg = MAL_SUCCEED;
    1513             :         BAT *col1 = NULL, *col2 = NULL, *col3 = NULL, *col4 = NULL, *col5 = NULL, *col6 = NULL, *col7 = NULL;
    1514           0 :         bat *b1 = getArgReference_bat(stk, pci, 0),
    1515           0 :                 *b2 = getArgReference_bat(stk, pci, 1),
    1516           0 :                 *b3 = getArgReference_bat(stk, pci, 2),
    1517           0 :                 *b4 = getArgReference_bat(stk, pci, 3),
    1518           0 :                 *b5 = getArgReference_bat(stk, pci, 4),
    1519           0 :                 *b6 = getArgReference_bat(stk, pci, 5),
    1520           0 :                 *b7 = getArgReference_bat(stk, pci, 6);
    1521             :         sql_schema *s = NULL;
    1522             :         sql_table *t = NULL;
    1523             :         sql_column *c = NULL;
    1524             :         node *n;
    1525             :         BUN nrows = 0;
    1526             :         lng deletes, segments;
    1527             : 
    1528           0 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    1529             :                 return msg;
    1530             : 
    1531           0 :         sqlstore *store = m->store;
    1532           0 :         sql_trans *tr = m->session->tr;
    1533           0 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    1534           0 :                 goto cleanup;
    1535             : 
    1536           0 :         if (!(s = mvc_bind_schema(m, sname)))
    1537           0 :                 throw(SQL, "sql.delta", SQLSTATE(3F000) "No such schema '%s'", sname);
    1538             : 
    1539           0 :         if (tname) {
    1540           0 :                 if (!(t = mvc_bind_table(m, s, tname)))
    1541           0 :                         throw(SQL, "sql.delta", SQLSTATE(3F000) "No such table '%s' in schema '%s'", tname, s->base.name);
    1542           0 :                 if (!isTable(t))
    1543           0 :                         throw(SQL, "sql.delta", SQLSTATE(42000) "%s doesn't have delta values", TABLE_TYPE_DESCRIPTION(t->type, t->properties));
    1544           0 :                 if (cname) {
    1545           0 :                         if (!(c = mvc_bind_column(m, t, cname)))
    1546           0 :                                 throw(SQL, "sql.delta", SQLSTATE(3F000) "No such column '%s' in table '%s'", cname, t->base.name);
    1547             :                         nrows = 1;
    1548             :                 } else {
    1549           0 :                         nrows = (BUN) ol_length(t->columns);
    1550             :                 }
    1551           0 :         } else if (s->tables) {
    1552             :                 struct os_iter oi;
    1553           0 :                 os_iterator(&oi, s->tables, tr, NULL);
    1554           0 :                 for (sql_base *b = oi_next(&oi); b; b = oi_next(&oi)) {
    1555             :                         t = (sql_table *)b;
    1556           0 :                         if (isTable(t))
    1557           0 :                                 nrows += (BUN) ol_length(t->columns);
    1558             :                 }
    1559             :         }
    1560             : 
    1561           0 :         if ((col1 = COLnew(0, TYPE_int, nrows, TRANSIENT)) == NULL) {
    1562           0 :                 msg = createException(SQL, "sql.delta", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1563           0 :                 goto cleanup;
    1564             :         }
    1565           0 :         if ((col2 = COLnew(0, TYPE_lng, nrows, TRANSIENT)) == NULL) {
    1566           0 :                 msg = createException(SQL, "sql.delta", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1567           0 :                 goto cleanup;
    1568             :         }
    1569           0 :         if ((col3 = COLnew(0, TYPE_lng, nrows, TRANSIENT)) == NULL) {
    1570           0 :                 msg = createException(SQL, "sql.delta", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1571           0 :                 goto cleanup;
    1572             :         }
    1573           0 :         if ((col4 = COLnew(0, TYPE_lng, nrows, TRANSIENT)) == NULL) {
    1574           0 :                 msg = createException(SQL, "sql.delta", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1575           0 :                 goto cleanup;
    1576             :         }
    1577           0 :         if ((col5 = COLnew(0, TYPE_lng, nrows, TRANSIENT)) == NULL) {
    1578           0 :                 msg = createException(SQL, "sql.delta", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1579           0 :                 goto cleanup;
    1580             :         }
    1581           0 :         if ((col6 = COLnew(0, TYPE_lng, nrows, TRANSIENT)) == NULL) {
    1582           0 :                 msg = createException(SQL, "sql.delta", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1583           0 :                 goto cleanup;
    1584             :         }
    1585           0 :         if ((col7 = COLnew(0, TYPE_int, nrows, TRANSIENT)) == NULL) {
    1586           0 :                 msg = createException(SQL, "sql.delta", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1587           0 :                 goto cleanup;
    1588             :         }
    1589             : 
    1590           0 :         if (nrows) {
    1591           0 :                 if (tname) {
    1592           0 :                         deletes = (lng) store->storage_api.count_del(m->session->tr, t, 0);
    1593           0 :                         segments = (lng) store->storage_api.count_del(m->session->tr, t, 10);
    1594           0 :                         if (cname) {
    1595           0 :                                 if ((msg=mvc_insert_delta_values(m, col1, col2, col3, col4, col5, col6, col7, c, segments, deletes)) != NULL)
    1596           0 :                                         goto cleanup;
    1597             :                         } else {
    1598           0 :                                 for (n = ol_first_node(t->columns); n ; n = n->next) {
    1599           0 :                                         c = (sql_column*) n->data;
    1600           0 :                                         if ((msg=mvc_insert_delta_values(m, col1, col2, col3, col4, col5, col6, col7, c, segments, deletes)) != NULL)
    1601           0 :                                                 goto cleanup;
    1602             :                                 }
    1603             :                         }
    1604           0 :                 } else if (s->tables) {
    1605             :                         struct os_iter oi;
    1606           0 :                         os_iterator(&oi, s->tables, tr, NULL);
    1607           0 :                         for (sql_base *b = oi_next(&oi); b; b = oi_next(&oi)) {
    1608             :                                 t = (sql_table *)b;
    1609           0 :                                 if (isTable(t)) {
    1610           0 :                                         deletes = (lng) store->storage_api.count_del(m->session->tr, t, 0);
    1611           0 :                                         segments = (lng) store->storage_api.count_del(m->session->tr, t, 10);
    1612             : 
    1613           0 :                                         for (node *nn = ol_first_node(t->columns); nn ; nn = nn->next) {
    1614           0 :                                                 c = (sql_column*) nn->data;
    1615             : 
    1616           0 :                                                 if ((msg=mvc_insert_delta_values(m, col1, col2, col3, col4, col5, col6, col7, c, segments, deletes)) != NULL)
    1617           0 :                                                         goto cleanup;
    1618             :                                         }
    1619             :                                 }
    1620             :                         }
    1621             :                 }
    1622             :         }
    1623             : 
    1624           0 : cleanup:
    1625           0 :         if (msg) {
    1626           0 :                 if (col1)
    1627           0 :                         BBPreclaim(col1);
    1628           0 :                 if (col2)
    1629           0 :                         BBPreclaim(col2);
    1630           0 :                 if (col3)
    1631           0 :                         BBPreclaim(col3);
    1632           0 :                 if (col4)
    1633           0 :                         BBPreclaim(col4);
    1634           0 :                 if (col5)
    1635           0 :                         BBPreclaim(col5);
    1636           0 :                 if (col6)
    1637           0 :                         BBPreclaim(col6);
    1638           0 :                 if (col7)
    1639           0 :                         BBPreclaim(col7);
    1640             :         } else {
    1641           0 :                 BBPkeepref(*b1 = col1->batCacheid);
    1642           0 :                 BBPkeepref(*b2 = col2->batCacheid);
    1643           0 :                 BBPkeepref(*b3 = col3->batCacheid);
    1644           0 :                 BBPkeepref(*b4 = col4->batCacheid);
    1645           0 :                 BBPkeepref(*b5 = col5->batCacheid);
    1646           0 :                 BBPkeepref(*b6 = col6->batCacheid);
    1647           0 :                 BBPkeepref(*b7 = col7->batCacheid);
    1648             :         }
    1649             :         return msg;
    1650             : }
    1651             : 
    1652             : /* str mvc_bind_idxbat_wrap(int *bid, str *sname, str *tname, str *iname, int *access); */
    1653             : str
    1654        5984 : mvc_bind_idxbat_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    1655             : {
    1656        5984 :         int upd = (pci->argc == 7 || pci->argc == 9);
    1657             :         BAT *b = NULL, *bn;
    1658        5984 :         bat *bid = getArgReference_bat(stk, pci, 0);
    1659        5984 :         int coltype = getBatType(getArgType(mb, pci, 0));
    1660        5984 :         mvc *m = NULL;
    1661             :         str msg;
    1662        5984 :         const char *sname = *getArgReference_str(stk, pci, 2 + upd);
    1663        5984 :         const char *tname = *getArgReference_str(stk, pci, 3 + upd);
    1664        5984 :         const char *iname = *getArgReference_str(stk, pci, 4 + upd);
    1665        5984 :         int access = *getArgReference_int(stk, pci, 5 + upd);
    1666             : 
    1667        5984 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    1668             :                 return msg;
    1669        5984 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    1670             :                 return msg;
    1671        5984 :         sqlstore *store = m->store;
    1672        5984 :         sql_schema *s = mvc_bind_schema(m, sname);
    1673        5982 :         sql_table *t = mvc_bind_table(m, s, tname);
    1674        5984 :         if (t && !isTable(t))
    1675           0 :                 throw(SQL, "sql.tid", SQLSTATE(42000) "%s '%s' is not persistent",
    1676           0 :                           TABLE_TYPE_DESCRIPTION(t->type, t->properties), t->base.name);
    1677        5984 :         sql_idx *i = mvc_bind_idx(m, s, iname);
    1678        5983 :         b = mvc_bind_idxbat(m, sname, tname, iname, access);
    1679        5984 :         if (b && b->ttype && b->ttype != coltype) {
    1680           0 :                 BBPunfix(b->batCacheid);
    1681           0 :                 throw(SQL,"sql.bind",SQLSTATE(42000) "Index column type mismatch %s.%s.%s",sname,tname,iname);
    1682             :         }
    1683        5984 :         if (b) {
    1684       10844 :                 if (pci->argc == (8 + upd) && getArgType(mb, pci, 6 + upd) == TYPE_int) {
    1685        4860 :                         BUN cnt = store->storage_api.count_idx(m->session->tr, i, 0), psz;
    1686             :                         /* partitioned access */
    1687        4860 :                         int part_nr = *getArgReference_int(stk, pci, 6 + upd);
    1688        4860 :                         int nr_parts = *getArgReference_int(stk, pci, 7 + upd);
    1689             : 
    1690        4860 :                         if (access == 0) {
    1691             :                                 BUN l, h;
    1692        4860 :                                 psz = cnt ? (cnt / nr_parts) : 0;
    1693        4860 :                                 l = part_nr * psz;
    1694             :                                 if (l > cnt)
    1695             :                                         l = cnt;
    1696        4860 :                                 h = (part_nr + 1 == nr_parts) ? cnt : ((part_nr + 1) * psz);
    1697             :                                 if (h > cnt)
    1698             :                                         h = cnt;
    1699        4860 :                                 bn = BATslice(b, l, h);
    1700        4858 :                                 if(bn == NULL){
    1701           0 :                                         BBPunfix(b->batCacheid);
    1702           0 :                                         throw(SQL, "sql.bindidx", GDK_EXCEPTION);
    1703             :                                 }
    1704        4858 :                                 BAThseqbase(bn, l);
    1705             :                         } else {
    1706             :                                 /* BAT b holds the UPD_ID bat */
    1707             :                                 oid l, h;
    1708           0 :                                 cnt = store->storage_api.count_idx(m->session->tr, i, 0);
    1709           0 :                                 psz = cnt ? (cnt / nr_parts) : 0;
    1710           0 :                                 l = part_nr * psz;
    1711           0 :                                 if (l > cnt)
    1712           0 :                                         l = cnt;
    1713           0 :                                 h = (part_nr + 1 == nr_parts) ? cnt : ((part_nr + 1) * psz);
    1714           0 :                                 if (h > cnt)
    1715           0 :                                         h = cnt;
    1716           0 :                                 h--;
    1717           0 :                                 bn = BATselect(b, NULL, &l, &h, true, true, false);
    1718           0 :                                 if(bn == NULL) {
    1719           0 :                                         BBPunfix(b->batCacheid);
    1720           0 :                                         throw(SQL, "sql.bindidx", GDK_EXCEPTION);
    1721             :                                 }
    1722             :                         }
    1723        4860 :                         BBPunfix(b->batCacheid);
    1724             :                         b = bn;
    1725        1124 :                 } else if (upd) {
    1726          56 :                         BAT *uv = mvc_bind_idxbat(m, sname, tname, iname, RD_UPD_VAL);
    1727          56 :                         bat *uvl = getArgReference_bat(stk, pci, 1);
    1728          56 :                         if ( uv == NULL){
    1729           0 :                                 BBPunfix(b->batCacheid);
    1730           0 :                                 throw(SQL,"sql.bindidx",SQLSTATE(42000) "Cannot access index column %s.%s.%s",sname,tname,iname);
    1731             :                         }
    1732          56 :                         BBPkeepref(*bid = b->batCacheid);
    1733          56 :                         BBPkeepref(*uvl = uv->batCacheid);
    1734          56 :                         return MAL_SUCCEED;
    1735             :                 }
    1736        5928 :                 if (upd) {
    1737           0 :                         bat *uvl = getArgReference_bat(stk, pci, 1);
    1738             : 
    1739           0 :                         if (BATcount(b)) {
    1740           0 :                                 BAT *uv = mvc_bind_idxbat(m, sname, tname, iname, RD_UPD_VAL);
    1741           0 :                                 BAT *ui = mvc_bind_idxbat(m, sname, tname, iname, RD_UPD_ID);
    1742             :                                 BAT *id, *vl;
    1743           0 :                                 if ( ui == NULL || uv == NULL) {
    1744           0 :                                         bat_destroy(uv);
    1745           0 :                                         bat_destroy(ui);
    1746           0 :                                         BBPunfix(b->batCacheid);
    1747           0 :                                         throw(SQL,"sql.bindidx",SQLSTATE(42000) "Cannot access index column %s.%s.%s",sname,tname,iname);
    1748             :                                 }
    1749           0 :                                 id = BATproject(b, ui);
    1750           0 :                                 vl = BATproject(b, uv);
    1751           0 :                                 bat_destroy(ui);
    1752           0 :                                 bat_destroy(uv);
    1753           0 :                                 if (id == NULL || vl == NULL) {
    1754           0 :                                         bat_destroy(id);
    1755           0 :                                         bat_destroy(vl);
    1756           0 :                                         BBPunfix(b->batCacheid);
    1757           0 :                                         throw(SQL, "sql.bindidx", GDK_EXCEPTION);
    1758             :                                 }
    1759           0 :                                 if ( BATcount(id) != BATcount(vl)){
    1760           0 :                                         BBPunfix(b->batCacheid);
    1761           0 :                                         bat_destroy(id);
    1762           0 :                                         bat_destroy(vl);
    1763           0 :                                         throw(SQL, "sql.bindidx", SQLSTATE(0000) "Inconsistent BAT count");
    1764             :                                 }
    1765           0 :                                 BBPkeepref(*bid = id->batCacheid);
    1766           0 :                                 BBPkeepref(*uvl = vl->batCacheid);
    1767             :                         } else {
    1768           0 :                                 *bid = e_bat(TYPE_oid);
    1769           0 :                                 *uvl = e_bat((i->type==join_idx)?TYPE_oid:TYPE_lng);
    1770           0 :                                 if (*bid == BID_NIL || *uvl == BID_NIL) {
    1771           0 :                                         if (*bid)
    1772           0 :                                                 BBPunfix(*bid);
    1773           0 :                                         if (*uvl)
    1774           0 :                                                 BBPunfix(*uvl);
    1775           0 :                                         BBPunfix(b->batCacheid);
    1776           0 :                                         throw(SQL, "sql.bindidx", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1777             :                                 }
    1778             :                         }
    1779           0 :                         BBPunfix(b->batCacheid);
    1780             :                 } else {
    1781        5928 :                         BBPkeepref(*bid = b->batCacheid);
    1782             :                 }
    1783        5927 :                 return MAL_SUCCEED;
    1784             :         }
    1785           0 :         if (sname)
    1786           0 :                 throw(SQL, "sql.bindidx", SQLSTATE(HY005) "Cannot access column descriptor %s for %s.%s", iname, sname, tname);
    1787           0 :         throw(SQL, "sql.bindidx", SQLSTATE(HY005) "Cannot access column descriptor %s for %s", iname, tname);
    1788             : }
    1789             : 
    1790             : str
    1791          52 : mvc_append_column(sql_trans *t, sql_column *c, BUN offset, BAT *pos, BAT *ins)
    1792             : {
    1793          52 :         sqlstore *store = t->store;
    1794          52 :         int res = store->storage_api.append_col(t, c, offset, pos, ins, BATcount(ins), TYPE_bat);
    1795          52 :         if (res != LOG_OK) /* the conflict case should never happen, but leave it here */
    1796           0 :                 throw(SQL, "sql.append", SQLSTATE(42000) "Append failed%s", res == LOG_CONFLICT ? " due to conflict with another transaction" : "");
    1797             :         return MAL_SUCCEED;
    1798             : }
    1799             : 
    1800             : /*mvc_grow_wrap(int *bid, str *sname, str *tname, str *cname, ptr d) */
    1801             : str
    1802         341 : mvc_grow_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    1803             : {
    1804         341 :         int *res = getArgReference_int(stk, pci, 0);
    1805         341 :         bat Tid = *getArgReference_bat(stk, pci, 1);
    1806         341 :         ptr Ins = getArgReference(stk, pci, 2);
    1807         341 :         int tpe = getArgType(mb, pci, 2);
    1808             :         BAT *tid = 0, *ins = 0;
    1809             :         size_t cnt = 1;
    1810         341 :         oid v = 0;
    1811             : 
    1812             :         (void)cntxt;
    1813         341 :         *res = 0;
    1814         341 :         if ((tid = BATdescriptor(Tid)) == NULL)
    1815           0 :                 throw(SQL, "sql.grow", SQLSTATE(HY005) "Cannot access descriptor");
    1816         341 :         if (tpe > GDKatomcnt)
    1817             :                 tpe = TYPE_bat;
    1818         341 :         if (tpe == TYPE_bat && (ins = BATdescriptor(*(bat *) Ins)) == NULL) {
    1819           0 :                 BBPunfix(Tid);
    1820           0 :                 throw(SQL, "sql.grow", SQLSTATE(HY005) "Cannot access descriptor");
    1821             :         }
    1822             :         if (ins) {
    1823           9 :                 cnt = BATcount(ins);
    1824           9 :                 BBPunfix(ins->batCacheid);
    1825             :         }
    1826         341 :         if (BATcount(tid)) {
    1827         311 :                 (void)BATmax(tid, &v);
    1828         311 :                 v++;
    1829             :         }
    1830         691 :         for(;cnt>0; cnt--, v++) {
    1831         350 :                 if (BUNappend(tid, &v, false) != GDK_SUCCEED) {
    1832           0 :                         BBPunfix(Tid);
    1833           0 :                         throw(SQL, "sql.grow", GDK_EXCEPTION);
    1834             :                 }
    1835             :         }
    1836         341 :         BBPunfix(Tid);
    1837         341 :         return MAL_SUCCEED;
    1838             : }
    1839             : 
    1840             : /*mvc_append_wrap(int *bid, str *sname, str *tname, str *cname, ptr d) */
    1841             : str
    1842      520988 : mvc_append_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    1843             : {
    1844      520988 :         int *res = getArgReference_int(stk, pci, 0);
    1845      520988 :         mvc *m = NULL;
    1846             :         str msg;
    1847      520988 :         const char *sname = *getArgReference_str(stk, pci, 2);
    1848      520988 :         const char *tname = *getArgReference_str(stk, pci, 3);
    1849      520988 :         const char *cname = *getArgReference_str(stk, pci, 4);
    1850      520988 :         BUN offset = *(BUN*)getArgReference_oid(stk, pci, 5);
    1851      520988 :         bat Pos = *getArgReference_bat(stk, pci, 6);
    1852      520988 :         ptr ins = getArgReference(stk, pci, 7);
    1853      520805 :         int tpe = getArgType(mb, pci, 7), log_res = LOG_OK;
    1854             :         sql_schema *s;
    1855             :         sql_table *t;
    1856             :         sql_column *c;
    1857             :         sql_idx *i;
    1858             :         BAT *b = NULL, *pos = NULL;
    1859             :         BUN cnt = 1;
    1860             : 
    1861      520805 :         *res = 0;
    1862      520805 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    1863             :                 return msg;
    1864      520652 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    1865             :                 return msg;
    1866      520613 :         if (tpe > GDKatomcnt)
    1867             :                 tpe = TYPE_bat;
    1868      520613 :         if (Pos != bat_nil && (pos = BATdescriptor(Pos)) == NULL)
    1869           0 :                 throw(SQL, "sql.append", SQLSTATE(HY005) "Cannot access append positions descriptor");
    1870      520613 :         if (tpe == TYPE_bat && (ins = BATdescriptor(*(bat *) ins)) == NULL) {
    1871           0 :                 bat_destroy(pos);
    1872           0 :                 throw(SQL, "sql.append", SQLSTATE(HY005) "Cannot access append values descriptor");
    1873             :         }
    1874      520615 :         if (ATOMextern(tpe) && !ATOMvarsized(tpe))
    1875          11 :                 ins = *(ptr *) ins;
    1876      520615 :         if ( tpe == TYPE_bat)
    1877             :                 b =  (BAT*) ins;
    1878      520615 :         s = mvc_bind_schema(m, sname);
    1879      520735 :         if (s == NULL) {
    1880           0 :                 bat_destroy(pos);
    1881           0 :                 bat_destroy(b);
    1882           0 :                 throw(SQL, "sql.append", SQLSTATE(3F000) "Schema missing %s",sname);
    1883             :         }
    1884      520735 :         t = mvc_bind_table(m, s, tname);
    1885      520983 :         if (t == NULL) {
    1886           0 :                 bat_destroy(pos);
    1887           0 :                 bat_destroy(b);
    1888           0 :                 throw(SQL, "sql.append", SQLSTATE(42S02) "Table missing %s",tname);
    1889             :         }
    1890      520983 :         if (!isTable(t)) {
    1891           0 :                 bat_destroy(pos);
    1892           0 :                 bat_destroy(b);
    1893           0 :                 throw(SQL, "sql.append", SQLSTATE(42000) "%s '%s' is not persistent", TABLE_TYPE_DESCRIPTION(t->type, t->properties), t->base.name);
    1894             :         }
    1895      520983 :         if (b)
    1896       19517 :                 cnt = BATcount(b);
    1897      520983 :         sqlstore *store = m->session->tr->store;
    1898      520983 :         if (cname[0] != '%' && (c = mvc_bind_column(m, t, cname)) != NULL) {
    1899      518750 :                 log_res = store->storage_api.append_col(m->session->tr, c, offset, pos, ins, cnt, tpe);
    1900        2159 :         } else if (cname[0] == '%' && (i = mvc_bind_idx(m, s, cname + 1)) != NULL) {
    1901        2159 :                 log_res = store->storage_api.append_idx(m->session->tr, i, offset, pos, ins, cnt, tpe);
    1902             :         } else {
    1903           0 :                 bat_destroy(pos);
    1904           0 :                 bat_destroy(b);
    1905           0 :                 throw(SQL, "sql.append", SQLSTATE(38000) "Unable to find column or index %s.%s.%s",sname,tname,cname);
    1906             :         }
    1907      520608 :         bat_destroy(pos);
    1908      520548 :         bat_destroy(b);
    1909      520501 :         if (log_res != LOG_OK) /* the conflict case should never happen, but leave it here */
    1910           0 :                 throw(SQL, "sql.append", SQLSTATE(42000) "Append failed%s", log_res == LOG_CONFLICT ? " due to conflict with another transaction" : "");
    1911             :         return MAL_SUCCEED;
    1912             : }
    1913             : 
    1914             : /*mvc_update_wrap(int *bid, str *sname, str *tname, str *cname, ptr d) */
    1915             : str
    1916        3634 : mvc_update_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    1917             : {
    1918        3634 :         int *res = getArgReference_int(stk, pci, 0);
    1919        3634 :         mvc *m = NULL;
    1920             :         str msg;
    1921        3634 :         const char *sname = *getArgReference_str(stk, pci, 2);
    1922        3634 :         const char *tname = *getArgReference_str(stk, pci, 3);
    1923        3634 :         const char *cname = *getArgReference_str(stk, pci, 4);
    1924        3634 :         bat Tids = *getArgReference_bat(stk, pci, 5);
    1925        3634 :         bat Upd = *getArgReference_bat(stk, pci, 6);
    1926             :         BAT *tids, *upd;
    1927        3634 :         int tpe = getArgType(mb, pci, 6), log_res = LOG_OK;
    1928             :         sql_schema *s;
    1929             :         sql_table *t;
    1930             :         sql_column *c;
    1931             :         sql_idx *i;
    1932             : 
    1933        3634 :         *res = 0;
    1934        3634 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    1935             :                 return msg;
    1936        3634 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    1937             :                 return msg;
    1938        3634 :         if (tpe > TYPE_any)
    1939             :                 tpe = TYPE_bat;
    1940             :         else
    1941           0 :                 assert(0);
    1942             :         if (tpe != TYPE_bat)
    1943             :                 throw(SQL, "sql.update", SQLSTATE(HY005) "Update values is not a BAT input");
    1944        3634 :         if ((tids = BATdescriptor(Tids)) == NULL)
    1945           0 :                 throw(SQL, "sql.update", SQLSTATE(HY005) "Cannot access update positions descriptor");
    1946        3634 :         if ((upd = BATdescriptor(Upd)) == NULL) {
    1947           0 :                 BBPunfix(tids->batCacheid);
    1948           0 :                 throw(SQL, "sql.update", SQLSTATE(HY005) "Cannot access update values descriptor");
    1949             :         }
    1950        3634 :         s = mvc_bind_schema(m, sname);
    1951        3634 :         if (s == NULL) {
    1952           0 :                 BBPunfix(tids->batCacheid);
    1953           0 :                 BBPunfix(upd->batCacheid);
    1954           0 :                 throw(SQL, "sql.update", SQLSTATE(3F000) "Schema missing %s",sname);
    1955             :         }
    1956        3634 :         t = mvc_bind_table(m, s, tname);
    1957        3634 :         if (t == NULL) {
    1958           0 :                 BBPunfix(tids->batCacheid);
    1959           0 :                 BBPunfix(upd->batCacheid);
    1960           0 :                 throw(SQL, "sql.update", SQLSTATE(42S02) "Table missing %s.%s",sname,tname);
    1961             :         }
    1962        3634 :         if (!isTable(t)) {
    1963           0 :                 BBPunfix(tids->batCacheid);
    1964           0 :                 BBPunfix(upd->batCacheid);
    1965           0 :                 throw(SQL, "sql.update", SQLSTATE(42000) "%s '%s' is not persistent", TABLE_TYPE_DESCRIPTION(t->type, t->properties), t->base.name);
    1966             :         }
    1967        3634 :         sqlstore *store = m->session->tr->store;
    1968        3634 :         if (cname[0] != '%' && (c = mvc_bind_column(m, t, cname)) != NULL) {
    1969        2893 :                 log_res = store->storage_api.update_col(m->session->tr, c, tids, upd, TYPE_bat);
    1970         741 :         } else if (cname[0] == '%' && (i = mvc_bind_idx(m, s, cname + 1)) != NULL) {
    1971         741 :                 log_res = store->storage_api.update_idx(m->session->tr, i, tids, upd, TYPE_bat);
    1972             :         } else {
    1973           0 :                 BBPunfix(tids->batCacheid);
    1974           0 :                 BBPunfix(upd->batCacheid);
    1975           0 :                 throw(SQL, "sql.update", SQLSTATE(38000) "Unable to find column or index %s.%s.%s",sname,tname,cname);
    1976             :         }
    1977        3634 :         BBPunfix(tids->batCacheid);
    1978        3634 :         BBPunfix(upd->batCacheid);
    1979        3634 :         if (log_res != LOG_OK)
    1980           1 :                 throw(SQL, "sql.update", SQLSTATE(42000) "Update failed%s", log_res == LOG_CONFLICT ? " due to conflict with another transaction" : "");
    1981             :         return MAL_SUCCEED;
    1982             : }
    1983             : 
    1984             : /* str mvc_clear_table_wrap(lng *res, str *sname, str *tname); */
    1985             : str
    1986         181 : mvc_clear_table_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    1987             : {
    1988             :         sql_schema *s;
    1989             :         sql_table *t;
    1990         181 :         mvc *m = NULL;
    1991             :         str msg;
    1992             :         BUN clear_res;
    1993         181 :         lng *res = getArgReference_lng(stk, pci, 0);
    1994         181 :         const char *sname = *getArgReference_str(stk, pci, 1);
    1995         181 :         const char *tname = *getArgReference_str(stk, pci, 2);
    1996             : 
    1997         181 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    1998             :                 return msg;
    1999         181 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    2000             :                 return msg;
    2001         181 :         s = mvc_bind_schema(m, sname);
    2002         181 :         if (s == NULL)
    2003           0 :                 throw(SQL, "sql.clear_table", SQLSTATE(3F000) "Schema missing %s", sname);
    2004         181 :         t = mvc_bind_table(m, s, tname);
    2005         181 :         if (t == NULL)
    2006           0 :                 throw(SQL, "sql.clear_table", SQLSTATE(42S02) "Table missing %s.%s", sname,tname);
    2007         181 :         if (!isTable(t))
    2008           0 :                 throw(SQL, "sql.clear_table", SQLSTATE(42000) "%s '%s' is not persistent", TABLE_TYPE_DESCRIPTION(t->type, t->properties), t->base.name);
    2009         181 :         clear_res = mvc_clear_table(m, t);
    2010         181 :         if (clear_res >= BUN_NONE - 1)
    2011           1 :                 throw(SQL, "sql.clear_table", SQLSTATE(42000) "Table clear failed%s", clear_res == (BUN_NONE - 1) ? " due to conflict with another transaction" : "");
    2012         180 :         *res = (lng) clear_res;
    2013         180 :         return MAL_SUCCEED;
    2014             : }
    2015             : 
    2016             : /*mvc_delete_wrap(int *d, str *sname, str *tname, ptr d) */
    2017             : str
    2018         329 : mvc_delete_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    2019             : {
    2020         329 :         int *res = getArgReference_int(stk, pci, 0);
    2021         329 :         mvc *m = NULL;
    2022             :         str msg;
    2023         329 :         const char *sname = *getArgReference_str(stk, pci, 2);
    2024         329 :         const char *tname = *getArgReference_str(stk, pci, 3);
    2025         329 :         ptr ins = getArgReference(stk, pci, 4);
    2026         329 :         int tpe = getArgType(mb, pci, 4), log_res;
    2027             :         BAT *b = NULL;
    2028             :         sql_schema *s;
    2029             :         sql_table *t;
    2030             : 
    2031         329 :         *res = 0;
    2032         329 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    2033             :                 return msg;
    2034         329 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    2035             :                 return msg;
    2036         329 :         if (tpe > TYPE_any)
    2037             :                 tpe = TYPE_bat;
    2038         329 :         if (tpe == TYPE_bat && (b = BATdescriptor(*(bat *) ins)) == NULL)
    2039           0 :                 throw(SQL, "sql.delete", SQLSTATE(HY005) "Cannot access column descriptor");
    2040         329 :         if (tpe != TYPE_bat || (b->ttype != TYPE_oid && b->ttype != TYPE_void && b->ttype != TYPE_msk)) {
    2041           0 :                 if (b)
    2042           0 :                         BBPunfix(b->batCacheid);
    2043           0 :                 throw(SQL, "sql.delete", SQLSTATE(HY005) "Cannot access column descriptor");
    2044             :         }
    2045         329 :         s = mvc_bind_schema(m, sname);
    2046         329 :         if (s == NULL) {
    2047             :                 if (b)
    2048           0 :                         BBPunfix(b->batCacheid);
    2049           0 :                 throw(SQL, "sql.delete", SQLSTATE(3F000) "Schema missing %s",sname);
    2050             :         }
    2051         329 :         t = mvc_bind_table(m, s, tname);
    2052         329 :         if (t == NULL) {
    2053             :                 if (b)
    2054           0 :                         BBPunfix(b->batCacheid);
    2055           0 :                 throw(SQL, "sql.delete", SQLSTATE(42S02) "Table missing %s.%s",sname,tname);
    2056             :         }
    2057         329 :         if (!isTable(t)) {
    2058             :                 if (b)
    2059           0 :                         BBPunfix(b->batCacheid);
    2060           0 :                 throw(SQL, "sql.delete", SQLSTATE(42000) "%s '%s' is not persistent", TABLE_TYPE_DESCRIPTION(t->type, t->properties), t->base.name);
    2061             :         }
    2062         329 :         sqlstore *store = m->session->tr->store;
    2063         329 :         log_res = store->storage_api.delete_tab(m->session->tr, t, b, tpe);
    2064             :         if (b)
    2065         329 :                 BBPunfix(b->batCacheid);
    2066         329 :         if (log_res != LOG_OK)
    2067           1 :                 throw(SQL, "sql.delete", SQLSTATE(42000) "Delete failed%s", log_res == LOG_CONFLICT ? " due to conflict with another transaction" : "");
    2068             :         return MAL_SUCCEED;
    2069             : }
    2070             : 
    2071             : static inline BAT *
    2072          12 : setwritable(BAT *b)
    2073             : {
    2074          12 :         if (isVIEW(b)) {
    2075          12 :                 BAT *bn = COLcopy(b, b->ttype, true, TRANSIENT);
    2076          12 :                 BBPunfix(b->batCacheid);
    2077             :                 b = bn;
    2078             :         }
    2079          12 :         return b;
    2080             : }
    2081             : 
    2082             : str
    2083      305485 : DELTAbat(bat *result, const bat *col, const bat *uid, const bat *uval)
    2084             : {
    2085             :         BAT *c, *u_id, *u_val, *res;
    2086             : 
    2087      305485 :         if ((u_id = BBPquickdesc(*uid)) == NULL)
    2088           0 :                 throw(MAL, "sql.delta", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
    2089             : 
    2090             :         /* no updates */
    2091      305435 :         if (BATcount(u_id) == 0) {
    2092      305410 :                 BBPretain(*result = *col);
    2093      305432 :                 return MAL_SUCCEED;
    2094             :         }
    2095             : 
    2096          25 :         c = BATdescriptor(*col);
    2097          25 :         if (c == NULL)
    2098           0 :                 throw(MAL, "sql.delta", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
    2099          25 :         if ((res = COLcopy(c, c->ttype, true, TRANSIENT)) == NULL) {
    2100           0 :                 BBPunfix(c->batCacheid);
    2101           0 :                 throw(MAL, "sql.delta", GDK_EXCEPTION);
    2102             :         }
    2103          25 :         BBPunfix(c->batCacheid);
    2104             : 
    2105          25 :         if ((u_val = BATdescriptor(*uval)) == NULL) {
    2106           0 :                 BBPunfix(res->batCacheid);
    2107           0 :                 throw(MAL, "sql.delta", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
    2108             :         }
    2109          25 :         if ((u_id = BATdescriptor(*uid)) == NULL) {
    2110           0 :                 BBPunfix(u_val->batCacheid);
    2111           0 :                 BBPunfix(res->batCacheid);
    2112           0 :                 throw(MAL, "sql.delta", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
    2113             :         }
    2114          25 :         assert(BATcount(u_id) == BATcount(u_val));
    2115          50 :         if (BATcount(u_id) &&
    2116          25 :             BATreplace(res, u_id, u_val, true) != GDK_SUCCEED) {
    2117           0 :                 BBPunfix(u_id->batCacheid);
    2118           0 :                 BBPunfix(u_val->batCacheid);
    2119           0 :                 BBPunfix(res->batCacheid);
    2120           0 :                 throw(MAL, "sql.delta", GDK_EXCEPTION);
    2121             :         }
    2122          25 :         BBPunfix(u_id->batCacheid);
    2123          25 :         BBPunfix(u_val->batCacheid);
    2124             : 
    2125          25 :         BBPkeepref(*result = res->batCacheid);
    2126          25 :         return MAL_SUCCEED;
    2127             : }
    2128             : 
    2129             : str
    2130       59104 : DELTAsub(bat *result, const bat *col, const bat *cid, const bat *uid, const bat *uval)
    2131             : {
    2132             :         BAT *c, *cminu = NULL, *u_id, *u_val, *u, *res;
    2133             :         gdk_return ret;
    2134             : 
    2135       59104 :         if ((u_id = BBPquickdesc(*uid)) == NULL)
    2136           0 :                 throw(MAL, "sql.delta", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
    2137             : 
    2138             :         /* no updates */
    2139       59097 :         if (BATcount(u_id) == 0) {
    2140       59066 :                 BBPretain(*result = *col);
    2141       59070 :                 return MAL_SUCCEED;
    2142             :         }
    2143             : 
    2144          31 :         c = BATdescriptor(*col);
    2145          31 :         if (c == NULL)
    2146           0 :                 throw(MAL, "sql.delta", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
    2147             :         res = c;
    2148          31 :         if (BATcount(u_id)) {
    2149          31 :                 u_id = BATdescriptor(*uid);
    2150          31 :                 if (!u_id) {
    2151           0 :                         BBPunfix(c->batCacheid);
    2152           0 :                         throw(MAL, "sql.delta", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
    2153             :                 }
    2154          31 :                 cminu = BATdiff(c, u_id, NULL, NULL, false, false, BUN_NONE);
    2155          31 :                 if (!cminu) {
    2156           0 :                         BBPunfix(c->batCacheid);
    2157           0 :                         BBPunfix(u_id->batCacheid);
    2158           0 :                         throw(MAL, "sql.delta", GDK_EXCEPTION);
    2159             :                 }
    2160          31 :                 res = BATproject(cminu, c);
    2161          31 :                 BBPunfix(c->batCacheid);
    2162          31 :                 BBPunfix(cminu->batCacheid);
    2163             :                 cminu = NULL;
    2164          31 :                 if (!res) {
    2165           0 :                         BBPunfix(u_id->batCacheid);
    2166           0 :                         throw(MAL, "sql.delta", GDK_EXCEPTION);
    2167             :                 }
    2168             :                 c = res;
    2169             : 
    2170          31 :                 if ((u_val = BATdescriptor(*uval)) == NULL) {
    2171           0 :                         BBPunfix(c->batCacheid);
    2172           0 :                         BBPunfix(u_id->batCacheid);
    2173           0 :                         throw(MAL, "sql.delta", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
    2174             :                 }
    2175          31 :                 if (BATcount(u_val)) {
    2176           4 :                         u = BATproject(u_val, u_id);
    2177           4 :                         BBPunfix(u_val->batCacheid);
    2178           4 :                         BBPunfix(u_id->batCacheid);
    2179           4 :                         if (!u) {
    2180           0 :                                 BBPunfix(c->batCacheid);
    2181           0 :                                 throw(MAL, "sql.delta", GDK_EXCEPTION);
    2182             :                         }
    2183             : 
    2184             :                         /* check selected updated values against candidates */
    2185           4 :                         BAT *c_ids = BATdescriptor(*cid);
    2186             : 
    2187           4 :                         if (!c_ids) {
    2188           0 :                                 BBPunfix(c->batCacheid);
    2189           0 :                                 BBPunfix(u->batCacheid);
    2190           0 :                                 throw(MAL, "sql.delta", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
    2191             :                         }
    2192           4 :                         cminu = BATintersect(u, c_ids, NULL, NULL, false, false, BUN_NONE);
    2193           4 :                         BBPunfix(c_ids->batCacheid);
    2194           4 :                         if (cminu == NULL) {
    2195           0 :                                 BBPunfix(c->batCacheid);
    2196           0 :                                 BBPunfix(u->batCacheid);
    2197           0 :                                 throw(MAL, "sql.delta", GDK_EXCEPTION);
    2198             :                         }
    2199             :                         BAT *nres;
    2200           4 :                         if ((nres = COLcopy(res, res->ttype, true, TRANSIENT)) == NULL) {
    2201           0 :                                 BBPunfix(res->batCacheid);
    2202           0 :                                 BBPunfix(u->batCacheid);
    2203           0 :                                 BBPunfix(cminu->batCacheid);
    2204           0 :                                 throw(MAL, "sql.delta", GDK_EXCEPTION);
    2205             :                         }
    2206           4 :                         BBPunfix(res->batCacheid);
    2207             :                         res = nres;
    2208           4 :                         ret = BATappend(res, u, cminu, true);
    2209           4 :                         BBPunfix(u->batCacheid);
    2210           4 :                         BBPunfix(cminu->batCacheid);
    2211             :                         cminu = NULL;
    2212           4 :                         if (ret != GDK_SUCCEED) {
    2213           0 :                                 BBPunfix(res->batCacheid);
    2214           0 :                                 throw(MAL, "sql.delta", GDK_EXCEPTION);
    2215             :                         }
    2216             : 
    2217           4 :                         ret = BATsort(&u, NULL, NULL, res, NULL, NULL, false, false, false);
    2218           4 :                         BBPunfix(res->batCacheid);
    2219           4 :                         if (ret != GDK_SUCCEED) {
    2220           0 :                                 throw(MAL, "sql.delta", GDK_EXCEPTION);
    2221             :                         }
    2222           4 :                         res = u;
    2223             :                 } else {
    2224          27 :                         BBPunfix(u_val->batCacheid);
    2225          27 :                         BBPunfix(u_id->batCacheid);
    2226             :                 }
    2227             :         }
    2228             : 
    2229          31 :         BATkey(res, true);
    2230          31 :         BBPkeepref(*result = res->batCacheid);
    2231          31 :         return MAL_SUCCEED;
    2232             : }
    2233             : 
    2234             : str
    2235      216539 : DELTAproject(bat *result, const bat *sub, const bat *col, const bat *uid, const bat *uval)
    2236             : {
    2237             :         BAT *s, *c, *u_id, *u_val, *res, *tres;
    2238             : 
    2239      216539 :         if ((s = BATdescriptor(*sub)) == NULL)
    2240           0 :                 throw(MAL, "sql.delta", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
    2241             : 
    2242      216502 :         if ((c = BATdescriptor(*col)) == NULL) {
    2243           0 :                 BBPunfix(s->batCacheid);
    2244           0 :                 throw(MAL, "sql.delta", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
    2245             :         }
    2246             : 
    2247             :         /* projection(sub,col) */
    2248             :         res = c;
    2249      216481 :         tres = BATproject(s, res);
    2250      216471 :         BBPunfix(res->batCacheid);
    2251             : 
    2252      216510 :         if (tres == NULL) {
    2253           0 :                 BBPunfix(s->batCacheid);
    2254           0 :                 throw(MAL, "sql.projectdelta", GDK_EXCEPTION);
    2255             :         }
    2256             :         res = tres;
    2257             : 
    2258      216510 :         if ((u_id = BATdescriptor(*uid)) == NULL) {
    2259           0 :                 BBPunfix(res->batCacheid);
    2260           0 :                 BBPunfix(s->batCacheid);
    2261           0 :                 throw(MAL, "sql.delta", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
    2262             :         }
    2263      216538 :         if (!BATcount(u_id)) {
    2264      216501 :                 BBPunfix(u_id->batCacheid);
    2265      216497 :                 BBPunfix(s->batCacheid);
    2266      216502 :                 BBPkeepref(*result = res->batCacheid);
    2267      216462 :                 return MAL_SUCCEED;
    2268             :         }
    2269          37 :         if ((u_val = BATdescriptor(*uval)) == NULL) {
    2270           0 :                 BBPunfix(u_id->batCacheid);
    2271           0 :                 BBPunfix(res->batCacheid);
    2272           0 :                 BBPunfix(s->batCacheid);
    2273           0 :                 throw(MAL, "sql.delta", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
    2274             :         }
    2275             : 
    2276          37 :         if (BATcount(u_val)) {
    2277             :                 BAT *os, *ou;
    2278             :                 /* figure out the positions in res that we have to
    2279             :                  * replace with values from u_val */
    2280          37 :                 if (BATsemijoin(&ou, &os, u_id, s, NULL, NULL, false, false, BUN_NONE) != GDK_SUCCEED) {
    2281           0 :                         BBPunfix(s->batCacheid);
    2282           0 :                         BBPunfix(res->batCacheid);
    2283           0 :                         BBPunfix(u_id->batCacheid);
    2284           0 :                         BBPunfix(u_val->batCacheid);
    2285           0 :                         throw(MAL, "sql.delta", GDK_EXCEPTION);
    2286             :                 }
    2287             :                 /* BATcount(ou) == BATcount(os) */
    2288          37 :                 if (BATcount(ou) != 0) {
    2289             :                         /* ou contains the position in u_id/u_val that
    2290             :                          * contain the new values */
    2291          12 :                         BAT *nu_val = BATproject(ou, u_val);
    2292          12 :                         BBPunfix(ou->batCacheid);
    2293             :                         /* os contains the corresponding positions in
    2294             :                          * res that need to be replaced with those new
    2295             :                          * values */
    2296          24 :                         if (!nu_val || (res = setwritable(res)) == NULL ||
    2297          12 :                             BATreplace(res, os, nu_val, false) != GDK_SUCCEED) {
    2298           0 :                                 if (res)
    2299           0 :                                         BBPunfix(res->batCacheid);
    2300           0 :                                 BBPunfix(os->batCacheid);
    2301           0 :                                 BBPunfix(s->batCacheid);
    2302           0 :                                 BBPunfix(u_id->batCacheid);
    2303           0 :                                 BBPunfix(u_val->batCacheid);
    2304           0 :                                 if (nu_val)
    2305           0 :                                         BBPunfix(nu_val->batCacheid);
    2306           0 :                                 throw(MAL, "sql.delta", GDK_EXCEPTION);
    2307             :                         }
    2308          12 :                         BBPunfix(nu_val->batCacheid);
    2309             :                 } else {
    2310             :                         /* nothing to replace */
    2311          25 :                         BBPunfix(ou->batCacheid);
    2312             :                 }
    2313          37 :                 BBPunfix(os->batCacheid);
    2314             :         }
    2315          37 :         BBPunfix(s->batCacheid);
    2316          37 :         BBPunfix(u_id->batCacheid);
    2317          37 :         BBPunfix(u_val->batCacheid);
    2318             : 
    2319          37 :         BBPkeepref(*result = res->batCacheid);
    2320          37 :         return MAL_SUCCEED;
    2321             : }
    2322             : 
    2323             : str
    2324          26 : BATleftproject(bat *Res, const bat *Col, const bat *L, const bat *R)
    2325             : {
    2326             :         BAT *c, *l, *r, *res;
    2327             :         oid *p, *lp, *rp;
    2328             :         BUN cnt = 0, i;
    2329             :         BATiter li, ri;
    2330             : 
    2331          26 :         c = BATdescriptor(*Col);
    2332          26 :         if (c)
    2333          26 :                 cnt = BATcount(c);
    2334          26 :         l = BATdescriptor(*L);
    2335          26 :         r = BATdescriptor(*R);
    2336          26 :         res = COLnew(0, TYPE_oid, cnt, TRANSIENT);
    2337          26 :         if (!c || !l || !r || !res) {
    2338           0 :                 if (c)
    2339           0 :                         BBPunfix(c->batCacheid);
    2340           0 :                 if (l)
    2341           0 :                         BBPunfix(l->batCacheid);
    2342           0 :                 if (r)
    2343           0 :                         BBPunfix(r->batCacheid);
    2344           0 :                 if (res)
    2345           0 :                         BBPunfix(res->batCacheid);
    2346           0 :                 throw(MAL, "sql.delta", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
    2347             :         }
    2348          26 :         p = (oid*)Tloc(res,0);
    2349          51 :         for(i=0;i<cnt; i++)
    2350          25 :                 *p++ = oid_nil;
    2351          26 :         BATsetcount(res, cnt);
    2352             : 
    2353          26 :         cnt = BATcount(l);
    2354          26 :         p = (oid*)Tloc(res, 0);
    2355          26 :         li = bat_iterator(l);
    2356          26 :         ri = bat_iterator(r);
    2357          26 :         lp = (oid*)li.base;
    2358          26 :         rp = (oid*)ri.base;
    2359          26 :         if (l->ttype == TYPE_void) {
    2360          11 :                 oid lp = l->tseqbase;
    2361          11 :                 if (r->ttype == TYPE_void) {
    2362          11 :                         oid rp = r->tseqbase;
    2363          11 :                         for(i=0;i<cnt; i++, lp++, rp++)
    2364           0 :                                 p[lp] = rp;
    2365             :                 } else {
    2366           0 :                         for(i=0;i<cnt; i++, lp++)
    2367           0 :                                 p[lp] = rp[i];
    2368             :                 }
    2369             :         }
    2370          26 :         if (r->ttype == TYPE_void) {
    2371          23 :                 oid rp = r->tseqbase;
    2372          35 :                 for(i=0;i<cnt; i++, rp++)
    2373          12 :                         p[lp[i]] = rp;
    2374             :         } else {
    2375           9 :                 for(i=0;i<cnt; i++)
    2376           6 :                         p[lp[i]] = rp[i];
    2377             :         }
    2378          26 :         bat_iterator_end(&li);
    2379          26 :         bat_iterator_end(&ri);
    2380          26 :         res->tsorted = false;
    2381          26 :         res->trevsorted = false;
    2382          26 :         res->tnil = false;
    2383          26 :         res->tnonil = false;
    2384          26 :         res->tkey = false;
    2385          26 :         BBPunfix(c->batCacheid);
    2386          26 :         BBPunfix(l->batCacheid);
    2387          26 :         BBPunfix(r->batCacheid);
    2388          26 :         BBPkeepref(*Res = res->batCacheid);
    2389          26 :         return MAL_SUCCEED;
    2390             : }
    2391             : 
    2392             : /* str SQLtid(bat *result, mvc *m, str *sname, str *tname) */
    2393             : str
    2394      265742 : SQLtid(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    2395             : {
    2396      265742 :         bat *res = getArgReference_bat(stk, pci, 0);
    2397      265742 :         mvc *m = NULL;
    2398             :         str msg = MAL_SUCCEED;
    2399             :         sql_trans *tr;
    2400      265742 :         const char *sname = *getArgReference_str(stk, pci, 2);
    2401      265742 :         const char *tname = *getArgReference_str(stk, pci, 3);
    2402             :         sql_schema *s;
    2403             :         sql_table *t;
    2404             : 
    2405      265742 :         *res = bat_nil;
    2406      265742 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    2407             :                 return msg;
    2408      265742 :         tr = m->session->tr;
    2409      265742 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    2410             :                 return msg;
    2411      265742 :         s = mvc_bind_schema(m, sname);
    2412      265742 :         if (s == NULL)
    2413           0 :                 throw(SQL, "sql.tid", SQLSTATE(3F000) "Schema missing %s",sname);
    2414      265742 :         t = mvc_bind_table(m, s, tname);
    2415      265742 :         if (t == NULL)
    2416           0 :                 throw(SQL, "sql.tid", SQLSTATE(42S02) "Table missing %s.%s",sname,tname);
    2417      265742 :         if (!isTable(t))
    2418           0 :                 throw(SQL, "sql.tid", SQLSTATE(42000) "%s '%s' is not persistent",
    2419           0 :                           TABLE_TYPE_DESCRIPTION(t->type, t->properties), t->base.name);
    2420             : 
    2421      265742 :         sqlstore *store = m->store;
    2422             :         /* we have full table count, nr of deleted (unused rows) */
    2423             :         int part_nr = 0;
    2424             :         int nr_parts = 1;
    2425      265742 :         if (pci->argc == 6) {        /* partitioned version */
    2426      145252 :                 part_nr = *getArgReference_int(stk, pci, 4);
    2427      145252 :                 nr_parts = *getArgReference_int(stk, pci, 5);
    2428             :         }
    2429      265742 :         BAT *b = store->storage_api.bind_cands(tr, t, nr_parts, part_nr);
    2430      265742 :         if (b) {
    2431      265742 :                 BBPkeepref(*res = b->batCacheid);
    2432             :         } else {
    2433           0 :                 msg = createException(SQL, "sql.tid", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    2434             :         }
    2435             :         return msg;
    2436             : }
    2437             : 
    2438             : /* unsafe pattern resultSet(tbl:bat[:str], attr:bat[:str], tpe:bat[:str], len:bat[:int],scale:bat[:int], cols:bat[:any]...) :int */
    2439             : /* New result set rendering infrastructure */
    2440             : 
    2441             : static str
    2442       49345 : mvc_result_set_wrap( Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    2443             : {
    2444       49345 :         int *res_id =getArgReference_int(stk,pci,0);
    2445       49345 :         bat tblId= *getArgReference_bat(stk, pci,1);
    2446       49345 :         bat atrId= *getArgReference_bat(stk, pci,2);
    2447       49345 :         bat tpeId= *getArgReference_bat(stk, pci,3);
    2448       49345 :         bat lenId= *getArgReference_bat(stk, pci,4);
    2449       49345 :         bat scaleId= *getArgReference_bat(stk, pci,5);
    2450             :         bat bid;
    2451             :         int i, res, ok;
    2452             :         str tblname, colname, tpename, msg= MAL_SUCCEED;
    2453             :         int *digits, *scaledigits;
    2454             :         oid o = 0;
    2455             :         BATiter itertbl,iteratr,itertpe,iterdig,iterscl;
    2456       49345 :         backend *be = NULL;
    2457             :         BAT *b = NULL, *tbl = NULL, *atr = NULL, *tpe = NULL,*len = NULL,*scale = NULL;
    2458             : 
    2459       49345 :         if ((msg = getBackendContext(cntxt, &be)) != NULL)
    2460             :                 return msg;
    2461       49345 :         bid = *getArgReference_bat(stk,pci,6);
    2462       49345 :         b = BATdescriptor(bid);
    2463       49345 :         if ( b == NULL) {
    2464           0 :                 msg = createException(SQL, "sql.resultSet", SQLSTATE(HY005) "Cannot access column descriptor");
    2465           0 :                 goto wrapup_result_set;
    2466             :         }
    2467       49345 :         res = *res_id = mvc_result_table(be, mb->tag, pci->argc - (pci->retc + 5), Q_TABLE, b);
    2468       49345 :         BBPunfix(b->batCacheid);
    2469       49345 :         if (res < 0) {
    2470           0 :                 msg = createException(SQL, "sql.resultSet", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    2471           0 :                 goto wrapup_result_set;
    2472             :         }
    2473             : 
    2474       49345 :         tbl = BATdescriptor(tblId);
    2475       49345 :         atr = BATdescriptor(atrId);
    2476       49345 :         tpe = BATdescriptor(tpeId);
    2477       49345 :         len = BATdescriptor(lenId);
    2478       49345 :         scale = BATdescriptor(scaleId);
    2479       49345 :         if (tbl == NULL || atr == NULL || tpe == NULL || len == NULL || scale == NULL)
    2480           0 :                 goto wrapup_result_set;
    2481             :         /* mimick the old rsColumn approach; */
    2482       49345 :         itertbl = bat_iterator(tbl);
    2483       49345 :         iteratr = bat_iterator(atr);
    2484       49345 :         itertpe = bat_iterator(tpe);
    2485       49345 :         iterdig = bat_iterator(len);
    2486       49345 :         iterscl = bat_iterator(scale);
    2487       49345 :         digits = (int*) iterdig.base;
    2488       49345 :         scaledigits = (int*) iterscl.base;
    2489             : 
    2490      269643 :         for( i = 6; msg == MAL_SUCCEED && i< pci->argc; i++, o++){
    2491      220298 :                 bid = *getArgReference_bat(stk,pci,i);
    2492      220298 :                 tblname = BUNtvar(itertbl,o);
    2493      220298 :                 colname = BUNtvar(iteratr,o);
    2494      220298 :                 tpename = BUNtvar(itertpe,o);
    2495      220298 :                 b = BATdescriptor(bid);
    2496      220298 :                 if ( b == NULL)
    2497           0 :                         msg = createException(SQL, "sql.resultSet", SQLSTATE(HY005) "Cannot access column descriptor");
    2498      220298 :                 else if (mvc_result_column(be, tblname, colname, tpename, *digits++, *scaledigits++, b))
    2499           0 :                         msg = createException(SQL, "sql.resultSet", SQLSTATE(42000) "Cannot access column descriptor %s.%s",tblname,colname);
    2500      220298 :                 if( b)
    2501      220298 :                         BBPunfix(bid);
    2502             :         }
    2503       49345 :         bat_iterator_end(&itertbl);
    2504       49345 :         bat_iterator_end(&iteratr);
    2505       49345 :         bat_iterator_end(&itertpe);
    2506       49345 :         bat_iterator_end(&iterdig);
    2507       49345 :         bat_iterator_end(&iterscl);
    2508             :         /* now send it to the channel cntxt->fdout */
    2509       49345 :         if (!msg && (ok = mvc_export_result(cntxt->sqlcontext, cntxt->fdout, res, true, mb->starttime, mb->optimize)) < 0)
    2510           0 :                 msg = createException(SQL, "sql.resultSet", SQLSTATE(45000) "Result set construction failed: %s", mvc_export_error(cntxt->sqlcontext, cntxt->fdout, ok));
    2511       49345 :   wrapup_result_set:
    2512       49345 :         mb->starttime = 0;
    2513       49345 :         mb->optimize = 0;
    2514       49345 :         if( tbl) BBPunfix(tblId);
    2515       49345 :         if( atr) BBPunfix(atrId);
    2516       49345 :         if( tpe) BBPunfix(tpeId);
    2517       49345 :         if( len) BBPunfix(lenId);
    2518       49345 :         if( scale) BBPunfix(scaleId);
    2519             :         return msg;
    2520             : }
    2521             : 
    2522             : /* Copy the result set into a CSV file */
    2523             : str
    2524          11 : mvc_export_table_wrap( Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    2525             : {
    2526          11 :         int *res_id =getArgReference_int(stk,pci,0);
    2527          11 :         const char *filename = *getArgReference_str(stk,pci,1);
    2528          11 :         const char *format = *getArgReference_str(stk,pci,2);
    2529          11 :         const char *tsep = *getArgReference_str(stk, pci, 3);
    2530          11 :         const char *rsep = *getArgReference_str(stk, pci, 4);
    2531          11 :         const char *ssep = *getArgReference_str(stk, pci, 5);
    2532          11 :         const char *ns = *getArgReference_str(stk, pci, 6);
    2533          11 :         int onclient = *getArgReference_int(stk, pci, 7);
    2534             : 
    2535          11 :         bat tblId= *getArgReference_bat(stk, pci,8);
    2536          11 :         bat atrId= *getArgReference_bat(stk, pci,9);
    2537          11 :         bat tpeId= *getArgReference_bat(stk, pci,10);
    2538          11 :         bat lenId= *getArgReference_bat(stk, pci,11);
    2539          11 :         bat scaleId= *getArgReference_bat(stk, pci,12);
    2540             :         stream *s = NULL;
    2541             :         bat bid;
    2542             :         int i, res, ok;
    2543             :         str tblname, colname, tpename, msg= MAL_SUCCEED;
    2544             :         int *digits, *scaledigits;
    2545             :         oid o = 0;
    2546             :         BATiter itertbl,iteratr,itertpe,iterdig,iterscl;
    2547             :         backend *be;
    2548             :         mvc *m = NULL;
    2549             :         BAT *order = NULL, *b = NULL, *tbl = NULL, *atr = NULL, *tpe = NULL,*len = NULL,*scale = NULL;
    2550             :         res_table *t = NULL;
    2551             :         bool tostdout;
    2552             :         char buf[80];
    2553             :         ssize_t sz;
    2554             : 
    2555             :         (void) format;
    2556             : 
    2557          11 :         if ((msg = getBackendContext(cntxt, &be)) != NULL)
    2558             :                 return msg;
    2559          11 :         m = be->mvc;
    2560             : 
    2561          11 :         if (onclient && !cntxt->filetrans) {
    2562           0 :                 msg = createException(SQL, "sql.resultSet", SQLSTATE(42000) "Cannot transfer files to client");
    2563           0 :                 goto wrapup_result_set1;
    2564             :         }
    2565             : 
    2566          11 :         bid = *getArgReference_bat(stk,pci,13);
    2567          11 :         order = BATdescriptor(bid);
    2568          11 :         if ( order == NULL) {
    2569           0 :                 msg = createException(SQL, "sql.resultSet", SQLSTATE(HY005) "Cannot access column descriptor");
    2570           0 :                 goto wrapup_result_set1;
    2571             :         }
    2572          11 :         res = *res_id = mvc_result_table(be, mb->tag, pci->argc - (pci->retc + 12), Q_TABLE, order);
    2573          11 :         t = be->results;
    2574          11 :         if (res < 0) {
    2575           0 :                 msg = createException(SQL, "sql.resultSet", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    2576           0 :                 goto wrapup_result_set1;
    2577             :         }
    2578             : 
    2579          11 :         t->tsep = tsep;
    2580          11 :         t->rsep = rsep;
    2581          11 :         t->ssep = ssep;
    2582          11 :         t->ns = ns;
    2583             : 
    2584          11 :         tbl = BATdescriptor(tblId);
    2585          11 :         atr = BATdescriptor(atrId);
    2586          11 :         tpe = BATdescriptor(tpeId);
    2587          11 :         len = BATdescriptor(lenId);
    2588          11 :         scale = BATdescriptor(scaleId);
    2589          11 :         if( tbl == NULL || atr == NULL || tpe == NULL || len == NULL || scale == NULL)
    2590           0 :                 goto wrapup_result_set1;
    2591             :         /* mimick the old rsColumn approach; */
    2592          11 :         itertbl = bat_iterator(tbl);
    2593          11 :         iteratr = bat_iterator(atr);
    2594          11 :         itertpe = bat_iterator(tpe);
    2595          11 :         iterdig = bat_iterator(len);
    2596          11 :         iterscl = bat_iterator(scale);
    2597          11 :         digits = (int*) iterdig.base;
    2598          11 :         scaledigits = (int*) iterscl.base;
    2599             : 
    2600          34 :         for( i = 13; msg == MAL_SUCCEED && i< pci->argc; i++, o++){
    2601          23 :                 bid = *getArgReference_bat(stk,pci,i);
    2602          23 :                 tblname = BUNtvar(itertbl,o);
    2603          23 :                 colname = BUNtvar(iteratr,o);
    2604          23 :                 tpename = BUNtvar(itertpe,o);
    2605          23 :                 b = BATdescriptor(bid);
    2606          23 :                 if ( b == NULL)
    2607           0 :                         msg = createException(SQL, "sql.resultSet", SQLSTATE(HY005) "Cannot access column descriptor");
    2608          23 :                 else if (mvc_result_column(be, tblname, colname, tpename, *digits++, *scaledigits++, b))
    2609           0 :                         msg = createException(SQL, "sql.resultSet", SQLSTATE(42000) "Cannot access column descriptor %s.%s",tblname,colname);
    2610          23 :                 if( b)
    2611          23 :                         BBPunfix(bid);
    2612             :         }
    2613          11 :         bat_iterator_end(&itertbl);
    2614          11 :         bat_iterator_end(&iteratr);
    2615          11 :         bat_iterator_end(&itertpe);
    2616          11 :         bat_iterator_end(&iterdig);
    2617          11 :         bat_iterator_end(&iterscl);
    2618          11 :         if ( msg )
    2619           0 :                 goto wrapup_result_set1;
    2620             : 
    2621             :         /* now select the file channel */
    2622          11 :         if ((tostdout = strcmp(filename,"stdout") == 0)) {
    2623           8 :                 s = cntxt->fdout;
    2624           3 :         } else if (!onclient) {
    2625           3 :                 if ((s = open_wastream(filename)) == NULL || mnstr_errnr(s)) {
    2626           0 :                         msg=  createException(IO, "streams.open", SQLSTATE(42000) "%s", mnstr_peek_error(NULL));
    2627           0 :                         close_stream(s);
    2628           0 :                         goto wrapup_result_set1;
    2629             :                 }
    2630           3 :                 be->output_format = OFMT_CSV;
    2631             :         } else {
    2632           0 :                 while (!m->scanner.rs->eof)
    2633           0 :                         bstream_next(m->scanner.rs);
    2634           0 :                 s = m->scanner.ws;
    2635           0 :                 mnstr_write(s, PROMPT3, sizeof(PROMPT3) - 1, 1);
    2636           0 :                 mnstr_printf(s, "w %s\n", filename);
    2637           0 :                 mnstr_flush(s, MNSTR_FLUSH_DATA);
    2638           0 :                 if ((sz = mnstr_readline(m->scanner.rs->s, buf, sizeof(buf))) > 1) {
    2639             :                         /* non-empty line indicates failure on client */
    2640           0 :                         msg = createException(IO, "streams.open", "%s", buf);
    2641             :                         /* discard until client flushes */
    2642           0 :                         while (mnstr_read(m->scanner.rs->s, buf, 1, sizeof(buf)) > 0) {
    2643             :                                 /* ignore remainder of error message */
    2644             :                         }
    2645           0 :                         goto wrapup_result_set1;
    2646             :                 }
    2647             :         }
    2648          11 :         if ((ok = mvc_export_result(cntxt->sqlcontext, s, res, tostdout, mb->starttime, mb->optimize)) < 0) {
    2649           0 :                 msg = createException(SQL, "sql.resultSet", SQLSTATE(45000) "Result set construction failed: %s", mvc_export_error(cntxt->sqlcontext, s, ok));
    2650           0 :                 if (!onclient && !tostdout)
    2651           0 :                         close_stream(s);
    2652           0 :                 goto wrapup_result_set1;
    2653             :         }
    2654          11 :         if (onclient) {
    2655           0 :                 mnstr_flush(s, MNSTR_FLUSH_DATA);
    2656           0 :                 if ((sz = mnstr_readline(m->scanner.rs->s, buf, sizeof(buf))) > 1) {
    2657           0 :                         msg = createException(IO, "streams.open", "%s", buf);
    2658             :                 }
    2659           0 :                 while (sz > 0)
    2660           0 :                         sz = mnstr_readline(m->scanner.rs->s, buf, sizeof(buf));
    2661          11 :         } else if (!tostdout) {
    2662           3 :                 close_stream(s);
    2663             :         }
    2664           8 :   wrapup_result_set1:
    2665          11 :         mb->starttime = 0;
    2666          11 :         mb->optimize = 0;
    2667          11 :         if( order) BBPunfix(order->batCacheid);
    2668          11 :         if( tbl) BBPunfix(tblId);
    2669          11 :         if( atr) BBPunfix(atrId);
    2670          11 :         if( tpe) BBPunfix(tpeId);
    2671          11 :         if( len) BBPunfix(lenId);
    2672          11 :         if( scale) BBPunfix(scaleId);
    2673             :         return msg;
    2674             : }
    2675             : 
    2676             : /* unsafe pattern resultSet(tbl:bat[:str], attr:bat[:str], tpe:bat[:str], len:bat[:int],scale:bat[:int], cols:any...) :int */
    2677             : str
    2678         361 : mvc_row_result_wrap( Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    2679             : {
    2680         361 :         int *res_id= getArgReference_int(stk, pci,0);
    2681         361 :         bat tblId= *getArgReference_bat(stk, pci,1);
    2682         361 :         bat atrId= *getArgReference_bat(stk, pci,2);
    2683         361 :         bat tpeId= *getArgReference_bat(stk, pci,3);
    2684         361 :         bat lenId= *getArgReference_bat(stk, pci,4);
    2685         361 :         bat scaleId= *getArgReference_bat(stk, pci,5);
    2686             :         int i, res, ok;
    2687             :         str tblname, colname, tpename, msg= MAL_SUCCEED;
    2688             :         int *digits, *scaledigits;
    2689             :         oid o = 0;
    2690             :         BATiter itertbl,iteratr,itertpe,iterdig,iterscl;
    2691         361 :         backend *be = NULL;
    2692             :         ptr v;
    2693             :         int mtype;
    2694             :         BAT *tbl = NULL, *atr = NULL, *tpe = NULL, *len = NULL, *scale = NULL;
    2695             : 
    2696         361 :         if ((msg = getBackendContext(cntxt, &be)) != NULL)
    2697             :                 return msg;
    2698         361 :         res = *res_id = mvc_result_table(be, mb->tag, pci->argc - (pci->retc + 5), Q_TABLE, NULL);
    2699         361 :         if (res < 0) {
    2700           0 :                 msg = createException(SQL, "sql.resultSet", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    2701           0 :                 goto wrapup_result_set;
    2702             :         }
    2703             : 
    2704         361 :         tbl = BATdescriptor(tblId);
    2705         361 :         atr = BATdescriptor(atrId);
    2706         361 :         tpe = BATdescriptor(tpeId);
    2707         361 :         len = BATdescriptor(lenId);
    2708         361 :         scale = BATdescriptor(scaleId);
    2709         361 :         if( tbl == NULL || atr == NULL || tpe == NULL || len == NULL || scale == NULL)
    2710           0 :                 goto wrapup_result_set;
    2711             :         /* mimick the old rsColumn approach; */
    2712         361 :         itertbl = bat_iterator(tbl);
    2713         361 :         iteratr = bat_iterator(atr);
    2714         361 :         itertpe = bat_iterator(tpe);
    2715         361 :         iterdig = bat_iterator(len);
    2716         361 :         iterscl = bat_iterator(scale);
    2717         361 :         digits = (int*) iterdig.base;
    2718         361 :         scaledigits = (int*) iterscl.base;
    2719             : 
    2720        1272 :         for( i = 6; msg == MAL_SUCCEED && i< pci->argc; i++, o++){
    2721         911 :                 tblname = BUNtvar(itertbl,o);
    2722         911 :                 colname = BUNtvar(iteratr,o);
    2723         911 :                 tpename = BUNtvar(itertpe,o);
    2724             : 
    2725         911 :                 v = getArgReference(stk, pci, i);
    2726         911 :                 mtype = getArgType(mb, pci, i);
    2727         911 :                 if (ATOMextern(mtype))
    2728         138 :                         v = *(ptr *) v;
    2729         911 :                 if ((ok = mvc_result_value(be, tblname, colname, tpename, *digits++, *scaledigits++, v, mtype) < 0)) {
    2730           0 :                         msg = createException(SQL, "sql.rsColumn", SQLSTATE(45000) "Result set construction failed: %s", mvc_export_error(be, be->out, ok));
    2731           0 :                         bat_iterator_end(&itertbl);
    2732           0 :                         bat_iterator_end(&iteratr);
    2733           0 :                         bat_iterator_end(&itertpe);
    2734           0 :                         bat_iterator_end(&iterdig);
    2735           0 :                         bat_iterator_end(&iterscl);
    2736           0 :                         goto wrapup_result_set;
    2737             :                 }
    2738             :         }
    2739         361 :         bat_iterator_end(&itertbl);
    2740         361 :         bat_iterator_end(&iteratr);
    2741         361 :         bat_iterator_end(&itertpe);
    2742         361 :         bat_iterator_end(&iterdig);
    2743         361 :         bat_iterator_end(&iterscl);
    2744         361 :         if (!msg && (ok = mvc_export_result(cntxt->sqlcontext, cntxt->fdout, res, true, mb->starttime, mb->optimize)) < 0)
    2745           0 :                 msg = createException(SQL, "sql.resultSet", SQLSTATE(45000) "Result set construction failed: %s", mvc_export_error(cntxt->sqlcontext, cntxt->fdout, ok));
    2746         361 :   wrapup_result_set:
    2747         361 :         mb->starttime = 0;
    2748         361 :         mb->optimize = 0;
    2749         361 :         if( tbl) BBPunfix(tblId);
    2750         361 :         if( atr) BBPunfix(atrId);
    2751         361 :         if( tpe) BBPunfix(tpeId);
    2752         361 :         if( len) BBPunfix(lenId);
    2753         361 :         if( scale) BBPunfix(scaleId);
    2754             :         return msg;
    2755             : }
    2756             : 
    2757             : str
    2758           1 : mvc_export_row_wrap( Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    2759             : {
    2760           1 :         int *res_id= getArgReference_int(stk, pci,0);
    2761           1 :         str filename = * getArgReference_str(stk,pci,1);
    2762           1 :         const char *format = *getArgReference_str(stk,pci,2);
    2763           1 :         const char *tsep = *getArgReference_str(stk, pci, 3);
    2764           1 :         const char *rsep = *getArgReference_str(stk, pci, 4);
    2765           1 :         const char *ssep = *getArgReference_str(stk, pci, 5);
    2766           1 :         const char *ns = *getArgReference_str(stk, pci, 6);
    2767           1 :         int onclient = *getArgReference_int(stk, pci, 7);
    2768             : 
    2769           1 :         bat tblId= *getArgReference_bat(stk, pci,8);
    2770           1 :         bat atrId= *getArgReference_bat(stk, pci,9);
    2771           1 :         bat tpeId= *getArgReference_bat(stk, pci,10);
    2772           1 :         bat lenId= *getArgReference_bat(stk, pci,11);
    2773           1 :         bat scaleId= *getArgReference_bat(stk, pci,12);
    2774             : 
    2775             :         int i, res, ok;
    2776             :         stream *s = NULL;
    2777             :         str tblname, colname, tpename, msg= MAL_SUCCEED;
    2778             :         int *digits, *scaledigits;
    2779             :         oid o = 0;
    2780             :         BATiter itertbl,iteratr,itertpe,iterdig,iterscl;
    2781             :         backend *be;
    2782             :         mvc *m = NULL;
    2783             :         res_table *t = NULL;
    2784             :         ptr v;
    2785             :         int mtype;
    2786             :         BAT  *tbl = NULL, *atr = NULL, *tpe = NULL,*len = NULL,*scale = NULL;
    2787             :         bool tostdout;
    2788             :         char buf[80];
    2789             :         ssize_t sz;
    2790             : 
    2791             :         (void) format;
    2792           1 :         if ((msg = getBackendContext(cntxt, &be)) != NULL)
    2793             :                 return msg;
    2794           1 :         m = be->mvc;
    2795           1 :         if (onclient && !cntxt->filetrans) {
    2796           0 :                 msg = createException(SQL, "sql.resultSet", SQLSTATE(42000) "Cannot transfer files to client");
    2797           0 :                 goto wrapup_result_set;
    2798             :         }
    2799             : 
    2800           1 :         res = *res_id = mvc_result_table(be, mb->tag, pci->argc - (pci->retc + 12), Q_TABLE, NULL);
    2801             : 
    2802           1 :         t = be->results;
    2803           1 :         if (res < 0){
    2804           0 :                 msg = createException(SQL, "sql.resultSet", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    2805           0 :                 goto wrapup_result_set;
    2806             :         }
    2807             : 
    2808           1 :         t->tsep = tsep;
    2809           1 :         t->rsep = rsep;
    2810           1 :         t->ssep = ssep;
    2811           1 :         t->ns = ns;
    2812             : 
    2813           1 :         tbl = BATdescriptor(tblId);
    2814           1 :         atr = BATdescriptor(atrId);
    2815           1 :         tpe = BATdescriptor(tpeId);
    2816           1 :         len = BATdescriptor(lenId);
    2817           1 :         scale = BATdescriptor(scaleId);
    2818           1 :         if (tbl == NULL || atr == NULL || tpe == NULL || len == NULL || scale == NULL)
    2819           0 :                 goto wrapup_result_set;
    2820             :         /* mimick the old rsColumn approach; */
    2821           1 :         itertbl = bat_iterator(tbl);
    2822           1 :         iteratr = bat_iterator(atr);
    2823           1 :         itertpe = bat_iterator(tpe);
    2824           1 :         iterdig = bat_iterator(len);
    2825           1 :         iterscl = bat_iterator(scale);
    2826           1 :         digits = (int*) iterdig.base;
    2827           1 :         scaledigits = (int*) iterscl.base;
    2828             : 
    2829           2 :         for( i = 13; msg == MAL_SUCCEED && i< pci->argc; i++, o++){
    2830           1 :                 tblname = BUNtvar(itertbl,o);
    2831           1 :                 colname = BUNtvar(iteratr,o);
    2832           1 :                 tpename = BUNtvar(itertpe,o);
    2833             : 
    2834           1 :                 v = getArgReference(stk, pci, i);
    2835           1 :                 mtype = getArgType(mb, pci, i);
    2836           1 :                 if (ATOMextern(mtype))
    2837           0 :                         v = *(ptr *) v;
    2838           1 :                 if ((ok = mvc_result_value(be, tblname, colname, tpename, *digits++, *scaledigits++, v, mtype)) < 0) {
    2839           0 :                         msg = createException(SQL, "sql.rsColumn", SQLSTATE(45000) "Result set construction failed: %s", mvc_export_error(be, s, ok));
    2840           0 :                         bat_iterator_end(&itertbl);
    2841           0 :                         bat_iterator_end(&iteratr);
    2842           0 :                         bat_iterator_end(&itertpe);
    2843           0 :                         bat_iterator_end(&iterdig);
    2844           0 :                         bat_iterator_end(&iterscl);
    2845           0 :                         goto wrapup_result_set;
    2846             :                 }
    2847             :         }
    2848           1 :         bat_iterator_end(&itertbl);
    2849           1 :         bat_iterator_end(&iteratr);
    2850           1 :         bat_iterator_end(&itertpe);
    2851           1 :         bat_iterator_end(&iterdig);
    2852           1 :         bat_iterator_end(&iterscl);
    2853             :         /* now select the file channel */
    2854           1 :         if ((tostdout = strcmp(filename,"stdout") == 0)) {
    2855           1 :                 s = cntxt->fdout;
    2856           0 :         } else if (!onclient) {
    2857           0 :                 if ((s = open_wastream(filename)) == NULL || mnstr_errnr(s)) {
    2858           0 :                         msg=  createException(IO, "streams.open", SQLSTATE(42000) "%s", mnstr_peek_error(NULL));
    2859           0 :                         close_stream(s);
    2860           0 :                         goto wrapup_result_set;
    2861             :                 }
    2862             :         } else {
    2863           0 :                 while (!m->scanner.rs->eof)
    2864           0 :                         bstream_next(m->scanner.rs);
    2865           0 :                 s = m->scanner.ws;
    2866           0 :                 mnstr_write(s, PROMPT3, sizeof(PROMPT3) - 1, 1);
    2867           0 :                 mnstr_printf(s, "w %s\n", filename);
    2868           0 :                 mnstr_flush(s, MNSTR_FLUSH_DATA);
    2869           0 :                 if ((sz = mnstr_readline(m->scanner.rs->s, buf, sizeof(buf))) > 1) {
    2870             :                         /* non-empty line indicates failure on client */
    2871           0 :                         msg = createException(IO, "streams.open", "%s", buf);
    2872             :                         /* discard until client flushes */
    2873           0 :                         while (mnstr_read(m->scanner.rs->s, buf, 1, sizeof(buf)) > 0) {
    2874             :                                 /* ignore remainder of error message */
    2875             :                         }
    2876           0 :                         goto wrapup_result_set;
    2877             :                 }
    2878             :         }
    2879           1 :         if ((ok = mvc_export_result(cntxt->sqlcontext, s, res, strcmp(filename, "stdout") == 0, mb->starttime, mb->optimize)) < 0) {
    2880           0 :                 msg = createException(SQL, "sql.resultSet", SQLSTATE(45000) "Result set construction failed: %s", mvc_export_error(cntxt->sqlcontext, s, ok));
    2881           0 :                 if (!onclient && !tostdout)
    2882           0 :                         close_stream(s);
    2883           0 :                 goto wrapup_result_set;
    2884             :         }
    2885           1 :         if (onclient) {
    2886           0 :                 mnstr_flush(s, MNSTR_FLUSH_DATA);
    2887           0 :                 if ((sz = mnstr_readline(m->scanner.rs->s, buf, sizeof(buf))) > 1) {
    2888           0 :                         msg = createException(IO, "streams.open", "%s", buf);
    2889             :                 }
    2890           0 :                 while (sz > 0)
    2891           0 :                         sz = mnstr_readline(m->scanner.rs->s, buf, sizeof(buf));
    2892           1 :         } else if (!tostdout) {
    2893           0 :                 close_stream(s);
    2894             :         }
    2895           1 :   wrapup_result_set:
    2896           1 :         mb->starttime = 0;
    2897           1 :         mb->optimize = 0;
    2898           1 :         if( tbl) BBPunfix(tblId);
    2899           1 :         if( atr) BBPunfix(atrId);
    2900           1 :         if( tpe) BBPunfix(tpeId);
    2901           1 :         if( len) BBPunfix(lenId);
    2902           1 :         if( scale) BBPunfix(scaleId);
    2903             :         return msg;
    2904             : }
    2905             : 
    2906             : str
    2907       49345 : mvc_table_result_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    2908             : {
    2909             :         str res = MAL_SUCCEED;
    2910             :         BAT *order;
    2911       49345 :         backend *be = NULL;
    2912             :         str msg;
    2913             :         int *res_id;
    2914             :         int nr_cols;
    2915             :         mapi_query_t qtype;
    2916             :         bat order_bid;
    2917             : 
    2918       49345 :         if ( pci->argc > 6)
    2919       49345 :                 return mvc_result_set_wrap(cntxt,mb,stk,pci);
    2920             : 
    2921           0 :         res_id = getArgReference_int(stk, pci, 0);
    2922           0 :         nr_cols = *getArgReference_int(stk, pci, 1);
    2923           0 :         qtype = (mapi_query_t) *getArgReference_int(stk, pci, 2);
    2924           0 :         order_bid = *getArgReference_bat(stk, pci, 3);
    2925             : 
    2926           0 :         if ((msg = getBackendContext(cntxt, &be)) != NULL)
    2927             :                 return msg;
    2928           0 :         if ((order = BATdescriptor(order_bid)) == NULL)
    2929           0 :                 throw(SQL, "sql.resultSet", SQLSTATE(HY005) "Cannot access column descriptor");
    2930           0 :         *res_id = mvc_result_table(be, mb->tag, nr_cols, qtype, order);
    2931           0 :         if (*res_id < 0)
    2932           0 :                 res = createException(SQL, "sql.resultSet", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    2933           0 :         BBPunfix(order->batCacheid);
    2934           0 :         return res;
    2935             : }
    2936             : 
    2937             : /* str mvc_affected_rows_wrap(int *m, int m, lng *nr, str *w); */
    2938             : str
    2939       73811 : mvc_affected_rows_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    2940             : {
    2941             :         backend *b = NULL;
    2942       73811 :         int *res = getArgReference_int(stk, pci, 0), ok;
    2943             : #ifndef NDEBUG
    2944       73811 :         int mtype = getArgType(mb, pci, 2);
    2945             : #endif
    2946             :         lng nr;
    2947             :         str msg;
    2948             : 
    2949             :         (void) mb;              /* NOT USED */
    2950       73811 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    2951             :                 return msg;
    2952       73811 :         *res = 0;
    2953       73811 :         assert(mtype == TYPE_lng);
    2954       73811 :         nr = *getArgReference_lng(stk, pci, 2);
    2955       73811 :         b = cntxt->sqlcontext;
    2956       73811 :         ok = mvc_export_affrows(b, b->out, nr, "", mb->tag, mb->starttime, mb->optimize);
    2957       73811 :         mb->starttime = 0;
    2958       73811 :         mb->optimize = 0;
    2959       73811 :         if (ok < 0)
    2960           0 :                 throw(SQL, "sql.affectedRows", SQLSTATE(45000) "Result set construction failed: %s", mvc_export_error(b, b->out, ok));
    2961             :         return MAL_SUCCEED;
    2962             : }
    2963             : 
    2964             : /* str mvc_export_head_wrap(int *ret, stream **s, int *res_id); */
    2965             : str
    2966           0 : mvc_export_head_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    2967             : {
    2968             :         backend *b = NULL;
    2969           0 :         stream **s = (stream **) getArgReference(stk, pci, 1);
    2970           0 :         int res_id = *getArgReference_int(stk, pci, 2), ok;
    2971             :         str msg;
    2972             : 
    2973             :         (void) mb;              /* NOT USED */
    2974           0 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    2975             :                 return msg;
    2976           0 :         b = cntxt->sqlcontext;
    2977           0 :         ok = mvc_export_head(b, *s, res_id, FALSE, TRUE, mb->starttime, mb->optimize);
    2978           0 :         mb->starttime = 0;
    2979           0 :         mb->optimize = 0;
    2980           0 :         if (ok < 0)
    2981           0 :                 throw(SQL, "sql.exportHead", SQLSTATE(45000) "Result set construction failed: %s", mvc_export_error(b, *s, ok));
    2982             :         return MAL_SUCCEED;
    2983             : }
    2984             : 
    2985             : /* str mvc_export_result_wrap(int *ret, stream **s, int *res_id); */
    2986             : str
    2987           0 : mvc_export_result_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    2988             : {
    2989             :         backend *b = NULL;
    2990           0 :         stream **s = (stream **) getArgReference(stk, pci, 1), *sout;
    2991           0 :         int res_id = *getArgReference_int(stk, pci, 2), ok;
    2992             :         str msg;
    2993             : 
    2994             :         (void) mb;              /* NOT USED */
    2995           0 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    2996             :                 return msg;
    2997           0 :         b = cntxt->sqlcontext;
    2998           0 :         sout = pci->argc > 5 ? cntxt->fdout : *s;
    2999           0 :         ok = mvc_export_result(b, sout, res_id, false, mb->starttime, mb->optimize);
    3000           0 :         mb->starttime = 0;
    3001           0 :         mb->optimize = 0;
    3002           0 :         if (ok < 0)
    3003           0 :                 throw(SQL, "sql.exportResult", SQLSTATE(45000) "Result set construction failed: %s", mvc_export_error(b, sout, ok));
    3004             :         return MAL_SUCCEED;
    3005             : }
    3006             : 
    3007             : /* str mvc_export_chunk_wrap(int *ret, stream **s, int *res_id, str *w); */
    3008             : str
    3009           0 : mvc_export_chunk_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    3010             : {
    3011             :         backend *b = NULL;
    3012           0 :         stream **s = (stream **) getArgReference(stk, pci, 1);
    3013           0 :         int res_id = *getArgReference_int(stk, pci, 2), ok;
    3014             :         BUN offset = 0;
    3015             :         BUN nr = 0;
    3016             :         str msg;
    3017             : 
    3018             :         (void) mb;              /* NOT USED */
    3019           0 :         if (pci->argc == 5) {
    3020           0 :                 offset = (BUN) *getArgReference_int(stk, pci, 3);
    3021           0 :                 int cnt = *getArgReference_int(stk, pci, 4);
    3022           0 :                 nr = cnt < 0 ? BUN_NONE : (BUN) cnt;
    3023             :         }
    3024             : 
    3025           0 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    3026             :                 return msg;
    3027           0 :         b = cntxt->sqlcontext;
    3028           0 :         if ((ok = mvc_export_chunk(b, *s, res_id, offset, nr)) < 0)
    3029           0 :                 throw(SQL, "sql.exportChunk", SQLSTATE(45000) "Result set construction failed: %s", mvc_export_error(b, *s, ok));
    3030             :         return NULL;
    3031             : }
    3032             : 
    3033             : /* str mvc_export_operation_wrap(int *ret, str *w); */
    3034             : str
    3035       16530 : mvc_export_operation_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    3036             : {
    3037             :         backend *b = NULL;
    3038             :         str msg;
    3039             :         int ok;
    3040             : 
    3041             :         (void) stk;             /* NOT USED */
    3042             :         (void) pci;             /* NOT USED */
    3043       16530 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    3044             :                 return msg;
    3045       16530 :         b = cntxt->sqlcontext;
    3046       16530 :         ok = mvc_export_operation(b, b->out, "", mb->starttime, mb->optimize);
    3047       16530 :         mb->starttime = 0;
    3048       16530 :         mb->optimize = 0;
    3049       16530 :         if (ok < 0)
    3050           0 :                 throw(SQL, "sql.exportOperation", SQLSTATE(45000) "Result set construction failed: %s", mvc_export_error(b, b->out, ok));
    3051             :         return MAL_SUCCEED;
    3052             : }
    3053             : 
    3054             : str
    3055             : /*mvc_scalar_value_wrap(int *ret, int *qtype, str tn, str name, str type, int *digits, int *scale, int *eclass, ptr p, int mtype)*/
    3056       27954 : mvc_scalar_value_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    3057             : {
    3058       27954 :         const char *tn = *getArgReference_str(stk, pci, 1);
    3059       27954 :         const char *cn = *getArgReference_str(stk, pci, 2);
    3060       27954 :         const char *type = *getArgReference_str(stk, pci, 3);
    3061       27954 :         int digits = *getArgReference_int(stk, pci, 4);
    3062       27954 :         int scale = *getArgReference_int(stk, pci, 5);
    3063       27954 :         ptr p = getArgReference(stk, pci, 7);
    3064       27954 :         int mtype = getArgType(mb, pci, 7);
    3065             :         str msg;
    3066       27954 :         backend *be = NULL;
    3067             :         int res_id, ok;
    3068             :         (void) mb;              /* NOT USED */
    3069       27954 :         if ((msg = getBackendContext(cntxt, &be)) != NULL)
    3070             :                 return msg;
    3071       27954 :         if (ATOMextern(mtype))
    3072         533 :                 p = *(ptr *) p;
    3073             : 
    3074             :         // scalar values are single-column result sets
    3075       27954 :         if ((res_id = mvc_result_table(be, mb->tag, 1, Q_TABLE, NULL)) < 0) {
    3076           0 :                 mb->starttime = 0;
    3077           0 :                 mb->optimize = 0;
    3078           0 :                 throw(SQL, "sql.exportValue", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3079             :         }
    3080       27954 :         if ((ok = mvc_result_value(be, tn, cn, type, digits, scale, p, mtype)) < 0) {
    3081           0 :                 mb->starttime = 0;
    3082           0 :                 mb->optimize = 0;
    3083           0 :                 throw(SQL, "sql.exportValue", SQLSTATE(45000) "Result set construction failed: %s", mvc_export_error(be, be->out, ok));
    3084             :         }
    3085       27954 :         if (be->output_format == OFMT_NONE) {
    3086           1 :                 mb->starttime = 0;
    3087           1 :                 mb->optimize = 0;
    3088           1 :                 return MAL_SUCCEED;
    3089             :         }
    3090       27953 :         ok = mvc_export_result(be, be->out, res_id, true, mb->starttime, mb->optimize);
    3091       27953 :         mb->starttime = 0;
    3092       27953 :         mb->optimize = 0;
    3093       27953 :         if (ok < 0)
    3094           0 :                 throw(SQL, "sql.exportValue", SQLSTATE(45000) "Result set construction failed: %s", mvc_export_error(be, be->out, ok));
    3095             :         return MAL_SUCCEED;
    3096             : }
    3097             : 
    3098             : static void
    3099         950 : bat2return(MalStkPtr stk, InstrPtr pci, BAT **b)
    3100             : {
    3101             :         int i;
    3102             : 
    3103       10492 :         for (i = 0; i < pci->retc; i++) {
    3104        9542 :                 *getArgReference_bat(stk, pci, i) = b[i]->batCacheid;
    3105        9542 :                 BBPkeepref(b[i]->batCacheid);
    3106             :         }
    3107         950 : }
    3108             : 
    3109             : static char fwftsep[2] = {STREAM_FWF_FIELD_SEP, '\0'};
    3110             : static char fwfrsep[2] = {STREAM_FWF_RECORD_SEP, '\0'};
    3111             : 
    3112             : /* 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); */
    3113             : str
    3114         975 : mvc_import_table_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    3115             : {
    3116             :         backend *be;
    3117         975 :         BAT **b = NULL;
    3118             :         ssize_t len = 0;
    3119         975 :         sql_table *t = *(sql_table **) getArgReference(stk, pci, pci->retc + 0);
    3120         975 :         const char *tsep = *getArgReference_str(stk, pci, pci->retc + 1);
    3121         975 :         const char *rsep = *getArgReference_str(stk, pci, pci->retc + 2);
    3122         975 :         const char *ssep = *getArgReference_str(stk, pci, pci->retc + 3);
    3123         975 :         const char *ns = *getArgReference_str(stk, pci, pci->retc + 4);
    3124         975 :         const char *fname = *getArgReference_str(stk, pci, pci->retc + 5);
    3125         975 :         lng sz = *getArgReference_lng(stk, pci, pci->retc + 6);
    3126         975 :         lng offset = *getArgReference_lng(stk, pci, pci->retc + 7);
    3127         975 :         int besteffort = *getArgReference_int(stk, pci, pci->retc + 8);
    3128         975 :         char *fixed_widths = *getArgReference_str(stk, pci, pci->retc + 9);
    3129         975 :         int onclient = *getArgReference_int(stk, pci, pci->retc + 10);
    3130         975 :         bool escape = *getArgReference_int(stk, pci, pci->retc + 11);
    3131             :         str msg = MAL_SUCCEED;
    3132             :         bstream *s = NULL;
    3133             :         stream *ss;
    3134             : 
    3135             :         (void) mb;              /* NOT USED */
    3136         975 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    3137             :                 return msg;
    3138         975 :         if (onclient && !cntxt->filetrans)
    3139           0 :                 throw(MAL, "sql.copy_from", SQLSTATE(42000) "Cannot transfer files from client");
    3140             : 
    3141         975 :         be = cntxt->sqlcontext;
    3142             :         /* The CSV parser expects ssep to have the value 0 if the user does not
    3143             :          * specify a quotation character
    3144             :          */
    3145        1944 :         if (*ssep == 0 || strNil(ssep))
    3146             :                 ssep = NULL;
    3147             : 
    3148         975 :         if (strNil(fname))
    3149             :                 fname = NULL;
    3150         267 :         if (fname == NULL) {
    3151         708 :                 msg = mvc_import_table(cntxt, &b, be->mvc, be->mvc->scanner.rs, t, tsep, rsep, ssep, ns, sz, offset, besteffort, true, escape);
    3152             :         } else {
    3153         267 :                 if (onclient) {
    3154           0 :                         mnstr_write(be->mvc->scanner.ws, PROMPT3, sizeof(PROMPT3)-1, 1);
    3155           0 :                         if (offset > 1 && rsep && rsep[0] == '\n' && rsep[1] == '\0') {
    3156             :                                 /* only let client skip simple lines */
    3157           0 :                                 mnstr_printf(be->mvc->scanner.ws, "r " LLFMT " %s\n",
    3158             :                                              offset, fname);
    3159           0 :                                 offset = 0;
    3160             :                         } else {
    3161           0 :                                 mnstr_printf(be->mvc->scanner.ws, "r 0 %s\n", fname);
    3162             :                         }
    3163             :                         msg = MAL_SUCCEED;
    3164           0 :                         mnstr_flush(be->mvc->scanner.ws, MNSTR_FLUSH_DATA);
    3165           0 :                         while (!be->mvc->scanner.rs->eof)
    3166           0 :                                 bstream_next(be->mvc->scanner.rs);
    3167           0 :                         ss = be->mvc->scanner.rs->s;
    3168             :                         char buf[80];
    3169           0 :                         if ((len = mnstr_readline(ss, buf, sizeof(buf))) > 1) {
    3170           0 :                                 if (buf[0] == '!' && buf[6] == '!')
    3171           0 :                                         msg = createException(IO, "sql.copy_from", "%.7s%s: %s", buf, fname, buf+7);
    3172             :                                 else
    3173           0 :                                         msg = createException(IO, "sql.copy_from", "%s: %s", fname, buf);
    3174           0 :                                 while (buf[len - 1] != '\n' &&
    3175           0 :                                        (len = mnstr_readline(ss, buf, sizeof(buf))) > 0)
    3176             :                                         ;
    3177             :                                 /* read until flush marker */
    3178           0 :                                 while (mnstr_read(ss, buf, 1, sizeof(buf)) > 0)
    3179             :                                         ;
    3180           0 :                                 return msg;
    3181             :                         }
    3182             :                 } else {
    3183         267 :                         ss = open_rastream(fname);
    3184         267 :                         if (ss == NULL || mnstr_errnr(ss)) {
    3185           0 :                                 msg = createException(IO, "sql.copy_from", SQLSTATE(42000) "%s", mnstr_peek_error(NULL));
    3186           0 :                                 close_stream(ss);
    3187           0 :                                 return msg;
    3188             :                         }
    3189             :                 }
    3190             : 
    3191         267 :                 if (!strNil(fixed_widths)) {
    3192             :                         size_t ncol = 0, current_width_entry = 0, i;
    3193             :                         size_t *widths;
    3194             :                         char* val_start = fixed_widths;
    3195           1 :                         size_t width_len = strlen(fixed_widths);
    3196             :                         stream *ns;
    3197             : 
    3198          23 :                         for (i = 0; i < width_len; i++) {
    3199          22 :                                 if (fixed_widths[i] == '|') {
    3200          11 :                                         ncol++;
    3201             :                                 }
    3202             :                         }
    3203           1 :                         widths = malloc(sizeof(size_t) * ncol);
    3204           1 :                         if (!widths) {
    3205           0 :                                 close_stream(ss);
    3206           0 :                                 throw(MAL, "sql.copy_from", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3207             :                         }
    3208          23 :                         for (i = 0; i < width_len; i++) {
    3209          22 :                                 if (fixed_widths[i] == STREAM_FWF_FIELD_SEP) {
    3210          11 :                                         fixed_widths[i] = '\0';
    3211          11 :                                         widths[current_width_entry++] = (size_t) strtoll(val_start, NULL, 10);
    3212          11 :                                         val_start = fixed_widths + i + 1;
    3213             :                                 }
    3214             :                         }
    3215             :                         /* overwrite other delimiters to the ones the FWF stream uses */
    3216             :                         tsep = fwftsep;
    3217             :                         rsep = fwfrsep;
    3218             : 
    3219           1 :                         ns = stream_fwf_create(ss, ncol, widths, STREAM_FWF_FILLER);
    3220           1 :                         if (ns == NULL || mnstr_errnr(ns)) {
    3221           0 :                                 msg = createException(IO, "sql.copy_from", SQLSTATE(42000) "%s", mnstr_peek_error(NULL));
    3222           0 :                                 close_stream(ss);
    3223           0 :                                 free(widths);
    3224           0 :                                 return msg;
    3225             :                         }
    3226             :                         ss = ns;
    3227             :                 }
    3228             : #if SIZEOF_VOID_P == 4
    3229             :                 s = bstream_create(ss, 0x20000);
    3230             : #else
    3231         267 :                 s = bstream_create(ss, 0x200000);
    3232             : #endif
    3233         267 :                 if (s == NULL) {
    3234           0 :                         close_stream(ss);
    3235           0 :                         throw(MAL, "sql.copy_from", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3236             :                 }
    3237         267 :                 msg = mvc_import_table(cntxt, &b, be->mvc, s, t, tsep, rsep, ssep, ns, sz, offset, besteffort, false, escape);
    3238         267 :                 if (onclient) {
    3239           0 :                         mnstr_write(be->mvc->scanner.ws, PROMPT3, sizeof(PROMPT3)-1, 1);
    3240           0 :                         mnstr_flush(be->mvc->scanner.ws, MNSTR_FLUSH_DATA);
    3241           0 :                         be->mvc->scanner.rs->eof = s->eof;
    3242           0 :                         s->s = NULL;
    3243             :                 }
    3244         267 :                 bstream_destroy(s);
    3245             :         }
    3246         975 :         if (b && !msg)
    3247         950 :                 bat2return(stk, pci, b);
    3248         975 :         GDKfree(b);
    3249         975 :         return msg;
    3250             : }
    3251             : 
    3252             : str
    3253         233 : not_unique(bit *ret, const bat *bid)
    3254             : {
    3255             :         BAT *b;
    3256             : 
    3257         233 :         if ((b = BATdescriptor(*bid)) == NULL) {
    3258           0 :                 throw(SQL, "not_unique", SQLSTATE(HY005) "Cannot access column descriptor");
    3259             :         }
    3260             : 
    3261         233 :         *ret = FALSE;
    3262         233 :         if (BATtkey(b) || BATtdense(b) || BATcount(b) <= 1) {
    3263         232 :                 BBPunfix(b->batCacheid);
    3264         232 :                 return MAL_SUCCEED;
    3265           1 :         } else if (b->tsorted) {
    3266             :                 BUN p, q;
    3267           1 :                 BATiter bi = bat_iterator(b);
    3268           1 :                 oid c = ((oid *) bi.base)[0];
    3269             : 
    3270           2 :                 for (p = 1, q = BUNlast(b); p < q; p++) {
    3271           2 :                         oid v = ((oid *) bi.base)[p];
    3272           2 :                         if (v <= c) {
    3273           1 :                                 *ret = TRUE;
    3274           1 :                                 break;
    3275             :                         }
    3276             :                         c = v;
    3277             :                 }
    3278           1 :                 bat_iterator_end(&bi);
    3279             :         } else {
    3280           0 :                 BBPunfix(b->batCacheid);
    3281           0 :                 throw(SQL, "not_unique", SQLSTATE(42000) "Input column should be sorted");
    3282             :         }
    3283           1 :         BBPunfix(b->batCacheid);
    3284           1 :         return MAL_SUCCEED;
    3285             : }
    3286             : 
    3287             : /* row case */
    3288             : str
    3289          42 : SQLidentity(oid *ret, const void *i)
    3290             : {
    3291             :         (void)i;
    3292          42 :         *ret = 0;
    3293          42 :         return MAL_SUCCEED;
    3294             : }
    3295             : 
    3296             : str
    3297         332 : BATSQLidentity(bat *ret, const bat *bid)
    3298             : {
    3299         332 :         return BKCmirror(ret, bid);
    3300             : }
    3301             : 
    3302             : str
    3303        1066 : PBATSQLidentity(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    3304             : {
    3305        1066 :         bat *res = getArgReference_bat(stk, pci, 0);
    3306        1066 :         oid *ns = getArgReference_oid(stk, pci, 1);
    3307        1066 :         bat bid = *getArgReference_bat(stk, pci, 2);
    3308        1066 :         oid s = *getArgReference_oid(stk, pci, 3);
    3309             :         BAT *b, *bn = NULL;
    3310             : 
    3311             :         (void) cntxt;
    3312             :         (void) mb;
    3313        1066 :         if (!(b = BBPquickdesc(bid)))
    3314           0 :                 throw(MAL, "batcalc.identity", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
    3315        1066 :         if (!(bn = BATdense(b->hseqbase, s, BATcount(b))))
    3316           0 :                 throw(MAL, "batcalc.identity", GDK_EXCEPTION);
    3317        1066 :         *ns = s + BATcount(b);
    3318        1066 :         BBPkeepref(*res = bn->batCacheid);
    3319        1066 :         return MAL_SUCCEED;
    3320             : }
    3321             : 
    3322             : /*
    3323             :  * The core modules of Monet provide just a limited set of
    3324             :  * mathematical operators. The extensions required to support
    3325             :  * SQL-99 are shown below. At some point they also should be
    3326             :  * moved to module code base.
    3327             :  */
    3328             : 
    3329             : str
    3330          10 : SQLcst_alpha_cst(dbl *res, const dbl *decl, const dbl *theta)
    3331             : {
    3332             :         dbl s, c1, c2;
    3333             :         char *msg = MAL_SUCCEED;
    3334          10 :         if (is_dbl_nil(*decl) || is_dbl_nil(*theta)) {
    3335           0 :                 *res = dbl_nil;
    3336          10 :         } else if (fabs(*decl) + *theta > 89.9) {
    3337           0 :                 *res = 180.0;
    3338             :         } else {
    3339          10 :                 s = sin(radians(*theta));
    3340          10 :                 c1 = cos(radians(*decl - *theta));
    3341          10 :                 c2 = cos(radians(*decl + *theta));
    3342          10 :                 *res = degrees(fabs(atan(s / sqrt(fabs(c1 * c2)))));
    3343             :         }
    3344          10 :         return msg;
    3345             : }
    3346             : 
    3347             : /*
    3348             :   sql5_export str SQLcst_alpha_cst(dbl *res, dbl *decl, dbl *theta);
    3349             :   sql5_export str SQLbat_alpha_cst(bat *res, bat *decl, dbl *theta);
    3350             :   sql5_export str SQLcst_alpha_bat(bat *res, dbl *decl, bat *theta);
    3351             : */
    3352             : str
    3353           0 : SQLbat_alpha_cst(bat *res, const bat *decl, const dbl *theta)
    3354             : {
    3355             :         BAT *b, *bn;
    3356             :         BUN p, q;
    3357             :         dbl s, c1, c2, r;
    3358             :         char *msg = NULL;
    3359             : 
    3360           0 :         if (is_dbl_nil(*theta)) {
    3361           0 :                 throw(SQL, "SQLbat_alpha", SQLSTATE(42000) "Parameter theta should not be nil");
    3362             :         }
    3363           0 :         if ((b = BATdescriptor(*decl)) == NULL) {
    3364           0 :                 throw(SQL, "alpha", SQLSTATE(HY005) "Cannot access column descriptor");
    3365             :         }
    3366           0 :         bn = COLnew(b->hseqbase, TYPE_dbl, BATcount(b), TRANSIENT);
    3367           0 :         if (bn == NULL) {
    3368           0 :                 BBPunfix(b->batCacheid);
    3369           0 :                 throw(SQL, "sql.alpha", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3370             :         }
    3371           0 :         s = sin(radians(*theta));
    3372           0 :         BATiter bi = bat_iterator(b);
    3373           0 :         const dbl *vals = (const dbl *) bi.base;
    3374           0 :         BATloop(b, p, q) {
    3375           0 :                 dbl d = vals[p];
    3376           0 :                 if (is_dbl_nil(d))
    3377           0 :                         r = dbl_nil;
    3378           0 :                 else if (fabs(d) + *theta > 89.9)
    3379           0 :                         r = 180.0;
    3380             :                 else {
    3381           0 :                         c1 = cos(radians(d - *theta));
    3382           0 :                         c2 = cos(radians(d + *theta));
    3383           0 :                         r = degrees(fabs(atan(s / sqrt(fabs(c1 * c2)))));
    3384             :                 }
    3385           0 :                 if (BUNappend(bn, &r, false) != GDK_SUCCEED) {
    3386           0 :                         BBPreclaim(bn);
    3387           0 :                         bat_iterator_end(&bi);
    3388           0 :                         throw(SQL, "sql.alpha", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3389             :                 }
    3390             :         }
    3391           0 :         bat_iterator_end(&bi);
    3392           0 :         *res = bn->batCacheid;
    3393           0 :         BBPkeepref(bn->batCacheid);
    3394           0 :         BBPunfix(b->batCacheid);
    3395           0 :         return msg;
    3396             : }
    3397             : 
    3398             : str
    3399           0 : SQLcst_alpha_bat(bat *res, const dbl *decl, const bat *thetabid)
    3400             : {
    3401             :         BAT *b, *bn;
    3402             :         BUN p, q;
    3403             :         dbl s, c1, c2, r;
    3404             :         char *msg = NULL;
    3405             :         dbl *thetas;
    3406             : 
    3407           0 :         if ((b = BATdescriptor(*thetabid)) == NULL) {
    3408           0 :                 throw(SQL, "alpha", SQLSTATE(HY005) "Cannot access column descriptor");
    3409             :         }
    3410           0 :         bn = COLnew(b->hseqbase, TYPE_dbl, BATcount(b), TRANSIENT);
    3411           0 :         if (bn == NULL) {
    3412           0 :                 BBPunfix(b->batCacheid);
    3413           0 :                 throw(SQL, "sql.alpha", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3414             :         }
    3415           0 :         BATiter bi = bat_iterator(b);
    3416           0 :         thetas = (dbl *) bi.base;
    3417           0 :         BATloop(b, p, q) {
    3418           0 :                 dbl d = *decl;
    3419           0 :                 dbl theta = thetas[p];
    3420             : 
    3421           0 :                 if (is_dbl_nil(d))
    3422           0 :                         r = dbl_nil;
    3423           0 :                 else if (fabs(d) + theta > 89.9)
    3424           0 :                         r = (dbl) 180.0;
    3425             :                 else {
    3426           0 :                         s = sin(radians(theta));
    3427           0 :                         c1 = cos(radians(d - theta));
    3428           0 :                         c2 = cos(radians(d + theta));
    3429           0 :                         r = degrees(fabs(atan(s / sqrt(fabs(c1 * c2)))));
    3430             :                 }
    3431           0 :                 if (BUNappend(bn, &r, false) != GDK_SUCCEED) {
    3432           0 :                         BBPreclaim(bn);
    3433           0 :                         bat_iterator_end(&bi);
    3434           0 :                         throw(SQL, "sql.alpha", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3435             :                 }
    3436             :         }
    3437           0 :         bat_iterator_end(&bi);
    3438           0 :         BBPkeepref(*res = bn->batCacheid);
    3439           0 :         BBPunfix(b->batCacheid);
    3440           0 :         return msg;
    3441             : }
    3442             : 
    3443             : /* str dump_cache(int *r); */
    3444             : str
    3445           0 : dump_cache(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    3446             : {
    3447           0 :         mvc *m = NULL;
    3448             :         str msg;
    3449             :         int cnt;
    3450             :         cq *q = NULL;
    3451             :         BAT *query, *count;
    3452           0 :         bat *rquery = getArgReference_bat(stk, pci, 0);
    3453           0 :         bat *rcount = getArgReference_bat(stk, pci, 1);
    3454             : 
    3455           0 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    3456             :                 return msg;
    3457           0 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    3458             :                 return msg;
    3459           0 :         cnt = m->qc->id;
    3460           0 :         query = COLnew(0, TYPE_str, cnt, TRANSIENT);
    3461           0 :         if (query == NULL)
    3462           0 :                 throw(SQL, "sql.dumpcache", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3463           0 :         count = COLnew(0, TYPE_int, cnt, TRANSIENT);
    3464           0 :         if (count == NULL) {
    3465           0 :                 BBPunfix(query->batCacheid);
    3466           0 :                 throw(SQL, "sql.dumpcache", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3467             :         }
    3468             : 
    3469           0 :         for (q = m->qc->q; q; q = q->next) {
    3470           0 :                 if (BUNappend(query, q->f->query, false) != GDK_SUCCEED ||
    3471           0 :                     BUNappend(count, &q->count, false) != GDK_SUCCEED) {
    3472           0 :                         BBPunfix(query->batCacheid);
    3473           0 :                         BBPunfix(count->batCacheid);
    3474           0 :                         throw(SQL, "sql.dumpcache", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3475             :                 }
    3476             :         }
    3477           0 :         *rquery = query->batCacheid;
    3478           0 :         *rcount = count->batCacheid;
    3479           0 :         BBPkeepref(*rquery);
    3480           0 :         BBPkeepref(*rcount);
    3481           0 :         return MAL_SUCCEED;
    3482             : }
    3483             : 
    3484             : /* str dump_opt_stats(int *r); */
    3485             : str
    3486           0 : dump_opt_stats(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    3487             : {
    3488             :         backend *be;
    3489             :         str msg;
    3490             :         int cnt;
    3491             :         BAT *rewrite, *count;
    3492           0 :         bat *rrewrite = getArgReference_bat(stk, pci, 0);
    3493           0 :         bat *rcount = getArgReference_bat(stk, pci, 1);
    3494             : 
    3495             :         (void)mb;
    3496           0 :         if ((msg = getBackendContext(cntxt, &be)) != NULL)
    3497             :                 return msg;
    3498           0 :         cnt = be->mvc->qc->id;
    3499           0 :         rewrite = COLnew(0, TYPE_str, cnt, TRANSIENT);
    3500           0 :         count = COLnew(0, TYPE_int, cnt, TRANSIENT);
    3501           0 :         if (rewrite == NULL || count == NULL) {
    3502           0 :                 BBPreclaim(rewrite);
    3503           0 :                 BBPreclaim(count);
    3504           0 :                 throw(SQL, "sql.optstats", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3505             :         }
    3506             : 
    3507           0 :         if (BUNappend(rewrite, "joinidx", false) != GDK_SUCCEED ||
    3508           0 :             BUNappend(count, &be->join_idx, false) != GDK_SUCCEED) {
    3509           0 :                 BBPreclaim(rewrite);
    3510           0 :                 BBPreclaim(count);
    3511           0 :                 throw(SQL, "sql.optstats", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3512             :         }
    3513             :         /* TODO add other rewrites */
    3514             : 
    3515           0 :         *rrewrite = rewrite->batCacheid;
    3516           0 :         *rcount = count->batCacheid;
    3517           0 :         BBPkeepref(*rrewrite);
    3518           0 :         BBPkeepref(*rcount);
    3519           0 :         return MAL_SUCCEED;
    3520             : }
    3521             : 
    3522             : /* str dump_opt_stats(int *r); */
    3523             : str
    3524          63 : dump_trace(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    3525             : {
    3526             :         int i;
    3527             :         BAT *t[3];
    3528             :         bat id;
    3529             : 
    3530             :         (void) cntxt;
    3531             :         (void) mb;
    3532          63 :         if (TRACEtable(cntxt, t) != 3)
    3533           0 :                 throw(SQL, "sql.dump_trace", SQLSTATE(3F000) "Profiler not started");
    3534         252 :         for(i=0; i < 3; i++)
    3535         189 :         if( t[i]){
    3536         189 :                 id = t[i]->batCacheid;
    3537         189 :                 *getArgReference_bat(stk, pci, i) = id;
    3538         189 :                 BBPkeepref(id);
    3539             :         } else
    3540           0 :                 throw(SQL,"dump_trace", SQLSTATE(45000) "Missing trace BAT ");
    3541             :         return MAL_SUCCEED;
    3542             : }
    3543             : 
    3544             : static str
    3545          42 : sql_sessions_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    3546             : {
    3547          42 :         return CLTsessions(cntxt, mb, stk, pci);
    3548             : }
    3549             : 
    3550             : str
    3551          30 : sql_rt_credentials_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    3552             : {
    3553             :         BAT *urib = NULL;
    3554             :         BAT *unameb = NULL;
    3555             :         BAT *hashb = NULL;
    3556          30 :         bat *uri = getArgReference_bat(stk, pci, 0);
    3557          30 :         bat *uname = getArgReference_bat(stk, pci, 1);
    3558          30 :         bat *hash = getArgReference_bat(stk, pci, 2);
    3559          30 :         str *table = getArgReference_str(stk, pci, 3);
    3560          30 :         str uris = NULL;
    3561          30 :         str unames = NULL;
    3562          30 :         str hashs = NULL;
    3563             :         str msg = MAL_SUCCEED;
    3564             :         (void)mb;
    3565             :         (void)cntxt;
    3566             : 
    3567          30 :         urib = COLnew(0, TYPE_str, 0, TRANSIENT);
    3568          30 :         unameb = COLnew(0, TYPE_str, 0, TRANSIENT);
    3569          30 :         hashb = COLnew(0, TYPE_str, 0, TRANSIENT);
    3570             : 
    3571          30 :         if (urib == NULL || unameb == NULL || hashb == NULL) {
    3572           0 :                 msg = createException(SQL, "sql.remote_table_credentials", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3573           0 :                 goto bailout;
    3574             :         }
    3575             : 
    3576          30 :         if ((msg = AUTHgetRemoteTableCredentials(*table, &uris, &unames, &hashs)) != MAL_SUCCEED)
    3577           0 :                 goto bailout;
    3578             : 
    3579          30 :         MT_lock_set(&mal_contextLock);
    3580          31 :         if (BUNappend(urib, uris? uris: str_nil, false) != GDK_SUCCEED)
    3581           0 :                 goto lbailout;
    3582          31 :         if (BUNappend(unameb, unames? unames: str_nil , false) != GDK_SUCCEED)
    3583           0 :                 goto lbailout;
    3584          31 :         if (BUNappend(hashb, hashs? hashs: str_nil, false) != GDK_SUCCEED)
    3585           0 :                 goto lbailout;
    3586          30 :         MT_lock_unset(&mal_contextLock);
    3587          30 :         BBPkeepref(*uri = urib->batCacheid);
    3588          30 :         BBPkeepref(*uname = unameb->batCacheid);
    3589          30 :         BBPkeepref(*hash = hashb->batCacheid);
    3590             : 
    3591          30 :         if (hashs) GDKfree(hashs);
    3592             :         return MAL_SUCCEED;
    3593             : 
    3594           0 :   lbailout:
    3595           0 :         MT_lock_unset(&mal_contextLock);
    3596           0 :         msg = createException(SQL, "sql.remote_table_credentials", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3597           0 :   bailout:
    3598           0 :         if (hashs) GDKfree(hashs);
    3599           0 :         if (urib) BBPunfix(urib->batCacheid);
    3600           0 :         if (unameb) BBPunfix(unameb->batCacheid);
    3601           0 :         if (hashb) BBPunfix(hashb->batCacheid);
    3602             :         return msg;
    3603             : }
    3604             : 
    3605             : str
    3606         118 : sql_querylog_catalog(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    3607             : {
    3608             :         int i;
    3609             :         BAT *t[8];
    3610             :         str msg;
    3611             : 
    3612             :         (void) cntxt;
    3613             :         (void) mb;
    3614         118 :         msg = QLOGcatalog(t);
    3615         118 :         if( msg != MAL_SUCCEED)
    3616             :                 return msg;
    3617        1062 :         for (i = 0; i < 8; i++)
    3618         944 :         if( t[i]){
    3619         944 :                 bat id = t[i]->batCacheid;
    3620             : 
    3621         944 :                 *getArgReference_bat(stk, pci, i) = id;
    3622         944 :                 BBPkeepref(id);
    3623             :         } else
    3624           0 :                 throw(SQL,"sql.querylog", SQLSTATE(45000) "Missing query catalog BAT");
    3625             :         return MAL_SUCCEED;
    3626             : }
    3627             : 
    3628             : str
    3629          87 : sql_querylog_calls(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    3630             : {
    3631             :         int i;
    3632             :         BAT *t[10];
    3633             :         str msg;
    3634             : 
    3635             :         (void) cntxt;
    3636             :         (void) mb;
    3637          87 :         msg = QLOGcalls(t);
    3638          87 :         if( msg != MAL_SUCCEED)
    3639             :                 return msg;
    3640         870 :         for (i = 0; i < 9; i++)
    3641         783 :         if( t[i]){
    3642         783 :                 bat id = t[i]->batCacheid;
    3643             : 
    3644         783 :                 *getArgReference_bat(stk, pci, i) = id;
    3645         783 :                 BBPkeepref(id);
    3646             :         } else
    3647           0 :                 throw(SQL,"sql.querylog", SQLSTATE(45000) "Missing query call BAT");
    3648             :         return MAL_SUCCEED;
    3649             : }
    3650             : 
    3651             : str
    3652           1 : sql_querylog_empty(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    3653             : {
    3654             :         (void) cntxt;
    3655             :         (void) mb;
    3656             :         (void) stk;
    3657             :         (void) pci;
    3658           1 :         return QLOGempty(NULL);
    3659             : }
    3660             : 
    3661             : /* str sql_rowid(oid *rid, ptr v, str *sname, str *tname); */
    3662             : str
    3663           0 : sql_rowid(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    3664             : {
    3665             :         BAT *b;
    3666           0 :         mvc *m = NULL;
    3667             :         str msg;
    3668             :         sql_schema *s = NULL;
    3669             :         sql_table *t = NULL;
    3670             :         sql_column *c = NULL;
    3671           0 :         oid *rid = getArgReference_oid(stk, pci, 0);
    3672           0 :         const char *sname = *getArgReference_str(stk, pci, 2);
    3673           0 :         const char *tname = *getArgReference_str(stk, pci, 3);
    3674             : 
    3675           0 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    3676             :                 return msg;
    3677           0 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    3678             :                 return msg;
    3679           0 :         s = mvc_bind_schema(m, sname);
    3680           0 :         if (s == NULL)
    3681           0 :                 throw(SQL, "calc.rowid", SQLSTATE(3F000) "Schema missing %s", sname);
    3682           0 :         t = mvc_bind_table(m, s, tname);
    3683           0 :         if (t == NULL)
    3684           0 :                 throw(SQL, "calc.rowid", SQLSTATE(42S02) "Table missing %s.%s",sname,tname);
    3685           0 :         if (!isTable(t))
    3686           0 :                 throw(SQL, "calc.rowid", SQLSTATE(42000) "%s '%s' is not persistent",
    3687           0 :                           TABLE_TYPE_DESCRIPTION(t->type, t->properties), t->base.name);
    3688           0 :         if (!ol_first_node(t->columns))
    3689           0 :                 throw(SQL, "calc.rowid", SQLSTATE(42S22) "Column missing %s.%s",sname,tname);
    3690           0 :         c = ol_first_node(t->columns)->data;
    3691             :         /* HACK, get insert bat */
    3692           0 :         sqlstore *store = m->session->tr->store;
    3693           0 :         b = store->storage_api.bind_col(m->session->tr, c, QUICK);
    3694           0 :         if( b == NULL)
    3695           0 :                 throw(SQL,"calc.rowid", SQLSTATE(HY005) "Cannot access column descriptor");
    3696             :         /* UGH (move into storage backends!!) */
    3697           0 :         *rid = BATcount(b);
    3698           0 :         return MAL_SUCCEED;
    3699             : }
    3700             : 
    3701             : static str
    3702           0 : do_sql_rank_grp(bat *rid, const bat *bid, const bat *gid, int nrank, int dense, const char *name)
    3703             : {
    3704             :         BAT *r, *b, *g;
    3705             :         BUN p, q;
    3706             :         BATiter bi, gi;
    3707             :         int (*ocmp) (const void *, const void *);
    3708             :         int (*gcmp) (const void *, const void *);
    3709             :         const void *oc, *gc, *on, *gn;
    3710           0 :         int rank = 1;
    3711             :         int c;
    3712             : 
    3713           0 :         if ((b = BATdescriptor(*bid)) == NULL)
    3714           0 :                 throw(SQL, name, SQLSTATE(HY005) "Cannot access column descriptor");
    3715           0 :         if ((g = BATdescriptor(*gid)) == NULL) {
    3716           0 :                 BBPunfix(b->batCacheid);
    3717           0 :                 throw(SQL, name, SQLSTATE(HY005) "Cannot access column descriptor");
    3718             :         }
    3719           0 :         bi = bat_iterator(b);
    3720           0 :         gi = bat_iterator(g);
    3721           0 :         ocmp = ATOMcompare(b->ttype);
    3722           0 :         gcmp = ATOMcompare(g->ttype);
    3723           0 :         oc = BUNtail(bi, 0);
    3724           0 :         gc = BUNtail(gi, 0);
    3725           0 :         if (!ALIGNsynced(b, g)) {
    3726           0 :                 bat_iterator_end(&bi);
    3727           0 :                 bat_iterator_end(&gi);
    3728           0 :                 BBPunfix(b->batCacheid);
    3729           0 :                 BBPunfix(g->batCacheid);
    3730           0 :                 throw(SQL, name, SQLSTATE(45000) "Internal error, columns not aligned");
    3731             :         }
    3732             : /*
    3733             :   if (!BATtordered(b)) {
    3734             :   BBPunfix(b->batCacheid);
    3735             :   BBPunfix(g->batCacheid);
    3736             :   throw(SQL, name, SQLSTATE(45000) "Internal error, columns not sorted");
    3737             :   }
    3738             : */
    3739           0 :         r = COLnew(b->hseqbase, TYPE_int, BATcount(b), TRANSIENT);
    3740           0 :         if (r == NULL) {
    3741           0 :                 bat_iterator_end(&bi);
    3742           0 :                 bat_iterator_end(&gi);
    3743           0 :                 BBPunfix(b->batCacheid);
    3744           0 :                 BBPunfix(g->batCacheid);
    3745           0 :                 throw(SQL, name, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3746             :         }
    3747           0 :         BATloop(b, p, q) {
    3748           0 :                 on = BUNtail(bi, p);
    3749           0 :                 gn = BUNtail(gi, p);
    3750             : 
    3751           0 :                 if ((c = ocmp(on, oc)) != 0)
    3752           0 :                         rank = nrank;
    3753           0 :                 if (gcmp(gn, gc) != 0)
    3754           0 :                         c = rank = nrank = 1;
    3755             :                 oc = on;
    3756             :                 gc = gn;
    3757           0 :                 if (BUNappend(r, &rank, false) != GDK_SUCCEED) {
    3758           0 :                         bat_iterator_end(&bi);
    3759           0 :                         bat_iterator_end(&gi);
    3760           0 :                         BBPunfix(b->batCacheid);
    3761           0 :                         BBPunfix(g->batCacheid);
    3762           0 :                         BBPunfix(r->batCacheid);
    3763           0 :                         throw(SQL, name, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3764             :                 }
    3765           0 :                 nrank += !dense || c;
    3766             :         }
    3767           0 :         bat_iterator_end(&bi);
    3768           0 :         bat_iterator_end(&gi);
    3769           0 :         BBPunfix(b->batCacheid);
    3770           0 :         BBPunfix(g->batCacheid);
    3771           0 :         BBPkeepref(*rid = r->batCacheid);
    3772           0 :         return MAL_SUCCEED;
    3773             : }
    3774             : 
    3775             : static str
    3776           0 : do_sql_rank(bat *rid, const bat *bid, int nrank, int dense, const char *name)
    3777             : {
    3778             :         BAT *r, *b;
    3779             :         BATiter bi;
    3780             :         int (*cmp) (const void *, const void *);
    3781             :         const void *cur, *n;
    3782             :         BUN p, q;
    3783           0 :         int rank = 1;
    3784             :         int c;
    3785             : 
    3786           0 :         if ((b = BATdescriptor(*bid)) == NULL)
    3787           0 :                 throw(SQL, name, SQLSTATE(HY005) "Cannot access column descriptor");
    3788           0 :         if (!BATtordered(b) && !BATtrevordered(b)) {
    3789           0 :                 BBPunfix(b->batCacheid);
    3790           0 :                 throw(SQL, name, SQLSTATE(45000) "Internal error, columns not sorted");
    3791             :         }
    3792             : 
    3793           0 :         bi = bat_iterator(b);
    3794           0 :         cmp = ATOMcompare(b->ttype);
    3795           0 :         cur = BUNtail(bi, 0);
    3796           0 :         r = COLnew(b->hseqbase, TYPE_int, BATcount(b), TRANSIENT);
    3797           0 :         if (r == NULL) {
    3798           0 :                 bat_iterator_end(&bi);
    3799           0 :                 BBPunfix(b->batCacheid);
    3800           0 :                 throw(SQL, name, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3801             :         }
    3802           0 :         if (BATtdense(b)) {
    3803           0 :                 BATloop(b, p, q) {
    3804           0 :                         if (BUNappend(r, &rank, false) != GDK_SUCCEED)
    3805           0 :                                 goto bailout;
    3806           0 :                         rank++;
    3807             :                 }
    3808             :         } else {
    3809           0 :                 BATloop(b, p, q) {
    3810           0 :                         n = BUNtail(bi, p);
    3811           0 :                         if ((c = cmp(n, cur)) != 0)
    3812           0 :                                 rank = nrank;
    3813             :                         cur = n;
    3814           0 :                         if (BUNappend(r, &rank, false) != GDK_SUCCEED)
    3815           0 :                                 goto bailout;
    3816           0 :                         nrank += !dense || c;
    3817             :                 }
    3818             :         }
    3819           0 :         bat_iterator_end(&bi);
    3820           0 :         BBPunfix(b->batCacheid);
    3821           0 :         BBPkeepref(*rid = r->batCacheid);
    3822           0 :         return MAL_SUCCEED;
    3823           0 :   bailout:
    3824           0 :         bat_iterator_end(&bi);
    3825           0 :         BBPunfix(b->batCacheid);
    3826           0 :         BBPunfix(r->batCacheid);
    3827           0 :         throw(SQL, name, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3828             : }
    3829             : 
    3830             : str
    3831           0 : sql_rank_grp(bat *rid, const bat *bid, const bat *gid, const bat *gpe)
    3832             : {
    3833             :         (void) gpe;
    3834           0 :         return do_sql_rank_grp(rid, bid, gid, 1, 0, "sql.rank_grp");
    3835             : }
    3836             : 
    3837             : str
    3838           0 : sql_dense_rank_grp(bat *rid, const bat *bid, const bat *gid, const bat *gpe)
    3839             : {
    3840             :         (void) gpe;
    3841           0 :         return do_sql_rank_grp(rid, bid, gid, 2, 1, "sql.dense_rank_grp");
    3842             : }
    3843             : 
    3844             : str
    3845           0 : sql_rank(bat *rid, const bat *bid)
    3846             : {
    3847           0 :         return do_sql_rank(rid, bid, 1, 0, "sql.rank");
    3848             : }
    3849             : 
    3850             : str
    3851           0 : sql_dense_rank(bat *rid, const bat *bid)
    3852             : {
    3853           0 :         return do_sql_rank(rid, bid, 2, 1, "sql.dense_rank");
    3854             : }
    3855             : 
    3856             : str
    3857           5 : SQLargRecord(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    3858             : {
    3859             :         str s, t, *ret;
    3860             : 
    3861             :         (void) cntxt;
    3862           5 :         ret = getArgReference_str(stk, pci, 0);
    3863           5 :         s = instruction2str(mb, stk, getInstrPtr(mb, 0), LIST_MAL_CALL);
    3864           5 :         if(s == NULL)
    3865           0 :                 throw(SQL, "sql.argRecord", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3866           5 :         t = strchr(s, ' ');
    3867           5 :         if( ! t)
    3868           0 :                 t = strchr(s, '\t');
    3869           5 :         *ret = GDKstrdup(t ? t + 1 : s);
    3870           5 :         GDKfree(s);
    3871           5 :         if(*ret == NULL)
    3872           0 :                 throw(SQL, "sql.argRecord", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3873             :         return MAL_SUCCEED;
    3874             : }
    3875             : 
    3876             : /*
    3877             :  * The drop_hash operation cleans up any hash indices on any of the tables columns.
    3878             :  */
    3879             : str
    3880           0 : SQLdrop_hash(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    3881             : {
    3882           0 :         const char *sch = *getArgReference_str(stk, pci, 1);
    3883           0 :         const char *tbl = *getArgReference_str(stk, pci, 2);
    3884             :         sql_schema *s;
    3885             :         sql_table *t;
    3886             :         sql_column *c;
    3887           0 :         mvc *m = NULL;
    3888             :         str msg;
    3889             :         BAT *b;
    3890             :         node *o;
    3891             : 
    3892           0 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    3893             :                 return msg;
    3894           0 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    3895             :                 return msg;
    3896           0 :         s = mvc_bind_schema(m, sch);
    3897           0 :         if (s == NULL)
    3898           0 :                 throw(SQL, "sql.drop_hash", SQLSTATE(3F000) "Schema missing %s",sch);
    3899           0 :         if (!mvc_schema_privs(m, s))
    3900           0 :                 throw(SQL, "sql.drop_hash", SQLSTATE(42000) "Access denied for %s to schema '%s'", get_string_global_var(m, "current_user"), s->base.name);
    3901           0 :         t = mvc_bind_table(m, s, tbl);
    3902           0 :         if (t == NULL)
    3903           0 :                 throw(SQL, "sql.drop_hash", SQLSTATE(42S02) "Table missing %s.%s",sch, tbl);
    3904           0 :         if (!isTable(t))
    3905           0 :                 throw(SQL, "sql.drop_hash", SQLSTATE(42000) "%s '%s' is not persistent",
    3906           0 :                           TABLE_TYPE_DESCRIPTION(t->type, t->properties), t->base.name);
    3907             : 
    3908           0 :         sqlstore *store = m->session->tr->store;
    3909           0 :         for (o = ol_first_node(t->columns); o; o = o->next) {
    3910           0 :                 c = o->data;
    3911           0 :                 b = store->storage_api.bind_col(m->session->tr, c, RDONLY);
    3912           0 :                 if (b == NULL)
    3913           0 :                         throw(SQL, "sql.drop_hash", SQLSTATE(HY005) "Cannot access column descriptor");
    3914           0 :                 HASHdestroy(b);
    3915           0 :                 BBPunfix(b->batCacheid);
    3916             :         }
    3917             :         return MAL_SUCCEED;
    3918             : }
    3919             : 
    3920             : /* after an update on the optimizer catalog, we have to change
    3921             :  * the internal optimizer pipe line administration
    3922             :  * The minimal and default pipelines may not be changed.
    3923             :  */
    3924             : str
    3925           0 : SQLoptimizersUpdate(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    3926             : {
    3927           0 :         mvc *m = NULL;
    3928             :         str msg;
    3929             : 
    3930           0 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    3931             :                 return msg;
    3932           0 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    3933             :                 return msg;
    3934             :         /* find the optimizer pipeline */
    3935             :         (void) stk;
    3936             :         (void) pci;
    3937           0 :         throw(SQL, "updateOptimizer", SQLSTATE(0A000) PROGRAM_NYI);
    3938             : }
    3939             : 
    3940             : /*
    3941             :  * Inspection of the actual storage footprint is a recurring question of users.
    3942             :  * This is modelled as a generic SQL table producing function.
    3943             :  * create function storage()
    3944             :  * returns table ("schema" string, "table" string, "column" string, "type" string, "mode" string, location string, "count" bigint, width int, columnsize bigint, heapsize bigint indices bigint, sorted int)
    3945             :  * external name sql.storage;
    3946             :  */
    3947             : str
    3948         428 : sql_storage(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    3949             : {
    3950             :         BAT *sch, *tab, *col, *type, *loc, *cnt, *atom, *size, *heap, *indices, *phash, *sort, *imprints, *mode, *revsort, *key, *oidx, *bs = NULL;
    3951         428 :         mvc *m = NULL;
    3952             :         str msg = MAL_SUCCEED;
    3953             :         sql_trans *tr;
    3954             :         node *ncol;
    3955             :         int w;
    3956             :         bit bitval;
    3957         428 :         bat *rsch = getArgReference_bat(stk, pci, 0);
    3958         428 :         bat *rtab = getArgReference_bat(stk, pci, 1);
    3959         428 :         bat *rcol = getArgReference_bat(stk, pci, 2);
    3960         428 :         bat *rtype = getArgReference_bat(stk, pci, 3);
    3961         428 :         bat *rmode = getArgReference_bat(stk, pci, 4);
    3962         428 :         bat *rloc = getArgReference_bat(stk, pci, 5);
    3963         428 :         bat *rcnt = getArgReference_bat(stk, pci, 6);
    3964         428 :         bat *ratom = getArgReference_bat(stk, pci, 7);
    3965         428 :         bat *rsize = getArgReference_bat(stk, pci, 8);
    3966         428 :         bat *rheap = getArgReference_bat(stk, pci, 9);
    3967         428 :         bat *rindices = getArgReference_bat(stk, pci, 10);
    3968         428 :         bat *rphash = getArgReference_bat(stk, pci, 11);
    3969         428 :         bat *rimprints = getArgReference_bat(stk, pci, 12);
    3970         428 :         bat *rsort = getArgReference_bat(stk, pci, 13);
    3971         428 :         bat *rrevsort = getArgReference_bat(stk, pci, 14);
    3972         428 :         bat *rkey = getArgReference_bat(stk, pci, 15);
    3973         428 :         bat *roidx = getArgReference_bat(stk, pci, 16);
    3974             :         str sname = 0;
    3975             :         str tname = 0;
    3976             :         str cname = 0;
    3977         428 :         struct os_iter si = {0};
    3978             : 
    3979         428 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    3980             :                 return msg;
    3981         428 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    3982             :                 return msg;
    3983             : 
    3984         428 :         if( pci->argc - pci->retc >= 1) {
    3985          37 :                 sname = *getArgReference_str(stk, pci, pci->retc);
    3986          37 :                 if (strNil(sname))
    3987           0 :                         throw(SQL, "sql.storage", SQLSTATE(42000) "Schema name cannot be NULL");
    3988             :         }
    3989         428 :         if( pci->argc - pci->retc >= 2) {
    3990          37 :                 tname = *getArgReference_str(stk, pci, pci->retc + 1);
    3991          37 :                 if (strNil(tname))
    3992           0 :                         throw(SQL, "sql.storage", SQLSTATE(42000) "Table name cannot be NULL");
    3993             :         }
    3994         428 :         if( pci->argc - pci->retc >= 3) {
    3995          27 :                 cname = *getArgReference_str(stk, pci, pci->retc + 2);
    3996          27 :                 if (strNil(cname))
    3997           0 :                         throw(SQL, "sql.storage", SQLSTATE(42000) "Column name cannot be NULL");
    3998             :         }
    3999             : 
    4000         428 :         tr = m->session->tr;
    4001         428 :         sqlstore *store = tr->store;
    4002         428 :         sch = COLnew(0, TYPE_str, 0, TRANSIENT);
    4003         428 :         tab = COLnew(0, TYPE_str, 0, TRANSIENT);
    4004         428 :         col = COLnew(0, TYPE_str, 0, TRANSIENT);
    4005         428 :         type = COLnew(0, TYPE_str, 0, TRANSIENT);
    4006         428 :         mode = COLnew(0, TYPE_str, 0, TRANSIENT);
    4007         428 :         loc = COLnew(0, TYPE_str, 0, TRANSIENT);
    4008         428 :         cnt = COLnew(0, TYPE_lng, 0, TRANSIENT);
    4009         428 :         atom = COLnew(0, TYPE_int, 0, TRANSIENT);
    4010         428 :         size = COLnew(0, TYPE_lng, 0, TRANSIENT);
    4011         428 :         heap = COLnew(0, TYPE_lng, 0, TRANSIENT);
    4012         428 :         indices = COLnew(0, TYPE_lng, 0, TRANSIENT);
    4013         428 :         phash = COLnew(0, TYPE_bit, 0, TRANSIENT);
    4014         428 :         imprints = COLnew(0, TYPE_lng, 0, TRANSIENT);
    4015         428 :         sort = COLnew(0, TYPE_bit, 0, TRANSIENT);
    4016         428 :         revsort = COLnew(0, TYPE_bit, 0, TRANSIENT);
    4017         428 :         key = COLnew(0, TYPE_bit, 0, TRANSIENT);
    4018         428 :         oidx = COLnew(0, TYPE_lng, 0, TRANSIENT);
    4019             : 
    4020         428 :         if (sch == NULL || tab == NULL || col == NULL || type == NULL || mode == NULL || loc == NULL || imprints == NULL ||
    4021         428 :             sort == NULL || cnt == NULL || atom == NULL || size == NULL || heap == NULL || indices == NULL || phash == NULL ||
    4022         428 :             revsort == NULL || key == NULL || oidx == NULL) {
    4023           0 :                 msg = createException(SQL, "sql.storage", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    4024           0 :                 goto bailout;
    4025             :         }
    4026             : 
    4027             :         /* check for limited storage tables */
    4028         428 :         os_iterator(&si, tr->cat->schemas, tr, NULL);
    4029        3902 :         for (sql_base *b = oi_next(&si); b; b = oi_next(&si)) {
    4030             :                 sql_schema *s = (sql_schema *) b;
    4031        3474 :                 if ((sname && strcmp(b->name, sname) ) || b->name[0] == '%')
    4032         699 :                         continue;
    4033        2775 :                 if (s->tables) {
    4034             :                         struct os_iter oi;
    4035             : 
    4036        2775 :                         os_iterator(&oi, s->tables, tr, NULL);
    4037       57204 :                         for (sql_base *bt = oi_next(&oi); bt; bt = oi_next(&oi)) {
    4038             :                                 sql_table *t = (sql_table *) bt;
    4039       54429 :                                 if( tname && strcmp(bt->name, tname) )
    4040        4333 :                                         continue;
    4041       50096 :                                 if (isTable(t)) {
    4042       16860 :                                         if (ol_first_node(t->columns)) {
    4043      106266 :                                                 for (ncol = ol_first_node((t)->columns); ncol; ncol = ncol->next) {
    4044       89406 :                                                         sql_base *bc = ncol->data;
    4045             :                                                         sql_column *c = (sql_column *) ncol->data;
    4046             :                                                         lng sz;
    4047             : 
    4048       89406 :                                                         if( cname && strcmp(bc->name, cname) )
    4049          44 :                                                                 continue;
    4050       89362 :                                                         bs = store->storage_api.bind_col(tr, c, QUICK);
    4051       89362 :                                                         if (bs == NULL) {
    4052           0 :                                                                 msg = createException(SQL, "sql.storage", SQLSTATE(HY005) "Cannot access column descriptor");
    4053           0 :                                                                 goto bailout;
    4054             :                                                         }
    4055             : 
    4056             :                                                         /*printf("schema %s.%s.%s" , b->name, bt->name, bc->name); */
    4057      178724 :                                                         if (BUNappend(sch, b->name, false) != GDK_SUCCEED ||
    4058      178724 :                                                             BUNappend(tab, bt->name, false) != GDK_SUCCEED ||
    4059       89362 :                                                             BUNappend(col, bc->name, false) != GDK_SUCCEED)
    4060           0 :                                                                 goto bailout;
    4061       89362 :                                                         if (c->t->access == TABLE_WRITABLE) {
    4062       82711 :                                                                 if (BUNappend(mode, "writable", false) != GDK_SUCCEED)
    4063           0 :                                                                         goto bailout;
    4064        6651 :                                                         } else if (c->t->access == TABLE_APPENDONLY) {
    4065           0 :                                                                 if (BUNappend(mode, "appendonly", false) != GDK_SUCCEED)
    4066           0 :                                                                         goto bailout;
    4067        6651 :                                                         } else if (c->t->access == TABLE_READONLY) {
    4068        6651 :                                                                 if (BUNappend(mode, "readonly", false) != GDK_SUCCEED)
    4069           0 :                                                                         goto bailout;
    4070             :                                                         } else {
    4071           0 :                                                                 if (BUNappend(mode, str_nil, false) != GDK_SUCCEED)
    4072           0 :                                                                         goto bailout;
    4073             :                                                         }
    4074       89362 :                                                         if (BUNappend(type, c->type.type->base.name, false) != GDK_SUCCEED)
    4075           0 :                                                                 goto bailout;
    4076             : 
    4077             :                                                         /*printf(" cnt "BUNFMT, BATcount(bs)); */
    4078       89362 :                                                         sz = BATcount(bs);
    4079       89362 :                                                         if (BUNappend(cnt, &sz, false) != GDK_SUCCEED)
    4080           0 :                                                                 goto bailout;
    4081             : 
    4082             :                                                         /*printf(" loc %s", BBP_physical(bs->batCacheid)); */
    4083       89362 :                                                         if (BUNappend(loc, BBP_physical(bs->batCacheid), false) != GDK_SUCCEED)
    4084           0 :                                                                 goto bailout;
    4085             :                                                         /*printf(" width %d", bs->twidth); */
    4086       89362 :                                                         w = bs->twidth;
    4087       89362 :                                                         if (BUNappend(atom, &w, false) != GDK_SUCCEED)
    4088           0 :                                                                 goto bailout;
    4089             : 
    4090       89362 :                                                         sz = BATcount(bs) << bs->tshift;
    4091       89362 :                                                         if (BUNappend(size, &sz, false) != GDK_SUCCEED)
    4092           0 :                                                                 goto bailout;
    4093             : 
    4094       89362 :                                                         sz = heapinfo(bs->tvheap, bs->batCacheid);
    4095       89362 :                                                         if (BUNappend(heap, &sz, false) != GDK_SUCCEED)
    4096           0 :                                                                 goto bailout;
    4097             : 
    4098       89362 :                                                         MT_rwlock_rdlock(&bs->thashlock);
    4099       89362 :                                                         sz = hashinfo(bs->thash, bs->batCacheid);
    4100       89362 :                                                         MT_rwlock_rdunlock(&bs->thashlock);
    4101       89362 :                                                         if (BUNappend(indices, &sz, false) != GDK_SUCCEED)
    4102           0 :                                                                 goto bailout;
    4103             : 
    4104       89362 :                                                         bitval = 0; /* HASHispersistent(bs); */
    4105       89362 :                                                         if (BUNappend(phash, &bitval, false) != GDK_SUCCEED)
    4106           0 :                                                                 goto bailout;
    4107             : 
    4108       89362 :                                                         sz = IMPSimprintsize(bs);
    4109       89362 :                                                         if (BUNappend(imprints, &sz, false) != GDK_SUCCEED)
    4110           0 :                                                                 goto bailout;
    4111             :                                                         /*printf(" indices "BUNFMT, bs->thash?bs->thash->heap.size:0); */
    4112             :                                                         /*printf("\n"); */
    4113       89362 :                                                         bitval = BATtordered(bs);
    4114       89362 :                                                         if (!bitval && bs->tnosorted == 0)
    4115       24633 :                                                                 bitval = bit_nil;
    4116       89362 :                                                         if (BUNappend(sort, &bitval, false) != GDK_SUCCEED)
    4117           0 :                                                                 goto bailout;
    4118             : 
    4119       89362 :                                                         bitval = BATtrevordered(bs);
    4120       89362 :                                                         if (!bitval && bs->tnorevsorted == 0)
    4121       23678 :                                                                 bitval = bit_nil;
    4122       89362 :                                                         if (BUNappend(revsort, &bitval, false) != GDK_SUCCEED)
    4123           0 :                                                                 goto bailout;
    4124             : 
    4125       89362 :                                                         bitval = BATtkey(bs);
    4126       89362 :                                                         if (!bitval && bs->tnokey[0] == 0 && bs->tnokey[1] == 0)
    4127       45080 :                                                                 bitval = bit_nil;
    4128       89362 :                                                         if (BUNappend(key, &bitval, false) != GDK_SUCCEED)
    4129           0 :                                                                 goto bailout;
    4130             : 
    4131       89362 :                                                         sz = bs->torderidx && bs->torderidx != (Heap *) 1 ? bs->torderidx->free : 0;
    4132       89362 :                                                         if (BUNappend(oidx, &sz, false) != GDK_SUCCEED)
    4133           0 :                                                                 goto bailout;
    4134             :                                                 }
    4135             :                                         }
    4136             : 
    4137       16860 :                                         if (t->idxs) {
    4138       23521 :                                                 for (ncol = ol_first_node((t)->idxs); ncol; ncol = ncol->next) {
    4139        6661 :                                                         sql_base *bc = ncol->data;
    4140             :                                                         sql_idx *c = (sql_idx *) ncol->data;
    4141        6661 :                                                         if (idx_has_column(c->type)) {
    4142        6655 :                                                                 bs = store->storage_api.bind_idx(tr, c, QUICK);
    4143             :                                                                 lng sz;
    4144             : 
    4145        6655 :                                                                 if (bs == NULL) {
    4146           0 :                                                                         msg = createException(SQL, "sql.storage", SQLSTATE(HY005) "Cannot access column descriptor");
    4147           0 :                                                                         goto bailout;
    4148             :                                                                 }
    4149        6655 :                                                                 if( cname && strcmp(bc->name, cname) )
    4150           8 :                                                                         continue;
    4151             :                                                                 /*printf("schema %s.%s.%s" , b->name, bt->name, bc->name); */
    4152       13294 :                                                                 if (BUNappend(sch, b->name, false) != GDK_SUCCEED ||
    4153       13294 :                                                                     BUNappend(tab, bt->name, false) != GDK_SUCCEED ||
    4154        6647 :                                                                     BUNappend(col, bc->name, false) != GDK_SUCCEED)
    4155           0 :                                                                         goto bailout;
    4156        6647 :                                                                 if (c->t->access == TABLE_WRITABLE) {
    4157         782 :                                                                         if (BUNappend(mode, "writable", false) != GDK_SUCCEED)
    4158           0 :                                                                                 goto bailout;
    4159        5865 :                                                                 } else if (c->t->access == TABLE_APPENDONLY) {
    4160           0 :                                                                         if (BUNappend(mode, "appendonly", false) != GDK_SUCCEED)
    4161           0 :                                                                                 goto bailout;
    4162        5865 :                                                                 } else if (c->t->access == TABLE_READONLY) {
    4163        5865 :                                                                         if (BUNappend(mode, "readonly", false) != GDK_SUCCEED)
    4164           0 :                                                                                 goto bailout;
    4165             :                                                                 } else {
    4166           0 :                                                                         if (BUNappend(mode, str_nil, false) != GDK_SUCCEED)
    4167           0 :                                                                                 goto bailout;
    4168             :                                                                 }
    4169        6647 :                                                                 if (BUNappend(type, "oid", false) != GDK_SUCCEED)
    4170           0 :                                                                         goto bailout;
    4171             : 
    4172             :                                                                 /*printf(" cnt "BUNFMT, BATcount(bs)); */
    4173        6647 :                                                                 sz = BATcount(bs);
    4174        6647 :                                                                 if (BUNappend(cnt, &sz, false) != GDK_SUCCEED)
    4175           0 :                                                                         goto bailout;
    4176             : 
    4177             :                                                                 /*printf(" loc %s", BBP_physical(bs->batCacheid)); */
    4178        6647 :                                                                 if (BUNappend(loc, BBP_physical(bs->batCacheid), false) != GDK_SUCCEED)
    4179           0 :                                                                         goto bailout;
    4180             :                                                                 /*printf(" width %d", bs->twidth); */
    4181        6647 :                                                                 w = bs->twidth;
    4182        6647 :                                                                 if (BUNappend(atom, &w, false) != GDK_SUCCEED)
    4183           0 :                                                                         goto bailout;
    4184             :                                                                 /*printf(" size "BUNFMT, tailsize(bs,BATcount(bs)) + (bs->tvheap? bs->tvheap->size:0)); */
    4185        6647 :                                                                 sz = tailsize(bs, BATcount(bs));
    4186        6647 :                                                                 if (BUNappend(size, &sz, false) != GDK_SUCCEED)
    4187           0 :                                                                         goto bailout;
    4188             : 
    4189        6647 :                                                                 sz = bs->tvheap ? bs->tvheap->size : 0;
    4190        6647 :                                                                 if (BUNappend(heap, &sz, false) != GDK_SUCCEED)
    4191           0 :                                                                         goto bailout;
    4192             : 
    4193        6647 :                                                                 MT_rwlock_rdlock(&bs->thashlock);
    4194        6647 :                                                                 sz = bs->thash && bs->thash != (Hash *) 1 ? bs->thash->heaplink.size + bs->thash->heapbckt.size : 0; /* HASHsize() */
    4195        6647 :                                                                 MT_rwlock_rdunlock(&bs->thashlock);
    4196        6647 :                                                                 if (BUNappend(indices, &sz, false) != GDK_SUCCEED)
    4197           0 :                                                                         goto bailout;
    4198        6647 :                                                                 bitval = 0; /* HASHispersistent(bs); */
    4199        6647 :                                                                 if (BUNappend(phash, &bitval, false) != GDK_SUCCEED)
    4200           0 :                                                                         goto bailout;
    4201             : 
    4202        6647 :                                                                 sz = IMPSimprintsize(bs);
    4203        6647 :                                                                 if (BUNappend(imprints, &sz, false) != GDK_SUCCEED)
    4204           0 :                                                                         goto bailout;
    4205             :                                                                 /*printf(" indices "BUNFMT, bs->thash?bs->thash->heaplink.size+bs->thash->heapbckt.size:0); */
    4206             :                                                                 /*printf("\n"); */
    4207        6647 :                                                                 bitval = BATtordered(bs);
    4208        6647 :                                                                 if (!bitval && bs->tnosorted == 0)
    4209           0 :                                                                         bitval = bit_nil;
    4210        6647 :                                                                 if (BUNappend(sort, &bitval, false) != GDK_SUCCEED)
    4211           0 :                                                                         goto bailout;
    4212        6647 :                                                                 bitval = BATtrevordered(bs);
    4213        6647 :                                                                 if (!bitval && bs->tnorevsorted == 0)
    4214           0 :                                                                         bitval = bit_nil;
    4215        6647 :                                                                 if (BUNappend(revsort, &bitval, false) != GDK_SUCCEED)
    4216           0 :                                                                         goto bailout;
    4217        6647 :                                                                 bitval = BATtkey(bs);
    4218        6647 :                                                                 if (!bitval && bs->tnokey[0] == 0 && bs->tnokey[1] == 0)
    4219        6183 :                                                                         bitval = bit_nil;
    4220        6647 :                                                                 if (BUNappend(key, &bitval, false) != GDK_SUCCEED)
    4221           0 :                                                                         goto bailout;
    4222        6647 :                                                                 sz = bs->torderidx && bs->torderidx != (Heap *) 1 ? bs->torderidx->free : 0;
    4223        6647 :                                                                 if (BUNappend(oidx, &sz, false) != GDK_SUCCEED)
    4224           0 :                                                                         goto bailout;
    4225             :                                                         }
    4226             :                                                 }
    4227             :                                         }
    4228             :                                 }
    4229             :                         }
    4230             :                 }
    4231             :         }
    4232             : 
    4233         428 :         BBPkeepref(*rsch = sch->batCacheid);
    4234         428 :         BBPkeepref(*rtab = tab->batCacheid);
    4235         428 :         BBPkeepref(*rcol = col->batCacheid);
    4236         428 :         BBPkeepref(*rmode = mode->batCacheid);
    4237         428 :         BBPkeepref(*rloc = loc->batCacheid);
    4238         428 :         BBPkeepref(*rtype = type->batCacheid);
    4239         428 :         BBPkeepref(*rcnt = cnt->batCacheid);
    4240         428 :         BBPkeepref(*ratom = atom->batCacheid);
    4241         428 :         BBPkeepref(*rsize = size->batCacheid);
    4242         428 :         BBPkeepref(*rheap = heap->batCacheid);
    4243         428 :         BBPkeepref(*rindices = indices->batCacheid);
    4244         428 :         BBPkeepref(*rphash = phash->batCacheid);
    4245         428 :         BBPkeepref(*rimprints = imprints->batCacheid);
    4246         428 :         BBPkeepref(*rsort = sort->batCacheid);
    4247         428 :         BBPkeepref(*rrevsort = revsort->batCacheid);
    4248         428 :         BBPkeepref(*rkey = key->batCacheid);
    4249         428 :         BBPkeepref(*roidx = oidx->batCacheid);
    4250         428 :         return MAL_SUCCEED;
    4251             : 
    4252           0 :   bailout:
    4253           0 :         if (sch)
    4254           0 :                 BBPunfix(sch->batCacheid);
    4255           0 :         if (tab)
    4256           0 :                 BBPunfix(tab->batCacheid);
    4257           0 :         if (col)
    4258           0 :                 BBPunfix(col->batCacheid);
    4259           0 :         if (mode)
    4260           0 :                 BBPunfix(mode->batCacheid);
    4261           0 :         if (loc)
    4262           0 :                 BBPunfix(loc->batCacheid);
    4263           0 :         if (cnt)
    4264           0 :                 BBPunfix(cnt->batCacheid);
    4265           0 :         if (type)
    4266           0 :                 BBPunfix(type->batCacheid);
    4267           0 :         if (atom)
    4268           0 :                 BBPunfix(atom->batCacheid);
    4269           0 :         if (size)
    4270           0 :                 BBPunfix(size->batCacheid);
    4271           0 :         if (heap)
    4272           0 :                 BBPunfix(heap->batCacheid);
    4273           0 :         if (indices)
    4274           0 :                 BBPunfix(indices->batCacheid);
    4275           0 :         if (phash)
    4276           0 :                 BBPunfix(phash->batCacheid);
    4277           0 :         if (imprints)
    4278           0 :                 BBPunfix(imprints->batCacheid);
    4279           0 :         if (sort)
    4280           0 :                 BBPunfix(sort->batCacheid);
    4281           0 :         if (revsort)
    4282           0 :                 BBPunfix(revsort->batCacheid);
    4283           0 :         if (key)
    4284           0 :                 BBPunfix(key->batCacheid);
    4285           0 :         if (oidx)
    4286           0 :                 BBPunfix(oidx->batCacheid);
    4287           0 :         if (!msg)
    4288           0 :                 msg = createException(SQL, "sql.storage", GDK_EXCEPTION);
    4289             :         return msg;
    4290             : }
    4291             : 
    4292             : void
    4293      398663 : freeVariables(Client c, MalBlkPtr mb, MalStkPtr glb, int oldvtop, int oldvid)
    4294             : {
    4295      412802 :         for (int i = oldvtop; i < mb->vtop;) {
    4296       14139 :                 if (glb) {
    4297           0 :                         if (isVarCleanup(mb, i))
    4298           0 :                                 garbageElement(c, &glb->stk[i]);
    4299             :                         /* clean stack entry */
    4300           0 :                         glb->stk[i].vtype = TYPE_int;
    4301           0 :                         glb->stk[i].val.ival = 0;
    4302           0 :                         glb->stk[i].len = 0;
    4303             :                 }
    4304       14139 :                 clearVariable(mb, i);
    4305       14139 :                 i++;
    4306             :         }
    4307      398663 :         mb->vtop = oldvtop;
    4308      398663 :         mb->vid = oldvid;
    4309      398663 : }
    4310             : 
    4311             : str
    4312           0 : STRindex_int(int *i, const str *src, const bit *u)
    4313             : {
    4314             :         (void)src; (void)u;
    4315           0 :         *i = 0;
    4316           0 :         return MAL_SUCCEED;
    4317             : }
    4318             : 
    4319             : str
    4320           0 : BATSTRindex_int(bat *res, const bat *src, const bit *u)
    4321             : {
    4322             :         BAT *s, *r;
    4323             : 
    4324           0 :         if ((s = BATdescriptor(*src)) == NULL)
    4325           0 :                 throw(SQL, "calc.index", SQLSTATE(HY005) "Cannot access column descriptor");
    4326             : 
    4327           0 :         if (*u) {
    4328           0 :                 Heap *h = s->tvheap;
    4329             :                 size_t pad, pos;
    4330             :                 int v;
    4331             : 
    4332           0 :                 r = COLnew(0, TYPE_int, 1024, TRANSIENT);
    4333           0 :                 if (r == NULL) {
    4334           0 :                         BBPunfix(s->batCacheid);
    4335           0 :                         throw(SQL, "calc.index", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    4336             :                 }
    4337             :                 pos = GDK_STRHASHSIZE;
    4338           0 :                 while (pos < h->free) {
    4339             :                         const char *p;
    4340             : 
    4341           0 :                         pad = GDK_VARALIGN - (pos & (GDK_VARALIGN - 1));
    4342           0 :                         if (pad < sizeof(stridx_t))
    4343           0 :                                 pad += GDK_VARALIGN;
    4344           0 :                         pos += pad;
    4345           0 :                         p = h->base + pos;
    4346           0 :                         v = (int) (pos - GDK_STRHASHSIZE);
    4347           0 :                         if (BUNappend(r, &v, false) != GDK_SUCCEED) {
    4348           0 :                                 BBPreclaim(r);
    4349           0 :                                 BBPunfix(s->batCacheid);
    4350           0 :                                 throw(SQL, "calc.index", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    4351             :                         }
    4352           0 :                         pos += strLen(p);
    4353             :                 }
    4354             :         } else {
    4355           0 :                 r = VIEWcreate(s->hseqbase, s);
    4356           0 :                 if (r == NULL) {
    4357           0 :                         BBPunfix(s->batCacheid);
    4358           0 :                         throw(SQL, "calc.index", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    4359             :                 }
    4360           0 :                 r->ttype = TYPE_int;
    4361           0 :                 r->tvarsized = false;
    4362           0 :                 HEAPdecref(r->tvheap, false);
    4363           0 :                 r->tvheap = NULL;
    4364             :         }
    4365           0 :         BBPunfix(s->batCacheid);
    4366           0 :         BBPkeepref((*res = r->batCacheid));
    4367           0 :         return MAL_SUCCEED;
    4368             : }
    4369             : 
    4370             : str
    4371           0 : STRindex_sht(sht *i, const str *src, const bit *u)
    4372             : {
    4373             :         (void)src; (void)u;
    4374           0 :         *i = 0;
    4375           0 :         return MAL_SUCCEED;
    4376             : }
    4377             : 
    4378             : str
    4379           0 : BATSTRindex_sht(bat *res, const bat *src, const bit *u)
    4380             : {
    4381             :         BAT *s, *r;
    4382             : 
    4383           0 :         if ((s = BATdescriptor(*src)) == NULL)
    4384           0 :                 throw(SQL, "calc.index", SQLSTATE(HY005) "Cannot access column descriptor");
    4385             : 
    4386           0 :         if (*u) {
    4387           0 :                 Heap *h = s->tvheap;
    4388             :                 size_t pad, pos;
    4389             :                 sht v;
    4390             : 
    4391           0 :                 r = COLnew(0, TYPE_sht, 1024, TRANSIENT);
    4392           0 :                 if (r == NULL) {
    4393           0 :                         BBPunfix(s->batCacheid);
    4394           0 :                         throw(SQL, "calc.index", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    4395             :                 }
    4396             :                 pos = GDK_STRHASHSIZE;
    4397           0 :                 while (pos < h->free) {
    4398             :                         const char *s;
    4399             : 
    4400           0 :                         pad = GDK_VARALIGN - (pos & (GDK_VARALIGN - 1));
    4401           0 :                         if (pad < sizeof(stridx_t))
    4402           0 :                                 pad += GDK_VARALIGN;
    4403           0 :                         pos += pad;
    4404           0 :                         s = h->base + pos;
    4405           0 :                         v = (sht) (pos - GDK_STRHASHSIZE);
    4406           0 :                         if (BUNappend(r, &v, false) != GDK_SUCCEED) {
    4407           0 :                                 BBPreclaim(r);
    4408           0 :                                 throw(SQL, "calc.index", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    4409             :                         }
    4410           0 :                         pos += strLen(s);
    4411             :                 }
    4412             :         } else {
    4413           0 :                 r = VIEWcreate(s->hseqbase, s);
    4414           0 :                 if (r == NULL) {
    4415           0 :                         BBPunfix(s->batCacheid);
    4416           0 :                         throw(SQL, "calc.index", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    4417             :                 }
    4418           0 :                 r->ttype = TYPE_sht;
    4419           0 :                 r->tvarsized = false;
    4420           0 :                 HEAPdecref(r->tvheap, false);
    4421           0 :                 r->tvheap = NULL;
    4422             :         }
    4423           0 :         BBPunfix(s->batCacheid);
    4424           0 :         BBPkeepref((*res = r->batCacheid));
    4425           0 :         return MAL_SUCCEED;
    4426             : }
    4427             : 
    4428             : str
    4429           0 : STRindex_bte(bte *i, const str *src, const bit *u)
    4430             : {
    4431             :         (void)src; (void)u;
    4432           0 :         *i = 0;
    4433           0 :         return MAL_SUCCEED;
    4434             : }
    4435             : 
    4436             : str
    4437           5 : BATSTRindex_bte(bat *res, const bat *src, const bit *u)
    4438             : {
    4439             :         BAT *s, *r;
    4440             : 
    4441           5 :         if ((s = BATdescriptor(*src)) == NULL)
    4442           0 :                 throw(SQL, "calc.index", SQLSTATE(HY005) "Cannot access column descriptor");
    4443             : 
    4444           5 :         if (*u) {
    4445           5 :                 Heap *h = s->tvheap;
    4446             :                 size_t pad, pos;
    4447             :                 bte v;
    4448             : 
    4449           5 :                 r = COLnew(0, TYPE_bte, 64, TRANSIENT);
    4450           5 :                 if (r == NULL) {
    4451           0 :                         BBPunfix(s->batCacheid);
    4452           0 :                         throw(SQL, "calc.index", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    4453             :                 }
    4454             :                 pos = GDK_STRHASHSIZE;
    4455          26 :                 while (pos < h->free) {
    4456             :                         const char *p;
    4457             : 
    4458          21 :                         pad = GDK_VARALIGN - (pos & (GDK_VARALIGN - 1));
    4459          21 :                         if (pad < sizeof(stridx_t))
    4460          16 :                                 pad += GDK_VARALIGN;
    4461          21 :                         pos += pad;
    4462          21 :                         p = h->base + pos;
    4463          21 :                         v = (bte) (pos - GDK_STRHASHSIZE);
    4464          21 :                         if (BUNappend(r, &v, false) != GDK_SUCCEED) {
    4465           0 :                                 BBPreclaim(r);
    4466           0 :                                 BBPunfix(s->batCacheid);
    4467           0 :                                 throw(SQL, "calc.index", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    4468             :                         }
    4469          21 :                         pos += strLen(p);
    4470             :                 }
    4471             :         } else {
    4472           0 :                 r = VIEWcreate(s->hseqbase, s);
    4473           0 :                 if (r == NULL) {
    4474           0 :                         BBPunfix(s->batCacheid);
    4475           0 :                         throw(SQL, "calc.index", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    4476             :                 }
    4477           0 :                 r->ttype = TYPE_bte;
    4478           0 :                 r->tvarsized = false;
    4479           0 :                 HEAPdecref(r->tvheap, false);
    4480           0 :                 r->tvheap = NULL;
    4481             :         }
    4482           5 :         BBPunfix(s->batCacheid);
    4483           5 :         BBPkeepref((*res = r->batCacheid));
    4484           5 :         return MAL_SUCCEED;
    4485             : }
    4486             : 
    4487             : str
    4488           0 : STRstrings(str *i, const str *src)
    4489             : {
    4490             :         (void)src;
    4491           0 :         *i = 0;
    4492           0 :         return MAL_SUCCEED;
    4493             : }
    4494             : 
    4495             : str
    4496           0 : BATSTRstrings(bat *res, const bat *src)
    4497             : {
    4498             :         BAT *s, *r;
    4499             :         Heap *h;
    4500             :         size_t pad, pos;
    4501             : 
    4502           0 :         if ((s = BATdescriptor(*src)) == NULL)
    4503           0 :                 throw(SQL, "calc.strings", SQLSTATE(HY005) "Cannot access column descriptor");
    4504             : 
    4505           0 :         h = s->tvheap;
    4506           0 :         r = COLnew(0, TYPE_str, 1024, TRANSIENT);
    4507           0 :         if (r == NULL) {
    4508           0 :                 BBPunfix(s->batCacheid);
    4509           0 :                 throw(SQL, "calc.strings", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    4510             :         }
    4511             :         pos = GDK_STRHASHSIZE;
    4512           0 :         while (pos < h->free) {
    4513             :                 const char *p;
    4514             : 
    4515           0 :                 pad = GDK_VARALIGN - (pos & (GDK_VARALIGN - 1));
    4516           0 :                 if (pad < sizeof(stridx_t))
    4517           0 :                         pad += GDK_VARALIGN;
    4518           0 :                 pos += pad;
    4519           0 :                 p = h->base + pos;
    4520           0 :                 if (BUNappend(r, p, false) != GDK_SUCCEED) {
    4521           0 :                         BBPreclaim(r);
    4522           0 :                         BBPunfix(s->batCacheid);
    4523           0 :                         throw(SQL, "calc.strings", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    4524             :                 }
    4525           0 :                 pos += strLen(p);
    4526             :         }
    4527           0 :         BBPunfix(s->batCacheid);
    4528           0 :         BBPkeepref((*res = r->batCacheid));
    4529           0 :         return MAL_SUCCEED;
    4530             : }
    4531             : 
    4532             : str
    4533           0 : SQLresume_log_flushing(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    4534             : {
    4535             :         mvc *mvc;
    4536             : 
    4537             :         (void)stk; (void)pci;
    4538           0 :         char *msg = getSQLContext(cntxt, mb, &mvc, NULL);
    4539           0 :         if (msg)
    4540             :                 return msg;
    4541           0 :         store_resume_log(mvc->store);
    4542           0 :         return MAL_SUCCEED;
    4543             : }
    4544             : 
    4545             : str
    4546           0 : SQLsuspend_log_flushing(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    4547             : {
    4548             :         mvc *mvc;
    4549             : 
    4550             :         (void)stk; (void)pci;
    4551           0 :         char *msg = getSQLContext(cntxt, mb, &mvc, NULL);
    4552           0 :         if (msg)
    4553             :                 return msg;
    4554           0 :         store_suspend_log(mvc->store);
    4555           0 :         return MAL_SUCCEED;
    4556             : }
    4557             : 
    4558             : str
    4559             : /*SQLhot_snapshot(void *ret, const str *tarfile_arg)*/
    4560           5 : SQLhot_snapshot(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    4561             : {
    4562           5 :         char *tarfile = *getArgReference_str(stk, pci, 1);
    4563             :         mvc *mvc;
    4564             : 
    4565           5 :         char *msg = getSQLContext(cntxt, mb, &mvc, NULL);
    4566           5 :         if (msg)
    4567             :                 return msg;
    4568           5 :         lng result = store_hot_snapshot(mvc->session->tr->store, tarfile);
    4569           5 :         if (result)
    4570             :                 return MAL_SUCCEED;
    4571             :         else
    4572           0 :                 throw(SQL, "sql.hot_snapshot", GDK_EXCEPTION);
    4573             : }
    4574             : 
    4575             : str
    4576           0 : SQLhot_snapshot_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    4577             : {
    4578             :         char *filename;
    4579             :         bool onserver;
    4580             :         char *msg = MAL_SUCCEED;
    4581             :         char buf[80];
    4582             :         mvc *mvc;
    4583             :         ssize_t sz;
    4584             :         stream *s;
    4585             :         stream *cb = NULL;
    4586             :         lng result;
    4587             : 
    4588           0 :         filename = *getArgReference_str(stk, pci, 1);
    4589           0 :         onserver = *getArgReference_bit(stk, pci, 2);
    4590             : 
    4591           0 :         msg = getSQLContext(cntxt, mb, &mvc, NULL);
    4592           0 :         if (msg)
    4593             :                 return msg;
    4594             : 
    4595           0 :         sqlstore *store = mvc->session->tr->store;
    4596           0 :         if (onserver) {
    4597           0 :                 lng result = store_hot_snapshot(store, filename);
    4598           0 :                 if (result)
    4599             :                         return MAL_SUCCEED;
    4600             :                 else
    4601           0 :                         throw(SQL, "sql.hot_snapshot", GDK_EXCEPTION);
    4602             :         }
    4603             : 
    4604             :         // sync with client, copy pasted from mvc_export_table_wrap
    4605           0 :         while (!mvc->scanner.rs->eof)
    4606           0 :                 bstream_next(mvc->scanner.rs);
    4607             : 
    4608             :         // The snapshot code flushes from time to time.
    4609             :         // Use a callback stream to suppress those.
    4610           0 :         s = mvc->scanner.ws;
    4611           0 :         cb = callback_stream(
    4612             :                 /* private */ s,
    4613             :                 /* read */    NULL,
    4614             :                 /* write */   (void*)mnstr_write,
    4615             :                 /* close */   NULL,
    4616             :                 /* destroy */ NULL,
    4617             :                 "snapshot-callback"
    4618             :         );
    4619           0 :         if (!cb)
    4620           0 :                 throw(SQL, "sql.hot_snapshot", GDK_EXCEPTION);
    4621             : 
    4622             :         // tell client to open file, copy pasted from mvc_export_table_wrap
    4623           0 :         mnstr_write(s, PROMPT3, sizeof(PROMPT3) - 1, 1);
    4624           0 :         mnstr_printf(s, "w %s\n", filename);
    4625           0 :         mnstr_flush(s, MNSTR_FLUSH_DATA);
    4626           0 :         if ((sz = mnstr_readline(mvc->scanner.rs->s, buf, sizeof(buf))) > 1) {
    4627             :                 /* non-empty line indicates failure on client */
    4628           0 :                 msg = createException(IO, "streams.open", "%s", buf);
    4629             :                         /* discard until client flushes */
    4630           0 :                         while (mnstr_read(mvc->scanner.rs->s, buf, 1, sizeof(buf)) > 0) {
    4631             :                                 /* ignore remainder of error message */
    4632             :                         }
    4633           0 :                 goto end;
    4634             :         }
    4635             : 
    4636             :         // client is waiting for data now, send it.
    4637           0 :         result = store_hot_snapshot_to_stream(store, cb);
    4638           0 :         if (result)
    4639             :                 msg = MAL_SUCCEED;
    4640             :         else
    4641           0 :                 msg = createException(SQL, "sql.hot_snapshot", GDK_EXCEPTION);
    4642           0 :         mnstr_destroy(cb);
    4643             : 
    4644             :         // tell client no more data, also copy pasted from mvc_export_table_wrap
    4645           0 :         mnstr_flush(s, MNSTR_FLUSH_DATA);
    4646           0 :         if ((sz = mnstr_readline(mvc->scanner.rs->s, buf, sizeof(buf))) > 1) {
    4647           0 :                 msg = createException(IO, "streams.open", "%s", buf);
    4648             :         }
    4649           0 :         while (sz > 0)
    4650           0 :                 sz = mnstr_readline(mvc->scanner.rs->s, buf, sizeof(buf));
    4651             : 
    4652           0 : end:
    4653             :         return msg;
    4654             : }
    4655             : 
    4656             : str
    4657           9 : SQLsession_prepared_statements(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    4658             : {
    4659             :         BAT *sessionid, *user, *statementid, *statement, *created;
    4660           9 :         bat *sid = getArgReference_bat(stk,pci,0);
    4661           9 :         bat *u = getArgReference_bat(stk,pci,1);
    4662           9 :         bat *i = getArgReference_bat(stk,pci,2);
    4663           9 :         bat *s = getArgReference_bat(stk,pci,3);
    4664           9 :         bat *c = getArgReference_bat(stk,pci,4);
    4665             :         str msg = MAL_SUCCEED, usr;
    4666           9 :         mvc *sql = NULL;
    4667             :         cq *q = NULL;
    4668             : 
    4669             :         (void) stk;
    4670             :         (void) pci;
    4671           9 :         if ((msg = getSQLContext(cntxt, mb, &sql, NULL)) != NULL)
    4672             :                 return msg;
    4673           9 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    4674             :                 return msg;
    4675             : 
    4676           9 :         assert(sql->qc);
    4677             : 
    4678           9 :         sessionid = COLnew(0, TYPE_int, 256, TRANSIENT);
    4679           9 :         user = COLnew(0, TYPE_str, 256, TRANSIENT);
    4680           9 :         statementid = COLnew(0, TYPE_int, 256, TRANSIENT);
    4681           9 :         statement = COLnew(0, TYPE_str, 256, TRANSIENT);
    4682           9 :         created = COLnew(0, TYPE_timestamp, 256, TRANSIENT);
    4683           9 :         if (sessionid == NULL || user == NULL || statementid == NULL || statement == NULL || created == NULL) {
    4684           0 :                 msg = createException(SQL, "sql.session_prepared_statements", GDK_EXCEPTION);
    4685           0 :                 goto bailout;
    4686             :         }
    4687             : 
    4688          13 :         for (q = sql->qc->q; q; q = q->next) {
    4689             :                 gdk_return bun_res;
    4690           4 :                 if (BUNappend(sessionid, &(cntxt->idx), false) != GDK_SUCCEED) {
    4691           0 :                         msg = createException(SQL, "sql.session_prepared_statements", GDK_EXCEPTION);
    4692           0 :                         goto bailout;
    4693             :                 }
    4694             : 
    4695           4 :                 msg = AUTHgetUsername(&usr, cntxt);
    4696           4 :                 if (msg != MAL_SUCCEED)
    4697           0 :                         goto bailout;
    4698           4 :                 bun_res = BUNappend(user, usr, false);
    4699           4 :                 GDKfree(usr);
    4700           4 :                 if (bun_res != GDK_SUCCEED) {
    4701           0 :                         msg = createException(SQL, "sql.session_prepared_statements", GDK_EXCEPTION);
    4702           0 :                         goto bailout;
    4703             :                 }
    4704             : 
    4705           4 :                 if (BUNappend(statementid, &(q->id), false) != GDK_SUCCEED) {
    4706           0 :                         msg = createException(SQL, "sql.session_prepared_statements", GDK_EXCEPTION);
    4707           0 :                         goto bailout;
    4708             :                 }
    4709           4 :                 if (BUNappend(statement, q->f->query, false) != GDK_SUCCEED) {
    4710           0 :                         msg = createException(SQL, "sql.session_prepared_statements", GDK_EXCEPTION);
    4711           0 :                         goto bailout;
    4712             :                 }
    4713           4 :                 if (BUNappend(created, &(q->created), false) != GDK_SUCCEED) {
    4714           0 :                         msg = createException(SQL, "sql.session_prepared_statements", GDK_EXCEPTION);
    4715           0 :                         goto bailout;
    4716             :                 }
    4717             :         }
    4718             : 
    4719           9 : bailout:
    4720           9 :         if (msg) {
    4721           0 :                 BBPreclaim(sessionid);
    4722           0 :                 BBPreclaim(user);
    4723           0 :                 BBPreclaim(statementid);
    4724           0 :                 BBPreclaim(statement);
    4725           0 :                 BBPreclaim(created);
    4726             :         } else {
    4727           9 :                 BBPkeepref(*sid = sessionid->batCacheid);
    4728           9 :                 BBPkeepref(*u = user->batCacheid);
    4729           9 :                 BBPkeepref(*i = statementid->batCacheid);
    4730           9 :                 BBPkeepref(*s = statement->batCacheid);
    4731           9 :                 BBPkeepref(*c = created->batCacheid);
    4732             :         }
    4733             :         return msg;
    4734             : }
    4735             : 
    4736             : str
    4737           6 : SQLsession_prepared_statements_args(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    4738             : {
    4739             :         BAT *statementid, *type, *digits, *isinout, *number, *scale, *schema, *table, *column;
    4740           6 :         bat *sid = getArgReference_bat(stk,pci,0);
    4741           6 :         bat *t = getArgReference_bat(stk,pci,1);
    4742           6 :         bat *d = getArgReference_bat(stk,pci,2);
    4743           6 :         bat *s = getArgReference_bat(stk,pci,3);
    4744           6 :         bat *io = getArgReference_bat(stk,pci,4);
    4745           6 :         bat *n = getArgReference_bat(stk,pci,5);
    4746           6 :         bat *sch = getArgReference_bat(stk,pci,6);
    4747           6 :         bat *tbl = getArgReference_bat(stk,pci,7);
    4748           6 :         bat *col = getArgReference_bat(stk,pci,8);
    4749             :         str msg = MAL_SUCCEED;
    4750           6 :         mvc *sql = NULL;
    4751             :         cq *q = NULL;
    4752             : 
    4753             :         (void) stk;
    4754             :         (void) pci;
    4755           6 :         if ((msg = getSQLContext(cntxt, mb, &sql, NULL)) != NULL)
    4756             :                 return msg;
    4757           6 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    4758             :                 return msg;
    4759             : 
    4760           6 :         assert(sql->qc);
    4761             : 
    4762           6 :         statementid = COLnew(0, TYPE_int, 256, TRANSIENT);
    4763           6 :         type = COLnew(0, TYPE_str, 256, TRANSIENT);
    4764           6 :         digits = COLnew(0, TYPE_int, 256, TRANSIENT);
    4765           6 :         scale = COLnew(0, TYPE_int, 256, TRANSIENT);
    4766           6 :         isinout = COLnew(0, TYPE_bte, 256, TRANSIENT);
    4767           6 :         number = COLnew(0, TYPE_int, 256, TRANSIENT);
    4768           6 :         schema = COLnew(0, TYPE_str, 256, TRANSIENT);
    4769           6 :         table = COLnew(0, TYPE_str, 256, TRANSIENT);
    4770           6 :         column = COLnew(0, TYPE_str, 256, TRANSIENT);
    4771           6 :         if (!statementid || !type || !digits || !scale || !isinout || !number || !schema || !table || !column) {
    4772           0 :                 msg = createException(SQL, "sql.session_prepared_statements_args", GDK_EXCEPTION);
    4773           0 :                 goto bailout;
    4774             :         }
    4775             : 
    4776           9 :         for (q = sql->qc->q; q; q = q->next) {
    4777           3 :                 sql_rel *r = q->rel;
    4778           3 :                 int arg_number = 0;
    4779           3 :                 bte inout = ARG_OUT;
    4780             : 
    4781           3 :                 if (r && (is_topn(r->op) || is_sample(r->op)))
    4782           0 :                         r = r->l;
    4783             : 
    4784           3 :                 if (r && is_project(r->op) && r->exps) {
    4785           6 :                         for (node *n = r->exps->h; n; n = n->next, arg_number++) {
    4786           3 :                                 sql_exp *e = n->data;
    4787           3 :                                 sql_subtype *t = exp_subtype(e);
    4788           3 :                                 const char *name = exp_name(e), *rname = exp_relname(e), *rschema = ATOMnilptr(TYPE_str);
    4789             : 
    4790           3 :                                 if (!name && e->type == e_column && e->r)
    4791             :                                         name = e->r;
    4792           3 :                                 if (!name)
    4793             :                                         name = ATOMnilptr(TYPE_str);
    4794           3 :                                 if (!rname && e->type == e_column && e->l)
    4795             :                                         rname = e->l;
    4796           3 :                                 if (!rname)
    4797             :                                         rname = ATOMnilptr(TYPE_str);
    4798             : 
    4799           6 :                                 if (BUNappend(statementid, &(q->id), false) != GDK_SUCCEED ||
    4800           6 :                                         BUNappend(type, t->type->base.name, false) != GDK_SUCCEED ||
    4801           6 :                                         BUNappend(digits, &t->digits, false) != GDK_SUCCEED ||
    4802           6 :                                         BUNappend(scale, &t->scale, false) != GDK_SUCCEED ||
    4803           6 :                                         BUNappend(isinout, &inout, false) != GDK_SUCCEED ||
    4804           6 :                                         BUNappend(number, &arg_number, false) != GDK_SUCCEED ||
    4805           6 :                                         BUNappend(schema, rschema, false) != GDK_SUCCEED ||
    4806           6 :                                         BUNappend(table, rname, false) != GDK_SUCCEED ||
    4807           3 :                                         BUNappend(column, name, false) != GDK_SUCCEED) {
    4808           0 :                                         msg = createException(SQL, "sql.session_prepared_statements_args", GDK_EXCEPTION);
    4809           0 :                                         goto bailout;
    4810             :                                 }
    4811             :                         }
    4812             :                 }
    4813             : 
    4814           3 :                 if (q->f->ops) {
    4815           3 :                         inout = ARG_IN;
    4816           6 :                         for (node *n = q->f->ops->h; n; n=n->next, arg_number++) {
    4817           3 :                                 sql_arg *a = n->data;
    4818             :                                 sql_subtype *t = &a->type;
    4819             : 
    4820           6 :                                 if (BUNappend(statementid, &(q->id), false) != GDK_SUCCEED ||
    4821           6 :                                         BUNappend(type, t->type->base.name, false) != GDK_SUCCEED ||
    4822           6 :                                         BUNappend(digits, &(t->digits), false) != GDK_SUCCEED ||
    4823           6 :                                         BUNappend(scale, &(t->scale), false) != GDK_SUCCEED ||
    4824           6 :                                         BUNappend(isinout, &inout, false) != GDK_SUCCEED ||
    4825           6 :                                         BUNappend(number, &arg_number, false) != GDK_SUCCEED ||
    4826           6 :                                         BUNappend(schema, ATOMnilptr(TYPE_str), false) != GDK_SUCCEED ||
    4827           6 :                                         BUNappend(table, ATOMnilptr(TYPE_str), false) != GDK_SUCCEED ||
    4828           3 :                                         BUNappend(column, ATOMnilptr(TYPE_str), false) != GDK_SUCCEED) {
    4829           0 :                                         msg = createException(SQL, "sql.session_prepared_statements_args", GDK_EXCEPTION);
    4830           0 :                                         goto bailout;
    4831             :                                 }
    4832             :                         }
    4833             :                 }
    4834             :         }
    4835             : 
    4836           6 : bailout:
    4837           6 :         if (msg) {
    4838           0 :                 BBPreclaim(statementid);
    4839           0 :                 BBPreclaim(type);
    4840           0 :                 BBPreclaim(digits);
    4841           0 :                 BBPreclaim(scale);
    4842           0 :                 BBPreclaim(isinout);
    4843           0 :                 BBPreclaim(number);
    4844           0 :                 BBPreclaim(schema);
    4845           0 :                 BBPreclaim(table);
    4846           0 :                 BBPreclaim(column);
    4847             :         } else {
    4848           6 :                 BBPkeepref(*sid = statementid->batCacheid);
    4849           6 :                 BBPkeepref(*t = type->batCacheid);
    4850           6 :                 BBPkeepref(*d = digits->batCacheid);
    4851           6 :                 BBPkeepref(*s = scale->batCacheid);
    4852           6 :                 BBPkeepref(*io = isinout->batCacheid);
    4853           6 :                 BBPkeepref(*n = number->batCacheid);
    4854           6 :                 BBPkeepref(*sch = schema->batCacheid);
    4855           6 :                 BBPkeepref(*tbl = table->batCacheid);
    4856           6 :                 BBPkeepref(*col = column->batCacheid);
    4857             :         }
    4858             :         return msg;
    4859             : }
    4860             : 
    4861             : /* input id,row-input-values
    4862             :  * for each id call function(with row-input-values) return table
    4863             :  * return for each id the table, ie id (*length of table) and table results
    4864             :  */
    4865             : str
    4866           8 : SQLunionfunc(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    4867             : {
    4868           8 :         int arg = pci->retc;
    4869             :         str mod, fcn, ret = MAL_SUCCEED;
    4870             :         InstrPtr npci;
    4871             : 
    4872           8 :         mod = *getArgReference_str(stk, pci, arg++);
    4873           8 :         fcn = *getArgReference_str(stk, pci, arg++);
    4874           8 :         npci = newStmtArgs(mb, mod, fcn, pci->argc);
    4875             : 
    4876          16 :         for (int i = 1; i < pci->retc; i++) {
    4877           8 :                 int type = getArgType(mb, pci, i);
    4878             : 
    4879           8 :                 if (i==1)
    4880           8 :                         getArg(npci, 0) = newTmpVariable(mb, type);
    4881             :                 else
    4882           0 :                         npci = pushReturn(mb, npci, newTmpVariable(mb, type));
    4883             :         }
    4884          16 :         for (int i = pci->retc+2+1; i < pci->argc; i++) {
    4885           8 :                 int type = getBatType(getArgType(mb, pci, i));
    4886             : 
    4887           8 :                 npci = pushNil(mb, npci, type);
    4888             :         }
    4889             :         /* check program to get the proper malblk */
    4890           8 :         if (chkInstruction(cntxt->usermodule, mb, npci)) {
    4891           0 :                 freeInstruction(npci);
    4892           0 :                 return createException(MAL, "sql.unionfunc", SQLSTATE(42000) PROGRAM_GENERAL);
    4893             :         }
    4894             : 
    4895           8 :         if (npci) {
    4896             :                 BAT **res = NULL, **input = NULL;
    4897             :                 BATiter *bi = NULL;
    4898             :                 BUN cnt = 0;
    4899           8 :                 int nrinput = pci->argc - 2 - pci->retc;
    4900             :                 MalBlkPtr nmb = NULL;
    4901             :                 MalStkPtr env = NULL;
    4902             :                 InstrPtr q = NULL;
    4903             : 
    4904           8 :                 if (!(input = GDKzalloc(sizeof(BAT*) * nrinput))) {
    4905           0 :                         ret = createException(MAL, "sql.unionfunc", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    4906           0 :                         goto finalize;
    4907             :                 }
    4908           8 :                 if (!(bi = GDKmalloc(sizeof(BATiter) * nrinput))) {
    4909           0 :                         ret = createException(MAL, "sql.unionfunc", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    4910           0 :                         goto finalize;
    4911             :                 }
    4912           8 :                 assert(nrinput == pci->retc);
    4913          24 :                 for (int i = 0, j = pci->retc+2; j < pci->argc; i++, j++) {
    4914          16 :                         bat *b = getArgReference_bat(stk, pci, j);
    4915          16 :                         if (!(input[i] = BATdescriptor(*b))) {
    4916           0 :                                 ret = createException(MAL, "sql.unionfunc", SQLSTATE(HY005) "Cannot access column descriptor");
    4917           0 :                                 goto finalize;
    4918             :                         }
    4919          16 :                         bi[i] = bat_iterator(input[i]);
    4920          16 :                         cnt = BATcount(input[i]);
    4921             :                 }
    4922             : 
    4923             :                 /* create result bats */
    4924           8 :                 if (!(res = GDKzalloc(sizeof(BAT*) * pci->retc))) {
    4925           0 :                         ret = createException(MAL, "sql.unionfunc", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    4926           0 :                         goto finalize;
    4927             :                 }
    4928          24 :                 for (int i = 0; i<pci->retc; i++) {
    4929          16 :                         int type = getArgType(mb, pci, i);
    4930             : 
    4931          16 :                         if (!(res[i] = COLnew(0, getBatType(type), cnt, TRANSIENT))) {
    4932           0 :                                 ret = createException(MAL, "sql.unionfunc", GDK_EXCEPTION);
    4933           0 :                                 goto finalize;
    4934             :                         }
    4935             :                 }
    4936             : 
    4937           8 :                 if (!(nmb = copyMalBlk(npci->blk))) {
    4938           0 :                         ret = createException(MAL, "sql.unionfunc", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    4939           0 :                         goto finalize;
    4940             :                 }
    4941           8 :                 if (!(env = prepareMALstack(nmb, nmb->vsize))) { /* needed for result */
    4942           0 :                         ret = createException(MAL, "sql.unionfunc", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    4943           0 :                         goto finalize;
    4944             :                 }
    4945             : 
    4946           8 :                 q = getInstrPtr(nmb, 0);
    4947             : 
    4948          46 :                 for (BUN cur = 0; cur<cnt && !ret; cur++ ) {
    4949          38 :                         MalStkPtr nstk = prepareMALstack(nmb, nmb->vsize);
    4950             :                         int i,ii;
    4951             : 
    4952          38 :                         if (!nstk) { /* needed for result */
    4953           0 :                                 ret = createException(MAL, "sql.unionfunc", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    4954             :                         } else {
    4955             :                                 /* copy (input) arguments onto destination stack, skipping rowid col */
    4956          76 :                                 for (i = 1, ii = q->retc; ii < q->argc && !ret; ii++) {
    4957          38 :                                         ValPtr lhs = &nstk->stk[q->argv[ii]];
    4958          38 :                                         ptr rhs = (ptr)BUNtail(bi[i], cur);
    4959             : 
    4960          38 :                                         assert(lhs->vtype != TYPE_bat);
    4961          38 :                                         if (VALset(lhs, input[i]->ttype, rhs) == NULL)
    4962           0 :                                                 ret = createException(MAL, "sql.unionfunc", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    4963             :                                 }
    4964          38 :                                 if (!ret && ii == q->argc) {
    4965             :                                         BAT *fres = NULL;
    4966          38 :                                         ret = runMALsequence(cntxt, nmb, 1, nmb->stop, nstk, env /* copy result in nstk first instruction*/, q);
    4967             : 
    4968          38 :                                         if (!ret) {
    4969             :                                                 /* insert into result */
    4970          38 :                                                 if (!(fres = BBPquickdesc(env->stk[q->argv[0]].val.bval))) {
    4971           0 :                                                         ret = createException(MAL, "sql.unionfunc", SQLSTATE(HY005) "Cannot access column descriptor");
    4972             :                                                 } else {
    4973          38 :                                                         BAT *p = BATconstant(fres->hseqbase, res[0]->ttype, (ptr)BUNtail(bi[0], cur), BATcount(fres), TRANSIENT);
    4974             : 
    4975          38 :                                                         if (p) {
    4976          38 :                                                                 if (BATappend(res[0], p, NULL, FALSE) != GDK_SUCCEED)
    4977           0 :                                                                         ret = createException(MAL, "sql.unionfunc", GDK_EXCEPTION);
    4978          38 :                                                                 BBPunfix(p->batCacheid);
    4979             :                                                         } else {
    4980           0 :                                                                 ret = createException(MAL, "sql.unionfunc", GDK_EXCEPTION);
    4981             :                                                         }
    4982             :                                                 }
    4983             :                                                 i=1;
    4984          76 :                                                 for (ii = 0; i < pci->retc && !ret; i++) {
    4985             :                                                         BAT *b;
    4986             : 
    4987          38 :                                                         if (!(b = BATdescriptor(env->stk[q->argv[ii]].val.bval)))
    4988           0 :                                                                 ret = createException(MAL, "sql.unionfunc", SQLSTATE(HY005) "Cannot access column descriptor");
    4989          38 :                                                         else if (BATappend(res[i], b, NULL, FALSE) != GDK_SUCCEED)
    4990           0 :                                                                 ret = createException(MAL, "sql.unionfunc", GDK_EXCEPTION);
    4991          38 :                                                         if (b) {
    4992          38 :                                                                 BBPrelease(b->batCacheid); /* release ref from env stack */
    4993          38 :                                                                 BBPunfix(b->batCacheid);   /* free pointer */
    4994             :                                                         }
    4995             :                                                 }
    4996             :                                         }
    4997             :                                 }
    4998          38 :                                 GDKfree(nstk);
    4999             :                         }
    5000             :                 }
    5001           8 : finalize:
    5002           8 :                 GDKfree(env);
    5003           8 :                 if (nmb)
    5004           8 :                         freeMalBlk(nmb);
    5005           8 :                 if (res)
    5006          24 :                         for (int i = 0; i<pci->retc; i++) {
    5007          16 :                                 bat *b = getArgReference_bat(stk, pci, i);
    5008          16 :                                 if (res[i]) {
    5009          16 :                                         *b = res[i]->batCacheid;
    5010          16 :                                         if (ret)
    5011           0 :                                                 BBPunfix(*b);
    5012             :                                         else
    5013          16 :                                                 BBPkeepref(*b);
    5014             :                                 }
    5015             :                         }
    5016           8 :                 GDKfree(res);
    5017           8 :                 if (input)
    5018          24 :                         for (int i = 0; i<nrinput; i++) {
    5019          16 :                                 if (input[i]) {
    5020          16 :                                         bat_iterator_end(&bi[i]);
    5021          16 :                                         BBPunfix(input[i]->batCacheid);
    5022             :                                 }
    5023             :                         }
    5024           8 :                 GDKfree(input);
    5025           8 :                 GDKfree(bi);
    5026             :         }
    5027             :         return ret;
    5028             : }
    5029             : 
    5030             : static str
    5031           1 : do_str_column_vacuum(sql_trans *tr, sql_column *c, int access, char *sname, char *tname, char *cname) {
    5032             :         int res;
    5033             :         BAT* b = NULL;
    5034             :         BAT* bn = NULL;
    5035           1 :         sqlstore *store = tr->store;
    5036             : 
    5037           1 :         if ((b = store->storage_api.bind_col(tr, c, access)) == NULL)
    5038           0 :                 throw(SQL, "do_str_column_vacuum", SQLSTATE(42S22) "storage_api.bind_col failed for %s.%s.%s", sname, tname, cname);
    5039             :         // vacuum only string bats
    5040           1 :         if (ATOMstorage(b->ttype) == TYPE_str) {
    5041             :                 // TODO check for num of updates on the BAT against some threshold
    5042             :                 // and decide whether to proceed
    5043           1 :                 if ((bn = COLcopy(b, b->ttype, true, b->batRole)) == NULL) {
    5044           0 :                         BBPunfix(b->batCacheid);
    5045           0 :                         throw(SQL, "do_str_column_vacuum", SQLSTATE(42S22) "COLcopy failed for %s.%s.%s", sname, tname, cname);
    5046             :                 }
    5047           1 :                 if ((res = (int) store->storage_api.swap_bats(tr, c, bn)) != LOG_OK) {
    5048           0 :                         BBPreclaim(bn);
    5049           0 :                         BBPunfix(b->batCacheid);
    5050           0 :                         if (res == LOG_CONFLICT)
    5051           0 :                                 throw(SQL, "do_str_column_vacuum", SQLSTATE(25S01) "TRANSACTION CONFLICT in storage_api.swap_bats %s.%s.%s", sname, tname, cname);
    5052           0 :                         if (res == LOG_ERR)
    5053           0 :                                 throw(SQL, "do_str_column_vacuum", SQLSTATE(HY000) "LOG ERROR in storage_api.swap_bats %s.%s.%s", sname, tname, cname);
    5054           0 :                         throw(SQL, "do_str_column_vacuum", SQLSTATE(HY000) "ERROR in storage_api.swap_bats %s.%s.%s", sname, tname, cname);
    5055             :                 }
    5056             :         }
    5057           1 :         BBPunfix(b->batCacheid);
    5058           1 :         if (bn)
    5059           1 :                 BBPunfix(bn->batCacheid);
    5060             :         return MAL_SUCCEED;
    5061             : }
    5062             : 
    5063             : str
    5064           1 : SQLstr_column_vacuum(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    5065             : {
    5066           1 :         mvc *m = NULL;
    5067             :         str msg = NULL;
    5068             :         int access = 0;
    5069           1 :         char *sname = *getArgReference_str(stk, pci, 1);
    5070           1 :         char *tname = *getArgReference_str(stk, pci, 2);
    5071           1 :         char *cname = *getArgReference_str(stk, pci, 3);
    5072             : 
    5073           1 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    5074             :                 return msg;
    5075           1 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    5076             :                 return msg;
    5077             : 
    5078           1 :         sql_trans *tr = m->session->tr;
    5079             :         sql_schema *s = NULL;
    5080             :         sql_table *t = NULL;
    5081             :         sql_column *c = NULL;
    5082             : 
    5083           1 :         if ((s = mvc_bind_schema(m, sname)) == NULL)
    5084           0 :                 throw(SQL, "sql.str_column_vacuum", SQLSTATE(3F000) "Invalid or missing schema %s",sname);
    5085           1 :         if ((t = mvc_bind_table(m, s, tname)) == NULL)
    5086           0 :                 throw(SQL, "sql.str_column_vacuum", SQLSTATE(42S02) "Invalid or missing table %s.%s",sname,tname);
    5087           1 :         if (!isTable(t))
    5088           0 :                 throw(SQL, "sql.str_column_vacuum", SQLSTATE(42000) "%s '%s' is not persistent",
    5089           0 :                           TABLE_TYPE_DESCRIPTION(t->type, t->properties), t->base.name);
    5090           1 :         if ((c = mvc_bind_column(m, t, cname)) == NULL)
    5091           0 :                 throw(SQL, "sql.str_column_vacuum", SQLSTATE(42S22) "Column not found %s.%s",sname,tname);
    5092             : 
    5093           1 :         return do_str_column_vacuum(tr, c, access, sname, tname, cname);
    5094             : }
    5095             : 
    5096             : 
    5097             : static gdk_return
    5098           0 : str_column_vacuum_callback(int argc, void *argv[]) {
    5099           0 :         sqlstore *store = (sqlstore *) argv[0];
    5100           0 :         char *sname = (char *) argv[1];
    5101           0 :         char *tname = (char *) argv[2];
    5102           0 :         char *cname = (char *) argv[3];
    5103             :         sql_allocator *sa = NULL;
    5104             :         sql_session *session = NULL;
    5105             :         sql_schema *s = NULL;
    5106             :         sql_table *t = NULL;
    5107             :         sql_column *c = NULL;
    5108             :         int access = 0;
    5109             :         char *msg;
    5110             :         gdk_return res = GDK_SUCCEED;
    5111             : 
    5112             :         (void) argc;
    5113             : 
    5114           0 :         if ((sa = sa_create(NULL)) == NULL) {
    5115           0 :                 TRC_ERROR((component_t) SQL, "[str_column_vacuum_callback] -- Failed to create sql_allocator!");
    5116           0 :                 return GDK_FAIL;
    5117             :         }
    5118             : 
    5119           0 :         if ((session = sql_session_create(store, sa, 0)) == NULL) {
    5120           0 :                 TRC_ERROR((component_t) SQL, "[str_column_vacuum_callback] -- Failed to create session!");
    5121           0 :                 sa_destroy(sa);
    5122           0 :                 return GDK_FAIL;
    5123             :         }
    5124             : 
    5125           0 :         if (sql_trans_begin(session) < 0) {
    5126           0 :                 TRC_ERROR((component_t) SQL, "[str_column_vacuum_callback] -- Failed to begin transaction!");
    5127           0 :                 sql_session_destroy(session);
    5128           0 :                 sa_destroy(sa);
    5129           0 :                 return GDK_FAIL;
    5130             :         }
    5131             : 
    5132             :         do {
    5133           0 :                 if((s = find_sql_schema(session->tr, sname)) == NULL) {
    5134           0 :                         TRC_ERROR((component_t) SQL, "[str_column_vacuum_callback] -- Invalid or missing schema %s!",sname);
    5135             :                         res = GDK_FAIL;
    5136           0 :                         break;
    5137             :                 }
    5138             : 
    5139           0 :                 if((t = find_sql_table(session->tr, s, tname)) == NULL) {
    5140           0 :                         TRC_ERROR((component_t) SQL, "[str_column_vacuum_callback] -- Invalid or missing table %s!", tname);
    5141             :                         res = GDK_FAIL;
    5142           0 :                         break;
    5143             :                 }
    5144             : 
    5145           0 :                 if ((c = find_sql_column(t, cname)) == NULL) {
    5146           0 :                         TRC_ERROR((component_t) SQL, "[str_column_vacuum_callback] -- Invalid or missing column %s!", cname);
    5147             :                         res = GDK_FAIL;
    5148           0 :                         break;
    5149             :                 }
    5150             : 
    5151           0 :                 if((msg=do_str_column_vacuum(session->tr, c, access, sname, tname, cname)) != MAL_SUCCEED) {
    5152           0 :                         TRC_ERROR((component_t) SQL, "[str_column_vacuum_callback] -- %s", msg);
    5153             :                         res = GDK_FAIL;
    5154             :                 }
    5155             : 
    5156             :         } while(0);
    5157             : 
    5158           0 :         sql_trans_end(session, SQL_OK);
    5159           0 :         sql_session_destroy(session);
    5160           0 :         sa_destroy(sa);
    5161           0 :         return res;
    5162             : }
    5163             : 
    5164             : static gdk_return
    5165           0 : str_column_vacuum_callback_args_free(int argc, void *argv[])
    5166             : {
    5167             :         (void) argc;
    5168             :         // free up sname, tname, cname. First pointer points to sqlstore so leave it.
    5169           0 :         GDKfree(argv[1]); // sname
    5170           0 :         GDKfree(argv[2]); // tname
    5171           0 :         GDKfree(argv[3]); // cname
    5172           0 :         return GDK_SUCCEED;
    5173             : }
    5174             : 
    5175             : str
    5176           0 : SQLstr_column_auto_vacuum(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    5177             : {
    5178           0 :         mvc *m = NULL;
    5179             :         str msg = NULL;
    5180           0 :         char *sname = *getArgReference_str(stk, pci, 1);
    5181           0 :         char *tname = *getArgReference_str(stk, pci, 2);
    5182           0 :         char *cname = *getArgReference_str(stk, pci, 3);
    5183           0 :         int interval = *getArgReference_int(stk, pci, 4); // in sec
    5184             :         char *sname_copy = NULL, *tname_copy = NULL, *cname_copy = NULL;
    5185             : 
    5186           0 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    5187             :                 return msg;
    5188           0 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    5189             :                 return msg;
    5190             : 
    5191             :         sql_schema *s = NULL;
    5192             :         sql_table *t = NULL;
    5193             :         sql_column *c = NULL;
    5194             : 
    5195           0 :         if ((s = mvc_bind_schema(m, sname)) == NULL)
    5196           0 :                 throw(SQL, "sql.str_column_auto_vacuum", SQLSTATE(3F000) "Invalid or missing schema %s",sname);
    5197           0 :         if ((t = mvc_bind_table(m, s, tname)) == NULL)
    5198           0 :                 throw(SQL, "sql.str_column_auto_vacuum", SQLSTATE(42S02) "Invalid or missing table %s.%s",sname,tname);
    5199           0 :         if (!isTable(t))
    5200           0 :                 throw(SQL, "sql.str_column_auto_vacuum", SQLSTATE(42000) "%s '%s' is not persistent",
    5201           0 :                           TABLE_TYPE_DESCRIPTION(t->type, t->properties), t->base.name);
    5202           0 :         if ((c = mvc_bind_column(m, t, cname)) == NULL)
    5203           0 :                 throw(SQL, "sql.str_column_auto_vacuum", SQLSTATE(42S22) "Column not found %s.%s",sname,tname);
    5204             : 
    5205           0 :         if (!(sname_copy = GDKstrdup(sname)) || !(tname_copy = GDKstrdup(tname)) || !(cname_copy = GDKstrdup(cname))) {
    5206           0 :                 GDKfree(sname_copy);
    5207           0 :                 GDKfree(tname_copy);
    5208           0 :                 GDKfree(cname_copy);
    5209           0 :                 throw(SQL, "sql.str_column_auto_vacuum", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    5210             :         }
    5211           0 :         void *argv[4] = {m->store, sname_copy, tname_copy, cname_copy};
    5212             : 
    5213             :         gdk_return res;
    5214           0 :         if((res = gdk_add_callback("str_column_vacuum", str_column_vacuum_callback, 4, argv, interval)) != GDK_SUCCEED) {
    5215           0 :                 str_column_vacuum_callback_args_free(4, argv);
    5216           0 :                 throw(SQL, "sql.str_column_auto_vacuum", "adding vacuum callback failed!");
    5217             :         }
    5218             : 
    5219             :         return MAL_SUCCEED;
    5220             : }
    5221             : 
    5222             : str
    5223           0 : SQLstr_column_stop_vacuum(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    5224             : {
    5225           0 :         mvc *m = NULL;
    5226             :         str msg = NULL;
    5227           0 :         char *sname = *getArgReference_str(stk, pci, 1);
    5228           0 :         char *tname = *getArgReference_str(stk, pci, 2);
    5229           0 :         char *cname = *getArgReference_str(stk, pci, 3);
    5230             : 
    5231           0 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    5232             :                 return msg;
    5233           0 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    5234             :                 return msg;
    5235             : 
    5236             :         sql_schema *s = NULL;
    5237             :         sql_table *t = NULL;
    5238             :         sql_column *c = NULL;
    5239             : 
    5240           0 :         if ((s = mvc_bind_schema(m, sname)) == NULL)
    5241           0 :                 throw(SQL, "sql.str_column_stop_vacuum", SQLSTATE(3F000) "Invalid or missing schema %s",sname);
    5242           0 :         if ((t = mvc_bind_table(m, s, tname)) == NULL)
    5243           0 :                 throw(SQL, "sql.str_column_stop_vacuum", SQLSTATE(42S02) "Invalid or missing table %s.%s",sname,tname);
    5244           0 :         if (!isTable(t))
    5245           0 :                 throw(SQL, "sql.str_column_auto_vacuum", SQLSTATE(42000) "%s '%s' is not persistent",
    5246           0 :                           TABLE_TYPE_DESCRIPTION(t->type, t->properties), t->base.name);
    5247           0 :         if ((c = mvc_bind_column(m, t, cname)) == NULL)
    5248           0 :                 throw(SQL, "sql.str_column_stop_vacuum", SQLSTATE(42S22) "Column not found %s.%s",sname,tname);
    5249             : 
    5250           0 :         if(gdk_remove_callback("str_column_vacuum", str_column_vacuum_callback_args_free) != GDK_SUCCEED)
    5251           0 :                 throw(SQL, "sql.str_column_stop_vacuum", "removing vacuum callback failed!");
    5252             : 
    5253             :         return MAL_SUCCEED;
    5254             : }
    5255             : 
    5256             : 
    5257             : #include "wlr.h"
    5258             : #include "sql_cat.h"
    5259             : #include "sql_rank.h"
    5260             : #include "sql_user.h"
    5261             : #include "sql_assert.h"
    5262             : #include "sql_execute.h"
    5263             : #include "sql_orderidx.h"
    5264             : #include "sql_subquery.h"
    5265             : #include "sql_statistics.h"
    5266             : #include "sql_transaction.h"
    5267             : #include "mel.h"
    5268             : static mel_func sql_init_funcs[] = {
    5269             :  pattern("sql", "shutdown", SQLshutdown_wrap, false, "", args(1,3, arg("",str),arg("delay",bte),arg("force",bit))),
    5270             :  pattern("sql", "shutdown", SQLshutdown_wrap, false, "", args(1,3, arg("",str),arg("delay",sht),arg("force",bit))),
    5271             :  pattern("sql", "shutdown", SQLshutdown_wrap, false, "", args(1,3, arg("",str),arg("delay",int),arg("force",bit))),
    5272             :  pattern("sql", "shutdown", SQLshutdown_wrap, false, "", args(1,2, arg("",str),arg("delay",bte))),
    5273             :  pattern("sql", "shutdown", SQLshutdown_wrap, false, "", args(1,2, arg("",str),arg("delay",sht))),
    5274             :  pattern("sql", "shutdown", SQLshutdown_wrap, false, "", args(1,2, arg("",str),arg("delay",int))),
    5275             :  pattern("sql", "set_protocol", SQLset_protocol, true, "Configures the result set protocol", args(1,2, arg("",int), arg("protocol",int))),
    5276             :  pattern("sql", "mvc", SQLmvc, false, "Get the multiversion catalog context. \nNeeded for correct statement dependencies\n(ie sql.update, should be after sql.bind in concurrent execution)", args(1,1, arg("",int))),
    5277             :  pattern("sql", "transaction", SQLtransaction2, true, "Start an autocommit transaction", noargs),
    5278             :  pattern("sql", "commit", SQLcommit, true, "Trigger the commit operation for a MAL block", noargs),
    5279             :  pattern("sql", "abort", SQLabort, true, "Trigger the abort operation for a MAL block", noargs),
    5280             :  pattern("sql", "eval", SQLstatement, false, "Compile and execute a single sql statement", args(1,2, arg("",void),arg("cmd",str))),
    5281             :  pattern("sql", "eval", SQLstatement, false, "Compile and execute a single sql statement (and optionaly set the output to columnar format)", args(1,3, arg("",void),arg("cmd",str),arg("columnar",bit))),
    5282             :  pattern("sql", "include", SQLinclude, false, "Compile and execute a sql statements on the file", args(1,2, arg("",void),arg("fname",str))),
    5283             :  pattern("sql", "evalAlgebra", RAstatement, false, "Compile and execute a single 'relational algebra' statement", args(1,3, arg("",void),arg("cmd",str),arg("optimize",bit))),
    5284             :  pattern("sql", "register", RAstatement2, false, "", args(1,5, arg("",int),arg("mod",str),arg("fname",str),arg("rel_stmt",str),arg("sig",str))),
    5285             :  pattern("sql", "register", RAstatement2, false, "Compile the relational statement (rel_smt) and register it as mal function, mod.fname(signature)", args(1,6, arg("",int),arg("mod",str),arg("fname",str),arg("rel_stmt",str),arg("sig",str),arg("typ",str))),
    5286             :  pattern("sql", "deregister", RAstatementEnd, false, "Finish running transaction", args(1,1, arg("",int))),
    5287             :  pattern("sql", "hot_snapshot", SQLhot_snapshot, true, "Write db snapshot to the given tar(.gz) file", args(1,2, arg("",void),arg("tarfile",str))),
    5288             :  pattern("sql", "resume_log_flushing", SQLresume_log_flushing, true, "Resume WAL log flushing", args(1,1, arg("",void))),
    5289             :  pattern("sql", "suspend_log_flushing", SQLsuspend_log_flushing, true, "Suspend WAL log flushing", args(1,1, arg("",void))),
    5290             :  pattern("sql", "hot_snapshot", SQLhot_snapshot_wrap, true, "Write db snapshot to the given tar(.gz/.lz4/.bz/.xz) file on either server or client", args(1,3, arg("",void),arg("tarfile", str),arg("onserver",bit))),
    5291             :  pattern("sql", "assert", SQLassert, false, "Generate an exception when b==true", args(1,3, arg("",void),arg("b",bit),arg("msg",str))),
    5292             :  pattern("sql", "assert", SQLassertInt, false, "Generate an exception when b!=0", args(1,3, arg("",void),arg("b",int),arg("msg",str))),
    5293             :  pattern("sql", "assert", SQLassertLng, false, "Generate an exception when b!=0", args(1,3, arg("",void),arg("b",lng),arg("msg",str))),
    5294             :  pattern("sql", "setVariable", setVariable, true, "Set the value of a session variable", args(1,5, arg("",int),arg("mvc",int),arg("sname",str),arg("varname",str),argany("value",1))),
    5295             :  pattern("sql", "getVariable", getVariable, false, "Get the value of a session variable", args(1,4, argany("",1),arg("mvc",int),arg("sname",str),arg("varname",str))),
    5296             :  pattern("sql", "logfile", mvc_logfile, true, "Enable/disable saving the sql statement traces", args(1,2, arg("",void),arg("filename",str))),
    5297             :  pattern("sql", "next_value", mvc_next_value, false, "return the next value of the sequence", args(1,3, arg("",lng),arg("sname",str),arg("sequence",str))),
    5298             :  pattern("batsql", "next_value", mvc_bat_next_value, false, "return the next value of the sequence", args(1,3, batarg("",lng),batarg("sname",str),arg("sequence",str))),
    5299             :  pattern("batsql", "next_value", mvc_bat_next_value, false, "return the next value of sequences", args(1,3, batarg("",lng),arg("sname",str),batarg("sequence",str))),
    5300             :  pattern("batsql", "next_value", mvc_bat_next_value, false, "return the next value of sequences", args(1,3, batarg("",lng),batarg("sname",str),batarg("sequence",str))),
    5301             :  pattern("sql", "get_value", mvc_get_value, false, "return the current value of the sequence", args(1,3, arg("",lng),arg("sname",str),arg("sequence",str))),
    5302             :  pattern("batsql", "get_value", mvc_bat_get_value, false, "return the current value of the sequence", args(1,3, batarg("",lng),batarg("sname",str),arg("sequence",str))),
    5303             :  pattern("batsql", "get_value", mvc_bat_get_value, false, "return the current value of sequences", args(1,3, batarg("",lng),arg("sname",str),batarg("sequence",str))),
    5304             :  pattern("batsql", "get_value", mvc_bat_get_value, false, "return the current value of sequences", args(1,3, batarg("",lng),batarg("sname",str),batarg("sequence",str))),
    5305             :  pattern("sql", "restart", mvc_restart_seq, true, "restart the sequence with value start", args(1,4, arg("",lng),arg("sname",str),arg("sequence",str),arg("start",lng))),
    5306             :  pattern("batsql", "restart", mvc_bat_restart_seq, true, "restart the sequence with value start", args(1,4, batarg("",lng),batarg("sname",str),arg("sequence",str),arg("start",lng))),
    5307             :  pattern("batsql", "restart", mvc_bat_restart_seq, true, "restart the sequence with value start", args(1,4, batarg("",lng),arg("sname",str),batarg("sequence",str),arg("start",lng))),
    5308             :  pattern("batsql", "restart", mvc_bat_restart_seq, true, "restart the sequence with value start", args(1,4, batarg("",lng),arg("sname",str),arg("sequence",str),batarg("start",lng))),
    5309             :  pattern("batsql", "restart", mvc_bat_restart_seq, true, "restart the sequence with value start", args(1,4, batarg("",lng),batarg("sname",str),batarg("sequence",str),arg("start",lng))),
    5310             :  pattern("batsql", "restart", mvc_bat_restart_seq, true, "restart the sequence with value start", args(1,4, batarg("",lng),batarg("sname",str),arg("sequence",str),batarg("start",lng))),
    5311             :  pattern("batsql", "restart", mvc_bat_restart_seq, true, "restart the sequence with value start", args(1,4, batarg("",lng),arg("sname",str),batarg("sequence",str),batarg("start",lng))),
    5312             :  pattern("batsql", "restart", mvc_bat_restart_seq, true, "restart the sequence with value start", args(1,4, batarg("",lng),batarg("sname",str),batarg("sequence",str),batarg("start",lng))),
    5313             :  pattern("sql", "deltas", mvc_delta_values, false, "Return the delta values sizes of all columns of the schema's tables, plus the current transaction level", args(7,8, batarg("ids",int),batarg("segments",lng),batarg("all",lng),batarg("inserted",lng),batarg("updated",lng),batarg("deleted",lng),batarg("tr_level",int),arg("schema",str))),
    5314             :  pattern("sql", "deltas", mvc_delta_values, false, "Return the delta values sizes from the table's columns, plus the current transaction level", args(7,9, batarg("ids",int),batarg("segments",lng),batarg("all",lng),batarg("inserted",lng),batarg("updated",lng),batarg("deleted",lng),batarg("tr_level",int),arg("schema",str),arg("table",str))),
    5315             :  pattern("sql", "deltas", mvc_delta_values, false, "Return the delta values sizes of a column, plus the current transaction level", args(7,10, batarg("ids",int),batarg("segments",lng),batarg("all",lng),batarg("inserted",lng),batarg("updated",lng),batarg("deleted",lng),batarg("tr_level",int),arg("schema",str),arg("table",str),arg("column",str))),
    5316             :  pattern("sql", "emptybindidx", mvc_bind_idxbat_wrap, false, "", args(1,6, batargany("",1),arg("mvc",int),arg("schema",str),arg("table",str),arg("index",str),arg("access",int))),
    5317             :  pattern("sql", "bind_idxbat", mvc_bind_idxbat_wrap, false, "Bind the 'schema.table.index' BAT with access kind:\n0 - base table\n1 - inserts\n2 - updates", args(1,6, batargany("",1),arg("mvc",int),arg("schema",str),arg("table",str),arg("index",str),arg("access",int))),
    5318             :  pattern("sql", "emptybindidx", mvc_bind_idxbat_wrap, false, "", args(2,7, batarg("uid",oid),batargany("uval",1),arg("mvc",int),arg("schema",str),arg("table",str),arg("index",str),arg("access",int))),
    5319             :  pattern("sql", "bind_idxbat", mvc_bind_idxbat_wrap, false, "Bind the 'schema.table.index' BAT with access kind:\n0 - base table\n1 - inserts\n2 - updates", args(2,7, batarg("uid",oid),batargany("uval",1),arg("mvc",int),arg("schema",str),arg("table",str),arg("index",str),arg("access",int))),
    5320             :  pattern("sql", "emptybindidx", mvc_bind_idxbat_wrap, false, "", args(1,8, batargany("",1),arg("mvc",int),arg("schema",str),arg("table",str),arg("index",str),arg("access",int),arg("part_nr",int),arg("nr_parts",int))),
    5321             :  pattern("sql", "bind_idxbat", mvc_bind_idxbat_wrap, false, "Bind the 'schema.table.index' BAT with access kind:\n0 - base table\n1 - inserts\n2 - updates", args(1,8, batargany("",1),arg("mvc",int),arg("schema",str),arg("table",str),arg("index",str),arg("access",int),arg("part_nr",int),arg("nr_parts",int))),
    5322             :  pattern("sql", "emptybindidx", mvc_bind_idxbat_wrap, false, "", args(2,9, batarg("uid",oid),batargany("uval",1),arg("mvc",int),arg("schema",str),arg("table",str),arg("index",str),arg("access",int),arg("part_nr",int),arg("nr_parts",int))),
    5323             :  pattern("sql", "bind_idxbat", mvc_bind_idxbat_wrap, false, "Bind the 'schema.table.index' BAT with access kind:\n0 - base table\n1 - inserts\n2 - updates", args(2,9, batarg("uid",oid),batargany("uval",1),arg("mvc",int),arg("schema",str),arg("table",str),arg("index",str),arg("access",int),arg("part_nr",int),arg("nr_parts",int))),
    5324             :  pattern("sql", "emptybind", mvc_bind_wrap, false, "", args(1,6, batargany("",1),arg("mvc",int),arg("schema",str),arg("table",str),arg("column",str),arg("access",int))),
    5325             :  pattern("sql", "bind", mvc_bind_wrap, false, "Bind the 'schema.table.column' BAT with access kind:\n0 - base table\n1 - inserts\n2 - updates", args(1,6, batargany("",1),arg("mvc",int),arg("schema",str),arg("table",str),arg("column",str),arg("access",int))),
    5326             :  pattern("sql", "emptybind", mvc_bind_wrap, false, "", args(2,7, batarg("uid",oid),batargany("uval",1),arg("mvc",int),arg("schema",str),arg("table",str),arg("column",str),arg("access",int))),
    5327             :  pattern("sql", "bind", mvc_bind_wrap, false, "Bind the 'schema.table.column' BAT with access kind:\n0 - base table\n1 - inserts\n2 - updates", args(2,7, batarg("uid",oid),batargany("uval",1),arg("mvc",int),arg("schema",str),arg("table",str),arg("column",str),arg("access",int))),
    5328             :  pattern("sql", "emptybind", mvc_bind_wrap, false, "", args(1,8, batargany("",1),arg("mvc",int),arg("schema",str),arg("table",str),arg("column",str),arg("access",int),arg("part_nr",int),arg("nr_parts",int))),
    5329             :  pattern("sql", "bind", mvc_bind_wrap, false, "Bind the 'schema.table.column' BAT partition with access kind:\n0 - base table\n1 - inserts\n2 - updates", args(1,8, batargany("",1),arg("mvc",int),arg("schema",str),arg("table",str),arg("column",str),arg("access",int),arg("part_nr",int),arg("nr_parts",int))),
    5330             :  pattern("sql", "emptybind", mvc_bind_wrap, false, "", args(2,9, batarg("uid",oid),batargany("uval",1),arg("mvc",int),arg("schema",str),arg("table",str),arg("column",str),arg("access",int),arg("part_nr",int),arg("nr_parts",int))),
    5331             :  pattern("sql", "bind", mvc_bind_wrap, false, "Bind the 'schema.table.column' BAT with access kind:\n0 - base table\n1 - inserts\n2 - updates", args(2,9, batarg("uid",oid),batargany("uval",1),arg("mvc",int),arg("schema",str),arg("table",str),arg("column",str),arg("access",int),arg("part_nr",int),arg("nr_parts",int))),
    5332             :  command("sql", "delta", DELTAbat, false, "Return column bat with delta's applied.", args(1,4, batargany("",3),batargany("col",3),batarg("uid",oid),batargany("uval",3))),
    5333             :  command("sql", "projectdelta", DELTAproject, false, "Return column bat with delta's applied.", args(1,5, batargany("",3),batarg("select",oid),batargany("col",3),batarg("uid",oid),batargany("uval",3))),
    5334             :  command("sql", "subdelta", DELTAsub, false, "Return a single bat of selected delta.", args(1,5, batarg("",oid),batarg("col",oid),batarg("cand",oid),batarg("uid",oid),batarg("uval",oid))),
    5335             :  command("sql", "project", BATleftproject, false, "Last step of a left outer join, ie project the inner join (l,r) over the left input side (col)", args(1,4, batarg("",oid),batarg("col",oid),batarg("l",oid),batarg("r",oid))),
    5336             :  command("sql", "getVersion", mvc_getVersion, false, "Return the database version identifier for a client.", args(1,2, arg("",lng),arg("clientid",int))),
    5337             :  pattern("sql", "grow", mvc_grow_wrap, false, "Resize the tid column of a declared table.", args(1,3, arg("",int),batarg("tid",oid),argany("",1))),
    5338             :  pattern("sql", "claim", mvc_claim_wrap, true, "Claims slots for appending rows.", args(2,6, arg("",oid),batarg("",oid),arg("mvc",int),arg("sname",str),arg("tname",str),arg("cnt",lng))),
    5339             :  pattern("sql", "append", mvc_append_wrap, false, "Append to the column tname.cname (possibly optimized to replace the insert bat of tname.cname. Returns sequence number for order dependence.", args(1,8, arg("",int), arg("mvc",int),arg("sname",str),arg("tname",str),arg("cname",str),arg("offset",oid),batarg("pos",oid),argany("ins",0))),
    5340             :  pattern("sql", "update", mvc_update_wrap, false, "Update the values of the column tname.cname. Returns sequence number for order dependence)", args(1,7, arg("",int), arg("mvc",int),arg("sname",str),arg("tname",str),arg("cname",str),argany("rids",0),argany("upd",0))), pattern("sql", "clear_table", mvc_clear_table_wrap, true, "Clear the table sname.tname.", args(1,3, arg("",lng),arg("sname",str),arg("tname",str))),
    5341             :  pattern("sql", "tid", SQLtid, false, "Return a column with the valid tuple identifiers associated with the table sname.tname.", args(1,4, batarg("",oid),arg("mvc",int),arg("sname",str),arg("tname",str))),
    5342             :  pattern("sql", "tid", SQLtid, false, "Return the tables tid column.", args(1,6, batarg("",oid),arg("mvc",int),arg("sname",str),arg("tname",str),arg("part_nr",int),arg("nr_parts",int))),
    5343             :  pattern("sql", "delete", mvc_delete_wrap, true, "Delete a row from a table. Returns sequence number for order dependence.", args(1,5, arg("",int),arg("mvc",int),arg("sname",str),arg("tname",str),argany("b",0))),
    5344             :  pattern("sql", "resultSet", mvc_scalar_value_wrap, true, "Prepare a table result set for the client front-end.", args(1,8, arg("",int),arg("tbl",str),arg("attr",str),arg("tpe",str),arg("len",int),arg("scale",int),arg("eclass",int),argany("val",0))),
    5345             :  pattern("sql", "resultSet", mvc_row_result_wrap, true, "Prepare a table result set for the client front-end", args(1,7, arg("",int),batarg("tbl",str),batarg("attr",str),batarg("tpe",str),batarg("len",int),batarg("scale",int),varargany("cols",0))),
    5346             :  pattern("sql", "resultSet", mvc_table_result_wrap, true, "Prepare a table result set for the client in default CSV format", args(1,7, arg("",int),batarg("tbl",str),batarg("attr",str),batarg("tpe",str),batarg("len",int),batarg("scale",int),batvarargany("cols",0))),
    5347             :  pattern("sql", "export_table", mvc_export_row_wrap, true, "Prepare a table result set for the COPY INTO stream", args(1,14, arg("",int),arg("fname",str),arg("fmt",str),arg("colsep",str),arg("recsep",str),arg("qout",str),arg("nullrep",str),arg("onclient",int),batarg("tbl",str),batarg("attr",str),batarg("tpe",str),batarg("len",int),batarg("scale",int),varargany("cols",0))),
    5348             :  pattern("sql", "export_table", mvc_export_table_wrap, true, "Prepare a table result set for the COPY INTO stream", args(1,14, arg("",int),arg("fname",str),arg("fmt",str),arg("colsep",str),arg("recsep",str),arg("qout",str),arg("nullrep",str),arg("onclient",int),batarg("tbl",str),batarg("attr",str),batarg("tpe",str),batarg("len",int),batarg("scale",int),batvarargany("cols",0))),
    5349             :  pattern("sql", "exportHead", mvc_export_head_wrap, true, "Export a result (in order) to stream s", args(1,3, arg("",void),arg("s",streams),arg("res_id",int))),
    5350             :  pattern("sql", "exportResult", mvc_export_result_wrap, true, "Export a result (in order) to stream s", args(1,3, arg("",void),arg("s",streams),arg("res_id",int))),
    5351             :  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))),
    5352             :  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))),
    5353             :  pattern("sql", "exportOperation", mvc_export_operation_wrap, true, "Export result of schema/transaction queries", args(1,1, arg("",void))),
    5354             :  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))),
    5355             :  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))),
    5356             :  //we use bat.single now
    5357             :  //pattern("sql", "single", CMDBATsingle, false, "", args(1,2, batargany("",2),argany("x",2))),
    5358             :  pattern("sql", "importTable", mvc_bin_import_table_wrap, true, "Import a table from the files (fname)", args(1,6, batvarargany("",0),arg("sname",str),arg("tname",str),arg("onclient",int),arg("bswap",bit),vararg("fname",str))),
    5359             :  pattern("sql", "importColumn", mvc_bin_import_column_wrap, false, "Import a column from the given file", args(2, 7, batargany("", 0),arg("", oid), arg("method",str),arg("bswap",bit),arg("path",str),arg("onclient",int),arg("nrows",oid))),
    5360             :  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))),
    5361             :  command("sql", "optimizers", getPipeCatalog, false, "", args(3,3, batarg("",str),batarg("",str),batarg("",str))),
    5362             :  pattern("sql", "optimizer_updates", SQLoptimizersUpdate, false, "", noargs),
    5363             :  pattern("sql", "argRecord", SQLargRecord, false, "Glue together the calling sequence", args(1,1, arg("",str))),
    5364             :  pattern("sql", "argRecord", SQLargRecord, false, "Glue together the calling sequence", args(1,2, arg("",str),varargany("a",0))),
    5365             :  pattern("sql", "sql_variables", sql_variables, false, "return the table with session variables", args(4,4, batarg("sname",str),batarg("name",str),batarg("type",str),batarg("value",str))),
    5366             :  pattern("sql", "sessions", sql_sessions_wrap, false, "SQL export table of active sessions, their timeouts and idle status", args(9,9, batarg("id",int),batarg("user",str),batarg("start",timestamp),batarg("idle",timestamp),batarg("optmizer",str),batarg("stimeout",int),batarg("qtimeout",int),batarg("wlimit",int),batarg("mlimit",int))),
    5367             :  pattern("sql", "db_users", db_users_wrap, false, "return table of users with sql scenario", args(1,1, batarg("",str))),
    5368             :  pattern("sql", "password", db_password_wrap, false, "Return password hash of user", args(1,2, arg("",str),arg("user",str))),
    5369             :  pattern("batsql", "password", db_password_wrap, false, "Return password hash of user", args(1,2, batarg("",str),batarg("user",str))),
    5370             :  pattern("sql", "rt_credentials", sql_rt_credentials_wrap, false, "Return the remote table credentials for the given table", args(3,4, batarg("uri",str),batarg("username",str),batarg("hash",str),arg("tablename",str))),
    5371             :  pattern("sql", "dump_cache", dump_cache, false, "dump the content of the query cache", args(2,2, batarg("query",str),batarg("count",int))),
    5372             :  pattern("sql", "dump_opt_stats", dump_opt_stats, false, "dump the optimizer rewrite statistics", args(2,2, batarg("rewrite",str),batarg("count",int))),
    5373             :  pattern("sql", "dump_trace", dump_trace, false, "dump the trace statistics", args(3,3, batarg("ticks",lng),batarg("stmt",str),batarg("stmt",str))),
    5374             :  pattern("sql", "analyze", sql_analyze, true, "", args(1,3, arg("",void),arg("minmax",int),arg("sample",lng))),
    5375             :  pattern("sql", "analyze", sql_analyze, true, "", args(1,4, arg("",void),arg("minmax",int),arg("sample",lng),arg("sch",str))),
    5376             :  pattern("sql", "analyze", sql_analyze, true, "", args(1,5, arg("",void),arg("minmax",int),arg("sample",lng),arg("sch",str),arg("tbl",str))),
    5377             :  pattern("sql", "analyze", sql_analyze, true, "Update the database statistics table", args(1,6, arg("",void),arg("minmax",int),arg("sample",lng),arg("sch",str),arg("tbl",str),arg("col",str))),
    5378             :  pattern("sql", "storage", sql_storage, false, "return a table with storage information ", args(17,17, batarg("schema",str),batarg("table",str),batarg("column",str),batarg("type",str),batarg("mode",str),batarg("location",str),batarg("count",lng),batarg("atomwidth",int),batarg("columnsize",lng),batarg("heap",lng),batarg("hashes",lng),batarg("phash",bit),batarg("imprints",lng),batarg("sorted",bit),batarg("revsorted",bit),batarg("key",bit),batarg("orderidx",lng))),
    5379             :  pattern("sql", "storage", sql_storage, false, "return a table with storage information for a particular schema ", args(17,18, batarg("schema",str),batarg("table",str),batarg("column",str),batarg("type",str),batarg("mode",str),batarg("location",str),batarg("count",lng),batarg("atomwidth",int),batarg("columnsize",lng),batarg("heap",lng),batarg("hashes",lng),batarg("phash",bit),batarg("imprints",lng),batarg("sorted",bit),batarg("revsorted",bit),batarg("key",bit),batarg("orderidx",lng),arg("sname",str))),
    5380             :  pattern("sql", "storage", sql_storage, false, "return a table with storage information for a particular table", args(17,19, batarg("schema",str),batarg("table",str),batarg("column",str),batarg("type",str),batarg("mode",str),batarg("location",str),batarg("count",lng),batarg("atomwidth",int),batarg("columnsize",lng),batarg("heap",lng),batarg("hashes",lng),batarg("phash",bit),batarg("imprints",lng),batarg("sorted",bit),batarg("revsorted",bit),batarg("key",bit),batarg("orderidx",lng),arg("sname",str),arg("tname",str))),
    5381             :  pattern("sql", "storage", sql_storage, false, "return a table with storage information for a particular column", args(17,20, batarg("schema",str),batarg("table",str),batarg("column",str),batarg("type",str),batarg("mode",str),batarg("location",str),batarg("count",lng),batarg("atomwidth",int),batarg("columnsize",lng),batarg("heap",lng),batarg("hashes",lng),batarg("phash",bit),batarg("imprints",lng),batarg("sorted",bit),batarg("revsorted",bit),batarg("key",bit),batarg("orderidx",lng),arg("sname",str),arg("tname",str),arg("cname",str))),
    5382             :  pattern("sql", "createorderindex", sql_createorderindex, true, "Instantiate the order index on a column", args(0,3, arg("sch",str),arg("tbl",str),arg("col",str))),
    5383             :  pattern("sql", "droporderindex", sql_droporderindex, true, "Drop the order index on a column", args(0,3, arg("sch",str),arg("tbl",str),arg("col",str))),
    5384             :  command("calc", "identity", SQLidentity, false, "Returns a unique row identitfier.", args(1,2, arg("",oid),argany("",0))),
    5385             :  command("batcalc", "identity", BATSQLidentity, false, "Returns the unique row identitfiers.", args(1,2, batarg("",oid),batargany("b",0))),
    5386             :  pattern("batcalc", "identity", PBATSQLidentity, false, "Returns the unique row identitfiers.", args(2,4, batarg("resb",oid),arg("ns",oid),batargany("b",0),arg("s",oid))),
    5387             :  pattern("sql", "querylog_catalog", sql_querylog_catalog, false, "Obtain the query log catalog", args(8,8, batarg("id",oid),batarg("user",str),batarg("defined",timestamp),batarg("query",str),batarg("pipe",str),batarg("plan",str),batarg("mal",int),batarg("optimize",lng))),
    5388             :  pattern("sql", "querylog_calls", sql_querylog_calls, false, "Obtain the query log calls", args(9,9, batarg("id",oid),batarg("start",timestamp),batarg("stop",timestamp),batarg("arguments",str),batarg("tuples",lng),batarg("exec",lng),batarg("result",lng),batarg("cpuload",int),batarg("iowait",int))),
    5389             :  pattern("sql", "querylog_empty", sql_querylog_empty, true, "", noargs),
    5390             :  command("sql", "querylog_enable", QLOGenable, true, "", noargs),
    5391             :  command("sql", "querylog_enable", QLOGenableThreshold, true, "", args(0,1, arg("thres",int))),
    5392             :  command("sql", "querylog_disable", QLOGdisable, true, "", noargs),
    5393             :  pattern("sql", "prepared_statements", SQLsession_prepared_statements, false, "Available prepared statements in the current session", args(5,5, batarg("sessionid",int),batarg("user",str),batarg("statementid",int),batarg("statement",str),batarg("created",timestamp))),
    5394             :  pattern("sql", "prepared_statements_args", SQLsession_prepared_statements_args, false, "Available prepared statements' arguments in the current session", args(9,9, batarg("statementid",int),batarg("type",str),batarg("digits",int),batarg("scale",int),batarg("inout",bte),batarg("number",int),batarg("schema",str),batarg("table",str),batarg("column",str))),
    5395             :  pattern("sql", "copy_rejects", COPYrejects, false, "", args(4,4, batarg("rowid",lng),batarg("fldid",int),batarg("msg",str),batarg("inp",str))),
    5396             :  pattern("sql", "copy_rejects_clear", COPYrejects_clear, true, "", noargs),
    5397             :  command("calc", "dec_round", bte_dec_round_wrap, false, "round off the value v to nearests multiple of r", args(1,3, arg("",bte),arg("v",bte),arg("r",bte))),
    5398             :  pattern("batcalc", "dec_round", bte_bat_dec_round_wrap, false, "round off the value v to nearests multiple of r", args(1,3, batarg("",bte),batarg("v",bte),arg("r",bte))),
    5399             :  pattern("batcalc", "dec_round", bte_bat_dec_round_wrap, false, "round off the value v to nearests multiple of r", args(1,4, batarg("",bte),batarg("v",bte),arg("r",bte),batarg("s",oid))),
    5400             :  pattern("batcalc", "dec_round", bte_bat_dec_round_wrap_cst, false, "round off the value v to nearests multiple of r", args(1,3, batarg("",bte),arg("v",bte),batarg("r",bte))),
    5401             :  pattern("batcalc", "dec_round", bte_bat_dec_round_wrap_cst, false, "round off the value v to nearests multiple of r", args(1,4, batarg("",bte),arg("v",bte),batarg("r",bte),batarg("s",oid))),
    5402             :  pattern("batcalc", "dec_round", bte_bat_dec_round_wrap_nocst, false, "round off the value v to nearests multiple of r", args(1,3, batarg("",bte),batarg("v",bte),batarg("r",bte))),
    5403             :  pattern("batcalc", "dec_round", bte_bat_dec_round_wrap_nocst, false, "round off the value v to nearests multiple of r", args(1,5, batarg("",bte),batarg("v",bte),batarg("r",bte),batarg("s1",oid),batarg("s2",oid))),
    5404             :  command("calc", "round", bte_round_wrap, false, "round off the decimal v(d,s) to r digits behind the dot (if r < 0, before the dot)", args(1,5, arg("",bte),arg("v",bte),arg("r",bte),arg("d",int),arg("s",int))),
    5405             :  pattern("batcalc", "round", bte_bat_round_wrap, false, "round off the decimal v(d,s) to r digits behind the dot (if r < 0, before the dot)", args(1,5, batarg("",bte),batarg("v",bte),arg("r",bte),arg("d",int),arg("s",int))),
    5406             :  pattern("batcalc", "round", bte_bat_round_wrap, false, "round off the decimal v(d,s) to r digits behind the dot (if r < 0, before the dot)", args(1,6, batarg("",bte),batarg("v",bte),arg("r",bte),batarg("s",oid),arg("d",int),arg("s",int))),
    5407             :  pattern("batcalc", "round", bte_bat_round_wrap_cst, false, "round off the decimal v(d,s) to r digits behind the dot (if r < 0, before the dot)", args(1,5, batarg("",bte),arg("v",bte),batarg("r",bte),arg("d",int),arg("s",int))),
    5408             :  pattern("batcalc", "round", bte_bat_round_wrap_cst, false, "round off the decimal v(d,s) to r digits behind the dot (if r < 0, before the dot)", args(1,6, batarg("",bte),arg("v",bte),batarg("r",bte),batarg("s",oid),arg("d",int),arg("s",int))),
    5409             :  pattern("batcalc", "round", bte_bat_round_wrap_nocst, false, "round off the decimal v(d,s) to r digits behind the dot (if r < 0, before the dot)", args(1,5, batarg("",bte),batarg("v",bte),batarg("r",bte),arg("d",int),arg("s",int))),
    5410             :  pattern("batcalc", "round", bte_bat_round_wrap_nocst, false, "round off the decimal v(d,s) to r digits behind the dot (if r < 0, before the dot)", args(1,7, batarg("",bte),batarg("v",bte),batarg("r",bte),batarg("s1",oid),batarg("s2",oid),arg("d",int),arg("s",int))),
    5411             :  command("calc", "second_interval", bte_dec2second_interval, false, "cast bte decimal to a second_interval", args(1,5, arg("",lng),arg("sc",int),arg("v",bte),arg("ek",int),arg("sk",int))),
    5412             :  pattern("batcalc", "second_interval", bte_batdec2second_interval, false, "cast bte decimal to a second_interval", args(1,6, batarg("",lng),arg("sc",int),batarg("v",bte),batarg("s",oid),arg("ek",int),arg("sk",int))),
    5413             :  command("calc", "dec_round", sht_dec_round_wrap, false, "round off the value v to nearests multiple of r", args(1,3, arg("",sht),arg("v",sht),arg("r",sht))),
    5414             :  pattern("batcalc", "dec_round", sht_bat_dec_round_wrap, false, "round off the value v to nearests multiple of r", args(1,3, batarg("",sht),batarg("v",sht),arg("r",sht))),
    5415             :  pattern("batcalc", "dec_round", sht_bat_dec_round_wrap, false, "round off the value v to nearests multiple of r", args(1,4, batarg("",sht),batarg("v",sht),arg("r",sht),batarg("s",oid))),
    5416             :  pattern("batcalc", "dec_round", sht_bat_dec_round_wrap_cst, false, "round off the value v to nearests multiple of r", args(1,3, batarg("",sht),arg("v",sht),batarg("r",sht))),
    5417             :  pattern("batcalc", "dec_round", sht_bat_dec_round_wrap_cst, false, "round off the value v to nearests multiple of r", args(1,4, batarg("",sht),arg("v",sht),batarg("r",sht),batarg("s",oid))),
    5418             :  pattern("batcalc", "dec_round", sht_bat_dec_round_wrap_nocst, false, "round off the value v to nearests multiple of r", args(1,3, batarg("",sht),batarg("v",sht),batarg("r",sht))),
    5419             :  pattern("batcalc", "dec_round", sht_bat_dec_round_wrap_nocst, false, "round off the value v to nearests multiple of r", args(1,5, batarg("",sht),batarg("v",sht),batarg("r",sht),batarg("s1",oid),batarg("s2",oid))),
    5420             :  command("calc", "round", sht_round_wrap, false, "round off the decimal v(d,s) to r digits behind the dot (if r < 0, before the dot)", args(1,5, arg("",sht),arg("v",sht),arg("r",bte),arg("d",int),arg("s",int))),
    5421             :  pattern("batcalc", "round", sht_bat_round_wrap, false, "round off the decimal v(d,s) to r digits behind the dot (if r < 0, before the dot)", args(1,5, batarg("",sht),batarg("v",sht),arg("r",bte),arg("d",int),arg("s",int))),
    5422             :  pattern("batcalc", "round", sht_bat_round_wrap, false, "round off the decimal v(d,s) to r digits behind the dot (if r < 0, before the dot)", args(1,6, batarg("",sht),batarg("v",sht),arg("r",bte),batarg("s",oid),arg("d",int),arg("s",int))),
    5423             :  pattern("batcalc", "round", sht_bat_round_wrap_cst, false, "round off the decimal v(d,s) to r digits behind the dot (if r < 0, before the dot)", args(1,5, batarg("",sht),arg("v",sht),batarg("r",bte),arg("d",int),arg("s",int))),
    5424             :  pattern("batcalc", "round", sht_bat_round_wrap_cst, false, "round off the decimal v(d,s) to r digits behind the dot (if r < 0, before the dot)", args(1,6, batarg("",sht),arg("v",sht),batarg("r",bte),batarg("s",oid),arg("d",int),arg("s",int))),
    5425             :  pattern("batcalc", "round", sht_bat_round_wrap_nocst, false, "round off the decimal v(d,s) to r digits behind the dot (if r < 0, before the dot)", args(1,5, batarg("",sht),batarg("v",sht),batarg("r",bte),arg("d",int),arg("s",int))),
    5426             :  pattern("batcalc", "round", sht_bat_round_wrap_nocst, false, "round off the decimal v(d,s) to r digits behind the dot (if r < 0, before the dot)", args(1,7, batarg("",sht),batarg("v",sht),batarg("r",bte),batarg("s1",oid),batarg("s2",oid),arg("d",int),arg("s",int))),
    5427             :  command("calc", "second_interval", sht_dec2second_interval, false, "cast sht decimal to a second_interval", args(1,5, arg("",lng),arg("sc",int),arg("v",sht),arg("ek",int),arg("sk",int))),
    5428             :  pattern("batcalc", "second_interval", sht_batdec2second_interval, false, "cast sht decimal to a second_interval", args(1,6, batarg("",lng),arg("sc",int),batarg("v",sht),batarg("s",oid),arg("ek",int),arg("sk",int))),
    5429             :  command("calc", "dec_round", int_dec_round_wrap, false, "round off the value v to nearests multiple of r", args(1,3, arg("",int),arg("v",int),arg("r",int))),
    5430             :  pattern("batcalc", "dec_round", int_bat_dec_round_wrap, false, "round off the value v to nearests multiple of r", args(1,3, batarg("",int),batarg("v",int),arg("r",int))),
    5431             :  pattern("batcalc", "dec_round", int_bat_dec_round_wrap, false, "round off the value v to nearests multiple of r", args(1,4, batarg("",int),batarg("v",int),arg("r",int),batarg("s",oid))),
    5432             :  pattern("batcalc", "dec_round", int_bat_dec_round_wrap_cst, false, "round off the value v to nearests multiple of r", args(1,3, batarg("",int),arg("v",int),batarg("r",int))),
    5433             :  pattern("batcalc", "dec_round", int_bat_dec_round_wrap_cst, false, "round off the value v to nearests multiple of r", args(1,4, batarg("",int),arg("v",int),batarg("r",int),batarg("s",oid))),
    5434             :  pattern("batcalc", "dec_round", int_bat_dec_round_wrap_nocst, false, "round off the value v to nearests multiple of r", args(1,3, batarg("",int),batarg("v",int),batarg("r",int))),
    5435             :  pattern("batcalc", "dec_round", int_bat_dec_round_wrap_nocst, false, "round off the value v to nearests multiple of r", args(1,5, batarg("",int),batarg("v",int),batarg("r",int),batarg("s1",oid),batarg("s2",oid))),
    5436             :  command("calc", "round", int_round_wrap, false, "round off the decimal v(d,s) to r digits behind the dot (if r < 0, before the dot)", args(1,5, arg("",int),arg("v",int),arg("r",bte),arg("d",int),arg("s",int))),
    5437             :  pattern("batcalc", "round", int_bat_round_wrap, false, "round off the decimal v(d,s) to r digits behind the dot (if r < 0, before the dot)", args(1,5, batarg("",int),batarg("v",int),arg("r",bte),arg("d",int),arg("s",int))),
    5438             :  pattern("batcalc", "round", int_bat_round_wrap, false, "round off the decimal v(d,s) to r digits behind the dot (if r < 0, before the dot)", args(1,6, batarg("",int),batarg("v",int),arg("r",bte),batarg("s",oid),arg("d",int),arg("s",int))),
    5439             :  pattern("batcalc", "round", int_bat_round_wrap_cst, false, "round off the decimal v(d,s) to r digits behind the dot (if r < 0, before the dot)", args(1,5, batarg("",int),arg("v",int),batarg("r",bte),arg("d",int),arg("s",int))),
    5440             :  pattern("batcalc", "round", int_bat_round_wrap_cst, false, "round off the decimal v(d,s) to r digits behind the dot (if r < 0, before the dot)", args(1,6, batarg("",int),arg("v",int),batarg("r",bte),batarg("s",oid),arg("d",int),arg("s",int))),
    5441             :  pattern("batcalc", "round", int_bat_round_wrap_nocst, false, "round off the decimal v(d,s) to r digits behind the dot (if r < 0, before the dot)", args(1,5, batarg("",int),batarg("v",int),batarg("r",bte),arg("d",int),arg("s",int))),
    5442             :  pattern("batcalc", "round", int_bat_round_wrap_nocst, false, "round off the decimal v(d,s) to r digits behind the dot (if r < 0, before the dot)", args(1,7, batarg("",int),batarg("v",int),batarg("r",bte),batarg("s1",oid),batarg("s2",oid),arg("d",int),arg("s",int))),
    5443             :  command("calc", "second_interval", int_dec2second_interval, false, "cast int decimal to a second_interval", args(1,5, arg("",lng),arg("sc",int),arg("v",int),arg("ek",int),arg("sk",int))),
    5444             :  pattern("batcalc", "second_interval", int_batdec2second_interval, false, "cast int decimal to a second_interval", args(1,6, batarg("",lng),arg("sc",int),batarg("v",int),batarg("s",oid),arg("ek",int),arg("sk",int))),
    5445             :  command("calc", "dec_round", lng_dec_round_wrap, false, "round off the value v to nearests multiple of r", args(1,3, arg("",lng),arg("v",lng),arg("r",lng))),
    5446             :  pattern("batcalc", "dec_round", lng_bat_dec_round_wrap, false, "round off the value v to nearests multiple of r", args(1,3, batarg("",lng),batarg("v",lng),arg("r",lng))),
    5447             :  pattern("batcalc", "dec_round", lng_bat_dec_round_wrap, false, "round off the value v to nearests multiple of r", args(1,4, batarg("",lng),batarg("v",lng),arg("r",lng),batarg("s",oid))),
    5448             :  pattern("batcalc", "dec_round", lng_bat_dec_round_wrap_cst, false, "round off the value v to nearests multiple of r", args(1,3, batarg("",lng),arg("v",lng),batarg("r",lng))),
    5449             :  pattern("batcalc", "dec_round", lng_bat_dec_round_wrap_cst, false, "round off the value v to nearests multiple of r", args(1,4, batarg("",lng),arg("v",lng),batarg("r",lng),batarg("s",oid))),
    5450             :  pattern("batcalc", "dec_round", lng_bat_dec_round_wrap_nocst, false, "round off the value v to nearests multiple of r", args(1,3, batarg("",lng),batarg("v",lng),batarg("r",lng))),
    5451             :  pattern("batcalc", "dec_round", lng_bat_dec_round_wrap_nocst, false, "round off the value v to nearests multiple of r", args(1,5, batarg("",lng),batarg("v",lng),batarg("r",lng),batarg("s1",oid),batarg("s2",oid))),
    5452             :  command("calc", "round", lng_round_wrap, false, "round off the decimal v(d,s) to r digits behind the dot (if r < 0, before the dot)", args(1,5, arg("",lng),arg("v",lng),arg("r",bte),arg("d",int),arg("s",int))),
    5453             :  pattern("batcalc", "round", lng_bat_round_wrap, false, "round off the decimal v(d,s) to r digits behind the dot (if r < 0, before the dot)", args(1,5, batarg("",lng),batarg("v",lng),arg("r",bte),arg("d",int),arg("s",int))),
    5454             :  pattern("batcalc", "round", lng_bat_round_wrap, false, "round off the decimal v(d,s) to r digits behind the dot (if r < 0, before the dot)", args(1,6, batarg("",lng),batarg("v",lng),arg("r",bte),batarg("s",oid),arg("d",int),arg("s",int))),
    5455             :  pattern("batcalc", "round", lng_bat_round_wrap_cst, false, "round off the decimal v(d,s) to r digits behind the dot (if r < 0, before the dot)", args(1,5, batarg("",lng),arg("v",lng),batarg("r",bte),arg("d",int),arg("s",int))),
    5456             :  pattern("batcalc", "round", lng_bat_round_wrap_cst, false, "round off the decimal v(d,s) to r digits behind the dot (if r < 0, before the dot)", args(1,6, batarg("",lng),arg("v",lng),batarg("r",bte),batarg("s",oid),arg("d",int),arg("s",int))),
    5457             :  pattern("batcalc", "round", lng_bat_round_wrap_nocst, false, "round off the decimal v(d,s) to r digits behind the dot (if r < 0, before the dot)", args(1,5, batarg("",lng),batarg("v",lng),batarg("r",bte),arg("d",int),arg("s",int))),
    5458             :  pattern("batcalc", "round", lng_bat_round_wrap_nocst, false, "round off the decimal v(d,s) to r digits behind the dot (if r < 0, before the dot)", args(1,7, batarg("",lng),batarg("v",lng),batarg("r",bte),batarg("s1",oid),batarg("s2",oid),arg("d",int),arg("s",int))),
    5459             :  command("calc", "second_interval", lng_dec2second_interval, false, "cast lng decimal to a second_interval", args(1,5, arg("",lng),arg("sc",int),arg("v",lng),arg("ek",int),arg("sk",int))),
    5460             :  pattern("batcalc", "second_interval", lng_batdec2second_interval, false, "cast lng decimal to a second_interval", args(1,6, batarg("",lng),arg("sc",int),batarg("v",lng),batarg("s",oid),arg("ek",int),arg("sk",int))),
    5461             :  command("calc", "dec_round", flt_dec_round_wrap, false, "round off the value v to nearests multiple of r", args(1,3, arg("",flt),arg("v",flt),arg("r",flt))),
    5462             :  pattern("batcalc", "dec_round", flt_bat_dec_round_wrap, false, "round off the value v to nearests multiple of r", args(1,3, batarg("",flt),batarg("v",flt),arg("r",flt))),
    5463             :  pattern("batcalc", "dec_round", flt_bat_dec_round_wrap, false, "round off the value v to nearests multiple of r", args(1,4, batarg("",flt),batarg("v",flt),arg("r",flt),batarg("s",oid))),
    5464             :  pattern("batcalc", "dec_round", flt_bat_dec_round_wrap_cst, false, "round off the value v to nearests multiple of r", args(1,3, batarg("",flt),arg("v",flt),batarg("r",flt))),
    5465             :  pattern("batcalc", "dec_round", flt_bat_dec_round_wrap_cst, false, "round off the value v to nearests multiple of r", args(1,4, batarg("",flt),arg("v",flt),batarg("r",flt),batarg("s",oid))),
    5466             :  pattern("batcalc", "dec_round", flt_bat_dec_round_wrap_nocst, false, "round off the value v to nearests multiple of r", args(1,3, batarg("",flt),batarg("v",flt),batarg("r",flt))),
    5467             :  pattern("batcalc", "dec_round", flt_bat_dec_round_wrap_nocst, false, "round off the value v to nearests multiple of r", args(1,5, batarg("",flt),batarg("v",flt),batarg("r",flt),batarg("s1",oid),batarg("s2",oid))),
    5468             :  command("calc", "round", flt_round_wrap, false, "round off the floating point v to r digits behind the dot (if r < 0, before the dot)", args(1,3, arg("",flt),arg("v",flt),arg("r",bte))),
    5469             :  pattern("batcalc", "round", flt_bat_round_wrap, false, "round off the floating point v to r digits behind the dot (if r < 0, before the dot)", args(1,3, batarg("",flt),batarg("v",flt),arg("r",bte))),
    5470             :  pattern("batcalc", "round", flt_bat_round_wrap, false, "round off the floating point v to r digits behind the dot (if r < 0, before the dot)", args(1,4, batarg("",flt),batarg("v",flt),arg("r",bte),batarg("s",oid))),
    5471             :  pattern("batcalc", "round", flt_bat_round_wrap_cst, false, "round off the floating point v to r digits behind the dot (if r < 0, before the dot)", args(1,3, batarg("",flt),arg("v",flt),batarg("r",bte))),
    5472             :  pattern("batcalc", "round", flt_bat_round_wrap_cst, false, "round off the floating point v to r digits behind the dot (if r < 0, before the dot)", args(1,4, batarg("",flt),arg("v",flt),batarg("r",bte),batarg("s",oid))),
    5473             :  pattern("batcalc", "round", flt_bat_round_wrap_nocst, false, "round off the floating point v to r digits behind the dot (if r < 0, before the dot)", args(1,3, batarg("",flt),batarg("v",flt),batarg("r",bte))),
    5474             :  pattern("batcalc", "round", flt_bat_round_wrap_nocst, false, "round off the floating point v to r digits behind the dot (if r < 0, before the dot)", args(1,5, batarg("",flt),batarg("v",flt),batarg("r",bte),batarg("s1",oid),batarg("s2",oid))),
    5475             :  command("sql", "ms_trunc", flt_trunc_wrap, false, "truncate the floating point v to r digits behind the dot (if r < 0, before the dot)", args(1,3, arg("",flt),arg("v",flt),arg("r",int))),
    5476             :  command("calc", "dec_round", dbl_dec_round_wrap, false, "round off the value v to nearests multiple of r", args(1,3, arg("",dbl),arg("v",dbl),arg("r",dbl))),
    5477             :  pattern("batcalc", "dec_round", dbl_bat_dec_round_wrap, false, "round off the value v to nearests multiple of r", args(1,3, batarg("",dbl),batarg("v",dbl),arg("r",dbl))),
    5478             :  pattern("batcalc", "dec_round", dbl_bat_dec_round_wrap, false, "round off the value v to nearests multiple of r", args(1,4, batarg("",dbl),batarg("v",dbl),arg("r",dbl),batarg("s",oid))),
    5479             :  pattern("batcalc", "dec_round", dbl_bat_dec_round_wrap_cst, false, "round off the value v to nearests multiple of r", args(1,3, batarg("",dbl),arg("v",dbl),batarg("r",dbl))),
    5480             :  pattern("batcalc", "dec_round", dbl_bat_dec_round_wrap_cst, false, "round off the value v to nearests multiple of r", args(1,4, batarg("",dbl),arg("v",dbl),batarg("r",dbl),batarg("s",oid))),
    5481             :  pattern("batcalc", "dec_round", dbl_bat_dec_round_wrap_nocst, false, "round off the value v to nearests multiple of r", args(1,3, batarg("",dbl),batarg("v",dbl),batarg("r",dbl))),
    5482             :  pattern("batcalc", "dec_round", dbl_bat_dec_round_wrap_nocst, false, "round off the value v to nearests multiple of r", args(1,5, batarg("",dbl),batarg("v",dbl),batarg("r",dbl),batarg("s1",oid),batarg("s2",oid))),
    5483             :  command("calc", "round", dbl_round_wrap, false, "round off the floating point v to r digits behind the dot (if r < 0, before the dot)", args(1,3, arg("",dbl),arg("v",dbl),arg("r",bte))),
    5484             :  pattern("batcalc", "round", dbl_bat_round_wrap, false, "round off the floating point v to r digits behind the dot (if r < 0, before the dot)", args(1,3, batarg("",dbl),batarg("v",dbl),arg("r",bte))),
    5485             :  pattern("batcalc", "round", dbl_bat_round_wrap, false, "round off the floating point v to r digits behind the dot (if r < 0, before the dot)", args(1,4, batarg("",dbl),batarg("v",dbl),arg("r",bte),batarg("s",oid))),
    5486             :  pattern("batcalc", "round", dbl_bat_round_wrap_cst, false, "round off the floating point v to r digits behind the dot (if r < 0, before the dot)", args(1,3, batarg("",dbl),arg("v",dbl),batarg("r",bte))),
    5487             :  pattern("batcalc", "round", dbl_bat_round_wrap_cst, false, "round off the floating point v to r digits behind the dot (if r < 0, before the dot)", args(1,4, batarg("",dbl),arg("v",dbl),batarg("r",bte),batarg("s",oid))),
    5488             :  pattern("batcalc", "round", dbl_bat_round_wrap_nocst, false, "round off the floating point v to r digits behind the dot (if r < 0, before the dot)", args(1,3, batarg("",dbl),batarg("v",dbl),batarg("r",bte))),
    5489             :  pattern("batcalc", "round", dbl_bat_round_wrap_nocst, false, "round off the floating point v to r digits behind the dot (if r < 0, before the dot)", args(1,5, batarg("",dbl),batarg("v",dbl),batarg("r",bte),batarg("s1",oid),batarg("s2",oid))),
    5490             :  command("sql", "ms_trunc", dbl_trunc_wrap, false, "truncate the floating point v to r digits behind the dot (if r < 0, before the dot)", args(1,3, arg("",dbl),arg("v",dbl),arg("r",int))),
    5491             :  command("sql", "alpha", SQLcst_alpha_cst, false, "Implementation of astronomy alpha function: expands the radius theta depending on the declination", args(1,3, arg("",dbl),arg("dec",dbl),arg("theta",dbl))),
    5492             :  command("batsql", "alpha", SQLbat_alpha_cst, false, "BAT implementation of astronomy alpha function", args(1,3, batarg("",dbl),batarg("dec",dbl),arg("theta",dbl))),
    5493             :  command("batsql", "alpha", SQLcst_alpha_bat, false, "BAT implementation of astronomy alpha function", args(1,3, batarg("",dbl),arg("dec",dbl),batarg("theta",dbl))),
    5494             :  command("calc", "bte", nil_2dec_bte, false, "cast to dec(bte) and check for overflow", args(1,4, arg("",bte),arg("v",void),arg("digits",int),arg("scale",int))),
    5495             :  command("batcalc", "bte", batnil_2dec_bte, false, "cast to dec(bte) and check for overflow", args(1,4, batarg("",bte),batarg("v",oid),arg("digits",int),arg("scale",int))),
    5496             :  command("calc", "bte", str_2dec_bte, false, "cast to dec(bte) and check for overflow", args(1,4, arg("",bte),arg("v",str),arg("digits",int),arg("scale",int))),
    5497             :  pattern("batcalc", "bte", batstr_2dec_bte, false, "cast to dec(bte) and check for overflow", args(1,5, batarg("",bte),batarg("v",str),batarg("s",oid),arg("digits",int),arg("scale",int))),
    5498             :  command("calc", "sht", nil_2dec_sht, false, "cast to dec(sht) and check for overflow", args(1,4, arg("",sht),arg("v",void),arg("digits",int),arg("scale",int))),
    5499             :  command("batcalc", "sht", batnil_2dec_sht, false, "cast to dec(sht) and check for overflow", args(1,4, batarg("",sht),batarg("v",oid),arg("digits",int),arg("scale",int))),
    5500             :  command("calc", "sht", str_2dec_sht, false, "cast to dec(sht) and check for overflow", args(1,4, arg("",sht),arg("v",str),arg("digits",int),arg("scale",int))),
    5501             :  pattern("batcalc", "sht", batstr_2dec_sht, false, "cast to dec(sht) and check for overflow", args(1,5, batarg("",sht),batarg("v",str),batarg("s",oid),arg("digits",int),arg("scale",int))),
    5502             :  command("calc", "int", nil_2dec_int, false, "cast to dec(int) and check for overflow", args(1,4, arg("",int),arg("v",void),arg("digits",int),arg("scale",int))),
    5503             :  command("batcalc", "int", batnil_2dec_int, false, "cast to dec(int) and check for overflow", args(1,4, batarg("",int),batarg("v",oid),arg("digits",int),arg("scale",int))),
    5504             :  command("calc", "int", str_2dec_int, false, "cast to dec(int) and check for overflow", args(1,4, arg("",int),arg("v",str),arg("digits",int),arg("scale",int))),
    5505             :  pattern("batcalc", "int", batstr_2dec_int, false, "cast to dec(int) and check for overflow", args(1,5, batarg("",int),batarg("v",str),batarg("s",oid),arg("digits",int),arg("scale",int))),
    5506             :  command("calc", "lng", nil_2dec_lng, false, "cast to dec(lng) and check for overflow", args(1,4, arg("",lng),arg("v",void),arg("digits",int),arg("scale",int))),
    5507             :  command("batcalc", "lng", batnil_2dec_lng, false, "cast to dec(lng) and check for overflow", args(1,4, batarg("",lng),batarg("v",oid),arg("digits",int),arg("scale",int))),
    5508             :  command("calc", "lng", str_2dec_lng, false, "cast to dec(lng) and check for overflow", args(1,4, arg("",lng),arg("v",str),arg("digits",int),arg("scale",int))),
    5509             :  pattern("batcalc", "lng", batstr_2dec_lng, false, "cast to dec(lng) and check for overflow", args(1,5, batarg("",lng),batarg("v",str),batarg("s",oid),arg("digits",int),arg("scale",int))),
    5510             :  pattern("calc", "timestamp", nil_2time_timestamp, false, "cast to timestamp and check for overflow", args(1,3, arg("",timestamp),arg("v",void),arg("digits",int))),
    5511             :  pattern("batcalc", "timestamp", nil_2time_timestamp, false, "cast to timestamp and check for overflow", args(1,3, batarg("",timestamp),batarg("v",oid),arg("digits",int))),
    5512             :  pattern("batcalc", "timestamp", nil_2time_timestamp, false, "cast to timestamp and check for overflow", args(1,4, batarg("",timestamp),batarg("v",oid),arg("digits",int),batarg("r",bat))),
    5513             :  pattern("calc", "timestamp", str_2time_timestamp, false, "cast to timestamp and check for overflow", args(1,3, arg("",timestamp),arg("v",str),arg("digits",int))),
    5514             :  pattern("calc", "timestamp", str_2time_timestamptz, false, "cast to timestamp and check for overflow", args(1,4, arg("",timestamp),arg("v",str),arg("digits",int),arg("has_tz",int))),
    5515             :  pattern("calc", "timestamp", timestamp_2time_timestamp, false, "cast timestamp to timestamp and check for overflow", args(1,3, arg("",timestamp),arg("v",timestamp),arg("digits",int))),
    5516             :  command("batcalc", "timestamp", batstr_2time_timestamp, false, "cast to timestamp and check for overflow", args(1,4, batarg("",timestamp),batarg("v",str),batarg("s",oid),arg("digits",int))),
    5517             :  command("batcalc", "timestamp", batstr_2time_timestamptz, false, "cast to timestamp and check for overflow", args(1,5, batarg("",timestamp),batarg("v",str),batarg("s",oid),arg("digits",int),arg("has_tz",int))),
    5518             :  pattern("batcalc", "timestamp", timestamp_2time_timestamp, false, "cast timestamp to timestamp and check for overflow", args(1,4, batarg("",timestamp),batarg("v",timestamp),batarg("s",oid),arg("digits",int))),
    5519             :  pattern("batcalc", "daytime", nil_2time_daytime, false, "cast to daytime and check for overflow", args(1,3, batarg("",daytime),batarg("v",oid),arg("digits",int))),
    5520             :  pattern("calc", "daytime", str_2time_daytime, false, "cast to daytime and check for overflow", args(1,3, arg("",daytime),arg("v",str),arg("digits",int))),
    5521             :  pattern("calc", "daytime", str_2time_daytimetz, false, "cast to daytime and check for overflow", args(1,4, arg("",daytime),arg("v",str),arg("digits",int),arg("has_tz",int))),
    5522             :  pattern("calc", "daytime", daytime_2time_daytime, false, "cast daytime to daytime and check for overflow", args(1,3, arg("",daytime),arg("v",daytime),arg("digits",int))),
    5523             :  command("batcalc", "daytime", batstr_2time_daytime, false, "cast to daytime and check for overflow", args(1,4, batarg("",daytime),batarg("v",str),batarg("s",oid),arg("digits",int))),
    5524             :  pattern("batcalc", "daytime", str_2time_daytimetz, false, "cast daytime to daytime and check for overflow", args(1,5, batarg("",daytime),batarg("v",str),batarg("s",oid),arg("digits",int),arg("has_tz",int))),
    5525             :  pattern("batcalc", "daytime", daytime_2time_daytime, false, "cast daytime to daytime and check for overflow", args(1,4, batarg("",daytime),batarg("v",daytime),batarg("s",oid),arg("digits",int))),
    5526             :  command("sql", "date_trunc", bat_date_trunc, false, "Truncate a timestamp to (millennium, century,decade,year,quarter,month,week,day,hour,minute,second, milliseconds,microseconds)", args(1,3, batarg("",timestamp),arg("scale",str),batarg("v",timestamp))),
    5527             :  command("sql", "date_trunc", date_trunc, false, "Truncate a timestamp to (millennium, century,decade,year,quarter,month,week,day,hour,minute,second, milliseconds,microseconds)", args(1,3, arg("",timestamp),arg("scale",str),arg("v",timestamp))),
    5528             :  pattern("sql", "current_time", SQLcurrent_daytime, false, "Get the clients current daytime", args(1,1, arg("",daytime))),
    5529             :  pattern("sql", "current_timestamp", SQLcurrent_timestamp, false, "Get the clients current timestamp", args(1,1, arg("",timestamp))),
    5530             :  pattern("calc", "date", nil_2_date, false, "cast to date", args(1,2, arg("",date),arg("v",void))),
    5531             :  pattern("batcalc", "date", nil_2_date, false, "cast to date", args(1,2, batarg("",date),batarg("v",oid))),
    5532             :  pattern("calc", "date", str_2_date, false, "cast to date", args(1,2, arg("",date),arg("v",str))),
    5533             :  command("batcalc", "date", batstr_2_date, false, "cast to date", args(1,3, batarg("",date),batarg("v",str),batarg("s",oid))),
    5534             :  command("calc", "blob", str_2_blob, false, "cast to blob", args(1,2, arg("",blob),arg("v",str))),
    5535             :  command("batcalc", "blob", batstr_2_blob, false, "cast to blob", args(1,3, batarg("",blob),batarg("v",str),batarg("s",oid))),
    5536             :  pattern("calc", "str", SQLstr_cast, false, "cast to string and check for overflow", args(1,7, arg("",str),arg("eclass",int),arg("d1",int),arg("s1",int),arg("has_tz",int),argany("v",1),arg("digits",int))),
    5537             :  pattern("batcalc", "str", SQLbatstr_cast, false, "cast to string and check for overflow", args(1,8, batarg("",str),arg("eclass",int),arg("d1",int),arg("s1",int),arg("has_tz",int),batargany("v",1),batarg("s",oid),arg("digits",int))),
    5538             :  pattern("calc", "month_interval", month_interval_str, false, "cast str to a month_interval and check for overflow", args(1,4, arg("",int),arg("v",str),arg("ek",int),arg("sk",int))),
    5539             :  pattern("batcalc", "month_interval", month_interval_str, false, "cast str to a month_interval and check for overflow", args(1,5, batarg("",int),batarg("v",str),batarg("s",oid),arg("ek",int),arg("sk",int))),
    5540             :  pattern("calc", "second_interval", second_interval_str, false, "cast str to a second_interval and check for overflow", args(1,4, arg("",lng),arg("v",str),arg("ek",int),arg("sk",int))),
    5541             :  pattern("batcalc", "second_interval", second_interval_str, false, "cast str to a second_interval and check for overflow", args(1,5, batarg("",lng),batarg("v",str),batarg("s",oid),arg("ek",int),arg("sk",int))),
    5542             :  pattern("calc", "month_interval", month_interval, false, "cast bte to a month_interval and check for overflow", args(1,4, arg("",int),arg("v",bte),arg("ek",int),arg("sk",int))),
    5543             :  pattern("batcalc", "month_interval", month_interval, false, "cast bte to a month_interval and check for overflow", args(1,5, batarg("",int),batarg("v",bte),batarg("s",oid),arg("ek",int),arg("sk",int))),
    5544             :  pattern("calc", "second_interval", second_interval, false, "cast bte to a second_interval and check for overflow", args(1,4, arg("",lng),arg("v",bte),arg("ek",int),arg("sk",int))),
    5545             :  pattern("batcalc", "second_interval", second_interval, false, "cast bte to a second_interval and check for overflow", args(1,5, batarg("",lng),batarg("v",bte),batarg("s",oid),arg("ek",int),arg("sk",int))),
    5546             :  pattern("calc", "month_interval", month_interval, false, "cast sht to a month_interval and check for overflow", args(1,4, arg("",int),arg("v",sht),arg("ek",int),arg("sk",int))),
    5547             :  pattern("batcalc", "month_interval", month_interval, false, "cast sht to a month_interval and check for overflow", args(1,5, batarg("",int),batarg("v",sht),batarg("s",oid),arg("ek",int),arg("sk",int))),
    5548             :  pattern("calc", "second_interval", second_interval, false, "cast sht to a second_interval and check for overflow", args(1,4, arg("",lng),arg("v",sht),arg("ek",int),arg("sk",int))),
    5549             :  pattern("batcalc", "second_interval", second_interval, false, "cast sht to a second_interval and check for overflow", args(1,5, batarg("",lng),batarg("v",sht),batarg("s",oid),arg("ek",int),arg("sk",int))),
    5550             :  pattern("calc", "month_interval", month_interval, false, "cast int to a month_interval and check for overflow", args(1,4, arg("",int),arg("v",int),arg("ek",int),arg("sk",int))),
    5551             :  pattern("batcalc", "month_interval", month_interval, false, "cast int to a month_interval and check for overflow", args(1,5, batarg("",int),batarg("v",int),batarg("s",oid),arg("ek",int),arg("sk",int))),
    5552             :  pattern("calc", "second_interval", second_interval, false, "cast int to a second_interval and check for overflow", args(1,4, arg("",lng),arg("v",int),arg("ek",int),arg("sk",int))),
    5553             :  pattern("batcalc", "second_interval", second_interval, false, "cast int to a second_interval and check for overflow", args(1,5, batarg("",lng),batarg("v",int),batarg("s",oid),arg("ek",int),arg("sk",int))),
    5554             :  pattern("calc", "month_interval", month_interval, false, "cast lng to a month_interval and check for overflow", args(1,4, arg("",int),arg("v",lng),arg("ek",int),arg("sk",int))),
    5555             :  pattern("batcalc", "month_interval", month_interval, false, "cast lng to a month_interval and check for overflow", args(1,5, batarg("",int),batarg("v",lng),batarg("s",oid),arg("ek",int),arg("sk",int))),
    5556             :  pattern("calc", "second_interval", second_interval, false, "cast lng to a second_interval and check for overflow", args(1,4, arg("",lng),arg("v",lng),arg("ek",int),arg("sk",int))),
    5557             :  pattern("batcalc", "second_interval", second_interval, false, "cast lng to a second_interval and check for overflow", args(1,5, batarg("",lng),batarg("v",lng),batarg("s",oid),arg("ek",int),arg("sk",int))),
    5558             :  pattern("calc", "rowid", sql_rowid, false, "return the next rowid", args(1,4, arg("",oid),argany("v",1),arg("schema",str),arg("table",str))),
    5559             :  pattern("sql", "drop_hash", SQLdrop_hash, true, "Drop hash indices for the given table", args(0,2, arg("sch",str),arg("tbl",str))),
    5560             :  pattern("sql", "prelude", SQLprelude, false, "", noargs),
    5561             :  command("sql", "epilogue", SQLepilogue, false, "", noargs),
    5562             :  pattern("calc", "second_interval", second_interval_daytime, false, "cast daytime to a second_interval and check for overflow", args(1,4, arg("",lng),arg("v",daytime),arg("ek",int),arg("sk",int))),
    5563             :  pattern("batcalc", "second_interval", second_interval_daytime, false, "cast daytime to a second_interval and check for overflow", args(1,5, batarg("",lng),batarg("v",daytime),batarg("s",oid),arg("ek",int),arg("sk",int))),
    5564             :  pattern("calc", "daytime", second_interval_2_daytime, false, "cast second_interval to a daytime and check for overflow", args(1,3, arg("",daytime),arg("v",lng),arg("d",int))),
    5565             :  pattern("batcalc", "daytime", second_interval_2_daytime, false, "cast second_interval to a daytime and check for overflow", args(1,4, batarg("",daytime),batarg("v",lng),batarg("s",oid),arg("d",int))),
    5566             :  pattern("calc", "daytime", timestamp_2_daytime, false, "cast timestamp to a daytime and check for overflow", args(1,3, arg("",daytime),arg("v",timestamp),arg("d",int))),
    5567             :  pattern("batcalc", "daytime", timestamp_2_daytime, false, "cast timestamp to a daytime and check for overflow", args(1,4, batarg("",daytime),batarg("v",timestamp),batarg("s",oid),arg("d",int))),
    5568             :  pattern("calc", "timestamp", date_2_timestamp, false, "cast date to a timestamp and check for overflow", args(1,3, arg("",timestamp),arg("v",date),arg("d",int))),
    5569             :  pattern("batcalc", "timestamp", date_2_timestamp, false, "cast date to a timestamp and check for overflow", args(1,4, batarg("",timestamp),batarg("v",date),batarg("s",oid),arg("d",int))),
    5570             :  command("sql", "index", STRindex_bte, false, "Return the offsets as an index bat", args(1,3, arg("",bte),arg("v",str),arg("u",bit))), /* TODO add candidate list support? */
    5571             :  command("batsql", "index", BATSTRindex_bte, false, "Return the offsets as an index bat", args(1,3, batarg("",bte),batarg("v",str),arg("u",bit))),
    5572             :  command("sql", "index", STRindex_sht, false, "Return the offsets as an index bat", args(1,3, arg("",sht),arg("v",str),arg("u",bit))),
    5573             :  command("batsql", "index", BATSTRindex_sht, false, "Return the offsets as an index bat", args(1,3, batarg("",sht),batarg("v",str),arg("u",bit))),
    5574             :  command("sql", "index", STRindex_int, false, "Return the offsets as an index bat", args(1,3, arg("",int),arg("v",str),arg("u",bit))),
    5575             :  command("batsql", "index", BATSTRindex_int, false, "Return the offsets as an index bat", args(1,3, batarg("",int),batarg("v",str),arg("u",bit))),
    5576             :  command("sql", "strings", STRstrings, false, "Return the strings", args(1,2, arg("",str),arg("v",str))), /* TODO add candidate list support? */
    5577             :  command("batsql", "strings", BATSTRstrings, false, "Return the strings", args(1,2, batarg("",str),batarg("v",str))),
    5578             :  pattern("sql", "update_tables", SYSupdate_tables, true, "Procedure triggered on update of the sys._tables table", args(1,1, arg("",void))),
    5579             :  pattern("sql", "update_schemas", SYSupdate_schemas, true, "Procedure triggered on update of the sys.schemas table", args(1,1, arg("",void))),
    5580             :  pattern("sql", "unionfunc", SQLunionfunc, false, "", args(1,4, varargany("",0),arg("mod",str),arg("fcn",str),varargany("",0))),
    5581             :  /* decimals */
    5582             :  command("calc", "bte", flt_num2dec_bte, false, "cast number to decimal(bte) and check for overflow", args(1,4, arg("",bte),arg("v",flt),arg("digits",int),arg("scale",int))),
    5583             :  command("batcalc", "bte", batflt_num2dec_bte, false, "cast number to decimal(bte) and check for overflow", args(1,5, batarg("",bte),batarg("v",flt),batarg("s",oid),arg("digits",int),arg("scale",int))),
    5584             :  command("calc", "bte", dbl_num2dec_bte, false, "cast number to decimal(bte) and check for overflow", args(1,4, arg("",bte),arg("v",dbl),arg("digits",int),arg("scale",int))),
    5585             :  command("batcalc", "bte", batdbl_num2dec_bte, false, "cast number to decimal(bte) and check for overflow", args(1,5, batarg("",bte),batarg("v",dbl),batarg("s",oid),arg("digits",int),arg("scale",int))),
    5586             :  command("calc", "sht", flt_num2dec_sht, false, "cast number to decimal(sht) and check for overflow", args(1,4, arg("",sht),arg("v",flt),arg("digits",int),arg("scale",int))),
    5587             :  command("batcalc", "sht", batflt_num2dec_sht, false, "cast number to decimal(sht) and check for overflow", args(1,5, batarg("",sht),batarg("v",flt),batarg("s",oid),arg("digits",int),arg("scale",int))),
    5588             :  command("calc", "sht", dbl_num2dec_sht, false, "cast number to decimal(sht) and check for overflow", args(1,4, arg("",sht),arg("v",dbl),arg("digits",int),arg("scale",int))),
    5589             :  command("batcalc", "sht", batdbl_num2dec_sht, false, "cast number to decimal(sht) and check for overflow", args(1,5, batarg("",sht),batarg("v",dbl),batarg("s",oid),arg("digits",int),arg("scale",int))),
    5590             :  command("calc", "int", flt_num2dec_int, false, "cast number to decimal(int) and check for overflow", args(1,4, arg("",int),arg("v",flt),arg("digits",int),arg("scale",int))),
    5591             :  command("batcalc", "int", batflt_num2dec_int, false, "cast number to decimal(int) and check for overflow", args(1,5, batarg("",int),batarg("v",flt),batarg("s",oid),arg("digits",int),arg("scale",int))),
    5592             :  command("calc", "int", dbl_num2dec_int, false, "cast number to decimal(int) and check for overflow", args(1,4, arg("",int),arg("v",dbl),arg("digits",int),arg("scale",int))),
    5593             :  command("batcalc", "int", batdbl_num2dec_int, false, "cast number to decimal(int) and check for overflow", args(1,5, batarg("",int),batarg("v",dbl),batarg("s",oid),arg("digits",int),arg("scale",int))),
    5594             :  command("calc", "lng", flt_num2dec_lng, false, "cast number to decimal(lng) and check for overflow", args(1,4, arg("",lng),arg("v",flt),arg("digits",int),arg("scale",int))),
    5595             :  command("batcalc", "lng", batflt_num2dec_lng, false, "cast number to decimal(lng) and check for overflow", args(1,5, batarg("",lng),batarg("v",flt),batarg("s",oid),arg("digits",int),arg("scale",int))),
    5596             :  command("calc", "lng", dbl_num2dec_lng, false, "cast number to decimal(lng) and check for overflow", args(1,4, arg("",lng),arg("v",dbl),arg("digits",int),arg("scale",int))),
    5597             :  command("batcalc", "lng", batdbl_num2dec_lng, false, "cast number to decimal(lng) and check for overflow", args(1,5, batarg("",lng),batarg("v",dbl),batarg("s",oid),arg("digits",int),arg("scale",int))),
    5598             :  command("calc", "bte", bte_num2dec_bte, false, "cast number to decimal(bte) and check for overflow", args(1,4, arg("",bte),arg("v",bte),arg("digits",int),arg("scale",int))),
    5599             :  command("batcalc", "bte", batbte_num2dec_bte, false, "cast number to decimal(bte) and check for overflow", args(1,5, batarg("",bte),batarg("v",bte),batarg("s",oid),arg("digits",int),arg("scale",int))),
    5600             :  command("calc", "bte", bte_dec2_bte, false, "cast decimal(bte) to bte and check for overflow", args(1,3, arg("",bte),arg("s1",int),arg("v",bte))),
    5601             :  command("calc", "bte", bte_dec2dec_bte, false, "cast decimal(bte) to decimal(bte) and check for overflow", args(1,5, arg("",bte),arg("s1",int),arg("v",bte),arg("d2",int),arg("s2",int))),
    5602             :  command("batcalc", "bte", batbte_dec2_bte, false, "cast decimal(bte) to bte and check for overflow", args(1,4, batarg("",bte),arg("s1",int),batarg("v",bte),batarg("s",oid))),
    5603             :  command("batcalc", "bte", batbte_dec2dec_bte, false, "cast decimal(bte) to decimal(bte) and check for overflow", args(1,6, batarg("",bte),arg("s1",int),batarg("v",bte),batarg("s",oid),arg("d2",int),arg("s2",int))),
    5604             :  command("calc", "bte", sht_num2dec_bte, false, "cast number to decimal(bte) and check for overflow", args(1,4, arg("",bte),arg("v",sht),arg("digits",int),arg("scale",int))),
    5605             :  command("batcalc", "bte", batsht_num2dec_bte, false, "cast number to decimal(bte) and check for overflow", args(1,5, batarg("",bte),batarg("v",sht),batarg("s",oid),arg("digits",int),arg("scale",int))),
    5606             :  command("calc", "bte", sht_dec2_bte, false, "cast decimal(sht) to bte and check for overflow", args(1,3, arg("",bte),arg("s1",int),arg("v",sht))),
    5607             :  command("calc", "bte", sht_dec2dec_bte, false, "cast decimal(sht) to decimal(bte) and check for overflow", args(1,5, arg("",bte),arg("s1",int),arg("v",sht),arg("d2",int),arg("s2",int))),
    5608             :  command("batcalc", "bte", batsht_dec2_bte, false, "cast decimal(sht) to bte and check for overflow", args(1,4, batarg("",bte),arg("s1",int),batarg("v",sht),batarg("s",oid))),
    5609             :  command("batcalc", "bte", batsht_dec2dec_bte, false, "cast decimal(sht) to decimal(bte) and check for overflow", args(1,6, batarg("",bte),arg("s1",int),batarg("v",sht),batarg("s",oid),arg("d2",int),arg("s2",int))),
    5610             :  command("calc", "bte", int_num2dec_bte, false, "cast number to decimal(bte) and check for overflow", args(1,4, arg("",bte),arg("v",int),arg("digits",int),arg("scale",int))),
    5611             :  command("batcalc", "bte", batint_num2dec_bte, false, "cast number to decimal(bte) and check for overflow", args(1,5, batarg("",bte),batarg("v",int),batarg("s",oid),arg("digits",int),arg("scale",int))),
    5612             :  command("calc", "bte", int_dec2_bte, false, "cast decimal(int) to bte and check for overflow", args(1,3, arg("",bte),arg("s1",int),arg("v",int))),
    5613             :  command("calc", "bte", int_dec2dec_bte, false, "cast decimal(int) to decimal(bte) and check for overflow", args(1,5, arg("",bte),arg("s1",int),arg("v",int),arg("d2",int),arg("s2",int))),
    5614             :  command("batcalc", "bte", batint_dec2_bte, false, "cast decimal(int) to bte and check for overflow", args(1,4, batarg("",bte),arg("s1",int),batarg("v",int),batarg("s",oid))),
    5615             :  command("batcalc", "bte", batint_dec2dec_bte, false, "cast decimal(int) to decimal(bte) and check for overflow", args(1,6, batarg("",bte),arg("s1",int),batarg("v",int),batarg("s",oid),arg("d2",int),arg("s2",int))),
    5616             :  command("calc", "bte", lng_num2dec_bte, false, "cast number to decimal(bte) and check for overflow", args(1,4, arg("",bte),arg("v",lng),arg("digits",int),arg("scale",int))),
    5617             :  command("batcalc", "bte", batlng_num2dec_bte, false, "cast number to decimal(bte) and check for overflow", args(1,5, batarg("",bte),batarg("v",lng),batarg("s",oid),arg("digits",int),arg("scale",int))),
    5618             :  command("calc", "bte", lng_dec2_bte, false, "cast decimal(lng) to bte and check for overflow", args(1,3, arg("",bte),arg("s1",int),arg("v",lng))),
    5619             :  command("calc", "bte", lng_dec2dec_bte, false, "cast decimal(lng) to decimal(bte) and check for overflow", args(1,5, arg("",bte),arg("s1",int),arg("v",lng),arg("d2",int),arg("s2",int))),
    5620             :  command("batcalc", "bte", batlng_dec2_bte, false, "cast decimal(lng) to bte and check for overflow", args(1,4, batarg("",bte),arg("s1",int),batarg("v",lng),batarg("s",oid))),
    5621             :  command("batcalc", "bte", batlng_dec2dec_bte, false, "cast decimal(lng) to decimal(bte) and check for overflow", args(1,6, batarg("",bte),arg("s1",int),batarg("v",lng),batarg("s",oid),arg("d2",int),arg("s2",int))),
    5622             :  command("calc", "sht", bte_num2dec_sht, false, "cast number to decimal(sht) and check for overflow", args(1,4, arg("",sht),arg("v",bte),arg("digits",int),arg("scale",int))),
    5623             :  command("batcalc", "sht", batbte_num2dec_sht, false, "cast number to decimal(sht) and check for overflow", args(1,5, batarg("",sht),batarg("v",bte),batarg("s",oid),arg("digits",int),arg("scale",int))),
    5624             :  command("calc", "sht", bte_dec2_sht, false, "cast decimal(bte) to sht and check for overflow", args(1,3, arg("",sht),arg("s1",int),arg("v",bte))),
    5625             :  command("calc", "sht", bte_dec2dec_sht, false, "cast decimal(bte) to decimal(sht) and check for overflow", args(1,5, arg("",sht),arg("s1",int),arg("v",bte),arg("d2",int),arg("s2",int))),
    5626             :  command("batcalc", "sht", batbte_dec2_sht, false, "cast decimal(bte) to sht and check for overflow", args(1,4, batarg("",sht),arg("s1",int),batarg("v",bte),batarg("s",oid))),
    5627             :  command("batcalc", "sht", batbte_dec2dec_sht, false, "cast decimal(bte) to decimal(sht) and check for overflow", args(1,6, batarg("",sht),arg("s1",int),batarg("v",bte),batarg("s",oid),arg("d2",int),arg("s2",int))),
    5628             :  command("calc", "sht", sht_num2dec_sht, false, "cast number to decimal(sht) and check for overflow", args(1,4, arg("",sht),arg("v",sht),arg("digits",int),arg("scale",int))),
    5629             :  command("batcalc", "sht", batsht_num2dec_sht, false, "cast number to decimal(sht) and check for overflow", args(1,5, batarg("",sht),batarg("v",sht),batarg("s",oid),arg("digits",int),arg("scale",int))),
    5630             :  command("calc", "sht", sht_dec2_sht, false, "cast decimal(sht) to sht and check for overflow", args(1,3, arg("",sht),arg("s1",int),arg("v",sht))),
    5631             :  command("calc", "sht", sht_dec2dec_sht, false, "cast decimal(sht) to decimal(sht) and check for overflow", args(1,5, arg("",sht),arg("s1",int),arg("v",sht),arg("d2",int),arg("s2",int))),
    5632             :  command("batcalc", "sht", batsht_dec2_sht, false, "cast decimal(sht) to sht and check for overflow", args(1,4, batarg("",sht),arg("s1",int),batarg("v",sht),batarg("s",oid))),
    5633             :  command("batcalc", "sht", batsht_dec2dec_sht, false, "cast decimal(sht) to decimal(sht) and check for overflow", args(1,6, batarg("",sht),arg("s1",int),batarg("v",sht),batarg("s",oid),arg("d2",int),arg("s2",int))),
    5634             :  command("calc", "sht", int_num2dec_sht, false, "cast number to decimal(sht) and check for overflow", args(1,4, arg("",sht),arg("v",int),arg("digits",int),arg("scale",int))),
    5635             :  command("batcalc", "sht", batint_num2dec_sht, false, "cast number to decimal(sht) and check for overflow", args(1,5, batarg("",sht),batarg("v",int),batarg("s",oid),arg("digits",int),arg("scale",int))),
    5636             :  command("calc", "sht", int_dec2_sht, false, "cast decimal(int) to sht and check for overflow", args(1,3, arg("",sht),arg("s1",int),arg("v",int))),
    5637             :  command("calc", "sht", int_dec2dec_sht, false, "cast decimal(int) to decimal(sht) and check for overflow", args(1,5, arg("",sht),arg("s1",int),arg("v",int),arg("d2",int),arg("s2",int))),
    5638             :  command("batcalc", "sht", batint_dec2_sht, false, "cast decimal(int) to sht and check for overflow", args(1,4, batarg("",sht),arg("s1",int),batarg("v",int),batarg("s",oid))),
    5639             :  command("batcalc", "sht", batint_dec2dec_sht, false, "cast decimal(int) to decimal(sht) and check for overflow", args(1,6, batarg("",sht),arg("s1",int),batarg("v",int),batarg("s",oid),arg("d2",int),arg("s2",int))),
    5640             :  command("calc", "sht", lng_num2dec_sht, false, "cast number to decimal(sht) and check for overflow", args(1,4, arg("",sht),arg("v",lng),arg("digits",int),arg("scale",int))),
    5641             :  command("batcalc", "sht", batlng_num2dec_sht, false, "cast number to decimal(sht) and check for overflow", args(1,5, batarg("",sht),batarg("v",lng),batarg("s",oid),arg("digits",int),arg("scale",int))),
    5642             :  command("calc", "sht", lng_dec2_sht, false, "cast decimal(lng) to sht and check for overflow", args(1,3, arg("",sht),arg("s1",int),arg("v",lng))),
    5643             :  command("calc", "sht", lng_dec2dec_sht, false, "cast decimal(lng) to decimal(sht) and check for overflow", args(1,5, arg("",sht),arg("s1",int),arg("v",lng),arg("d2",int),arg("s2",int))),
    5644             :  command("batcalc", "sht", batlng_dec2_sht, false, "cast decimal(lng) to sht and check for overflow", args(1,4, batarg("",sht),arg("s1",int),batarg("v",lng),batarg("s",oid))),
    5645             :  command("batcalc", "sht", batlng_dec2dec_sht, false, "cast decimal(lng) to decimal(sht) and check for overflow", args(1,6, batarg("",sht),arg("s1",int),batarg("v",lng),batarg("s",oid),arg("d2",int),arg("s2",int))),
    5646             :  command("calc", "int", bte_num2dec_int, false, "cast number to decimal(int) and check for overflow", args(1,4, arg("",int),arg("v",bte),arg("digits",int),arg("scale",int))),
    5647             :  command("batcalc", "int", batbte_num2dec_int, false, "cast number to decimal(int) and check for overflow", args(1,5, batarg("",int),batarg("v",bte),batarg("s",oid),arg("digits",int),arg("scale",int))),
    5648             :  command("calc", "int", bte_dec2_int, false, "cast decimal(bte) to int and check for overflow", args(1,3, arg("",int),arg("s1",int),arg("v",bte))),
    5649             :  command("calc", "int", bte_dec2dec_int, false, "cast decimal(bte) to decimal(int) and check for overflow", args(1,5, arg("",int),arg("s1",int),arg("v",bte),arg("d2",int),arg("s2",int))),
    5650             :  command("batcalc", "int", batbte_dec2_int, false, "cast decimal(bte) to int and check for overflow", args(1,4, batarg("",int),arg("s1",int),batarg("v",bte),batarg("s",oid))),
    5651             :  command("batcalc", "int", batbte_dec2dec_int, false, "cast decimal(bte) to decimal(int) and check for overflow", args(1,6, batarg("",int),arg("s1",int),batarg("v",bte),batarg("s",oid),arg("d2",int),arg("s2",int))),
    5652             :  command("calc", "int", sht_num2dec_int, false, "cast number to decimal(int) and check for overflow", args(1,4, arg("",int),arg("v",sht),arg("digits",int),arg("scale",int))),
    5653             :  command("batcalc", "int", batsht_num2dec_int, false, "cast number to decimal(int) and check for overflow", args(1,5, batarg("",int),batarg("v",sht),batarg("s",oid),arg("digits",int),arg("scale",int))),
    5654             :  command("calc", "int", sht_dec2_int, false, "cast decimal(sht) to int and check for overflow", args(1,3, arg("",int),arg("s1",int),arg("v",sht))),
    5655             :  command("calc", "int", sht_dec2dec_int, false, "cast decimal(sht) to decimal(int) and check for overflow", args(1,5, arg("",int),arg("s1",int),arg("v",sht),arg("d2",int),arg("s2",int))),
    5656             :  command("batcalc", "int", batsht_dec2_int, false, "cast decimal(sht) to int and check for overflow", args(1,4, batarg("",int),arg("s1",int),batarg("v",sht),batarg("s",oid))),
    5657             :  command("batcalc", "int", batsht_dec2dec_int, false, "cast decimal(sht) to decimal(int) and check for overflow", args(1,6, batarg("",int),arg("s1",int),batarg("v",sht),batarg("s",oid),arg("d2",int),arg("s2",int))),
    5658             :  command("calc", "int", int_num2dec_int, false, "cast number to decimal(int) and check for overflow", args(1,4, arg("",int),arg("v",int),arg("digits",int),arg("scale",int))),
    5659             :  command("batcalc", "int", batint_num2dec_int, false, "cast number to decimal(int) and check for overflow", args(1,5, batarg("",int),batarg("v",int),batarg("s",oid),arg("digits",int),arg("scale",int))),
    5660             :  command("calc", "int", int_dec2_int, false, "cast decimal(int) to int and check for overflow", args(1,3, arg("",int),arg("s1",int),arg("v",int))),
    5661             :  command("calc", "int", int_dec2dec_int, false, "cast decimal(int) to decimal(int) and check for overflow", args(1,5, arg("",int),arg("s1",int),arg("v",int),arg("d2",int),arg("s2",int))),
    5662             :  command("batcalc", "int", batint_dec2_int, false, "cast decimal(int) to int and check for overflow", args(1,4, batarg("",int),arg("s1",int),batarg("v",int),batarg("s",oid))),
    5663             :  command("batcalc", "int", batint_dec2dec_int, false, "cast decimal(int) to decimal(int) and check for overflow", args(1,6, batarg("",int),arg("s1",int),batarg("v",int),batarg("s",oid),arg("d2",int),arg("s2",int))),
    5664             :  command("calc", "int", lng_num2dec_int, false, "cast number to decimal(int) and check for overflow", args(1,4, arg("",int),arg("v",lng),arg("digits",int),arg("scale",int))),
    5665             :  command("batcalc", "int", batlng_num2dec_int, false, "cast number to decimal(int) and check for overflow", args(1,5, batarg("",int),batarg("v",lng),batarg("s",oid),arg("digits",int),arg("scale",int))),
    5666             :  command("calc", "int", lng_dec2_int, false, "cast decimal(lng) to int and check for overflow", args(1,3, arg("",int),arg("s1",int),arg("v",lng))),
    5667             :  command("calc", "int", lng_dec2dec_int, false, "cast decimal(lng) to decimal(int) and check for overflow", args(1,5, arg("",int),arg("s1",int),arg("v",lng),arg("d2",int),arg("s2",int))),
    5668             :  command("batcalc", "int", batlng_dec2_int, false, "cast decimal(lng) to int and check for overflow", args(1,4, batarg("",int),arg("s1",int),batarg("v",lng),batarg("s",oid))),
    5669             :  command("batcalc", "int", batlng_dec2dec_int, false, "cast decimal(lng) to decimal(int) and check for overflow", args(1,6, batarg("",int),arg("s1",int),batarg("v",lng),batarg("s",oid),arg("d2",int),arg("s2",int))),
    5670             :  command("calc", "lng", bte_num2dec_lng, false, "cast number to decimal(lng) and check for overflow", args(1,4, arg("",lng),arg("v",bte),arg("digits",int),arg("scale",int))),
    5671             :  command("batcalc", "lng", batbte_num2dec_lng, false, "cast number to decimal(lng) and check for overflow", args(1,5, batarg("",lng),batarg("v",bte),batarg("s",oid),arg("digits",int),arg("scale",int))),
    5672             :  command("calc", "lng", bte_dec2_lng, false, "cast decimal(bte) to lng and check for overflow", args(1,3, arg("",lng),arg("s1",int),arg("v",bte))),
    5673             :  command("calc", "lng", bte_dec2dec_lng, false, "cast decimal(bte) to decimal(lng) and check for overflow", args(1,5, arg("",lng),arg("s1",int),arg("v",bte),arg("d2",int),arg("s2",int))),
    5674             :  command("batcalc", "lng", batbte_dec2_lng, false, "cast decimal(bte) to lng and check for overflow", args(1,4, batarg("",lng),arg("s1",int),batarg("v",bte),batarg("s",oid))),
    5675             :  command("batcalc", "lng", batbte_dec2dec_lng, false, "cast decimal(bte) to decimal(lng) and check for overflow", args(1,6, batarg("",lng),arg("s1",int),batarg("v",bte),batarg("s",oid),arg("d2",int),arg("s2",int))),
    5676             :  command("calc", "lng", sht_num2dec_lng, false, "cast number to decimal(lng) and check for overflow", args(1,4, arg("",lng),arg("v",sht),arg("digits",int),arg("scale",int))),
    5677             :  command("batcalc", "lng", batsht_num2dec_lng, false, "cast number to decimal(lng) and check for overflow", args(1,5, batarg("",lng),batarg("v",sht),batarg("s",oid),arg("digits",int),arg("scale",int))),
    5678             :  command("calc", "lng", sht_dec2_lng, false, "cast decimal(sht) to lng and check for overflow", args(1,3, arg("",lng),arg("s1",int),arg("v",sht))),
    5679             :  command("calc", "lng", sht_dec2dec_lng, false, "cast decimal(sht) to decimal(lng) and check for overflow", args(1,5, arg("",lng),arg("s1",int),arg("v",sht),arg("d2",int),arg("s2",int))),
    5680             :  command("batcalc", "lng", batsht_dec2_lng, false, "cast decimal(sht) to lng and check for overflow", args(1,4, batarg("",lng),arg("s1",int),batarg("v",sht),batarg("s",oid))),
    5681             :  command("batcalc", "lng", batsht_dec2dec_lng, false, "cast decimal(sht) to decimal(lng) and check for overflow", args(1,6, batarg("",lng),arg("s1",int),batarg("v",sht),batarg("s",oid),arg("d2",int),arg("s2",int))),
    5682             :  command("calc", "lng", int_num2dec_lng, false, "cast number to decimal(lng) and check for overflow", args(1,4, arg("",lng),arg("v",int),arg("digits",int),arg("scale",int))),
    5683             :  command("batcalc", "lng", batint_num2dec_lng, false, "cast number to decimal(lng) and check for overflow", args(1,5, batarg("",lng),batarg("v",int),batarg("s",oid),arg("digits",int),arg("scale",int))),
    5684             :  command("calc", "lng", int_dec2_lng, false, "cast decimal(int) to lng and check for overflow", args(1,3, arg("",lng),arg("s1",int),arg("v",int))),
    5685             :  command("calc", "lng", int_dec2dec_lng, false, "cast decimal(int) to decimal(lng) and check for overflow", args(1,5, arg("",lng),arg("s1",int),arg("v",int),arg("d2",int),arg("s2",int))),
    5686             :  command("batcalc", "lng", batint_dec2_lng, false, "cast decimal(int) to lng and check for overflow", args(1,4, batarg("",lng),arg("s1",int),batarg("v",int),batarg("s",oid))),
    5687             :  command("batcalc", "lng", batint_dec2dec_lng, false, "cast decimal(int) to decimal(lng) and check for overflow", args(1,6, batarg("",lng),arg("s1",int),batarg("v",int),batarg("s",oid),arg("d2",int),arg("s2",int))),
    5688             :  command("calc", "lng", lng_num2dec_lng, false, "cast number to decimal(lng) and check for overflow", args(1,4, arg("",lng),arg("v",lng),arg("digits",int),arg("scale",int))),
    5689             :  command("batcalc", "lng", batlng_num2dec_lng, false, "cast number to decimal(lng) and check for overflow", args(1,5, batarg("",lng),batarg("v",lng),batarg("s",oid),arg("digits",int),arg("scale",int))),
    5690             :  command("calc", "lng", lng_dec2_lng, false, "cast decimal(lng) to lng and check for overflow", args(1,3, arg("",lng),arg("s1",int),arg("v",lng))),
    5691             :  command("calc", "lng", lng_dec2dec_lng, false, "cast decimal(lng) to decimal(lng) and check for overflow", args(1,5, arg("",lng),arg("s1",int),arg("v",lng),arg("d2",int),arg("s2",int))),
    5692             :  command("batcalc", "lng", batlng_dec2_lng, false, "cast decimal(lng) to lng and check for overflow", args(1,4, batarg("",lng),arg("s1",int),batarg("v",lng),batarg("s",oid))),
    5693             :  command("batcalc", "lng", batlng_dec2dec_lng, false, "cast decimal(lng) to decimal(lng) and check for overflow", args(1,6, batarg("",lng),arg("s1",int),batarg("v",lng),batarg("s",oid),arg("d2",int),arg("s2",int))),
    5694             :  command("calc", "flt", bte_num2dec_flt, false, "cast number to decimal(flt) and check for overflow", args(1,4, arg("",flt),arg("v",bte),arg("digits",int),arg("scale",int))),
    5695             :  command("batcalc", "flt", batbte_num2dec_flt, false, "cast number to decimal(flt) and check for overflow", args(1,5, batarg("",flt),batarg("v",bte),batarg("s",oid),arg("digits",int),arg("scale",int))),
    5696             :  command("calc", "flt", bte_dec2_flt, false, "cast decimal(bte) to flt and check for overflow", args(1,3, arg("",flt),arg("s1",int),arg("v",bte))),
    5697             :  command("calc", "flt", bte_dec2dec_flt, false, "cast decimal(bte) to decimal(flt) and check for overflow", args(1,5, arg("",flt),arg("s1",int),arg("v",bte),arg("d2",int),arg("s2",int))),
    5698             :  command("batcalc", "flt", batbte_dec2_flt, false, "cast decimal(bte) to flt and check for overflow", args(1,4, batarg("",flt),arg("s1",int),batarg("v",bte),batarg("s",oid))),
    5699             :  command("batcalc", "flt", batbte_dec2dec_flt, false, "cast decimal(bte) to decimal(flt) and check for overflow", args(1,6, batarg("",flt),arg("s1",int),batarg("v",bte),batarg("s",oid),arg("d2",int),arg("s2",int))),
    5700             :  command("calc", "flt", sht_num2dec_flt, false, "cast number to decimal(flt) and check for overflow", args(1,4, arg("",flt),arg("v",sht),arg("digits",int),arg("scale",int))),
    5701             :  command("batcalc", "flt", batsht_num2dec_flt, false, "cast number to decimal(flt) and check for overflow", args(1,5, batarg("",flt),batarg("v",sht),batarg("s",oid),arg("digits",int),arg("scale",int))),
    5702             :  command("calc", "flt", sht_dec2_flt, false, "cast decimal(sht) to flt and check for overflow", args(1,3, arg("",flt),arg("s1",int),arg("v",sht))),
    5703             :  command("calc", "flt", sht_dec2dec_flt, false, "cast decimal(sht) to decimal(flt) and check for overflow", args(1,5, arg("",flt),arg("s1",int),arg("v",sht),arg("d2",int),arg("s2",int))),
    5704             :  command("batcalc", "flt", batsht_dec2_flt, false, "cast decimal(sht) to flt and check for overflow", args(1,4, batarg("",flt),arg("s1",int),batarg("v",sht),batarg("s",oid))),
    5705             :  command("batcalc", "flt", batsht_dec2dec_flt, false, "cast decimal(sht) to decimal(flt) and check for overflow", args(1,6, batarg("",flt),arg("s1",int),batarg("v",sht),batarg("s",oid),arg("d2",int),arg("s2",int))),
    5706             :  command("calc", "flt", int_num2dec_flt, false, "cast number to decimal(flt) and check for overflow", args(1,4, arg("",flt),arg("v",int),arg("digits",int),arg("scale",int))),
    5707             :  command("batcalc", "flt", batint_num2dec_flt, false, "cast number to decimal(flt) and check for overflow", args(1,5, batarg("",flt),batarg("v",int),batarg("s",oid),arg("digits",int),arg("scale",int))),
    5708             :  command("calc", "flt", int_dec2_flt, false, "cast decimal(int) to flt and check for overflow", args(1,3, arg("",flt),arg("s1",int),arg("v",int))),
    5709             :  command("calc", "flt", int_dec2dec_flt, false, "cast decimal(int) to decimal(flt) and check for overflow", args(1,5, arg("",flt),arg("s1",int),arg("v",int),arg("d2",int),arg("s2",int))),
    5710             :  command("batcalc", "flt", batint_dec2_flt, false, "cast decimal(int) to flt and check for overflow", args(1,4, batarg("",flt),arg("s1",int),batarg("v",int),batarg("s",oid))),
    5711             :  command("batcalc", "flt", batint_dec2dec_flt, false, "cast decimal(int) to decimal(flt) and check for overflow", args(1,6, batarg("",flt),arg("s1",int),batarg("v",int),batarg("s",oid),arg("d2",int),arg("s2",int))),
    5712             :  command("calc", "flt", lng_num2dec_flt, false, "cast number to decimal(flt) and check for overflow", args(1,4, arg("",flt),arg("v",lng),arg("digits",int),arg("scale",int))),
    5713             :  command("batcalc", "flt", batlng_num2dec_flt, false, "cast number to decimal(flt) and check for overflow", args(1,5, batarg("",flt),batarg("v",lng),batarg("s",oid),arg("digits",int),arg("scale",int))),
    5714             :  command("calc", "flt", lng_dec2_flt, false, "cast decimal(lng) to flt and check for overflow", args(1,3, arg("",flt),arg("s1",int),arg("v",lng))),
    5715             :  command("calc", "flt", lng_dec2dec_flt, false, "cast decimal(lng) to decimal(flt) and check for overflow", args(1,5, arg("",flt),arg("s1",int),arg("v",lng),arg("d2",int),arg("s2",int))),
    5716             :  command("batcalc", "flt", batlng_dec2_flt, false, "cast decimal(lng) to flt and check for overflow", args(1,4, batarg("",flt),arg("s1",int),batarg("v",lng),batarg("s",oid))),
    5717             :  command("batcalc", "flt", batlng_dec2dec_flt, false, "cast decimal(lng) to decimal(flt) and check for overflow", args(1,6, batarg("",flt),arg("s1",int),batarg("v",lng),batarg("s",oid),arg("d2",int),arg("s2",int))),
    5718             :  command("calc", "dbl", bte_num2dec_dbl, false, "cast number to decimal(dbl) and check for overflow", args(1,4, arg("",dbl),arg("v",bte),arg("digits",int),arg("scale",int))),
    5719             :  command("batcalc", "dbl", batbte_num2dec_dbl, false, "cast number to decimal(dbl) and check for overflow", args(1,5, batarg("",dbl),batarg("v",bte),batarg("s",oid),arg("digits",int),arg("scale",int))),
    5720             :  command("calc", "dbl", bte_dec2_dbl, false, "cast decimal(bte) to dbl and check for overflow", args(1,3, arg("",dbl),arg("s1",int),arg("v",bte))),
    5721             :  command("calc", "dbl", bte_dec2dec_dbl, false, "cast decimal(bte) to decimal(dbl) and check for overflow", args(1,5, arg("",dbl),arg("s1",int),arg("v",bte),arg("d2",int),arg("s2",int))),
    5722             :  command("batcalc", "dbl", batbte_dec2_dbl, false, "cast decimal(bte) to dbl and check for overflow", args(1,4, batarg("",dbl),arg("s1",int),batarg("v",bte),batarg("s",oid))),
    5723             :  command("batcalc", "dbl", batbte_dec2dec_dbl, false, "cast decimal(bte) to decimal(dbl) and check for overflow", args(1,6, batarg("",dbl),arg("s1",int),batarg("v",bte),batarg("s",oid),arg("d2",int),arg("s2",int))),
    5724             :  command("calc", "dbl", sht_num2dec_dbl, false, "cast number to decimal(dbl) and check for overflow", args(1,4, arg("",dbl),arg("v",sht),arg("digits",int),arg("scale",int))),
    5725             :  command("batcalc", "dbl", batsht_num2dec_dbl, false, "cast number to decimal(dbl) and check for overflow", args(1,5, batarg("",dbl),batarg("v",sht),batarg("s",oid),arg("digits",int),arg("scale",int))),
    5726             :  command("calc", "dbl", sht_dec2_dbl, false, "cast decimal(sht) to dbl and check for overflow", args(1,3, arg("",dbl),arg("s1",int),arg("v",sht))),
    5727             :  command("calc", "dbl", sht_dec2dec_dbl, false, "cast decimal(sht) to decimal(dbl) and check for overflow", args(1,5, arg("",dbl),arg("s1",int),arg("v",sht),arg("d2",int),arg("s2",int))),
    5728             :  command("batcalc", "dbl", batsht_dec2_dbl, false, "cast decimal(sht) to dbl and check for overflow", args(1,4, batarg("",dbl),arg("s1",int),batarg("v",sht),batarg("s",oid))),
    5729             :  command("batcalc", "dbl", batsht_dec2dec_dbl, false, "cast decimal(sht) to decimal(dbl) and check for overflow", args(1,6, batarg("",dbl),arg("s1",int),batarg("v",sht),batarg("s",oid),arg("d2",int),arg("s2",int))),
    5730             :  command("calc", "dbl", int_num2dec_dbl, false, "cast number to decimal(dbl) and check for overflow", args(1,4, arg("",dbl),arg("v",int),arg("digits",int),arg("scale",int))),
    5731             :  command("batcalc", "dbl", batint_num2dec_dbl, false, "cast number to decimal(dbl) and check for overflow", args(1,5, batarg("",dbl),batarg("v",int),batarg("s",oid),arg("digits",int),arg("scale",int))),
    5732             :  command("calc", "dbl", int_dec2_dbl, false, "cast decimal(int) to dbl and check for overflow", args(1,3, arg("",dbl),arg("s1",int),arg("v",int))),
    5733             :  command("calc", "dbl", int_dec2dec_dbl, false, "cast decimal(int) to decimal(dbl) and check for overflow", args(1,5, arg("",dbl),arg("s1",int),arg("v",int),arg("d2",int),arg("s2",int))),
    5734             :  command("batcalc", "dbl", batint_dec2_dbl, false, "cast decimal(int) to dbl and check for overflow", args(1,4, batarg("",dbl),arg("s1",int),batarg("v",int),batarg("s",oid))),
    5735             :  command("batcalc", "dbl", batint_dec2dec_dbl, false, "cast decimal(int) to decimal(dbl) and check for overflow", args(1,6, batarg("",dbl),arg("s1",int),batarg("v",int),batarg("s",oid),arg("d2",int),arg("s2",int))),
    5736             :  command("calc", "dbl", lng_num2dec_dbl, false, "cast number to decimal(dbl) and check for overflow", args(1,4, arg("",dbl),arg("v",lng),arg("digits",int),arg("scale",int))),
    5737             :  command("batcalc", "dbl", batlng_num2dec_dbl, false, "cast number to decimal(dbl) and check for overflow", args(1,5, batarg("",dbl),batarg("v",lng),batarg("s",oid),arg("digits",int),arg("scale",int))),
    5738             :  command("calc", "dbl", lng_dec2_dbl, false, "cast decimal(lng) to dbl and check for overflow", args(1,3, arg("",dbl),arg("s1",int),arg("v",lng))),
    5739             :  command("calc", "dbl", lng_dec2dec_dbl, false, "cast decimal(lng) to decimal(dbl) and check for overflow", args(1,5, arg("",dbl),arg("s1",int),arg("v",lng),arg("d2",int),arg("s2",int))),
    5740             :  command("batcalc", "dbl", batlng_dec2_dbl, false, "cast decimal(lng) to dbl and check for overflow", args(1,4, batarg("",dbl),arg("s1",int),batarg("v",lng),batarg("s",oid))),
    5741             :  command("batcalc", "dbl", batlng_dec2dec_dbl, false, "cast decimal(lng) to decimal(dbl) and check for overflow", args(1,6, batarg("",dbl),arg("s1",int),batarg("v",lng),batarg("s",oid),arg("d2",int),arg("s2",int))),
    5742             :  /* sql_rank */
    5743             :  pattern("sql", "diff", SQLdiff, false, "return true if cur != prev row", args(1,2, arg("",bit),argany("b",1))),
    5744             :  pattern("batsql", "diff", SQLdiff, false, "return true if cur != prev row", args(1,2, batarg("",bit),batargany("b",1))),
    5745             :  pattern("sql", "diff", SQLdiff, false, "return true if cur != prev row", args(1,3, arg("",bit),arg("p",bit),argany("b",1))),
    5746             :  pattern("batsql", "diff", SQLdiff, false, "return true if cur != prev row", args(1,3, batarg("",bit),arg("p",bit),batargany("b",1))),
    5747             :  pattern("batsql", "diff", SQLdiff, false, "return true if cur != prev row", args(1,3, batarg("",bit),batarg("p",bit),argany("b",1))),
    5748             :  pattern("batsql", "diff", SQLdiff, false, "return true if cur != prev row", args(1,3, batarg("",bit),batarg("p",bit),batargany("b",1))),
    5749             :  pattern("sql", "window_bound", SQLwindow_bound, false, "computes window ranges for each row", args(1,6, arg("",oid),argany("b",1),arg("unit",int),arg("bound",int),arg("excl",int),arg("limit",bte))),
    5750             :  pattern("batsql", "window_bound", SQLwindow_bound, false, "computes window ranges for each row", args(1,6, batarg("",oid),batargany("b",1),arg("unit",int),arg("bound",int),arg("excl",int),arg("limit",bte))),
    5751             :  pattern("sql", "window_bound", SQLwindow_bound, false, "computes window ranges for each row", args(1,7, arg("",oid),arg("p",bit),argany("b",1),arg("unit",int),arg("bound",int),arg("excl",int),arg("limit",bte))),
    5752             :  pattern("batsql", "window_bound", SQLwindow_bound, false, "computes window ranges for each row", args(1,7, batarg("",oid),batarg("p",bit),batargany("b",1),arg("unit",int),arg("bound",int),arg("excl",int),arg("limit",bte))),
    5753             :  pattern("batsql", "window_bound", SQLwindow_bound, false, "computes window ranges for each row", args(1,6, batarg("",oid),batargany("b",1),arg("unit",int),arg("bound",int),arg("excl",int),batarg("limit",bte))),
    5754             :  pattern("batsql", "window_bound", SQLwindow_bound, false, "computes window ranges for each row", args(1,7, batarg("",oid),batarg("p",bit),batargany("b",1),arg("unit",int),arg("bound",int),arg("excl",int),batarg("limit",bte))),
    5755             :  pattern("sql", "window_bound", SQLwindow_bound, false, "computes window ranges for each row", args(1,6, arg("",oid),argany("b",1),arg("unit",int),arg("bound",int),arg("excl",int),arg("limit",sht))),
    5756             :  pattern("batsql", "window_bound", SQLwindow_bound, false, "computes window ranges for each row", args(1,6, batarg("",oid),batargany("b",1),arg("unit",int),arg("bound",int),arg("excl",int),arg("limit",sht))),
    5757             :  pattern("sql", "window_bound", SQLwindow_bound, false, "computes window ranges for each row", args(1,7, arg("",oid),arg("p",bit),argany("b",1),arg("unit",int),arg("bound",int),arg("excl",int),arg("limit",sht))),
    5758             :  pattern("batsql", "window_bound", SQLwindow_bound, false, "computes window ranges for each row", args(1,7, batarg("",oid),batarg("p",bit),batargany("b",1),arg("unit",int),arg("bound",int),arg("excl",int),arg("limit",sht))),
    5759             :  pattern("batsql", "window_bound", SQLwindow_bound, false, "computes window ranges for each row", args(1,6, batarg("",oid),batargany("b",1),arg("unit",int),arg("bound",int),arg("excl",int),batarg("limit",sht))),
    5760             :  pattern("batsql", "window_bound", SQLwindow_bound, false, "computes window ranges for each row", args(1,7, batarg("",oid),batarg("p",bit),batargany("b",1),arg("unit",int),arg("bound",int),arg("excl",int),batarg("limit",sht))),
    5761             :  pattern("sql", "window_bound", SQLwindow_bound, false, "computes window ranges for each row", args(1,6, arg("",oid),argany("b",1),arg("unit",int),arg("bound",int),arg("excl",int),arg("limit",int))),
    5762             :  pattern("batsql", "window_bound", SQLwindow_bound, false, "computes window ranges for each row", args(1,6, batarg("",oid),batargany("b",1),arg("unit",int),arg("bound",int),arg("excl",int),arg("limit",int))),
    5763             :  pattern("sql", "window_bound", SQLwindow_bound, false, "computes window ranges for each row", args(1,7, arg("",oid),arg("p",bit),argany("b",1),arg("unit",int),arg("bound",int),arg("excl",int),arg("limit",int))),
    5764             :  pattern("batsql", "window_bound", SQLwindow_bound, false, "computes window ranges for each row", args(1,7, batarg("",oid),batarg("p",bit),batargany("b",1),arg("unit",int),arg("bound",int),arg("excl",int),arg("limit",int))),
    5765             :  pattern("batsql", "window_bound", SQLwindow_bound, false, "computes window ranges for each row", args(1,6, batarg("",oid),batargany("b",1),arg("unit",int),arg("bound",int),arg("excl",int),batarg("limit",int))),
    5766             :  pattern("batsql", "window_bound", SQLwindow_bound, false, "computes window ranges for each row", args(1,7, batarg("",oid),batarg("p",bit),batargany("b",1),arg("unit",int),arg("bound",int),arg("excl",int),batarg("limit",int))),
    5767             :  pattern("sql", "window_bound", SQLwindow_bound, false, "computes window ranges for each row", args(1,6, arg("",oid),argany("b",1),arg("unit",int),arg("bound",int),arg("excl",int),arg("limit",lng))),
    5768             :  pattern("batsql", "window_bound", SQLwindow_bound, false, "computes window ranges for each row", args(1,6, batarg("",oid),batargany("b",1),arg("unit",int),arg("bound",int),arg("excl",int),arg("limit",lng))),
    5769             :  pattern("sql", "window_bound", SQLwindow_bound, false, "computes window ranges for each row", args(1,7, arg("",oid),arg("p",bit),argany("b",1),arg("unit",int),arg("bound",int),arg("excl",int),arg("limit",lng))),
    5770             :  pattern("batsql", "window_bound", SQLwindow_bound, false, "computes window ranges for each row", args(1,7, batarg("",oid),batarg("p",bit),batargany("b",1),arg("unit",int),arg("bound",int),arg("excl",int),arg("limit",lng))),
    5771             :  pattern("batsql", "window_bound", SQLwindow_bound, false, "computes window ranges for each row", args(1,6, batarg("",oid),batargany("b",1),arg("unit",int),arg("bound",int),arg("excl",int),batarg("limit",lng))),
    5772             :  pattern("batsql", "window_bound", SQLwindow_bound, false, "computes window ranges for each row", args(1,7, batarg("",oid),batarg("p",bit),batargany("b",1),arg("unit",int),arg("bound",int),arg("excl",int),batarg("limit",lng))),
    5773             :  pattern("sql", "window_bound", SQLwindow_bound, false, "computes window ranges for each row", args(1,6, arg("",oid),argany("b",1),arg("unit",int),arg("bound",int),arg("excl",int),arg("limit",flt))),
    5774             :  pattern("batsql", "window_bound", SQLwindow_bound, false, "computes window ranges for each row", args(1,6, batarg("",oid),batargany("b",1),arg("unit",int),arg("bound",int),arg("excl",int),arg("limit",flt))),
    5775             :  pattern("sql", "window_bound", SQLwindow_bound, false, "computes window ranges for each row", args(1,7, arg("",oid),arg("p",bit),argany("b",1),arg("unit",int),arg("bound",int),arg("excl",int),arg("limit",flt))),
    5776             :  pattern("batsql", "window_bound", SQLwindow_bound, false, "computes window ranges for each row", args(1,7, batarg("",oid),batarg("p",bit),batargany("b",1),arg("unit",int),arg("bound",int),arg("excl",int),arg("limit",flt))),
    5777             :  pattern("batsql", "window_bound", SQLwindow_bound, false, "computes window ranges for each row", args(1,6, batarg("",oid),batargany("b",1),arg("unit",int),arg("bound",int),arg("excl",int),batarg("limit",flt))),
    5778             :  pattern("batsql", "window_bound", SQLwindow_bound, false, "computes window ranges for each row", args(1,7, batarg("",oid),batarg("p",bit),batargany("b",1),arg("unit",int),arg("bound",int),arg("excl",int),batarg("limit",flt))),
    5779             :  pattern("sql", "window_bound", SQLwindow_bound, false, "computes window ranges for each row", args(1,6, arg("",oid),argany("b",1),arg("unit",int),arg("bound",int),arg("excl",int),arg("limit",dbl))),
    5780             :  pattern("batsql", "window_bound", SQLwindow_bound, false, "computes window ranges for each row", args(1,6, batarg("",oid),batargany("b",1),arg("unit",int),arg("bound",int),arg("excl",int),arg("limit",dbl))),
    5781             :  pattern("sql", "window_bound", SQLwindow_bound, false, "computes window ranges for each row", args(1,7, arg("",oid),arg("p",bit),argany("b",1),arg("unit",int),arg("bound",int),arg("excl",int),arg("limit",dbl))),
    5782             :  pattern("batsql", "window_bound", SQLwindow_bound, false, "computes window ranges for each row", args(1,7, batarg("",oid),batarg("p",bit),batargany("b",1),arg("unit",int),arg("bound",int),arg("excl",int),arg("limit",dbl))),
    5783             :  pattern("batsql", "window_bound", SQLwindow_bound, false, "computes window ranges for each row", args(1,6, batarg("",oid),batargany("b",1),arg("unit",int),arg("bound",int),arg("excl",int),batarg("limit",dbl))),
    5784             :  pattern("batsql", "window_bound", SQLwindow_bound, false, "computes window ranges for each row", args(1,7, batarg("",oid),batarg("p",bit),batargany("b",1),arg("unit",int),arg("bound",int),arg("excl",int),batarg("limit",dbl))),
    5785             :  pattern("sql", "row_number", SQLrow_number, false, "return the row_numer-ed groups", args(1,4, arg("",int),argany("b",1),arg("p",bit),arg("o",bit))),
    5786             :  pattern("batsql", "row_number", SQLrow_number, false, "return the row_numer-ed groups", args(1,4, batarg("",int),batargany("b",1),argany("p",2),argany("o",3))),
    5787             :  pattern("sql", "rank", SQLrank, false, "return the ranked groups", args(1,4, arg("",int),argany("b",1),arg("p",bit),arg("o",bit))),
    5788             :  pattern("batsql", "rank", SQLrank, false, "return the ranked groups", args(1,4, batarg("",int),batargany("b",1),argany("p",2),argany("o",3))),
    5789             :  pattern("sql", "dense_rank", SQLdense_rank, false, "return the densely ranked groups", args(1,4, arg("",int),argany("b",1),arg("p",bit),arg("o",bit))),
    5790             :  pattern("batsql", "dense_rank", SQLdense_rank, false, "return the densely ranked groups", args(1,4, batarg("",int),batargany("b",1),argany("p",2),argany("o",3))),
    5791             :  pattern("sql", "percent_rank", SQLpercent_rank, false, "return the percentage into the total number of groups for each row", args(1,4, arg("",dbl),argany("b",1),arg("p",bit),arg("o",bit))),
    5792             :  pattern("batsql", "percent_rank", SQLpercent_rank, false, "return the percentage into the total number of groups for each row", args(1,4, batarg("",dbl),batargany("b",1),argany("p",2),argany("o",3))),
    5793             :  pattern("sql", "cume_dist", SQLcume_dist, false, "return the accumulated distribution of the number of rows per group to the total number of partition rows", args(1,4, arg("",dbl),argany("b",1),arg("p",bit),arg("o",bit))),
    5794             :  pattern("batsql", "cume_dist", SQLcume_dist, false, "return the accumulated distribution of the number of rows per group to the total number of partition rows", args(1,4, batarg("",dbl),batargany("b",1),argany("p",2),argany("o",3))),
    5795             :  pattern("sql", "lag", SQLlag, false, "return the value in the previous row in the partition or NULL if non existent", args(1,4, argany("",1),argany("b",1),arg("p",bit),arg("o",bit))),
    5796             :  pattern("batsql", "lag", SQLlag, false, "return the value in the previous row in the partition or NULL if non existent", args(1,4, batargany("",1),batargany("b",1),argany("p",2),argany("o",3))),
    5797             :  pattern("sql", "lag", SQLlag, false, "return the value in the previous 'l' row in the partition or NULL if non existent", args(1,5, argany("",1),argany("b",1),argany("l",0),arg("p",bit),arg("o",bit))),
    5798             :  pattern("batsql", "lag", SQLlag, false, "return the value in the previous 'l' row in the partition or NULL if non existent", args(1,5, batargany("",1),batargany("b",1),argany("l",0),argany("p",2),argany("o",3))),
    5799             :  pattern("batsql", "lag", SQLlag, false, "return the value in the previous 'l' row in the partition or NULL if non existent", args(1,5, batargany("",1),argany("b",1),batargany("l",0),argany("p",2),argany("o",3))),
    5800             :  pattern("batsql", "lag", SQLlag, false, "return the value in the previous 'l' row in the partition or NULL if non existent", args(1,5, batargany("",1),batargany("b",1),batargany("l",0),argany("p",2),argany("o",3))),
    5801             :  pattern("sql", "lag", SQLlag, false, "return the value in the previous 'l' row in the partition or 'd' if non existent", args(1,6, argany("",1),argany("b",1),argany("l",0),argany("d",1),arg("p",bit),arg("o",bit))),
    5802             :  pattern("batsql", "lag", SQLlag, false, "return the value in the previous 'l' row in the partition or 'd' if non existent", args(1,6, batargany("",1),batargany("b",1),argany("l",0),argany("d",1),argany("p",2),argany("o",3))),
    5803             :  pattern("batsql", "lag", SQLlag, false, "return the value in the previous 'l' row in the partition or 'd' if non existent", args(1,6, batargany("",1),argany("b",1),batargany("l",0),argany("d",1),argany("p",2),argany("o",3))),
    5804             :  pattern("batsql", "lag", SQLlag, false, "return the value in the previous 'l' row in the partition or 'd' if non existent", args(1,6, batargany("",1),batargany("b",1),batargany("l",0),argany("d",1),argany("p",2),argany("o",3))),
    5805             :  pattern("batsql", "lag", SQLlag, false, "return the value in the previous 'l' row in the partition or 'd' if non existent", args(1,6, batargany("",1),argany("b",1),argany("l",0),batargany("d",1),argany("p",2),argany("o",3))),
    5806             :  pattern("batsql", "lag", SQLlag, false, "return the value in the previous 'l' row in the partition or 'd' if non existent", args(1,6, batargany("",1),batargany("b",1),argany("l",0),batargany("d",1),argany("p",2),argany("o",3))),
    5807             :  pattern("batsql", "lag", SQLlag, false, "return the value in the previous 'l' row in the partition or 'd' if non existent", args(1,6, batargany("",1),argany("b",1),batargany("l",0),batargany("d",1),argany("p",2),argany("o",3))),
    5808             :  pattern("batsql", "lag", SQLlag, false, "return the value in the previous 'l' row in the partition or 'd' if non existent", args(1,6, batargany("",1),batargany("b",1),batargany("l",0),batargany("d",1),argany("p",2),argany("o",3))),
    5809             :  pattern("sql", "lead", SQLlead, false, "return the value in the next row in the partition or NULL if non existent", args(1,4, argany("",1),argany("b",1),arg("p",bit),arg("o",bit))),
    5810             :  pattern("batsql", "lead", SQLlead, false, "return the value in the next row in the partition or NULL if non existent", args(1,4, batargany("",1),batargany("b",1),argany("p",2),argany("o",3))),
    5811             :  pattern("sql", "lead", SQLlead, false, "return the value in the next 'l' row in the partition or NULL if non existent", args(1,5, argany("",1),argany("b",1),argany("l",0),arg("p",bit),arg("o",bit))),
    5812             :  pattern("batsql", "lead", SQLlead, false, "return the value in the next 'l' row in the partition or NULL if non existent", args(1,5, batargany("",1),batargany("b",1),argany("l",0),argany("p",2),argany("o",3))),
    5813             :  pattern("batsql", "lead", SQLlead, false, "return the value in the next 'l' row in the partition or NULL if non existent", args(1,5, batargany("",1),argany("b",1),batargany("l",0),argany("p",2),argany("o",3))),
    5814             :  pattern("batsql", "lead", SQLlead, false, "return the value in the next 'l' row in the partition or NULL if non existent", args(1,5, batargany("",1),batargany("b",1),batargany("l",0),argany("p",2),argany("o",3))),
    5815             :  pattern("sql", "lead", SQLlead, false, "return the value in the next 'l' row in the partition or 'd' if non existent", args(1,6, argany("",1),argany("b",1),argany("l",0),argany("d",1),arg("p",bit),arg("o",bit))),
    5816             :  pattern("batsql", "lead", SQLlead, false, "return the value in the next 'l' row in the partition or 'd' if non existent", args(1,6, batargany("",1),batargany("b",1),argany("l",0),argany("d",1),argany("p",2),argany("o",3))),
    5817             :  pattern("batsql", "lead", SQLlead, false, "return the value in the next 'l' row in the partition or 'd' if non existent", args(1,6, batargany("",1),argany("b",1),batargany("l",0),argany("d",1),argany("p",2),argany("o",3))),
    5818             :  pattern("batsql", "lead", SQLlead, false, "return the value in the next 'l' row in the partition or 'd' if non existent", args(1,6, batargany("",1),batargany("b",1),batargany("l",0),argany("d",1),argany("p",2),argany("o",3))),
    5819             :  pattern("batsql", "lead", SQLlead, false, "return the value in the next 'l' row in the partition or 'd' if non existent", args(1,6, batargany("",1),argany("b",1),argany("l",0),batargany("d",1),argany("p",2),argany("o",3))),
    5820             :  pattern("batsql", "lead", SQLlead, false, "return the value in the next 'l' row in the partition or 'd' if non existent", args(1,6, batargany("",1),batargany("b",1),argany("l",0),batargany("d",1),argany("p",2),argany("o",3))),
    5821             :  pattern("batsql", "lead", SQLlead, false, "return the value in the next 'l' row in the partition or 'd' if non existent", args(1,6, batargany("",1),argany("b",1),batargany("l",0),batargany("d",1),argany("p",2),argany("o",3))),
    5822             :  pattern("batsql", "lead", SQLlead, false, "return the value in the next 'l' row in the partition or 'd' if non existent", args(1,6, batargany("",1),batargany("b",1),batargany("l",0),batargany("d",1),argany("p",2),argany("o",3))),
    5823             :  pattern("sql", "ntile", SQLntile, false, "return the groups divided as equally as possible", args(1,5, argany("",1),argany("b",0),argany("n",1),arg("p",bit),arg("o",bit))),
    5824             :  pattern("batsql", "ntile", SQLntile, false, "return the groups divided as equally as possible", args(1,5, batargany("",1),batargany("b",0),argany("n",1),argany("p",2),argany("o",3))),
    5825             :  pattern("batsql", "ntile", SQLntile, false, "return the groups divided as equally as possible", args(1,5, batargany("",1),argany("b",0),batargany("n",1),argany("p",2),argany("o",3))),
    5826             :  pattern("batsql", "ntile", SQLntile, false, "return the groups divided as equally as possible", args(1,5, batargany("",1),batargany("b",0),batargany("n",1),argany("p",2),argany("o",3))),
    5827             : 
    5828             :  /* these window functions support frames */
    5829             :  pattern("sql", "first_value", SQLfirst_value, false, "return the first value of groups", args(1,7, argany("",1),argany("b",1),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5830             :  pattern("batsql", "first_value", SQLfirst_value, false, "return the first value of groups", args(1,7, batargany("",1),batargany("b",1),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5831             :  pattern("sql", "last_value", SQLlast_value, false, "return the last value of groups", args(1,7, argany("",1),argany("b",1),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5832             :  pattern("batsql", "last_value", SQLlast_value, false, "return the last value of groups", args(1,7, batargany("",1),batargany("b",1),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5833             :  pattern("sql", "nth_value", SQLnth_value, false, "return the nth value of each group", args(1,8, argany("",1),argany("b",1),arg("n",lng),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5834             :  pattern("batsql", "nth_value", SQLnth_value, false, "return the nth value of each group", args(1,8, batargany("",1),batargany("b",1),arg("n",lng),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5835             :  pattern("batsql", "nth_value", SQLnth_value, false, "return the nth value of each group", args(1,8, batargany("",1),argany("b",1),batarg("n",lng),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5836             :  pattern("batsql", "nth_value", SQLnth_value, false, "return the nth value of each group", args(1,8, batargany("",1),batargany("b",1),batarg("n",lng),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5837             :  pattern("sql", "min", SQLmin, false, "return the minimum of groups", args(1,7, argany("",1),argany("b",1),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5838             :  pattern("batsql", "min", SQLmin, false, "return the minimum of groups", args(1,7, batargany("",1),batargany("b",1),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5839             :  pattern("sql", "max", SQLmax, false, "return the maximum of groups", args(1,7, argany("",1),argany("b",1),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5840             :  pattern("batsql", "max", SQLmax, false, "return the maximum of groups",args(1,7, batargany("",1),batargany("b",1),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5841             :  pattern("sql", "count", SQLbasecount, false, "return count of basetable", args(1,3, arg("",lng),arg("sname",str),arg("tname",str))),
    5842             :  pattern("sql", "count", SQLcount, false, "return count of groups", args(1,8, arg("",lng),argany("b",1),arg("ignils",bit),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5843             :  pattern("batsql", "count", SQLcount, false,"return count of groups",args(1,8, batarg("",lng),batargany("b",1),arg("ignils",bit),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5844             :  pattern("sql", "sum", SQLsum, false, "return the sum of groups", args(1,7, arg("",lng),arg("b",bte),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5845             :  pattern("batsql", "sum", SQLsum, false, "return the sum of groups", args(1,7, batarg("",lng),batarg("b",bte),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5846             :  pattern("sql", "sum", SQLsum, false, "return the sum of groups", args(1,7, arg("",lng),arg("b",sht),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5847             :  pattern("batsql", "sum", SQLsum, false, "return the sum of groups", args(1,7, batarg("",lng),batarg("b",sht),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5848             :  pattern("sql", "sum", SQLsum, false, "return the sum of groups", args(1,7, arg("",lng),arg("b",int),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5849             :  pattern("batsql", "sum", SQLsum, false, "return the sum of groups", args(1,7, batarg("",lng),batarg("b",int),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5850             :  pattern("sql", "sum", SQLsum, false, "return the sum of groups", args(1,7, arg("",lng),arg("b",lng),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5851             :  pattern("batsql", "sum", SQLsum, false, "return the sum of groups", args(1,7, batarg("",lng),batarg("b",lng),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5852             :  pattern("sql", "sum", SQLsum, false, "return the sum of groups", args(1,7, arg("",flt),arg("b",flt),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5853             :  pattern("batsql", "sum", SQLsum, false, "return the sum of groups", args(1,7, batarg("",flt),batarg("b",flt),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5854             :  pattern("sql", "sum", SQLsum, false, "return the sum of groups", args(1,7, arg("",dbl),arg("b",flt),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5855             :  pattern("batsql", "sum", SQLsum, false, "return the sum of groups", args(1,7, batarg("",dbl),batarg("b",flt),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5856             :  pattern("sql", "sum", SQLsum, false, "return the sum of groups", args(1,7, arg("",dbl),arg("b",dbl),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5857             :  pattern("batsql", "sum", SQLsum, false, "return the sum of groups", args(1,7, batarg("",dbl),batarg("b",dbl),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5858             :  pattern("sql", "prod", SQLprod, false, "return the product of groups", args(1,7, arg("",lng),arg("b",bte),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5859             :  pattern("batsql", "prod", SQLprod, false, "return the product of groups", args(1,7, batarg("",lng),batarg("b",bte),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5860             :  pattern("sql", "prod", SQLprod, false, "return the product of groups", args(1,7, arg("",lng),arg("b",sht),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5861             :  pattern("batsql", "prod", SQLprod, false, "return the product of groups", args(1,7, batarg("",lng),batarg("b",sht),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5862             :  pattern("sql", "prod", SQLprod, false, "return the product of groups", args(1,7, arg("",lng),arg("b",int),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5863             :  pattern("batsql", "prod", SQLprod, false, "return the product of groups", args(1,7, batarg("",lng),batarg("b",int),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5864             :  pattern("sql", "prod", SQLprod, false, "return the product of groups", args(1,7, arg("",lng),arg("b",lng),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5865             :  pattern("batsql", "prod", SQLprod, false, "return the product of groups", args(1,7, batarg("",lng),batarg("b",lng),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5866             :  pattern("sql", "prod", SQLprod, false, "return the product of groups", args(1,7, arg("",flt),arg("b",flt),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5867             :  pattern("batsql", "prod", SQLprod, false, "return the product of groups", args(1,7, batarg("",flt),batarg("b",flt),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5868             :  pattern("sql", "prod", SQLprod, false, "return the product of groups", args(1,7, arg("",dbl),arg("b",flt),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5869             :  pattern("batsql", "prod", SQLprod, false, "return the product of groups", args(1,7, batarg("",dbl),batarg("b",flt),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5870             :  pattern("sql", "prod", SQLprod, false, "return the product of groups", args(1,7, arg("",dbl),arg("b",dbl),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5871             :  pattern("batsql", "prod", SQLprod, false, "return the product of groups", args(1,7, batarg("",dbl),batarg("b",dbl),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5872             :  pattern("sql", "avg", SQLavg, false, "return the average of groups", args(1,7, arg("",dbl),arg("b",bte),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5873             :  pattern("batsql", "avg", SQLavg, false, "return the average of groups", args(1,7, batarg("",dbl),batarg("b",bte),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5874             :  pattern("sql", "avg", SQLavg, false, "return the average of groups", args(1,7, arg("",dbl),arg("b",sht),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5875             :  pattern("batsql", "avg", SQLavg, false, "return the average of groups", args(1,7, batarg("",dbl),batarg("b",sht),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5876             :  pattern("sql", "avg", SQLavg, false, "return the average of groups", args(1,7, arg("",dbl),arg("b",int),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5877             :  pattern("batsql", "avg", SQLavg, false, "return the average of groups", args(1,7, batarg("",dbl),batarg("b",int),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5878             :  pattern("sql", "avg", SQLavg, false, "return the average of groups", args(1,7, arg("",dbl),arg("b",lng),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5879             :  pattern("batsql", "avg", SQLavg, false, "return the average of groups", args(1,7, batarg("",dbl),batarg("b",lng),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5880             :  pattern("sql", "avg", SQLavg, false, "return the average of groups", args(1,7, arg("",dbl),arg("b",flt),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5881             :  pattern("batsql", "avg", SQLavg, false, "return the average of groups", args(1,7, batarg("",dbl),batarg("b",flt),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5882             :  pattern("sql", "avg", SQLavg, false, "return the average of groups", args(1,7, arg("",dbl),arg("b",dbl),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5883             :  pattern("batsql", "avg", SQLavg, false, "return the average of groups", args(1,7, batarg("",dbl),batarg("b",dbl),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5884             :  pattern("sql", "avg", SQLavginteger, false, "return the average of groups", args(1,7, arg("",bte),arg("b",bte),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5885             :  pattern("batsql", "avg", SQLavginteger, false, "return the average of groups", args(1,7, batarg("",bte),batarg("b",bte),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5886             :  pattern("sql", "avg", SQLavginteger, false, "return the average of groups", args(1,7, arg("",sht),arg("b",sht),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5887             :  pattern("batsql", "avg", SQLavginteger, false, "return the average of groups", args(1,7, batarg("",sht),batarg("b",sht),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5888             :  pattern("sql", "avg", SQLavginteger, false, "return the average of groups", args(1,7, arg("",int),arg("b",int),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5889             :  pattern("batsql", "avg", SQLavginteger, false, "return the average of groups", args(1,7, batarg("",int),batarg("b",int),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5890             :  pattern("sql", "avg", SQLavginteger, false, "return the average of groups", args(1,7, arg("",lng),arg("b",lng),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5891             :  pattern("batsql", "avg", SQLavginteger, false, "return the average of groups", args(1,7, batarg("",lng),batarg("b",lng),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5892             :  pattern("sql", "stdev", SQLstddev_samp, false, "return the standard deviation sample of groups", args(1,7, arg("",dbl),arg("b",bte),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5893             :  pattern("batsql", "stdev", SQLstddev_samp, false, "return the standard deviation sample of groups", args(1,7, batarg("",dbl),batarg("b",bte),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5894             :  pattern("sql", "stdev", SQLstddev_samp, false, "return the standard deviation sample of groups", args(1,7, arg("",dbl),arg("b",sht),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5895             :  pattern("batsql", "stdev", SQLstddev_samp, false, "return the standard deviation sample of groups", args(1,7, batarg("",dbl),batarg("b",sht),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5896             :  pattern("sql", "stdev", SQLstddev_samp, false, "return the standard deviation sample of groups", args(1,7, arg("",dbl),arg("b",int),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5897             :  pattern("batsql", "stdev", SQLstddev_samp, false, "return the standard deviation sample of groups", args(1,7, batarg("",dbl),batarg("b",int),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5898             :  pattern("sql", "stdev", SQLstddev_samp, false, "return the standard deviation sample of groups", args(1,7, arg("",dbl),arg("b",lng),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5899             :  pattern("batsql", "stdev", SQLstddev_samp, false, "return the standard deviation sample of groups", args(1,7, batarg("",dbl),batarg("b",lng),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5900             :  pattern("sql", "stdev", SQLstddev_samp, false, "return the standard deviation sample of groups", args(1,7, arg("",dbl),arg("b",flt),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5901             :  pattern("batsql", "stdev", SQLstddev_samp, false, "return the standard deviation sample of groups", args(1,7, batarg("",dbl),batarg("b",flt),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5902             :  pattern("sql", "stdev", SQLstddev_samp, false, "return the standard deviation sample of groups", args(1,7, arg("",dbl),arg("b",dbl),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5903             :  pattern("batsql", "stdev", SQLstddev_samp, false, "return the standard deviation sample of groups", args(1,7, batarg("",dbl),batarg("b",dbl),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5904             :  pattern("sql", "stdevp", SQLstddev_pop, false, "return the standard deviation population of groups", args(1,7, arg("",dbl),arg("b",bte),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5905             :  pattern("batsql", "stdevp", SQLstddev_pop, false, "return the standard deviation population of groups", args(1,7, batarg("",dbl),batarg("b",bte),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5906             :  pattern("sql", "stdevp", SQLstddev_pop, false, "return the standard deviation population of groups", args(1,7, arg("",dbl),arg("b",sht),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5907             :  pattern("batsql", "stdevp", SQLstddev_pop, false, "return the standard deviation population of groups", args(1,7, batarg("",dbl),batarg("b",sht),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5908             :  pattern("sql", "stdevp", SQLstddev_pop, false, "return the standard deviation population of groups", args(1,7, arg("",dbl),arg("b",int),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5909             :  pattern("batsql", "stdevp", SQLstddev_pop, false, "return the standard deviation population of groups", args(1,7, batarg("",dbl),batarg("b",int),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5910             :  pattern("sql", "stdevp", SQLstddev_pop, false, "return the standard deviation population of groups", args(1,7, arg("",dbl),arg("b",lng),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5911             :  pattern("batsql", "stdevp", SQLstddev_pop, false, "return the standard deviation population of groups", args(1,7, batarg("",dbl),batarg("b",lng),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5912             :  pattern("sql", "stdevp", SQLstddev_pop, false, "return the standard deviation population of groups", args(1,7, arg("",dbl),arg("b",flt),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5913             :  pattern("batsql", "stdevp", SQLstddev_pop, false, "return the standard deviation population of groups", args(1,7, batarg("",dbl),batarg("b",flt),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5914             :  pattern("sql", "stdevp", SQLstddev_pop, false, "return the standard deviation population of groups", args(1,7, arg("",dbl),arg("b",dbl),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5915             :  pattern("batsql", "stdevp", SQLstddev_pop, false, "return the standard deviation population of groups", args(1,7, batarg("",dbl),batarg("b",dbl),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5916             :  pattern("sql", "variance", SQLvar_samp, false, "return the variance sample of groups", args(1,7, arg("",dbl),arg("b",bte),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5917             :  pattern("batsql", "variance", SQLvar_samp, false, "return the variance sample of groups", args(1,7, batarg("",dbl),batarg("b",bte),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5918             :  pattern("sql", "variance", SQLvar_samp, false, "return the variance sample of groups", args(1,7, arg("",dbl),arg("b",sht),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5919             :  pattern("batsql", "variance", SQLvar_samp, false, "return the variance sample of groups", args(1,7, batarg("",dbl),batarg("b",sht),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5920             :  pattern("sql", "variance", SQLvar_samp, false, "return the variance sample of groups", args(1,7, arg("",dbl),arg("b",int),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5921             :  pattern("batsql", "variance", SQLvar_samp, false, "return the variance sample of groups", args(1,7, batarg("",dbl),batarg("b",int),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5922             :  pattern("sql", "variance", SQLvar_samp, false, "return the variance sample of groups", args(1,7, arg("",dbl),arg("b",lng),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5923             :  pattern("batsql", "variance", SQLvar_samp, false, "return the variance sample of groups", args(1,7, batarg("",dbl),batarg("b",lng),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5924             :  pattern("sql", "variance", SQLvar_samp, false, "return the variance sample of groups", args(1,7, arg("",dbl),arg("b",flt),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5925             :  pattern("batsql", "variance", SQLvar_samp, false, "return the variance sample of groups", args(1,7, batarg("",dbl),batarg("b",flt),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5926             :  pattern("sql", "variance", SQLvar_samp, false, "return the variance sample of groups", args(1,7, arg("",dbl),arg("b",dbl),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5927             :  pattern("batsql", "variance", SQLvar_samp, false, "return the variance sample of groups", args(1,7, batarg("",dbl),batarg("b",dbl),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5928             :  pattern("sql", "variancep", SQLvar_pop, false, "return the variance population of groups", args(1,7, arg("",dbl),arg("b",bte),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5929             :  pattern("batsql", "variancep", SQLvar_pop, false, "return the variance population of groups", args(1,7, batarg("",dbl),batarg("b",bte),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5930             :  pattern("sql", "variancep", SQLvar_pop, false, "return the variance population of groups", args(1,7, arg("",dbl),arg("b",sht),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5931             :  pattern("batsql", "variancep", SQLvar_pop, false, "return the variance population of groups", args(1,7, batarg("",dbl),batarg("b",sht),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5932             :  pattern("sql", "variancep", SQLvar_pop, false, "return the variance population of groups", args(1,7, arg("",dbl),arg("b",int),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5933             :  pattern("batsql", "variancep", SQLvar_pop, false, "return the variance population of groups", args(1,7, batarg("",dbl),batarg("b",int),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5934             :  pattern("sql", "variancep", SQLvar_pop, false, "return the variance population of groups", args(1,7, arg("",dbl),arg("b",lng),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5935             :  pattern("batsql", "variancep", SQLvar_pop, false, "return the variance population of groups", args(1,7, batarg("",dbl),batarg("b",lng),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5936             :  pattern("sql", "variancep", SQLvar_pop, false, "return the variance population of groups", args(1,7, arg("",dbl),arg("b",flt),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5937             :  pattern("batsql", "variancep", SQLvar_pop, false, "return the variance population of groups", args(1,7, batarg("",dbl),batarg("b",flt),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5938             :  pattern("sql", "variancep", SQLvar_pop, false, "return the variance population of groups", args(1,7, arg("",dbl),arg("b",dbl),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5939             :  pattern("batsql", "variancep", SQLvar_pop, false, "return the variance population of groups", args(1,7, batarg("",dbl),batarg("b",dbl),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5940             :  pattern("sql", "covariance", SQLcovar_samp, false, "return the covariance sample value of groups", args(1,8, arg("",dbl),arg("b",bte),arg("c",bte),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5941             :  pattern("batsql", "covariance", SQLcovar_samp, false, "return the covariance sample value of groups", args(1,8, batarg("",dbl),arg("b",bte),batarg("c",bte),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5942             :  pattern("batsql", "covariance", SQLcovar_samp, false, "return the covariance sample value of groups", args(1,8, batarg("",dbl),batarg("b",bte),arg("c",bte),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5943             :  pattern("batsql", "covariance", SQLcovar_samp, false, "return the covariance sample value of groups", args(1,8, batarg("",dbl),batarg("b",bte),batarg("c",bte),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5944             :  pattern("sql", "covariance", SQLcovar_samp, false, "return the covariance sample value of groups", args(1,8, arg("",dbl),arg("b",sht),arg("c",sht),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5945             :  pattern("batsql", "covariance", SQLcovar_samp, false, "return the covariance sample value of groups", args(1,8, batarg("",dbl),arg("b",sht),batarg("c",sht),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5946             :  pattern("batsql", "covariance", SQLcovar_samp, false, "return the covariance sample value of groups", args(1,8, batarg("",dbl),batarg("b",sht),arg("c",sht),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5947             :  pattern("batsql", "covariance", SQLcovar_samp, false, "return the covariance sample value of groups", args(1,8, batarg("",dbl),batarg("b",sht),batarg("c",sht),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5948             :  pattern("sql", "covariance", SQLcovar_samp, false, "return the covariance sample value of groups", args(1,8, arg("",dbl),arg("b",int),arg("c",int),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5949             :  pattern("batsql", "covariance", SQLcovar_samp, false, "return the covariance sample value of groups", args(1,8, batarg("",dbl),arg("b",int),batarg("c",int),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5950             :  pattern("batsql", "covariance", SQLcovar_samp, false, "return the covariance sample value of groups", args(1,8, batarg("",dbl),batarg("b",int),arg("c",int),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5951             :  pattern("batsql", "covariance", SQLcovar_samp, false, "return the covariance sample value of groups", args(1,8, batarg("",dbl),batarg("b",int),batarg("c",int),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5952             :  pattern("sql", "covariance", SQLcovar_samp, false, "return the covariance sample value of groups", args(1,8, arg("",dbl),arg("b",lng),arg("c",lng),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5953             :  pattern("batsql", "covariance", SQLcovar_samp, false, "return the covariance sample value of groups", args(1,8, batarg("",dbl),arg("b",lng),batarg("c",lng),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5954             :  pattern("batsql", "covariance", SQLcovar_samp, false, "return the covariance sample value of groups", args(1,8, batarg("",dbl),batarg("b",lng),arg("c",lng),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5955             :  pattern("batsql", "covariance", SQLcovar_samp, false, "return the covariance sample value of groups", args(1,8, batarg("",dbl),batarg("b",lng),batarg("c",lng),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5956             :  pattern("sql", "covariance", SQLcovar_samp, false, "return the covariance sample value of groups", args(1,8, arg("",dbl),arg("b",flt),arg("c",flt),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5957             :  pattern("batsql", "covariance", SQLcovar_samp, false, "return the covariance sample value of groups", args(1,8, batarg("",dbl),arg("b",flt),batarg("c",flt),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5958             :  pattern("batsql", "covariance", SQLcovar_samp, false, "return the covariance sample value of groups", args(1,8, batarg("",dbl),batarg("b",flt),arg("c",flt),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5959             :  pattern("batsql", "covariance", SQLcovar_samp, false, "return the covariance sample value of groups", args(1,8, batarg("",dbl),batarg("b",flt),batarg("c",flt),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5960             :  pattern("sql", "covariance", SQLcovar_samp, false, "return the covariance sample value of groups", args(1,8, arg("",dbl),arg("b",dbl),arg("c",dbl),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5961             :  pattern("batsql", "covariance", SQLcovar_samp, false, "return the covariance sample value of groups", args(1,8, batarg("",dbl),arg("b",dbl),batarg("c",dbl),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5962             :  pattern("batsql", "covariance", SQLcovar_samp, false, "return the covariance sample value of groups", args(1,8, batarg("",dbl),batarg("b",dbl),arg("c",dbl),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5963             :  pattern("batsql", "covariance", SQLcovar_samp, false, "return the covariance sample value of groups", args(1,8, batarg("",dbl),batarg("b",dbl),batarg("c",dbl),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5964             :  pattern("sql", "covariancep", SQLcovar_pop, false, "return the covariance population value of groups", args(1,8, arg("",dbl),arg("b",bte),arg("c",bte),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5965             :  pattern("batsql", "covariancep", SQLcovar_pop, false, "return the covariance population value of groups", args(1,8, batarg("",dbl),arg("b",bte),batarg("c",bte),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5966             :  pattern("batsql", "covariancep", SQLcovar_pop, false, "return the covariance population value of groups", args(1,8, batarg("",dbl),batarg("b",bte),arg("c",bte),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5967             :  pattern("batsql", "covariancep", SQLcovar_pop, false, "return the covariance population value of groups", args(1,8, batarg("",dbl),batarg("b",bte),batarg("c",bte),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5968             :  pattern("sql", "covariancep", SQLcovar_pop, false, "return the covariance population value of groups", args(1,8, arg("",dbl),arg("b",sht),arg("c",sht),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5969             :  pattern("batsql", "covariancep", SQLcovar_pop, false, "return the covariance population value of groups", args(1,8, batarg("",dbl),arg("b",sht),batarg("c",sht),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5970             :  pattern("batsql", "covariancep", SQLcovar_pop, false, "return the covariance population value of groups", args(1,8, batarg("",dbl),batarg("b",sht),arg("c",sht),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5971             :  pattern("batsql", "covariancep", SQLcovar_pop, false, "return the covariance population value of groups", args(1,8, batarg("",dbl),batarg("b",sht),batarg("c",sht),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5972             :  pattern("sql", "covariancep", SQLcovar_pop, false, "return the covariance population value of groups", args(1,8, arg("",dbl),arg("b",int),arg("c",int),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5973             :  pattern("batsql", "covariancep", SQLcovar_pop, false, "return the covariance population value of groups", args(1,8, batarg("",dbl),arg("b",int),batarg("c",int),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5974             :  pattern("batsql", "covariancep", SQLcovar_pop, false, "return the covariance population value of groups", args(1,8, batarg("",dbl),batarg("b",int),arg("c",int),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5975             :  pattern("batsql", "covariancep", SQLcovar_pop, false, "return the covariance population value of groups", args(1,8, batarg("",dbl),batarg("b",int),batarg("c",int),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5976             :  pattern("sql", "covariancep", SQLcovar_pop, false, "return the covariance population value of groups", args(1,8, arg("",dbl),arg("b",lng),arg("c",lng),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5977             :  pattern("batsql", "covariancep", SQLcovar_pop, false, "return the covariance population value of groups", args(1,8, batarg("",dbl),arg("b",lng),batarg("c",lng),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5978             :  pattern("batsql", "covariancep", SQLcovar_pop, false, "return the covariance population value of groups", args(1,8, batarg("",dbl),batarg("b",lng),arg("c",lng),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5979             :  pattern("batsql", "covariancep", SQLcovar_pop, false, "return the covariance population value of groups", args(1,8, batarg("",dbl),batarg("b",lng),batarg("c",lng),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5980             :  pattern("sql", "covariancep", SQLcovar_pop, false, "return the covariance population value of groups", args(1,8, arg("",dbl),arg("b",flt),arg("c",flt),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5981             :  pattern("batsql", "covariancep", SQLcovar_pop, false, "return the covariance population value of groups", args(1,8, batarg("",dbl),arg("b",flt),batarg("c",flt),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5982             :  pattern("batsql", "covariancep", SQLcovar_pop, false, "return the covariance population value of groups", args(1,8, batarg("",dbl),batarg("b",flt),arg("c",flt),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5983             :  pattern("batsql", "covariancep", SQLcovar_pop, false, "return the covariance population value of groups", args(1,8, batarg("",dbl),batarg("b",flt),batarg("c",flt),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5984             :  pattern("sql", "covariancep", SQLcovar_pop, false, "return the covariance population value of groups", args(1,8, arg("",dbl),arg("b",dbl),arg("c",dbl),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5985             :  pattern("batsql", "covariancep", SQLcovar_pop, false, "return the covariance population value of groups", args(1,8, batarg("",dbl),arg("b",dbl),batarg("c",dbl),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5986             :  pattern("batsql", "covariancep", SQLcovar_pop, false, "return the covariance population value of groups", args(1,8, batarg("",dbl),batarg("b",dbl),arg("c",dbl),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5987             :  pattern("batsql", "covariancep", SQLcovar_pop, false, "return the covariance population value of groups", args(1,8, batarg("",dbl),batarg("b",dbl),batarg("c",dbl),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5988             :  pattern("sql", "corr", SQLcorr, false, "return the correlation value of groups", args(1,8, arg("",dbl),arg("b",bte),arg("c",bte),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5989             :  pattern("batsql", "corr", SQLcorr, false, "return the correlation value of groups", args(1,8, batarg("",dbl),arg("b",bte),batarg("c",bte),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5990             :  pattern("batsql", "corr", SQLcorr, false, "return the correlation value of groups", args(1,8, batarg("",dbl),batarg("b",bte),arg("c",bte),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5991             :  pattern("batsql", "corr", SQLcorr, false, "return the correlation value of groups", args(1,8, batarg("",dbl),batarg("b",bte),batarg("c",bte),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5992             :  pattern("sql", "corr", SQLcorr, false, "return the correlation value of groups", args(1,8, arg("",dbl),arg("b",sht),arg("c",sht),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5993             :  pattern("batsql", "corr", SQLcorr, false, "return the correlation value of groups", args(1,8, batarg("",dbl),arg("b",sht),batarg("c",sht),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5994             :  pattern("batsql", "corr", SQLcorr, false, "return the correlation value of groups", args(1,8, batarg("",dbl),batarg("b",sht),arg("c",sht),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5995             :  pattern("batsql", "corr", SQLcorr, false, "return the correlation value of groups", args(1,8, batarg("",dbl),batarg("b",sht),batarg("c",sht),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5996             :  pattern("sql", "corr", SQLcorr, false, "return the correlation value of groups", args(1,8, arg("",dbl),arg("b",int),arg("c",int),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    5997             :  pattern("batsql", "corr", SQLcorr, false, "return the correlation value of groups", args(1,8, batarg("",dbl),arg("b",int),batarg("c",int),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5998             :  pattern("batsql", "corr", SQLcorr, false, "return the correlation value of groups", args(1,8, batarg("",dbl),batarg("b",int),arg("c",int),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    5999             :  pattern("batsql", "corr", SQLcorr, false, "return the correlation value of groups", args(1,8, batarg("",dbl),batarg("b",int),batarg("c",int),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    6000             :  pattern("sql", "corr", SQLcorr, false, "return the correlation value of groups", args(1,8, arg("",dbl),arg("b",lng),arg("c",lng),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    6001             :  pattern("batsql", "corr", SQLcorr, false, "return the correlation value of groups", args(1,8, batarg("",dbl),arg("b",lng),batarg("c",lng),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    6002             :  pattern("batsql", "corr", SQLcorr, false, "return the correlation value of groups", args(1,8, batarg("",dbl),batarg("b",lng),arg("c",lng),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    6003             :  pattern("batsql", "corr", SQLcorr, false, "return the correlation value of groups", args(1,8, batarg("",dbl),batarg("b",lng),batarg("c",lng),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    6004             :  pattern("sql", "corr", SQLcorr, false, "return the correlation value of groups", args(1,8, arg("",dbl),arg("b",flt),arg("c",flt),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    6005             :  pattern("batsql", "corr", SQLcorr, false, "return the correlation value of groups", args(1,8, batarg("",dbl),arg("b",flt),batarg("c",flt),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    6006             :  pattern("batsql", "corr", SQLcorr, false, "return the correlation value of groups", args(1,8, batarg("",dbl),batarg("b",flt),arg("c",flt),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    6007             :  pattern("batsql", "corr", SQLcorr, false, "return the correlation value of groups", args(1,8, batarg("",dbl),batarg("b",flt),batarg("c",flt),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    6008             :  pattern("sql", "corr", SQLcorr, false, "return the correlation value of groups", args(1,8, arg("",dbl),arg("b",dbl),arg("c",dbl),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    6009             :  pattern("batsql", "corr", SQLcorr, false, "return the correlation value of groups", args(1,8, batarg("",dbl),arg("b",dbl),batarg("c",dbl),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    6010             :  pattern("batsql", "corr", SQLcorr, false, "return the correlation value of groups", args(1,8, batarg("",dbl),batarg("b",dbl),arg("c",dbl),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    6011             :  pattern("batsql", "corr", SQLcorr, false, "return the correlation value of groups", args(1,8, batarg("",dbl),batarg("b",dbl),batarg("c",dbl),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    6012             :  pattern("sql", "str_group_concat", SQLstrgroup_concat, false, "return the string concatenation of groups", args(1,7, arg("",str),arg("b",str),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    6013             :  pattern("batsql", "str_group_concat", SQLstrgroup_concat, false, "return the string concatenation of groups", args(1,7, batarg("",str),batarg("b",str),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    6014             :  pattern("sql", "str_group_concat", SQLstrgroup_concat, false, "return the string concatenation of groups with a custom separator", args(1,8, arg("",str),arg("b",str),arg("sep",str),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    6015             :  pattern("batsql", "str_group_concat", SQLstrgroup_concat, false, "return the string concatenation of groups with a custom separator", args(1,8, batarg("",str),arg("b",str),batarg("sep",str),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    6016             :  pattern("batsql", "str_group_concat", SQLstrgroup_concat, false, "return the string concatenation of groups with a custom separator", args(1,8, batarg("",str),batarg("b",str),arg("sep",str),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    6017             :  pattern("batsql", "str_group_concat", SQLstrgroup_concat, false, "return the string concatenation of groups with a custom separator", args(1,8, batarg("",str),batarg("b",str),batarg("sep",str),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    6018             :  /* sql_subquery */
    6019             :  command("aggr", "zero_or_one", zero_or_one, false, "if col contains exactly one value return this. Incase of more raise an exception else return nil", args(1,2, argany("",1),batargany("col",1))),
    6020             :  command("aggr", "zero_or_one", zero_or_one_error, false, "if col contains exactly one value return this. Incase of more raise an exception if err is true else return nil", args(1,3, argany("",1),batargany("col",1),arg("err",bit))),
    6021             :  command("aggr", "zero_or_one", zero_or_one_error_bat, false, "if col contains exactly one value return this. Incase of more raise an exception if err is true else return nil", args(1,3, argany("",1),batargany("col",1),batarg("err",bit))),
    6022             :  command("aggr", "subzero_or_one", SQLsubzero_or_one, false, "", args(1,5, batargany("",1),batargany("b",1),batarg("g",oid),batarg("e",oid),arg("no_nil",bit))),
    6023             :  command("aggr", "all", SQLall, false, "if all values in b are equal return this, else nil", args(1,2, argany("",1),batargany("b",1))),
    6024             :  pattern("aggr", "suball", SQLall_grp, false, "if all values in l are equal (per group) return the value, else nil", args(1,5, batargany("",1),batargany("l",1),batarg("g",oid),batarg("e",oid),arg("no_nil",bit))),
    6025             :  pattern("aggr", "suball", SQLall_grp, false, "if all values in l are equal (per group) return the value, else nil", args(1,6, batargany("",1),batargany("l",1),batarg("g",oid),batarg("e",oid),batarg("s",oid),arg("no_nil",bit))),
    6026             :  command("aggr", "null", SQLnil, false, "if b has a nil return true, else false", args(1,2, arg("",bit),batargany("b",1))),
    6027             :  pattern("aggr", "subnull", SQLnil_grp, false, "if any value in l is nil with in a group return true for that group, else false", args(1,5, batarg("",bit),batargany("l",1),batarg("g",oid),batarg("e",oid),arg("no_nil",bit))),
    6028             :  pattern("aggr", "subnull", SQLnil_grp, false, "if any value in l is nil with in a group return true for that group, else false; with candidate list", args(1,6, batarg("",bit),batargany("l",1),batarg("g",oid),batarg("e",oid),batarg("s",oid),arg("no_nil",bit))),
    6029             :  pattern("sql", "any", SQLany_cmp, false, "if cmp then true, (nl or nr) nil then nil, else false", args(1,4, arg("",bit),arg("cmp",bit),arg("nl",bit),arg("nr",bit))),
    6030             :  pattern("batsql", "any", SQLany_cmp, false, "if cmp then true, (nl or nr) nil then nil, else false", args(1,4, batarg("",bit),batarg("cmp",bit),arg("nl",bit),arg("nr",bit))),
    6031             :  pattern("batsql", "any", SQLany_cmp, false, "if cmp then true, (nl or nr) nil then nil, else false", args(1,4, batarg("",bit),arg("cmp",bit),batarg("nl",bit),arg("nr",bit))),
    6032             :  pattern("batsql", "any", SQLany_cmp, false, "if cmp then true, (nl or nr) nil then nil, else false", args(1,4, batarg("",bit),arg("cmp",bit),arg("nl",bit),batarg("nr",bit))),
    6033             :  pattern("batsql", "any", SQLany_cmp, false, "if cmp then true, (nl or nr) nil then nil, else false", args(1,4, batarg("",bit),arg("cmp",bit),batarg("nl",bit),batarg("nr",bit))),
    6034             :  pattern("batsql", "any", SQLany_cmp, false, "if cmp then true, (nl or nr) nil then nil, else false", args(1,4, batarg("",bit),batarg("cmp",bit),arg("nl",bit),batarg("nr",bit))),
    6035             :  pattern("batsql", "any", SQLany_cmp, false, "if cmp then true, (nl or nr) nil then nil, else false", args(1,4, batarg("",bit),batarg("cmp",bit),batarg("nl",bit),arg("nr",bit))),
    6036             :  pattern("batsql", "any", SQLany_cmp, false, "if cmp then true, (nl or nr) nil then nil, else false", args(1,4, batarg("",bit),batarg("cmp",bit),batarg("nl",bit),batarg("nr",bit))),
    6037             :  pattern("sql", "all", SQLall_cmp, false, "if !cmp then false, (nl or nr) then nil, else true", args(1,4, arg("",bit),arg("cmp",bit),arg("nl",bit),arg("nr",bit))),
    6038             :  pattern("batsql", "all", SQLall_cmp, false, "if !cmp then false, (nl or nr) then nil, else true", args(1,4, batarg("",bit),batarg("cmp",bit),arg("nl",bit),arg("nr",bit))),
    6039             :  pattern("batsql", "all", SQLall_cmp, false, "if !cmp then false, (nl or nr) then nil, else true", args(1,4, batarg("",bit),arg("cmp",bit),batarg("nl",bit),arg("nr",bit))),
    6040             :  pattern("batsql", "all", SQLall_cmp, false, "if !cmp then false, (nl or nr) then nil, else true", args(1,4, batarg("",bit),arg("cmp",bit),arg("nl",bit),batarg("nr",bit))),
    6041             :  pattern("batsql", "all", SQLall_cmp, false, "if !cmp then false, (nl or nr) then nil, else true", args(1,4, batarg("",bit),arg("cmp",bit),batarg("nl",bit),batarg("nr",bit))),
    6042             :  pattern("batsql", "all", SQLall_cmp, false, "if !cmp then false, (nl or nr) then nil, else true", args(1,4, batarg("",bit),batarg("cmp",bit),arg("nl",bit),batarg("nr",bit))),
    6043             :  pattern("batsql", "all", SQLall_cmp, false, "if !cmp then false, (nl or nr) then nil, else true", args(1,4, batarg("",bit),batarg("cmp",bit),batarg("nl",bit),arg("nr",bit))),
    6044             :  pattern("batsql", "all", SQLall_cmp, false, "if !cmp then false, (nl or nr) then nil, else true", args(1,4, batarg("",bit),batarg("cmp",bit),batarg("nl",bit),batarg("nr",bit))),
    6045             :  pattern("aggr", "anyequal", SQLanyequal, false, "if any value in r is equal to l return true, else if r has nil nil else false", args(1,3, arg("",bit),batargany("l",1),batargany("r",1))),
    6046             :  pattern("bataggr", "anyequal", SQLanyequal, false, "", args(1,3, batarg("",bit),batargany("l",1),batargany("r",1))),
    6047             :  pattern("aggr", "allnotequal", SQLallnotequal, false, "if all values in r are not equal to l return true, else if r has nil nil else false", args(1,3, arg("",bit),batargany("l",1),batargany("r",1))),
    6048             :  pattern("bataggr", "allnotequal", SQLallnotequal, false, "", args(1,3, arg("",bit),batargany("l",1),batargany("r",1))),
    6049             :  pattern("aggr", "subanyequal", SQLanyequal_grp, false, "if any value in r is equal to l return true, else if r has nil nil else false", args(1,6, batarg("",bit),batargany("l",1),batargany("r",1),batarg("g",oid),batarg("e",oid),arg("no_nil",bit))),
    6050             :  pattern("aggr", "subanyequal", SQLanyequal_grp, false, "if any value in r is equal to l return true, else if r has nil nil else false; with candidate list", args(1,7, batarg("",bit),batargany("l",1),batargany("r",1),batarg("g",oid),batarg("e",oid),batarg("s",oid),arg("no_nil",bit))),
    6051             :  pattern("aggr", "subanyequal", SQLanyequal_grp2, false, "if any value in r is equal to l return true, else if r has nil nil else false, except if rid is nil (ie empty) then false", args(1,7, batarg("",bit),batargany("l",1),batargany("r",1),batarg("rid",oid),batarg("g",oid),batarg("e",oid),arg("no_nil",bit))),
    6052             :  pattern("aggr", "subanyequal", SQLanyequal_grp2, false, "if any value in r is equal to l return true, else if r has nil nil else false, except if rid is nil (ie empty) then false; with candidate list", args(1,8, batarg("",bit),batargany("l",1),batargany("r",1),batarg("rid",oid),batarg("g",oid),batarg("e",oid),batarg("s",oid),arg("no_nil",bit))),
    6053             :  pattern("aggr", "suballnotequal", SQLallnotequal_grp, false, "if all values in r are not equal to l return true, else if r has nil nil else false", args(1,6, batarg("",bit),batargany("l",1),batargany("r",1),batarg("g",oid),batarg("e",oid),arg("no_nil",bit))),
    6054             :  pattern("aggr", "suballnotequal", SQLallnotequal_grp, false, "if all values in r are not equal to l return true, else if r has nil nil else false; with candidate list", args(1,7, batarg("",bit),batargany("l",1),batargany("r",1),batarg("g",oid),batarg("e",oid),batarg("s",oid),arg("no_nil",bit))),
    6055             :  pattern("aggr", "suballnotequal", SQLallnotequal_grp2, false, "if all values in r are not equal to l return true, else if r has nil nil else false, except if rid is nil (ie empty) then true", args(1,7, batarg("",bit),batargany("l",1),batargany("r",1),batarg("rid",oid),batarg("g",oid),batarg("e",oid),arg("no_nil",bit))),
    6056             :  pattern("aggr", "suballnotequal", SQLallnotequal_grp2, false, "if all values in r are not equal to l return true, else if r has nil nil else false, except if rid is nil (ie empty) then true; with candidate list", args(1,8, batarg("",bit),batargany("l",1),batargany("r",1),batarg("rid",oid),batarg("g",oid),batarg("e",oid),batarg("s",oid),arg("no_nil",bit))),
    6057             :  pattern("aggr", "exist", SQLexist, false, "", args(1,2, arg("",bit), argany("b",1))),
    6058             :  pattern("bataggr", "exist", SQLexist, false, "", args(1,2, batarg("",bit), argany("b",1))),
    6059             :  pattern("bataggr", "exist", SQLexist, false, "", args(1,2, arg("",bit), batargany("b",1))),
    6060             :  pattern("bataggr", "exist", SQLexist, false, "", args(1,2, batarg("",bit), batargany("b",1))),
    6061             :  pattern("aggr", "subexist", SQLsubexist, false, "", args(1,5, batarg("",bit),batargany("b",0),batarg("g",oid),batarg("e",oid),arg("no_nil",bit))),
    6062             :  pattern("aggr", "subexist", SQLsubexist, false, "", args(1,6, batarg("",bit),batargany("b",0),batarg("g",oid),batarg("e",oid),batarg("s",oid),arg("no_nil",bit))),
    6063             :  pattern("aggr", "not_exist", SQLnot_exist, false, "", args(1,2, arg("",bit), argany("b",1))),
    6064             :  pattern("bataggr", "not_exist", SQLnot_exist, false, "", args(1,2, batarg("",bit), argany("b",1))),
    6065             :  pattern("bataggr", "not_exist", SQLnot_exist, false, "", args(1,2, arg("",bit), batargany("b",1))),
    6066             :  pattern("bataggr", "not_exist", SQLnot_exist, false, "", args(1,2, batarg("",bit), batargany("b",1))),
    6067             :  pattern("aggr", "subnot_exist", SQLsubnot_exist, false, "", args(1,5, batarg("",bit),batargany("b",0),batarg("g",oid),batarg("e",oid),arg("no_nil",bit))),
    6068             :  pattern("aggr", "subnot_exist", SQLsubnot_exist, false, "", args(1,6, batarg("",bit),batargany("b",0),batarg("g",oid),batarg("e",oid),batarg("s",oid),arg("no_nil",bit))),
    6069             :  /* wlr */
    6070             :  pattern("wlr", "master", WLRmaster, false, "Initialize the replicator thread", args(0,1, arg("dbname",str))),
    6071             :  pattern("wlr", "stop", WLRstop, false, "Stop the replicator thread", noargs),
    6072             :  pattern("wlr", "accept", WLRaccept, false, "Accept failing transaction", noargs),
    6073             :  pattern("wlr", "replicate", WLRreplicate, false, "Continue to keep the replica in sink", noargs),
    6074             :  pattern("wlr", "replicate", WLRreplicate, false, "Roll the snapshot forward to an up-to-date clone", args(0,1, arg("ts",timestamp))),
    6075             :  pattern("wlr", "replicate", WLRreplicate, false, "Roll the snapshot forward to a specific transaction id", args(0,1, arg("id",bte))),
    6076             :  pattern("wlr", "replicate", WLRreplicate, false, "Roll the snapshot forward to a specific transaction id", args(0,1, arg("id",sht))),
    6077             :  pattern("wlr", "replicate", WLRreplicate, false, "Roll the snapshot forward to a specific transaction id", args(0,1, arg("id",int))),
    6078             :  pattern("wlr", "replicate", WLRreplicate, false, "Roll the snapshot forward to a specific transaction id", args(0,1, arg("id",lng))),
    6079             :  pattern("wlr", "getMaster", WLRgetmaster, false, "What is the current master database", args(1,1, arg("",str))),
    6080             :  pattern("wlr", "setbeat", WLRsetbeat, false, "Threshold (in seconds) for re-running queries", args(0,1, arg("dur",int))),
    6081             :  pattern("wlr", "getclock", WLRgetclock, false, "Timestamp of last replicated transaction.", args(1,1, arg("",str))),
    6082             :  pattern("wlr", "gettick", WLRgettick, false, "Transaction identifier of the last replicated transaction.", args(1,1, arg("",lng))),
    6083             :  pattern("wlr", "transaction", WLRtransaction, false, "Mark the beginning of the work unit which can be a compound transaction", args(0,3, arg("tid",lng),arg("started",str),arg("user",str))),
    6084             :  pattern("wlr", "commit", WLRcommit, false, "Mark the end of the work unit", noargs),
    6085             :  pattern("wlr", "rollback", WLRrollback, false, "Mark the end of the work unit", noargs),
    6086             :  pattern("wlr", "catalog", WLRcatalog, false, "A catalog changing query", args(0,1, arg("q",str))),
    6087             :  pattern("wlr", "action", WLRaction, false, "A query producing updates", args(0,1, arg("q",str))),
    6088             :  pattern("wlr", "append", WLRappend, false, "Apply the insertions in the workload-capture-replay list", args(1,7, arg("",int),arg("sname",str),arg("tname",str),arg("cname",str),arg("offset", oid), batarg("pos", oid), varargany("ins",0))),
    6089             :  pattern("wlr", "update", WLRupdate, false, "Apply the update in the workload-capture-replay list", args(1,6, arg("",int),arg("sname",str),arg("tname",str),arg("cname",str),arg("tid",oid),argany("val",0))),
    6090             :  pattern("wlr", "delete", WLRdelete, false, "Apply the deletions in the workload-capture-replay list", args(1,4, arg("",int),arg("sname",str),arg("tname",str),vararg("b",oid))),
    6091             :  pattern("wlr", "clear_table", WLRclear_table, false, "Destroy the tuples in the table", args(1,3, arg("",int),arg("sname",str),arg("tname",str))),
    6092             :  pattern("wlr", "create_seq", WLRgeneric, false, "Catalog operation create_seq", args(0,3, arg("sname",str),arg("seqname",str),arg("action",int))),
    6093             :  pattern("wlr", "alter_seq", WLRgeneric, false, "Catalog operation alter_seq", args(0,3, arg("sname",str),arg("seqname",str),arg("val",lng))),
    6094             :  pattern("wlr", "alter_seq", WLRgeneric, false, "Catalog operation alter_seq", args(0,4, arg("sname",str),arg("seqname",str),arg("seq",ptr),batarg("val",lng))),
    6095             :  pattern("wlr", "drop_seq", WLRgeneric, false, "Catalog operation drop_seq", args(0,3, arg("sname",str),arg("nme",str),arg("action",int))),
    6096             :  pattern("wlr", "create_schema", WLRgeneric, false, "Catalog operation create_schema", args(0,3, arg("sname",str),arg("auth",str),arg("action",int))),
    6097             :  pattern("wlr", "drop_schema", WLRgeneric, false, "Catalog operation drop_schema", args(0,3, arg("sname",str),arg("ifexists",int),arg("action",int))),
    6098             :  pattern("wlr", "create_table", WLRgeneric, false, "Catalog operation create_table", args(0,3, arg("sname",str),arg("tname",str),arg("temp",int))),
    6099             :  pattern("wlr", "create_view", WLRgeneric, false, "Catalog operation create_view", args(0,4, arg("sname",str),arg("tname",str),arg("temp",int),arg("replace",int))),
    6100             :  pattern("wlr", "drop_table", WLRgeneric, false, "Catalog operation drop_table", args(0,4, arg("sname",str),arg("name",str),arg("action",int),arg("ifexists",int))),
    6101             :  pattern("wlr", "drop_view", WLRgeneric, false, "Catalog operation drop_view", args(0,4, arg("sname",str),arg("name",str),arg("action",int),arg("ifexists",int))),
    6102             :  pattern("wlr", "drop_constraint", WLRgeneric, false, "Catalog operation drop_constraint", args(0,5, arg("sname",str),arg("tname",str),arg("name",str),arg("action",int),arg("ifexists",int))),
    6103             :  pattern("wlr", "alter_table", WLRgeneric, false, "Catalog operation alter_table", args(0,3, arg("sname",str),arg("tname",str),arg("action",int))),
    6104             :  pattern("wlr", "create_type", WLRgeneric, false, "Catalog operation create_type", args(0,3, arg("sname",str),arg("nme",str),arg("impl",str))),
    6105             :  pattern("wlr", "drop_type", WLRgeneric, false, "Catalog operation drop_type", args(0,3, arg("sname",str),arg("nme",str),arg("action",int))),
    6106             :  pattern("wlr", "grant_roles", WLRgeneric, false, "Catalog operation grant_roles", args(0,4, arg("sname",str),arg("auth",str),arg("grantor",int),arg("admin",int))),
    6107             :  pattern("wlr", "revoke_roles", WLRgeneric, false, "Catalog operation revoke_roles", args(0,4, arg("sname",str),arg("auth",str),arg("grantor",int),arg("admin",int))),
    6108             :  pattern("wlr", "grant", WLRgeneric, false, "Catalog operation grant", args(0,7, arg("sname",str),arg("tbl",str),arg("grantee",str),arg("privs",int),arg("cname",str),arg("gr",int),arg("grantor",int))),
    6109             :  pattern("wlr", "revoke", WLRgeneric, false, "Catalog operation revoke", args(0,7, arg("sname",str),arg("tbl",str),arg("grantee",str),arg("privs",int),arg("cname",str),arg("grant",int),arg("grantor",int))),
    6110             :  pattern("wlr", "grant_function", WLRgeneric, false, "Catalog operation grant_function", args(0,6, arg("sname",str),arg("fcnid",int),arg("grantee",str),arg("privs",int),arg("grant",int),arg("grantor",int))),
    6111             :  pattern("wlr", "revoke_function", WLRgeneric, false, "Catalog operation revoke_function", args(0,6, arg("sname",str),arg("fcnid",int),arg("grantee",str),arg("privs",int),arg("grant",int),arg("grantor",int))),
    6112             :  pattern("wlr", "create_user", WLRgeneric, false, "Catalog operation create_user", args(0,5, arg("sname",str),arg("passwrd",str),arg("enc",int),arg("schema",str),arg("fullname",str))),
    6113             :  pattern("wlr", "drop_user", WLRgeneric, false, "Catalog operation drop_user", args(0,2, arg("sname",str),arg("action",int))),
    6114             :  pattern("wlr", "drop_user", WLRgeneric, false, "Catalog operation drop_user", args(0,3, arg("sname",str),arg("auth",str),arg("action",int))),
    6115             :  pattern("wlr", "alter_user", WLRgeneric, false, "Catalog operation alter_user", args(0,5, arg("sname",str),arg("passwrd",str),arg("enc",int),arg("schema",str),arg("oldpasswrd",str))),
    6116             :  pattern("wlr", "rename_user", WLRgeneric, false, "Catalog operation rename_user", args(0,3, arg("sname",str),arg("newnme",str),arg("action",int))),
    6117             :  pattern("wlr", "create_role", WLRgeneric, false, "Catalog operation create_role", args(0,3, arg("sname",str),arg("role",str),arg("grator",int))),
    6118             :  pattern("wlr", "drop_role", WLRgeneric, false, "Catalog operation drop_role", args(0,3, arg("auth",str),arg("role",str),arg("action",int))),
    6119             :  pattern("wlr", "drop_role", WLRgeneric, false, "Catalog operation drop_role", args(0,2, arg("role",str),arg("action",int))),
    6120             :  pattern("wlr", "drop_index", WLRgeneric, false, "Catalog operation drop_index", args(0,3, arg("sname",str),arg("iname",str),arg("action",int))),
    6121             :  pattern("wlr", "drop_function", WLRgeneric, false, "Catalog operation drop_function", args(0,5, arg("sname",str),arg("fname",str),arg("fid",int),arg("type",int),arg("action",int))),
    6122             :  pattern("wlr", "create_function", WLRgeneric, false, "Catalog operation create_function", args(0,3, arg("sname",str),arg("fname",str),arg("replace",int))),
    6123             :  pattern("wlr", "create_trigger", WLRgeneric, false, "Catalog operation create_trigger", args(0,11, arg("sname",str),arg("tname",str),arg("triggername",str),arg("time",int),arg("orientation",int),arg("event",int),arg("old",str),arg("new",str),arg("cond",str),arg("qry",str),arg("replace",int))),
    6124             :  pattern("wlr", "drop_trigger", WLRgeneric, false, "Catalog operation drop_trigger", args(0,3, arg("sname",str),arg("nme",str),arg("ifexists",int))),
    6125             :  pattern("wlr", "alter_add_table", WLRgeneric, false, "Catalog operation alter_add_table", args(0,5, arg("sname",str),arg("mtnme",str),arg("psnme",str),arg("ptnme",str),arg("action",int))),
    6126             :  pattern("wlr", "alter_del_table", WLRgeneric, false, "Catalog operation alter_del_table", args(0,5, arg("sname",str),arg("mtnme",str),arg("psnme",str),arg("ptnme",str),arg("action",int))),
    6127             :  pattern("wlr", "alter_set_table", WLRgeneric, false, "Catalog operation alter_set_table", args(0,3, arg("sname",str),arg("tnme",str),arg("access",int))),
    6128             :  pattern("wlr", "alter_add_range_partition", WLRgeneric, false, "Catalog operation alter_add_range_partition", args(0,8, arg("sname",str),arg("mtnme",str),arg("psnme",str),arg("ptnme",str),arg("min",str),arg("max",str),arg("nills",bit),arg("update",int))),
    6129             :  pattern("wlr", "comment_on", WLRgeneric, false, "Catalog operation comment_on", args(0,2, arg("objid",int),arg("remark",str))),
    6130             :  pattern("wlr", "rename_schema", WLRgeneric, false, "Catalog operation rename_schema", args(0,2, arg("sname",str),arg("newnme",str))),
    6131             :  pattern("wlr", "rename_table", WLRgeneric, false, "Catalog operation rename_table", args(0,4, arg("osname",str),arg("nsname",str),arg("otname",str),arg("ntname",str))),
    6132             :  pattern("wlr", "rename_column", WLRgeneric, false, "Catalog operation rename_column", args(0,4, arg("sname",str),arg("tname",str),arg("cname",str),arg("newnme",str))),
    6133             :  pattern("wlr", "transaction_release", WLRgeneric, false, "A transaction statement (type can be commit,release,rollback or start)", args(1,3, arg("",void),arg("chain",int),arg("name",str))),
    6134             :  pattern("wlr", "transaction_commit", WLRgeneric, false, "A transaction statement (type can be commit,release,rollback or start)", args(1,3, arg("",void),arg("chain",int),arg("name",str))),
    6135             :  pattern("wlr", "transaction_rollback", WLRgeneric, false, "A transaction statement (type can be commit,release,rollback or start)", args(1,3, arg("",void),arg("chain",int),arg("name",str))),
    6136             :  pattern("wlr", "transaction_begin", WLRgeneric, false, "A transaction statement (type can be commit,release,rollback or start)", args(1,3, arg("",void),arg("chain",int),arg("name",str))),
    6137             :  pattern("wlr", "transaction", WLRgeneric, true, "Start an autocommit transaction", noargs),
    6138             :  pattern("wlr", "alter_add_value_partition", WLRgeneric, false, "Catalog operation alter_add_value_partition", args(0,6, arg("sname",str),arg("mtnme",str),arg("psnme",str),arg("ptnme",str),arg("nills",bit),arg("update",int))),
    6139             :  pattern("wlr", "alter_add_value_partition", WLRgeneric, false, "Catalog operation alter_add_value_partition", args(0,7, arg("sname",str),arg("mtnme",str),arg("psnme",str),arg("ptnme",str),arg("nills",bit),arg("update",int),vararg("arg",str))),
    6140             :  /* sqlcatalog */
    6141             :  pattern("sqlcatalog", "create_seq", SQLcreate_seq, false, "Catalog operation create_seq", args(0,4, arg("sname",str),arg("seqname",str),arg("seq",ptr),arg("action",int))),
    6142             :  pattern("sqlcatalog", "alter_seq", SQLalter_seq, false, "Catalog operation alter_seq", args(0,4, arg("sname",str),arg("seqname",str),arg("seq",ptr),arg("val",lng))),
    6143             :  pattern("sqlcatalog", "alter_seq", SQLalter_seq, false, "Catalog operation alter_seq", args(0,4, arg("sname",str),arg("seqname",str),arg("seq",ptr),batarg("val",lng))),
    6144             :  pattern("sqlcatalog", "drop_seq", SQLdrop_seq, false, "Catalog operation drop_seq", args(0,3, arg("sname",str),arg("nme",str),arg("action",int))),
    6145             :  pattern("sqlcatalog", "create_schema", SQLcreate_schema, false, "Catalog operation create_schema", args(0,3, arg("sname",str),arg("auth",str),arg("action",int))),
    6146             :  pattern("sqlcatalog", "drop_schema", SQLdrop_schema, false, "Catalog operation drop_schema", args(0,3, arg("sname",str),arg("ifexists",int),arg("action",int))),
    6147             :  pattern("sqlcatalog", "create_table", SQLcreate_table, false, "Catalog operation create_table", args(0,4, arg("sname",str),arg("tname",str),arg("tbl",ptr),arg("temp",int))),
    6148             :  pattern("sqlcatalog", "create_view", SQLcreate_view, false, "Catalog operation create_view", args(0,5, arg("sname",str),arg("vname",str),arg("tbl",ptr),arg("temp",int),arg("replace",int))),
    6149             :  pattern("sqlcatalog", "drop_table", SQLdrop_table, false, "Catalog operation drop_table", args(0,4, arg("sname",str),arg("name",str),arg("action",int),arg("ifexists",int))),
    6150             :  pattern("sqlcatalog", "drop_view", SQLdrop_view, false, "Catalog operation drop_view", args(0,4, arg("sname",str),arg("name",str),arg("action",int),arg("ifexists",int))),
    6151             :  pattern("sqlcatalog", "drop_constraint", SQLdrop_constraint, false, "Catalog operation drop_constraint", args(0,5, arg("sname",str),arg("tname",str),arg("name",str),arg("action",int),arg("ifexists",int))),
    6152             :  pattern("sqlcatalog", "alter_table", SQLalter_table, false, "Catalog operation alter_table", args(0,4, arg("sname",str),arg("tname",str),arg("tbl",ptr),arg("action",int))),
    6153             :  pattern("sqlcatalog", "create_type", SQLcreate_type, false, "Catalog operation create_type", args(0,3, arg("sname",str),arg("nme",str),arg("impl",str))),
    6154             :  pattern("sqlcatalog", "drop_type", SQLdrop_type, false, "Catalog operation drop_type", args(0,3, arg("sname",str),arg("nme",str),arg("action",int))),
    6155             :  pattern("sqlcatalog", "grant_roles", SQLgrant_roles, false, "Catalog operation grant_roles", args(0,4, arg("sname",str),arg("auth",str),arg("grantor",int),arg("admin",int))),
    6156             :  pattern("sqlcatalog", "revoke_roles", SQLrevoke_roles, false, "Catalog operation revoke_roles", args(0,4, arg("sname",str),arg("auth",str),arg("grantor",int),arg("admin",int))),
    6157             :  pattern("sqlcatalog", "grant", SQLgrant, false, "Catalog operation grant", args(0,7, arg("sname",str),arg("tbl",str),arg("grantee",str),arg("privs",int),arg("cname",str),arg("gr",int),arg("grantor",int))),
    6158             :  pattern("sqlcatalog", "revoke", SQLrevoke, false, "Catalog operation revoke", args(0,7, arg("sname",str),arg("tbl",str),arg("grantee",str),arg("privs",int),arg("cname",str),arg("grant",int),arg("grantor",int))),
    6159             :  pattern("sqlcatalog", "grant_function", SQLgrant_function, false, "Catalog operation grant_function", args(0,6, arg("sname",str),arg("fcnid",int),arg("grantee",str),arg("privs",int),arg("grant",int),arg("grantor",int))),
    6160             :  pattern("sqlcatalog", "revoke_function", SQLrevoke_function, false, "Catalog operation revoke_function", args(0,6, arg("sname",str),arg("fcnid",int),arg("grantee",str),arg("privs",int),arg("grant",int),arg("grantor",int))),
    6161             :  pattern("sqlcatalog", "create_user", SQLcreate_user, false, "Catalog operation create_user", args(0,6, arg("sname",str),arg("passwrd",str),arg("enc",int),arg("schema",str),arg("schemapath",str),arg("fullname",str))),
    6162             :  pattern("sqlcatalog", "drop_user", SQLdrop_user, false, "Catalog operation drop_user", args(0,2, arg("sname",str),arg("action",int))),
    6163             :  pattern("sqlcatalog", "drop_user", SQLdrop_user, false, "Catalog operation drop_user", args(0,3, arg("sname",str),arg("auth",str),arg("action",int))),
    6164             :  pattern("sqlcatalog", "alter_user", SQLalter_user, false, "Catalog operation alter_user", args(0,6, arg("sname",str),arg("passwrd",str),arg("enc",int),arg("schema",str),arg("schemapath",str),arg("oldpasswrd",str))),
    6165             :  pattern("sqlcatalog", "rename_user", SQLrename_user, false, "Catalog operation rename_user", args(0,3, arg("sname",str),arg("newnme",str),arg("action",int))),
    6166             :  pattern("sqlcatalog", "create_role", SQLcreate_role, false, "Catalog operation create_role", args(0,3, arg("sname",str),arg("role",str),arg("grator",int))),
    6167             :  pattern("sqlcatalog", "drop_role", SQLdrop_role, false, "Catalog operation drop_role", args(0,3, arg("auth",str),arg("role",str),arg("action",int))),
    6168             :  pattern("sqlcatalog", "drop_role", SQLdrop_role, false, "Catalog operation drop_role", args(0,2, arg("role",str),arg("action",int))),
    6169             :  pattern("sqlcatalog", "drop_index", SQLdrop_index, false, "Catalog operation drop_index", args(0,3, arg("sname",str),arg("iname",str),arg("action",int))),
    6170             :  pattern("sqlcatalog", "drop_function", SQLdrop_function, false, "Catalog operation drop_function", args(0,5, arg("sname",str),arg("fname",str),arg("fid",int),arg("type",int),arg("action",int))),
    6171             :  pattern("sqlcatalog", "create_function", SQLcreate_function, false, "Catalog operation create_function", args(0,4, arg("sname",str),arg("fname",str),arg("fcn",ptr),arg("replace",int))),
    6172             :  pattern("sqlcatalog", "create_trigger", SQLcreate_trigger, false, "Catalog operation create_trigger", args(0,11, arg("sname",str),arg("tname",str),arg("triggername",str),arg("time",int),arg("orientation",int),arg("event",int),arg("old",str),arg("new",str),arg("cond",str),arg("qry",str),arg("replace",int))),
    6173             :  pattern("sqlcatalog", "drop_trigger", SQLdrop_trigger, false, "Catalog operation drop_trigger", args(0,3, arg("sname",str),arg("nme",str),arg("ifexists",int))),
    6174             :  pattern("sqlcatalog", "alter_add_table", SQLalter_add_table, false, "Catalog operation alter_add_table", args(0,5, arg("sname",str),arg("mtnme",str),arg("psnme",str),arg("ptnme",str),arg("action",int))),
    6175             :  pattern("sqlcatalog", "alter_del_table", SQLalter_del_table, false, "Catalog operation alter_del_table", args(0,5, arg("sname",str),arg("mtnme",str),arg("psnme",str),arg("ptnme",str),arg("action",int))),
    6176             :  pattern("sqlcatalog", "alter_set_table", SQLalter_set_table, false, "Catalog operation alter_set_table", args(0,3, arg("sname",str),arg("tnme",str),arg("access",int))),
    6177             :  pattern("sqlcatalog", "alter_add_range_partition", SQLalter_add_range_partition, false, "Catalog operation alter_add_range_partition", args(0,8, arg("sname",str),arg("mtnme",str),arg("psnme",str),arg("ptnme",str),argany("min",1),argany("max",1),arg("nills",bit),arg("update",int))),
    6178             :  pattern("sqlcatalog", "alter_add_value_partition", SQLalter_add_value_partition, false, "Catalog operation alter_add_value_partition", args(0,6, arg("sname",str),arg("mtnme",str),arg("psnme",str),arg("ptnme",str),arg("nills",bit),arg("update",int))),
    6179             :  pattern("sqlcatalog", "alter_add_value_partition", SQLalter_add_value_partition, false, "Catalog operation alter_add_value_partition", args(0,7, arg("sname",str),arg("mtnme",str),arg("psnme",str),arg("ptnme",str),arg("nills",bit),arg("update",int),varargany("arg",0))),
    6180             :  pattern("sqlcatalog", "comment_on", SQLcomment_on, false, "Catalog operation comment_on", args(0,2, arg("objid",int),arg("remark",str))),
    6181             :  pattern("sqlcatalog", "rename_schema", SQLrename_schema, false, "Catalog operation rename_schema", args(0,2, arg("sname",str),arg("newnme",str))),
    6182             :  pattern("sqlcatalog", "rename_table", SQLrename_table, false, "Catalog operation rename_table", args(0,4, arg("osname",str),arg("nsname",str),arg("otname",str),arg("ntname",str))),
    6183             :  pattern("sqlcatalog", "rename_column", SQLrename_column, false, "Catalog operation rename_column", args(0,4, arg("sname",str),arg("tname",str),arg("cname",str),arg("newnme",str))),
    6184             :  /* sql_transaction */
    6185             :  pattern("sql", "transaction_release", SQLtransaction_release, true, "A transaction statement (type can be commit,release,rollback or start)", args(1,3, arg("",void),arg("chain",int),arg("name",str))),
    6186             :  pattern("sql", "transaction_commit", SQLtransaction_commit, true, "A transaction statement (type can be commit,release,rollback or start)", args(1,3, arg("",void),arg("chain",int),arg("name",str))),
    6187             :  pattern("sql", "transaction_rollback", SQLtransaction_rollback, true, "A transaction statement (type can be commit,release,rollback or start)", args(1,3, arg("",void),arg("chain",int),arg("name",str))),
    6188             :  pattern("sql", "transaction_begin", SQLtransaction_begin, true, "A transaction statement (type can be commit,release,rollback or start)", args(1,3, arg("",void),arg("chain",int),arg("name",str))),
    6189             :  pattern("sql", "transaction", SQLtransaction2, true, "Start an autocommit transaction", noargs),
    6190             : #ifdef HAVE_HGE
    6191             :  /* sql_hge */
    6192             :  command("calc", "dec_round", hge_dec_round_wrap, false, "round off the value v to nearests multiple of r", args(1,3, arg("",hge),arg("v",hge),arg("r",hge))),
    6193             :  pattern("batcalc", "dec_round", hge_bat_dec_round_wrap, false, "round off the value v to nearests multiple of r", args(1,3, batarg("",hge),batarg("v",hge),arg("r",hge))),
    6194             :  pattern("batcalc", "dec_round", hge_bat_dec_round_wrap, false, "round off the value v to nearests multiple of r", args(1,4, batarg("",hge),batarg("v",hge),arg("r",hge),batarg("s",oid))),
    6195             :  pattern("batcalc", "dec_round", hge_bat_dec_round_wrap_cst, false, "round off the value v to nearests multiple of r", args(1,3, batarg("",hge),arg("v",hge),batarg("r",hge))),
    6196             :  pattern("batcalc", "dec_round", hge_bat_dec_round_wrap_cst, false, "round off the value v to nearests multiple of r", args(1,4, batarg("",hge),arg("v",hge),batarg("r",hge),batarg("s",oid))),
    6197             :  pattern("batcalc", "dec_round", hge_bat_dec_round_wrap_nocst, false, "round off the value v to nearests multiple of r", args(1,3, batarg("",hge),batarg("v",hge),batarg("r",hge))),
    6198             :  pattern("batcalc", "dec_round", hge_bat_dec_round_wrap_nocst, false, "round off the value v to nearests multiple of r", args(1,5, batarg("",hge),batarg("v",hge),batarg("r",hge),batarg("s1",oid),batarg("s2",oid))),
    6199             :  command("calc", "round", hge_round_wrap, false, "round off the decimal v(d,s) to r digits behind the dot (if r < 0, before the dot)", args(1,5, arg("",hge),arg("v",hge),arg("r",bte),arg("d",int),arg("s",int))),
    6200             :  pattern("batcalc", "round", hge_bat_round_wrap, false, "round off the decimal v(d,s) to r digits behind the dot (if r < 0, before the dot)", args(1,5, batarg("",hge),batarg("v",hge),arg("r",bte),arg("d",int),arg("s",int))),
    6201             :  pattern("batcalc", "round", hge_bat_round_wrap, false, "round off the decimal v(d,s) to r digits behind the dot (if r < 0, before the dot)", args(1,6, batarg("",hge),batarg("v",hge),arg("r",bte),batarg("s",oid),arg("d",int),arg("s",int))),
    6202             :  pattern("batcalc", "round", hge_bat_round_wrap_cst, false, "round off the decimal v(d,s) to r digits behind the dot (if r < 0, before the dot)", args(1,5, batarg("",hge),arg("v",hge),batarg("r",bte),arg("d",int),arg("s",int))),
    6203             :  pattern("batcalc", "round", hge_bat_round_wrap_cst, false, "round off the decimal v(d,s) to r digits behind the dot (if r < 0, before the dot)", args(1,6, batarg("",hge),arg("v",hge),batarg("r",bte),batarg("s",oid),arg("d",int),arg("s",int))),
    6204             :  pattern("batcalc", "round", hge_bat_round_wrap_nocst, false, "round off the decimal v(d,s) to r digits behind the dot (if r < 0, before the dot)", args(1,5, batarg("",hge),batarg("v",hge),batarg("r",bte),arg("d",int),arg("s",int))),
    6205             :  pattern("batcalc", "round", hge_bat_round_wrap_nocst, false, "round off the decimal v(d,s) to r digits behind the dot (if r < 0, before the dot)", args(1,7, batarg("",hge),batarg("v",hge),batarg("r",bte),batarg("s1",oid),batarg("s2",oid),arg("d",int),arg("s",int))),
    6206             :  command("calc", "second_interval", hge_dec2second_interval, false, "cast hge decimal to a second_interval", args(1,5, arg("",lng),arg("sc",int),arg("v",hge),arg("ek",int),arg("sk",int))),
    6207             :  pattern("batcalc", "second_interval", hge_batdec2second_interval, false, "cast hge decimal to a second_interval", args(1,6, batarg("",lng),arg("sc",int),batarg("v",hge),batarg("s",oid),arg("ek",int),arg("sk",int))),
    6208             :  command("calc", "hge", nil_2dec_hge, false, "cast to dec(hge) and check for overflow", args(1,4, arg("",hge),arg("v",void),arg("digits",int),arg("scale",int))),
    6209             :  command("batcalc", "hge", batnil_2dec_hge, false, "cast to dec(hge) and check for overflow", args(1,4, batarg("",hge),batarg("v",void),arg("digits",int),arg("scale",int))),
    6210             :  command("calc", "hge", str_2dec_hge, false, "cast to dec(hge) and check for overflow", args(1,4, arg("",hge),arg("v",str),arg("digits",int),arg("scale",int))),
    6211             :  pattern("batcalc", "hge", batstr_2dec_hge, false, "cast to dec(hge) and check for overflow", args(1,5, batarg("",hge),batarg("v",str),batarg("s",oid),arg("digits",int),arg("scale",int))),
    6212             :  pattern("calc", "month_interval", month_interval, false, "cast hge to a month_interval and check for overflow", args(1,4, arg("",int),arg("v",hge),arg("ek",int),arg("sk",int))),
    6213             :  pattern("batcalc", "month_interval", month_interval, false, "cast hge to a month_interval and check for overflow", args(1,5, batarg("",int),batarg("v",hge),batarg("s",oid),arg("ek",int),arg("sk",int))),
    6214             :  pattern("calc", "second_interval", second_interval, false, "cast hge to a second_interval and check for overflow", args(1,4, arg("",lng),arg("v",hge),arg("ek",int),arg("sk",int))),
    6215             :  pattern("batcalc", "second_interval", second_interval, false, "cast hge to a second_interval and check for overflow", args(1,5, batarg("",lng),batarg("v",hge),batarg("s",oid),arg("ek",int),arg("sk",int))),
    6216             :  /* sql_decimal_hge */
    6217             :  command("calc", "hge", flt_num2dec_hge, false, "cast number to decimal(hge) and check for overflow", args(1,4, arg("",hge),arg("v",flt),arg("digits",int),arg("scale",int))),
    6218             :  command("batcalc", "hge", batflt_num2dec_hge, false, "cast number to decimal(hge) and check for overflow", args(1,5, batarg("",hge),batarg("v",flt),batarg("s",oid),arg("digits",int),arg("scale",int))),
    6219             :  command("calc", "hge", dbl_num2dec_hge, false, "cast number to decimal(hge) and check for overflow", args(1,4, arg("",hge),arg("v",dbl),arg("digits",int),arg("scale",int))),
    6220             :  command("batcalc", "hge", batdbl_num2dec_hge, false, "cast number to decimal(hge) and check for overflow", args(1,5, batarg("",hge),batarg("v",dbl),batarg("s",oid),arg("digits",int),arg("scale",int))),
    6221             :  command("calc", "hge", bte_num2dec_hge, false, "cast number to decimal(hge) and check for overflow", args(1,4, arg("",hge),arg("v",bte),arg("digits",int),arg("scale",int))),
    6222             :  command("batcalc", "hge", batbte_num2dec_hge, false, "cast number to decimal(hge) and check for overflow", args(1,5, batarg("",hge),batarg("v",bte),batarg("s",oid),arg("digits",int),arg("scale",int))),
    6223             :  command("calc", "hge", bte_dec2_hge, false, "cast decimal(bte) to hge and check for overflow", args(1,3, arg("",hge),arg("s1",int),arg("v",bte))),
    6224             :  command("calc", "hge", bte_dec2dec_hge, false, "cast decimal(bte) to decimal(hge) and check for overflow", args(1,5, arg("",hge),arg("s1",int),arg("v",bte),arg("d2",int),arg("s2",int))),
    6225             :  command("batcalc", "hge", batbte_dec2_hge, false, "cast decimal(bte) to hge and check for overflow", args(1,4, batarg("",hge),arg("s1",int),batarg("v",bte),batarg("s",oid))),
    6226             :  command("batcalc", "hge", batbte_dec2dec_hge, false, "cast decimal(bte) to decimal(hge) and check for overflow", args(1,6, batarg("",hge),arg("s1",int),batarg("v",bte),batarg("s",oid),arg("d2",int),arg("s2",int))),
    6227             :  command("calc", "hge", sht_num2dec_hge, false, "cast number to decimal(hge) and check for overflow", args(1,4, arg("",hge),arg("v",sht),arg("digits",int),arg("scale",int))),
    6228             :  command("batcalc", "hge", batsht_num2dec_hge, false, "cast number to decimal(hge) and check for overflow", args(1,5, batarg("",hge),batarg("v",sht),batarg("s",oid),arg("digits",int),arg("scale",int))),
    6229             :  command("calc", "hge", sht_dec2_hge, false, "cast decimal(sht) to hge and check for overflow", args(1,3, arg("",hge),arg("s1",int),arg("v",sht))),
    6230             :  command("calc", "hge", sht_dec2dec_hge, false, "cast decimal(sht) to decimal(hge) and check for overflow", args(1,5, arg("",hge),arg("s1",int),arg("v",sht),arg("d2",int),arg("s2",int))),
    6231             :  command("batcalc", "hge", batsht_dec2_hge, false, "cast decimal(sht) to hge and check for overflow", args(1,4, batarg("",hge),arg("s1",int),batarg("v",sht),batarg("s",oid))),
    6232             :  command("batcalc", "hge", batsht_dec2dec_hge, false, "cast decimal(sht) to decimal(hge) and check for overflow", args(1,6, batarg("",hge),arg("s1",int),batarg("v",sht),batarg("s",oid),arg("d2",int),arg("s2",int))),
    6233             :  command("calc", "hge", int_num2dec_hge, false, "cast number to decimal(hge) and check for overflow", args(1,4, arg("",hge),arg("v",int),arg("digits",int),arg("scale",int))),
    6234             :  command("batcalc", "hge", batint_num2dec_hge, false, "cast number to decimal(hge) and check for overflow", args(1,5, batarg("",hge),batarg("v",int),batarg("s",oid),arg("digits",int),arg("scale",int))),
    6235             :  command("calc", "hge", int_dec2_hge, false, "cast decimal(int) to hge and check for overflow", args(1,3, arg("",hge),arg("s1",int),arg("v",int))),
    6236             :  command("calc", "hge", int_dec2dec_hge, false, "cast decimal(int) to decimal(hge) and check for overflow", args(1,5, arg("",hge),arg("s1",int),arg("v",int),arg("d2",int),arg("s2",int))),
    6237             :  command("batcalc", "hge", batint_dec2_hge, false, "cast decimal(int) to hge and check for overflow", args(1,4, batarg("",hge),arg("s1",int),batarg("v",int),batarg("s",oid))),
    6238             :  command("batcalc", "hge", batint_dec2dec_hge, false, "cast decimal(int) to decimal(hge) and check for overflow", args(1,6, batarg("",hge),arg("s1",int),batarg("v",int),batarg("s",oid),arg("d2",int),arg("s2",int))),
    6239             :  command("calc", "hge", lng_num2dec_hge, false, "cast number to decimal(hge) and check for overflow", args(1,4, arg("",hge),arg("v",lng),arg("digits",int),arg("scale",int))),
    6240             :  command("batcalc", "hge", batlng_num2dec_hge, false, "cast number to decimal(hge) and check for overflow", args(1,5, batarg("",hge),batarg("v",lng),batarg("s",oid),arg("digits",int),arg("scale",int))),
    6241             :  command("calc", "hge", lng_dec2_hge, false, "cast decimal(lng) to hge and check for overflow", args(1,3, arg("",hge),arg("s1",int),arg("v",lng))),
    6242             :  command("calc", "hge", lng_dec2dec_hge, false, "cast decimal(lng) to decimal(hge) and check for overflow", args(1,5, arg("",hge),arg("s1",int),arg("v",lng),arg("d2",int),arg("s2",int))),
    6243             :  command("batcalc", "hge", batlng_dec2_hge, false, "cast decimal(lng) to hge and check for overflow", args(1,4, batarg("",hge),arg("s1",int),batarg("v",lng),batarg("s",oid))),
    6244             :  command("batcalc", "hge", batlng_dec2dec_hge, false, "cast decimal(lng) to decimal(hge) and check for overflow", args(1,6, batarg("",hge),arg("s1",int),batarg("v",lng),batarg("s",oid),arg("d2",int),arg("s2",int))),
    6245             :  command("calc", "hge", hge_num2dec_hge, false, "cast number to decimal(hge) and check for overflow", args(1,4, arg("",hge),arg("v",hge),arg("digits",int),arg("scale",int))),
    6246             :  command("batcalc", "hge", bathge_num2dec_hge, false, "cast number to decimal(hge) and check for overflow", args(1,5, batarg("",hge),batarg("v",hge),batarg("s",oid),arg("digits",int),arg("scale",int))),
    6247             :  command("calc", "hge", hge_dec2_hge, false, "cast decimal(hge) to hge and check for overflow", args(1,3, arg("",hge),arg("s1",int),arg("v",hge))),
    6248             :  command("calc", "hge", hge_dec2dec_hge, false, "cast decimal(hge) to decimal(hge) and check for overflow", args(1,5, arg("",hge),arg("s1",int),arg("v",hge),arg("d2",int),arg("s2",int))),
    6249             :  command("batcalc", "hge", bathge_dec2_hge, false, "cast decimal(hge) to hge and check for overflow", args(1,4, batarg("",hge),arg("s1",int),batarg("v",hge),batarg("s",oid))),
    6250             :  command("batcalc", "hge", bathge_dec2dec_hge, false, "cast decimal(hge) to decimal(hge) and check for overflow", args(1,6, batarg("",hge),arg("s1",int),batarg("v",hge),batarg("s",oid),arg("d2",int),arg("s2",int))),
    6251             :  command("calc", "bte", hge_num2dec_bte, false, "cast number to decimal(bte) and check for overflow", args(1,4, arg("",bte),arg("v",hge),arg("digits",int),arg("scale",int))),
    6252             :  command("batcalc", "bte", bathge_num2dec_bte, false, "cast number to decimal(bte) and check for overflow", args(1,5, batarg("",bte),batarg("v",hge),batarg("s",oid),arg("digits",int),arg("scale",int))),
    6253             :  command("calc", "bte", hge_dec2_bte, false, "cast decimal(hge) to bte and check for overflow", args(1,3, arg("",bte),arg("s1",int),arg("v",hge))),
    6254             :  command("calc", "bte", hge_dec2dec_bte, false, "cast decimal(hge) to decimal(bte) and check for overflow", args(1,5, arg("",bte),arg("s1",int),arg("v",hge),arg("d2",int),arg("s2",int))),
    6255             :  command("batcalc", "bte", bathge_dec2_bte, false, "cast decimal(hge) to bte and check for overflow", args(1,4, batarg("",bte),arg("s1",int),batarg("v",hge),batarg("s",oid))),
    6256             :  command("batcalc", "bte", bathge_dec2dec_bte, false, "cast decimal(hge) to decimal(bte) and check for overflow", args(1,6, batarg("",bte),arg("s1",int),batarg("v",hge),batarg("s",oid),arg("d2",int),arg("s2",int))),
    6257             :  command("calc", "sht", hge_num2dec_sht, false, "cast number to decimal(sht) and check for overflow", args(1,4, arg("",sht),arg("v",hge),arg("digits",int),arg("scale",int))),
    6258             :  command("batcalc", "sht", bathge_num2dec_sht, false, "cast number to decimal(sht) and check for overflow", args(1,5, batarg("",sht),batarg("v",hge),batarg("s",oid),arg("digits",int),arg("scale",int))),
    6259             :  command("calc", "sht", hge_dec2_sht, false, "cast decimal(hge) to sht and check for overflow", args(1,3, arg("",sht),arg("s1",int),arg("v",hge))),
    6260             :  command("calc", "sht", hge_dec2dec_sht, false, "cast decimal(hge) to decimal(sht) and check for overflow", args(1,5, arg("",sht),arg("s1",int),arg("v",hge),arg("d2",int),arg("s2",int))),
    6261             :  command("batcalc", "sht", bathge_dec2_sht, false, "cast decimal(hge) to sht and check for overflow", args(1,4, batarg("",sht),arg("s1",int),batarg("v",hge),batarg("s",oid))),
    6262             :  command("batcalc", "sht", bathge_dec2dec_sht, false, "cast decimal(hge) to decimal(sht) and check for overflow", args(1,6, batarg("",sht),arg("s1",int),batarg("v",hge),batarg("s",oid),arg("d2",int),arg("s2",int))),
    6263             :  command("calc", "int", hge_num2dec_int, false, "cast number to decimal(int) and check for overflow", args(1,4, arg("",int),arg("v",hge),arg("digits",int),arg("scale",int))),
    6264             :  command("batcalc", "int", bathge_num2dec_int, false, "cast number to decimal(int) and check for overflow", args(1,5, batarg("",int),batarg("v",hge),batarg("s",oid),arg("digits",int),arg("scale",int))),
    6265             :  command("calc", "int", hge_dec2_int, false, "cast decimal(hge) to int and check for overflow", args(1,3, arg("",int),arg("s1",int),arg("v",hge))),
    6266             :  command("calc", "int", hge_dec2dec_int, false, "cast decimal(hge) to decimal(int) and check for overflow", args(1,5, arg("",int),arg("s1",int),arg("v",hge),arg("d2",int),arg("s2",int))),
    6267             :  command("batcalc", "int", bathge_dec2_int, false, "cast decimal(hge) to int and check for overflow", args(1,4, batarg("",int),arg("s1",int),batarg("v",hge),batarg("s",oid))),
    6268             :  command("batcalc", "int", bathge_dec2dec_int, false, "cast decimal(hge) to decimal(int) and check for overflow", args(1,6, batarg("",int),arg("s1",int),batarg("v",hge),batarg("s",oid),arg("d2",int),arg("s2",int))),
    6269             :  command("calc", "lng", hge_num2dec_lng, false, "cast number to decimal(lng) and check for overflow", args(1,4, arg("",lng),arg("v",hge),arg("digits",int),arg("scale",int))),
    6270             :  command("batcalc", "lng", bathge_num2dec_lng, false, "cast number to decimal(lng) and check for overflow", args(1,5, batarg("",lng),batarg("v",hge),batarg("s",oid),arg("digits",int),arg("scale",int))),
    6271             :  command("calc", "lng", hge_dec2_lng, false, "cast decimal(hge) to lng and check for overflow", args(1,3, arg("",lng),arg("s1",int),arg("v",hge))),
    6272             :  command("calc", "lng", hge_dec2dec_lng, false, "cast decimal(hge) to decimal(lng) and check for overflow", args(1,5, arg("",lng),arg("s1",int),arg("v",hge),arg("d2",int),arg("s2",int))),
    6273             :  command("batcalc", "lng", bathge_dec2_lng, false, "cast decimal(hge) to lng and check for overflow", args(1,4, batarg("",lng),arg("s1",int),batarg("v",hge),batarg("s",oid))),
    6274             :  command("batcalc", "lng", bathge_dec2dec_lng, false, "cast decimal(hge) to decimal(lng) and check for overflow", args(1,6, batarg("",lng),arg("s1",int),batarg("v",hge),batarg("s",oid),arg("d2",int),arg("s2",int))),
    6275             :  command("calc", "flt", hge_num2dec_flt, false, "cast number to decimal(flt) and check for overflow", args(1,4, arg("",flt),arg("v",hge),arg("digits",int),arg("sc