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

Generated by: LCOV version 1.14