LCOV - code coverage report
Current view: top level - sql/backends/monet5 - sql.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 2270 3623 62.7 %
Date: 2024-04-26 00:35:57 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       17681 : rel_is_table(sql_rel *rel)
      61             : {
      62       17681 :         if (!rel || is_base(rel->op))
      63       13331 :                 return 1;
      64             :         return 0;
      65             : }
      66             : 
      67             : static int
      68       13331 : exp_is_point_select(sql_exp *e)
      69             : {
      70       13331 :         if (!e)
      71             :                 return 1;
      72       13331 :         if (e->type == e_cmp && !e->f && e->flag == (int) cmp_equal) {
      73        6730 :                 sql_exp *r = e->r;
      74        6730 :                 sql_exp *l = e->l;
      75             : 
      76        6730 :                 if (!is_func(l->type) && r->card <= CARD_AGGR)
      77        6636 :                         return 1;
      78             :         }
      79             :         return 0;
      80             : }
      81             : 
      82             : static int
      83      552902 : rel_no_mitosis(mvc *sql, sql_rel *rel)
      84             : {
      85     1295179 :         if (mvc_highwater(sql))
      86             :                 return 0;
      87      971809 :         if (!rel || is_basetable(rel->op))
      88             :                 return 1;
      89             :         /* use mitosis on order topn */
      90      790330 :         if (is_topn(rel->op)) {
      91       16503 :                 sql_rel *l = rel->l;
      92       16503 :                 if (l && is_simple_project(l->op) && l->r)
      93             :                         return 0;
      94             :         }
      95      790051 :         if (is_topn(rel->op) || is_sample(rel->op) || is_simple_project(rel->op))
      96      317804 :                 return rel_no_mitosis(sql, rel->l);
      97      472247 :         if (is_ddl(rel->op) && rel->flag == ddl_output) {
      98             :                 // COPY SELECT ... INTO
      99          74 :                 return rel_no_mitosis(sql, rel->l);
     100             :         }
     101      472173 :         if ((is_delete(rel->op) || is_truncate(rel->op)) && rel->card <= CARD_AGGR)
     102             :                 return 1;
     103      430691 :         if ((is_insert(rel->op) || is_update(rel->op)) && rel->card <= CARD_AGGR)
     104      100914 :                 return rel_no_mitosis(sql, rel->r);
     105      343108 :         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       13331 :                 if (exp_is_point_select(rel->exps->h->data))
     108             :                         return 1;
     109             :         }
     110             :         return 0;
     111             : }
     112             : 
     113             : static int
     114      323420 : rel_need_distinct_query(sql_rel *rel)
     115             : {
     116      323420 :         int need_distinct = 0;
     117             : 
     118      362610 :         while (rel && is_simple_project(rel->op))
     119       39190 :                 rel = rel->l;
     120      323420 :         if (rel && is_groupby(rel->op) && !list_empty(rel->exps) && list_empty(rel->r)) {
     121        2378 :                 for (node *n = rel->exps->h; n && !need_distinct; n = n->next) {
     122        1331 :                         sql_exp *e = n->data;
     123             : 
     124        1331 :                         if (e->type == e_aggr && need_distinct(e))
     125        1331 :                                 need_distinct = 1;
     126             :                 }
     127             :         }
     128      323420 :         return need_distinct;
     129             : }
     130             : 
     131             : sql_rel *
     132      555669 : sql_symbol2relation(backend *be, symbol *sym)
     133             : {
     134      555669 :         sql_rel *rel;
     135      555669 :         sql_query *query = query_create(be->mvc);
     136      555814 :         lng Tbegin, Tend;
     137      555814 :         int value_based_opt = be->mvc->emode != m_prepare, storage_based_opt;
     138      555814 :         int profile = be->mvc->emode == m_plan;
     139      555814 :         Client c = be->client;
     140             : 
     141      555814 :         Tbegin = GDKusec();
     142      556088 :         rel = rel_semantic(query, sym);
     143      555336 :         Tend = GDKusec();
     144      555538 :         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      555538 :         storage_based_opt = value_based_opt && rel && !is_ddl(rel->op);
     150      283467 :         Tbegin = Tend;
     151        2555 :         if (rel)
     152      553331 :                 rel = sql_processrelation(be->mvc, rel, profile, 1, value_based_opt, storage_based_opt);
     153      553564 :         if (rel)
     154      553545 :                 rel = rel_partition(be->mvc, rel);
     155      552960 :         if (rel && (rel_no_mitosis(be->mvc, rel) || rel_need_distinct_query(rel)))
     156      230087 :                 be->no_mitosis = 1;
     157      553455 :         if (rel /*&& (be->mvc->emode != m_plan || (ATOMIC_GET(&GDKdebug) & FORCEMITOMASK) == 0)*/)
     158      553455 :                 rel = rel_physical(be->mvc, rel);
     159      555592 :         Tend = GDKusec();
     160      555868 :         be->reloptimizer = Tend - Tbegin;
     161             : 
     162      555868 :         if(profilerStatus > 0)
     163           0 :                 profilerEvent(NULL,
     164             :                                           &(struct NonMalEvent)
     165           0 :                                           {REL_OPT, c, Tend, NULL, NULL, rel?0:1, be->reloptimizer});
     166      555868 :         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      725396 : sqlcleanup(backend *be, int err)
     176             : {
     177      725396 :         sql_destroy_params(be->mvc);
     178             : 
     179             :         /* some statements dynamically disable caching */
     180      725618 :         be->mvc->sym = NULL;
     181      725618 :         be->mvc->runs = NULL;
     182      725618 :         if (be->mvc->ta)
     183      725547 :                 be->mvc->ta = sa_reset(be->mvc->ta);
     184      725614 :         if (be->mvc->sa)
     185      725256 :                 be->mvc->sa = sa_reset(be->mvc->sa);
     186      725682 :         if (err >0)
     187         363 :                 be->mvc->session->status = -err;
     188      725682 :         if (err <0)
     189       35053 :                 be->mvc->session->status = err;
     190      725682 :         be->mvc->label = 0;
     191      725682 :         be->no_mitosis = 0;
     192      725682 :         scanner_query_processed(&(be->mvc->scanner));
     193      725509 :         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     7980616 : checkSQLContext(Client cntxt)
     204             : {
     205     7980616 :         backend *be;
     206             : 
     207     7980616 :         if (cntxt == NULL)
     208           0 :                 throw(SQL, "mvc", SQLSTATE(42005) "No client record");
     209     7980616 :         if (cntxt->sqlcontext == NULL)
     210           0 :                 throw(SQL, "mvc", SQLSTATE(42006) "SQL module not initialized");
     211     7980616 :         be = (backend *) cntxt->sqlcontext;
     212     7980616 :         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      121527 : getBackendContext(Client cntxt, backend **be)
     219             : {
     220      121527 :         str msg;
     221             : 
     222      121527 :         if ((msg = checkSQLContext(cntxt)) != MAL_SUCCEED)
     223             :                 return msg;
     224      121532 :         *be = (backend *) cntxt->sqlcontext;
     225      121532 :         return MAL_SUCCEED;
     226             : }
     227             : 
     228             : str
     229     3867266 : getSQLContext(Client cntxt, MalBlkPtr mb, mvc **c, backend **b)
     230             : {
     231     3867266 :         backend *be;
     232     3867266 :         (void) mb;
     233     3867266 :         str msg;
     234             : 
     235     3867266 :         if ((msg = checkSQLContext(cntxt)) != MAL_SUCCEED)
     236             :                 return msg;
     237     3879909 :         be = (backend *) cntxt->sqlcontext;
     238     3879909 :         if (c)
     239     3879909 :                 *c = be->mvc;
     240     3879909 :         if (b)
     241         433 :                 *b = be;
     242             :         return MAL_SUCCEED;
     243             : }
     244             : 
     245             : str
     246      286982 : SQLmvc(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
     247             : {
     248      286982 :         mvc *sql = NULL;
     249      286982 :         str msg;
     250      286982 :         int *res = getArgReference_int(stk, pci, 0);
     251             : 
     252      286982 :         if ((msg = getSQLContext(cntxt, mb, &sql, NULL)) != NULL)
     253             :                 return msg;
     254      286988 :         if ((msg = checkSQLContext(cntxt)) != NULL)
     255             :                 return msg;
     256      286987 :         *res = 0;
     257      286987 :         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       31401 : create_table_or_view(mvc *sql, char *sname, char *tname, sql_table *t, int temp, int replace)
     295             : {
     296       31401 :         allocator *osa;
     297       31401 :         sql_schema *s = mvc_bind_schema(sql, sname);
     298       31401 :         sql_table *nt = NULL, *ot;
     299       31401 :         node *n;
     300       31401 :         int check = 0;
     301       31401 :         const char *action = (temp == SQL_DECLARED_TABLE) ? "DECLARE" : (replace ? "CREATE OR REPLACE" : "CREATE");
     302       31401 :         const char *obj = t->query ? "VIEW" : "TABLE";
     303       31401 :         str msg = MAL_SUCCEED;
     304             : 
     305       31401 :         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       31401 :         if (!s)
     309           0 :                 throw(SQL, "sql.catalog", SQLSTATE(3F000) "%s %s: schema '%s' doesn't exist", action, obj, sname);
     310       31401 :         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       31401 :         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       31399 :         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       31399 :         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       31395 :                         break;
     340             :         }
     341       31395 :         osa = sql->sa;
     342       31395 :         allocator *nsa = sql->sa = sa_create(NULL);
     343             :         /* first check default values */
     344      281827 :         for (n = ol_first_node(t->columns); n; n = n->next) {
     345      250433 :                 sql_column *c = n->data;
     346             : 
     347      250433 :                 if (c->def) {
     348             :                         /* TODO please don't place an auto incremented sequence in the default value */
     349        1353 :                         const char *next_value_for = "next value for \"sys\".\"seq_";
     350        1353 :                         sql_rel *r = NULL;
     351             : 
     352        1353 :                         sa_reset(nsa);
     353        1353 :                         sql->sa = nsa;
     354        1353 :                         r = rel_parse(sql, s, sa_message(sql->ta, "select %s;", c->def), m_deps);
     355        2706 :                         if (!r || !is_project(r->op) || !r->exps || list_length(r->exps) != 1 ||
     356        1353 :                                 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        1352 :                         if (strncmp(c->def, next_value_for, strlen(next_value_for)) != 0) {
     369        1154 :                                 list *blist = rel_dependencies(sql, r);
     370        1154 :                                 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        1352 :                         sa_reset(sql->sa);
     377             :                 }
     378             :         }
     379             : 
     380      281826 :         for (n = ol_first_node(t->columns); n; n = n->next) {
     381      250432 :                 sql_column *c = n->data, *copied = NULL;
     382             : 
     383      250432 :                 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      250432 :                                 break;
     395             :                 }
     396      250432 :                 if (isPartitionedByColumnTable(t) && c->base.id == t->part.pcol->base.id)
     397          76 :                         nt->part.pcol = copied;
     398             :         }
     399       31394 :         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       31391 :         check = sql_trans_set_partition_table(sql->session->tr, nt);
     413       31391 :         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       31391 :         } 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       31391 :         if (t->idxs) {
     424       37205 :                 for (n = ol_first_node(t->idxs); n; n = n->next) {
     425        5814 :                         sql_idx *i = n->data;
     426             : 
     427        5814 :                         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        5814 :                                         break;
     439             :                         }
     440             :                 }
     441             :         }
     442       31391 :         if (t->keys) {
     443       37198 :                 for (n = ol_first_node(t->keys); n; n = n->next) {
     444        5814 :                         sql_key *k = n->data;
     445        5814 :                         char *err = NULL;
     446             : 
     447        5814 :                         err = sql_partition_validate_key(sql, nt, k, "CREATE");
     448        5814 :                         if (err) {
     449           6 :                                 sa_destroy(nsa);
     450           6 :                                 sql->sa = osa;
     451           6 :                                 return err;
     452             :                         }
     453        5808 :                         sa_reset(sql->sa);
     454        5808 :                         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        5807 :                                         break;
     466             :                         }
     467        5807 :                         sa_reset(sql->sa);
     468             :                 }
     469             :         }
     470       31384 :         if (t->triggers) {
     471       31384 :                 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       31384 :         if (nt->query && isView(nt)) {
     491       22071 :                 sql_rel *r = NULL;
     492             : 
     493       22071 :                 sa_reset(nsa);
     494       22071 :                 r = rel_parse(sql, s, nt->query, m_deps);
     495       22071 :                 if (r)
     496       22071 :                         r = sql_processrelation(sql, r, 0, 0, 0, 0);
     497       22071 :                 if (r) {
     498       22071 :                         list *blist = rel_dependencies(sql, r);
     499       22071 :                         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       22071 :                 sql->sa = osa;
     506       22071 :                 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       31384 :         sa_destroy(nsa);
     515       31384 :         sql->sa = osa;
     516       31384 :         return MAL_SUCCEED;
     517             : }
     518             : 
     519             : static int
     520      107720 : mvc_claim_slots(sql_trans *tr, sql_table *t, size_t cnt, BUN *offset, BAT **pos)
     521             : {
     522      107720 :         sqlstore *store = tr->store;
     523      107720 :         return store->storage_api.claim_tab(tr, t, cnt, offset, pos);
     524             : }
     525             : 
     526             : str
     527      107469 : mvc_claim_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
     528             : {
     529      107469 :         BUN *offset = (BUN*)getArgReference_oid(stk, pci, 0);
     530      107469 :         bat *res = getArgReference_bat(stk, pci, 1);
     531      107469 :         mvc *m = NULL;
     532      107469 :         str msg;
     533      107469 :         const char *sname = *getArgReference_str(stk, pci, 3);
     534      107469 :         const char *tname = *getArgReference_str(stk, pci, 4);
     535      107469 :         lng cnt = *getArgReference_lng(stk, pci, 5);
     536      107469 :         BAT *pos = NULL;
     537      107469 :         sql_schema *s;
     538      107469 :         sql_table *t;
     539             : 
     540      107469 :         *res = 0;
     541      107469 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
     542             :                 return msg;
     543      107604 :         if ((msg = checkSQLContext(cntxt)) != NULL)
     544             :                 return msg;
     545             : 
     546      107599 :         s = mvc_bind_schema(m, sname);
     547      107590 :         if (s == NULL)
     548           0 :                 throw(SQL, "sql.claim", SQLSTATE(3F000) "Schema missing %s", sname);
     549      107590 :         t = mvc_bind_table(m, s, tname);
     550      107707 :         if (t == NULL)
     551           0 :                 throw(SQL, "sql.claim", SQLSTATE(42S02) "Table missing %s.%s", sname, tname);
     552      107707 :         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      107707 :         if (mvc_claim_slots(m->session->tr, t, (size_t)cnt, offset, &pos) == LOG_OK) {
     555      107504 :                 *res = bat_nil;
     556      107504 :                 if (pos) {
     557           7 :                         *res = pos->batCacheid;
     558           7 :                         BBPkeepref(pos);
     559             :                 }
     560      107593 :                 return MAL_SUCCEED;
     561             :         }
     562           0 :         throw(SQL, "sql.claim", SQLSTATE(3F000) "Could not claim slots");
     563             : }
     564             : 
     565             : str
     566      136756 : mvc_add_dependency_change(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
     567             : {
     568      136756 :         str msg;
     569      136756 :         mvc *m = NULL;
     570      136756 :         const char *sname = *getArgReference_str(stk, pci, 1);
     571      136756 :         const char *tname = *getArgReference_str(stk, pci, 2);
     572      136756 :         lng cnt = *getArgReference_lng(stk, pci, 3);
     573      136756 :         sql_schema *s;
     574      136756 :         sql_table *t;
     575             : 
     576      136756 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
     577             :                 return msg;
     578      136915 :         if ((msg = checkSQLContext(cntxt)) != NULL)
     579             :                 return msg;
     580             : 
     581      136976 :         if ((s = mvc_bind_schema(m, sname)) == NULL)
     582           0 :                 throw(SQL, "sql.dependency_change", SQLSTATE(3F000) "Schema missing %s", sname);
     583      136965 :         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      136852 :         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      136852 :         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       12517 : mvc_add_column_predicate(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
     594             : {
     595       12517 :         str msg;
     596       12517 :         mvc *m = NULL;
     597       12517 :         const char *sname = *getArgReference_str(stk, pci, 1);
     598       12517 :         const char *tname = *getArgReference_str(stk, pci, 2);
     599       12517 :         const char *cname = *getArgReference_str(stk, pci, 3);
     600       12517 :         sql_schema *s;
     601       12517 :         sql_table *t;
     602       12517 :         sql_column *c;
     603             : 
     604       12517 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
     605             :                 return msg;
     606       12519 :         if ((msg = checkSQLContext(cntxt)) != NULL)
     607             :                 return msg;
     608             : 
     609       12519 :         if ((s = mvc_bind_schema(m, sname)) == NULL)
     610           0 :                 throw(SQL, "sql.column_predicate", SQLSTATE(3F000) "Schema missing %s", sname);
     611       12519 :         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       12519 :         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       12518 :         if ((m->session->level & tr_snapshot) == tr_snapshot || isNew(c) || !isGlobal(c->t) || isGlobalTemp(c->t))
     617             :                 return MAL_SUCCEED;
     618        6257 :         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, "varchar", 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         257 : mvc_bind(mvc *m, const char *sname, const char *tname, const char *cname, int access)
     755             : {
     756         257 :         sql_trans *tr = m->session->tr;
     757         257 :         BAT *b = NULL;
     758         257 :         sql_schema *s = NULL;
     759         257 :         sql_table *t = NULL;
     760         257 :         sql_column *c = NULL;
     761             : 
     762         257 :         s = mvc_bind_schema(m, sname);
     763         257 :         if (s == NULL)
     764             :                 return NULL;
     765         257 :         t = mvc_bind_table(m, s, tname);
     766         257 :         if (t == NULL || !isTable(t))
     767             :                 return NULL;
     768         257 :         c = mvc_bind_column(m, t, cname);
     769         257 :         if (c == NULL)
     770             :                 return NULL;
     771             : 
     772         257 :         sqlstore *store = tr->store;
     773         257 :         b = store->storage_api.bind_col(tr, c, access);
     774         257 :         return b;
     775             : }
     776             : 
     777             : /* setVariable(int *ret, str *sname, str *name, any value) */
     778             : str
     779         353 : setVariable(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
     780             : {
     781         353 :         int *res = getArgReference_int(stk, pci, 0);
     782         353 :         mvc *m = NULL;
     783         353 :         str msg;
     784         353 :         const char *sname = *getArgReference_str(stk, pci, 2);
     785         353 :         const char *varname = *getArgReference_str(stk, pci, 3);
     786         353 :         int mtype = getArgType(mb, pci, 4);
     787         353 :         sql_schema *s;
     788         353 :         sql_var *var;
     789             : 
     790         353 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
     791             :                 return msg;
     792         353 :         if ((msg = checkSQLContext(cntxt)) != NULL)
     793             :                 return msg;
     794             : 
     795         353 :         if (!(s = mvc_bind_schema(m, sname)))
     796           0 :                 throw(SQL, "sql.setVariable", SQLSTATE(3F000) "Cannot find the schema '%s'", sname);
     797             : 
     798         353 :         *res = 0;
     799         353 :         if (mtype < 0 || mtype >= 255)
     800           0 :                 throw(SQL, "sql.setVariable", SQLSTATE(42100) "Variable type error");
     801             : 
     802         353 :         if ((var = find_global_var(m, s, varname))) {
     803         493 :                 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         186 :                         ValPtr ptr = &stk->stk[getArg(pci, 4)];
     821             : 
     822         186 :                         if ((msg = sql_update_var(m, s, varname, ptr)))
     823             :                                 return msg;
     824         172 :                         if (!sqlvar_set(var, ptr))
     825           0 :                                 throw(SQL, "sql.setVariable", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     826             :                 }
     827         312 :                 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         317 : getVariable(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
     835             : {
     836         317 :         int mtype = getArgType(mb, pci, 0);
     837         317 :         mvc *m = NULL;
     838         317 :         str msg;
     839         317 :         const char *sname = *getArgReference_str(stk, pci, 2);
     840         317 :         const char *varname = *getArgReference_str(stk, pci, 3);
     841         317 :         ValRecord *dst, *src;
     842         317 :         sql_schema *s;
     843         317 :         sql_var *var;
     844             : 
     845         317 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
     846             :                 return msg;
     847         317 :         if ((msg = checkSQLContext(cntxt)) != NULL)
     848             :                 return msg;
     849             : 
     850         317 :         if (!(s = mvc_bind_schema(m, sname)))
     851           0 :                 throw(SQL, "sql.getVariable", SQLSTATE(3F000) "Cannot find the schema '%s'", sname);
     852         317 :         if (mtype < 0 || mtype >= 255)
     853           0 :                 throw(SQL, "sql.getVariable", SQLSTATE(42100) "Variable type error");
     854         317 :         if (!(var = find_global_var(m, s, varname)))
     855           0 :                 throw(SQL, "sql.getVariable", SQLSTATE(42100) "Variable '%s.%s' unknown", sname, varname);
     856         317 :         src = &(var->var.data);
     857         317 :         dst = &stk->stk[getArg(pci, 0)];
     858         317 :         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         968 : mvc_next_value(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
     976             : {
     977         968 :         backend *be = NULL;
     978         968 :         str msg;
     979         968 :         sql_schema *s;
     980         968 :         sql_sequence *seq;
     981         968 :         lng *res = getArgReference_lng(stk, pci, 0);
     982         968 :         const char *sname = *getArgReference_str(stk, pci, 1);
     983         968 :         const char *seqname = *getArgReference_str(stk, pci, 2);
     984             : 
     985         968 :         (void)mb;
     986         968 :         if ((msg = getBackendContext(cntxt, &be)) != NULL)
     987             :                 return msg;
     988         968 :         if (!(s = mvc_bind_schema(be->mvc, sname)))
     989           0 :                 throw(SQL, "sql.next_value", SQLSTATE(3F000) "Cannot find the schema %s", sname);
     990         968 :         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         968 :         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         968 :         if (seq_next_value(be->mvc->session->tr->store, seq, res)) {
     996         967 :                 be->last_id = *res;
     997         967 :                 sqlvar_set_number(find_global_var(be->mvc, mvc_bind_schema(be->mvc, "sys"), "last_id"), be->last_id);
     998         967 :                 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         222 : mvc_get_value_bulk(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    1076             : {
    1077         222 :         mvc *m = NULL;
    1078         222 :         sql_schema *s;
    1079         222 :         sql_sequence *seq;
    1080         222 :         BATiter schi, seqi;
    1081         222 :         BAT *bn = NULL, *scheb = NULL, *sches = NULL, *seqb = NULL, *seqs = NULL;
    1082         222 :         lng *restrict vals;
    1083         222 :         str msg = MAL_SUCCEED;
    1084         222 :         bool nils = false;
    1085         222 :         struct canditer ci1 = {0}, ci2 = {0};
    1086         222 :         oid off1, off2;
    1087         222 :         bat *res = getArgReference_bat(stk, pci, 0), *l = getArgReference_bat(stk, pci, 1), *r = getArgReference_bat(stk, pci, 2),
    1088         222 :                 *sid1 = pci->argc == 5 ? getArgReference_bat(stk, pci, 3) : NULL, *sid2 = pci->argc == 5 ? getArgReference_bat(stk, pci, 4) : NULL;
    1089             : 
    1090         222 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    1091             :                 return msg;
    1092         222 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    1093             :                 return msg;
    1094         222 :         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         222 :         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         222 :         canditer_init(&ci1, scheb, sches);
    1103         222 :         canditer_init(&ci2, seqb, seqs);
    1104         222 :         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         222 :         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         222 :         off1 = scheb->hseqbase;
    1114         222 :         off2 = seqb->hseqbase;
    1115         222 :         schi = bat_iterator(scheb);
    1116         221 :         seqi = bat_iterator(seqb);
    1117         222 :         vals = Tloc(bn, 0);
    1118         272 :         for (BUN i = 0; i < ci1.ncand; i++) {
    1119          51 :                 oid p1 = canditer_next(&ci1) - off1, p2 = canditer_next(&ci2) - off2;
    1120          50 :                 const char *sname = BUNtvar(schi, p1);
    1121          50 :                 const char *seqname = BUNtvar(seqi, p2);
    1122             : 
    1123         100 :                 if (strNil(sname) || strNil(seqname)) {
    1124           0 :                         vals[i] = lng_nil;
    1125           0 :                         nils = true;
    1126             :                 } else {
    1127          50 :                         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          50 :                         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          50 :                         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         221 : bailout1:
    1143         221 :         bat_iterator_end(&schi);
    1144         222 :         bat_iterator_end(&seqi);
    1145         222 : bailout:
    1146         222 :         BBPreclaim(scheb);
    1147         222 :         BBPreclaim(sches);
    1148         222 :         BBPreclaim(seqb);
    1149         222 :         BBPreclaim(seqs);
    1150         222 :         if (bn && !msg) {
    1151         222 :                 BATsetcount(bn, ci1.ncand);
    1152         222 :                 bn->tnil = nils;
    1153         222 :                 bn->tnonil = !nils;
    1154         222 :                 bn->tkey = BATcount(bn) <= 1;
    1155         222 :                 bn->tsorted = BATcount(bn) <= 1;
    1156         222 :                 bn->trevsorted = BATcount(bn) <= 1;
    1157         222 :                 *res = bn->batCacheid;
    1158         222 :                 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     2045212 : mvc_bind_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    1248             : {
    1249     2045212 :         int upd = (pci->argc == 7 || pci->argc == 9);
    1250     2045212 :         BAT *b = NULL;
    1251     2045212 :         bat *bid = getArgReference_bat(stk, pci, 0);
    1252     2045212 :         mvc *m = NULL;
    1253     2045212 :         str msg;
    1254     2045212 :         const char *sname       = *getArgReference_str(stk, pci, 2 + upd);
    1255     2045212 :         const char *tname       = *getArgReference_str(stk, pci, 3 + upd);
    1256     2045212 :         const char *cname       = *getArgReference_str(stk, pci, 4 + upd);
    1257     2045212 :         const int       access  = *getArgReference_int(stk, pci, 5 + upd);
    1258             : 
    1259     2045212 :         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     2045212 :         assert(access != QUICK);
    1263     2045212 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    1264             :                 return msg;
    1265     2043446 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    1266             :                 return msg;
    1267     2046871 :         sqlstore *store = m->store;
    1268     2046871 :         sql_schema *s = mvc_bind_schema(m, sname);
    1269     2045313 :         sql_table *t = mvc_bind_table(m, s, tname);
    1270     2054742 :         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     2054742 :         sql_column *c = mvc_bind_column(m, t, cname);
    1274             : 
    1275     2045272 :         if (partitioned_access) {
    1276             :                 /* partitioned access */
    1277     1697028 :                 int part_nr = *getArgReference_int(stk, pci, 6 + upd);
    1278     1697028 :                 int nr_parts = *getArgReference_int(stk, pci, 7 + upd);
    1279     1697028 :                 BUN cnt = store->storage_api.count_col(m->session->tr, c, 0), psz;
    1280     1710608 :                 oid l, h;
    1281     1710608 :                 psz = cnt ? (cnt / nr_parts) : 0;
    1282     1710608 :                 l = part_nr * psz;
    1283     1710608 :                 if (l > cnt)
    1284           0 :                         l = cnt;
    1285     1710608 :                 h = (part_nr + 1 == nr_parts) ? cnt : ((part_nr + 1) * psz);
    1286     1710608 :                 if (h > cnt)
    1287           0 :                         h = cnt;
    1288             : 
    1289     1710608 :                 if (upd) {
    1290      414335 :                         BAT *ui = NULL, *uv = NULL;
    1291      414335 :                         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      413913 :                         h--;
    1295      413913 :                         BAT* bn = BATselect(ui, NULL, &l, &h, true, true, false);
    1296      413224 :                         if(bn == NULL) {
    1297           0 :                                 BBPunfix(ui->batCacheid);
    1298           0 :                                 BBPunfix(uv->batCacheid);
    1299           0 :                                 throw(SQL, "sql.bind", GDK_EXCEPTION);
    1300             :                         }
    1301             : 
    1302      413224 :                         bat *uvl = getArgReference_bat(stk, pci, 1);
    1303             : 
    1304      413224 :                         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      413221 :                                 *bid = e_bat(TYPE_oid);
    1337      414083 :                                 *uvl = e_bat(c->type.type->localtype);
    1338      414209 :                                 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     1296273 :                         int coltype = getBatType(getArgType(mb, pci, 0));
    1349     1296273 :                         b = store->storage_api.bind_col(m->session->tr, c, access);
    1350     1287961 :                         if (b == NULL)
    1351           0 :                                 throw(SQL, "sql.bind", SQLSTATE(42000) "Cannot bind column %s.%s.%s", sname, tname, cname);
    1352             : 
    1353     1287961 :                         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     1287961 :                         BAT* bn = BATslice(b, l, h);
    1359     1284511 :                         if(bn == NULL) {
    1360           0 :                                 BBPunfix(b->batCacheid);
    1361           0 :                                 throw(SQL, "sql.bind", GDK_EXCEPTION);
    1362             :                         }
    1363     1284511 :                         BAThseqbase(bn, l);
    1364     1280288 :                         BBPunfix(b->batCacheid);
    1365     1288431 :                         BBPkeepref(bn);
    1366     1287850 :                         *bid = bn->batCacheid;
    1367             :                 }
    1368             :         }
    1369      348244 :         else if (upd) { /*unpartitioned access to update bats*/
    1370      103353 :                 BAT *ui = NULL, *uv = NULL;
    1371      103353 :                 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      103453 :                 bat *uvl = getArgReference_bat(stk, pci, 1);
    1375      103453 :                 BBPkeepref(ui);
    1376      103448 :                 BBPkeepref(uv);
    1377      103456 :                 *bid = ui->batCacheid;
    1378      103456 :                 *uvl = uv->batCacheid;
    1379             :         }
    1380             :         else { /*unpartitioned access to base column*/
    1381      244891 :                 int coltype = getBatType(getArgType(mb, pci, 0));
    1382      244891 :                 b = store->storage_api.bind_col(m->session->tr, c, access);
    1383      243169 :                 if (b == NULL)
    1384           0 :                         throw(SQL, "sql.bin", "Couldn't bind column");
    1385             : 
    1386      243169 :                 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      243169 :                 BBPkeepref(b);
    1391      243272 :                 *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        3934 : mvc_bind_idxbat_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    1602             : {
    1603        3934 :         int upd = (pci->argc == 7 || pci->argc == 9);
    1604        3934 :         BAT *b = NULL;
    1605        3934 :         bat *bid = getArgReference_bat(stk, pci, 0);
    1606        3934 :         mvc *m = NULL;
    1607        3934 :         str msg;
    1608        3934 :         const char *sname       = *getArgReference_str(stk, pci, 2 + upd);
    1609        3934 :         const char *tname       = *getArgReference_str(stk, pci, 3 + upd);
    1610        3934 :         const char *iname       = *getArgReference_str(stk, pci, 4 + upd);
    1611        3934 :         const int       access  = *getArgReference_int(stk, pci, 5 + upd);
    1612             : 
    1613        3934 :         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        3934 :         assert(access != QUICK);
    1617        3934 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    1618             :                 return msg;
    1619        3933 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    1620             :                 return msg;
    1621        3943 :         sqlstore *store = m->store;
    1622        3943 :         sql_schema *s = mvc_bind_schema(m, sname);
    1623        3941 :         sql_table *t = mvc_bind_table(m, s, tname);
    1624        3953 :         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        3953 :         sql_idx *i = mvc_bind_idx(m, s, iname);
    1628             : 
    1629        3860 :         if (partitioned_access) {
    1630             :                 /* partitioned access */
    1631        2719 :                 int part_nr = *getArgReference_int(stk, pci, 6 + upd);
    1632        2719 :                 int nr_parts = *getArgReference_int(stk, pci, 7 + upd);
    1633        2719 :                 BUN cnt = store->storage_api.count_idx(m->session->tr, i, 0), psz;
    1634        2820 :                 oid l, h;
    1635        2820 :                 psz = cnt ? (cnt / nr_parts) : 0;
    1636        2820 :                 l = part_nr * psz;
    1637        2820 :                 if (l > cnt)
    1638           0 :                         l = cnt;
    1639        2820 :                 h = (part_nr + 1 == nr_parts) ? cnt : ((part_nr + 1) * psz);
    1640        2820 :                 if (h > cnt)
    1641           0 :                         h = cnt;
    1642             : 
    1643        2820 :                 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        2820 :                         int idxtype = getBatType(getArgType(mb, pci, 0));
    1702        2820 :                         b = store->storage_api.bind_idx(m->session->tr, i, access);
    1703             : 
    1704        2817 :                         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        2817 :                         BAT* bn = BATslice(b, l, h);
    1710        2814 :                         if(bn == NULL) {
    1711           0 :                                 BBPunfix(b->batCacheid);
    1712           0 :                                 throw(SQL, "sql.bindidx", GDK_EXCEPTION);
    1713             :                         }
    1714        2814 :                         BAThseqbase(bn, l);
    1715        2806 :                         BBPunfix(b->batCacheid);
    1716        2809 :                         BBPkeepref(bn);
    1717        2811 :                         *bid = bn->batCacheid;
    1718             :                 }
    1719             :         }
    1720        1141 :         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        1084 :                 int idxtype = getBatType(getArgType(mb, pci, 0));
    1733        1084 :                 b = store->storage_api.bind_idx(m->session->tr, i, access);
    1734        1083 :                 if (b == NULL)
    1735           0 :                         throw(SQL,"sql.bindidx", "Couldn't bind index");
    1736             : 
    1737        1083 :                 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        1083 :                 BBPkeepref(b);
    1742        1084 :                 *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), true, ins->ttype);
    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 :         bool isbat = false;
    1766         341 :         BAT *tid = 0, *ins = 0;
    1767         341 :         size_t cnt = 1;
    1768         341 :         oid v = 0;
    1769             : 
    1770         341 :         (void)cntxt;
    1771         341 :         *res = 0;
    1772         341 :         if ((tid = BATdescriptor(Tid)) == NULL)
    1773           0 :                 throw(SQL, "sql.grow", SQLSTATE(HY005) "Cannot access descriptor");
    1774         341 :         if (isaBatType(tpe))
    1775           8 :                 isbat = true;
    1776           8 :         if (isbat && (ins = BATdescriptor(*(bat *) Ins)) == NULL) {
    1777           0 :                 BBPunfix(Tid);
    1778           0 :                 throw(SQL, "sql.grow", SQLSTATE(HY005) "Cannot access descriptor");
    1779             :         }
    1780           8 :         if (ins) {
    1781           8 :                 cnt = BATcount(ins);
    1782           8 :                 BBPunfix(ins->batCacheid);
    1783             :         }
    1784         341 :         if (BATcount(tid)) {
    1785         311 :                 (void)BATmax(tid, &v);
    1786         311 :                 v++;
    1787             :         }
    1788         691 :         for(;cnt>0; cnt--, v++) {
    1789         350 :                 if (BUNappend(tid, &v, false) != GDK_SUCCEED) {
    1790           0 :                         BBPunfix(Tid);
    1791           0 :                         throw(SQL, "sql.grow", GDK_EXCEPTION);
    1792             :                 }
    1793             :         }
    1794         341 :         BBPunfix(Tid);
    1795         341 :         return MAL_SUCCEED;
    1796             : }
    1797             : 
    1798             : /*mvc_append_wrap(int *bid, str *sname, str *tname, str *cname, ptr d) */
    1799             : str
    1800      659508 : mvc_append_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    1801             : {
    1802      659508 :         int *res = getArgReference_int(stk, pci, 0);
    1803      659508 :         mvc *m = NULL;
    1804      659508 :         str msg;
    1805      659508 :         const char *sname = *getArgReference_str(stk, pci, 2);
    1806      659508 :         const char *tname = *getArgReference_str(stk, pci, 3);
    1807      659508 :         const char *cname = *getArgReference_str(stk, pci, 4);
    1808      659508 :         BUN offset = (BUN)*getArgReference_oid(stk, pci, 5);
    1809      659508 :         bat Pos = *getArgReference_bat(stk, pci, 6);
    1810      659508 :         ptr ins = getArgReference(stk, pci, 7);
    1811      659304 :         int tpe = getArgType(mb, pci, 7), log_res = LOG_OK;
    1812      659304 :         bool isbat = false;
    1813      659304 :         sql_schema *s;
    1814      659304 :         sql_table *t;
    1815      659304 :         sql_column *c;
    1816      659304 :         sql_idx *i;
    1817      659304 :         BAT *b = NULL, *pos = NULL;
    1818      659304 :         BUN cnt = 1;
    1819             : 
    1820      659304 :         *res = 0;
    1821      659304 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    1822             :                 return msg;
    1823      659311 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    1824             :                 return msg;
    1825      660305 :         if (isaBatType(tpe)) {
    1826      149622 :                 isbat = true;
    1827      149622 :                 tpe = getBatType(tpe);
    1828             :         }
    1829      660305 :         if (Pos != bat_nil && (pos = BATdescriptor(Pos)) == NULL)
    1830           0 :                 throw(SQL, "sql.append", SQLSTATE(HY005) "Cannot access append positions descriptor");
    1831      660305 :         if (isbat && (ins = BATdescriptor(*(bat *) ins)) == NULL) {
    1832           0 :                 bat_destroy(pos);
    1833           0 :                 throw(SQL, "sql.append", SQLSTATE(HY005) "Cannot access append values descriptor");
    1834             :         }
    1835      661473 :         if (!isbat && ATOMextern(tpe) && !ATOMvarsized(tpe))
    1836          11 :                 ins = *(ptr *) ins;
    1837      149964 :         if (isbat) {
    1838      149964 :                 b =  (BAT*) ins;
    1839      149964 :                 if (VIEWtparent(b) || VIEWvtparent(b)) {
    1840             :                         /* note, b == (BAT*)ins */
    1841         700 :                         b = COLcopy(b, b->ttype, true, TRANSIENT);
    1842         673 :                         BBPreclaim(ins);
    1843         691 :                         ins = b;
    1844         691 :                         if (b == NULL)
    1845           0 :                                 throw(SQL, "sql.append", GDK_EXCEPTION);
    1846             :                 }
    1847             :         }
    1848      661464 :         s = mvc_bind_schema(m, sname);
    1849      661115 :         if (s == NULL) {
    1850           0 :                 bat_destroy(pos);
    1851           0 :                 bat_destroy(b);
    1852           0 :                 throw(SQL, "sql.append", SQLSTATE(3F000) "Schema missing %s",sname);
    1853             :         }
    1854      661115 :         t = mvc_bind_table(m, s, tname);
    1855      664834 :         if (t == NULL) {
    1856           0 :                 bat_destroy(pos);
    1857           0 :                 bat_destroy(b);
    1858           0 :                 throw(SQL, "sql.append", SQLSTATE(42S02) "Table missing %s",tname);
    1859             :         }
    1860      664834 :         if (!isTable(t)) {
    1861           0 :                 bat_destroy(pos);
    1862           0 :                 bat_destroy(b);
    1863           0 :                 throw(SQL, "sql.append", SQLSTATE(42000) "%s '%s' is not persistent", TABLE_TYPE_DESCRIPTION(t->type, t->properties), t->base.name);
    1864             :         }
    1865      664834 :         if (b)
    1866      166506 :                 cnt = BATcount(b);
    1867      664834 :         sqlstore *store = m->session->tr->store;
    1868      664834 :         if (cname[0] != '%' && (c = mvc_bind_column(m, t, cname)) != NULL) {
    1869      665850 :                 log_res = store->storage_api.append_col(m->session->tr, c, offset, pos, ins, cnt, isbat, tpe);
    1870        2175 :         } else if (cname[0] == '%' && (i = mvc_bind_idx(m, s, cname + 1)) != NULL) {
    1871        2175 :                 log_res = store->storage_api.append_idx(m->session->tr, i, offset, pos, ins, cnt, isbat, tpe);
    1872             :         } else {
    1873           0 :                 bat_destroy(pos);
    1874           0 :                 bat_destroy(b);
    1875           0 :                 throw(SQL, "sql.append", SQLSTATE(38000) "Unable to find column or index %s.%s.%s",sname,tname,cname);
    1876             :         }
    1877      661071 :         bat_destroy(pos);
    1878      656192 :         bat_destroy(b);
    1879      657354 :         if (log_res != LOG_OK) /* the conflict case should never happen, but leave it here */
    1880           0 :                 throw(SQL, "sql.append", SQLSTATE(42000) "Append failed %s", log_res == LOG_CONFLICT ? "due to conflict with another transaction" : GDKerrbuf);
    1881             :         return MAL_SUCCEED;
    1882             : }
    1883             : 
    1884             : /*mvc_update_wrap(int *bid, str *sname, str *tname, str *cname, ptr d) */
    1885             : str
    1886        4024 : mvc_update_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    1887             : {
    1888        4024 :         int *res = getArgReference_int(stk, pci, 0);
    1889        4024 :         mvc *m = NULL;
    1890        4024 :         str msg;
    1891        4024 :         const char *sname = *getArgReference_str(stk, pci, 2);
    1892        4024 :         const char *tname = *getArgReference_str(stk, pci, 3);
    1893        4024 :         const char *cname = *getArgReference_str(stk, pci, 4);
    1894        4024 :         bat Tids = *getArgReference_bat(stk, pci, 5);
    1895        4024 :         bat Upd = *getArgReference_bat(stk, pci, 6);
    1896        4024 :         BAT *tids, *upd;
    1897        4024 :         int tpe = getArgType(mb, pci, 6), log_res = LOG_OK;
    1898        4024 :         bool isbat = false;
    1899        4024 :         sql_schema *s;
    1900        4024 :         sql_table *t;
    1901        4024 :         sql_column *c;
    1902        4024 :         sql_idx *i;
    1903             : 
    1904        4024 :         *res = 0;
    1905        4024 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    1906             :                 return msg;
    1907        4024 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    1908             :                 return msg;
    1909        4024 :         if (isaBatType(tpe))
    1910        4024 :                 isbat = true;
    1911             :         else
    1912           0 :                 assert(0);
    1913        4024 :         if (!isbat)
    1914             :                 throw(SQL, "sql.update", SQLSTATE(HY005) "Update values is not a BAT input");
    1915        4024 :         if ((tids = BATdescriptor(Tids)) == NULL)
    1916           0 :                 throw(SQL, "sql.update", SQLSTATE(HY005) "Cannot access update positions descriptor");
    1917        4024 :         if ((upd = BATdescriptor(Upd)) == NULL) {
    1918           0 :                 BBPunfix(tids->batCacheid);
    1919           0 :                 throw(SQL, "sql.update", SQLSTATE(HY005) "Cannot access update values descriptor");
    1920             :         }
    1921        4024 :         s = mvc_bind_schema(m, sname);
    1922        4024 :         if (s == NULL) {
    1923           0 :                 BBPunfix(tids->batCacheid);
    1924           0 :                 BBPunfix(upd->batCacheid);
    1925           0 :                 throw(SQL, "sql.update", SQLSTATE(3F000) "Schema missing %s",sname);
    1926             :         }
    1927        4024 :         t = mvc_bind_table(m, s, tname);
    1928        4024 :         if (t == NULL) {
    1929           0 :                 BBPunfix(tids->batCacheid);
    1930           0 :                 BBPunfix(upd->batCacheid);
    1931           0 :                 throw(SQL, "sql.update", SQLSTATE(42S02) "Table missing %s.%s",sname,tname);
    1932             :         }
    1933        4024 :         if (!isTable(t)) {
    1934           0 :                 BBPunfix(tids->batCacheid);
    1935           0 :                 BBPunfix(upd->batCacheid);
    1936           0 :                 throw(SQL, "sql.update", SQLSTATE(42000) "%s '%s' is not persistent", TABLE_TYPE_DESCRIPTION(t->type, t->properties), t->base.name);
    1937             :         }
    1938        4024 :         sqlstore *store = m->session->tr->store;
    1939        4024 :         if (cname[0] != '%' && (c = mvc_bind_column(m, t, cname)) != NULL) {
    1940        3248 :                 log_res = store->storage_api.update_col(m->session->tr, c, tids, upd, isbat);
    1941         776 :         } else if (cname[0] == '%' && (i = mvc_bind_idx(m, s, cname + 1)) != NULL) {
    1942         776 :                 log_res = store->storage_api.update_idx(m->session->tr, i, tids, upd, isbat);
    1943             :         } else {
    1944           0 :                 BBPunfix(tids->batCacheid);
    1945           0 :                 BBPunfix(upd->batCacheid);
    1946           0 :                 throw(SQL, "sql.update", SQLSTATE(38000) "Unable to find column or index %s.%s.%s",sname,tname,cname);
    1947             :         }
    1948        4024 :         BBPunfix(tids->batCacheid);
    1949        4024 :         BBPunfix(upd->batCacheid);
    1950        4024 :         if (log_res != LOG_OK)
    1951           1 :                 throw(SQL, "sql.update", SQLSTATE(42000) "Update failed%s", log_res == LOG_CONFLICT ? " due to conflict with another transaction" : "");
    1952             :         return MAL_SUCCEED;
    1953             : }
    1954             : 
    1955             : /* str mvc_clear_table_wrap(lng *res, str *sname, str *tname); */
    1956             : str
    1957       41818 : mvc_clear_table_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    1958             : {
    1959       41818 :         sql_schema *s;
    1960       41818 :         sql_table *t;
    1961       41818 :         mvc *m = NULL;
    1962       41818 :         str msg;
    1963       41818 :         BUN clear_res;
    1964       41818 :         lng *res = getArgReference_lng(stk, pci, 0);
    1965       41818 :         const char *sname = *getArgReference_str(stk, pci, 1);
    1966       41818 :         const char *tname = *getArgReference_str(stk, pci, 2);
    1967       41818 :         int restart_sequences = *getArgReference_int(stk, pci, 3);
    1968             : 
    1969       41818 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    1970             :                 return msg;
    1971       41820 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    1972             :                 return msg;
    1973       41819 :         s = mvc_bind_schema(m, sname);
    1974       41817 :         if (s == NULL)
    1975           0 :                 throw(SQL, "sql.clear_table", SQLSTATE(3F000) "Schema missing %s", sname);
    1976       41817 :         t = mvc_bind_table(m, s, tname);
    1977       41818 :         if (t == NULL)
    1978           0 :                 throw(SQL, "sql.clear_table", SQLSTATE(42S02) "Table missing %s.%s", sname,tname);
    1979       41818 :         if (!isTable(t))
    1980           0 :                 throw(SQL, "sql.clear_table", SQLSTATE(42000) "%s '%s' is not persistent", TABLE_TYPE_DESCRIPTION(t->type, t->properties), t->base.name);
    1981       41818 :         clear_res = mvc_clear_table(m, t);
    1982       41823 :         if (clear_res >= BUN_NONE - 1)
    1983       15763 :                 throw(SQL, "sql.clear_table", SQLSTATE(42000) "Table clear failed%s", clear_res == (BUN_NONE - 1) ? " due to conflict with another transaction" : "");
    1984       26060 :         if (restart_sequences) { /* restart the sequences if it's the case */
    1985           2 :                 sql_trans *tr = m->session->tr;
    1986           2 :                 const char *next_value_for = "next value for ";
    1987             : 
    1988           5 :                 for (node *n = ol_first_node(t->columns); n; n = n->next) {
    1989           3 :                         sql_column *col = n->data;
    1990             : 
    1991           3 :                         if (col->def && !strncmp(col->def, next_value_for, strlen(next_value_for))) {
    1992           2 :                                 sql_schema *seqs = NULL;
    1993           2 :                                 sql_sequence *seq = NULL;
    1994           2 :                                 char *schema = NULL, *seq_name = NULL;
    1995             : 
    1996           2 :                                 extract_schema_and_sequence_name(m->ta, col->def + strlen(next_value_for), &schema, &seq_name);
    1997           2 :                                 if (!schema || !seq_name || !(seqs = find_sql_schema(tr, schema)))
    1998           0 :                                         continue;
    1999             : 
    2000             :                                 /* TODO - At the moment the sequence may not be stored in the same schema as the table itself */
    2001           2 :                                 if ((seq = find_sql_sequence(tr, seqs, seq_name))) {
    2002           2 :                                         switch (sql_trans_sequence_restart(tr, seq, seq->start)) {
    2003           0 :                                                 case -1:
    2004           0 :                                                         throw(SQL, "sql.clear_table", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    2005           0 :                                                 case -2:
    2006             :                                                 case -3:
    2007           0 :                                                         throw(SQL, "sql.clear_table", SQLSTATE(HY005) "RESTART SEQUENCE: transaction conflict detected");
    2008           0 :                                                 case -4:
    2009           0 :                                                         throw(SQL, "sql.clear_table", SQLSTATE(HY005) "Could not restart sequence %s.%s", seqs->base.name, seq_name);
    2010             :                                                 default:
    2011             :                                                         break;
    2012             :                                         }
    2013             :                                 }
    2014             :                         }
    2015             :                 }
    2016             :         }
    2017       26060 :         *res = (lng) clear_res;
    2018       26060 :         return MAL_SUCCEED;
    2019             : }
    2020             : 
    2021             : /*mvc_delete_wrap(int *d, str *sname, str *tname, ptr d) */
    2022             : str
    2023         336 : mvc_delete_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    2024             : {
    2025         336 :         int *res = getArgReference_int(stk, pci, 0);
    2026         336 :         mvc *m = NULL;
    2027         336 :         str msg;
    2028         336 :         const char *sname = *getArgReference_str(stk, pci, 2);
    2029         336 :         const char *tname = *getArgReference_str(stk, pci, 3);
    2030         336 :         ptr ins = getArgReference(stk, pci, 4);
    2031         336 :         int tpe = getArgType(mb, pci, 4), log_res;
    2032         336 :         bool isbat = false;
    2033         336 :         BAT *b = NULL;
    2034         336 :         sql_schema *s;
    2035         336 :         sql_table *t;
    2036             : 
    2037         336 :         *res = 0;
    2038         336 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    2039             :                 return msg;
    2040         336 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    2041             :                 return msg;
    2042         336 :         if (isaBatType(tpe))
    2043         336 :                 isbat = true;
    2044         336 :         if (isbat && (b = BATdescriptor(*(bat *) ins)) == NULL)
    2045           0 :                 throw(SQL, "sql.delete", SQLSTATE(HY005) "Cannot access column descriptor");
    2046         336 :         if (!isbat || (b->ttype != TYPE_oid && b->ttype != TYPE_void && b->ttype != TYPE_msk)) {
    2047           0 :                 BBPreclaim(b);
    2048           0 :                 throw(SQL, "sql.delete", SQLSTATE(HY005) "Cannot access column descriptor");
    2049             :         }
    2050         336 :         s = mvc_bind_schema(m, sname);
    2051         336 :         if (s == NULL) {
    2052           0 :                 BBPreclaim(b);
    2053           0 :                 throw(SQL, "sql.delete", SQLSTATE(3F000) "Schema missing %s",sname);
    2054             :         }
    2055         336 :         t = mvc_bind_table(m, s, tname);
    2056         336 :         if (t == NULL) {
    2057           0 :                 BBPreclaim(b);
    2058           0 :                 throw(SQL, "sql.delete", SQLSTATE(42S02) "Table missing %s.%s",sname,tname);
    2059             :         }
    2060         336 :         if (!isTable(t)) {
    2061           0 :                 BBPreclaim(b);
    2062           0 :                 throw(SQL, "sql.delete", SQLSTATE(42000) "%s '%s' is not persistent", TABLE_TYPE_DESCRIPTION(t->type, t->properties), t->base.name);
    2063             :         }
    2064         336 :         sqlstore *store = m->session->tr->store;
    2065         336 :         log_res = store->storage_api.delete_tab(m->session->tr, t, b, isbat);
    2066         336 :         BBPreclaim(b);
    2067         336 :         if (log_res != LOG_OK)
    2068           1 :                 throw(SQL, "sql.delete", SQLSTATE(42000) "Delete failed%s", log_res == LOG_CONFLICT ? " due to conflict with another transaction" : "");
    2069             :         return MAL_SUCCEED;
    2070             : }
    2071             : 
    2072             : static inline BAT *
    2073          13 : setwritable(BAT *b)
    2074             : {
    2075          13 :         if (isVIEW(b)) {
    2076          12 :                 BAT *bn = COLcopy(b, b->ttype, true, TRANSIENT);
    2077          12 :                 BBPunfix(b->batCacheid);
    2078          12 :                 b = bn;
    2079             :         }
    2080          13 :         return b;
    2081             : }
    2082             : 
    2083             : str
    2084      219570 : DELTAbat(bat *result, const bat *col, const bat *uid, const bat *uval)
    2085             : {
    2086      219570 :         BAT *c, *u_id, *u_val, *res;
    2087             : 
    2088      219570 :         if ((u_id = BBPquickdesc(*uid)) == NULL)
    2089           0 :                 throw(MAL, "sql.delta", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
    2090             : 
    2091             :         /* no updates */
    2092      219146 :         if (BATcount(u_id) == 0) {
    2093      219120 :                 BBPretain(*result = *col);
    2094      219120 :                 return MAL_SUCCEED;
    2095             :         }
    2096             : 
    2097          26 :         c = BATdescriptor(*col);
    2098          26 :         if (c == NULL)
    2099           0 :                 throw(MAL, "sql.delta", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
    2100          26 :         if ((res = COLcopy(c, c->ttype, true, TRANSIENT)) == NULL) {
    2101           0 :                 BBPunfix(c->batCacheid);
    2102           0 :                 throw(MAL, "sql.delta", GDK_EXCEPTION);
    2103             :         }
    2104          26 :         BBPunfix(c->batCacheid);
    2105             : 
    2106          26 :         if ((u_val = BATdescriptor(*uval)) == NULL) {
    2107           0 :                 BBPunfix(res->batCacheid);
    2108           0 :                 throw(MAL, "sql.delta", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
    2109             :         }
    2110          26 :         if ((u_id = BATdescriptor(*uid)) == NULL) {
    2111           0 :                 BBPunfix(u_val->batCacheid);
    2112           0 :                 BBPunfix(res->batCacheid);
    2113           0 :                 throw(MAL, "sql.delta", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
    2114             :         }
    2115          26 :         assert(BATcount(u_id) == BATcount(u_val));
    2116          52 :         if (BATcount(u_id) &&
    2117          26 :             BATreplace(res, u_id, u_val, true) != GDK_SUCCEED) {
    2118           0 :                 BBPunfix(u_id->batCacheid);
    2119           0 :                 BBPunfix(u_val->batCacheid);
    2120           0 :                 BBPunfix(res->batCacheid);
    2121           0 :                 throw(MAL, "sql.delta", GDK_EXCEPTION);
    2122             :         }
    2123          26 :         BBPunfix(u_id->batCacheid);
    2124          26 :         BBPunfix(u_val->batCacheid);
    2125             : 
    2126          26 :         *result = res->batCacheid;
    2127          26 :         BBPkeepref(res);
    2128          26 :         return MAL_SUCCEED;
    2129             : }
    2130             : 
    2131             : str
    2132       69629 : DELTAsub(bat *result, const bat *col, const bat *cid, const bat *uid, const bat *uval)
    2133             : {
    2134       69629 :         BAT *c, *cminu = NULL, *u_id, *u_val, *u, *res;
    2135       69629 :         gdk_return ret;
    2136             : 
    2137       69629 :         if ((u_id = BBPquickdesc(*uid)) == NULL)
    2138           0 :                 throw(MAL, "sql.delta", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
    2139             : 
    2140             :         /* no updates */
    2141       69598 :         if (BATcount(u_id) == 0) {
    2142       69462 :                 BBPretain(*result = *col);
    2143       69462 :                 return MAL_SUCCEED;
    2144             :         }
    2145             : 
    2146         136 :         c = BATdescriptor(*col);
    2147         136 :         if (c == NULL)
    2148           0 :                 throw(MAL, "sql.delta", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
    2149         136 :         res = c;
    2150         136 :         if (BATcount(u_id)) {
    2151         136 :                 u_id = BATdescriptor(*uid);
    2152         136 :                 if (!u_id) {
    2153           0 :                         BBPunfix(c->batCacheid);
    2154           0 :                         throw(MAL, "sql.delta", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
    2155             :                 }
    2156         136 :                 cminu = BATdiff(c, u_id, NULL, NULL, false, false, BUN_NONE);
    2157         136 :                 if (!cminu) {
    2158           0 :                         BBPunfix(c->batCacheid);
    2159           0 :                         BBPunfix(u_id->batCacheid);
    2160           0 :                         throw(MAL, "sql.delta", GDK_EXCEPTION);
    2161             :                 }
    2162         136 :                 res = BATproject(cminu, c);
    2163         136 :                 BBPunfix(c->batCacheid);
    2164         136 :                 BBPunfix(cminu->batCacheid);
    2165         136 :                 cminu = NULL;
    2166         136 :                 if (!res) {
    2167           0 :                         BBPunfix(u_id->batCacheid);
    2168           0 :                         throw(MAL, "sql.delta", GDK_EXCEPTION);
    2169             :                 }
    2170         136 :                 c = res;
    2171             : 
    2172         136 :                 if ((u_val = BATdescriptor(*uval)) == NULL) {
    2173           0 :                         BBPunfix(c->batCacheid);
    2174           0 :                         BBPunfix(u_id->batCacheid);
    2175           0 :                         throw(MAL, "sql.delta", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
    2176             :                 }
    2177         136 :                 if (BATcount(u_val)) {
    2178         132 :                         u = BATproject(u_val, u_id);
    2179         132 :                         BBPunfix(u_val->batCacheid);
    2180         132 :                         BBPunfix(u_id->batCacheid);
    2181         132 :                         if (!u) {
    2182           0 :                                 BBPunfix(c->batCacheid);
    2183           0 :                                 throw(MAL, "sql.delta", GDK_EXCEPTION);
    2184             :                         }
    2185             : 
    2186             :                         /* check selected updated values against candidates */
    2187         132 :                         BAT *c_ids = BATdescriptor(*cid);
    2188             : 
    2189         132 :                         if (!c_ids) {
    2190           0 :                                 BBPunfix(c->batCacheid);
    2191           0 :                                 BBPunfix(u->batCacheid);
    2192           0 :                                 throw(MAL, "sql.delta", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
    2193             :                         }
    2194         132 :                         cminu = BATintersect(u, c_ids, NULL, NULL, false, false, BUN_NONE);
    2195         132 :                         BBPunfix(c_ids->batCacheid);
    2196         132 :                         if (cminu == NULL) {
    2197           0 :                                 BBPunfix(c->batCacheid);
    2198           0 :                                 BBPunfix(u->batCacheid);
    2199           0 :                                 throw(MAL, "sql.delta", GDK_EXCEPTION);
    2200             :                         }
    2201         132 :                         BAT *nres;
    2202         132 :                         if ((nres = COLcopy(res, res->ttype, true, TRANSIENT)) == NULL) {
    2203           0 :                                 BBPunfix(res->batCacheid);
    2204           0 :                                 BBPunfix(u->batCacheid);
    2205           0 :                                 BBPunfix(cminu->batCacheid);
    2206           0 :                                 throw(MAL, "sql.delta", GDK_EXCEPTION);
    2207             :                         }
    2208         132 :                         BBPunfix(res->batCacheid);
    2209         132 :                         res = nres;
    2210         132 :                         ret = BATappend(res, u, cminu, true);
    2211         132 :                         BBPunfix(u->batCacheid);
    2212         132 :                         BBPunfix(cminu->batCacheid);
    2213         132 :                         cminu = NULL;
    2214         132 :                         if (ret != GDK_SUCCEED) {
    2215           0 :                                 BBPunfix(res->batCacheid);
    2216           0 :                                 throw(MAL, "sql.delta", GDK_EXCEPTION);
    2217             :                         }
    2218             : 
    2219         132 :                         ret = BATsort(&u, NULL, NULL, res, NULL, NULL, false, false, false);
    2220         132 :                         BBPunfix(res->batCacheid);
    2221         132 :                         if (ret != GDK_SUCCEED) {
    2222           0 :                                 throw(MAL, "sql.delta", GDK_EXCEPTION);
    2223             :                         }
    2224         132 :                         res = u;
    2225             :                 } else {
    2226           4 :                         BBPunfix(u_val->batCacheid);
    2227           4 :                         BBPunfix(u_id->batCacheid);
    2228             :                 }
    2229             :         }
    2230             : 
    2231         136 :         BATkey(res, true);
    2232         136 :         *result = res->batCacheid;
    2233         136 :         BBPkeepref(res);
    2234         136 :         return MAL_SUCCEED;
    2235             : }
    2236             : 
    2237             : str
    2238      200631 : DELTAproject(bat *result, const bat *sub, const bat *col, const bat *uid, const bat *uval)
    2239             : {
    2240      200631 :         BAT *s, *c, *u_id, *u_val, *res, *tres;
    2241             : 
    2242      200631 :         if ((s = BATdescriptor(*sub)) == NULL)
    2243           0 :                 throw(MAL, "sql.delta", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
    2244             : 
    2245      202779 :         if ((c = BATdescriptor(*col)) == NULL) {
    2246           0 :                 BBPunfix(s->batCacheid);
    2247           0 :                 throw(MAL, "sql.delta", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
    2248             :         }
    2249             : 
    2250             :         /* projection(sub,col) */
    2251      202733 :         res = c;
    2252      202733 :         tres = BATproject(s, res);
    2253      200572 :         BBPunfix(res->batCacheid);
    2254             : 
    2255      201879 :         if (tres == NULL) {
    2256           0 :                 BBPunfix(s->batCacheid);
    2257           0 :                 throw(MAL, "sql.projectdelta", GDK_EXCEPTION);
    2258             :         }
    2259      201879 :         res = tres;
    2260             : 
    2261      201879 :         if ((u_id = BATdescriptor(*uid)) == NULL) {
    2262           0 :                 BBPunfix(res->batCacheid);
    2263           0 :                 BBPunfix(s->batCacheid);
    2264           0 :                 throw(MAL, "sql.delta", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
    2265             :         }
    2266      202744 :         if (!BATcount(u_id)) {
    2267      202601 :                 BBPunfix(u_id->batCacheid);
    2268      202617 :                 BBPunfix(s->batCacheid);
    2269      202654 :                 *result = res->batCacheid;
    2270      202654 :                 BBPkeepref(res);
    2271      202654 :                 return MAL_SUCCEED;
    2272             :         }
    2273         143 :         if ((u_val = BATdescriptor(*uval)) == NULL) {
    2274           0 :                 BBPunfix(u_id->batCacheid);
    2275           0 :                 BBPunfix(res->batCacheid);
    2276           0 :                 BBPunfix(s->batCacheid);
    2277           0 :                 throw(MAL, "sql.delta", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
    2278             :         }
    2279             : 
    2280         143 :         if (BATcount(u_val)) {
    2281         143 :                 BAT *os, *ou;
    2282             :                 /* figure out the positions in res that we have to
    2283             :                  * replace with values from u_val */
    2284         143 :                 if (BATsemijoin(&ou, &os, u_id, s, NULL, NULL, false, false, BUN_NONE) != GDK_SUCCEED) {
    2285           0 :                         BBPunfix(s->batCacheid);
    2286           0 :                         BBPunfix(res->batCacheid);
    2287           0 :                         BBPunfix(u_id->batCacheid);
    2288           0 :                         BBPunfix(u_val->batCacheid);
    2289           0 :                         throw(MAL, "sql.delta", GDK_EXCEPTION);
    2290             :                 }
    2291             :                 /* BATcount(ou) == BATcount(os) */
    2292         143 :                 if (BATcount(ou) != 0) {
    2293             :                         /* ou contains the position in u_id/u_val that
    2294             :                          * contain the new values */
    2295          13 :                         BAT *nu_val = BATproject(ou, u_val);
    2296          13 :                         BBPunfix(ou->batCacheid);
    2297             :                         /* os contains the corresponding positions in
    2298             :                          * res that need to be replaced with those new
    2299             :                          * values */
    2300          26 :                         if (!nu_val || (res = setwritable(res)) == NULL ||
    2301          13 :                             BATreplace(res, os, nu_val, false) != GDK_SUCCEED) {
    2302           0 :                                 BBPreclaim(res);
    2303           0 :                                 BBPunfix(os->batCacheid);
    2304           0 :                                 BBPunfix(s->batCacheid);
    2305           0 :                                 BBPunfix(u_id->batCacheid);
    2306           0 :                                 BBPunfix(u_val->batCacheid);
    2307           0 :                                 BBPreclaim(nu_val);
    2308           0 :                                 throw(MAL, "sql.delta", GDK_EXCEPTION);
    2309             :                         }
    2310          13 :                         BBPunfix(nu_val->batCacheid);
    2311             :                 } else {
    2312             :                         /* nothing to replace */
    2313         130 :                         BBPunfix(ou->batCacheid);
    2314             :                 }
    2315         143 :                 BBPunfix(os->batCacheid);
    2316             :         }
    2317         143 :         BBPunfix(s->batCacheid);
    2318         143 :         BBPunfix(u_id->batCacheid);
    2319         143 :         BBPunfix(u_val->batCacheid);
    2320             : 
    2321         143 :         *result = res->batCacheid;
    2322         143 :         BBPkeepref(res);
    2323         143 :         return MAL_SUCCEED;
    2324             : }
    2325             : 
    2326             : str
    2327          26 : BATleftproject(bat *Res, const bat *Col, const bat *L, const bat *R)
    2328             : {
    2329          26 :         BAT *c, *l, *r, *res;
    2330          26 :         oid *p, *lp, *rp;
    2331          26 :         BUN cnt = 0, i;
    2332          26 :         BATiter li, ri;
    2333             : 
    2334          26 :         c = BATdescriptor(*Col);
    2335          26 :         if (c)
    2336          26 :                 cnt = BATcount(c);
    2337          26 :         l = BATdescriptor(*L);
    2338          26 :         r = BATdescriptor(*R);
    2339          26 :         res = COLnew(0, TYPE_oid, cnt, TRANSIENT);
    2340          26 :         if (!c || !l || !r || !res) {
    2341           0 :                 BBPreclaim(c);
    2342           0 :                 BBPreclaim(l);
    2343           0 :                 BBPreclaim(r);
    2344           0 :                 BBPreclaim(res);
    2345           0 :                 throw(MAL, "sql.delta", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
    2346             :         }
    2347          26 :         p = (oid*)Tloc(res,0);
    2348          51 :         for(i=0;i<cnt; i++)
    2349          25 :                 *p++ = oid_nil;
    2350          26 :         BATsetcount(res, cnt);
    2351             : 
    2352          26 :         cnt = BATcount(l);
    2353          26 :         p = (oid*)Tloc(res, 0);
    2354          26 :         li = bat_iterator(l);
    2355          26 :         ri = bat_iterator(r);
    2356          26 :         lp = (oid*)li.base;
    2357          26 :         rp = (oid*)ri.base;
    2358          26 :         if (l->ttype == TYPE_void) {
    2359          11 :                 oid lp = l->tseqbase;
    2360          11 :                 if (r->ttype == TYPE_void) {
    2361          11 :                         oid rp = r->tseqbase;
    2362          11 :                         for(i=0;i<cnt; i++, lp++, rp++)
    2363           0 :                                 p[lp] = rp;
    2364             :                 } else {
    2365           0 :                         for(i=0;i<cnt; i++, lp++)
    2366           0 :                                 p[lp] = rp[i];
    2367             :                 }
    2368             :         }
    2369          26 :         if (r->ttype == TYPE_void) {
    2370          23 :                 oid rp = r->tseqbase;
    2371          35 :                 for(i=0;i<cnt; i++, rp++)
    2372          12 :                         p[lp[i]] = rp;
    2373             :         } else {
    2374           9 :                 for(i=0;i<cnt; i++)
    2375           6 :                         p[lp[i]] = rp[i];
    2376             :         }
    2377          26 :         bat_iterator_end(&li);
    2378          26 :         bat_iterator_end(&ri);
    2379          26 :         res->tsorted = false;
    2380          26 :         res->trevsorted = false;
    2381          26 :         res->tnil = false;
    2382          26 :         res->tnonil = false;
    2383          26 :         res->tkey = false;
    2384          26 :         BBPunfix(c->batCacheid);
    2385          26 :         BBPunfix(l->batCacheid);
    2386          26 :         BBPunfix(r->batCacheid);
    2387          26 :         *Res = res->batCacheid;
    2388          26 :         BBPkeepref(res);
    2389          26 :         return MAL_SUCCEED;
    2390             : }
    2391             : 
    2392             : /* str SQLtid(bat *result, mvc *m, str *sname, str *tname) */
    2393             : str
    2394      284783 : SQLtid(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    2395             : {
    2396      284783 :         bat *res = getArgReference_bat(stk, pci, 0);
    2397      284783 :         mvc *m = NULL;
    2398      284783 :         str msg = MAL_SUCCEED;
    2399      284783 :         sql_trans *tr;
    2400      284783 :         const char *sname = *getArgReference_str(stk, pci, 2);
    2401      284783 :         const char *tname = *getArgReference_str(stk, pci, 3);
    2402      284783 :         sql_schema *s;
    2403      284783 :         sql_table *t;
    2404             : 
    2405      284783 :         *res = bat_nil;
    2406      284783 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    2407             :                 return msg;
    2408      284794 :         tr = m->session->tr;
    2409      284794 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    2410             :                 return msg;
    2411      284872 :         s = mvc_bind_schema(m, sname);
    2412      284803 :         if (s == NULL)
    2413           0 :                 throw(SQL, "sql.tid", SQLSTATE(3F000) "Schema missing %s",sname);
    2414      284803 :         t = mvc_bind_table(m, s, tname);
    2415      284870 :         if (t == NULL)
    2416           0 :                 throw(SQL, "sql.tid", SQLSTATE(42S02) "Table missing %s.%s",sname,tname);
    2417      284870 :         if (!isTable(t))
    2418           0 :                 throw(SQL, "sql.tid", SQLSTATE(42000) "%s '%s' is not persistent",
    2419           0 :                           TABLE_TYPE_DESCRIPTION(t->type, t->properties), t->base.name);
    2420             : 
    2421      284870 :         sqlstore *store = m->store;
    2422             :         /* we have full table count, nr of deleted (unused rows) */
    2423      284870 :         int part_nr = 0;
    2424      284870 :         int nr_parts = 1;
    2425      284870 :         if (pci->argc == 6) {        /* partitioned version */
    2426      172623 :                 part_nr = *getArgReference_int(stk, pci, 4);
    2427      172623 :                 nr_parts = *getArgReference_int(stk, pci, 5);
    2428             :         }
    2429      284870 :         BAT *b = store->storage_api.bind_cands(tr, t, nr_parts, part_nr);
    2430      284730 :         if (b) {
    2431      284730 :                 *res = b->batCacheid;
    2432      284730 :                 BBPkeepref(b);
    2433             :         } else {
    2434           0 :                 msg = createException(SQL, "sql.tid", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    2435             :         }
    2436             :         return msg;
    2437             : }
    2438             : 
    2439             : /* unsafe pattern resultSet(tbl:bat[:str], attr:bat[:str], tpe:bat[:str], len:bat[:int],scale:bat[:int], cols:bat[:any]...) :int */
    2440             : /* New result set rendering infrastructure */
    2441             : 
    2442             : static str
    2443       59653 : mvc_result_set_wrap( Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    2444             : {
    2445       59653 :         int *res_id =getArgReference_int(stk,pci,0);
    2446       59653 :         bat tblId= *getArgReference_bat(stk, pci,1);
    2447       59653 :         bat atrId= *getArgReference_bat(stk, pci,2);
    2448       59653 :         bat tpeId= *getArgReference_bat(stk, pci,3);
    2449       59653 :         bat lenId= *getArgReference_bat(stk, pci,4);
    2450       59653 :         bat scaleId= *getArgReference_bat(stk, pci,5);
    2451       59653 :         bat bid;
    2452       59653 :         int i, res, ok;
    2453       59653 :         const char *tblname, *colname, *tpename;
    2454       59653 :         str msg= MAL_SUCCEED;
    2455       59653 :         int *digits, *scaledigits;
    2456       59653 :         oid o = 0;
    2457       59653 :         BATiter itertbl,iteratr,itertpe,iterdig,iterscl;
    2458       59653 :         backend *be = NULL;
    2459       59653 :         BAT *b = NULL, *tbl = NULL, *atr = NULL, *tpe = NULL,*len = NULL,*scale = NULL;
    2460             : 
    2461       59653 :         if ((msg = getBackendContext(cntxt, &be)) != NULL)
    2462             :                 return msg;
    2463       59656 :         bid = *getArgReference_bat(stk,pci,6);
    2464       59656 :         b = BATdescriptor(bid);
    2465       59656 :         if ( b == NULL) {
    2466           0 :                 msg = createException(SQL, "sql.resultSet", SQLSTATE(HY005) "Cannot access column descriptor");
    2467           0 :                 goto wrapup_result_set;
    2468             :         }
    2469       59656 :         res = *res_id = mvc_result_table(be, mb->tag, pci->argc - (pci->retc + 5), Q_TABLE);
    2470       59656 :         BBPunfix(b->batCacheid);
    2471       59656 :         if (res < 0) {
    2472           0 :                 msg = createException(SQL, "sql.resultSet", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    2473           0 :                 goto wrapup_result_set;
    2474             :         }
    2475             : 
    2476       59656 :         tbl = BATdescriptor(tblId);
    2477       59655 :         atr = BATdescriptor(atrId);
    2478       59656 :         tpe = BATdescriptor(tpeId);
    2479       59656 :         len = BATdescriptor(lenId);
    2480       59656 :         scale = BATdescriptor(scaleId);
    2481       59656 :         if (tbl == NULL || atr == NULL || tpe == NULL || len == NULL || scale == NULL)
    2482           0 :                 goto wrapup_result_set;
    2483             :         /* mimick the old rsColumn approach; */
    2484       59656 :         itertbl = bat_iterator(tbl);
    2485       59654 :         iteratr = bat_iterator(atr);
    2486       59656 :         itertpe = bat_iterator(tpe);
    2487       59656 :         iterdig = bat_iterator(len);
    2488       59656 :         iterscl = bat_iterator(scale);
    2489       59656 :         digits = (int*) iterdig.base;
    2490       59656 :         scaledigits = (int*) iterscl.base;
    2491             : 
    2492      311098 :         for( i = 6; msg == MAL_SUCCEED && i< pci->argc; i++, o++){
    2493      251442 :                 bid = *getArgReference_bat(stk,pci,i);
    2494      251442 :                 tblname = BUNtvar(itertbl,o);
    2495      251441 :                 colname = BUNtvar(iteratr,o);
    2496      251444 :                 tpename = BUNtvar(itertpe,o);
    2497      251444 :                 b = BATdescriptor(bid);
    2498      251462 :                 if ( b == NULL)
    2499           0 :                         msg = createException(SQL, "sql.resultSet", SQLSTATE(HY005) "Cannot access column descriptor");
    2500      251462 :                 else if (mvc_result_column(be, tblname, colname, tpename, *digits++, *scaledigits++, b))
    2501           0 :                         msg = createException(SQL, "sql.resultSet", SQLSTATE(42000) "Cannot access column descriptor %s.%s",tblname,colname);
    2502      251457 :                 if( b)
    2503      251457 :                         BBPunfix(bid);
    2504             :         }
    2505       59656 :         bat_iterator_end(&itertbl);
    2506       59656 :         bat_iterator_end(&iteratr);
    2507       59655 :         bat_iterator_end(&itertpe);
    2508       59656 :         bat_iterator_end(&iterdig);
    2509       59656 :         bat_iterator_end(&iterscl);
    2510             :         /* now send it to the channel cntxt->fdout */
    2511       59653 :         if (bstream_getoob(cntxt->fdin))
    2512           0 :                 msg = createException(SQL, "sql.resultSet", SQLSTATE(HY000) "Query aboted");
    2513       59655 :         else if (!msg && (ok = mvc_export_result(be, cntxt->fdout, res, true, cntxt->qryctx.starttime, mb->optimize)) < 0)
    2514           0 :                 msg = createException(SQL, "sql.resultSet", SQLSTATE(45000) "Result set construction failed: %s", mvc_export_error(be, cntxt->fdout, ok));
    2515       59642 :   wrapup_result_set:
    2516       59642 :         cntxt->qryctx.starttime = 0;
    2517       59642 :         cntxt->qryctx.endtime = 0;
    2518       59642 :         mb->optimize = 0;
    2519       59642 :         if( tbl) BBPunfix(tblId);
    2520       59654 :         if( atr) BBPunfix(atrId);
    2521       59654 :         if( tpe) BBPunfix(tpeId);
    2522       59652 :         if( len) BBPunfix(lenId);
    2523       59652 :         if( scale) BBPunfix(scaleId);
    2524             :         return msg;
    2525             : }
    2526             : 
    2527             : /* Copy the result set into a CSV file */
    2528             : str
    2529          31 : mvc_export_table_wrap( Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    2530             : {
    2531          31 :         int *res_id =getArgReference_int(stk,pci,0);
    2532          31 :         const char *filename = *getArgReference_str(stk,pci,1);
    2533          31 :         const char *format = *getArgReference_str(stk,pci,2);
    2534          31 :         const char *tsep = *getArgReference_str(stk, pci, 3);
    2535          31 :         const char *rsep = *getArgReference_str(stk, pci, 4);
    2536          31 :         const char *ssep = *getArgReference_str(stk, pci, 5);
    2537          31 :         const char *ns = *getArgReference_str(stk, pci, 6);
    2538          31 :         int onclient = *getArgReference_int(stk, pci, 7);
    2539             : 
    2540          31 :         bat tblId= *getArgReference_bat(stk, pci,8);
    2541          31 :         bat atrId= *getArgReference_bat(stk, pci,9);
    2542          31 :         bat tpeId= *getArgReference_bat(stk, pci,10);
    2543          31 :         bat lenId= *getArgReference_bat(stk, pci,11);
    2544          31 :         bat scaleId= *getArgReference_bat(stk, pci,12);
    2545          31 :         stream *s = NULL;
    2546          31 :         bat bid;
    2547          31 :         int i, res, ok;
    2548          31 :         const char *tblname, *colname, *tpename;
    2549          31 :         str msg= MAL_SUCCEED;
    2550          31 :         int *digits, *scaledigits;
    2551          31 :         oid o = 0;
    2552          31 :         BATiter itertbl,iteratr,itertpe,iterdig,iterscl;
    2553          31 :         backend *be;
    2554          31 :         mvc *m = NULL;
    2555          31 :         BAT *b = NULL, *tbl = NULL, *atr = NULL, *tpe = NULL,*len = NULL,*scale = NULL;
    2556          31 :         res_table *t = NULL;
    2557          31 :         bool tostdout;
    2558          31 :         char buf[80];
    2559          31 :         ssize_t sz;
    2560             : 
    2561          31 :         (void) format;
    2562             : 
    2563          31 :         if ((msg = getBackendContext(cntxt, &be)) != NULL)
    2564             :                 return msg;
    2565          31 :         m = be->mvc;
    2566             : 
    2567          31 :         if (onclient && !cntxt->filetrans) {
    2568           0 :                 msg = createException(SQL, "sql.resultSet", SQLSTATE(42000) "Cannot transfer files to client");
    2569           0 :                 goto wrapup_result_set1;
    2570             :         }
    2571             : 
    2572          31 :         bid = *getArgReference_bat(stk,pci,13);
    2573          31 :         res = *res_id = mvc_result_table(be, mb->tag, pci->argc - (pci->retc + 12), Q_TABLE);
    2574          31 :         t = be->results;
    2575          31 :         if (res < 0) {
    2576           0 :                 msg = createException(SQL, "sql.resultSet", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    2577           0 :                 goto wrapup_result_set1;
    2578             :         }
    2579             : 
    2580          31 :         t->tsep = tsep;
    2581          31 :         t->rsep = rsep;
    2582          31 :         t->ssep = ssep;
    2583          31 :         t->ns = ns;
    2584             : 
    2585          31 :         tbl = BATdescriptor(tblId);
    2586          31 :         atr = BATdescriptor(atrId);
    2587          31 :         tpe = BATdescriptor(tpeId);
    2588          31 :         len = BATdescriptor(lenId);
    2589          31 :         scale = BATdescriptor(scaleId);
    2590          31 :         if( tbl == NULL || atr == NULL || tpe == NULL || len == NULL || scale == NULL)
    2591           0 :                 goto wrapup_result_set1;
    2592             :         /* mimick the old rsColumn approach; */
    2593          31 :         itertbl = bat_iterator(tbl);
    2594          31 :         iteratr = bat_iterator(atr);
    2595          31 :         itertpe = bat_iterator(tpe);
    2596          31 :         iterdig = bat_iterator(len);
    2597          31 :         iterscl = bat_iterator(scale);
    2598          31 :         digits = (int*) iterdig.base;
    2599          31 :         scaledigits = (int*) iterscl.base;
    2600             : 
    2601         170 :         for( i = 13; msg == MAL_SUCCEED && i< pci->argc; i++, o++){
    2602         139 :                 bid = *getArgReference_bat(stk,pci,i);
    2603         139 :                 tblname = BUNtvar(itertbl,o);
    2604         139 :                 colname = BUNtvar(iteratr,o);
    2605         139 :                 tpename = BUNtvar(itertpe,o);
    2606         139 :                 b = BATdescriptor(bid);
    2607         139 :                 if ( b == NULL)
    2608           0 :                         msg = createException(SQL, "sql.resultSet", SQLSTATE(HY005) "Cannot access column descriptor");
    2609         139 :                 else if (mvc_result_column(be, tblname, colname, tpename, *digits++, *scaledigits++, b))
    2610           0 :                         msg = createException(SQL, "sql.resultSet", SQLSTATE(42000) "Cannot access column descriptor %s.%s",tblname,colname);
    2611         139 :                 if( b)
    2612         139 :                         BBPunfix(bid);
    2613             :         }
    2614          31 :         bat_iterator_end(&itertbl);
    2615          31 :         bat_iterator_end(&iteratr);
    2616          31 :         bat_iterator_end(&itertpe);
    2617          31 :         bat_iterator_end(&iterdig);
    2618          31 :         bat_iterator_end(&iterscl);
    2619          31 :         if ( msg )
    2620           0 :                 goto wrapup_result_set1;
    2621             : 
    2622             :         /* now select the file channel */
    2623          31 :         if ((tostdout = strcmp(filename,"stdout") == 0)) {
    2624           8 :                 s = cntxt->fdout;
    2625          23 :         } else if (!onclient) {
    2626           4 :                 if ((s = open_wastream(filename)) == NULL || mnstr_errnr(s) != MNSTR_NO__ERROR) {
    2627           0 :                         msg=  createException(IO, "streams.open", SQLSTATE(42000) "%s", mnstr_peek_error(NULL));
    2628           0 :                         close_stream(s);
    2629           0 :                         goto wrapup_result_set1;
    2630             :                 }
    2631           4 :                 be->output_format = OFMT_CSV;
    2632             :         } else {
    2633          19 :                 while (!m->scanner.rs->eof)
    2634           0 :                         bstream_next(m->scanner.rs);
    2635          19 :                 s = m->scanner.ws;
    2636          19 :                 mnstr_write(s, PROMPT3, sizeof(PROMPT3) - 1, 1);
    2637          19 :                 mnstr_printf(s, "w %s\n", filename);
    2638          19 :                 mnstr_flush(s, MNSTR_FLUSH_DATA);
    2639          19 :                 if ((sz = mnstr_readline(m->scanner.rs->s, buf, sizeof(buf))) > 1) {
    2640             :                         /* non-empty line indicates failure on client */
    2641           7 :                         msg = createException(IO, "streams.open", "%s", buf);
    2642             :                         /* discard until client flushes */
    2643          15 :                         while (mnstr_read(m->scanner.rs->s, buf, 1, sizeof(buf)) > 0) {
    2644             :                                 /* ignore remainder of error message */
    2645           8 :                         }
    2646           7 :                         goto wrapup_result_set1;
    2647             :                 }
    2648             :         }
    2649          24 :         if ((ok = mvc_export_result(cntxt->sqlcontext, s, res, tostdout, cntxt->qryctx.starttime, mb->optimize)) < 0) {
    2650           0 :                 msg = createException(SQL, "sql.resultSet", SQLSTATE(45000) "Result set construction failed: %s", mvc_export_error(cntxt->sqlcontext, s, ok));
    2651           0 :                 if (!onclient && !tostdout)
    2652           0 :                         close_stream(s);
    2653           0 :                 if (ok != -5)
    2654           0 :                         goto wrapup_result_set1;
    2655             :         }
    2656          24 :         if (onclient) {
    2657          12 :                 mnstr_flush(s, MNSTR_FLUSH_DATA);
    2658          12 :                 if ((sz = mnstr_readline(m->scanner.rs->s, buf, sizeof(buf))) > 1) {
    2659           0 :                         msg = createException(IO, "streams.open", "%s", buf);
    2660             :                 }
    2661          24 :                 while (sz > 0)
    2662          12 :                         sz = mnstr_readline(m->scanner.rs->s, buf, sizeof(buf));
    2663          12 :         } else if (!tostdout) {
    2664           4 :                 close_stream(s);
    2665             :         }
    2666           8 :   wrapup_result_set1:
    2667          31 :         cntxt->qryctx.starttime = 0;
    2668          31 :         cntxt->qryctx.endtime = 0;
    2669          31 :         mb->optimize = 0;
    2670          31 :         if( tbl) BBPunfix(tblId);
    2671          31 :         if( atr) BBPunfix(atrId);
    2672          31 :         if( tpe) BBPunfix(tpeId);
    2673          31 :         if( len) BBPunfix(lenId);
    2674          31 :         if( scale) BBPunfix(scaleId);
    2675             :         return msg;
    2676             : }
    2677             : 
    2678             : /* unsafe pattern resultSet(tbl:bat[:str], attr:bat[:str], tpe:bat[:str], len:bat[:int],scale:bat[:int], cols:any...) :int */
    2679             : str
    2680         516 : mvc_row_result_wrap( Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    2681             : {
    2682         516 :         int *res_id= getArgReference_int(stk, pci,0);
    2683         516 :         bat tblId= *getArgReference_bat(stk, pci,1);
    2684         516 :         bat atrId= *getArgReference_bat(stk, pci,2);
    2685         516 :         bat tpeId= *getArgReference_bat(stk, pci,3);
    2686         516 :         bat lenId= *getArgReference_bat(stk, pci,4);
    2687         516 :         bat scaleId= *getArgReference_bat(stk, pci,5);
    2688         516 :         int i, res, ok;
    2689         516 :         const char *tblname, *colname, *tpename;
    2690         516 :         str msg= MAL_SUCCEED;
    2691         516 :         int *digits, *scaledigits;
    2692         516 :         oid o = 0;
    2693         516 :         BATiter itertbl,iteratr,itertpe,iterdig,iterscl;
    2694         516 :         backend *be = NULL;
    2695         516 :         ptr v;
    2696         516 :         int mtype;
    2697         516 :         BAT *tbl = NULL, *atr = NULL, *tpe = NULL, *len = NULL, *scale = NULL;
    2698             : 
    2699         516 :         if ((msg = getBackendContext(cntxt, &be)) != NULL)
    2700             :                 return msg;
    2701         516 :         res = *res_id = mvc_result_table(be, mb->tag, pci->argc - (pci->retc + 5), Q_TABLE);
    2702         516 :         if (res < 0) {
    2703           0 :                 msg = createException(SQL, "sql.resultSet", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    2704           0 :                 goto wrapup_result_set;
    2705             :         }
    2706             : 
    2707         516 :         tbl = BATdescriptor(tblId);
    2708         516 :         atr = BATdescriptor(atrId);
    2709         516 :         tpe = BATdescriptor(tpeId);
    2710         516 :         len = BATdescriptor(lenId);
    2711         516 :         scale = BATdescriptor(scaleId);
    2712         516 :         if( tbl == NULL || atr == NULL || tpe == NULL || len == NULL || scale == NULL)
    2713           0 :                 goto wrapup_result_set;
    2714             :         /* mimick the old rsColumn approach; */
    2715         516 :         itertbl = bat_iterator(tbl);
    2716         516 :         iteratr = bat_iterator(atr);
    2717         516 :         itertpe = bat_iterator(tpe);
    2718         516 :         iterdig = bat_iterator(len);
    2719         516 :         iterscl = bat_iterator(scale);
    2720         516 :         digits = (int*) iterdig.base;
    2721         516 :         scaledigits = (int*) iterscl.base;
    2722             : 
    2723        1820 :         for( i = 6; msg == MAL_SUCCEED && i< pci->argc; i++, o++){
    2724        1304 :                 tblname = BUNtvar(itertbl,o);
    2725        1304 :                 colname = BUNtvar(iteratr,o);
    2726        1304 :                 tpename = BUNtvar(itertpe,o);
    2727             : 
    2728        1304 :                 v = getArgReference(stk, pci, i);
    2729        1304 :                 mtype = getArgType(mb, pci, i);
    2730        1304 :                 if (ATOMextern(mtype))
    2731         170 :                         v = *(ptr *) v;
    2732        1304 :                 if ((ok = mvc_result_value(be, tblname, colname, tpename, *digits++, *scaledigits++, v, mtype) < 0)) {
    2733           0 :                         msg = createException(SQL, "sql.rsColumn", SQLSTATE(45000) "Result set construction failed: %s", mvc_export_error(be, be->out, ok));
    2734           0 :                         bat_iterator_end(&itertbl);
    2735           0 :                         bat_iterator_end(&iteratr);
    2736           0 :                         bat_iterator_end(&itertpe);
    2737           0 :                         bat_iterator_end(&iterdig);
    2738           0 :                         bat_iterator_end(&iterscl);
    2739           0 :                         goto wrapup_result_set;
    2740             :                 }
    2741             :         }
    2742         516 :         bat_iterator_end(&itertbl);
    2743         516 :         bat_iterator_end(&iteratr);
    2744         516 :         bat_iterator_end(&itertpe);
    2745         516 :         bat_iterator_end(&iterdig);
    2746         516 :         bat_iterator_end(&iterscl);
    2747         516 :         if (!msg && (ok = mvc_export_result(cntxt->sqlcontext, cntxt->fdout, res, true, cntxt->qryctx.starttime, mb->optimize)) < 0)
    2748           0 :                 msg = createException(SQL, "sql.resultSet", SQLSTATE(45000) "Result set construction failed: %s", mvc_export_error(cntxt->sqlcontext, cntxt->fdout, ok));
    2749         516 :   wrapup_result_set:
    2750         516 :         cntxt->qryctx.starttime = 0;
    2751         516 :         cntxt->qryctx.endtime = 0;
    2752         516 :         mb->optimize = 0;
    2753         516 :         if( tbl) BBPunfix(tblId);
    2754         516 :         if( atr) BBPunfix(atrId);
    2755         516 :         if( tpe) BBPunfix(tpeId);
    2756         516 :         if( len) BBPunfix(lenId);
    2757         516 :         if( scale) BBPunfix(scaleId);
    2758             :         return msg;
    2759             : }
    2760             : 
    2761             : str
    2762           1 : mvc_export_row_wrap( Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    2763             : {
    2764           1 :         int *res_id= getArgReference_int(stk, pci,0);
    2765           1 :         str filename = * getArgReference_str(stk,pci,1);
    2766           1 :         const char *format = *getArgReference_str(stk,pci,2);
    2767           1 :         const char *tsep = *getArgReference_str(stk, pci, 3);
    2768           1 :         const char *rsep = *getArgReference_str(stk, pci, 4);
    2769           1 :         const char *ssep = *getArgReference_str(stk, pci, 5);
    2770           1 :         const char *ns = *getArgReference_str(stk, pci, 6);
    2771           1 :         int onclient = *getArgReference_int(stk, pci, 7);
    2772             : 
    2773           1 :         bat tblId= *getArgReference_bat(stk, pci,8);
    2774           1 :         bat atrId= *getArgReference_bat(stk, pci,9);
    2775           1 :         bat tpeId= *getArgReference_bat(stk, pci,10);
    2776           1 :         bat lenId= *getArgReference_bat(stk, pci,11);
    2777           1 :         bat scaleId= *getArgReference_bat(stk, pci,12);
    2778             : 
    2779           1 :         int i, res, ok;
    2780           1 :         stream *s = NULL;
    2781           1 :         const char *tblname, *colname, *tpename;
    2782           1 :         str msg = MAL_SUCCEED;
    2783           1 :         int *digits, *scaledigits;
    2784           1 :         oid o = 0;
    2785           1 :         BATiter itertbl,iteratr,itertpe,iterdig,iterscl;
    2786           1 :         backend *be;
    2787           1 :         mvc *m = NULL;
    2788           1 :         res_table *t = NULL;
    2789           1 :         ptr v;
    2790           1 :         int mtype;
    2791           1 :         BAT  *tbl = NULL, *atr = NULL, *tpe = NULL,*len = NULL,*scale = NULL;
    2792           1 :         bool tostdout;
    2793           1 :         char buf[80];
    2794           1 :         ssize_t sz;
    2795             : 
    2796           1 :         (void) format;
    2797           1 :         if ((msg = getBackendContext(cntxt, &be)) != NULL)
    2798             :                 return msg;
    2799           1 :         m = be->mvc;
    2800           1 :         if (onclient && !cntxt->filetrans) {
    2801           0 :                 msg = createException(SQL, "sql.resultSet", SQLSTATE(42000) "Cannot transfer files to client");
    2802           0 :                 goto wrapup_result_set;
    2803             :         }
    2804             : 
    2805           1 :         res = *res_id = mvc_result_table(be, mb->tag, pci->argc - (pci->retc + 12), Q_TABLE);
    2806             : 
    2807           1 :         t = be->results;
    2808           1 :         if (res < 0){
    2809           0 :                 msg = createException(SQL, "sql.resultSet", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    2810           0 :                 goto wrapup_result_set;
    2811             :         }
    2812             : 
    2813           1 :         t->tsep = tsep;
    2814           1 :         t->rsep = rsep;
    2815           1 :         t->ssep = ssep;
    2816           1 :         t->ns = ns;
    2817             : 
    2818           1 :         tbl = BATdescriptor(tblId);
    2819           1 :         atr = BATdescriptor(atrId);
    2820           1 :         tpe = BATdescriptor(tpeId);
    2821           1 :         len = BATdescriptor(lenId);
    2822           1 :         scale = BATdescriptor(scaleId);
    2823           1 :         if (tbl == NULL || atr == NULL || tpe == NULL || len == NULL || scale == NULL)
    2824           0 :                 goto wrapup_result_set;
    2825             :         /* mimick the old rsColumn approach; */
    2826           1 :         itertbl = bat_iterator(tbl);
    2827           1 :         iteratr = bat_iterator(atr);
    2828           1 :         itertpe = bat_iterator(tpe);
    2829           1 :         iterdig = bat_iterator(len);
    2830           1 :         iterscl = bat_iterator(scale);
    2831           1 :         digits = (int*) iterdig.base;
    2832           1 :         scaledigits = (int*) iterscl.base;
    2833             : 
    2834           2 :         for( i = 13; msg == MAL_SUCCEED && i< pci->argc; i++, o++){
    2835           1 :                 tblname = BUNtvar(itertbl,o);
    2836           1 :                 colname = BUNtvar(iteratr,o);
    2837           1 :                 tpename = BUNtvar(itertpe,o);
    2838             : 
    2839           1 :                 v = getArgReference(stk, pci, i);
    2840           1 :                 mtype = getArgType(mb, pci, i);
    2841           1 :                 if (ATOMextern(mtype))
    2842           0 :                         v = *(ptr *) v;
    2843           1 :                 if ((ok = mvc_result_value(be, tblname, colname, tpename, *digits++, *scaledigits++, v, mtype)) < 0) {
    2844           0 :                         msg = createException(SQL, "sql.rsColumn", SQLSTATE(45000) "Result set construction failed: %s", mvc_export_error(be, s, ok));
    2845           0 :                         bat_iterator_end(&itertbl);
    2846           0 :                         bat_iterator_end(&iteratr);
    2847           0 :                         bat_iterator_end(&itertpe);
    2848           0 :                         bat_iterator_end(&iterdig);
    2849           0 :                         bat_iterator_end(&iterscl);
    2850           0 :                         goto wrapup_result_set;
    2851             :                 }
    2852             :         }
    2853           1 :         bat_iterator_end(&itertbl);
    2854           1 :         bat_iterator_end(&iteratr);
    2855           1 :         bat_iterator_end(&itertpe);
    2856           1 :         bat_iterator_end(&iterdig);
    2857           1 :         bat_iterator_end(&iterscl);
    2858             :         /* now select the file channel */
    2859           1 :         if ((tostdout = strcmp(filename,"stdout") == 0)) {
    2860           1 :                 s = cntxt->fdout;
    2861           0 :         } else if (!onclient) {
    2862           0 :                 if ((s = open_wastream(filename)) == NULL || mnstr_errnr(s) != MNSTR_NO__ERROR) {
    2863           0 :                         msg=  createException(IO, "streams.open", SQLSTATE(42000) "%s", mnstr_peek_error(NULL));
    2864           0 :                         close_stream(s);
    2865           0 :                         goto wrapup_result_set;
    2866             :                 }
    2867             :         } else {
    2868           0 :                 while (!m->scanner.rs->eof)
    2869           0 :                         bstream_next(m->scanner.rs);
    2870           0 :                 s = m->scanner.ws;
    2871           0 :                 mnstr_write(s, PROMPT3, sizeof(PROMPT3) - 1, 1);
    2872           0 :                 mnstr_printf(s, "w %s\n", filename);
    2873           0 :                 mnstr_flush(s, MNSTR_FLUSH_DATA);
    2874           0 :                 if ((sz = mnstr_readline(m->scanner.rs->s, buf, sizeof(buf))) > 1) {
    2875             :                         /* non-empty line indicates failure on client */
    2876           0 :                         msg = createException(IO, "streams.open", "%s", buf);
    2877             :                         /* discard until client flushes */
    2878           0 :                         while (mnstr_read(m->scanner.rs->s, buf, 1, sizeof(buf)) > 0) {
    2879             :                                 /* ignore remainder of error message */
    2880           0 :                         }
    2881           0 :                         goto wrapup_result_set;
    2882             :                 }
    2883             :         }
    2884           1 :         if ((ok = mvc_export_result(cntxt->sqlcontext, s, res, strcmp(filename, "stdout") == 0, cntxt->qryctx.starttime, mb->optimize)) < 0) {
    2885           0 :                 msg = createException(SQL, "sql.resultSet", SQLSTATE(45000) "Result set construction failed: %s", mvc_export_error(cntxt->sqlcontext, s, ok));
    2886           0 :                 if (!onclient && !tostdout)
    2887           0 :                         close_stream(s);
    2888           0 :                 goto wrapup_result_set;
    2889             :         }
    2890           1 :         if (onclient) {
    2891           0 :                 mnstr_flush(s, MNSTR_FLUSH_DATA);
    2892           0 :                 if ((sz = mnstr_readline(m->scanner.rs->s, buf, sizeof(buf))) > 1) {
    2893           0 :                         msg = createException(IO, "streams.open", "%s", buf);
    2894             :                 }
    2895           0 :                 while (sz > 0)
    2896           0 :                         sz = mnstr_readline(m->scanner.rs->s, buf, sizeof(buf));
    2897           1 :         } else if (!tostdout) {
    2898           0 :                 close_stream(s);
    2899             :         }
    2900           1 :   wrapup_result_set:
    2901           1 :         cntxt->qryctx.starttime = 0;
    2902           1 :         cntxt->qryctx.endtime = 0;
    2903           1 :         mb->optimize = 0;
    2904           1 :         if( tbl) BBPunfix(tblId);
    2905           1 :         if( atr) BBPunfix(atrId);
    2906           1 :         if( tpe) BBPunfix(tpeId);
    2907           1 :         if( len) BBPunfix(lenId);
    2908           1 :         if( scale) BBPunfix(scaleId);
    2909             :         return msg;
    2910             : }
    2911             : 
    2912             : str
    2913       59655 : mvc_table_result_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    2914             : {
    2915       59655 :         str res = MAL_SUCCEED;
    2916       59655 :         backend *be = NULL;
    2917       59655 :         str msg;
    2918       59655 :         int *res_id;
    2919       59655 :         int nr_cols;
    2920       59655 :         mapi_query_t qtype;
    2921             : 
    2922       59655 :         if ( pci->argc > 6)
    2923       59655 :                 return mvc_result_set_wrap(cntxt,mb,stk,pci);
    2924             : 
    2925           0 :         assert(0);
    2926             :         res_id = getArgReference_int(stk, pci, 0);
    2927             :         nr_cols = *getArgReference_int(stk, pci, 1);
    2928             :         qtype = (mapi_query_t) *getArgReference_int(stk, pci, 2);
    2929             :         bat order_bid = *getArgReference_bat(stk, pci, 3);
    2930             :         (void)order_bid;
    2931             :         /* TODO remove use */
    2932             : 
    2933             :         if ((msg = getBackendContext(cntxt, &be)) != NULL)
    2934             :                 return msg;
    2935             :         *res_id = mvc_result_table(be, mb->tag, nr_cols, qtype);
    2936             :         if (*res_id < 0)
    2937             :                 res = createException(SQL, "sql.resultSet", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    2938             :         return res;
    2939             : }
    2940             : 
    2941             : /* str mvc_affected_rows_wrap(int *m, int m, lng *nr, str *w); */
    2942             : str
    2943      136300 : mvc_affected_rows_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    2944             : {
    2945      136300 :         backend *b = NULL;
    2946      136300 :         int *res = getArgReference_int(stk, pci, 0), ok;
    2947             : #ifndef NDEBUG
    2948      136300 :         int mtype = getArgType(mb, pci, 2);
    2949             : #endif
    2950      136300 :         lng nr;
    2951      136300 :         str msg;
    2952             : 
    2953      136300 :         (void) mb;              /* NOT USED */
    2954      136300 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    2955             :                 return msg;
    2956      136663 :         *res = 0;
    2957      136663 :         assert(mtype == TYPE_lng);
    2958      136663 :         nr = *getArgReference_lng(stk, pci, 2);
    2959      136663 :         b = cntxt->sqlcontext;
    2960      136663 :         ok = mvc_export_affrows(b, b->out, nr, "", mb->tag, cntxt->qryctx.starttime, mb->optimize);
    2961      136506 :         cntxt->qryctx.starttime = 0;
    2962      136506 :         cntxt->qryctx.endtime = 0;
    2963      136506 :         mb->optimize = 0;
    2964      136506 :         if (ok < 0)
    2965           0 :                 throw(SQL, "sql.affectedRows", SQLSTATE(45000) "Result set construction failed: %s", mvc_export_error(b, b->out, ok));
    2966             :         return MAL_SUCCEED;
    2967             : }
    2968             : 
    2969             : /* str mvc_export_head_wrap(int *ret, stream **s, int *res_id); */
    2970             : str
    2971           0 : mvc_export_head_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    2972             : {
    2973           0 :         backend *b = NULL;
    2974           0 :         stream **s = (stream **) getArgReference(stk, pci, 1);
    2975           0 :         int res_id = *getArgReference_int(stk, pci, 2), ok;
    2976           0 :         str msg;
    2977             : 
    2978           0 :         (void) mb;              /* NOT USED */
    2979           0 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    2980             :                 return msg;
    2981           0 :         b = cntxt->sqlcontext;
    2982           0 :         ok = mvc_export_head(b, *s, res_id, FALSE, TRUE, cntxt->qryctx.starttime, mb->optimize);
    2983           0 :         cntxt->qryctx.starttime = 0;
    2984           0 :         cntxt->qryctx.endtime = 0;
    2985           0 :         mb->optimize = 0;
    2986           0 :         if (ok < 0)
    2987           0 :                 throw(SQL, "sql.exportHead", SQLSTATE(45000) "Result set construction failed: %s", mvc_export_error(b, *s, ok));
    2988             :         return MAL_SUCCEED;
    2989             : }
    2990             : 
    2991             : /* str mvc_export_result_wrap(int *ret, stream **s, int *res_id); */
    2992             : str
    2993           0 : mvc_export_result_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    2994             : {
    2995           0 :         backend *b = NULL;
    2996           0 :         stream **s = (stream **) getArgReference(stk, pci, 1), *sout;
    2997           0 :         int res_id = *getArgReference_int(stk, pci, 2), ok;
    2998           0 :         str msg;
    2999             : 
    3000           0 :         (void) mb;              /* NOT USED */
    3001           0 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    3002             :                 return msg;
    3003           0 :         b = cntxt->sqlcontext;
    3004           0 :         sout = pci->argc > 5 ? cntxt->fdout : *s;
    3005           0 :         ok = mvc_export_result(b, sout, res_id, false, cntxt->qryctx.starttime, mb->optimize);
    3006           0 :         cntxt->qryctx.starttime = 0;
    3007           0 :         cntxt->qryctx.endtime = 0;
    3008           0 :         mb->optimize = 0;
    3009           0 :         if (ok < 0)
    3010           0 :                 throw(SQL, "sql.exportResult", SQLSTATE(45000) "Result set construction failed: %s", mvc_export_error(b, sout, ok));
    3011             :         return MAL_SUCCEED;
    3012             : }
    3013             : 
    3014             : /* str mvc_export_chunk_wrap(int *ret, stream **s, int *res_id, str *w); */
    3015             : str
    3016           0 : mvc_export_chunk_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    3017             : {
    3018           0 :         backend *b = NULL;
    3019           0 :         stream **s = (stream **) getArgReference(stk, pci, 1);
    3020           0 :         int res_id = *getArgReference_int(stk, pci, 2), ok;
    3021           0 :         BUN offset = 0;
    3022           0 :         BUN nr = 0;
    3023           0 :         str msg;
    3024             : 
    3025           0 :         (void) mb;              /* NOT USED */
    3026           0 :         if (pci->argc == 5) {
    3027           0 :                 offset = (BUN) *getArgReference_int(stk, pci, 3);
    3028           0 :                 int cnt = *getArgReference_int(stk, pci, 4);
    3029           0 :                 nr = cnt < 0 ? BUN_NONE : (BUN) cnt;
    3030             :         }
    3031             : 
    3032           0 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    3033             :                 return msg;
    3034           0 :         b = cntxt->sqlcontext;
    3035           0 :         if ((ok = mvc_export_chunk(b, *s, res_id, offset, nr)) < 0)
    3036           0 :                 throw(SQL, "sql.exportChunk", SQLSTATE(45000) "Result set construction failed: %s", mvc_export_error(b, *s, ok));
    3037             :         return NULL;
    3038             : }
    3039             : 
    3040             : /* str mvc_export_operation_wrap(int *ret, str *w); */
    3041             : str
    3042       17973 : mvc_export_operation_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    3043             : {
    3044       17973 :         backend *b = NULL;
    3045       17973 :         str msg;
    3046       17973 :         int ok = 0;
    3047             : 
    3048       17973 :         (void) stk;             /* NOT USED */
    3049       17973 :         (void) pci;             /* NOT USED */
    3050       17973 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    3051             :                 return msg;
    3052       17973 :         b = cntxt->sqlcontext;
    3053       17973 :         if (b->out)
    3054       17972 :                 ok = mvc_export_operation(b, b->out, "", cntxt->qryctx.starttime, mb->optimize);
    3055       17973 :         cntxt->qryctx.starttime = 0;
    3056       17973 :         cntxt->qryctx.endtime = 0;
    3057       17973 :         mb->optimize = 0;
    3058       17973 :         if (ok < 0)
    3059           0 :                 throw(SQL, "sql.exportOperation", SQLSTATE(45000) "Result set construction failed: %s", mvc_export_error(b, b->out, ok));
    3060             :         return MAL_SUCCEED;
    3061             : }
    3062             : 
    3063             : str
    3064             : /*mvc_scalar_value_wrap(int *ret, int *qtype, str tn, str name, str type, int *digits, int *scale, int *eclass, ptr p, int mtype)*/
    3065       60210 : mvc_scalar_value_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    3066             : {
    3067       60210 :         const char *tn = *getArgReference_str(stk, pci, 1);
    3068       60210 :         const char *cn = *getArgReference_str(stk, pci, 2);
    3069       60210 :         const char *type = *getArgReference_str(stk, pci, 3);
    3070       60210 :         int digits = *getArgReference_int(stk, pci, 4);
    3071       60210 :         int scale = *getArgReference_int(stk, pci, 5);
    3072       60210 :         ptr p = getArgReference(stk, pci, 7);
    3073       60210 :         int mtype = getArgType(mb, pci, 7);
    3074       60210 :         str msg;
    3075       60210 :         backend *be = NULL;
    3076       60210 :         int res_id, ok;
    3077       60210 :         (void) mb;              /* NOT USED */
    3078       60210 :         if ((msg = getBackendContext(cntxt, &be)) != NULL)
    3079             :                 return msg;
    3080       60211 :         if (ATOMextern(mtype))
    3081         579 :                 p = *(ptr *) p;
    3082             : 
    3083             :         // scalar values are single-column result sets
    3084       60211 :         if ((res_id = mvc_result_table(be, mb->tag, 1, Q_TABLE)) < 0) {
    3085           0 :                 cntxt->qryctx.starttime = 0;
    3086           0 :                 cntxt->qryctx.endtime = 0;
    3087           0 :                 mb->optimize = 0;
    3088           0 :                 throw(SQL, "sql.exportValue", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3089             :         }
    3090       60211 :         if ((ok = mvc_result_value(be, tn, cn, type, digits, scale, p, mtype)) < 0) {
    3091           0 :                 cntxt->qryctx.starttime = 0;
    3092           0 :                 cntxt->qryctx.endtime = 0;
    3093           0 :                 mb->optimize = 0;
    3094           0 :                 throw(SQL, "sql.exportValue", SQLSTATE(45000) "Result set construction failed: %s", mvc_export_error(be, be->out, ok));
    3095             :         }
    3096       60198 :         if (be->output_format == OFMT_NONE) {
    3097           2 :                 cntxt->qryctx.starttime = 0;
    3098           2 :                 cntxt->qryctx.endtime = 0;
    3099           2 :                 mb->optimize = 0;
    3100           2 :                 return MAL_SUCCEED;
    3101             :         }
    3102       60196 :         ok = mvc_export_result(be, be->out, res_id, true, cntxt->qryctx.starttime, mb->optimize);
    3103       60158 :         cntxt->qryctx.starttime = 0;
    3104       60158 :         cntxt->qryctx.endtime = 0;
    3105       60158 :         mb->optimize = 0;
    3106       60158 :         if (ok < 0)
    3107           0 :                 throw(SQL, "sql.exportValue", SQLSTATE(45000) "Result set construction failed: %s", mvc_export_error(be, be->out, ok));
    3108             :         return MAL_SUCCEED;
    3109             : }
    3110             : 
    3111             : static void
    3112        1046 : bat2return(MalStkPtr stk, InstrPtr pci, BAT **b)
    3113             : {
    3114        1046 :         int i;
    3115             : 
    3116       10999 :         for (i = 0; i < pci->retc; i++) {
    3117        9953 :                 *getArgReference_bat(stk, pci, i) = b[i]->batCacheid;
    3118        9953 :                 BBPkeepref(b[i]);
    3119             :         }
    3120        1046 : }
    3121             : 
    3122             : static const char fwftsep[2] = {STREAM_FWF_FIELD_SEP, '\0'};
    3123             : static const char fwfrsep[2] = {STREAM_FWF_RECORD_SEP, '\0'};
    3124             : 
    3125             : /* 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); */
    3126             : str
    3127        1072 : mvc_import_table_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    3128             : {
    3129        1072 :         backend *be;
    3130        1072 :         BAT **b = NULL;
    3131        1072 :         sql_table *t = *(sql_table **) getArgReference(stk, pci, pci->retc + 0);
    3132        1072 :         const char *tsep = *getArgReference_str(stk, pci, pci->retc + 1);
    3133        1072 :         const char *rsep = *getArgReference_str(stk, pci, pci->retc + 2);
    3134        1072 :         const char *ssep = *getArgReference_str(stk, pci, pci->retc + 3);
    3135        1072 :         const char *ns = *getArgReference_str(stk, pci, pci->retc + 4);
    3136        1072 :         const char *fname = *getArgReference_str(stk, pci, pci->retc + 5);
    3137        1072 :         lng sz = *getArgReference_lng(stk, pci, pci->retc + 6);
    3138        1072 :         lng offset = *getArgReference_lng(stk, pci, pci->retc + 7);
    3139        1072 :         int besteffort = *getArgReference_int(stk, pci, pci->retc + 8);
    3140        1072 :         const char *fixed_widths = *getArgReference_str(stk, pci, pci->retc + 9);
    3141        1072 :         int onclient = *getArgReference_int(stk, pci, pci->retc + 10);
    3142        1072 :         bool escape = *getArgReference_int(stk, pci, pci->retc + 11);
    3143        1072 :         const char *decsep = *getArgReference_str(stk, pci, pci->retc + 12);
    3144        1072 :         const char *decskip = *getArgReference_str(stk, pci, pci->retc + 13);
    3145        1072 :         str msg = MAL_SUCCEED;
    3146        1072 :         bstream *s = NULL;
    3147        1072 :         stream *ss;
    3148             : 
    3149        1072 :         (void) mb;              /* NOT USED */
    3150        1072 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    3151             :                 return msg;
    3152        1072 :         if (onclient && !cntxt->filetrans)
    3153           0 :                 throw(MAL, "sql.copy_from", SQLSTATE(42000) "Cannot transfer files from client");
    3154        1072 :         if (strNil(decsep))
    3155           0 :                 throw(MAL, "sql.copy_from", SQLSTATE(42000) "decimal separator cannot be nil");
    3156        1078 :         if (strNil(decskip))
    3157             :                 decskip = NULL;
    3158             : 
    3159        1072 :         be = cntxt->sqlcontext;
    3160             :         /* The CSV parser expects ssep to have the value 0 if the user does not
    3161             :          * specify a quotation character
    3162             :          */
    3163        1672 :         if (*ssep == 0 || strNil(ssep))
    3164             :                 ssep = NULL;
    3165             : 
    3166        1072 :         if (strNil(fname))
    3167         742 :                 fname = NULL;
    3168        1072 :         if (fname == NULL) {
    3169         742 :                 msg = mvc_import_table(cntxt, &b, be->mvc, be->mvc->scanner.rs, t, tsep, rsep, ssep, ns, sz, offset, besteffort, true, escape, decsep, decskip);
    3170             :         } else {
    3171         330 :                 if (onclient) {
    3172          46 :                         ss = mapi_request_upload(fname, false, be->mvc->scanner.rs, be->mvc->scanner.ws);
    3173             :                 } else {
    3174         284 :                         ss = open_rastream(fname);
    3175             :                 }
    3176         330 :                 if (ss == NULL || mnstr_errnr(ss) != MNSTR_NO__ERROR) {
    3177           2 :                         msg = createException(IO, "sql.copy_from", SQLSTATE(42000) "%s", mnstr_peek_error(NULL));
    3178           2 :                         close_stream(ss);
    3179           2 :                         return msg;
    3180             :                 }
    3181             : 
    3182         656 :                 if (!strNil(fixed_widths)) {
    3183           1 :                         size_t ncol = 0, current_width_entry = 0, i;
    3184           1 :                         size_t *widths;
    3185           1 :                         const char* val_start = fixed_widths;
    3186           1 :                         size_t width_len = strlen(fixed_widths);
    3187           1 :                         stream *ns;
    3188             : 
    3189          23 :                         for (i = 0; i < width_len; i++) {
    3190          22 :                                 if (fixed_widths[i] == STREAM_FWF_FIELD_SEP) {
    3191          11 :                                         ncol++;
    3192             :                                 }
    3193             :                         }
    3194           1 :                         widths = malloc(sizeof(size_t) * ncol);
    3195           1 :                         if (!widths) {
    3196           0 :                                 close_stream(ss);
    3197           0 :                                 throw(MAL, "sql.copy_from", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3198             :                         }
    3199          23 :                         for (i = 0; i < width_len; i++) {
    3200          22 :                                 if (fixed_widths[i] == STREAM_FWF_FIELD_SEP) {
    3201          11 :                                         widths[current_width_entry++] = (size_t) strtoll(val_start, NULL, 10);
    3202          11 :                                         val_start = fixed_widths + i + 1;
    3203             :                                 }
    3204             :                         }
    3205             :                         /* overwrite other delimiters to the ones the FWF stream uses */
    3206           1 :                         tsep = fwftsep;
    3207           1 :                         rsep = fwfrsep;
    3208             : 
    3209           1 :                         ns = stream_fwf_create(ss, ncol, widths, STREAM_FWF_FILLER);
    3210           1 :                         if (ns == NULL || mnstr_errnr(ns) != MNSTR_NO__ERROR) {
    3211           0 :                                 msg = createException(IO, "sql.copy_from", SQLSTATE(42000) "%s", mnstr_peek_error(NULL));
    3212           0 :                                 close_stream(ss);
    3213           0 :                                 free(widths);
    3214           0 :                                 return msg;
    3215             :                         }
    3216             :                         ss = ns;
    3217             :                 }
    3218             : #if SIZEOF_VOID_P == 4
    3219             :                 s = bstream_create(ss, 0x20000);
    3220             : #else
    3221         328 :                 s = bstream_create(ss, 0x200000);
    3222             : #endif
    3223         328 :                 if (s == NULL) {
    3224           0 :                         close_stream(ss);
    3225           0 :                         throw(MAL, "sql.copy_from", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3226             :                 }
    3227         328 :                 msg = mvc_import_table(cntxt, &b, be->mvc, s, t, tsep, rsep, ssep, ns, sz, offset, besteffort, false, escape, decsep, decskip);
    3228             :                 // This also closes ss:
    3229         328 :                 bstream_destroy(s);
    3230             :         }
    3231        1070 :         if (b && !msg)
    3232        1046 :                 bat2return(stk, pci, b);
    3233        1070 :         GDKfree(b);
    3234        1070 :         return msg;
    3235             : }
    3236             : 
    3237             : str
    3238         238 : not_unique(bit *ret, const bat *bid)
    3239             : {
    3240         238 :         BAT *b;
    3241             : 
    3242         238 :         if ((b = BATdescriptor(*bid)) == NULL) {
    3243           0 :                 throw(SQL, "not_unique", SQLSTATE(HY005) "Cannot access column descriptor");
    3244             :         }
    3245             : 
    3246         238 :         *ret = FALSE;
    3247         238 :         BATiter bi = bat_iterator(b);
    3248         238 :         if (bi.key || BATtdensebi(&bi) || bi.count <= 1) {
    3249         233 :                 bat_iterator_end(&bi);
    3250         233 :                 BBPunfix(b->batCacheid);
    3251         233 :                 return MAL_SUCCEED;
    3252           5 :         } else if (bi.sorted) {
    3253           5 :                 BUN p;
    3254           5 :                 oid c = ((oid *) bi.base)[0];
    3255             : 
    3256           6 :                 for (p = 1; p < bi.count; p++) {
    3257           6 :                         oid v = ((oid *) bi.base)[p];
    3258           6 :                         if (v <= c) {
    3259           5 :                                 *ret = TRUE;
    3260           5 :                                 break;
    3261             :                         }
    3262           1 :                         c = v;
    3263             :                 }
    3264             :         } else {
    3265           0 :                 bat_iterator_end(&bi);
    3266           0 :                 BBPunfix(b->batCacheid);
    3267           0 :                 throw(SQL, "not_unique", SQLSTATE(42000) "Input column should be sorted");
    3268             :         }
    3269           5 :         bat_iterator_end(&bi);
    3270           5 :         BBPunfix(b->batCacheid);
    3271           5 :         return MAL_SUCCEED;
    3272             : }
    3273             : 
    3274             : /* row case */
    3275             : str
    3276          27 : SQLidentity(oid *ret, const void *i)
    3277             : {
    3278          27 :         (void)i;
    3279          27 :         *ret = 0;
    3280          27 :         return MAL_SUCCEED;
    3281             : }
    3282             : 
    3283             : str
    3284          28 : BATSQLidentity(bat *ret, const bat *bid)
    3285             : {
    3286          28 :         return BKCmirror(ret, bid);
    3287             : }
    3288             : 
    3289             : str
    3290           4 : PBATSQLidentity(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    3291             : {
    3292           4 :         bat *res = getArgReference_bat(stk, pci, 0);
    3293           4 :         oid *ns = getArgReference_oid(stk, pci, 1);
    3294           4 :         bat bid = *getArgReference_bat(stk, pci, 2);
    3295           4 :         oid s = *getArgReference_oid(stk, pci, 3);
    3296           4 :         BAT *b, *bn = NULL;
    3297             : 
    3298           4 :         (void) cntxt;
    3299           4 :         (void) mb;
    3300           4 :         if (!(b = BBPquickdesc(bid)))
    3301           0 :                 throw(MAL, "batcalc.identity", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
    3302           4 :         if (!(bn = BATdense(b->hseqbase, s, BATcount(b))))
    3303           0 :                 throw(MAL, "batcalc.identity", GDK_EXCEPTION);
    3304           4 :         *ns = s + BATcount(b);
    3305           4 :         *res = bn->batCacheid;
    3306           4 :         BBPkeepref(bn);
    3307           4 :         return MAL_SUCCEED;
    3308             : }
    3309             : 
    3310             : /*
    3311             :  * The core modules of Monet provide just a limited set of
    3312             :  * mathematical operators. The extensions required to support
    3313             :  * SQL-99 are shown below. At some point they also should be
    3314             :  * moved to module code base.
    3315             :  */
    3316             : 
    3317             : str
    3318          10 : SQLcst_alpha_cst(dbl *res, const dbl *decl, const dbl *theta)
    3319             : {
    3320          10 :         dbl s, c1, c2;
    3321          10 :         char *msg = MAL_SUCCEED;
    3322          10 :         if (is_dbl_nil(*decl) || is_dbl_nil(*theta)) {
    3323           0 :                 *res = dbl_nil;
    3324          10 :         } else if (fabs(*decl) + *theta > 89.9) {
    3325           0 :                 *res = 180.0;
    3326             :         } else {
    3327          10 :                 s = sin(radians(*theta));
    3328          10 :                 c1 = cos(radians(*decl - *theta));
    3329          10 :                 c2 = cos(radians(*decl + *theta));
    3330          10 :                 *res = degrees(fabs(atan(s / sqrt(fabs(c1 * c2)))));
    3331             :         }
    3332          10 :         return msg;
    3333             : }
    3334             : 
    3335             : /*
    3336             :   sql5_export str SQLcst_alpha_cst(dbl *res, dbl *decl, dbl *theta);
    3337             :   sql5_export str SQLbat_alpha_cst(bat *res, bat *decl, dbl *theta);
    3338             :   sql5_export str SQLcst_alpha_bat(bat *res, dbl *decl, bat *theta);
    3339             : */
    3340             : str
    3341           0 : SQLbat_alpha_cst(bat *res, const bat *decl, const dbl *theta)
    3342             : {
    3343           0 :         BAT *b, *bn;
    3344           0 :         BUN p, q;
    3345           0 :         dbl s, c1, c2, r;
    3346           0 :         char *msg = NULL;
    3347             : 
    3348           0 :         if (is_dbl_nil(*theta)) {
    3349           0 :                 throw(SQL, "SQLbat_alpha", SQLSTATE(42000) "Parameter theta should not be nil");
    3350             :         }
    3351           0 :         if ((b = BATdescriptor(*decl)) == NULL) {
    3352           0 :                 throw(SQL, "alpha", SQLSTATE(HY005) "Cannot access column descriptor");
    3353             :         }
    3354           0 :         bn = COLnew(b->hseqbase, TYPE_dbl, BATcount(b), TRANSIENT);
    3355           0 :         if (bn == NULL) {
    3356           0 :                 BBPunfix(b->batCacheid);
    3357           0 :                 throw(SQL, "sql.alpha", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3358             :         }
    3359           0 :         s = sin(radians(*theta));
    3360           0 :         BATiter bi = bat_iterator(b);
    3361           0 :         const dbl *vals = (const dbl *) bi.base;
    3362           0 :         BATloop(b, p, q) {
    3363           0 :                 dbl d = vals[p];
    3364           0 :                 if (is_dbl_nil(d))
    3365           0 :                         r = dbl_nil;
    3366           0 :                 else if (fabs(d) + *theta > 89.9)
    3367           0 :                         r = 180.0;
    3368             :                 else {
    3369           0 :                         c1 = cos(radians(d - *theta));
    3370           0 :                         c2 = cos(radians(d + *theta));
    3371           0 :                         r = degrees(fabs(atan(s / sqrt(fabs(c1 * c2)))));
    3372             :                 }
    3373           0 :                 if (BUNappend(bn, &r, false) != GDK_SUCCEED) {
    3374           0 :                         BBPreclaim(bn);
    3375           0 :                         bat_iterator_end(&bi);
    3376           0 :                         BBPunfix(b->batCacheid);
    3377           0 :                         throw(SQL, "sql.alpha", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3378             :                 }
    3379             :         }
    3380           0 :         bat_iterator_end(&bi);
    3381           0 :         *res = bn->batCacheid;
    3382           0 :         BBPkeepref(bn);
    3383           0 :         BBPunfix(b->batCacheid);
    3384           0 :         return msg;
    3385             : }
    3386             : 
    3387             : str
    3388           0 : SQLcst_alpha_bat(bat *res, const dbl *decl, const bat *thetabid)
    3389             : {
    3390           0 :         BAT *b, *bn;
    3391           0 :         BUN p, q;
    3392           0 :         dbl s, c1, c2, r;
    3393           0 :         char *msg = NULL;
    3394           0 :         dbl *thetas;
    3395             : 
    3396           0 :         if ((b = BATdescriptor(*thetabid)) == NULL) {
    3397           0 :                 throw(SQL, "alpha", SQLSTATE(HY005) "Cannot access column descriptor");
    3398             :         }
    3399           0 :         bn = COLnew(b->hseqbase, TYPE_dbl, BATcount(b), TRANSIENT);
    3400           0 :         if (bn == NULL) {
    3401           0 :                 BBPunfix(b->batCacheid);
    3402           0 :                 throw(SQL, "sql.alpha", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3403             :         }
    3404           0 :         BATiter bi = bat_iterator(b);
    3405           0 :         thetas = (dbl *) bi.base;
    3406           0 :         BATloop(b, p, q) {
    3407           0 :                 dbl d = *decl;
    3408           0 :                 dbl theta = thetas[p];
    3409             : 
    3410           0 :                 if (is_dbl_nil(d))
    3411           0 :                         r = dbl_nil;
    3412           0 :                 else if (fabs(d) + theta > 89.9)
    3413           0 :                         r = (dbl) 180.0;
    3414             :                 else {
    3415           0 :                         s = sin(radians(theta));
    3416           0 :                         c1 = cos(radians(d - theta));
    3417           0 :                         c2 = cos(radians(d + theta));
    3418           0 :                         r = degrees(fabs(atan(s / sqrt(fabs(c1 * c2)))));
    3419             :                 }
    3420           0 :                 if (BUNappend(bn, &r, false) != GDK_SUCCEED) {
    3421           0 :                         BBPreclaim(bn);
    3422           0 :                         bat_iterator_end(&bi);
    3423           0 :                         BBPunfix(b->batCacheid);
    3424           0 :                         throw(SQL, "sql.alpha", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3425             :                 }
    3426             :         }
    3427           0 :         bat_iterator_end(&bi);
    3428           0 :         *res = bn->batCacheid;
    3429           0 :         BBPkeepref(bn);
    3430           0 :         BBPunfix(b->batCacheid);
    3431           0 :         return msg;
    3432             : }
    3433             : 
    3434             : /* str dump_cache(int *r); */
    3435             : str
    3436           0 : dump_cache(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    3437             : {
    3438           0 :         mvc *m = NULL;
    3439           0 :         str msg;
    3440           0 :         int cnt;
    3441           0 :         cq *q = NULL;
    3442           0 :         BAT *query, *count;
    3443           0 :         bat *rquery = getArgReference_bat(stk, pci, 0);
    3444           0 :         bat *rcount = getArgReference_bat(stk, pci, 1);
    3445             : 
    3446           0 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    3447             :                 return msg;
    3448           0 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    3449             :                 return msg;
    3450           0 :         cnt = m->qc->id;
    3451           0 :         query = COLnew(0, TYPE_str, cnt, TRANSIENT);
    3452           0 :         if (query == NULL)
    3453           0 :                 throw(SQL, "sql.dumpcache", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3454           0 :         count = COLnew(0, TYPE_int, cnt, TRANSIENT);
    3455           0 :         if (count == NULL) {
    3456           0 :                 BBPunfix(query->batCacheid);
    3457           0 :                 throw(SQL, "sql.dumpcache", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3458             :         }
    3459             : 
    3460           0 :         for (q = m->qc->q; q; q = q->next) {
    3461           0 :                 if (BUNappend(query, q->f->query, false) != GDK_SUCCEED ||
    3462           0 :                     BUNappend(count, &q->count, false) != GDK_SUCCEED) {
    3463           0 :                         BBPunfix(query->batCacheid);
    3464           0 :                         BBPunfix(count->batCacheid);
    3465           0 :                         throw(SQL, "sql.dumpcache", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3466             :                 }
    3467             :         }
    3468           0 :         *rquery = query->batCacheid;
    3469           0 :         *rcount = count->batCacheid;
    3470           0 :         BBPkeepref(query);
    3471           0 :         BBPkeepref(count);
    3472           0 :         return MAL_SUCCEED;
    3473             : }
    3474             : 
    3475             : /* str dump_opt_stats(int *r); */
    3476             : str
    3477           0 : dump_opt_stats(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    3478             : {
    3479           0 :         backend *be;
    3480           0 :         str msg;
    3481           0 :         int cnt;
    3482           0 :         BAT *rewrite, *count;
    3483           0 :         bat *rrewrite = getArgReference_bat(stk, pci, 0);
    3484           0 :         bat *rcount = getArgReference_bat(stk, pci, 1);
    3485             : 
    3486           0 :         (void)mb;
    3487           0 :         if ((msg = getBackendContext(cntxt, &be)) != NULL)
    3488             :                 return msg;
    3489           0 :         cnt = be->mvc->qc->id;
    3490           0 :         rewrite = COLnew(0, TYPE_str, cnt, TRANSIENT);
    3491           0 :         count = COLnew(0, TYPE_int, cnt, TRANSIENT);
    3492           0 :         if (rewrite == NULL || count == NULL) {
    3493           0 :                 BBPreclaim(rewrite);
    3494           0 :                 BBPreclaim(count);
    3495           0 :                 throw(SQL, "sql.optstats", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3496             :         }
    3497             : 
    3498           0 :         if (BUNappend(rewrite, "joinidx", false) != GDK_SUCCEED ||
    3499           0 :             BUNappend(count, &be->join_idx, false) != GDK_SUCCEED) {
    3500           0 :                 BBPreclaim(rewrite);
    3501           0 :                 BBPreclaim(count);
    3502           0 :                 throw(SQL, "sql.optstats", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3503             :         }
    3504             :         /* TODO add other rewrites */
    3505             : 
    3506           0 :         *rrewrite = rewrite->batCacheid;
    3507           0 :         *rcount = count->batCacheid;
    3508           0 :         BBPkeepref(rewrite);
    3509           0 :         BBPkeepref(count);
    3510           0 :         return MAL_SUCCEED;
    3511             : }
    3512             : 
    3513             : /* str dump_opt_stats(int *r); */
    3514             : str
    3515          51 : dump_trace(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    3516             : {
    3517          51 :         int i;
    3518          51 :         BAT *t[3];
    3519             : 
    3520          51 :         (void) cntxt;
    3521          51 :         (void) mb;
    3522          51 :         if (TRACEtable(cntxt, t) != 3)
    3523           0 :                 throw(SQL, "sql.dump_trace", SQLSTATE(3F000) "Profiler not started");
    3524         204 :         for (i = 0; i < 3; i++) {
    3525         153 :                 *getArgReference_bat(stk, pci, i) = t[i]->batCacheid;
    3526         153 :                 BBPkeepref(t[i]);
    3527             :         }
    3528             :         return MAL_SUCCEED;
    3529             : }
    3530             : 
    3531             : static str
    3532          25 : sql_sessions_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    3533             : {
    3534          25 :         return CLTsessions(cntxt, mb, stk, pci);
    3535             : }
    3536             : 
    3537             : str
    3538          71 : sql_querylog_catalog(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    3539             : {
    3540          71 :         int i;
    3541          71 :         BAT *t[8];
    3542          71 :         str msg;
    3543             : 
    3544          71 :         (void) cntxt;
    3545          71 :         (void) mb;
    3546          71 :         msg = QLOGcatalog(t);
    3547          71 :         if( msg != MAL_SUCCEED)
    3548             :                 return msg;
    3549         639 :         for (i = 0; i < 8; i++)
    3550         568 :         if( t[i]){
    3551         568 :                 *getArgReference_bat(stk, pci, i) = t[i]->batCacheid;
    3552         568 :                 BBPkeepref(t[i]);
    3553             :         } else
    3554           0 :                 throw(SQL,"sql.querylog", SQLSTATE(45000) "Missing query catalog BAT");
    3555             :         return MAL_SUCCEED;
    3556             : }
    3557             : 
    3558             : str
    3559          55 : sql_querylog_calls(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    3560             : {
    3561          55 :         int i;
    3562          55 :         BAT *t[10];
    3563          55 :         str msg;
    3564             : 
    3565          55 :         (void) cntxt;
    3566          55 :         (void) mb;
    3567          55 :         msg = QLOGcalls(t);
    3568          55 :         if( msg != MAL_SUCCEED)
    3569             :                 return msg;
    3570         550 :         for (i = 0; i < 9; i++)
    3571         495 :         if( t[i]){
    3572         495 :                 *getArgReference_bat(stk, pci, i) = t[i]->batCacheid;
    3573         495 :                 BBPkeepref(t[i]);
    3574             :         } else
    3575           0 :                 throw(SQL,"sql.querylog", SQLSTATE(45000) "Missing query call BAT");
    3576             :         return MAL_SUCCEED;
    3577             : }
    3578             : 
    3579             : str
    3580           1 : sql_querylog_empty(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    3581             : {
    3582           1 :         (void) cntxt;
    3583           1 :         (void) mb;
    3584           1 :         (void) stk;
    3585           1 :         (void) pci;
    3586           1 :         return QLOGempty(NULL);
    3587             : }
    3588             : 
    3589             : /* str sql_rowid(oid *rid, ptr v, str *sname, str *tname); */
    3590             : str
    3591           0 : sql_rowid(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    3592             : {
    3593           0 :         BAT *b;
    3594           0 :         mvc *m = NULL;
    3595           0 :         str msg;
    3596           0 :         sql_schema *s = NULL;
    3597           0 :         sql_table *t = NULL;
    3598           0 :         sql_column *c = NULL;
    3599           0 :         oid *rid = getArgReference_oid(stk, pci, 0);
    3600           0 :         const char *sname = *getArgReference_str(stk, pci, 2);
    3601           0 :         const char *tname = *getArgReference_str(stk, pci, 3);
    3602             : 
    3603           0 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    3604             :                 return msg;
    3605           0 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    3606             :                 return msg;
    3607           0 :         s = mvc_bind_schema(m, sname);
    3608           0 :         if (s == NULL)
    3609           0 :                 throw(SQL, "calc.rowid", SQLSTATE(3F000) "Schema missing %s", sname);
    3610           0 :         t = mvc_bind_table(m, s, tname);
    3611           0 :         if (t == NULL)
    3612           0 :                 throw(SQL, "calc.rowid", SQLSTATE(42S02) "Table missing %s.%s",sname,tname);
    3613           0 :         if (!isTable(t))
    3614           0 :                 throw(SQL, "calc.rowid", SQLSTATE(42000) "%s '%s' is not persistent",
    3615           0 :                           TABLE_TYPE_DESCRIPTION(t->type, t->properties), t->base.name);
    3616           0 :         if (!ol_first_node(t->columns))
    3617           0 :                 throw(SQL, "calc.rowid", SQLSTATE(42S22) "Column missing %s.%s",sname,tname);
    3618           0 :         c = ol_first_node(t->columns)->data;
    3619             :         /* HACK, get insert bat */
    3620           0 :         sqlstore *store = m->session->tr->store;
    3621           0 :         b = store->storage_api.bind_col(m->session->tr, c, QUICK);
    3622           0 :         if( b == NULL)
    3623           0 :                 throw(SQL,"calc.rowid", SQLSTATE(HY005) "Cannot access column descriptor");
    3624             :         /* UGH (move into storage backends!!) */
    3625           0 :         *rid = BATcount(b);
    3626           0 :         return MAL_SUCCEED;
    3627             : }
    3628             : 
    3629             : static str
    3630           0 : do_sql_rank_grp(bat *rid, const bat *bid, const bat *gid, int nrank, int dense, const char *name)
    3631             : {
    3632           0 :         BAT *r, *b, *g;
    3633           0 :         BUN p, q;
    3634           0 :         BATiter bi, gi;
    3635           0 :         int (*ocmp) (const void *, const void *);
    3636           0 :         int (*gcmp) (const void *, const void *);
    3637           0 :         const void *oc, *gc, *on, *gn;
    3638           0 :         int rank = 1;
    3639           0 :         int c;
    3640             : 
    3641           0 :         if ((b = BATdescriptor(*bid)) == NULL)
    3642           0 :                 throw(SQL, name, SQLSTATE(HY005) "Cannot access column descriptor");
    3643           0 :         if ((g = BATdescriptor(*gid)) == NULL) {
    3644           0 :                 BBPunfix(b->batCacheid);
    3645           0 :                 throw(SQL, name, SQLSTATE(HY005) "Cannot access column descriptor");
    3646             :         }
    3647           0 :         bi = bat_iterator(b);
    3648           0 :         gi = bat_iterator(g);
    3649           0 :         ocmp = ATOMcompare(b->ttype);
    3650           0 :         gcmp = ATOMcompare(g->ttype);
    3651           0 :         oc = BUNtail(bi, 0);
    3652           0 :         gc = BUNtail(gi, 0);
    3653           0 :         if (!ALIGNsynced(b, g)) {
    3654           0 :                 bat_iterator_end(&bi);
    3655           0 :                 bat_iterator_end(&gi);
    3656           0 :                 BBPunfix(b->batCacheid);
    3657           0 :                 BBPunfix(g->batCacheid);
    3658           0 :                 throw(SQL, name, SQLSTATE(45000) "Internal error, columns not aligned");
    3659             :         }
    3660             : /*
    3661             :   if (!b->tsorted) {
    3662             :   BBPunfix(b->batCacheid);
    3663             :   BBPunfix(g->batCacheid);
    3664             :   throw(SQL, name, SQLSTATE(45000) "Internal error, columns not sorted");
    3665             :   }
    3666             : */
    3667           0 :         r = COLnew(b->hseqbase, TYPE_int, BATcount(b), TRANSIENT);
    3668           0 :         if (r == NULL) {
    3669           0 :                 bat_iterator_end(&bi);
    3670           0 :                 bat_iterator_end(&gi);
    3671           0 :                 BBPunfix(b->batCacheid);
    3672           0 :                 BBPunfix(g->batCacheid);
    3673           0 :                 throw(SQL, name, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3674             :         }
    3675           0 :         BATloop(b, p, q) {
    3676           0 :                 on = BUNtail(bi, p);
    3677           0 :                 gn = BUNtail(gi, p);
    3678             : 
    3679           0 :                 if ((c = ocmp(on, oc)) != 0)
    3680           0 :                         rank = nrank;
    3681           0 :                 if (gcmp(gn, gc) != 0)
    3682           0 :                         c = rank = nrank = 1;
    3683           0 :                 oc = on;
    3684           0 :                 gc = gn;
    3685           0 :                 if (BUNappend(r, &rank, false) != GDK_SUCCEED) {
    3686           0 :                         bat_iterator_end(&bi);
    3687           0 :                         bat_iterator_end(&gi);
    3688           0 :                         BBPunfix(b->batCacheid);
    3689           0 :                         BBPunfix(g->batCacheid);
    3690           0 :                         BBPunfix(r->batCacheid);
    3691           0 :                         throw(SQL, name, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3692             :                 }
    3693           0 :                 nrank += !dense || c;
    3694             :         }
    3695           0 :         bat_iterator_end(&bi);
    3696           0 :         bat_iterator_end(&gi);
    3697           0 :         BBPunfix(b->batCacheid);
    3698           0 :         BBPunfix(g->batCacheid);
    3699           0 :         *rid = r->batCacheid;
    3700           0 :         BBPkeepref(r);
    3701           0 :         return MAL_SUCCEED;
    3702             : }
    3703             : 
    3704             : static str
    3705           0 : do_sql_rank(bat *rid, const bat *bid, int nrank, int dense, const char *name)
    3706             : {
    3707           0 :         BAT *r, *b;
    3708           0 :         BATiter bi;
    3709           0 :         int (*cmp) (const void *, const void *);
    3710           0 :         const void *cur, *n;
    3711           0 :         BUN p, q;
    3712           0 :         int rank = 1;
    3713           0 :         int c;
    3714             : 
    3715           0 :         if ((b = BATdescriptor(*bid)) == NULL)
    3716           0 :                 throw(SQL, name, SQLSTATE(HY005) "Cannot access column descriptor");
    3717           0 :         bi = bat_iterator(b);
    3718           0 :         if (!bi.sorted && !bi.revsorted) {
    3719           0 :                 bat_iterator_end(&bi);
    3720           0 :                 BBPunfix(b->batCacheid);
    3721           0 :                 throw(SQL, name, SQLSTATE(45000) "Internal error, columns not sorted");
    3722             :         }
    3723             : 
    3724           0 :         cmp = ATOMcompare(bi.type);
    3725           0 :         cur = BUNtail(bi, 0);
    3726           0 :         r = COLnew(b->hseqbase, TYPE_int, BATcount(b), TRANSIENT);
    3727           0 :         if (r == NULL) {
    3728           0 :                 bat_iterator_end(&bi);
    3729           0 :                 BBPunfix(b->batCacheid);
    3730           0 :                 throw(SQL, name, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3731             :         }
    3732           0 :         if (BATtdensebi(&bi)) {
    3733           0 :                 BATloop(b, p, q) {
    3734           0 :                         if (BUNappend(r, &rank, false) != GDK_SUCCEED)
    3735           0 :                                 goto bailout;
    3736           0 :                         rank++;
    3737             :                 }
    3738             :         } else {
    3739           0 :                 BATloop(b, p, q) {
    3740           0 :                         n = BUNtail(bi, p);
    3741           0 :                         if ((c = cmp(n, cur)) != 0)
    3742           0 :                                 rank = nrank;
    3743           0 :                         cur = n;
    3744           0 :                         if (BUNappend(r, &rank, false) != GDK_SUCCEED)
    3745           0 :                                 goto bailout;
    3746           0 :                         nrank += !dense || c;
    3747             :                 }
    3748             :         }
    3749           0 :         bat_iterator_end(&bi);
    3750           0 :         BBPunfix(b->batCacheid);
    3751           0 :         *rid = r->batCacheid;
    3752           0 :         BBPkeepref(r);
    3753           0 :         return MAL_SUCCEED;
    3754           0 :   bailout:
    3755           0 :         bat_iterator_end(&bi);
    3756           0 :         BBPunfix(b->batCacheid);
    3757           0 :         BBPunfix(r->batCacheid);
    3758           0 :         throw(SQL, name, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3759             : }
    3760             : 
    3761             : str
    3762           0 : sql_rank_grp(bat *rid, const bat *bid, const bat *gid, const bat *gpe)
    3763             : {
    3764           0 :         (void) gpe;
    3765           0 :         return do_sql_rank_grp(rid, bid, gid, 1, 0, "sql.rank_grp");
    3766             : }
    3767             : 
    3768             : str
    3769           0 : sql_dense_rank_grp(bat *rid, const bat *bid, const bat *gid, const bat *gpe)
    3770             : {
    3771           0 :         (void) gpe;
    3772           0 :         return do_sql_rank_grp(rid, bid, gid, 2, 1, "sql.dense_rank_grp");
    3773             : }
    3774             : 
    3775             : str
    3776           0 : sql_rank(bat *rid, const bat *bid)
    3777             : {
    3778           0 :         return do_sql_rank(rid, bid, 1, 0, "sql.rank");
    3779             : }
    3780             : 
    3781             : str
    3782           0 : sql_dense_rank(bat *rid, const bat *bid)
    3783             : {
    3784           0 :         return do_sql_rank(rid, bid, 2, 1, "sql.dense_rank");
    3785             : }
    3786             : 
    3787             : str
    3788           5 : SQLargRecord(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    3789             : {
    3790           5 :         str s, t, *ret;
    3791             : 
    3792           5 :         (void) cntxt;
    3793           5 :         ret = getArgReference_str(stk, pci, 0);
    3794           5 :         s = instruction2str(mb, stk, getInstrPtr(mb, 0), LIST_MAL_CALL);
    3795           5 :         if(s == NULL)
    3796           0 :                 throw(SQL, "sql.argRecord", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3797           5 :         t = strchr(s, ' ');
    3798           5 :         if( ! t)
    3799           0 :                 t = strchr(s, '\t');
    3800           5 :         *ret = GDKstrdup(t ? t + 1 : s);
    3801           5 :         GDKfree(s);
    3802           5 :         if(*ret == NULL)
    3803           0 :                 throw(SQL, "sql.argRecord", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3804             :         return MAL_SUCCEED;
    3805             : }
    3806             : 
    3807             : /*
    3808             :  * The drop_hash operation cleans up any hash indices on any of the tables columns.
    3809             :  */
    3810             : str
    3811           0 : SQLdrop_hash(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    3812             : {
    3813           0 :         const char *sch = *getArgReference_str(stk, pci, 1);
    3814           0 :         const char *tbl = *getArgReference_str(stk, pci, 2);
    3815           0 :         sql_schema *s;
    3816           0 :         sql_table *t;
    3817           0 :         mvc *m = NULL;
    3818           0 :         str msg;
    3819             : 
    3820           0 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    3821             :                 return msg;
    3822           0 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    3823             :                 return msg;
    3824           0 :         s = mvc_bind_schema(m, sch);
    3825           0 :         if (s == NULL)
    3826           0 :                 throw(SQL, "sql.drop_hash", SQLSTATE(3F000) "Schema missing %s",sch);
    3827           0 :         if (!mvc_schema_privs(m, s))
    3828           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);
    3829           0 :         t = mvc_bind_table(m, s, tbl);
    3830           0 :         if (t == NULL)
    3831           0 :                 throw(SQL, "sql.drop_hash", SQLSTATE(42S02) "Table missing %s.%s",sch, tbl);
    3832           0 :         if (!isTable(t))
    3833           0 :                 throw(SQL, "sql.drop_hash", SQLSTATE(42000) "%s '%s' is not persistent",
    3834           0 :                           TABLE_TYPE_DESCRIPTION(t->type, t->properties), t->base.name);
    3835             : 
    3836           0 :         sqlstore *store = m->session->tr->store;
    3837           0 :         for (node *n = ol_first_node(t->columns); n; n = n->next) {
    3838           0 :                 sql_column *c = n->data;
    3839           0 :                 BAT *b = NULL, *nb = NULL;
    3840             : 
    3841           0 :                 if (!(b = store->storage_api.bind_col(m->session->tr, c, RDONLY)))
    3842           0 :                         throw(SQL, "sql.drop_hash", SQLSTATE(HY005) "Cannot access column descriptor");
    3843           0 :                 if (VIEWtparent(b) && (nb = BBP_desc(VIEWtparent(b)))) {
    3844           0 :                         BBPunfix(b->batCacheid);
    3845           0 :                         if (!(b = BATdescriptor(nb->batCacheid)))
    3846           0 :                                 throw(SQL, "sql.drop_hash", SQLSTATE(HY005) "Cannot access column descriptor");
    3847             :                 }
    3848           0 :                 HASHdestroy(b);
    3849           0 :                 BBPunfix(b->batCacheid);
    3850             :         }
    3851             :         return MAL_SUCCEED;
    3852             : }
    3853             : 
    3854             : /* after an update on the optimizer catalog, we have to change
    3855             :  * the internal optimizer pipe line administration
    3856             :  * The minimal and default pipelines may not be changed.
    3857             :  */
    3858             : str
    3859           0 : SQLoptimizersUpdate(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    3860             : {
    3861           0 :         mvc *m = NULL;
    3862           0 :         str msg;
    3863             : 
    3864           0 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    3865             :                 return msg;
    3866           0 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    3867             :                 return msg;
    3868             :         /* find the optimizer pipeline */
    3869           0 :         (void) stk;
    3870           0 :         (void) pci;
    3871           0 :         throw(SQL, "updateOptimizer", SQLSTATE(0A000) PROGRAM_NYI);
    3872             : }
    3873             : 
    3874             : /*
    3875             :  * Inspection of the actual storage footprint is a recurring question of users.
    3876             :  * This is modelled as a generic SQL table producing function.
    3877             :  * create function storage()
    3878             :  * 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)
    3879             :  * external name sql.storage;
    3880             :  */
    3881             : str
    3882         296 : sql_storage(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    3883             : {
    3884         296 :         BAT *sch, *tab, *col, *type, *loc, *cnt, *atom, *size, *heap, *indices, *phash, *sort, *imprints, *mode, *revsort, *key, *oidx, *bs = NULL;
    3885         296 :         BATiter bsi = bat_iterator(NULL);
    3886         296 :         mvc *m = NULL;
    3887         296 :         str msg = MAL_SUCCEED;
    3888         296 :         sql_trans *tr;
    3889         296 :         node *ncol;
    3890         296 :         int w;
    3891         296 :         bit bitval;
    3892         296 :         bat *rsch = getArgReference_bat(stk, pci, 0);
    3893         296 :         bat *rtab = getArgReference_bat(stk, pci, 1);
    3894         296 :         bat *rcol = getArgReference_bat(stk, pci, 2);
    3895         296 :         bat *rtype = getArgReference_bat(stk, pci, 3);
    3896         296 :         bat *rmode = getArgReference_bat(stk, pci, 4);
    3897         296 :         bat *rloc = getArgReference_bat(stk, pci, 5);
    3898         296 :         bat *rcnt = getArgReference_bat(stk, pci, 6);
    3899         296 :         bat *ratom = getArgReference_bat(stk, pci, 7);
    3900         296 :         bat *rsize = getArgReference_bat(stk, pci, 8);
    3901         296 :         bat *rheap = getArgReference_bat(stk, pci, 9);
    3902         296 :         bat *rindices = getArgReference_bat(stk, pci, 10);
    3903         296 :         bat *rphash = getArgReference_bat(stk, pci, 11);
    3904         296 :         bat *rimprints = getArgReference_bat(stk, pci, 12);
    3905         296 :         bat *rsort = getArgReference_bat(stk, pci, 13);
    3906         296 :         bat *rrevsort = getArgReference_bat(stk, pci, 14);
    3907         296 :         bat *rkey = getArgReference_bat(stk, pci, 15);
    3908         296 :         bat *roidx = getArgReference_bat(stk, pci, 16);
    3909         296 :         str sname = 0;
    3910         296 :         str tname = 0;
    3911         296 :         str cname = 0;
    3912         296 :         struct os_iter si = {0};
    3913             : 
    3914         296 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    3915             :                 return msg;
    3916         296 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    3917             :                 return msg;
    3918             : 
    3919         296 :         if( pci->argc - pci->retc >= 1) {
    3920          53 :                 sname = *getArgReference_str(stk, pci, pci->retc);
    3921          53 :                 if (strNil(sname))
    3922           0 :                         throw(SQL, "sql.storage", SQLSTATE(42000) "Schema name cannot be NULL");
    3923             :         }
    3924         296 :         if( pci->argc - pci->retc >= 2) {
    3925          53 :                 tname = *getArgReference_str(stk, pci, pci->retc + 1);
    3926          53 :                 if (strNil(tname))
    3927           0 :                         throw(SQL, "sql.storage", SQLSTATE(42000) "Table name cannot be NULL");
    3928             :         }
    3929         296 :         if( pci->argc - pci->retc >= 3) {
    3930          31 :                 cname = *getArgReference_str(stk, pci, pci->retc + 2);
    3931          31 :                 if (strNil(cname))
    3932           0 :                         throw(SQL, "sql.storage", SQLSTATE(42000) "Column name cannot be NULL");
    3933             :         }
    3934             : 
    3935         296 :         tr = m->session->tr;
    3936         296 :         sqlstore *store = tr->store;
    3937         296 :         sch = COLnew(0, TYPE_str, 0, TRANSIENT);
    3938         296 :         tab = COLnew(0, TYPE_str, 0, TRANSIENT);
    3939         296 :         col = COLnew(0, TYPE_str, 0, TRANSIENT);
    3940         296 :         type = COLnew(0, TYPE_str, 0, TRANSIENT);
    3941         296 :         mode = COLnew(0, TYPE_str, 0, TRANSIENT);
    3942         296 :         loc = COLnew(0, TYPE_str, 0, TRANSIENT);
    3943         296 :         cnt = COLnew(0, TYPE_lng, 0, TRANSIENT);
    3944         296 :         atom = COLnew(0, TYPE_int, 0, TRANSIENT);
    3945         296 :         size = COLnew(0, TYPE_lng, 0, TRANSIENT);
    3946         296 :         heap = COLnew(0, TYPE_lng, 0, TRANSIENT);
    3947         296 :         indices = COLnew(0, TYPE_lng, 0, TRANSIENT);
    3948         296 :         phash = COLnew(0, TYPE_bit, 0, TRANSIENT);
    3949         296 :         imprints = COLnew(0, TYPE_lng, 0, TRANSIENT);
    3950         296 :         sort = COLnew(0, TYPE_bit, 0, TRANSIENT);
    3951         296 :         revsort = COLnew(0, TYPE_bit, 0, TRANSIENT);
    3952         296 :         key = COLnew(0, TYPE_bit, 0, TRANSIENT);
    3953         296 :         oidx = COLnew(0, TYPE_lng, 0, TRANSIENT);
    3954             : 
    3955         296 :         if (sch == NULL || tab == NULL || col == NULL || type == NULL || mode == NULL || loc == NULL || imprints == NULL ||
    3956         296 :             sort == NULL || cnt == NULL || atom == NULL || size == NULL || heap == NULL || indices == NULL || phash == NULL ||
    3957         296 :             revsort == NULL || key == NULL || oidx == NULL) {
    3958           0 :                 msg = createException(SQL, "sql.storage", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    3959           0 :                 goto bailout;
    3960             :         }
    3961             : 
    3962             :         /* check for limited storage tables */
    3963         296 :         os_iterator(&si, tr->cat->schemas, tr, NULL);
    3964        2440 :         for (sql_base *b = oi_next(&si); b; b = oi_next(&si)) {
    3965        2144 :                 sql_schema *s = (sql_schema *) b;
    3966        2144 :                 if ((sname && strcmp(b->name, sname) ) || b->name[0] == '%')
    3967         614 :                         continue;
    3968        1530 :                 if (s->tables) {
    3969        1530 :                         struct os_iter oi;
    3970             : 
    3971        1530 :                         os_iterator(&oi, s->tables, tr, NULL);
    3972       42524 :                         for (sql_base *bt = oi_next(&oi); bt; bt = oi_next(&oi)) {
    3973       40994 :                                 sql_table *t = (sql_table *) bt;
    3974       40994 :                                 if( tname && strcmp(bt->name, tname) )
    3975        6268 :                                         continue;
    3976       34726 :                                 if (isTable(t)) {
    3977       10912 :                                         if (ol_first_node(t->columns)) {
    3978       66466 :                                                 for (ncol = ol_first_node((t)->columns); ncol; ncol = ncol->next) {
    3979       55554 :                                                         sql_base *bc = ncol->data;
    3980       55554 :                                                         sql_column *c = (sql_column *) ncol->data;
    3981       55554 :                                                         lng sz;
    3982             : 
    3983       55554 :                                                         if( cname && strcmp(bc->name, cname) )
    3984          48 :                                                                 continue;
    3985       55506 :                                                         bs = store->storage_api.bind_col(tr, c, QUICK);
    3986       55506 :                                                         if (bs == NULL) {
    3987           0 :                                                                 msg = createException(SQL, "sql.storage", SQLSTATE(HY005) "Cannot access column descriptor");
    3988           0 :                                                                 goto bailout;
    3989             :                                                         }
    3990             : 
    3991       55506 :                                                         bsi = bat_iterator(bs);
    3992             :                                                         /*printf("schema %s.%s.%s" , b->name, bt->name, bc->name); */
    3993      111012 :                                                         if (BUNappend(sch, b->name, false) != GDK_SUCCEED ||
    3994      111012 :                                                             BUNappend(tab, bt->name, false) != GDK_SUCCEED ||
    3995       55506 :                                                             BUNappend(col, bc->name, false) != GDK_SUCCEED)
    3996           0 :                                                                 goto bailout1;
    3997       55506 :                                                         if (c->t->access == TABLE_WRITABLE) {
    3998       50885 :                                                                 if (BUNappend(mode, "writable", false) != GDK_SUCCEED)
    3999           0 :                                                                         goto bailout1;
    4000        4621 :                                                         } else if (c->t->access == TABLE_APPENDONLY) {
    4001           0 :                                                                 if (BUNappend(mode, "appendonly", false) != GDK_SUCCEED)
    4002           0 :                                                                         goto bailout1;
    4003        4621 :                                                         } else if (c->t->access == TABLE_READONLY) {
    4004        4621 :                                                                 if (BUNappend(mode, "readonly", false) != GDK_SUCCEED)
    4005           0 :                                                                         goto bailout1;
    4006             :                                                         } else {
    4007           0 :                                                                 if (BUNappend(mode, str_nil, false) != GDK_SUCCEED)
    4008           0 :                                                                         goto bailout1;
    4009             :                                                         }
    4010       55506 :                                                         if (BUNappend(type, c->type.type->base.name, false) != GDK_SUCCEED)
    4011           0 :                                                                 goto bailout1;
    4012             : 
    4013             :                                                         /*printf(" cnt "BUNFMT, bsi.count); */
    4014       55506 :                                                         sz = bsi.count;
    4015       55506 :                                                         if (BUNappend(cnt, &sz, false) != GDK_SUCCEED)
    4016           0 :                                                                 goto bailout1;
    4017             : 
    4018             :                                                         /*printf(" loc %s", BBP_physical(bs->batCacheid)); */
    4019       55506 :                                                         if (BUNappend(loc, BBP_physical(bs->batCacheid), false) != GDK_SUCCEED)
    4020           0 :                                                                 goto bailout1;
    4021             :                                                         /*printf(" width %d", bsi.width); */
    4022       55506 :                                                         w = bsi.width;
    4023       55506 :                                                         if (BUNappend(atom, &w, false) != GDK_SUCCEED)
    4024           0 :                                                                 goto bailout1;
    4025             : 
    4026       55506 :                                                         sz = bsi.count << bsi.shift;
    4027       55506 :                                                         if (BUNappend(size, &sz, false) != GDK_SUCCEED)
    4028           0 :                                                                 goto bailout1;
    4029             : 
    4030       55506 :                                                         sz = heapinfo(bs->tvheap, bs->batCacheid);
    4031       55506 :                                                         if (BUNappend(heap, &sz, false) != GDK_SUCCEED)
    4032           0 :                                                                 goto bailout1;
    4033             : 
    4034       55506 :                                                         MT_rwlock_rdlock(&bs->thashlock);
    4035       55506 :                                                         sz = hashinfo(bs->thash, bs->batCacheid);
    4036       55506 :                                                         MT_rwlock_rdunlock(&bs->thashlock);
    4037       55506 :                                                         if (BUNappend(indices, &sz, false) != GDK_SUCCEED)
    4038           0 :                                                                 goto bailout1;
    4039             : 
    4040       55506 :                                                         bitval = 0; /* HASHispersistent(bs); */
    4041       55506 :                                                         if (BUNappend(phash, &bitval, false) != GDK_SUCCEED)
    4042           0 :                                                                 goto bailout1;
    4043             : 
    4044       55506 :                                                         sz = IMPSimprintsize(bs);
    4045       55506 :                                                         if (BUNappend(imprints, &sz, false) != GDK_SUCCEED)
    4046           0 :                                                                 goto bailout1;
    4047             :                                                         /*printf(" indices "BUNFMT, bs->thash?bs->thash->heap.size:0); */
    4048             :                                                         /*printf("\n"); */
    4049       55506 :                                                         bitval = bsi.sorted;
    4050       55506 :                                                         if (!bitval && bsi.nosorted == 0)
    4051        2610 :                                                                 bitval = bit_nil;
    4052       55506 :                                                         if (BUNappend(sort, &bitval, false) != GDK_SUCCEED)
    4053           0 :                                                                 goto bailout1;
    4054             : 
    4055       55506 :                                                         bitval = bsi.revsorted;
    4056       55506 :                                                         if (!bitval && bsi.norevsorted == 0)
    4057        2289 :                                                                 bitval = bit_nil;
    4058       55506 :                                                         if (BUNappend(revsort, &bitval, false) != GDK_SUCCEED)
    4059           0 :                                                                 goto bailout1;
    4060             : 
    4061       55506 :                                                         bitval = BATtkey(bs);
    4062       55506 :                                                         if (!bitval && bsi.nokey[0] == 0 && bsi.nokey[1] == 0)
    4063       12795 :                                                                 bitval = bit_nil;
    4064       55506 :                                                         if (BUNappend(key, &bitval, false) != GDK_SUCCEED)
    4065           0 :                                                                 goto bailout1;
    4066             : 
    4067       55506 :                                                         sz = bs->torderidx && bs->torderidx != (Heap *) 1 ? bs->torderidx->free : 0;
    4068       55506 :                                                         if (BUNappend(oidx, &sz, false) != GDK_SUCCEED)
    4069           0 :                                                                 goto bailout1;
    4070       55506 :                                                         bat_iterator_end(&bsi);
    4071             :                                                 }
    4072             :                                         }
    4073             : 
    4074       10912 :                                         if (t->idxs) {
    4075       15700 :                                                 for (ncol = ol_first_node((t)->idxs); ncol; ncol = ncol->next) {
    4076        4788 :                                                         sql_base *bc = ncol->data;
    4077        4788 :                                                         sql_idx *c = (sql_idx *) ncol->data;
    4078        4788 :                                                         if (idx_has_column(c->type)) {
    4079        4782 :                                                                 bs = store->storage_api.bind_idx(tr, c, QUICK);
    4080        4782 :                                                                 lng sz;
    4081             : 
    4082        4782 :                                                                 if (bs == NULL) {
    4083           0 :                                                                         msg = createException(SQL, "sql.storage", SQLSTATE(HY005) "Cannot access column descriptor");
    4084           0 :                                                                         goto bailout;
    4085             :                                                                 }
    4086        4782 :                                                                 if( cname && strcmp(bc->name, cname) )
    4087          12 :                                                                         continue;
    4088        4770 :                                                                 bsi = bat_iterator(bs);
    4089             :                                                                 /*printf("schema %s.%s.%s" , b->name, bt->name, bc->name); */
    4090        9540 :                                                                 if (BUNappend(sch, b->name, false) != GDK_SUCCEED ||
    4091        9540 :                                                                     BUNappend(tab, bt->name, false) != GDK_SUCCEED ||
    4092        4770 :                                                                     BUNappend(col, bc->name, false) != GDK_SUCCEED)
    4093           0 :                                                                         goto bailout1;
    4094        4770 :                                                                 if (c->t->access == TABLE_WRITABLE) {
    4095         882 :                                                                         if (BUNappend(mode, "writable", false) != GDK_SUCCEED)
    4096           0 :                                                                                 goto bailout1;
    4097        3888 :                                                                 } else if (c->t->access == TABLE_APPENDONLY) {
    4098           0 :                                                                         if (BUNappend(mode, "appendonly", false) != GDK_SUCCEED)
    4099           0 :                                                                                 goto bailout1;
    4100        3888 :                                                                 } else if (c->t->access == TABLE_READONLY) {
    4101        3888 :                                                                         if (BUNappend(mode, "readonly", false) != GDK_SUCCEED)
    4102           0 :                                                                                 goto bailout1;
    4103             :                                                                 } else {
    4104           0 :                                                                         if (BUNappend(mode, str_nil, false) != GDK_SUCCEED)
    4105           0 :                                                                                 goto bailout1;
    4106             :                                                                 }
    4107        4770 :                                                                 if (BUNappend(type, "oid", false) != GDK_SUCCEED)
    4108           0 :                                                                         goto bailout1;
    4109             : 
    4110             :                                                                 /*printf(" cnt "BUNFMT, bsi.count); */
    4111        4770 :                                                                 sz = bsi.count;
    4112        4770 :                                                                 if (BUNappend(cnt, &sz, false) != GDK_SUCCEED)
    4113           0 :                                                                         goto bailout1;
    4114             : 
    4115             :                                                                 /*printf(" loc %s", BBP_physical(bs->batCacheid)); */
    4116        4770 :                                                                 if (BUNappend(loc, BBP_physical(bs->batCacheid), false) != GDK_SUCCEED)
    4117           0 :                                                                         goto bailout1;
    4118             :                                                                 /*printf(" width %d", bsi.width); */
    4119        4770 :                                                                 w = bsi.width;
    4120        4770 :                                                                 if (BUNappend(atom, &w, false) != GDK_SUCCEED)
    4121           0 :                                                                         goto bailout1;
    4122             :                                                                 /*printf(" size "BUNFMT, tailsize(bs,bsi.count) + (bs->tvheap? bs->tvheap->size:0)); */
    4123        4770 :                                                                 sz = tailsize(bs, bsi.count);
    4124        4770 :                                                                 if (BUNappend(size, &sz, false) != GDK_SUCCEED)
    4125           0 :                                                                         goto bailout1;
    4126             : 
    4127        4770 :                                                                 sz = bs->tvheap ? bs->tvheap->size : 0;
    4128        4770 :                                                                 if (BUNappend(heap, &sz, false) != GDK_SUCCEED)
    4129           0 :                                                                         goto bailout1;
    4130             : 
    4131        4770 :                                                                 MT_rwlock_rdlock(&bs->thashlock);
    4132        4770 :                                                                 sz = bs->thash && bs->thash != (Hash *) 1 ? bs->thash->heaplink.size + bs->thash->heapbckt.size : 0; /* HASHsize() */
    4133        4770 :                                                                 MT_rwlock_rdunlock(&bs->thashlock);
    4134        4770 :                                                                 if (BUNappend(indices, &sz, false) != GDK_SUCCEED)
    4135           0 :                                                                         goto bailout1;
    4136        4770 :                                                                 bitval = 0; /* HASHispersistent(bs); */
    4137        4770 :                                                                 if (BUNappend(phash, &bitval, false) != GDK_SUCCEED)
    4138           0 :                                                                         goto bailout1;
    4139             : 
    4140        4770 :                                                                 sz = IMPSimprintsize(bs);
    4141        4770 :                                                                 if (BUNappend(imprints, &sz, false) != GDK_SUCCEED)
    4142           0 :                                                                         goto bailout1;
    4143             :                                                                 /*printf(" indices "BUNFMT, bs->thash?bs->thash->heaplink.size+bs->thash->heapbckt.size:0); */
    4144             :                                                                 /*printf("\n"); */
    4145        4770 :                                                                 bitval = bsi.sorted;
    4146        4770 :                                                                 if (!bitval && bsi.nosorted == 0)
    4147           0 :                                                                         bitval = bit_nil;
    4148        4770 :                                                                 if (BUNappend(sort, &bitval, false) != GDK_SUCCEED)
    4149           0 :                                                                         goto bailout1;
    4150        4770 :                                                                 bitval = bsi.revsorted;
    4151        4770 :                                                                 if (!bitval && bsi.norevsorted == 0)
    4152           0 :                                                                         bitval = bit_nil;
    4153        4770 :                                                                 if (BUNappend(revsort, &bitval, false) != GDK_SUCCEED)
    4154           0 :                                                                         goto bailout1;
    4155        4770 :                                                                 bitval = BATtkey(bs);
    4156        4770 :                                                                 if (!bitval && bsi.nokey[0] == 0 && bsi.nokey[1] == 0)
    4157        2088 :                                                                         bitval = bit_nil;
    4158        4770 :                                                                 if (BUNappend(key, &bitval, false) != GDK_SUCCEED)
    4159           0 :                                                                         goto bailout1;
    4160        4770 :                                                                 sz = bs->torderidx && bs->torderidx != (Heap *) 1 ? bs->torderidx->free : 0;
    4161        4770 :                                                                 if (BUNappend(oidx, &sz, false) != GDK_SUCCEED)
    4162           0 :                                                                         goto bailout1;
    4163        4770 :                                                                 bat_iterator_end(&bsi);
    4164             :                                                         }
    4165             :                                                 }
    4166             :                                         }
    4167             :                                 }
    4168             :                         }
    4169             :                 }
    4170             :         }
    4171             : 
    4172         296 :         *rsch = sch->batCacheid;
    4173         296 :         BBPkeepref(sch);
    4174         296 :         *rtab = tab->batCacheid;
    4175         296 :         BBPkeepref(tab);
    4176         296 :         *rcol = col->batCacheid;
    4177         296 :         BBPkeepref(col);
    4178         296 :         *rmode = mode->batCacheid;
    4179         296 :         BBPkeepref(mode);
    4180         296 :         *rloc = loc->batCacheid;
    4181         296 :         BBPkeepref(loc);
    4182         296 :         *rtype = type->batCacheid;
    4183         296 :         BBPkeepref(type);
    4184         296 :         *rcnt = cnt->batCacheid;
    4185         296 :         BBPkeepref(cnt);
    4186         296 :         *ratom = atom->batCacheid;
    4187         296 :         BBPkeepref(atom);
    4188         296 :         *rsize = size->batCacheid;
    4189         296 :         BBPkeepref(size);
    4190         296 :         *rheap = heap->batCacheid;
    4191         296 :         BBPkeepref(heap);
    4192         296 :         *rindices = indices->batCacheid;
    4193         296 :         BBPkeepref(indices);
    4194         296 :         *rphash = phash->batCacheid;
    4195         296 :         BBPkeepref(phash);
    4196         296 :         *rimprints = imprints->batCacheid;
    4197         296 :         BBPkeepref(imprints);
    4198         296 :         *rsort = sort->batCacheid;
    4199         296 :         BBPkeepref(sort);
    4200         296 :         *rrevsort = revsort->batCacheid;
    4201         296 :         BBPkeepref(revsort);
    4202         296 :         *rkey = key->batCacheid;
    4203         296 :         BBPkeepref(key);
    4204         296 :         *roidx = oidx->batCacheid;
    4205         296 :         BBPkeepref(oidx);
    4206         296 :         return MAL_SUCCEED;
    4207             : 
    4208           0 :   bailout1:
    4209           0 :         bat_iterator_end(&bsi);
    4210           0 :   bailout:
    4211           0 :         BBPreclaim(sch);
    4212           0 :         BBPreclaim(tab);
    4213           0 :         BBPreclaim(col);
    4214           0 :         BBPreclaim(mode);
    4215           0 :         BBPreclaim(loc);
    4216           0 :         BBPreclaim(cnt);
    4217           0 :         BBPreclaim(type);
    4218           0 :         BBPreclaim(atom);
    4219           0 :         BBPreclaim(size);
    4220           0 :         BBPreclaim(heap);
    4221           0 :         BBPreclaim(indices);
    4222           0 :         BBPreclaim(phash);
    4223           0 :         BBPreclaim(imprints);
    4224           0 :         BBPreclaim(sort);
    4225           0 :         BBPreclaim(revsort);
    4226           0 :         BBPreclaim(key);
    4227           0 :         BBPreclaim(oidx);
    4228           0 :         if (!msg)
    4229           0 :                 msg = createException(SQL, "sql.storage", GDK_EXCEPTION);
    4230             :         return msg;
    4231             : }
    4232             : 
    4233             : void
    4234      294345 : freeVariables(Client c, MalBlkPtr mb, MalStkPtr glb, int oldvtop)
    4235             : {
    4236      363977 :         for (int i = oldvtop; i < mb->vtop;) {
    4237       69661 :                 if (glb) {
    4238           0 :                         if (isVarCleanup(mb, i))
    4239           0 :                                 garbageElement(c, &glb->stk[i]);
    4240             :                         /* clean stack entry */
    4241           0 :                         glb->stk[i].vtype = TYPE_int;
    4242           0 :                         glb->stk[i].val.ival = 0;
    4243           0 :                         glb->stk[i].len = 0;
    4244             :                 }
    4245       69661 :                 clearVariable(mb, i);
    4246       69632 :                 i++;
    4247             :         }
    4248      294316 :         assert(oldvtop <= mb->vsize);
    4249      294316 :         mb->vtop = oldvtop;
    4250      294316 : }
    4251             : 
    4252             : str
    4253           0 : SQLresume_log_flushing(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    4254             : {
    4255           0 :         mvc *mvc;
    4256             : 
    4257           0 :         (void)stk; (void)pci;
    4258           0 :         char *msg = getSQLContext(cntxt, mb, &mvc, NULL);
    4259           0 :         if (msg)
    4260             :                 return msg;
    4261           0 :         store_resume_log(mvc->store);
    4262           0 :         return MAL_SUCCEED;
    4263             : }
    4264             : 
    4265             : str
    4266           0 : SQLsuspend_log_flushing(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    4267             : {
    4268           0 :         mvc *mvc;
    4269             : 
    4270           0 :         (void)stk; (void)pci;
    4271           0 :         char *msg = getSQLContext(cntxt, mb, &mvc, NULL);
    4272           0 :         if (msg)
    4273             :                 return msg;
    4274           0 :         store_suspend_log(mvc->store);
    4275           0 :         return MAL_SUCCEED;
    4276             : }
    4277             : 
    4278             : str
    4279             : /*SQLhot_snapshot(void *ret, const str *tarfile_arg [, bool onserver ])*/
    4280           5 : SQLhot_snapshot(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    4281             : {
    4282           5 :         char *filename;
    4283           5 :         bool onserver;
    4284           5 :         char *msg = MAL_SUCCEED;
    4285           5 :         char buf[80];
    4286           5 :         mvc *mvc;
    4287           5 :         ssize_t sz;
    4288           5 :         stream *s;
    4289           5 :         stream *cb = NULL;
    4290           5 :         lng result;
    4291             : 
    4292           5 :         filename = *getArgReference_str(stk, pci, 1);
    4293           5 :         onserver = pci->argc == 3 ? *getArgReference_bit(stk, pci, 2) : true;
    4294             : 
    4295           5 :         msg = getSQLContext(cntxt, mb, &mvc, NULL);
    4296           5 :         if (msg)
    4297             :                 return msg;
    4298             : 
    4299           5 :         sqlstore *store = mvc->session->tr->store;
    4300           5 :         if (onserver) {
    4301           5 :                 lng result = store_hot_snapshot(store, filename);
    4302           5 :                 if (result)
    4303             :                         return MAL_SUCCEED;
    4304             :                 else
    4305           0 :                         throw(SQL, "sql.hot_snapshot", GDK_EXCEPTION);
    4306             :         }
    4307             : 
    4308             :         // sync with client, copy pasted from mvc_export_table_wrap
    4309           0 :         while (!mvc->scanner.rs->eof)
    4310           0 :                 bstream_next(mvc->scanner.rs);
    4311             : 
    4312             :         // The snapshot code flushes from time to time.
    4313             :         // Use a callback stream to suppress those.
    4314           0 :         s = mvc->scanner.ws;
    4315           0 :         cb = callback_stream(
    4316             :                 /* private */ s,
    4317             :                 /* read */    NULL,
    4318             :                 /* write */   (void*)mnstr_write,
    4319             :                 /* close */   NULL,
    4320             :                 /* destroy */ NULL,
    4321             :                 "snapshot-callback"
    4322             :         );
    4323           0 :         if (!cb)
    4324           0 :                 throw(SQL, "sql.hot_snapshot", GDK_EXCEPTION);
    4325             : 
    4326             :         // tell client to open file, copy pasted from mvc_export_table_wrap
    4327           0 :         mnstr_write(s, PROMPT3, sizeof(PROMPT3) - 1, 1);
    4328           0 :         mnstr_printf(s, "wb %s\n", filename);
    4329           0 :         mnstr_flush(s, MNSTR_FLUSH_DATA);
    4330           0 :         if ((sz = mnstr_readline(mvc->scanner.rs->s, buf, sizeof(buf))) > 1) {
    4331             :                 /* non-empty line indicates failure on client */
    4332           0 :                 msg = createException(IO, "streams.open", "%s", buf);
    4333             :                         /* discard until client flushes */
    4334           0 :                         while (mnstr_read(mvc->scanner.rs->s, buf, 1, sizeof(buf)) > 0) {
    4335             :                                 /* ignore remainder of error message */
    4336           0 :                         }
    4337           0 :                 goto end;
    4338             :         }
    4339             : 
    4340             :         // client is waiting for data now, send it.
    4341           0 :         result = store_hot_snapshot_to_stream(store, cb);
    4342           0 :         if (result)
    4343             :                 msg = MAL_SUCCEED;
    4344             :         else
    4345           0 :                 msg = createException(SQL, "sql.hot_snapshot", GDK_EXCEPTION);
    4346           0 :         mnstr_destroy(cb);
    4347             : 
    4348             :         // tell client no more data, also copy pasted from mvc_export_table_wrap
    4349           0 :         mnstr_flush(s, MNSTR_FLUSH_DATA);
    4350           0 :         if ((sz = mnstr_readline(mvc->scanner.rs->s, buf, sizeof(buf))) > 1) {
    4351           0 :                 msg = createException(IO, "streams.open", "%s", buf);
    4352             :         }
    4353           0 :         while (sz > 0)
    4354           0 :                 sz = mnstr_readline(mvc->scanner.rs->s, buf, sizeof(buf));
    4355             : 
    4356           0 : end:
    4357             :         return msg;
    4358             : }
    4359             : 
    4360             : MT_Lock lock_persist_unlogged = MT_LOCK_INITIALIZER(lock_persist_unlogged);
    4361             : 
    4362             : str
    4363           2 : SQLpersist_unlogged(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    4364             : {
    4365           2 :         (void)stk;
    4366           2 :         (void)pci;
    4367             : 
    4368           2 :         assert(pci->argc == 5);
    4369             : 
    4370           2 :         bat *o0 = getArgReference_bat(stk, pci, 0),
    4371           2 :                 *o1 = getArgReference_bat(stk, pci, 1),
    4372           2 :                 *o2 = getArgReference_bat(stk, pci, 2);
    4373           2 :         str sname = *getArgReference_str(stk, pci, 3),
    4374           2 :                 tname = *getArgReference_str(stk, pci, 4),
    4375           2 :                 msg = MAL_SUCCEED;
    4376             : 
    4377           2 :         mvc *m = NULL;
    4378           2 :         msg = getSQLContext(cntxt, mb, &m, NULL);
    4379             : 
    4380           2 :         if (msg)
    4381             :                 return msg;
    4382             : 
    4383           2 :         sqlstore *store = store = m->session->tr->store;
    4384             : 
    4385           2 :         sql_schema *s = mvc_bind_schema(m, sname);
    4386           2 :         if (s == NULL)
    4387           0 :                 throw(SQL, "sql.persist_unlogged", SQLSTATE(3F000) "Schema missing %s.", sname);
    4388             : 
    4389           2 :         if (!mvc_schema_privs(m, s))
    4390           0 :                 throw(SQL, "sql.persist_unlogged", SQLSTATE(42000) "Access denied for %s to schema '%s'.",
    4391             :                           get_string_global_var(m, "current_user"), s->base.name);
    4392             : 
    4393           2 :         sql_table *t = mvc_bind_table(m, s, tname);
    4394           2 :         if (t == NULL)
    4395           0 :                 throw(SQL, "sql.persist_unlogged", SQLSTATE(42S02) "Table missing %s.%s", sname, tname);
    4396             : 
    4397           2 :         if (!isUnloggedTable(t) || t->access != TABLE_APPENDONLY)
    4398           0 :                 throw(SQL, "sql.persist_unlogged", "Unlogged and Insert Only mode combination required for table %s.%s", sname, tname);
    4399             : 
    4400           2 :         lng count = 0;
    4401             : 
    4402           2 :         sql_trans *tr = m->session->tr;
    4403           2 :         storage *t_del = bind_del_data(tr, t, NULL);
    4404             : 
    4405           2 :         BAT *d = BATdescriptor(t_del->cs.bid);
    4406             : 
    4407           2 :         if (t_del == NULL || d == NULL)
    4408           0 :                 throw(SQL, "sql.persist_unlogged", "Cannot access %s column storage.", tname);
    4409             : 
    4410           2 :         MT_lock_set(&lock_persist_unlogged);
    4411           2 :         BATiter d_bi = bat_iterator(d);
    4412             : 
    4413           2 :         if (BBP_status(d->batCacheid) & BBPEXISTING) {
    4414             : 
    4415           1 :                 assert(d->batInserted <= d_bi.count);
    4416             : 
    4417           1 :                 if (d->batInserted < d_bi.count) {
    4418             : 
    4419           1 :                         int n = 100;
    4420           1 :                         bat *commit_list = GDKzalloc(sizeof(bat) * (n + 1));
    4421           1 :                         BUN *sizes = GDKzalloc(sizeof(BUN) * (n + 1));
    4422             : 
    4423           1 :                         if (commit_list == NULL || sizes == NULL) {
    4424           0 :                                 MT_lock_unset(&lock_persist_unlogged);
    4425           0 :                                 GDKfree(commit_list);
    4426           0 :                                 GDKfree(sizes);
    4427           0 :                                 throw(SQL, "sql.persist_unlogged", SQLSTATE(HY001));
    4428             :                         }
    4429             : 
    4430           1 :                         commit_list[0] = 0;
    4431           1 :                         sizes[0] = 0;
    4432           1 :                         int i = 1;
    4433             : 
    4434           2 :                         for (node *ncol = ol_first_node(t->columns); ncol; ncol = ncol->next) {
    4435             : 
    4436           1 :                                 sql_column *c = (sql_column *) ncol->data;
    4437           1 :                                 BAT *b = store->storage_api.bind_col(tr, c, QUICK);
    4438             : 
    4439           1 :                                 if (b == NULL) {
    4440           0 :                                         MT_lock_unset(&lock_persist_unlogged);
    4441           0 :                                         GDKfree(commit_list);
    4442           0 :                                         GDKfree(sizes);
    4443           0 :                                         throw(SQL, "sql.persist_unlogged", "Cannot access column descriptor.");
    4444             :                                 }
    4445             : 
    4446           1 :                                 if (i == n && ncol->next) {
    4447           0 :                                         n = n * 2;
    4448           0 :                                         commit_list = GDKrealloc(commit_list, sizeof(bat) * n);
    4449           0 :                                         sizes = GDKrealloc(sizes, sizeof(BUN) * n);
    4450             :                                 }
    4451             : 
    4452           1 :                                 if (commit_list == NULL || sizes == NULL) {
    4453           0 :                                         MT_lock_unset(&lock_persist_unlogged);
    4454           0 :                                         GDKfree(commit_list);
    4455           0 :                                         GDKfree(sizes);
    4456           0 :                                         throw(SQL, "sql.persist_unlogged", SQLSTATE(HY001));
    4457             :                                 }
    4458             : 
    4459           1 :                                 commit_list[i] = b->batCacheid;
    4460           1 :                                 sizes[i] = d_bi.count;
    4461           1 :                                 i++;
    4462             :                         }
    4463             : 
    4464           1 :                         commit_list[i] = d->batCacheid;
    4465           1 :                         sizes[i] = d_bi.count;
    4466           1 :                         i++;
    4467             : 
    4468           1 :                         if (TMsubcommit_list(commit_list, sizes, i, -1) != GDK_SUCCEED) {
    4469           0 :                                 MT_lock_unset(&lock_persist_unlogged);
    4470           0 :                                 GDKfree(commit_list);
    4471           0 :                                 GDKfree(sizes);
    4472           0 :                                 throw(SQL, "sql.persist_unlogged", "Lower level commit operation failed");
    4473             :                         }
    4474             : 
    4475           1 :                         GDKfree(commit_list);
    4476           1 :                         GDKfree(sizes);
    4477             :                 }
    4478             : 
    4479           1 :                 count = d_bi.count;
    4480             :         }
    4481             : 
    4482           2 :         bat_iterator_end(&d_bi);
    4483           2 :         MT_lock_unset(&lock_persist_unlogged);
    4484           2 :         BBPreclaim(d);
    4485             : 
    4486           2 :         BAT *table = COLnew(0, TYPE_str, 0, TRANSIENT),
    4487           2 :                 *tableid = COLnew(0, TYPE_int, 0, TRANSIENT),
    4488           2 :                 *rowcount = COLnew(0, TYPE_lng, 0, TRANSIENT);
    4489             : 
    4490           2 :         if (table == NULL || tableid == NULL || rowcount == NULL) {
    4491           0 :                 BBPnreclaim(3, table, tableid, rowcount);
    4492           0 :                 throw(SQL, "sql.persist_unlogged", SQLSTATE(HY001));
    4493             :         }
    4494             : 
    4495           4 :         if (BUNappend(table, tname, false) != GDK_SUCCEED ||
    4496           4 :                 BUNappend(tableid, &(t->base.id), false) != GDK_SUCCEED ||
    4497           2 :                 BUNappend(rowcount, &count, false) != GDK_SUCCEED) {
    4498           0 :                 BBPnreclaim(3, table, tableid, rowcount);
    4499           0 :                 throw(SQL, "sql.persist_unlogged", SQLSTATE(HY001));
    4500             :         }
    4501             : 
    4502           2 :         *o0 = table->batCacheid;
    4503           2 :         *o1 = tableid->batCacheid;
    4504           2 :         *o2 = rowcount->batCacheid;
    4505           2 :         BBPkeepref(table);
    4506           2 :         BBPkeepref(tableid);
    4507           2 :         BBPkeepref(rowcount);
    4508             : 
    4509           2 :         return msg;
    4510             : }
    4511             : 
    4512             : str
    4513           9 : SQLsession_prepared_statements(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    4514             : {
    4515           9 :         BAT *sessionid, *user, *statementid, *statement, *created;
    4516           9 :         bat *sid = getArgReference_bat(stk,pci,0);
    4517           9 :         bat *u = getArgReference_bat(stk,pci,1);
    4518           9 :         bat *i = getArgReference_bat(stk,pci,2);
    4519           9 :         bat *s = getArgReference_bat(stk,pci,3);
    4520           9 :         bat *c = getArgReference_bat(stk,pci,4);
    4521           9 :         str msg = MAL_SUCCEED;
    4522           9 :         mvc *sql = NULL;
    4523           9 :         cq *q = NULL;
    4524             : 
    4525           9 :         (void) stk;
    4526           9 :         (void) pci;
    4527           9 :         if ((msg = getSQLContext(cntxt, mb, &sql, NULL)) != NULL)
    4528             :                 return msg;
    4529           9 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    4530             :                 return msg;
    4531             : 
    4532           9 :         assert(sql->qc);
    4533             : 
    4534           9 :         sessionid = COLnew(0, TYPE_int, 256, TRANSIENT);
    4535           9 :         user = COLnew(0, TYPE_str, 256, TRANSIENT);
    4536           9 :         statementid = COLnew(0, TYPE_int, 256, TRANSIENT);
    4537           9 :         statement = COLnew(0, TYPE_str, 256, TRANSIENT);
    4538           9 :         created = COLnew(0, TYPE_timestamp, 256, TRANSIENT);
    4539           9 :         if (sessionid == NULL || user == NULL || statementid == NULL || statement == NULL || created == NULL) {
    4540           0 :                 msg = createException(SQL, "sql.session_prepared_statements", GDK_EXCEPTION);
    4541           0 :                 goto bailout;
    4542             :         }
    4543             : 
    4544          13 :         for (q = sql->qc->q; q; q = q->next) {
    4545           4 :                 gdk_return bun_res;
    4546           4 :                 if (BUNappend(sessionid, &(cntxt->idx), false) != GDK_SUCCEED) {
    4547           0 :                         msg = createException(SQL, "sql.session_prepared_statements", GDK_EXCEPTION);
    4548           0 :                         goto bailout;
    4549             :                 }
    4550             : 
    4551           4 :                 if (msg != MAL_SUCCEED)
    4552             :                         goto bailout;
    4553           4 :                 bun_res = BUNappend(user, cntxt->username, false);
    4554           4 :                 if (bun_res != GDK_SUCCEED) {
    4555           0 :                         msg = createException(SQL, "sql.session_prepared_statements", GDK_EXCEPTION);
    4556           0 :                         goto bailout;
    4557             :                 }
    4558             : 
    4559           4 :                 if (BUNappend(statementid, &(q->id), false) != GDK_SUCCEED) {
    4560           0 :                         msg = createException(SQL, "sql.session_prepared_statements", GDK_EXCEPTION);
    4561           0 :                         goto bailout;
    4562             :                 }
    4563           4 :                 if (BUNappend(statement, q->f->query, false) != GDK_SUCCEED) {
    4564           0 :                         msg = createException(SQL, "sql.session_prepared_statements", GDK_EXCEPTION);
    4565           0 :                         goto bailout;
    4566             :                 }
    4567           4 :                 if (BUNappend(created, &(q->created), false) != GDK_SUCCEED) {
    4568           0 :                         msg = createException(SQL, "sql.session_prepared_statements", GDK_EXCEPTION);
    4569           0 :                         goto bailout;
    4570             :                 }
    4571             :         }
    4572             : 
    4573           9 : bailout:
    4574           0 :         if (msg) {
    4575           0 :                 BBPreclaim(sessionid);
    4576           0 :                 BBPreclaim(user);
    4577           0 :                 BBPreclaim(statementid);
    4578           0 :                 BBPreclaim(statement);
    4579           0 :                 BBPreclaim(created);
    4580             :         } else {
    4581           9 :                 *sid = sessionid->batCacheid;
    4582           9 :                 BBPkeepref(sessionid);
    4583           9 :                 *u = user->batCacheid;
    4584           9 :                 BBPkeepref(user);
    4585           9 :                 *i = statementid->batCacheid;
    4586           9 :                 BBPkeepref(statementid);
    4587           9 :                 *s = statement->batCacheid;
    4588           9 :                 BBPkeepref(statement);
    4589           9 :                 *c = created->batCacheid;
    4590           9 :                 BBPkeepref(created);
    4591             :         }
    4592             :         return msg;
    4593             : }
    4594             : 
    4595             : str
    4596           6 : SQLsession_prepared_statements_args(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    4597             : {
    4598           6 :         BAT *statementid, *type, *digits, *isinout, *number, *scale, *schema, *table, *column;
    4599           6 :         bat *sid = getArgReference_bat(stk,pci,0);
    4600           6 :         bat *t = getArgReference_bat(stk,pci,1);
    4601           6 :         bat *d = getArgReference_bat(stk,pci,2);
    4602           6 :         bat *s = getArgReference_bat(stk,pci,3);
    4603           6 :         bat *io = getArgReference_bat(stk,pci,4);
    4604           6 :         bat *n = getArgReference_bat(stk,pci,5);
    4605           6 :         bat *sch = getArgReference_bat(stk,pci,6);
    4606           6 :         bat *tbl = getArgReference_bat(stk,pci,7);
    4607           6 :         bat *col = getArgReference_bat(stk,pci,8);
    4608           6 :         str msg = MAL_SUCCEED;
    4609           6 :         mvc *sql = NULL;
    4610           6 :         cq *q = NULL;
    4611             : 
    4612           6 :         (void) stk;
    4613           6 :         (void) pci;
    4614           6 :         if ((msg = getSQLContext(cntxt, mb, &sql, NULL)) != NULL)
    4615             :                 return msg;
    4616           6 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    4617             :                 return msg;
    4618             : 
    4619           6 :         assert(sql->qc);
    4620             : 
    4621           6 :         statementid = COLnew(0, TYPE_int, 256, TRANSIENT);
    4622           6 :         type = COLnew(0, TYPE_str, 256, TRANSIENT);
    4623           6 :         digits = COLnew(0, TYPE_int, 256, TRANSIENT);
    4624           6 :         scale = COLnew(0, TYPE_int, 256, TRANSIENT);
    4625           6 :         isinout = COLnew(0, TYPE_bte, 256, TRANSIENT);
    4626           6 :         number = COLnew(0, TYPE_int, 256, TRANSIENT);
    4627           6 :         schema = COLnew(0, TYPE_str, 256, TRANSIENT);
    4628           6 :         table = COLnew(0, TYPE_str, 256, TRANSIENT);
    4629           6 :         column = COLnew(0, TYPE_str, 256, TRANSIENT);
    4630           6 :         if (!statementid || !type || !digits || !scale || !isinout || !number || !schema || !table || !column) {
    4631           0 :                 msg = createException(SQL, "sql.session_prepared_statements_args", GDK_EXCEPTION);
    4632           0 :                 goto bailout;
    4633             :         }
    4634             : 
    4635           9 :         for (q = sql->qc->q; q; q = q->next) {
    4636           3 :                 sql_rel *r = q->rel;
    4637           3 :                 int arg_number = 0;
    4638           3 :                 bte inout = ARG_OUT;
    4639             : 
    4640           3 :                 if (r && (is_topn(r->op) || is_sample(r->op)))
    4641           0 :                         r = r->l;
    4642             : 
    4643           3 :                 if (r && is_project(r->op) && r->exps) {
    4644           6 :                         for (node *n = r->exps->h; n; n = n->next, arg_number++) {
    4645           3 :                                 sql_exp *e = n->data;
    4646           3 :                                 sql_subtype *t = exp_subtype(e);
    4647           3 :                                 const char *name = exp_name(e), *rname = exp_relname(e), *rschema = ATOMnilptr(TYPE_str);
    4648             : 
    4649           3 :                                 if (!name && e->type == e_column && e->r)
    4650           0 :                                         name = e->r;
    4651           0 :                                 if (!name)
    4652           0 :                                         name = ATOMnilptr(TYPE_str);
    4653           3 :                                 if (!rname && e->type == e_column && e->l)
    4654           0 :                                         rname = e->l;
    4655           0 :                                 if (!rname)
    4656           0 :                                         rname = ATOMnilptr(TYPE_str);
    4657             : 
    4658           6 :                                 if (BUNappend(statementid, &(q->id), false) != GDK_SUCCEED ||
    4659           6 :                                         BUNappend(type, t->type->base.name, false) != GDK_SUCCEED ||
    4660           6 :                                         BUNappend(digits, &t->digits, false) != GDK_SUCCEED ||
    4661           6 :                                         BUNappend(scale, &t->scale, false) != GDK_SUCCEED ||
    4662           6 :                                         BUNappend(isinout, &inout, false) != GDK_SUCCEED ||
    4663           6 :                                         BUNappend(number, &arg_number, false) != GDK_SUCCEED ||
    4664           6 :                                         BUNappend(schema, rschema, false) != GDK_SUCCEED ||
    4665           6 :                                         BUNappend(table, rname, false) != GDK_SUCCEED ||
    4666           3 :                                         BUNappend(column, name, false) != GDK_SUCCEED) {
    4667           0 :                                         msg = createException(SQL, "sql.session_prepared_statements_args", GDK_EXCEPTION);
    4668           0 :                                         goto bailout;
    4669             :                                 }
    4670             :                         }
    4671             :                 }
    4672             : 
    4673           3 :                 if (q->f->ops) {
    4674           3 :                         inout = ARG_IN;
    4675           6 :                         for (node *n = q->f->ops->h; n; n=n->next, arg_number++) {
    4676           3 :                                 sql_arg *a = n->data;
    4677           3 :                                 sql_subtype *t = &a->type;
    4678             : 
    4679           6 :                                 if (BUNappend(statementid, &(q->id), false) != GDK_SUCCEED ||
    4680           6 :                                         BUNappend(type, t->type->base.name, false) != GDK_SUCCEED ||
    4681           6 :                                         BUNappend(digits, &(t->digits), false) != GDK_SUCCEED ||
    4682           6 :                                         BUNappend(scale, &(t->scale), false) != GDK_SUCCEED ||
    4683           6 :                                         BUNappend(isinout, &inout, false) != GDK_SUCCEED ||
    4684           6 :                                         BUNappend(number, &arg_number, false) != GDK_SUCCEED ||
    4685           6 :                                         BUNappend(schema, ATOMnilptr(TYPE_str), false) != GDK_SUCCEED ||
    4686           6 :                                         BUNappend(table, ATOMnilptr(TYPE_str), false) != GDK_SUCCEED ||
    4687           3 :                                         BUNappend(column, ATOMnilptr(TYPE_str), false) != GDK_SUCCEED) {
    4688           0 :                                         msg = createException(SQL, "sql.session_prepared_statements_args", GDK_EXCEPTION);
    4689           0 :                                         goto bailout;
    4690             :                                 }
    4691             :                         }
    4692             :                 }
    4693             :         }
    4694             : 
    4695           6 : bailout:
    4696           0 :         if (msg) {
    4697           0 :                 BBPreclaim(statementid);
    4698           0 :                 BBPreclaim(type);
    4699           0 :                 BBPreclaim(digits);
    4700           0 :                 BBPreclaim(scale);
    4701           0 :                 BBPreclaim(isinout);
    4702           0 :                 BBPreclaim(number);
    4703           0 :                 BBPreclaim(schema);
    4704           0 :                 BBPreclaim(table);
    4705           0 :                 BBPreclaim(column);
    4706             :         } else {
    4707           6 :                 *sid = statementid->batCacheid;
    4708           6 :                 BBPkeepref(statementid);
    4709           6 :                 *t = type->batCacheid;
    4710           6 :                 BBPkeepref(type);
    4711           6 :                 *d = digits->batCacheid;
    4712           6 :                 BBPkeepref(digits);
    4713           6 :                 *s = scale->batCacheid;
    4714           6 :                 BBPkeepref(scale);
    4715           6 :                 *io = isinout->batCacheid;
    4716           6 :                 BBPkeepref(isinout);
    4717           6 :                 *n = number->batCacheid;
    4718           6 :                 BBPkeepref(number);
    4719           6 :                 *sch = schema->batCacheid;
    4720           6 :                 BBPkeepref(schema);
    4721           6 :                 *tbl = table->batCacheid;
    4722           6 :                 BBPkeepref(table);
    4723           6 :                 *col = column->batCacheid;
    4724           6 :                 BBPkeepref(column);
    4725             :         }
    4726             :         return msg;
    4727             : }
    4728             : 
    4729             : /* input id, row-input-values
    4730             :  * for each id call function(with row-input-values) return table
    4731             :  * return for each id the table, ie id (*length of table) and table results
    4732             :  */
    4733             : str
    4734          11 : SQLunionfunc(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    4735             : {
    4736          11 :         int arg = pci->retc;
    4737          11 :         str mod, fcn, ret = MAL_SUCCEED;
    4738          11 :         InstrPtr npci;
    4739          11 :         MalBlkPtr nmb = newMalBlk(1), omb = NULL;
    4740             : 
    4741          11 :         if (!nmb)
    4742           0 :                 return createException(MAL, "sql.unionfunc", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    4743          11 :         mod = *getArgReference_str(stk, pci, arg++);
    4744          11 :         fcn = *getArgReference_str(stk, pci, arg++);
    4745          11 :         npci = newStmtArgs(nmb, mod, fcn, pci->argc);
    4746          11 :         if (npci == NULL) {
    4747           0 :                 freeMalBlk(nmb);
    4748           0 :                 return createException(MAL, "sql.unionfunc", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    4749             :         }
    4750             : 
    4751          22 :         for (int i = 1; i < pci->retc; i++) {
    4752          11 :                 int type = getArgType(mb, pci, i);
    4753             : 
    4754          11 :                 if (i==1)
    4755          11 :                         getArg(npci, 0) = newTmpVariable(nmb, type);
    4756             :                 else
    4757           0 :                         npci = pushReturn(nmb, npci, newTmpVariable(nmb, type));
    4758             :         }
    4759          27 :         for (int i = pci->retc+2+1; i < pci->argc; i++) {
    4760          16 :                 int type = getBatType(getArgType(mb, pci, i));
    4761             : 
    4762          16 :                 npci = pushNil(nmb, npci, type);
    4763             :         }
    4764          11 :         pushInstruction(nmb, npci);
    4765             :         /* check program to get the proper malblk */
    4766          11 :         if (chkInstruction(cntxt->usermodule, nmb, npci)) {
    4767           0 :                 freeMalBlk(nmb);
    4768           0 :                 return createException(MAL, "sql.unionfunc", SQLSTATE(42000) PROGRAM_GENERAL);
    4769             :         }
    4770             : 
    4771          11 :         if (npci) {
    4772          11 :                 BAT **res = NULL, **input = NULL;
    4773          11 :                 BATiter *bi = NULL;
    4774          11 :                 BUN cnt = 0;
    4775          11 :                 int nrinput = pci->argc - 2 - pci->retc;
    4776          11 :                 MalStkPtr env = NULL;
    4777          11 :                 InstrPtr q = NULL;
    4778             : 
    4779          11 :                 if (!(input = GDKzalloc(sizeof(BAT*) * nrinput))) {
    4780           0 :                         ret = createException(MAL, "sql.unionfunc", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    4781           0 :                         goto finalize;
    4782             :                 }
    4783          11 :                 if (!(bi = GDKmalloc(sizeof(BATiter) * nrinput))) {
    4784           0 :                         ret = createException(MAL, "sql.unionfunc", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    4785           0 :                         goto finalize;
    4786             :                 }
    4787          11 :                 assert(pci->retc + 2 + nrinput == pci->argc);
    4788          38 :                 for (int i = 0, j = pci->retc+2; j < pci->argc; i++, j++) {
    4789          27 :                         bat *b = getArgReference_bat(stk, pci, j);
    4790          27 :                         if (!(input[i] = BATdescriptor(*b))) {
    4791           0 :                                 ret = createException(MAL, "sql.unionfunc", SQLSTATE(HY005) "Cannot access column descriptor");
    4792           0 :                                 while (i > 0) {
    4793           0 :                                         i--;
    4794           0 :                                         bat_iterator_end(&bi[i]);
    4795           0 :                                         BBPunfix(input[i]->batCacheid);
    4796             :                                 }
    4797           0 :                                 GDKfree(input);
    4798           0 :                                 input = NULL;
    4799           0 :                                 goto finalize;
    4800             :                         }
    4801          27 :                         bi[i] = bat_iterator(input[i]);
    4802          27 :                         cnt = BATcount(input[i]);
    4803             :                 }
    4804             : 
    4805             :                 /* create result bats */
    4806          11 :                 if (!(res = GDKzalloc(sizeof(BAT*) * pci->retc))) {
    4807           0 :                         ret = createException(MAL, "sql.unionfunc", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    4808           0 :                         goto finalize;
    4809             :                 }
    4810          33 :                 for (int i = 0; i<pci->retc; i++) {
    4811          22 :                         int type = getArgType(mb, pci, i);
    4812             : 
    4813          22 :                         if (!(res[i] = COLnew(0, getBatType(type), cnt, TRANSIENT))) {
    4814           0 :                                 ret = createException(MAL, "sql.unionfunc", GDK_EXCEPTION);
    4815           0 :                                 goto finalize;
    4816             :                         }
    4817             :                 }
    4818             : 
    4819          11 :                 if (npci->blk && npci->blk->stop > 1) {
    4820           8 :                         omb = nmb;
    4821           8 :                         if (!(nmb = copyMalBlk(npci->blk))) {
    4822           0 :                                 ret = createException(MAL, "sql.unionfunc", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    4823           0 :                                 goto finalize;
    4824             :                         }
    4825             :                 }
    4826          11 :                 if (!(env = prepareMALstack(nmb, nmb->vsize))) { /* needed for result */
    4827           0 :                         ret = createException(MAL, "sql.unionfunc", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    4828           0 :                         goto finalize;
    4829             :                 }
    4830          11 :                 q = getInstrPtr(nmb, 0);
    4831             : 
    4832          49 :                 for (BUN cur = 0; cur<cnt && !ret; cur++ ) {
    4833          38 :                         MalStkPtr nstk = prepareMALstack(nmb, nmb->vsize);
    4834          38 :                         int i,ii;
    4835             : 
    4836          38 :                         if (!nstk) { /* needed for result */
    4837           0 :                                 ret = createException(MAL, "sql.unionfunc", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    4838             :                         } else {
    4839             :                                 /* copy (input) arguments onto destination stack, skipping rowid col */
    4840          76 :                                 for (i = 1, ii = q->retc; ii < q->argc && !ret; ii++) {
    4841          38 :                                         ValPtr lhs = &nstk->stk[q->argv[ii]];
    4842          38 :                                         ptr rhs = (ptr)BUNtail(bi[i], cur);
    4843             : 
    4844          38 :                                         if (VALset(lhs, input[i]->ttype, rhs) == NULL)
    4845           0 :                                                 ret = createException(MAL, "sql.unionfunc", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    4846             :                                 }
    4847          38 :                                 if (!ret && ii == q->argc) {
    4848          38 :                                         BAT *fres = NULL;
    4849          38 :                                         if (!omb && npci->fcn)
    4850           0 :                                                 ret = (*(str (*)(Client, MalBlkPtr, MalStkPtr, InstrPtr))npci->fcn)(cntxt, nmb, nstk, npci);
    4851             :                                         else
    4852          38 :                                                 ret = runMALsequence(cntxt, nmb, 1, nmb->stop, nstk, env /* copy result in nstk first instruction*/, q);
    4853             : 
    4854          38 :                                         if (!ret) {
    4855             :                                                 /* insert into result */
    4856          38 :                                                 if (!(fres = BBPquickdesc(omb?env->stk[q->argv[0]].val.bval:nstk->stk[q->argv[0]].val.bval))) {
    4857           0 :                                                         ret = createException(MAL, "sql.unionfunc", SQLSTATE(HY005) "Cannot access column descriptor");
    4858             :                                                 } else {
    4859          38 :                                                         BAT *p = BATconstant(fres->hseqbase, res[0]->ttype, (ptr)BUNtail(bi[0], cur), BATcount(fres), TRANSIENT);
    4860             : 
    4861          38 :                                                         if (p) {
    4862          38 :                                                                 if (BATappend(res[0], p, NULL, FALSE) != GDK_SUCCEED)
    4863           0 :                                                                         ret = createException(MAL, "sql.unionfunc", GDK_EXCEPTION);
    4864          38 :                                                                 BBPunfix(p->batCacheid);
    4865             :                                                         } else {
    4866           0 :                                                                 ret = createException(MAL, "sql.unionfunc", GDK_EXCEPTION);
    4867             :                                                         }
    4868             :                                                 }
    4869             :                                                 i=1;
    4870          76 :                                                 for (ii = 0; i < pci->retc && !ret; i++) {
    4871          38 :                                                         BAT *b;
    4872             : 
    4873          38 :                                                         if (!(b = BATdescriptor(omb?env->stk[q->argv[ii]].val.bval:nstk->stk[q->argv[ii]].val.bval)))
    4874           0 :                                                                 ret = createException(MAL, "sql.unionfunc", SQLSTATE(HY005) "Cannot access column descriptor");
    4875          38 :                                                         else if (BATappend(res[i], b, NULL, FALSE) != GDK_SUCCEED)
    4876           0 :                                                                 ret = createException(MAL, "sql.unionfunc", GDK_EXCEPTION);
    4877          38 :                                                         if (b) {
    4878          38 :                                                                 BBPrelease(b->batCacheid); /* release ref from env stack */
    4879          38 :                                                                 BBPunfix(b->batCacheid);   /* free pointer */
    4880             :                                                         }
    4881             :                                                 }
    4882             :                                         }
    4883             :                                 }
    4884          38 :                                 GDKfree(nstk);
    4885             :                         }
    4886             :                 }
    4887          11 : finalize:
    4888          11 :                 GDKfree(env);
    4889          11 :                 if (nmb)
    4890          11 :                         freeMalBlk(nmb);
    4891          11 :                 if (omb)
    4892           8 :                         freeMalBlk(omb);
    4893          11 :                 if (res)
    4894          33 :                         for (int i = 0; i<pci->retc; i++) {
    4895          22 :                                 bat *b = getArgReference_bat(stk, pci, i);
    4896          22 :                                 if (res[i]) {
    4897          22 :                                         *b = res[i]->batCacheid;
    4898          22 :                                         if (ret)
    4899           0 :                                                 BBPunfix(*b);
    4900             :                                         else
    4901          22 :                                                 BBPkeepref(res[i]);
    4902             :                                 }
    4903             :                         }
    4904          11 :                 GDKfree(res);
    4905          11 :                 if (input) {
    4906          38 :                         for (int i = 0; i<nrinput; i++) {
    4907          27 :                                 if (input[i]) {
    4908          27 :                                         bat_iterator_end(&bi[i]);
    4909          27 :                                         BBPunfix(input[i]->batCacheid);
    4910             :                                 }
    4911             :                         }
    4912          11 :                         GDKfree(input);
    4913             :                 }
    4914          11 :                 GDKfree(bi);
    4915             :         }
    4916             :         return ret;
    4917             : }
    4918             : 
    4919             : static str
    4920           1 : do_str_column_vacuum(sql_trans *tr, sql_column *c, char *sname, char *tname, char *cname)
    4921             : {
    4922           1 :         int res;
    4923           1 :         int access = 0;
    4924           1 :         BAT* b = NULL;
    4925           1 :         BAT* bn = NULL;
    4926           1 :         sqlstore *store = tr->store;
    4927             : 
    4928           1 :         if ((b = store->storage_api.bind_col(tr, c, access)) == NULL)
    4929           0 :                 throw(SQL, "do_str_column_vacuum", SQLSTATE(42S22) "storage_api.bind_col failed for %s.%s.%s", sname, tname, cname);
    4930             :         // vacuum varsized bats
    4931           1 :         if (ATOMvarsized(c->type.type->localtype)) {
    4932             :                 // TODO check for num of updates on the BAT against some threshold
    4933             :                 // and decide whether to proceed
    4934           1 :                 if ((bn = COLcopy(b, b->ttype, true, PERSISTENT)) == NULL) {
    4935           0 :                         BBPunfix(b->batCacheid);
    4936           0 :                         throw(SQL, "do_str_column_vacuum", SQLSTATE(42S22) "COLcopy failed for %s.%s.%s", sname, tname, cname);
    4937             :                 }
    4938           1 :                 if ((res = (int) store->storage_api.swap_bats(tr, c, bn)) != LOG_OK) {
    4939           0 :                         BBPreclaim(bn);
    4940           0 :                         BBPunfix(b->batCacheid);
    4941           0 :                         if (res == LOG_CONFLICT)
    4942           0 :                                 throw(SQL, "do_str_column_vacuum", SQLSTATE(25S01) "TRANSACTION CONFLICT in storage_api.swap_bats %s.%s.%s", sname, tname, cname);
    4943           0 :                         if (res == LOG_ERR)
    4944           0 :                                 throw(SQL, "do_str_column_vacuum", SQLSTATE(HY000) "LOG ERROR in storage_api.swap_bats %s.%s.%s", sname, tname, cname);
    4945           0 :                         throw(SQL, "do_str_column_vacuum", SQLSTATE(HY000) "ERROR in storage_api.swap_bats %s.%s.%s", sname, tname, cname);
    4946             :                 }
    4947             :         }
    4948           1 :         BBPunfix(b->batCacheid);
    4949           1 :         BBPreclaim(bn);
    4950             :         return MAL_SUCCEED;
    4951             : }
    4952             : 
    4953             : str
    4954           1 : SQLstr_column_vacuum(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    4955             : {
    4956           1 :         mvc *m = NULL;
    4957           1 :         str msg = NULL;
    4958           1 :         char *sname = *getArgReference_str(stk, pci, 1);
    4959           1 :         char *tname = *getArgReference_str(stk, pci, 2);
    4960           1 :         char *cname = *getArgReference_str(stk, pci, 3);
    4961             : 
    4962           1 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    4963             :                 return msg;
    4964           1 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    4965             :                 return msg;
    4966             : 
    4967           1 :         sql_trans *tr = m->session->tr;
    4968           1 :         sql_schema *s = NULL;
    4969           1 :         sql_table *t = NULL;
    4970           1 :         sql_column *c = NULL;
    4971             : 
    4972           1 :         if (strNil(sname))
    4973           0 :                 throw(SQL, "sql.str_column_vacuum", SQLSTATE(42000) "Schema name cannot be NULL");
    4974           1 :         if (strNil(tname))
    4975           0 :                 throw(SQL, "sql.str_column_vacuum", SQLSTATE(42000) "Table name cannot be NULL");
    4976           1 :         if (strNil(cname))
    4977           0 :                 throw(SQL, "sql.str_column_vacuum", SQLSTATE(42000) "Column name cannot be NULL");
    4978           1 :         if ((s = mvc_bind_schema(m, sname)) == NULL)
    4979           0 :                 throw(SQL, "sql.str_column_vacuum", SQLSTATE(3F000) "Invalid or missing schema %s",sname);
    4980           1 :         if ((t = mvc_bind_table(m, s, tname)) == NULL)
    4981           0 :                 throw(SQL, "sql.str_column_vacuum", SQLSTATE(42S02) "Invalid or missing table %s.%s",sname,tname);
    4982           1 :         if (!isTable(t))
    4983           0 :                 throw(SQL, "sql.str_column_vacuum", SQLSTATE(42000) "%s '%s' is not persistent",
    4984           0 :                           TABLE_TYPE_DESCRIPTION(t->type, t->properties), t->base.name);
    4985           1 :         if (isTempTable(t))
    4986           0 :                 throw(SQL, "sql.str_column_vacuum", SQLSTATE(42000) "Cannot vacuum column from temporary table");
    4987           1 :         if ((c = mvc_bind_column(m, t, cname)) == NULL)
    4988           0 :                 throw(SQL, "sql.str_column_vacuum", SQLSTATE(42S22) "Column not found in %s.%s.%s",sname,tname,cname);
    4989           1 :         if (c->storage_type)
    4990           0 :                 throw(SQL, "sql.str_column_vacuum", SQLSTATE(42000) "Cannot vacuum compressed column");
    4991             : 
    4992           1 :         return do_str_column_vacuum(tr, c, sname, tname, cname);
    4993             : }
    4994             : 
    4995             : 
    4996             : static gdk_return
    4997           0 : str_column_vacuum_callback(int argc, void *argv[]) {
    4998           0 :         sqlstore *store = (sqlstore *) argv[0];
    4999           0 :         char *sname = (char *) argv[1];
    5000           0 :         char *tname = (char *) argv[2];
    5001           0 :         char *cname = (char *) argv[3];
    5002           0 :         allocator *sa = NULL;
    5003           0 :         sql_session *session = NULL;
    5004           0 :         sql_schema *s = NULL;
    5005           0 :         sql_table *t = NULL;
    5006           0 :         sql_column *c = NULL;
    5007           0 :         char *msg;
    5008           0 :         gdk_return res = GDK_SUCCEED;
    5009             : 
    5010           0 :         (void) argc;
    5011             : 
    5012           0 :         if ((sa = sa_create(NULL)) == NULL) {
    5013           0 :                 TRC_ERROR((component_t) SQL, "[str_column_vacuum_callback] -- Failed to create allocator!");
    5014           0 :                 return GDK_FAIL;
    5015             :         }
    5016             : 
    5017           0 :         if ((session = sql_session_create(store, sa, 0)) == NULL) {
    5018           0 :                 TRC_ERROR((component_t) SQL, "[str_column_vacuum_callback] -- Failed to create session!");
    5019           0 :                 sa_destroy(sa);
    5020           0 :                 return GDK_FAIL;
    5021             :         }
    5022             : 
    5023           0 :         if (sql_trans_begin(session) < 0) {
    5024           0 :                 TRC_ERROR((component_t) SQL, "[str_column_vacuum_callback] -- Failed to begin transaction!");
    5025           0 :                 sql_session_destroy(session);
    5026           0 :                 sa_destroy(sa);
    5027           0 :                 return GDK_FAIL;
    5028             :         }
    5029             : 
    5030           0 :         do {
    5031           0 :                 if((s = find_sql_schema(session->tr, sname)) == NULL) {
    5032           0 :                         TRC_ERROR((component_t) SQL, "[str_column_vacuum_callback] -- Invalid or missing schema %s!",sname);
    5033           0 :                         res = GDK_FAIL;
    5034           0 :                         break;
    5035             :                 }
    5036             : 
    5037           0 :                 if((t = find_sql_table(session->tr, s, tname)) == NULL) {
    5038           0 :                         TRC_ERROR((component_t) SQL, "[str_column_vacuum_callback] -- Invalid or missing table %s!", tname);
    5039           0 :                         res = GDK_FAIL;
    5040           0 :                         break;
    5041             :                 }
    5042             : 
    5043           0 :                 if ((c = find_sql_column(t, cname)) == NULL) {
    5044           0 :                         TRC_ERROR((component_t) SQL, "[str_column_vacuum_callback] -- Invalid or missing column %s!", cname);
    5045           0 :                         res = GDK_FAIL;
    5046           0 :                         break;
    5047             :                 }
    5048             : 
    5049           0 :                 if((msg=do_str_column_vacuum(session->tr, c, sname, tname, cname)) != MAL_SUCCEED) {
    5050           0 :                         TRC_ERROR((component_t) SQL, "[str_column_vacuum_callback] -- %s", msg);
    5051           0 :                         res = GDK_FAIL;
    5052             :                 }
    5053             : 
    5054             :         } while(0);
    5055             : 
    5056           0 :         if (res == GDK_SUCCEED) { /* everything is ok, do the commit route */
    5057           0 :                 switch (sql_trans_end(session, SQL_OK)) {
    5058           0 :                         case SQL_ERR:
    5059           0 :                                 TRC_ERROR((component_t) SQL, "[str_column_vacuum_callback] -- transaction commit failed (kernel error: %s)", GDKerrbuf);
    5060           0 :                                 res = GDK_FAIL;
    5061           0 :                                 break;
    5062           0 :                         case SQL_CONFLICT:
    5063           0 :                                 TRC_ERROR((component_t) SQL, "[str_column_vacuum_callback] -- transaction is aborted because of concurrency conflicts, will ROLLBACK instead");
    5064           0 :                                 res = GDK_FAIL;
    5065           0 :                                 break;
    5066             :                         default:
    5067             :                                 break;
    5068             :                 }
    5069             :         } else { /* an error triggered, rollback and ignore further errors */
    5070           0 :                 (void)sql_trans_end(session, SQL_ERR);
    5071             :         }
    5072             : 
    5073           0 :         sql_session_destroy(session);
    5074           0 :         sa_destroy(sa);
    5075           0 :         return res;
    5076             : }
    5077             : 
    5078             : static gdk_return
    5079           0 : str_column_vacuum_callback_args_free(int argc, void *argv[])
    5080             : {
    5081           0 :         (void) argc;
    5082             :         // free up sname, tname, cname. First pointer points to sqlstore so leave it.
    5083           0 :         GDKfree(argv[1]); // sname
    5084           0 :         GDKfree(argv[2]); // tname
    5085           0 :         GDKfree(argv[3]); // cname
    5086           0 :         return GDK_SUCCEED;
    5087             : }
    5088             : 
    5089             : str
    5090           0 : SQLstr_column_auto_vacuum(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    5091             : {
    5092           0 :         mvc *m = NULL;
    5093           0 :         str msg = NULL;
    5094           0 :         char *sname = *getArgReference_str(stk, pci, 1);
    5095           0 :         char *tname = *getArgReference_str(stk, pci, 2);
    5096           0 :         char *cname = *getArgReference_str(stk, pci, 3);
    5097           0 :         int interval = *getArgReference_int(stk, pci, 4); // in sec
    5098           0 :         char *sname_copy = NULL, *tname_copy = NULL, *cname_copy = NULL;
    5099             : 
    5100           0 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    5101             :                 return msg;
    5102           0 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    5103             :                 return msg;
    5104             : 
    5105           0 :         sql_schema *s = NULL;
    5106           0 :         sql_table *t = NULL;
    5107           0 :         sql_column *c = NULL;
    5108             : 
    5109           0 :         if (strNil(sname))
    5110           0 :                 throw(SQL, "sql.str_column_auto_vacuum", SQLSTATE(42000) "Schema name cannot be NULL");
    5111           0 :         if (strNil(tname))
    5112           0 :                 throw(SQL, "sql.str_column_auto_vacuum", SQLSTATE(42000) "Table name cannot be NULL");
    5113           0 :         if (strNil(cname))
    5114           0 :                 throw(SQL, "sql.str_column_auto_vacuum", SQLSTATE(42000) "Column name cannot be NULL");
    5115           0 :         if ((s = mvc_bind_schema(m, sname)) == NULL)
    5116           0 :                 throw(SQL, "sql.str_column_auto_vacuum", SQLSTATE(3F000) "Invalid or missing schema %s",sname);
    5117           0 :         if ((t = mvc_bind_table(m, s, tname)) == NULL)
    5118           0 :                 throw(SQL, "sql.str_column_auto_vacuum", SQLSTATE(42S02) "Invalid or missing table %s.%s",sname,tname);
    5119           0 :         if (!isTable(t))
    5120           0 :                 throw(SQL, "sql.str_column_auto_vacuum", SQLSTATE(42000) "%s '%s' is not persistent",
    5121           0 :                           TABLE_TYPE_DESCRIPTION(t->type, t->properties), t->base.name);
    5122           0 :         if (isTempTable(t))
    5123           0 :                 throw(SQL, "sql.str_column_auto_vacuum", SQLSTATE(42000) "Cannot vacuum column from temporary table");
    5124           0 :         if ((c = mvc_bind_column(m, t, cname)) == NULL)
    5125           0 :                 throw(SQL, "sql.str_column_auto_vacuum", SQLSTATE(42S22) "Column not found in %s.%s.%s",sname,tname,cname);
    5126           0 :         if (c->storage_type)
    5127           0 :                 throw(SQL, "sql.str_column_auto_vacuum", SQLSTATE(42000) "Cannot vacuum compressed column");
    5128             : 
    5129           0 :         if (!(sname_copy = GDKstrdup(sname)) || !(tname_copy = GDKstrdup(tname)) || !(cname_copy = GDKstrdup(cname))) {
    5130           0 :                 GDKfree(sname_copy);
    5131           0 :                 GDKfree(tname_copy);
    5132           0 :                 GDKfree(cname_copy);
    5133           0 :                 throw(SQL, "sql.str_column_auto_vacuum", SQLSTATE(HY013) MAL_MALLOC_FAIL);
    5134             :         }
    5135           0 :         void *argv[4] = {m->store, sname_copy, tname_copy, cname_copy};
    5136             : 
    5137           0 :         if (gdk_add_callback("str_column_vacuum", str_column_vacuum_callback, 4, argv, interval) != GDK_SUCCEED) {
    5138           0 :                 str_column_vacuum_callback_args_free(4, argv);
    5139           0 :                 throw(SQL, "sql.str_column_auto_vacuum", "adding vacuum callback failed!");
    5140             :         }
    5141             : 
    5142             :         return MAL_SUCCEED;
    5143             : }
    5144             : 
    5145             : str
    5146           0 : SQLstr_column_stop_vacuum(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    5147             : {
    5148           0 :         mvc *m = NULL;
    5149           0 :         str msg = NULL;
    5150           0 :         char *sname = *getArgReference_str(stk, pci, 1);
    5151           0 :         char *tname = *getArgReference_str(stk, pci, 2);
    5152           0 :         char *cname = *getArgReference_str(stk, pci, 3);
    5153             : 
    5154           0 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    5155             :                 return msg;
    5156           0 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    5157             :                 return msg;
    5158             : 
    5159           0 :         sql_schema *s = NULL;
    5160           0 :         sql_table *t = NULL;
    5161           0 :         sql_column *c = NULL;
    5162             : 
    5163           0 :         if (strNil(sname))
    5164           0 :                 throw(SQL, "sql.str_column_stop_vacuum", SQLSTATE(42000) "Schema name cannot be NULL");
    5165           0 :         if (strNil(tname))
    5166           0 :                 throw(SQL, "sql.str_column_stop_vacuum", SQLSTATE(42000) "Table name cannot be NULL");
    5167           0 :         if (strNil(cname))
    5168           0 :                 throw(SQL, "sql.str_column_stop_vacuum", SQLSTATE(42000) "Column name cannot be NULL");
    5169           0 :         if ((s = mvc_bind_schema(m, sname)) == NULL)
    5170           0 :                 throw(SQL, "sql.str_column_stop_vacuum", SQLSTATE(3F000) "Invalid or missing schema %s",sname);
    5171           0 :         if ((t = mvc_bind_table(m, s, tname)) == NULL)
    5172           0 :                 throw(SQL, "sql.str_column_stop_vacuum", SQLSTATE(42S02) "Invalid or missing table %s.%s",sname,tname);
    5173           0 :         if (!isTable(t))
    5174           0 :                 throw(SQL, "sql.str_column_stop_vacuum", SQLSTATE(42000) "%s '%s' is not persistent",
    5175           0 :                           TABLE_TYPE_DESCRIPTION(t->type, t->properties), t->base.name);
    5176           0 :         if (isTempTable(t))
    5177           0 :                 throw(SQL, "sql.str_column_stop_vacuum", SQLSTATE(42000) "Cannot vacuum column from temporary table");
    5178           0 :         if ((c = mvc_bind_column(m, t, cname)) == NULL)
    5179           0 :                 throw(SQL, "sql.str_column_stop_vacuum", SQLSTATE(42S22) "Column not found in %s.%s.%s",sname,tname,cname);
    5180             : 
    5181           0 :         if(gdk_remove_callback("str_column_vacuum", str_column_vacuum_callback_args_free) != GDK_SUCCEED)
    5182           0 :                 throw(SQL, "sql.str_column_stop_vacuum", "removing vacuum callback failed!");
    5183             : 
    5184             :         return MAL_SUCCEED;
    5185             : }
    5186             : 
    5187             : 
    5188             : #include "sql_cat.h"
    5189             : #include "sql_rank.h"
    5190             : #include "sql_user.h"
    5191             : #include "sql_assert.h"
    5192             : #include "sql_execute.h"
    5193             : #include "sql_orderidx.h"
    5194             : #include "sql_strimps.h"
    5195             : #include "sql_subquery.h"
    5196             : #include "sql_statistics.h"
    5197             : #include "sql_transaction.h"
    5198             : #include "for.h"
    5199             : #include "dict.h"
    5200             : #include "mel.h"
    5201             : 
    5202             : 
    5203             : static str
    5204          50 : SQLuser_password(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    5205             : {
    5206          50 :         mvc *m = NULL;
    5207          50 :         str msg = NULL;
    5208          50 :         str *password = getArgReference_str(stk, pci, 0);
    5209          50 :         const char *username = *getArgReference_str(stk, pci, 1);
    5210             : 
    5211          50 :         (void) password;
    5212             : 
    5213          50 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    5214             :                 return msg;
    5215          50 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    5216             :                 return msg;
    5217          50 :         if (cntxt->username != username) {
    5218             :                 // only MAL_ADMIN and user himself can access password
    5219          50 :                 if ((msg = AUTHrequireAdmin(cntxt)) != MAL_SUCCEED)
    5220             :                         return msg;
    5221             :         }
    5222          50 :         *password = monet5_password_hash(m, username);
    5223          50 :         return MAL_SUCCEED;
    5224             : }
    5225             : 
    5226             : static str
    5227          10 : SQLdecypher(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
    5228             : {
    5229          10 :         mvc *m = NULL;
    5230          10 :         str msg = NULL;
    5231          10 :         str *pwhash = getArgReference_str(stk, pci, 0);
    5232          10 :         const char *cypher = *getArgReference_str(stk, pci, 1);
    5233             : 
    5234          10 :         (void) pwhash;
    5235             : 
    5236          10 :         if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL)
    5237             :                 return msg;
    5238          10 :         if ((msg = checkSQLContext(cntxt)) != NULL)
    5239             :                 return msg;
    5240          10 :         return AUTHdecypherValue(pwhash, cypher);
    5241             : }
    5242             : 
    5243             : static mel_func sql_init_funcs[] = {
    5244             :  pattern("sql", "shutdown", SQLshutdown_wrap, true, "", args(1,3, arg("",str),arg("delay",bte),arg("force",bit))),
    5245             :  pattern("sql", "shutdown", SQLshutdown_wrap, true, "", args(1,3, arg("",str),arg("delay",sht),arg("force",bit))),
    5246             :  pattern("sql", "shutdown", SQLshutdown_wrap, true, "", args(1,3, arg("",str),arg("delay",int),arg("force",bit))),
    5247             :  pattern("sql", "shutdown", SQLshutdown_wrap, true, "", args(1,2, arg("",str),arg("delay",bte))),
    5248             :  pattern("sql", "shutdown", SQLshutdown_wrap, true, "", args(1,2, arg("",str),arg("delay",sht))),
    5249             :  pattern("sql", "shutdown", SQLshutdown_wrap, true, "", args(1,2, arg("",str),arg("delay",int))),
    5250             :  pattern("sql", "set_protocol", SQLset_protocol, true, "Configures the result set protocol", args(1,2, arg("",int), arg("protocol",int))),
    5251             :  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))),
    5252             :  pattern("sql", "eval", SQLstatement, true, "Compile and execute a single sql statement", args(1,2, arg("",void),arg("cmd",str))),
    5253             :  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))),
    5254             :  pattern("sql", "include", SQLinclude, true, "Compile and execute a sql statements on the file", args(1,2, arg("",void),arg("fname",str))),
    5255             :  pattern("sql", "evalAlgebra", RAstatement, true, "Compile and execute a single 'relational algebra' statement", args(1,3, arg("",void),arg("cmd",str),arg("optimize",bit))),
    5256             :  pattern("sql", "register", RAstatement2, true, "", args(1,5, arg("",int),arg("mod",str),arg("fname",str),arg("rel_stmt",str),arg("sig",str))),
    5257             :  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))),
    5258             :  pattern("sql", "deregister", RAstatementEnd, true, "Finish running transaction", args(1,1, arg("",int))),
    5259             :  pattern("sql", "hot_snapshot", SQLhot_snapshot, true, "Write db snapshot to the given tar(.gz) file", args(1,2, arg("",void),arg("tarfile",str))),
    5260             :  pattern("sql", "resume_log_flushing", SQLresume_log_flushing, true, "Resume WAL log flushing", args(1,1, arg("",void))),
    5261             :  pattern("sql", "suspend_log_flushing", SQLsuspend_log_flushing, true, "Suspend WAL log flushing", args(1,1, arg("",void))),
    5262             :  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))),
    5263             :  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))),
    5264             :  pattern("sql", "assert", SQLassert, false, "Generate an exception when b==true", args(1,3, arg("",void),arg("b",bit),arg("msg",str))),
    5265             :  pattern("sql", "assert", SQLassertInt, false, "Generate an exception when b!=0", args(1,3, arg("",void),arg("b",int),arg("msg",str))),
    5266             :  pattern("sql", "assert", SQLassertLng, false, "Generate an exception when b!=0", args(1,3, arg("",void),arg("b",lng),arg("msg",str))),
    5267             :  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))),
    5268             :  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))),
    5269             :  pattern("sql", "logfile", mvc_logfile, true, "Enable/disable saving the sql statement traces", args(1,2, arg("",void),arg("filename",str))),
    5270             :  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))),
    5271             :  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))),
    5272             :  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))),
    5273             :  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))),
    5274             :  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))),
    5275             :  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))),
    5276             :  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))),
    5277             :  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))),
    5278             :  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))),
    5279             :  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))),
    5280             :  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))),
    5281             :  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))),
    5282             :  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))),
    5283             :  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))),
    5284             :  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))),
    5285             :  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))),
    5286             :  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))),
    5287             :  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))),
    5288             :  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))),
    5289             :  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))),
    5290             :  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))),
    5291             :  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))),
    5292             :  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))),
    5293             :  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))),
    5294             :  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))),
    5295             :  command("sql", "delta", DELTAbat, false, "Return column bat with delta's applied.", args(1,4, batargany("",1),batargany("col",1),batarg("uid",oid),batargany("uval",1))),
    5296             :  command("sql", "projectdelta", DELTAproject, false, "Return column bat with delta's applied.", args(1,5, batargany("",1),batarg("select",oid),batargany("col",1),batarg("uid",oid),batargany("uval",1))),
    5297             :  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))),
    5298             :  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))),
    5299             :  command("sql", "getVersion", mvc_getVersion, false, "Return the database version identifier for a client.", args(1,2, arg("",lng),arg("clientid",int))),
    5300             :  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))),
    5301             :  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))),
    5302             :  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))),
    5303             :  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))),
    5304             :  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))),
    5305             :  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))),
    5306             :  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))),
    5307             :  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))),
    5308             :  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))),
    5309             :  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))),
    5310             :  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))),
    5311             :  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))),
    5312             :  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))),
    5313             :  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))),
    5314             :  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))),
    5315             :  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))),
    5316             :  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))),
    5317             :  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))),
    5318             :  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))),
    5319             :  pattern("sql", "exportOperation", mvc_export_operation_wrap, true, "Export result of schema/transaction queries", args(1,1, arg("",void))),
    5320             :  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))),
    5321             :  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))),
    5322             :  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))),
    5323             :  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,15, 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),arg("decsep",str),arg("decskip",str))),
    5324             :  //we use bat.single now
    5325             :  //pattern("sql", "single", CMDBATsingle, false, "", args(1,2, batargany("",2),argany("x",2))),
    5326             :  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))),
    5327             :  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))),
    5328             :  command("sql", "optimizers", getPipeCatalog, false, "", args(3,3, batarg("",str),batarg("",str),batarg("",str))),
    5329             :  pattern("sql", "optimizer_updates", SQLoptimizersUpdate, false, "", noargs),
    5330             :  pattern("sql", "argRecord", SQLargRecord, false, "Glue together the calling sequence", args(1,1, arg("",str))),
    5331             :  pattern("sql", "argRecord", SQLargRecord, false, "Glue together the calling sequence", args(1,2, arg("",str),varargany("a",0))),
    5332             :  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))),
    5333             :  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))),
    5334             : pattern("sql", "password", SQLuser_password, false, "Return password hash of user", args(1,2, arg("",str),arg("user",str))),
    5335             : pattern("sql", "decypher", SQLdecypher, false, "Return decyphered password", args(1,2, arg("",str),arg("hash",str))),
    5336             :  pattern("sql", "dump_cache", dump_cache, false, "dump the content of the query cache", args(2,2, batarg("query",str),batarg("count",int))),
    5337             :  pattern("sql", "dump_opt_stats", dump_opt_stats, false, "dump the optimizer rewrite statistics", args(2,2, batarg("rewrite",str),batarg("count",int))),
    5338             :  pattern("sql", "dump_trace", dump_trace, false, "dump the trace statistics", args(3,3, batarg("ticks",lng),batarg("stmt",str),batarg("stmt",str))),
    5339             :  pattern("sql", "analyze", sql_analyze, true, "Update statistics for every column in the database", args(1,1, arg("",void))),
    5340             :  pattern("sql", "analyze", sql_analyze, true, "Update statistics for schema", args(1,2, arg("",void),arg("sch",str))),
    5341             :  pattern("sql", "analyze", sql_analyze, true, "Update statistics for table", args(1,3, arg("",void),arg("sch",str),arg("tbl",str))),
    5342             :  pattern("sql", "analyze", sql_analyze, true, "Update statistics for column", args(1,4, arg("",void),arg("sch",str),arg("tbl",str),arg("col",str))),
    5343             :  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))),
    5344             :  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))),
    5345             :  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))),
    5346             :  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))),
    5347             :  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))),
    5348             :  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))),
    5349             :  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))),
    5350             :  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))),
    5351             :  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))),
    5352             :  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))),
    5353             :  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))),
    5354             :  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))),
    5355             :  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))),
    5356             :  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))),
    5357             :  command("calc", "identity", SQLidentity, false, "Returns a unique row identitfier.", args(1,2, arg("",oid),argany("",0))),
    5358             :  command("batcalc", "identity", BATSQLidentity, false, "Returns the unique row identitfiers.", args(1,2, batarg("",oid),batargany("b",0))),
    5359             :  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))),
    5360             :  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))),
    5361             :  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))),
    5362             :  pattern("sql", "querylog_empty", sql_querylog_empty, true, "", noargs),
    5363             :  command("sql", "querylog_enable", QLOGenable, true, "", noargs),
    5364             :  command("sql", "querylog_enable", QLOGenableThreshold, true, "", args(0,1, arg("thres",int))),
    5365             :  command("sql", "querylog_disable", QLOGdisable, true, "", noargs),
    5366             :  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))),
    5367             :  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))),
    5368             :  pattern("sql", "copy_rejects", COPYrejects, false, "", args(4,4, batarg("rowid",lng),batarg("fldid",int),batarg("msg",str),batarg("inp",str))),
    5369             :  pattern("sql", "copy_rejects_clear", COPYrejects_clear, true, "", noargs),
    5370             :  pattern("for", "compress", FORcompress_col, false, "compress a sql column", args(0, 3, arg("schema", str), arg("table", str), arg("column", str))),
    5371             :  pattern("for", "decompress", FORdecompress, false, "decompress a for compressed (sub)column", args(1, 3, batargany("", 1), batargany("o", 0), argany("minval", 1))),
    5372             :  pattern("dict", "compress", DICTcompress, false, "dict compress a bat", args(2, 3, batargany("o", 0), batargany("v", 1), batargany("b", 1))),
    5373             :  pattern("dict", "compress", DICTcompress_col, false, "compress a sql column", args(0, 3, arg("schema", str), arg("table", str), arg("column", str))),
    5374             :  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))),
    5375             :  pattern("dict", "decompress", DICTdecompress, false, "decompress a dictionary compressed (sub)column", args(1, 3, batargany("", 1), batargany("o", 0), batargany("u", 1))),
    5376             :  pattern("dict", "convert", DICTconvert, false, "convert candidate list into compressed offsets", args(1, 2, batargany("", 1), batargany("o", 0))),
    5377             :  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))),
    5378             :  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))),
    5379             :  pattern("dict", "renumber", DICTrenumber, false, "renumber offsets", args(1, 3, batargany("n", 1), batargany("o", 1), batargany("r", 1))),
    5380             :  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))),
    5381             :  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))),
    5382             :  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))),
    5383             :  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))),
    5384             :  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))),
    5385             :  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))),
    5386             :  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))),
    5387             :  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))),
    5388             :  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))),
    5389             :  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))),
    5390             :  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))),
    5391             :  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))),
    5392             :  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))),
    5393             :  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))),
    5394             :  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))),
    5395             :  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))),
    5396             :  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))),
    5397             :  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))),
    5398             :  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))),
    5399             :  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))),
    5400             :  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))),
    5401             :  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))),
    5402             :  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))),
    5403             :  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))),
    5404             :  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))),
    5405             :  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))),
    5406             :  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))),
    5407             :  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))),
    5408             :  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))),
    5409             :  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))),
    5410             :  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))),
    5411             :  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))),
    5412             :  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))),
    5413             :  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))),
    5414             :  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))),
    5415             :  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))),
    5416             :  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))),
    5417             :  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))),
    5418             :  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))),
    5419             :  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))),
    5420             :  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))),
    5421             :  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))),
    5422             :  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))),
    5423             :  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))),
    5424             :  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))),
    5425             :  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))),
    5426             :  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))),
    5427             :  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))),
    5428             :  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))),
    5429             :  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))),
    5430             :  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))),
    5431             :  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))),
    5432             :  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))),
    5433             :  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))),
    5434             :  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))),
    5435             :  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))),
    5436             :  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))),
    5437             :  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))),
    5438             :  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))),
    5439             :  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))),
    5440             :  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))),
    5441             :  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))),
    5442             :  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))),
    5443             :  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))),
    5444             :  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))),
    5445             :  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))),
    5446             :  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))),
    5447             :  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))),
    5448             :  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))),
    5449             :  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))),
    5450             :  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))),
    5451             :  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))),
    5452             :  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))),
    5453             :  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))),
    5454             :  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))),
    5455             :  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))),
    5456             :  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))),
    5457             :  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))),
    5458             :  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))),
    5459             :  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))),
    5460             :  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))),
    5461             :  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))),
    5462             :  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))),
    5463             :  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))),
    5464             :  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))),
    5465             :  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))),
    5466             :  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))),
    5467             :  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))),
    5468             :  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))),
    5469             :  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))),
    5470             :  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))),
    5471             :  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))),
    5472             :  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))),
    5473             :  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))),
    5474             :  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))),
    5475             :  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))),
    5476             :  command("batsql", "alpha", SQLbat_alpha_cst, false, "BAT implementation of astronomy alpha function", args(1,3, batarg("",dbl),batarg("dec",dbl),arg("theta",dbl))),
    5477             :  command("batsql", "alpha", SQLcst_alpha_bat, false, "BAT implementation of astronomy alpha function", args(1,3, batarg("",dbl),arg("dec",dbl),batarg("theta",dbl))),
    5478             :  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))),
    5479             :  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))),
    5480             :  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))),
    5481             :  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))),
    5482             :  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))),
    5483             :  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))),
    5484             :  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))),
    5485             :  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))),
    5486             :  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))),
    5487             :  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))),
    5488             :  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))),
    5489             :  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))),
    5490             :  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))),
    5491             :  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))),
    5492             :  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))),
    5493             :  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))),
    5494             :  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))),
    5495             :  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))),
    5496             :  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))),
    5497             :  pattern("calc", "timestamptz", str_2time_timestamptz, false, "cast to timestamp and check for overflow", args(1,4, arg("",timestamp),arg("v",str),arg("digits",int),arg("tz_msec",lng))),
    5498             :  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))),
    5499             :  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))),
    5500             :  command("batcalc", "timestamptz", 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("tz_msec",lng))),
    5501             :  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))),
    5502             :  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))),
    5503             :  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))),
    5504             :  pattern("calc", "daytimetz", str_2time_daytimetz, false, "cast to daytime and check for overflow", args(1,4, arg("",daytime),arg("v",str),arg("digits",int),arg("tz_msec",lng))),
    5505             :  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))),
    5506             :  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))),
    5507             :  pattern("batcalc", "daytimetz", 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("tz_msec",lng))),
    5508             :  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))),
    5509             :  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))),
    5510             :  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))),
    5511             :  pattern("sql", "current_time", SQLcurrent_daytime, false, "Get the clients current daytime", args(1,1, arg("",daytime))),
    5512             :  pattern("sql", "current_timestamp", SQLcurrent_timestamp, false, "Get the clients current timestamp", args(1,1, arg("",timestamp))),
    5513             :  pattern("calc", "date", nil_2_date, false, "cast to date", args(1,2, arg("",date),arg("v",void))),
    5514             :  pattern("batcalc", "date", nil_2_date, false, "cast to date", args(1,2, batarg("",date),batarg("v",oid))),
    5515             :  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))),
    5516             :  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))),
    5517             :  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))),
    5518             :  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))),
    5519             :  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))),
    5520             :  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))),
    5521             :  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))),
    5522             :  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))),
    5523             :  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))),
    5524             :  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))),
    5525             :  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))),
    5526             :  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))),
    5527             :  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))),
    5528             :  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))),
    5529             :  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))),
    5530             :  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))),
    5531             :  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))),
    5532             :  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))),
    5533             :  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))),
    5534             :  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))),
    5535             :  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))),
    5536             :  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))),
    5537             :  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))),
    5538             :  pattern("calc", "rowid", sql_rowid, false, "return the next rowid", args(1,4, arg("",oid),argany("v",1),arg("schema",str),arg("table",str))),
    5539             :  pattern("sql", "drop_hash", SQLdrop_hash, true, "Drop hash indices for the given table", args(0,2, arg("sch",str),arg("tbl",str))),
    5540             :  pattern("sql", "prelude", SQLprelude, false, "", noargs),
    5541             :  command("sql", "epilogue", SQLepilogue, false, "", noargs),
    5542             :  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))),
    5543             :  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))),
    5544             :  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))),
    5545             :  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))),
    5546             :  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))),
    5547             :  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))),
    5548             :  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))),
    5549             :  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))),
    5550             :  pattern("sql", "update_tables", SYSupdate_tables, true, "Procedure triggered on update of the sys._tables table", args(1,1, arg("",void))),
    5551             :  pattern("sql", "update_schemas", SYSupdate_schemas, true, "Procedure triggered on update of the sys.schemas table", args(1,1, arg("",void))),
    5552             :  pattern("sql", "unionfunc", SQLunionfunc, false, "", args(1,4, varargany("",0),arg("mod",str),arg("fcn",str),varargany("",0))),
    5553             :  /* decimals */
    5554             :  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))),
    5555             :  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))),
    5556             :  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))),
    5557             :  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))),
    5558             :  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))),
    5559             :  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))),
    5560             :  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))),
    5561             :  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))),
    5562             :  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))),
    5563             :  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))),
    5564             :  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))),
    5565             :  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))),
    5566             :  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))),
    5567             :  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))),
    5568             :  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))),
    5569             :  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))),
    5570             :  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))),
    5571             :  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))),
    5572             :  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))),
    5573             :  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))),
    5574             :  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))),
    5575             :  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))),
    5576             :  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))),
    5577             :  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))),
    5578             :  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))),
    5579             :  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))),
    5580             :  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))),
    5581             :  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))),
    5582             :  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))),
    5583             :  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))),
    5584             :  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))),
    5585             :  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))),
    5586             :  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))),
    5587             :  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))),
    5588             :  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))),
    5589             :  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))),
    5590             :  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))),
    5591             :  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))),
    5592             :  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))),
    5593             :  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))),
    5594             :  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))),
    5595             :  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))),
    5596             :  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))),
    5597             :  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))),
    5598             :  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))),
    5599             :  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))),
    5600             :  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))),
    5601             :  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))),
    5602             :  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))),
    5603             :  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))),
    5604             :  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))),
    5605             :  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))),
    5606             :  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))),
    5607             :  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))),
    5608             :  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))),
    5609             :  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))),
    5610             :  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))),
    5611             :  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))),
    5612             :  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))),
    5613             :  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))),
    5614             :  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))),
    5615             :  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))),
    5616             :  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))),
    5617             :  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))),
    5618             :  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))),
    5619             :  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))),
    5620             :  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))),
    5621             :  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))),
    5622             :  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))),
    5623             :  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))),
    5624             :  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))),
    5625             :  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))),
    5626             :  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))),
    5627             :  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))),
    5628             :  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))),
    5629             :  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))),
    5630             :  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))),
    5631             :  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))),
    5632             :  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))),
    5633             :  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))),
    5634             :  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))),
    5635             :  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))),
    5636             :  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))),
    5637             :  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))),
    5638             :  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))),
    5639             :  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))),
    5640             :  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))),
    5641             :  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))),
    5642             :  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))),
    5643             :  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))),
    5644             :  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))),
    5645             :  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))),
    5646             :  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))),
    5647             :  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))),
    5648             :  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))),
    5649             :  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))),
    5650             :  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))),
    5651             :  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))),
    5652             :  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))),
    5653             :  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))),
    5654             :  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))),
    5655             :  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))),
    5656             :  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))),
    5657             :  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))),
    5658             :  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))),
    5659             :  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))),
    5660             :  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))),
    5661             :  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))),
    5662             :  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))),
    5663             :  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))),
    5664             :  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))),
    5665             :  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))),
    5666             :  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))),
    5667             :  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))),
    5668             :  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))),
    5669             :  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))),
    5670             :  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))),
    5671             :  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))),
    5672             :  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))),
    5673             :  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))),
    5674             :  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))),
    5675             :  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))),
    5676             :  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))),
    5677             :  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))),
    5678             :  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))),
    5679             :  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))),
    5680             :  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))),
    5681             :  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))),
    5682             :  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))),
    5683             :  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))),
    5684             :  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))),
    5685             :  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))),
    5686             :  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))),
    5687             :  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))),
    5688             :  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))),
    5689             :  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))),
    5690             :  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))),
    5691             :  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))),
    5692             :  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))),
    5693             :  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))),
    5694             :  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))),
    5695             :  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))),
    5696             :  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))),
    5697             :  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))),
    5698             :  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))),
    5699             :  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))),
    5700             :  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))),
    5701             :  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))),
    5702             :  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))),
    5703             :  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))),
    5704             :  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))),
    5705             :  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))),
    5706             :  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))),
    5707             :  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))),
    5708             :  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))),
    5709             :  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))),
    5710             :  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))),
    5711             :  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))),
    5712             :  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))),
    5713             :  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))),
    5714             :  /* sql_rank */
    5715             :  pattern("sql", "diff", SQLdiff, false, "return true if cur != prev row", args(1,2, arg("",bit),argany("b",1))),
    5716             :  pattern("batsql", "diff", SQLdiff, false, "return true if cur != prev row", args(1,2, batarg("",bit),batargany("b",1))),
    5717             :  pattern("sql", "diff", SQLdiff, false, "return true if cur != prev row", args(1,3, arg("",bit),arg("p",bit),argany("b",1))),
    5718             :  pattern("batsql", "diff", SQLdiff, false, "return true if cur != prev row", args(1,3, batarg("",bit),arg("p",bit),batargany("b",1))),
    5719             :  pattern("batsql", "diff", SQLdiff, false, "return true if cur != prev row", args(1,3, batarg("",bit),batarg("p",bit),argany("b",1))),
    5720             :  pattern("batsql", "diff", SQLdiff, false, "return true if cur != prev row", args(1,3, batarg("",bit),batarg("p",bit),batargany("b",1))),
    5721             :  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))),
    5722             :  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),optbatarg("limit",bte))),
    5723             :  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))),
    5724             :  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),optbatarg("limit",bte))),
    5725             :  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))),
    5726             :  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),optbatarg("limit",sht))),
    5727             :  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))),
    5728             :  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),optbatarg("limit",sht))),
    5729             :  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))),
    5730             :  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),optbatarg("limit",int))),
    5731             :  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))),
    5732             :  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),optbatarg("limit",int))),
    5733             :  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))),
    5734             :  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),optbatarg("limit",lng))),
    5735             :  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))),
    5736             :  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),optbatarg("limit",lng))),
    5737             :  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))),
    5738             :  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),optbatarg("limit",flt))),
    5739             :  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))),
    5740             :  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),optbatarg("limit",flt))),
    5741             :  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))),
    5742             :  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),optbatarg("limit",dbl))),
    5743             :  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))),
    5744             :  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),optbatarg("limit",dbl))),
    5745             :  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))),
    5746             :  pattern("batsql", "row_number", SQLrow_number, false, "return the row_numer-ed groups", args(1,4, batarg("",int),batargany("b",1),optbatarg("p",bit),optbatarg("o",bit))),
    5747             :  pattern("sql", "rank", SQLrank, false, "return the ranked groups", args(1,4, arg("",int),argany("b",1),arg("p",bit),arg("o",bit))),
    5748             :  pattern("batsql", "rank", SQLrank, false, "return the ranked groups", args(1,4, batarg("",int),batargany("b",1),optbatarg("p",bit),optbatarg("o",bit))),
    5749             :  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))),
    5750             :  pattern("batsql", "dense_rank", SQLdense_rank, false, "return the densely ranked groups", args(1,4, batarg("",int),batargany("b",1),optbatarg("p",bit),optbatarg("o",bit))),
    5751             :  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))),
    5752             :  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),optbatarg("p",bit),optbatarg("o",bit))),
    5753             :  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))),
    5754             :  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),optbatarg("p",bit),optbatarg("o",bit))),
    5755             :  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))),
    5756             :  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),optbatarg("p",bit),optbatarg("o",bit))),
    5757             :  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))),
    5758             :  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),optbatargany("b",1),optbatargany("l",0),optbatarg("p",bit),optbatarg("o",bit))),
    5759             :  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))),
    5760             :  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),optbatargany("b",1),optbatargany("l",0),optbatargany("d",1),optbatarg("p",bit),optbatarg("o",bit))),
    5761             :  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))),
    5762             :  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),optbatarg("p",bit),optbatarg("o",bit))),
    5763             :  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))),
    5764             :  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),optbatargany("b",1),optbatargany("l",0),optbatarg("p",bit),optbatarg("o",bit))),
    5765             :  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))),
    5766             :  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),optbatargany("b",1),optbatargany("l",0),optbatargany("d",1),optbatarg("p",bit),optbatarg("o",bit))),
    5767             :  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))),
    5768             :  pattern("batsql", "ntile", SQLntile, false, "return the groups divided as equally as possible", args(1,5, batargany("",1),optbatargany("b",0),optbatargany("n",1),optbatarg("p",bit),optbatarg("o",bit))),
    5769             :  /* these window functions support frames */
    5770             :  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))),
    5771             :  pattern("batsql", "first_value", SQLfirst_value, false, "return the first value of groups", args(1,7, batargany("",1),batargany("b",1),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5772             :  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))),
    5773             :  pattern("batsql", "last_value", SQLlast_value, false, "return the last value of groups", args(1,7, batargany("",1),batargany("b",1),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5774             :  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))),
    5775             :  pattern("batsql", "nth_value", SQLnth_value, false, "return the nth value of each group", args(1,8, batargany("",1),optbatargany("b",1),optbatarg("n",lng),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5776             :  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))),
    5777             :  pattern("batsql", "min", SQLmin, false, "return the minimum of groups", args(1,7, batargany("",1),batargany("b",1),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5778             :  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))),
    5779             :  pattern("batsql", "max", SQLmax, false, "return the maximum of groups",args(1,7, batargany("",1),batargany("b",1),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5780             :  pattern("sql", "count", SQLbasecount, false, "return count of basetable", args(1,3, arg("",lng),arg("sname",str),arg("tname",str))),
    5781             :  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))),
    5782             :  pattern("batsql", "count", SQLcount, false,"return count of groups",args(1,8, batarg("",lng),batargany("b",1),arg("ignils",bit),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5783             :  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))),
    5784             :  pattern("batsql", "sum", SQLsum, false, "return the sum of groups", args(1,7, batarg("",lng),batarg("b",bte),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5785             :  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))),
    5786             :  pattern("batsql", "sum", SQLsum, false, "return the sum of groups", args(1,7, batarg("",lng),batarg("b",sht),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5787             :  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))),
    5788             :  pattern("batsql", "sum", SQLsum, false, "return the sum of groups", args(1,7, batarg("",lng),batarg("b",int),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5789             :  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))),
    5790             :  pattern("batsql", "sum", SQLsum, false, "return the sum of groups", args(1,7, batarg("",lng),batarg("b",lng),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5791             :  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))),
    5792             :  pattern("batsql", "sum", SQLsum, false, "return the sum of groups", args(1,7, batarg("",flt),batarg("b",flt),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5793             :  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))),
    5794             :  pattern("batsql", "sum", SQLsum, false, "return the sum of groups", args(1,7, batarg("",dbl),batarg("b",flt),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5795             :  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))),
    5796             :  pattern("batsql", "sum", SQLsum, false, "return the sum of groups", args(1,7, batarg("",dbl),batarg("b",dbl),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5797             :  /* sql.sum for month intervals */
    5798             :  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))),
    5799             :  pattern("batsql", "sum", SQLsum, false, "return the sum of groups", args(1,7, batarg("",int),batarg("b",int),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5800             :  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))),
    5801             :  pattern("batsql", "prod", SQLprod, false, "return the product of groups", args(1,7, batarg("",lng),batarg("b",bte),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5802             :  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))),
    5803             :  pattern("batsql", "prod", SQLprod, false, "return the product of groups", args(1,7, batarg("",lng),batarg("b",sht),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5804             :  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))),
    5805             :  pattern("batsql", "prod", SQLprod, false, "return the product of groups", args(1,7, batarg("",lng),batarg("b",int),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5806             :  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))),
    5807             :  pattern("batsql", "prod", SQLprod, false, "return the product of groups", args(1,7, batarg("",lng),batarg("b",lng),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5808             :  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))),
    5809             :  pattern("batsql", "prod", SQLprod, false, "return the product of groups", args(1,7, batarg("",flt),batarg("b",flt),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5810             :  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))),
    5811             :  pattern("batsql", "prod", SQLprod, false, "return the product of groups", args(1,7, batarg("",dbl),batarg("b",flt),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5812             :  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))),
    5813             :  pattern("batsql", "prod", SQLprod, false, "return the product of groups", args(1,7, batarg("",dbl),batarg("b",dbl),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5814             :  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))),
    5815             :  pattern("batsql", "avg", SQLavg, false, "return the average of groups", args(1,7, batarg("",dbl),batarg("b",bte),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5816             :  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))),
    5817             :  pattern("batsql", "avg", SQLavg, false, "return the average of groups", args(1,7, batarg("",dbl),batarg("b",sht),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5818             :  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))),
    5819             :  pattern("batsql", "avg", SQLavg, false, "return the average of groups", args(1,7, batarg("",dbl),batarg("b",int),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5820             :  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))),
    5821             :  pattern("batsql", "avg", SQLavg, false, "return the average of groups", args(1,7, batarg("",dbl),batarg("b",lng),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5822             :  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))),
    5823             :  pattern("batsql", "avg", SQLavg, false, "return the average of groups", args(1,7, batarg("",dbl),batarg("b",flt),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5824             :  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))),
    5825             :  pattern("batsql", "avg", SQLavg, false, "return the average of groups", args(1,7, batarg("",dbl),batarg("b",dbl),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5826             :  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))),
    5827             :  pattern("batsql", "avg", SQLavginteger, false, "return the average of groups", args(1,7, batarg("",bte),batarg("b",bte),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5828             :  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))),
    5829             :  pattern("batsql", "avg", SQLavginteger, false, "return the average of groups", args(1,7, batarg("",sht),batarg("b",sht),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5830             :  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))),
    5831             :  pattern("batsql", "avg", SQLavginteger, false, "return the average of groups", args(1,7, batarg("",int),batarg("b",int),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5832             :  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))),
    5833             :  pattern("batsql", "avg", SQLavginteger, false, "return the average of groups", args(1,7, batarg("",lng),batarg("b",lng),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5834             :  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))),
    5835             :  pattern("batsql", "stdev", SQLstddev_samp, false, "return the standard deviation sample of groups", args(1,7, batarg("",dbl),batarg("b",bte),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5836             :  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))),
    5837             :  pattern("batsql", "stdev", SQLstddev_samp, false, "return the standard deviation sample of groups", args(1,7, batarg("",dbl),batarg("b",sht),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5838             :  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))),
    5839             :  pattern("batsql", "stdev", SQLstddev_samp, false, "return the standard deviation sample of groups", args(1,7, batarg("",dbl),batarg("b",int),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5840             :  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))),
    5841             :  pattern("batsql", "stdev", SQLstddev_samp, false, "return the standard deviation sample of groups", args(1,7, batarg("",dbl),batarg("b",lng),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5842             :  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))),
    5843             :  pattern("batsql", "stdev", SQLstddev_samp, false, "return the standard deviation sample of groups", args(1,7, batarg("",dbl),batarg("b",flt),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5844             :  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))),
    5845             :  pattern("batsql", "stdev", SQLstddev_samp, false, "return the standard deviation sample of groups", args(1,7, batarg("",dbl),batarg("b",dbl),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5846             :  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))),
    5847             :  pattern("batsql", "stdevp", SQLstddev_pop, false, "return the standard deviation population of groups", args(1,7, batarg("",dbl),batarg("b",bte),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5848             :  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))),
    5849             :  pattern("batsql", "stdevp", SQLstddev_pop, false, "return the standard deviation population of groups", args(1,7, batarg("",dbl),batarg("b",sht),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5850             :  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))),
    5851             :  pattern("batsql", "stdevp", SQLstddev_pop, false, "return the standard deviation population of groups", args(1,7, batarg("",dbl),batarg("b",int),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5852             :  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))),
    5853             :  pattern("batsql", "stdevp", SQLstddev_pop, false, "return the standard deviation population of groups", args(1,7, batarg("",dbl),batarg("b",lng),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5854             :  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))),
    5855             :  pattern("batsql", "stdevp", SQLstddev_pop, false, "return the standard deviation population of groups", args(1,7, batarg("",dbl),batarg("b",flt),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5856             :  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))),
    5857             :  pattern("batsql", "stdevp", SQLstddev_pop, false, "return the standard deviation population of groups", args(1,7, batarg("",dbl),batarg("b",dbl),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5858             :  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))),
    5859             :  pattern("batsql", "variance", SQLvar_samp, false, "return the variance sample of groups", args(1,7, batarg("",dbl),batarg("b",bte),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5860             :  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))),
    5861             :  pattern("batsql", "variance", SQLvar_samp, false, "return the variance sample of groups", args(1,7, batarg("",dbl),batarg("b",sht),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5862             :  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))),
    5863             :  pattern("batsql", "variance", SQLvar_samp, false, "return the variance sample of groups", args(1,7, batarg("",dbl),batarg("b",int),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5864             :  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))),
    5865             :  pattern("batsql", "variance", SQLvar_samp, false, "return the variance sample of groups", args(1,7, batarg("",dbl),batarg("b",lng),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5866             :  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))),
    5867             :  pattern("batsql", "variance", SQLvar_samp, false, "return the variance sample of groups", args(1,7, batarg("",dbl),batarg("b",flt),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5868             :  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))),
    5869             :  pattern("batsql", "variance", SQLvar_samp, false, "return the variance sample of groups", args(1,7, batarg("",dbl),batarg("b",dbl),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5870             :  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))),
    5871             :  pattern("batsql", "variancep", SQLvar_pop, false, "return the variance population of groups", args(1,7, batarg("",dbl),batarg("b",bte),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5872             :  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))),
    5873             :  pattern("batsql", "variancep", SQLvar_pop, false, "return the variance population of groups", args(1,7, batarg("",dbl),batarg("b",sht),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5874             :  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))),
    5875             :  pattern("batsql", "variancep", SQLvar_pop, false, "return the variance population of groups", args(1,7, batarg("",dbl),batarg("b",int),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5876             :  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))),
    5877             :  pattern("batsql", "variancep", SQLvar_pop, false, "return the variance population of groups", args(1,7, batarg("",dbl),batarg("b",lng),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5878             :  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))),
    5879             :  pattern("batsql", "variancep", SQLvar_pop, false, "return the variance population of groups", args(1,7, batarg("",dbl),batarg("b",flt),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5880             :  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))),
    5881             :  pattern("batsql", "variancep", SQLvar_pop, false, "return the variance population of groups", args(1,7, batarg("",dbl),batarg("b",dbl),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5882             :  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))),
    5883             :  pattern("batsql", "covariance", SQLcovar_samp, false, "return the covariance sample value of groups", args(1,8, batarg("",dbl),optbatarg("b",bte),optbatarg("c",bte),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5884             :  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))),
    5885             :  pattern("batsql", "covariance", SQLcovar_samp, false, "return the covariance sample value of groups", args(1,8, batarg("",dbl),optbatarg("b",sht),optbatarg("c",sht),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5886             :  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))),
    5887             :  pattern("batsql", "covariance", SQLcovar_samp, false, "return the covariance sample value of groups", args(1,8, batarg("",dbl),optbatarg("b",int),optbatarg("c",int),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5888             :  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))),
    5889             :  pattern("batsql", "covariance", SQLcovar_samp, false, "return the covariance sample value of groups", args(1,8, batarg("",dbl),optbatarg("b",lng),optbatarg("c",lng),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5890             :  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))),
    5891             :  pattern("batsql", "covariance", SQLcovar_samp, false, "return the covariance sample value of groups", args(1,8, batarg("",dbl),optbatarg("b",flt),optbatarg("c",flt),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5892             :  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))),
    5893             :  pattern("batsql", "covariance", SQLcovar_samp, false, "return the covariance sample value of groups", args(1,8, batarg("",dbl),optbatarg("b",dbl),optbatarg("c",dbl),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5894             :  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))),
    5895             :  pattern("batsql", "covariancep", SQLcovar_pop, false, "return the covariance population value of groups", args(1,8, batarg("",dbl),optbatarg("b",bte),optbatarg("c",bte),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5896             :  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))),
    5897             :  pattern("batsql", "covariancep", SQLcovar_pop, false, "return the covariance population value of groups", args(1,8, batarg("",dbl),optbatarg("b",sht),optbatarg("c",sht),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5898             :  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))),
    5899             :  pattern("batsql", "covariancep", SQLcovar_pop, false, "return the covariance population value of groups", args(1,8, batarg("",dbl),optbatarg("b",int),optbatarg("c",int),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5900             :  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))),
    5901             :  pattern("batsql", "covariancep", SQLcovar_pop, false, "return the covariance population value of groups", args(1,8, batarg("",dbl),optbatarg("b",lng),optbatarg("c",lng),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5902             :  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))),
    5903             :  pattern("batsql", "covariancep", SQLcovar_pop, false, "return the covariance population value of groups", args(1,8, batarg("",dbl),optbatarg("b",flt),optbatarg("c",flt),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5904             :  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))),
    5905             :  pattern("batsql", "covariancep", SQLcovar_pop, false, "return the covariance population value of groups", args(1,8, batarg("",dbl),optbatarg("b",dbl),optbatarg("c",dbl),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5906             :  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))),
    5907             :  pattern("batsql", "corr", SQLcorr, false, "return the correlation value of groups", args(1,8, batarg("",dbl),optbatarg("b",bte),optbatarg("c",bte),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5908             :  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))),
    5909             :  pattern("batsql", "corr", SQLcorr, false, "return the correlation value of groups", args(1,8, batarg("",dbl),optbatarg("b",sht),optbatarg("c",sht),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5910             :  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))),
    5911             :  pattern("batsql", "corr", SQLcorr, false, "return the correlation value of groups", args(1,8, batarg("",dbl),optbatarg("b",int),optbatarg("c",int),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5912             :  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))),
    5913             :  pattern("batsql", "corr", SQLcorr, false, "return the correlation value of groups", args(1,8, batarg("",dbl),optbatarg("b",lng),optbatarg("c",lng),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5914             :  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))),
    5915             :  pattern("batsql", "corr", SQLcorr, false, "return the correlation value of groups", args(1,8, batarg("",dbl),optbatarg("b",flt),optbatarg("c",flt),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5916             :  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))),
    5917             :  pattern("batsql", "corr", SQLcorr, false, "return the correlation value of groups", args(1,8, batarg("",dbl),optbatarg("b",dbl),optbatarg("c",dbl),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5918             :  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))),
    5919             :  pattern("batsql", "str_group_concat", SQLstrgroup_concat, false, "return the string concatenation of groups", args(1,7, batarg("",str),batarg("b",str),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5920             :  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))),
    5921             :  pattern("batsql", "str_group_concat", SQLstrgroup_concat, false, "return the string concatenation of groups with a custom separator", args(1,8, batarg("",str),optbatarg("b",str),optbatarg("sep",str),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    5922             :  /* sql_subquery */
    5923             :  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))),
    5924             :  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))),
    5925             :  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))),
    5926             :  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))),
    5927             :  command("aggr", "all", SQLall, false, "if all values in b are equal return this, else nil", args(1,2, argany("",1),batargany("b",1))),
    5928             :  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))),
    5929             :  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))),
    5930             :  command("aggr", "null", SQLnil, false, "if b has a nil return true, else false", args(1,2, arg("",bit),batargany("b",1))),
    5931             :  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))),
    5932             :  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))),
    5933             :  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))),
    5934             :  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))),
    5935             :  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))),
    5936             :  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))),
    5937             :  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))),
    5938             :  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))),
    5939             :  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))),
    5940             :  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))),
    5941             :  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))),
    5942             :  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))),
    5943             :  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))),
    5944             :  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))),
    5945             :  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))),
    5946             :  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))),
    5947             :  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))),
    5948             :  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))),
    5949             :  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))),
    5950             :  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))),
    5951             :  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))),
    5952             :  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))),
    5953             :  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))),
    5954             : // 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))),
    5955             :  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))),
    5956             :  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))),
    5957             :  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))),
    5958             : // 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))),
    5959             :  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))),
    5960             :  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))),
    5961             :  pattern("aggr", "exist", SQLexist, false, "", args(1,2, arg("",bit), argany("b",1))),
    5962             :  pattern("bataggr", "exist", SQLexist, false, "", args(1,2, batarg("",bit), argany("b",1))),
    5963             :  pattern("bataggr", "exist", SQLexist, false, "", args(1,2, arg("",bit), batargany("b",1))),
    5964             :  pattern("bataggr", "exist", SQLexist, false, "", args(1,2, batarg("",bit), batargany("b",1))),
    5965             :  pattern("aggr", "subexist", SQLsubexist, false, "", args(1,5, batarg("",bit),batargany("b",0),batarg("g",oid),batarg("e",oid),arg("no_nil",bit))),
    5966             :  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))),
    5967             :  pattern("aggr", "not_exist", SQLnot_exist, false, "", args(1,2, arg("",bit), argany("b",1))),
    5968             :  pattern("bataggr", "not_exist", SQLnot_exist, false, "", args(1,2, batarg("",bit), argany("b",1))),
    5969             :  pattern("bataggr", "not_exist", SQLnot_exist, false, "", args(1,2, arg("",bit), batargany("b",1))),
    5970             :  pattern("bataggr", "not_exist", SQLnot_exist, false, "", args(1,2, batarg("",bit), batargany("b",1))),
    5971             :  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))),
    5972             :  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))),
    5973             :  /* sqlcatalog */
    5974             :  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))),
    5975             :  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))),
    5976             :  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))),
    5977             :  pattern("sqlcatalog", "drop_seq", SQLdrop_seq, false, "Catalog operation drop_seq", args(0,3, arg("sname",str),arg("nme",str),arg("action",int))),
    5978             :  pattern("sqlcatalog", "create_schema", SQLcreate_schema, false, "Catalog operation create_schema", args(0,3, arg("sname",str),arg("auth",str),arg("action",int))),
    5979             :  pattern("sqlcatalog", "drop_schema", SQLdrop_schema, false, "Catalog operation drop_schema", args(0,3, arg("sname",str),arg("ifexists",int),arg("action",int))),
    5980             :  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))),
    5981             :  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))),
    5982             :  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))),
    5983             :  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))),
    5984             :  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))),
    5985             :  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))),
    5986             :  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))),
    5987             :  pattern("sqlcatalog", "create_type", SQLcreate_type, false, "Catalog operation create_type", args(0,3, arg("sname",str),arg("nme",str),arg("impl",str))),
    5988             :  pattern("sqlcatalog", "drop_type", SQLdrop_type, false, "Catalog operation drop_type", args(0,3, arg("sname",str),arg("nme",str),arg("action",int))),
    5989             :  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))),
    5990             :  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))),
    5991             :  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))),
    5992             :  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))),
    5993             :  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))),
    5994             :  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))),
    5995             :  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))),
    5996             :  pattern("sqlcatalog", "drop_user", SQLdrop_user, false, "Catalog operation drop_user", args(0,2, arg("sname",str),arg("action",int))),
    5997             :  pattern("sqlcatalog", "drop_user", SQLdrop_user, false, "Catalog operation drop_user", args(0,3, arg("sname",str),arg("auth",str),arg("action",int))),
    5998             :  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))),
    5999             :  pattern("sqlcatalog", "rename_user", SQLrename_user, false, "Catalog operation rename_user", args(0,3, arg("sname",str),arg("newnme",str),arg("action",int))),
    6000             :  pattern("sqlcatalog", "create_role", SQLcreate_role, false, "Catalog operation create_role", args(0,3, arg("sname",str),arg("role",str),arg("grator",int))),
    6001             :  pattern("sqlcatalog", "drop_role", SQLdrop_role, false, "Catalog operation drop_role", args(0,3, arg("auth",str),arg("role",str),arg("action",int))),
    6002             :  pattern("sqlcatalog", "drop_role", SQLdrop_role, false, "Catalog operation drop_role", args(0,2, arg("role",str),arg("action",int))),
    6003             :  pattern("sqlcatalog", "drop_index", SQLdrop_index, false, "Catalog operation drop_index", args(0,3, arg("sname",str),arg("iname",str),arg("action",int))),
    6004             :  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))),
    6005             :  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))),
    6006             :  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))),
    6007             :  pattern("sqlcatalog", "drop_trigger", SQLdrop_trigger, false, "Catalog operation drop_trigger", args(0,3, arg("sname",str),arg("nme",str),arg("ifexists",int))),
    6008             :  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))),
    6009             :  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))),
    6010             :  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))),
    6011             :  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))),
    6012             :  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))),
    6013             :  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))),
    6014             :  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))),
    6015             :  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))),
    6016             :  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))),
    6017             :  pattern("sqlcatalog", "comment_on", SQLcomment_on, false, "Catalog operation comment_on", args(0,2, arg("objid",int),arg("remark",str))),
    6018             :  pattern("sqlcatalog", "rename_schema", SQLrename_schema, false, "Catalog operation rename_schema", args(0,2, arg("sname",str),arg("newnme",str))),
    6019             :  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))),
    6020             :  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))),
    6021             :  /* sql_transaction */
    6022             :  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))),
    6023             :  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))),
    6024             :  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))),
    6025             :  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))),
    6026             : #ifdef HAVE_HGE
    6027             :  /* sql_hge */
    6028             :  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))),
    6029             :  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))),
    6030             :  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))),
    6031             :  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))),
    6032             :  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))),
    6033             :  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))),
    6034             :  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))),
    6035             :  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))),
    6036             :  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))),
    6037             :  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))),
    6038             :  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))),
    6039             :  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))),
    6040             :  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))),
    6041             :  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))),
    6042             :  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))),
    6043             :  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))),
    6044             :  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))),
    6045             :  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))),
    6046             :  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))),
    6047             :  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))),
    6048             :  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))),
    6049             :  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))),
    6050             :  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))),
    6051             :  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))),
    6052             :  /* sql_decimal_hge */
    6053             :  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))),
    6054             :  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))),
    6055             :  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))),
    6056             :  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))),
    6057             :  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))),
    6058             :  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))),
    6059             :  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))),
    6060             :  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))),
    6061             :  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))),
    6062             :  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))),
    6063             :  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))),
    6064             :  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))),
    6065             :  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))),
    6066             :  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))),
    6067             :  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))),
    6068             :  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))),
    6069             :  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))),
    6070             :  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))),
    6071             :  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))),
    6072             :  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))),
    6073             :  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))),
    6074             :  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))),
    6075             :  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))),
    6076             :  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))),
    6077             :  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))),
    6078             :  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))),
    6079             :  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))),
    6080             :  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))),
    6081             :  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))),
    6082             :  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))),
    6083             :  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))),
    6084             :  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))),
    6085             :  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))),
    6086             :  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))),
    6087             :  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))),
    6088             :  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))),
    6089             :  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))),
    6090             :  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))),
    6091             :  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))),
    6092             :  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))),
    6093             :  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))),
    6094             :  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))),
    6095             :  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))),
    6096             :  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))),
    6097             :  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))),
    6098             :  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))),
    6099             :  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))),
    6100             :  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))),
    6101             :  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))),
    6102             :  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))),
    6103             :  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))),
    6104             :  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))),
    6105             :  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))),
    6106             :  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))),
    6107             :  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))),
    6108             :  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))),
    6109             :  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))),
    6110             :  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))),
    6111             :  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))),
    6112             :  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))),
    6113             :  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))),
    6114             :  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))),
    6115             :  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))),
    6116             :  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))),
    6117             :  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))),
    6118             :  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))),
    6119             :  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))),
    6120             :  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))),
    6121             :  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))),
    6122             :  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))),
    6123             :  /* sql_rank_hge */
    6124             :  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",hge))),
    6125             :  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),optbatarg("limit",hge))),
    6126             :  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",hge))),
    6127             :  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),optbatarg("limit",hge))),
    6128             :  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))),
    6129             :  pattern("batsql", "sum", SQLsum, false, "return the sum of groups", args(1,7, batarg("",hge),batarg("b",bte),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    6130             :  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))),
    6131             :  pattern("batsql", "sum", SQLsum, false, "return the sum of groups", args(1,7, batarg("",hge),batarg("b",sht),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    6132             :  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))),
    6133             :  pattern("batsql", "sum", SQLsum, false, "return the sum of groups", args(1,7, batarg("",hge),batarg("b",int),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    6134             :  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))),
    6135             :  pattern("batsql", "sum", SQLsum, false, "return the sum of groups", args(1,7, batarg("",hge),batarg("b",lng),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    6136             :  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))),
    6137             :  pattern("batsql", "sum", SQLsum, false, "return the sum of groups", args(1,7, batarg("",hge),batarg("b",hge),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    6138             :  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))),
    6139             :  pattern("batsql", "prod", SQLprod, false, "return the product of groups", args(1,7, batarg("",hge),batarg("b",bte),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    6140             :  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))),
    6141             :  pattern("batsql", "prod", SQLprod, false, "return the product of groups", args(1,7, batarg("",hge),batarg("b",sht),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    6142             :  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))),
    6143             :  pattern("batsql", "prod", SQLprod, false, "return the product of groups", args(1,7, batarg("",hge),batarg("b",int),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    6144             :  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))),
    6145             :  pattern("batsql", "prod", SQLprod, false, "return the product of groups", args(1,7, batarg("",hge),batarg("b",lng),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    6146             :  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))),
    6147             :  pattern("batsql", "prod", SQLprod, false, "return the product of groups", args(1,7, batarg("",hge),batarg("b",hge),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    6148             :  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))),
    6149             :  pattern("batsql", "avg", SQLavg, false, "return the average of groups", args(1,7, batarg("",dbl),batarg("b",hge),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    6150             :  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))),
    6151             :  pattern("batsql", "avg", SQLavginteger, false, "return the average of groups", args(1,7, batarg("",hge),batarg("b",hge),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    6152             :  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))),
    6153             :  pattern("batsql", "stdev", SQLstddev_samp, false, "return the standard deviation sample of groups", args(1,7, batarg("",dbl),batarg("b",hge),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    6154             :  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))),
    6155             :  pattern("batsql", "stdevp", SQLstddev_pop, false, "return the standard deviation population of groups", args(1,7, batarg("",dbl),batarg("b",hge),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    6156             :  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))),
    6157             :  pattern("batsql", "variance", SQLvar_samp, false, "return the variance sample of groups", args(1,7, batarg("",dbl),batarg("b",hge),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    6158             :  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))),
    6159             :  pattern("batsql", "variancep", SQLvar_pop, false, "return the variance population of groups", args(1,7, batarg("",dbl),batarg("b",hge),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    6160             :  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))),
    6161             :  pattern("batsql", "covariance", SQLcovar_samp, false, "return the covariance sample value of groups", args(1,8, batarg("",dbl),optbatarg("b",hge),optbatarg("c",hge),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    6162             :  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))),
    6163             :  pattern("batsql", "covariancep", SQLcovar_pop, false, "return the covariance population value of groups", args(1,8, batarg("",dbl),optbatarg("b",hge),optbatarg("c",hge),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    6164             :  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))),
    6165             :  pattern("batsql", "corr", SQLcorr, false, "return the correlation value of groups", args(1,8, batarg("",dbl),optbatarg("b",hge),optbatarg("c",hge),optbatarg("p",bit),optbatarg("o",bit),arg("t",int),optbatarg("s",oid),optbatarg("e",oid))),
    6166             : #endif
    6167             :  pattern("sql", "vacuum", SQLstr_column_vacuum, true, "vacuum a string column", args(0,3, arg("sname",str),arg("tname",str),arg("cname",str))),
    6168             :  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))),
    6169             :  pattern("sql", "stop_vacuum", SQLstr_column_stop_vacuum, true, "stop auto vacuum", args(0,3, arg("sname",str),arg("tname",str),arg("cname",str))),
    6170             :  { .imp=NULL }
    6171             : };
    6172             : #include "mal_import.h"
    6173             : #ifdef _MSC_VER
    6174             : #undef read
    6175             : #pragma section(".CRT$XCU",read)
    6176             : #endif
    6177         331 : LIB_STARTUP_FUNC(init_sql_mal)
    6178         331 : { mal_module("sql", NULL, sql_init_funcs); }

Generated by: LCOV version 1.14