LCOV - code coverage report
Current view: top level - sql/server - rel_dump.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 1255 1811 69.3 %
Date: 2024-10-04 20:04:04 Functions: 31 36 86.1 %

          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             : #include "monetdb_config.h"
      14             : #define LINESIZE 160
      15             : #define TABSTOP 2
      16             : 
      17             : #include "rel_dump.h"
      18             : #include "rel_rel.h"
      19             : #include "rel_basetable.h"
      20             : #include "rel_exp.h"
      21             : #include "rel_updates.h"
      22             : #include "rel_select.h"
      23             : #include "rel_remote.h"
      24             : #include "rel_rewriter.h"
      25             : #include "rel_optimizer.h"
      26             : #include "sql_privileges.h"
      27             : 
      28             : static void
      29        3747 : print_indent(mvc *sql, stream *fout, int depth, int decorate)
      30             : {
      31        3747 :         char buf[LINESIZE+1];
      32        3747 :         int i;
      33             : 
      34        3747 :         (void)sql;
      35        3747 :         if (!decorate) {
      36         987 :                 mnstr_printf(fout, "\n");
      37         987 :                 return ;
      38             :         }
      39        2760 :         depth *= TABSTOP;
      40        2760 :         if (depth > LINESIZE)
      41             :                 depth = LINESIZE;
      42       21646 :         for (i = 0; i < depth; i++){
      43       18886 :                 if ((i % TABSTOP) == 0)
      44        9443 :                         buf[i] = '|';
      45             :                 else
      46        9443 :                         buf[i] = ' ';
      47             :         }
      48        2760 :         buf[i] = 0;
      49        2760 :         mnstr_printf(fout, "\n=%s", buf);
      50             : }
      51             : 
      52             : static void
      53         668 : cmp_print(mvc *sql, stream *fout, int cmp)
      54             : {
      55         668 :         char *r = NULL;
      56             : 
      57         668 :         (void)sql;
      58         668 :         switch(cmp) {
      59          28 :         case cmp_gt:            r = ">"; break;
      60          10 :         case cmp_gte:           r = ">="; break;
      61         107 :         case cmp_lte:           r = "<="; break;
      62          57 :         case cmp_lt:            r = "<"; break;
      63         355 :         case cmp_equal:         r = "="; break;
      64           6 :         case cmp_notequal:      r = "!="; break;
      65             : 
      66           0 :         case cmp_filter:        r = "filter"; break;
      67          51 :         case cmp_or:            r = "or"; break;
      68          46 :         case cmp_in:            r = "in"; break;
      69           8 :         case cmp_notin:         r = "notin"; break;
      70             : 
      71           0 :         case cmp_all:
      72             :         case cmp_project:
      73             :         case cmp_joined:
      74             :         case cmp_left_project:
      75           0 :                                 r = "inner"; break;
      76             :         }
      77         668 :         mnstr_printf(fout, " %s ", r);
      78         668 : }
      79             : 
      80             : static const char *
      81       18417 : dump_escape_ident(allocator *sa, const char *s)
      82             : {
      83       18417 :         char *res = NULL;
      84       18417 :         if (s) {
      85       18417 :                 size_t l = strlen(s);
      86       18417 :                 char *r = SA_NEW_ARRAY(sa, char, (l * 2) + 1);
      87             : 
      88       18417 :                 res = r;
      89       96694 :                 while (*s) {
      90       78277 :                         if (*s == '"' || *s == '\\')
      91          12 :                                 *r++ = '\\';
      92       78277 :                         *r++ = *s++;
      93             :                 }
      94       18417 :                 *r = '\0';
      95             :         }
      96       18417 :         return res;
      97             : }
      98             : 
      99             : static char *
     100        1321 : dump_sql_subtype(allocator *sa, sql_subtype *t)
     101             : {
     102        1321 :         char buf[BUFSIZ];
     103             : 
     104        1321 :         if (t->digits && t->scale)
     105          29 :                 snprintf(buf, BUFSIZ, "%s(%u,%u)", t->type->base.name, t->digits, t->scale);
     106        1292 :         else if (t->digits)
     107        1141 :                 snprintf(buf, BUFSIZ, "%s(%u)", t->type->base.name, t->digits);
     108             :         else
     109         151 :                 snprintf(buf, BUFSIZ, "%s", t->type->base.name);
     110        1321 :         return sa_strdup(sa, buf);
     111             : }
     112             : 
     113             : static void exps_print(mvc *sql, stream *fout, list *exps, int depth, list *refs, int alias, int brackets, int decorate, int expbrk);
     114             : 
     115             : static void rel_print_rel(mvc *sql, stream  *fout, sql_rel *rel, int depth, list *refs, int decorate);
     116             : 
     117             : void
     118        8592 : exp_print(mvc *sql, stream *fout, sql_exp *e, int depth, list *refs, int comma, int alias, int decorate)
     119             : {
     120        8592 :         (void)sql;
     121        8592 :         if (!e)
     122             :                 return;
     123             :         /*mnstr_printf(fout, "%p ", e);*/
     124        8592 :         if (mvc_debug_on(sql, 4) && e->alias.label < 0)
     125           0 :                 mnstr_printf(fout, "%d: ", e->alias.label);
     126        8592 :         switch(e->type) {
     127           0 :         case e_psm: {
     128           0 :                 if (e->flag & PSM_SET) {
     129           0 :                         const char *rname = exp_relname(e);
     130           0 :                         int level = GET_PSM_LEVEL(e->flag);
     131           0 :                         if (rname)
     132           0 :                                 mnstr_printf(fout, "\"%s\".", dump_escape_ident(sql->ta, rname));
     133           0 :                         mnstr_printf(fout, "\"%s\" = ",  dump_escape_ident(sql->ta, exp_name(e)));
     134           0 :                         exp_print(sql, fout, e->l, depth, refs, 0, 0, decorate);
     135           0 :                         mnstr_printf(fout, " FRAME %d ", level);
     136           0 :                         alias = 0;
     137           0 :                 } else if (e->flag & PSM_VAR) {
     138             :                         // todo output table def (from e->f)
     139           0 :                         const char *rname = exp_relname(e);
     140           0 :                         char *type_str = e->f ? NULL : dump_sql_subtype(sql->ta, exp_subtype(e));
     141           0 :                         int level = GET_PSM_LEVEL(e->flag);
     142           0 :                         mnstr_printf(fout, "declare ");
     143           0 :                         if (rname)
     144           0 :                                 mnstr_printf(fout, "\"%s\".", dump_escape_ident(sql->ta, rname));
     145           0 :                         mnstr_printf(fout, "\"%s\" %s FRAME %d ", dump_escape_ident(sql->ta, exp_name(e)), type_str ? type_str : "", level);
     146           0 :                         alias = 0;
     147           0 :                 } else if (e->flag & PSM_RETURN) {
     148           0 :                         int level = GET_PSM_LEVEL(e->flag);
     149           0 :                         mnstr_printf(fout, "return ");
     150           0 :                         exp_print(sql, fout, e->l, depth, refs, 0, 0, decorate);
     151           0 :                         mnstr_printf(fout, " FRAME %d ", level);
     152           0 :                         alias = 0;
     153           0 :                 } else if (e->flag & PSM_WHILE) {
     154           0 :                         mnstr_printf(fout, "while ");
     155           0 :                         exp_print(sql, fout, e->l, depth, refs, 0, 0, decorate);
     156           0 :                         exps_print(sql, fout, e->r, depth, refs, 0, 0, decorate, 0);
     157           0 :                         alias = 0;
     158           0 :                 } else if (e->flag & PSM_IF) {
     159           0 :                         mnstr_printf(fout, "if ");
     160           0 :                         exp_print(sql, fout, e->l, depth, refs, 0, 0, decorate);
     161           0 :                         exps_print(sql, fout, e->r, depth, refs, 0, 0, decorate, 0);
     162           0 :                         if (e->f)
     163           0 :                                 exps_print(sql, fout, e->f, depth, refs, 0, 0, decorate, 0);
     164             :                         alias = 0;
     165           0 :                 } else if (e->flag & PSM_REL) {
     166           0 :                         rel_print_rel(sql, fout, e->l, depth+10, refs, 1);
     167           0 :                 } else if (e->flag & PSM_EXCEPTION) {
     168           0 :                         mnstr_printf(fout, "except ");
     169           0 :                         exp_print(sql, fout, e->l, depth, refs, 0, 0, decorate);
     170           0 :                         mnstr_printf(fout, " error %s", (const char *) e->r);
     171           0 :                         alias = 0;
     172             :                 }
     173             :                 break;
     174             :         }
     175          86 :         case e_convert: {
     176          86 :                 char *to_type = dump_sql_subtype(sql->ta, exp_subtype(e));
     177          86 :                 mnstr_printf(fout, "%s[", to_type);
     178          86 :                 exp_print(sql, fout, e->l, depth, refs, 0, 0, decorate);
     179          86 :                 mnstr_printf(fout, "]");
     180          86 :                 break;
     181             :         }
     182        1306 :         case e_atom: {
     183        1306 :                 if (e->l) {
     184        1237 :                         atom *a = e->l;
     185        1237 :                         if (atom_type(a)->type->localtype == TYPE_ptr) {
     186           2 :                                 sql_table *t = a->data.val.pval;
     187           2 :                                 mnstr_printf(fout, "%s(\"%s\")",
     188           2 :                                         isMergeTable(t)?"merge table":
     189           2 :                                         isReplicaTable(t)?"replica table":"table",
     190           2 :                                         dump_escape_ident(sql->ta, t->base.name));
     191             :                         } else {
     192        1235 :                                 char *t = dump_sql_subtype(sql->ta, atom_type(a));
     193        1235 :                                 if (a->isnull)
     194          56 :                                         mnstr_printf(fout, "%s NULL", t);
     195             :                                 else {
     196        1179 :                                         char *s = ATOMformat(a->data.vtype, VALptr(&a->data));
     197        1179 :                                         if (s && *s == '"')
     198         137 :                                                 mnstr_printf(fout, "%s %s", t, s);
     199        1042 :                                         else if (s)
     200        1042 :                                                 mnstr_printf(fout, "%s \"%s\"", t, s);
     201        1179 :                                         GDKfree(s);
     202             :                                 }
     203             :                         }
     204             :                 } else { /* variables */
     205          69 :                         if (e->r) { /* named parameters and declared variables */
     206          22 :                                 sql_var_name *vname = (sql_var_name*) e->r;
     207          22 :                                 if (vname->sname)
     208           3 :                                         mnstr_printf(fout, "\"%s\".", dump_escape_ident(sql->ta, vname->sname));
     209          22 :                                 mnstr_printf(fout, "\"%s\"", dump_escape_ident(sql->ta, vname->name));
     210          47 :                         } else if (e->f) {   /* values list */
     211          47 :                                 list *l = e->f;
     212          47 :                                 exps_print(sql, fout, l, depth, refs, 0, 0, decorate, 0);
     213             :                         } else { /* numbered arguments */
     214           0 :                                 mnstr_printf(fout, "A%u", e->flag);
     215             :                         }
     216             :                 }
     217             :         }       break;
     218         358 :         case e_func: {
     219         358 :                 sql_subfunc *f = e->f;
     220         358 :                 mnstr_printf(fout, "\"%s\".\"%s\"",
     221         358 :                                 f->func->s?dump_escape_ident(sql->ta, f->func->s->base.name):"sys",
     222         358 :                                 dump_escape_ident(sql->ta, f->func->base.name));
     223         358 :                 exps_print(sql, fout, e->l, depth, refs, 0, 1, decorate, 0);
     224         358 :                 if (e->r) { /* list of optional lists */
     225           0 :                         list *l = e->r;
     226           0 :                         for(node *n = l->h; n; n = n->next)
     227           0 :                                 exps_print(sql, fout, n->data, depth, refs, 0, 1, decorate, 0);
     228             :                 }
     229         358 :                 if (e->flag && is_compare_func(f))
     230           0 :                         mnstr_printf(fout, " %s", e->flag==1?"ANY":"ALL");
     231             :         } break;
     232          90 :         case e_aggr: {
     233          90 :                 sql_subfunc *a = e->f;
     234          90 :                 mnstr_printf(fout, "\"%s\".\"%s\"",
     235          90 :                                 a->func->s?dump_escape_ident(sql->ta, a->func->s->base.name):"sys",
     236          90 :                                 dump_escape_ident(sql->ta, a->func->base.name));
     237          90 :                 if (need_distinct(e))
     238           4 :                         mnstr_printf(fout, " unique ");
     239          90 :                 if (need_no_nil(e))
     240          29 :                         mnstr_printf(fout, " no nil ");
     241          90 :                 if (zero_if_empty(e))
     242           0 :                         mnstr_printf(fout, " zero if empty ");
     243          90 :                 if (e->l)
     244          33 :                         exps_print(sql, fout, e->l, depth, refs, 0, 1, decorate, 0);
     245             :                 else
     246          57 :                         mnstr_printf(fout, "()");
     247             :         } break;
     248        6144 :         case e_column: {
     249        6144 :                 if (is_freevar(e))
     250           0 :                         mnstr_printf(fout, "!!!FREE!!! ");
     251        6144 :                 if (mvc_debug_on(sql, 4) && e->nid)
     252           0 :                         mnstr_printf(fout, "<%d", e->nid);
     253        6144 :                 if (e->l)
     254        6046 :                         mnstr_printf(fout, "\"%s\".", dump_escape_ident(sql->ta, (char*)e->l));
     255        6144 :                 mnstr_printf(fout, "\"%s\"", dump_escape_ident(sql->ta, (char*)e->r));
     256        6144 :                 if (exp_relname(e) && exp_name(e) && e->l && e->r &&
     257        5985 :                         strcmp(exp_relname(e), e->l) == 0 &&
     258        4290 :                         strcmp(exp_name(e), e->r) == 0)
     259        6144 :                         alias = 0;
     260        6144 :                 if(!exp_relname(e) && exp_name(e) && !e->l && strcmp(exp_name(e), e->r)==0)
     261          76 :                         alias = 0;
     262             :         } break;
     263         608 :         case e_cmp:
     264         608 :                 if (e->flag == cmp_in || e->flag == cmp_notin) {
     265          54 :                         mnstr_printf(fout, "(");
     266          54 :                         exp_print(sql, fout, e->l, depth+1, refs, 0, 0, decorate);
     267          54 :                         mnstr_printf(fout, ")");
     268          54 :                         if (is_anti(e))
     269           0 :                                 mnstr_printf(fout, " !");
     270          54 :                         cmp_print(sql, fout, e->flag);
     271          54 :                         exps_print(sql, fout, e->r, depth, refs, 0, 1, decorate, 0);
     272         554 :                 } else if (e->flag == cmp_or) {
     273          51 :                         exps_print(sql, fout, e->l, depth, refs, 0, 1, decorate, 0);
     274          51 :                         if (is_anti(e))
     275           0 :                                 mnstr_printf(fout, " !");
     276          51 :                         cmp_print(sql, fout, e->flag);
     277          51 :                         exps_print(sql, fout, e->r, depth, refs, 0, 1, decorate, 0);
     278         503 :                 } else if (e->flag == cmp_filter) {
     279          15 :                         sql_subfunc *f = e->f;
     280             : 
     281          15 :                         exps_print(sql, fout, e->l, depth, refs, 0, 1, decorate, 0);
     282          15 :                         if (is_anti(e))
     283           2 :                                 mnstr_printf(fout, " !");
     284          15 :                         mnstr_printf(fout, " FILTER \"%s\".\"%s\"",
     285          15 :                                         f->func->s?dump_escape_ident(sql->ta, f->func->s->base.name):"sys",
     286          15 :                                         dump_escape_ident(sql->ta, f->func->base.name));
     287          15 :                         exps_print(sql, fout, e->r, depth, refs, 0, 1, decorate, 0);
     288         488 :                 } else if (e->f) {
     289          75 :                         mnstr_printf(fout, "(");
     290          75 :                         exp_print(sql, fout, e->r, depth+1, refs, 0, 0, decorate);
     291          75 :                         mnstr_printf(fout, ")");
     292          75 :                         if (is_anti(e))
     293           6 :                                 mnstr_printf(fout, " !");
     294          75 :                         cmp_print(sql, fout, swap_compare(range2lcompare(e->flag)));
     295          75 :                         mnstr_printf(fout, "(");
     296          75 :                         exp_print(sql, fout, e->l, depth+1, refs, 0, 0, decorate);
     297          75 :                         mnstr_printf(fout, ")");
     298          75 :                         if (is_anti(e))
     299           6 :                                 mnstr_printf(fout, " !");
     300          75 :                         cmp_print(sql, fout, range2rcompare(e->flag));
     301          75 :                         mnstr_printf(fout, "(");
     302          75 :                         exp_print(sql, fout, e->f, depth+1, refs, 0, 0, decorate);
     303          75 :                         mnstr_printf(fout, ")");
     304          75 :                         if (is_symmetric(e))
     305           1 :                                 mnstr_printf(fout, " SYM");
     306             :                 } else {
     307         413 :                         mnstr_printf(fout, "(");
     308         413 :                         exp_print(sql, fout, e->l, depth+1, refs, 0, 0, decorate);
     309         413 :                         mnstr_printf(fout, ")");
     310         413 :                         if (is_anti(e))
     311           8 :                                 mnstr_printf(fout, " !");
     312         413 :                         if (is_semantics(e))
     313          20 :                                 mnstr_printf(fout, " *");
     314         413 :                         if (is_any(e))
     315           2 :                                 mnstr_printf(fout, " +");
     316         413 :                         cmp_print(sql, fout, e->flag);
     317             : 
     318         413 :                         mnstr_printf(fout, "(");
     319         413 :                         exp_print(sql, fout, e->r, depth+1, refs, 0, 0, decorate);
     320         413 :                         mnstr_printf(fout, ")");
     321             :                 }
     322             :                 break;
     323        8592 :         default:
     324        8592 :                 ;
     325             :         }
     326        8592 :         if (e->type != e_atom && e->type != e_cmp && is_ascending(e))
     327          90 :                 mnstr_printf(fout, " ASC");
     328        8592 :         if (e->type != e_atom && e->type != e_cmp && nulls_last(e))
     329          24 :                 mnstr_printf(fout, " NULLS LAST");
     330        8592 :         if (e->type != e_atom && e->type != e_cmp && !has_nil(e))
     331        3152 :                 mnstr_printf(fout, " NOT NULL");
     332        8592 :         if (e->type != e_atom && e->type != e_cmp && is_unique(e))
     333        1379 :                 mnstr_printf(fout, " UNIQUE");
     334             :         /* don't show properties on value lists */
     335        8592 :         if (decorate && e->p && e->type != e_atom && !exp_is_atom(e)) {
     336        9261 :                 for (prop *p = e->p; p; p = p->p) {
     337             :                         /* Don't show min/max/unique est on atoms, or when running tests with forcemito */
     338        6745 :                         if ((ATOMIC_GET(&GDKdebug) & FORCEMITOMASK) == 0 ||
     339        6745 :                                 (p->kind != PROP_MIN && p->kind != PROP_MAX && p->kind != PROP_NUNIQUES)) {
     340         478 :                                 char *pv = propvalue2string(sql->ta, p);
     341         478 :                                 mnstr_printf(fout, " %s %s", propkind2string(p), pv);
     342             :                         }
     343             :                 }
     344             :         }
     345        8592 :         if (exp_name(e) && alias) {
     346        2151 :                 mnstr_printf(fout, " as ");
     347        2151 :                 if (exp_relname(e))
     348        2074 :                         mnstr_printf(fout, "\"%s\".", dump_escape_ident(sql->ta, exp_relname(e)));
     349        2151 :                 mnstr_printf(fout, "\"%s\"", dump_escape_ident(sql->ta, exp_name(e)));
     350             :         }
     351             : 
     352        8592 :         if (e->comment) {
     353          32 :                 str s = ATOMformat(TYPE_str, e->comment);
     354          32 :                 mnstr_printf(fout,  " COMMENT %s ", s);
     355          32 :                 GDKfree(s);
     356             :         }
     357        8592 :         if (comma)
     358        4369 :                 mnstr_printf(fout, ", ");
     359             : }
     360             : 
     361             : 
     362             : str
     363         186 : rel2str( mvc *sql, sql_rel *rel)
     364             : {
     365         186 :         buffer *b = NULL;
     366         186 :         stream *s = NULL;
     367         186 :         list *refs = NULL;
     368         186 :         char *res = NULL;
     369             : 
     370         186 :         b = buffer_create(1024);
     371         186 :         if(b == NULL)
     372           0 :                 goto cleanup;
     373         186 :         s = buffer_wastream(b, "rel_dump");
     374         186 :         if(s == NULL)
     375           0 :                 goto cleanup;
     376         186 :         refs = sa_list(sql->sa);
     377         186 :         if (!refs)
     378           0 :                 goto cleanup;
     379             : 
     380         186 :         rel_print_refs(sql, s, rel, 0, refs, 0);
     381         186 :         rel_print_(sql, s, rel, 0, refs, 0);
     382         186 :         mnstr_printf(s, "\n");
     383         186 :         res = buffer_get_buf(b);
     384             : 
     385         186 : cleanup:
     386         186 :         if(b)
     387         186 :                 buffer_destroy(b);
     388         186 :         if(s)
     389         186 :                 close_stream(s);
     390             : 
     391         186 :         char* fres = SA_STRDUP(sql->sa, res);
     392         186 :         free (res);
     393         186 :         return fres;
     394             : }
     395             : 
     396             : str
     397          32 : exp2str( mvc *sql, sql_exp *exp)
     398             : {
     399          32 :         buffer *b = NULL;
     400          32 :         stream *s = NULL;
     401          32 :         char *res = NULL;
     402             : 
     403          32 :         b = buffer_create(1024);
     404          32 :         if(b == NULL)
     405           0 :                 goto cleanup;
     406          32 :         s = buffer_wastream(b, "exp_dump");
     407          32 :         if(s == NULL)
     408           0 :                 goto cleanup;
     409             : 
     410          32 :         exp_print(sql, s, exp, 0, NULL, 0, 0, 0);
     411          32 :         res = buffer_get_buf(b);
     412             : 
     413          32 : cleanup:
     414          32 :         if(b)
     415          32 :                 buffer_destroy(b);
     416          32 :         if(s)
     417          32 :                 close_stream(s);
     418             : 
     419          32 :         char* fres = SA_STRDUP(sql->sa, res);
     420          32 :         free (res);
     421          32 :         return fres;
     422             : }
     423             : 
     424             : static void
     425        3100 : exps_print(mvc *sql, stream *fout, list *exps, int depth, list *refs, int alias, int brackets, int decorate, int expbrk)
     426             : {
     427        3100 :         node *en;
     428             : 
     429        3100 :         if (brackets)
     430         577 :                 mnstr_printf(fout, "(");
     431             :         else
     432        2523 :                 mnstr_printf(fout, " [ ");
     433        3100 :         if (exps)
     434       10362 :                 for (en = exps->h; en; en = en->next){
     435        7362 :                         exp_print(sql, fout, en->data, depth+1, refs, (en->next!=NULL), alias, decorate);
     436             :                         /* break for each entry of the expression list */
     437        7362 :                         if (expbrk && en->next!=NULL)
     438           0 :                                 print_indent(sql, fout, depth+2, decorate);
     439             :                 }
     440        3100 :         int multi_exps = expbrk && (list_length(exps) > 1);
     441           0 :         if (multi_exps)
     442           0 :                 print_indent(sql, fout, depth+1, decorate);
     443        3100 :         if (brackets)
     444         577 :                 mnstr_printf(fout, ")");
     445             :         else
     446        5046 :                 mnstr_printf(fout, multi_exps?"]":" ]");
     447        3100 : }
     448             : 
     449             : static int
     450          14 : find_ref( list *refs, sql_rel *rel )
     451             : {
     452          14 :         node *n;
     453          14 :         int nr = 1;
     454             : 
     455          18 :         for(n=refs->h; n; n = n->next, nr++){
     456          15 :                 if (n->data == rel)
     457             :                         return nr;
     458             :         }
     459             :         return 0;
     460             : }
     461             : 
     462             : static void
     463        2279 : rel_print_rel(mvc *sql, stream  *fout, sql_rel *rel, int depth, list *refs, int decorate)
     464             : {
     465        2279 :         char *r = NULL;
     466             : 
     467        2279 :         if (!rel)
     468             :                 return;
     469             : 
     470        2279 :         if (rel_is_ref(rel)) {
     471           3 :                 int nr = list_length(refs) + 1;
     472           3 :                 int cnt = rel->ref.refcnt;
     473           3 :                 mnstr_printf(fout, "\n%cREF %d (%d)", decorate?'=':' ', nr, cnt);
     474             :         }
     475             : 
     476        2279 :         print_indent(sql, fout, depth, decorate);
     477             : 
     478        2279 :         if (is_single(rel))
     479           7 :                 mnstr_printf(fout, "single ");
     480             : 
     481        2279 :         switch (rel->op) {
     482         747 :         case op_basetable: {
     483         747 :                 sql_table *t = rel->l;
     484         747 :                 const char *sname = t->s ? t->s->base.name : NULL; /* All tables, but declared ones on the stack have schema */
     485         747 :                 const char *tname = t->base.name;
     486             : 
     487         747 :                 if (isRemote(t)) {
     488         215 :                         const char *uri = t->query;
     489             : 
     490         215 :                         sname = mapiuri_schema( uri, sql->sa, sname);
     491         215 :                         tname = mapiuri_table( uri, sql->sa, tname);
     492             :                 }
     493         747 :                 if (sname)
     494        1494 :                         mnstr_printf(fout, "%s(\"%s\".\"%s\")",
     495         747 :                                 isRemote(t)&&decorate?"REMOTE":
     496         728 :                                 isReplicaTable(t)?"REPLICA":"table",
     497             :                                 dump_escape_ident(sql->ta, sname), dump_escape_ident(sql->ta, tname));
     498             :                 else
     499           0 :                         mnstr_printf(fout, "%s(\"%s\")",
     500           0 :                                 isRemote(t)&&decorate?"REMOTE":
     501           0 :                                 isReplicaTable(t)?"REPLICA":"table",
     502             :                                 dump_escape_ident(sql->ta, tname));
     503         747 :                 if (rel->exps)
     504         747 :                         exps_print(sql, fout, rel->exps, depth, refs, 1, 0, decorate, 0);
     505             :                 else
     506           0 :                         rel_base_dump_exps(fout, rel);
     507             :         }       break;
     508          19 :         case op_table:
     509          19 :                 mnstr_printf(fout, "table (");
     510             : 
     511          19 :                 if (rel->r)
     512           7 :                         exp_print(sql, fout, rel->r, depth, refs, 1, 0, decorate);
     513          19 :                 if (rel->l) {
     514          17 :                         if (rel->flag == TRIGGER_WRAPPER)
     515           0 :                                 mnstr_printf(fout, "rel_dump not yet implemented for trigger input");
     516             :                         else
     517          17 :                                 rel_print_rel(sql, fout, rel->l, depth+1, refs, decorate);
     518             :                 }
     519          19 :                 print_indent(sql, fout, depth, decorate);
     520          19 :                 mnstr_printf(fout, ")");
     521          19 :                 if (rel->exps)
     522          19 :                         exps_print(sql, fout, rel->exps, depth, refs, 1, 0, decorate, 0);
     523             :                 break;
     524           2 :         case op_ddl:
     525           2 :                 mnstr_printf(fout, "ddl");
     526           2 :                 if (rel->l)
     527           0 :                         rel_print_rel(sql, fout, rel->l, depth+1, refs, decorate);
     528           2 :                 if (rel->r)
     529           0 :                         rel_print_rel(sql, fout, rel->r, depth+1, refs, decorate);
     530           2 :                 if (rel->exps && (rel->flag == ddl_psm || rel->flag == ddl_exception || rel->flag == ddl_list))
     531           0 :                         exps_print(sql, fout, rel->exps, depth, refs, 1, 0, decorate, 0);
     532             :                 break;
     533         252 :         case op_join:
     534             :         case op_left:
     535             :         case op_right:
     536             :         case op_full:
     537             :         case op_semi:
     538             :         case op_anti:
     539             :         case op_union:
     540             :         case op_inter:
     541             :         case op_except:
     542         252 :                 r = "join";
     543         252 :                 if (rel->op == op_left)
     544          45 :                         r = rel->attr?"left outer group join":"left outer join";
     545             :                 else if (rel->op == op_right)
     546             :                         r = "right outer join";
     547             :                 else if (rel->op == op_full)
     548             :                         r = "full outer join";
     549             :                 else if (rel->op == op_semi)
     550             :                         r = "semijoin";
     551             :                 else if (rel->op == op_anti)
     552             :                         r = "antijoin";
     553             :                 else if (rel->op == op_union)
     554             :                         r = "union";
     555             :                 else if (rel->op == op_inter)
     556             :                         r = "intersect";
     557             :                 else if (rel->op == op_except)
     558             :                         r = "except";
     559         166 :                 else if (rel->op == op_join) {
     560         166 :                         if (list_empty(rel->exps))
     561          26 :                                 r = rel->attr?"group crossproduct":"crossproduct";
     562             :                         else
     563         140 :                                 r = rel->attr?"group join":"join";
     564             :                 }
     565             : 
     566         252 :                 if (is_dependent(rel))
     567           0 :                         mnstr_printf(fout, "dependent ");
     568         252 :                 if (need_distinct(rel))
     569           8 :                         mnstr_printf(fout, "distinct ");
     570         252 :                 mnstr_printf(fout, "%s (", r);
     571         252 :                 if (rel->l) {
     572         252 :                         if (rel_is_ref(rel->l)) {
     573           3 :                                 int nr = find_ref(refs, rel->l);
     574           3 :                                 print_indent(sql, fout, depth+1, decorate);
     575           3 :                                 mnstr_printf(fout, "& REF %d ", nr);
     576             :                         } else
     577         249 :                                 rel_print_rel(sql, fout, rel->l, depth+1, refs, decorate);
     578             :                 }
     579         252 :                 mnstr_printf(fout, ",");
     580         252 :                 if (rel->r) {
     581         252 :                         if (rel_is_ref(rel->r)) {
     582           0 :                                 int nr = find_ref(refs, rel->r);
     583           0 :                                 print_indent(sql, fout, depth+1, decorate);
     584           0 :                                 mnstr_printf(fout, "& REF %d  ", nr);
     585             :                         } else
     586         252 :                                 rel_print_rel(sql, fout, rel->r, depth+1, refs, decorate);
     587             :                 }
     588         252 :                 print_indent(sql, fout, depth, decorate);
     589         252 :                 mnstr_printf(fout, ")");
     590         252 :                 exps_print(sql, fout, rel->exps, depth, refs, 1, 0, decorate, 0);
     591         252 :                 if (is_join(rel->op) && rel->attr) /* group joins */
     592           2 :                         exps_print(sql, fout, rel->attr, depth, refs, 1, 0, decorate, 0);
     593             :                 break;
     594          41 :         case op_munion:
     595          41 :                 r = "munion";
     596          41 :                 if (is_dependent(rel))
     597           0 :                         mnstr_printf(fout, "dependent ");
     598          41 :                 if (need_distinct(rel))
     599           5 :                         mnstr_printf(fout, "distinct ");
     600          41 :                 mnstr_printf(fout, "%s (", r);
     601          41 :                 assert(rel->l);
     602         145 :                 for (node *n = ((list*)rel->l)->h; n; n = n->next) {
     603         104 :                         if (rel_is_ref(n->data)) {
     604           0 :                                 int nr = find_ref(refs, n->data);
     605           0 :                                 print_indent(sql, fout, depth+1, decorate);
     606           0 :                                 mnstr_printf(fout, "& REF %d ", nr);
     607             :                         } else {
     608         104 :                                 rel_print_rel(sql, fout, n->data, depth+1, refs, decorate);
     609             :                         }
     610         104 :                         if (n->next)
     611          63 :                                 mnstr_printf(fout, ",");
     612             :                 }
     613          41 :                 print_indent(sql, fout, depth, decorate);
     614          41 :                 mnstr_printf(fout, ")");
     615          41 :                 exps_print(sql, fout, rel->exps, depth, refs, 1, 0, decorate, 0);
     616          41 :                 break;
     617        1211 :         case op_project:
     618             :         case op_select:
     619             :         case op_groupby:
     620             :         case op_topn:
     621             :         case op_sample:
     622        1211 :                 r = "project";
     623        1211 :                 if (rel->op == op_select)
     624         249 :                         r = "select";
     625        1211 :                 if (rel->op == op_groupby)
     626         111 :                         r = "group by";
     627        1211 :                 if (rel->op == op_topn)
     628          12 :                         r = "top N";
     629        1211 :                 if (rel->op == op_sample)
     630           0 :                         r = "sample";
     631             : 
     632        1211 :                 if (rel->l) {
     633        1143 :                         if (need_distinct(rel))
     634           0 :                                 mnstr_printf(fout, "distinct ");
     635        1143 :                         mnstr_printf(fout, "%s (", r);
     636        1143 :                         if (rel_is_ref(rel->l)) {
     637           2 :                                 int nr = find_ref(refs, rel->l);
     638           2 :                                 print_indent(sql, fout, depth+1, decorate);
     639           2 :                                 mnstr_printf(fout, "& REF %d ", nr);
     640             :                         } else
     641        1141 :                                 rel_print_rel(sql, fout, rel->l, depth+1, refs, decorate);
     642        1143 :                         print_indent(sql, fout, depth, decorate);
     643        1143 :                         mnstr_printf(fout, ")");
     644             :                 }
     645        1211 :                 if (rel->op == op_groupby)  /* group by columns */
     646         111 :                         exps_print(sql, fout, rel->r, depth, refs, 1, 0, decorate, 0);
     647        1211 :                 exps_print(sql, fout, rel->exps, depth, refs, 1, 0, decorate, 0);
     648        1211 :                 if (rel->r && rel->op == op_project) /* order by columns */
     649          92 :                         exps_print(sql, fout, rel->r, depth, refs, 1, 0, decorate, 0);
     650             :                 break;
     651           7 :         case op_insert:
     652             :         case op_update:
     653             :         case op_delete:
     654             :         case op_truncate:
     655             :         case op_merge: {
     656             : 
     657           7 :                 if (rel->op == op_insert)
     658           1 :                         mnstr_printf(fout, "insert(");
     659           6 :                 else if (rel->op == op_update)
     660           1 :                         mnstr_printf(fout, "update(");
     661           5 :                 else if (rel->op == op_delete)
     662           0 :                         mnstr_printf(fout, "delete(");
     663           5 :                 else if (rel->op == op_merge)
     664           0 :                         mnstr_printf(fout, "merge(");
     665           5 :                 else if (rel->op == op_truncate) {
     666           5 :                         assert(list_length(rel->exps) == 2);
     667           5 :                         sql_exp *first = (sql_exp*) rel->exps->h->data, *second = (sql_exp*) rel->exps->h->next->data;
     668           5 :                         int restart_sequences = ((atom*)first->l)->data.val.ival,
     669           5 :                                 drop_action = ((atom*)second->l)->data.val.ival;
     670          13 :                         mnstr_printf(fout, "truncate %s identity, %s(", restart_sequences ? "restart" : "continue",
     671             :                                                                                                    drop_action ? "cascade" : "restrict");
     672             :                 }
     673             : 
     674           7 :                 if (rel->l) {
     675           7 :                         if (rel_is_ref(rel->l)) {
     676           1 :                                 int nr = find_ref(refs, rel->l);
     677           1 :                                 print_indent(sql, fout, depth+1, decorate);
     678           1 :                                 mnstr_printf(fout, "& REF %d ", nr);
     679             :                         } else
     680           6 :                                 rel_print_rel(sql, fout, rel->l, depth+1, refs, decorate);
     681             :                 }
     682           7 :                 if (rel->r) {
     683           2 :                         if (rel_is_ref(rel->r)) {
     684           0 :                                 int nr = find_ref(refs, rel->r);
     685           0 :                                 print_indent(sql, fout, depth+1, decorate);
     686           0 :                                 mnstr_printf(fout, "& REF %d ", nr);
     687             :                         } else
     688           2 :                                 rel_print_rel(sql, fout, rel->r, depth+1, refs, decorate);
     689             :                 }
     690           7 :                 print_indent(sql, fout, depth, decorate);
     691           7 :                 mnstr_printf(fout, ")");
     692           7 :                 if (rel->op != op_truncate && rel->op != op_merge && rel->exps)
     693           1 :                         exps_print(sql, fout, rel->exps, depth, refs, 1, 0, decorate, 0);
     694             :         }       break;
     695             :         default:
     696           0 :                 assert(0);
     697             :         }
     698        2279 :         if (decorate && rel->p) {
     699        3162 :                 for (prop *p = rel->p; p; p = p->p) {
     700        1581 :                         if (p->kind != PROP_COUNT || (ATOMIC_GET(&GDKdebug) & FORCEMITOMASK) == 0) {
     701          12 :                                 char *pv = propvalue2string(sql->ta, p);
     702          12 :                                 mnstr_printf(fout, " %s %s", propkind2string(p), pv);
     703             :                         }
     704             :                 }
     705             :         }
     706             : }
     707             : 
     708             : void
     709        2300 : rel_print_refs(mvc *sql, stream* fout, sql_rel *rel, int depth, list *refs, int decorate)
     710             : {
     711        2300 :         if (!rel)
     712             :                 return;
     713        2300 :         switch (rel->op) {
     714         775 :         case op_basetable:
     715             :         case op_table:
     716         775 :                 if (rel->op == op_table && rel->l && rel->flag != TRIGGER_WRAPPER) {
     717          17 :                         rel_print_refs(sql, fout, rel->l, depth, refs, decorate);
     718          17 :                         if (rel_is_ref(rel->l) && !find_ref(refs, rel->l)) {
     719           0 :                                 rel_print_rel(sql, fout, rel->l, depth, refs, decorate);
     720           0 :                                 list_append(refs, rel->l);
     721             :                         }
     722             :                 }
     723             :                 break;
     724           2 :         case op_ddl:
     725           2 :                 if (rel->flag == ddl_list || rel->flag == ddl_exception) {
     726           0 :                         if (rel->l) {
     727           0 :                                 rel_print_refs(sql, fout, rel->l, depth, refs, decorate);
     728           0 :                                 if (rel_is_ref(rel->l) && !find_ref(refs, rel->l)) {
     729           0 :                                         rel_print_rel(sql, fout, rel->l, depth, refs, decorate);
     730           0 :                                         list_append(refs, rel->l);
     731             :                                 }
     732             :                         }
     733           0 :                         if (rel->r) {
     734           0 :                                 rel_print_refs(sql, fout, rel->r, depth, refs, decorate);
     735           0 :                                 if (rel_is_ref(rel->r) && !find_ref(refs, rel->r)) {
     736           0 :                                         rel_print_rel(sql, fout, rel->r, depth, refs, decorate);
     737           0 :                                         list_append(refs, rel->r);
     738             :                                 }
     739             :                         }
     740             :                 }
     741             :                 break;
     742         258 :         case op_join:
     743             :         case op_left:
     744             :         case op_right:
     745             :         case op_full:
     746             :         case op_semi:
     747             :         case op_anti:
     748             :         case op_union:
     749             :         case op_inter:
     750             :         case op_except:
     751         258 :                 if (rel->l)
     752         258 :                         rel_print_refs(sql, fout, rel->l, depth, refs, decorate);
     753         258 :                 if (rel->r)
     754         258 :                         rel_print_refs(sql, fout, rel->r, depth, refs, decorate);
     755         262 :                 if (rel->l && rel_is_ref(rel->l) && !find_ref(refs, rel->l)) {
     756           0 :                         rel_print_rel(sql, fout, rel->l, depth, refs, decorate);
     757           0 :                         list_append(refs, rel->l);
     758             :                 }
     759         258 :                 if (rel->r && rel_is_ref(rel->r) && !find_ref(refs, rel->r)) {
     760           0 :                         rel_print_rel(sql, fout, rel->r, depth, refs, decorate);
     761           0 :                         list_append(refs, rel->r);
     762             :                 }
     763             :                 break;
     764        1217 :         case op_project:
     765             :         case op_select:
     766             :         case op_groupby:
     767             :         case op_topn:
     768             :         case op_sample:
     769        1217 :                 if (rel->l)
     770        1149 :                         rel_print_refs(sql, fout, rel->l, depth, refs, decorate);
     771        1220 :                 if (rel->l && rel_is_ref(rel->l) && !find_ref(refs, rel->l)) {
     772           2 :                         rel_print_rel(sql, fout, rel->l, depth, refs, decorate);
     773           2 :                         list_append(refs, rel->l);
     774             :                 }
     775             :                 break;
     776          41 :         case op_munion:
     777          41 :                 assert(rel->l);
     778         145 :                 for (node *n = ((list*)rel->l)->h; n; n = n->next) {
     779             :                         // TODO: do we need to check n->data?
     780         104 :                         if (n->data)
     781         104 :                                 rel_print_refs(sql, fout, n->data, depth, refs, decorate);
     782         104 :                         if (n->data && rel_is_ref(n->data) && !find_ref(refs, n->data)) {
     783           0 :                                 rel_print_rel(sql, fout, n->data, depth, refs, decorate);
     784           0 :                                 list_append(refs, n->data);
     785             :                         }
     786             :                 }
     787             :                 break;
     788           7 :         case op_insert:
     789             :         case op_update:
     790             :         case op_delete:
     791             :         case op_truncate:
     792             :         case op_merge:
     793           7 :                 if (rel->l)
     794           7 :                         rel_print_refs(sql, fout, rel->l, depth, refs, decorate);
     795           8 :                 if (rel->l && rel_is_ref(rel->l) && !find_ref(refs, rel->l)) {
     796           1 :                         rel_print_rel(sql, fout, rel->l, depth, refs, decorate);
     797           1 :                         list_append(refs, rel->l);
     798             :                 }
     799           7 :                 if (rel->r)
     800           2 :                         rel_print_refs(sql, fout, rel->r, depth, refs, decorate);
     801           7 :                 if (rel->r && rel_is_ref(rel->r) && !find_ref(refs, rel->r)) {
     802           0 :                         rel_print_rel(sql, fout, rel->r, depth, refs, decorate);
     803           0 :                         list_append(refs, rel->r);
     804             :                 }
     805             :                 break;
     806             :         }
     807             : }
     808             : 
     809             : void
     810         505 : rel_print_(mvc *sql, stream *fout, sql_rel *rel, int depth, list *refs, int decorate)
     811             : {
     812         505 :         rel_print_rel(sql, fout, rel, depth, refs, decorate);
     813         505 :         if (sql->runs) {
     814           0 :                 for (int i = 0 ; i < NSQLREWRITERS ; i++) {
     815           0 :                         sql_optimizer_run *run = &(sql->runs[i]);
     816             : 
     817           0 :                         if (run->name) { /* if name is set, then the optimizer did run */
     818           0 :                                 print_indent(sql, fout, depth, decorate);
     819           0 :                                 mnstr_printf(fout, "# %-36s %3d actions " LLFMT " usec",
     820             :                                                          run->name, run->nchanges, run->time);
     821             :                         }
     822             :                 }
     823             :         }
     824         505 : }
     825             : 
     826             : static void
     827       30737 : skipWS( char *r, int *pos)
     828             : {
     829       45206 :         while(r[*pos] && (isspace((unsigned char) r[*pos]) || r[*pos] == '|'))
     830       14469 :                 (*pos)++;
     831       30737 : }
     832             : 
     833             : static void
     834           0 : skipUntilWS( char *r, int *pos)
     835             : {
     836           0 :         while(r[*pos] && (!isspace((unsigned char) r[*pos]) || r[*pos] == '|'))
     837           0 :                 (*pos)++;
     838           0 : }
     839             : 
     840             : static void
     841       11162 : skipIdent( char *r, int *pos)
     842             : {
     843       11162 :         if (r[*pos] == '"') {
     844       10487 :                 (*pos)++;
     845       58888 :                 while (r[*pos] && r[*pos] != '"') {
     846             :                         /* We send escaped '"' and '\' characters */
     847       48401 :                         if (r[*pos] == '\\' && (r[*pos + 1] == '"' || r[*pos + 1] == '\\'))
     848          12 :                                 (*pos)+=2;
     849             :                         else
     850       48389 :                                 (*pos)++;
     851             :                 }
     852             :         } else {
     853        3392 :                 while(r[*pos] && (isalnum((unsigned char) r[*pos]) || r[*pos] == '_' || r[*pos] == '%'))
     854        2717 :                         (*pos)++;
     855             :         }
     856       11162 : }
     857             : 
     858             : static void
     859       11357 : convertIdent(char *r)
     860             : {
     861       11357 :         int i = 0, j = 0;
     862       63544 :         while (r[i] && r[i] != '"') {
     863             :                 /* We send escaped '"' and '\' characters */
     864       52187 :                 if (r[i] == '\\' && (r[i + 1] == '"' || r[i + 1] == '\\')) {
     865          12 :                         r[j++] = r[i + 1];
     866          12 :                         i+=2;
     867             :                 } else {
     868       52175 :                         r[j++] = r[i++];
     869             :                 }
     870             :         }
     871       11357 :         r[j] = '\0';
     872       11357 : }
     873             : 
     874             : static void
     875        3511 : skipIdentOrSymbol( char *r, int *pos)
     876             : {
     877        3511 :         if (r[*pos] == '"') {
     878        3511 :                 skipIdent(r, pos);
     879             :         } else {
     880           0 :                 while(r[*pos] && (isalnum((unsigned char) r[*pos]) ||
     881             :                                   r[*pos] == '=' ||
     882             :                                   r[*pos] == '_' || r[*pos] == '%' ||
     883             :                                   r[*pos] == '<' || r[*pos] == '>' ||
     884             :                                   r[*pos] == '/' || r[*pos] == '*' ||
     885             :                                   r[*pos] == '-' || r[*pos] == '+' ||
     886             :                                   r[*pos] == '~' || r[*pos] == '^' ))
     887           0 :                         (*pos)++;
     888             :         }
     889        3511 : }
     890             : 
     891             : static int
     892         490 : readInt( char *r, int *pos)
     893             : {
     894         490 :         int res = 0;
     895             : 
     896        1232 :         while (isdigit((unsigned char) r[*pos])) {
     897         742 :                 res *= 10;
     898         742 :                 res += r[*pos]-'0';
     899         742 :                 (*pos)++;
     900             :         }
     901         490 :         return res;
     902             : }
     903             : 
     904             : static void *
     905         540 : readAtomString(int localtype, char *r, int *pos)
     906             : {
     907         540 :         void *res = NULL;
     908         540 :         size_t nbytes = 0;
     909         540 :         int firstpos = 0, rtype = ATOMstorage(localtype) == TYPE_str ? TYPE_str : localtype;
     910             : 
     911             :         /* TODO I had issues with the 'external' flag on the JSONfromString function, maybe something is missing there? */
     912         540 :         assert(r[*pos] == '"'); /* skip first '"' */
     913         540 :         (*pos)++;
     914             : 
     915         540 :         firstpos = *pos;
     916         540 :         if (rtype == TYPE_str) /* string reads require double quotes at the beginning */
     917         165 :                 firstpos--;
     918        2903 :         while (r[*pos] && r[*pos] != '"') { /* compute end of atom string */
     919        2363 :                 if (r[*pos] == '\\')
     920           9 :                         (*pos)+=2;
     921             :                 else
     922        2354 :                         (*pos)++;
     923             :         }
     924         540 :         if (!r[*pos])
     925             :                 return NULL;
     926             : 
     927         540 :         assert(r[*pos] == '"'); /* skip second '"' */
     928         540 :         if (rtype != TYPE_str) /* string reads require double quotes at the end */
     929         375 :                 r[*pos] = '\0';
     930         540 :         (*pos)++;
     931             : 
     932         540 :         if (ATOMfromstr(rtype, &res, &nbytes, r + firstpos, true) < 0) {
     933           0 :                 GDKfree(res);
     934           0 :                 return NULL;
     935             :         }
     936         540 :         return res;
     937             : }
     938             : 
     939             : static sql_exp*
     940        8135 : read_prop(mvc *sql, sql_exp *exp, char *r, int *pos, bool *found)
     941             : {
     942             :         /* PROPs */
     943        8135 :         if (strncmp(r+*pos, "JOINIDX",  strlen("JOINIDX")) == 0) {
     944           0 :                 char *sname, *tname, *iname;
     945           0 :                 sql_schema *s = NULL;
     946           0 :                 prop *p;
     947             : 
     948           0 :                 (*pos)+= (int) strlen("JOINIDX");
     949           0 :                 skipWS(r, pos);
     950             :                 /* schema.table.index */
     951           0 :                 sname = r+*pos + 1;
     952           0 :                 skipIdent(r,pos);
     953           0 :                 convertIdent(sname);
     954           0 :                 (*pos)++;
     955           0 :                 if (r[*pos] != '.')
     956           0 :                         return sql_error(sql, -1, SQLSTATE(42000) "JOINIDX: missing '.'\n");
     957           0 :                 (*pos)++;
     958           0 :                 tname = r+*pos + 1;
     959           0 :                 skipIdent(r,pos);
     960           0 :                 convertIdent(tname);
     961           0 :                 (*pos)++;
     962           0 :                 if (r[*pos] != '.')
     963           0 :                         return sql_error(sql, -1, SQLSTATE(42000) "JOINIDX: missing '.'\n");
     964           0 :                 (*pos)++;
     965           0 :                 iname = r+*pos + 1;
     966           0 :                 skipIdent(r,pos);
     967           0 :                 convertIdent(iname);
     968           0 :                 (*pos)++;
     969             : 
     970           0 :                 (void) tname;
     971           0 :                 s = mvc_bind_schema(sql, sname);
     972           0 :                 if (sname && !s)
     973           0 :                         return sql_error(sql, -1, SQLSTATE(42000) "Schema %s missing\n", sname);
     974           0 :                 if (!find_prop(exp->p, PROP_JOINIDX)) {
     975           0 :                         p = exp->p = prop_create(sql->sa, PROP_JOINIDX, exp->p);
     976           0 :                         if (!(p->value.pval = mvc_bind_idx(sql, s, iname)))
     977           0 :                                 return sql_error(sql, -1, SQLSTATE(42000) "Index %s missing\n", iname);
     978             :                 }
     979           0 :                 skipWS(r,pos);
     980           0 :                 if (found)
     981           0 :                         *found = true;
     982             :         }
     983             :         return exp;
     984             : }
     985             : 
     986             : static list*
     987        1204 : read_exps(mvc *sql, sql_rel *lrel, sql_rel *rrel, list *top_exps, char *r, int *pos, char bracket, int grp, int top)
     988             : {
     989        1204 :         list *exps = new_exp_list(sql->sa);
     990        1205 :         sql_exp *e;
     991        1205 :         char ebracket = (bracket == '[')?']':')';
     992             : 
     993        1205 :         if (r[*pos] == bracket) {
     994        1205 :                 skipWS( r, pos);
     995             : 
     996        1204 :                 (*pos)++;
     997        1204 :                 skipWS( r, pos);
     998        1740 :                 e = exp_read(sql, lrel, rrel, top ? exps : top_exps, r, pos, grp);
     999        1205 :                 if (!e && r[*pos] != ebracket) {
    1000           1 :                         return sql_error(sql, -1, SQLSTATE(42000) "Missing closing %c\n", ebracket);
    1001          83 :                 } else if (!e) {
    1002          83 :                         (*pos)++;
    1003          83 :                         skipWS(r, pos);
    1004          83 :                         return sql->errstr[0] ? NULL : exps; /* A function call might not have any input expressions, so return empty exps on that case */
    1005             :                 }
    1006        1121 :                 append(exps, e);
    1007        1121 :                 skipWS( r, pos);
    1008        1120 :                 if (!read_prop(sql, e, r, pos, NULL))
    1009             :                         return NULL;
    1010        3980 :                 while (r[*pos] == ',') {
    1011             : 
    1012        2860 :                         (*pos)++;
    1013        2860 :                         skipWS( r, pos);
    1014        2860 :                         e = exp_read(sql, lrel, rrel, top ? exps : top_exps, r, pos, grp);
    1015        2860 :                         if (!e)
    1016             :                                 return NULL;
    1017        2860 :                         append(exps, e);
    1018        2860 :                         skipWS( r, pos);
    1019        2860 :                         if (!read_prop(sql, e, r, pos, NULL))
    1020             :                                 return NULL;
    1021             :                 }
    1022        1120 :                 if (r[*pos] != ebracket)
    1023           0 :                         return sql_error(sql, -1, SQLSTATE(42000) "Missing closing %c\n", ebracket);
    1024        1120 :                 (*pos)++;
    1025        1120 :                 skipWS( r, pos);
    1026             :         }
    1027             :         return exps;
    1028             : }
    1029             : 
    1030             : static sql_exp*
    1031           0 : exp_read_min_or_max(mvc *sql, sql_exp *exp, char *r, int *pos, const char *prop_str, rel_prop kind)
    1032             : {
    1033           0 :         atom *a;
    1034           0 :         sql_subtype *tpe = exp_subtype(exp);
    1035             : 
    1036           0 :         (*pos)+= (int) strlen(prop_str);
    1037           0 :         skipWS(r, pos);
    1038             : 
    1039           0 :         if (strncmp(r+*pos, "NULL",  strlen("NULL")) == 0) {
    1040           0 :                 (*pos)+= (int) strlen("NULL");
    1041           0 :                 a = atom_general(sql->sa, tpe, NULL, 0);
    1042             :         } else {
    1043           0 :                 void *ptr = readAtomString(tpe->type->localtype, r, pos);
    1044           0 :                 if (!ptr)
    1045           0 :                         return sql_error(sql, -1, SQLSTATE(42000) "Invalid atom string\n");
    1046           0 :                 a = atom_general_ptr(sql->sa, tpe, ptr);
    1047           0 :                 GDKfree(ptr);
    1048             :         }
    1049           0 :         if (!find_prop(exp->p, kind)) {
    1050           0 :                 prop *p = exp->p = prop_create(sql->sa, kind, exp->p);
    1051           0 :                 p->value.pval = a;
    1052             :         }
    1053           0 :         skipWS(r, pos);
    1054           0 :         return exp;
    1055             : }
    1056             : 
    1057             : static sql_exp*
    1058           0 : exp_read_nuniques(mvc *sql, sql_exp *exp, char *r, int *pos)
    1059             : {
    1060           0 :         void *ptr = NULL;
    1061           0 :         size_t nbytes = 0;
    1062           0 :         ssize_t res = 0;
    1063           0 :         sql_subtype *tpe = sql_bind_localtype("dbl");
    1064             : 
    1065           0 :         (*pos)+= (int) strlen("NUNIQUES");
    1066           0 :         skipWS(r, pos);
    1067             : 
    1068           0 :         if ((res = ATOMfromstr(tpe->type->localtype, &ptr, &nbytes, r + *pos, true)) < 0) {
    1069           0 :                 GDKfree(ptr);
    1070           0 :                 return sql_error(sql, -1, SQLSTATE(42000) "Invalid atom string\n");
    1071             :         }
    1072             : 
    1073           0 :         if (!find_prop(exp->p, PROP_NUNIQUES)) {
    1074           0 :                 prop *p = exp->p = prop_create(sql->sa, PROP_NUNIQUES, exp->p);
    1075           0 :                 p->value.dval = *(dbl*)ptr;
    1076             :         }
    1077           0 :         (*pos) += (int) res; /* it should always fit */
    1078           0 :         GDKfree(ptr);
    1079           0 :         skipWS(r, pos);
    1080           0 :         return exp;
    1081             : }
    1082             : 
    1083             : static sql_exp*
    1084        4156 : read_exp_properties(mvc *sql, sql_exp *exp, char *r, int *pos)
    1085             : {
    1086        4156 :         bool found = true;
    1087        8313 :         while (found) {
    1088        4157 :                 found = false;
    1089             : 
    1090        4157 :                 if (strncmp(r+*pos, "COUNT",  strlen("COUNT")) == 0) {
    1091           0 :                         (*pos)+= (int) strlen("COUNT");
    1092           0 :                         if (!find_prop(exp->p, PROP_COUNT))
    1093           0 :                                 exp->p = prop_create(sql->sa, PROP_COUNT, exp->p);
    1094           0 :                         skipWS(r,pos);
    1095           0 :                         found = true;
    1096        4157 :                 } else if (strncmp(r+*pos, "HASHIDX",  strlen("HASHIDX")) == 0) {
    1097           0 :                         (*pos)+= (int) strlen("HASHIDX");
    1098           0 :                         if (!find_prop(exp->p, PROP_HASHIDX))
    1099           0 :                                 exp->p = prop_create(sql->sa, PROP_HASHIDX, exp->p);
    1100           0 :                         skipWS(r,pos);
    1101           0 :                         found = true;
    1102        4157 :                 } else if (strncmp(r+*pos, "HASHCOL",  strlen("HASHCOL")) == 0) {
    1103           0 :                         (*pos)+= (int) strlen("HASHCOL");
    1104           0 :                         if (!find_prop(exp->p, PROP_HASHCOL))
    1105           0 :                                 exp->p = prop_create(sql->sa, PROP_HASHCOL, exp->p);
    1106           0 :                         skipWS(r,pos);
    1107           0 :                         found = true;
    1108        4157 :                 } else if (strncmp(r+*pos, "MIN",  strlen("MIN")) == 0) {
    1109           0 :                         if (!exp_read_min_or_max(sql, exp, r, pos, "MIN", PROP_MIN))
    1110             :                                 return NULL;
    1111           0 :                         found = true;
    1112        4157 :                 } else if (strncmp(r+*pos, "MAX",  strlen("MAX")) == 0) {
    1113           0 :                         if (!exp_read_min_or_max(sql, exp, r, pos, "MAX", PROP_MAX))
    1114             :                                 return NULL;
    1115           0 :                         found = true;
    1116        4157 :                 } else if (strncmp(r+*pos, "NUNIQUES",  strlen("NUNIQUES")) == 0) {
    1117           0 :                         if (!exp_read_nuniques(sql, exp, r, pos))
    1118             :                                 return NULL;
    1119           0 :                         found = true;
    1120             :                 }
    1121        4157 :                 if (!read_prop(sql, exp, r, pos, &found))
    1122             :                         return NULL;
    1123             :         }
    1124             :         return exp;
    1125             : }
    1126             : 
    1127             : static sql_exp*
    1128         453 : parse_atom(mvc *sql, char *r, int *pos, sql_subtype *tpe)
    1129             : {
    1130         453 :         if (strncmp(r+*pos, "NULL",  strlen("NULL")) == 0) {
    1131          26 :                 (*pos)+= (int) strlen("NULL");
    1132          26 :                 return exp_atom(sql->sa, atom_general(sql->sa, tpe, NULL, 0));
    1133             :         } else {
    1134         427 :                 void *ptr = readAtomString(tpe->type->localtype, r, pos);
    1135         427 :                 if (!ptr)
    1136           0 :                         return sql_error(sql, -1, SQLSTATE(42000) "Invalid atom string\n");
    1137         427 :                 sql_exp *res = exp_atom(sql->sa, atom_general_ptr(sql->sa, tpe, ptr));
    1138         427 :                 GDKfree(ptr);
    1139         427 :                 return res;
    1140             :         }
    1141             : }
    1142             : 
    1143             : static sql_exp*
    1144           1 : function_error_string(mvc *sql, const char *schema, const char *fname, list *exps, bool found, sql_ftype type)
    1145             : {
    1146           1 :         char *arg_list = NULL, *F = NULL, *fn = NULL;
    1147             : 
    1148           1 :         FUNC_TYPE_STR(type, F, fn)
    1149             : 
    1150           1 :         (void) F;
    1151           1 :         if (!list_empty(exps)) {
    1152           2 :                 for (node *n = exps->h; n ; n = n->next) {
    1153           1 :                         sql_subtype *t = exp_subtype(n->data);
    1154           1 :                         char *tpe = t ? sql_subtype_string(sql->ta, t) : "?";
    1155             : 
    1156           1 :                         if (arg_list) {
    1157           0 :                                 arg_list = sa_message(sql->ta, "%s, %s", arg_list, tpe);
    1158             :                         } else {
    1159             :                                 arg_list = tpe;
    1160             :                         }
    1161             :                 }
    1162             :         }
    1163           2 :         return sql_error(sql, ERR_NOTFOUND, SQLSTATE(42000) "%s %s %s%s%s'%s'(%s)",
    1164             :                                         found ? "Insufficient privileges for" : "No such", fn, schema ? "'":"", schema ? schema : "",
    1165             :                                         schema ? "'.":"", fname, arg_list ? arg_list : "");
    1166             : }
    1167             : 
    1168             : static unsigned int /* keep updating the label count */
    1169        3028 : try_update_label_count(mvc *sql, const char *label)
    1170             : {
    1171        3028 :         if (label && label[0] == '%' && isdigit((unsigned char) label[1])) {
    1172         338 :                 char *eptr = NULL;
    1173         338 :                 unsigned int value = (unsigned int) strtol(label + 1, &eptr, 10);
    1174         338 :                 if (eptr && eptr[0] == '\0') {
    1175         338 :                         sql->label = MAX(sql->label, value);
    1176         338 :                         return value;
    1177             :                 }
    1178             :         }
    1179             :         return 0;
    1180             : }
    1181             : 
    1182             : sql_exp*
    1183        4240 : exp_read(mvc *sql, sql_rel *lrel, sql_rel *rrel, list *top_exps, char *r, int *pos, int grp)
    1184             : {
    1185        4240 :         int old, d=0, s=0, unique = 0, no_nils = 0, quote = 0, zero_if_empty = 0;
    1186        4240 :         char *tname = NULL, *cname = NULL, *var_cname = NULL, *e, *b = r + *pos;
    1187        4240 :         sql_exp *exp = NULL;
    1188        4240 :         list *exps = NULL;
    1189        4240 :         sql_type *t = NULL;
    1190        4240 :         sql_subtype tpe;
    1191             : 
    1192        4240 :         quote = (r[*pos] == '"');
    1193        4240 :         b += quote;
    1194        4240 :         skipIdent(r, pos);
    1195        4240 :         e = r+*pos;
    1196        4240 :         (*pos) += quote;
    1197        4240 :         skipWS(r, pos);
    1198        4241 :         switch(r[*pos]) {
    1199        3511 :         case '.':
    1200        3511 :                 *e = 0;
    1201        3511 :                 (*pos)++;
    1202        3511 :                 tname = b;
    1203        3511 :                 convertIdent(tname);
    1204        3511 :                 cname = r + *pos + quote;
    1205        3511 :                 skipIdentOrSymbol(r, pos);
    1206        3511 :                 e = r+*pos;
    1207        3511 :                 if (quote) {
    1208        3511 :                         old = ' ';
    1209        3511 :                         convertIdent(cname);
    1210             :                 } else {
    1211           0 :                         old = *e;
    1212             :                 }
    1213        3511 :                 *e = 0;
    1214             : 
    1215        3511 :                 tname = sa_strdup(sql->sa, tname);
    1216        3510 :                 cname = sa_strdup(sql->sa, cname);
    1217        3510 :                 *e = old;
    1218        3510 :                 skipWS(r, pos);
    1219        3510 :                 if (r[*pos] != '(') { /* if there's a function/aggregate call next don't attempt to bind columns */
    1220        3157 :                         if (top_exps) {
    1221        3000 :                                 exp = exps_bind_column2(top_exps, tname, cname, NULL);
    1222        3000 :                                 if (exp)
    1223          36 :                                         exp = exp_ref(sql, exp);
    1224             :                         }
    1225        3157 :                         if (!exp && lrel) {
    1226        3121 :                                 exp = rel_bind_column2(sql, lrel, tname, cname, 0);
    1227        3122 :                                 if (!exp && rrel)
    1228          13 :                                         exp = rel_bind_column2(sql, rrel, tname, cname, 0);
    1229          36 :                         } else if (!exp) {
    1230           0 :                                 exp = exp_column(sql->sa, tname, cname, NULL, CARD_ATOM, 1, 0, cname[0] == '%');
    1231           0 :                                 exp->alias.label = -(sql->nid++);
    1232             :                         }
    1233             :                 }
    1234             :                 break;
    1235             :         /* atom */
    1236         542 :         case '(':
    1237         542 :                 if (b == (r+*pos)) { /* comparison expression */
    1238          81 :                         int anti = 0, sym = 0, semantics = 0, any = 0;
    1239          81 :                         comp_type ctype = cmp_all, ctype2 = cmp_all;
    1240          81 :                         list *lexps = NULL, *rexps = NULL, *fexps = NULL;
    1241          81 :                         char *sname = NULL, *fname = NULL;
    1242             : 
    1243          81 :                         if (!(lexps = read_exps(sql, lrel, rrel, top_exps, r, pos, '(', 0, 0)))
    1244             :                                 return NULL;
    1245          81 :                         skipWS(r, pos);
    1246          81 :                         if (r[*pos] == '!') {
    1247           5 :                                 anti = 1;
    1248           5 :                                 (*pos)++;
    1249           5 :                                 skipWS(r, pos);
    1250             :                         }
    1251          81 :                         if (r[*pos] == '*') {
    1252           5 :                                 semantics = 1;
    1253           5 :                                 (*pos)++;
    1254           5 :                                 skipWS(r, pos);
    1255             :                         }
    1256          81 :                         if (r[*pos] == '+') {
    1257           1 :                                 any = 1;
    1258           1 :                                 (*pos)++;
    1259           1 :                                 skipWS(r, pos);
    1260             :                         }
    1261             : 
    1262          81 :                         switch(r[*pos]) {
    1263           1 :                         case 'n':
    1264           1 :                                 if (strncmp(r+*pos, "notin",  strlen("notin")) == 0) {
    1265           1 :                                         (*pos)+= (int) strlen("notin");
    1266           1 :                                         ctype = cmp_notin;
    1267             :                                 }
    1268             :                                 break;
    1269           3 :                         case 'F':
    1270           3 :                                 if (strncmp(r+*pos, "FILTER",  strlen("FILTER")) == 0) {
    1271           3 :                                         (*pos)+= (int) strlen("FILTER");
    1272           3 :                                         ctype = cmp_filter;
    1273           3 :                                         skipWS(r, pos);
    1274           3 :                                         sname = r+*pos + 1;
    1275           3 :                                         skipIdent(r, pos);
    1276           3 :                                         convertIdent(sname);
    1277           3 :                                         (*pos)+=2;
    1278           3 :                                         fname = r+*pos + 1;
    1279           3 :                                         skipIdent(r, pos);
    1280           3 :                                         convertIdent(fname);
    1281           3 :                                         (*pos)++;
    1282             :                                 }
    1283             :                                 break;
    1284           1 :                         case 'i':
    1285           1 :                                 if (strncmp(r+*pos, "in",  strlen("in")) == 0) {
    1286           1 :                                         (*pos)+= (int) strlen("in");
    1287           1 :                                         ctype = cmp_in;
    1288             :                                 }
    1289             :                                 break;
    1290           4 :                         case 'o':
    1291           4 :                                 if (strncmp(r+*pos, "or",  strlen("or")) == 0) {
    1292           4 :                                         (*pos)+= (int) strlen("or");
    1293           4 :                                         ctype = cmp_or;
    1294             :                                 }
    1295             :                                 break;
    1296           0 :                         case '!':
    1297           0 :                                 ctype = cmp_notequal;
    1298           0 :                                 (*pos)++;
    1299           0 :                                 if (r[(*pos)] == '=')
    1300           0 :                                         (*pos)++;
    1301             :                                 break;
    1302          45 :                         case '=':
    1303          45 :                                 ctype = cmp_equal;
    1304          45 :                                 (*pos)++;
    1305          45 :                                 break;
    1306          22 :                         case '<':
    1307          22 :                                 ctype = cmp_lt;
    1308          22 :                                 (*pos)++;
    1309          22 :                                 if (r[(*pos)] == '=') {
    1310          15 :                                         ctype = cmp_lte;
    1311          15 :                                         (*pos)++;
    1312             :                                 }
    1313             :                                 break;
    1314           5 :                         case '>':
    1315           5 :                                 ctype = cmp_gt;
    1316           5 :                                 (*pos)++;
    1317           5 :                                 if (r[(*pos)] == '=') {
    1318           3 :                                         ctype = cmp_gte;
    1319           3 :                                         (*pos)++;
    1320             :                                 }
    1321             :                                 break;
    1322           0 :                         default:
    1323           0 :                                 return sql_error(sql, -1, SQLSTATE(42000) "Type: missing comparison type\n");
    1324             :                         }
    1325             : 
    1326          81 :                         skipWS(r, pos);
    1327          81 :                         if (!(rexps = read_exps(sql, lrel, rrel, top_exps, r, pos, '(', 0, 0)))
    1328             :                                 return NULL;
    1329          81 :                         skipWS(r, pos);
    1330             : 
    1331          81 :                         switch (ctype) {
    1332          72 :                                 case cmp_gt:
    1333             :                                 case cmp_gte:
    1334             :                                 case cmp_lte:
    1335             :                                 case cmp_lt:
    1336             :                                 case cmp_equal:
    1337             :                                 case cmp_notequal:
    1338          72 :                                         if (r[*pos] == '!' || r[*pos] == '<' || r[*pos] == '>') { /* BETWEEN case */
    1339          17 :                                                 if (r[*pos] == '!') { /* ignore next anti */
    1340           2 :                                                         (*pos)++;
    1341           2 :                                                         skipWS(r, pos);
    1342             :                                                 }
    1343          17 :                                                 switch(r[*pos]) {
    1344          17 :                                                 case '<':
    1345          17 :                                                         ctype2 = cmp_lt;
    1346          17 :                                                         (*pos)++;
    1347          17 :                                                         if (r[(*pos)] == '=') {
    1348          14 :                                                                 ctype2 = cmp_lte;
    1349          14 :                                                                 (*pos)++;
    1350             :                                                         }
    1351             :                                                         break;
    1352           0 :                                                 case '>':
    1353           0 :                                                         ctype2 = cmp_gt;
    1354           0 :                                                         (*pos)++;
    1355           0 :                                                         if (r[(*pos)] == '=') {
    1356           0 :                                                                 ctype2 = cmp_gte;
    1357           0 :                                                                 (*pos)++;
    1358             :                                                         }
    1359             :                                                         break;
    1360           0 :                                                 default:
    1361           0 :                                                         return sql_error(sql, -1, SQLSTATE(42000) "Type: missing comparison type\n");
    1362             :                                                 }
    1363          17 :                                                 skipWS(r, pos);
    1364          17 :                                                 if (!(fexps = read_exps(sql, lrel, rrel, top_exps, r, pos, '(', 0, 0)))
    1365             :                                                         return NULL;
    1366          17 :                                                 skipWS(r, pos);
    1367          17 :                                                 if (strncmp(r+*pos, "SYM",  strlen("SYM")) == 0) {
    1368           1 :                                                         (*pos)+= (int) strlen("SYM");
    1369           1 :                                                         skipWS(r, pos);
    1370           1 :                                                         sym = 1;
    1371             :                                                 }
    1372          17 :                                                 exp = exp_compare2(sql->sa, rexps->h->data, lexps->h->data, fexps->h->data, compare2range(swap_compare(ctype), ctype2), sym);
    1373             :                                         } else {
    1374          55 :                                                 exp = exp_compare(sql->sa, lexps->h->data, rexps->h->data, ctype);
    1375          55 :                                                 if (semantics)
    1376           5 :                                                         set_semantics(exp);
    1377          55 :                                                 if (any)
    1378           1 :                                                         set_any(exp);
    1379             :                                         }
    1380          72 :                                         if (anti)
    1381           5 :                                                 set_anti(exp);
    1382          72 :                                         assert(list_length(lexps) == 1 && list_length(rexps) == 1 && (!fexps || list_length(fexps) == 1));
    1383             :                                         break;
    1384           2 :                                 case cmp_in:
    1385             :                                 case cmp_notin:
    1386           2 :                                         assert(list_length(lexps) == 1);
    1387           2 :                                         exp = exp_in(sql->sa, lexps->h->data, rexps, ctype);
    1388           2 :                                         if (anti)
    1389           0 :                                                 set_anti(exp);
    1390             :                                         break;
    1391           3 :                                 case cmp_filter: {
    1392           3 :                                         sql_subfunc *f = NULL;
    1393           3 :                                         list *tl = sa_list(sql->sa);
    1394             : 
    1395           3 :                                         if (!list_empty(lexps)) {
    1396           6 :                                                 for (node *n = lexps->h; n; n = n->next){
    1397           3 :                                                         sql_exp *e = n->data;
    1398             : 
    1399           3 :                                                         list_append(tl, exp_subtype(e));
    1400             :                                                 }
    1401             :                                         }
    1402           3 :                                         if (!list_empty(rexps)) {
    1403          12 :                                                 for (node *n = rexps->h; n; n = n->next){
    1404           9 :                                                         sql_exp *e = n->data;
    1405             : 
    1406           9 :                                                         list_append(tl, exp_subtype(e));
    1407             :                                                 }
    1408             :                                         }
    1409             : 
    1410           3 :                                         if (sname && !mvc_bind_schema(sql, sname))
    1411           0 :                                                 return sql_error(sql, ERR_NOTFOUND, SQLSTATE(3F000) "No such schema '%s'\n", sname);
    1412           3 :                                         if (!(f = sql_bind_func_(sql, sname, fname, tl, F_FILT, true, false)))
    1413           0 :                                                 return sql_error(sql, ERR_NOTFOUND, SQLSTATE(42000) "Filter: missing function '%s'.'%s'\n", sname, fname);
    1414           3 :                                         if (!execute_priv(sql, f->func))
    1415           0 :                                                 return sql_error(sql, -1, SQLSTATE(42000) "Filter: no privilege to call filter function '%s'.'%s'\n", sname, fname);
    1416           3 :                                         exp = exp_filter(sql->sa, lexps, rexps, f, anti);
    1417           3 :                                 } break;
    1418           4 :                                 case cmp_or:
    1419           4 :                                         exp = exp_or(sql->sa, lexps, rexps, anti);
    1420           4 :                                         break;
    1421           0 :                                 default:
    1422           0 :                                         return sql_error(sql, -1, SQLSTATE(42000) "Type: missing comparison type\n");
    1423             :                         }
    1424             :                         break;
    1425             :                 }
    1426             :                 /* fall through */
    1427             :         case '[':
    1428         468 :                 tname = b;
    1429         468 :                 if (tname && *tname == '[') { /* list of values */
    1430           0 :                         if (!(exps = read_exps(sql, lrel, rrel, top_exps, r, pos, '[', 0, 0)))
    1431             :                                 return NULL;
    1432           0 :                         exp = exp_values(sql->sa, exps);
    1433             :                 } else {
    1434         468 :                         old = *e;
    1435         468 :                         *e = 0;
    1436         468 :                         if (old != '[') {
    1437         461 :                                 (*pos)++;
    1438         461 :                                 d = readInt(r,pos);
    1439         461 :                                 if (r[*pos] != ')' && r[*pos] != ',')
    1440           0 :                                         return sql_error(sql, -1, SQLSTATE(42000) "Type: missing ')' or ','\n");
    1441         461 :                                 if (r[*pos] == ',') {
    1442          29 :                                         (*pos)++;
    1443          29 :                                         s = readInt(r,pos);
    1444             :                                 }
    1445         461 :                                 if (r[*pos] != ')')
    1446           0 :                                         return sql_error(sql, -1, SQLSTATE(42000) "Type: missing ')'\n");
    1447         461 :                                 (*pos)++;
    1448             :                         }
    1449         468 :                         convertIdent(tname);
    1450         468 :                         if (!sql_find_subtype(&tpe, tname, d, s)) {
    1451           0 :                                 if (!(t = mvc_bind_type(sql, tname))) /* try an external type */
    1452           0 :                                         return sql_error(sql, ERR_NOTFOUND, SQLSTATE(42000) "SQL type %s(%d, %d) not found\n", tname, d, s);
    1453           0 :                                 sql_init_subtype(&tpe, t, d, s);
    1454             :                         }
    1455         468 :                         skipWS(r, pos);
    1456         468 :                         *e = old;
    1457         468 :                         if (r[*pos] == '[') { /* convert */
    1458          61 :                                 (*pos)++;
    1459          61 :                                 skipWS(r, pos);
    1460          61 :                                 if (!(exp = exp_read(sql, lrel, rrel, top_exps, r, pos, 0)))
    1461             :                                         return NULL;
    1462          61 :                                 if (r[*pos] != ']')
    1463           0 :                                         return sql_error(sql, -1, SQLSTATE(42000) "Convert: missing ']'\n");
    1464          61 :                                 (*pos)++;
    1465          61 :                                 skipWS(r, pos);
    1466          61 :                                 exp = exp_convert(sql, exp, exp_subtype(exp), &tpe);
    1467             :                         } else {
    1468         407 :                                 if (!(exp = parse_atom(sql, r, pos, &tpe)))
    1469             :                                         return NULL;
    1470         407 :                                 skipWS(r, pos);
    1471             :                         }
    1472             :                 }
    1473             :                 break;
    1474          54 :         case '\"':
    1475             :         case 'N': /* for NULL values, but 'NOT NULL' and 'NULLS LAST' cannot match here */
    1476          54 :                 if (r[*pos] == '\"' || (strncmp(r+*pos, "NULL", strlen("NULL")) == 0 && r[*pos+4] != 'S')) {
    1477          46 :                         *e = 0;
    1478          46 :                         tname = b;
    1479          46 :                         convertIdent(tname);
    1480          46 :                         if (!sql_find_subtype(&tpe, tname, 0, 0)) {
    1481          18 :                                 if (!(t = mvc_bind_type(sql, tname))) /* try an external type */
    1482           0 :                                         return sql_error(sql, ERR_NOTFOUND, SQLSTATE(42000) "SQL type %s not found\n", tname);
    1483          18 :                                 sql_init_subtype(&tpe, t, 0, 0);
    1484             :                         }
    1485          46 :                         if (!(exp = parse_atom(sql, r, pos, &tpe)))
    1486             :                                 return NULL;
    1487          46 :                         skipWS(r, pos);
    1488             :                 }
    1489             :                 break;
    1490        4241 :         default:
    1491        4241 :                 (void)sql;
    1492             :         }
    1493             : 
    1494             :         /* func or aggr */
    1495        4241 :         if (grp) {
    1496          55 :                 skipWS(r, pos);
    1497          55 :                 if (r[*pos] == 'u') {
    1498           0 :                         unique = 1;
    1499           0 :                         (*pos)+= (int) strlen("unique");
    1500           0 :                         skipWS(r, pos);
    1501             :                 }
    1502          55 :                 if (r[*pos] == 'n') {
    1503           3 :                         no_nils = 1;
    1504           3 :                         (*pos)+= (int) strlen("no nil");
    1505           3 :                         skipWS(r, pos);
    1506             :                 }
    1507          55 :                 if (r[*pos] == 'z') {
    1508           0 :                         zero_if_empty = 1;
    1509           0 :                         (*pos)+= (int) strlen("zero if empty");
    1510           0 :                         skipWS(r, pos);
    1511             :                 }
    1512             :         }
    1513        4241 :         if (r[*pos] == '(') {
    1514         356 :                 sql_subfunc *f = NULL;
    1515             : 
    1516         356 :                 if (!(exps = read_exps(sql, lrel, rrel, top_exps, r, pos, '(', 0, 0)))
    1517             :                         return NULL;
    1518         356 :                 tname = b;
    1519         356 :                 *e = 0;
    1520         356 :                 convertIdent(tname);
    1521         356 :                 if (tname && !mvc_bind_schema(sql, tname))
    1522           0 :                         return sql_error(sql, ERR_NOTFOUND, SQLSTATE(3F000) "No such schema '%s'\n", tname);
    1523         356 :                 if (grp) {
    1524          54 :                         if (exps && exps->h) {
    1525          11 :                                 list *ops = sa_list(sql->sa);
    1526          23 :                                 for( node *n = exps->h; n; n = n->next)
    1527          12 :                                         append(ops, exp_subtype(n->data));
    1528          11 :                                 f = sql_bind_func_(sql, tname, cname, ops, F_AGGR, true, false);
    1529             :                         } else {
    1530          32 :                                 f = sql_bind_func(sql, tname, cname, sql_bind_localtype("void"), NULL, F_AGGR, true, true); /* count(*) */
    1531             :                         }
    1532          43 :                         if (!f)
    1533           0 :                                 return function_error_string(sql, tname, cname, exps, false, F_AGGR);
    1534          43 :                         if (!execute_priv(sql, f->func))
    1535           0 :                                 return function_error_string(sql, tname, cname, exps, true, F_AGGR);
    1536          43 :                         exp = exp_aggr(sql->sa, exps, f, unique, no_nils, CARD_ATOM, 1);
    1537          43 :                         if (zero_if_empty)
    1538           0 :                                 set_zero_if_empty(exp);
    1539             :                 } else {
    1540         313 :                         int nops = list_length(exps);
    1541         313 :                         if (!strcmp(tname, "sys") && (!strcmp(cname, "case") || !strcmp(cname, "casewhen") || !strcmp(cname, "coalesce") || !strcmp(cname, "nullif"))) {
    1542             :                                 /* these functions are bound on a different way */
    1543          25 :                                 if ((f = sql_find_func(sql, NULL, cname, 2, F_FUNC, true, NULL))) {
    1544          25 :                                         if (!execute_priv(sql, f->func))
    1545           0 :                                                 return function_error_string(sql, tname, cname, exps, true, F_FUNC);
    1546          25 :                                         sql_exp *res = exps->t->data;
    1547          25 :                                         sql_subtype *restype = exp_subtype(res), *condtype = NULL;
    1548          25 :                                         f->res->h->data = sql_create_subtype(sql->sa, restype->type, restype->digits, restype->scale);
    1549             :                                         /* As the inner functions may return smaller types (because of statistics and optimization),
    1550             :                                          * ie upcast here */
    1551             :                                         /* case exps are lists of (result, condition) ending with single value */
    1552             :                                         /* casewhen exps are lists of first (fe) a expression followed by (resultN, valueN) ending with single  last result  (fe == value1 -> result1 etc else last result */
    1553             :                                         /* nullif is list of values */
    1554             :                                         /* coalesce is list of values */
    1555          25 :                                         bool skip = false;
    1556          25 :                                         node *n = exps->h;
    1557          25 :                                         if (strcmp(cname, "case") == 0 && n->next) {
    1558          25 :                                                 skip = true;
    1559          25 :                                                 n = n->next;
    1560             :                                         }
    1561          25 :                                         if (strcmp(cname, "casewhen") == 0) {
    1562          18 :                                                 sql_exp *e = n->data;
    1563          18 :                                                 condtype = exp_subtype(e);
    1564          18 :                                                 n = n->next;
    1565             :                                         }
    1566          82 :                                         for (; n; n = n->next) {
    1567          57 :                                                 sql_exp *e = n->data;
    1568             : 
    1569          57 :                                                 if (condtype && n->next) {
    1570          22 :                                                         n->data = exp_check_type(sql, condtype, NULL, e, type_equal);
    1571          22 :                                                         n = n->next;
    1572          22 :                                                         e = n->data;
    1573             :                                                 }
    1574          57 :                                                 n->data = exp_check_type(sql, restype, NULL, e, type_equal);
    1575             : 
    1576          57 :                                                 if (skip && n->next && n->next->next)
    1577          57 :                                                         n = n->next;
    1578             :                                         }
    1579             :                                 }
    1580             :                         } else {
    1581         288 :                                 list *ops = sa_list(sql->sa);
    1582         845 :                                 for( node *n = exps->h; n; n = n->next)
    1583         557 :                                         append(ops, exp_subtype(n->data));
    1584             : 
    1585         288 :                                 f = sql_bind_func_(sql, tname, cname, ops, F_FUNC, true, false);
    1586         287 :                                 if (!f) {
    1587          11 :                                         sql->session->status = 0; /* if the function was not found clean the error */
    1588          11 :                                         sql->errstr[0] = '\0';
    1589          11 :                                         f = sql_bind_func_(sql, tname, cname, ops, F_ANALYTIC, true, false);
    1590             :                                 }
    1591         287 :                                 if (!f && nops > 1) { /* window functions without frames get 2 extra arguments */
    1592           6 :                                         sql->session->status = 0; /* if the function was not found clean the error */
    1593           6 :                                         sql->errstr[0] = '\0';
    1594           6 :                                         list_remove_node(ops, NULL, ops->t);
    1595           6 :                                         list_remove_node(ops, NULL, ops->t);
    1596           6 :                                         f = sql_bind_func_(sql, tname, cname, ops, F_ANALYTIC, true, false);
    1597             :                                 }
    1598         287 :                                 if (!f && nops > 4) { /* window functions with frames get 5 extra arguments */
    1599           4 :                                         sql->session->status = 0; /* if the function was not found clean the error */
    1600           4 :                                         sql->errstr[0] = '\0';
    1601          16 :                                         for (int i = 0 ; i < 3 ; i++)
    1602          12 :                                                 list_remove_node(ops, NULL, ops->t);
    1603           4 :                                         f = sql_bind_func_(sql, tname, cname, ops, F_ANALYTIC, true, false);
    1604             :                                 }
    1605         287 :                                 if (f)
    1606         286 :                                         exps = check_arguments_and_find_largest_any_type(sql, NULL, exps, f, 0, true);
    1607             : 
    1608         287 :                                 if (f && !execute_priv(sql, f->func))
    1609           0 :                                         return function_error_string(sql, tname, cname, exps, true, F_FUNC);
    1610             :                                 /* apply scale fixes if needed */
    1611         287 :                                 if (f && f->func->type != F_ANALYTIC) {
    1612         277 :                                         if (list_length(exps) == 2) {
    1613         228 :                                                 sql_exp *l = exps->h->data;
    1614         228 :                                                 sql_exp *r = exps->h->next->data;
    1615             : 
    1616             :                                                 /* Find converted value type for division and update function output type */
    1617         228 :                                                 if (f->func->fix_scale == SCALE_DIV) {
    1618           2 :                                                         sql_subtype *lt = exp_subtype(l);
    1619           2 :                                                         sql_subtype *rt = exp_subtype(r);
    1620             : 
    1621           2 :                                                         if (lt->type->scale == SCALE_FIX && rt->scale && strcmp(sql_func_imp(f->func), "/") == 0) {
    1622             :                                                                 /* TODO move into exps_scale_algebra (with internal flag) */
    1623           2 :                                                                 sql_subtype *res = f->res->h->data;
    1624           2 :                                                                 unsigned int scale = lt->scale - rt->scale;
    1625           2 :                                                                 unsigned int digits = (lt->digits > rt->digits) ? lt->digits : rt->digits;
    1626             : 
    1627             : #ifdef HAVE_HGE
    1628           2 :                                                                 if (res->type->radix == 10 && digits > 38)
    1629           2 :                                                                         digits = 38;
    1630           2 :                                                                 if (res->type->radix == 2 && digits > 127)
    1631           2 :                                                                         digits = 127;
    1632             : #else
    1633             :                                                                 if (res->type->radix == 10 && digits > 18)
    1634             :                                                                         digits = 18;
    1635             :                                                                 if (res->type->radix == 2 && digits > 63)
    1636             :                                                                         digits = 63;
    1637             : #endif
    1638             : 
    1639           2 :                                                                 sql_find_subtype(res, lt->type->base.name, digits, scale);
    1640             :                                                         }
    1641             :                                                 }
    1642             :                                         }
    1643             :                                 }
    1644             :                         }
    1645             :                         if (f) {
    1646         314 :                                 exp = exp_op(sql->sa, list_empty(exps) ? NULL : exps, f);
    1647         312 :                                 if (is_compare_func(f)) { /* has to parse any/all */
    1648         142 :                                         skipWS(r,pos);
    1649             :                                         /* [ ANY|ALL ] */
    1650         142 :                                         if (strncmp(r+*pos, "ANY",  strlen("ANY")) == 0) {
    1651           0 :                                                 (*pos)+= (int) strlen("ANY");
    1652           0 :                                                 skipWS(r, pos);
    1653           0 :                                                 exp->flag = 1;
    1654             :                                         }
    1655         142 :                                         if (strncmp(r+*pos, "ALL",  strlen("ALL")) == 0) {
    1656           0 :                                                 (*pos)+= (int) strlen("ALL");
    1657           0 :                                                 skipWS(r, pos);
    1658           0 :                                                 exp->flag = 2;
    1659             :                                         }
    1660             :                                 }
    1661             :                         } else {
    1662           1 :                                 return function_error_string(sql, tname, cname, exps, false, F_FUNC);
    1663             :                         }
    1664             :                 }
    1665             :         }
    1666             : 
    1667        4240 :         if (!exp && lrel && b != e) { /* simple ident */
    1668          57 :                 int amb = 0, mul = 0;
    1669             : 
    1670          57 :                 old = *e;
    1671          57 :                 *e = 0;
    1672          57 :                 convertIdent(b);
    1673          57 :                 var_cname = sa_strdup(sql->sa, b);
    1674          57 :                 if (top_exps) {
    1675          57 :                         exp = exps_bind_column(top_exps, var_cname, &amb, &mul, 1);
    1676          57 :                         if (exp)
    1677           0 :                                 exp = exp_ref(sql, exp);
    1678             :                 }
    1679          57 :                 (void)amb;
    1680          57 :                 (void)mul;
    1681          57 :                 assert(amb == 0 && mul == 0);
    1682          57 :                 if (!exp && lrel)
    1683          57 :                         exp = rel_bind_column(sql, lrel, var_cname, 0, 1);
    1684          57 :                 if (!exp && rrel)
    1685           1 :                         exp = rel_bind_column(sql, rrel, var_cname, 0, 1);
    1686          57 :                 *e = old;
    1687          57 :                 skipWS(r,pos);
    1688             :         }
    1689             : 
    1690        4240 :         if (!exp && (cname || var_cname)) { /* Try a variable */
    1691          29 :                 sql_var *var = NULL;
    1692          29 :                 sql_subtype *tpe = NULL;
    1693          29 :                 int level = 0;
    1694          29 :                 sql_arg *a = NULL;
    1695          29 :                 bool has_tname = cname && tname && strcmp(tname, cname) != 0;
    1696             : 
    1697          54 :                 if (find_variable_on_scope(sql, has_tname ? tname : NULL, cname ? cname : var_cname, &var, &a, &tpe, &level, "SELECT")) {
    1698          28 :                         if (var) /* if variable is known from the stack or a global var */
    1699          31 :                                 exp = exp_param_or_declared(sql->sa, var->sname ? sa_strdup(sql->sa, var->sname) : NULL, sa_strdup(sql->sa, var->name), &(var->var.tpe), level);
    1700          28 :                         if (a) /* if variable is a parameter */
    1701           0 :                                 exp = exp_param_or_declared(sql->sa, NULL, sa_strdup(sql->sa, cname), &(a->type), level);
    1702             :                 }
    1703             :         }
    1704             : 
    1705         111 :         if (!exp) {
    1706          83 :                 if (cname) {
    1707           1 :                         bool has_tname = tname && strcmp(tname, cname) != 0;
    1708           1 :                         return sql_error(sql, ERR_NOTFOUND, SQLSTATE(42000) "Identifier %s%s%s doesn't exist\n", has_tname ? tname : "", has_tname ? "." : "", cname);
    1709          82 :                 } else if (var_cname) {
    1710           0 :                         return sql_error(sql, ERR_NOTFOUND, SQLSTATE(42000) "Identifier %s doesn't exist\n", var_cname);
    1711             :                 }
    1712             :                 return NULL;
    1713             :         }
    1714             : 
    1715             :         /* [ ASC ] */
    1716        4157 :         if (strncmp(r+*pos, "ASC",  strlen("ASC")) == 0) {
    1717          12 :                 (*pos)+= (int) strlen("ASC");
    1718          12 :                 skipWS(r, pos);
    1719          12 :                 set_ascending(exp);
    1720             :         }
    1721             :         /* [ NULLS LAST ] */
    1722        4157 :         if (strncmp(r+*pos, "NULLS LAST",  strlen("NULLS LAST")) == 0) {
    1723           9 :                 (*pos)+= (int) strlen("NULLS LAST");
    1724           9 :                 skipWS(r, pos);
    1725           9 :                 set_nulls_last(exp);
    1726             :         }
    1727             :         /* [ NOT NULL ] */
    1728        4157 :         if (strncmp(r+*pos, "NOT NULL",  strlen("NOT NULL")) == 0) {
    1729         320 :                 (*pos)+= (int) strlen("NOT NULL");
    1730         320 :                 skipWS(r, pos);
    1731         321 :                 set_has_no_nil(exp);
    1732             :         }
    1733             :         /* [ UNIQUE ] */
    1734        4158 :         if (strncmp(r+*pos, "UNIQUE",  strlen("UNIQUE")) == 0) {
    1735         225 :                 (*pos)+= (int) strlen("UNIQUE");
    1736         225 :                 skipWS(r, pos);
    1737         226 :                 set_unique(exp);
    1738             :         }
    1739             : 
    1740        4159 :         if (!(exp = read_exp_properties(sql, exp, r, pos)))
    1741             :                 return NULL;
    1742             : 
    1743             :         /* as alias */
    1744        4156 :         if (strncmp(r+*pos, "as", 2) == 0) {
    1745        1492 :                 unsigned int rlabel = 0, nlabel = 0;
    1746        1492 :                 (*pos)+=2;
    1747        1492 :                 skipWS(r, pos);
    1748             : 
    1749        1492 :                 tname = r+*pos+1;
    1750        1492 :                 skipIdent(r, pos);
    1751        1492 :                 convertIdent(tname);
    1752        1492 :                 (*pos)++;
    1753        1492 :                 if (r[*pos] != '.') {
    1754          20 :                         cname = tname;
    1755          20 :                         tname = NULL;
    1756          20 :                         exp_setname(sql, exp, NULL, sa_strdup(sql->sa, cname));
    1757          20 :                         skipWS(r, pos);
    1758             :                 } else {
    1759        1472 :                         (*pos)++;
    1760        1472 :                         cname = r+*pos+1;
    1761        1472 :                         skipIdent(r, pos);
    1762        1472 :                         convertIdent(cname);
    1763        1472 :                         (*pos)++;
    1764        1472 :                         skipWS(r, pos);
    1765        1473 :                         exp_setname(sql, exp, sa_strdup(sql->sa, tname), sa_strdup(sql->sa, cname));
    1766             :                 }
    1767        1493 :                 rlabel = try_update_label_count(sql, tname);
    1768        1493 :                 nlabel = try_update_label_count(sql, cname);
    1769        1493 :                 if (rlabel && rlabel == nlabel)
    1770         156 :                         exp->alias.label = rlabel;
    1771             :         }
    1772             : 
    1773        4157 :         skipWS(r, pos);
    1774             : 
    1775             : 
    1776             :         //void *ptr = readAtomString(tpe->type->localtype, r, pos);
    1777        4157 :         if (strncmp(r+*pos, "COMMENT",  strlen("COMMENT")) == 0) {
    1778         113 :                 (*pos)+= (int) strlen("COMMENT");
    1779         113 :                 skipWS(r, pos);
    1780         113 :                 str comment = readAtomString(TYPE_str, r, pos);
    1781         113 :                 exp->comment = sa_strdup(sql->sa, comment);
    1782         113 :                 GDKfree(comment);
    1783             :         }
    1784             : 
    1785             :         return exp;
    1786             : }
    1787             : 
    1788             : static int
    1789           2 : rel_set_types(mvc *sql, sql_rel *rel)
    1790             : {
    1791           2 :         list *iexps = rel_projections( sql, rel->l, NULL, 0, 1);
    1792           2 :         node *n, *m;
    1793             : 
    1794           2 :         if (!iexps || list_length(iexps) > list_length(rel->exps))
    1795           0 :                 return -1;
    1796           7 :         for(n=iexps->h, m=rel->exps->h; n && m; n = n->next, m = m->next) {
    1797           5 :                 sql_exp *e = m->data;
    1798             : 
    1799           5 :                 if (!e->tpe.type)
    1800           0 :                         e->tpe = *exp_subtype( n->data );
    1801             :         }
    1802             :         return 0;
    1803             : }
    1804             : 
    1805             : static int
    1806           0 : rel_set_types_n_ary(mvc *sql, sql_rel *rel)
    1807             : {
    1808           0 :         list *rels = rel->l;
    1809           0 :         list *iexps = rel_projections( sql, rels->h->data, NULL, 0, 1);
    1810           0 :         node *n, *m;
    1811             : 
    1812           0 :         if (!iexps || list_length(iexps) > list_length(rel->exps))
    1813           0 :                 return -1;
    1814           0 :         for(n=iexps->h, m=rel->exps->h; n && m; n = n->next, m = m->next) {
    1815           0 :                 sql_exp *e = m->data;
    1816             : 
    1817           0 :                 if (!e->tpe.type)
    1818           0 :                         e->tpe = *exp_subtype( n->data );
    1819             :         }
    1820             :         return 0;
    1821             : }
    1822             : 
    1823             : static sql_rel*
    1824           0 : rel_read_count(mvc *sql, sql_rel *rel, char *r, int *pos)
    1825             : {
    1826           0 :         void *ptr = NULL;
    1827           0 :         size_t nbytes = 0;
    1828           0 :         ssize_t res = 0;
    1829           0 :         sql_subtype *tpe = sql_bind_localtype("oid");
    1830             : 
    1831           0 :         (*pos)+= (int) strlen("COUNT");
    1832           0 :         skipWS(r, pos);
    1833             : 
    1834           0 :         if ((res = ATOMfromstr(tpe->type->localtype, &ptr, &nbytes, r + *pos, true)) < 0) {
    1835           0 :                 GDKfree(ptr);
    1836           0 :                 return sql_error(sql, -1, SQLSTATE(42000) "Invalid atom string\n");
    1837             :         }
    1838             : 
    1839           0 :         set_count_prop(sql->sa, rel, *(BUN*)ptr);
    1840           0 :         (*pos) += (int) res; /* it should always fit */
    1841           0 :         GDKfree(ptr);
    1842           0 :         skipWS(r, pos);
    1843           0 :         return rel;
    1844             : }
    1845             : 
    1846             : static sql_rel*
    1847         594 : read_rel_properties(mvc *sql, sql_rel *rel, char *r, int *pos)
    1848             : {
    1849         594 :         bool found = true;
    1850         594 :         while (found) {
    1851         594 :                 found = false;
    1852             : 
    1853         594 :                 if (strncmp(r+*pos, "COUNT",  strlen("COUNT")) == 0) {
    1854           0 :                         if (!rel_read_count(sql, rel, r, pos))
    1855             :                                 return NULL;
    1856             :                         found = true;
    1857         594 :                 } else if (strncmp(r+*pos, "REMOTE", strlen("REMOTE")) == 0) { /* Remote tables under remote tables not supported, so remove REMOTE property */
    1858           0 :                         (*pos)+= (int) strlen("REMOTE");
    1859           0 :                         skipWS(r, pos);
    1860           0 :                         skipUntilWS(r, pos);
    1861           0 :                         skipWS(r, pos);
    1862           0 :                         found = true;
    1863         594 :                 } else if (strncmp(r+*pos, "USED", strlen("USED")) == 0) {
    1864           0 :                         (*pos)+= (int) strlen("USED");
    1865           0 :                         if (!find_prop(rel->p, PROP_USED))
    1866           0 :                                 rel->p = prop_create(sql->sa, PROP_USED, rel->p);
    1867           0 :                         skipWS(r, pos);
    1868           0 :                         found = true;
    1869         594 :                 } else if (strncmp(r+*pos, "GROUPINGS", strlen("GROUPINGS")) == 0) {
    1870           0 :                         (*pos)+= (int) strlen("GROUPINGS");
    1871           0 :                         if (!find_prop(rel->p, PROP_GROUPINGS))
    1872           0 :                                 rel->p = prop_create(sql->sa, PROP_GROUPINGS, rel->p);
    1873           0 :                         skipWS(r, pos);
    1874           0 :                         found = true;
    1875             :                 }
    1876             :         }
    1877             :         return rel;
    1878             : }
    1879             : 
    1880             : sql_rel*
    1881         603 : rel_read(mvc *sql, char *r, int *pos, list *refs)
    1882             : {
    1883         603 :         sql_rel *rel = NULL, *nrel, *lrel, *rrel = NULL;
    1884         603 :         list *exps, *gexps, *rels = NULL;
    1885         603 :         int distinct = 0, dependent = 0, single = 0;
    1886         603 :         operator_type j = op_basetable;
    1887         603 :         bool groupjoin = false;
    1888             : 
    1889         603 :         skipWS(r,pos);
    1890         603 :         if (r[*pos] == 'R') {
    1891           0 :                 *pos += (int) strlen("REF");
    1892             : 
    1893           0 :                 skipWS(r, pos);
    1894           0 :                 (void)readInt(r,pos);
    1895           0 :                 skipWS(r, pos);
    1896           0 :                 (*pos)++; /* ( */
    1897           0 :                 (void)readInt(r,pos); /* skip nr refs */
    1898           0 :                 (*pos)++; /* ) */
    1899           0 :                 if (!(rel = rel_read(sql, r, pos, refs)))
    1900             :                         return NULL;
    1901           0 :                 append(refs, rel);
    1902           0 :                 skipWS(r,pos);
    1903             :         }
    1904         603 :         if (r[*pos] == '&') {
    1905           0 :                 int nr;
    1906           0 :                 (*pos)++;
    1907           0 :                 skipWS(r, pos);
    1908           0 :                 *pos += (int) strlen("REF");
    1909           0 :                 skipWS(r, pos);
    1910           0 :                 nr = readInt(r,pos); /* skip nr refs */
    1911           0 :                 return rel_dup(list_fetch(refs, nr-1));
    1912             :         }
    1913             : 
    1914         603 :         if (r[*pos] == 'i' && r[*pos+1] == 'n' && r[*pos+2] == 's') {
    1915           0 :                 sql_table *t;
    1916             : 
    1917           0 :                 *pos += (int) strlen("insert");
    1918           0 :                 skipWS(r, pos);
    1919           0 :                 (*pos)++; /* ( */
    1920           0 :                 if (!(lrel = rel_read(sql, r, pos, refs))) /* to be inserted relation */
    1921             :                         return NULL;
    1922           0 :                 skipWS(r,pos);
    1923           0 :                 if (!(rrel = rel_read(sql, r, pos, refs))) /* the inserts relation */
    1924             :                         return NULL;
    1925           0 :                 skipWS(r,pos);
    1926           0 :                 (*pos)++; /* ) */
    1927             : 
    1928           0 :                 t = get_table(lrel);
    1929           0 :                 if (!insert_allowed(sql, t, t->base.name, "INSERT", "insert"))
    1930             :                         return NULL;
    1931             : 
    1932           0 :                 if (!(rel = rel_insert(sql, lrel, rrel)) || !(rel = read_rel_properties(sql, rel, r, pos)))
    1933           0 :                         return NULL;
    1934             :         }
    1935             : 
    1936         603 :         if (r[*pos] == 'd' && r[*pos+1] == 'e' && r[*pos+2] == 'l') {
    1937           0 :                 sql_table *t;
    1938             : 
    1939           0 :                 *pos += (int) strlen("delete");
    1940           0 :                 skipWS(r, pos);
    1941           0 :                 (*pos)++; /* ( */
    1942           0 :                 if (!(lrel = rel_read(sql, r, pos, refs))) /* to be deleted relation */
    1943             :                         return NULL;
    1944           0 :                 skipWS(r,pos);
    1945           0 :                 if (!(rrel = rel_read(sql, r, pos, refs))) /* the deletes relation */
    1946             :                         return NULL;
    1947           0 :                 skipWS(r,pos);
    1948           0 :                 (*pos)++; /* ) */
    1949             : 
    1950           0 :                 t = get_table(lrel);
    1951           0 :                 if (!update_allowed(sql, t, t->base.name, "DELETE", "delete", 1))
    1952             :                         return NULL;
    1953             : 
    1954           0 :                 if (!(rel = rel_delete(sql->sa, lrel, rrel)) || !(rel = read_rel_properties(sql, rel, r, pos)))
    1955           0 :                         return NULL;
    1956             :         }
    1957             : 
    1958         603 :         if (r[*pos] == 't' && r[*pos+1] == 'r' && r[*pos+2] == 'u') {
    1959           0 :                 sql_table *t;
    1960           0 :                 int restart_sequences = 0, drop_action = 0;
    1961             : 
    1962           0 :                 *pos += (int) strlen("truncate ");
    1963           0 :                 if (r[*pos] == 'r') {
    1964           0 :                         restart_sequences = 1;
    1965           0 :                         *pos += (int) strlen("restart identity, ");
    1966             :                 } else {
    1967           0 :                         *pos += (int) strlen("continue identity, ");
    1968             :                 }
    1969           0 :                 if (r[*pos] == 'c') {
    1970           0 :                         drop_action = 1;
    1971           0 :                         *pos += (int) strlen("cascade");
    1972             :                 } else {
    1973           0 :                         *pos += (int) strlen("restrict");
    1974             :                 }
    1975           0 :                 skipWS(r, pos);
    1976           0 :                 (*pos)++; /* ( */
    1977           0 :                 if (!(lrel = rel_read(sql, r, pos, refs))) /* to be truncated relation */
    1978             :                         return NULL;
    1979           0 :                 skipWS(r,pos);
    1980           0 :                 (*pos)++; /* ) */
    1981             : 
    1982           0 :                 t = get_table(lrel);
    1983           0 :                 if (!update_allowed(sql, t, t->base.name, "TRUNCATE", "truncate", 2))
    1984             :                         return NULL;
    1985             : 
    1986           0 :                 if (!(rel = rel_truncate(sql->sa, lrel, restart_sequences, drop_action)) || !(rel = read_rel_properties(sql, rel, r, pos)))
    1987           0 :                         return NULL;
    1988             :         }
    1989             : 
    1990         603 :         if (r[*pos] == 'u' && r[*pos+1] == 'p' && r[*pos+2] == 'd') {
    1991           0 :                 sql_table *t;
    1992           0 :                 list *nexps = new_exp_list(sql->sa);
    1993             : 
    1994           0 :                 *pos += (int) strlen("update");
    1995           0 :                 skipWS(r, pos);
    1996           0 :                 (*pos)++; /* ( */
    1997           0 :                 if (!(lrel = rel_read(sql, r, pos, refs))) /* to be updated relation */
    1998             :                         return NULL;
    1999           0 :                 skipWS(r,pos);
    2000           0 :                 if (!(rrel = rel_read(sql, r, pos, refs))) /* the updates relation */
    2001             :                         return NULL;
    2002           0 :                 skipWS(r,pos);
    2003           0 :                 (*pos)++; /* ) */
    2004             : 
    2005           0 :                 t = get_table(lrel);
    2006           0 :                 if (!update_allowed(sql, t, t->base.name, "UPDATE", "update", 0) )
    2007             :                         return NULL;
    2008             : 
    2009           0 :                 if (!(exps = read_exps(sql, lrel, rrel, NULL, r, pos, '[', 0, 1))) /* columns to be updated */
    2010             :                         return NULL;
    2011             : 
    2012           0 :                 for (node *n = rel->exps->h ; n ; n = n->next) {
    2013           0 :                         sql_exp *e = (sql_exp *) n->data;
    2014           0 :                         const char *cname = exp_name(e);
    2015             : 
    2016           0 :                         if (cname[0] != '%') { /* Skip TID column */
    2017           0 :                                 sql_column *c = mvc_bind_column(sql, t, cname);
    2018             : 
    2019           0 :                                 if (!c)
    2020           0 :                                         return sql_error(sql, ERR_NOTFOUND, SQLSTATE(42S22) "UPDATE: no such column '%s.%s'\n", t->base.name, cname);
    2021           0 :                                 if (!(e = update_check_column(sql, t, c, e, rrel, c->base.name, "UPDATE")))
    2022             :                                         return NULL;
    2023             :                         }
    2024           0 :                         list_append(nexps, e);
    2025             :                 }
    2026             : 
    2027           0 :                 if (!(rel = rel_update(sql, lrel, rrel, NULL, nexps)) || !(rel = read_rel_properties(sql, rel, r, pos)))
    2028           0 :                         return NULL;
    2029             :         }
    2030             : 
    2031         603 :         if (r[*pos] == 'm' && r[*pos+1] == 'e' && r[*pos+2] == 'r')
    2032           0 :                 return sql_error(sql, -1, SQLSTATE(42000) "Merge statements not supported in remote plans\n");
    2033             : 
    2034         603 :         if (r[*pos] == 'd' && r[*pos+1] == 'i') {
    2035           0 :                 *pos += (int) strlen("distinct");
    2036           0 :                 skipWS(r, pos);
    2037           0 :                 distinct = 1;
    2038             :         }
    2039         603 :         if (r[*pos] == 's' && r[*pos+1] == 'i') {
    2040           0 :                 *pos += (int) strlen("single");
    2041           0 :                 skipWS(r, pos);
    2042           0 :                 single = 1;
    2043             :         }
    2044         603 :         if (r[*pos] == 'd' && r[*pos+1] == 'e') {
    2045           0 :                 *pos += (int) strlen("dependent");
    2046           0 :                 skipWS(r, pos);
    2047           0 :                 dependent = 1;
    2048             :         }
    2049             : 
    2050         603 :         switch(r[*pos]) {
    2051         203 :         case 't':
    2052         203 :                 if (r[*pos+1] == 'a') {
    2053         200 :                         sql_schema *s = NULL;
    2054         200 :                         sql_table *t = NULL;
    2055         200 :                         char *sname, *tname;
    2056         200 :                         *pos += (int) strlen("table");
    2057         200 :                         skipWS(r, pos);
    2058         200 :                         if (r[*pos] != '(')
    2059           0 :                                 return sql_error(sql, -1, SQLSTATE(42000) "Table: missing '('\n");
    2060         200 :                         (*pos)++;
    2061         200 :                         skipWS(r, pos);
    2062         200 :                         sname = r+*pos + 1;
    2063         200 :                         skipIdent(r, pos);
    2064         200 :                         convertIdent(sname);
    2065         200 :                         (*pos)++;
    2066         200 :                         if (r[*pos] != '.')
    2067           0 :                                 return sql_error(sql, -1, SQLSTATE(42000) "Table: missing '.' in table name\n");
    2068         200 :                         (*pos)++;
    2069         200 :                         tname = r+*pos + 1;
    2070         200 :                         skipIdent(r, pos);
    2071         200 :                         convertIdent(tname);
    2072         200 :                         (*pos)++;
    2073         200 :                         skipWS(r, pos);
    2074         200 :                         if (r[*pos] == '(') { /* table returning function */
    2075             :                                 node *m;
    2076             :                                 sql_exp *tudf, *next;
    2077             :                                 list *inputs, *outputs;
    2078             :                                 sql_subfunc *sf;
    2079             :                                 int x = *pos, y; /* save current position, after parsing the input relation we have to parse the input parameters */
    2080             :                                 bool inside_identifier = false;
    2081             : 
    2082         422 :                                 while (r[*pos] && (inside_identifier || r[*pos] != '\n')) { /* the input parameters must be parsed after the input relation, skip them for now  */
    2083         208 :                                         if (inside_identifier && r[*pos] == '\\' && (r[*pos + 1] == '"' || r[*pos + 1] == '\\')) {
    2084           1 :                                                 (*pos)+=2;
    2085         416 :                                         } else if (r[*pos] == '"') {
    2086         102 :                                                 inside_identifier = !inside_identifier;
    2087         102 :                                                 (*pos)++;
    2088             :                                         } else {
    2089         314 :                                                 (*pos)++;
    2090             :                                         }
    2091             :                                 }
    2092           5 :                                 if (r[*pos] != '\n')
    2093           0 :                                         return sql_error(sql, -1, SQLSTATE(42000) "Table returning function: missing ']' for output parameters\n");
    2094             : 
    2095           5 :                                 skipWS(r, pos); /* now parse the input relation */
    2096           5 :                                 if (!(lrel = rel_read(sql, r, pos, refs)))
    2097             :                                         return NULL;
    2098           5 :                                 y = *pos; /* later we have to return here to parse the output identifiers */
    2099           5 :                                 *pos = x;
    2100           5 :                                 if (!(inputs = read_exps(sql, lrel, NULL, NULL, r, pos, '(', 0, 1)))
    2101             :                                         return NULL;
    2102             : 
    2103           5 :                                 if (!mvc_bind_schema(sql, sname))
    2104           0 :                                         return sql_error(sql, ERR_NOTFOUND, SQLSTATE(3F000) "No such schema '%s'\n", sname);
    2105           5 :                                 if (!(tudf = find_table_function(sql, sname, tname, list_empty(inputs) ? NULL : inputs, list_empty(inputs) ? NULL : exp_types(sql->sa, inputs), F_UNION)))
    2106             :                                         return NULL;
    2107           5 :                                 sf = tudf->f;
    2108           5 :                                 if (tudf->type != e_func || sf->func->type != F_UNION)
    2109           0 :                                         return sql_error(sql, 02, SQLSTATE(42000) "'%s' does not return a table\n", exp_func_name(tudf));
    2110             : 
    2111           5 :                                 *pos = y; /* now at the end of the input relation */
    2112           5 :                                 skipWS(r, pos);
    2113           5 :                                 if (r[*pos] != ')')
    2114           0 :                                         return sql_error(sql, -1, SQLSTATE(42000) "Table returning function: missing ')' at the end of the input relation\n");
    2115           5 :                                 (*pos)++;
    2116           5 :                                 skipWS(r, pos);
    2117             : 
    2118             :                                 /* Parse identifiers manually, we cannot use read_exps because the labels may not match */
    2119           5 :                                 if (r[*pos] != '[')
    2120           0 :                                         return sql_error(sql, -1, SQLSTATE(42000) "Table returning function: missing '[' for output parameters\n");
    2121           5 :                                 (*pos)++;
    2122           5 :                                 skipWS(r, pos);
    2123           5 :                                 m = sf->func->res->h;
    2124           5 :                                 outputs = new_exp_list(sql->sa);
    2125          27 :                                 while (r[*pos] && r[*pos] != ']' && m) {
    2126          22 :                                         sql_arg *a = m->data;
    2127          22 :                                         unsigned int rlabel, nlabel;
    2128          22 :                                         char *nrname, *ncname;
    2129             : 
    2130          22 :                                         if (r[*pos] != '"')
    2131           0 :                                                 return sql_error(sql, -1, SQLSTATE(42000) "Table returning function: missing identifier for output parameters\n");
    2132          22 :                                         nrname = r+*pos + 1;
    2133          22 :                                         skipIdent(r, pos);
    2134          22 :                                         if (r[*pos] != '"')
    2135           0 :                                                 return sql_error(sql, -1, SQLSTATE(42000) "Table returning function: missing identifier for output parameters\n");
    2136          22 :                                         convertIdent(nrname);
    2137          22 :                                         (*pos)++;
    2138          22 :                                         if (r[*pos] != '.')
    2139           0 :                                                 return sql_error(sql, -1, SQLSTATE(42000) "Table returning function: missing '.' for output parameters\n");
    2140          22 :                                         (*pos)++; /* skip '.' */
    2141          22 :                                         if (r[*pos] != '"')
    2142           0 :                                                 return sql_error(sql, -1, SQLSTATE(42000) "Table returning function: missing identifier for output parameters\n");
    2143          22 :                                         ncname = r+*pos + 1;
    2144          22 :                                         skipIdent(r, pos);
    2145          22 :                                         if (r[*pos] != '"')
    2146           0 :                                                 return sql_error(sql, -1, SQLSTATE(42000) "Table returning function: missing identifier for output parameters\n");
    2147          22 :                                         convertIdent(ncname);
    2148          22 :                                         (*pos)++;
    2149          22 :                                         if (r[*pos] == ',')
    2150          17 :                                                 (*pos)++;
    2151             : 
    2152          22 :                                         next = exp_column(sql->sa, nrname, ncname, &a->type, CARD_MULTI, 1, 0, 0);
    2153          22 :                                         next->alias.label = -(sql->nid++);
    2154          22 :                                         rlabel = try_update_label_count(sql, nrname);
    2155          22 :                                         nlabel = try_update_label_count(sql, ncname);
    2156          22 :                                         if (rlabel && rlabel == nlabel)
    2157           0 :                                                 next->alias.label = rlabel;
    2158          22 :                                         set_basecol(next);
    2159          22 :                                         append(outputs, next);
    2160          22 :                                         m = m->next;
    2161          22 :                                         skipWS(r, pos);
    2162             :                                 }
    2163           5 :                                 if (r[*pos] != ']')
    2164           0 :                                         return sql_error(sql, -1, SQLSTATE(42000) "Table returning function: missing ']' for output parameters\n");
    2165           5 :                                 (*pos)++;
    2166           5 :                                 skipWS(r, pos);
    2167           5 :                                 if (list_length(outputs) != list_length(sf->func->res))
    2168           0 :                                         return sql_error(sql, -1, SQLSTATE(42000) "Table returning function: the number of output parameters don't match the table ones relation outputs: %d != function outputs: %d\n",
    2169           0 :                                                                          list_length(outputs), list_length(sf->func->res));
    2170           5 :                                 rel = rel_table_func(sql->sa, lrel, tudf, outputs, TABLE_FROM_RELATION);
    2171           5 :                                 set_processed(rel);
    2172             :                         } else {
    2173         195 :                                 if (r[*pos] != ')')
    2174           0 :                                         sql_error(sql, -1, SQLSTATE(42000) "Table: missing ')'\n");
    2175         195 :                                 (*pos)++;
    2176         195 :                                 skipWS(r, pos);
    2177         195 :                                 if (!(s = mvc_bind_schema(sql, sname)))
    2178           0 :                                         return sql_error(sql, ERR_NOTFOUND, SQLSTATE(3F000) "No such schema '%s'\n", sname);
    2179         195 :                                 if (stack_has_frame(sql, "ALTER TABLE ADD CONSTRAINT CHECK") && !(t = frame_find_table(sql, tname)))
    2180           0 :                                         return sql_error(sql, ERR_NOTFOUND, SQLSTATE(42S02) "Table missing '%s.%s'\n", sname, tname);
    2181         195 :                                 if (!t && !(t = mvc_bind_table(sql, s, tname)))
    2182           0 :                                         return sql_error(sql, ERR_NOTFOUND, SQLSTATE(42S02) "Table missing '%s.%s'\n", sname, tname);
    2183         195 :                                 if (isMergeTable(t))
    2184           1 :                                         return sql_error(sql, -1, SQLSTATE(42000) "Merge tables not supported under remote connections\n");
    2185         194 :                                 if (isRemote(t))
    2186           1 :                                         return sql_error(sql, -1, SQLSTATE(42000) "Remote tables not supported under remote connections\n");
    2187         193 :                                 if (isReplicaTable(t))
    2188           0 :                                         return sql_error(sql, -1, SQLSTATE(42000) "Replica tables not supported under remote connections\n");
    2189         193 :                                 rel = rel_basetable(sql, t, tname);
    2190         193 :                                 if (!table_privs(sql, t, PRIV_SELECT))  {
    2191           0 :                                         rel_base_disallow(rel);
    2192           0 :                                         if (rel_base_has_column_privileges(sql, rel) == 0)
    2193           0 :                                                 return sql_error(sql, -1, SQLSTATE(42000) "Access denied for %s to table '%s.%s'\n",
    2194             :                                                                          get_string_global_var(sql, "current_user"), s->base.name, tname);
    2195             :                                 }
    2196         193 :                                 rel_base_use_all(sql, rel);
    2197         193 :                                 rel = rewrite_basetable(sql, rel);
    2198             : 
    2199         193 :                                 if (!r[*pos])
    2200             :                                         return rel;
    2201             : 
    2202             :                                 /* scan aliases */
    2203         193 :                                 if (!(exps = read_exps(sql, rel, NULL, NULL, r, pos, '[', 0, 1)))
    2204             :                                         return NULL;
    2205         192 :                                 rel->exps = exps;
    2206             :                         }
    2207             :                 } else { /* top N */
    2208           3 :                         *pos += (int) strlen("top N");
    2209           3 :                         skipWS(r, pos);
    2210           3 :                         if (r[*pos] != '(')
    2211           0 :                                 return sql_error(sql, -1, SQLSTATE(42000) "Top N: missing '('\n");
    2212           3 :                         (*pos)++;
    2213           3 :                         skipWS(r, pos);
    2214           3 :                         if (!(nrel = rel_read(sql, r, pos, refs)))
    2215             :                                 return NULL;
    2216           3 :                         if (r[*pos] != ')')
    2217           0 :                                 return sql_error(sql, -1, SQLSTATE(42000) "Top N: missing ')'\n");
    2218           3 :                         (*pos)++;
    2219           3 :                         skipWS(r, pos);
    2220           3 :                         if (!(exps = read_exps(sql, nrel, NULL, NULL, r, pos, '[', 0, 1)))
    2221             :                                 return NULL;
    2222           3 :                         rel = rel_topn(sql->sa, nrel, exps);
    2223           3 :                         set_processed(rel);
    2224             :                 }
    2225             :                 break;
    2226         274 :         case 'p':
    2227         274 :                 *pos += (int) strlen("project");
    2228         274 :                 skipWS(r, pos);
    2229             : 
    2230         274 :                 if (r[*pos] != '(')
    2231           0 :                         return sql_error(sql, -1, SQLSTATE(42000) "Project: missing '('\n");
    2232         274 :                 (*pos)++;
    2233         274 :                 skipWS(r, pos);
    2234         274 :                 if (!(nrel = rel_read(sql, r, pos, refs)))
    2235             :                         return NULL;
    2236         270 :                 skipWS(r, pos);
    2237         270 :                 if (r[*pos] != ')')
    2238           0 :                         return sql_error(sql, -1, SQLSTATE(42000) "Project: missing ')'\n");
    2239         270 :                 (*pos)++;
    2240         270 :                 skipWS(r, pos);
    2241             : 
    2242         270 :                 if (!(exps = read_exps(sql, nrel, NULL, NULL, r, pos, '[', 0, 1)))
    2243             :                         return NULL;
    2244         269 :                 rel = rel_project(sql->sa, nrel, exps);
    2245         269 :                 set_processed(rel);
    2246             :                 /* order by ? */
    2247             :                 /* first projected expressions, then left relation projections */
    2248         269 :                 if (r[*pos] == '[' && !(rel->r = read_exps(sql, rel, nrel, NULL, r, pos, '[', 0, 1)))
    2249             :                         return NULL;
    2250             :                 break;
    2251          61 :         case 's':
    2252             :         case 'a':
    2253          61 :                 if (r[*pos+1] == 'a') {
    2254           0 :                         *pos += (int) strlen("sample");
    2255           0 :                         skipWS(r, pos);
    2256           0 :                         if (r[*pos] != '(')
    2257           0 :                                 return sql_error(sql, -1, SQLSTATE(42000) "Sample: missing '('\n");
    2258           0 :                         (*pos)++;
    2259           0 :                         skipWS(r, pos);
    2260           0 :                         if (!(nrel = rel_read(sql, r, pos, refs)))
    2261             :                                 return NULL;
    2262           0 :                         if (r[*pos] != ')')
    2263           0 :                                 return sql_error(sql, -1, SQLSTATE(42000) "Sample: missing ')'\n");
    2264           0 :                         (*pos)++;
    2265           0 :                         skipWS(r, pos);
    2266           0 :                         if (!(exps = read_exps(sql, nrel, NULL, NULL, r, pos, '[', 0, 1)))
    2267             :                                 return NULL;
    2268           0 :                         rel = rel_sample(sql->sa, nrel, exps);
    2269           0 :                         set_processed(rel);
    2270          61 :                 } else if (r[*pos+2] == 'l') {
    2271          61 :                         *pos += (int) strlen("select");
    2272          61 :                         skipWS(r, pos);
    2273          61 :                         if (r[*pos] != '(')
    2274           0 :                                 return sql_error(sql, -1, SQLSTATE(42000) "Select: missing '('\n");
    2275          61 :                         (*pos)++;
    2276          61 :                         skipWS(r, pos);
    2277          61 :                         if (!(nrel = rel_read(sql, r, pos, refs)))
    2278             :                                 return NULL;
    2279          61 :                         skipWS(r, pos);
    2280          61 :                         if (r[*pos] != ')')
    2281           0 :                                 return sql_error(sql, -1, SQLSTATE(42000) "Select: missing ')'\n");
    2282          61 :                         (*pos)++;
    2283          61 :                         skipWS(r, pos);
    2284             : 
    2285          61 :                         if (!(exps = read_exps(sql, nrel, NULL, NULL, r, pos, '[', 0, 1)))
    2286             :                                 return NULL;
    2287          61 :                         rel = rel_select_copy(sql->sa, nrel, exps);
    2288          61 :                         set_processed(rel);
    2289             :                         /* semijoin or antijoin */
    2290           0 :                 } else if (r[*pos+1] == 'e' || r[*pos+1] == 'n') {
    2291           0 :                         if (r[*pos+1] == 'n') {
    2292           0 :                                 j = op_anti;
    2293           0 :                                 *pos += (int) strlen("antijoin");
    2294             :                         } else {
    2295           0 :                                 j = op_semi;
    2296           0 :                                 *pos += (int) strlen("semijoin");
    2297             :                         }
    2298             : 
    2299           0 :                         skipWS(r, pos);
    2300           0 :                         if (r[*pos] != '(')
    2301           0 :                                 return sql_error(sql, -1, SQLSTATE(42000) "%s: missing '('\n", (j == op_semi)?"Semijoin":"Antijoin");
    2302           0 :                         (*pos)++;
    2303           0 :                         skipWS(r, pos);
    2304           0 :                         if (!(lrel = rel_read(sql, r, pos, refs)))
    2305             :                                 return NULL;
    2306           0 :                         skipWS(r, pos);
    2307             : 
    2308           0 :                         if (r[*pos] != ',')
    2309           0 :                                 return sql_error(sql, -1, SQLSTATE(42000) "%s: missing ','\n", (j == op_semi)?"Semijoin":"Antijoin");
    2310           0 :                         (*pos)++;
    2311           0 :                         skipWS(r, pos);
    2312           0 :                         if (!(rrel = rel_read(sql, r, pos, refs)))
    2313             :                                 return NULL;
    2314             : 
    2315           0 :                         skipWS(r, pos);
    2316           0 :                         if (r[*pos] != ')')
    2317           0 :                                 return sql_error(sql, -1, SQLSTATE(42000) "%s: missing ')'\n", (j == op_semi)?"Semijoin":"Antijoin");
    2318           0 :                         (*pos)++;
    2319           0 :                         skipWS(r, pos);
    2320             : 
    2321           0 :                         if (!(exps = read_exps(sql, lrel, rrel, NULL, r, pos, '[', 0, 1)))
    2322             :                                 return NULL;
    2323           0 :                         rel = rel_crossproduct(sql->sa, lrel, rrel, j);
    2324           0 :                         rel->exps = exps;
    2325           0 :                         set_processed(rel);
    2326             :                 }
    2327             :                 break;
    2328          53 :         case 'g':
    2329          53 :                 *pos += (int) strlen("group");
    2330          53 :                 skipWS(r, pos);
    2331             : 
    2332          53 :                 if (r[*pos] == 'b') {
    2333          53 :                         *pos += (int) strlen("by");
    2334          53 :                         skipWS(r, pos);
    2335             : 
    2336          53 :                         if (r[*pos] != '(')
    2337           0 :                                 return sql_error(sql, -1, SQLSTATE(42000) "Group by: missing '('\n");
    2338          53 :                         (*pos)++;
    2339          53 :                         skipWS(r, pos);
    2340          53 :                         if (!(nrel = rel_read(sql, r, pos, refs)))
    2341             :                                 return NULL;
    2342          53 :                         skipWS(r, pos);
    2343          53 :                         if (r[*pos] != ')')
    2344           0 :                                 return sql_error(sql, -1, SQLSTATE(42000) "Group by: missing ')'\n");
    2345          53 :                         (*pos)++;
    2346          53 :                         skipWS(r, pos);
    2347             : 
    2348          53 :                         if (!(gexps = read_exps(sql, nrel, NULL, NULL, r, pos, '[', 0, 1)))
    2349             :                                 return NULL;
    2350          53 :                         skipWS(r, pos);
    2351          53 :                         rel = rel_groupby(sql, nrel, gexps);
    2352          53 :                         rel->exps = new_exp_list(sql->sa); /* empty projection list for now */
    2353          53 :                         set_processed(rel); /* don't search beyond the group by */
    2354             :                         /* first group projected expressions, then group by columns, then left relation projections */
    2355          53 :                         if (!(exps = read_exps(sql, rel, nrel, NULL, r, pos, '[', 1, 1)))
    2356             :                                 return NULL;
    2357          53 :                         rel->exps = exps;
    2358          53 :                         rel->nrcols = list_length(exps);
    2359          53 :                         break;
    2360             :                 } else {
    2361             :                         groupjoin = true;
    2362             :                 }
    2363             :                 /* fall through */
    2364           1 :         case 'l':
    2365           1 :                 if (strncmp(r+*pos, "left outer join", strlen("left outer join")) == 0) {
    2366           0 :                         *pos += (int) strlen("left outer join");
    2367             :                 } else {
    2368           1 :                         groupjoin = true;
    2369           1 :                         *pos += (int) strlen("left outer group join");
    2370             :                 }
    2371             :                 j = op_left;
    2372             :                 /* fall through */
    2373             :         case 'r':
    2374           0 :                 if (j == op_basetable) {
    2375           0 :                         *pos += (int) strlen("right outer join");
    2376           0 :                         j = op_right;
    2377             :                 }
    2378             :                 /* fall through */
    2379             :         case 'f':
    2380           0 :                 if (j == op_basetable) {
    2381           0 :                         *pos += (int) strlen("full outer join");
    2382           0 :                         j = op_full;
    2383             :                 }
    2384             :                 /* fall through */
    2385             :         case 'c':
    2386           0 :                 if (j == op_basetable) {
    2387           7 :                         *pos += (int) strlen("crossproduct");
    2388           7 :                         j = op_join;
    2389             :                 }
    2390             :                 /* fall through */
    2391             :         case 'j':
    2392           7 :                 if (j == op_basetable) {
    2393           1 :                         *pos += (int) strlen("join");
    2394           1 :                         j = op_join;
    2395             :                 }
    2396           9 :                 skipWS(r, pos);
    2397             : 
    2398           9 :                 if (r[*pos] != '(')
    2399           0 :                         return sql_error(sql, -1, SQLSTATE(42000) "Join: missing '('\n");
    2400           9 :                 (*pos)++;
    2401           9 :                 skipWS(r, pos);
    2402           9 :                 if (!(lrel = rel_read(sql, r, pos, refs)))
    2403             :                         return NULL;
    2404           9 :                 skipWS(r, pos);
    2405             : 
    2406           9 :                 if (r[*pos] != ',')
    2407           0 :                         return sql_error(sql, -1, SQLSTATE(42000) "Join: missing ','\n");
    2408           9 :                 (*pos)++;
    2409           9 :                 skipWS(r, pos);
    2410           9 :                 if (!(rrel = rel_read(sql, r, pos, refs)))
    2411             :                         return NULL;
    2412             : 
    2413           9 :                 skipWS(r, pos);
    2414           9 :                 if (r[*pos] != ')')
    2415           0 :                         return sql_error(sql, -1, SQLSTATE(42000) "Join: missing ')'\n");
    2416           9 :                 (*pos)++;
    2417           9 :                 skipWS(r, pos);
    2418             : 
    2419           9 :                 if (!(exps = read_exps(sql, lrel, rrel, NULL, r, pos, '[', 0, 1)))
    2420             :                         return NULL;
    2421           9 :                 rel = rel_crossproduct(sql->sa, lrel, rrel, j);
    2422           9 :                 rel->exps = exps;
    2423           9 :                 if (groupjoin) {
    2424           1 :                         list *attr = NULL;
    2425           1 :                         if (!(attr = read_exps(sql, lrel, rrel, NULL, r, pos, '[', 0, 1)))
    2426             :                                 return NULL;
    2427           1 :                         rel->attr = attr;
    2428             :                 }
    2429           9 :                 set_processed(rel);
    2430           9 :                 break;
    2431           0 :         case 'm':
    2432           0 :                 if (strcmp(r+*pos, "munion") == 0 && j == op_basetable) {
    2433           0 :                         *pos += (int) strlen("munion");
    2434           0 :                         j = op_munion;
    2435             :                 }
    2436             :                 /* fall through */
    2437             :         case 'u':
    2438           0 :                 if (j == op_basetable) {
    2439           0 :                         *pos += (int) strlen("union");
    2440           0 :                         j = op_union;
    2441             :                 }
    2442             :                 /* fall through */
    2443             :         case 'i':
    2444           0 :                 if (j == op_basetable) {
    2445           1 :                         *pos += (int) strlen("intersect");
    2446           1 :                         j = op_inter;
    2447             :                 }
    2448             :                 /* fall through */
    2449             :         case 'e':
    2450           1 :                 if (j == op_basetable) {
    2451           1 :                         *pos += (int) strlen("except");
    2452           1 :                         j = op_except;
    2453             :                 }
    2454           2 :                 skipWS(r, pos);
    2455             : 
    2456           2 :                 if (r[*pos] != '(')
    2457           0 :                         return sql_error(sql, -1, SQLSTATE(42000) "Setop: missing '('\n");
    2458           2 :                 (*pos)++;
    2459           2 :                 skipWS(r, pos);
    2460           2 :                 if (!(lrel = rel_read(sql, r, pos, refs)))
    2461             :                         return NULL;
    2462           2 :                 skipWS(r, pos);
    2463             : 
    2464           2 :                 if (j == op_munion) {
    2465           0 :                         rels = sa_list(sql->sa);
    2466           0 :                         append(rels, lrel);
    2467             : 
    2468           0 :                         while (r[*pos] == ',') {
    2469           0 :                                 (*pos)++;
    2470           0 :                                 skipWS(r, pos);
    2471           0 :                                 if (!(rrel = rel_read(sql, r, pos, refs)))
    2472             :                                         return NULL;
    2473           0 :                                 skipWS(r, pos);
    2474           0 :                                 append(rels, rrel);
    2475             :                         }
    2476             :                 } else {
    2477           2 :                         if (r[*pos] != ',')
    2478           0 :                                 return sql_error(sql, -1, SQLSTATE(42000) "Setop: missing ','\n");
    2479           2 :                         (*pos)++;
    2480           2 :                         skipWS(r, pos);
    2481           2 :                         if (!(rrel = rel_read(sql, r, pos, refs)))
    2482             :                                 return NULL;
    2483           2 :                         skipWS(r, pos);
    2484             :                 }
    2485             : 
    2486           2 :                 if (r[*pos] != ')')
    2487           0 :                         return sql_error(sql, -1, SQLSTATE(42000) "Setop: missing ')'\n");
    2488           2 :                 (*pos)++;
    2489           2 :                 skipWS(r, pos);
    2490             : 
    2491           2 :                 if (!(exps = read_exps(sql, lrel, rrel, NULL, r, pos, '[', 0, 1)))
    2492             :                         return NULL;
    2493           2 :                 if (j == op_munion) {
    2494           0 :                         rel = rel_setop_n_ary(sql->sa, rels, j);
    2495           0 :                         rel_setop_n_ary_set_exps(sql, rel, exps, false);
    2496           0 :                         if (rel_set_types_n_ary(sql, rel) < 0)
    2497           0 :                                 return sql_error(sql, -1, SQLSTATE(42000) "Setop: number of expressions don't match\n");
    2498             :                 } else {
    2499           2 :                         rel = rel_setop(sql->sa, lrel, rrel, j);
    2500           2 :                         rel_setop_set_exps(sql, rel, exps, false);
    2501           2 :                         if (rel_set_types(sql, rel) < 0)
    2502           0 :                                 return sql_error(sql, -1, SQLSTATE(42000) "Setop: number of expressions don't match\n");
    2503             :                 }
    2504           2 :                 set_processed(rel);
    2505           2 :                 break;
    2506           1 :         case '[': /* projection of list of values */
    2507           1 :                 if (!(exps = read_exps(sql, NULL, NULL, NULL, r, pos, '[', 0, 1)))
    2508             :                         return NULL;
    2509           1 :                 rel = rel_project(sql->sa, NULL, exps);
    2510             :                 /* order by ? */
    2511           1 :                 if (r[*pos] == '[' && !(rel->r = read_exps(sql, NULL, rel, NULL, r, pos, '[', 0, 1)))
    2512             :                         return NULL;
    2513           1 :                 set_processed(rel);
    2514           1 :                 break;
    2515           0 :         case 'd':
    2516             :                 /* 'ddl' not supported */
    2517             :         default:
    2518           0 :                 return sql_error(sql, -1, SQLSTATE(42000) "Could not determine the input relation\n");
    2519             :         }
    2520             : 
    2521         326 :         if (!rel)
    2522           0 :                 return sql_error(sql, -1, SQLSTATE(42000) "Could not determine the input relation\n");
    2523         595 :         if (distinct)
    2524           0 :                 set_distinct(rel);
    2525         595 :         if (single)
    2526           0 :                 set_single(rel);
    2527         595 :         if (dependent)
    2528           0 :                 set_dependent(rel);
    2529             : 
    2530             :         /* sometimes, properties are sent */
    2531         595 :         if (!(rel = read_rel_properties(sql, rel, r, pos)))
    2532             :                 return NULL;
    2533             :         return rel;
    2534             : }
    2535             : 
    2536             : static bool
    2537           2 : is_infix(sql_func *f)
    2538             : {
    2539           2 :         if (strlen(f->base.name) == 1) {
    2540             :                 return true;
    2541           0 :         } else if (strlen(f->base.name) == 2) {
    2542           0 :                 if (f->base.name[0] == '<' && f->base.name[1] == '>')
    2543             :                         return true;
    2544           0 :                 if (f->base.name[0] == '<' && f->base.name[1] == '=')
    2545             :                         return true;
    2546           0 :                 if (f->base.name[0] == '>' && f->base.name[1] == '=')
    2547             :                         return true;
    2548           0 :                 if (f->base.name[0] == '|' && f->base.name[1] == '|')
    2549             :                         return true;
    2550           0 :                 if (f->base.name[0] == 'o' && f->base.name[1] == 'r')
    2551           0 :                         return true;
    2552           0 :         } else if (strlen(f->base.name) == 3) {
    2553           0 :                 if (f->base.name[0] == 'a' && f->base.name[1] == 'n' && f->base.name[2] == 'd')
    2554           0 :                         return true;
    2555             :         }
    2556             :         return false;
    2557             : }
    2558             : 
    2559             : static void
    2560           2 : exp2sql_dquoted(stream *fout, const char *pref, const char *val, const char *suff)
    2561             : {
    2562           2 :         if (pref)
    2563           0 :                 mnstr_printf(fout, "%s", pref);
    2564           2 :         mnstr_write(fout, "\"", 1, 1);
    2565           2 :         while (*val) {
    2566           2 :                 const char *p = strchr(val, '"');
    2567           2 :                 if (p) {
    2568           0 :                         if (p > val)
    2569           0 :                                 mnstr_write(fout, val, 1, p - val);
    2570           0 :                         mnstr_write(fout, "\"\"", 1, 2);
    2571           0 :                         val = p + 1;
    2572             :                 } else {
    2573           2 :                         mnstr_printf(fout, "%s", val);
    2574           2 :                         break;
    2575             :                 }
    2576             :         }
    2577           2 :         mnstr_write(fout, "\"", 1, 1);
    2578           2 :         if (suff)
    2579           0 :                 mnstr_printf(fout, "%s", suff);
    2580           2 : }
    2581             : 
    2582             : /* only simple expressions, ie recursive no psm */
    2583             : static void
    2584           6 : exp2sql_print(mvc *sql, stream *fout, sql_exp *e, int depth)
    2585             : {
    2586           6 :         switch (e->type) {
    2587           2 :                 case e_func: {
    2588           2 :                         sql_subfunc *sf = e->f;
    2589           2 :                         list *args = e->l;
    2590           2 :                         if (list_length(args) == 2 && is_infix(sf->func)) {
    2591           2 :                                 if (depth)
    2592           0 :                                         mnstr_printf(fout, "( " );
    2593           2 :                                 exp2sql_print(sql, fout, args->h->data, depth+1);
    2594           2 :                                 mnstr_printf(fout, " %s ", sf->func->base.name);
    2595           2 :                                 exp2sql_print(sql, fout, args->h->next->data, depth+1);
    2596           2 :                                 if (depth)
    2597           0 :                                         mnstr_printf(fout, " )" );
    2598             :                         } else {
    2599           0 :                                 exp2sql_dquoted(fout, NULL, sf->func->base.name, "(");
    2600           0 :                                 if (args)
    2601           0 :                                         for (node *n = args->h; n; n = n->next) {
    2602           0 :                                                 exp2sql_print(sql, fout, n->data, depth+1);
    2603           0 :                                                 if (n->next)
    2604           0 :                                                         mnstr_printf(fout, ", ");
    2605             :                                         }
    2606           0 :                                 mnstr_printf(fout, ")");
    2607             :                         }
    2608             :                 }       break;
    2609           2 :                 case e_column:
    2610           2 :                         exp2sql_dquoted(fout, NULL, exp_name(e), NULL);
    2611           2 :                         break;
    2612           0 :                 case e_convert:
    2613           0 :                         mnstr_printf(fout, "CAST (" );
    2614           0 :                         exp2sql_print(sql, fout, e->l, depth+1);
    2615           0 :                         mnstr_printf(fout, "AS %s)", sql_subtype_string(sql->sa, exp_subtype(e)));
    2616           0 :                         break;
    2617           2 :                 case e_atom:
    2618           2 :                         mnstr_printf(fout, "%s", atom2sql(sql->sa, e->l, 0));
    2619           2 :                         break;
    2620             :                 case e_aggr: /* fall-through */
    2621             :                 case e_cmp: /* fall-through */
    2622             :                 case e_psm:
    2623           0 :                         assert(0);
    2624             :                         break;
    2625             :         }
    2626           6 : }
    2627             : 
    2628             : char *
    2629           2 : exp2sql( mvc *sql, sql_exp *exp)
    2630             : {
    2631           2 :         buffer *b = NULL;
    2632           2 :         stream *s = NULL;
    2633           2 :         char *res = NULL;
    2634             : 
    2635           2 :         b = buffer_create(1024);
    2636           2 :         if(b == NULL)
    2637           0 :                 goto cleanup;
    2638           2 :         s = buffer_wastream(b, "exp_dump");
    2639           2 :         if(s == NULL)
    2640           0 :                 goto cleanup;
    2641             : 
    2642           2 :         exp2sql_print(sql, s, exp, 0);
    2643             : 
    2644           2 :         res = buffer_get_buf(b);
    2645             : 
    2646           2 : cleanup:
    2647           2 :         if(b)
    2648           2 :                 buffer_destroy(b);
    2649           2 :         if(s)
    2650           2 :                 close_stream(s);
    2651             : 
    2652           2 :         char* fres = SA_STRDUP(sql->sa, res);
    2653           2 :         free (res);
    2654           2 :         return fres;
    2655             : }

Generated by: LCOV version 1.14