LCOV - code coverage report
Current view: top level - sql/backends/monet5 - sql.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 2429 3888 62.5 %
Date: 2024-11-11 20:03:36 Functions: 78 106 73.6 %

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

Generated by: LCOV version 1.14