LCOV - code coverage report
Current view: top level - sql/common - sql_types.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 806 823 97.9 %
Date: 2021-10-13 02:24:04 Functions: 38 39 97.4 %

          Line data    Source code
       1             : /*
       2             :  * This Source Code Form is subject to the terms of the Mozilla Public
       3             :  * License, v. 2.0.  If a copy of the MPL was not distributed with this
       4             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       5             :  *
       6             :  * Copyright 1997 - July 2008 CWI, August 2008 - 2021 MonetDB B.V.
       7             :  */
       8             : 
       9             : /*
      10             :  * The typing scheme of SQL is quite elaborate. The standard introduces
      11             :  * several basic types with a plethora of functions.
      12             :  * As long as we haven't implemented a scheme to accept the
      13             :  * function type signature and relate it to a C-function linked
      14             :  * with the system, we have to patch the code below.
      15             :  *
      16             :  * Given the large number of examples, it should be relatively
      17             :  * easy to find something akin you intend to enter.
      18             :  */
      19             : 
      20             : #include "monetdb_config.h"
      21             : #include "sql_types.h"
      22             : #include "sql_keyword.h"      /* for keyword_exists(), keywords_insert(), init_keywords(), exit_keywords() */
      23             : #include "gdk_geomlogger.h"   /* for geomcatalogfix_get */
      24             : 
      25             : list *aliases = NULL;
      26             : list *types = NULL;
      27             : list *funcs = NULL;
      28             : 
      29             : static sql_type *BIT = NULL;
      30             : static list *localtypes = NULL;
      31             : 
      32      889139 : unsigned int digits2bits(unsigned int digits)
      33             : {
      34      889139 :         if (digits < 3)
      35             :                 return 8;
      36      330353 :         else if (digits < 5)
      37             :                 return 16;
      38       62811 :         else if (digits <= 5)
      39             :                 return 17;
      40        2378 :         else if (digits <= 6)
      41             :                 return 20;
      42        2191 :         else if (digits <= 7)
      43             :                 return 24;
      44        2065 :         else if (digits <= 8)
      45             :                 return 27;
      46        1262 :         else if (digits < 10)
      47             :                 return 32;
      48         871 :         else if (digits < 17)
      49             :                 return 51;
      50             : #ifdef HAVE_HGE
      51         214 :         else if (digits < 19)
      52         105 :                 return 64;
      53             :         return 128;
      54             : #else
      55             :         return 64;
      56             : #endif
      57             : }
      58             : 
      59        9358 : unsigned int bits2digits(unsigned int bits)
      60             : {
      61        9358 :         if (bits < 4)
      62             :                 return 1;
      63        8249 :         else if (bits < 7)
      64             :                 return 2;
      65        8190 :         else if (bits < 10)
      66             :                 return 3;
      67        7857 :         else if (bits < 14)
      68             :                 return 4;
      69        7837 :         else if (bits < 16)
      70             :                 return 5;
      71        7810 :         else if (bits < 20)
      72             :                 return 6;
      73        7752 :         else if (bits < 24)
      74             :                 return 7;
      75        7744 :         else if (bits <= 27)
      76             :                 return 8;
      77        7731 :         else if (bits <= 30)
      78             :                 return 9;
      79        7682 :         else if (bits <= 32)
      80             :                 return 10;
      81             : #ifdef HAVE_HGE
      82        4564 :         else if (bits <= 64)
      83        4524 :                 return 19;
      84             :         return 39;
      85             : #else
      86             :         return 19;
      87             : #endif
      88             : }
      89             : 
      90      514620 : unsigned int type_digits_to_char_digits(sql_subtype *t)
      91             : {
      92      514620 :         if (!t)
      93             :                 return 0;
      94      514620 :         switch (t->type->eclass) {
      95             :                 case EC_BIT:
      96             :                         return 1;
      97        3389 :                 case EC_POS:
      98             :                 case EC_NUM:
      99             :                 case EC_MONTH:
     100        3389 :                         return bits2digits(t->digits) + 1; /* add '-' */
     101         421 :                 case EC_FLT:
     102         421 :                         return bits2digits(t->digits) + 2; /* TODO for floating-points maybe more is needed */
     103         446 :                 case EC_DEC:
     104             :                 case EC_SEC:
     105         446 :                         return t->digits + 2; /* add '-' and '.' */
     106           9 :                 case EC_TIMESTAMP:
     107             :                 case EC_TIMESTAMP_TZ:
     108           9 :                         return 40; /* TODO this needs more tunning */
     109           0 :                 case EC_TIME:
     110             :                 case EC_TIME_TZ:
     111           0 :                         return 20; /* TODO this needs more tunning */
     112           0 :                 case EC_DATE:
     113           0 :                         return 20; /* TODO this needs more tunning */
     114           4 :                 case EC_BLOB:
     115           4 :                         return t->digits * 2; /* TODO BLOBs don't have digits, so this is wrong */
     116      509462 :                 default:
     117      509462 :                         return t->digits; /* What to do with EC_GEOM? */
     118             :         }
     119             : }
     120             : 
     121             : /* 0 cannot convert */
     122             : /* 1 set operations have very limited coersion rules */
     123             : /* 2 automatic coersion (could still require dynamic checks for overflow) */
     124             : /* 3 casts are allowed (requires dynamic checks) (sofar not used) */
     125             : static int convert_matrix[EC_MAX][EC_MAX] = {
     126             : 
     127             : /* EC_ANY */            { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, /* NULL */
     128             : /* EC_TABLE */          { 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
     129             : /* EC_BIT */            { 0, 0, 1, 1, 1, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
     130             : /* EC_CHAR */           { 2, 2, 2, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 },
     131             : /* EC_STRING */         { 2, 2, 2, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 },
     132             : /* EC_BLOB */           { 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
     133             : /* EC_POS */            { 0, 0, 2, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0 },
     134             : /* EC_NUM */            { 0, 0, 2, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0 },
     135             : /* EC_MONTH*/           { 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
     136             : /* EC_SEC*/                     { 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0 },
     137             : /* EC_DEC */            { 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0 },
     138             : /* EC_FLT */            { 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0 },
     139             : /* EC_TIME */           { 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 2, 0, 0, 0, 0, 0 },
     140             : /* EC_TIME_TZ */        { 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 },
     141             : /* EC_DATE */           { 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 0, 0 },
     142             : /* EC_TSTAMP */         { 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 0, 0 },
     143             : /* EC_TSTAMP_TZ */      { 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0 },
     144             : /* EC_GEOM */           { 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0 },
     145             : /* EC_EXTERNAL*/        { 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
     146             : };
     147             : 
     148      420229 : int sql_type_convert (int from, int to)
     149             : {
     150      420229 :         return convert_matrix[from][to];
     151             : }
     152             : 
     153      882740 : bool is_commutative(const char *sname, const char *fnm)
     154             : {
     155      882740 :         return (!sname || strcmp("sys", sname) == 0) && (strcmp("sql_add", fnm) == 0 || strcmp("sql_mul", fnm) == 0 || strcmp("scale_up", fnm) == 0);
     156             : }
     157             : 
     158             : void
     159     1065687 : base_init(sql_allocator *sa, sql_base * b, sqlid id, bool isnew, const char *name)
     160             : {
     161     1065687 :         *b = (sql_base) {
     162             :                 .id = id,
     163             :                 .new = isnew,
     164             :                 .refcnt = 1,
     165     1065687 :                 .name = (name) ? SA_STRDUP(sa, name) : NULL,
     166             :         };
     167     1065687 : }
     168             : 
     169             : void
     170     7497580 : sql_init_subtype(sql_subtype *res, sql_type *t, unsigned int digits, unsigned int scale)
     171             : {
     172     7497580 :         res->type = t;
     173     7497580 :         res->digits = digits ? digits : t->digits;
     174     7497580 :         if (t->digits && res->digits > t->digits)
     175        4324 :                 res->digits = t->digits;
     176     7497580 :         res->scale = scale;
     177     7497580 : }
     178             : 
     179             : sql_subtype *
     180     2386180 : sql_create_subtype(sql_allocator *sa, sql_type *t, unsigned int digits, unsigned int scale)
     181             : {
     182     2386180 :         sql_subtype *res = SA_ZNEW(sa, sql_subtype);
     183             : 
     184     2386180 :         sql_init_subtype(res, t, digits, scale);
     185     2386180 :         return res;
     186             : }
     187             : 
     188             : static bool
     189     2364774 : localtypes_cmp(int nlt, int olt)
     190             : {
     191     2364774 :         if (nlt == TYPE_flt || nlt == TYPE_dbl) {
     192             :                 nlt = TYPE_dbl;
     193             : #ifdef HAVE_HGE
     194     2364774 :         } else if (nlt == TYPE_bte || nlt == TYPE_sht || nlt == TYPE_int || nlt == TYPE_lng || nlt == TYPE_hge) {
     195             :                 nlt = TYPE_hge;
     196             : #else
     197             :         } else if (nlt == TYPE_bte || nlt == TYPE_sht || nlt == TYPE_int || nlt == TYPE_lng) {
     198             :                 nlt = TYPE_lng;
     199             : #endif
     200             :         }
     201     2364774 :         return nlt == olt;
     202             : }
     203             : 
     204             : sql_subtype *
     205      214968 : sql_find_numeric(sql_subtype *r, int localtype, unsigned int digits)
     206             : {
     207             :         node *m, *n;
     208             : 
     209      214968 :         if (localtype == TYPE_flt || localtype == TYPE_dbl) {
     210             :                 localtype = TYPE_dbl;
     211             :         } else {
     212             : #ifdef HAVE_HGE
     213             :                 localtype = TYPE_hge;
     214             :                 if (digits >= 128)
     215             :                         digits = 127;
     216             : #else
     217             :                 localtype = TYPE_lng;
     218             :                 if (digits >= 64)
     219             :                         digits = 63;
     220             : #endif
     221             :         }
     222             : 
     223     1934712 :         for (n = types->h; n; n = n->next) {
     224     1934712 :                 sql_type *t = n->data;
     225             : 
     226     1934712 :                 if (localtypes_cmp(t->localtype, localtype)) {
     227      214968 :                         if (digits == 0 ? t->digits == 0 : t->digits > digits) {
     228       62939 :                                 sql_init_subtype(r, t, digits, 0);
     229       62939 :                                 return r;
     230             :                         }
     231      430062 :                         for (m = n->next; m; m = m->next) {
     232      430062 :                                 t = m->data;
     233      430062 :                                 if (!localtypes_cmp(t->localtype, localtype)) {
     234             :                                         break;
     235             :                                 }
     236             :                                 n = m;
     237      430062 :                                 if (digits == 0 ? t->digits == 0 : t->digits > digits) {
     238      152029 :                                         sql_init_subtype(r, t, digits, 0);
     239      152029 :                                         return r;
     240             :                                 }
     241             :                         }
     242             :                 }
     243             :         }
     244             :         return NULL;
     245             : }
     246             : 
     247             : sql_subtype *
     248          17 : arg_type( sql_arg *a)
     249             : {
     250          17 :         return &a->type;
     251             : }
     252             : 
     253             : int
     254     4854152 : sql_find_subtype(sql_subtype *res, const char *name, unsigned int digits, unsigned int scale)
     255             : {
     256             :         /* todo add approximate info
     257             :          * if digits/scale == 0 and no approximate with digits/scale == 0
     258             :          * exists we could return the type with largest digits
     259             :          *
     260             :          * returning the largest when no exact match is found is now the
     261             :          * (wrong?) default
     262             :          */
     263             :         /* assumes the types are ordered on name,digits,scale where is always
     264             :          * 0 > n
     265             :          */
     266             :         node *m, *n;
     267             : 
     268    44947989 :         for (n = types->h; n; n = n->next) {
     269    44942841 :                 sql_type *t = n->data;
     270             : 
     271    44942841 :                 if (t->base.name[0] == name[0] && strcmp(t->base.name, name) == 0) {
     272     4849004 :                         if ((digits && t->digits >= digits) || (digits == t->digits)) {
     273     2937297 :                                 sql_init_subtype(res, t, digits, scale);
     274     2937297 :                                 return 1;
     275             :                         }
     276     1950026 :                         for (m = n->next; m; m = m->next) {
     277     1950026 :                                 t = m->data;
     278     1950026 :                                 if (strcmp(t->base.name, name) != 0) {
     279             :                                         break;
     280             :                                 }
     281             :                                 n = m;
     282       54863 :                                 if ((digits && t->digits >= digits) || (digits == t->digits)) {
     283       16544 :                                         sql_init_subtype(res, t, digits, scale);
     284       16544 :                                         return 1;
     285             :                                 }
     286             :                         }
     287     1895163 :                         t = n->data;
     288     1895163 :                         sql_init_subtype(res, t, digits, scale);
     289     1895163 :                         return 1;
     290             :                 }
     291             :         }
     292             :         return 0;
     293             : }
     294             : 
     295             : sql_subtype *
     296         580 : sql_bind_subtype(sql_allocator *sa, const char *name, unsigned int digits, unsigned int scale)
     297             : {
     298         580 :         sql_subtype *res = (sa)?SA_ZNEW(sa, sql_subtype):ZNEW(sql_subtype);
     299             : 
     300         580 :         if (!sql_find_subtype(res, name, digits, scale)) {
     301           0 :                 return NULL;
     302             :         }
     303             :         return res;
     304             : }
     305             : 
     306             : sql_subtype *
     307     1158502 : sql_bind_localtype(const char *name)
     308             : {
     309     1158502 :         node *n = localtypes->h;
     310             : 
     311     7065666 :         while (n) {
     312     7065666 :                 sql_subtype *t = n->data;
     313             : 
     314     7065666 :                 if (strcmp(t->type->impl, name) == 0) {
     315             :                         return t;
     316             :                 }
     317     5907164 :                 n = n->next;
     318             :         }
     319           0 :         assert(0);
     320             :         return NULL;
     321             : }
     322             : 
     323             : int
     324     9846907 : type_cmp(sql_type *t1, sql_type *t2)
     325             : {
     326             :         int res = 0;
     327             : 
     328     9846907 :         if (!t1 || !t2)
     329             :                 return -1;
     330             :         /* types are only equal
     331             :            iff they map onto the same systemtype */
     332     9846907 :         res = (t1->localtype - t2->localtype);
     333     9846907 :         if (res)
     334             :                 return res;
     335             : 
     336             :         /* iff they fall into the same equivalence class */
     337     4379950 :         res = (t1->eclass - t2->eclass);
     338     4379950 :         if (res)
     339             :                 return res;
     340             : 
     341             :         /* external types with the same system type are treated equaly */
     342     4078463 :         if (t1->eclass == EC_EXTERNAL)
     343             :                 return res;
     344             : 
     345             :         /* sql base types need the same 'sql' name */
     346     4050722 :         return (strcmp(t1->base.name, t2->base.name));
     347             : }
     348             : 
     349             : int
     350     4910529 : subtype_cmp(sql_subtype *t1, sql_subtype *t2)
     351             : {
     352     4910529 :         if (!t1->type || !t2->type)
     353             :                 return -1;
     354             : 
     355     4910529 :         if (t1->type->eclass == t2->type->eclass && t1->type->eclass == EC_SEC)
     356             :                 return 0;
     357     4909886 :         if (t1->type->eclass == t2->type->eclass && t1->type->eclass == EC_MONTH)
     358             :                 return 0;
     359     4909426 :         if ( !(t1->type->eclass == t2->type->eclass &&
     360     4405811 :               (EC_INTERVAL(t1->type->eclass) || t1->type->eclass == EC_NUM)) &&
     361     1938495 :               (t1->digits != t2->digits ||
     362     1087198 :               (!(t1->type->eclass == t2->type->eclass &&
     363     1150260 :                t1->type->eclass == EC_FLT) &&
     364     1150260 :                t1->scale != t2->scale)) )
     365             :                 return -1;
     366             : 
     367             :         /* subtypes are only equal iff
     368             :            they map onto the same systemtype */
     369     4135673 :         return (type_cmp(t1->type, t2->type));
     370             : }
     371             : 
     372             : int
     373    13556574 : is_subtype(sql_subtype *sub, sql_subtype *super)
     374             : /* returns true if sub is a sub type of super */
     375             : {
     376    13556574 :         if (!sub || !super)
     377             :                 return 0;
     378    13556567 :         if (super->digits > 0 && sub->digits > super->digits)
     379             :                 return 0;
     380             :         /* while binding a function, 'char' types match each other */
     381     6670512 :         if (super->digits == 0 && 
     382     2080497 :                 ((super->type->eclass == EC_STRING && EC_VARCHAR(sub->type->eclass)) ||
     383      263333 :                  (super->type->eclass == EC_CHAR && sub->type->eclass == EC_CHAR)))
     384             :                 return 1;
     385     5705665 :         if (super->digits != sub->digits && sub->type->eclass == EC_CHAR)
     386             :                 return 0;
     387             :         /* subtypes are only equal iff
     388             :            they map onto the same systemtype */
     389     5703528 :         return (type_cmp(sub->type, super->type) == 0);
     390             : }
     391             : 
     392             : char *
     393       13304 : sql_subtype_string(sql_allocator *sa, sql_subtype *t)
     394             : {
     395             :         char buf[BUFSIZ];
     396             : 
     397       13304 :         if (t->digits && t->scale)
     398         335 :                 snprintf(buf, BUFSIZ, "%s(%u,%u)", t->type->base.name, t->digits, t->scale);
     399       12969 :         else if (t->digits && t->type->radix != 2)
     400        2105 :                 snprintf(buf, BUFSIZ, "%s(%u)", t->type->base.name, t->digits);
     401             :         else
     402       10864 :                 snprintf(buf, BUFSIZ, "%s", t->type->base.name);
     403       13304 :         return sa_strdup(sa, buf);
     404             : }
     405             : 
     406             : char *
     407           7 : subtype2string2(sql_allocator *sa, sql_subtype *tpe) /* distinguish char(n), decimal(n,m) from other SQL types */
     408             : {
     409             :         char buf[BUFSIZ];
     410             : 
     411           7 :         switch (tpe->type->eclass) {
     412           0 :                 case EC_SEC:
     413           0 :                         snprintf(buf, BUFSIZ, "INTERVAL SECOND");
     414           0 :                         break;
     415           0 :                 case EC_MONTH:
     416           0 :                         snprintf(buf, BUFSIZ, "INTERVAL MONTH");
     417           0 :                         break;
     418           3 :                 case EC_CHAR:
     419             :                 case EC_STRING:
     420             :                 case EC_DEC:
     421           3 :                         return sql_subtype_string(sa, tpe);
     422           4 :                 default:
     423           4 :                         snprintf(buf, BUFSIZ, "%s", tpe->type->base.name);
     424             :         }
     425           4 :         return sa_strdup(sa, buf);
     426             : }
     427             : 
     428             : int
     429      471096 : subfunc_cmp( sql_subfunc *f1, sql_subfunc *f2)
     430             : {
     431      471096 :         if (f1->func == f2->func)
     432      184447 :                 return list_cmp(f1->res, f2->res, (fcmp) &subtype_cmp);
     433             :         return -1;
     434             : }
     435             : 
     436             : int
     437    14361838 : arg_subtype_cmp(sql_arg *a, sql_subtype *t)
     438             : {
     439    14361838 :         if (a->type.type->eclass == EC_ANY)
     440             :                 return 0;
     441    13511001 :         return (is_subtype(t, &a->type )?0:-1);
     442             : }
     443             : 
     444             : char *
     445      282309 : sql_func_imp(sql_func *f)
     446             : {
     447      282309 :         if (f->sql)
     448        1131 :                 return f->base.name;
     449             :         else
     450      281178 :                 return f->imp;
     451             : }
     452             : 
     453             : char *
     454      868388 : sql_func_mod(sql_func *f)
     455             : {
     456      868388 :         if (!f->mod)
     457           0 :                 return "";
     458             :         return f->mod;
     459             : }
     460             : 
     461             : int
     462           0 : is_sqlfunc(sql_func *f)
     463             : {
     464           0 :         return f->sql;
     465             : }
     466             : 
     467             : sql_subfunc*
     468     1171326 : sql_dup_subfunc(sql_allocator *sa, sql_func *f, list *ops, sql_subtype *member)
     469             : {
     470             :         node *tn;
     471             :         unsigned int scale = 0, digits = 0;
     472     1171326 :         sql_subfunc *fres = SA_ZNEW(sa, sql_subfunc);
     473             : 
     474     1171326 :         fres->func = f;
     475     1171326 :         if (IS_FILT(f)) {
     476         837 :                 fres->res = sa_list(sa);
     477         837 :                 list_append(fres->res, sql_bind_localtype("bit"));
     478     1170489 :         } else if (IS_FUNC(f) || IS_UNION(f) || IS_ANALYTIC(f) || IS_AGGR(f)) { /* not needed for PROC */
     479             :                 unsigned int mscale = 0, mdigits = 0;
     480             : 
     481     1141796 :                 if (ops) {
     482     1016284 :                         if (ops->h && ops->h->data && f->imp &&
     483      988164 :                             strcmp(f->imp, "round") == 0) {
     484             :                                 /* special case for round(): result is
     485             :                                  * same type as first argument */
     486             :                                 sql_subtype *a = ops->h->data;
     487          77 :                                 mscale = a->scale;
     488          77 :                                 mdigits = a->digits;
     489             :                         } else {
     490     2856947 :                                 for (tn = ops->h; tn; tn = tn->next) {
     491     1840740 :                                         sql_subtype *a = tn->data;
     492             : 
     493             :                                         /* same scale as the input */
     494     1840740 :                                         if (a && a->scale > mscale)
     495             :                                                 mscale = a->scale;
     496     1840740 :                                         if (a && f->fix_scale == INOUT && tn == ops->h)
     497        2815 :                                                 mdigits = a->digits;
     498             :                                 }
     499             :                         }
     500             :                 }
     501             : 
     502     1141796 :                 if (!member) {
     503             :                         node *m;
     504             :                         sql_arg *ma = NULL;
     505             : 
     506     2965569 :                         if (ops) for (tn = ops->h, m = f->ops->h; tn; tn = tn->next, m = m->next) {
     507     1840894 :                                 sql_arg *s = m->data;
     508             : 
     509     1840894 :                                 if (!member && s->type.type->eclass == EC_ANY) {
     510      397622 :                                         member = tn->data;
     511             :                                         ma = s;
     512             :                                 }
     513             :                                 /* largest type */
     514     1840894 :                                 if (member && s->type.type->eclass == EC_ANY &&
     515      714411 :                                     s->type.type->localtype > ma->type.type->localtype ) {
     516           0 :                                         member = tn->data;
     517             :                                         ma = s;
     518             :                                 }
     519             :                         }
     520             :                 }
     521             : 
     522     1141796 :                 if (f->res) {
     523     1141796 :                         fres->res = sa_list(sa);
     524     2379215 :                         for(tn = f->res->h; tn; tn = tn->next) {
     525     1237419 :                                 sql_arg *rarg = tn->data;
     526     1237419 :                                 sql_subtype *res, *r = &rarg->type;
     527             : 
     528             :                                 /* same scale as the input */
     529     1237419 :                                 if (member && member->scale > scale)
     530             :                                 /* same scale as the input if result has a scale */
     531        1098 :                                 if (member && (r->type->eclass == EC_ANY || r->type->scale != SCALE_NONE) && member->scale > scale)
     532             :                                         scale = member->scale;
     533     1237419 :                                 digits = r->digits;
     534     1237419 :                                 if (!member) {
     535      822676 :                                         if (f->fix_scale > SCALE_NONE && f->fix_scale < SCALE_EQ) {
     536             :                                                 scale = mscale;
     537             :                                                 digits = mdigits;
     538      298267 :                                         } else if (r->scale)
     539             :                                                 scale = r->scale;
     540             :                                 }
     541     1237419 :                                 if (member && (f->fix_scale == INOUT || r->type->eclass == EC_ANY))
     542       30654 :                                         digits = member->digits;
     543     1237419 :                                 if (IS_ANALYTIC(f) && mscale)
     544             :                                         scale = mscale;
     545     1237419 :                                 if (member && r->type->eclass == EC_ANY)
     546             :                                         r = member;
     547     1237419 :                                 res = sql_create_subtype(sa, r->type, digits, scale);
     548     1237419 :                                 list_append(fres->res, res);
     549             :                         }
     550             :                 }
     551     1141796 :                 if (member) { /* check that the types of all EC_ANY's are equal */
     552             :                         sql_subtype *st = NULL;
     553             :                         node *m;
     554             : 
     555     1164576 :                         if (ops) for (tn = ops->h, m = f->ops->h; tn; tn = tn->next, m = m->next) {
     556      765041 :                                 sql_arg *s = m->data;
     557      765041 :                                 sql_subtype *opt = tn->data;
     558             : 
     559      765041 :                                 if (s->type.type->eclass == EC_ANY) {
     560      714411 :                                         if (!st || st->type->eclass == EC_ANY) /* if input parameter is ANY, skip validation */
     561             :                                                 st = tn->data;
     562      316775 :                                         else if (opt && subtype_cmp(st, opt))
     563             :                                                 return NULL;
     564             :                                 }
     565             :                         }
     566             :                 }
     567             :         }
     568             :         return fres;
     569             : }
     570             : 
     571             : 
     572             : static void
     573         265 : sql_create_alias(sql_allocator *sa, const char *name, const char *alias)
     574             : {
     575         265 :         sql_alias *a = SA_ZNEW(sa, sql_alias);
     576             : 
     577         265 :         if(a) {
     578         265 :                 a->name = sa_strdup(sa, name);
     579         265 :                 a->alias = sa_strdup(sa, alias);
     580         265 :                 list_append(aliases, a);
     581         265 :                 if (!keyword_exists(a->alias) )
     582         265 :                         (void) keywords_insert(a->alias, KW_ALIAS);
     583             :         }
     584         265 : }
     585             : 
     586             : char *
     587        3588 : sql_bind_alias(const char *alias)
     588             : {
     589             :         node *n;
     590             : 
     591        3588 :         for (n = aliases->h; n; n = n->next) {
     592        3588 :                 sql_alias *a = n->data;
     593             : 
     594        3588 :                 if (strcmp(a->alias, alias) == 0) {
     595        3588 :                         return a->name;
     596             :                 }
     597             :         }
     598             :         return NULL;
     599             : }
     600             : 
     601             : static sqlid local_id = 1;
     602             : 
     603             : static sql_type *
     604        8444 : sql_create_type(sql_allocator *sa, const char *sqlname, unsigned int digits, unsigned int scale, unsigned char radix, sql_class eclass, const char *impl)
     605             : {
     606        8444 :         sql_type *t = SA_ZNEW(sa, sql_type);
     607             : 
     608        8444 :         base_init(sa, &t->base, local_id++, false, sqlname);
     609        8444 :         t->impl = sa_strdup(sa, impl);
     610        8444 :         t->digits = digits;
     611        8444 :         t->scale = scale;
     612        8444 :         t->localtype = ATOMindex(t->impl);
     613        8444 :         t->radix = radix;
     614        8444 :         t->eclass = eclass;
     615        8444 :         t->s = NULL;
     616        8444 :         if (!keyword_exists(t->base.name) && !EC_INTERVAL(eclass))
     617        1831 :                 (void) keywords_insert(t->base.name, KW_TYPE);
     618        8444 :         list_append(types, t);
     619             : 
     620        8444 :         list_append(localtypes, sql_create_subtype(sa, t, 0, 0));
     621             : 
     622        8444 :         return t;
     623             : }
     624             : 
     625             : static sql_arg *
     626     1026594 : create_arg(sql_allocator *sa, const char *name, sql_subtype *t, char inout)
     627             : {
     628     1026594 :         sql_arg *a = (sa)?SA_ZNEW(sa, sql_arg):ZNEW(sql_arg);
     629             : 
     630     1026594 :         if(a) {
     631     1026594 :                 a->name = name?sa_strdup(sa, name):NULL;
     632     1026594 :                 a->type = *t;
     633     1026594 :                 a->inout = inout;
     634             :         }
     635     1026594 :         return a;
     636             : }
     637             : 
     638             : sql_arg *
     639      222869 : sql_create_arg(sql_allocator *sa, const char *name, sql_subtype *t, char inout)
     640             : {
     641      222869 :         return create_arg(sa, name, t, inout);
     642             : }
     643             : 
     644             : static sql_func *
     645      282130 : sql_create_func_(sql_allocator *sa, const char *name, const char *mod, const char *imp, sql_ftype type, bit semantics, bit side_effect,
     646             :                                  int fix_scale, unsigned int res_scale, sql_type *res, int nargs, va_list valist)
     647             : {
     648      282130 :         list *ops = SA_LIST(sa, (fdestroy) &arg_destroy);
     649             :         sql_arg *fres = NULL;
     650      282130 :         sql_func *t = SA_ZNEW(sa, sql_func);
     651             : 
     652      804255 :         for (int i = 0; i < nargs; i++) {
     653      522125 :                 sql_type *tpe = va_arg(valist, sql_type*);
     654      522125 :                 list_append(ops, create_arg(sa, NULL, sql_create_subtype(sa, tpe, 0, 0), ARG_IN));
     655             :         }
     656      282130 :         if (res)
     657      281600 :                 fres = create_arg(sa, NULL, sql_create_subtype(sa, res, 0, 0), ARG_OUT);
     658      282130 :         base_init(sa, &t->base, local_id++, false, name);
     659             : 
     660      282130 :         t->imp = sa_strdup(sa, imp);
     661      282130 :         t->mod = sa_strdup(sa, mod);
     662      282130 :         t->ops = ops;
     663      282130 :         t->type = type;
     664      282130 :         if (fres) {
     665      281600 :                 if (res_scale)
     666        2915 :                         fres->type.scale = res_scale;
     667      281600 :                 t->res = list_append(SA_LIST(sa, (fdestroy) &arg_destroy), fres);
     668             :         } else
     669         530 :                 t->res = NULL;
     670      282130 :         t->sql = 0;
     671      282130 :         t->lang = FUNC_LANG_INT;
     672      282130 :         t->semantics = semantics;
     673      282130 :         t->side_effect = side_effect;
     674      282130 :         t->fix_scale = fix_scale;
     675      282130 :         t->s = NULL;
     676      282130 :         t->system = TRUE;
     677      282130 :         list_append(funcs, t);
     678             : 
     679             :         /* grouping aggregate doesn't have a backend */
     680      282130 :         assert(strlen(imp) == 0 || strlen(mod) == 0 || backend_resolve_function(&(int){0}, t));
     681             : 
     682      282130 :         return t;
     683             : }
     684             : 
     685             : static sql_func *
     686         530 : sql_create_procedure(sql_allocator *sa, const char *name, const char *mod, const char *imp, bit side_effect, int nargs, ...)
     687             : {
     688             :         sql_func *res;
     689             :         va_list valist;
     690             : 
     691         530 :         va_start(valist, nargs);
     692         530 :         res = sql_create_func_(sa, name, mod, imp, F_PROC, TRUE, side_effect, SCALE_NONE, 0, NULL, nargs, valist);
     693         530 :         va_end(valist);
     694         530 :         return res;
     695             : }
     696             : 
     697             : static sql_func *
     698      233635 : sql_create_func(sql_allocator *sa, const char *name, const char *mod, const char *imp, bit semantics, bit side_effect, int fix_scale,
     699             :                                 unsigned int res_scale, sql_type *fres, int nargs, ...)
     700             : {
     701             :         sql_func *res;
     702             :         va_list valist;
     703             : 
     704      233635 :         va_start(valist, nargs);
     705      233635 :         res = sql_create_func_(sa, name, mod, imp, F_FUNC, semantics, side_effect, fix_scale, res_scale, fres, nargs, valist);
     706      233635 :         va_end(valist);
     707      233635 :         return res;
     708             : }
     709             : 
     710             : static sql_func *
     711       15900 : sql_create_aggr(sql_allocator *sa, const char *name, const char *mod, const char *imp, bit semantics, sql_type *fres, int nargs, ...)
     712             : {
     713             :         sql_func *res;
     714             :         va_list valist;
     715             : 
     716       15900 :         va_start(valist, nargs);
     717       15900 :         res = sql_create_func_(sa, name, mod, imp, F_AGGR, semantics, FALSE, SCALE_NONE, 0, fres, nargs, valist);
     718       15900 :         va_end(valist);
     719       15900 :         return res;
     720             : }
     721             : 
     722             : static sql_func *
     723        1590 : sql_create_filter(sql_allocator *sa, const char *name, const char *mod, const char *imp, bit semantics, bit side_effect, int fix_scale,
     724             :                                 unsigned int res_scale, int nargs, ...)
     725             : {
     726             :         sql_func *res;
     727             :         va_list valist;
     728             : 
     729        1590 :         va_start(valist, nargs);
     730        1590 :         res = sql_create_func_(sa, name, mod, imp, F_FILT, semantics, side_effect, fix_scale, res_scale, BIT, nargs, valist);
     731        1590 :         va_end(valist);
     732        1590 :         return res;
     733             : }
     734             : 
     735             : static sql_func *
     736         530 : sql_create_union(sql_allocator *sa, const char *name, const char *mod, const char *imp, bit side_effect, int fix_scale,
     737             :                                 unsigned int res_scale, sql_type *fres, int nargs, ...)
     738             : {
     739             :         sql_func *res;
     740             :         va_list valist;
     741             : 
     742         530 :         va_start(valist, nargs);
     743         530 :         res = sql_create_func_(sa, name, mod, imp, F_UNION, TRUE, side_effect, fix_scale, res_scale, fres, nargs, valist);
     744         530 :         va_end(valist);
     745         530 :         return res;
     746             : }
     747             : 
     748             : static sql_func *
     749       29945 : sql_create_analytic(sql_allocator *sa, const char *name, const char *mod, const char *imp, int fix_scale, sql_type *fres, int nargs, ...)
     750             : {
     751             :         sql_func *res;
     752             :         va_list valist;
     753             : 
     754       29945 :         va_start(valist, nargs);
     755       29945 :         res = sql_create_func_(sa, name, mod, imp, F_ANALYTIC, TRUE, FALSE, fix_scale, 0, fres, nargs, valist);
     756       29945 :         va_end(valist);
     757       29945 :         return res;
     758             : }
     759             : 
     760             : /* SQL service initialization
     761             : This C-code version initializes the
     762             : parser catalogs with typing information. Although, in principle,
     763             : many of the function signatures can be obtained from the underlying
     764             : database kernel, we have chosen for this explicit scheme for one
     765             : simple reason. The SQL standard dictates the types and we have to
     766             : check their availability in the kernel only. The kernel itself could
     767             : include many functions for which there is no standard.
     768             : */
     769             : 
     770             : static void
     771         265 : sqltypeinit( sql_allocator *sa)
     772             : {
     773             :         sql_type *ts[100];
     774             :         sql_type **strings, **numerical;
     775             :         sql_type **decimals, **floats, **dates, **t;
     776             :         sql_type *STR, *BTE, *SHT, *INT, *LNG, *OID, *FLT, *DBL, *DEC;
     777             : #ifdef HAVE_HGE
     778             :         sql_type *HGE = NULL;
     779             : #endif
     780             :         sql_type *SECINT, *DAYINT, *MONINT, *DTE;
     781             :         sql_type *TME, *TMETZ, *TMESTAMP, *TMESTAMPTZ;
     782             :         sql_type *BLOB;
     783             :         sql_type *ANY, *TABLE, *PTR;
     784             :         sql_type *GEOM, *MBR;
     785             :         sql_func *f;
     786             :         sql_type *BigDEC;
     787             :         sql_type *LargestINT, *LargestDEC;
     788             : 
     789         265 :         ANY = sql_create_type(sa, "ANY", 0, 0, 0, EC_ANY, "void");
     790             : 
     791             :         t = ts;
     792         265 :         TABLE = *t++ = sql_create_type(sa, "TABLE", 0, 0, 0, EC_TABLE, "bat");
     793         265 :         PTR = *t++ = sql_create_type(sa, "PTR", 0, 0, 0, EC_TABLE, "ptr");
     794             : 
     795         265 :         BIT = *t++ = sql_create_type(sa, "BOOLEAN", 1, 0, 2, EC_BIT, "bit");
     796         265 :         sql_create_alias(sa, BIT->base.name, "BOOL");
     797             : 
     798             :         strings = t;
     799             :         /* create clob type first, so functions by default will bind to the clob version which doesn't require length validation on some cases */
     800         265 :         STR = *t++ = sql_create_type(sa, "CLOB",    0, 0, 0, EC_STRING, "str");
     801         265 :         *t++ = sql_create_type(sa, "VARCHAR", 0, 0, 0, EC_STRING, "str");
     802         265 :         *t++ = sql_create_type(sa, "CHAR",    0, 0, 0, EC_CHAR,   "str");
     803             : 
     804             :         numerical = t;
     805             : #if SIZEOF_OID == SIZEOF_INT
     806             :         OID = *t++ = sql_create_type(sa, "OID", 31, 0, 2, EC_POS, "oid");
     807             : #endif
     808             : #if SIZEOF_OID == SIZEOF_LNG
     809         265 :         OID = *t++ = sql_create_type(sa, "OID", 63, 0, 2, EC_POS, "oid");
     810             : #endif
     811             : 
     812         265 :         BTE = *t++ = sql_create_type(sa, "TINYINT",   8, SCALE_FIX, 2, EC_NUM, "bte");
     813         265 :         SHT = *t++ = sql_create_type(sa, "SMALLINT", 16, SCALE_FIX, 2, EC_NUM, "sht");
     814         265 :         INT = *t++ = sql_create_type(sa, "INT",      32, SCALE_FIX, 2, EC_NUM, "int");
     815             :         LargestINT =
     816         265 :         LNG = *t++ = sql_create_type(sa, "BIGINT",   64, SCALE_FIX, 2, EC_NUM, "lng");
     817             : #ifdef HAVE_HGE
     818             :         LargestINT =
     819         265 :                 HGE = *t++ = sql_create_type(sa, "HUGEINT",  128, SCALE_FIX, 2, EC_NUM, "hge");
     820             : #endif
     821             : 
     822             :         decimals = t;
     823             :         /* decimal(d,s) (d indicates nr digits,
     824             :            s scale indicates nr of digits after the dot .) */
     825         265 :         *t++ = sql_create_type(sa, "DECIMAL",  2, SCALE_FIX, 10, EC_DEC, "bte");
     826         265 :         *t++ = sql_create_type(sa, "DECIMAL",  4, SCALE_FIX, 10, EC_DEC, "sht");
     827             :         DEC =
     828         265 :         *t++ = sql_create_type(sa, "DECIMAL",  9, SCALE_FIX, 10, EC_DEC, "int");
     829             :         LargestDEC = BigDEC =
     830         265 :         *t++ = sql_create_type(sa, "DECIMAL", 18, SCALE_FIX, 10, EC_DEC, "lng");
     831             : #ifdef HAVE_HGE
     832             :         LargestDEC =
     833         265 :                 *t++ = sql_create_type(sa, "DECIMAL", 38, SCALE_FIX, 10, EC_DEC, "hge");
     834             : #endif
     835             : 
     836             :         /* float(n) (n indicates precision of at least n digits) */
     837             :         /* ie n <= 23 -> flt */
     838             :         /*    n <= 51 -> dbl */
     839             :         /*    n <= 62 -> long long dbl (with -ieee) (not supported) */
     840             :         /* this requires a type definition */
     841             : 
     842             :         floats = t;
     843         265 :         FLT = *t++ = sql_create_type(sa, "REAL", 24, SCALE_NOFIX, 2, EC_FLT, "flt");
     844         265 :         DBL = *t++ = sql_create_type(sa, "DOUBLE", 53, SCALE_NOFIX, 2, EC_FLT, "dbl");
     845             : 
     846             :         dates = t;
     847         265 :         MONINT = *t++ = sql_create_type(sa, "MONTH_INTERVAL", 3, 0, 10, EC_MONTH, "int"); /* 1 .. 13 enumerates the 13 different interval types */
     848         265 :         DAYINT = *t++ = sql_create_type(sa, "DAY_INTERVAL", 4, 0, 10, EC_SEC, "lng");
     849         265 :         SECINT = *t++ = sql_create_type(sa, "SEC_INTERVAL", 13, SCALE_FIX, 10, EC_SEC, "lng");
     850         265 :         TME = *t++ = sql_create_type(sa, "TIME", 7, 0, 0, EC_TIME, "daytime");
     851         265 :         TMETZ = *t++ = sql_create_type(sa, "TIMETZ", 7, SCALE_FIX, 0, EC_TIME_TZ, "daytime");
     852         265 :         DTE = *t++ = sql_create_type(sa, "DATE", 0, 0, 0, EC_DATE, "date");
     853         265 :         TMESTAMP = *t++ = sql_create_type(sa, "TIMESTAMP", 7, 0, 0, EC_TIMESTAMP, "timestamp");
     854         265 :         TMESTAMPTZ = *t++ = sql_create_type(sa, "TIMESTAMPTZ", 7, SCALE_FIX, 0, EC_TIMESTAMP_TZ, "timestamp");
     855             : 
     856         265 :         BLOB = *t++ = sql_create_type(sa, "BLOB", 0, 0, 0, EC_BLOB, "blob");
     857             : 
     858         265 :         sql_create_func(sa, "length", "blob", "nitems", FALSE, FALSE, SCALE_NONE, 0, INT, 1, BLOB);
     859         265 :         sql_create_func(sa, "octet_length", "blob", "nitems", FALSE, FALSE, SCALE_NONE, 0, INT, 1, BLOB);
     860             : 
     861         265 :         if (backend_has_module(&(int){0}, "geom")) { /* not the old version, change into check for module existence */
     862             :                 // the geom module is loaded
     863         253 :                 GEOM = *t++ = sql_create_type(sa, "GEOMETRY", 0, SCALE_NONE, 0, EC_GEOM, "wkb");
     864             :                 /*POINT =*/ //*t++ = sql_create_type(sa, "POINT", 0, SCALE_FIX, 0, EC_GEOM, "wkb");
     865             :                 // TODO: The GEOMETRYA  and MBR types should actually also be part of EC_GEOM. However this requires more (bat)calc.<convert> functions.
     866         253 :                 *t++ = sql_create_type(sa, "GEOMETRYA", 0, SCALE_NONE, 0, EC_EXTERNAL, "wkba");
     867             : 
     868         253 :                 MBR = *t++ = sql_create_type(sa, "MBR", 0, SCALE_NONE, 0, EC_EXTERNAL, "mbr");
     869             : 
     870             :                 /* mbr operator functions */
     871         253 :                 sql_create_func(sa, "mbr_overlap", "geom", "mbrOverlaps", TRUE, FALSE, SCALE_FIX, 0, BIT, 2, GEOM, GEOM);
     872         253 :                 sql_create_func(sa, "mbr_overlap", "geom", "mbrOverlaps", TRUE, FALSE, SCALE_FIX, 0, BIT, 2, MBR, MBR);
     873         253 :                 sql_create_func(sa, "mbr_above", "geom", "mbrAbove", TRUE, FALSE, SCALE_FIX, 0, BIT, 2, GEOM, GEOM);
     874         253 :                 sql_create_func(sa, "mbr_above", "geom", "mbrAbove", TRUE, FALSE, SCALE_FIX, 0, BIT, 2, MBR, MBR);
     875         253 :                 sql_create_func(sa, "mbr_below", "geom", "mbrBelow", TRUE, FALSE, SCALE_FIX, 0, BIT, 2, GEOM, GEOM);
     876         253 :                 sql_create_func(sa, "mbr_below", "geom", "mbrBelow", TRUE, FALSE, SCALE_FIX, 0, BIT, 2, MBR, MBR);
     877         253 :                 sql_create_func(sa, "mbr_right", "geom", "mbrRight", TRUE, FALSE, SCALE_FIX, 0, BIT, 2, GEOM, GEOM);
     878         253 :                 sql_create_func(sa, "mbr_right", "geom", "mbrRight", TRUE, FALSE, SCALE_FIX, 0, BIT, 2, MBR, MBR);
     879         253 :                 sql_create_func(sa, "mbr_left", "geom", "mbrLeft", TRUE, FALSE, SCALE_FIX, 0, BIT, 2, GEOM, GEOM);
     880         253 :                 sql_create_func(sa, "mbr_left", "geom", "mbrLeft", TRUE, FALSE, SCALE_FIX, 0, BIT, 2, MBR, MBR);
     881         253 :                 sql_create_func(sa, "mbr_overlap_or_above", "geom", "mbrOverlapOrAbove", TRUE, FALSE, SCALE_FIX, 0, BIT, 2, GEOM, GEOM);
     882         253 :                 sql_create_func(sa, "mbr_overlap_or_above", "geom", "mbrOverlapOrAbove", TRUE, FALSE, SCALE_FIX, 0, BIT, 2, MBR, MBR);
     883         253 :                 sql_create_func(sa, "mbr_overlap_or_below", "geom", "mbrOverlapOrBelow", TRUE, FALSE, SCALE_FIX, 0, BIT, 2, GEOM, GEOM);
     884         253 :                 sql_create_func(sa, "mbr_overlap_or_below", "geom", "mbrOverlapOrBelow", TRUE, FALSE, SCALE_FIX, 0, BIT, 2, MBR, MBR);
     885         253 :                 sql_create_func(sa, "mbr_overlap_or_right", "geom", "mbrOverlapOrRight", TRUE, FALSE, SCALE_FIX, 0, BIT, 2, GEOM, GEOM);
     886         253 :                 sql_create_func(sa, "mbr_overlap_or_right", "geom", "mbrOverlapOrRight", TRUE, FALSE, SCALE_FIX, 0, BIT, 2, MBR, MBR);
     887         253 :                 sql_create_func(sa, "mbr_overlap_or_left", "geom", "mbrOverlapOrLeft", TRUE, FALSE, SCALE_FIX, 0, BIT, 2, GEOM, GEOM);
     888         253 :                 sql_create_func(sa, "mbr_overlap_or_left", "geom", "mbrOverlapOrLeft", TRUE, FALSE, SCALE_FIX, 0, BIT, 2, MBR, MBR);
     889         253 :                 sql_create_func(sa, "mbr_contains", "geom", "mbrContains", TRUE, FALSE, SCALE_FIX, 0, BIT, 2, GEOM, GEOM);
     890         253 :                 sql_create_func(sa, "mbr_contains", "geom", "mbrContains", TRUE, FALSE, SCALE_FIX, 0, BIT, 2, MBR, MBR);
     891         253 :                 sql_create_func(sa, "mbr_contained", "geom", "mbrContained", TRUE, FALSE, SCALE_FIX, 0, BIT, 2, GEOM, GEOM);
     892         253 :                 sql_create_func(sa, "mbr_contained", "geom", "mbrContained", TRUE, FALSE, SCALE_FIX, 0, BIT, 2, MBR, MBR);
     893         253 :                 sql_create_func(sa, "mbr_equal", "geom", "mbrEqual", TRUE, FALSE, SCALE_FIX, 0, BIT, 2, GEOM, GEOM);
     894         253 :                 sql_create_func(sa, "mbr_equal", "geom", "mbrEqual", TRUE, FALSE, SCALE_FIX, 0, BIT, 2, MBR, MBR);
     895         253 :                 sql_create_func(sa, "mbr_distance", "geom", "mbrDistance", TRUE, FALSE, SCALE_FIX, 0, DBL, 2, GEOM, GEOM);
     896         253 :                 sql_create_func(sa, "mbr_distance", "geom", "mbrDistance", TRUE, FALSE, SCALE_FIX, 0, DBL, 2, MBR, MBR);
     897         253 :                 sql_create_func(sa, "left_shift", "geom", "mbrLeft", TRUE, FALSE, SCALE_FIX, 0, BIT, 2, GEOM, GEOM);
     898         253 :                 sql_create_func(sa, "left_shift", "geom", "mbrLeft", TRUE, FALSE, SCALE_FIX, 0, BIT, 2, MBR, MBR);
     899         253 :                 sql_create_func(sa, "right_shift", "geom", "mbrRight", TRUE, FALSE, SCALE_FIX, 0, BIT, 2, GEOM, GEOM);
     900         253 :                 sql_create_func(sa, "right_shift", "geom", "mbrRight", TRUE, FALSE, SCALE_FIX, 0, BIT, 2, MBR, MBR);
     901             :         }
     902             : 
     903         265 :         *t = NULL;
     904             : 
     905             : //      sql_create_func(sa, "st_pointfromtext", "geom", "st_pointformtext", FALSE, SCALE_NONE, 0, OID, 1, OID);
     906             :         /* The grouping aggregate doesn't have a backend implementation. It gets replaced at rel_unnest */
     907         265 :         sql_create_aggr(sa, "grouping", "", "", TRUE, BTE, 1, ANY);
     908         265 :         sql_create_aggr(sa, "grouping", "", "", TRUE, SHT, 1, ANY);
     909         265 :         sql_create_aggr(sa, "grouping", "", "", TRUE, INT, 1, ANY);
     910         265 :         sql_create_aggr(sa, "grouping", "", "", TRUE, LNG, 1, ANY);
     911             : #ifdef HAVE_HGE
     912         265 :         sql_create_aggr(sa, "grouping", "", "", TRUE, HGE, 1, ANY);
     913             : #endif
     914             : 
     915         265 :         sql_create_aggr(sa, "not_unique", "aggr", "not_unique", TRUE, BIT, 1, OID);
     916             :         /* well to be precise it does reduce and map */
     917             : 
     918             :         /* functions needed for all types */
     919         265 :         sql_create_func(sa, "hash", "mkey", "hash", TRUE, FALSE, SCALE_FIX, 0, LNG, 1, ANY);
     920         265 :         sql_create_func(sa, "rotate_xor_hash", "calc", "rotate_xor_hash", TRUE, FALSE, SCALE_NONE, 0, LNG, 3, LNG, INT, ANY);
     921         265 :         sql_create_func(sa, "=", "calc", "=", FALSE, FALSE, SCALE_FIX, 0, BIT, 2, ANY, ANY);
     922         265 :         sql_create_func(sa, "<>", "calc", "!=", FALSE, FALSE, SCALE_FIX, 0, BIT, 2, ANY, ANY);
     923         265 :         sql_create_func(sa, "isnull", "calc", "isnil", TRUE, FALSE, SCALE_FIX, 0, BIT, 1, ANY);
     924         265 :         sql_create_func(sa, "isnotnull", "calc", "isnotnil", TRUE, FALSE, SCALE_FIX, 0, BIT, 1, ANY);
     925         265 :         sql_create_func(sa, ">", "calc", ">", FALSE, FALSE, SCALE_FIX, 0, BIT, 2, ANY, ANY);
     926         265 :         sql_create_func(sa, ">=", "calc", ">=", FALSE, FALSE, SCALE_FIX, 0, BIT, 2, ANY, ANY);
     927         265 :         sql_create_func(sa, "<", "calc", "<", FALSE, FALSE, SCALE_FIX, 0, BIT, 2, ANY, ANY);
     928         265 :         sql_create_func(sa, "<=", "calc", "<=", FALSE, FALSE, SCALE_FIX, 0, BIT, 2, ANY, ANY);
     929         265 :         sql_create_func(sa, "between", "calc", "between", FALSE, FALSE, SCALE_FIX, 0, BIT, 8, ANY, ANY, ANY, BIT, BIT, BIT, BIT, BIT);
     930         265 :         sql_create_aggr(sa, "zero_or_one", "aggr", "zero_or_one", TRUE, ANY, 1, ANY);
     931         265 :         sql_create_aggr(sa, "all", "aggr", "all", TRUE, ANY, 1, ANY);
     932         265 :         sql_create_aggr(sa, "null", "aggr", "null", TRUE, BIT, 1, ANY);
     933         265 :         sql_create_func(sa, "any", "sql", "any", TRUE, FALSE, SCALE_NONE, 0, BIT, 3, BIT, BIT, BIT);
     934         265 :         sql_create_func(sa, "all", "sql", "all", TRUE, FALSE, SCALE_NONE, 0, BIT, 3, BIT, BIT, BIT);
     935         265 :         sql_create_aggr(sa, "anyequal", "aggr", "anyequal", TRUE, BIT, 1, ANY); /* needs 3 arguments (l,r,nil)(ugh) */
     936         265 :         sql_create_aggr(sa, "allnotequal", "aggr", "allnotequal", TRUE, BIT, 1, ANY); /* needs 3 arguments (l,r,nil)(ugh) */
     937         265 :         sql_create_func(sa, "sql_anyequal", "aggr", "anyequal", TRUE, FALSE, SCALE_NONE, 0, BIT, 2, ANY, ANY);
     938         265 :         sql_create_func(sa, "sql_not_anyequal", "aggr", "not_anyequal", TRUE, FALSE, SCALE_NONE, 0, BIT, 2, ANY, ANY);
     939         265 :         sql_create_aggr(sa, "exist", "aggr", "exist", TRUE, BIT, 1, ANY);
     940         265 :         sql_create_aggr(sa, "not_exist", "aggr", "not_exist", TRUE, BIT, 1, ANY);
     941         265 :         sql_create_func(sa, "sql_exists", "aggr", "exist", TRUE, FALSE, SCALE_NONE, 0, BIT, 1, ANY);
     942         265 :         sql_create_func(sa, "sql_not_exists", "aggr", "not_exist", TRUE, FALSE, SCALE_NONE, 0, BIT, 1, ANY);
     943             :         /* needed for relational version */
     944         265 :         sql_create_func(sa, "identity", "calc", "identity", TRUE, FALSE, SCALE_NONE, 0, OID, 1, ANY);
     945         265 :         sql_create_func(sa, "rowid", "calc", "identity", TRUE, FALSE, SCALE_NONE, 0, INT, 1, ANY);
     946             :         /* needed for indices/clusters oid(schema.table,val) returns max(head(schema.table))+1 */
     947         265 :         sql_create_func(sa, "rowid", "calc", "rowid", TRUE, FALSE, SCALE_NONE, 0, OID, 3, ANY, STR, STR);
     948         265 :         sql_create_aggr(sa, "min", "aggr", "min", FALSE, ANY, 1, ANY);
     949         265 :         sql_create_aggr(sa, "max", "aggr", "max", FALSE, ANY, 1, ANY);
     950         265 :         sql_create_func(sa, "sql_min", "calc", "min", FALSE, FALSE, SCALE_FIX, 0, ANY, 2, ANY, ANY);
     951         265 :         sql_create_func(sa, "sql_max", "calc", "max", FALSE, FALSE, SCALE_FIX, 0, ANY, 2, ANY, ANY);
     952         265 :         sql_create_func(sa, "least", "calc", "min_no_nil", TRUE, FALSE, SCALE_FIX, 0, ANY, 2, ANY, ANY);
     953         265 :         sql_create_func(sa, "greatest", "calc", "max_no_nil", TRUE, FALSE, SCALE_FIX, 0, ANY, 2, ANY, ANY);
     954         265 :         sql_create_func(sa, "ifthenelse", "calc", "ifthenelse", TRUE, FALSE, SCALE_FIX, 0, ANY, 3, BIT, ANY, ANY);
     955             :         /* nullif, coalesce, casewhen and case don't have a backend implementation */
     956         265 :         sql_create_func(sa, "nullif", "", "", TRUE, FALSE, SCALE_FIX, 0, ANY, 2, ANY, ANY);
     957         265 :         sql_create_func(sa, "coalesce", "", "", TRUE, FALSE, SCALE_FIX, 0, ANY, 2, ANY, ANY);
     958         265 :         sql_create_func(sa, "casewhen", "", "", TRUE, FALSE, SCALE_FIX, 0, ANY, 2, ANY, ANY);
     959         265 :         sql_create_func(sa, "case", "", "", TRUE, FALSE, SCALE_FIX, 0, ANY, 2, ANY, ANY);
     960             :         /* needed for count(*) and window functions without input col */
     961         265 :         sql_create_func(sa, "star", "", "", TRUE, FALSE, SCALE_FIX, 0, ANY, 0);
     962             : 
     963             :         /* sum for numerical and decimals */
     964         265 :         sql_create_aggr(sa, "sum", "aggr", "sum", FALSE, LargestINT, 1, BTE);
     965         265 :         sql_create_aggr(sa, "sum", "aggr", "sum", FALSE, LargestINT, 1, SHT);
     966         265 :         sql_create_aggr(sa, "sum", "aggr", "sum", FALSE, LargestINT, 1, INT);
     967             :         //sql_create_aggr(sa, "sum", "aggr", "sum", LargestINT, 1, LNG, LargestINT);
     968             : #ifdef HAVE_HGE
     969         265 :         sql_create_aggr(sa, "sum", "aggr", "sum", FALSE, LargestINT, 1, HGE);
     970             : #endif
     971         265 :         sql_create_aggr(sa, "sum", "aggr", "sum", FALSE, LNG, 1, LNG);
     972             : 
     973             :         t = decimals; /* BTE */
     974         265 :         sql_create_aggr(sa, "sum", "aggr", "sum", FALSE, LargestDEC, 1, *(t));
     975             :         t++; /* SHT */
     976         265 :         sql_create_aggr(sa, "sum", "aggr", "sum", FALSE, LargestDEC, 1, *(t));
     977             :         t++; /* INT */
     978         265 :         sql_create_aggr(sa, "sum", "aggr", "sum", FALSE, LargestDEC, 1, *(t));
     979             :         t++; /* LNG */
     980         265 :         sql_create_aggr(sa, "sum", "aggr", "sum", FALSE, LargestDEC, 1, *(t));
     981             : #ifdef HAVE_HGE
     982             :         t++; /* HGE */
     983         265 :         sql_create_aggr(sa, "sum", "aggr", "sum", FALSE, LargestDEC, 1, *(t));
     984             : #endif
     985             : 
     986             :         /* prod for numerical and decimals */
     987         265 :         sql_create_aggr(sa, "prod", "aggr", "prod", FALSE, LargestINT, 1, BTE);
     988         265 :         sql_create_aggr(sa, "prod", "aggr", "prod", FALSE, LargestINT, 1, SHT);
     989         265 :         sql_create_aggr(sa, "prod", "aggr", "prod", FALSE, LargestINT, 1, INT);
     990         265 :         sql_create_aggr(sa, "prod", "aggr", "prod", FALSE, LargestINT, 1, LNG);
     991             : #ifdef HAVE_HGE
     992         265 :         sql_create_aggr(sa, "prod", "aggr", "prod", FALSE, LargestINT, 1, HGE);
     993             : #endif
     994             : 
     995             : #if 0
     996             :         /* prod for decimals introduce errors in the output scales */
     997             :         t = decimals; /* BTE */
     998             :         sql_create_aggr(sa, "prod", "aggr", "prod", FALSE, LargestDEC, 1, *(t));
     999             :         t++; /* SHT */
    1000             :         sql_create_aggr(sa, "prod", "aggr", "prod", FALSE, LargestDEC, 1, *(t));
    1001             :         t++; /* INT */
    1002             :         sql_create_aggr(sa, "prod", "aggr", "prod", FALSE, LargestDEC, 1, *(t));
    1003             :         t++; /* LNG */
    1004             :         sql_create_aggr(sa, "prod", "aggr", "prod", FALSE, LargestDEC, 1, *(t));
    1005             : #ifdef HAVE_HGE
    1006             :         t++; /* HGE */
    1007             :         sql_create_aggr(sa, "prod", "aggr", "prod", FALSE, LargestDEC, 1, *(t));
    1008             : #endif
    1009             : #endif
    1010             : 
    1011        3710 :         for (t = numerical; t < dates; t++) {
    1012        3445 :                 if (*t == OID)
    1013         265 :                         continue;
    1014        3180 :                 sql_create_func(sa, "mod", "calc", "%", FALSE, FALSE, SCALE_FIX, 0, *t, 2, *t, *t);
    1015             :         }
    1016             : 
    1017         795 :         for (t = floats; t < dates; t++) {
    1018         530 :                 sql_create_aggr(sa, "sum", "aggr", "sum", FALSE, *t, 1, *t);
    1019         530 :                 sql_create_aggr(sa, "prod", "aggr", "prod", FALSE, *t, 1, *t);
    1020             :         }
    1021         265 :         sql_create_aggr(sa, "sum", "aggr", "sum", FALSE, MONINT, 1, MONINT);
    1022         265 :         sql_create_aggr(sa, "sum", "aggr", "sum", FALSE, DAYINT, 1, DAYINT);
    1023         265 :         sql_create_aggr(sa, "sum", "aggr", "sum", FALSE, SECINT, 1, SECINT);
    1024             :         /* do DBL first so that it is chosen as cast destination for
    1025             :          * unknown types */
    1026         265 :         sql_create_aggr(sa, "avg", "aggr", "avg", FALSE, DBL, 1, DBL);
    1027         265 :         sql_create_aggr(sa, "avg", "aggr", "avg", FALSE, DBL, 1, BTE);
    1028         265 :         sql_create_aggr(sa, "avg", "aggr", "avg", FALSE, DBL, 1, SHT);
    1029         265 :         sql_create_aggr(sa, "avg", "aggr", "avg", FALSE, DBL, 1, INT);
    1030         265 :         sql_create_aggr(sa, "avg", "aggr", "avg", FALSE, DBL, 1, LNG);
    1031             : #ifdef HAVE_HGE
    1032         265 :         sql_create_aggr(sa, "avg", "aggr", "avg", FALSE, DBL, 1, HGE);
    1033             : #endif
    1034         265 :         sql_create_aggr(sa, "avg", "aggr", "avg", FALSE, DBL, 1, FLT);
    1035             : 
    1036             :         t = decimals; /* BTE */
    1037         265 :         sql_create_aggr(sa, "avg", "aggr", "avg", FALSE, *(t), 1, *(t));
    1038             :         t++; /* SHT */
    1039         265 :         sql_create_aggr(sa, "avg", "aggr", "avg", FALSE, *(t), 1, *(t));
    1040             :         t++; /* INT */
    1041         265 :         sql_create_aggr(sa, "avg", "aggr", "avg", FALSE, *(t), 1, *(t));
    1042             :         t++; /* LNG */
    1043         265 :         sql_create_aggr(sa, "avg", "aggr", "avg", FALSE, *(t), 1, *(t));
    1044             : #ifdef HAVE_HGE
    1045             :         t++; /* HGE */
    1046         265 :         sql_create_aggr(sa, "avg", "aggr", "avg", FALSE, *(t), 1, *(t));
    1047             : #endif
    1048             : 
    1049         265 :         sql_create_aggr(sa, "avg", "aggr", "avg", FALSE, MONINT, 1, MONINT);
    1050         265 :         sql_create_aggr(sa, "avg", "aggr", "avg", FALSE, DAYINT, 1, DAYINT);
    1051         265 :         sql_create_aggr(sa, "avg", "aggr", "avg", FALSE, SECINT, 1, SECINT);
    1052             : 
    1053         265 :         sql_create_aggr(sa, "count_no_nil", "aggr", "count_no_nil", TRUE, LNG, 0);
    1054         265 :         sql_create_aggr(sa, "count", "aggr", "count", TRUE, LNG, 1, ANY);
    1055         265 :         sql_create_func(sa, "cnt", "sql", "count", TRUE, FALSE, SCALE_FIX, 0, LNG, 2, STR, STR);
    1056             : 
    1057        1060 :         for (t = strings; t < numerical; t++) {
    1058         795 :                 sql_create_aggr(sa, "listagg", "aggr", "str_group_concat", TRUE, *t, 1, *t);
    1059         795 :                 sql_create_aggr(sa, "listagg", "aggr", "str_group_concat", TRUE, *t, 2, *t, *t);
    1060             :         }
    1061             : 
    1062             :         /* order based operators */
    1063         265 :         sql_create_analytic(sa, "diff", "sql", "diff", SCALE_NONE, BIT, 1, ANY);
    1064         265 :         sql_create_analytic(sa, "diff", "sql", "diff", SCALE_NONE, BIT, 2, BIT, ANY);
    1065        4505 :         for (t = numerical; *t != TME; t++) {
    1066        4240 :                 if (*t == OID)
    1067         265 :                         continue;
    1068        3975 :                 sql_create_analytic(sa, "window_bound", "sql", "window_bound", SCALE_NONE, OID, 5, ANY, INT, INT, INT, *t);
    1069        3975 :                 sql_create_analytic(sa, "window_bound", "sql", "window_bound", SCALE_NONE, OID, 6, BIT, ANY, INT, INT, INT, *t);
    1070             :         }
    1071             : 
    1072         265 :         sql_create_analytic(sa, "rank", "sql", "rank", SCALE_NONE, INT, 1, ANY);
    1073         265 :         sql_create_analytic(sa, "dense_rank", "sql", "dense_rank", SCALE_NONE, INT, 1, ANY);
    1074         265 :         sql_create_analytic(sa, "row_number", "sql", "row_number", SCALE_NONE, INT, 1, ANY);
    1075         265 :         sql_create_analytic(sa, "percent_rank", "sql", "percent_rank", SCALE_NONE, DBL, 1, ANY);
    1076         265 :         sql_create_analytic(sa, "cume_dist", "sql", "cume_dist", SCALE_NONE, DBL, 1, ANY);
    1077             : 
    1078         265 :         sql_create_analytic(sa, "ntile", "sql", "ntile", SCALE_NONE, BTE, 2, ANY, BTE);
    1079         265 :         sql_create_analytic(sa, "ntile", "sql", "ntile", SCALE_NONE, SHT, 2, ANY, SHT);
    1080         265 :         sql_create_analytic(sa, "ntile", "sql", "ntile", SCALE_NONE, INT, 2, ANY, INT);
    1081         265 :         sql_create_analytic(sa, "ntile", "sql", "ntile", SCALE_NONE, LNG, 2, ANY, LNG);
    1082             : #ifdef HAVE_HGE
    1083         265 :         sql_create_analytic(sa, "ntile", "sql", "ntile", SCALE_NONE, HGE, 2, ANY, HGE);
    1084             : #endif
    1085             : 
    1086         265 :         sql_create_analytic(sa, "lag", "sql", "lag", SCALE_NONE, ANY, 1, ANY);
    1087         265 :         sql_create_analytic(sa, "lag", "sql", "lag", SCALE_NONE, ANY, 2, ANY, BTE);
    1088         265 :         sql_create_analytic(sa, "lag", "sql", "lag", SCALE_NONE, ANY, 2, ANY, SHT);
    1089         265 :         sql_create_analytic(sa, "lag", "sql", "lag", SCALE_NONE, ANY, 2, ANY, INT);
    1090         265 :         sql_create_analytic(sa, "lag", "sql", "lag", SCALE_NONE, ANY, 2, ANY, LNG);
    1091             : #ifdef HAVE_HGE
    1092         265 :         sql_create_analytic(sa, "lag", "sql", "lag", SCALE_NONE, ANY, 2, ANY, HGE);
    1093             : #endif
    1094         265 :         sql_create_analytic(sa, "lag", "sql", "lag", SCALE_NONE, ANY, 3, ANY, BTE, ANY);
    1095         265 :         sql_create_analytic(sa, "lag", "sql", "lag", SCALE_NONE, ANY, 3, ANY, SHT, ANY);
    1096         265 :         sql_create_analytic(sa, "lag", "sql", "lag", SCALE_NONE, ANY, 3, ANY, INT, ANY);
    1097         265 :         sql_create_analytic(sa, "lag", "sql", "lag", SCALE_NONE, ANY, 3, ANY, LNG, ANY);
    1098             : #ifdef HAVE_HGE
    1099         265 :         sql_create_analytic(sa, "lag", "sql", "lag", SCALE_NONE, ANY, 3, ANY, HGE, ANY);
    1100             : #endif
    1101             : 
    1102         265 :         sql_create_analytic(sa, "lead", "sql", "lead", SCALE_NONE, ANY, 1, ANY);
    1103         265 :         sql_create_analytic(sa, "lead", "sql", "lead", SCALE_NONE, ANY, 2, ANY, BTE);
    1104         265 :         sql_create_analytic(sa, "lead", "sql", "lead", SCALE_NONE, ANY, 2, ANY, SHT);
    1105         265 :         sql_create_analytic(sa, "lead", "sql", "lead", SCALE_NONE, ANY, 2, ANY, INT);
    1106         265 :         sql_create_analytic(sa, "lead", "sql", "lead", SCALE_NONE, ANY, 2, ANY, LNG);
    1107             : #ifdef HAVE_HGE
    1108         265 :         sql_create_analytic(sa, "lead", "sql", "lead", SCALE_NONE, ANY, 2, ANY, HGE);
    1109             : #endif
    1110         265 :         sql_create_analytic(sa, "lead", "sql", "lead", SCALE_NONE, ANY, 3, ANY, BTE, ANY);
    1111         265 :         sql_create_analytic(sa, "lead", "sql", "lead", SCALE_NONE, ANY, 3, ANY, SHT, ANY);
    1112         265 :         sql_create_analytic(sa, "lead", "sql", "lead", SCALE_NONE, ANY, 3, ANY, INT, ANY);
    1113         265 :         sql_create_analytic(sa, "lead", "sql", "lead", SCALE_NONE, ANY, 3, ANY, LNG, ANY);
    1114             : #ifdef HAVE_HGE
    1115         265 :         sql_create_analytic(sa, "lead", "sql", "lead", SCALE_NONE, ANY, 3, ANY, HGE, ANY);
    1116             : #endif
    1117             : 
    1118             :         /* these analytic functions support frames */
    1119         265 :         sql_create_analytic(sa, "first_value", "sql", "first_value", SCALE_NONE, ANY, 1, ANY);
    1120         265 :         sql_create_analytic(sa, "last_value", "sql", "last_value", SCALE_NONE, ANY, 1, ANY);
    1121         265 :         sql_create_analytic(sa, "nth_value", "sql", "nth_value", SCALE_NONE, ANY, 2, ANY, LNG);
    1122             : 
    1123         265 :         sql_create_analytic(sa, "count", "sql", "count", SCALE_NONE, LNG, 2, ANY, BIT);
    1124         265 :         sql_create_analytic(sa, "min", "sql", "min", SCALE_NONE, ANY, 1, ANY);
    1125         265 :         sql_create_analytic(sa, "max", "sql", "max", SCALE_NONE, ANY, 1, ANY);
    1126             : 
    1127             :         /* analytical sum for numerical and decimals */
    1128         265 :         sql_create_analytic(sa, "sum", "sql", "sum", SCALE_NONE, LargestINT, 1, BTE);
    1129         265 :         sql_create_analytic(sa, "sum", "sql", "sum", SCALE_NONE, LargestINT, 1, SHT);
    1130         265 :         sql_create_analytic(sa, "sum", "sql", "sum", SCALE_NONE, LargestINT, 1, INT);
    1131         265 :         sql_create_analytic(sa, "sum", "sql", "sum", SCALE_NONE, LargestINT, 1, LNG);
    1132             : #ifdef HAVE_HGE
    1133         265 :         sql_create_analytic(sa, "sum", "sql", "sum", SCALE_NONE, LargestINT, 1, HGE);
    1134             : #endif
    1135             : 
    1136             :         t = decimals; /* BTE */
    1137         265 :         sql_create_analytic(sa, "sum", "sql", "sum", SCALE_NONE, LargestDEC, 1, *(t));
    1138             :         t++; /* SHT */
    1139         265 :         sql_create_analytic(sa, "sum", "sql", "sum", SCALE_NONE, LargestDEC, 1, *(t));
    1140             :         t++; /* INT */
    1141         265 :         sql_create_analytic(sa, "sum", "sql", "sum", SCALE_NONE, LargestDEC, 1, *(t));
    1142             :         t++; /* LNG */
    1143         265 :         sql_create_analytic(sa, "sum", "sql", "sum", SCALE_NONE, LargestDEC, 1, *(t));
    1144             : #ifdef HAVE_HGE
    1145             :         t++; /* HGE */
    1146         265 :         sql_create_analytic(sa, "sum", "sql", "sum", SCALE_NONE, LargestDEC, 1, *(t));
    1147             : #endif
    1148             : 
    1149             :         /* analytical product for numerical and decimals */
    1150         265 :         sql_create_analytic(sa, "prod", "sql", "prod", SCALE_NONE, LargestINT, 1, BTE);
    1151         265 :         sql_create_analytic(sa, "prod", "sql", "prod", SCALE_NONE, LargestINT, 1, SHT);
    1152         265 :         sql_create_analytic(sa, "prod", "sql", "prod", SCALE_NONE, LargestINT, 1, INT);
    1153         265 :         sql_create_analytic(sa, "prod", "sql", "prod", SCALE_NONE, LargestINT, 1, LNG);
    1154             : #ifdef HAVE_HGE
    1155         265 :         sql_create_analytic(sa, "prod", "sql", "prod", SCALE_NONE, LargestINT, 1, HGE);
    1156             : #endif
    1157             : 
    1158             : #if 0
    1159             :         /* prod for decimals introduce errors in the output scales */
    1160             :         t = decimals; /* BTE */
    1161             :         sql_create_analytic(sa, "prod", "sql", "prod", SCALE_NONE, LargestDEC, 1, *(t));
    1162             :         t++; /* SHT */
    1163             :         sql_create_analytic(sa, "prod", "sql", "prod", SCALE_NONE, LargestDEC, 1, *(t));
    1164             :         t++; /* INT */
    1165             :         sql_create_analytic(sa, "prod", "sql", "prod", SCALE_NONE, LargestDEC, 1, *(t));
    1166             :         t++; /* LNG */
    1167             :         sql_create_analytic(sa, "prod", "sql", "prod", SCALE_NONE, LargestDEC, 1, *(t));
    1168             : #ifdef HAVE_HGE
    1169             :         t++; /* HGE */
    1170             :         sql_create_analytic(sa, "prod", "sql", "prod", SCALE_NONE, LargestDEC, 1, *(t));
    1171             : #endif
    1172             : #endif
    1173             : 
    1174         795 :         for (t = floats; t < dates; t++) {
    1175         530 :                 sql_create_analytic(sa, "sum", "sql", "sum", SCALE_NONE, *t, 1, *t);
    1176         530 :                 sql_create_analytic(sa, "prod", "sql", "prod", SCALE_NONE, *t, 1, *t);
    1177             :         }
    1178         265 :         sql_create_analytic(sa, "sum", "sql", "sum", SCALE_NONE, MONINT, 1, MONINT);
    1179         265 :         sql_create_analytic(sa, "sum", "sql", "sum", SCALE_NONE, DAYINT, 1, DAYINT);
    1180         265 :         sql_create_analytic(sa, "sum", "sql", "sum", SCALE_NONE, SECINT, 1, SECINT);
    1181             : 
    1182             :         //analytical average for numerical types
    1183         265 :         sql_create_analytic(sa, "avg", "sql", "avg", SCALE_NONE, DBL, 1, DBL);
    1184         265 :         sql_create_analytic(sa, "avg", "sql", "avg", SCALE_NONE, DBL, 1, BTE);
    1185         265 :         sql_create_analytic(sa, "avg", "sql", "avg", SCALE_NONE, DBL, 1, SHT);
    1186         265 :         sql_create_analytic(sa, "avg", "sql", "avg", SCALE_NONE, DBL, 1, INT);
    1187         265 :         sql_create_analytic(sa, "avg", "sql", "avg", SCALE_NONE, DBL, 1, LNG);
    1188             : #ifdef HAVE_HGE
    1189         265 :         sql_create_analytic(sa, "avg", "sql", "avg", SCALE_NONE, DBL, 1, HGE);
    1190             : #endif
    1191             : 
    1192         265 :         sql_create_analytic(sa, "avg", "sql", "avg", SCALE_NONE, DBL, 1, FLT);
    1193             : 
    1194             :         t = decimals; /* BTE */
    1195         265 :         sql_create_analytic(sa, "avg", "sql", "avg", SCALE_NONE, *(t), 1, *(t));
    1196             :         t++; /* SHT */
    1197         265 :         sql_create_analytic(sa, "avg", "sql", "avg", SCALE_NONE, *(t), 1, *(t));
    1198             :         t++; /* INT */
    1199         265 :         sql_create_analytic(sa, "avg", "sql", "avg", SCALE_NONE, *(t), 1, *(t));
    1200             :         t++; /* LNG */
    1201         265 :         sql_create_analytic(sa, "avg", "sql", "avg", SCALE_NONE, *(t), 1, *(t));
    1202             : #ifdef HAVE_HGE
    1203             :         t++; /* HGE */
    1204         265 :         sql_create_analytic(sa, "avg", "sql", "avg", SCALE_NONE, *(t), 1, *(t));
    1205             : #endif
    1206             : 
    1207         265 :         sql_create_analytic(sa, "avg", "sql", "avg", SCALE_NONE, MONINT, 1, MONINT);
    1208         265 :         sql_create_analytic(sa, "avg", "sql", "avg", SCALE_NONE, DAYINT, 1, DAYINT);
    1209         265 :         sql_create_analytic(sa, "avg", "sql", "avg", SCALE_NONE, SECINT, 1, SECINT);
    1210             : 
    1211        1060 :         for (t = strings; t < numerical; t++) {
    1212         795 :                 sql_create_analytic(sa, "listagg", "sql", "str_group_concat", SCALE_NONE, *t, 1, *t);
    1213         795 :                 sql_create_analytic(sa, "listagg", "sql", "str_group_concat", SCALE_NONE, *t, 2, *t, *t);
    1214             :         }
    1215         265 :         sql_create_func(sa, "and", "calc", "and", TRUE, FALSE, SCALE_FIX, 0, BIT, 2, BIT, BIT);
    1216         265 :         sql_create_func(sa, "or",  "calc",  "or", TRUE, FALSE, SCALE_FIX, 0, BIT, 2, BIT, BIT);
    1217         265 :         sql_create_func(sa, "xor", "calc", "xor", FALSE, FALSE, SCALE_FIX, 0, BIT, 2, BIT, BIT);
    1218         265 :         sql_create_func(sa, "not", "calc", "not", FALSE, FALSE, SCALE_FIX, 0, BIT, 1, BIT);
    1219             : 
    1220             :         /* functions for interval types */
    1221        1060 :         for (t = dates; *t != TME; t++) {
    1222         795 :                 sql_subtype *lt = sql_bind_localtype((*t)->impl);
    1223             : 
    1224         795 :                 sql_create_func(sa, "sql_sub", "calc", "-", FALSE, FALSE, SCALE_NONE, 0, *t, 2, *t, *t);
    1225         795 :                 sql_create_func(sa, "sql_add", "calc", "+", FALSE, FALSE, SCALE_NONE, 0, *t, 2, *t, *t);
    1226         795 :                 sql_create_func(sa, "sql_neg", "calc", "-", FALSE, FALSE, INOUT, 0, *t, 1, *t);
    1227         795 :                 sql_create_func(sa, "abs", "calc", "abs", FALSE, FALSE, SCALE_NONE, 0, *t, 1, *t);
    1228         795 :                 sql_create_func(sa, "sign", "calc", "sign", FALSE, FALSE, SCALE_NONE, 0, BTE, 1, *t);
    1229             :                 /* scale fixing for intervals */
    1230         795 :                 sql_create_func(sa, "scale_up", "calc", "*", FALSE, FALSE, SCALE_NONE, 0, *t, 2, *t, lt->type);
    1231         795 :                 sql_create_func(sa, "scale_down", "calc", "dec_round", FALSE, FALSE, SCALE_NONE, 0, *t, 2, *t, lt->type);
    1232             :         }
    1233             : 
    1234             :         /* allow smaller types for arguments of mul/div */
    1235        2915 :         for (t = numerical, t++; t != floats; t++) {
    1236             :                 sql_type **u;
    1237       15900 :                 for (u = numerical, u++; u != decimals; u++) {
    1238       13250 :                         if (*t == OID)
    1239           0 :                                 continue;
    1240       13250 :                         if (t != u && (*t)->localtype >  (*u)->localtype) {
    1241        5300 :                                 sql_create_func(sa, "sql_mul", "calc", "*", FALSE, FALSE, SCALE_MUL, 0, *t, 2, *t, *u);
    1242        5300 :                                 sql_create_func(sa, "sql_mul", "calc", "*", FALSE, FALSE, SCALE_MUL, 0, *t, 2, *u, *t);
    1243        5300 :                                 sql_create_func(sa, "sql_div", "calc", "/", FALSE, FALSE, SCALE_DIV, 0, *t, 2, *t, *u);
    1244             :                         }
    1245             :                 }
    1246             :         }
    1247        1325 :         for (t = decimals, t++; t != floats; t++) {
    1248             :                 sql_type **u;
    1249             : 
    1250        5300 :                 for (u = decimals, u++; u != floats; u++) {
    1251        4240 :                         if (t != u && (*t)->localtype >  (*u)->localtype) {
    1252        1590 :                                 sql_create_func(sa, "sql_mul", "calc", "*", FALSE, FALSE, SCALE_MUL, 0, *t, 2, *t, *u);
    1253        1590 :                                 sql_create_func(sa, "sql_div", "calc", "/", FALSE, FALSE, SCALE_DIV, 0, *t, 2, *t, *u);
    1254             :                         }
    1255             :                 }
    1256             :         }
    1257             : 
    1258             :         /* all numericals */
    1259        3710 :         for (t = numerical; t < dates; t++) {
    1260             :                 sql_subtype *lt;
    1261             : 
    1262        3445 :                 if (*t == OID)
    1263         265 :                         continue;
    1264             : 
    1265        3180 :                 lt = sql_bind_localtype((*t)->impl);
    1266             : 
    1267        3180 :                 sql_create_func(sa, "sql_sub", "calc", "-", FALSE, FALSE, SCALE_FIX, 0, *t, 2, *t, *t);
    1268        3180 :                 sql_create_func(sa, "sql_add", "calc", "+", FALSE, FALSE, SCALE_FIX, 0, *t, 2, *t, *t);
    1269        3180 :                 sql_create_func(sa, "sql_mul", "calc", "*", FALSE, FALSE, SCALE_MUL, 0, *t, 2, *t, *t);
    1270        3180 :                 sql_create_func(sa, "sql_div", "calc", "/", FALSE, FALSE, SCALE_DIV, 0, *t, 2, *t, *t);
    1271        3180 :                 if (t < floats) {
    1272        2650 :                         sql_create_func(sa, "bit_and", "calc", "and", FALSE, FALSE, SCALE_FIX, 0, *t, 2, *t, *t);
    1273        2650 :                         sql_create_func(sa, "bit_or", "calc", "or", FALSE, FALSE, SCALE_FIX, 0, *t, 2, *t, *t);
    1274        2650 :                         sql_create_func(sa, "bit_xor", "calc", "xor", FALSE, FALSE, SCALE_FIX, 0, *t, 2, *t, *t);
    1275        2650 :                         sql_create_func(sa, "bit_not", "calc", "not", FALSE, FALSE, SCALE_FIX, 0, *t, 1, *t);
    1276        2650 :                         sql_create_func(sa, "left_shift", "calc", "<<", FALSE, FALSE, SCALE_FIX, 0, *t, 2, *t, INT);
    1277        2650 :                         sql_create_func(sa, "right_shift", "calc", ">>", FALSE, FALSE, SCALE_FIX, 0, *t, 2, *t, INT);
    1278             :                 }
    1279        3180 :                 sql_create_func(sa, "sql_neg", "calc", "-", FALSE, FALSE, INOUT, 0, *t, 1, *t);
    1280        3180 :                 sql_create_func(sa, "abs", "calc", "abs", FALSE, FALSE, SCALE_FIX, 0, *t, 1, *t);
    1281        3180 :                 sql_create_func(sa, "sign", "calc", "sign", FALSE, FALSE, SCALE_NONE, 0, BTE, 1, *t);
    1282             :                 /* scale fixing for all numbers */
    1283        3180 :                 sql_create_func(sa, "scale_up", "calc", "*", FALSE, FALSE, SCALE_NONE, 0, *t, 2, *t, lt->type);
    1284        3180 :                 sql_create_func(sa, "scale_down", "calc", "dec_round", FALSE, FALSE, SCALE_NONE, 0, *t, 2, *t, lt->type);
    1285             :                 /* numeric functions on INTERVALS */
    1286        3180 :                 if (t >= floats || (*t)->localtype <= MONINT->localtype)
    1287        2120 :                         sql_create_func(sa, "sql_mul", "calc", "*", FALSE, FALSE, SCALE_MUL, 0, MONINT, 2, MONINT, *t);
    1288        3180 :                 if (t >= floats || (*t)->localtype <= DAYINT->localtype)
    1289        2650 :                         sql_create_func(sa, "sql_mul", "calc", "*", FALSE, FALSE, SCALE_MUL, 0, DAYINT, 2, DAYINT, *t);
    1290        3180 :                 if (t >= floats || (*t)->localtype <= SECINT->localtype)
    1291        2650 :                         sql_create_func(sa, "sql_mul", "calc", "*", FALSE, FALSE, SCALE_MUL, 0, SECINT, 2, SECINT, *t);
    1292        3180 :                 sql_create_func(sa, "sql_div", "calc", "/", FALSE, FALSE, SCALE_DIV, 0, MONINT, 2, MONINT, *t);
    1293        3180 :                 sql_create_func(sa, "sql_div", "calc", "/", FALSE, FALSE, SCALE_DIV, 0, DAYINT, 2, DAYINT, *t);
    1294        3180 :                 sql_create_func(sa, "sql_div", "calc", "/", FALSE, FALSE, SCALE_DIV, 0, SECINT, 2, SECINT, *t);
    1295             :         }
    1296             : 
    1297        1325 :         for (t = decimals, t++; t != floats; t++) {
    1298             :                 sql_type **u;
    1299       12720 :                 for (u = numerical; u != floats; u++) {
    1300       11660 :                         if (*u == OID)
    1301        1060 :                                 continue;
    1302       10600 :                         if ((*t)->localtype > (*u)->localtype) {
    1303        5300 :                                 sql_create_func(sa, "sql_mul", "calc", "*", FALSE, FALSE, SCALE_MUL, 0, *t, 2, *t, *u);
    1304        5300 :                                 sql_create_func(sa, "sql_mul", "calc", "*", FALSE, FALSE, SCALE_MUL, 0, *t, 2, *u, *t);
    1305             :                         }
    1306             :                 }
    1307             :         }
    1308             : 
    1309        2120 :         for (t = decimals; t < dates; t++)
    1310        1855 :                 sql_create_func(sa, "round", "calc", "round", FALSE, FALSE, INOUT, 0, *t, 2, *t, BTE);
    1311             : 
    1312        4505 :         for (t = numerical; *t != TME; t++) {
    1313        4240 :                 if (*t == OID || *t == FLT || *t == DBL)
    1314         795 :                         continue;
    1315       58565 :                 for (sql_type **u = numerical; *u != TME; u++) {
    1316       55120 :                         if (*u == OID || *u == FLT || *u == DBL)
    1317       10335 :                                 continue;
    1318       44785 :                         if ((*t)->localtype > (*u)->localtype) {
    1319       17490 :                                 sql_create_func(sa, "scale_up", "calc", "*", FALSE, FALSE, SCALE_NONE, 0, *t, 2, *t, *u);
    1320       17490 :                                 sql_create_func(sa, "scale_up", "calc", "*", FALSE, FALSE, SCALE_NONE, 0, *t, 2, *u, *t);
    1321             :                         }
    1322             :                 }
    1323             :         }
    1324             : 
    1325         795 :         for (t = floats; t < dates; t++) {
    1326         530 :                 sql_create_func(sa, "power", "mmath", "pow", FALSE, FALSE, SCALE_FIX, 0, *t, 2, *t, *t);
    1327         530 :                 sql_create_func(sa, "floor", "mmath", "floor", FALSE, FALSE, SCALE_FIX, 0, *t, 1, *t);
    1328         530 :                 sql_create_func(sa, "ceil", "mmath", "ceil", FALSE, FALSE, SCALE_FIX, 0, *t, 1, *t);
    1329         530 :                 sql_create_func(sa, "ceiling", "mmath", "ceil", FALSE, FALSE, SCALE_FIX, 0, *t, 1, *t);   /* JDBC */
    1330         530 :                 sql_create_func(sa, "sin", "mmath", "sin", FALSE, FALSE, SCALE_FIX, 0, *t, 1, *t);
    1331         530 :                 sql_create_func(sa, "cos", "mmath", "cos", FALSE, FALSE, SCALE_FIX, 0, *t, 1, *t);
    1332         530 :                 sql_create_func(sa, "tan", "mmath", "tan", FALSE, FALSE, SCALE_FIX, 0, *t, 1, *t);
    1333         530 :                 sql_create_func(sa, "asin", "mmath", "asin", FALSE, FALSE, SCALE_FIX, 0, *t, 1, *t);
    1334         530 :                 sql_create_func(sa, "acos", "mmath", "acos", FALSE, FALSE, SCALE_FIX, 0, *t, 1, *t);
    1335         530 :                 sql_create_func(sa, "atan", "mmath", "atan", FALSE, FALSE, SCALE_FIX, 0, *t, 1, *t);
    1336         530 :                 sql_create_func(sa, "atan", "mmath", "atan2", FALSE, FALSE, SCALE_FIX, 0, *t, 2, *t, *t);
    1337         530 :                 sql_create_func(sa, "sinh", "mmath", "sinh", FALSE, FALSE, SCALE_FIX, 0, *t, 1, *t);
    1338         530 :                 sql_create_func(sa, "cot", "mmath", "cot", FALSE, FALSE, SCALE_FIX, 0, *t, 1, *t);
    1339         530 :                 sql_create_func(sa, "cosh", "mmath", "cosh", FALSE, FALSE, SCALE_FIX, 0, *t, 1, *t);
    1340         530 :                 sql_create_func(sa, "tanh", "mmath", "tanh", FALSE, FALSE, SCALE_FIX, 0, *t, 1, *t);
    1341         530 :                 sql_create_func(sa, "sqrt", "mmath", "sqrt", FALSE, FALSE, SCALE_FIX, 0, *t, 1, *t);
    1342         530 :                 sql_create_func(sa, "cbrt", "mmath", "cbrt", FALSE, FALSE, SCALE_FIX, 0, *t, 1, *t);
    1343         530 :                 sql_create_func(sa, "exp", "mmath", "exp", FALSE, FALSE, SCALE_FIX, 0, *t, 1, *t);
    1344         530 :                 sql_create_func(sa, "log", "mmath", "log", FALSE, FALSE, SCALE_FIX, 0, *t, 1, *t);
    1345         530 :                 sql_create_func(sa, "ln", "mmath", "log", FALSE, FALSE, SCALE_FIX, 0, *t, 1, *t);
    1346         530 :                 sql_create_func(sa, "log", "mmath", "log2arg", FALSE, FALSE, SCALE_FIX, 0, *t, 2, *t, *t);
    1347         530 :                 sql_create_func(sa, "log10", "mmath", "log10", FALSE, FALSE, SCALE_FIX, 0, *t, 1, *t);
    1348         530 :                 sql_create_func(sa, "log2", "mmath", "log2", FALSE, FALSE, SCALE_FIX, 0, *t, 1, *t);
    1349         530 :                 sql_create_func(sa, "degrees", "mmath", "degrees", FALSE, FALSE, SCALE_FIX, 0, *t, 1, *t);
    1350         530 :                 sql_create_func(sa, "radians", "mmath", "radians", FALSE, FALSE, SCALE_FIX, 0, *t, 1, *t);
    1351             :         }
    1352         265 :         sql_create_func(sa, "pi", "mmath", "pi", FALSE, FALSE, SCALE_NONE, 0, DBL, 0);
    1353             : 
    1354         265 :         sql_create_func(sa, "rand", "mmath", "rand", TRUE, TRUE, SCALE_NONE, 0, INT, 0);
    1355         265 :         sql_create_func(sa, "rand", "mmath", "sqlrand", TRUE, TRUE, SCALE_NONE, 0, INT, 1, INT);
    1356             : 
    1357             :         /* Date functions */
    1358         265 :         sql_create_func(sa, "curdate", "mtime", "current_date", FALSE, FALSE, SCALE_NONE, 0, DTE, 0);
    1359         265 :         sql_create_func(sa, "current_date", "mtime", "current_date", FALSE, FALSE, SCALE_NONE, 0, DTE, 0);
    1360         265 :         sql_create_func(sa, "curtime", "mtime", "current_time", FALSE, FALSE, SCALE_NONE, 0, TMETZ, 0);
    1361         265 :         sql_create_func(sa, "current_time", "mtime", "current_time", FALSE, FALSE, SCALE_NONE, 0, TMETZ, 0);
    1362         265 :         sql_create_func(sa, "current_timestamp", "mtime", "current_timestamp", FALSE, FALSE, SCALE_NONE, 0, TMESTAMPTZ, 0);
    1363         265 :         sql_create_func(sa, "localtime", "sql", "current_time", FALSE, FALSE, SCALE_NONE, 0, TME, 0);
    1364         265 :         sql_create_func(sa, "localtimestamp", "sql", "current_timestamp", FALSE, FALSE, SCALE_NONE, 0, TMESTAMP, 0);
    1365             : 
    1366         265 :         sql_create_func(sa, "sql_sub", "mtime", "diff", FALSE, FALSE, SCALE_FIX, 0, DAYINT, 2, DTE, DTE);
    1367         265 :         sql_create_func(sa, "sql_sub", "mtime", "diff", FALSE, FALSE, SCALE_NONE, 0, SECINT, 2, TMETZ, TMETZ);
    1368         265 :         sql_create_func(sa, "sql_sub", "mtime", "diff", FALSE, FALSE, SCALE_FIX, 0, SECINT, 2, TME, TME);
    1369         265 :         sql_create_func(sa, "sql_sub", "mtime", "diff", FALSE, FALSE, SCALE_NONE, 0, SECINT, 2, TMESTAMPTZ, TMESTAMPTZ);
    1370         265 :         sql_create_func(sa, "sql_sub", "mtime", "diff", FALSE, FALSE, SCALE_FIX, 0, SECINT, 2, TMESTAMP, TMESTAMP);
    1371             : 
    1372         265 :         sql_create_func(sa, "sql_sub", "mtime", "date_sub_msec_interval", FALSE, FALSE, SCALE_NONE, 0, DTE, 2, DTE, SECINT);
    1373         265 :         sql_create_func(sa, "sql_sub", "mtime", "date_sub_msec_interval", FALSE, FALSE, SCALE_NONE, 0, DTE, 2, DTE, DAYINT);
    1374         265 :         sql_create_func(sa, "sql_sub", "mtime", "date_sub_month_interval", FALSE, FALSE, SCALE_NONE, 0, DTE, 2, DTE, MONINT);
    1375         265 :         sql_create_func(sa, "sql_sub", "mtime", "time_sub_msec_interval", FALSE, FALSE, SCALE_NONE, 0, TME, 2, TME, SECINT);
    1376         265 :         sql_create_func(sa, "sql_sub", "mtime", "time_sub_msec_interval", FALSE, FALSE, SCALE_NONE, 0, TMETZ, 2, TMETZ, SECINT);
    1377         265 :         sql_create_func(sa, "sql_sub", "mtime", "timestamp_sub_msec_interval", FALSE, FALSE, SCALE_NONE, 0, TMESTAMP, 2, TMESTAMP, SECINT);
    1378         265 :         sql_create_func(sa, "sql_sub", "mtime", "timestamp_sub_msec_interval", FALSE, FALSE, SCALE_NONE, 0, TMESTAMP, 2, TMESTAMP, DAYINT);
    1379         265 :         sql_create_func(sa, "sql_sub", "mtime", "timestamp_sub_month_interval", FALSE, FALSE, SCALE_NONE, 0, TMESTAMP, 2, TMESTAMP, MONINT);
    1380         265 :         sql_create_func(sa, "sql_sub", "mtime", "timestamp_sub_msec_interval", FALSE, FALSE, SCALE_NONE, 0, TMESTAMPTZ, 2, TMESTAMPTZ, SECINT);
    1381         265 :         sql_create_func(sa, "sql_sub", "mtime", "timestamp_sub_msec_interval", FALSE, FALSE, SCALE_NONE, 0, TMESTAMPTZ, 2, TMESTAMPTZ, DAYINT);
    1382         265 :         sql_create_func(sa, "sql_sub", "mtime", "timestamp_sub_month_interval", FALSE, FALSE, SCALE_NONE, 0, TMESTAMPTZ, 2, TMESTAMPTZ, MONINT);
    1383             : 
    1384         265 :         sql_create_func(sa, "sql_add", "mtime", "date_add_msec_interval", FALSE, FALSE, SCALE_NONE, 0, DTE, 2, DTE, SECINT);
    1385         265 :         sql_create_func(sa, "sql_add", "mtime", "date_add_msec_interval", FALSE, FALSE, SCALE_NONE, 0, DTE, 2, DTE, DAYINT);
    1386         265 :         sql_create_func(sa, "sql_add", "mtime", "addmonths", FALSE, FALSE, SCALE_NONE, 0, DTE, 2, DTE, MONINT);
    1387         265 :         sql_create_func(sa, "sql_add", "mtime", "timestamp_add_msec_interval", FALSE, FALSE, SCALE_NONE, 0, TMESTAMP, 2, TMESTAMP, SECINT);
    1388         265 :         sql_create_func(sa, "sql_add", "mtime", "timestamp_add_msec_interval", FALSE, FALSE, SCALE_NONE, 0, TMESTAMP, 2, TMESTAMP, DAYINT);
    1389         265 :         sql_create_func(sa, "sql_add", "mtime", "timestamp_add_month_interval", FALSE, FALSE, SCALE_NONE, 0, TMESTAMP, 2, TMESTAMP, MONINT);
    1390         265 :         sql_create_func(sa, "sql_add", "mtime", "timestamp_add_msec_interval", FALSE, FALSE, SCALE_NONE, 0, TMESTAMPTZ, 2, TMESTAMPTZ, SECINT);
    1391         265 :         sql_create_func(sa, "sql_add", "mtime", "timestamp_add_msec_interval", FALSE, FALSE, SCALE_NONE, 0, TMESTAMPTZ, 2, TMESTAMPTZ, DAYINT);
    1392         265 :         sql_create_func(sa, "sql_add", "mtime", "timestamp_add_month_interval", FALSE, FALSE, SCALE_NONE, 0, TMESTAMPTZ, 2, TMESTAMPTZ, MONINT);
    1393         265 :         sql_create_func(sa, "sql_add", "mtime", "time_add_msec_interval", FALSE, FALSE, SCALE_NONE, 0, TME, 2, TME, SECINT);
    1394         265 :         sql_create_func(sa, "sql_add", "mtime", "time_add_msec_interval", FALSE, FALSE, SCALE_NONE, 0, TMETZ, 2, TMETZ, SECINT);
    1395             : 
    1396         265 :         sql_create_func(sa, "local_timezone", "mtime", "local_timezone", FALSE, FALSE, SCALE_FIX, 0, SECINT, 0);
    1397             : 
    1398         265 :         sql_create_func(sa, "century", "mtime", "century", FALSE, FALSE, SCALE_FIX, 0, INT, 1, DTE);
    1399         265 :         sql_create_func(sa, "decade", "mtime", "decade", FALSE, FALSE, SCALE_FIX, 0, INT, 1, DTE);
    1400         265 :         sql_create_func(sa, "year", "mtime", "year", FALSE, FALSE, SCALE_FIX, 0, INT, 1, DTE);
    1401         265 :         sql_create_func(sa, "quarter", "mtime", "quarter", FALSE, FALSE, SCALE_FIX, 0, INT, 1, DTE);
    1402         265 :         sql_create_func(sa, "month", "mtime", "month", FALSE, FALSE, SCALE_FIX, 0, INT, 1, DTE);
    1403         265 :         sql_create_func(sa, "day", "mtime", "day", FALSE, FALSE, SCALE_FIX, 0, INT, 1, DTE);
    1404         265 :         sql_create_func(sa, "dayofyear", "mtime", "dayofyear", FALSE, FALSE, SCALE_FIX, 0, INT, 1, DTE);
    1405         265 :         sql_create_func(sa, "weekofyear", "mtime", "weekofyear", FALSE, FALSE, SCALE_FIX, 0, INT, 1, DTE);
    1406         265 :         sql_create_func(sa, "usweekofyear", "mtime", "usweekofyear", FALSE, FALSE, SCALE_FIX, 0, INT, 1, DTE);
    1407         265 :         sql_create_func(sa, "dayofweek", "mtime", "dayofweek", FALSE, FALSE, SCALE_FIX, 0, INT, 1, DTE);
    1408         265 :         sql_create_func(sa, "dayofmonth", "mtime", "day", FALSE, FALSE, SCALE_FIX, 0, INT, 1, DTE);
    1409         265 :         sql_create_func(sa, "week", "mtime", "weekofyear", FALSE, FALSE, SCALE_FIX, 0, INT, 1, DTE);
    1410         265 :         sql_create_func(sa, "epoch_ms", "mtime", "epoch_ms", FALSE, FALSE, SCALE_NONE, 3, BigDEC, 1, DTE);
    1411             : 
    1412         265 :         sql_create_func(sa, "hour", "mtime", "hours", FALSE, FALSE, SCALE_FIX, 0, INT, 1, TME);
    1413         265 :         sql_create_func(sa, "minute", "mtime", "minutes", FALSE, FALSE, SCALE_FIX, 0, INT, 1, TME);
    1414         265 :         sql_create_func(sa, "second", "mtime", "sql_seconds", FALSE, FALSE, SCALE_NONE, 6, DEC, 1, TME);
    1415         265 :         sql_create_func(sa, "epoch_ms", "mtime", "epoch_ms", FALSE, FALSE, SCALE_NONE, 3, BigDEC, 1, TME);
    1416         265 :         sql_create_func(sa, "hour", "mtime", "hours", FALSE, FALSE, SCALE_FIX, 0, INT, 1, TMETZ);
    1417         265 :         sql_create_func(sa, "minute", "mtime", "minutes", FALSE, FALSE, SCALE_FIX, 0, INT, 1, TMETZ);
    1418         265 :         sql_create_func(sa, "second", "mtime", "sql_seconds", FALSE, FALSE, SCALE_NONE, 6, DEC, 1, TMETZ);
    1419         265 :         sql_create_func(sa, "epoch_ms", "mtime", "epoch_ms", FALSE, FALSE, SCALE_NONE, 3, BigDEC, 1, TMETZ);
    1420             : 
    1421         265 :         sql_create_func(sa, "century", "mtime", "century", FALSE, FALSE, SCALE_FIX, 0, INT, 1, TMESTAMP);
    1422         265 :         sql_create_func(sa, "decade", "mtime", "decade", FALSE, FALSE, SCALE_FIX, 0, INT, 1, TMESTAMP);
    1423         265 :         sql_create_func(sa, "year", "mtime", "year", FALSE, FALSE, SCALE_FIX, 0, INT, 1, TMESTAMP);
    1424         265 :         sql_create_func(sa, "quarter", "mtime", "quarter", FALSE, FALSE, SCALE_FIX, 0, INT, 1, TMESTAMP);
    1425         265 :         sql_create_func(sa, "month", "mtime", "month", FALSE, FALSE, SCALE_FIX, 0, INT, 1, TMESTAMP);
    1426         265 :         sql_create_func(sa, "day", "mtime", "day", FALSE, FALSE, SCALE_FIX, 0, INT, 1, TMESTAMP);
    1427         265 :         sql_create_func(sa, "hour", "mtime", "hours", FALSE, FALSE, SCALE_FIX, 0, INT, 1, TMESTAMP);
    1428         265 :         sql_create_func(sa, "minute", "mtime", "minutes", FALSE, FALSE, SCALE_FIX, 0, INT, 1, TMESTAMP);
    1429         265 :         sql_create_func(sa, "second", "mtime", "sql_seconds", FALSE, FALSE, SCALE_NONE, 6, DEC, 1, TMESTAMP);
    1430         265 :         sql_create_func(sa, "epoch_ms", "mtime", "epoch_ms", FALSE, FALSE, SCALE_NONE, 3, BigDEC, 1, TMESTAMP);
    1431             : 
    1432         265 :         sql_create_func(sa, "century", "mtime", "century", FALSE, FALSE, SCALE_FIX, 0, INT, 1, TMESTAMPTZ);
    1433         265 :         sql_create_func(sa, "decade", "mtime", "decade", FALSE, FALSE, SCALE_FIX, 0, INT, 1, TMESTAMPTZ);
    1434         265 :         sql_create_func(sa, "year", "mtime", "year", FALSE, FALSE, SCALE_FIX, 0, INT, 1, TMESTAMPTZ);
    1435         265 :         sql_create_func(sa, "quarter", "mtime", "quarter", FALSE, FALSE, SCALE_FIX, 0, INT, 1, TMESTAMPTZ);
    1436         265 :         sql_create_func(sa, "month", "mtime", "month", FALSE, FALSE, SCALE_FIX, 0, INT, 1, TMESTAMPTZ);
    1437         265 :         sql_create_func(sa, "day", "mtime", "day", FALSE, FALSE, SCALE_FIX, 0, INT, 1, TMESTAMPTZ);
    1438         265 :         sql_create_func(sa, "hour", "mtime", "hours", FALSE, FALSE, SCALE_FIX, 0, INT, 1, TMESTAMPTZ);
    1439         265 :         sql_create_func(sa, "minute", "mtime", "minutes", FALSE, FALSE, SCALE_FIX, 0, INT, 1, TMESTAMPTZ);
    1440         265 :         sql_create_func(sa, "second", "mtime", "sql_seconds", FALSE, FALSE, SCALE_NONE, 6, DEC, 1, TMESTAMPTZ);
    1441         265 :         sql_create_func(sa, "epoch_ms", "mtime", "epoch_ms", FALSE, FALSE, SCALE_NONE, 3, BigDEC, 1, TMESTAMPTZ);
    1442             : 
    1443         265 :         sql_create_func(sa, "year", "mtime", "year", FALSE, FALSE, SCALE_NONE, 0, INT, 1, MONINT);
    1444         265 :         sql_create_func(sa, "month", "mtime", "month", FALSE, FALSE, SCALE_NONE, 0, INT, 1, MONINT);
    1445         265 :         sql_create_func(sa, "day", "mtime", "day", FALSE, FALSE, SCALE_NONE, 0, LNG, 1, DAYINT);
    1446         265 :         sql_create_func(sa, "hour", "mtime", "hours", FALSE, FALSE, SCALE_NONE, 0, INT, 1, DAYINT);
    1447         265 :         sql_create_func(sa, "minute", "mtime", "minutes", FALSE, FALSE, SCALE_NONE, 0, INT, 1, DAYINT);
    1448         265 :         sql_create_func(sa, "second", "mtime", "seconds", FALSE, FALSE, SCALE_NONE, 0, INT, 1, DAYINT);
    1449         265 :         sql_create_func(sa, "epoch_ms", "mtime", "epoch_ms", FALSE, FALSE, SCALE_NONE, 3, BigDEC, 1, DAYINT);
    1450         265 :         sql_create_func(sa, "day", "mtime", "day", FALSE, FALSE, SCALE_NONE, 0, LNG, 1, SECINT);
    1451         265 :         sql_create_func(sa, "hour", "mtime", "hours", FALSE, FALSE, SCALE_NONE, 0, INT, 1, SECINT);
    1452         265 :         sql_create_func(sa, "minute", "mtime", "minutes", FALSE, FALSE, SCALE_NONE, 0, INT, 1, SECINT);
    1453         265 :         sql_create_func(sa, "second", "mtime", "seconds", FALSE, FALSE, SCALE_NONE, 0, INT, 1, SECINT);
    1454         265 :         sql_create_func(sa, "epoch_ms", "mtime", "epoch_ms", FALSE, FALSE, SCALE_NONE, 3, BigDEC, 1, SECINT);
    1455             : 
    1456        1060 :         for (t = strings; t < numerical; t++) {
    1457         795 :                 sql_create_func(sa, "next_value_for", "sql", "next_value", TRUE, TRUE, SCALE_NONE, 0, LNG, 2, *t, *t);
    1458         795 :                 sql_create_func(sa, "get_value_for", "sql", "get_value", TRUE, FALSE, SCALE_NONE, 0, LNG, 2, *t, *t);
    1459         795 :                 sql_create_func(sa, "restart", "sql", "restart", TRUE, TRUE, SCALE_NONE, 0, LNG, 3, *t, *t, LNG);
    1460             : 
    1461         795 :                 sql_create_func(sa, "index", "sql", "index", TRUE, FALSE, SCALE_NONE, 0, BTE, 2, *t, BIT);
    1462         795 :                 sql_create_func(sa, "index", "sql", "index", TRUE, FALSE, SCALE_NONE, 0, SHT, 2, *t, BIT);
    1463         795 :                 sql_create_func(sa, "index", "sql", "index", TRUE, FALSE, SCALE_NONE, 0, INT, 2, *t, BIT);
    1464         795 :                 sql_create_func(sa, "strings", "sql", "strings", FALSE, FALSE, SCALE_NONE, 0, *t, 1, *t);
    1465             : 
    1466         795 :                 sql_create_func(sa, "locate", "str", "locate", FALSE, FALSE, SCALE_NONE, 0, INT, 2, *t, *t);
    1467         795 :                 sql_create_func(sa, "locate", "str", "locate3", FALSE, FALSE, SCALE_NONE, 0, INT, 3, *t, *t, INT);
    1468         795 :                 sql_create_func(sa, "charindex", "str", "locate", FALSE, FALSE, SCALE_NONE, 0, INT, 2, *t, *t);
    1469         795 :                 sql_create_func(sa, "charindex", "str", "locate3", FALSE, FALSE, SCALE_NONE, 0, INT, 3, *t, *t, INT);
    1470         795 :                 sql_create_func(sa, "splitpart", "str", "splitpart", FALSE, FALSE, INOUT, 0, *t, 3, *t, *t, INT);
    1471         795 :                 sql_create_func(sa, "substring", "str", "substring", FALSE, FALSE, INOUT, 0, *t, 2, *t, INT);
    1472         795 :                 sql_create_func(sa, "substring", "str", "substring3", FALSE, FALSE, INOUT, 0, *t, 3, *t, INT, INT);
    1473         795 :                 sql_create_func(sa, "substr", "str", "substring", FALSE, FALSE, INOUT, 0, *t, 2, *t, INT);
    1474         795 :                 sql_create_func(sa, "substr", "str", "substring3", FALSE, FALSE, INOUT, 0, *t, 3, *t, INT, INT);
    1475             : 
    1476         795 :                 sql_create_filter(sa, "like", "algebra", "like", FALSE, FALSE, SCALE_NONE, 0, 4, *t, *t, *t, BIT);
    1477         795 :                 sql_create_filter(sa, "not_like", "algebra", "not_like", FALSE, FALSE, SCALE_NONE, 0, 4, *t, *t, *t, BIT);
    1478             : 
    1479         795 :                 sql_create_func(sa, "patindex", "pcre", "patindex", FALSE, FALSE, SCALE_NONE, 0, INT, 2, *t, *t);
    1480         795 :                 sql_create_func(sa, "truncate", "str", "stringleft", FALSE, FALSE, SCALE_NONE, 0, *t, 2, *t, INT);
    1481         795 :                 sql_create_func(sa, "concat", "calc", "+", FALSE, FALSE, DIGITS_ADD, 0, *t, 2, *t, *t);
    1482         795 :                 sql_create_func(sa, "ascii", "str", "ascii", TRUE, FALSE, SCALE_NONE, 0, INT, 1, *t); /* ascii of empty string is null */
    1483         795 :                 sql_create_func(sa, "code", "str", "unicode", FALSE, FALSE, SCALE_NONE, 0, *t, 1, INT);
    1484         795 :                 sql_create_func(sa, "length", "str", "length", FALSE, FALSE, SCALE_NONE, 0, INT, 1, *t);
    1485         795 :                 sql_create_func(sa, "right", "str", "stringright", FALSE, FALSE, SCALE_NONE, 0, *t, 2, *t, INT);
    1486         795 :                 sql_create_func(sa, "left", "str", "stringleft", FALSE, FALSE, SCALE_NONE, 0, *t, 2, *t, INT);
    1487         795 :                 sql_create_func(sa, "upper", "str", "toUpper", FALSE, FALSE, SCALE_NONE, 0, *t, 1, *t);
    1488         795 :                 sql_create_func(sa, "ucase", "str", "toUpper", FALSE, FALSE, SCALE_NONE, 0, *t, 1, *t);
    1489         795 :                 sql_create_func(sa, "lower", "str", "toLower", FALSE, FALSE, SCALE_NONE, 0, *t, 1, *t);
    1490         795 :                 sql_create_func(sa, "lcase", "str", "toLower", FALSE, FALSE, SCALE_NONE, 0, *t, 1, *t);
    1491         795 :                 sql_create_func(sa, "trim", "str", "trim", FALSE, FALSE, SCALE_NONE, 0, *t, 1, *t);
    1492         795 :                 sql_create_func(sa, "trim", "str", "trim2", FALSE, FALSE, SCALE_NONE, 0, *t, 2, *t, *t);
    1493         795 :                 sql_create_func(sa, "ltrim", "str", "ltrim", FALSE, FALSE, SCALE_NONE, 0, *t, 1, *t);
    1494         795 :                 sql_create_func(sa, "ltrim", "str", "ltrim2", FALSE, FALSE, SCALE_NONE, 0, *t, 2, *t, *t);
    1495         795 :                 sql_create_func(sa, "rtrim", "str", "rtrim", FALSE, FALSE, SCALE_NONE, 0, *t, 1, *t);
    1496         795 :                 sql_create_func(sa, "rtrim", "str", "rtrim2", FALSE, FALSE, SCALE_NONE, 0, *t, 2, *t, *t);
    1497             : 
    1498         795 :                 sql_create_func(sa, "lpad", "str", "lpad", FALSE, FALSE, SCALE_NONE, 0, *t, 2, *t, INT);
    1499         795 :                 sql_create_func(sa, "lpad", "str", "lpad3", FALSE, FALSE, SCALE_NONE, 0, *t, 3, *t, INT, *t);
    1500         795 :                 sql_create_func(sa, "rpad", "str", "rpad", FALSE, FALSE, SCALE_NONE, 0, *t, 2, *t, INT);
    1501         795 :                 sql_create_func(sa, "rpad", "str", "rpad3", FALSE, FALSE, SCALE_NONE, 0, *t, 3, *t, INT, *t);
    1502             : 
    1503         795 :                 sql_create_func(sa, "insert", "str", "insert", FALSE, FALSE, SCALE_NONE, 0, *t, 4, *t, INT, INT, *t);
    1504         795 :                 sql_create_func(sa, "replace", "str", "replace", FALSE, FALSE, SCALE_NONE, 0, *t, 3, *t, *t, *t);
    1505         795 :                 sql_create_func(sa, "repeat", "str", "repeat", TRUE, FALSE, SCALE_NONE, 0, *t, 2, *t, INT); /* repeat -1 times is null */
    1506         795 :                 sql_create_func(sa, "space", "str", "space", TRUE, FALSE, SCALE_NONE, 0, *t, 1, INT); /* space -1 times is null */
    1507         795 :                 sql_create_func(sa, "char_length", "str", "length", FALSE, FALSE, SCALE_NONE, 0, INT, 1, *t);
    1508         795 :                 sql_create_func(sa, "character_length", "str", "length", FALSE, FALSE, SCALE_NONE, 0, INT, 1, *t);
    1509         795 :                 sql_create_func(sa, "octet_length", "str", "nbytes", FALSE, FALSE, SCALE_NONE, 0, INT, 1, *t);
    1510             : 
    1511         795 :                 sql_create_func(sa, "soundex", "txtsim", "soundex", FALSE, FALSE, SCALE_NONE, 0, *t, 1, *t);
    1512         795 :                 sql_create_func(sa, "difference", "txtsim", "stringdiff", TRUE, FALSE, SCALE_NONE, 0, INT, 2, *t, *t);
    1513         795 :                 sql_create_func(sa, "editdistance", "txtsim", "editdistance", TRUE, FALSE, SCALE_FIX, 0, INT, 2, *t, *t);
    1514         795 :                 sql_create_func(sa, "editdistance2", "txtsim", "editdistance2", TRUE, FALSE, SCALE_FIX, 0, INT, 2, *t, *t);
    1515             : 
    1516         795 :                 sql_create_func(sa, "similarity", "txtsim", "similarity", TRUE, FALSE, SCALE_FIX, 0, DBL, 2, *t, *t);
    1517         795 :                 sql_create_func(sa, "qgramnormalize", "txtsim", "qgramnormalize", FALSE, FALSE, SCALE_NONE, 0, *t, 1, *t);
    1518             : 
    1519         795 :                 sql_create_func(sa, "levenshtein", "txtsim", "levenshtein", TRUE, FALSE, SCALE_FIX, 0, INT, 2, *t, *t);
    1520         795 :                 sql_create_func(sa, "levenshtein", "txtsim", "levenshtein", TRUE, FALSE, SCALE_FIX, 0, INT, 5, *t, *t, INT, INT, INT);
    1521             :         }
    1522             :         /* copyfrom fname (arg 12) */
    1523         265 :         f = sql_create_union(sa, "copyfrom", "sql", "copy_from", FALSE, SCALE_FIX, 0, TABLE, 12, PTR, STR, STR, STR, STR, STR, LNG, LNG, INT, STR, INT, INT);
    1524         265 :         f->varres = 1;
    1525             : 
    1526             :         /* bincopyfrom */
    1527         265 :         f = sql_create_union(sa, "copyfrom", "sql", "importTable", FALSE, SCALE_FIX, 0, TABLE, 3, STR, STR, INT);
    1528         265 :         f->varres = 1;
    1529             : 
    1530             :         /* sys_update_schemas, sys_update_tables */
    1531         265 :         sql_create_procedure(sa, "sys_update_schemas", "sql", "update_schemas", FALSE, 0);
    1532         265 :         sql_create_procedure(sa, "sys_update_tables", "sql", "update_tables", FALSE, 0);
    1533         265 : }
    1534             : 
    1535             : void
    1536         265 : types_init(sql_allocator *sa)
    1537             : {
    1538         265 :         local_id = 1;
    1539         265 :         aliases = sa_list(sa);
    1540         265 :         types = sa_list(sa);
    1541         265 :         localtypes = sa_list(sa);
    1542         265 :         funcs = sa_list(sa);
    1543         265 :         MT_lock_set(&funcs->ht_lock);
    1544         265 :         funcs->ht = hash_new(sa, 1024, (fkeyvalue)&base_key);
    1545         265 :         MT_lock_unset(&funcs->ht_lock);
    1546         265 :         sqltypeinit( sa );
    1547         265 : }

Generated by: LCOV version 1.14