LCOV - code coverage report
Current view: top level - sql/backends/monet5 - sql.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 2247 3591 62.6 %
Date: 2024-04-25 20:03:45 Functions: 75 101 74.3 %

          Line data    Source code
       1             : /*
       2             :  * SPDX-License-Identifier: MPL-2.0
       3             :  *
       4             :  * This Source Code Form is subject to the terms of the Mozilla Public
       5             :  * License, v. 2.0.  If a copy of the MPL was not distributed with this
       6             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       7             :  *
       8             :  * Copyright 2024 MonetDB Foundation;
       9             :  * Copyright August 2008 - 2023 MonetDB B.V.;
      10             :  * Copyright 1997 - July 2008 CWI.
      11             :  */
      12             : 
      13             : /*
      14             :  * authors M Kersten, N Nes
      15             :  * SQL support implementation
      16             :  * This module contains the wrappers around the SQL
      17             :  * multi-version-catalog and support routines copied
      18             :  * from the Version 4 code base.
      19             :  */
      20             : #include "monetdb_config.h"
      21             : #include "sql.h"
      22             : #include "mapi_prompt.h"
      23             : #include "sql_result.h"
      24             : #include "sql_storage.h"
      25             : #include "sql_scenario.h"
      26             : #include "store_sequence.h"
      27             : #include "sql_partition.h"
      28             : #include "rel_partition.h"
      29             : #include "rel_rel.h"
      30             : #include "rel_exp.h"
      31             : #include "rel_physical.h"
      32             : #include "mal.h"
      33             : #include "mal_client.h"
      34             : #include "mal_interpreter.h"
      35             : #include "mal_resolve.h"
      36             : #include "mal_client.h"
      37             : #include "mal_interpreter.h"
      38             : #include "mal_profiler.h"
      39             : #include "bat5.h"
      40             : #include "opt_pipes.h"
      41             : #include "clients.h"
      42             : #include "mal_instruction.h"
      43             : #include "mal_resource.h"
      44             : #include "mal_authorize.h"
      45             : #include "gdk_cand.h"
      46             : 
      47             : static inline void
      48           0 : BBPnreclaim(int nargs, ...)
      49             : {
      50           0 :         va_list valist;
      51           0 :         va_start(valist, nargs);
      52           0 :         for (int i = 0; i < nargs; i++) {
      53           0 :                 BAT *b = va_arg(valist, BAT *);
      54           0 :                 BBPreclaim(b);
      55             :         }
      56           0 :         va_end(valist);
      57           0 : }
      58             : 
      59             : static int
      60       18180 : rel_is_table(sql_rel *rel)
      61             : {
      62       18180 :         if (!rel || is_base(rel->op))
      63       13996 :                 return 1;
      64             :         return 0;
      65             : }
      66             : 
      67             : static int
      68       13996 : exp_is_point_select(sql_exp *e)
      69             : {
      70       13996 :         if (!e)
      71             :                 return 1;
      72       13996 :         if (e->type == e_cmp && !e->f && e->flag == (int) cmp_equal) {
      73        6988 :                 sql_exp *r = e->r;
      74        6988 :                 sql_exp *l = e->l;
      75             : 
      76        6988 :                 if (!is_func(l->type) && r->card <= CARD_AGGR)
      77        6887 :                         return 1;
      78             :         }
      79             :         return 0;
      80             : }
      81             : 
      82             : static int
      83      544062 : rel_no_mitosis(mvc *sql, sql_rel *rel)
      84             : {
      85     1279538 :         if (mvc_highwater(sql))
      86             :                 return 0;
      87      961413 :         if (!rel || is_basetable(rel->op))
      88             :                 return 1;
      89             :         /* use mitosis on order topn */
      90      784158 :         if (is_topn(rel->op)) {
      91       16499 :                 sql_rel *l = rel->l;
      92       16499 :                 if (l && is_simple_project(l->op) && l->r)
      93             :                         return 0;
      94             :         }
      95      783872 :         if (is_topn(rel->op) || is_sample(rel->op) || is_simple_project(rel->op))
      96      312756 :                 return rel_no_mitosis(sql, rel->l);
      97      471116 :         if (is_ddl(rel->op) && rel->flag == ddl_output) {
      98             :                 // COPY SELECT ... INTO
      99          74 :                 return rel_no_mitosis(sql, rel->l);
     100             :         }
     101      471042 :         if ((is_delete(rel->op) || is_truncate(rel->op)) && rel->card <= CARD_AGGR)
     102             :                 return 1;
     103      429255 :         if ((is_insert(rel->op) || is_update(rel->op)) && rel->card <= CARD_AGGR)
     104      104516 :                 return rel_no_mitosis(sql, rel->r);
     105      338735 :         if (is_select(rel->op) && rel_is_table(rel->l) && !list_empty(rel->exps)) {
     106             :                 /* just one point expression makes this a point query */
     107       13996 :                 if (exp_is_point_select(rel->exps->h->data))
     108             :                         return 1;
     109             :         }
     110             :         return 0;
     111             : }
     112             : 
     113             : static int
     114      318138 : rel_need_distinct_query(sql_rel *rel)
     115             : {
     116      318138 :         int need_distinct = 0;
     117             : 
     118      356743 :         while (rel && is_simple_project(rel->op))
     119       38605 :                 rel = rel->l;
     120      318138 :         if (rel && is_groupby(rel->op) && !list_empty(rel->exps) && list_empty(rel->r)) {
     121        2447 :                 for (node *n = rel->exps->h; n && !need_distinct; n = n->next) {
     122        1367 :                         sql_exp *e = n->data;
     123             : 
     124        1367 :                         if (e->type == e_aggr && need_distinct(e))
     125        1367 :                                 need_distinct = 1;
     126             :                 }
     127             :         }
     128      318138 :         return need_distinct;
     129             : }
     130             : 
     131             : sql_rel *
     132      546394 : sql_symbol2relation(backend *be, symbol *sym)
     133             : {
     134      546394 :         sql_rel *rel;
     135      546394 :         sql_query *query = query_create(be->mvc);
     136      546391 :         lng Tbegin, Tend;
     137      546391 :         int value_based_opt = be->mvc->emode != m_prepare, storage_based_opt;
     138      546391 :         int profile = be->mvc->emode == m_plan;
     139      546391 :         Client c = be->client;
     140             : 
     141      546391 :         Tbegin = GDKusec();
     142      546391 :         rel = rel_semantic(query, sym);
     143      546391 :         Tend = GDKusec();
     144      546391 :         if(profilerStatus > 0 )
     145           0 :                 profilerEvent(NULL,
     146             :                                           &(struct NonMalEvent)
     147           0 :                                           {SQL_TO_REL, c, Tend, NULL, NULL, rel?0:1, Tend-Tbegin});
     148             : 
     149      546391 :         storage_based_opt = value_based_opt && rel && !is_ddl(rel->op);
     150      277809 :         Tbegin = Tend;
     151        2651 :         if (rel)
     152      544087 :                 rel = sql_processrelation(be->mvc, rel, profile, 1, value_based_opt, storage_based_opt);
     153      544082 :         if (rel)
     154      544061 :                 rel = rel_partition(be->mvc, rel);
     155      544061 :         if (rel && (rel_no_mitosis(be->mvc, rel) || rel_need_distinct_query(rel)))
     156      225988 :                 be->no_mitosis = 1;
     157      544066 :         if (rel /*&& (be->mvc->emode != m_plan || (ATOMIC_GET(&GDKdebug) & FORCEMITOMASK) == 0)*/)
     158      544066 :                 rel = rel_physical(be->mvc, rel);
     159      546379 :         Tend = GDKusec();
     160      546380 :         be->reloptimizer = Tend - Tbegin;
     161             : 
     162      546380 :         if(profilerStatus > 0)
     163           0 :                 profilerEvent(NULL,
     164             :                                           &(struct NonMalEvent)
     165           0 :                                           {REL_OPT, c, Tend, NULL, NULL, rel?0:1, be->reloptimizer});
     166      546380 :         return rel;
     167             : }
     168             : 
     169             : /*
     170             :  * After the SQL statement has been executed, its data structures
     171             :  * should be garbage collected. For successful actions we have to finish
     172             :  * the transaction as well, e.g. commit or rollback.
     173             :  */
     174             : int
     175      716367 : sqlcleanup(backend *be, int err)
     176             : {
     177      716367 :         sql_destroy_params(be->mvc);
     178             : 
     179             :         /* some statements dynamically disable caching */
     180      716350 :         be->mvc->sym = NULL;
     181      716350 :         be->mvc->runs = NULL;
     182      716350 :         if (be->mvc->ta)
     183      716356 :                 be->mvc->ta = sa_reset(be->mvc->ta);
     184      716354 :         if (be->mvc->sa)
     185      716021 :                 be->mvc->sa = sa_reset(be->mvc->sa);
     186      716350 :         if (err >0)
     187         362 :                 be->mvc->session->status = -err;
     188      716350 :         if (err <0)
     189       34775 :                 be->mvc->session->status = err;
     190      716350 :         be->mvc->label = 0;
     191      716350 :         be->no_mitosis = 0;
     192      716350 :         scanner_query_processed(&(be->mvc->scanner));
     193      716353 :         return err;
     194             : }
     195             : 
     196             : /*
     197             :  * The internal administration of the MAL compiler and execution state
     198             :  * is administered by a state descriptor accessible in each phase.
     199             :  * Failure to find the state descriptor aborts the session.
     200             :  */
     201             : 
     202             : str
     203     6110774 : checkSQLContext(Client cntxt)
     204             : {
     205     6110774 :         backend *be;
     206             : 
     207     6110774 :         if (cntxt == NULL)
     208           0 :                 throw(SQL, "mvc", SQLSTATE(42005) "No client record");
     209     6110774 :         if (cntxt->sqlcontext == NULL)
     210           0 :                 throw(SQL, "mvc", SQLSTATE(42006) "SQL module not initialized");
     211     6110774 :         be = (backend *) cntxt->sqlcontext;
     212     6110774 :         if (be->mvc == NULL)
     213           0 :                 throw(SQL, "mvc", SQLSTATE(42006) "SQL module not initialized, mvc struct missing");
     214             :         return MAL_SUCCEED;
     215             : }
     216             : 
     217             : str
     218      122307 : getBackendContext(Client cntxt, backend **be)
     219             : {
     220      122307 :         str msg;
     221             : 
     222      122307 :         if ((msg = checkSQLContext(cntxt)) != MAL_SUCCEED)
     223             :                 return msg;
     224      122307 :         *be = (backend *) cntxt->sqlcontext;
     225      122307 :         return MAL_SUCCEED;
     226             : }
     227             : 
     228             : str
     229     2919322 : getSQLContext(Client cntxt, MalBlkPtr mb, mvc **c, backend **b)
     230             : {
     231     2919322 :         backend *be;
     232     2919322 :         (void) mb;
     233     2919322 :         str msg;
     234             : 
     235     2919322 :         if ((msg = checkSQLContext(cntxt)) != MAL_SUCCEED)
     236             :                 return msg;
     237     2918972 :         be = (backend *) cntxt->sqlcontext;
     238     2918972 :         if (c)
     239     2918972 :                 *c = be->mvc;
     240     2918972 :         if (b)
     241         458 :                 *b = be;
     242             :         return MAL_SUCCEED;
     243             : }
     244             : 
     245             : str
     246      263328 : SQLmvc(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
     247             : {
     248      263328 :         mvc *sql = NULL;
     249      263328 :         str msg;
     250      263328 :         int *res = getArgReference_int(stk, pci, 0);
     251             : 
     252      263328 :         if ((msg = getSQLContext(cntxt, mb, &sql, NULL)) != NULL)
     253             :                 return msg;
     254      263329 :         if ((msg = checkSQLContext(cntxt)) != NULL)
     255             :                 return msg;
     256      263328 :         *res = 0;
     257      263328 :         return MAL_SUCCEED;
     258             : }
     259             : 
     260             : static str
     261           2 : SQLshutdown_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
     262             : {
     263           2 :         str msg;
     264             : 
     265           2 :         if ((msg = CLTshutdown(cntxt, mb, stk, pci)) == MAL_SUCCEED) {
     266             :                 /* administer the shutdown in the system log */
     267           2 :                 TRC_INFO(SQL_TRANS, "Shutdown: %s\n", *getArgReference_str(stk, pci, 0));
     268             :         }
     269           2 :         return msg;
     270             : }
     271             : 
     272             : static str
     273           1 : SQLset_protocol(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
     274             : {
     275           1 :         const int protocol = *getArgReference_int(stk, pci, 1);
     276             : 
     277           1 :         (void) mb;
     278           1 :         (void) stk;
     279             : 
     280           1 :         if (!(
     281           1 :                 protocol == PROTOCOL_AUTO ||
     282             :                 protocol == PROTOCOL_9 ||
     283           1 :                 protocol == PROTOCOL_COLUMNAR))
     284             :         {
     285           0 :                 return createException(SQL, "sql.set_protocol", "unknown protocol: %d", protocol);
     286             :         }
     287             : 
     288           1 :         *getArgReference_int(stk, pci, 0) = (cntxt->protocol = (protocol_version) protocol);
     289             : 
     290           1 :         return MAL_SUCCEED;
     291             : }
     292             : 
     293             : str
     294       31780 : create_table_or_view(mvc *sql, char *sname, char *tname, sql_table *t, int temp, int replace)
     295             : {
     296       31780 :         sql_allocator *osa;
     297       31780 :         sql_schema *s = mvc_bind_schema(sql, sname);
     298       31780 :         sql_table *nt = NULL, *ot;
     299       31780 :         node *n;
     300       31780 :         int check = 0;
     301       31780 :         const char *action = (temp == SQL_DECLARED_TABLE) ? "DECLARE" : (replace ? "CREATE OR REPLACE" : "CREATE");
     302       31780 :         const char *obj = t->query ? "VIEW" : "TABLE";
     303       31780 :         str msg = MAL_SUCCEED;
     304             : 
     305       31780 :         if (store_readonly(sql->session->tr->store))
     306           0 :                 throw(SQL, "sql.catalog", SQLSTATE(25006) "schema statements cannot be executed on a readonly database.");
     307             : 
     308       31780 :         if (!s)
     309           0 :                 throw(SQL, "sql.catalog", SQLSTATE(3F000) "%s %s: schema '%s' doesn't exist", action, obj, sname);
     310       31780 :         if (temp != SQL_DECLARED_TABLE && (!mvc_schema_privs(sql, s) && !(isTempSchema(s) && temp == SQL_LOCAL_TEMP)))
     311           0 :                 throw(SQL, "sql.catalog", SQLSTATE(42000) "%s %s: insufficient privileges for user '%s' in schema '%s'",
     312             :                                                  action, obj, get_string_global_var(sql, "current_user"), s->base.name);
     313       31780 :         if ((ot = mvc_bind_table(sql, s, t->base.name))) {
     314          19 :                 if (replace) {
     315          19 :                         if (ot->type != t->type)
     316           0 :                                 throw(SQL, "sql.catalog", SQLSTATE(42000) "%s %s: unable to drop %s '%s': is a %s",
     317           0 :                                                                  action, obj, obj, t->base.name, TABLE_TYPE_DESCRIPTION(ot->type, ot->properties));
     318          19 :                         if (ot->system)
     319           0 :                                 throw(SQL, "sql.catalog", SQLSTATE(42000) "%s %s: cannot replace system %s '%s'", action, obj, obj, t->base.name);
     320          19 :                         if (mvc_check_dependency(sql, ot->base.id, isView(ot) ? VIEW_DEPENDENCY : TABLE_DEPENDENCY, NULL))
     321           2 :                                 throw(SQL, "sql.catalog", SQLSTATE(42000) "%s %s: cannot replace %s '%s', there are database objects which depend on it",
     322             :                                                                  action, obj, obj, t->base.name);
     323          17 :                         if ((msg = mvc_drop_table(sql, s, ot, 0)) != MAL_SUCCEED)
     324             :                                 return msg;
     325             :                 } else {
     326           0 :                         throw(SQL, "sql.catalog", SQLSTATE(42S01) "%s %s: name '%s' already in use", action, obj, t->base.name);
     327             :                 }
     328             :         }
     329       31778 :         if (temp == SQL_DECLARED_TABLE && ol_length(t->keys))
     330           0 :                 throw(SQL, "sql.catalog", SQLSTATE(42000) "%s %s: '%s' cannot have constraints", action, obj, t->base.name);
     331             : 
     332       31778 :         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)) {
     333           0 :                 case -1:
     334           0 :                         throw(SQL, "sql.catalog", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     335           4 :                 case -2:
     336             :                 case -3:
     337           4 :                         throw(SQL, "sql.catalog", SQLSTATE(42000) "%s %s: '%s' name conflicts", action, obj, t->base.name);
     338             :                 default:
     339       31774 :                         break;
     340             :         }
     341       31774 :         osa = sql->sa;
     342       31774 :         sql_allocator *nsa = sql->sa = sa_create(NULL);
     343             :         /* first check default values */
     344      284523 :         for (n = ol_first_node(t->columns); n; n = n->next) {
     345      252750 :                 sql_column *c = n->data;
     346             : 
     347      252750 :                 if (c->def) {
     348             :                         /* TODO please don't place an auto incremented sequence in the default value */
     349        1376 :                         const char *next_value_for = "next value for \"sys\".\"seq_";
     350        1376 :                         sql_rel *r = NULL;
     351             : 
     352        1376 :                         sa_reset(nsa);
     353        1376 :                         sql->sa = nsa;
     354        1376 :                         r = rel_parse(sql, s, sa_message(sql->ta, "select %s;", c->def), m_deps);
     355        2752 :                         if (!r || !is_project(r->op) || !r->exps || list_length(r->exps) != 1 ||
     356        1376 :                                 exp_check_type(sql, &c->type, r, r->exps->h->data, type_equal) == NULL) {
     357           1 :                                 if (r)
     358           1 :                                         rel_destroy(r);
     359           1 :                                 sa_destroy(nsa);
     360           1 :                                 sql->sa = osa;
     361           1 :                                 if (strlen(sql->errstr) > 6 && sql->errstr[5] == '!')
     362           1 :                                         throw(SQL, "sql.catalog", "%s", sql->errstr);
     363             :                                 else
     364           0 :                                         throw(SQL, "sql.catalog", SQLSTATE(42000) "%s", sql->errstr);
     365             :                         }
     366             :                         /* For a self incremented column, it's sequence will get a BEDROPPED_DEPENDENCY,
     367             :                                 so no additional dependencies are needed */
     368        1375 :                         if (strncmp(c->def, next_value_for, strlen(next_value_for)) != 0) {
     369        1175 :                                 list *blist = rel_dependencies(sql, r);
     370        1175 :                                 if (mvc_create_dependencies(sql, blist, nt->base.id, FUNC_DEPENDENCY)) {
     371           0 :                                         sa_destroy(nsa);
     372           0 :                                         sql->sa = osa;
     373           0 :                                         throw(SQL, "sql.catalog", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     374             :                                 }
     375             :                         }
     376        1375 :                         sa_reset(sql->sa);
     377             :                 }
     378             :         }
     379             : 
     380      284522 :         for (n = ol_first_node(t->columns); n; n = n->next) {
     381      252749 :                 sql_column *c = n->data, *copied = NULL;
     382             : 
     383      252749 :                 switch (mvc_copy_column(sql, nt, c, &copied)) {
     384           0 :                         case -1:
     385           0 :                                 sa_destroy(nsa);
     386           0 :                                 sql->sa = osa;
     387           0 :                                 throw(SQL, "sql.catalog", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     388           0 :                         case -2:
     389             :                         case -3:
     390           0 :                                 sa_destroy(nsa);
     391           0 :                                 sql->sa = osa;
     392           0 :                                 throw(SQL, "sql.catalog", SQLSTATE(42000) "CREATE TABLE: %s_%s_%s conflicts", s->base.name, t->base.name, c->base.name);
     393             :                         default:
     394      252749 :                                 break;
     395             :                 }
     396      252749 :                 if (isPartitionedByColumnTable(t) && c->base.id == t->part.pcol->base.id)
     397          72 :                         nt->part.pcol = copied;
     398             :         }
     399       31773 :         if (isPartitionedByExpressionTable(t)) {
     400          22 :                 char *err = NULL;
     401             : 
     402          22 :                 _DELETE(nt->part.pexp->exp);
     403          22 :                 nt->part.pexp->exp = _STRDUP(t->part.pexp->exp);
     404          22 :                 err = bootstrap_partition_expression(sql, nt, 1);
     405          22 :                 if (err) {
     406           3 :                         sa_destroy(nsa);
     407           3 :                         sql->sa = osa;
     408           3 :                         return err;
     409             :                 }
     410          19 :                 sa_reset(nsa);
     411             :         }
     412       31770 :         check = sql_trans_set_partition_table(sql->session->tr, nt);
     413       31770 :         if (check == -4) {
     414           0 :                 sa_destroy(nsa);
     415           0 :                 sql->sa = osa;
     416           0 :                 throw(SQL, "sql.catalog", SQLSTATE(42000) "CREATE TABLE: %s_%s: the partition's expression is too long", s->base.name, t->base.name);
     417       31770 :         } else if (check) {
     418           0 :                 sa_destroy(nsa);
     419           0 :                 sql->sa = osa;
     420           0 :                 throw(SQL, "sql.catalog", SQLSTATE(42000) "CREATE TABLE: %s_%s: an internal error occurred", s->base.name, t->base.name);
     421             :         }
     422             : 
     423       31770 :         if (t->idxs) {
     424       37682 :                 for (n = ol_first_node(t->idxs); n; n = n->next) {
     425        5912 :                         sql_idx *i = n->data;
     426             : 
     427        5912 :                         switch (mvc_copy_idx(sql, nt, i, NULL)) {
     428           0 :                                 case -1:
     429           0 :                                         sa_destroy(nsa);
     430           0 :                                         sql->sa = osa;
     431           0 :                                         throw(SQL, "sql.catalog", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     432           0 :                                 case -2:
     433             :                                 case -3:
     434           0 :                                         sa_destroy(nsa);
     435           0 :                                         sql->sa = osa;
     436           0 :                                         throw(SQL, "sql.catalog", SQLSTATE(42000) "CREATE TABLE: %s_%s_%s index conflicts", s->base.name, t->base.name, i->base.name);
     437             :                                 default:
     438        5912 :                                         break;
     439             :                         }
     440             :                 }
     441             :         }
     442       31770 :         if (t->keys) {
     443       37675 :                 for (n = ol_first_node(t->keys); n; n = n->next) {
     444        5912 :                         sql_key *k = n->data;
     445        5912 :                         char *err = NULL;
     446             : 
     447        5912 :                         err = sql_partition_validate_key(sql, nt, k, "CREATE");
     448        5912 :                         if (err) {
     449           6 :                                 sa_destroy(nsa);
     450           6 :                                 sql->sa = osa;
     451           6 :                                 return err;
     452             :                         }
     453        5906 :                         sa_reset(sql->sa);
     454        5906 :                         switch (mvc_copy_key(sql, nt, k, NULL)) {
     455           1 :                                 case -1:
     456           1 :                                         sa_destroy(nsa);
     457           1 :                                         sql->sa = osa;
     458           1 :                                         throw(SQL, "sql.catalog", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     459           0 :                                 case -2:
     460             :                                 case -3:
     461           0 :                                         sa_destroy(nsa);
     462           0 :                                         sql->sa = osa;
     463           0 :                                         throw(SQL, "sql.catalog", SQLSTATE(42000) "CREATE TABLE: %s_%s_%s constraint conflicts", s->base.name, t->base.name, k->base.name);
     464             :                                 default:
     465        5905 :                                         break;
     466             :                         }
     467        5905 :                         sa_reset(sql->sa);
     468             :                 }
     469             :         }
     470       31763 :         if (t->triggers) {
     471       31763 :                 for (n = ol_first_node(t->triggers); n; n = n->next) {
     472           0 :                         sql_trigger *tr = n->data;
     473             : 
     474           0 :                         switch (mvc_copy_trigger(sql, nt, tr, NULL)) {
     475           0 :                                 case -1:
     476           0 :                                         sa_destroy(nsa);
     477           0 :                                         sql->sa = osa;
     478           0 :                                         throw(SQL, "sql.catalog", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     479           0 :                                 case -2:
     480             :                                 case -3:
     481           0 :                                         sa_destroy(nsa);
     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           0 :                                         break;
     486             :                         }
     487             :                 }
     488             :         }
     489             :         /* also create dependencies when not renaming */
     490       31763 :         if (nt->query && isView(nt)) {
     491       22179 :                 sql_rel *r = NULL;
     492             : 
     493       22179 :                 sa_reset(nsa);
     494       22179 :                 r = rel_parse(sql, s, nt->query, m_deps);
     495       22179 :                 if (r)
     496       22179 :                         r = sql_processrelation(sql, r, 0, 0, 0, 0);
     497       22179 :                 if (r) {
     498       22179 :                         list *blist = rel_dependencies(sql, r);
     499       22179 :                         if (mvc_create_dependencies(sql, blist, nt->base.id, VIEW_DEPENDENCY)) {
     500           0 :                                 sa_destroy(nsa);
     501           0 :                                 sql->sa = osa;
     502           0 :                                 throw(SQL, "sql.catalog", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     503             :                         }
     504             :                 }
     505       22179 :                 sql->sa = osa;
     506       22179 :                 if (!r) {
     507           0 :                         sa_destroy(nsa);
     508           0 :                         if (strlen(sql->errstr) > 6 && sql->errstr[5] == '!')
     509           0 :                                 throw(SQL, "sql.catalog", "%s", sql->errstr);
     510             :                         else
     511           0 :                                 throw(SQL, "sql.catalog", SQLSTATE(42000) "%s", sql->errstr);
     512             :                 }
     513             :         }
     514       31763 :         sa_destroy(nsa);
     515       31763 :         sql->sa = osa;
     516       31763 :         return MAL_SUCCEED;
     517             : }
     518             : 
     519             : static int
     520      103301 : mvc_claim_slots(sql_trans *tr, sql_table *t, size_t cnt, BUN *offset, BAT **pos)
     521             : {
     522      103301 :         sqlstore *store = tr->store;
     523      103301 :         return store->storage_api.claim_tab(tr, t, cnt, offset, pos);
     524             : }
     525             : 
     526             : str
     527      103288 : mvc_claim_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
     528             : {
     529      103288 :         BUN *offset = (BUN*)getArgReference_oid(stk, pci, 0);
     530      103288 :         bat *res = getArgReference_bat(stk, pci, 1);
     531      103288 :         mvc *m = NULL;
     532      103288 :         str msg;
     533      103288 :         const char *sname = *getArgReference_str(stk, pci, 3);
     534      103288 :         const char *tname = *getArgReference_str(stk, pci, 4);
     535      103288 :         lng cnt = *getArgReference_lng(stk, pci, 5);
     536      103288 :         BAT *pos = NULL;
     537      103288 :         sql_schema *s;
     538      103288 :         sql_table *t;
     539             : 
     540      103288 :         *res = 0;
     541      103288 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
     542             :                 return msg;
     543      103288 :         if ((msg = checkSQLContext(cntxt)) != NULL)
     544             :                 return msg;
     545             : 
     546      103288 :         s = mvc_bind_schema(m, sname);
     547      103288 :         if (s == NULL)
     548           0 :                 throw(SQL, "sql.claim", SQLSTATE(3F000) "Schema missing %s", sname);
     549      103288 :         t = mvc_bind_table(m, s, tname);
     550      103288 :         if (t == NULL)
     551           0 :                 throw(SQL, "sql.claim", SQLSTATE(42S02) "Table missing %s.%s", sname, tname);
     552      103288 :         if (!isTable(t))
     553           0 :                 throw(SQL, "sql.claim", SQLSTATE(42000) "%s '%s' is not persistent", TABLE_TYPE_DESCRIPTION(t->type, t->properties), t->base.name);
     554      103288 :         if (mvc_claim_slots(m->session->tr, t, (size_t)cnt, offset, &pos) == LOG_OK) {
     555      103288 :                 *res = bat_nil;
     556      103288 :                 if (pos) {
     557           7 :                         *res = pos->batCacheid;
     558           7 :                         BBPkeepref(pos);
     559             :                 }
     560      103288 :                 return MAL_SUCCEED;
     561             :         }
     562           0 :         throw(SQL, "sql.claim", SQLSTATE(3F000) "Could not claim slots");
     563             : }
     564             : 
     565             : str
     566      132367 : mvc_add_dependency_change(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
     567             : {
     568      132367 :         str msg;
     569      132367 :         mvc *m = NULL;
     570      132367 :         const char *sname = *getArgReference_str(stk, pci, 1);
     571      132367 :         const char *tname = *getArgReference_str(stk, pci, 2);
     572      132367 :         lng cnt = *getArgReference_lng(stk, pci, 3);
     573      132367 :         sql_schema *s;
     574      132367 :         sql_table *t;
     575             : 
     576      132367 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
     577             :                 return msg;
     578      132365 :         if ((msg = checkSQLContext(cntxt)) != NULL)
     579             :                 return msg;
     580             : 
     581      132365 :         if ((s = mvc_bind_schema(m, sname)) == NULL)
     582           0 :                 throw(SQL, "sql.dependency_change", SQLSTATE(3F000) "Schema missing %s", sname);
     583      132360 :         if ((t = mvc_bind_table(m, s, tname)) == NULL)
     584           0 :                 throw(SQL, "sql.dependency_change", SQLSTATE(42S02) "Table missing %s.%s", sname, tname);
     585      132365 :         if (!isTable(t))
     586           0 :                 throw(SQL, "sql.dependency_change", SQLSTATE(42000) "%s '%s' is not persistent", TABLE_TYPE_DESCRIPTION(t->type, t->properties), t->base.name);
     587      132365 :         if (cnt > 0 && !isNew(t) && isGlobal(t) && !isGlobalTemp(t) && sql_trans_add_dependency_change(m->session->tr, t->base.id, dml) != LOG_OK)
     588           0 :                 throw(SQL, "sql.dependency_change", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     589             :         return MAL_SUCCEED;
     590             : }
     591             : 
     592             : str
     593       12521 : mvc_add_column_predicate(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
     594             : {
     595       12521 :         str msg;
     596       12521 :         mvc *m = NULL;
     597       12521 :         const char *sname = *getArgReference_str(stk, pci, 1);
     598       12521 :         const char *tname = *getArgReference_str(stk, pci, 2);
     599       12521 :         const char *cname = *getArgReference_str(stk, pci, 3);
     600       12521 :         sql_schema *s;
     601       12521 :         sql_table *t;
     602       12521 :         sql_column *c;
     603             : 
     604       12521 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
     605             :                 return msg;
     606       12521 :         if ((msg = checkSQLContext(cntxt)) != NULL)
     607             :                 return msg;
     608             : 
     609       12521 :         if ((s = mvc_bind_schema(m, sname)) == NULL)
     610           0 :                 throw(SQL, "sql.column_predicate", SQLSTATE(3F000) "Schema missing %s", sname);
     611       12521 :         if ((t = mvc_bind_table(m, s, tname)) == NULL)
     612           0 :                 throw(SQL, "sql.column_predicate", SQLSTATE(42S02) "Table missing %s.%s", sname, tname);
     613       12521 :         if ((c = mvc_bind_column(m, t, cname)) == NULL)
     614           0 :                 throw(SQL, "sql.column_predicate", SQLSTATE(42S22) "Column not found in %s.%s.%s",sname,tname,cname);
     615             : 
     616       12521 :         if ((m->session->level & tr_snapshot) == tr_snapshot || isNew(c) || !isGlobal(c->t) || isGlobalTemp(c->t))
     617             :                 return MAL_SUCCEED;
     618        5592 :         if (sql_trans_add_predicate(m->session->tr, c, 0, NULL, NULL, false, false) != LOG_OK)
     619           0 :                 throw(SQL, "sql.column_predicate", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     620             :         return MAL_SUCCEED;
     621             : }
     622             : 
     623             : str
     624           1 : create_table_from_emit(Client cntxt, char *sname, char *tname, sql_emit_col *columns, size_t ncols)
     625             : {
     626           1 :         size_t i;
     627           1 :         sql_table *t = NULL;
     628           1 :         sql_schema *s = NULL;
     629           1 :         mvc *sql = NULL;
     630           1 :         str msg = MAL_SUCCEED;
     631             : 
     632           1 :         if ((msg = getSQLContext(cntxt, NULL, &sql, NULL)) != NULL)
     633             :                 return msg;
     634           1 :         if ((msg = checkSQLContext(cntxt)) != NULL)
     635             :                 return msg;
     636             : 
     637           1 :         if (!sname)
     638           0 :                 sname = "sys";
     639           1 :         if (!(s = mvc_bind_schema(sql, sname)))
     640           0 :                 throw(SQL, "sql.catalog", SQLSTATE(3F000) "CREATE TABLE: no such schema '%s'", sname);
     641           1 :         if (!mvc_schema_privs(sql, s))
     642           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);
     643           1 :         switch (mvc_create_table(&t, sql, s, tname, tt_table, 0, SQL_DECLARED_TABLE, CA_COMMIT, -1, 0)) {
     644           0 :                 case -1:
     645           0 :                         throw(SQL, "sql.catalog", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     646           0 :                 case -2:
     647             :                 case -3:
     648           0 :                         throw(SQL, "sql.catalog", SQLSTATE(42000) "CREATE TABLE: transaction conflict detected");
     649             :                 default:
     650             :                         break;
     651             :         }
     652             : 
     653           3 :         for (i = 0; i < ncols; i++) {
     654           2 :                 BAT *b = columns[i].b;
     655           2 :                 const char *atomname = ATOMname(b->ttype);
     656           2 :                 sql_subtype tpe;
     657           2 :                 sql_column *col = NULL;
     658             : 
     659           2 :                 if (!strcmp(atomname, "str"))
     660           2 :                         sql_find_subtype(&tpe, "clob", 0, 0);
     661             :                 else {
     662           0 :                         sql_subtype *t = sql_bind_localtype(atomname);
     663           0 :                         if (!t)
     664           0 :                                 throw(SQL, "sql.catalog", SQLSTATE(3F000) "CREATE TABLE: could not find type for column");
     665           0 :                         tpe = *t;
     666             :                 }
     667             : 
     668           2 :                 if (columns[i].name && columns[i].name[0] == '%')
     669           0 :                         throw(SQL, "sql.catalog", SQLSTATE(42000) "CREATE TABLE: generated labels not allowed in column names, use an alias instead");
     670           2 :                 if (tpe.type->eclass == EC_ANY)
     671           0 :                         throw(SQL, "sql.catalog", SQLSTATE(42000) "CREATE TABLE: any type (plain null value) not allowed as a column type, use an explicit cast");
     672           2 :                 switch (mvc_create_column(&col, sql, t, columns[i].name, &tpe)) {
     673           0 :                         case -1:
     674           0 :                                 throw(SQL, "sql.catalog", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     675           0 :                         case -2:
     676             :                         case -3:
     677           0 :                                 throw(SQL, "sql.catalog", SQLSTATE(42000) "CREATE TABLE: transaction conflict detected");
     678             :                         default:
     679           2 :                                 break;
     680             :                 }
     681             :         }
     682           1 :         if ((msg = create_table_or_view(sql, sname, t->base.name, t, 0, 0)) != MAL_SUCCEED)
     683             :                 return msg;
     684           1 :         if (!(t = mvc_bind_table(sql, s, tname)))
     685           0 :                 throw(SQL, "sql.catalog", SQLSTATE(3F000) "CREATE TABLE: could not bind table %s", tname);
     686           1 :         BUN offset;
     687           1 :         BAT *pos = NULL;
     688           1 :         if (mvc_claim_slots(sql->session->tr, t, BATcount(columns[0].b), &offset, &pos) != LOG_OK)
     689           0 :                 throw(SQL, "sql.catalog", SQLSTATE(3F000) "CREATE TABLE: Could not insert data");
     690           3 :         for (i = 0; i < ncols; i++) {
     691           2 :                 BAT *b = columns[i].b;
     692           2 :                 sql_column *col = NULL;
     693             : 
     694           2 :                 if (!(col = mvc_bind_column(sql, t, columns[i].name))) {
     695           0 :                         bat_destroy(pos);
     696           0 :                         throw(SQL, "sql.catalog", SQLSTATE(3F000) "CREATE TABLE: could not bind column %s", columns[i].name);
     697             :                 }
     698           2 :                 if ((msg = mvc_append_column(sql->session->tr, col, offset, pos, b)) != MAL_SUCCEED) {
     699           0 :                         bat_destroy(pos);
     700           0 :                         return msg;
     701             :                 }
     702             :         }
     703           1 :         bat_destroy(pos);
     704           1 :         return msg;
     705             : }
     706             : 
     707             : str
     708          12 : append_to_table_from_emit(Client cntxt, char *sname, char *tname, sql_emit_col *columns, size_t ncols)
     709             : {
     710          12 :         size_t i;
     711          12 :         sql_table *t;
     712          12 :         sql_schema *s;
     713          12 :         mvc *sql = NULL;
     714          12 :         str msg = MAL_SUCCEED;
     715             : 
     716          12 :         if ((msg = getSQLContext(cntxt, NULL, &sql, NULL)) != NULL)
     717             :                 return msg;
     718          12 :         if ((msg = checkSQLContext(cntxt)) != NULL)
     719             :                 return msg;
     720             : 
     721          12 :         if (!sname)
     722           0 :                 sname = "sys";
     723          12 :         if (!(s = mvc_bind_schema(sql, sname)))
     724           0 :                 throw(SQL, "sql.catalog", SQLSTATE(3F000) "APPEND TABLE: no such schema '%s'", sname);
     725          12 :         if (!(t = mvc_bind_table(sql, s, tname)))
     726           0 :                 throw(SQL, "sql.catalog", SQLSTATE(3F000) "APPEND TABLE: could not bind table %s", tname);
     727          12 :         if (!isTable(t))
     728           0 :                 throw(SQL, "sql.catalog", SQLSTATE(42000) "APPEND TABLE: %s '%s' is not persistent", TABLE_TYPE_DESCRIPTION(t->type, t->properties), t->base.name);
     729          12 :         BUN offset;
     730          12 :         BAT *pos = NULL;
     731          12 :         if (mvc_claim_slots(sql->session->tr, t, BATcount(columns[0].b), &offset, &pos) != LOG_OK)
     732           0 :                 throw(SQL, "sql.catalog", SQLSTATE(3F000) "APPEND TABLE: Could not append data");
     733          62 :         for (i = 0; i < ncols; i++) {
     734          50 :                 BAT *b = columns[i].b;
     735          50 :                 sql_column *col = NULL;
     736             : 
     737          50 :                 if (!(col = mvc_bind_column(sql, t, columns[i].name))) {
     738           0 :                         bat_destroy(pos);
     739           0 :                         throw(SQL, "sql.catalog", SQLSTATE(3F000) "APPEND TABLE: could not bind column %s", columns[i].name);
     740             :                 }
     741          50 :                 if ((msg = mvc_append_column(sql->session->tr, col, offset, pos, b)) != MAL_SUCCEED) {
     742           0 :                         bat_destroy(pos);
     743           0 :                         return msg;
     744             :                 }
     745             :         }
     746          12 :         bat_destroy(pos);
     747          16 :         if (BATcount(columns[0].b) > 0 && !isNew(t) && isGlobal(t) && !isGlobalTemp(t) &&
     748           4 :                 sql_trans_add_dependency_change(sql->session->tr, t->base.id, dml) != LOG_OK)
     749           0 :                 throw(SQL, "sql.catalog", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     750             :         return msg;
     751             : }
     752             : 
     753             : BAT *
     754         253 : mvc_bind(mvc *m, const char *sname, const char *tname, const char *cname, int access)
     755             : {
     756         253 :         sql_trans *tr = m->session->tr;
     757         253 :         BAT *b = NULL;
     758         253 :         sql_schema *s = NULL;
     759         253 :         sql_table *t = NULL;
     760         253 :         sql_column *c = NULL;
     761             : 
     762         253 :         s = mvc_bind_schema(m, sname);
     763         253 :         if (s == NULL)
     764             :                 return NULL;
     765         253 :         t = mvc_bind_table(m, s, tname);
     766         253 :         if (t == NULL || !isTable(t))
     767             :                 return NULL;
     768         253 :         c = mvc_bind_column(m, t, cname);
     769         253 :         if (c == NULL)
     770             :                 return NULL;
     771             : 
     772         253 :         sqlstore *store = tr->store;
     773         253 :         b = store->storage_api.bind_col(tr, c, access);
     774         253 :         return b;
     775             : }
     776             : 
     777             : /* setVariable(int *ret, str *sname, str *name, any value) */
     778             : str
     779         361 : setVariable(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
     780             : {
     781         361 :         int *res = getArgReference_int(stk, pci, 0);
     782         361 :         mvc *m = NULL;
     783         361 :         str msg;
     784         361 :         const char *sname = *getArgReference_str(stk, pci, 2);
     785         361 :         const char *varname = *getArgReference_str(stk, pci, 3);
     786         361 :         int mtype = getArgType(mb, pci, 4);
     787         361 :         sql_schema *s;
     788         361 :         sql_var *var;
     789             : 
     790         361 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
     791             :                 return msg;
     792         361 :         if ((msg = checkSQLContext(cntxt)) != NULL)
     793             :                 return msg;
     794             : 
     795         361 :         if (!(s = mvc_bind_schema(m, sname)))
     796           0 :                 throw(SQL, "sql.setVariable", SQLSTATE(3F000) "Cannot find the schema '%s'", sname);
     797             : 
     798         361 :         *res = 0;
     799         361 :         if (mtype < 0 || mtype >= 255)
     800           0 :                 throw(SQL, "sql.setVariable", SQLSTATE(42100) "Variable type error");
     801             : 
     802         361 :         if ((var = find_global_var(m, s, varname))) {
     803         501 :                 if (!strcmp("sys", s->base.name) && !strcmp("optimizer", varname)) {
     804         167 :                         const char *newopt = *getArgReference_str(stk, pci, 4);
     805         167 :                         char buf[18];
     806             : 
     807         167 :                         if (strNil(newopt))
     808          27 :                                 throw(SQL, "sql.setVariable", SQLSTATE(42000) "Variable '%s.%s' cannot be NULL", sname, varname);
     809         167 :                         if (!isOptimizerPipe(newopt) && strchr(newopt, (int) ';') == 0)
     810           5 :                                 throw(SQL, "sql.setVariable", SQLSTATE(42100) "optimizer '%s' unknown", newopt);
     811         162 :                         (void) snprintf(buf, sizeof(buf), "user_%d", cntxt->idx); /* should always suffice */
     812         162 :                         if (!isOptimizerPipe(newopt) || strcmp(buf, newopt) == 0) {
     813          23 :                                 if ((msg = addPipeDefinition(cntxt, buf, newopt)))
     814             :                                         return msg;
     815           1 :                                 if (!sqlvar_set_string(find_global_var(m, s, varname), buf))
     816           0 :                                         throw(SQL, "sql.setVariable", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     817         139 :                         } else if (!sqlvar_set_string(find_global_var(m, s, varname), newopt))
     818           0 :                                 throw(SQL, "sql.setVariable", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     819             :                 } else {
     820         194 :                         ValPtr ptr = &stk->stk[getArg(pci, 4)];
     821             : 
     822         194 :                         if ((msg = sql_update_var(m, s, varname, ptr)))
     823             :                                 return msg;
     824         179 :                         if (!sqlvar_set(var, ptr))
     825           0 :                                 throw(SQL, "sql.setVariable", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     826             :                 }
     827         319 :                 return MAL_SUCCEED;
     828             :         }
     829           0 :         throw(SQL, "sql.setVariable", SQLSTATE(42100) "Variable '%s.%s' unknown", sname, varname);
     830             : }
     831             : 
     832             : /* getVariable(int *ret, str *name) */
     833             : str
     834         341 : getVariable(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
     835             : {
     836         341 :         int mtype = getArgType(mb, pci, 0);
     837         341 :         mvc *m = NULL;
     838         341 :         str msg;
     839         341 :         const char *sname = *getArgReference_str(stk, pci, 2);
     840         341 :         const char *varname = *getArgReference_str(stk, pci, 3);
     841         341 :         ValRecord *dst, *src;
     842         341 :         sql_schema *s;
     843         341 :         sql_var *var;
     844             : 
     845         341 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
     846             :                 return msg;
     847         341 :         if ((msg = checkSQLContext(cntxt)) != NULL)
     848             :                 return msg;
     849             : 
     850         341 :         if (!(s = mvc_bind_schema(m, sname)))
     851           0 :                 throw(SQL, "sql.getVariable", SQLSTATE(3F000) "Cannot find the schema '%s'", sname);
     852         341 :         if (mtype < 0 || mtype >= 255)
     853           0 :                 throw(SQL, "sql.getVariable", SQLSTATE(42100) "Variable type error");
     854         341 :         if (!(var = find_global_var(m, s, varname)))
     855           0 :                 throw(SQL, "sql.getVariable", SQLSTATE(42100) "Variable '%s.%s' unknown", sname, varname);
     856         341 :         src = &(var->var.data);
     857         341 :         dst = &stk->stk[getArg(pci, 0)];
     858         341 :         if (VALcopy(dst, src) == NULL)
     859           0 :                 throw(MAL, "sql.getVariable", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     860             :         return MAL_SUCCEED;
     861             : }
     862             : 
     863             : str
     864           1 : sql_variables(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
     865             : {
     866           1 :         mvc *m = NULL;
     867           1 :         BAT *schemas, *names, *types, *values;
     868           1 :         str msg = MAL_SUCCEED;
     869           1 :         bat *s = getArgReference_bat(stk,pci,0);
     870           1 :         bat *n = getArgReference_bat(stk,pci,1);
     871           1 :         bat *t = getArgReference_bat(stk,pci,2);
     872           1 :         bat *v = getArgReference_bat(stk,pci,3);
     873           1 :         int nvars;
     874             : 
     875           1 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
     876             :                 return msg;
     877           1 :         if ((msg = checkSQLContext(cntxt)) != NULL)
     878             :                 return msg;
     879             : 
     880           1 :         nvars = list_length(m->global_vars);
     881           1 :         schemas = COLnew(0, TYPE_str, nvars, TRANSIENT);
     882           1 :         names = COLnew(0, TYPE_str, nvars, TRANSIENT);
     883           1 :         types = COLnew(0, TYPE_str, nvars, TRANSIENT);
     884           1 :         values = COLnew(0, TYPE_str, nvars, TRANSIENT);
     885           1 :         if (!schemas || !names || !types || !values) {
     886           0 :                 msg = createException(SQL, "sql.variables", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     887           0 :                 goto bailout;
     888             :         }
     889             : 
     890           1 :         if (m->global_vars) {
     891          10 :                 for (node *n = m->global_vars->h; n ; n = n->next) {
     892           9 :                         sql_var *var = (sql_var*) n->data;
     893           9 :                         atom value = var->var;
     894           9 :                         ValPtr myptr = &(value.data);
     895           9 :                         ValRecord val = (ValRecord) {.vtype = TYPE_void,};
     896           9 :                         gdk_return res;
     897             : 
     898           9 :                         if (value.tpe.type->localtype != TYPE_str) {
     899           5 :                                 ptr ok = VALcopy(&val, myptr);
     900           5 :                                 if (ok)
     901           5 :                                         ok = VALconvert(TYPE_str, &val);
     902           5 :                                 if (!ok) {
     903           0 :                                         VALclear(&val);
     904           0 :                                         msg = createException(SQL, "sql.variables", SQLSTATE(HY013) "Failed to convert variable '%s.%s' into a string", var->sname, var->name);
     905           0 :                                         goto bailout;
     906             :                                 }
     907             :                                 myptr = &val;
     908             :                         }
     909           9 :                         res = BUNappend(values, VALget(myptr), false);
     910           9 :                         VALclear(&val);
     911           9 :                         if (res != GDK_SUCCEED) {
     912           0 :                                 msg = createException(SQL, "sql.variables", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     913           0 :                                 goto bailout;
     914             :                         }
     915           9 :                         if (BUNappend(schemas, var->sname, false) != GDK_SUCCEED) {
     916           0 :                                 msg = createException(SQL, "sql.variables", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     917           0 :                                 goto bailout;
     918             :                         }
     919           9 :                         if (BUNappend(names, var->name, false) != GDK_SUCCEED) {
     920           0 :                                 msg = createException(SQL, "sql.variables", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     921           0 :                                 goto bailout;
     922             :                         }
     923           9 :                         if (BUNappend(types, value.tpe.type->base.name, false) != GDK_SUCCEED) {
     924           0 :                                 msg = createException(SQL, "sql.variables", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     925           0 :                                 goto bailout;
     926             :                         }
     927             :                 }
     928             :         }
     929             : 
     930           1 : bailout:
     931           0 :         if (msg) {
     932           0 :                 BBPreclaim(schemas);
     933           0 :                 BBPreclaim(names);
     934           0 :                 BBPreclaim(types);
     935           0 :                 BBPreclaim(values);
     936             :         } else {
     937           1 :                 *s = schemas->batCacheid;
     938           1 :                 BBPkeepref(schemas);
     939           1 :                 *n = names->batCacheid;
     940           1 :                 BBPkeepref(names);
     941           1 :                 *t = types->batCacheid;
     942           1 :                 BBPkeepref(types);
     943           1 :                 *v = values->batCacheid;
     944           1 :                 BBPkeepref(values);
     945             :         }
     946             :         return msg;
     947             : }
     948             : 
     949             : /* str mvc_logfile(int *d, str *filename); */
     950             : str
     951           0 : mvc_logfile(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
     952             : {
     953           0 :         mvc *m = NULL;
     954           0 :         str msg;
     955           0 :         const char *filename = *getArgReference_str(stk, pci, 1);
     956             : 
     957           0 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
     958             :                 return msg;
     959           0 :         if ((msg = checkSQLContext(cntxt)) != NULL)
     960             :                 return msg;
     961           0 :         if (m->scanner.log) {
     962           0 :                 close_stream(m->scanner.log);
     963           0 :                 m->scanner.log = NULL;
     964             :         }
     965             : 
     966           0 :         if (!strNil(filename)) {
     967           0 :                 if((m->scanner.log = open_wastream(filename)) == NULL)
     968           0 :                         throw(SQL, "sql.logfile", SQLSTATE(HY013) "%s", mnstr_peek_error(NULL));
     969             :         }
     970             :         return MAL_SUCCEED;
     971             : }
     972             : 
     973             : /* str mvc_next_value(lng *res, str *sname, str *seqname); */
     974             : str
     975         962 : mvc_next_value(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
     976             : {
     977         962 :         backend *be = NULL;
     978         962 :         str msg;
     979         962 :         sql_schema *s;
     980         962 :         sql_sequence *seq;
     981         962 :         lng *res = getArgReference_lng(stk, pci, 0);
     982         962 :         const char *sname = *getArgReference_str(stk, pci, 1);
     983         962 :         const char *seqname = *getArgReference_str(stk, pci, 2);
     984             : 
     985         962 :         (void)mb;
     986         962 :         if ((msg = getBackendContext(cntxt, &be)) != NULL)
     987             :                 return msg;
     988         962 :         if (!(s = mvc_bind_schema(be->mvc, sname)))
     989           0 :                 throw(SQL, "sql.next_value", SQLSTATE(3F000) "Cannot find the schema %s", sname);
     990         962 :         if (!mvc_schema_privs(be->mvc, s))
     991           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);
     992         962 :         if (!(seq = find_sql_sequence(be->mvc->session->tr, s, seqname)))
     993           0 :                 throw(SQL, "sql.next_value", SQLSTATE(HY050) "Cannot find the sequence %s.%s", sname, seqname);
     994             : 
     995         962 :         if (seq_next_value(be->mvc->session->tr->store, seq, res)) {
     996         961 :                 be->last_id = *res;
     997         961 :                 sqlvar_set_number(find_global_var(be->mvc, mvc_bind_schema(be->mvc, "sys"), "last_id"), be->last_id);
     998         961 :                 return MAL_SUCCEED;
     999             :         }
    1000           1 :         throw(SQL, "sql.next_value", SQLSTATE(HY050) "Cannot generate next sequence value %s.%s", sname, seqname);
    1001             : }
    1002             : 
    1003             : str
    1004          88 : mvc_next_value_bulk(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    1005             : {
    1006          88 :         backend *be = NULL;
    1007          88 :         str msg;
    1008          88 :         sql_schema *s;
    1009          88 :         sql_sequence *seq;
    1010          88 :         bat *res = getArgReference_bat(stk, pci, 0);
    1011          88 :         BUN card = (BUN)*getArgReference_lng(stk, pci, 1);
    1012          88 :         const char *sname = *getArgReference_str(stk, pci, 2);
    1013          88 :         const char *seqname = *getArgReference_str(stk, pci, 3);
    1014          88 :         BAT *r = NULL;
    1015             : 
    1016          88 :         (void)mb;
    1017          88 :         if ((msg = getBackendContext(cntxt, &be)) != NULL)
    1018             :                 return msg;
    1019          88 :         if (!(s = mvc_bind_schema(be->mvc, sname)))
    1020           0 :                 throw(SQL, "sql.next_value", SQLSTATE(3F000) "Cannot find the schema %s", sname);
    1021          88 :         if (!mvc_schema_privs(be->mvc, s))
    1022           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);
    1023          88 :         if (!(seq = find_sql_sequence(be->mvc->session->tr, s, seqname)))
    1024           0 :                 throw(SQL, "sql.next_value", SQLSTATE(HY050) "Cannot find the sequence %s.%s", sname, seqname);
    1025          88 :         if (!(r = COLnew(0, TYPE_lng, card, TRANSIENT)))
    1026           0 :                 throw(SQL, "sql.next_value", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1027             : 
    1028          88 :         lng *restrict rb = Tloc(r, 0);
    1029             : 
    1030          88 :         if (seqbulk_next_value(be->mvc->session->tr->store, seq, card, rb)) {
    1031          88 :                 be->last_id = rb[card-1];
    1032          88 :                 sqlvar_set_number(find_global_var(be->mvc, mvc_bind_schema(be->mvc, "sys"), "last_id"), be->last_id);
    1033          88 :                 BATsetcount(r, card);
    1034          88 :                 r->tnonil = true;
    1035          88 :                 r->tnil = false;
    1036             :                 /* TODO set the min/max, tsorted/trevsorted and tkey properties based on the sequence values */
    1037          88 :                 r->tsorted = r->trevsorted = r->tkey = BATcount(r) <= 1;
    1038          88 :                 *res = r->batCacheid;
    1039          88 :                 BBPkeepref(r);
    1040          88 :                 return MAL_SUCCEED;
    1041             :         }
    1042           0 :         BBPreclaim(r);
    1043           0 :         throw(SQL, "sql.next_value", SQLSTATE(HY050) "Cannot generate next sequence value %s.%s", sname, seqname);
    1044             : }
    1045             : 
    1046             : /* str mvc_get_value(lng *res, str *sname, str *seqname); */
    1047             : str
    1048          26 : mvc_get_value(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    1049             : {
    1050          26 :         mvc *m = NULL;
    1051          26 :         str msg;
    1052          26 :         sql_schema *s;
    1053          26 :         sql_sequence *seq;
    1054          26 :         lng *res = getArgReference_lng(stk, pci, 0);
    1055          26 :         const char *sname = *getArgReference_str(stk, pci, 1);
    1056          26 :         const char *seqname = *getArgReference_str(stk, pci, 2);
    1057             : 
    1058          26 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    1059             :                 return msg;
    1060          26 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    1061             :                 return msg;
    1062          26 :         if (!(s = mvc_bind_schema(m, sname)))
    1063           0 :                 throw(SQL, "sql.get_value", SQLSTATE(3F000) "Cannot find the schema %s", sname);
    1064          26 :         if (!(seq = find_sql_sequence(m->session->tr, s, seqname)))
    1065           0 :                 throw(SQL, "sql.get_value", SQLSTATE(HY050) "Cannot find the sequence %s.%s", sname, seqname);
    1066             : 
    1067          26 :         if (seq_get_value(m->session->tr->store, seq, res))
    1068             :                 return MAL_SUCCEED;
    1069             : 
    1070           0 :         throw(SQL, "sql.get_value", SQLSTATE(HY050) "Cannot get sequence value %s.%s", sname, seqname);
    1071             : }
    1072             : 
    1073             : /* needed for msqldump and describe_sequences view */
    1074             : static str
    1075         116 : mvc_get_value_bulk(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    1076             : {
    1077         116 :         mvc *m = NULL;
    1078         116 :         sql_schema *s;
    1079         116 :         sql_sequence *seq;
    1080         116 :         BATiter schi, seqi;
    1081         116 :         BAT *bn = NULL, *scheb = NULL, *sches = NULL, *seqb = NULL, *seqs = NULL;
    1082         116 :         lng *restrict vals;
    1083         116 :         str msg = MAL_SUCCEED;
    1084         116 :         bool nils = false;
    1085         116 :         struct canditer ci1 = {0}, ci2 = {0};
    1086         116 :         oid off1, off2;
    1087         116 :         bat *res = getArgReference_bat(stk, pci, 0), *l = getArgReference_bat(stk, pci, 1), *r = getArgReference_bat(stk, pci, 2),
    1088         116 :                 *sid1 = pci->argc == 5 ? getArgReference_bat(stk, pci, 3) : NULL, *sid2 = pci->argc == 5 ? getArgReference_bat(stk, pci, 4) : NULL;
    1089             : 
    1090         116 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    1091             :                 return msg;
    1092         116 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    1093             :                 return msg;
    1094         116 :         if (!(scheb = BATdescriptor(*l)) || !(seqb = BATdescriptor(*r))) {
    1095           0 :                 msg = createException(SQL, "sql.get_value", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
    1096           0 :                 goto bailout;
    1097             :         }
    1098         116 :         if ((sid1 && !is_bat_nil(*sid1) && !(sches = BATdescriptor(*sid1))) || (sid2 && !is_bat_nil(*sid2) && !(seqs = BATdescriptor(*sid2)))) {
    1099           0 :                 msg = createException(SQL, "sql.get_value", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
    1100           0 :                 goto bailout;
    1101             :         }
    1102         116 :         canditer_init(&ci1, scheb, sches);
    1103         116 :         canditer_init(&ci2, seqb, seqs);
    1104         116 :         if (ci2.ncand != ci1.ncand || ci1.hseq != ci2.hseq) {
    1105           0 :                 msg = createException(SQL, "sql.get_value", ILLEGAL_ARGUMENT " Requires bats of identical size");
    1106           0 :                 goto bailout;
    1107             :         }
    1108         116 :         if (!(bn = COLnew(ci1.hseq, TYPE_lng, ci1.ncand, TRANSIENT))) {
    1109           0 :                 msg = createException(SQL, "sql.get_value", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1110           0 :                 goto bailout;
    1111             :         }
    1112             : 
    1113         116 :         off1 = scheb->hseqbase;
    1114         116 :         off2 = seqb->hseqbase;
    1115         116 :         schi = bat_iterator(scheb);
    1116         116 :         seqi = bat_iterator(seqb);
    1117         116 :         vals = Tloc(bn, 0);
    1118         162 :         for (BUN i = 0; i < ci1.ncand; i++) {
    1119          46 :                 oid p1 = canditer_next(&ci1) - off1, p2 = canditer_next(&ci2) - off2;
    1120          46 :                 const char *sname = BUNtvar(schi, p1);
    1121          46 :                 const char *seqname = BUNtvar(seqi, p2);
    1122             : 
    1123          92 :                 if (strNil(sname) || strNil(seqname)) {
    1124           0 :                         vals[i] = lng_nil;
    1125           0 :                         nils = true;
    1126             :                 } else {
    1127          46 :                         if (!(s = mvc_bind_schema(m, sname))) {
    1128           0 :                                 msg = createException(SQL, "sql.get_value", SQLSTATE(3F000) "Cannot find the schema %s", sname);
    1129           0 :                                 goto bailout1;
    1130             :                         }
    1131          46 :                         if (!(seq = find_sql_sequence(m->session->tr, s, seqname))) {
    1132           0 :                                 msg = createException(SQL, "sql.get_value", SQLSTATE(HY050) "Cannot find the sequence %s.%s", sname, seqname);
    1133           0 :                                 goto bailout1;
    1134             :                         }
    1135          46 :                         if (!seq_get_value(m->session->tr->store, seq, &(vals[i]))) {
    1136           0 :                                 msg = createException(SQL, "sql.get_value", SQLSTATE(HY050) "Cannot get the next sequence value %s.%s", sname, seqname);
    1137           0 :                                 goto bailout1;
    1138             :                         }
    1139             :                 }
    1140             :         }
    1141             : 
    1142         116 : bailout1:
    1143         116 :         bat_iterator_end(&schi);
    1144         116 :         bat_iterator_end(&seqi);
    1145         116 : bailout:
    1146         116 :         BBPreclaim(scheb);
    1147         116 :         BBPreclaim(sches);
    1148         116 :         BBPreclaim(seqb);
    1149         116 :         BBPreclaim(seqs);
    1150         116 :         if (bn && !msg) {
    1151         116 :                 BATsetcount(bn, ci1.ncand);
    1152         116 :                 bn->tnil = nils;
    1153         116 :                 bn->tnonil = !nils;
    1154         116 :                 bn->tkey = BATcount(bn) <= 1;
    1155         116 :                 bn->tsorted = BATcount(bn) <= 1;
    1156         116 :                 bn->trevsorted = BATcount(bn) <= 1;
    1157         116 :                 *res = bn->batCacheid;
    1158         116 :                 BBPkeepref(bn);
    1159           0 :         } else if (bn)
    1160           0 :                 BBPreclaim(bn);
    1161             :         return msg;
    1162             : }
    1163             : 
    1164             : str
    1165           0 : mvc_getVersion(lng *version, const int *clientid)
    1166             : {
    1167           0 :         mvc *m = NULL;
    1168           0 :         Client cntxt = MCgetClient(*clientid);
    1169           0 :         str msg;
    1170             : 
    1171           0 :         if ((msg = getSQLContext(cntxt, NULL, &m, NULL)) != NULL)
    1172             :                 return msg;
    1173           0 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    1174             :                 return msg;
    1175           0 :         *version = -1;
    1176           0 :         if (m->session->tr)
    1177           0 :                 *version = (lng)m->session->tr->ts;
    1178             :         return MAL_SUCCEED;
    1179             : }
    1180             : 
    1181             : /* str mvc_restart_seq(lng *res, str *sname, str *seqname, lng *start); */
    1182             : str
    1183           2 : mvc_restart_seq(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    1184             : {
    1185           2 :         mvc *m = NULL;
    1186           2 :         str msg;
    1187           2 :         sql_schema *s;
    1188           2 :         sql_sequence *seq;
    1189           2 :         lng *res = getArgReference_lng(stk, pci, 0);
    1190           2 :         const char *sname = *getArgReference_str(stk, pci, 1);
    1191           2 :         const char *seqname = *getArgReference_str(stk, pci, 2);
    1192           2 :         lng start = *getArgReference_lng(stk, pci, 3);
    1193             : 
    1194           2 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    1195             :                 return msg;
    1196           2 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    1197             :                 return msg;
    1198           2 :         if (!(s = mvc_bind_schema(m, sname)))
    1199           0 :                 throw(SQL, "sql.restart", SQLSTATE(3F000) "Cannot find the schema %s", sname);
    1200           2 :         if (!mvc_schema_privs(m, s))
    1201           0 :                 throw(SQL, "sql.restart", SQLSTATE(42000) "Access denied for %s to schema '%s'", get_string_global_var(m, "current_user"), s->base.name);
    1202           2 :         if (!(seq = find_sql_sequence(m->session->tr, s, seqname)))
    1203           0 :                 throw(SQL, "sql.restart", SQLSTATE(HY050) "Failed to fetch sequence %s.%s", sname, seqname);
    1204           2 :         if (is_lng_nil(start))
    1205           0 :                 throw(SQL, "sql.restart", SQLSTATE(HY050) "Cannot (re)start sequence %s.%s with NULL", sname, seqname);
    1206           2 :         if (start < seq->minvalue)
    1207           0 :                 throw(SQL, "sql.restart", SQLSTATE(HY050) "Cannot set sequence %s.%s start to a value less than the minimum ("LLFMT" < "LLFMT")", sname, seqname, start, seq->minvalue);
    1208           2 :         if (start > seq->maxvalue)
    1209           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);
    1210           2 :         switch (sql_trans_sequence_restart(m->session->tr, seq, start)) {
    1211           0 :                 case -1:
    1212           0 :                         throw(SQL,"sql.restart",SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1213           0 :                 case -2:
    1214             :                 case -3:
    1215           0 :                         throw(SQL,"sql.restart",SQLSTATE(42000) "RESTART SEQUENCE: transaction conflict detected");
    1216           0 :                 case -4:
    1217           0 :                         throw(SQL,"sql.restart",SQLSTATE(HY050) "Cannot (re)start sequence %s.%s", sname, seqname);
    1218           2 :                 default:
    1219           2 :                         *res = start;
    1220             :         }
    1221           2 :         return MAL_SUCCEED;
    1222             : }
    1223             : 
    1224             : BAT *
    1225           0 : mvc_bind_idxbat(mvc *m, const char *sname, const char *tname, const char *iname, int access)
    1226             : {
    1227           0 :         sql_trans *tr = m->session->tr;
    1228           0 :         BAT *b = NULL;
    1229           0 :         sql_schema *s = NULL;
    1230           0 :         sql_idx *i = NULL;
    1231             : 
    1232           0 :         s = mvc_bind_schema(m, sname);
    1233           0 :         if (s == NULL)
    1234             :                 return NULL;
    1235           0 :         i = mvc_bind_idx(m, s, iname);
    1236           0 :         if (i == NULL || !isTable(i->t))
    1237             :                 return NULL;
    1238             : 
    1239           0 :         (void) tname;
    1240           0 :         sqlstore *store = tr->store;
    1241           0 :         b = store->storage_api.bind_idx(tr, i, access);
    1242           0 :         return b;
    1243             : }
    1244             : 
    1245             : /* str mvc_bind_wrap(int *bid, str *sname, str *tname, str *cname, int *access); */
    1246             : str
    1247     1228950 : mvc_bind_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    1248             : {
    1249     1228950 :         int upd = (pci->argc == 7 || pci->argc == 9);
    1250     1228950 :         BAT *b = NULL;
    1251     1228950 :         bat *bid = getArgReference_bat(stk, pci, 0);
    1252     1228950 :         mvc *m = NULL;
    1253     1228950 :         str msg;
    1254     1228950 :         const char *sname       = *getArgReference_str(stk, pci, 2 + upd);
    1255     1228950 :         const char *tname       = *getArgReference_str(stk, pci, 3 + upd);
    1256     1228950 :         const char *cname       = *getArgReference_str(stk, pci, 4 + upd);
    1257     1228950 :         const int       access  = *getArgReference_int(stk, pci, 5 + upd);
    1258             : 
    1259     1228950 :         const bool partitioned_access = pci->argc == (8 + upd) && getArgType(mb, pci, 6 + upd) == TYPE_int;
    1260             : 
    1261             :         /* This doesn't work with quick access for now... */
    1262     1228950 :         assert(access != QUICK);
    1263     1228950 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    1264             :                 return msg;
    1265     1228704 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    1266             :                 return msg;
    1267     1228598 :         sqlstore *store = m->store;
    1268     1228598 :         sql_schema *s = mvc_bind_schema(m, sname);
    1269     1228542 :         sql_table *t = mvc_bind_table(m, s, tname);
    1270     1228550 :         if (t && !isTable(t))
    1271           0 :                 throw(SQL, "sql.bind", SQLSTATE(42000) "%s '%s' is not persistent",
    1272           0 :                           TABLE_TYPE_DESCRIPTION(t->type, t->properties), t->base.name);
    1273     1228550 :         sql_column *c = mvc_bind_column(m, t, cname);
    1274             : 
    1275     1228705 :         if (partitioned_access) {
    1276             :                 /* partitioned access */
    1277      879251 :                 int part_nr = *getArgReference_int(stk, pci, 6 + upd);
    1278      879251 :                 int nr_parts = *getArgReference_int(stk, pci, 7 + upd);
    1279      879251 :                 BUN cnt = store->storage_api.count_col(m->session->tr, c, 0), psz;
    1280      879460 :                 oid l, h;
    1281      879460 :                 psz = cnt ? (cnt / nr_parts) : 0;
    1282      879460 :                 l = part_nr * psz;
    1283      879460 :                 if (l > cnt)
    1284           0 :                         l = cnt;
    1285      879460 :                 h = (part_nr + 1 == nr_parts) ? cnt : ((part_nr + 1) * psz);
    1286      879460 :                 if (h > cnt)
    1287           0 :                         h = cnt;
    1288             : 
    1289      879460 :                 if (upd) {
    1290      213597 :                         BAT *ui = NULL, *uv = NULL;
    1291      213597 :                         if (store->storage_api.bind_updates(m->session->tr, c, &ui, &uv) == LOG_ERR)
    1292           0 :                                 throw(SQL,"sql.bind",SQLSTATE(HY005) "Cannot access the update columns");
    1293             : 
    1294      213593 :                         h--;
    1295      213593 :                         BAT* bn = BATselect(ui, NULL, &l, &h, true, true, false);
    1296      213533 :                         if(bn == NULL) {
    1297           0 :                                 BBPunfix(ui->batCacheid);
    1298           0 :                                 BBPunfix(uv->batCacheid);
    1299           0 :                                 throw(SQL, "sql.bind", GDK_EXCEPTION);
    1300             :                         }
    1301             : 
    1302      213533 :                         bat *uvl = getArgReference_bat(stk, pci, 1);
    1303             : 
    1304      213533 :                         if (BATcount(bn)) {
    1305           3 :                                 BAT *id;
    1306           3 :                                 BAT *vl;
    1307           3 :                                 if (ui == NULL || uv == NULL) {
    1308           0 :                                         bat_destroy(uv);
    1309           0 :                                         bat_destroy(ui);
    1310           0 :                                         BBPunfix(bn->batCacheid);
    1311           0 :                                         throw(SQL,"sql.bind",SQLSTATE(HY005) "Cannot access the insert column %s.%s.%s",
    1312             :                                                 sname, tname, cname);
    1313             :                                 }
    1314           3 :                                 assert(uv->batCount == ui->batCount);
    1315           3 :                                 id = BATproject(bn, ui);
    1316           3 :                                 vl = BATproject(bn, uv);
    1317           3 :                                 bat_destroy(ui);
    1318           3 :                                 bat_destroy(uv);
    1319           3 :                                 if (id == NULL || vl == NULL) {
    1320           0 :                                         BBPunfix(bn->batCacheid);
    1321           0 :                                         bat_destroy(id);
    1322           0 :                                         bat_destroy(vl);
    1323           0 :                                         throw(SQL, "sql.bind", GDK_EXCEPTION);
    1324             :                                 }
    1325           3 :                                 if ( BATcount(id) != BATcount(vl)){
    1326           0 :                                         BBPunfix(bn->batCacheid);
    1327           0 :                                         bat_destroy(id);
    1328           0 :                                         bat_destroy(vl);
    1329           0 :                                         throw(SQL, "sql.bind", SQLSTATE(0000) "Inconsistent BAT count");
    1330             :                                 }
    1331           3 :                                 BBPkeepref(id);
    1332           3 :                                 BBPkeepref(vl);
    1333           3 :                                 *bid = id->batCacheid;
    1334           3 :                                 *uvl = vl->batCacheid;
    1335             :                         } else {
    1336      213530 :                                 *bid = e_bat(TYPE_oid);
    1337      213567 :                                 *uvl = e_bat(c->type.type->localtype);
    1338      213583 :                                 if (*bid == BID_NIL || *uvl == BID_NIL) {
    1339           0 :                                         if (*bid)
    1340           0 :                                                 BBPunfix(*bid);
    1341           0 :                                         if (*uvl)
    1342           0 :                                                 BBPunfix(*uvl);
    1343           0 :                                         BBPunfix(b->batCacheid);
    1344           0 :                                         throw(SQL, "sql.bind", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1345             :                                 }
    1346             :                         }
    1347             :                 } else {
    1348      665863 :                         int coltype = getBatType(getArgType(mb, pci, 0));
    1349      665863 :                         b = store->storage_api.bind_col(m->session->tr, c, access);
    1350      665702 :                         if (b == NULL)
    1351           0 :                                 throw(SQL, "sql.bind", SQLSTATE(42000) "Cannot bind column %s.%s.%s", sname, tname, cname);
    1352             : 
    1353      665702 :                         if (b->ttype && b->ttype != coltype) {
    1354           0 :                                 BBPunfix(b->batCacheid);
    1355           0 :                                 throw(SQL,"sql.bind",SQLSTATE(42000) "Column type mismatch %s.%s.%s",sname,tname,cname);
    1356             :                         }
    1357             : 
    1358      665702 :                         BAT* bn = BATslice(b, l, h);
    1359      665708 :                         if(bn == NULL) {
    1360           0 :                                 BBPunfix(b->batCacheid);
    1361           0 :                                 throw(SQL, "sql.bind", GDK_EXCEPTION);
    1362             :                         }
    1363      665708 :                         BAThseqbase(bn, l);
    1364      665853 :                         BBPunfix(b->batCacheid);
    1365      665694 :                         BBPkeepref(bn);
    1366      665733 :                         *bid = bn->batCacheid;
    1367             :                 }
    1368             :         }
    1369      349454 :         else if (upd) { /*unpartitioned access to update bats*/
    1370      103635 :                 BAT *ui = NULL, *uv = NULL;
    1371      103635 :                 if (store->storage_api.bind_updates(m->session->tr, c, &ui, &uv) == LOG_ERR)
    1372           0 :                         throw(SQL,"sql.bind",SQLSTATE(HY005) "Cannot access the update columns");
    1373             : 
    1374      103636 :                 bat *uvl = getArgReference_bat(stk, pci, 1);
    1375      103636 :                 BBPkeepref(ui);
    1376      103635 :                 BBPkeepref(uv);
    1377      103634 :                 *bid = ui->batCacheid;
    1378      103634 :                 *uvl = uv->batCacheid;
    1379             :         }
    1380             :         else { /*unpartitioned access to base column*/
    1381      245819 :                 int coltype = getBatType(getArgType(mb, pci, 0));
    1382      245819 :                 b = store->storage_api.bind_col(m->session->tr, c, access);
    1383      245760 :                 if (b == NULL)
    1384           0 :                         throw(SQL, "sql.bin", "Couldn't bind column");
    1385             : 
    1386      245760 :                 if (b->ttype && b->ttype != coltype) {
    1387           0 :                         BBPunfix(b->batCacheid);
    1388           0 :                         throw(SQL,"sql.bind",SQLSTATE(42000) "Column type mismatch %s.%s.%s",sname,tname,cname);
    1389             :                 }
    1390      245760 :                 BBPkeepref(b);
    1391      245808 :                 *bid = b->batCacheid;
    1392             :         }
    1393             :         return MAL_SUCCEED;
    1394             : }
    1395             : 
    1396             : /* The output of this function are 7 columns:
    1397             :  *  - The sqlid of the column
    1398             :  *  - Number of values of the column.
    1399             :  *  - Number of segments, indication of the fragmentation
    1400             :  *  - Number of inserted rows during the current transaction.
    1401             :  *  - Number of updated rows during the current transaction.
    1402             :  *  - Number of deletes of the column's table.
    1403             :  *  - the number in the transaction chain (.i.e for each savepoint a new transaction is added in the chain)
    1404             :  */
    1405             : 
    1406             : static str
    1407          17 : 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)
    1408             : {
    1409          17 :         int level = 0;
    1410          17 :         sqlstore *store = m->session->tr->store;
    1411             : 
    1412          17 :         lng inserted = (lng) store->storage_api.count_col(m->session->tr, c, 1);
    1413          17 :         lng all = (lng) store->storage_api.count_col(m->session->tr, c, 0);
    1414          17 :         lng updates = (lng) store->storage_api.count_col(m->session->tr, c, 2);
    1415             : 
    1416          17 :         if (BUNappend(col1, &c->base.id, false) != GDK_SUCCEED) {
    1417           0 :                 return createException(SQL,"sql.delta", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1418             :         }
    1419          17 :         if (BUNappend(col2, &segments, false) != GDK_SUCCEED) {
    1420           0 :                 return createException(SQL,"sql.delta", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1421             :         }
    1422          17 :         if (BUNappend(col3, &all, false) != GDK_SUCCEED) {
    1423           0 :                 return createException(SQL,"sql.delta", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1424             :         }
    1425          17 :         if (BUNappend(col4, &inserted, false) != GDK_SUCCEED) {
    1426           0 :                 return createException(SQL,"sql.delta", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1427             :         }
    1428          17 :         if (BUNappend(col5, &updates, false) != GDK_SUCCEED) {
    1429           0 :                 return createException(SQL,"sql.delta", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1430             :         }
    1431          17 :         if (BUNappend(col6, &deletes, false) != GDK_SUCCEED) {
    1432           0 :                 return createException(SQL,"sql.delta", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1433             :         }
    1434             :         /* compute level using global transaction */
    1435          17 :         if (c) {
    1436          34 :                 for(sql_delta *d = ATOMIC_PTR_GET(&c->data); d; d = d->next)
    1437          17 :                         level++;
    1438             :         }
    1439          17 :         if (BUNappend(col7, &level, false) != GDK_SUCCEED) {
    1440           0 :                 return createException(SQL,"sql.delta", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1441             :         }
    1442             :         return MAL_SUCCEED;
    1443             : }
    1444             : 
    1445             : str
    1446          17 : mvc_delta_values(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    1447             : {
    1448          17 :         const char *sname = *getArgReference_str(stk, pci, 7),
    1449          17 :                            *tname = (pci->argc > 8) ? *getArgReference_str(stk, pci, 8) : NULL,
    1450          17 :                            *cname = (pci->argc > 9) ? *getArgReference_str(stk, pci, 9) : NULL;
    1451          17 :         mvc *m;
    1452          17 :         str msg = MAL_SUCCEED;
    1453          17 :         BAT *col1 = NULL, *col2 = NULL, *col3 = NULL, *col4 = NULL, *col5 = NULL, *col6 = NULL, *col7 = NULL;
    1454          17 :         bat *b1 = getArgReference_bat(stk, pci, 0),
    1455          17 :                 *b2 = getArgReference_bat(stk, pci, 1),
    1456          17 :                 *b3 = getArgReference_bat(stk, pci, 2),
    1457          17 :                 *b4 = getArgReference_bat(stk, pci, 3),
    1458          17 :                 *b5 = getArgReference_bat(stk, pci, 4),
    1459          17 :                 *b6 = getArgReference_bat(stk, pci, 5),
    1460          17 :                 *b7 = getArgReference_bat(stk, pci, 6);
    1461          17 :         sql_schema *s = NULL;
    1462          17 :         sql_table *t = NULL;
    1463          17 :         sql_column *c = NULL;
    1464          17 :         node *n;
    1465          17 :         BUN nrows = 0;
    1466          17 :         lng deletes, segments;
    1467             : 
    1468          17 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    1469             :                 return msg;
    1470             : 
    1471          17 :         sqlstore *store = m->store;
    1472          17 :         sql_trans *tr = m->session->tr;
    1473          17 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    1474           0 :                 goto cleanup;
    1475             : 
    1476          17 :         if (!(s = mvc_bind_schema(m, sname)))
    1477           0 :                 throw(SQL, "sql.delta", SQLSTATE(3F000) "No such schema '%s'", sname);
    1478             : 
    1479          17 :         if (tname) {
    1480          17 :                 if (!(t = mvc_bind_table(m, s, tname)))
    1481           0 :                         throw(SQL, "sql.delta", SQLSTATE(3F000) "No such table '%s' in schema '%s'", tname, s->base.name);
    1482          17 :                 if (!isTable(t))
    1483           0 :                         throw(SQL, "sql.delta", SQLSTATE(42000) "%s doesn't have delta values", TABLE_TYPE_DESCRIPTION(t->type, t->properties));
    1484          17 :                 if (cname) {
    1485           0 :                         if (!(c = mvc_bind_column(m, t, cname)))
    1486           0 :                                 throw(SQL, "sql.delta", SQLSTATE(3F000) "No such column '%s' in table '%s'", cname, t->base.name);
    1487             :                         nrows = 1;
    1488             :                 } else {
    1489          17 :                         nrows = (BUN) ol_length(t->columns);
    1490             :                 }
    1491           0 :         } else if (s->tables) {
    1492           0 :                 struct os_iter oi;
    1493           0 :                 os_iterator(&oi, s->tables, tr, NULL);
    1494           0 :                 for (sql_base *b = oi_next(&oi); b; b = oi_next(&oi)) {
    1495           0 :                         t = (sql_table *)b;
    1496           0 :                         if (isTable(t))
    1497           0 :                                 nrows += (BUN) ol_length(t->columns);
    1498             :                 }
    1499             :         }
    1500             : 
    1501          17 :         if ((col1 = COLnew(0, TYPE_int, nrows, TRANSIENT)) == NULL) {
    1502           0 :                 msg = createException(SQL, "sql.delta", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1503           0 :                 goto cleanup;
    1504             :         }
    1505          17 :         if ((col2 = COLnew(0, TYPE_lng, nrows, TRANSIENT)) == NULL) {
    1506           0 :                 msg = createException(SQL, "sql.delta", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1507           0 :                 goto cleanup;
    1508             :         }
    1509          17 :         if ((col3 = COLnew(0, TYPE_lng, nrows, TRANSIENT)) == NULL) {
    1510           0 :                 msg = createException(SQL, "sql.delta", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1511           0 :                 goto cleanup;
    1512             :         }
    1513          17 :         if ((col4 = COLnew(0, TYPE_lng, nrows, TRANSIENT)) == NULL) {
    1514           0 :                 msg = createException(SQL, "sql.delta", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1515           0 :                 goto cleanup;
    1516             :         }
    1517          17 :         if ((col5 = COLnew(0, TYPE_lng, nrows, TRANSIENT)) == NULL) {
    1518           0 :                 msg = createException(SQL, "sql.delta", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1519           0 :                 goto cleanup;
    1520             :         }
    1521          17 :         if ((col6 = COLnew(0, TYPE_lng, nrows, TRANSIENT)) == NULL) {
    1522           0 :                 msg = createException(SQL, "sql.delta", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1523           0 :                 goto cleanup;
    1524             :         }
    1525          17 :         if ((col7 = COLnew(0, TYPE_int, nrows, TRANSIENT)) == NULL) {
    1526           0 :                 msg = createException(SQL, "sql.delta", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1527           0 :                 goto cleanup;
    1528             :         }
    1529             : 
    1530          17 :         if (nrows) {
    1531          17 :                 if (tname) {
    1532          17 :                         deletes = (lng) store->storage_api.count_del(m->session->tr, t, 0);
    1533          17 :                         segments = (lng) store->storage_api.count_del(m->session->tr, t, 10);
    1534          17 :                         if (cname) {
    1535           0 :                                 if ((msg=mvc_insert_delta_values(m, col1, col2, col3, col4, col5, col6, col7, c, segments, deletes)) != NULL)
    1536           0 :                                         goto cleanup;
    1537             :                         } else {
    1538          34 :                                 for (n = ol_first_node(t->columns); n ; n = n->next) {
    1539          17 :                                         c = (sql_column*) n->data;
    1540          17 :                                         if ((msg=mvc_insert_delta_values(m, col1, col2, col3, col4, col5, col6, col7, c, segments, deletes)) != NULL)
    1541           0 :                                                 goto cleanup;
    1542             :                                 }
    1543             :                         }
    1544           0 :                 } else if (s->tables) {
    1545           0 :                         struct os_iter oi;
    1546           0 :                         os_iterator(&oi, s->tables, tr, NULL);
    1547           0 :                         for (sql_base *b = oi_next(&oi); b; b = oi_next(&oi)) {
    1548           0 :                                 t = (sql_table *)b;
    1549           0 :                                 if (isTable(t)) {
    1550           0 :                                         deletes = (lng) store->storage_api.count_del(m->session->tr, t, 0);
    1551           0 :                                         segments = (lng) store->storage_api.count_del(m->session->tr, t, 10);
    1552             : 
    1553           0 :                                         for (node *nn = ol_first_node(t->columns); nn ; nn = nn->next) {
    1554           0 :                                                 c = (sql_column*) nn->data;
    1555             : 
    1556           0 :                                                 if ((msg=mvc_insert_delta_values(m, col1, col2, col3, col4, col5, col6, col7, c, segments, deletes)) != NULL)
    1557           0 :                                                         goto cleanup;
    1558             :                                         }
    1559             :                                 }
    1560             :                         }
    1561             :                 }
    1562             :         }
    1563             : 
    1564           0 : cleanup:
    1565          17 :         if (msg) {
    1566           0 :                 if (col1)
    1567           0 :                         BBPreclaim(col1);
    1568           0 :                 if (col2)
    1569           0 :                         BBPreclaim(col2);
    1570           0 :                 if (col3)
    1571           0 :                         BBPreclaim(col3);
    1572           0 :                 if (col4)
    1573           0 :                         BBPreclaim(col4);
    1574           0 :                 if (col5)
    1575           0 :                         BBPreclaim(col5);
    1576           0 :                 if (col6)
    1577           0 :                         BBPreclaim(col6);
    1578           0 :                 if (col7)
    1579           0 :                         BBPreclaim(col7);
    1580             :         } else {
    1581          17 :                 *b1 = col1->batCacheid;
    1582          17 :                 BBPkeepref(col1);
    1583          17 :                 *b2 = col2->batCacheid;
    1584          17 :                 BBPkeepref(col2);
    1585          17 :                 *b3 = col3->batCacheid;
    1586          17 :                 BBPkeepref(col3);
    1587          17 :                 *b4 = col4->batCacheid;
    1588          17 :                 BBPkeepref(col4);
    1589          17 :                 *b5 = col5->batCacheid;
    1590          17 :                 BBPkeepref(col5);
    1591          17 :                 *b6 = col6->batCacheid;
    1592          17 :                 BBPkeepref(col6);
    1593          17 :                 *b7 = col7->batCacheid;
    1594          17 :                 BBPkeepref(col7);
    1595             :         }
    1596             :         return msg;
    1597             : }
    1598             : 
    1599             : /* str mvc_bind_idxbat_wrap(int *bid, str *sname, str *tname, str *iname, int *access); */
    1600             : str
    1601        2589 : mvc_bind_idxbat_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    1602             : {
    1603        2589 :         int upd = (pci->argc == 7 || pci->argc == 9);
    1604        2589 :         BAT *b = NULL;
    1605        2589 :         bat *bid = getArgReference_bat(stk, pci, 0);
    1606        2589 :         mvc *m = NULL;
    1607        2589 :         str msg;
    1608        2589 :         const char *sname       = *getArgReference_str(stk, pci, 2 + upd);
    1609        2589 :         const char *tname       = *getArgReference_str(stk, pci, 3 + upd);
    1610        2589 :         const char *iname       = *getArgReference_str(stk, pci, 4 + upd);
    1611        2589 :         const int       access  = *getArgReference_int(stk, pci, 5 + upd);
    1612             : 
    1613        2589 :         const bool partitioned_access = pci->argc == (8 + upd) && getArgType(mb, pci, 6 + upd) == TYPE_int;
    1614             : 
    1615             :         /* This doesn't work with quick access for now... */
    1616        2589 :         assert(access != QUICK);
    1617        2589 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    1618             :                 return msg;
    1619        2587 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    1620             :                 return msg;
    1621        2585 :         sqlstore *store = m->store;
    1622        2585 :         sql_schema *s = mvc_bind_schema(m, sname);
    1623        2589 :         sql_table *t = mvc_bind_table(m, s, tname);
    1624        2588 :         if (t && !isTable(t))
    1625           0 :                 throw(SQL, "sql.bindidx", SQLSTATE(42000) "%s '%s' is not persistent",
    1626           0 :                           TABLE_TYPE_DESCRIPTION(t->type, t->properties), t->base.name);
    1627        2588 :         sql_idx *i = mvc_bind_idx(m, s, iname);
    1628             : 
    1629        2588 :         if (partitioned_access) {
    1630             :                 /* partitioned access */
    1631        1458 :                 int part_nr = *getArgReference_int(stk, pci, 6 + upd);
    1632        1458 :                 int nr_parts = *getArgReference_int(stk, pci, 7 + upd);
    1633        1458 :                 BUN cnt = store->storage_api.count_idx(m->session->tr, i, 0), psz;
    1634        1459 :                 oid l, h;
    1635        1459 :                 psz = cnt ? (cnt / nr_parts) : 0;
    1636        1459 :                 l = part_nr * psz;
    1637        1459 :                 if (l > cnt)
    1638           0 :                         l = cnt;
    1639        1459 :                 h = (part_nr + 1 == nr_parts) ? cnt : ((part_nr + 1) * psz);
    1640        1459 :                 if (h > cnt)
    1641           0 :                         h = cnt;
    1642             : 
    1643        1459 :                 if (upd) {
    1644           0 :                         BAT *ui = NULL, *uv = NULL;
    1645           0 :                         if (store->storage_api.bind_updates_idx(m->session->tr, i, &ui, &uv) == LOG_ERR)
    1646           0 :                                 throw(SQL,"sql.bindidx",SQLSTATE(HY005) "Cannot access the update columns");
    1647             : 
    1648           0 :                         h--;
    1649           0 :                         BAT* bn = BATselect(ui, NULL, &l, &h, true, true, false);
    1650           0 :                         if(bn == NULL) {
    1651           0 :                                 BBPunfix(ui->batCacheid);
    1652           0 :                                 BBPunfix(uv->batCacheid);
    1653           0 :                                 throw(SQL, "sql.bindidx", GDK_EXCEPTION);
    1654             :                         }
    1655             : 
    1656           0 :                         bat *uvl = getArgReference_bat(stk, pci, 1);
    1657             : 
    1658           0 :                         if (BATcount(bn)) {
    1659           0 :                                 BAT *id;
    1660           0 :                                 BAT *vl;
    1661           0 :                                 if (ui == NULL || uv == NULL) {
    1662           0 :                                         bat_destroy(uv);
    1663           0 :                                         bat_destroy(ui);
    1664           0 :                                         BBPunfix(bn->batCacheid);
    1665           0 :                                         throw(SQL,"sql.bindidx",SQLSTATE(42000) "Cannot access index column %s.%s.%s",sname,tname,iname);
    1666             :                                 }
    1667           0 :                                 assert(uv->batCount == ui->batCount);
    1668           0 :                                 id = BATproject(bn, ui);
    1669           0 :                                 vl = BATproject(bn, uv);
    1670           0 :                                 bat_destroy(ui);
    1671           0 :                                 bat_destroy(uv);
    1672           0 :                                 if (id == NULL || vl == NULL) {
    1673           0 :                                         BBPunfix(bn->batCacheid);
    1674           0 :                                         bat_destroy(id);
    1675           0 :                                         bat_destroy(vl);
    1676           0 :                                         throw(SQL, "sql.bindidx", GDK_EXCEPTION);
    1677             :                                 }
    1678           0 :                                 if ( BATcount(id) != BATcount(vl)){
    1679           0 :                                         BBPunfix(bn->batCacheid);
    1680           0 :                                         bat_destroy(id);
    1681           0 :                                         bat_destroy(vl);
    1682           0 :                                         throw(SQL, "sql.bindidx", SQLSTATE(0000) "Inconsistent BAT count");
    1683             :                                 }
    1684           0 :                                 BBPkeepref(id);
    1685           0 :                                 BBPkeepref(vl);
    1686           0 :                                 *bid = id->batCacheid;
    1687           0 :                                 *uvl = vl->batCacheid;
    1688             :                         } else {
    1689           0 :                                 *bid = e_bat(TYPE_oid);
    1690           0 :                                 *uvl = e_bat((i->type==join_idx)?TYPE_oid:TYPE_lng);
    1691           0 :                                 if (*bid == BID_NIL || *uvl == BID_NIL) {
    1692           0 :                                         if (*bid)
    1693           0 :                                                 BBPunfix(*bid);
    1694           0 :                                         if (*uvl)
    1695           0 :                                                 BBPunfix(*uvl);
    1696           0 :                                         BBPunfix(b->batCacheid);
    1697           0 :                                         throw(SQL, "sql.bindidx", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1698             :                                 }
    1699             :                         }
    1700             :                 } else {
    1701        1459 :                         int idxtype = getBatType(getArgType(mb, pci, 0));
    1702        1459 :                         b = store->storage_api.bind_idx(m->session->tr, i, access);
    1703             : 
    1704        1459 :                         if (b && b->ttype && b->ttype != idxtype) {
    1705           0 :                                 BBPunfix(b->batCacheid);
    1706           0 :                                 throw(SQL,"sql.bindidx",SQLSTATE(42000) "Index type mismatch %s.%s.%s",sname,tname,iname);
    1707             :                         }
    1708             : 
    1709        1459 :                         BAT* bn = BATslice(b, l, h);
    1710        1459 :                         if(bn == NULL) {
    1711           0 :                                 BBPunfix(b->batCacheid);
    1712           0 :                                 throw(SQL, "sql.bindidx", GDK_EXCEPTION);
    1713             :                         }
    1714        1459 :                         BAThseqbase(bn, l);
    1715        1459 :                         BBPunfix(b->batCacheid);
    1716        1459 :                         BBPkeepref(bn);
    1717        1459 :                         *bid = bn->batCacheid;
    1718             :                 }
    1719             :         }
    1720        1130 :         else if (upd) { /*unpartitioned access to update bats*/
    1721          57 :                 BAT *ui = NULL, *uv = NULL;
    1722          57 :                 if (store->storage_api.bind_updates_idx(m->session->tr, i, &ui, &uv) == LOG_ERR)
    1723           0 :                         throw(SQL,"sql.bindidx",SQLSTATE(HY005) "Cannot access the update columns");
    1724             : 
    1725          57 :                 bat *uvl = getArgReference_bat(stk, pci, 1);
    1726          57 :                 BBPkeepref(ui);
    1727          57 :                 BBPkeepref(uv);
    1728          57 :                 *bid = ui->batCacheid;
    1729          57 :                 *uvl = uv->batCacheid;
    1730             :         }
    1731             :         else { /*unpartitioned access to base index*/
    1732        1073 :                 int idxtype = getBatType(getArgType(mb, pci, 0));
    1733        1073 :                 b = store->storage_api.bind_idx(m->session->tr, i, access);
    1734        1073 :                 if (b == NULL)
    1735           0 :                         throw(SQL,"sql.bindidx", "Couldn't bind index");
    1736             : 
    1737        1073 :                 if (b->ttype && b->ttype != idxtype) {
    1738           0 :                         BBPunfix(b->batCacheid);
    1739           0 :                         throw(SQL,"sql.bindidx",SQLSTATE(42000) "Index type mismatch %s.%s.%s",sname,tname,iname);
    1740             :                 }
    1741        1073 :                 BBPkeepref(b);
    1742        1073 :                 *bid = b->batCacheid;
    1743             :         }
    1744             :         return MAL_SUCCEED;
    1745             : }
    1746             : 
    1747             : str
    1748          52 : mvc_append_column(sql_trans *t, sql_column *c, BUN offset, BAT *pos, BAT *ins)
    1749             : {
    1750          52 :         sqlstore *store = t->store;
    1751          52 :         int res = store->storage_api.append_col(t, c, offset, pos, ins, BATcount(ins), TYPE_bat);
    1752          52 :         if (res != LOG_OK) /* the conflict case should never happen, but leave it here */
    1753           0 :                 throw(SQL, "sql.append", SQLSTATE(42000) "Append failed %s", res == LOG_CONFLICT ? "due to conflict with another transaction" : GDKerrbuf);
    1754             :         return MAL_SUCCEED;
    1755             : }
    1756             : 
    1757             : /*mvc_grow_wrap(int *bid, str *sname, str *tname, str *cname, ptr d) */
    1758             : str
    1759         341 : mvc_grow_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    1760             : {
    1761         341 :         int *res = getArgReference_int(stk, pci, 0);
    1762         341 :         bat Tid = *getArgReference_bat(stk, pci, 1);
    1763         341 :         ptr Ins = getArgReference(stk, pci, 2);
    1764         341 :         int tpe = getArgType(mb, pci, 2);
    1765         341 :         BAT *tid = 0, *ins = 0;
    1766         341 :         size_t cnt = 1;
    1767         341 :         oid v = 0;
    1768             : 
    1769         341 :         (void)cntxt;
    1770         341 :         *res = 0;
    1771         341 :         if ((tid = BATdescriptor(Tid)) == NULL)
    1772           0 :                 throw(SQL, "sql.grow", SQLSTATE(HY005) "Cannot access descriptor");
    1773         341 :         if (tpe > GDKatomcnt)
    1774             :                 tpe = TYPE_bat;
    1775         341 :         if (tpe == TYPE_bat && (ins = BATdescriptor(*(bat *) Ins)) == NULL) {
    1776           0 :                 BBPunfix(Tid);
    1777           0 :                 throw(SQL, "sql.grow", SQLSTATE(HY005) "Cannot access descriptor");
    1778             :         }
    1779           9 :         if (ins) {
    1780           9 :                 cnt = BATcount(ins);
    1781           9 :                 BBPunfix(ins->batCacheid);
    1782             :         }
    1783         341 :         if (BATcount(tid)) {
    1784         311 :                 (void)BATmax(tid, &v);
    1785         311 :                 v++;
    1786             :         }
    1787         691 :         for(;cnt>0; cnt--, v++) {
    1788         350 :                 if (BUNappend(tid, &v, false) != GDK_SUCCEED) {
    1789           0 :                         BBPunfix(Tid);
    1790           0 :                         throw(SQL, "sql.grow", GDK_EXCEPTION);
    1791             :                 }
    1792             :         }
    1793         341 :         BBPunfix(Tid);
    1794         341 :         return MAL_SUCCEED;
    1795             : }
    1796             : 
    1797             : /*mvc_append_wrap(int *bid, str *sname, str *tname, str *cname, ptr d) */
    1798             : str
    1799      626729 : mvc_append_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    1800             : {
    1801      626729 :         int *res = getArgReference_int(stk, pci, 0);
    1802      626729 :         mvc *m = NULL;
    1803      626729 :         str msg;
    1804      626729 :         const char *sname = *getArgReference_str(stk, pci, 2);
    1805      626729 :         const char *tname = *getArgReference_str(stk, pci, 3);
    1806      626729 :         const char *cname = *getArgReference_str(stk, pci, 4);
    1807      626729 :         BUN offset = (BUN)*getArgReference_oid(stk, pci, 5);
    1808      626729 :         bat Pos = *getArgReference_bat(stk, pci, 6);
    1809      626729 :         ptr ins = getArgReference(stk, pci, 7);
    1810      626874 :         int tpe = getArgType(mb, pci, 7), log_res = LOG_OK;
    1811      626874 :         sql_schema *s;
    1812      626874 :         sql_table *t;
    1813      626874 :         sql_column *c;
    1814      626874 :         sql_idx *i;
    1815      626874 :         BAT *b = NULL, *pos = NULL;
    1816      626874 :         BUN cnt = 1;
    1817             : 
    1818      626874 :         *res = 0;
    1819      626874 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    1820             :                 return msg;
    1821      626646 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    1822             :                 return msg;
    1823      626598 :         if (tpe > GDKatomcnt)
    1824      150545 :                 tpe = TYPE_bat;
    1825      626598 :         if (Pos != bat_nil && (pos = BATdescriptor(Pos)) == NULL)
    1826           0 :                 throw(SQL, "sql.append", SQLSTATE(HY005) "Cannot access append positions descriptor");
    1827      626598 :         if (tpe == TYPE_bat && (ins = BATdescriptor(*(bat *) ins)) == NULL) {
    1828           0 :                 bat_destroy(pos);
    1829           0 :                 throw(SQL, "sql.append", SQLSTATE(HY005) "Cannot access append values descriptor");
    1830             :         }
    1831      626614 :         if (ATOMextern(tpe) && !ATOMvarsized(tpe))
    1832          11 :                 ins = *(ptr *) ins;
    1833      626614 :         if ( tpe == TYPE_bat)
    1834      150557 :                 b =  (BAT*) ins;
    1835      626614 :         s = mvc_bind_schema(m, sname);
    1836      626528 :         if (s == NULL) {
    1837           0 :                 bat_destroy(pos);
    1838           0 :                 bat_destroy(b);
    1839           0 :                 throw(SQL, "sql.append", SQLSTATE(3F000) "Schema missing %s",sname);
    1840             :         }
    1841      626528 :         t = mvc_bind_table(m, s, tname);
    1842      626696 :         if (t == NULL) {
    1843           0 :                 bat_destroy(pos);
    1844           0 :                 bat_destroy(b);
    1845           0 :                 throw(SQL, "sql.append", SQLSTATE(42S02) "Table missing %s",tname);
    1846             :         }
    1847      626696 :         if (!isTable(t)) {
    1848           0 :                 bat_destroy(pos);
    1849           0 :                 bat_destroy(b);
    1850           0 :                 throw(SQL, "sql.append", SQLSTATE(42000) "%s '%s' is not persistent", TABLE_TYPE_DESCRIPTION(t->type, t->properties), t->base.name);
    1851             :         }
    1852      626696 :         if (b)
    1853      150618 :                 cnt = BATcount(b);
    1854      626696 :         sqlstore *store = m->session->tr->store;
    1855      626696 :         if (cname[0] != '%' && (c = mvc_bind_column(m, t, cname)) != NULL) {
    1856      624521 :                 log_res = store->storage_api.append_col(m->session->tr, c, offset, pos, ins, cnt, tpe);
    1857        2166 :         } else if (cname[0] == '%' && (i = mvc_bind_idx(m, s, cname + 1)) != NULL) {
    1858        2166 :                 log_res = store->storage_api.append_idx(m->session->tr, i, offset, pos, ins, cnt, tpe);
    1859             :         } else {
    1860           0 :                 bat_destroy(pos);
    1861           0 :                 bat_destroy(b);
    1862           0 :                 throw(SQL, "sql.append", SQLSTATE(38000) "Unable to find column or index %s.%s.%s",sname,tname,cname);
    1863             :         }
    1864      626667 :         bat_destroy(pos);
    1865      626655 :         bat_destroy(b);
    1866      626503 :         if (log_res != LOG_OK) /* the conflict case should never happen, but leave it here */
    1867           0 :                 throw(SQL, "sql.append", SQLSTATE(42000) "Append failed %s", log_res == LOG_CONFLICT ? "due to conflict with another transaction" : GDKerrbuf);
    1868             :         return MAL_SUCCEED;
    1869             : }
    1870             : 
    1871             : /*mvc_update_wrap(int *bid, str *sname, str *tname, str *cname, ptr d) */
    1872             : str
    1873        3827 : mvc_update_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    1874             : {
    1875        3827 :         int *res = getArgReference_int(stk, pci, 0);
    1876        3827 :         mvc *m = NULL;
    1877        3827 :         str msg;
    1878        3827 :         const char *sname = *getArgReference_str(stk, pci, 2);
    1879        3827 :         const char *tname = *getArgReference_str(stk, pci, 3);
    1880        3827 :         const char *cname = *getArgReference_str(stk, pci, 4);
    1881        3827 :         bat Tids = *getArgReference_bat(stk, pci, 5);
    1882        3827 :         bat Upd = *getArgReference_bat(stk, pci, 6);
    1883        3827 :         BAT *tids, *upd;
    1884        3827 :         int tpe = getArgType(mb, pci, 6), log_res = LOG_OK;
    1885        3827 :         sql_schema *s;
    1886        3827 :         sql_table *t;
    1887        3827 :         sql_column *c;
    1888        3827 :         sql_idx *i;
    1889             : 
    1890        3827 :         *res = 0;
    1891        3827 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    1892             :                 return msg;
    1893        3827 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    1894             :                 return msg;
    1895        3827 :         if (tpe > TYPE_any)
    1896        3827 :                 tpe = TYPE_bat;
    1897             :         else
    1898           0 :                 assert(0);
    1899        3827 :         if (tpe != TYPE_bat)
    1900             :                 throw(SQL, "sql.update", SQLSTATE(HY005) "Update values is not a BAT input");
    1901        3827 :         if ((tids = BATdescriptor(Tids)) == NULL)
    1902           0 :                 throw(SQL, "sql.update", SQLSTATE(HY005) "Cannot access update positions descriptor");
    1903        3827 :         if ((upd = BATdescriptor(Upd)) == NULL) {
    1904           0 :                 BBPunfix(tids->batCacheid);
    1905           0 :                 throw(SQL, "sql.update", SQLSTATE(HY005) "Cannot access update values descriptor");
    1906             :         }
    1907        3827 :         s = mvc_bind_schema(m, sname);
    1908        3827 :         if (s == NULL) {
    1909           0 :                 BBPunfix(tids->batCacheid);
    1910           0 :                 BBPunfix(upd->batCacheid);
    1911           0 :                 throw(SQL, "sql.update", SQLSTATE(3F000) "Schema missing %s",sname);
    1912             :         }
    1913        3827 :         t = mvc_bind_table(m, s, tname);
    1914        3827 :         if (t == NULL) {
    1915           0 :                 BBPunfix(tids->batCacheid);
    1916           0 :                 BBPunfix(upd->batCacheid);
    1917           0 :                 throw(SQL, "sql.update", SQLSTATE(42S02) "Table missing %s.%s",sname,tname);
    1918             :         }
    1919        3827 :         if (!isTable(t)) {
    1920           0 :                 BBPunfix(tids->batCacheid);
    1921           0 :                 BBPunfix(upd->batCacheid);
    1922           0 :                 throw(SQL, "sql.update", SQLSTATE(42000) "%s '%s' is not persistent", TABLE_TYPE_DESCRIPTION(t->type, t->properties), t->base.name);
    1923             :         }
    1924        3827 :         sqlstore *store = m->session->tr->store;
    1925        3827 :         if (cname[0] != '%' && (c = mvc_bind_column(m, t, cname)) != NULL) {
    1926        3050 :                 log_res = store->storage_api.update_col(m->session->tr, c, tids, upd, TYPE_bat);
    1927         777 :         } else if (cname[0] == '%' && (i = mvc_bind_idx(m, s, cname + 1)) != NULL) {
    1928         777 :                 log_res = store->storage_api.update_idx(m->session->tr, i, tids, upd, TYPE_bat);
    1929             :         } else {
    1930           0 :                 BBPunfix(tids->batCacheid);
    1931           0 :                 BBPunfix(upd->batCacheid);
    1932           0 :                 throw(SQL, "sql.update", SQLSTATE(38000) "Unable to find column or index %s.%s.%s",sname,tname,cname);
    1933             :         }
    1934        3827 :         BBPunfix(tids->batCacheid);
    1935        3827 :         BBPunfix(upd->batCacheid);
    1936        3827 :         if (log_res != LOG_OK)
    1937           1 :                 throw(SQL, "sql.update", SQLSTATE(42000) "Update failed%s", log_res == LOG_CONFLICT ? " due to conflict with another transaction" : "");
    1938             :         return MAL_SUCCEED;
    1939             : }
    1940             : 
    1941             : /* str mvc_clear_table_wrap(lng *res, str *sname, str *tname); */
    1942             : str
    1943       41811 : mvc_clear_table_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    1944             : {
    1945       41811 :         sql_schema *s;
    1946       41811 :         sql_table *t;
    1947       41811 :         mvc *m = NULL;
    1948       41811 :         str msg;
    1949       41811 :         BUN clear_res;
    1950       41811 :         lng *res = getArgReference_lng(stk, pci, 0);
    1951       41811 :         const char *sname = *getArgReference_str(stk, pci, 1);
    1952       41811 :         const char *tname = *getArgReference_str(stk, pci, 2);
    1953       41811 :         int restart_sequences = *getArgReference_int(stk, pci, 3);
    1954             : 
    1955       41811 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    1956             :                 return msg;
    1957       41811 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    1958             :                 return msg;
    1959       41811 :         s = mvc_bind_schema(m, sname);
    1960       41811 :         if (s == NULL)
    1961           0 :                 throw(SQL, "sql.clear_table", SQLSTATE(3F000) "Schema missing %s", sname);
    1962       41811 :         t = mvc_bind_table(m, s, tname);
    1963       41811 :         if (t == NULL)
    1964           0 :                 throw(SQL, "sql.clear_table", SQLSTATE(42S02) "Table missing %s.%s", sname,tname);
    1965       41811 :         if (!isTable(t))
    1966           0 :                 throw(SQL, "sql.clear_table", SQLSTATE(42000) "%s '%s' is not persistent", TABLE_TYPE_DESCRIPTION(t->type, t->properties), t->base.name);
    1967       41811 :         clear_res = mvc_clear_table(m, t);
    1968       41811 :         if (clear_res >= BUN_NONE - 1)
    1969       15801 :                 throw(SQL, "sql.clear_table", SQLSTATE(42000) "Table clear failed%s", clear_res == (BUN_NONE - 1) ? " due to conflict with another transaction" : "");
    1970       26010 :         if (restart_sequences) { /* restart the sequences if it's the case */
    1971           2 :                 sql_trans *tr = m->session->tr;
    1972           2 :                 const char *next_value_for = "next value for ";
    1973             : 
    1974           5 :                 for (node *n = ol_first_node(t->columns); n; n = n->next) {
    1975           3 :                         sql_column *col = n->data;
    1976             : 
    1977           3 :                         if (col->def && !strncmp(col->def, next_value_for, strlen(next_value_for))) {
    1978           2 :                                 sql_schema *seqs = NULL;
    1979           2 :                                 sql_sequence *seq = NULL;
    1980           2 :                                 char *schema = NULL, *seq_name = NULL;
    1981             : 
    1982           2 :                                 extract_schema_and_sequence_name(m->ta, col->def + strlen(next_value_for), &schema, &seq_name);
    1983           2 :                                 if (!schema || !seq_name || !(seqs = find_sql_schema(tr, schema)))
    1984           0 :                                         continue;
    1985             : 
    1986             :                                 /* TODO - At the moment the sequence may not be stored in the same schema as the table itself */
    1987           2 :                                 if ((seq = find_sql_sequence(tr, seqs, seq_name))) {
    1988           2 :                                         switch (sql_trans_sequence_restart(tr, seq, seq->start)) {
    1989           0 :                                                 case -1:
    1990           0 :                                                         throw(SQL, "sql.clear_table", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1991           0 :                                                 case -2:
    1992             :                                                 case -3:
    1993           0 :                                                         throw(SQL, "sql.clear_table", SQLSTATE(HY005) "RESTART SEQUENCE: transaction conflict detected");
    1994           0 :                                                 case -4:
    1995           0 :                                                         throw(SQL, "sql.clear_table", SQLSTATE(HY005) "Could not restart sequence %s.%s", seqs->base.name, seq_name);
    1996             :                                                 default:
    1997             :                                                         break;
    1998             :                                         }
    1999             :                                 }
    2000             :                         }
    2001             :                 }
    2002             :         }
    2003       26010 :         *res = (lng) clear_res;
    2004       26010 :         return MAL_SUCCEED;
    2005             : }
    2006             : 
    2007             : /*mvc_delete_wrap(int *d, str *sname, str *tname, ptr d) */
    2008             : str
    2009         336 : mvc_delete_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    2010             : {
    2011         336 :         int *res = getArgReference_int(stk, pci, 0);
    2012         336 :         mvc *m = NULL;
    2013         336 :         str msg;
    2014         336 :         const char *sname = *getArgReference_str(stk, pci, 2);
    2015         336 :         const char *tname = *getArgReference_str(stk, pci, 3);
    2016         336 :         ptr ins = getArgReference(stk, pci, 4);
    2017         336 :         int tpe = getArgType(mb, pci, 4), log_res;
    2018         336 :         BAT *b = NULL;
    2019         336 :         sql_schema *s;
    2020         336 :         sql_table *t;
    2021             : 
    2022         336 :         *res = 0;
    2023         336 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    2024             :                 return msg;
    2025         336 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    2026             :                 return msg;
    2027         336 :         if (tpe > TYPE_any)
    2028             :                 tpe = TYPE_bat;
    2029         336 :         if (tpe == TYPE_bat && (b = BATdescriptor(*(bat *) ins)) == NULL)
    2030           0 :                 throw(SQL, "sql.delete", SQLSTATE(HY005) "Cannot access column descriptor");
    2031         336 :         if (tpe != TYPE_bat || (b->ttype != TYPE_oid && b->ttype != TYPE_void && b->ttype != TYPE_msk)) {
    2032             :                 BBPreclaim(b);
    2033           0 :                 throw(SQL, "sql.delete", SQLSTATE(HY005) "Cannot access column descriptor");
    2034             :         }
    2035         336 :         s = mvc_bind_schema(m, sname);
    2036         336 :         if (s == NULL) {
    2037           0 :                 BBPreclaim(b);
    2038           0 :                 throw(SQL, "sql.delete", SQLSTATE(3F000) "Schema missing %s",sname);
    2039             :         }
    2040         336 :         t = mvc_bind_table(m, s, tname);
    2041         336 :         if (t == NULL) {
    2042           0 :                 BBPreclaim(b);
    2043           0 :                 throw(SQL, "sql.delete", SQLSTATE(42S02) "Table missing %s.%s",sname,tname);
    2044             :         }
    2045         336 :         if (!isTable(t)) {
    2046           0 :                 BBPreclaim(b);
    2047           0 :                 throw(SQL, "sql.delete", SQLSTATE(42000) "%s '%s' is not persistent", TABLE_TYPE_DESCRIPTION(t->type, t->properties), t->base.name);
    2048             :         }
    2049         336 :         sqlstore *store = m->session->tr->store;
    2050         336 :         log_res = store->storage_api.delete_tab(m->session->tr, t, b, tpe);
    2051         336 :         BBPreclaim(b);
    2052         336 :         if (log_res != LOG_OK)
    2053           1 :                 throw(SQL, "sql.delete", SQLSTATE(42000) "Delete failed%s", log_res == LOG_CONFLICT ? " due to conflict with another transaction" : "");
    2054             :         return MAL_SUCCEED;
    2055             : }
    2056             : 
    2057             : static inline BAT *
    2058          13 : setwritable(BAT *b)
    2059             : {
    2060          13 :         if (isVIEW(b)) {
    2061          12 :                 BAT *bn = COLcopy(b, b->ttype, true, TRANSIENT);
    2062          12 :                 BBPunfix(b->batCacheid);
    2063          12 :                 b = bn;
    2064             :         }
    2065          13 :         return b;
    2066             : }
    2067             : 
    2068             : str
    2069      134242 : DELTAbat(bat *result, const bat *col, const bat *uid, const bat *uval)
    2070             : {
    2071      134242 :         BAT *c, *u_id, *u_val, *res;
    2072             : 
    2073      134242 :         if ((u_id = BBPquickdesc(*uid)) == NULL)
    2074           0 :                 throw(MAL, "sql.delta", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
    2075             : 
    2076             :         /* no updates */
    2077      134097 :         if (BATcount(u_id) == 0) {
    2078      134071 :                 BBPretain(*result = *col);
    2079      134071 :                 return MAL_SUCCEED;
    2080             :         }
    2081             : 
    2082          26 :         c = BATdescriptor(*col);
    2083          26 :         if (c == NULL)
    2084           0 :                 throw(MAL, "sql.delta", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
    2085          26 :         if ((res = COLcopy(c, c->ttype, true, TRANSIENT)) == NULL) {
    2086           0 :                 BBPunfix(c->batCacheid);
    2087           0 :                 throw(MAL, "sql.delta", GDK_EXCEPTION);
    2088             :         }
    2089          26 :         BBPunfix(c->batCacheid);
    2090             : 
    2091          26 :         if ((u_val = BATdescriptor(*uval)) == NULL) {
    2092           0 :                 BBPunfix(res->batCacheid);
    2093           0 :                 throw(MAL, "sql.delta", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
    2094             :         }
    2095          26 :         if ((u_id = BATdescriptor(*uid)) == NULL) {
    2096           0 :                 BBPunfix(u_val->batCacheid);
    2097           0 :                 BBPunfix(res->batCacheid);
    2098           0 :                 throw(MAL, "sql.delta", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
    2099             :         }
    2100          26 :         assert(BATcount(u_id) == BATcount(u_val));
    2101          52 :         if (BATcount(u_id) &&
    2102          26 :             BATreplace(res, u_id, u_val, true) != GDK_SUCCEED) {
    2103           0 :                 BBPunfix(u_id->batCacheid);
    2104           0 :                 BBPunfix(u_val->batCacheid);
    2105           0 :                 BBPunfix(res->batCacheid);
    2106           0 :                 throw(MAL, "sql.delta", GDK_EXCEPTION);
    2107             :         }
    2108          26 :         BBPunfix(u_id->batCacheid);
    2109          26 :         BBPunfix(u_val->batCacheid);
    2110             : 
    2111          26 :         *result = res->batCacheid;
    2112          26 :         BBPkeepref(res);
    2113          26 :         return MAL_SUCCEED;
    2114             : }
    2115             : 
    2116             : str
    2117       50218 : DELTAsub(bat *result, const bat *col, const bat *cid, const bat *uid, const bat *uval)
    2118             : {
    2119       50218 :         BAT *c, *cminu = NULL, *u_id, *u_val, *u, *res;
    2120       50218 :         gdk_return ret;
    2121             : 
    2122       50218 :         if ((u_id = BBPquickdesc(*uid)) == NULL)
    2123           0 :                 throw(MAL, "sql.delta", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
    2124             : 
    2125             :         /* no updates */
    2126       50219 :         if (BATcount(u_id) == 0) {
    2127       50211 :                 BBPretain(*result = *col);
    2128       50211 :                 return MAL_SUCCEED;
    2129             :         }
    2130             : 
    2131           8 :         c = BATdescriptor(*col);
    2132           8 :         if (c == NULL)
    2133           0 :                 throw(MAL, "sql.delta", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
    2134           8 :         res = c;
    2135           8 :         if (BATcount(u_id)) {
    2136           8 :                 u_id = BATdescriptor(*uid);
    2137           8 :                 if (!u_id) {
    2138           0 :                         BBPunfix(c->batCacheid);
    2139           0 :                         throw(MAL, "sql.delta", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
    2140             :                 }
    2141           8 :                 cminu = BATdiff(c, u_id, NULL, NULL, false, false, BUN_NONE);
    2142           8 :                 if (!cminu) {
    2143           0 :                         BBPunfix(c->batCacheid);
    2144           0 :                         BBPunfix(u_id->batCacheid);
    2145           0 :                         throw(MAL, "sql.delta", GDK_EXCEPTION);
    2146             :                 }
    2147           8 :                 res = BATproject(cminu, c);
    2148           8 :                 BBPunfix(c->batCacheid);
    2149           8 :                 BBPunfix(cminu->batCacheid);
    2150           8 :                 cminu = NULL;
    2151           8 :                 if (!res) {
    2152           0 :                         BBPunfix(u_id->batCacheid);
    2153           0 :                         throw(MAL, "sql.delta", GDK_EXCEPTION);
    2154             :                 }
    2155           8 :                 c = res;
    2156             : 
    2157           8 :                 if ((u_val = BATdescriptor(*uval)) == NULL) {
    2158           0 :                         BBPunfix(c->batCacheid);
    2159           0 :                         BBPunfix(u_id->batCacheid);
    2160           0 :                         throw(MAL, "sql.delta", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
    2161             :                 }
    2162           8 :                 if (BATcount(u_val)) {
    2163           4 :                         u = BATproject(u_val, u_id);
    2164           4 :                         BBPunfix(u_val->batCacheid);
    2165           4 :                         BBPunfix(u_id->batCacheid);
    2166           4 :                         if (!u) {
    2167           0 :                                 BBPunfix(c->batCacheid);
    2168           0 :                                 throw(MAL, "sql.delta", GDK_EXCEPTION);
    2169             :                         }
    2170             : 
    2171             :                         /* check selected updated values against candidates */
    2172           4 :                         BAT *c_ids = BATdescriptor(*cid);
    2173             : 
    2174           4 :                         if (!c_ids) {
    2175           0 :                                 BBPunfix(c->batCacheid);
    2176           0 :                                 BBPunfix(u->batCacheid);
    2177           0 :                                 throw(MAL, "sql.delta", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
    2178             :                         }
    2179           4 :                         cminu = BATintersect(u, c_ids, NULL, NULL, false, false, BUN_NONE);
    2180           4 :                         BBPunfix(c_ids->batCacheid);
    2181           4 :                         if (cminu == NULL) {
    2182           0 :                                 BBPunfix(c->batCacheid);
    2183           0 :                                 BBPunfix(u->batCacheid);
    2184           0 :                                 throw(MAL, "sql.delta", GDK_EXCEPTION);
    2185             :                         }
    2186           4 :                         BAT *nres;
    2187           4 :                         if ((nres = COLcopy(res, res->ttype, true, TRANSIENT)) == NULL) {
    2188           0 :                                 BBPunfix(res->batCacheid);
    2189           0 :                                 BBPunfix(u->batCacheid);
    2190           0 :                                 BBPunfix(cminu->batCacheid);
    2191           0 :                                 throw(MAL, "sql.delta", GDK_EXCEPTION);
    2192             :                         }
    2193           4 :                         BBPunfix(res->batCacheid);
    2194           4 :                         res = nres;
    2195           4 :                         ret = BATappend(res, u, cminu, true);
    2196           4 :                         BBPunfix(u->batCacheid);
    2197           4 :                         BBPunfix(cminu->batCacheid);
    2198           4 :                         cminu = NULL;
    2199           4 :                         if (ret != GDK_SUCCEED) {
    2200           0 :                                 BBPunfix(res->batCacheid);
    2201           0 :                                 throw(MAL, "sql.delta", GDK_EXCEPTION);
    2202             :                         }
    2203             : 
    2204           4 :                         ret = BATsort(&u, NULL, NULL, res, NULL, NULL, false, false, false);
    2205           4 :                         BBPunfix(res->batCacheid);
    2206           4 :                         if (ret != GDK_SUCCEED) {
    2207           0 :                                 throw(MAL, "sql.delta", GDK_EXCEPTION);
    2208             :                         }
    2209           4 :                         res = u;
    2210             :                 } else {
    2211           4 :                         BBPunfix(u_val->batCacheid);
    2212           4 :                         BBPunfix(u_id->batCacheid);
    2213             :                 }
    2214             :         }
    2215             : 
    2216           8 :         BATkey(res, true);
    2217           8 :         *result = res->batCacheid;
    2218           8 :         BBPkeepref(res);
    2219           8 :         return MAL_SUCCEED;
    2220             : }
    2221             : 
    2222             : str
    2223      134796 : DELTAproject(bat *result, const bat *sub, const bat *col, const bat *uid, const bat *uval)
    2224             : {
    2225      134796 :         BAT *s, *c, *u_id, *u_val, *res, *tres;
    2226             : 
    2227      134796 :         if ((s = BATdescriptor(*sub)) == NULL)
    2228           0 :                 throw(MAL, "sql.delta", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
    2229             : 
    2230      134794 :         if ((c = BATdescriptor(*col)) == NULL) {
    2231           0 :                 BBPunfix(s->batCacheid);
    2232           0 :                 throw(MAL, "sql.delta", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
    2233             :         }
    2234             : 
    2235             :         /* projection(sub,col) */
    2236      134788 :         res = c;
    2237      134788 :         tres = BATproject(s, res);
    2238      134774 :         BBPunfix(res->batCacheid);
    2239             : 
    2240      134747 :         if (tres == NULL) {
    2241           0 :                 BBPunfix(s->batCacheid);
    2242           0 :                 throw(MAL, "sql.projectdelta", GDK_EXCEPTION);
    2243             :         }
    2244      134747 :         res = tres;
    2245             : 
    2246      134747 :         if ((u_id = BATdescriptor(*uid)) == NULL) {
    2247           0 :                 BBPunfix(res->batCacheid);
    2248           0 :                 BBPunfix(s->batCacheid);
    2249           0 :                 throw(MAL, "sql.delta", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
    2250             :         }
    2251      134794 :         if (!BATcount(u_id)) {
    2252      134779 :                 BBPunfix(u_id->batCacheid);
    2253      134771 :                 BBPunfix(s->batCacheid);
    2254      134784 :                 *result = res->batCacheid;
    2255      134784 :                 BBPkeepref(res);
    2256      134784 :                 return MAL_SUCCEED;
    2257             :         }
    2258          15 :         if ((u_val = BATdescriptor(*uval)) == NULL) {
    2259           0 :                 BBPunfix(u_id->batCacheid);
    2260           0 :                 BBPunfix(res->batCacheid);
    2261           0 :                 BBPunfix(s->batCacheid);
    2262           0 :                 throw(MAL, "sql.delta", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
    2263             :         }
    2264             : 
    2265          15 :         if (BATcount(u_val)) {
    2266          15 :                 BAT *os, *ou;
    2267             :                 /* figure out the positions in res that we have to
    2268             :                  * replace with values from u_val */
    2269          15 :                 if (BATsemijoin(&ou, &os, u_id, s, NULL, NULL, false, false, BUN_NONE) != GDK_SUCCEED) {
    2270           0 :                         BBPunfix(s->batCacheid);
    2271           0 :                         BBPunfix(res->batCacheid);
    2272           0 :                         BBPunfix(u_id->batCacheid);
    2273           0 :                         BBPunfix(u_val->batCacheid);
    2274           0 :                         throw(MAL, "sql.delta", GDK_EXCEPTION);
    2275             :                 }
    2276             :                 /* BATcount(ou) == BATcount(os) */
    2277          15 :                 if (BATcount(ou) != 0) {
    2278             :                         /* ou contains the position in u_id/u_val that
    2279             :                          * contain the new values */
    2280          13 :                         BAT *nu_val = BATproject(ou, u_val);
    2281          13 :                         BBPunfix(ou->batCacheid);
    2282             :                         /* os contains the corresponding positions in
    2283             :                          * res that need to be replaced with those new
    2284             :                          * values */
    2285          26 :                         if (!nu_val || (res = setwritable(res)) == NULL ||
    2286          13 :                             BATreplace(res, os, nu_val, false) != GDK_SUCCEED) {
    2287           0 :                                 BBPreclaim(res);
    2288           0 :                                 BBPunfix(os->batCacheid);
    2289           0 :                                 BBPunfix(s->batCacheid);
    2290           0 :                                 BBPunfix(u_id->batCacheid);
    2291           0 :                                 BBPunfix(u_val->batCacheid);
    2292           0 :                                 BBPreclaim(nu_val);
    2293           0 :                                 throw(MAL, "sql.delta", GDK_EXCEPTION);
    2294             :                         }
    2295          13 :                         BBPunfix(nu_val->batCacheid);
    2296             :                 } else {
    2297             :                         /* nothing to replace */
    2298           2 :                         BBPunfix(ou->batCacheid);
    2299             :                 }
    2300          15 :                 BBPunfix(os->batCacheid);
    2301             :         }
    2302          15 :         BBPunfix(s->batCacheid);
    2303          15 :         BBPunfix(u_id->batCacheid);
    2304          15 :         BBPunfix(u_val->batCacheid);
    2305             : 
    2306          15 :         *result = res->batCacheid;
    2307          15 :         BBPkeepref(res);
    2308          15 :         return MAL_SUCCEED;
    2309             : }
    2310             : 
    2311             : str
    2312          26 : BATleftproject(bat *Res, const bat *Col, const bat *L, const bat *R)
    2313             : {
    2314          26 :         BAT *c, *l, *r, *res;
    2315          26 :         oid *p, *lp, *rp;
    2316          26 :         BUN cnt = 0, i;
    2317          26 :         BATiter li, ri;
    2318             : 
    2319          26 :         c = BATdescriptor(*Col);
    2320          26 :         if (c)
    2321          26 :                 cnt = BATcount(c);
    2322          26 :         l = BATdescriptor(*L);
    2323          26 :         r = BATdescriptor(*R);
    2324          26 :         res = COLnew(0, TYPE_oid, cnt, TRANSIENT);
    2325          26 :         if (!c || !l || !r || !res) {
    2326           0 :                 BBPreclaim(c);
    2327           0 :                 BBPreclaim(l);
    2328           0 :                 BBPreclaim(r);
    2329           0 :                 BBPreclaim(res);
    2330           0 :                 throw(MAL, "sql.delta", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
    2331             :         }
    2332          26 :         p = (oid*)Tloc(res,0);
    2333          51 :         for(i=0;i<cnt; i++)
    2334          25 :                 *p++ = oid_nil;
    2335          26 :         BATsetcount(res, cnt);
    2336             : 
    2337          26 :         cnt = BATcount(l);
    2338          26 :         p = (oid*)Tloc(res, 0);
    2339          26 :         li = bat_iterator(l);
    2340          26 :         ri = bat_iterator(r);
    2341          26 :         lp = (oid*)li.base;
    2342          26 :         rp = (oid*)ri.base;
    2343          26 :         if (l->ttype == TYPE_void) {
    2344          11 :                 oid lp = l->tseqbase;
    2345          11 :                 if (r->ttype == TYPE_void) {
    2346          11 :                         oid rp = r->tseqbase;
    2347          11 :                         for(i=0;i<cnt; i++, lp++, rp++)
    2348           0 :                                 p[lp] = rp;
    2349             :                 } else {
    2350           0 :                         for(i=0;i<cnt; i++, lp++)
    2351           0 :                                 p[lp] = rp[i];
    2352             :                 }
    2353             :         }
    2354          26 :         if (r->ttype == TYPE_void) {
    2355          23 :                 oid rp = r->tseqbase;
    2356          35 :                 for(i=0;i<cnt; i++, rp++)
    2357          12 :                         p[lp[i]] = rp;
    2358             :         } else {
    2359           9 :                 for(i=0;i<cnt; i++)
    2360           6 :                         p[lp[i]] = rp[i];
    2361             :         }
    2362          26 :         bat_iterator_end(&li);
    2363          26 :         bat_iterator_end(&ri);
    2364          26 :         res->tsorted = false;
    2365          26 :         res->trevsorted = false;
    2366          26 :         res->tnil = false;
    2367          26 :         res->tnonil = false;
    2368          26 :         res->tkey = false;
    2369          26 :         BBPunfix(c->batCacheid);
    2370          26 :         BBPunfix(l->batCacheid);
    2371          26 :         BBPunfix(r->batCacheid);
    2372          26 :         *Res = res->batCacheid;
    2373          26 :         BBPkeepref(res);
    2374          26 :         return MAL_SUCCEED;
    2375             : }
    2376             : 
    2377             : /* str SQLtid(bat *result, mvc *m, str *sname, str *tname) */
    2378             : str
    2379      207340 : SQLtid(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    2380             : {
    2381      207340 :         bat *res = getArgReference_bat(stk, pci, 0);
    2382      207340 :         mvc *m = NULL;
    2383      207340 :         str msg = MAL_SUCCEED;
    2384      207340 :         sql_trans *tr;
    2385      207340 :         const char *sname = *getArgReference_str(stk, pci, 2);
    2386      207340 :         const char *tname = *getArgReference_str(stk, pci, 3);
    2387      207340 :         sql_schema *s;
    2388      207340 :         sql_table *t;
    2389             : 
    2390      207340 :         *res = bat_nil;
    2391      207340 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    2392             :                 return msg;
    2393      207340 :         tr = m->session->tr;
    2394      207340 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    2395             :                 return msg;
    2396      207339 :         s = mvc_bind_schema(m, sname);
    2397      207339 :         if (s == NULL)
    2398           0 :                 throw(SQL, "sql.tid", SQLSTATE(3F000) "Schema missing %s",sname);
    2399      207339 :         t = mvc_bind_table(m, s, tname);
    2400      207336 :         if (t == NULL)
    2401           0 :                 throw(SQL, "sql.tid", SQLSTATE(42S02) "Table missing %s.%s",sname,tname);
    2402      207336 :         if (!isTable(t))
    2403           0 :                 throw(SQL, "sql.tid", SQLSTATE(42000) "%s '%s' is not persistent",
    2404           0 :                           TABLE_TYPE_DESCRIPTION(t->type, t->properties), t->base.name);
    2405             : 
    2406      207336 :         sqlstore *store = m->store;
    2407             :         /* we have full table count, nr of deleted (unused rows) */
    2408      207336 :         int part_nr = 0;
    2409      207336 :         int nr_parts = 1;
    2410      207336 :         if (pci->argc == 6) {        /* partitioned version */
    2411       94685 :                 part_nr = *getArgReference_int(stk, pci, 4);
    2412       94685 :                 nr_parts = *getArgReference_int(stk, pci, 5);
    2413             :         }
    2414      207336 :         BAT *b = store->storage_api.bind_cands(tr, t, nr_parts, part_nr);
    2415      207339 :         if (b) {
    2416      207339 :                 *res = b->batCacheid;
    2417      207339 :                 BBPkeepref(b);
    2418             :         } else {
    2419           0 :                 msg = createException(SQL, "sql.tid", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    2420             :         }
    2421             :         return msg;
    2422             : }
    2423             : 
    2424             : /* unsafe pattern resultSet(tbl:bat[:str], attr:bat[:str], tpe:bat[:str], len:bat[:int],scale:bat[:int], cols:bat[:any]...) :int */
    2425             : /* New result set rendering infrastructure */
    2426             : 
    2427             : static str
    2428       60180 : mvc_result_set_wrap( Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    2429             : {
    2430       60180 :         int *res_id =getArgReference_int(stk,pci,0);
    2431       60180 :         bat tblId= *getArgReference_bat(stk, pci,1);
    2432       60180 :         bat atrId= *getArgReference_bat(stk, pci,2);
    2433       60180 :         bat tpeId= *getArgReference_bat(stk, pci,3);
    2434       60180 :         bat lenId= *getArgReference_bat(stk, pci,4);
    2435       60180 :         bat scaleId= *getArgReference_bat(stk, pci,5);
    2436       60180 :         bat bid;
    2437       60180 :         int i, res, ok;
    2438       60180 :         const char *tblname, *colname, *tpename;
    2439       60180 :         str msg= MAL_SUCCEED;
    2440       60180 :         int *digits, *scaledigits;
    2441       60180 :         oid o = 0;
    2442       60180 :         BATiter itertbl,iteratr,itertpe,iterdig,iterscl;
    2443       60180 :         backend *be = NULL;
    2444       60180 :         BAT *b = NULL, *tbl = NULL, *atr = NULL, *tpe = NULL,*len = NULL,*scale = NULL;
    2445             : 
    2446       60180 :         if ((msg = getBackendContext(cntxt, &be)) != NULL)
    2447             :                 return msg;
    2448       60180 :         bid = *getArgReference_bat(stk,pci,6);
    2449       60180 :         b = BATdescriptor(bid);
    2450       60180 :         if ( b == NULL) {
    2451           0 :                 msg = createException(SQL, "sql.resultSet", SQLSTATE(HY005) "Cannot access column descriptor");
    2452           0 :                 goto wrapup_result_set;
    2453             :         }
    2454       60180 :         res = *res_id = mvc_result_table(be, mb->tag, pci->argc - (pci->retc + 5), Q_TABLE);
    2455       60180 :         BBPunfix(b->batCacheid);
    2456       60180 :         if (res < 0) {
    2457           0 :                 msg = createException(SQL, "sql.resultSet", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    2458           0 :                 goto wrapup_result_set;
    2459             :         }
    2460             : 
    2461       60180 :         tbl = BATdescriptor(tblId);
    2462       60180 :         atr = BATdescriptor(atrId);
    2463       60180 :         tpe = BATdescriptor(tpeId);
    2464       60179 :         len = BATdescriptor(lenId);
    2465       60180 :         scale = BATdescriptor(scaleId);
    2466       60180 :         if (tbl == NULL || atr == NULL || tpe == NULL || len == NULL || scale == NULL)
    2467           0 :                 goto wrapup_result_set;
    2468             :         /* mimick the old rsColumn approach; */
    2469       60180 :         itertbl = bat_iterator(tbl);
    2470       60179 :         iteratr = bat_iterator(atr);
    2471       60180 :         itertpe = bat_iterator(tpe);
    2472       60180 :         iterdig = bat_iterator(len);
    2473       60180 :         iterscl = bat_iterator(scale);
    2474       60180 :         digits = (int*) iterdig.base;
    2475       60180 :         scaledigits = (int*) iterscl.base;
    2476             : 
    2477      312934 :         for( i = 6; msg == MAL_SUCCEED && i< pci->argc; i++, o++){
    2478      252755 :                 bid = *getArgReference_bat(stk,pci,i);
    2479      252755 :                 tblname = BUNtvar(itertbl,o);
    2480      252755 :                 colname = BUNtvar(iteratr,o);
    2481      252753 :                 tpename = BUNtvar(itertpe,o);
    2482      252752 :                 b = BATdescriptor(bid);
    2483      252762 :                 if ( b == NULL)
    2484           0 :                         msg = createException(SQL, "sql.resultSet", SQLSTATE(HY005) "Cannot access column descriptor");
    2485      252762 :                 else if (mvc_result_column(be, tblname, colname, tpename, *digits++, *scaledigits++, b))
    2486           0 :                         msg = createException(SQL, "sql.resultSet", SQLSTATE(42000) "Cannot access column descriptor %s.%s",tblname,colname);
    2487      252769 :                 if( b)
    2488      252769 :                         BBPunfix(bid);
    2489             :         }
    2490       60179 :         bat_iterator_end(&itertbl);
    2491       60180 :         bat_iterator_end(&iteratr);
    2492       60179 :         bat_iterator_end(&itertpe);
    2493       60180 :         bat_iterator_end(&iterdig);
    2494       60180 :         bat_iterator_end(&iterscl);
    2495             :         /* now send it to the channel cntxt->fdout */
    2496       60180 :         if (!msg && (ok = mvc_export_result(cntxt->sqlcontext, cntxt->fdout, res, true, cntxt->qryctx.starttime, mb->optimize)) < 0)
    2497           0 :                 msg = createException(SQL, "sql.resultSet", SQLSTATE(45000) "Result set construction failed: %s", mvc_export_error(cntxt->sqlcontext, cntxt->fdout, ok));
    2498       60180 :   wrapup_result_set:
    2499       60180 :         cntxt->qryctx.starttime = 0;
    2500       60180 :         mb->optimize = 0;
    2501       60180 :         if( tbl) BBPunfix(tblId);
    2502       60179 :         if( atr) BBPunfix(atrId);
    2503       60180 :         if( tpe) BBPunfix(tpeId);
    2504       60180 :         if( len) BBPunfix(lenId);
    2505       60180 :         if( scale) BBPunfix(scaleId);
    2506             :         return msg;
    2507             : }
    2508             : 
    2509             : /* Copy the result set into a CSV file */
    2510             : str
    2511          31 : mvc_export_table_wrap( Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    2512             : {
    2513          31 :         int *res_id =getArgReference_int(stk,pci,0);
    2514          31 :         const char *filename = *getArgReference_str(stk,pci,1);
    2515          31 :         const char *format = *getArgReference_str(stk,pci,2);
    2516          31 :         const char *tsep = *getArgReference_str(stk, pci, 3);
    2517          31 :         const char *rsep = *getArgReference_str(stk, pci, 4);
    2518          31 :         const char *ssep = *getArgReference_str(stk, pci, 5);
    2519          31 :         const char *ns = *getArgReference_str(stk, pci, 6);
    2520          31 :         int onclient = *getArgReference_int(stk, pci, 7);
    2521             : 
    2522          31 :         bat tblId= *getArgReference_bat(stk, pci,8);
    2523          31 :         bat atrId= *getArgReference_bat(stk, pci,9);
    2524          31 :         bat tpeId= *getArgReference_bat(stk, pci,10);
    2525          31 :         bat lenId= *getArgReference_bat(stk, pci,11);
    2526          31 :         bat scaleId= *getArgReference_bat(stk, pci,12);
    2527          31 :         stream *s = NULL;
    2528          31 :         bat bid;
    2529          31 :         int i, res, ok;
    2530          31 :         const char *tblname, *colname, *tpename;
    2531          31 :         str msg= MAL_SUCCEED;
    2532          31 :         int *digits, *scaledigits;
    2533          31 :         oid o = 0;
    2534          31 :         BATiter itertbl,iteratr,itertpe,iterdig,iterscl;
    2535          31 :         backend *be;
    2536          31 :         mvc *m = NULL;
    2537          31 :         BAT *b = NULL, *tbl = NULL, *atr = NULL, *tpe = NULL,*len = NULL,*scale = NULL;
    2538          31 :         res_table *t = NULL;
    2539          31 :         bool tostdout;
    2540          31 :         char buf[80];
    2541          31 :         ssize_t sz;
    2542             : 
    2543          31 :         (void) format;
    2544             : 
    2545          31 :         if ((msg = getBackendContext(cntxt, &be)) != NULL)
    2546             :                 return msg;
    2547          31 :         m = be->mvc;
    2548             : 
    2549          31 :         if (onclient && !cntxt->filetrans) {
    2550           0 :                 msg = createException(SQL, "sql.resultSet", SQLSTATE(42000) "Cannot transfer files to client");
    2551           0 :                 goto wrapup_result_set1;
    2552             :         }
    2553             : 
    2554          31 :         bid = *getArgReference_bat(stk,pci,13);
    2555          31 :         res = *res_id = mvc_result_table(be, mb->tag, pci->argc - (pci->retc + 12), Q_TABLE);
    2556          31 :         t = be->results;
    2557          31 :         if (res < 0) {
    2558           0 :                 msg = createException(SQL, "sql.resultSet", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    2559           0 :                 goto wrapup_result_set1;
    2560             :         }
    2561             : 
    2562          31 :         t->tsep = tsep;
    2563          31 :         t->rsep = rsep;
    2564          31 :         t->ssep = ssep;
    2565          31 :         t->ns = ns;
    2566             : 
    2567          31 :         tbl = BATdescriptor(tblId);
    2568          31 :         atr = BATdescriptor(atrId);
    2569          31 :         tpe = BATdescriptor(tpeId);
    2570          31 :         len = BATdescriptor(lenId);
    2571          31 :         scale = BATdescriptor(scaleId);
    2572          31 :         if( tbl == NULL || atr == NULL || tpe == NULL || len == NULL || scale == NULL)
    2573           0 :                 goto wrapup_result_set1;
    2574             :         /* mimick the old rsColumn approach; */
    2575          31 :         itertbl = bat_iterator(tbl);
    2576          31 :         iteratr = bat_iterator(atr);
    2577          31 :         itertpe = bat_iterator(tpe);
    2578          31 :         iterdig = bat_iterator(len);
    2579          31 :         iterscl = bat_iterator(scale);
    2580          31 :         digits = (int*) iterdig.base;
    2581          31 :         scaledigits = (int*) iterscl.base;
    2582             : 
    2583         170 :         for( i = 13; msg == MAL_SUCCEED && i< pci->argc; i++, o++){
    2584         139 :                 bid = *getArgReference_bat(stk,pci,i);
    2585         139 :                 tblname = BUNtvar(itertbl,o);
    2586         139 :                 colname = BUNtvar(iteratr,o);
    2587         139 :                 tpename = BUNtvar(itertpe,o);
    2588         139 :                 b = BATdescriptor(bid);
    2589         139 :                 if ( b == NULL)
    2590           0 :                         msg = createException(SQL, "sql.resultSet", SQLSTATE(HY005) "Cannot access column descriptor");
    2591         139 :                 else if (mvc_result_column(be, tblname, colname, tpename, *digits++, *scaledigits++, b))
    2592           0 :                         msg = createException(SQL, "sql.resultSet", SQLSTATE(42000) "Cannot access column descriptor %s.%s",tblname,colname);
    2593         139 :                 if( b)
    2594         139 :                         BBPunfix(bid);
    2595             :         }
    2596          31 :         bat_iterator_end(&itertbl);
    2597          31 :         bat_iterator_end(&iteratr);
    2598          31 :         bat_iterator_end(&itertpe);
    2599          31 :         bat_iterator_end(&iterdig);
    2600          31 :         bat_iterator_end(&iterscl);
    2601          31 :         if ( msg )
    2602           0 :                 goto wrapup_result_set1;
    2603             : 
    2604             :         /* now select the file channel */
    2605          31 :         if ((tostdout = strcmp(filename,"stdout") == 0)) {
    2606           8 :                 s = cntxt->fdout;
    2607          23 :         } else if (!onclient) {
    2608           4 :                 if ((s = open_wastream(filename)) == NULL || mnstr_errnr(s) != MNSTR_NO__ERROR) {
    2609           0 :                         msg=  createException(IO, "streams.open", SQLSTATE(42000) "%s", mnstr_peek_error(NULL));
    2610           0 :                         close_stream(s);
    2611           0 :                         goto wrapup_result_set1;
    2612             :                 }
    2613           4 :                 be->output_format = OFMT_CSV;
    2614             :         } else {
    2615          19 :                 while (!m->scanner.rs->eof)
    2616           0 :                         bstream_next(m->scanner.rs);
    2617          19 :                 s = m->scanner.ws;
    2618          19 :                 mnstr_write(s, PROMPT3, sizeof(PROMPT3) - 1, 1);
    2619          19 :                 mnstr_printf(s, "w %s\n", filename);
    2620          19 :                 mnstr_flush(s, MNSTR_FLUSH_DATA);
    2621          19 :                 if ((sz = mnstr_readline(m->scanner.rs->s, buf, sizeof(buf))) > 1) {
    2622             :                         /* non-empty line indicates failure on client */
    2623           7 :                         msg = createException(IO, "streams.open", "%s", buf);
    2624             :                         /* discard until client flushes */
    2625          15 :                         while (mnstr_read(m->scanner.rs->s, buf, 1, sizeof(buf)) > 0) {
    2626             :                                 /* ignore remainder of error message */
    2627           8 :                         }
    2628           7 :                         goto wrapup_result_set1;
    2629             :                 }
    2630             :         }
    2631          24 :         if ((ok = mvc_export_result(cntxt->sqlcontext, s, res, tostdout, cntxt->qryctx.starttime, mb->optimize)) < 0) {
    2632           0 :                 msg = createException(SQL, "sql.resultSet", SQLSTATE(45000) "Result set construction failed: %s", mvc_export_error(cntxt->sqlcontext, s, ok));
    2633           0 :                 if (!onclient && !tostdout)
    2634           0 :                         close_stream(s);
    2635           0 :                 goto wrapup_result_set1;
    2636             :         }
    2637          24 :         if (onclient) {
    2638          12 :                 mnstr_flush(s, MNSTR_FLUSH_DATA);
    2639          12 :                 if ((sz = mnstr_readline(m->scanner.rs->s, buf, sizeof(buf))) > 1) {
    2640           0 :                         msg = createException(IO, "streams.open", "%s", buf);
    2641             :                 }
    2642          24 :                 while (sz > 0)
    2643          12 :                         sz = mnstr_readline(m->scanner.rs->s, buf, sizeof(buf));
    2644          12 :         } else if (!tostdout) {
    2645           4 :                 close_stream(s);
    2646             :         }
    2647           8 :   wrapup_result_set1:
    2648          31 :         cntxt->qryctx.starttime = 0;
    2649          31 :         mb->optimize = 0;
    2650          31 :         if( tbl) BBPunfix(tblId);
    2651          31 :         if( atr) BBPunfix(atrId);
    2652          31 :         if( tpe) BBPunfix(tpeId);
    2653          31 :         if( len) BBPunfix(lenId);
    2654          31 :         if( scale) BBPunfix(scaleId);
    2655             :         return msg;
    2656             : }
    2657             : 
    2658             : /* unsafe pattern resultSet(tbl:bat[:str], attr:bat[:str], tpe:bat[:str], len:bat[:int],scale:bat[:int], cols:any...) :int */
    2659             : str
    2660         520 : mvc_row_result_wrap( Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    2661             : {
    2662         520 :         int *res_id= getArgReference_int(stk, pci,0);
    2663         520 :         bat tblId= *getArgReference_bat(stk, pci,1);
    2664         520 :         bat atrId= *getArgReference_bat(stk, pci,2);
    2665         520 :         bat tpeId= *getArgReference_bat(stk, pci,3);
    2666         520 :         bat lenId= *getArgReference_bat(stk, pci,4);
    2667         520 :         bat scaleId= *getArgReference_bat(stk, pci,5);
    2668         520 :         int i, res, ok;
    2669         520 :         const char *tblname, *colname, *tpename;
    2670         520 :         str msg= MAL_SUCCEED;
    2671         520 :         int *digits, *scaledigits;
    2672         520 :         oid o = 0;
    2673         520 :         BATiter itertbl,iteratr,itertpe,iterdig,iterscl;
    2674         520 :         backend *be = NULL;
    2675         520 :         ptr v;
    2676         520 :         int mtype;
    2677         520 :         BAT *tbl = NULL, *atr = NULL, *tpe = NULL, *len = NULL, *scale = NULL;
    2678             : 
    2679         520 :         if ((msg = getBackendContext(cntxt, &be)) != NULL)
    2680             :                 return msg;
    2681         520 :         res = *res_id = mvc_result_table(be, mb->tag, pci->argc - (pci->retc + 5), Q_TABLE);
    2682         520 :         if (res < 0) {
    2683           0 :                 msg = createException(SQL, "sql.resultSet", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    2684           0 :                 goto wrapup_result_set;
    2685             :         }
    2686             : 
    2687         520 :         tbl = BATdescriptor(tblId);
    2688         520 :         atr = BATdescriptor(atrId);
    2689         520 :         tpe = BATdescriptor(tpeId);
    2690         520 :         len = BATdescriptor(lenId);
    2691         520 :         scale = BATdescriptor(scaleId);
    2692         520 :         if( tbl == NULL || atr == NULL || tpe == NULL || len == NULL || scale == NULL)
    2693           0 :                 goto wrapup_result_set;
    2694             :         /* mimick the old rsColumn approach; */
    2695         520 :         itertbl = bat_iterator(tbl);
    2696         520 :         iteratr = bat_iterator(atr);
    2697         520 :         itertpe = bat_iterator(tpe);
    2698         520 :         iterdig = bat_iterator(len);
    2699         520 :         iterscl = bat_iterator(scale);
    2700         520 :         digits = (int*) iterdig.base;
    2701         520 :         scaledigits = (int*) iterscl.base;
    2702             : 
    2703        1843 :         for( i = 6; msg == MAL_SUCCEED && i< pci->argc; i++, o++){
    2704        1323 :                 tblname = BUNtvar(itertbl,o);
    2705        1323 :                 colname = BUNtvar(iteratr,o);
    2706        1323 :                 tpename = BUNtvar(itertpe,o);
    2707             : 
    2708        1323 :                 v = getArgReference(stk, pci, i);
    2709        1323 :                 mtype = getArgType(mb, pci, i);
    2710        1323 :                 if (ATOMextern(mtype))
    2711         177 :                         v = *(ptr *) v;
    2712        1323 :                 if ((ok = mvc_result_value(be, tblname, colname, tpename, *digits++, *scaledigits++, v, mtype) < 0)) {
    2713           0 :                         msg = createException(SQL, "sql.rsColumn", SQLSTATE(45000) "Result set construction failed: %s", mvc_export_error(be, be->out, ok));
    2714           0 :                         bat_iterator_end(&itertbl);
    2715           0 :                         bat_iterator_end(&iteratr);
    2716           0 :                         bat_iterator_end(&itertpe);
    2717           0 :                         bat_iterator_end(&iterdig);
    2718           0 :                         bat_iterator_end(&iterscl);
    2719           0 :                         goto wrapup_result_set;
    2720             :                 }
    2721             :         }
    2722         520 :         bat_iterator_end(&itertbl);
    2723         520 :         bat_iterator_end(&iteratr);
    2724         520 :         bat_iterator_end(&itertpe);
    2725         520 :         bat_iterator_end(&iterdig);
    2726         520 :         bat_iterator_end(&iterscl);
    2727         520 :         if (!msg && (ok = mvc_export_result(cntxt->sqlcontext, cntxt->fdout, res, true, cntxt->qryctx.starttime, mb->optimize)) < 0)
    2728           0 :                 msg = createException(SQL, "sql.resultSet", SQLSTATE(45000) "Result set construction failed: %s", mvc_export_error(cntxt->sqlcontext, cntxt->fdout, ok));
    2729         520 :   wrapup_result_set:
    2730         520 :         cntxt->qryctx.starttime = 0;
    2731         520 :         mb->optimize = 0;
    2732         520 :         if( tbl) BBPunfix(tblId);
    2733         520 :         if( atr) BBPunfix(atrId);
    2734         520 :         if( tpe) BBPunfix(tpeId);
    2735         520 :         if( len) BBPunfix(lenId);
    2736         520 :         if( scale) BBPunfix(scaleId);
    2737             :         return msg;
    2738             : }
    2739             : 
    2740             : str
    2741           1 : mvc_export_row_wrap( Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    2742             : {
    2743           1 :         int *res_id= getArgReference_int(stk, pci,0);
    2744           1 :         str filename = * getArgReference_str(stk,pci,1);
    2745           1 :         const char *format = *getArgReference_str(stk,pci,2);
    2746           1 :         const char *tsep = *getArgReference_str(stk, pci, 3);
    2747           1 :         const char *rsep = *getArgReference_str(stk, pci, 4);
    2748           1 :         const char *ssep = *getArgReference_str(stk, pci, 5);
    2749           1 :         const char *ns = *getArgReference_str(stk, pci, 6);
    2750           1 :         int onclient = *getArgReference_int(stk, pci, 7);
    2751             : 
    2752           1 :         bat tblId= *getArgReference_bat(stk, pci,8);
    2753           1 :         bat atrId= *getArgReference_bat(stk, pci,9);
    2754           1 :         bat tpeId= *getArgReference_bat(stk, pci,10);
    2755           1 :         bat lenId= *getArgReference_bat(stk, pci,11);
    2756           1 :         bat scaleId= *getArgReference_bat(stk, pci,12);
    2757             : 
    2758           1 :         int i, res, ok;
    2759           1 :         stream *s = NULL;
    2760           1 :         const char *tblname, *colname, *tpename;
    2761           1 :         str msg = MAL_SUCCEED;
    2762           1 :         int *digits, *scaledigits;
    2763           1 :         oid o = 0;
    2764           1 :         BATiter itertbl,iteratr,itertpe,iterdig,iterscl;
    2765           1 :         backend *be;
    2766           1 :         mvc *m = NULL;
    2767           1 :         res_table *t = NULL;
    2768           1 :         ptr v;
    2769           1 :         int mtype;
    2770           1 :         BAT  *tbl = NULL, *atr = NULL, *tpe = NULL,*len = NULL,*scale = NULL;
    2771           1 :         bool tostdout;
    2772           1 :         char buf[80];
    2773           1 :         ssize_t sz;
    2774             : 
    2775           1 :         (void) format;
    2776           1 :         if ((msg = getBackendContext(cntxt, &be)) != NULL)
    2777             :                 return msg;
    2778           1 :         m = be->mvc;
    2779           1 :         if (onclient && !cntxt->filetrans) {
    2780           0 :                 msg = createException(SQL, "sql.resultSet", SQLSTATE(42000) "Cannot transfer files to client");
    2781           0 :                 goto wrapup_result_set;
    2782             :         }
    2783             : 
    2784           1 :         res = *res_id = mvc_result_table(be, mb->tag, pci->argc - (pci->retc + 12), Q_TABLE);
    2785             : 
    2786           1 :         t = be->results;
    2787           1 :         if (res < 0){
    2788           0 :                 msg = createException(SQL, "sql.resultSet", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    2789           0 :                 goto wrapup_result_set;
    2790             :         }
    2791             : 
    2792           1 :         t->tsep = tsep;
    2793           1 :         t->rsep = rsep;
    2794           1 :         t->ssep = ssep;
    2795           1 :         t->ns = ns;
    2796             : 
    2797           1 :         tbl = BATdescriptor(tblId);
    2798           1 :         atr = BATdescriptor(atrId);
    2799           1 :         tpe = BATdescriptor(tpeId);
    2800           1 :         len = BATdescriptor(lenId);
    2801           1 :         scale = BATdescriptor(scaleId);
    2802           1 :         if (tbl == NULL || atr == NULL || tpe == NULL || len == NULL || scale == NULL)
    2803           0 :                 goto wrapup_result_set;
    2804             :         /* mimick the old rsColumn approach; */
    2805           1 :         itertbl = bat_iterator(tbl);
    2806           1 :         iteratr = bat_iterator(atr);
    2807           1 :         itertpe = bat_iterator(tpe);
    2808           1 :         iterdig = bat_iterator(len);
    2809           1 :         iterscl = bat_iterator(scale);
    2810           1 :         digits = (int*) iterdig.base;
    2811           1 :         scaledigits = (int*) iterscl.base;
    2812             : 
    2813           2 :         for( i = 13; msg == MAL_SUCCEED && i< pci->argc; i++, o++){
    2814           1 :                 tblname = BUNtvar(itertbl,o);
    2815           1 :                 colname = BUNtvar(iteratr,o);
    2816           1 :                 tpename = BUNtvar(itertpe,o);
    2817             : 
    2818           1 :                 v = getArgReference(stk, pci, i);
    2819           1 :                 mtype = getArgType(mb, pci, i);
    2820           1 :                 if (ATOMextern(mtype))
    2821           0 :                         v = *(ptr *) v;
    2822           1 :                 if ((ok = mvc_result_value(be, tblname, colname, tpename, *digits++, *scaledigits++, v, mtype)) < 0) {
    2823           0 :                         msg = createException(SQL, "sql.rsColumn", SQLSTATE(45000) "Result set construction failed: %s", mvc_export_error(be, s, ok));
    2824           0 :                         bat_iterator_end(&itertbl);
    2825           0 :                         bat_iterator_end(&iteratr);
    2826           0 :                         bat_iterator_end(&itertpe);
    2827           0 :                         bat_iterator_end(&iterdig);
    2828           0 :                         bat_iterator_end(&iterscl);
    2829           0 :                         goto wrapup_result_set;
    2830             :                 }
    2831             :         }
    2832           1 :         bat_iterator_end(&itertbl);
    2833           1 :         bat_iterator_end(&iteratr);
    2834           1 :         bat_iterator_end(&itertpe);
    2835           1 :         bat_iterator_end(&iterdig);
    2836           1 :         bat_iterator_end(&iterscl);
    2837             :         /* now select the file channel */
    2838           1 :         if ((tostdout = strcmp(filename,"stdout") == 0)) {
    2839           1 :                 s = cntxt->fdout;
    2840           0 :         } else if (!onclient) {
    2841           0 :                 if ((s = open_wastream(filename)) == NULL || mnstr_errnr(s) != MNSTR_NO__ERROR) {
    2842           0 :                         msg=  createException(IO, "streams.open", SQLSTATE(42000) "%s", mnstr_peek_error(NULL));
    2843           0 :                         close_stream(s);
    2844           0 :                         goto wrapup_result_set;
    2845             :                 }
    2846             :         } else {
    2847           0 :                 while (!m->scanner.rs->eof)
    2848           0 :                         bstream_next(m->scanner.rs);
    2849           0 :                 s = m->scanner.ws;
    2850           0 :                 mnstr_write(s, PROMPT3, sizeof(PROMPT3) - 1, 1);
    2851           0 :                 mnstr_printf(s, "w %s\n", filename);
    2852           0 :                 mnstr_flush(s, MNSTR_FLUSH_DATA);
    2853           0 :                 if ((sz = mnstr_readline(m->scanner.rs->s, buf, sizeof(buf))) > 1) {
    2854             :                         /* non-empty line indicates failure on client */
    2855           0 :                         msg = createException(IO, "streams.open", "%s", buf);
    2856             :                         /* discard until client flushes */
    2857           0 :                         while (mnstr_read(m->scanner.rs->s, buf, 1, sizeof(buf)) > 0) {
    2858             :                                 /* ignore remainder of error message */
    2859           0 :                         }
    2860           0 :                         goto wrapup_result_set;
    2861             :                 }
    2862             :         }
    2863           1 :         if ((ok = mvc_export_result(cntxt->sqlcontext, s, res, strcmp(filename, "stdout") == 0, cntxt->qryctx.starttime, mb->optimize)) < 0) {
    2864           0 :                 msg = createException(SQL, "sql.resultSet", SQLSTATE(45000) "Result set construction failed: %s", mvc_export_error(cntxt->sqlcontext, s, ok));
    2865           0 :                 if (!onclient && !tostdout)
    2866           0 :                         close_stream(s);
    2867           0 :                 goto wrapup_result_set;
    2868             :         }
    2869           1 :         if (onclient) {
    2870           0 :                 mnstr_flush(s, MNSTR_FLUSH_DATA);
    2871           0 :                 if ((sz = mnstr_readline(m->scanner.rs->s, buf, sizeof(buf))) > 1) {
    2872           0 :                         msg = createException(IO, "streams.open", "%s", buf);
    2873             :                 }
    2874           0 :                 while (sz > 0)
    2875           0 :                         sz = mnstr_readline(m->scanner.rs->s, buf, sizeof(buf));
    2876           1 :         } else if (!tostdout) {
    2877           0 :                 close_stream(s);
    2878             :         }
    2879           1 :   wrapup_result_set:
    2880           1 :         cntxt->qryctx.starttime = 0;
    2881           1 :         mb->optimize = 0;
    2882           1 :         if( tbl) BBPunfix(tblId);
    2883           1 :         if( atr) BBPunfix(atrId);
    2884           1 :         if( tpe) BBPunfix(tpeId);
    2885           1 :         if( len) BBPunfix(lenId);
    2886           1 :         if( scale) BBPunfix(scaleId);
    2887             :         return msg;
    2888             : }
    2889             : 
    2890             : str
    2891       60180 : mvc_table_result_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    2892             : {
    2893       60180 :         str res = MAL_SUCCEED;
    2894       60180 :         backend *be = NULL;
    2895       60180 :         str msg;
    2896       60180 :         int *res_id;
    2897       60180 :         int nr_cols;
    2898       60180 :         mapi_query_t qtype;
    2899             : 
    2900       60180 :         if ( pci->argc > 6)
    2901       60180 :                 return mvc_result_set_wrap(cntxt,mb,stk,pci);
    2902             : 
    2903           0 :         assert(0);
    2904             :         res_id = getArgReference_int(stk, pci, 0);
    2905             :         nr_cols = *getArgReference_int(stk, pci, 1);
    2906             :         qtype = (mapi_query_t) *getArgReference_int(stk, pci, 2);
    2907             :         bat order_bid = *getArgReference_bat(stk, pci, 3);
    2908             :         (void)order_bid;
    2909             :         /* TODO remove use */
    2910             : 
    2911             :         if ((msg = getBackendContext(cntxt, &be)) != NULL)
    2912             :                 return msg;
    2913             :         *res_id = mvc_result_table(be, mb->tag, nr_cols, qtype);
    2914             :         if (*res_id < 0)
    2915             :                 res = createException(SQL, "sql.resultSet", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    2916             :         return res;
    2917             : }
    2918             : 
    2919             : /* str mvc_affected_rows_wrap(int *m, int m, lng *nr, str *w); */
    2920             : str
    2921      132118 : mvc_affected_rows_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    2922             : {
    2923      132118 :         backend *b = NULL;
    2924      132118 :         int *res = getArgReference_int(stk, pci, 0), ok;
    2925             : #ifndef NDEBUG
    2926      132118 :         int mtype = getArgType(mb, pci, 2);
    2927             : #endif
    2928      132118 :         lng nr;
    2929      132118 :         str msg;
    2930             : 
    2931      132118 :         (void) mb;              /* NOT USED */
    2932      132118 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    2933             :                 return msg;
    2934      132118 :         *res = 0;
    2935      132118 :         assert(mtype == TYPE_lng);
    2936      132118 :         nr = *getArgReference_lng(stk, pci, 2);
    2937      132118 :         b = cntxt->sqlcontext;
    2938      132118 :         ok = mvc_export_affrows(b, b->out, nr, "", mb->tag, cntxt->qryctx.starttime, mb->optimize);
    2939      132115 :         cntxt->qryctx.starttime = 0;
    2940      132115 :         mb->optimize = 0;
    2941      132115 :         if (ok < 0)
    2942           0 :                 throw(SQL, "sql.affectedRows", SQLSTATE(45000) "Result set construction failed: %s", mvc_export_error(b, b->out, ok));
    2943             :         return MAL_SUCCEED;
    2944             : }
    2945             : 
    2946             : /* str mvc_export_head_wrap(int *ret, stream **s, int *res_id); */
    2947             : str
    2948           0 : mvc_export_head_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    2949             : {
    2950           0 :         backend *b = NULL;
    2951           0 :         stream **s = (stream **) getArgReference(stk, pci, 1);
    2952           0 :         int res_id = *getArgReference_int(stk, pci, 2), ok;
    2953           0 :         str msg;
    2954             : 
    2955           0 :         (void) mb;              /* NOT USED */
    2956           0 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    2957             :                 return msg;
    2958           0 :         b = cntxt->sqlcontext;
    2959           0 :         ok = mvc_export_head(b, *s, res_id, FALSE, TRUE, cntxt->qryctx.starttime, mb->optimize);
    2960           0 :         cntxt->qryctx.starttime = 0;
    2961           0 :         mb->optimize = 0;
    2962           0 :         if (ok < 0)
    2963           0 :                 throw(SQL, "sql.exportHead", SQLSTATE(45000) "Result set construction failed: %s", mvc_export_error(b, *s, ok));
    2964             :         return MAL_SUCCEED;
    2965             : }
    2966             : 
    2967             : /* str mvc_export_result_wrap(int *ret, stream **s, int *res_id); */
    2968             : str
    2969           0 : mvc_export_result_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    2970             : {
    2971           0 :         backend *b = NULL;
    2972           0 :         stream **s = (stream **) getArgReference(stk, pci, 1), *sout;
    2973           0 :         int res_id = *getArgReference_int(stk, pci, 2), ok;
    2974           0 :         str msg;
    2975             : 
    2976           0 :         (void) mb;              /* NOT USED */
    2977           0 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    2978             :                 return msg;
    2979           0 :         b = cntxt->sqlcontext;
    2980           0 :         sout = pci->argc > 5 ? cntxt->fdout : *s;
    2981           0 :         ok = mvc_export_result(b, sout, res_id, false, cntxt->qryctx.starttime, mb->optimize);
    2982           0 :         cntxt->qryctx.starttime = 0;
    2983           0 :         mb->optimize = 0;
    2984           0 :         if (ok < 0)
    2985           0 :                 throw(SQL, "sql.exportResult", SQLSTATE(45000) "Result set construction failed: %s", mvc_export_error(b, sout, ok));
    2986             :         return MAL_SUCCEED;
    2987             : }
    2988             : 
    2989             : /* str mvc_export_chunk_wrap(int *ret, stream **s, int *res_id, str *w); */
    2990             : str
    2991           0 : mvc_export_chunk_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    2992             : {
    2993           0 :         backend *b = NULL;
    2994           0 :         stream **s = (stream **) getArgReference(stk, pci, 1);
    2995           0 :         int res_id = *getArgReference_int(stk, pci, 2), ok;
    2996           0 :         BUN offset = 0;
    2997           0 :         BUN nr = 0;
    2998           0 :         str msg;
    2999             : 
    3000           0 :         (void) mb;              /* NOT USED */
    3001           0 :         if (pci->argc == 5) {
    3002           0 :                 offset = (BUN) *getArgReference_int(stk, pci, 3);
    3003           0 :                 int cnt = *getArgReference_int(stk, pci, 4);
    3004           0 :                 nr = cnt < 0 ? BUN_NONE : (BUN) cnt;
    3005             :         }
    3006             : 
    3007           0 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    3008             :                 return msg;
    3009           0 :         b = cntxt->sqlcontext;
    3010           0 :         if ((ok = mvc_export_chunk(b, *s, res_id, offset, nr)) < 0)
    3011           0 :                 throw(SQL, "sql.exportChunk", SQLSTATE(45000) "Result set construction failed: %s", mvc_export_error(b, *s, ok));
    3012             :         return NULL;
    3013             : }
    3014             : 
    3015             : /* str mvc_export_operation_wrap(int *ret, str *w); */
    3016             : str
    3017       18670 : mvc_export_operation_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    3018             : {
    3019       18670 :         backend *b = NULL;
    3020       18670 :         str msg;
    3021       18670 :         int ok = 0;
    3022             : 
    3023       18670 :         (void) stk;             /* NOT USED */
    3024       18670 :         (void) pci;             /* NOT USED */
    3025       18670 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    3026             :                 return msg;
    3027       18670 :         b = cntxt->sqlcontext;
    3028       18670 :         if (b->out)
    3029       18669 :                 ok = mvc_export_operation(b, b->out, "", cntxt->qryctx.starttime, mb->optimize);
    3030       18670 :         cntxt->qryctx.starttime = 0;
    3031       18670 :         mb->optimize = 0;
    3032       18670 :         if (ok < 0)
    3033           0 :                 throw(SQL, "sql.exportOperation", SQLSTATE(45000) "Result set construction failed: %s", mvc_export_error(b, b->out, ok));
    3034             :         return MAL_SUCCEED;
    3035             : }
    3036             : 
    3037             : str
    3038             : /*mvc_scalar_value_wrap(int *ret, int *qtype, str tn, str name, str type, int *digits, int *scale, int *eclass, ptr p, int mtype)*/
    3039       60464 : mvc_scalar_value_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    3040             : {
    3041       60464 :         const char *tn = *getArgReference_str(stk, pci, 1);
    3042       60464 :         const char *cn = *getArgReference_str(stk, pci, 2);
    3043       60464 :         const char *type = *getArgReference_str(stk, pci, 3);
    3044       60464 :         int digits = *getArgReference_int(stk, pci, 4);
    3045       60464 :         int scale = *getArgReference_int(stk, pci, 5);
    3046       60464 :         ptr p = getArgReference(stk, pci, 7);
    3047       60464 :         int mtype = getArgType(mb, pci, 7);
    3048       60464 :         str msg;
    3049       60464 :         backend *be = NULL;
    3050       60464 :         int res_id, ok;
    3051       60464 :         (void) mb;              /* NOT USED */
    3052       60464 :         if ((msg = getBackendContext(cntxt, &be)) != NULL)
    3053             :                 return msg;
    3054       60464 :         if (ATOMextern(mtype))
    3055         583 :                 p = *(ptr *) p;
    3056             : 
    3057             :         // scalar values are single-column result sets
    3058       60464 :         if ((res_id = mvc_result_table(be, mb->tag, 1, Q_TABLE)) < 0) {
    3059           0 :                 cntxt->qryctx.starttime = 0;
    3060           0 :                 mb->optimize = 0;
    3061           0 :                 throw(SQL, "sql.exportValue", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3062             :         }
    3063       60464 :         if ((ok = mvc_result_value(be, tn, cn, type, digits, scale, p, mtype)) < 0) {
    3064           0 :                 cntxt->qryctx.starttime = 0;
    3065           0 :                 mb->optimize = 0;
    3066           0 :                 throw(SQL, "sql.exportValue", SQLSTATE(45000) "Result set construction failed: %s", mvc_export_error(be, be->out, ok));
    3067             :         }
    3068       60464 :         if (be->output_format == OFMT_NONE) {
    3069           2 :                 cntxt->qryctx.starttime = 0;
    3070           2 :                 mb->optimize = 0;
    3071           2 :                 return MAL_SUCCEED;
    3072             :         }
    3073       60462 :         ok = mvc_export_result(be, be->out, res_id, true, cntxt->qryctx.starttime, mb->optimize);
    3074       60462 :         cntxt->qryctx.starttime = 0;
    3075       60462 :         mb->optimize = 0;
    3076       60462 :         if (ok < 0)
    3077           0 :                 throw(SQL, "sql.exportValue", SQLSTATE(45000) "Result set construction failed: %s", mvc_export_error(be, be->out, ok));
    3078             :         return MAL_SUCCEED;
    3079             : }
    3080             : 
    3081             : static void
    3082        1046 : bat2return(MalStkPtr stk, InstrPtr pci, BAT **b)
    3083             : {
    3084        1046 :         int i;
    3085             : 
    3086       11066 :         for (i = 0; i < pci->retc; i++) {
    3087       10020 :                 *getArgReference_bat(stk, pci, i) = b[i]->batCacheid;
    3088       10020 :                 BBPkeepref(b[i]);
    3089             :         }
    3090        1046 : }
    3091             : 
    3092             : static const char fwftsep[2] = {STREAM_FWF_FIELD_SEP, '\0'};
    3093             : static const char fwfrsep[2] = {STREAM_FWF_RECORD_SEP, '\0'};
    3094             : 
    3095             : /* 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); */
    3096             : str
    3097        1073 : mvc_import_table_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    3098             : {
    3099        1073 :         backend *be;
    3100        1073 :         BAT **b = NULL;
    3101        1073 :         sql_table *t = *(sql_table **) getArgReference(stk, pci, pci->retc + 0);
    3102        1073 :         const char *tsep = *getArgReference_str(stk, pci, pci->retc + 1);
    3103        1073 :         const char *rsep = *getArgReference_str(stk, pci, pci->retc + 2);
    3104        1073 :         const char *ssep = *getArgReference_str(stk, pci, pci->retc + 3);
    3105        1073 :         const char *ns = *getArgReference_str(stk, pci, pci->retc + 4);
    3106        1073 :         const char *fname = *getArgReference_str(stk, pci, pci->retc + 5);
    3107        1073 :         lng sz = *getArgReference_lng(stk, pci, pci->retc + 6);
    3108        1073 :         lng offset = *getArgReference_lng(stk, pci, pci->retc + 7);
    3109        1073 :         int besteffort = *getArgReference_int(stk, pci, pci->retc + 8);
    3110        1073 :         const char *fixed_widths = *getArgReference_str(stk, pci, pci->retc + 9);
    3111        1073 :         int onclient = *getArgReference_int(stk, pci, pci->retc + 10);
    3112        1073 :         bool escape = *getArgReference_int(stk, pci, pci->retc + 11);
    3113        1073 :         str msg = MAL_SUCCEED;
    3114        1073 :         bstream *s = NULL;
    3115        1073 :         stream *ss;
    3116             : 
    3117        1073 :         (void) mb;              /* NOT USED */
    3118        1073 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    3119             :                 return msg;
    3120        1073 :         if (onclient && !cntxt->filetrans)
    3121           0 :                 throw(MAL, "sql.copy_from", SQLSTATE(42000) "Cannot transfer files from client");
    3122             : 
    3123        1073 :         be = cntxt->sqlcontext;
    3124             :         /* The CSV parser expects ssep to have the value 0 if the user does not
    3125             :          * specify a quotation character
    3126             :          */
    3127        1656 :         if (*ssep == 0 || strNil(ssep))
    3128             :                 ssep = NULL;
    3129             : 
    3130        1073 :         if (strNil(fname))
    3131         760 :                 fname = NULL;
    3132        1073 :         if (fname == NULL) {
    3133         760 :                 msg = mvc_import_table(cntxt, &b, be->mvc, be->mvc->scanner.rs, t, tsep, rsep, ssep, ns, sz, offset, besteffort, true, escape);
    3134             :         } else {
    3135         313 :                 if (onclient) {
    3136          28 :                         ss = mapi_request_upload(fname, false, be->mvc->scanner.rs, be->mvc->scanner.ws);
    3137             :                 } else {
    3138         285 :                         ss = open_rastream(fname);
    3139             :                 }
    3140         313 :                 if (ss == NULL || mnstr_errnr(ss) != MNSTR_NO__ERROR) {
    3141           2 :                         msg = createException(IO, "sql.copy_from", SQLSTATE(42000) "%s", mnstr_peek_error(NULL));
    3142           2 :                         close_stream(ss);
    3143           2 :                         return msg;
    3144             :                 }
    3145             : 
    3146         622 :                 if (!strNil(fixed_widths)) {
    3147           1 :                         size_t ncol = 0, current_width_entry = 0, i;
    3148           1 :                         size_t *widths;
    3149           1 :                         const char* val_start = fixed_widths;
    3150           1 :                         size_t width_len = strlen(fixed_widths);
    3151           1 :                         stream *ns;
    3152             : 
    3153          23 :                         for (i = 0; i < width_len; i++) {
    3154          22 :                                 if (fixed_widths[i] == STREAM_FWF_FIELD_SEP) {
    3155          11 :                                         ncol++;
    3156             :                                 }
    3157             :                         }
    3158           1 :                         widths = malloc(sizeof(size_t) * ncol);
    3159           1 :                         if (!widths) {
    3160           0 :                                 close_stream(ss);
    3161           0 :                                 throw(MAL, "sql.copy_from", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3162             :                         }
    3163          23 :                         for (i = 0; i < width_len; i++) {
    3164          22 :                                 if (fixed_widths[i] == STREAM_FWF_FIELD_SEP) {
    3165          11 :                                         widths[current_width_entry++] = (size_t) strtoll(val_start, NULL, 10);
    3166          11 :                                         val_start = fixed_widths + i + 1;
    3167             :                                 }
    3168             :                         }
    3169             :                         /* overwrite other delimiters to the ones the FWF stream uses */
    3170           1 :                         tsep = fwftsep;
    3171           1 :                         rsep = fwfrsep;
    3172             : 
    3173           1 :                         ns = stream_fwf_create(ss, ncol, widths, STREAM_FWF_FILLER);
    3174           1 :                         if (ns == NULL || mnstr_errnr(ns) != MNSTR_NO__ERROR) {
    3175           0 :                                 msg = createException(IO, "sql.copy_from", SQLSTATE(42000) "%s", mnstr_peek_error(NULL));
    3176           0 :                                 close_stream(ss);
    3177           0 :                                 free(widths);
    3178           0 :                                 return msg;
    3179             :                         }
    3180             :                         ss = ns;
    3181             :                 }
    3182             : #if SIZEOF_VOID_P == 4
    3183             :                 s = bstream_create(ss, 0x20000);
    3184             : #else
    3185         311 :                 s = bstream_create(ss, 0x200000);
    3186             : #endif
    3187         311 :                 if (s == NULL) {
    3188           0 :                         close_stream(ss);
    3189           0 :                         throw(MAL, "sql.copy_from", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3190             :                 }
    3191         311 :                 msg = mvc_import_table(cntxt, &b, be->mvc, s, t, tsep, rsep, ssep, ns, sz, offset, besteffort, false, escape);
    3192             :                 // This also closes ss:
    3193         311 :                 bstream_destroy(s);
    3194             :         }
    3195        1071 :         if (b && !msg)
    3196        1046 :                 bat2return(stk, pci, b);
    3197        1071 :         GDKfree(b);
    3198        1071 :         return msg;
    3199             : }
    3200             : 
    3201             : str
    3202         236 : not_unique(bit *ret, const bat *bid)
    3203             : {
    3204         236 :         BAT *b;
    3205             : 
    3206         236 :         if ((b = BATdescriptor(*bid)) == NULL) {
    3207           0 :                 throw(SQL, "not_unique", SQLSTATE(HY005) "Cannot access column descriptor");
    3208             :         }
    3209             : 
    3210         236 :         *ret = FALSE;
    3211         236 :         BATiter bi = bat_iterator(b);
    3212         236 :         if (bi.key || BATtdensebi(&bi) || bi.count <= 1) {
    3213         235 :                 bat_iterator_end(&bi);
    3214         235 :                 BBPunfix(b->batCacheid);
    3215         235 :                 return MAL_SUCCEED;
    3216           1 :         } else if (bi.sorted) {
    3217           1 :                 BUN p;
    3218           1 :                 oid c = ((oid *) bi.base)[0];
    3219             : 
    3220           2 :                 for (p = 1; p < bi.count; p++) {
    3221           2 :                         oid v = ((oid *) bi.base)[p];
    3222           2 :                         if (v <= c) {
    3223           1 :                                 *ret = TRUE;
    3224           1 :                                 break;
    3225             :                         }
    3226           1 :                         c = v;
    3227             :                 }
    3228             :         } else {
    3229           0 :                 bat_iterator_end(&bi);
    3230           0 :                 BBPunfix(b->batCacheid);
    3231           0 :                 throw(SQL, "not_unique", SQLSTATE(42000) "Input column should be sorted");
    3232             :         }
    3233           1 :         bat_iterator_end(&bi);
    3234           1 :         BBPunfix(b->batCacheid);
    3235           1 :         return MAL_SUCCEED;
    3236             : }
    3237             : 
    3238             : /* row case */
    3239             : str
    3240          29 : SQLidentity(oid *ret, const void *i)
    3241             : {
    3242          29 :         (void)i;
    3243          29 :         *ret = 0;
    3244          29 :         return MAL_SUCCEED;
    3245             : }
    3246             : 
    3247             : str
    3248          27 : BATSQLidentity(bat *ret, const bat *bid)
    3249             : {
    3250          27 :         return BKCmirror(ret, bid);
    3251             : }
    3252             : 
    3253             : str
    3254           4 : PBATSQLidentity(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    3255             : {
    3256           4 :         bat *res = getArgReference_bat(stk, pci, 0);
    3257           4 :         oid *ns = getArgReference_oid(stk, pci, 1);
    3258           4 :         bat bid = *getArgReference_bat(stk, pci, 2);
    3259           4 :         oid s = *getArgReference_oid(stk, pci, 3);
    3260           4 :         BAT *b, *bn = NULL;
    3261             : 
    3262           4 :         (void) cntxt;
    3263           4 :         (void) mb;
    3264           4 :         if (!(b = BBPquickdesc(bid)))
    3265           0 :                 throw(MAL, "batcalc.identity", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
    3266           4 :         if (!(bn = BATdense(b->hseqbase, s, BATcount(b))))
    3267           0 :                 throw(MAL, "batcalc.identity", GDK_EXCEPTION);
    3268           4 :         *ns = s + BATcount(b);
    3269           4 :         *res = bn->batCacheid;
    3270           4 :         BBPkeepref(bn);
    3271           4 :         return MAL_SUCCEED;
    3272             : }
    3273             : 
    3274             : /*
    3275             :  * The core modules of Monet provide just a limited set of
    3276             :  * mathematical operators. The extensions required to support
    3277             :  * SQL-99 are shown below. At some point they also should be
    3278             :  * moved to module code base.
    3279             :  */
    3280             : 
    3281             : str
    3282          10 : SQLcst_alpha_cst(dbl *res, const dbl *decl, const dbl *theta)
    3283             : {
    3284          10 :         dbl s, c1, c2;
    3285          10 :         char *msg = MAL_SUCCEED;
    3286          10 :         if (is_dbl_nil(*decl) || is_dbl_nil(*theta)) {
    3287           0 :                 *res = dbl_nil;
    3288          10 :         } else if (fabs(*decl) + *theta > 89.9) {
    3289           0 :                 *res = 180.0;
    3290             :         } else {
    3291          10 :                 s = sin(radians(*theta));
    3292          10 :                 c1 = cos(radians(*decl - *theta));
    3293          10 :                 c2 = cos(radians(*decl + *theta));
    3294          10 :                 *res = degrees(fabs(atan(s / sqrt(fabs(c1 * c2)))));
    3295             :         }
    3296          10 :         return msg;
    3297             : }
    3298             : 
    3299             : /*
    3300             :   sql5_export str SQLcst_alpha_cst(dbl *res, dbl *decl, dbl *theta);
    3301             :   sql5_export str SQLbat_alpha_cst(bat *res, bat *decl, dbl *theta);
    3302             :   sql5_export str SQLcst_alpha_bat(bat *res, dbl *decl, bat *theta);
    3303             : */
    3304             : str
    3305           0 : SQLbat_alpha_cst(bat *res, const bat *decl, const dbl *theta)
    3306             : {
    3307           0 :         BAT *b, *bn;
    3308           0 :         BUN p, q;
    3309           0 :         dbl s, c1, c2, r;
    3310           0 :         char *msg = NULL;
    3311             : 
    3312           0 :         if (is_dbl_nil(*theta)) {
    3313           0 :                 throw(SQL, "SQLbat_alpha", SQLSTATE(42000) "Parameter theta should not be nil");
    3314             :         }
    3315           0 :         if ((b = BATdescriptor(*decl)) == NULL) {
    3316           0 :                 throw(SQL, "alpha", SQLSTATE(HY005) "Cannot access column descriptor");
    3317             :         }
    3318           0 :         bn = COLnew(b->hseqbase, TYPE_dbl, BATcount(b), TRANSIENT);
    3319           0 :         if (bn == NULL) {
    3320           0 :                 BBPunfix(b->batCacheid);
    3321           0 :                 throw(SQL, "sql.alpha", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3322             :         }
    3323           0 :         s = sin(radians(*theta));
    3324           0 :         BATiter bi = bat_iterator(b);
    3325           0 :         const dbl *vals = (const dbl *) bi.base;
    3326           0 :         BATloop(b, p, q) {
    3327           0 :                 dbl d = vals[p];
    3328           0 :                 if (is_dbl_nil(d))
    3329           0 :                         r = dbl_nil;
    3330           0 :                 else if (fabs(d) + *theta > 89.9)
    3331           0 :                         r = 180.0;
    3332             :                 else {
    3333           0 :                         c1 = cos(radians(d - *theta));
    3334           0 :                         c2 = cos(radians(d + *theta));
    3335           0 :                         r = degrees(fabs(atan(s / sqrt(fabs(c1 * c2)))));
    3336             :                 }
    3337           0 :                 if (BUNappend(bn, &r, false) != GDK_SUCCEED) {
    3338           0 :                         BBPreclaim(bn);
    3339           0 :                         bat_iterator_end(&bi);
    3340           0 :                         BBPunfix(b->batCacheid);
    3341           0 :                         throw(SQL, "sql.alpha", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3342             :                 }
    3343             :         }
    3344           0 :         bat_iterator_end(&bi);
    3345           0 :         *res = bn->batCacheid;
    3346           0 :         BBPkeepref(bn);
    3347           0 :         BBPunfix(b->batCacheid);
    3348           0 :         return msg;
    3349             : }
    3350             : 
    3351             : str
    3352           0 : SQLcst_alpha_bat(bat *res, const dbl *decl, const bat *thetabid)
    3353             : {
    3354           0 :         BAT *b, *bn;
    3355           0 :         BUN p, q;
    3356           0 :         dbl s, c1, c2, r;
    3357           0 :         char *msg = NULL;
    3358           0 :         dbl *thetas;
    3359             : 
    3360           0 :         if ((b = BATdescriptor(*thetabid)) == NULL) {
    3361           0 :                 throw(SQL, "alpha", SQLSTATE(HY005) "Cannot access column descriptor");
    3362             :         }
    3363           0 :         bn = COLnew(b->hseqbase, TYPE_dbl, BATcount(b), TRANSIENT);
    3364           0 :         if (bn == NULL) {
    3365           0 :                 BBPunfix(b->batCacheid);
    3366           0 :                 throw(SQL, "sql.alpha", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3367             :         }
    3368           0 :         BATiter bi = bat_iterator(b);
    3369           0 :         thetas = (dbl *) bi.base;
    3370           0 :         BATloop(b, p, q) {
    3371           0 :                 dbl d = *decl;
    3372           0 :                 dbl theta = thetas[p];
    3373             : 
    3374           0 :                 if (is_dbl_nil(d))
    3375           0 :                         r = dbl_nil;
    3376           0 :                 else if (fabs(d) + theta > 89.9)
    3377           0 :                         r = (dbl) 180.0;
    3378             :                 else {
    3379           0 :                         s = sin(radians(theta));
    3380           0 :                         c1 = cos(radians(d - theta));
    3381           0 :                         c2 = cos(radians(d + theta));
    3382           0 :                         r = degrees(fabs(atan(s / sqrt(fabs(c1 * c2)))));
    3383             :                 }
    3384           0 :                 if (BUNappend(bn, &r, false) != GDK_SUCCEED) {
    3385           0 :                         BBPreclaim(bn);
    3386           0 :                         bat_iterator_end(&bi);
    3387           0 :                         BBPunfix(b->batCacheid);
    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);
    3394           0 :         BBPunfix(b->batCacheid);
    3395           0 :         return msg;
    3396             : }
    3397             : 
    3398             : /* str dump_cache(int *r); */
    3399             : str
    3400           0 : dump_cache(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    3401             : {
    3402           0 :         mvc *m = NULL;
    3403           0 :         str msg;
    3404           0 :         int cnt;
    3405           0 :         cq *q = NULL;
    3406           0 :         BAT *query, *count;
    3407           0 :         bat *rquery = getArgReference_bat(stk, pci, 0);
    3408           0 :         bat *rcount = getArgReference_bat(stk, pci, 1);
    3409             : 
    3410           0 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    3411             :                 return msg;
    3412           0 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    3413             :                 return msg;
    3414           0 :         cnt = m->qc->id;
    3415           0 :         query = COLnew(0, TYPE_str, cnt, TRANSIENT);
    3416           0 :         if (query == NULL)
    3417           0 :                 throw(SQL, "sql.dumpcache", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3418           0 :         count = COLnew(0, TYPE_int, cnt, TRANSIENT);
    3419           0 :         if (count == NULL) {
    3420           0 :                 BBPunfix(query->batCacheid);
    3421           0 :                 throw(SQL, "sql.dumpcache", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3422             :         }
    3423             : 
    3424           0 :         for (q = m->qc->q; q; q = q->next) {
    3425           0 :                 if (BUNappend(query, q->f->query, false) != GDK_SUCCEED ||
    3426           0 :                     BUNappend(count, &q->count, false) != GDK_SUCCEED) {
    3427           0 :                         BBPunfix(query->batCacheid);
    3428           0 :                         BBPunfix(count->batCacheid);
    3429           0 :                         throw(SQL, "sql.dumpcache", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3430             :                 }
    3431             :         }
    3432           0 :         *rquery = query->batCacheid;
    3433           0 :         *rcount = count->batCacheid;
    3434           0 :         BBPkeepref(query);
    3435           0 :         BBPkeepref(count);
    3436           0 :         return MAL_SUCCEED;
    3437             : }
    3438             : 
    3439             : /* str dump_opt_stats(int *r); */
    3440             : str
    3441           0 : dump_opt_stats(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    3442             : {
    3443           0 :         backend *be;
    3444           0 :         str msg;
    3445           0 :         int cnt;
    3446           0 :         BAT *rewrite, *count;
    3447           0 :         bat *rrewrite = getArgReference_bat(stk, pci, 0);
    3448           0 :         bat *rcount = getArgReference_bat(stk, pci, 1);
    3449             : 
    3450           0 :         (void)mb;
    3451           0 :         if ((msg = getBackendContext(cntxt, &be)) != NULL)
    3452             :                 return msg;
    3453           0 :         cnt = be->mvc->qc->id;
    3454           0 :         rewrite = COLnew(0, TYPE_str, cnt, TRANSIENT);
    3455           0 :         count = COLnew(0, TYPE_int, cnt, TRANSIENT);
    3456           0 :         if (rewrite == NULL || count == NULL) {
    3457           0 :                 BBPreclaim(rewrite);
    3458           0 :                 BBPreclaim(count);
    3459           0 :                 throw(SQL, "sql.optstats", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3460             :         }
    3461             : 
    3462           0 :         if (BUNappend(rewrite, "joinidx", false) != GDK_SUCCEED ||
    3463           0 :             BUNappend(count, &be->join_idx, false) != GDK_SUCCEED) {
    3464           0 :                 BBPreclaim(rewrite);
    3465           0 :                 BBPreclaim(count);
    3466           0 :                 throw(SQL, "sql.optstats", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3467             :         }
    3468             :         /* TODO add other rewrites */
    3469             : 
    3470           0 :         *rrewrite = rewrite->batCacheid;
    3471           0 :         *rcount = count->batCacheid;
    3472           0 :         BBPkeepref(rewrite);
    3473           0 :         BBPkeepref(count);
    3474           0 :         return MAL_SUCCEED;
    3475             : }
    3476             : 
    3477             : /* str dump_opt_stats(int *r); */
    3478             : str
    3479          53 : dump_trace(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    3480             : {
    3481          53 :         int i;
    3482          53 :         BAT *t[3];
    3483             : 
    3484          53 :         (void) cntxt;
    3485          53 :         (void) mb;
    3486          53 :         if (TRACEtable(cntxt, t) != 3)
    3487           0 :                 throw(SQL, "sql.dump_trace", SQLSTATE(3F000) "Profiler not started");
    3488         212 :         for (i = 0; i < 3; i++) {
    3489         159 :                 *getArgReference_bat(stk, pci, i) = t[i]->batCacheid;
    3490         159 :                 BBPkeepref(t[i]);
    3491             :         }
    3492             :         return MAL_SUCCEED;
    3493             : }
    3494             : 
    3495             : static str
    3496          26 : sql_sessions_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    3497             : {
    3498          26 :         return CLTsessions(cntxt, mb, stk, pci);
    3499             : }
    3500             : 
    3501             : str
    3502          71 : sql_querylog_catalog(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    3503             : {
    3504          71 :         int i;
    3505          71 :         BAT *t[8];
    3506          71 :         str msg;
    3507             : 
    3508          71 :         (void) cntxt;
    3509          71 :         (void) mb;
    3510          71 :         msg = QLOGcatalog(t);
    3511          71 :         if( msg != MAL_SUCCEED)
    3512             :                 return msg;
    3513         639 :         for (i = 0; i < 8; i++)
    3514         568 :         if( t[i]){
    3515         568 :                 *getArgReference_bat(stk, pci, i) = t[i]->batCacheid;
    3516         568 :                 BBPkeepref(t[i]);
    3517             :         } else
    3518           0 :                 throw(SQL,"sql.querylog", SQLSTATE(45000) "Missing query catalog BAT");
    3519             :         return MAL_SUCCEED;
    3520             : }
    3521             : 
    3522             : str
    3523          55 : sql_querylog_calls(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    3524             : {
    3525          55 :         int i;
    3526          55 :         BAT *t[10];
    3527          55 :         str msg;
    3528             : 
    3529          55 :         (void) cntxt;
    3530          55 :         (void) mb;
    3531          55 :         msg = QLOGcalls(t);
    3532          55 :         if( msg != MAL_SUCCEED)
    3533             :                 return msg;
    3534         550 :         for (i = 0; i < 9; i++)
    3535         495 :         if( t[i]){
    3536         495 :                 *getArgReference_bat(stk, pci, i) = t[i]->batCacheid;
    3537         495 :                 BBPkeepref(t[i]);
    3538             :         } else
    3539           0 :                 throw(SQL,"sql.querylog", SQLSTATE(45000) "Missing query call BAT");
    3540             :         return MAL_SUCCEED;
    3541             : }
    3542             : 
    3543             : str
    3544           1 : sql_querylog_empty(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    3545             : {
    3546           1 :         (void) cntxt;
    3547           1 :         (void) mb;
    3548           1 :         (void) stk;
    3549           1 :         (void) pci;
    3550           1 :         return QLOGempty(NULL);
    3551             : }
    3552             : 
    3553             : /* str sql_rowid(oid *rid, ptr v, str *sname, str *tname); */
    3554             : str
    3555           0 : sql_rowid(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    3556             : {
    3557           0 :         BAT *b;
    3558           0 :         mvc *m = NULL;
    3559           0 :         str msg;
    3560           0 :         sql_schema *s = NULL;
    3561           0 :         sql_table *t = NULL;
    3562           0 :         sql_column *c = NULL;
    3563           0 :         oid *rid = getArgReference_oid(stk, pci, 0);
    3564           0 :         const char *sname = *getArgReference_str(stk, pci, 2);
    3565           0 :         const char *tname = *getArgReference_str(stk, pci, 3);
    3566             : 
    3567           0 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    3568             :                 return msg;
    3569           0 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    3570             :                 return msg;
    3571           0 :         s = mvc_bind_schema(m, sname);
    3572           0 :         if (s == NULL)
    3573           0 :                 throw(SQL, "calc.rowid", SQLSTATE(3F000) "Schema missing %s", sname);
    3574           0 :         t = mvc_bind_table(m, s, tname);
    3575           0 :         if (t == NULL)
    3576           0 :                 throw(SQL, "calc.rowid", SQLSTATE(42S02) "Table missing %s.%s",sname,tname);
    3577           0 :         if (!isTable(t))
    3578           0 :                 throw(SQL, "calc.rowid", SQLSTATE(42000) "%s '%s' is not persistent",
    3579           0 :                           TABLE_TYPE_DESCRIPTION(t->type, t->properties), t->base.name);
    3580           0 :         if (!ol_first_node(t->columns))
    3581           0 :                 throw(SQL, "calc.rowid", SQLSTATE(42S22) "Column missing %s.%s",sname,tname);
    3582           0 :         c = ol_first_node(t->columns)->data;
    3583             :         /* HACK, get insert bat */
    3584           0 :         sqlstore *store = m->session->tr->store;
    3585           0 :         b = store->storage_api.bind_col(m->session->tr, c, QUICK);
    3586           0 :         if( b == NULL)
    3587           0 :                 throw(SQL,"calc.rowid", SQLSTATE(HY005) "Cannot access column descriptor");
    3588             :         /* UGH (move into storage backends!!) */
    3589           0 :         *rid = BATcount(b);
    3590           0 :         return MAL_SUCCEED;
    3591             : }
    3592             : 
    3593             : static str
    3594           0 : do_sql_rank_grp(bat *rid, const bat *bid, const bat *gid, int nrank, int dense, const char *name)
    3595             : {
    3596           0 :         BAT *r, *b, *g;
    3597           0 :         BUN p, q;
    3598           0 :         BATiter bi, gi;
    3599           0 :         int (*ocmp) (const void *, const void *);
    3600           0 :         int (*gcmp) (const void *, const void *);
    3601           0 :         const void *oc, *gc, *on, *gn;
    3602           0 :         int rank = 1;
    3603           0 :         int c;
    3604             : 
    3605           0 :         if ((b = BATdescriptor(*bid)) == NULL)
    3606           0 :                 throw(SQL, name, SQLSTATE(HY005) "Cannot access column descriptor");
    3607           0 :         if ((g = BATdescriptor(*gid)) == NULL) {
    3608           0 :                 BBPunfix(b->batCacheid);
    3609           0 :                 throw(SQL, name, SQLSTATE(HY005) "Cannot access column descriptor");
    3610             :         }
    3611           0 :         bi = bat_iterator(b);
    3612           0 :         gi = bat_iterator(g);
    3613           0 :         ocmp = ATOMcompare(b->ttype);
    3614           0 :         gcmp = ATOMcompare(g->ttype);
    3615           0 :         oc = BUNtail(bi, 0);
    3616           0 :         gc = BUNtail(gi, 0);
    3617           0 :         if (!ALIGNsynced(b, g)) {
    3618           0 :                 bat_iterator_end(&bi);
    3619           0 :                 bat_iterator_end(&gi);
    3620           0 :                 BBPunfix(b->batCacheid);
    3621           0 :                 BBPunfix(g->batCacheid);
    3622           0 :                 throw(SQL, name, SQLSTATE(45000) "Internal error, columns not aligned");
    3623             :         }
    3624             : /*
    3625             :   if (!b->tsorted) {
    3626             :   BBPunfix(b->batCacheid);
    3627             :   BBPunfix(g->batCacheid);
    3628             :   throw(SQL, name, SQLSTATE(45000) "Internal error, columns not sorted");
    3629             :   }
    3630             : */
    3631           0 :         r = COLnew(b->hseqbase, TYPE_int, BATcount(b), TRANSIENT);
    3632           0 :         if (r == NULL) {
    3633           0 :                 bat_iterator_end(&bi);
    3634           0 :                 bat_iterator_end(&gi);
    3635           0 :                 BBPunfix(b->batCacheid);
    3636           0 :                 BBPunfix(g->batCacheid);
    3637           0 :                 throw(SQL, name, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3638             :         }
    3639           0 :         BATloop(b, p, q) {
    3640           0 :                 on = BUNtail(bi, p);
    3641           0 :                 gn = BUNtail(gi, p);
    3642             : 
    3643           0 :                 if ((c = ocmp(on, oc)) != 0)
    3644           0 :                         rank = nrank;
    3645           0 :                 if (gcmp(gn, gc) != 0)
    3646           0 :                         c = rank = nrank = 1;
    3647           0 :                 oc = on;
    3648           0 :                 gc = gn;
    3649           0 :                 if (BUNappend(r, &rank, false) != GDK_SUCCEED) {
    3650           0 :                         bat_iterator_end(&bi);
    3651           0 :                         bat_iterator_end(&gi);
    3652           0 :                         BBPunfix(b->batCacheid);
    3653           0 :                         BBPunfix(g->batCacheid);
    3654           0 :                         BBPunfix(r->batCacheid);
    3655           0 :                         throw(SQL, name, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3656             :                 }
    3657           0 :                 nrank += !dense || c;
    3658             :         }
    3659           0 :         bat_iterator_end(&bi);
    3660           0 :         bat_iterator_end(&gi);
    3661           0 :         BBPunfix(b->batCacheid);
    3662           0 :         BBPunfix(g->batCacheid);
    3663           0 :         *rid = r->batCacheid;
    3664           0 :         BBPkeepref(r);
    3665           0 :         return MAL_SUCCEED;
    3666             : }
    3667             : 
    3668             : static str
    3669           0 : do_sql_rank(bat *rid, const bat *bid, int nrank, int dense, const char *name)
    3670             : {
    3671           0 :         BAT *r, *b;
    3672           0 :         BATiter bi;
    3673           0 :         int (*cmp) (const void *, const void *);
    3674           0 :         const void *cur, *n;
    3675           0 :         BUN p, q;
    3676           0 :         int rank = 1;
    3677           0 :         int c;
    3678             : 
    3679           0 :         if ((b = BATdescriptor(*bid)) == NULL)
    3680           0 :                 throw(SQL, name, SQLSTATE(HY005) "Cannot access column descriptor");
    3681           0 :         bi = bat_iterator(b);
    3682           0 :         if (!bi.sorted && !bi.revsorted) {
    3683           0 :                 bat_iterator_end(&bi);
    3684           0 :                 BBPunfix(b->batCacheid);
    3685           0 :                 throw(SQL, name, SQLSTATE(45000) "Internal error, columns not sorted");
    3686             :         }
    3687             : 
    3688           0 :         cmp = ATOMcompare(bi.type);
    3689           0 :         cur = BUNtail(bi, 0);
    3690           0 :         r = COLnew(b->hseqbase, TYPE_int, BATcount(b), TRANSIENT);
    3691           0 :         if (r == NULL) {
    3692           0 :                 bat_iterator_end(&bi);
    3693           0 :                 BBPunfix(b->batCacheid);
    3694           0 :                 throw(SQL, name, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3695             :         }
    3696           0 :         if (BATtdensebi(&bi)) {
    3697           0 :                 BATloop(b, p, q) {
    3698           0 :                         if (BUNappend(r, &rank, false) != GDK_SUCCEED)
    3699           0 :                                 goto bailout;
    3700           0 :                         rank++;
    3701             :                 }
    3702             :         } else {
    3703           0 :                 BATloop(b, p, q) {
    3704           0 :                         n = BUNtail(bi, p);
    3705           0 :                         if ((c = cmp(n, cur)) != 0)
    3706           0 :                                 rank = nrank;
    3707           0 :                         cur = n;
    3708           0 :                         if (BUNappend(r, &rank, false) != GDK_SUCCEED)
    3709           0 :                                 goto bailout;
    3710           0 :                         nrank += !dense || c;
    3711             :                 }
    3712             :         }
    3713           0 :         bat_iterator_end(&bi);
    3714           0 :         BBPunfix(b->batCacheid);
    3715           0 :         *rid = r->batCacheid;
    3716           0 :         BBPkeepref(r);
    3717           0 :         return MAL_SUCCEED;
    3718           0 :   bailout:
    3719           0 :         bat_iterator_end(&bi);
    3720           0 :         BBPunfix(b->batCacheid);
    3721           0 :         BBPunfix(r->batCacheid);
    3722           0 :         throw(SQL, name, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3723             : }
    3724             : 
    3725             : str
    3726           0 : sql_rank_grp(bat *rid, const bat *bid, const bat *gid, const bat *gpe)
    3727             : {
    3728           0 :         (void) gpe;
    3729           0 :         return do_sql_rank_grp(rid, bid, gid, 1, 0, "sql.rank_grp");
    3730             : }
    3731             : 
    3732             : str
    3733           0 : sql_dense_rank_grp(bat *rid, const bat *bid, const bat *gid, const bat *gpe)
    3734             : {
    3735           0 :         (void) gpe;
    3736           0 :         return do_sql_rank_grp(rid, bid, gid, 2, 1, "sql.dense_rank_grp");
    3737             : }
    3738             : 
    3739             : str
    3740           0 : sql_rank(bat *rid, const bat *bid)
    3741             : {
    3742           0 :         return do_sql_rank(rid, bid, 1, 0, "sql.rank");
    3743             : }
    3744             : 
    3745             : str
    3746           0 : sql_dense_rank(bat *rid, const bat *bid)
    3747             : {
    3748           0 :         return do_sql_rank(rid, bid, 2, 1, "sql.dense_rank");
    3749             : }
    3750             : 
    3751             : str
    3752           5 : SQLargRecord(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    3753             : {
    3754           5 :         str s, t, *ret;
    3755             : 
    3756           5 :         (void) cntxt;
    3757           5 :         ret = getArgReference_str(stk, pci, 0);
    3758           5 :         s = instruction2str(mb, stk, getInstrPtr(mb, 0), LIST_MAL_CALL);
    3759           5 :         if(s == NULL)
    3760           0 :                 throw(SQL, "sql.argRecord", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3761           5 :         t = strchr(s, ' ');
    3762           5 :         if( ! t)
    3763           0 :                 t = strchr(s, '\t');
    3764           5 :         *ret = GDKstrdup(t ? t + 1 : s);
    3765           5 :         GDKfree(s);
    3766           5 :         if(*ret == NULL)
    3767           0 :                 throw(SQL, "sql.argRecord", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3768             :         return MAL_SUCCEED;
    3769             : }
    3770             : 
    3771             : /*
    3772             :  * The drop_hash operation cleans up any hash indices on any of the tables columns.
    3773             :  */
    3774             : str
    3775           0 : SQLdrop_hash(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    3776             : {
    3777           0 :         const char *sch = *getArgReference_str(stk, pci, 1);
    3778           0 :         const char *tbl = *getArgReference_str(stk, pci, 2);
    3779           0 :         sql_schema *s;
    3780           0 :         sql_table *t;
    3781           0 :         mvc *m = NULL;
    3782           0 :         str msg;
    3783             : 
    3784           0 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    3785             :                 return msg;
    3786           0 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    3787             :                 return msg;
    3788           0 :         s = mvc_bind_schema(m, sch);
    3789           0 :         if (s == NULL)
    3790           0 :                 throw(SQL, "sql.drop_hash", SQLSTATE(3F000) "Schema missing %s",sch);
    3791           0 :         if (!mvc_schema_privs(m, s))
    3792           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);
    3793           0 :         t = mvc_bind_table(m, s, tbl);
    3794           0 :         if (t == NULL)
    3795           0 :                 throw(SQL, "sql.drop_hash", SQLSTATE(42S02) "Table missing %s.%s",sch, tbl);
    3796           0 :         if (!isTable(t))
    3797           0 :                 throw(SQL, "sql.drop_hash", SQLSTATE(42000) "%s '%s' is not persistent",
    3798           0 :                           TABLE_TYPE_DESCRIPTION(t->type, t->properties), t->base.name);
    3799             : 
    3800           0 :         sqlstore *store = m->session->tr->store;
    3801           0 :         for (node *n = ol_first_node(t->columns); n; n = n->next) {
    3802           0 :                 sql_column *c = n->data;
    3803           0 :                 BAT *b = NULL, *nb = NULL;
    3804             : 
    3805           0 :                 if (!(b = store->storage_api.bind_col(m->session->tr, c, RDONLY)))
    3806           0 :                         throw(SQL, "sql.drop_hash", SQLSTATE(HY005) "Cannot access column descriptor");
    3807           0 :                 if (VIEWtparent(b) && (nb = BBP_desc(VIEWtparent(b)))) {
    3808           0 :                         BBPunfix(b->batCacheid);
    3809           0 :                         if (!(b = BATdescriptor(nb->batCacheid)))
    3810           0 :                                 throw(SQL, "sql.drop_hash", SQLSTATE(HY005) "Cannot access column descriptor");
    3811             :                 }
    3812           0 :                 HASHdestroy(b);
    3813           0 :                 BBPunfix(b->batCacheid);
    3814             :         }
    3815             :         return MAL_SUCCEED;
    3816             : }
    3817             : 
    3818             : /* after an update on the optimizer catalog, we have to change
    3819             :  * the internal optimizer pipe line administration
    3820             :  * The minimal and default pipelines may not be changed.
    3821             :  */
    3822             : str
    3823           0 : SQLoptimizersUpdate(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    3824             : {
    3825           0 :         mvc *m = NULL;
    3826           0 :         str msg;
    3827             : 
    3828           0 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    3829             :                 return msg;
    3830           0 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    3831             :                 return msg;
    3832             :         /* find the optimizer pipeline */
    3833           0 :         (void) stk;
    3834           0 :         (void) pci;
    3835           0 :         throw(SQL, "updateOptimizer", SQLSTATE(0A000) PROGRAM_NYI);
    3836             : }
    3837             : 
    3838             : /*
    3839             :  * Inspection of the actual storage footprint is a recurring question of users.
    3840             :  * This is modelled as a generic SQL table producing function.
    3841             :  * create function storage()
    3842             :  * 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)
    3843             :  * external name sql.storage;
    3844             :  */
    3845             : str
    3846         302 : sql_storage(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    3847             : {
    3848         302 :         BAT *sch, *tab, *col, *type, *loc, *cnt, *atom, *size, *heap, *indices, *phash, *sort, *imprints, *mode, *revsort, *key, *oidx, *bs = NULL;
    3849         302 :         BATiter bsi = bat_iterator(NULL);
    3850         302 :         mvc *m = NULL;
    3851         302 :         str msg = MAL_SUCCEED;
    3852         302 :         sql_trans *tr;
    3853         302 :         node *ncol;
    3854         302 :         int w;
    3855         302 :         bit bitval;
    3856         302 :         bat *rsch = getArgReference_bat(stk, pci, 0);
    3857         302 :         bat *rtab = getArgReference_bat(stk, pci, 1);
    3858         302 :         bat *rcol = getArgReference_bat(stk, pci, 2);
    3859         302 :         bat *rtype = getArgReference_bat(stk, pci, 3);
    3860         302 :         bat *rmode = getArgReference_bat(stk, pci, 4);
    3861         302 :         bat *rloc = getArgReference_bat(stk, pci, 5);
    3862         302 :         bat *rcnt = getArgReference_bat(stk, pci, 6);
    3863         302 :         bat *ratom = getArgReference_bat(stk, pci, 7);
    3864         302 :         bat *rsize = getArgReference_bat(stk, pci, 8);
    3865         302 :         bat *rheap = getArgReference_bat(stk, pci, 9);
    3866         302 :         bat *rindices = getArgReference_bat(stk, pci, 10);
    3867         302 :         bat *rphash = getArgReference_bat(stk, pci, 11);
    3868         302 :         bat *rimprints = getArgReference_bat(stk, pci, 12);
    3869         302 :         bat *rsort = getArgReference_bat(stk, pci, 13);
    3870         302 :         bat *rrevsort = getArgReference_bat(stk, pci, 14);
    3871         302 :         bat *rkey = getArgReference_bat(stk, pci, 15);
    3872         302 :         bat *roidx = getArgReference_bat(stk, pci, 16);
    3873         302 :         str sname = 0;
    3874         302 :         str tname = 0;
    3875         302 :         str cname = 0;
    3876         302 :         struct os_iter si = {0};
    3877             : 
    3878         302 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    3879             :                 return msg;
    3880         302 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    3881             :                 return msg;
    3882             : 
    3883         302 :         if( pci->argc - pci->retc >= 1) {
    3884          53 :                 sname = *getArgReference_str(stk, pci, pci->retc);
    3885          53 :                 if (strNil(sname))
    3886           0 :                         throw(SQL, "sql.storage", SQLSTATE(42000) "Schema name cannot be NULL");
    3887             :         }
    3888         302 :         if( pci->argc - pci->retc >= 2) {
    3889          53 :                 tname = *getArgReference_str(stk, pci, pci->retc + 1);
    3890          53 :                 if (strNil(tname))
    3891           0 :                         throw(SQL, "sql.storage", SQLSTATE(42000) "Table name cannot be NULL");
    3892             :         }
    3893         302 :         if( pci->argc - pci->retc >= 3) {
    3894          31 :                 cname = *getArgReference_str(stk, pci, pci->retc + 2);
    3895          31 :                 if (strNil(cname))
    3896           0 :                         throw(SQL, "sql.storage", SQLSTATE(42000) "Column name cannot be NULL");
    3897             :         }
    3898             : 
    3899         302 :         tr = m->session->tr;
    3900         302 :         sqlstore *store = tr->store;
    3901         302 :         sch = COLnew(0, TYPE_str, 0, TRANSIENT);
    3902         302 :         tab = COLnew(0, TYPE_str, 0, TRANSIENT);
    3903         302 :         col = COLnew(0, TYPE_str, 0, TRANSIENT);
    3904         302 :         type = COLnew(0, TYPE_str, 0, TRANSIENT);
    3905         302 :         mode = COLnew(0, TYPE_str, 0, TRANSIENT);
    3906         302 :         loc = COLnew(0, TYPE_str, 0, TRANSIENT);
    3907         302 :         cnt = COLnew(0, TYPE_lng, 0, TRANSIENT);
    3908         302 :         atom = COLnew(0, TYPE_int, 0, TRANSIENT);
    3909         302 :         size = COLnew(0, TYPE_lng, 0, TRANSIENT);
    3910         302 :         heap = COLnew(0, TYPE_lng, 0, TRANSIENT);
    3911         302 :         indices = COLnew(0, TYPE_lng, 0, TRANSIENT);
    3912         302 :         phash = COLnew(0, TYPE_bit, 0, TRANSIENT);
    3913         302 :         imprints = COLnew(0, TYPE_lng, 0, TRANSIENT);
    3914         302 :         sort = COLnew(0, TYPE_bit, 0, TRANSIENT);
    3915         302 :         revsort = COLnew(0, TYPE_bit, 0, TRANSIENT);
    3916         302 :         key = COLnew(0, TYPE_bit, 0, TRANSIENT);
    3917         302 :         oidx = COLnew(0, TYPE_lng, 0, TRANSIENT);
    3918             : 
    3919         302 :         if (sch == NULL || tab == NULL || col == NULL || type == NULL || mode == NULL || loc == NULL || imprints == NULL ||
    3920         302 :             sort == NULL || cnt == NULL || atom == NULL || size == NULL || heap == NULL || indices == NULL || phash == NULL ||
    3921         302 :             revsort == NULL || key == NULL || oidx == NULL) {
    3922           0 :                 msg = createException(SQL, "sql.storage", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3923           0 :                 goto bailout;
    3924             :         }
    3925             : 
    3926             :         /* check for limited storage tables */
    3927         302 :         os_iterator(&si, tr->cat->schemas, tr, NULL);
    3928        2492 :         for (sql_base *b = oi_next(&si); b; b = oi_next(&si)) {
    3929        2190 :                 sql_schema *s = (sql_schema *) b;
    3930        2190 :                 if ((sname && strcmp(b->name, sname) ) || b->name[0] == '%')
    3931         620 :                         continue;
    3932        1570 :                 if (s->tables) {
    3933        1570 :                         struct os_iter oi;
    3934             : 
    3935        1570 :                         os_iterator(&oi, s->tables, tr, NULL);
    3936       43457 :                         for (sql_base *bt = oi_next(&oi); bt; bt = oi_next(&oi)) {
    3937       41887 :                                 sql_table *t = (sql_table *) bt;
    3938       41887 :                                 if( tname && strcmp(bt->name, tname) )
    3939        6268 :                                         continue;
    3940       35619 :                                 if (isTable(t)) {
    3941       11217 :                                         if (ol_first_node(t->columns)) {
    3942       68375 :                                                 for (ncol = ol_first_node((t)->columns); ncol; ncol = ncol->next) {
    3943       57158 :                                                         sql_base *bc = ncol->data;
    3944       57158 :                                                         sql_column *c = (sql_column *) ncol->data;
    3945       57158 :                                                         lng sz;
    3946             : 
    3947       57158 :                                                         if( cname && strcmp(bc->name, cname) )
    3948          48 :                                                                 continue;
    3949       57110 :                                                         bs = store->storage_api.bind_col(tr, c, QUICK);
    3950       57110 :                                                         if (bs == NULL) {
    3951           0 :                                                                 msg = createException(SQL, "sql.storage", SQLSTATE(HY005) "Cannot access column descriptor");
    3952           0 :                                                                 goto bailout;
    3953             :                                                         }
    3954             : 
    3955       57110 :                                                         bsi = bat_iterator(bs);
    3956             :                                                         /*printf("schema %s.%s.%s" , b->name, bt->name, bc->name); */
    3957      114220 :                                                         if (BUNappend(sch, b->name, false) != GDK_SUCCEED ||
    3958      114220 :                                                             BUNappend(tab, bt->name, false) != GDK_SUCCEED ||
    3959       57110 :                                                             BUNappend(col, bc->name, false) != GDK_SUCCEED)
    3960           0 :                                                                 goto bailout1;
    3961       57110 :                                                         if (c->t->access == TABLE_WRITABLE) {
    3962       52375 :                                                                 if (BUNappend(mode, "writable", false) != GDK_SUCCEED)
    3963           0 :                                                                         goto bailout1;
    3964        4735 :                                                         } else if (c->t->access == TABLE_APPENDONLY) {
    3965           0 :                                                                 if (BUNappend(mode, "appendonly", false) != GDK_SUCCEED)
    3966           0 :                                                                         goto bailout1;
    3967        4735 :                                                         } else if (c->t->access == TABLE_READONLY) {
    3968        4735 :                                                                 if (BUNappend(mode, "readonly", false) != GDK_SUCCEED)
    3969           0 :                                                                         goto bailout1;
    3970             :                                                         } else {
    3971           0 :                                                                 if (BUNappend(mode, str_nil, false) != GDK_SUCCEED)
    3972           0 :                                                                         goto bailout1;
    3973             :                                                         }
    3974       57110 :                                                         if (BUNappend(type, c->type.type->base.name, false) != GDK_SUCCEED)
    3975           0 :                                                                 goto bailout1;
    3976             : 
    3977             :                                                         /*printf(" cnt "BUNFMT, bsi.count); */
    3978       57110 :                                                         sz = bsi.count;
    3979       57110 :                                                         if (BUNappend(cnt, &sz, false) != GDK_SUCCEED)
    3980           0 :                                                                 goto bailout1;
    3981             : 
    3982             :                                                         /*printf(" loc %s", BBP_physical(bs->batCacheid)); */
    3983       57110 :                                                         if (BUNappend(loc, BBP_physical(bs->batCacheid), false) != GDK_SUCCEED)
    3984           0 :                                                                 goto bailout1;
    3985             :                                                         /*printf(" width %d", bsi.width); */
    3986       57110 :                                                         w = bsi.width;
    3987       57110 :                                                         if (BUNappend(atom, &w, false) != GDK_SUCCEED)
    3988           0 :                                                                 goto bailout1;
    3989             : 
    3990       57110 :                                                         sz = bsi.count << bsi.shift;
    3991       57110 :                                                         if (BUNappend(size, &sz, false) != GDK_SUCCEED)
    3992           0 :                                                                 goto bailout1;
    3993             : 
    3994       57110 :                                                         sz = heapinfo(bs->tvheap, bs->batCacheid);
    3995       57110 :                                                         if (BUNappend(heap, &sz, false) != GDK_SUCCEED)
    3996           0 :                                                                 goto bailout1;
    3997             : 
    3998       57110 :                                                         MT_rwlock_rdlock(&bs->thashlock);
    3999       57110 :                                                         sz = hashinfo(bs->thash, bs->batCacheid);
    4000       57110 :                                                         MT_rwlock_rdunlock(&bs->thashlock);
    4001       57110 :                                                         if (BUNappend(indices, &sz, false) != GDK_SUCCEED)
    4002           0 :                                                                 goto bailout1;
    4003             : 
    4004       57110 :                                                         bitval = 0; /* HASHispersistent(bs); */
    4005       57110 :                                                         if (BUNappend(phash, &bitval, false) != GDK_SUCCEED)
    4006           0 :                                                                 goto bailout1;
    4007             : 
    4008       57110 :                                                         sz = IMPSimprintsize(bs);
    4009       57110 :                                                         if (BUNappend(imprints, &sz, false) != GDK_SUCCEED)
    4010           0 :                                                                 goto bailout1;
    4011             :                                                         /*printf(" indices "BUNFMT, bs->thash?bs->thash->heap.size:0); */
    4012             :                                                         /*printf("\n"); */
    4013       57110 :                                                         bitval = bsi.sorted;
    4014       57110 :                                                         if (!bitval && bsi.nosorted == 0)
    4015        2141 :                                                                 bitval = bit_nil;
    4016       57110 :                                                         if (BUNappend(sort, &bitval, false) != GDK_SUCCEED)
    4017           0 :                                                                 goto bailout1;
    4018             : 
    4019       57110 :                                                         bitval = bsi.revsorted;
    4020       57110 :                                                         if (!bitval && bsi.norevsorted == 0)
    4021        1711 :                                                                 bitval = bit_nil;
    4022       57110 :                                                         if (BUNappend(revsort, &bitval, false) != GDK_SUCCEED)
    4023           0 :                                                                 goto bailout1;
    4024             : 
    4025       57110 :                                                         bitval = BATtkey(bs);
    4026       57110 :                                                         if (!bitval && bsi.nokey[0] == 0 && bsi.nokey[1] == 0)
    4027       25791 :                                                                 bitval = bit_nil;
    4028       57110 :                                                         if (BUNappend(key, &bitval, false) != GDK_SUCCEED)
    4029           0 :                                                                 goto bailout1;
    4030             : 
    4031       57110 :                                                         sz = bs->torderidx && bs->torderidx != (Heap *) 1 ? bs->torderidx->free : 0;
    4032       57110 :                                                         if (BUNappend(oidx, &sz, false) != GDK_SUCCEED)
    4033           0 :                                                                 goto bailout1;
    4034       57110 :                                                         bat_iterator_end(&bsi);
    4035             :                                                 }
    4036             :                                         }
    4037             : 
    4038       11217 :                                         if (t->idxs) {
    4039       16201 :                                                 for (ncol = ol_first_node((t)->idxs); ncol; ncol = ncol->next) {
    4040        4984 :                                                         sql_base *bc = ncol->data;
    4041        4984 :                                                         sql_idx *c = (sql_idx *) ncol->data;
    4042        4984 :                                                         if (idx_has_column(c->type)) {
    4043        4978 :                                                                 bs = store->storage_api.bind_idx(tr, c, QUICK);
    4044        4978 :                                                                 lng sz;
    4045             : 
    4046        4978 :                                                                 if (bs == NULL) {
    4047           0 :                                                                         msg = createException(SQL, "sql.storage", SQLSTATE(HY005) "Cannot access column descriptor");
    4048           0 :                                                                         goto bailout;
    4049             :                                                                 }
    4050        4978 :                                                                 if( cname && strcmp(bc->name, cname) )
    4051          12 :                                                                         continue;
    4052        4966 :                                                                 bsi = bat_iterator(bs);
    4053             :                                                                 /*printf("schema %s.%s.%s" , b->name, bt->name, bc->name); */
    4054        9932 :                                                                 if (BUNappend(sch, b->name, false) != GDK_SUCCEED ||
    4055        9932 :                                                                     BUNappend(tab, bt->name, false) != GDK_SUCCEED ||
    4056        4966 :                                                                     BUNappend(col, bc->name, false) != GDK_SUCCEED)
    4057           0 :                                                                         goto bailout1;
    4058        4966 :                                                                 if (c->t->access == TABLE_WRITABLE) {
    4059         982 :                                                                         if (BUNappend(mode, "writable", false) != GDK_SUCCEED)
    4060           0 :                                                                                 goto bailout1;
    4061        3984 :                                                                 } else if (c->t->access == TABLE_APPENDONLY) {
    4062           0 :                                                                         if (BUNappend(mode, "appendonly", false) != GDK_SUCCEED)
    4063           0 :                                                                                 goto bailout1;
    4064        3984 :                                                                 } else if (c->t->access == TABLE_READONLY) {
    4065        3984 :                                                                         if (BUNappend(mode, "readonly", false) != GDK_SUCCEED)
    4066           0 :                                                                                 goto bailout1;
    4067             :                                                                 } else {
    4068           0 :                                                                         if (BUNappend(mode, str_nil, false) != GDK_SUCCEED)
    4069           0 :                                                                                 goto bailout1;
    4070             :                                                                 }
    4071        4966 :                                                                 if (BUNappend(type, "oid", false) != GDK_SUCCEED)
    4072           0 :                                                                         goto bailout1;
    4073             : 
    4074             :                                                                 /*printf(" cnt "BUNFMT, bsi.count); */
    4075        4966 :                                                                 sz = bsi.count;
    4076        4966 :                                                                 if (BUNappend(cnt, &sz, false) != GDK_SUCCEED)
    4077           0 :                                                                         goto bailout1;
    4078             : 
    4079             :                                                                 /*printf(" loc %s", BBP_physical(bs->batCacheid)); */
    4080        4966 :                                                                 if (BUNappend(loc, BBP_physical(bs->batCacheid), false) != GDK_SUCCEED)
    4081           0 :                                                                         goto bailout1;
    4082             :                                                                 /*printf(" width %d", bsi.width); */
    4083        4966 :                                                                 w = bsi.width;
    4084        4966 :                                                                 if (BUNappend(atom, &w, false) != GDK_SUCCEED)
    4085           0 :                                                                         goto bailout1;
    4086             :                                                                 /*printf(" size "BUNFMT, tailsize(bs,bsi.count) + (bs->tvheap? bs->tvheap->size:0)); */
    4087        4966 :                                                                 sz = tailsize(bs, bsi.count);
    4088        4966 :                                                                 if (BUNappend(size, &sz, false) != GDK_SUCCEED)
    4089           0 :                                                                         goto bailout1;
    4090             : 
    4091        4966 :                                                                 sz = bs->tvheap ? bs->tvheap->size : 0;
    4092        4966 :                                                                 if (BUNappend(heap, &sz, false) != GDK_SUCCEED)
    4093           0 :                                                                         goto bailout1;
    4094             : 
    4095        4966 :                                                                 MT_rwlock_rdlock(&bs->thashlock);
    4096        4966 :                                                                 sz = bs->thash && bs->thash != (Hash *) 1 ? bs->thash->heaplink.size + bs->thash->heapbckt.size : 0; /* HASHsize() */
    4097        4966 :                                                                 MT_rwlock_rdunlock(&bs->thashlock);
    4098        4966 :                                                                 if (BUNappend(indices, &sz, false) != GDK_SUCCEED)
    4099           0 :                                                                         goto bailout1;
    4100        4966 :                                                                 bitval = 0; /* HASHispersistent(bs); */
    4101        4966 :                                                                 if (BUNappend(phash, &bitval, false) != GDK_SUCCEED)
    4102           0 :                                                                         goto bailout1;
    4103             : 
    4104        4966 :                                                                 sz = IMPSimprintsize(bs);
    4105        4966 :                                                                 if (BUNappend(imprints, &sz, false) != GDK_SUCCEED)
    4106           0 :                                                                         goto bailout1;
    4107             :                                                                 /*printf(" indices "BUNFMT, bs->thash?bs->thash->heaplink.size+bs->thash->heapbckt.size:0); */
    4108             :                                                                 /*printf("\n"); */
    4109        4966 :                                                                 bitval = bsi.sorted;
    4110        4966 :                                                                 if (!bitval && bsi.nosorted == 0)
    4111           0 :                                                                         bitval = bit_nil;
    4112        4966 :                                                                 if (BUNappend(sort, &bitval, false) != GDK_SUCCEED)
    4113           0 :                                                                         goto bailout1;
    4114        4966 :                                                                 bitval = bsi.revsorted;
    4115        4966 :                                                                 if (!bitval && bsi.norevsorted == 0)
    4116           0 :                                                                         bitval = bit_nil;
    4117        4966 :                                                                 if (BUNappend(revsort, &bitval, false) != GDK_SUCCEED)
    4118           0 :                                                                         goto bailout1;
    4119        4966 :                                                                 bitval = BATtkey(bs);
    4120        4966 :                                                                 if (!bitval && bsi.nokey[0] == 0 && bsi.nokey[1] == 0)
    4121        4966 :                                                                         bitval = bit_nil;
    4122        4966 :                                                                 if (BUNappend(key, &bitval, false) != GDK_SUCCEED)
    4123           0 :                                                                         goto bailout1;
    4124        4966 :                                                                 sz = bs->torderidx && bs->torderidx != (Heap *) 1 ? bs->torderidx->free : 0;
    4125        4966 :                                                                 if (BUNappend(oidx, &sz, false) != GDK_SUCCEED)
    4126           0 :                                                                         goto bailout1;
    4127        4966 :                                                                 bat_iterator_end(&bsi);
    4128             :                                                         }
    4129             :                                                 }
    4130             :                                         }
    4131             :                                 }
    4132             :                         }
    4133             :                 }
    4134             :         }
    4135             : 
    4136         302 :         *rsch = sch->batCacheid;
    4137         302 :         BBPkeepref(sch);
    4138         302 :         *rtab = tab->batCacheid;
    4139         302 :         BBPkeepref(tab);
    4140         302 :         *rcol = col->batCacheid;
    4141         302 :         BBPkeepref(col);
    4142         302 :         *rmode = mode->batCacheid;
    4143         302 :         BBPkeepref(mode);
    4144         302 :         *rloc = loc->batCacheid;
    4145         302 :         BBPkeepref(loc);
    4146         302 :         *rtype = type->batCacheid;
    4147         302 :         BBPkeepref(type);
    4148         302 :         *rcnt = cnt->batCacheid;
    4149         302 :         BBPkeepref(cnt);
    4150         302 :         *ratom = atom->batCacheid;
    4151         302 :         BBPkeepref(atom);
    4152         302 :         *rsize = size->batCacheid;
    4153         302 :         BBPkeepref(size);
    4154         302 :         *rheap = heap->batCacheid;
    4155         302 :         BBPkeepref(heap);
    4156         302 :         *rindices = indices->batCacheid;
    4157         302 :         BBPkeepref(indices);
    4158         302 :         *rphash = phash->batCacheid;
    4159         302 :         BBPkeepref(phash);
    4160         302 :         *rimprints = imprints->batCacheid;
    4161         302 :         BBPkeepref(imprints);
    4162         302 :         *rsort = sort->batCacheid;
    4163         302 :         BBPkeepref(sort);
    4164         302 :         *rrevsort = revsort->batCacheid;
    4165         302 :         BBPkeepref(revsort);
    4166         302 :         *rkey = key->batCacheid;
    4167         302 :         BBPkeepref(key);
    4168         302 :         *roidx = oidx->batCacheid;
    4169         302 :         BBPkeepref(oidx);
    4170         302 :         return MAL_SUCCEED;
    4171             : 
    4172           0 :   bailout1:
    4173           0 :         bat_iterator_end(&bsi);
    4174           0 :   bailout:
    4175           0 :         BBPreclaim(sch);
    4176           0 :         BBPreclaim(tab);
    4177           0 :         BBPreclaim(col);
    4178           0 :         BBPreclaim(mode);
    4179           0 :         BBPreclaim(loc);
    4180           0 :         BBPreclaim(cnt);
    4181           0 :         BBPreclaim(type);
    4182           0 :         BBPreclaim(atom);
    4183           0 :         BBPreclaim(size);
    4184           0 :         BBPreclaim(heap);
    4185           0 :         BBPreclaim(indices);
    4186           0 :         BBPreclaim(phash);
    4187           0 :         BBPreclaim(imprints);
    4188           0 :         BBPreclaim(sort);
    4189           0 :         BBPreclaim(revsort);
    4190           0 :         BBPreclaim(key);
    4191           0 :         BBPreclaim(oidx);
    4192           0 :         if (!msg)
    4193           0 :                 msg = createException(SQL, "sql.storage", GDK_EXCEPTION);
    4194             :         return msg;
    4195             : }
    4196             : 
    4197             : void
    4198      592431 : freeVariables(Client c, MalBlkPtr mb, MalStkPtr glb, int oldvtop, int oldvid)
    4199             : {
    4200      661208 :         for (int i = oldvtop; i < mb->vtop;) {
    4201       68777 :                 if (glb) {
    4202           0 :                         if (isVarCleanup(mb, i))
    4203           0 :                                 garbageElement(c, &glb->stk[i]);
    4204             :                         /* clean stack entry */
    4205           0 :                         glb->stk[i].vtype = TYPE_int;
    4206           0 :                         glb->stk[i].val.ival = 0;
    4207           0 :                         glb->stk[i].len = 0;
    4208             :                 }
    4209       68777 :                 clearVariable(mb, i);
    4210       68777 :                 i++;
    4211             :         }
    4212      592431 :         assert(oldvtop <= mb->vsize);
    4213      592431 :         mb->vtop = oldvtop;
    4214      592431 :         mb->vid = oldvid;
    4215      592431 : }
    4216             : 
    4217             : str
    4218           0 : SQLresume_log_flushing(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    4219             : {
    4220           0 :         mvc *mvc;
    4221             : 
    4222           0 :         (void)stk; (void)pci;
    4223           0 :         char *msg = getSQLContext(cntxt, mb, &mvc, NULL);
    4224           0 :         if (msg)
    4225             :                 return msg;
    4226           0 :         store_resume_log(mvc->store);
    4227           0 :         return MAL_SUCCEED;
    4228             : }
    4229             : 
    4230             : str
    4231           0 : SQLsuspend_log_flushing(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    4232             : {
    4233           0 :         mvc *mvc;
    4234             : 
    4235           0 :         (void)stk; (void)pci;
    4236           0 :         char *msg = getSQLContext(cntxt, mb, &mvc, NULL);
    4237           0 :         if (msg)
    4238             :                 return msg;
    4239           0 :         store_suspend_log(mvc->store);
    4240           0 :         return MAL_SUCCEED;
    4241             : }
    4242             : 
    4243             : str
    4244             : /*SQLhot_snapshot(void *ret, const str *tarfile_arg [, bool onserver ])*/
    4245           5 : SQLhot_snapshot(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    4246             : {
    4247           5 :         char *filename;
    4248           5 :         bool onserver;
    4249           5 :         char *msg = MAL_SUCCEED;
    4250           5 :         char buf[80];
    4251           5 :         mvc *mvc;
    4252           5 :         ssize_t sz;
    4253           5 :         stream *s;
    4254           5 :         stream *cb = NULL;
    4255           5 :         lng result;
    4256             : 
    4257           5 :         filename = *getArgReference_str(stk, pci, 1);
    4258           5 :         onserver = pci->argc == 3 ? *getArgReference_bit(stk, pci, 2) : true;
    4259             : 
    4260           5 :         msg = getSQLContext(cntxt, mb, &mvc, NULL);
    4261           5 :         if (msg)
    4262             :                 return msg;
    4263             : 
    4264           5 :         sqlstore *store = mvc->session->tr->store;
    4265           5 :         if (onserver) {
    4266           5 :                 lng result = store_hot_snapshot(store, filename);
    4267           5 :                 if (result)
    4268             :                         return MAL_SUCCEED;
    4269             :                 else
    4270           0 :                         throw(SQL, "sql.hot_snapshot", GDK_EXCEPTION);
    4271             :         }
    4272             : 
    4273             :         // sync with client, copy pasted from mvc_export_table_wrap
    4274           0 :         while (!mvc->scanner.rs->eof)
    4275           0 :                 bstream_next(mvc->scanner.rs);
    4276             : 
    4277             :         // The snapshot code flushes from time to time.
    4278             :         // Use a callback stream to suppress those.
    4279           0 :         s = mvc->scanner.ws;
    4280           0 :         cb = callback_stream(
    4281             :                 /* private */ s,
    4282             :                 /* read */    NULL,
    4283             :                 /* write */   (void*)mnstr_write,
    4284             :                 /* close */   NULL,
    4285             :                 /* destroy */ NULL,
    4286             :                 "snapshot-callback"
    4287             :         );
    4288           0 :         if (!cb)
    4289           0 :                 throw(SQL, "sql.hot_snapshot", GDK_EXCEPTION);
    4290             : 
    4291             :         // tell client to open file, copy pasted from mvc_export_table_wrap
    4292           0 :         mnstr_write(s, PROMPT3, sizeof(PROMPT3) - 1, 1);
    4293           0 :         mnstr_printf(s, "wb %s\n", filename);
    4294           0 :         mnstr_flush(s, MNSTR_FLUSH_DATA);
    4295           0 :         if ((sz = mnstr_readline(mvc->scanner.rs->s, buf, sizeof(buf))) > 1) {
    4296             :                 /* non-empty line indicates failure on client */
    4297           0 :                 msg = createException(IO, "streams.open", "%s", buf);
    4298             :                         /* discard until client flushes */
    4299           0 :                         while (mnstr_read(mvc->scanner.rs->s, buf, 1, sizeof(buf)) > 0) {
    4300             :                                 /* ignore remainder of error message */
    4301           0 :                         }
    4302           0 :                 goto end;
    4303             :         }
    4304             : 
    4305             :         // client is waiting for data now, send it.
    4306           0 :         result = store_hot_snapshot_to_stream(store, cb);
    4307           0 :         if (result)
    4308             :                 msg = MAL_SUCCEED;
    4309             :         else
    4310           0 :                 msg = createException(SQL, "sql.hot_snapshot", GDK_EXCEPTION);
    4311           0 :         mnstr_destroy(cb);
    4312             : 
    4313             :         // tell client no more data, also copy pasted from mvc_export_table_wrap
    4314           0 :         mnstr_flush(s, MNSTR_FLUSH_DATA);
    4315           0 :         if ((sz = mnstr_readline(mvc->scanner.rs->s, buf, sizeof(buf))) > 1) {
    4316           0 :                 msg = createException(IO, "streams.open", "%s", buf);
    4317             :         }
    4318           0 :         while (sz > 0)
    4319           0 :                 sz = mnstr_readline(mvc->scanner.rs->s, buf, sizeof(buf));
    4320             : 
    4321           0 : end:
    4322             :         return msg;
    4323             : }
    4324             : 
    4325             : MT_Lock lock_persist_unlogged = MT_LOCK_INITIALIZER(lock_persist_unlogged);
    4326             : 
    4327             : str
    4328           2 : SQLpersist_unlogged(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    4329             : {
    4330           2 :         (void)stk;
    4331           2 :         (void)pci;
    4332             : 
    4333           2 :         assert(pci->argc == 5);
    4334             : 
    4335           2 :         bat *o0 = getArgReference_bat(stk, pci, 0),
    4336           2 :                 *o1 = getArgReference_bat(stk, pci, 1),
    4337           2 :                 *o2 = getArgReference_bat(stk, pci, 2);
    4338           2 :         str sname = *getArgReference_str(stk, pci, 3),
    4339           2 :                 tname = *getArgReference_str(stk, pci, 4),
    4340           2 :                 msg = MAL_SUCCEED;
    4341             : 
    4342           2 :         mvc *m = NULL;
    4343           2 :         msg = getSQLContext(cntxt, mb, &m, NULL);
    4344             : 
    4345           2 :         if (msg)
    4346             :                 return msg;
    4347             : 
    4348           2 :         sqlstore *store = store = m->session->tr->store;
    4349             : 
    4350           2 :         sql_schema *s = mvc_bind_schema(m, sname);
    4351           2 :         if (s == NULL)
    4352           0 :                 throw(SQL, "sql.persist_unlogged", SQLSTATE(3F000) "Schema missing %s.", sname);
    4353             : 
    4354           2 :         if (!mvc_schema_privs(m, s))
    4355           0 :                 throw(SQL, "sql.persist_unlogged", SQLSTATE(42000) "Access denied for %s to schema '%s'.",
    4356             :                           get_string_global_var(m, "current_user"), s->base.name);
    4357             : 
    4358           2 :         sql_table *t = mvc_bind_table(m, s, tname);
    4359           2 :         if (t == NULL)
    4360           0 :                 throw(SQL, "sql.persist_unlogged", SQLSTATE(42S02) "Table missing %s.%s", sname, tname);
    4361             : 
    4362           2 :         if (!isUnloggedTable(t) || t->access != TABLE_APPENDONLY)
    4363           0 :                 throw(SQL, "sql.persist_unlogged", "Unlogged and Insert Only mode combination required for table %s.%s", sname, tname);
    4364             : 
    4365           2 :         lng count = 0;
    4366             : 
    4367           2 :         sql_trans *tr = m->session->tr;
    4368           2 :         storage *t_del = bind_del_data(tr, t, NULL);
    4369             : 
    4370           2 :         BAT *d = BATdescriptor(t_del->cs.bid);
    4371             : 
    4372           2 :         if (t_del == NULL || d == NULL)
    4373           0 :                 throw(SQL, "sql.persist_unlogged", "Cannot access %s column storage.", tname);
    4374             : 
    4375           2 :         MT_lock_set(&lock_persist_unlogged);
    4376           2 :         BATiter d_bi = bat_iterator(d);
    4377             : 
    4378           2 :         if (BBP_status(d->batCacheid) & BBPEXISTING) {
    4379             : 
    4380           1 :                 assert(d->batInserted <= d_bi.count);
    4381             : 
    4382           1 :                 if (d->batInserted < d_bi.count) {
    4383             : 
    4384           1 :                         int n = 100;
    4385           1 :                         bat *commit_list = GDKzalloc(sizeof(bat) * (n + 1));
    4386           1 :                         BUN *sizes = GDKzalloc(sizeof(BUN) * (n + 1));
    4387             : 
    4388           1 :                         if (commit_list == NULL || sizes == NULL) {
    4389           0 :                                 MT_lock_unset(&lock_persist_unlogged);
    4390           0 :                                 GDKfree(commit_list);
    4391           0 :                                 GDKfree(sizes);
    4392           0 :                                 throw(SQL, "sql.persist_unlogged", SQLSTATE(HY001));
    4393             :                         }
    4394             : 
    4395           1 :                         commit_list[0] = 0;
    4396           1 :                         sizes[0] = 0;
    4397           1 :                         int i = 1;
    4398             : 
    4399           2 :                         for (node *ncol = ol_first_node(t->columns); ncol; ncol = ncol->next) {
    4400             : 
    4401           1 :                                 sql_column *c = (sql_column *) ncol->data;
    4402           1 :                                 BAT *b = store->storage_api.bind_col(tr, c, QUICK);
    4403             : 
    4404           1 :                                 if (b == NULL) {
    4405           0 :                                         MT_lock_unset(&lock_persist_unlogged);
    4406           0 :                                         GDKfree(commit_list);
    4407           0 :                                         GDKfree(sizes);
    4408           0 :                                         throw(SQL, "sql.persist_unlogged", "Cannot access column descriptor.");
    4409             :                                 }
    4410             : 
    4411           1 :                                 if (i == n && ncol->next) {
    4412           0 :                                         n = n * 2;
    4413           0 :                                         commit_list = GDKrealloc(commit_list, sizeof(bat) * n);
    4414           0 :                                         sizes = GDKrealloc(sizes, sizeof(BUN) * n);
    4415             :                                 }
    4416             : 
    4417           1 :                                 if (commit_list == NULL || sizes == NULL) {
    4418           0 :                                         MT_lock_unset(&lock_persist_unlogged);
    4419           0 :                                         GDKfree(commit_list);
    4420           0 :                                         GDKfree(sizes);
    4421           0 :                                         throw(SQL, "sql.persist_unlogged", SQLSTATE(HY001));
    4422             :                                 }
    4423             : 
    4424           1 :                                 commit_list[i] = b->batCacheid;
    4425           1 :                                 sizes[i] = d_bi.count;
    4426           1 :                                 i++;
    4427             :                         }
    4428             : 
    4429           1 :                         commit_list[i] = d->batCacheid;
    4430           1 :                         sizes[i] = d_bi.count;
    4431           1 :                         i++;
    4432             : 
    4433           1 :                         if (TMsubcommit_list(commit_list, sizes, i, -1, -1) != GDK_SUCCEED) {
    4434           0 :                                 MT_lock_unset(&lock_persist_unlogged);
    4435           0 :                                 GDKfree(commit_list);
    4436           0 :                                 GDKfree(sizes);
    4437           0 :                                 throw(SQL, "sql.persist_unlogged", "Lower level commit operation failed");
    4438             :                         }
    4439             : 
    4440           1 :                         GDKfree(commit_list);
    4441           1 :                         GDKfree(sizes);
    4442             :                 }
    4443             : 
    4444           1 :                 count = d_bi.count;
    4445             :         }
    4446             : 
    4447           2 :         bat_iterator_end(&d_bi);
    4448           2 :         MT_lock_unset(&lock_persist_unlogged);
    4449           2 :         BBPreclaim(d);
    4450             : 
    4451           2 :         BAT *table = COLnew(0, TYPE_str, 0, TRANSIENT),
    4452           2 :                 *tableid = COLnew(0, TYPE_int, 0, TRANSIENT),
    4453           2 :                 *rowcount = COLnew(0, TYPE_lng, 0, TRANSIENT);
    4454             : 
    4455           2 :         if (table == NULL || tableid == NULL || rowcount == NULL) {
    4456           0 :                 BBPnreclaim(3, table, tableid, rowcount);
    4457           0 :                 throw(SQL, "sql.persist_unlogged", SQLSTATE(HY001));
    4458             :         }
    4459             : 
    4460           4 :         if (BUNappend(table, tname, false) != GDK_SUCCEED ||
    4461           4 :                 BUNappend(tableid, &(t->base.id), false) != GDK_SUCCEED ||
    4462           2 :                 BUNappend(rowcount, &count, false) != GDK_SUCCEED) {
    4463           0 :                 BBPnreclaim(3, table, tableid, rowcount);
    4464           0 :                 throw(SQL, "sql.persist_unlogged", SQLSTATE(HY001));
    4465             :         }
    4466             : 
    4467           2 :         *o0 = table->batCacheid;
    4468           2 :         *o1 = tableid->batCacheid;
    4469           2 :         *o2 = rowcount->batCacheid;
    4470           2 :         BBPkeepref(table);
    4471           2 :         BBPkeepref(tableid);
    4472           2 :         BBPkeepref(rowcount);
    4473             : 
    4474           2 :         return msg;
    4475             : }
    4476             : 
    4477             : str
    4478           9 : SQLsession_prepared_statements(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    4479             : {
    4480           9 :         BAT *sessionid, *user, *statementid, *statement, *created;
    4481           9 :         bat *sid = getArgReference_bat(stk,pci,0);
    4482           9 :         bat *u = getArgReference_bat(stk,pci,1);
    4483           9 :         bat *i = getArgReference_bat(stk,pci,2);
    4484           9 :         bat *s = getArgReference_bat(stk,pci,3);
    4485           9 :         bat *c = getArgReference_bat(stk,pci,4);
    4486           9 :         str msg = MAL_SUCCEED;
    4487           9 :         mvc *sql = NULL;
    4488           9 :         cq *q = NULL;
    4489             : 
    4490           9 :         (void) stk;
    4491           9 :         (void) pci;
    4492           9 :         if ((msg = getSQLContext(cntxt, mb, &sql, NULL)) != NULL)
    4493             :                 return msg;
    4494           9 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    4495             :                 return msg;
    4496             : 
    4497           9 :         assert(sql->qc);
    4498             : 
    4499           9 :         sessionid = COLnew(0, TYPE_int, 256, TRANSIENT);
    4500           9 :         user = COLnew(0, TYPE_str, 256, TRANSIENT);
    4501           9 :         statementid = COLnew(0, TYPE_int, 256, TRANSIENT);
    4502           9 :         statement = COLnew(0, TYPE_str, 256, TRANSIENT);
    4503           9 :         created = COLnew(0, TYPE_timestamp, 256, TRANSIENT);
    4504           9 :         if (sessionid == NULL || user == NULL || statementid == NULL || statement == NULL || created == NULL) {
    4505           0 :                 msg = createException(SQL, "sql.session_prepared_statements", GDK_EXCEPTION);
    4506           0 :                 goto bailout;
    4507             :         }
    4508             : 
    4509          13 :         for (q = sql->qc->q; q; q = q->next) {
    4510           4 :                 gdk_return bun_res;
    4511           4 :                 if (BUNappend(sessionid, &(cntxt->idx), false) != GDK_SUCCEED) {
    4512           0 :                         msg = createException(SQL, "sql.session_prepared_statements", GDK_EXCEPTION);
    4513           0 :                         goto bailout;
    4514             :                 }
    4515             : 
    4516           4 :                 if (msg != MAL_SUCCEED)
    4517             :                         goto bailout;
    4518           4 :                 bun_res = BUNappend(user, cntxt->username, false);
    4519           4 :                 if (bun_res != GDK_SUCCEED) {
    4520           0 :                         msg = createException(SQL, "sql.session_prepared_statements", GDK_EXCEPTION);
    4521           0 :                         goto bailout;
    4522             :                 }
    4523             : 
    4524           4 :                 if (BUNappend(statementid, &(q->id), false) != GDK_SUCCEED) {
    4525           0 :                         msg = createException(SQL, "sql.session_prepared_statements", GDK_EXCEPTION);
    4526           0 :                         goto bailout;
    4527             :                 }
    4528           4 :                 if (BUNappend(statement, q->f->query, false) != GDK_SUCCEED) {
    4529           0 :                         msg = createException(SQL, "sql.session_prepared_statements", GDK_EXCEPTION);
    4530           0 :                         goto bailout;
    4531             :                 }
    4532           4 :                 if (BUNappend(created, &(q->created), false) != GDK_SUCCEED) {
    4533           0 :                         msg = createException(SQL, "sql.session_prepared_statements", GDK_EXCEPTION);
    4534           0 :                         goto bailout;
    4535             :                 }
    4536             :         }
    4537             : 
    4538           9 : bailout:
    4539           0 :         if (msg) {
    4540           0 :                 BBPreclaim(sessionid);
    4541           0 :                 BBPreclaim(user);
    4542           0 :                 BBPreclaim(statementid);
    4543           0 :                 BBPreclaim(statement);
    4544           0 :                 BBPreclaim(created);
    4545             :         } else {
    4546           9 :                 *sid = sessionid->batCacheid;
    4547           9 :                 BBPkeepref(sessionid);
    4548           9 :                 *u = user->batCacheid;
    4549           9 :                 BBPkeepref(user);
    4550           9 :                 *i = statementid->batCacheid;
    4551           9 :                 BBPkeepref(statementid);
    4552           9 :                 *s = statement->batCacheid;
    4553           9 :                 BBPkeepref(statement);
    4554           9 :                 *c = created->batCacheid;
    4555           9 :                 BBPkeepref(created);
    4556             :         }
    4557             :         return msg;
    4558             : }
    4559             : 
    4560             : str
    4561           6 : SQLsession_prepared_statements_args(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    4562             : {
    4563           6 :         BAT *statementid, *type, *digits, *isinout, *number, *scale, *schema, *table, *column;
    4564           6 :         bat *sid = getArgReference_bat(stk,pci,0);
    4565           6 :         bat *t = getArgReference_bat(stk,pci,1);
    4566           6 :         bat *d = getArgReference_bat(stk,pci,2);
    4567           6 :         bat *s = getArgReference_bat(stk,pci,3);
    4568           6 :         bat *io = getArgReference_bat(stk,pci,4);
    4569           6 :         bat *n = getArgReference_bat(stk,pci,5);
    4570           6 :         bat *sch = getArgReference_bat(stk,pci,6);
    4571           6 :         bat *tbl = getArgReference_bat(stk,pci,7);
    4572           6 :         bat *col = getArgReference_bat(stk,pci,8);
    4573           6 :         str msg = MAL_SUCCEED;
    4574           6 :         mvc *sql = NULL;
    4575           6 :         cq *q = NULL;
    4576             : 
    4577           6 :         (void) stk;
    4578           6 :         (void) pci;
    4579           6 :         if ((msg = getSQLContext(cntxt, mb, &sql, NULL)) != NULL)
    4580             :                 return msg;
    4581           6 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    4582             :                 return msg;
    4583             : 
    4584           6 :         assert(sql->qc);
    4585             : 
    4586           6 :         statementid = COLnew(0, TYPE_int, 256, TRANSIENT);
    4587           6 :         type = COLnew(0, TYPE_str, 256, TRANSIENT);
    4588           6 :         digits = COLnew(0, TYPE_int, 256, TRANSIENT);
    4589           6 :         scale = COLnew(0, TYPE_int, 256, TRANSIENT);
    4590           6 :         isinout = COLnew(0, TYPE_bte, 256, TRANSIENT);
    4591           6 :         number = COLnew(0, TYPE_int, 256, TRANSIENT);
    4592           6 :         schema = COLnew(0, TYPE_str, 256, TRANSIENT);
    4593           6 :         table = COLnew(0, TYPE_str, 256, TRANSIENT);
    4594           6 :         column = COLnew(0, TYPE_str, 256, TRANSIENT);
    4595           6 :         if (!statementid || !type || !digits || !scale || !isinout || !number || !schema || !table || !column) {
    4596           0 :                 msg = createException(SQL, "sql.session_prepared_statements_args", GDK_EXCEPTION);
    4597           0 :                 goto bailout;
    4598             :         }
    4599             : 
    4600           9 :         for (q = sql->qc->q; q; q = q->next) {
    4601           3 :                 sql_rel *r = q->rel;
    4602           3 :                 int arg_number = 0;
    4603           3 :                 bte inout = ARG_OUT;
    4604             : 
    4605           3 :                 if (r && (is_topn(r->op) || is_sample(r->op)))
    4606           0 :                         r = r->l;
    4607             : 
    4608           3 :                 if (r && is_project(r->op) && r->exps) {
    4609           6 :                         for (node *n = r->exps->h; n; n = n->next, arg_number++) {
    4610           3 :                                 sql_exp *e = n->data;
    4611           3 :                                 sql_subtype *t = exp_subtype(e);
    4612           3 :                                 const char *name = exp_name(e), *rname = exp_relname(e), *rschema = ATOMnilptr(TYPE_str);
    4613             : 
    4614           3 :                                 if (!name && e->type == e_column && e->r)
    4615           0 :                                         name = e->r;
    4616           0 :                                 if (!name)
    4617           0 :                                         name = ATOMnilptr(TYPE_str);
    4618           3 :                                 if (!rname && e->type == e_column && e->l)
    4619           0 :                                         rname = e->l;
    4620           0 :                                 if (!rname)
    4621           0 :                                         rname = ATOMnilptr(TYPE_str);
    4622             : 
    4623           6 :                                 if (BUNappend(statementid, &(q->id), false) != GDK_SUCCEED ||
    4624           6 :                                         BUNappend(type, t->type->base.name, false) != GDK_SUCCEED ||
    4625           6 :                                         BUNappend(digits, &t->digits, false) != GDK_SUCCEED ||
    4626           6 :                                         BUNappend(scale, &t->scale, false) != GDK_SUCCEED ||
    4627           6 :                                         BUNappend(isinout, &inout, false) != GDK_SUCCEED ||
    4628           6 :                                         BUNappend(number, &arg_number, false) != GDK_SUCCEED ||
    4629           6 :                                         BUNappend(schema, rschema, false) != GDK_SUCCEED ||
    4630           6 :                                         BUNappend(table, rname, false) != GDK_SUCCEED ||
    4631           3 :                                         BUNappend(column, name, false) != GDK_SUCCEED) {
    4632           0 :                                         msg = createException(SQL, "sql.session_prepared_statements_args", GDK_EXCEPTION);
    4633           0 :                                         goto bailout;
    4634             :                                 }
    4635             :                         }
    4636             :                 }
    4637             : 
    4638           3 :                 if (q->f->ops) {
    4639           3 :                         inout = ARG_IN;
    4640           6 :                         for (node *n = q->f->ops->h; n; n=n->next, arg_number++) {
    4641           3 :                                 sql_arg *a = n->data;
    4642           3 :                                 sql_subtype *t = &a->type;
    4643             : 
    4644           6 :                                 if (BUNappend(statementid, &(q->id), false) != GDK_SUCCEED ||
    4645           6 :                                         BUNappend(type, t->type->base.name, false) != GDK_SUCCEED ||
    4646           6 :                                         BUNappend(digits, &(t->digits), false) != GDK_SUCCEED ||
    4647           6 :                                         BUNappend(scale, &(t->scale), false) != GDK_SUCCEED ||
    4648           6 :                                         BUNappend(isinout, &inout, false) != GDK_SUCCEED ||
    4649           6 :                                         BUNappend(number, &arg_number, false) != GDK_SUCCEED ||
    4650           6 :                                         BUNappend(schema, ATOMnilptr(TYPE_str), false) != GDK_SUCCEED ||
    4651           6 :                                         BUNappend(table, ATOMnilptr(TYPE_str), false) != GDK_SUCCEED ||
    4652           3 :                                         BUNappend(column, ATOMnilptr(TYPE_str), false) != GDK_SUCCEED) {
    4653           0 :                                         msg = createException(SQL, "sql.session_prepared_statements_args", GDK_EXCEPTION);
    4654           0 :                                         goto bailout;
    4655             :                                 }
    4656             :                         }
    4657             :                 }
    4658             :         }
    4659             : 
    4660           6 : bailout:
    4661           0 :         if (msg) {
    4662           0 :                 BBPreclaim(statementid);
    4663           0 :                 BBPreclaim(type);
    4664           0 :                 BBPreclaim(digits);
    4665           0 :                 BBPreclaim(scale);
    4666           0 :                 BBPreclaim(isinout);
    4667           0 :                 BBPreclaim(number);
    4668           0 :                 BBPreclaim(schema);
    4669           0 :                 BBPreclaim(table);
    4670           0 :                 BBPreclaim(column);
    4671             :         } else {
    4672           6 :                 *sid = statementid->batCacheid;
    4673           6 :                 BBPkeepref(statementid);
    4674           6 :                 *t = type->batCacheid;
    4675           6 :                 BBPkeepref(type);
    4676           6 :                 *d = digits->batCacheid;
    4677           6 :                 BBPkeepref(digits);
    4678           6 :                 *s = scale->batCacheid;
    4679           6 :                 BBPkeepref(scale);
    4680           6 :                 *io = isinout->batCacheid;
    4681           6 :                 BBPkeepref(isinout);
    4682           6 :                 *n = number->batCacheid;
    4683           6 :                 BBPkeepref(number);
    4684           6 :                 *sch = schema->batCacheid;
    4685           6 :                 BBPkeepref(schema);
    4686           6 :                 *tbl = table->batCacheid;
    4687           6 :                 BBPkeepref(table);
    4688           6 :                 *col = column->batCacheid;
    4689           6 :                 BBPkeepref(column);
    4690             :         }
    4691             :         return msg;
    4692             : }
    4693             : 
    4694             : /* input id,row-input-values
    4695             :  * for each id call function(with row-input-values) return table
    4696             :  * return for each id the table, ie id (*length of table) and table results
    4697             :  */
    4698             : str
    4699          11 : SQLunionfunc(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    4700             : {
    4701          11 :         int arg = pci->retc;
    4702          11 :         str mod, fcn, ret = MAL_SUCCEED;
    4703          11 :         InstrPtr npci;
    4704          11 :         MalBlkPtr nmb = newMalBlk(1), omb = NULL;
    4705             : 
    4706          11 :         if (!nmb)
    4707           0 :                 return createException(MAL, "sql.unionfunc", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    4708          11 :         mod = *getArgReference_str(stk, pci, arg++);
    4709          11 :         fcn = *getArgReference_str(stk, pci, arg++);
    4710          11 :         npci = newStmtArgs(nmb, mod, fcn, pci->argc);
    4711          11 :         if (npci == NULL) {
    4712           0 :                 freeMalBlk(nmb);
    4713           0 :                 return createException(MAL, "sql.unionfunc", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    4714             :         }
    4715             : 
    4716          22 :         for (int i = 1; i < pci->retc; i++) {
    4717          11 :                 int type = getArgType(mb, pci, i);
    4718             : 
    4719          11 :                 if (i==1)
    4720          11 :                         getArg(npci, 0) = newTmpVariable(nmb, type);
    4721             :                 else
    4722           0 :                         npci = pushReturn(nmb, npci, newTmpVariable(nmb, type));
    4723             :         }
    4724          27 :         for (int i = pci->retc+2+1; i < pci->argc; i++) {
    4725          16 :                 int type = getBatType(getArgType(mb, pci, i));
    4726             : 
    4727          16 :                 npci = pushNil(nmb, npci, type);
    4728             :         }
    4729          11 :         pushInstruction(nmb, npci);
    4730             :         /* check program to get the proper malblk */
    4731          11 :         if (chkInstruction(cntxt->usermodule, nmb, npci)) {
    4732           0 :                 freeMalBlk(nmb);
    4733           0 :                 return createException(MAL, "sql.unionfunc", SQLSTATE(42000) PROGRAM_GENERAL);
    4734             :         }
    4735             : 
    4736          11 :         if (npci) {
    4737          11 :                 BAT **res = NULL, **input = NULL;
    4738          11 :                 BATiter *bi = NULL;
    4739          11 :                 BUN cnt = 0;
    4740          11 :                 int nrinput = pci->argc - 2 - pci->retc;
    4741          11 :                 MalStkPtr env = NULL;
    4742          11 :                 InstrPtr q = NULL;
    4743             : 
    4744          11 :                 if (!(input = GDKzalloc(sizeof(BAT*) * nrinput))) {
    4745           0 :                         ret = createException(MAL, "sql.unionfunc", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    4746           0 :                         goto finalize;
    4747             :                 }
    4748          11 :                 if (!(bi = GDKmalloc(sizeof(BATiter) * nrinput))) {
    4749           0 :                         ret = createException(MAL, "sql.unionfunc", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    4750           0 :                         goto finalize;
    4751             :                 }
    4752          11 :                 assert(pci->retc + 2 + nrinput == pci->argc);
    4753          38 :                 for (int i = 0, j = pci->retc+2; j < pci->argc; i++, j++) {
    4754          27 :                         bat *b = getArgReference_bat(stk, pci, j);
    4755          27 :                         if (!(input[i] = BATdescriptor(*b))) {
    4756           0 :                                 ret = createException(MAL, "sql.unionfunc", SQLSTATE(HY005) "Cannot access column descriptor");
    4757           0 :                                 while (i > 0) {
    4758           0 :                                         i--;
    4759           0 :                                         bat_iterator_end(&bi[i]);
    4760           0 :                                         BBPunfix(input[i]->batCacheid);
    4761             :                                 }
    4762           0 :                                 GDKfree(input);
    4763           0 :                                 input = NULL;
    4764           0 :                                 goto finalize;
    4765             :                         }
    4766          27 :                         bi[i] = bat_iterator(input[i]);
    4767          27 :                         cnt = BATcount(input[i]);
    4768             :                 }
    4769             : 
    4770             :                 /* create result bats */
    4771          11 :                 if (!(res = GDKzalloc(sizeof(BAT*) * pci->retc))) {
    4772           0 :                         ret = createException(MAL, "sql.unionfunc", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    4773           0 :                         goto finalize;
    4774             :                 }
    4775          33 :                 for (int i = 0; i<pci->retc; i++) {
    4776          22 :                         int type = getArgType(mb, pci, i);
    4777             : 
    4778          22 :                         if (!(res[i] = COLnew(0, getBatType(type), cnt, TRANSIENT))) {
    4779           0 :                                 ret = createException(MAL, "sql.unionfunc", GDK_EXCEPTION);
    4780           0 :                                 goto finalize;
    4781             :                         }
    4782             :                 }
    4783             : 
    4784          11 :                 if (npci->blk->stop > 1) {
    4785           8 :                         omb = nmb;
    4786           8 :                         if (!(nmb = copyMalBlk(npci->blk))) {
    4787           0 :                                 ret = createException(MAL, "sql.unionfunc", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    4788           0 :                                 goto finalize;
    4789             :                         }
    4790             :                 }
    4791          11 :                 if (!(env = prepareMALstack(nmb, nmb->vsize))) { /* needed for result */
    4792           0 :                         ret = createException(MAL, "sql.unionfunc", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    4793           0 :                         goto finalize;
    4794             :                 }
    4795          11 :                 q = getInstrPtr(nmb, 0);
    4796             : 
    4797          49 :                 for (BUN cur = 0; cur<cnt && !ret; cur++ ) {
    4798          38 :                         MalStkPtr nstk = prepareMALstack(nmb, nmb->vsize);
    4799          38 :                         int i,ii;
    4800             : 
    4801          38 :                         if (!nstk) { /* needed for result */
    4802           0 :                                 ret = createException(MAL, "sql.unionfunc", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    4803             :                         } else {
    4804             :                                 /* copy (input) arguments onto destination stack, skipping rowid col */
    4805          76 :                                 for (i = 1, ii = q->retc; ii < q->argc && !ret; ii++) {
    4806          38 :                                         ValPtr lhs = &nstk->stk[q->argv[ii]];
    4807          38 :                                         ptr rhs = (ptr)BUNtail(bi[i], cur);
    4808             : 
    4809          38 :                                         assert(lhs->vtype != TYPE_bat);
    4810          38 :                                         if (VALset(lhs, input[i]->ttype, rhs) == NULL)
    4811           0 :                                                 ret = createException(MAL, "sql.unionfunc", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    4812             :                                 }
    4813          38 :                                 if (!ret && ii == q->argc) {
    4814          38 :                                         BAT *fres = NULL;
    4815          38 :                                         if (!omb && npci->fcn)
    4816           0 :                                                 ret = (*(str (*)(Client, MalBlkPtr, MalStkPtr, InstrPtr))npci->fcn)(cntxt, nmb, nstk, npci);
    4817             :                                         else
    4818          38 :                                                 ret = runMALsequence(cntxt, nmb, 1, nmb->stop, nstk, env /* copy result in nstk first instruction*/, q);
    4819             : 
    4820          38 :                                         if (!ret) {
    4821             :                                                 /* insert into result */
    4822          38 :                                                 if (!(fres = BBPquickdesc(omb?env->stk[q->argv[0]].val.bval:nstk->stk[q->argv[0]].val.bval))) {
    4823           0 :                                                         ret = createException(MAL, "sql.unionfunc", SQLSTATE(HY005) "Cannot access column descriptor");
    4824             :                                                 } else {
    4825          38 :                                                         BAT *p = BATconstant(fres->hseqbase, res[0]->ttype, (ptr)BUNtail(bi[0], cur), BATcount(fres), TRANSIENT);
    4826             : 
    4827          38 :                                                         if (p) {
    4828          38 :                                                                 if (BATappend(res[0], p, NULL, FALSE) != GDK_SUCCEED)
    4829           0 :                                                                         ret = createException(MAL, "sql.unionfunc", GDK_EXCEPTION);
    4830          38 :                                                                 BBPunfix(p->batCacheid);
    4831             :                                                         } else {
    4832           0 :                                                                 ret = createException(MAL, "sql.unionfunc", GDK_EXCEPTION);
    4833             :                                                         }
    4834             :                                                 }
    4835             :                                                 i=1;
    4836          76 :                                                 for (ii = 0; i < pci->retc && !ret; i++) {
    4837          38 :                                                         BAT *b;
    4838             : 
    4839          38 :                                                         if (!(b = BATdescriptor(omb?env->stk[q->argv[ii]].val.bval:nstk->stk[q->argv[ii]].val.bval)))
    4840           0 :                                                                 ret = createException(MAL, "sql.unionfunc", SQLSTATE(HY005) "Cannot access column descriptor");
    4841          38 :                                                         else if (BATappend(res[i], b, NULL, FALSE) != GDK_SUCCEED)
    4842           0 :                                                                 ret = createException(MAL, "sql.unionfunc", GDK_EXCEPTION);
    4843          38 :                                                         if (b) {
    4844          38 :                                                                 BBPrelease(b->batCacheid); /* release ref from env stack */
    4845          38 :                                                                 BBPunfix(b->batCacheid);   /* free pointer */
    4846             :                                                         }
    4847             :                                                 }
    4848             :                                         }
    4849             :                                 }
    4850          38 :                                 GDKfree(nstk);
    4851             :                         }
    4852             :                 }
    4853          11 : finalize:
    4854          11 :                 GDKfree(env);
    4855          11 :                 if (nmb)
    4856          11 :                         freeMalBlk(nmb);
    4857          11 :                 if (omb)
    4858           8 :                         freeMalBlk(omb);
    4859          11 :                 if (res)
    4860          33 :                         for (int i = 0; i<pci->retc; i++) {
    4861          22 :                                 bat *b = getArgReference_bat(stk, pci, i);
    4862          22 :                                 if (res[i]) {
    4863          22 :                                         *b = res[i]->batCacheid;
    4864          22 :                                         if (ret)
    4865           0 :                                                 BBPunfix(*b);
    4866             :                                         else
    4867          22 :                                                 BBPkeepref(res[i]);
    4868             :                                 }
    4869             :                         }
    4870          11 :                 GDKfree(res);
    4871          11 :                 if (input) {
    4872          38 :                         for (int i = 0; i<nrinput; i++) {
    4873          27 :                                 if (input[i]) {
    4874          27 :                                         bat_iterator_end(&bi[i]);
    4875          27 :                                         BBPunfix(input[i]->batCacheid);
    4876             :                                 }
    4877             :                         }
    4878          11 :                         GDKfree(input);
    4879             :                 }
    4880          11 :                 GDKfree(bi);
    4881             :         }
    4882             :         return ret;
    4883             : }
    4884             : 
    4885             : static str
    4886           1 : do_str_column_vacuum(sql_trans *tr, sql_column *c, char *sname, char *tname, char *cname)
    4887             : {
    4888           1 :         int res;
    4889           1 :         int access = 0;
    4890           1 :         BAT* b = NULL;
    4891           1 :         BAT* bn = NULL;
    4892           1 :         sqlstore *store = tr->store;
    4893             : 
    4894           1 :         if ((b = store->storage_api.bind_col(tr, c, access)) == NULL)
    4895           0 :                 throw(SQL, "do_str_column_vacuum", SQLSTATE(42S22) "storage_api.bind_col failed for %s.%s.%s", sname, tname, cname);
    4896             :         // vacuum varsized bats
    4897           1 :         if (ATOMvarsized(c->type.type->localtype)) {
    4898             :                 // TODO check for num of updates on the BAT against some threshold
    4899             :                 // and decide whether to proceed
    4900           1 :                 if ((bn = COLcopy(b, b->ttype, true, PERSISTENT)) == NULL) {
    4901           0 :                         BBPunfix(b->batCacheid);
    4902           0 :                         throw(SQL, "do_str_column_vacuum", SQLSTATE(42S22) "COLcopy failed for %s.%s.%s", sname, tname, cname);
    4903             :                 }
    4904           1 :                 if ((res = (int) store->storage_api.swap_bats(tr, c, bn)) != LOG_OK) {
    4905           0 :                         BBPreclaim(bn);
    4906           0 :                         BBPunfix(b->batCacheid);
    4907           0 :                         if (res == LOG_CONFLICT)
    4908           0 :                                 throw(SQL, "do_str_column_vacuum", SQLSTATE(25S01) "TRANSACTION CONFLICT in storage_api.swap_bats %s.%s.%s", sname, tname, cname);
    4909           0 :                         if (res == LOG_ERR)
    4910           0 :                                 throw(SQL, "do_str_column_vacuum", SQLSTATE(HY000) "LOG ERROR in storage_api.swap_bats %s.%s.%s", sname, tname, cname);
    4911           0 :                         throw(SQL, "do_str_column_vacuum", SQLSTATE(HY000) "ERROR in storage_api.swap_bats %s.%s.%s", sname, tname, cname);
    4912             :                 }
    4913             :         }
    4914           1 :         BBPunfix(b->batCacheid);
    4915           1 :         BBPreclaim(bn);
    4916             :         return MAL_SUCCEED;
    4917             : }
    4918             : 
    4919             : str
    4920           1 : SQLstr_column_vacuum(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    4921             : {
    4922           1 :         mvc *m = NULL;
    4923           1 :         str msg = NULL;
    4924           1 :         char *sname = *getArgReference_str(stk, pci, 1);
    4925           1 :         char *tname = *getArgReference_str(stk, pci, 2);
    4926           1 :         char *cname = *getArgReference_str(stk, pci, 3);
    4927             : 
    4928           1 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    4929             :                 return msg;
    4930           1 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    4931             :                 return msg;
    4932             : 
    4933           1 :         sql_trans *tr = m->session->tr;
    4934           1 :         sql_schema *s = NULL;
    4935           1 :         sql_table *t = NULL;
    4936           1 :         sql_column *c = NULL;
    4937             : 
    4938           1 :         if (strNil(sname))
    4939           0 :                 throw(SQL, "sql.str_column_vacuum", SQLSTATE(42000) "Schema name cannot be NULL");
    4940           1 :         if (strNil(tname))
    4941           0 :                 throw(SQL, "sql.str_column_vacuum", SQLSTATE(42000) "Table name cannot be NULL");
    4942           1 :         if (strNil(cname))
    4943           0 :                 throw(SQL, "sql.str_column_vacuum", SQLSTATE(42000) "Column name cannot be NULL");
    4944           1 :         if ((s = mvc_bind_schema(m, sname)) == NULL)
    4945           0 :                 throw(SQL, "sql.str_column_vacuum", SQLSTATE(3F000) "Invalid or missing schema %s",sname);
    4946           1 :         if ((t = mvc_bind_table(m, s, tname)) == NULL)
    4947           0 :                 throw(SQL, "sql.str_column_vacuum", SQLSTATE(42S02) "Invalid or missing table %s.%s",sname,tname);
    4948           1 :         if (!isTable(t))
    4949           0 :                 throw(SQL, "sql.str_column_vacuum", SQLSTATE(42000) "%s '%s' is not persistent",
    4950           0 :                           TABLE_TYPE_DESCRIPTION(t->type, t->properties), t->base.name);
    4951           1 :         if (isTempTable(t))
    4952           0 :                 throw(SQL, "sql.str_column_vacuum", SQLSTATE(42000) "Cannot vacuum column from temporary table");
    4953           1 :         if ((c = mvc_bind_column(m, t, cname)) == NULL)
    4954           0 :                 throw(SQL, "sql.str_column_vacuum", SQLSTATE(42S22) "Column not found in %s.%s.%s",sname,tname,cname);
    4955           1 :         if (c->storage_type)
    4956           0 :                 throw(SQL, "sql.str_column_vacuum", SQLSTATE(42000) "Cannot vacuum compressed column");
    4957             : 
    4958           1 :         return do_str_column_vacuum(tr, c, sname, tname, cname);
    4959             : }
    4960             : 
    4961             : 
    4962             : static gdk_return
    4963           0 : str_column_vacuum_callback(int argc, void *argv[]) {
    4964           0 :         sqlstore *store = (sqlstore *) argv[0];
    4965           0 :         char *sname = (char *) argv[1];
    4966           0 :         char *tname = (char *) argv[2];
    4967           0 :         char *cname = (char *) argv[3];
    4968           0 :         sql_allocator *sa = NULL;
    4969           0 :         sql_session *session = NULL;
    4970           0 :         sql_schema *s = NULL;
    4971           0 :         sql_table *t = NULL;
    4972           0 :         sql_column *c = NULL;
    4973           0 :         char *msg;
    4974           0 :         gdk_return res = GDK_SUCCEED;
    4975             : 
    4976           0 :         (void) argc;
    4977             : 
    4978           0 :         if ((sa = sa_create(NULL)) == NULL) {
    4979           0 :                 TRC_ERROR((component_t) SQL, "[str_column_vacuum_callback] -- Failed to create sql_allocator!");
    4980           0 :                 return GDK_FAIL;
    4981             :         }
    4982             : 
    4983           0 :         if ((session = sql_session_create(store, sa, 0)) == NULL) {
    4984           0 :                 TRC_ERROR((component_t) SQL, "[str_column_vacuum_callback] -- Failed to create session!");
    4985           0 :                 sa_destroy(sa);
    4986           0 :                 return GDK_FAIL;
    4987             :         }
    4988             : 
    4989           0 :         if (sql_trans_begin(session) < 0) {
    4990           0 :                 TRC_ERROR((component_t) SQL, "[str_column_vacuum_callback] -- Failed to begin transaction!");
    4991           0 :                 sql_session_destroy(session);
    4992           0 :                 sa_destroy(sa);
    4993           0 :                 return GDK_FAIL;
    4994             :         }
    4995             : 
    4996           0 :         do {
    4997           0 :                 if((s = find_sql_schema(session->tr, sname)) == NULL) {
    4998           0 :                         TRC_ERROR((component_t) SQL, "[str_column_vacuum_callback] -- Invalid or missing schema %s!",sname);
    4999           0 :                         res = GDK_FAIL;
    5000           0 :                         break;
    5001             :                 }
    5002             : 
    5003           0 :                 if((t = find_sql_table(session->tr, s, tname)) == NULL) {
    5004           0 :                         TRC_ERROR((component_t) SQL, "[str_column_vacuum_callback] -- Invalid or missing table %s!", tname);
    5005           0 :                         res = GDK_FAIL;
    5006           0 :                         break;
    5007             :                 }
    5008             : 
    5009           0 :                 if ((c = find_sql_column(t, cname)) == NULL) {
    5010           0 :                         TRC_ERROR((component_t) SQL, "[str_column_vacuum_callback] -- Invalid or missing column %s!", cname);
    5011           0 :                         res = GDK_FAIL;
    5012           0 :                         break;
    5013             :                 }
    5014             : 
    5015           0 :                 if((msg=do_str_column_vacuum(session->tr, c, sname, tname, cname)) != MAL_SUCCEED) {
    5016           0 :                         TRC_ERROR((component_t) SQL, "[str_column_vacuum_callback] -- %s", msg);
    5017           0 :                         res = GDK_FAIL;
    5018             :                 }
    5019             : 
    5020             :         } while(0);
    5021             : 
    5022           0 :         if (res == GDK_SUCCEED) { /* everything is ok, do the commit route */
    5023           0 :                 switch (sql_trans_end(session, SQL_OK)) {
    5024           0 :                         case SQL_ERR:
    5025           0 :                                 TRC_ERROR((component_t) SQL, "[str_column_vacuum_callback] -- transaction commit failed (kernel error: %s)", GDKerrbuf);
    5026           0 :                                 res = GDK_FAIL;
    5027           0 :                                 break;
    5028           0 :                         case SQL_CONFLICT:
    5029           0 :                                 TRC_ERROR((component_t) SQL, "[str_column_vacuum_callback] -- transaction is aborted because of concurrency conflicts, will ROLLBACK instead");
    5030           0 :                                 res = GDK_FAIL;
    5031           0 :                                 break;
    5032             :                         default:
    5033             :                                 break;
    5034             :                 }
    5035             :         } else { /* an error triggered, rollback and ignore further errors */
    5036           0 :                 (void)sql_trans_end(session, SQL_ERR);
    5037             :         }
    5038             : 
    5039           0 :         sql_session_destroy(session);
    5040           0 :         sa_destroy(sa);
    5041           0 :         return res;
    5042             : }
    5043             : 
    5044             : static gdk_return
    5045           0 : str_column_vacuum_callback_args_free(int argc, void *argv[])
    5046             : {
    5047           0 :         (void) argc;
    5048             :         // free up sname, tname, cname. First pointer points to sqlstore so leave it.
    5049           0 :         GDKfree(argv[1]); // sname
    5050           0 :         GDKfree(argv[2]); // tname
    5051           0 :         GDKfree(argv[3]); // cname
    5052           0 :         return GDK_SUCCEED;
    5053             : }
    5054             : 
    5055             : str
    5056           0 : SQLstr_column_auto_vacuum(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    5057             : {
    5058           0 :         mvc *m = NULL;
    5059           0 :         str msg = NULL;
    5060           0 :         char *sname = *getArgReference_str(stk, pci, 1);
    5061           0 :         char *tname = *getArgReference_str(stk, pci, 2);
    5062           0 :         char *cname = *getArgReference_str(stk, pci, 3);
    5063           0 :         int interval = *getArgReference_int(stk, pci, 4); // in sec
    5064           0 :         char *sname_copy = NULL, *tname_copy = NULL, *cname_copy = NULL;
    5065             : 
    5066           0 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    5067             :                 return msg;
    5068           0 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    5069             :                 return msg;
    5070             : 
    5071           0 :         sql_schema *s = NULL;
    5072           0 :         sql_table *t = NULL;
    5073           0 :         sql_column *c = NULL;
    5074             : 
    5075           0 :         if (strNil(sname))
    5076           0 :                 throw(SQL, "sql.str_column_auto_vacuum", SQLSTATE(42000) "Schema name cannot be NULL");
    5077           0 :         if (strNil(tname))
    5078           0 :                 throw(SQL, "sql.str_column_auto_vacuum", SQLSTATE(42000) "Table name cannot be NULL");
    5079           0 :         if (strNil(cname))
    5080           0 :                 throw(SQL, "sql.str_column_auto_vacuum", SQLSTATE(42000) "Column name cannot be NULL");
    5081           0 :         if ((s = mvc_bind_schema(m, sname)) == NULL)
    5082           0 :                 throw(SQL, "sql.str_column_auto_vacuum", SQLSTATE(3F000) "Invalid or missing schema %s",sname);
    5083           0 :         if ((t = mvc_bind_table(m, s, tname)) == NULL)
    5084           0 :                 throw(SQL, "sql.str_column_auto_vacuum", SQLSTATE(42S02) "Invalid or missing table %s.%s",sname,tname);
    5085           0 :         if (!isTable(t))
    5086           0 :                 throw(SQL, "sql.str_column_auto_vacuum", SQLSTATE(42000) "%s '%s' is not persistent",
    5087           0 :                           TABLE_TYPE_DESCRIPTION(t->type, t->properties), t->base.name);
    5088           0 :         if (isTempTable(t))
    5089           0 :                 throw(SQL, "sql.str_column_auto_vacuum", SQLSTATE(42000) "Cannot vacuum column from temporary table");
    5090           0 :         if ((c = mvc_bind_column(m, t, cname)) == NULL)
    5091           0 :                 throw(SQL, "sql.str_column_auto_vacuum", SQLSTATE(42S22) "Column not found in %s.%s.%s",sname,tname,cname);
    5092           0 :         if (c->storage_type)
    5093           0 :                 throw(SQL, "sql.str_column_auto_vacuum", SQLSTATE(42000) "Cannot vacuum compressed column");
    5094             : 
    5095           0 :         if (!(sname_copy = GDKstrdup(sname)) || !(tname_copy = GDKstrdup(tname)) || !(cname_copy = GDKstrdup(cname))) {
    5096           0 :                 GDKfree(sname_copy);
    5097           0 :                 GDKfree(tname_copy);
    5098           0 :                 GDKfree(cname_copy);
    5099           0 :                 throw(SQL, "sql.str_column_auto_vacuum", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    5100             :         }
    5101           0 :         void *argv[4] = {m->store, sname_copy, tname_copy, cname_copy};
    5102             : 
    5103           0 :         if (gdk_add_callback("str_column_vacuum", str_column_vacuum_callback, 4, argv, interval) != GDK_SUCCEED) {
    5104           0 :                 str_column_vacuum_callback_args_free(4, argv);
    5105           0 :                 throw(SQL, "sql.str_column_auto_vacuum", "adding vacuum callback failed!");
    5106             :         }
    5107             : 
    5108             :         return MAL_SUCCEED;
    5109             : }
    5110             : 
    5111             : str
    5112           0 : SQLstr_column_stop_vacuum(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    5113             : {
    5114           0 :         mvc *m = NULL;
    5115           0 :         str msg = NULL;
    5116           0 :         char *sname = *getArgReference_str(stk, pci, 1);
    5117           0 :         char *tname = *getArgReference_str(stk, pci, 2);
    5118           0 :         char *cname = *getArgReference_str(stk, pci, 3);
    5119             : 
    5120           0 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    5121             :                 return msg;
    5122           0 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    5123             :                 return msg;
    5124             : 
    5125           0 :         sql_schema *s = NULL;
    5126           0 :         sql_table *t = NULL;
    5127           0 :         sql_column *c = NULL;
    5128             : 
    5129           0 :         if (strNil(sname))
    5130           0 :                 throw(SQL, "sql.str_column_stop_vacuum", SQLSTATE(42000) "Schema name cannot be NULL");
    5131           0 :         if (strNil(tname))
    5132           0 :                 throw(SQL, "sql.str_column_stop_vacuum", SQLSTATE(42000) "Table name cannot be NULL");
    5133           0 :         if (strNil(cname))
    5134           0 :                 throw(SQL, "sql.str_column_stop_vacuum", SQLSTATE(42000) "Column name cannot be NULL");
    5135           0 :         if ((s = mvc_bind_schema(m, sname)) == NULL)
    5136           0 :                 throw(SQL, "sql.str_column_stop_vacuum", SQLSTATE(3F000) "Invalid or missing schema %s",sname);
    5137           0 :         if ((t = mvc_bind_table(m, s, tname)) == NULL)
    5138           0 :                 throw(SQL, "sql.str_column_stop_vacuum", SQLSTATE(42S02) "Invalid or missing table %s.%s",sname,tname);
    5139           0 :         if (!isTable(t))
    5140           0 :                 throw(SQL, "sql.str_column_stop_vacuum", SQLSTATE(42000) "%s '%s' is not persistent",
    5141           0 :                           TABLE_TYPE_DESCRIPTION(t->type, t->properties), t->base.name);
    5142           0 :         if (isTempTable(t))
    5143           0 :                 throw(SQL, "sql.str_column_stop_vacuum", SQLSTATE(42000) "Cannot vacuum column from temporary table");
    5144           0 :         if ((c = mvc_bind_column(m, t, cname)) == NULL)
    5145           0 :                 throw(SQL, "sql.str_column_stop_vacuum", SQLSTATE(42S22) "Column not found in %s.%s.%s",sname,tname,cname);
    5146             : 
    5147           0 :         if(gdk_remove_callback("str_column_vacuum", str_column_vacuum_callback_args_free) != GDK_SUCCEED)
    5148           0 :                 throw(SQL, "sql.str_column_stop_vacuum", "removing vacuum callback failed!");
    5149             : 
    5150             :         return MAL_SUCCEED;
    5151             : }
    5152             : 
    5153             : 
    5154             : #include "sql_cat.h"
    5155             : #include "sql_rank.h"
    5156             : #include "sql_user.h"
    5157             : #include "sql_assert.h"
    5158             : #include "sql_execute.h"
    5159             : #include "sql_orderidx.h"
    5160             : #include "sql_strimps.h"
    5161             : #include "sql_subquery.h"
    5162             : #include "sql_statistics.h"
    5163             : #include "sql_transaction.h"
    5164             : #include "for.h"
    5165             : #include "dict.h"
    5166             : #include "mel.h"
    5167             : 
    5168             : 
    5169             : static str
    5170          42 : SQLuser_password(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    5171             : {
    5172          42 :         mvc *m = NULL;
    5173          42 :         str msg = NULL;
    5174          42 :         str *password = getArgReference_str(stk, pci, 0);
    5175          42 :         const char *username = *getArgReference_str(stk, pci, 1);
    5176             : 
    5177          42 :         (void) password;
    5178             : 
    5179          42 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    5180             :                 return msg;
    5181          42 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    5182             :                 return msg;
    5183          42 :         if (cntxt->username != username) {
    5184             :                 // only MAL_ADMIN and user himself can access password
    5185          42 :                 if ((msg = AUTHrequireAdmin(cntxt)) != MAL_SUCCEED)
    5186             :                         return msg;
    5187             :         }
    5188          42 :         *password = monet5_password_hash(m, username);
    5189          42 :         return MAL_SUCCEED;
    5190             : }
    5191             : 
    5192             : static str
    5193           8 : SQLdecypher(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    5194             : {
    5195           8 :         mvc *m = NULL;
    5196           8 :         str msg = NULL;
    5197           8 :         str *pwhash = getArgReference_str(stk, pci, 0);
    5198           8 :         const char *cypher = *getArgReference_str(stk, pci, 1);
    5199             : 
    5200           8 :         (void) pwhash;
    5201             : 
    5202           8 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    5203             :                 return msg;
    5204           8 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    5205             :                 return msg;
    5206           8 :         return AUTHdecypherValue(pwhash, cypher);
    5207             : }
    5208             : 
    5209             : static mel_func sql_init_funcs[] = {
    5210             :  pattern("sql", "shutdown", SQLshutdown_wrap, true, "", args(1,3, arg("",str),arg("delay",bte),arg("force",bit))),
    5211             :  pattern("sql", "shutdown", SQLshutdown_wrap, true, "", args(1,3, arg("",str),arg("delay",sht),arg("force",bit))),
    5212             :  pattern("sql", "shutdown", SQLshutdown_wrap, true, "", args(1,3, arg("",str),arg("delay",int),arg("force",bit))),
    5213             :  pattern("sql", "shutdown", SQLshutdown_wrap, true, "", args(1,2, arg("",str),arg("delay",bte))),
    5214             :  pattern("sql", "shutdown", SQLshutdown_wrap, true, "", args(1,2, arg("",str),arg("delay",sht))),
    5215             :  pattern("sql", "shutdown", SQLshutdown_wrap, true, "", args(1,2, arg("",str),arg("delay",int))),
    5216             :  pattern("sql", "set_protocol", SQLset_protocol, true, "Configures the result set protocol", args(1,2, arg("",int), arg("protocol",int))),
    5217             :  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))),
    5218             :  pattern("sql", "eval", SQLstatement, true, "Compile and execute a single sql statement", args(1,2, arg("",void),arg("cmd",str))),
    5219             :  pattern("sql", "eval", SQLstatement, true, "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))),
    5220             :  pattern("sql", "include", SQLinclude, true, "Compile and execute a sql statements on the file", args(1,2, arg("",void),arg("fname",str))),
    5221             :  pattern("sql", "evalAlgebra", RAstatement, true, "Compile and execute a single 'relational algebra' statement", args(1,3, arg("",void),arg("cmd",str),arg("optimize",bit))),
    5222             :  pattern("sql", "register", RAstatement2, true, "", args(1,5, arg("",int),arg("mod",str),arg("fname",str),arg("rel_stmt",str),arg("sig",str))),
    5223             :  pattern("sql", "register", RAstatement2, true, "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))),
    5224             :  pattern("sql", "deregister", RAstatementEnd, true, "Finish running transaction", args(1,1, arg("",int))),
    5225             :  pattern("sql", "hot_snapshot", SQLhot_snapshot, true, "Write db snapshot to the given tar(.gz) file", args(1,2, arg("",void),arg("tarfile",str))),
    5226             :  pattern("sql", "resume_log_flushing", SQLresume_log_flushing, true, "Resume WAL log flushing", args(1,1, arg("",void))),
    5227             :  pattern("sql", "suspend_log_flushing", SQLsuspend_log_flushing, true, "Suspend WAL log flushing", args(1,1, arg("",void))),
    5228             :  pattern("sql", "hot_snapshot", SQLhot_snapshot, 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))),
    5229             :  pattern("sql", "persist_unlogged", SQLpersist_unlogged, true, "Persist deltas on append only table in schema s table t", args(3, 5, batarg("table", str), batarg("table_id", int), batarg("rowcount", lng), arg("s", str), arg("t", str))),
    5230             :  pattern("sql", "assert", SQLassert, false, "Generate an exception when b==true", args(1,3, arg("",void),arg("b",bit),arg("msg",str))),
    5231             :  pattern("sql", "assert", SQLassertInt, false, "Generate an exception when b!=0", args(1,3, arg("",void),arg("b",int),arg("msg",str))),
    5232             :  pattern("sql", "assert", SQLassertLng, false, "Generate an exception when b!=0", args(1,3, arg("",void),arg("b",lng),arg("msg",str))),
    5233             :  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))),
    5234             :  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))),
    5235             :  pattern("sql", "logfile", mvc_logfile, true, "Enable/disable saving the sql statement traces", args(1,2, arg("",void),arg("filename",str))),
    5236             :  pattern("sql", "next_value", mvc_next_value, true, "return the next value of the sequence", args(1,3, arg("",lng),arg("sname",str),arg("sequence",str))),
    5237             :  pattern("batsql", "next_value", mvc_next_value_bulk, true, "return the next value of the sequence", args(1,4, batarg("",lng),arg("card",lng), arg("sname",str),arg("sequence",str))),
    5238             :  pattern("sql", "get_value", mvc_get_value, false, "return the current value of the sequence (ie the next to be used value)", args(1,3, arg("",lng),arg("sname",str),arg("sequence",str))),
    5239             :  pattern("batsql", "get_value", mvc_get_value_bulk, false, "return the current value of the sequence (ie the next to be used value)", args(1,3, batarg("",lng),batarg("sname",str),batarg("sequence",str))),
    5240             :  pattern("batsql", "get_value", mvc_get_value_bulk, false, "return the current value of the sequence (ie the next to be used value)", args(1,5, batarg("",lng),batarg("sname",str),batarg("sequence",str),batarg("s1",oid),batarg("s2",oid))),
    5241             :  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))),
    5242             :  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))),
    5243             :  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))),
    5244             :  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))),
    5245             :  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))),
    5246             :  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))),
    5247             :  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))),
    5248             :  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))),
    5249             :  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))),
    5250             :  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))),
    5251             :  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))),
    5252             :  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))),
    5253             :  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))),
    5254             :  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))),
    5255             :  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))),
    5256             :  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))),
    5257             :  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))),
    5258             :  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))),
    5259             :  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))),
    5260             :  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))),
    5261             :  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))),
    5262             :  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))),
    5263             :  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))),
    5264             :  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))),
    5265             :  command("sql", "getVersion", mvc_getVersion, false, "Return the database version identifier for a client.", args(1,2, arg("",lng),arg("clientid",int))),
    5266             :  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))),
    5267             :  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))),
    5268             :  pattern("sql", "depend", mvc_add_dependency_change, true, "Set dml dependency on current transaction for a table.", args(0,3, arg("sname",str),arg("tname",str),arg("cnt",lng))),
    5269             :  pattern("sql", "predicate", mvc_add_column_predicate, true, "Add predicate on current transaction for a table column.", args(0,3, arg("sname",str),arg("tname",str),arg("cname",str))),
    5270             :  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))),
    5271             :  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))),
    5272             :  pattern("sql", "clear_table", mvc_clear_table_wrap, true, "Clear the table sname.tname.", args(1,4, arg("",lng),arg("sname",str),arg("tname",str),arg("restart_sequences",int))),
    5273             :  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))),
    5274             :  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))),
    5275             :  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))),
    5276             :  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))),
    5277             :  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))),
    5278             :  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))),
    5279             :  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))),
    5280             :  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))),
    5281             :  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))),
    5282             :  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))),
    5283             :  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))),
    5284             :  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))),
    5285             :  pattern("sql", "exportOperation", mvc_export_operation_wrap, true, "Export result of schema/transaction queries", args(1,1, arg("",void))),
    5286             :  pattern("sql", "export_bin_column", mvc_bin_export_column_wrap, true, "export column as binary", args(1, 5, arg("", lng), batargany("col", 1), arg("byteswap", bit), arg("filename", str), arg("onclient", int))),
    5287             :  pattern("sql", "export_bin_column", mvc_bin_export_column_wrap, true, "export column as binary", args(1, 5, arg("", lng), argany("val", 1), arg("byteswap", bit), arg("filename", str), arg("onclient", int))),
    5288             :  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))),
    5289             :  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))),
    5290             :  //we use bat.single now
    5291             :  //pattern("sql", "single", CMDBATsingle, false, "", args(1,2, batargany("",2),argany("x",2))),
    5292             :  pattern("sql", "importColumn", mvc_bin_import_column_wrap, false, "Import a column from the given file", args(2, 8, batargany("", 0),arg("", oid), arg("method",str),arg("width",int),arg("bswap",bit),arg("path",str),arg("onclient",int),arg("nrows",oid))),
    5293             :  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))),
    5294             :  command("sql", "optimizers", getPipeCatalog, false, "", args(3,3, batarg("",str),batarg("",str),batarg("",str))),
    5295             :  pattern("sql", "optimizer_updates", SQLoptimizersUpdate, false, "", noargs),
    5296             :  pattern("sql", "argRecord", SQLargRecord, false, "Glue together the calling sequence", args(1,1, arg("",str))),
    5297             :  pattern("sql", "argRecord", SQLargRecord, false, "Glue together the calling sequence", args(1,2, arg("",str),varargany("a",0))),
    5298             :  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))),
    5299             :  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))),
    5300             : pattern("sql", "password", SQLuser_password, false, "Return password hash of user", args(1,2, arg("",str),arg("user",str))),
    5301             : pattern("sql", "decypher", SQLdecypher, false, "Return decyphered password", args(1,2, arg("",str),arg("hash",str))),
    5302             :  pattern("sql", "dump_cache", dump_cache, false, "dump the content of the query cache", args(2,2, batarg("query",str),batarg("count",int))),
    5303             :  pattern("sql", "dump_opt_stats", dump_opt_stats, false, "dump the optimizer rewrite statistics", args(2,2, batarg("rewrite",str),batarg("count",int))),
    5304             :  pattern("sql", "dump_trace", dump_trace, false, "dump the trace statistics", args(3,3, batarg("ticks",lng),batarg("stmt",str),batarg("stmt",str))),
    5305             :  pattern("sql", "analyze", sql_analyze, true, "Update statistics for every column in the database", args(1,1, arg("",void))),
    5306             :  pattern("sql", "analyze", sql_analyze, true, "Update statistics for schema", args(1,2, arg("",void),arg("sch",str))),
    5307             :  pattern("sql", "analyze", sql_analyze, true, "Update statistics for table", args(1,3, arg("",void),arg("sch",str),arg("tbl",str))),
    5308             :  pattern("sql", "analyze", sql_analyze, true, "Update statistics for column", args(1,4, arg("",void),arg("sch",str),arg("tbl",str),arg("col",str))),
    5309             :  pattern("sql", "set_count_distinct", sql_set_count_distinct, true, "Set count distinct for column", args(1,5, arg("",void),arg("sch",str),arg("tbl",str),arg("col",str),arg("val",lng))),
    5310             :  pattern("sql", "set_min", sql_set_min, true, "Set min for column", args(1,5, arg("",void),arg("sch",str),arg("tbl",str),arg("col",str),argany("val",1))),
    5311             :  pattern("sql", "set_max", sql_set_max, true, "Set max for column", args(1,5, arg("",void),arg("sch",str),arg("tbl",str),arg("col",str),argany("val",1))),
    5312             :  pattern("sql", "statistics", sql_statistics, false, "return a table with statistics information", args(13,13, batarg("columnid",int),batarg("schema",str),batarg("table",str),batarg("column",str),batarg("type",str),batarg("with",int),batarg("count",lng),batarg("unique",bit),batarg("nils",bit),batarg("minval",str),batarg("maxval",str),batarg("sorted",bit),batarg("revsorted",bit))),
    5313             :  pattern("sql", "statistics", sql_statistics, false, "return a table with statistics information for a particular schema", args(13,14, batarg("columnid",int),batarg("schema",str),batarg("table",str),batarg("column",str),batarg("type",str),batarg("with",int),batarg("count",lng),batarg("unique",bit),batarg("nils",bit),batarg("minval",str),batarg("maxval",str),batarg("sorted",bit),batarg("revsorted",bit),arg("sname",str))),
    5314             :  pattern("sql", "statistics", sql_statistics, false, "return a table with statistics information for a particular table", args(13,15, batarg("columnid",int),batarg("schema",str),batarg("table",str),batarg("column",str),batarg("type",str),batarg("with",int),batarg("count",lng),batarg("unique",bit),batarg("nils",bit),batarg("minval",str),batarg("maxval",str),batarg("sorted",bit),batarg("revsorted",bit),arg("sname",str),arg("tname",str))),
    5315             :  pattern("sql", "statistics", sql_statistics, false, "return a table with statistics information for a particular column", args(13,16, batarg("columnid",int),batarg("schema",str),batarg("table",str),batarg("column",str),batarg("type",str),batarg("with",int),batarg("count",lng),batarg("unique",bit),batarg("nils",bit),batarg("minval",str),batarg("maxval",str),batarg("sorted",bit),batarg("revsorted",bit),arg("sname",str),arg("tname",str),arg("cname",str))),
    5316             :  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))),
    5317             :  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))),
    5318             :  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))),
    5319             :  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))),
    5320             :  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))),
    5321             :  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))),
    5322             :  pattern("sql", "createstrimps", sql_createstrimps, true, "Instantiate the strimps index on a column", args(0,3, arg("sch",str),arg("tbl",str),arg("col",str))),
    5323             :  command("calc", "identity", SQLidentity, false, "Returns a unique row identitfier.", args(1,2, arg("",oid),argany("",0))),
    5324             :  command("batcalc", "identity", BATSQLidentity, false, "Returns the unique row identitfiers.", args(1,2, batarg("",oid),batargany("b",0))),
    5325             :  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))),
    5326             :  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))),
    5327             :  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))),
    5328             :  pattern("sql", "querylog_empty", sql_querylog_empty, true, "", noargs),
    5329             :  command("sql", "querylog_enable", QLOGenable, true, "", noargs),
    5330             :  command("sql", "querylog_enable", QLOGenableThreshold, true, "", args(0,1, arg("thres",int))),
    5331             :  command("sql", "querylog_disable", QLOGdisable, true, "", noargs),
    5332             :  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))),
    5333             :  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))),
    5334             :  pattern("sql", "copy_rejects", COPYrejects, false, "", args(4,4, batarg("rowid",lng),batarg("fldid",int),batarg("msg",str),batarg("inp",str))),
    5335             :  pattern("sql", "copy_rejects_clear", COPYrejects_clear, true, "", noargs),
    5336             :  pattern("for", "compress", FORcompress_col, false, "compress a sql column", args(0, 3, arg("schema", str), arg("table", str), arg("column", str))),
    5337             :  pattern("for", "decompress", FORdecompress, false, "decompress a for compressed (sub)column", args(1, 3, batargany("", 1), batargany("o", 0), argany("minval", 1))),
    5338             :  pattern("dict", "compress", DICTcompress, false, "dict compress a bat", args(2, 3, batargany("o", 0), batargany("v", 1), batargany("b", 1))),
    5339             :  pattern("dict", "compress", DICTcompress_col, false, "compress a sql column", args(0, 3, arg("schema", str), arg("table", str), arg("column", str))),
    5340             :  pattern("dict", "compress", DICTcompress_col, false, "compress a sql column", args(0, 4, arg("schema", str), arg("table", str), arg("column", str), arg("ordered", bit))),
    5341             :  pattern("dict", "decompress", DICTdecompress, false, "decompress a dictionary compressed (sub)column", args(1, 3, batargany("", 1), batargany("o", 0), batargany("u", 1))),
    5342             :  pattern("dict", "convert", DICTconvert, false, "convert candidate list into compressed offsets", args(1, 2, batargany("", 1), batargany("o", 0))),
    5343             :  pattern("dict", "join", DICTjoin, false, "join 2 dictionaries", args(2, 10, batarg("r0", oid), batarg("r1", oid), batargany("lo", 0), batargany("lv", 1), batargany("ro", 0), batargany("rv", 1), batarg("lc", oid), batarg("rc", oid), arg("nil_matches",bit), arg("estimate",lng))),
    5344             :  pattern("dict", "thetaselect", DICTthetaselect, false, "thetaselect on a dictionary", args(1, 6, batarg("r0", oid), batargany("lo", 0), batarg("lc", oid), batargany("lv", 1), argany("val",1), arg("op", str))),
    5345             :  pattern("dict", "renumber", DICTrenumber, false, "renumber offsets", args(1, 3, batargany("n", 1), batargany("o", 1), batargany("r", 1))),
    5346             :  pattern("dict", "select", DICTselect, false, "value - range select on a dictionary", args(1, 10, batarg("r0", oid), batargany("lo", 0), batarg("lc", oid), batargany("lv", 1), argany("l", 1), argany("h", 1), arg("li", bit), arg("hi", bit), arg("anti", bit),  arg("unknown", bit))),
    5347             :  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))),
    5348             :  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))),
    5349             :  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))),
    5350             :  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))),
    5351             :  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))),
    5352             :  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))),
    5353             :  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))),
    5354             :  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))),
    5355             :  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))),
    5356             :  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))),
    5357             :  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))),
    5358             :  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))),
    5359             :  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))),
    5360             :  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))),
    5361             :  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))),
    5362             :  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))),
    5363             :  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))),
    5364             :  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))),
    5365             :  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))),
    5366             :  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))),
    5367             :  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))),
    5368             :  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))),
    5369             :  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))),
    5370             :  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))),
    5371             :  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))),
    5372             :  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))),
    5373             :  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))),
    5374             :  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))),
    5375             :  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))),
    5376             :  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))),
    5377             :  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))),
    5378             :  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))),
    5379             :  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))),
    5380             :  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))),
    5381             :  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))),
    5382             :  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))),
    5383             :  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))),
    5384             :  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))),
    5385             :  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))),
    5386             :  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))),
    5387             :  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))),
    5388             :  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))),
    5389             :  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))),
    5390             :  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))),
    5391             :  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))),
    5392             :  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))),
    5393             :  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))),
    5394             :  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))),
    5395             :  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))),
    5396             :  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))),
    5397             :  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))),
    5398             :  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))),
    5399             :  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))),
    5400             :  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))),
    5401             :  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))),
    5402             :  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))),
    5403             :  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))),
    5404             :  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))),
    5405             :  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))),
    5406             :  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))),
    5407             :  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))),
    5408             :  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))),
    5409             :  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))),
    5410             :  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))),
    5411             :  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))),
    5412             :  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))),
    5413             :  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))),
    5414             :  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))),
    5415             :  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))),
    5416             :  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))),
    5417             :  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))),
    5418             :  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))),
    5419             :  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))),
    5420             :  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))),
    5421             :  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))),
    5422             :  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))),
    5423             :  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))),
    5424             :  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))),
    5425             :  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))),
    5426             :  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))),
    5427             :  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))),
    5428             :  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))),
    5429             :  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))),
    5430             :  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))),
    5431             :  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))),
    5432             :  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))),
    5433             :  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))),
    5434             :  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))),
    5435             :  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))),
    5436             :  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))),
    5437             :  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))),
    5438             :  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))),
    5439             :  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))),
    5440             :  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))),
    5441             :  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))),
    5442             :  command("batsql", "alpha", SQLbat_alpha_cst, false, "BAT implementation of astronomy alpha function", args(1,3, batarg("",dbl),batarg("dec",dbl),arg("theta",dbl))),
    5443             :  command("batsql", "alpha", SQLcst_alpha_bat, false, "BAT implementation of astronomy alpha function", args(1,3, batarg("",dbl),arg("dec",dbl),batarg("theta",dbl))),
    5444             :  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))),
    5445             :  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))),
    5446             :  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))),
    5447             :  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))),
    5448             :  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))),
    5449             :  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))),
    5450             :  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))),
    5451             :  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))),
    5452             :  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))),
    5453             :  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))),
    5454             :  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))),
    5455             :  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))),
    5456             :  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))),
    5457             :  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))),
    5458             :  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))),
    5459             :  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))),
    5460             :  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))),
    5461             :  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))),
    5462             :  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))),
    5463             :  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))),
    5464             :  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))),
    5465             :  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))),
    5466             :  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))),
    5467             :  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))),
    5468             :  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))),
    5469             :  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))),
    5470             :  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))),
    5471             :  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))),
    5472             :  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))),
    5473             :  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))),
    5474             :  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))),
    5475             :  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))),
    5476             :  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))),
    5477             :  pattern("sql", "current_time", SQLcurrent_daytime, false, "Get the clients current daytime", args(1,1, arg("",daytime))),
    5478             :  pattern("sql", "current_timestamp", SQLcurrent_timestamp, false, "Get the clients current timestamp", args(1,1, arg("",timestamp))),
    5479             :  pattern("calc", "date", nil_2_date, false, "cast to date", args(1,2, arg("",date),arg("v",void))),
    5480             :  pattern("batcalc", "date", nil_2_date, false, "cast to date", args(1,2, batarg("",date),batarg("v",oid))),
    5481             :  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))),
    5482             :  pattern("batcalc", "str", SQLbatstr_cast, false, "cast to string and check for overflow, no candidate list", args(1,7, batarg("",str),arg("eclass",int),arg("d1",int),arg("s1",int),arg("has_tz",int),batargany("v",1),arg("digits",int))),
    5483             :  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))),
    5484             :  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))),
    5485             :  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))),
    5486             :  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))),
    5487             :  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))),
    5488             :  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))),
    5489             :  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))),
    5490             :  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))),
    5491             :  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))),
    5492             :  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))),
    5493             :  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))),
    5494             :  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))),
    5495             :  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))),
    5496             :  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))),
    5497             :  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))),
    5498             :  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))),
    5499             :  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))),
    5500             :  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))),
    5501             :  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))),
    5502             :  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))),
    5503             :  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))),
    5504             :  pattern("calc", "rowid", sql_rowid, false, "return the next rowid", args(1,4, arg("",oid),argany("v",1),arg("schema",str),arg("table",str))),
    5505             :  pattern("sql", "drop_hash", SQLdrop_hash, true, "Drop hash indices for the given table", args(0,2, arg("sch",str),arg("tbl",str))),
    5506             :  pattern("sql", "prelude", SQLprelude, false, "", noargs),
    5507             :  command("sql", "epilogue", SQLepilogue, false, "", noargs),
    5508             :  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))),
    5509             :  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))),
    5510             :  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))),
    5511             :  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))),
    5512             :  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))),
    5513             :  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))),
    5514             :  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))),
    5515             :  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))),
    5516             :  pattern("sql", "update_tables", SYSupdate_tables, true, "Procedure triggered on update of the sys._tables table", args(1,1, arg("",void))),
    5517             :  pattern("sql", "update_schemas", SYSupdate_schemas, true, "Procedure triggered on update of the sys.schemas table", args(1,1, arg("",void))),
    5518             :  pattern("sql", "unionfunc", SQLunionfunc, false, "", args(1,4, varargany("",0),arg("mod",str),arg("fcn",str),varargany("",0))),
    5519             :  /* decimals */
    5520             :  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))),
    5521             :  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))),
    5522             :  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))),
    5523             :  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))),
    5524             :  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))),
    5525             :  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))),
    5526             :  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))),
    5527             :  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))),
    5528             :  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))),
    5529             :  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))),
    5530             :  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))),
    5531             :  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))),
    5532             :  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))),
    5533             :  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))),
    5534             :  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))),
    5535             :  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))),
    5536             :  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))),
    5537             :  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))),
    5538             :  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))),
    5539             :  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))),
    5540             :  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))),
    5541             :  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))),
    5542             :  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))),
    5543             :  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))),
    5544             :  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))),
    5545             :  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))),
    5546             :  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))),
    5547             :  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))),
    5548             :  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))),
    5549             :  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))),
    5550             :  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))),
    5551             :  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))),
    5552             :  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))),
    5553             :  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))),
    5554             :  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))),
    5555             :  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))),
    5556             :  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))),
    5557             :  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))),
    5558             :  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))),
    5559             :  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))),
    5560             :  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))),
    5561             :  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))),
    5562             :  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))),
    5563             :  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))),
    5564             :  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))),
    5565             :  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))),
    5566             :  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))),
    5567             :  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))),
    5568             :  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))),
    5569             :  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))),
    5570             :  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))),
    5571             :  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))),
    5572             :  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))),
    5573             :  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))),
    5574             :  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))),
    5575             :  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))),
    5576             :  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))),
    5577             :  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))),
    5578             :  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))),
    5579             :  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))),
    5580             :  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))),
    5581             :  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))),
    5582             :  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))),
    5583             :  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))),
    5584             :  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))),
    5585             :  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))),
    5586             :  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))),
    5587             :  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))),
    5588             :  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))),
    5589             :  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))),
    5590             :  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))),
    5591             :  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))),
    5592             :  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))),
    5593             :  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))),
    5594             :  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))),
    5595             :  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))),
    5596             :  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))),
    5597             :  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))),
    5598             :  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))),
    5599             :  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))),
    5600             :  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))),
    5601             :  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))),
    5602             :  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))),
    5603             :  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))),
    5604             :  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))),
    5605             :  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))),
    5606             :  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))),
    5607             :  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))),
    5608             :  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))),
    5609             :  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))),
    5610             :  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))),
    5611             :  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))),
    5612             :  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))),
    5613             :  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))),
    5614             :  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))),
    5615             :  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))),
    5616             :  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))),
    5617             :  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))),
    5618             :  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))),
    5619             :  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))),
    5620             :  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))),
    5621             :  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))),
    5622             :  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))),
    5623             :  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))),
    5624             :  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))),
    5625             :  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))),
    5626             :  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))),
    5627             :  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))),
    5628             :  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))),
    5629             :  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))),
    5630             :  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))),
    5631             :  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))),
    5632             :  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))),
    5633             :  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))),
    5634             :  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))),
    5635             :  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))),
    5636             :  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))),
    5637             :  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))),
    5638             :  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))),
    5639             :  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))),
    5640             :  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))),
    5641             :  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))),
    5642             :  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))),
    5643             :  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))),
    5644             :  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))),
    5645             :  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))),
    5646             :  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))),
    5647             :  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))),
    5648             :  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))),
    5649             :  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))),
    5650             :  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))),
    5651             :  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))),
    5652             :  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))),
    5653             :  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))),
    5654             :  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))),
    5655             :  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))),
    5656             :  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))),
    5657             :  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))),
    5658             :  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))),
    5659             :  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))),
    5660             :  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))),
    5661             :  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))),
    5662             :  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))),
    5663             :  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))),
    5664             :  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))),
    5665             :  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))),
    5666             :  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))),
    5667             :  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))),
    5668             :  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))),
    5669             :  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))),
    5670             :  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))),
    5671             :  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))),
    5672             :  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))),
    5673             :  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))),
    5674             :  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))),
    5675             :  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))),
    5676             :  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))),
    5677             :  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))),
    5678             :  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))),
    5679             :  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))),
    5680             :  /* sql_rank */
    5681             :  pattern("sql", "diff", SQLdiff, false, "return true if cur != prev row", args(1,2, arg("",bit),argany("b",1))),
    5682             :  pattern("batsql", "diff", SQLdiff, false, "return true if cur != prev row", args(1,2, batarg("",bit),batargany("b",1))),
    5683             :  pattern("sql", "diff", SQLdiff, false, "return true if cur != prev row", args(1,3, arg("",bit),arg("p",bit),argany("b",1))),
    5684             :  pattern("batsql", "diff", SQLdiff, false, "return true if cur != prev row", args(1,3, batarg("",bit),arg("p",bit),batargany("b",1))),
    5685             :  pattern("batsql", "diff", SQLdiff, false, "return true if cur != prev row", args(1,3, batarg("",bit),batarg("p",bit),argany("b",1))),
    5686             :  pattern("batsql", "diff", SQLdiff, false, "return true if cur != prev row", args(1,3, batarg("",bit),batarg("p",bit),batargany("b",1))),
    5687             :  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))),
    5688             :  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))),
    5689             :  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))),
    5690             :  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))),
    5691             :  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))),
    5692             :  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))),
    5693             :  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))),
    5694             :  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))),
    5695             :  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))),
    5696             :  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))),
    5697             :  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))),
    5698             :  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))),
    5699             :  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))),
    5700             :  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))),
    5701             :  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))),
    5702             :  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))),
    5703             :  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))),
    5704             :  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))),
    5705             :  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))),
    5706             :  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))),
    5707             :  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))),
    5708             :  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))),
    5709             :  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))),
    5710             :  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))),
    5711             :  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))),
    5712             :  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))),
    5713             :  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))),
    5714             :  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))),
    5715             :  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))),
    5716             :  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))),
    5717             :  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))),
    5718             :  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))),
    5719             :  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))),
    5720             :  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))),
    5721             :  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))),
    5722             :  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))),
    5723             :  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))),
    5724             :  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))),
    5725             :  pattern("sql", "rank", SQLrank, false, "return the ranked groups", args(1,4, arg("",int),argany("b",1),arg("p",bit),arg("o",bit))),
    5726             :  pattern("batsql", "rank", SQLrank, false, "return the ranked groups", args(1,4, batarg("",int),batargany("b",1),argany("p",2),argany("o",3))),
    5727             :  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))),
    5728             :  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))),
    5729             :  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))),
    5730             :  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))),
    5731             :  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))),
    5732             :  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))),
    5733             :  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))),
    5734             :  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))),
    5735             :  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))),
    5736             :  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))),
    5737             :  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))),
    5738             :  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))),
    5739             :  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))),
    5740             :  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))),
    5741             :  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))),
    5742             :  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))),
    5743             :  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))),
    5744             :  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))),
    5745             :  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))),
    5746             :  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))),
    5747             :  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))),
    5748             :  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))),
    5749             :  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))),
    5750             :  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))),
    5751             :  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))),
    5752             :  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))),
    5753             :  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))),
    5754             :  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))),
    5755             :  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))),
    5756             :  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))),
    5757             :  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))),
    5758             :  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))),
    5759             :  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))),
    5760             :  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))),
    5761             :  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))),
    5762             :  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))),
    5763             :  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))),
    5764             :  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))),
    5765             : 
    5766             :  /* these window functions support frames */
    5767             :  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))),
    5768             :  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))),
    5769             :  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))),
    5770             :  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))),
    5771             :  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))),
    5772             :  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))),
    5773             :  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))),
    5774             :  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))),
    5775             :  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))),
    5776             :  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))),
    5777             :  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))),
    5778             :  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))),
    5779             :  pattern("sql", "count", SQLbasecount, false, "return count of basetable", args(1,3, arg("",lng),arg("sname",str),arg("tname",str))),
    5780             :  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))),
    5781             :  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))),
    5782             :  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))),
    5783             :  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))),
    5784             :  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))),
    5785             :  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))),
    5786             :  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))),
    5787             :  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))),
    5788             :  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))),
    5789             :  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))),
    5790             :  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))),
    5791             :  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))),
    5792             :  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))),
    5793             :  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))),
    5794             :  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))),
    5795             :  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))),
    5796             :  /* sql.sum for month intervals */
    5797             :  pattern("sql", "sum", SQLsum, false, "return the sum 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))),
    5798             :  pattern("batsql", "sum", SQLsum, false, "return the sum 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))),
    5799             :  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))),
    5800             :  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))),
    5801             :  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))),
    5802             :  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))),
    5803             :  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))),
    5804             :  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))),
    5805             :  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))),
    5806             :  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))),
    5807             :  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))),
    5808             :  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))),
    5809             :  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))),
    5810             :  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))),
    5811             :  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))),
    5812             :  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))),
    5813             :  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))),
    5814             :  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))),
    5815             :  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))),
    5816             :  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))),
    5817             :  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))),
    5818             :  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))),
    5819             :  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))),
    5820             :  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))),
    5821             :  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))),
    5822             :  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))),
    5823             :  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))),
    5824             :  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))),
    5825             :  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))),
    5826             :  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))),
    5827             :  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))),
    5828             :  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))),
    5829             :  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))),
    5830             :  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))),
    5831             :  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))),
    5832             :  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))),
    5833             :  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))),
    5834             :  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))),
    5835             :  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))),
    5836             :  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))),
    5837             :  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))),
    5838             :  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))),
    5839             :  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))),
    5840             :  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))),
    5841             :  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))),
    5842             :  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))),
    5843             :  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))),
    5844             :  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))),
    5845             :  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))),
    5846             :  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))),
    5847             :  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))),
    5848             :  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))),
    5849             :  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))),
    5850             :  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))),
    5851             :  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))),
    5852             :  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))),
    5853             :  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))),
    5854             :  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))),
    5855             :  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))),
    5856             :  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))),
    5857             :  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))),
    5858             :  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))),
    5859             :  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))),
    5860             :  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))),
    5861             :  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))),
    5862             :  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))),
    5863             :  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))),
    5864             :  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))),
    5865             :  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))),
    5866             :  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))),
    5867             :  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))),
    5868             :  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))),
    5869             :  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))),
    5870             :  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))),
    5871             :  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))),
    5872             :  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))),
    5873             :  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))),
    5874             :  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))),
    5875             :  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))),
    5876             :  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))),
    5877             :  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))),
    5878             :  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))),
    5879             :  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))),
    5880             :  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))),
    5881             :  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))),
    5882             :  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))),
    5883             :  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))),
    5884             :  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))),
    5885             :  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))),
    5886             :  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))),
    5887             :  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))),
    5888             :  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))),
    5889             :  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))),
    5890             :  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))),
    5891             :  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))),
    5892             :  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))),
    5893             :  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))),
    5894             :  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))),
    5895             :  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))),
    5896             :  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))),
    5897             :  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))),
    5898             :  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))),
    5899             :  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))),
    5900             :  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))),
    5901             :  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))),
    5902             :  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))),
    5903             :  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))),
    5904             :  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))),
    5905             :  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))),
    5906             :  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))),
    5907             :  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))),
    5908             :  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))),
    5909             :  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))),
    5910             :  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))),
    5911             :  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))),
    5912             :  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))),
    5913             :  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))),
    5914             :  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))),
    5915             :  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))),
    5916             :  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))),
    5917             :  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))),
    5918             :  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))),
    5919             :  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))),
    5920             :  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))),
    5921             :  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))),
    5922             :  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))),
    5923             :  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))),
    5924             :  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))),
    5925             :  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))),
    5926             :  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))),
    5927             :  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))),
    5928             :  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))),
    5929             :  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))),
    5930             :  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))),
    5931             :  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))),
    5932             :  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))),
    5933             :  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))),
    5934             :  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))),
    5935             :  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))),
    5936             :  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))),
    5937             :  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))),
    5938             :  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))),
    5939             :  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))),
    5940             :  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))),
    5941             :  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))),
    5942             :  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))),
    5943             :  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))),
    5944             :  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))),
    5945             :  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))),
    5946             :  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))),
    5947             :  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))),
    5948             :  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))),
    5949             :  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))),
    5950             :  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))),
    5951             :  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))),
    5952             :  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))),
    5953             :  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))),
    5954             :  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))),
    5955             :  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))),
    5956             :  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))),
    5957             :  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))),
    5958             :  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))),
    5959             :  /* sql_subquery */
    5960             :  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))),
    5961             :  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))),
    5962             :  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))),
    5963             :  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))),
    5964             :  command("aggr", "all", SQLall, false, "if all values in b are equal return this, else nil", args(1,2, argany("",1),batargany("b",1))),
    5965             :  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))),
    5966             :  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))),
    5967             :  command("aggr", "null", SQLnil, false, "if b has a nil return true, else false", args(1,2, arg("",bit),batargany("b",1))),
    5968             :  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))),
    5969             :  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))),
    5970             :  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))),
    5971             :  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))),
    5972             :  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))),
    5973             :  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))),
    5974             :  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))),
    5975             :  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))),
    5976             :  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))),
    5977             :  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))),
    5978             :  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))),
    5979             :  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))),
    5980             :  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))),
    5981             :  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))),
    5982             :  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))),
    5983             :  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))),
    5984             :  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))),
    5985             :  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))),
    5986             :  pattern("aggr", "anyequal", SQLanyequal, false, "if any value in r is equal to l, return true, else if r has nil, return nil, else return false", args(1,3, arg("",bit),batargany("l",1),batargany("r",1))),
    5987             :  pattern("bataggr", "anyequal", SQLanyequal, false, "if any value in r is equal to l, return true, else if r has nil, return nil, else return false", args(1,3, batarg("",bit),batargany("l",1),batargany("r",1))),
    5988             :  pattern("aggr", "allnotequal", SQLallnotequal, false, "if all values in r are not equal to l, return true, else if r has nil, return nil, else return false", args(1,3, arg("",bit),batargany("l",1),batargany("r",1))),
    5989             :  pattern("bataggr", "allnotequal", SQLallnotequal, false, "if all values in r are not equal to l, return true, else if r has nil, return nil, else return false", args(1,3, arg("",bit),batargany("l",1),batargany("r",1))),
    5990             :  pattern("aggr", "subanyequal", SQLanyequal_grp, false, "if any value in r is equal to l, return true, else if r has nil, return nil, else return false", args(1,6, batarg("",bit),batargany("l",1),batargany("r",1),batarg("g",oid),batarg("e",oid),arg("no_nil",bit))),
    5991             : // pattern("aggr", "subanyequal", SQLanyequal_grp, false, "if any value in r is equal to l, return true, else if r has nil, return nil, else return 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))),
    5992             :  pattern("aggr", "subanyequal", SQLanyequal_grp2, false, "if any value in r is equal to l, return true, else if r has nil, return nil, else return false, except if rid is nil (ie empty) then return 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))),
    5993             :  pattern("aggr", "subanyequal", SQLanyequal_grp2, false, "if any value in r is equal to l, return true, else if r has nil, return nil, else return false, except if rid is nil (ie empty) then return 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))),
    5994             :  pattern("aggr", "suballnotequal", SQLallnotequal_grp, false, "if all values in r are not equal to l, return true, else if r has nil, return nil else return false", args(1,6, batarg("",bit),batargany("l",1),batargany("r",1),batarg("g",oid),batarg("e",oid),arg("no_nil",bit))),
    5995             : // pattern("aggr", "suballnotequal", SQLallnotequal_grp, false, "if all values in r are not equal to l, return true, else if r has nil, return nil else return 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))),
    5996             :  pattern("aggr", "suballnotequal", SQLallnotequal_grp2, false, "if all values in r are not equal to l, return true, else if r has nil return nil, else return false, except if rid is nil (ie empty) then return 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))),
    5997             :  pattern("aggr", "suballnotequal", SQLallnotequal_grp2, false, "if all values in r are not equal to l, return true, else if r has nil return nil, else return false, except if rid is nil (ie empty) then return 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))),
    5998             :  pattern("aggr", "exist", SQLexist, false, "", args(1,2, arg("",bit), argany("b",1))),
    5999             :  pattern("bataggr", "exist", SQLexist, false, "", args(1,2, batarg("",bit), argany("b",1))),
    6000             :  pattern("bataggr", "exist", SQLexist, false, "", args(1,2, arg("",bit), batargany("b",1))),
    6001             :  pattern("bataggr", "exist", SQLexist, false, "", args(1,2, batarg("",bit), batargany("b",1))),
    6002             :  pattern("aggr", "subexist", SQLsubexist, false, "", args(1,5, batarg("",bit),batargany("b",0),batarg("g",oid),batarg("e",oid),arg("no_nil",bit))),
    6003             :  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))),
    6004             :  pattern("aggr", "not_exist", SQLnot_exist, false, "", args(1,2, arg("",bit), argany("b",1))),
    6005             :  pattern("bataggr", "not_exist", SQLnot_exist, false, "", args(1,2, batarg("",bit), argany("b",1))),
    6006             :  pattern("bataggr", "not_exist", SQLnot_exist, false, "", args(1,2, arg("",bit), batargany("b",1))),
    6007             :  pattern("bataggr", "not_exist", SQLnot_exist, false, "", args(1,2, batarg("",bit), batargany("b",1))),
    6008             :  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))),
    6009             :  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))),
    6010             :  /* sqlcatalog */
    6011             :  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))),
    6012             :  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))),
    6013             :  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))),
    6014             :  pattern("sqlcatalog", "drop_seq", SQLdrop_seq, false, "Catalog operation drop_seq", args(0,3, arg("sname",str),arg("nme",str),arg("action",int))),
    6015             :  pattern("sqlcatalog", "create_schema", SQLcreate_schema, false, "Catalog operation create_schema", args(0,3, arg("sname",str),arg("auth",str),arg("action",int))),
    6016             :  pattern("sqlcatalog", "drop_schema", SQLdrop_schema, false, "Catalog operation drop_schema", args(0,3, arg("sname",str),arg("ifexists",int),arg("action",int))),
    6017             :  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))),
    6018             :  pattern("sqlcatalog", "create_table", SQLcreate_table, false, "Catalog operation create_table", args(0,6, arg("sname",str),arg("tname",str),arg("tbl",ptr),arg("pw_encrypted",int),arg("username",str),arg("passwd",str))),
    6019             :  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))),
    6020             :  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))),
    6021             :  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))),
    6022             :  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))),
    6023             :  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))),
    6024             :  pattern("sqlcatalog", "create_type", SQLcreate_type, false, "Catalog operation create_type", args(0,3, arg("sname",str),arg("nme",str),arg("impl",str))),
    6025             :  pattern("sqlcatalog", "drop_type", SQLdrop_type, false, "Catalog operation drop_type", args(0,3, arg("sname",str),arg("nme",str),arg("action",int))),
    6026             :  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))),
    6027             :  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))),
    6028             :  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))),
    6029             :  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))),
    6030             :  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))),
    6031             :  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))),
    6032             :  pattern("sqlcatalog", "create_user", SQLcreate_user, false, "Catalog operation create_user", args(0,10, arg("sname",str),arg("passwrd",str),arg("enc",int),arg("schema",str),arg("schemapath",str),arg("fullname",str), arg("max_memory", lng), arg("max_workers", int), arg("optimizer", str), arg("default_role", str))),
    6033             :  pattern("sqlcatalog", "drop_user", SQLdrop_user, false, "Catalog operation drop_user", args(0,2, arg("sname",str),arg("action",int))),
    6034             :  pattern("sqlcatalog", "drop_user", SQLdrop_user, false, "Catalog operation drop_user", args(0,3, arg("sname",str),arg("auth",str),arg("action",int))),
    6035             :  pattern("sqlcatalog", "alter_user", SQLalter_user, false, "Catalog operation alter_user", args(0,9, arg("sname",str),arg("passwrd",str),arg("enc",int),arg("schema",str),arg("schemapath",str),arg("oldpasswrd",str),arg("role",str),arg("max_memory",lng),arg("max_workers",int))),
    6036             :  pattern("sqlcatalog", "rename_user", SQLrename_user, false, "Catalog operation rename_user", args(0,3, arg("sname",str),arg("newnme",str),arg("action",int))),
    6037             :  pattern("sqlcatalog", "create_role", SQLcreate_role, false, "Catalog operation create_role", args(0,3, arg("sname",str),arg("role",str),arg("grator",int))),
    6038             :  pattern("sqlcatalog", "drop_role", SQLdrop_role, false, "Catalog operation drop_role", args(0,3, arg("auth",str),arg("role",str),arg("action",int))),
    6039             :  pattern("sqlcatalog", "drop_role", SQLdrop_role, false, "Catalog operation drop_role", args(0,2, arg("role",str),arg("action",int))),
    6040             :  pattern("sqlcatalog", "drop_index", SQLdrop_index, false, "Catalog operation drop_index", args(0,3, arg("sname",str),arg("iname",str),arg("action",int))),
    6041             :  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))),
    6042             :  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))),
    6043             :  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))),
    6044             :  pattern("sqlcatalog", "drop_trigger", SQLdrop_trigger, false, "Catalog operation drop_trigger", args(0,3, arg("sname",str),arg("nme",str),arg("ifexists",int))),
    6045             :  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))),
    6046             :  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))),
    6047             :  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))),
    6048             :  pattern("sqlcatalog", "alter_add_range_partition", SQLalter_add_range_partition, false, "Catalog operation alter_add_range_partition", args(0,9, arg("sname",str),arg("mtnme",str),arg("psnme",str),arg("ptnme",str),argany("min",1),argany("max",1),arg("nills",bit),arg("update",int),arg("assert",lng))),
    6049             :  pattern("sqlcatalog", "alter_add_range_partition", SQLalter_add_range_partition, false, "Catalog operation alter_add_range_partition", args(0,9, arg("sname",str),arg("mtnme",str),arg("psnme",str),arg("ptnme",str),argany("min",1),argany("max",1),arg("nills",bit),arg("update",int),batarg("assert",lng))),
    6050             :  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),arg("assert",lng))),
    6051             :  pattern("sqlcatalog", "alter_add_value_partition", SQLalter_add_value_partition, false, "Catalog operation alter_add_value_partition", args(0,8, arg("sname",str),arg("mtnme",str),arg("psnme",str),arg("ptnme",str),arg("nills",bit),arg("update",int),arg("assert",lng), varargany("arg",0))),
    6052             :  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),batarg("assert",lng))),
    6053             :  pattern("sqlcatalog", "alter_add_value_partition", SQLalter_add_value_partition, false, "Catalog operation alter_add_value_partition", args(0,8, arg("sname",str),arg("mtnme",str),arg("psnme",str),arg("ptnme",str),arg("nills",bit),arg("update",int),batarg("assert",lng), varargany("arg",0))),
    6054             :  pattern("sqlcatalog", "comment_on", SQLcomment_on, false, "Catalog operation comment_on", args(0,2, arg("objid",int),arg("remark",str))),
    6055             :  pattern("sqlcatalog", "rename_schema", SQLrename_schema, false, "Catalog operation rename_schema", args(0,2, arg("sname",str),arg("newnme",str))),
    6056             :  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))),
    6057             :  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))),
    6058             :  /* sql_transaction */
    6059             :  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))),
    6060             :  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))),
    6061             :  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))),
    6062             :  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))),
    6063             : #ifdef HAVE_HGE
    6064             :  /* sql_hge */
    6065             :  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))),
    6066             :  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))),
    6067             :  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))),
    6068             :  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))),
    6069             :  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))),
    6070             :  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))),
    6071             :  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))),
    6072             :  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))),
    6073             :  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))),
    6074             :  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))),
    6075             :  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))),
    6076             :  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))),
    6077             :  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))),
    6078             :  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))),
    6079             :  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))),
    6080             :  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))),
    6081             :  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))),
    6082             :  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))),
    6083             :  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))),
    6084             :  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))),
    6085             :  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))),
    6086             :  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))),
    6087             :  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))),
    6088             :  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))),
    6089             :  /* sql_decimal_hge */
    6090             :  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))),
    6091             :  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))),
    6092             :  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))),
    6093             :  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))),
    6094             :  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))),
    6095             :  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))),
    6096             :  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))),
    6097             :  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))),
    6098             :  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))),
    6099             :  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))),
    6100             :  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))),
    6101             :  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))),
    6102             :  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))),
    6103             :  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))),
    6104             :  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))),
    6105             :  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))),
    6106             :  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))),
    6107             :  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))),
    6108             :  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))),
    6109             :  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))),
    6110             :  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))),
    6111             :  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))),
    6112             :  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))),
    6113             :  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))),
    6114             :  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))),
    6115             :  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))),
    6116             :  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))),
    6117             :  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))),
    6118             :  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))),
    6119             :  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))),
    6120             :  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))),
    6121             :  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))),
    6122             :  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))),
    6123             :  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))),
    6124             :  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))),
    6125             :  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))),
    6126             :  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))),
    6127             :  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))),
    6128             :  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))),
    6129             :  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))),
    6130             :  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))),
    6131             :  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))),
    6132             :  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))),
    6133             :  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))),
    6134             :  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))),
    6135             :  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))),
    6136             :  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))),
    6137             :  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))),
    6138             :  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))),
    6139             :  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))),
    6140             :  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))),
    6141             :  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))),
    6142             :  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))),
    6143             :  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))),
    6144             :  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))),
    6145             :  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))),
    6146             :  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))),
    6147             :  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))),
    6148             :  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("scale",int))),
    6149             :  command("batcalc", "flt", bathge_num2dec_flt, false, "cast number to decimal(flt) and check for overflow", args(1,5, batarg("",flt),batarg("v",hge),batarg("s",oid),arg("digits",int),arg("scale",int))),
    6150             :  command("calc", "flt", hge_dec2_flt, false, "cast decimal(hge) to flt and check for overflow", args(1,3, arg("",flt),arg("s1",int),arg("v",hge))),
    6151             :  command("calc", "flt", hge_dec2dec_flt, false, "cast decimal(hge) to decimal(flt) and check for overflow", args(1,5, arg("",flt),arg("s1",int),arg("v",hge),arg("d2",int),arg("s2",int))),
    6152             :  command("batcalc", "flt", bathge_dec2_flt, false, "cast decimal(hge) to flt and check for overflow", args(1,4, batarg("",flt),arg("s1",int),batarg("v",hge),batarg("s",oid))),
    6153             :  command("batcalc", "flt", bathge_dec2dec_flt, false, "cast decimal(hge) to decimal(flt) and check for overflow", args(1,6, batarg("",flt),arg("s1",int),batarg("v",hge),batarg("s",oid),arg("d2",int),arg("s2",int))),
    6154             :  command("calc", "dbl", hge_num2dec_dbl, false, "cast number to decimal(dbl) and check for overflow", args(1,4, arg("",dbl),arg("v",hge),arg("digits",int),arg("scale",int))),
    6155             :  command("batcalc", "dbl", bathge_num2dec_dbl, false, "cast number to decimal(dbl) and check for overflow", args(1,5, batarg("",dbl),batarg("v",hge),batarg("s",oid),arg("digits",int),arg("scale",int))),
    6156             :  command("calc", "dbl", hge_dec2_dbl, false, "cast decimal(hge) to dbl and check for overflow", args(1,3, arg("",dbl),arg("s1",int),arg("v",hge))),
    6157             :  command("calc", "dbl", hge_dec2dec_dbl, false, "cast decimal(hge) to decimal(dbl) and check for overflow", args(1,5, arg("",dbl),arg("s1",int),arg("v",hge),arg("d2",int),arg("s2",int))),
    6158             :  command("batcalc", "dbl", bathge_dec2_dbl, false, "cast decimal(hge) to dbl and check for overflow", args(1,4, batarg("",dbl),arg("s1",int),batarg("v",hge),batarg("s",oid))),
    6159             :  command("batcalc", "dbl", bathge_dec2dec_dbl, false, "cast decimal(hge) to decimal(dbl) and check for overflow", args(1,6, batarg("",dbl),arg("s1",int),batarg("v",hge),batarg("s",oid),arg("d2",int),arg("s2",int))),
    6160             :  /* sql_rank_hge */
    6161             :  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("start",hge))),
    6162             :  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("start",hge))),
    6163             :  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("start",hge))),
    6164             :  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("start",hge))),
    6165             :  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("start",hge))),
    6166             :  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("start",hge))),
    6167             :  pattern("sql", "sum", SQLsum, false, "return the sum of groups", args(1,7, arg("",hge),arg("b",bte),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    6168             :  pattern("batsql", "sum", SQLsum, false, "return the sum of groups", args(1,7, batarg("",hge),batarg("b",bte),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    6169             :  pattern("sql", "sum", SQLsum, false, "return the sum of groups", args(1,7, arg("",hge),arg("b",sht),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    6170             :  pattern("batsql", "sum", SQLsum, false, "return the sum of groups", args(1,7, batarg("",hge),batarg("b",sht),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    6171             :  pattern("sql", "sum", SQLsum, false, "return the sum of groups", args(1,7, arg("",hge),arg("b",int),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    6172             :  pattern("batsql", "sum", SQLsum, false, "return the sum of groups", args(1,7, batarg("",hge),batarg("b",int),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    6173             :  pattern("sql", "sum", SQLsum, false, "return the sum of groups", args(1,7, arg("",hge),arg("b",lng),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    6174             :  pattern("batsql", "sum", SQLsum, false, "return the sum of groups", args(1,7, batarg("",hge),batarg("b",lng),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    6175             :  pattern("sql", "sum", SQLsum, false, "return the sum of groups", args(1,7, arg("",hge),arg("b",hge),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    6176             :  pattern("batsql", "sum", SQLsum, false, "return the sum of groups", args(1,7, batarg("",hge),batarg("b",hge),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    6177             :  pattern("sql", "prod", SQLprod, false, "return the product of groups", args(1,7, arg("",hge),arg("b",bte),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    6178             :  pattern("batsql", "prod", SQLprod, false, "return the product of groups", args(1,7, batarg("",hge),batarg("b",bte),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    6179             :  pattern("sql", "prod", SQLprod, false, "return the product of groups", args(1,7, arg("",hge),arg("b",sht),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    6180             :  pattern("batsql", "prod", SQLprod, false, "return the product of groups", args(1,7, batarg("",hge),batarg("b",sht),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    6181             :  pattern("sql", "prod", SQLprod, false, "return the product of groups", args(1,7, arg("",hge),arg("b",int),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    6182             :  pattern("batsql", "prod", SQLprod, false, "return the product of groups", args(1,7, batarg("",hge),batarg("b",int),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    6183             :  pattern("sql", "prod", SQLprod, false, "return the product of groups", args(1,7, arg("",hge),arg("b",lng),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    6184             :  pattern("batsql", "prod", SQLprod, false, "return the product of groups", args(1,7, batarg("",hge),batarg("b",lng),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    6185             :  pattern("sql", "prod", SQLprod, false, "return the product of groups", args(1,7, arg("",hge),arg("b",hge),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    6186             :  pattern("batsql", "prod", SQLprod, false, "return the product of groups", args(1,7, batarg("",hge),batarg("b",hge),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    6187             :  pattern("sql", "avg", SQLavg, false, "return the average of groups", args(1,7, arg("",dbl),arg("b",hge),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    6188             :  pattern("batsql", "avg", SQLavg, false, "return the average of groups", args(1,7, batarg("",dbl),batarg("b",hge),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    6189             :  pattern("sql", "avg", SQLavginteger, false, "return the average of groups", args(1,7, arg("",hge),arg("b",hge),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    6190             :  pattern("batsql", "avg", SQLavginteger, false, "return the average of groups", args(1,7, batarg("",hge),batarg("b",hge),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    6191             :  pattern("sql", "stdev", SQLstddev_samp, false, "return the standard deviation sample of groups", args(1,7, arg("",dbl),arg("b",hge),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    6192             :  pattern("batsql", "stdev", SQLstddev_samp, false, "return the standard deviation sample of groups", args(1,7, batarg("",dbl),batarg("b",hge),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    6193             :  pattern("sql", "stdevp", SQLstddev_pop, false, "return the standard deviation population of groups", args(1,7, arg("",dbl),arg("b",hge),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    6194             :  pattern("batsql", "stdevp", SQLstddev_pop, false, "return the standard deviation population of groups", args(1,7, batarg("",dbl),batarg("b",hge),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    6195             :  pattern("sql", "variance", SQLvar_samp, false, "return the variance sample of groups", args(1,7, arg("",dbl),arg("b",hge),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    6196             :  pattern("batsql", "variance", SQLvar_samp, false, "return the variance sample of groups", args(1,7, batarg("",dbl),batarg("b",hge),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    6197             :  pattern("sql", "variancep", SQLvar_pop, false, "return the variance population of groups", args(1,7, arg("",dbl),arg("b",hge),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    6198             :  pattern("batsql", "variancep", SQLvar_pop, false, "return the variance population of groups", args(1,7, batarg("",dbl),batarg("b",hge),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    6199             :  pattern("sql", "covariance", SQLcovar_samp, false, "return the covariance sample value of groups", args(1,8, arg("",dbl),arg("b",hge),arg("c",hge),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    6200             :  pattern("batsql", "covariance", SQLcovar_samp, false, "return the covariance sample value of groups", args(1,8, batarg("",dbl),arg("b",hge),batarg("c",hge),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    6201             :  pattern("batsql", "covariance", SQLcovar_samp, false, "return the covariance sample value of groups", args(1,8, batarg("",dbl),batarg("b",hge),arg("c",hge),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    6202             :  pattern("batsql", "covariance", SQLcovar_samp, false, "return the covariance sample value of groups", args(1,8, batarg("",dbl),batarg("b",hge),batarg("c",hge),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    6203             :  pattern("sql", "covariancep", SQLcovar_pop, false, "return the covariance population value of groups", args(1,8, arg("",dbl),arg("b",hge),arg("c",hge),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    6204             :  pattern("batsql", "covariancep", SQLcovar_pop, false, "return the covariance population value of groups", args(1,8, batarg("",dbl),arg("b",hge),batarg("c",hge),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    6205             :  pattern("batsql", "covariancep", SQLcovar_pop, false, "return the covariance population value of groups", args(1,8, batarg("",dbl),batarg("b",hge),arg("c",hge),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    6206             :  pattern("batsql", "covariancep", SQLcovar_pop, false, "return the covariance population value of groups", args(1,8, batarg("",dbl),batarg("b",hge),batarg("c",hge),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    6207             :  pattern("sql", "corr", SQLcorr, false, "return the correlation value of groups", args(1,8, arg("",dbl),arg("b",hge),arg("c",hge),arg("p",bit),arg("o",bit),arg("t",int),arg("s",oid),arg("e",oid))),
    6208             :  pattern("batsql", "corr", SQLcorr, false, "return the correlation value of groups", args(1,8, batarg("",dbl),arg("b",hge),batarg("c",hge),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    6209             :  pattern("batsql", "corr", SQLcorr, false, "return the correlation value of groups", args(1,8, batarg("",dbl),batarg("b",hge),arg("c",hge),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    6210             :  pattern("batsql", "corr", SQLcorr, false, "return the correlation value of groups", args(1,8, batarg("",dbl),batarg("b",hge),batarg("c",hge),argany("p",0),argany("o",0),arg("t",int),argany("s",0),argany("e",0))),
    6211             : #endif
    6212             :  pattern("sql", "vacuum", SQLstr_column_vacuum, true, "vacuum a string column", args(0,3, arg("sname",str),arg("tname",str),arg("cname",str))),
    6213             :  pattern("sql", "vacuum", SQLstr_column_auto_vacuum, true, "auto vacuum string column with interval(sec)", args(0,4, arg("sname",str),arg("tname",str),arg("cname",str),arg("interval", int))),
    6214             :  pattern("sql", "stop_vacuum", SQLstr_column_stop_vacuum, true, "stop auto vacuum", args(0,3, arg("sname",str),arg("tname",str),arg("cname",str))),
    6215             :  { .imp=NULL }
    6216             : };
    6217             : #include "mal_import.h"
    6218             : #ifdef _MSC_VER
    6219             : #undef read
    6220             : #pragma section(".CRT$XCU",read)
    6221             : #endif
    6222         326 : LIB_STARTUP_FUNC(init_sql_mal)
    6223         326 : { mal_module("sql", NULL, sql_init_funcs); }

Generated by: LCOV version 1.14