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 - 2019 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#ifdef HAVE_HGE
24#include "mal.h" /* for have_hge */
25#endif
26#include <string.h>
27
28#define END_SUBAGGR 1
29#define END_AGGR 2
30#define END_SUBTYPE 3
31#define END_TYPE 4
32
33list *aliases = NULL;
34list *types = NULL;
35list *aggrs = NULL;
36list *funcs = NULL;
37
38static sql_type *BIT = NULL;
39static list *localtypes = NULL;
40
41int digits2bits(int digits)
42{
43 if (digits < 3)
44 return 8;
45 else if (digits < 5)
46 return 16;
47 else if (digits <= 5)
48 return 17;
49 else if (digits <= 6)
50 return 20;
51 else if (digits <= 7)
52 return 24;
53 else if (digits <= 8)
54 return 27;
55 else if (digits < 10)
56 return 32;
57 else if (digits < 17)
58 return 51;
59#ifdef HAVE_HGE
60 else if (digits < 19 || !have_hge)
61 return 64;
62 return 128;
63#else
64 return 64;
65#endif
66}
67
68int bits2digits(int bits)
69{
70 if (bits < 4)
71 return 1;
72 else if (bits < 7)
73 return 2;
74 else if (bits < 10)
75 return 3;
76 else if (bits < 14)
77 return 4;
78 else if (bits < 16)
79 return 5;
80 else if (bits < 20)
81 return 6;
82 else if (bits < 24)
83 return 7;
84 else if (bits <= 27)
85 return 8;
86 else if (bits <= 30)
87 return 9;
88 else if (bits <= 32)
89 return 10;
90#ifdef HAVE_HGE
91 else if (bits <= 64 || !have_hge)
92 return 19;
93 return 39;
94#else
95 return 19;
96#endif
97}
98
99/* 0 cannot convert */
100/* 1 set operations have very limited coersion rules */
101/* 2 automatic coersion (could still require dynamic checks for overflow) */
102/* 3 casts are allowed (requires dynamic checks) (sofar not used) */
103static int convert_matrix[EC_MAX][EC_MAX] = {
104
105/* EC_ANY */ { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, /* NULL */
106/* EC_TABLE */ { 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
107/* EC_BIT */ { 0, 0, 1, 1, 1, 0, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0 },
108/* EC_CHAR */ { 2, 2, 2, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 },
109/* EC_STRING */ { 2, 2, 2, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 },
110/* EC_BLOB */ { 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
111/* EC_POS */ { 0, 0, 2, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 },
112/* EC_NUM */ { 0, 0, 2, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 },
113/* EC_MONTH*/ { 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0 },
114/* EC_SEC*/ { 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0 },
115/* EC_DEC */ { 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0 },
116/* EC_FLT */ { 0, 0, 0, 1, 1, 0, 1, 1, 0, 3, 1, 1, 0, 0, 0, 0, 0 },
117/* EC_TIME */ { 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 },
118/* EC_DATE */ { 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 0, 0 },
119/* EC_TSTAMP */ { 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0 },
120/* EC_GEOM */ { 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0 },
121/* EC_EXTERNAL*/{ 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
122};
123
124int sql_type_convert (int from, int to)
125{
126 return convert_matrix[from][to];
127}
128
129bool is_commutative(const char *fnm)
130{
131 return strcmp("sql_add", fnm) == 0 ||
132 strcmp("sql_mul", fnm) == 0;
133}
134
135void
136base_init(sql_allocator *sa, sql_base * b, sqlid id, int flags, const char *name)
137{
138 b->id = id;
139
140 assert(sa);
141 b->wtime = 0;
142 b->rtime = 0;
143 b->stime = 0;
144 b->flags = flags;
145 b->name = NULL;
146 b->refcnt = 1;
147 if (name)
148 b->name = sa_strdup(sa,name);
149}
150
151void
152sql_init_subtype(sql_subtype *res, sql_type *t, unsigned int digits, unsigned int scale)
153{
154 res->type = t;
155 res->digits = digits ? digits : t->digits;
156 if (t->digits && res->digits > t->digits)
157 res->digits = t->digits;
158 res->scale = scale;
159}
160
161sql_subtype *
162sql_create_subtype(sql_allocator *sa, sql_type *t, unsigned int digits, unsigned int scale)
163{
164 sql_subtype *res = SA_ZNEW(sa, sql_subtype);
165
166 sql_init_subtype(res, t, digits, scale);
167 return res;
168}
169
170static bool
171localtypes_cmp(int nlt, int olt)
172{
173 if (nlt == TYPE_flt || nlt == TYPE_dbl) {
174 nlt = TYPE_dbl;
175#ifdef HAVE_HGE
176 } else if (nlt == TYPE_bte || nlt == TYPE_sht || nlt == TYPE_int || nlt == TYPE_lng || nlt == TYPE_hge) {
177 assert(have_hge || nlt != TYPE_hge);
178 nlt = have_hge ? TYPE_hge : TYPE_lng;
179#else
180 } else if (nlt == TYPE_bte || nlt == TYPE_sht || nlt == TYPE_int || nlt == TYPE_lng) {
181 nlt = TYPE_lng;
182#endif
183 }
184 return nlt == olt;
185}
186
187sql_subtype *
188sql_find_numeric(sql_subtype *r, int localtype, unsigned int digits)
189{
190 node *m, *n;
191
192 if (localtype == TYPE_flt || localtype == TYPE_dbl) {
193 localtype = TYPE_dbl;
194 } else {
195#ifdef HAVE_HGE
196 if (have_hge) {
197 localtype = TYPE_hge;
198 if (digits >= 128)
199 digits = 127;
200 } else
201#endif
202 {
203 localtype = TYPE_lng;
204 if (digits >= 64)
205 digits = 63;
206 }
207 }
208
209 for (n = types->h; n; n = n->next) {
210 sql_type *t = n->data;
211
212 if (localtypes_cmp(t->localtype, localtype)) {
213 if (digits == 0 ? t->digits == 0 : t->digits > digits) {
214 sql_init_subtype(r, t, digits, 0);
215 return r;
216 }
217 for (m = n->next; m; m = m->next) {
218 t = m->data;
219 if (!localtypes_cmp(t->localtype, localtype)) {
220 break;
221 }
222 n = m;
223 if (digits == 0 ? t->digits == 0 : t->digits > digits) {
224 sql_init_subtype(r, t, digits, 0);
225 return r;
226 }
227 }
228 }
229 }
230 return NULL;
231}
232
233int
234sql_find_subtype(sql_subtype *res, const char *name, unsigned int digits, unsigned int scale)
235{
236 /* todo add approximate info
237 * if digits/scale == 0 and no approximate with digits/scale == 0
238 * exists we could return the type with largest digits
239 *
240 * returning the largest when no exact match is found is now the
241 * (wrong?) default
242 */
243 /* assumes the types are ordered on name,digits,scale where is always
244 * 0 > n
245 */
246 node *m, *n;
247
248 for (n = types->h; n; n = n->next) {
249 sql_type *t = n->data;
250
251 if (t->sqlname[0] == name[0] && strcmp(t->sqlname, name) == 0) {
252 if ((digits && t->digits >= digits) || (digits == t->digits)) {
253 sql_init_subtype(res, t, digits, scale);
254 return 1;
255 }
256 for (m = n->next; m; m = m->next) {
257 t = m->data;
258 if (strcmp(t->sqlname, name) != 0) {
259 break;
260 }
261 n = m;
262 if ((digits && t->digits >= digits) || (digits == t->digits)) {
263 sql_init_subtype(res, t, digits, scale);
264 return 1;
265 }
266 }
267 t = n->data;
268 sql_init_subtype(res, t, digits, scale);
269 return 1;
270 }
271 }
272 return 0;
273}
274
275sql_subtype *
276sql_bind_subtype(sql_allocator *sa, const char *name, unsigned int digits, unsigned int scale)
277{
278 sql_subtype *res = (sa)?SA_ZNEW(sa, sql_subtype):ZNEW(sql_subtype);
279
280 if (!sql_find_subtype(res, name, digits, scale)) {
281 return NULL;
282 }
283 return res;
284}
285
286char *
287sql_subtype_string(sql_subtype *t)
288{
289 char buf[BUFSIZ];
290
291 if (t->digits && t->scale)
292 snprintf(buf, BUFSIZ, "%s(%u,%u)", t->type->sqlname, t->digits, t->scale);
293 else if (t->digits && t->type->radix != 2)
294 snprintf(buf, BUFSIZ, "%s(%u)", t->type->sqlname, t->digits);
295
296 else
297 snprintf(buf, BUFSIZ, "%s", t->type->sqlname);
298 return _STRDUP(buf);
299}
300
301sql_subtype *
302sql_bind_localtype(const char *name)
303{
304 node *n = localtypes->h;
305
306 while (n) {
307 sql_subtype *t = n->data;
308
309 if (strcmp(t->type->base.name, name) == 0) {
310 return t;
311 }
312 n = n->next;
313 }
314 assert(0);
315 return NULL;
316}
317
318int
319type_cmp(sql_type *t1, sql_type *t2)
320{
321 int res = 0;
322
323 if (!t1 || !t2)
324 return -1;
325 /* types are only equal
326 iff they map onto the same systemtype */
327 res = (t1->localtype - t2->localtype);
328 if (res)
329 return res;
330
331 /* iff they fall into the same equivalence class */
332 res = (t1->eclass - t2->eclass);
333 if (res)
334 return res;
335
336 /* external types with the same system type are treated equaly */
337 if (t1->eclass == EC_EXTERNAL)
338 return res;
339
340 /* sql base types need the same 'sql' name */
341 return (strcmp(t1->sqlname, t2->sqlname));
342}
343
344int
345subtype_cmp(sql_subtype *t1, sql_subtype *t2)
346{
347 if (!t1->type || !t2->type)
348 return -1;
349
350 if ( !(t1->type->eclass == t2->type->eclass &&
351 (EC_INTERVAL(t1->type->eclass) || t1->type->eclass == EC_NUM)) &&
352 (t1->digits != t2->digits ||
353 (!(t1->type->eclass == t2->type->eclass &&
354 t1->type->eclass == EC_FLT) &&
355 t1->scale != t2->scale)) )
356 return -1;
357
358 /* subtypes are only equal iff
359 they map onto the same systemtype */
360 return (type_cmp(t1->type, t2->type));
361}
362
363int
364is_subtype(sql_subtype *sub, sql_subtype *super)
365/* returns true if sub is a sub type of super */
366{
367 if (!sub || !super)
368 return 0;
369 if (super->digits > 0 && sub->digits > super->digits)
370 return 0;
371 if (super->digits == 0 && super->type->eclass == EC_STRING &&
372 (sub->type->eclass == EC_STRING || sub->type->eclass == EC_CHAR))
373 return 1;
374 if (super->digits != sub->digits && sub->type->eclass == EC_CHAR)
375 return 0;
376 /* subtypes are only equal iff
377 they map onto the same systemtype */
378 return (type_cmp(sub->type, super->type) == 0);
379}
380
381static int
382is_subtypeof(sql_subtype *sub, sql_subtype *super)
383/* returns true if sub is a sub type of super */
384{
385 if (!sub || !super)
386 return 0;
387 if (super->digits > 0 && sub->digits > super->digits)
388 return 0;
389 if (super->digits == 0 && super->type->eclass == EC_STRING &&
390 (sub->type->eclass == EC_STRING || sub->type->eclass == EC_CHAR))
391 return 1;
392 if (super->type->eclass == sub->type->eclass)
393 return 1;
394 /* subtypes are only equal iff
395 they map onto the same systemtype */
396 return (type_cmp(sub->type, super->type) == 0);
397}
398
399char *
400subtype2string(sql_subtype *t)
401{
402 char buf[BUFSIZ];
403
404 if (t->digits > 0) {
405 if (t->scale > 0)
406 snprintf(buf, BUFSIZ, "%s(%u,%u)",
407 t->type->sqlname, t->digits, t->scale);
408 else
409 snprintf(buf, BUFSIZ, "%s(%u)",
410 t->type->sqlname, t->digits);
411 } else {
412 snprintf(buf, BUFSIZ, "%s", t->type->sqlname);
413 }
414 return _STRDUP(buf);
415}
416
417char *
418subtype2string2(sql_subtype *tpe) //distinguish char(n), decimal(n,m) from other SQL types
419{
420 char buf[BUFSIZ];
421
422 switch (tpe->type->eclass) {
423 case EC_SEC:
424 snprintf(buf, BUFSIZ, "BIGINT");
425 break;
426 case EC_MONTH:
427 snprintf(buf, BUFSIZ, "INT");
428 break;
429 case EC_CHAR:
430 case EC_STRING:
431 case EC_DEC:
432 return subtype2string(tpe);
433 default:
434 snprintf(buf, BUFSIZ, "%s", tpe->type->sqlname);
435 }
436 return _STRDUP(buf);
437}
438
439int
440subaggr_cmp( sql_subaggr *a1, sql_subaggr *a2)
441{
442 if (a1->aggr == a2->aggr)
443 return list_cmp(a1->res, a2->res, (fcmp) &subtype_cmp);
444 return -1;
445}
446
447int
448subfunc_cmp( sql_subfunc *f1, sql_subfunc *f2)
449{
450 if (f1->func == f2->func)
451 return list_cmp(f1->res, f2->res, (fcmp) &subtype_cmp);
452 return -1;
453}
454
455int
456arg_subtype_cmp(sql_arg *a, sql_subtype *t)
457{
458 if (a->type.type->eclass == EC_ANY)
459 return 0;
460 return (is_subtype(t, &a->type )?0:-1);
461}
462
463static sql_subaggr *
464_dup_subaggr(sql_allocator *sa, sql_func *a, sql_subtype *member)
465{
466 node *tn;
467 unsigned int scale = 0, digits = 0;
468 sql_subaggr *ares = SA_ZNEW(sa, sql_subaggr);
469
470 assert (a->res);
471
472 ares->aggr = a;
473 ares->res = sa_list(sa);
474 for(tn = a->res->h; tn; tn = tn->next) {
475 sql_arg *rarg = tn->data;
476 sql_subtype *res, *r = &rarg->type;
477
478 digits = r->digits;
479 scale = r->scale;
480 /* same scale as the input */
481 if (member && (member->scale != scale ||
482 (digits != member->digits && !EC_NUMBER(member->type->eclass)))) {
483 if (member->digits > digits)
484 digits = member->digits;
485 scale = member->scale;
486 }
487 /* same type as the input */
488 if (r->type->eclass == EC_ANY)
489 r = member;
490 res = sql_create_subtype(sa, r->type, digits, scale);
491 list_append(ares->res, res);
492 }
493 return ares;
494}
495
496sql_subaggr *
497sql_bind_aggr(sql_allocator *sa, sql_schema *s, const char *sqlaname, sql_subtype *type)
498{
499 node *n = aggrs->h;
500
501 while (n) {
502 sql_func *a = n->data;
503 sql_arg *arg = NULL;
504
505 if (a->ops->h)
506 arg = a->ops->h->data;
507
508 if (strcmp(a->base.name, sqlaname) == 0 && (!arg ||
509 arg->type.type->eclass == EC_ANY ||
510 (type && is_subtype(type, &arg->type ))))
511 return _dup_subaggr(sa, a, type);
512 n = n->next;
513 }
514 if (s) {
515 node *n;
516
517 if (s->funcs.set) for (n=s->funcs.set->h; n; n = n->next) {
518 sql_func *a = n->data;
519 sql_arg *arg = NULL;
520
521 if ((!IS_AGGR(a) || !a->res))
522 continue;
523
524 if (a->ops->h)
525 arg = a->ops->h->data;
526
527 if (strcmp(a->base.name, sqlaname) == 0 && (!arg ||
528 arg->type.type->eclass == EC_ANY ||
529 (type && is_subtype(type, &arg->type ))))
530 return _dup_subaggr(sa, a, type);
531 }
532 }
533 return NULL;
534}
535
536sql_subaggr *
537sql_bind_aggr_(sql_allocator *sa, sql_schema *s, const char *sqlaname, list *ops)
538{
539 node *n = aggrs->h;
540 sql_subtype *type = NULL;
541
542 if (ops->h)
543 type = ops->h->data;
544
545 while (n) {
546 sql_func *a = n->data;
547
548 if (strcmp(a->base.name, sqlaname) == 0 &&
549 list_cmp(a->ops, ops, (fcmp) &arg_subtype_cmp) == 0)
550 return _dup_subaggr(sa, a, type);
551 n = n->next;
552 }
553 if (s) {
554 node *n;
555
556 if (s->funcs.set) for (n=s->funcs.set->h; n; n = n->next) {
557 sql_func *a = n->data;
558
559 if ((!IS_AGGR(a) || !a->res))
560 continue;
561
562 if (strcmp(a->base.name, sqlaname) == 0 &&
563 list_cmp(a->ops, ops, (fcmp) &arg_subtype_cmp) == 0)
564 return _dup_subaggr(sa, a, type);
565 }
566 }
567 return NULL;
568}
569
570sql_subaggr *
571sql_bind_member_aggr(sql_allocator *sa, sql_schema *s, const char *sqlaname, sql_subtype *type, int nrargs)
572{
573 node *n = aggrs->h;
574
575 while (n) {
576 sql_func *a = n->data;
577
578 if (strcmp(a->base.name, sqlaname) == 0 && list_length(a->ops) == nrargs &&
579 arg_subtype_cmp(a->ops->h->data, type) == 0)
580 return _dup_subaggr(sa, a, NULL);
581 n = n->next;
582 }
583 if (s) {
584 node *n;
585
586 if (s->funcs.set) for (n=s->funcs.set->h; n; n = n->next) {
587 sql_func *a = n->data;
588
589 if ((!IS_AGGR(a) || !a->res))
590 continue;
591
592 if (strcmp(a->base.name, sqlaname) == 0 && list_length(a->ops) == nrargs &&
593 arg_subtype_cmp(a->ops->h->data, type) == 0)
594 return _dup_subaggr(sa, a, NULL);
595 }
596 }
597 return NULL;
598}
599
600sql_subaggr *
601sql_find_aggr(sql_allocator *sa, sql_schema *s, const char *sqlaname)
602{
603 node *n = aggrs->h;
604
605 while (n) {
606 sql_func *a = n->data;
607
608 if (strcmp(a->base.name, sqlaname) == 0)
609 return _dup_subaggr(sa, a, NULL);
610 n = n->next;
611 }
612 if (s) {
613 node *n;
614
615 if (s->funcs.set) for (n=s->funcs.set->h; n; n = n->next) {
616 sql_func *a = n->data;
617
618 if ((!IS_AGGR(a) || !a->res))
619 continue;
620
621 if (strcmp(a->base.name, sqlaname) == 0)
622 return _dup_subaggr(sa, a, NULL);
623 }
624 }
625 return NULL;
626}
627
628char *
629sql_func_imp(sql_func *f)
630{
631 if (f->sql)
632 return f->base.name;
633 else
634 return f->imp;
635}
636
637char *
638sql_func_mod(sql_func *f)
639{
640 return f->mod;
641}
642
643int
644is_sqlfunc(sql_func *f)
645{
646 return f->sql;
647}
648
649sql_subfunc*
650sql_dup_subfunc(sql_allocator *sa, sql_func *f, list *ops, sql_subtype *member)
651{
652 node *tn;
653 unsigned int scale = 0, digits = 0;
654 sql_subfunc *fres = SA_ZNEW(sa, sql_subfunc);
655
656 fres->func = f;
657 if (IS_FILT(f)) {
658 fres->res = sa_list(sa);
659 list_append(fres->res, sql_bind_localtype("bit"));
660 } else if (IS_FUNC(f) || IS_UNION(f) || IS_ANALYTIC(f)) { /* not needed for PROC */
661 unsigned int mscale = 0, mdigits = 0;
662
663 if (ops) {
664 if (ops->h && ops->h->data && f->imp &&
665 strcmp(f->imp, "round") == 0) {
666 /* special case for round(): result is
667 * same type as first argument */
668 sql_subtype *a = ops->h->data;
669 mscale = a->scale;
670 mdigits = a->digits;
671 } else {
672 for (tn = ops->h; tn; tn = tn->next) {
673 sql_subtype *a = tn->data;
674
675 /* same scale as the input */
676 if (a && a->scale > mscale)
677 mscale = a->scale;
678 if (a && f->fix_scale == INOUT)
679 mdigits = a->digits;
680 }
681 }
682 }
683
684 if (!member) {
685 node *m;
686 sql_arg *ma = NULL;
687
688 if (ops) for (tn = ops->h, m = f->ops->h; tn; tn = tn->next, m = m->next) {
689 sql_arg *s = m->data;
690
691 if (!member && s->type.type->eclass == EC_ANY) {
692 member = tn->data;
693 ma = s;
694 }
695 /* largest type */
696 if (member && s->type.type->eclass == EC_ANY &&
697 s->type.type->localtype > ma->type.type->localtype ) {
698 member = tn->data;
699 ma = s;
700 }
701 }
702 }
703
704 if (f->res) {
705 fres->res = sa_list(sa);
706 for(tn = f->res->h; tn; tn = tn->next) {
707 sql_arg *rarg = tn->data;
708 sql_subtype *res, *r = &rarg->type;
709
710 /* same scale as the input */
711 if (member && member->scale > scale)
712 scale = member->scale;
713 digits = r->digits;
714 if (!member) {
715 if (f->fix_scale > SCALE_NONE && f->fix_scale < SCALE_EQ) {
716 scale = mscale;
717 digits = mdigits;
718 } else if (r->scale)
719 scale = r->scale;
720 }
721 if (member && f->fix_scale == INOUT)
722 digits = member->digits;
723 if (IS_ANALYTIC(f) && mscale)
724 scale = mscale;
725 if (member && r->type->eclass == EC_ANY)
726 r = member;
727 res = sql_create_subtype(sa, r->type, digits, scale);
728 list_append(fres->res, res);
729 }
730 }
731 if (member) { /* check that the types of all EC_ANY's are equal */
732 sql_subtype *st = NULL;
733 node *m;
734
735 if (ops) for (tn = ops->h, m = f->ops->h; tn; tn = tn->next, m = m->next) {
736 sql_arg *s = m->data;
737
738 if (s->type.type->eclass == EC_ANY) {
739 if (!st)
740 st = tn->data;
741 else if (subtype_cmp(st, tn->data))
742 return NULL;
743 }
744 }
745 }
746 }
747 return fres;
748}
749
750static sql_subfunc *
751func_cmp(sql_allocator *sa, sql_func *f, const char *name, int nrargs)
752{
753 if (strcmp(f->base.name, name) == 0) {
754 if (f->vararg)
755 return sql_dup_subfunc(sa, f, NULL, NULL);
756 if (nrargs < 0 || list_length(f->ops) == nrargs)
757 return sql_dup_subfunc(sa, f, NULL, NULL);
758 }
759 return NULL;
760}
761
762sql_subfunc *
763sql_find_func_by_name(sql_allocator *sa, sql_schema *s, const char *name, int nrargs, sql_ftype type)
764{
765 if (s && s->funcs.set)
766 for (node *n=s->funcs.set->h; n; n = n->next) {
767 sql_func *f = n->data;
768
769 if ((f->type != type || !f->res || list_length(f->ops) != nrargs))
770 continue;
771 if (strcmp(f->base.name, name) == 0)
772 return sql_dup_subfunc(sa, f, NULL, NULL);
773 }
774 for (node *n=funcs->h; n; n = n->next) {
775 sql_func *f = n->data;
776
777 if ((f->type != type || !f->res || list_length(f->ops) != nrargs))
778 continue;
779 if (strcmp(f->base.name, name) == 0)
780 return sql_dup_subfunc(sa, f, NULL, NULL);
781 }
782 return NULL;
783}
784
785sql_subfunc *
786sql_find_func(sql_allocator *sa, sql_schema *s, const char *sqlfname, int nrargs, sql_ftype type, sql_subfunc *prev)
787{
788 sql_subfunc *fres;
789 int key = hash_key(sqlfname);
790 sql_hash_e *he;
791 int found = 0;
792 sql_ftype filt = (type == F_FUNC)?F_FILT:type;
793
794 assert(nrargs);
795 MT_lock_set(&funcs->ht_lock);
796 he = funcs->ht->buckets[key&(funcs->ht->size-1)];
797 if (prev) {
798 for (; he && !found; he = he->chain)
799 if (he->value == prev->func)
800 found = 1;
801 }
802 for (; he; he = he->chain) {
803 sql_func *f = he->value;
804
805 if (f->type != type && f->type != filt)
806 continue;
807 if ((fres = func_cmp(sa, f, sqlfname, nrargs )) != NULL) {
808 MT_lock_unset(&funcs->ht_lock);
809 return fres;
810 }
811 }
812 MT_lock_unset(&funcs->ht_lock);
813 if (s) {
814 node *n;
815 /*
816 sql_func * f = find_sql_func(s, sqlfname);
817
818 if (f && f->type == type && (fres = func_cmp(sa, f, sqlfname, nrargs )) != NULL)
819 return fres;
820 */
821 if (s->funcs.set) {
822 MT_lock_set(&s->funcs.set->ht_lock);
823 if (s->funcs.set->ht) {
824 he = s->funcs.set->ht->buckets[key&(s->funcs.set->ht->size-1)];
825 if (prev) {
826 for (; he && !found; he = he->chain)
827 if (he->value == prev->func)
828 found = 1;
829 }
830 for (; he; he = he->chain) {
831 sql_func *f = he->value;
832
833 if (f->type != type && f->type != filt)
834 continue;
835 if ((fres = func_cmp(sa, f, sqlfname, nrargs )) != NULL) {
836 MT_lock_unset(&s->funcs.set->ht_lock);
837 return fres;
838 }
839 }
840 MT_lock_unset(&s->funcs.set->ht_lock);
841 } else {
842 MT_lock_unset(&s->funcs.set->ht_lock);
843 n = s->funcs.set->h;
844 if (prev) {
845 for (; n && !found; n = n->next)
846 if (n->data == prev)
847 found = 1;
848 }
849 for (; n; n = n->next) {
850 sql_func *f = n->data;
851
852 if (f->type != type && f->type != filt)
853 continue;
854 if ((fres = func_cmp(sa, f, sqlfname, nrargs )) != NULL)
855 return fres;
856 }
857 }
858 }
859 }
860 return NULL;
861}
862
863list *
864sql_find_funcs(sql_allocator *sa, sql_schema *s, const char *sqlfname, int nrargs, sql_ftype type)
865{
866 sql_subfunc *fres;
867 int key = hash_key(sqlfname);
868 sql_hash_e *he;
869 sql_ftype filt = (type == F_FUNC)?F_FILT:type;
870 list *res = sa_list(sa);
871
872 assert(nrargs);
873 MT_lock_set(&funcs->ht_lock);
874 he = funcs->ht->buckets[key&(funcs->ht->size-1)];
875 for (; he; he = he->chain) {
876 sql_func *f = he->value;
877
878 if (f->type != type && f->type != filt)
879 continue;
880 if ((fres = func_cmp(sa, f, sqlfname, nrargs )) != NULL)
881 list_append(res, fres);
882 }
883 MT_lock_unset(&funcs->ht_lock);
884 if (s) {
885 node *n;
886
887 if (s->funcs.set) {
888 MT_lock_set(&s->funcs.set->ht_lock);
889 if (s->funcs.set->ht) {
890 he = s->funcs.set->ht->buckets[key&(s->funcs.set->ht->size-1)];
891 for (; he; he = he->chain) {
892 sql_func *f = he->value;
893
894 if (f->type != type && f->type != filt)
895 continue;
896 if ((fres = func_cmp(sa, f, sqlfname, nrargs )) != NULL)
897 list_append(res, fres);
898 }
899 } else {
900 n = s->funcs.set->h;
901 for (; n; n = n->next) {
902 sql_func *f = n->data;
903
904 if (f->type != type && f->type != filt)
905 continue;
906 if ((fres = func_cmp(sa, f, sqlfname, nrargs )) != NULL)
907 list_append(res, fres);
908 }
909 }
910 MT_lock_unset(&s->funcs.set->ht_lock);
911 }
912 }
913 return res;
914}
915
916/* find function based on first argument */
917sql_subfunc *
918sql_bind_member(sql_allocator *sa, sql_schema *s, const char *sqlfname, sql_subtype *tp, int nrargs, sql_subfunc *prev)
919{
920 node *n = funcs->h;
921 int found = 1;
922
923 assert(nrargs);
924 if (prev) {
925 found = 0;
926 for(; n && !found; n = n->next)
927 if (n->data == prev->func)
928 found = 1;
929 }
930 for (; n; n = n->next) {
931 sql_func *f = n->data;
932
933 if (!f->res && !IS_FILT(f))
934 continue;
935 if (strcmp(f->base.name, sqlfname) == 0) {
936 if (list_length(f->ops) == nrargs && is_subtypeof(tp, &((sql_arg *) f->ops->h->data)->type))
937 return sql_dup_subfunc(sa, f, NULL, tp);
938 }
939 }
940 if (s) {
941 node *n;
942
943 if (!s->funcs.set)
944 return NULL;
945 n = s->funcs.set->h;
946 if (prev && !found) {
947 for(; n && !found; n = n->next)
948 if (n->data == prev->func)
949 found = 1;
950 }
951 for (; n; n = n->next) {
952 sql_func *f = n->data;
953
954 if (!f->res && !IS_FILT(f))
955 continue;
956 if (strcmp(f->base.name, sqlfname) == 0) {
957 if (list_length(f->ops) == nrargs && is_subtypeof(tp, &((sql_arg *) f->ops->h->data)->type))
958 return sql_dup_subfunc(sa, f, NULL, tp);
959 }
960 }
961 }
962 return NULL;
963}
964
965sql_subfunc *
966sql_bind_func(sql_allocator *sa, sql_schema *s, const char *sqlfname, sql_subtype *tp1, sql_subtype *tp2, sql_ftype type)
967{
968 list *l = sa_list(sa);
969 sql_subfunc *fres;
970
971 if (tp1)
972 list_append(l, tp1);
973 if (tp2)
974 list_append(l, tp2);
975
976 fres = sql_bind_func_(sa, s, sqlfname, l, type);
977 return fres;
978}
979
980sql_subfunc *
981sql_bind_func3(sql_allocator *sa, sql_schema *s, const char *sqlfname, sql_subtype *tp1, sql_subtype *tp2, sql_subtype *tp3, sql_ftype type)
982{
983 list *l = sa_list(sa);
984 sql_subfunc *fres;
985
986 if (tp1)
987 list_append(l, tp1);
988 if (tp2)
989 list_append(l, tp2);
990 if (tp3)
991 list_append(l, tp3);
992
993 fres = sql_bind_func_(sa, s, sqlfname, l, type);
994 return fres;
995}
996
997sql_subfunc *
998sql_bind_func_(sql_allocator *sa, sql_schema *s, const char *sqlfname, list *ops, sql_ftype type)
999{
1000 node *n = funcs->h;
1001 sql_ftype filt = (type == F_FUNC)?F_FILT:type;
1002
1003 for (; n; n = n->next) {
1004 sql_func *f = n->data;
1005
1006 if (f->type != type && f->type != filt)
1007 continue;
1008 if (strcmp(f->base.name, sqlfname) == 0) {
1009 if (list_cmp(f->ops, ops, (fcmp) &arg_subtype_cmp) == 0)
1010 return sql_dup_subfunc(sa, f, ops, NULL);
1011 }
1012 }
1013 if (s) {
1014 node *n;
1015
1016 if (s->funcs.set) for (n=s->funcs.set->h; n; n = n->next) {
1017 sql_func *f = n->data;
1018
1019 if (f->type != type && f->type != filt)
1020 continue;
1021 if (strcmp(f->base.name, sqlfname) == 0) {
1022 if (list_cmp(f->ops, ops, (fcmp) &arg_subtype_cmp) == 0)
1023 return sql_dup_subfunc(sa, f, ops, NULL);
1024 }
1025 }
1026 }
1027 return NULL;
1028}
1029
1030static sql_subfunc *
1031sql_bind_func_result_(sql_allocator *sa, sql_schema *s, const char *sqlfname, list *ops, sql_subtype *res)
1032{
1033 node *n = funcs->h;
1034
1035 for (; n; n = n->next) {
1036 sql_func *f = n->data;
1037 sql_arg *firstres = NULL;
1038
1039 if (!f->res && !IS_FILT(f))
1040 continue;
1041 firstres = IS_FILT(f)?BIT:f->res->h->data;
1042 if (strcmp(f->base.name, sqlfname) == 0 && (is_subtype(&firstres->type, res) || firstres->type.type->eclass == EC_ANY) && list_cmp(f->ops, ops, (fcmp) &arg_subtype_cmp) == 0)
1043 return sql_dup_subfunc(sa, f, ops, NULL);
1044 }
1045 if (s && s->funcs.set)
1046 n = s->funcs.set->h;
1047 for (; n; n = n->next) {
1048 sql_func *f = n->data;
1049 sql_arg *firstres = NULL;
1050
1051 if (!f->res && !IS_FILT(f))
1052 continue;
1053 firstres = IS_FILT(f)?BIT:f->res->h->data;
1054 if (strcmp(f->base.name, sqlfname) == 0 && (is_subtype(&firstres->type, res) || firstres->type.type->eclass == EC_ANY) && list_cmp(f->ops, ops, (fcmp) &arg_subtype_cmp) == 0)
1055 return sql_dup_subfunc(sa, f, ops, NULL);
1056 }
1057 return NULL;
1058}
1059
1060sql_subfunc *
1061sql_bind_func_result(sql_allocator *sa, sql_schema *s, const char *sqlfname, sql_subtype *tp1, sql_subtype *tp2, sql_subtype *res)
1062{
1063 list *l = sa_list(sa);
1064 sql_subfunc *fres;
1065
1066 if (tp1)
1067 list_append(l, tp1);
1068 if (tp2)
1069 list_append(l, tp2);
1070
1071 fres = sql_bind_func_result_(sa, s, sqlfname, l, res);
1072 return fres;
1073}
1074
1075sql_subfunc *
1076sql_bind_func_result3(sql_allocator *sa, sql_schema *s, const char *sqlfname, sql_subtype *tp1, sql_subtype *tp2, sql_subtype *tp3, sql_subtype *res)
1077{
1078 list *l = sa_list(sa);
1079 sql_subfunc *fres;
1080
1081 if (tp1)
1082 list_append(l, tp1);
1083 if (tp2)
1084 list_append(l, tp2);
1085 if (tp3)
1086 list_append(l, tp3);
1087
1088 fres = sql_bind_func_result_(sa, s, sqlfname, l, res);
1089 return fres;
1090}
1091
1092static void
1093sql_create_alias(sql_allocator *sa, const char *name, const char *alias)
1094{
1095 sql_alias *a = SA_ZNEW(sa, sql_alias);
1096
1097 if(a) {
1098 a->name = sa_strdup(sa, name);
1099 a->alias = sa_strdup(sa, alias);
1100 list_append(aliases, a);
1101 if (!keyword_exists(a->alias) )
1102 (void) keywords_insert(a->alias, KW_ALIAS);
1103 }
1104}
1105
1106char *
1107sql_bind_alias(const char *alias)
1108{
1109 node *n;
1110
1111 for (n = aliases->h; n; n = n->next) {
1112 sql_alias *a = n->data;
1113
1114 if (strcmp(a->alias, alias) == 0) {
1115 return a->name;
1116 }
1117 }
1118 return NULL;
1119}
1120
1121static sql_type *
1122sql_create_type(sql_allocator *sa, const char *sqlname, unsigned int digits, unsigned int scale, unsigned char radix, sql_class eclass, const char *name)
1123{
1124 sql_type *t = SA_ZNEW(sa, sql_type);
1125
1126 base_init(sa, &t->base, store_next_oid(), 0, name);
1127 t->sqlname = sa_strdup(sa, sqlname);
1128 t->digits = digits;
1129 t->scale = scale;
1130 t->localtype = ATOMindex(t->base.name);
1131 t->radix = radix;
1132 t->eclass = eclass;
1133 t->s = NULL;
1134 if (!keyword_exists(t->sqlname) && !EC_INTERVAL(eclass))
1135 (void) keywords_insert(t->sqlname, KW_TYPE);
1136 list_append(types, t);
1137
1138 list_append(localtypes, sql_create_subtype(sa, t, 0, 0));
1139
1140 return t;
1141}
1142
1143static sql_arg *
1144create_arg(sql_allocator *sa, const char *name, sql_subtype *t, char inout)
1145{
1146 sql_arg *a = (sa)?SA_ZNEW(sa, sql_arg):ZNEW(sql_arg);
1147
1148 if(a) {
1149 a->name = name?sa_strdup(sa, name):NULL;
1150 a->type = *t;
1151 a->inout = inout;
1152 }
1153 return a;
1154}
1155
1156sql_arg *
1157sql_create_arg(sql_allocator *sa, const char *name, sql_subtype *t, char inout)
1158{
1159 return create_arg(sa, name, t, inout);
1160}
1161
1162sql_arg *
1163arg_dup(sql_allocator *sa, sql_arg *oa)
1164{
1165 sql_arg *a = SA_ZNEW(sa, sql_arg);
1166
1167 if(a) {
1168 a->name = sa_strdup(sa, oa->name);
1169 a->type = oa->type;
1170 a->inout = oa->inout;
1171 }
1172 return a;
1173}
1174
1175static sql_func *
1176sql_create_func_(sql_allocator *sa, const char *name, const char *mod, const char *imp, list *ops, sql_arg *res, bit side_effect, sql_ftype type, int fix_scale)
1177{
1178 sql_func *t = SA_ZNEW(sa, sql_func);
1179
1180 if (!ops)
1181 ops = sa_list(sa);
1182 base_init(sa, &t->base, store_next_oid(), 0, name);
1183 t->imp = sa_strdup(sa, imp);
1184 t->mod = sa_strdup(sa, mod);
1185 t->ops = ops;
1186 t->type = type;
1187 if (res) {
1188 t->res = sa_list(sa);
1189 list_append(t->res, res);
1190 } else {
1191 t->res = NULL;
1192 }
1193 t->nr = list_length(funcs);
1194 t->sql = 0;
1195 t->lang = FUNC_LANG_INT;
1196 t->side_effect = side_effect;
1197 t->fix_scale = fix_scale;
1198 t->s = NULL;
1199 t->system = TRUE;
1200 if (type == F_AGGR) {
1201 list_append(aggrs, t);
1202 } else {
1203 list_append(funcs, t);
1204 }
1205 return t;
1206}
1207
1208static sql_func *
1209sql_create_aggr(sql_allocator *sa, const char *name, const char *mod, const char *imp, sql_type *tpe, sql_type *res)
1210{
1211 list *l = sa_list(sa);
1212 sql_arg *sres;
1213
1214 if (tpe)
1215 list_append(l, create_arg(sa, NULL, sql_create_subtype(sa, tpe, 0, 0), ARG_IN));
1216 assert(res);
1217 sres = create_arg(sa, NULL, sql_create_subtype(sa, res, 0, 0), ARG_OUT);
1218 return sql_create_func_(sa, name, mod, imp, l, sres, FALSE, F_AGGR, SCALE_NONE);
1219}
1220
1221static sql_func *
1222sql_create_func(sql_allocator *sa, const char *name, const char *mod, const char *imp, sql_type *tpe1, sql_type *tpe2, sql_type *res, int fix_scale)
1223{
1224 list *l = sa_list(sa);
1225 sql_arg *sres;
1226
1227 if (tpe1)
1228 list_append(l,create_arg(sa, NULL, sql_create_subtype(sa, tpe1, 0, 0), ARG_IN));
1229 if (tpe2)
1230 list_append(l,create_arg(sa, NULL, sql_create_subtype(sa, tpe2, 0, 0), ARG_IN));
1231
1232 sres = create_arg(sa, NULL, sql_create_subtype(sa, res, 0, 0), ARG_OUT);
1233 return sql_create_func_(sa, name, mod, imp, l, sres, FALSE, F_FUNC, fix_scale);
1234}
1235
1236static sql_func *
1237sql_create_func_res(sql_allocator *sa, const char *name, const char *mod, const char *imp, sql_type *tpe1, sql_type *tpe2, sql_type *res, int fix_scale, unsigned int scale)
1238{
1239 list *l = sa_list(sa);
1240 sql_arg *sres;
1241
1242 if (tpe1)
1243 list_append(l,create_arg(sa, NULL, sql_create_subtype(sa, tpe1, 0, 0), ARG_IN));
1244 if (tpe2)
1245 list_append(l,create_arg(sa, NULL, sql_create_subtype(sa, tpe2, 0, 0), ARG_IN));
1246
1247 sres = create_arg(sa, NULL, sql_create_subtype(sa, res, 0, 0), ARG_OUT);
1248 sres->type.scale = scale;
1249 return sql_create_func_(sa, name, mod, imp, l, sres, FALSE, F_FUNC, fix_scale);
1250}
1251
1252static sql_func *
1253sql_create_funcSE(sql_allocator *sa, const char *name, const char *mod, const char *imp, sql_type *tpe1, sql_type *tpe2, sql_type *res, int fix_scale)
1254{
1255 list *l = sa_list(sa);
1256 sql_arg *sres;
1257
1258 if (tpe1)
1259 list_append(l,create_arg(sa, NULL, sql_create_subtype(sa, tpe1, 0, 0), ARG_IN));
1260 if (tpe2)
1261 list_append(l,create_arg(sa, NULL, sql_create_subtype(sa, tpe2, 0, 0), ARG_IN));
1262
1263 sres = create_arg(sa, NULL, sql_create_subtype(sa, res, 0, 0), ARG_OUT);
1264 return sql_create_func_(sa, name, mod, imp, l, sres, TRUE, F_FUNC, fix_scale);
1265}
1266
1267static sql_func *
1268sql_create_func3(sql_allocator *sa, const char *name, const char *mod, const char *imp, sql_type *tpe1, sql_type *tpe2, sql_type *tpe3, sql_type *res, int fix_scale)
1269{
1270 list *l = sa_list(sa);
1271 sql_arg *sres;
1272
1273 list_append(l, create_arg(sa, NULL, sql_create_subtype(sa, tpe1, 0, 0), ARG_IN));
1274 list_append(l, create_arg(sa, NULL, sql_create_subtype(sa, tpe2, 0, 0), ARG_IN));
1275 list_append(l, create_arg(sa, NULL, sql_create_subtype(sa, tpe3, 0, 0), ARG_IN));
1276 sres = create_arg(sa, NULL, sql_create_subtype(sa, res, 0, 0), ARG_OUT);
1277 return sql_create_func_(sa, name, mod, imp, l, sres, FALSE, F_FUNC, fix_scale);
1278}
1279
1280static sql_func *
1281sql_create_analytic(sql_allocator *sa, const char *name, const char *mod, const char *imp, sql_type *tpe1, sql_type *res,
1282 int fix_scale)
1283{
1284 list *l = sa_list(sa);
1285 sql_arg *sres;
1286
1287 list_append(l, create_arg(sa, NULL, sql_create_subtype(sa, tpe1, 0, 0), ARG_IN));
1288 sres = create_arg(sa, NULL, sql_create_subtype(sa, res, 0, 0), ARG_OUT);
1289 return sql_create_func_(sa, name, mod, imp, l, sres, FALSE, F_ANALYTIC, fix_scale);
1290}
1291
1292static sql_func *
1293sql_create_analytic2(sql_allocator *sa, const char *name, const char *mod, const char *imp, sql_type *tpe1,
1294 sql_type *tpe2, sql_type *res, int fix_scale)
1295{
1296 list *l = sa_list(sa);
1297 sql_arg *sres;
1298
1299 list_append(l, create_arg(sa, NULL, sql_create_subtype(sa, tpe1, 0, 0), ARG_IN));
1300 list_append(l, create_arg(sa, NULL, sql_create_subtype(sa, tpe2, 0, 0), ARG_IN));
1301 sres = create_arg(sa, NULL, sql_create_subtype(sa, res, 0, 0), ARG_OUT);
1302 return sql_create_func_(sa, name, mod, imp, l, sres, FALSE, F_ANALYTIC, fix_scale);
1303}
1304
1305static sql_func *
1306sql_create_analytic3(sql_allocator *sa, const char *name, const char *mod, const char *imp, sql_type *tpe1,
1307 sql_type *tpe2, sql_type *tpe3, sql_type *res, int fix_scale)
1308{
1309 list *l = sa_list(sa);
1310 sql_arg *sres;
1311
1312 list_append(l, create_arg(sa, NULL, sql_create_subtype(sa, tpe1, 0, 0), ARG_IN));
1313 list_append(l, create_arg(sa, NULL, sql_create_subtype(sa, tpe2, 0, 0), ARG_IN));
1314 list_append(l, create_arg(sa, NULL, sql_create_subtype(sa, tpe3, 0, 0), ARG_IN));
1315 sres = create_arg(sa, NULL, sql_create_subtype(sa, res, 0, 0), ARG_OUT);
1316 return sql_create_func_(sa, name, mod, imp, l, sres, FALSE, F_ANALYTIC, fix_scale);
1317}
1318
1319static sql_func *
1320sql_create_analytic4(sql_allocator *sa, const char *name, const char *mod, const char *imp, sql_type *tpe1,
1321 sql_type *tpe2, sql_type *tpe3, sql_type *tpe4, sql_type *res, int fix_scale)
1322{
1323 list *l = sa_list(sa);
1324 sql_arg *sres;
1325
1326 list_append(l, create_arg(sa, NULL, sql_create_subtype(sa, tpe1, 0, 0), ARG_IN));
1327 list_append(l, create_arg(sa, NULL, sql_create_subtype(sa, tpe2, 0, 0), ARG_IN));
1328 list_append(l, create_arg(sa, NULL, sql_create_subtype(sa, tpe3, 0, 0), ARG_IN));
1329 list_append(l, create_arg(sa, NULL, sql_create_subtype(sa, tpe4, 0, 0), ARG_IN));
1330 sres = create_arg(sa, NULL, sql_create_subtype(sa, res, 0, 0), ARG_OUT);
1331 return sql_create_func_(sa, name, mod, imp, l, sres, FALSE, F_ANALYTIC, fix_scale);
1332}
1333
1334/* TODO maybe doing a varsized C function would be better :) */
1335static sql_func *
1336sql_create_analytic5(sql_allocator *sa, const char *name, const char *mod, const char *imp, sql_type *tpe1,
1337 sql_type *tpe2, sql_type *tpe3, sql_type *tpe4, sql_type *tpe5, sql_type *res, int fix_scale)
1338{
1339 list *l = sa_list(sa);
1340 sql_arg *sres;
1341
1342 list_append(l, create_arg(sa, NULL, sql_create_subtype(sa, tpe1, 0, 0), ARG_IN));
1343 list_append(l, create_arg(sa, NULL, sql_create_subtype(sa, tpe2, 0, 0), ARG_IN));
1344 list_append(l, create_arg(sa, NULL, sql_create_subtype(sa, tpe3, 0, 0), ARG_IN));
1345 list_append(l, create_arg(sa, NULL, sql_create_subtype(sa, tpe4, 0, 0), ARG_IN));
1346 list_append(l, create_arg(sa, NULL, sql_create_subtype(sa, tpe5, 0, 0), ARG_IN));
1347 sres = create_arg(sa, NULL, sql_create_subtype(sa, res, 0, 0), ARG_OUT);
1348 return sql_create_func_(sa, name, mod, imp, l, sres, FALSE, F_ANALYTIC, fix_scale);
1349}
1350
1351static sql_func *
1352sql_create_analytic6(sql_allocator *sa, const char *name, const char *mod, const char *imp, sql_type *tpe1, sql_type *tpe2,
1353 sql_type *tpe3, sql_type *tpe4, sql_type *tpe5, sql_type *tpe6, sql_type *res, int fix_scale)
1354{
1355 list *l = sa_list(sa);
1356 sql_arg *sres;
1357
1358 list_append(l, create_arg(sa, NULL, sql_create_subtype(sa, tpe1, 0, 0), ARG_IN));
1359 list_append(l, create_arg(sa, NULL, sql_create_subtype(sa, tpe2, 0, 0), ARG_IN));
1360 list_append(l, create_arg(sa, NULL, sql_create_subtype(sa, tpe3, 0, 0), ARG_IN));
1361 list_append(l, create_arg(sa, NULL, sql_create_subtype(sa, tpe4, 0, 0), ARG_IN));
1362 list_append(l, create_arg(sa, NULL, sql_create_subtype(sa, tpe5, 0, 0), ARG_IN));
1363 list_append(l, create_arg(sa, NULL, sql_create_subtype(sa, tpe6, 0, 0), ARG_IN));
1364 sres = create_arg(sa, NULL, sql_create_subtype(sa, res, 0, 0), ARG_OUT);
1365 return sql_create_func_(sa, name, mod, imp, l, sres, FALSE, F_ANALYTIC, fix_scale);
1366}
1367
1368static sql_func *
1369sql_create_func4(sql_allocator *sa, const char *name, const char *mod, const char *imp, sql_type *tpe1, sql_type *tpe2,
1370 sql_type *tpe3, sql_type *tpe4, sql_type *res, int fix_scale)
1371{
1372 list *l = sa_list(sa);
1373 sql_arg *sres;
1374
1375 list_append(l, create_arg(sa, NULL, sql_create_subtype(sa, tpe1, 0, 0), ARG_IN));
1376 list_append(l, create_arg(sa, NULL, sql_create_subtype(sa, tpe2, 0, 0), ARG_IN));
1377 list_append(l, create_arg(sa, NULL, sql_create_subtype(sa, tpe3, 0, 0), ARG_IN));
1378 list_append(l, create_arg(sa, NULL, sql_create_subtype(sa, tpe4, 0, 0), ARG_IN));
1379 sres = create_arg(sa, NULL, sql_create_subtype(sa, res, 0, 0), ARG_OUT);
1380 return sql_create_func_(sa, name, mod, imp, l, sres, FALSE, F_FUNC, fix_scale);
1381}
1382
1383
1384/* SQL service initialization
1385This C-code version initializes the
1386parser catalogs with typing information. Although, in principle,
1387many of the function signatures can be obtained from the underlying
1388database kernel, we have chosen for this explicit scheme for one
1389simple reason. The SQL standard dictates the types and we have to
1390check their availability in the kernel only. The kernel itself could
1391include many functions for which there is no standard.
1392*/
1393
1394static void
1395sqltypeinit( sql_allocator *sa)
1396{
1397 sql_type *ts[100];
1398 sql_type **strings, **numerical;
1399 sql_type **decimals, **floats, **dates, **end, **t;
1400 sql_type *STR, *BTE, *SHT, *INT, *LNG, *OID, *FLT, *DBL, *DEC;
1401#ifdef HAVE_HGE
1402 sql_type *HGE = NULL;
1403#endif
1404 sql_type *SECINT, *MONINT, *DTE;
1405 sql_type *TME, *TMETZ, *TMESTAMP, *TMESTAMPTZ;
1406 sql_type *BLOB;
1407 sql_type *ANY, *TABLE, *PTR;
1408 sql_type *GEOM, *MBR;
1409 sql_func *f;
1410 sql_arg *sres;
1411 sql_type *LargestINT, *LargestDEC;
1412
1413 ANY = sql_create_type(sa, "ANY", 0, 0, 0, EC_ANY, "void");
1414
1415 t = ts;
1416 TABLE = *t++ = sql_create_type(sa, "TABLE", 0, 0, 0, EC_TABLE, "bat");
1417 PTR = *t++ = sql_create_type(sa, "PTR", 0, 0, 0, EC_TABLE, "ptr");
1418
1419 BIT = *t++ = sql_create_type(sa, "BOOLEAN", 1, 0, 2, EC_BIT, "bit");
1420 sql_create_alias(sa, BIT->sqlname, "BOOL");
1421
1422 strings = t;
1423 *t++ = sql_create_type(sa, "CHAR", 0, 0, 0, EC_CHAR, "str");
1424 STR = *t++ = sql_create_type(sa, "VARCHAR", 0, 0, 0, EC_STRING, "str");
1425 *t++ = sql_create_type(sa, "CLOB", 0, 0, 0, EC_STRING, "str");
1426
1427 numerical = t;
1428#if SIZEOF_OID == SIZEOF_INT
1429 OID = *t++ = sql_create_type(sa, "OID", 31, 0, 2, EC_POS, "oid");
1430#endif
1431#if SIZEOF_OID == SIZEOF_LNG
1432 OID = *t++ = sql_create_type(sa, "OID", 63, 0, 2, EC_POS, "oid");
1433#endif
1434
1435 BTE = *t++ = sql_create_type(sa, "TINYINT", 8, SCALE_FIX, 2, EC_NUM, "bte");
1436 SHT = *t++ = sql_create_type(sa, "SMALLINT", 16, SCALE_FIX, 2, EC_NUM, "sht");
1437 INT = *t++ = sql_create_type(sa, "INT", 32, SCALE_FIX, 2, EC_NUM, "int");
1438 LargestINT =
1439 LNG = *t++ = sql_create_type(sa, "BIGINT", 64, SCALE_FIX, 2, EC_NUM, "lng");
1440#ifdef HAVE_HGE
1441 if (have_hge) {
1442 LargestINT =
1443 HGE = *t++ = sql_create_type(sa, "HUGEINT", 128, SCALE_FIX, 2, EC_NUM, "hge");
1444 }
1445#endif
1446
1447 decimals = t;
1448 /* decimal(d,s) (d indicates nr digits,
1449 s scale indicates nr of digits after the dot .) */
1450 *t++ = sql_create_type(sa, "DECIMAL", 2, SCALE_FIX, 10, EC_DEC, "bte");
1451 *t++ = sql_create_type(sa, "DECIMAL", 4, SCALE_FIX, 10, EC_DEC, "sht");
1452 DEC =
1453 *t++ = sql_create_type(sa, "DECIMAL", 9, SCALE_FIX, 10, EC_DEC, "int");
1454 LargestDEC =
1455 *t++ = sql_create_type(sa, "DECIMAL", 18, SCALE_FIX, 10, EC_DEC, "lng");
1456#ifdef HAVE_HGE
1457 if (have_hge) {
1458 LargestDEC =
1459 *t++ = sql_create_type(sa, "DECIMAL", 38, SCALE_FIX, 10, EC_DEC, "hge");
1460 }
1461#endif
1462
1463 /* float(n) (n indicates precision of at least n digits) */
1464 /* ie n <= 23 -> flt */
1465 /* n <= 51 -> dbl */
1466 /* n <= 62 -> long long dbl (with -ieee) (not supported) */
1467 /* this requires a type definition */
1468
1469 floats = t;
1470 FLT = *t++ = sql_create_type(sa, "REAL", 24, SCALE_NOFIX, 2, EC_FLT, "flt");
1471 DBL = *t++ = sql_create_type(sa, "DOUBLE", 53, SCALE_NOFIX, 2, EC_FLT, "dbl");
1472
1473 dates = t;
1474 MONINT = *t++ = sql_create_type(sa, "MONTH_INTERVAL", 32, 0, 2, EC_MONTH, "int");
1475 SECINT = *t++ = sql_create_type(sa, "SEC_INTERVAL", 13, SCALE_FIX, 10, EC_SEC, "lng");
1476 TME = *t++ = sql_create_type(sa, "TIME", 7, 0, 0, EC_TIME, "daytime");
1477 TMETZ = *t++ = sql_create_type(sa, "TIMETZ", 7, SCALE_FIX, 0, EC_TIME, "daytime");
1478 DTE = *t++ = sql_create_type(sa, "DATE", 0, 0, 0, EC_DATE, "date");
1479 TMESTAMP = *t++ = sql_create_type(sa, "TIMESTAMP", 7, 0, 0, EC_TIMESTAMP, "timestamp");
1480 TMESTAMPTZ = *t++ = sql_create_type(sa, "TIMESTAMPTZ", 7, SCALE_FIX, 0, EC_TIMESTAMP, "timestamp");
1481
1482 BLOB = *t++ = sql_create_type(sa, "BLOB", 0, 0, 0, EC_BLOB, "blob");
1483
1484 sql_create_func(sa, "length", "blob", "nitems", BLOB, NULL, INT, SCALE_NONE);
1485 sql_create_func(sa, "octet_length", "blob", "nitems", BLOB, NULL, INT, SCALE_NONE);
1486
1487 if (geomcatalogfix_get() != NULL) {
1488 // the geom module is loaded
1489 GEOM = *t++ = sql_create_type(sa, "GEOMETRY", 0, SCALE_NONE, 0, EC_GEOM, "wkb");
1490 /*POINT =*/ //*t++ = sql_create_type(sa, "POINT", 0, SCALE_FIX, 0, EC_GEOM, "wkb");
1491 *t++ = sql_create_type(sa, "GEOMETRYA", 0, SCALE_NONE, 0, EC_EXTERNAL, "wkba");
1492
1493 MBR = *t++ = sql_create_type(sa, "MBR", 0, SCALE_NONE, 0, EC_EXTERNAL, "mbr");
1494
1495 /* mbr operator functions */
1496 sql_create_func(sa, "mbr_overlap", "geom", "mbrOverlaps", GEOM, GEOM, BIT, SCALE_FIX);
1497 sql_create_func(sa, "mbr_overlap", "geom", "mbrOverlaps", MBR, MBR, BIT, SCALE_FIX);
1498 sql_create_func(sa, "mbr_above", "geom", "mbrAbove", GEOM, GEOM, BIT, SCALE_FIX);
1499 sql_create_func(sa, "mbr_above", "geom", "mbrAbove", MBR, MBR, BIT, SCALE_FIX);
1500 sql_create_func(sa, "mbr_below", "geom", "mbrBelow", GEOM, GEOM, BIT, SCALE_FIX);
1501 sql_create_func(sa, "mbr_below", "geom", "mbrBelow", MBR, MBR, BIT, SCALE_FIX);
1502 sql_create_func(sa, "mbr_right", "geom", "mbrRight", GEOM, GEOM, BIT, SCALE_FIX);
1503 sql_create_func(sa, "mbr_right", "geom", "mbrRight", MBR, MBR, BIT, SCALE_FIX);
1504 sql_create_func(sa, "mbr_left", "geom", "mbrLeft", GEOM, GEOM, BIT, SCALE_FIX);
1505 sql_create_func(sa, "mbr_left", "geom", "mbrLeft", MBR, MBR, BIT, SCALE_FIX);
1506 sql_create_func(sa, "mbr_overlap_or_above", "geom", "mbrOverlapOrAbove", GEOM, GEOM, BIT, SCALE_FIX);
1507 sql_create_func(sa, "mbr_overlap_or_above", "geom", "mbrOverlapOrAbove", MBR, MBR, BIT, SCALE_FIX);
1508 sql_create_func(sa, "mbr_overlap_or_below", "geom", "mbrOverlapOrBelow", GEOM, GEOM, BIT, SCALE_FIX);
1509 sql_create_func(sa, "mbr_overlap_or_below", "geom", "mbrOverlapOrBelow", MBR, MBR, BIT, SCALE_FIX);
1510 sql_create_func(sa, "mbr_overlap_or_right", "geom", "mbrOverlapOrRight", GEOM, GEOM, BIT, SCALE_FIX);
1511 sql_create_func(sa, "mbr_overlap_or_right", "geom", "mbrOverlapOrRight", MBR, MBR, BIT, SCALE_FIX);
1512 sql_create_func(sa, "mbr_overlap_or_left", "geom", "mbrOverlapOrLeft", GEOM, GEOM, BIT, SCALE_FIX);
1513 sql_create_func(sa, "mbr_overlap_or_left", "geom", "mbrOverlapOrLeft", MBR, MBR, BIT, SCALE_FIX);
1514 sql_create_func(sa, "mbr_contains", "geom", "mbrContains", GEOM, GEOM, BIT, SCALE_FIX);
1515 sql_create_func(sa, "mbr_contains", "geom", "mbrContains", MBR, MBR, BIT, SCALE_FIX);
1516 sql_create_func(sa, "mbr_contained", "geom", "mbrContained", GEOM, GEOM, BIT, SCALE_FIX);
1517 sql_create_func(sa, "mbr_contained", "geom", "mbrContained", MBR, MBR, BIT, SCALE_FIX);
1518 sql_create_func(sa, "mbr_equal", "geom", "mbrEqual", GEOM, GEOM, BIT, SCALE_FIX);
1519 sql_create_func(sa, "mbr_equal", "geom", "mbrEqual", MBR, MBR, BIT, SCALE_FIX);
1520 sql_create_func(sa, "mbr_distance", "geom", "mbrDistance", GEOM, GEOM, DBL, SCALE_FIX);
1521 sql_create_func(sa, "mbr_distance", "geom", "mbrDistance", MBR, MBR, DBL, SCALE_FIX);
1522 sql_create_func(sa, "left_shift", "geom", "mbrLeft", GEOM, GEOM, BIT, SCALE_FIX);
1523 sql_create_func(sa, "left_shift", "geom", "mbrLeft", MBR, MBR, BIT, SCALE_FIX);
1524 sql_create_func(sa, "right_shift", "geom", "mbrRight", GEOM, GEOM, BIT, SCALE_FIX);
1525 sql_create_func(sa, "right_shift", "geom", "mbrRight", MBR, MBR, BIT, SCALE_FIX);
1526 }
1527
1528 end = t;
1529 *t = NULL;
1530
1531// sql_create_func(sa, "st_pointfromtext", "geom", "st_pointformtext", OID, NULL, OID, SCALE_FIX);
1532
1533 sql_create_aggr(sa, "not_unique", "sql", "not_unique", OID, BIT);
1534 /* well to be precise it does reduce and map */
1535 sql_create_func(sa, "not_uniques", "sql", "not_uniques", LNG, NULL, OID, SCALE_NONE);
1536 sql_create_func(sa, "not_uniques", "sql", "not_uniques", OID, NULL, OID, SCALE_NONE);
1537
1538 /* functions needed for all types */
1539 sql_create_func(sa, "hash", "mkey", "hash", ANY, NULL, LNG, SCALE_FIX);
1540 sql_create_func3(sa, "rotate_xor_hash", "calc", "rotate_xor_hash", LNG, INT, ANY, LNG, SCALE_NONE);
1541 sql_create_func(sa, "=", "calc", "=", ANY, ANY, BIT, SCALE_FIX);
1542 sql_create_func(sa, "<>", "calc", "!=", ANY, ANY, BIT, SCALE_FIX);
1543 sql_create_func(sa, "isnull", "calc", "isnil", ANY, NULL, BIT, SCALE_FIX);
1544 sql_create_func(sa, ">", "calc", ">", ANY, ANY, BIT, SCALE_FIX);
1545 sql_create_func(sa, ">=", "calc", ">=", ANY, ANY, BIT, SCALE_FIX);
1546 sql_create_func(sa, "<", "calc", "<", ANY, ANY, BIT, SCALE_FIX);
1547 sql_create_func(sa, "<=", "calc", "<=", ANY, ANY, BIT, SCALE_FIX);
1548 sres = create_arg(sa, NULL, sql_create_subtype(sa, BIT, 0, 0), ARG_OUT);
1549 sql_create_func_(sa, "between", "calc", "between",
1550 list_append(list_append(list_append (list_append (list_append(list_append(list_append(list_append(sa_list(sa),
1551 create_arg(sa, NULL, sql_create_subtype(sa, ANY, 0, 0), ARG_IN)),
1552 create_arg(sa, NULL, sql_create_subtype(sa, ANY, 0, 0), ARG_IN)),
1553 create_arg(sa, NULL, sql_create_subtype(sa, ANY, 0, 0), ARG_IN)),
1554 create_arg(sa, NULL, sql_create_subtype(sa, BIT, 0, 0), ARG_IN)),
1555 create_arg(sa, NULL, sql_create_subtype(sa, BIT, 0, 0), ARG_IN)),
1556 create_arg(sa, NULL, sql_create_subtype(sa, BIT, 0, 0), ARG_IN)),
1557 create_arg(sa, NULL, sql_create_subtype(sa, BIT, 0, 0), ARG_IN)),
1558 create_arg(sa, NULL, sql_create_subtype(sa, BIT, 0, 0), ARG_IN)),
1559 sres, FALSE, F_FUNC, SCALE_FIX);
1560 sql_create_aggr(sa, "zero_or_one", "sql", "zero_or_one", ANY, ANY);
1561 sql_create_aggr(sa, "all", "sql", "all", ANY, ANY);
1562 sql_create_aggr(sa, "null", "sql", "null", ANY, BIT);
1563 sql_create_func3(sa, "any", "sql", "any", BIT, BIT, BIT, BIT, SCALE_NONE);
1564 sql_create_func3(sa, "all", "sql", "all", BIT, BIT, BIT, BIT, SCALE_NONE);
1565 sql_create_aggr(sa, "anyequal", "aggr", "anyequal", ANY, BIT); /* needs 3 arguments (l,r,nil)(ugh) */
1566 sql_create_aggr(sa, "allnotequal", "aggr", "allnotequal", ANY, BIT); /* needs 3 arguments (l,r,nil)(ugh) */
1567 sql_create_func(sa, "sql_anyequal", "aggr", "anyequal", ANY, ANY, BIT, SCALE_NONE);
1568 sql_create_func(sa, "sql_not_anyequal", "aggr", "not_anyequal", ANY, ANY, BIT, SCALE_NONE);
1569 sql_create_aggr(sa, "exist", "aggr", "exist", ANY, BIT);
1570 sql_create_aggr(sa, "not_exist", "aggr", "not_exist", ANY, BIT);
1571 sql_create_func(sa, "sql_exists", "aggr", "exist", ANY, NULL, BIT, SCALE_NONE);
1572 sql_create_func(sa, "sql_not_exists", "aggr", "not_exist", ANY, NULL, BIT, SCALE_NONE);
1573 /* needed for relational version */
1574 sql_create_func(sa, "in", "calc", "in", ANY, ANY, BIT, SCALE_NONE);
1575 sql_create_func(sa, "identity", "calc", "identity", ANY, NULL, OID, SCALE_NONE);
1576 sql_create_func(sa, "rowid", "calc", "identity", ANY, NULL, INT, SCALE_NONE);
1577 /* needed for indices/clusters oid(schema.table,val) returns max(head(schema.table))+1 */
1578 sql_create_func3(sa, "rowid", "calc", "rowid", ANY, STR, STR, OID, SCALE_NONE);
1579 sql_create_aggr(sa, "min", "aggr", "min", ANY, ANY);
1580 sql_create_aggr(sa, "max", "aggr", "max", ANY, ANY);
1581 sql_create_func(sa, "sql_min", "calc", "min", ANY, ANY, ANY, SCALE_FIX);
1582 sql_create_func(sa, "sql_max", "calc", "max", ANY, ANY, ANY, SCALE_FIX);
1583 sql_create_func(sa, "least", "calc", "min_no_nil", ANY, ANY, ANY, SCALE_FIX);
1584 sql_create_func(sa, "greatest", "calc", "max_no_nil", ANY, ANY, ANY, SCALE_FIX);
1585 sql_create_func3(sa, "ifthenelse", "calc", "ifthenelse", BIT, ANY, ANY, ANY, SCALE_FIX);
1586
1587 /* sum for numerical and decimals */
1588 sql_create_aggr(sa, "sum", "aggr", "sum", BTE, LargestINT);
1589 sql_create_aggr(sa, "sum", "aggr", "sum", SHT, LargestINT);
1590 sql_create_aggr(sa, "sum", "aggr", "sum", INT, LargestINT);
1591 //sql_create_aggr(sa, "sum", "aggr", "sum", LNG, LargestINT);
1592#ifdef HAVE_HGE
1593 if (have_hge)
1594 sql_create_aggr(sa, "sum", "aggr", "sum", HGE, LargestINT);
1595#endif
1596 sql_create_aggr(sa, "sum", "aggr", "sum", LNG, LNG);
1597
1598 t = decimals; /* BTE */
1599 sql_create_aggr(sa, "sum", "aggr", "sum", *(t), LargestDEC);
1600 t++; /* SHT */
1601 sql_create_aggr(sa, "sum", "aggr", "sum", *(t), LargestDEC);
1602 t++; /* INT */
1603 sql_create_aggr(sa, "sum", "aggr", "sum", *(t), LargestDEC);
1604 t++; /* LNG */
1605 sql_create_aggr(sa, "sum", "aggr", "sum", *(t), LargestDEC);
1606#ifdef HAVE_HGE
1607 if (have_hge) {
1608 t++; /* HGE */
1609 sql_create_aggr(sa, "sum", "aggr", "sum", *(t), LargestDEC);
1610 }
1611#endif
1612
1613 /* prod for numerical and decimals */
1614 sql_create_aggr(sa, "prod", "aggr", "prod", BTE, LargestINT);
1615 sql_create_aggr(sa, "prod", "aggr", "prod", SHT, LargestINT);
1616 sql_create_aggr(sa, "prod", "aggr", "prod", INT, LargestINT);
1617 sql_create_aggr(sa, "prod", "aggr", "prod", LNG, LargestINT);
1618#ifdef HAVE_HGE
1619 if (HAVE_HGE)
1620 sql_create_aggr(sa, "prod", "aggr", "prod", HGE, LargestINT);
1621#endif
1622
1623#if 0
1624 /* prod for decimals introduce errors in the output scales */
1625 t = decimals; /* BTE */
1626 sql_create_aggr(sa, "prod", "aggr", "prod", *(t), LargestDEC);
1627 t++; /* SHT */
1628 sql_create_aggr(sa, "prod", "aggr", "prod", *(t), LargestDEC);
1629 t++; /* INT */
1630 sql_create_aggr(sa, "prod", "aggr", "prod", *(t), LargestDEC);
1631 t++; /* LNG */
1632 sql_create_aggr(sa, "prod", "aggr", "prod", *(t), LargestDEC);
1633#ifdef HAVE_HGE
1634 if (have_hge) {
1635 t++; /* HGE */
1636 sql_create_aggr(sa, "prod", "aggr", "prod", *(t), LargestDEC);
1637 }
1638#endif
1639#endif
1640
1641 for (t = numerical; t < dates; t++) {
1642 sql_create_func(sa, "mod", "calc", "%", *t, *t, *t, SCALE_FIX);
1643 }
1644
1645 for (t = floats; t < dates; t++) {
1646 sql_create_aggr(sa, "sum", "aggr", "sum", *t, *t);
1647 sql_create_aggr(sa, "prod", "aggr", "prod", *t, *t);
1648 }
1649 sql_create_aggr(sa, "sum", "aggr", "sum", MONINT, MONINT);
1650 sql_create_aggr(sa, "sum", "aggr", "sum", SECINT, SECINT);
1651 /* do DBL first so that it is chosen as cast destination for
1652 * unknown types */
1653 sql_create_aggr(sa, "avg", "aggr", "avg", DBL, DBL);
1654 sql_create_aggr(sa, "avg", "aggr", "avg", BTE, DBL);
1655 sql_create_aggr(sa, "avg", "aggr", "avg", SHT, DBL);
1656 sql_create_aggr(sa, "avg", "aggr", "avg", INT, DBL);
1657 sql_create_aggr(sa, "avg", "aggr", "avg", LNG, DBL);
1658#ifdef HAVE_HGE
1659 if (have_hge)
1660 sql_create_aggr(sa, "avg", "aggr", "avg", HGE, DBL);
1661#endif
1662 sql_create_aggr(sa, "avg", "aggr", "avg", FLT, DBL);
1663
1664 sql_create_aggr(sa, "avg", "aggr", "avg", MONINT, DBL);
1665 //sql_create_aggr(sa, "avg", "aggr", "avg", SECINT, DBL);
1666
1667 sql_create_aggr(sa, "count_no_nil", "aggr", "count_no_nil", NULL, LNG);
1668 sql_create_aggr(sa, "count", "aggr", "count", NULL, LNG);
1669
1670 /* order based operators */
1671 sql_create_analytic(sa, "diff", "sql", "diff", ANY, BIT, SCALE_NONE);
1672 sql_create_analytic2(sa, "diff", "sql", "diff", BIT, ANY, BIT, SCALE_NONE);
1673
1674 sql_create_analytic5(sa, "window_bound", "sql", "window_bound", ANY, INT, INT, INT, BTE, LNG, SCALE_NONE);
1675 sql_create_analytic6(sa, "window_bound", "sql", "window_bound", BIT, ANY, INT, INT, INT, BTE, LNG, SCALE_NONE);
1676 sql_create_analytic5(sa, "window_bound", "sql", "window_bound", ANY, INT, INT, INT, SHT, LNG, SCALE_NONE);
1677 sql_create_analytic6(sa, "window_bound", "sql", "window_bound", BIT, ANY, INT, INT, INT, SHT, LNG, SCALE_NONE);
1678 sql_create_analytic5(sa, "window_bound", "sql", "window_bound", ANY, INT, INT, INT, INT, LNG, SCALE_NONE);
1679 sql_create_analytic6(sa, "window_bound", "sql", "window_bound", BIT, ANY, INT, INT, INT, INT, LNG, SCALE_NONE);
1680 sql_create_analytic5(sa, "window_bound", "sql", "window_bound", ANY, INT, INT, INT, LNG, LNG, SCALE_NONE);
1681 sql_create_analytic6(sa, "window_bound", "sql", "window_bound", BIT, ANY, INT, INT, INT, LNG, LNG, SCALE_NONE);
1682 sql_create_analytic5(sa, "window_bound", "sql", "window_bound", ANY, INT, INT, INT, FLT, LNG, SCALE_NONE);
1683 sql_create_analytic6(sa, "window_bound", "sql", "window_bound", BIT, ANY, INT, INT, INT, FLT, LNG, SCALE_NONE);
1684 sql_create_analytic5(sa, "window_bound", "sql", "window_bound", ANY, INT, INT, INT, DBL, LNG, SCALE_NONE);
1685 sql_create_analytic6(sa, "window_bound", "sql", "window_bound", BIT, ANY, INT, INT, INT, DBL, LNG, SCALE_NONE);
1686#ifdef HAVE_HGE
1687 if (have_hge) {
1688 sql_create_analytic5(sa, "window_bound", "sql", "window_bound", ANY, INT, INT, INT, HGE, LNG, SCALE_NONE);
1689 sql_create_analytic6(sa, "window_bound", "sql", "window_bound", BIT, ANY, INT, INT, INT, HGE, LNG, SCALE_NONE);
1690 }
1691#endif
1692
1693 t = decimals; /* BTE */
1694 sql_create_analytic5(sa, "window_bound", "sql", "window_bound", ANY, INT, INT, INT, *(t), LNG, SCALE_NONE);
1695 sql_create_analytic6(sa, "window_bound", "sql", "window_bound", BIT, ANY, INT, INT, INT, *(t), LNG, SCALE_NONE);
1696 t++; /* SHT */
1697 sql_create_analytic5(sa, "window_bound", "sql", "window_bound", ANY, INT, INT, INT, *(t), LNG, SCALE_NONE);
1698 sql_create_analytic6(sa, "window_bound", "sql", "window_bound", BIT, ANY, INT, INT, INT, *(t), LNG, SCALE_NONE);
1699 t++; /* INT */
1700 sql_create_analytic5(sa, "window_bound", "sql", "window_bound", ANY, INT, INT, INT, *(t), LNG, SCALE_NONE);
1701 sql_create_analytic6(sa, "window_bound", "sql", "window_bound", BIT, ANY, INT, INT, INT, *(t), LNG, SCALE_NONE);
1702 t++; /* LNG */
1703 sql_create_analytic5(sa, "window_bound", "sql", "window_bound", ANY, INT, INT, INT, *(t), LNG, SCALE_NONE);
1704 sql_create_analytic6(sa, "window_bound", "sql", "window_bound", BIT, ANY, INT, INT, INT, *(t), LNG, SCALE_NONE);
1705#ifdef HAVE_HGE
1706 if (have_hge) {
1707 t++; /* HGE */
1708 sql_create_analytic5(sa, "window_bound", "sql", "window_bound", ANY, INT, INT, INT, *(t), LNG, SCALE_NONE);
1709 sql_create_analytic6(sa, "window_bound", "sql", "window_bound", BIT, ANY, INT, INT, INT, *(t), LNG, SCALE_NONE);
1710 }
1711#endif
1712
1713 sql_create_analytic5(sa, "window_bound", "sql", "window_bound", ANY, INT, INT, INT, MONINT, LNG, SCALE_NONE);
1714 sql_create_analytic6(sa, "window_bound", "sql", "window_bound", BIT, ANY, INT, INT, INT, MONINT, LNG, SCALE_NONE);
1715 sql_create_analytic5(sa, "window_bound", "sql", "window_bound", ANY, INT, INT, INT, SECINT, LNG, SCALE_NONE);
1716 sql_create_analytic6(sa, "window_bound", "sql", "window_bound", BIT, ANY, INT, INT, INT, SECINT, LNG, SCALE_NONE);
1717
1718 sql_create_analytic3(sa, "rank", "sql", "rank", ANY, BIT, BIT, INT, SCALE_NONE);
1719 sql_create_analytic3(sa, "dense_rank", "sql", "dense_rank", ANY, BIT, BIT, INT, SCALE_NONE);
1720 sql_create_analytic3(sa, "row_number", "sql", "row_number", ANY, BIT, BIT, INT, SCALE_NONE);
1721 sql_create_analytic3(sa, "percent_rank", "sql", "percent_rank", ANY, BIT, BIT, DBL, SCALE_NONE);
1722 sql_create_analytic3(sa, "cume_dist", "sql", "cume_dist", ANY, BIT, BIT, DBL, SCALE_NONE);
1723
1724 sql_create_analytic4(sa, "ntile", "sql", "ntile", ANY, BTE, BIT, BIT, BTE, SCALE_NONE);
1725 sql_create_analytic4(sa, "ntile", "sql", "ntile", ANY, SHT, BIT, BIT, SHT, SCALE_NONE);
1726 sql_create_analytic4(sa, "ntile", "sql", "ntile", ANY, INT, BIT, BIT, INT, SCALE_NONE);
1727 sql_create_analytic4(sa, "ntile", "sql", "ntile", ANY, LNG, BIT, BIT, LNG, SCALE_NONE);
1728#ifdef HAVE_HGE
1729 if (have_hge)
1730 sql_create_analytic4(sa, "ntile", "sql", "ntile", ANY, HGE, BIT, BIT, HGE, SCALE_NONE);
1731#endif
1732
1733 sql_create_analytic3(sa, "lag", "sql", "lag", ANY, BIT, BIT, ANY, SCALE_NONE);
1734 sql_create_analytic4(sa, "lag", "sql", "lag", ANY, BTE, BIT, BIT, ANY, SCALE_NONE);
1735 sql_create_analytic4(sa, "lag", "sql", "lag", ANY, SHT, BIT, BIT, ANY, SCALE_NONE);
1736 sql_create_analytic4(sa, "lag", "sql", "lag", ANY, INT, BIT, BIT, ANY, SCALE_NONE);
1737 sql_create_analytic4(sa, "lag", "sql", "lag", ANY, LNG, BIT, BIT, ANY, SCALE_NONE);
1738#ifdef HAVE_HGE
1739 if (have_hge)
1740 sql_create_analytic4(sa, "lag", "sql", "lag", ANY, HGE, BIT, BIT, ANY, SCALE_NONE);
1741#endif
1742 sql_create_analytic5(sa, "lag", "sql", "lag", ANY, BTE, ANY, BIT, BIT, ANY, SCALE_NONE);
1743 sql_create_analytic5(sa, "lag", "sql", "lag", ANY, SHT, ANY, BIT, BIT, ANY, SCALE_NONE);
1744 sql_create_analytic5(sa, "lag", "sql", "lag", ANY, INT, ANY, BIT, BIT, ANY, SCALE_NONE);
1745 sql_create_analytic5(sa, "lag", "sql", "lag", ANY, LNG, ANY, BIT, BIT, ANY, SCALE_NONE);
1746#ifdef HAVE_HGE
1747 if (have_hge)
1748 sql_create_analytic5(sa, "lag", "sql", "lag", ANY, HGE, ANY, BIT, BIT, ANY, SCALE_NONE);
1749#endif
1750
1751 sql_create_analytic3(sa, "lead", "sql", "lead", ANY, BIT, BIT, ANY, SCALE_NONE);
1752 sql_create_analytic4(sa, "lead", "sql", "lead", ANY, BTE, BIT, BIT, ANY, SCALE_NONE);
1753 sql_create_analytic4(sa, "lead", "sql", "lead", ANY, SHT, BIT, BIT, ANY, SCALE_NONE);
1754 sql_create_analytic4(sa, "lead", "sql", "lead", ANY, INT, BIT, BIT, ANY, SCALE_NONE);
1755 sql_create_analytic4(sa, "lead", "sql", "lead", ANY, LNG, BIT, BIT, ANY, SCALE_NONE);
1756#ifdef HAVE_HGE
1757 if (have_hge)
1758 sql_create_analytic4(sa, "lead", "sql", "lead", ANY, HGE, BIT, BIT, ANY, SCALE_NONE);
1759#endif
1760 sql_create_analytic5(sa, "lead", "sql", "lead", ANY, BTE, ANY, BIT, BIT, ANY, SCALE_NONE);
1761 sql_create_analytic5(sa, "lead", "sql", "lead", ANY, SHT, ANY, BIT, BIT, ANY, SCALE_NONE);
1762 sql_create_analytic5(sa, "lead", "sql", "lead", ANY, INT, ANY, BIT, BIT, ANY, SCALE_NONE);
1763 sql_create_analytic5(sa, "lead", "sql", "lead", ANY, LNG, ANY, BIT, BIT, ANY, SCALE_NONE);
1764#ifdef HAVE_HGE
1765 if (have_hge)
1766 sql_create_analytic5(sa, "lead", "sql", "lead", ANY, HGE, ANY, BIT, BIT, ANY, SCALE_NONE);
1767#endif
1768
1769 //these analytic functions support frames
1770 sql_create_analytic(sa, "first_value", "sql", "first_value", ANY, ANY, SCALE_NONE);
1771 sql_create_analytic(sa, "last_value", "sql", "last_value", ANY, ANY, SCALE_NONE);
1772
1773 sql_create_analytic2(sa, "nth_value", "sql", "nth_value", ANY, BTE, ANY, SCALE_NONE);
1774 sql_create_analytic2(sa, "nth_value", "sql", "nth_value", ANY, SHT, ANY, SCALE_NONE);
1775 sql_create_analytic2(sa, "nth_value", "sql", "nth_value", ANY, INT, ANY, SCALE_NONE);
1776 sql_create_analytic2(sa, "nth_value", "sql", "nth_value", ANY, LNG, ANY, SCALE_NONE);
1777#ifdef HAVE_HGE
1778 if (have_hge)
1779 sql_create_analytic2(sa, "nth_value", "sql", "nth_value", ANY, HGE, ANY, SCALE_NONE);
1780#endif
1781
1782 sql_create_analytic2(sa, "count", "sql", "count", ANY, BIT, LNG, SCALE_NONE);
1783 sql_create_analytic(sa, "min", "sql", "min", ANY, ANY, SCALE_NONE);
1784 sql_create_analytic(sa, "max", "sql", "max", ANY, ANY, SCALE_NONE);
1785
1786 //analytical sum for numerical and decimals
1787 sql_create_analytic(sa, "sum", "sql", "sum", BTE, LargestINT, SCALE_NONE);
1788 sql_create_analytic(sa, "sum", "sql", "sum", SHT, LargestINT, SCALE_NONE);
1789 sql_create_analytic(sa, "sum", "sql", "sum", INT, LargestINT, SCALE_NONE);
1790 sql_create_analytic(sa, "sum", "sql", "sum", LNG, LargestINT, SCALE_NONE);
1791#ifdef HAVE_HGE
1792 if (have_hge)
1793 sql_create_analytic(sa, "sum", "sql", "sum", HGE, LargestINT, SCALE_NONE);
1794#endif
1795
1796 t = decimals; // BTE
1797 sql_create_analytic(sa, "sum", "sql", "sum", *(t), LargestDEC, SCALE_NONE);
1798 t++; // SHT
1799 sql_create_analytic(sa, "sum", "sql", "sum", *(t), LargestDEC, SCALE_NONE);
1800 t++; // INT
1801 sql_create_analytic(sa, "sum", "sql", "sum", *(t), LargestDEC, SCALE_NONE);
1802 t++; // LNG
1803 sql_create_analytic(sa, "sum", "sql", "sum", *(t), LargestDEC, SCALE_NONE);
1804#ifdef HAVE_HGE
1805 if (have_hge) {
1806 t++; // HGE
1807 sql_create_analytic(sa, "sum", "sql", "sum", *(t), LargestDEC, SCALE_NONE);
1808 }
1809#endif
1810
1811 //analytical product for numerical and decimals
1812 sql_create_analytic(sa, "prod", "sql", "prod", BTE, LargestINT, SCALE_NONE);
1813 sql_create_analytic(sa, "prod", "sql", "prod", SHT, LargestINT, SCALE_NONE);
1814 sql_create_analytic(sa, "prod", "sql", "prod", INT, LargestINT, SCALE_NONE);
1815 sql_create_analytic(sa, "prod", "sql", "prod", LNG, LargestINT, SCALE_NONE);
1816#ifdef HAVE_HGE
1817 if (have_hge)
1818 sql_create_analytic(sa, "prod", "sql", "prod", HGE, LargestINT, SCALE_NONE);
1819#endif
1820
1821#if 0
1822 /* prod for decimals introduce errors in the output scales */
1823 t = decimals; // BTE
1824 sql_create_analytic(sa, "prod", "sql", "prod", *(t), LargestDEC, SCALE_NONE);
1825 t++; // SHT
1826 sql_create_analytic(sa, "prod", "sql", "prod", *(t), LargestDEC, SCALE_NONE);
1827 t++; // INT
1828 sql_create_analytic(sa, "prod", "sql", "prod", *(t), LargestDEC, SCALE_NONE);
1829 t++; // LNG
1830 sql_create_analytic(sa, "prod", "sql", "prod", *(t), LargestDEC, SCALE_NONE);
1831#ifdef HAVE_HGE
1832 if (have_hge) {
1833 t++; // HGE
1834 sql_create_analytic(sa, "prod", "sql", "prod", *(t), LargestDEC, SCALE_NONE);
1835 }
1836#endif
1837#endif
1838
1839 for (t = floats; t < dates; t++) {
1840 sql_create_analytic(sa, "sum", "sql", "sum", *t, *t, SCALE_NONE);
1841 sql_create_analytic(sa, "prod", "sql", "prod", *t, *t, SCALE_NONE);
1842 }
1843 sql_create_analytic(sa, "sum", "sql", "sum", MONINT, MONINT, SCALE_NONE);
1844 sql_create_analytic(sa, "sum", "sql", "sum", SECINT, SECINT, SCALE_NONE);
1845
1846 //analytical average for numerical types
1847 sql_create_analytic(sa, "avg", "sql", "avg", DBL, DBL, SCALE_NONE);
1848 sql_create_analytic(sa, "avg", "sql", "avg", BTE, DBL, SCALE_NONE);
1849 sql_create_analytic(sa, "avg", "sql", "avg", SHT, DBL, SCALE_NONE);
1850 sql_create_analytic(sa, "avg", "sql", "avg", INT, DBL, SCALE_NONE);
1851 sql_create_analytic(sa, "avg", "sql", "avg", LNG, DBL, SCALE_NONE);
1852#ifdef HAVE_HGE
1853 if (have_hge)
1854 sql_create_analytic(sa, "avg", "sql", "avg", HGE, DBL, SCALE_NONE);
1855#endif
1856
1857 sql_create_analytic(sa, "avg", "sql", "avg", MONINT, DBL, SCALE_NONE);
1858 //sql_create_analytic(sa, "avg", "sql", "avg", SECINT, DBL, SCALE_NONE);
1859
1860#if 0
1861 t = decimals; // BTE
1862 sql_create_analytic(sa, "avg", "sql", "avg", *(t), DBL, SCALE_NONE);
1863 t++; // SHT
1864 sql_create_analytic(sa, "avg", "sql", "avg", *(t), DBL, SCALE_NONE);
1865 t++; // INT
1866 sql_create_analytic(sa, "avg", "sql", "avg", *(t), DBL, SCALE_NONE);
1867 t++; // LNG
1868 sql_create_analytic(sa, "avg", "sql", "avg", *(t), DBL, SCALE_NONE);
1869#ifdef HAVE_HGE
1870 if (have_hge) {
1871 t++; // HGE
1872 sql_create_analytic(sa, "avg", "sql", "avg", *(t), DBL, SCALE_NONE);
1873 }
1874#endif
1875#endif
1876 sql_create_analytic(sa, "avg", "sql", "avg", FLT, DBL, SCALE_NONE);
1877
1878 sql_create_func(sa, "and", "calc", "and", BIT, BIT, BIT, SCALE_FIX);
1879 sql_create_func(sa, "or", "calc", "or", BIT, BIT, BIT, SCALE_FIX);
1880 sql_create_func(sa, "xor", "calc", "xor", BIT, BIT, BIT, SCALE_FIX);
1881 sql_create_func(sa, "not", "calc", "not", BIT, NULL,BIT, SCALE_FIX);
1882
1883 /* allow smaller types for arguments of mul/div */
1884 for (t = numerical, t++; t != decimals; t++) {
1885 sql_type **u;
1886 for (u = numerical, u++; u != decimals; u++) {
1887 if (t != u && (*t)->localtype > (*u)->localtype) {
1888 sql_create_func(sa, "sql_mul", "calc", "*", *t, *u, *t, SCALE_MUL);
1889 sql_create_func(sa, "sql_mul", "calc", "*", *u, *t, *t, SCALE_MUL);
1890 sql_create_func(sa, "sql_div", "calc", "/", *t, *u, *t, SCALE_DIV);
1891 }
1892 }
1893 }
1894 for (t = decimals, t++; t != floats; t++) {
1895 sql_type **u;
1896
1897 for (u = decimals, u++; u != floats; u++) {
1898 if (t != u && (*t)->localtype > (*u)->localtype) {
1899 sql_create_func(sa, "sql_mul", "calc", "*", *t, *u, *t, SCALE_MUL);
1900 sql_create_func(sa, "sql_div", "calc", "/", *t, *u, *t, SCALE_DIV);
1901 }
1902 }
1903 }
1904
1905 /* all numericals */
1906 for (t = numerical; *t != TME; t++) {
1907 sql_subtype *lt;
1908
1909 lt = sql_bind_localtype((*t)->base.name);
1910
1911 sql_create_func(sa, "sql_sub", "calc", "-", *t, *t, *t, SCALE_FIX);
1912 sql_create_func(sa, "sql_add", "calc", "+", *t, *t, *t, SCALE_FIX);
1913 sql_create_func(sa, "sql_mul", "calc", "*", *t, *t, *t, SCALE_MUL);
1914 sql_create_func(sa, "sql_div", "calc", "/", *t, *t, *t, SCALE_DIV);
1915 if (t < floats) {
1916 sql_create_func(sa, "bit_and", "calc", "and", *t, *t, *t, SCALE_FIX);
1917 sql_create_func(sa, "bit_or", "calc", "or", *t, *t, *t, SCALE_FIX);
1918 sql_create_func(sa, "bit_xor", "calc", "xor", *t, *t, *t, SCALE_FIX);
1919 sql_create_func(sa, "bit_not", "calc", "not", *t, NULL, *t, SCALE_FIX);
1920 sql_create_func(sa, "left_shift", "calc", "<<", *t, INT, *t, SCALE_FIX);
1921 sql_create_func(sa, "right_shift", "calc", ">>", *t, INT, *t, SCALE_FIX);
1922 }
1923 sql_create_func(sa, "sql_neg", "calc", "-", *t, NULL, *t, INOUT);
1924 sql_create_func(sa, "abs", "calc", "abs", *t, NULL, *t, SCALE_FIX);
1925 sql_create_func(sa, "sign", "calc", "sign", *t, NULL, BTE, SCALE_NONE);
1926 /* scale fixing for all numbers */
1927 sql_create_func(sa, "scale_up", "calc", "*", *t, lt->type, *t, SCALE_NONE);
1928 sql_create_func(sa, "scale_down", "sql", "dec_round", *t, lt->type, *t, SCALE_NONE);
1929 /* numeric function on INTERVALS */
1930 if (*t != MONINT && *t != SECINT){
1931 sql_create_func(sa, "sql_sub", "calc", "-", MONINT, *t, MONINT, SCALE_FIX);
1932 sql_create_func(sa, "sql_add", "calc", "+", MONINT, *t, MONINT, SCALE_FIX);
1933 sql_create_func(sa, "sql_mul", "calc", "*", MONINT, *t, MONINT, SCALE_MUL);
1934 sql_create_func(sa, "sql_div", "calc", "/", MONINT, *t, MONINT, SCALE_DIV);
1935 sql_create_func(sa, "sql_sub", "calc", "-", SECINT, *t, SECINT, SCALE_FIX);
1936 sql_create_func(sa, "sql_add", "calc", "+", SECINT, *t, SECINT, SCALE_FIX);
1937 sql_create_func(sa, "sql_mul", "calc", "*", SECINT, *t, SECINT, SCALE_MUL);
1938 sql_create_func(sa, "sql_div", "calc", "/", SECINT, *t, SECINT, SCALE_DIV);
1939 }
1940 }
1941 for (t = decimals, t++; t != floats; t++) {
1942 sql_type **u;
1943 for (u = numerical; u != floats; u++) {
1944 if (*u == OID)
1945 continue;
1946 if ((*t)->localtype > (*u)->localtype) {
1947 sql_create_func(sa, "sql_mul", "calc", "*", *t, *u, *t, SCALE_MUL);
1948 sql_create_func(sa, "sql_mul", "calc", "*", *u, *t, *t, SCALE_MUL);
1949 }
1950 }
1951 }
1952
1953 for (t = decimals; t < dates; t++)
1954 sql_create_func(sa, "round", "sql", "round", *t, BTE, *t, INOUT);
1955
1956 for (t = numerical; t < end; t++) {
1957 sql_type **u;
1958
1959 for (u = numerical; u < end; u++) {
1960 sql_create_func(sa, "scale_up", "calc", "*", *u, *t, *t, SCALE_NONE);
1961 }
1962 }
1963
1964 for (t = floats; t < dates; t++) {
1965 sql_create_func(sa, "power", "mmath", "pow", *t, *t, *t, SCALE_FIX);
1966 sql_create_func(sa, "floor", "mmath", "floor", *t, NULL, *t, SCALE_FIX);
1967 sql_create_func(sa, "ceil", "mmath", "ceil", *t, NULL, *t, SCALE_FIX);
1968 sql_create_func(sa, "ceiling", "mmath", "ceil", *t, NULL, *t, SCALE_FIX); /* JDBC */
1969 sql_create_func(sa, "sin", "mmath", "sin", *t, NULL, *t, SCALE_FIX);
1970 sql_create_func(sa, "cos", "mmath", "cos", *t, NULL, *t, SCALE_FIX);
1971 sql_create_func(sa, "tan", "mmath", "tan", *t, NULL, *t, SCALE_FIX);
1972 sql_create_func(sa, "asin", "mmath", "asin", *t, NULL, *t, SCALE_FIX);
1973 sql_create_func(sa, "acos", "mmath", "acos", *t, NULL, *t, SCALE_FIX);
1974 sql_create_func(sa, "atan", "mmath", "atan", *t, NULL, *t, SCALE_FIX);
1975 sql_create_func(sa, "atan", "mmath", "atan2", *t, *t, *t, SCALE_FIX);
1976 sql_create_func(sa, "sinh", "mmath", "sinh", *t, NULL, *t, SCALE_FIX);
1977 sql_create_func(sa, "cot", "mmath", "cot", *t, NULL, *t, SCALE_FIX);
1978 sql_create_func(sa, "cosh", "mmath", "cosh", *t, NULL, *t, SCALE_FIX);
1979 sql_create_func(sa, "tanh", "mmath", "tanh", *t, NULL, *t, SCALE_FIX);
1980 sql_create_func(sa, "sqrt", "mmath", "sqrt", *t, NULL, *t, SCALE_FIX);
1981 sql_create_func(sa, "exp", "mmath", "exp", *t, NULL, *t, SCALE_FIX);
1982 sql_create_func(sa, "log", "mmath", "log", *t, NULL, *t, SCALE_FIX);
1983 sql_create_func(sa, "ln", "mmath", "log", *t, NULL, *t, SCALE_FIX);
1984 sql_create_func(sa, "log", "mmath", "log", *t, *t, *t, SCALE_FIX);
1985 sql_create_func(sa, "log10", "mmath", "log10", *t, NULL, *t, SCALE_FIX);
1986 sql_create_func(sa, "log2", "mmath", "log2", *t, NULL, *t, SCALE_FIX);
1987 }
1988 sql_create_func(sa, "pi", "mmath", "pi", NULL, NULL, DBL, SCALE_NONE);
1989
1990 sql_create_funcSE(sa, "rand", "mmath", "rand", NULL, NULL, INT, SCALE_NONE);
1991 sql_create_funcSE(sa, "rand", "mmath", "sqlrand", INT, NULL, INT, SCALE_NONE);
1992
1993 /* Date functions */
1994 sql_create_func(sa, "curdate", "mtime", "current_date", NULL, NULL, DTE, SCALE_NONE);
1995 sql_create_func(sa, "current_date", "mtime", "current_date", NULL, NULL, DTE, SCALE_NONE);
1996 sql_create_func(sa, "curtime", "mtime", "current_time", NULL, NULL, TMETZ, SCALE_NONE);
1997 sql_create_func(sa, "current_time", "mtime", "current_time", NULL, NULL, TMETZ, SCALE_NONE);
1998 sql_create_func(sa, "current_timestamp", "mtime", "current_timestamp", NULL, NULL, TMESTAMPTZ, SCALE_NONE);
1999 sql_create_func(sa, "localtime", "sql", "current_time", NULL, NULL, TME, SCALE_NONE);
2000 sql_create_func(sa, "localtimestamp", "sql", "current_timestamp", NULL, NULL, TMESTAMP, SCALE_NONE);
2001
2002 sql_create_func(sa, "sql_sub", "mtime", "diff", DTE, DTE, INT, SCALE_FIX);
2003 sql_create_func(sa, "sql_sub", "mtime", "diff", TMETZ, TMETZ, SECINT, SCALE_NONE);
2004 sql_create_func(sa, "sql_sub", "mtime", "diff", TME, TME, SECINT, SCALE_FIX);
2005 sql_create_func(sa, "sql_sub", "mtime", "diff", TMESTAMPTZ, TMESTAMPTZ, SECINT, SCALE_NONE);
2006 sql_create_func(sa, "sql_sub", "mtime", "diff", TMESTAMP, TMESTAMP, SECINT, SCALE_FIX);
2007
2008 sql_create_func(sa, "sql_sub", "mtime", "date_sub_msec_interval", DTE, SECINT, DTE, SCALE_NONE);
2009 sql_create_func(sa, "sql_sub", "mtime", "date_sub_month_interval", DTE, MONINT, DTE, SCALE_NONE);
2010 sql_create_func(sa, "sql_sub", "mtime", "time_sub_msec_interval", TME, SECINT, TME, SCALE_NONE);
2011 sql_create_func(sa, "sql_sub", "mtime", "time_sub_msec_interval", TMETZ, SECINT, TMETZ, SCALE_NONE);
2012 sql_create_func(sa, "sql_sub", "mtime", "timestamp_sub_msec_interval", TMESTAMP, SECINT, TMESTAMP, SCALE_NONE);
2013 sql_create_func(sa, "sql_sub", "mtime", "timestamp_sub_month_interval", TMESTAMP, MONINT, TMESTAMP, SCALE_NONE);
2014 sql_create_func(sa, "sql_sub", "mtime", "timestamp_sub_msec_interval", TMESTAMPTZ, SECINT, TMESTAMPTZ, SCALE_NONE);
2015 sql_create_func(sa, "sql_sub", "mtime", "timestamp_sub_month_interval", TMESTAMPTZ, MONINT, TMESTAMPTZ, SCALE_NONE);
2016
2017 sql_create_func(sa, "sql_add", "mtime", "date_add_msec_interval", DTE, SECINT, DTE, SCALE_NONE);
2018 sql_create_func(sa, "sql_add", "mtime", "addmonths", DTE, MONINT, DTE, SCALE_NONE);
2019 sql_create_func(sa, "sql_add", "mtime", "timestamp_add_msec_interval", TMESTAMP, SECINT, TMESTAMP, SCALE_NONE);
2020 sql_create_func(sa, "sql_add", "mtime", "timestamp_add_month_interval", TMESTAMP, MONINT, TMESTAMP, SCALE_NONE);
2021 sql_create_func(sa, "sql_add", "mtime", "timestamp_add_msec_interval", TMESTAMPTZ, SECINT, TMESTAMPTZ, SCALE_NONE);
2022 sql_create_func(sa, "sql_add", "mtime", "timestamp_add_month_interval", TMESTAMPTZ, MONINT, TMESTAMPTZ, SCALE_NONE);
2023 sql_create_func(sa, "sql_add", "mtime", "time_add_msec_interval", TME, SECINT, TME, SCALE_NONE);
2024 sql_create_func(sa, "sql_add", "mtime", "time_add_msec_interval", TMETZ, SECINT, TMETZ, SCALE_NONE);
2025
2026 sql_create_func(sa, "local_timezone", "mtime", "local_timezone", NULL, NULL, SECINT, SCALE_FIX);
2027
2028 sql_create_func(sa, "century", "mtime", "century", DTE, NULL, INT, SCALE_FIX);
2029 sql_create_func(sa, "decade", "mtime", "decade", DTE, NULL, INT, SCALE_FIX);
2030 sql_create_func(sa, "year", "mtime", "year", DTE, NULL, INT, SCALE_FIX);
2031 sql_create_func(sa, "quarter", "mtime", "quarter", DTE, NULL, INT, SCALE_FIX);
2032 sql_create_func(sa, "month", "mtime", "month", DTE, NULL, INT, SCALE_FIX);
2033 sql_create_func(sa, "day", "mtime", "day", DTE, NULL, INT, SCALE_FIX);
2034 sql_create_func(sa, "dayofyear", "mtime", "dayofyear", DTE, NULL, INT, SCALE_FIX);
2035 sql_create_func(sa, "weekofyear", "mtime", "weekofyear", DTE, NULL, INT, SCALE_FIX);
2036 sql_create_func(sa, "dayofweek", "mtime", "dayofweek", DTE, NULL, INT, SCALE_FIX);
2037 sql_create_func(sa, "dayofmonth", "mtime", "day", DTE, NULL, INT, SCALE_FIX);
2038 sql_create_func(sa, "week", "mtime", "weekofyear", DTE, NULL, INT, SCALE_FIX);
2039
2040 sql_create_func(sa, "hour", "mtime", "hours", TME, NULL, INT, SCALE_FIX);
2041 sql_create_func(sa, "minute", "mtime", "minutes", TME, NULL, INT, SCALE_FIX);
2042 sql_create_func_res(sa, "second", "mtime", "sql_seconds", TME, NULL, DEC, SCALE_NONE, 6);
2043 sql_create_func(sa, "hour", "mtime", "hours", TMETZ, NULL, INT, SCALE_FIX);
2044 sql_create_func(sa, "minute", "mtime", "minutes", TMETZ, NULL, INT, SCALE_FIX);
2045 sql_create_func_res(sa, "second", "mtime", "sql_seconds", TMETZ, NULL, DEC, SCALE_NONE, 6);
2046
2047 sql_create_func(sa, "century", "mtime", "century", TMESTAMP, NULL, INT, SCALE_FIX);
2048 sql_create_func(sa, "decade", "mtime", "decade", TMESTAMP, NULL, INT, SCALE_FIX);
2049 sql_create_func(sa, "year", "mtime", "year", TMESTAMP, NULL, INT, SCALE_FIX);
2050 sql_create_func(sa, "quarter", "mtime", "quarter", TMESTAMP, NULL, INT, SCALE_FIX);
2051 sql_create_func(sa, "month", "mtime", "month", TMESTAMP, NULL, INT, SCALE_FIX);
2052 sql_create_func(sa, "day", "mtime", "day", TMESTAMP, NULL, INT, SCALE_FIX);
2053 sql_create_func(sa, "hour", "mtime", "hours", TMESTAMP, NULL, INT, SCALE_FIX);
2054 sql_create_func(sa, "minute", "mtime", "minutes", TMESTAMP, NULL, INT, SCALE_FIX);
2055 sql_create_func_res(sa, "second", "mtime", "sql_seconds", TMESTAMP, NULL, DEC, SCALE_NONE, 6);
2056
2057 sql_create_func(sa, "century", "mtime", "century", TMESTAMPTZ, NULL, INT, SCALE_FIX);
2058 sql_create_func(sa, "decade", "mtime", "decade", TMESTAMPTZ, NULL, INT, SCALE_FIX);
2059 sql_create_func(sa, "year", "mtime", "year", TMESTAMPTZ, NULL, INT, SCALE_FIX);
2060 sql_create_func(sa, "quarter", "mtime", "quarter", TMESTAMPTZ, NULL, INT, SCALE_FIX);
2061 sql_create_func(sa, "month", "mtime", "month", TMESTAMPTZ, NULL, INT, SCALE_FIX);
2062 sql_create_func(sa, "day", "mtime", "day", TMESTAMPTZ, NULL, INT, SCALE_FIX);
2063 sql_create_func(sa, "hour", "mtime", "hours", TMESTAMPTZ, NULL, INT, SCALE_FIX);
2064 sql_create_func(sa, "minute", "mtime", "minutes", TMESTAMPTZ, NULL, INT, SCALE_FIX);
2065 sql_create_func_res(sa, "second", "mtime", "sql_seconds", TMESTAMPTZ, NULL, DEC, SCALE_NONE, 6);
2066
2067 sql_create_func(sa, "year", "mtime", "year", MONINT, NULL, INT, SCALE_NONE);
2068 sql_create_func(sa, "month", "mtime", "month", MONINT, NULL, INT, SCALE_NONE);
2069 sql_create_func(sa, "day", "mtime", "day", SECINT, NULL, LNG, SCALE_NONE);
2070 sql_create_func(sa, "hour", "mtime", "hours", SECINT, NULL, INT, SCALE_NONE);
2071 sql_create_func(sa, "minute", "mtime", "minutes", SECINT, NULL, INT, SCALE_NONE);
2072 sql_create_func(sa, "second", "mtime", "seconds", SECINT, NULL, INT, SCALE_NONE);
2073
2074 sql_create_funcSE(sa, "next_value_for", "sql", "next_value", STR, STR, LNG, SCALE_NONE);
2075 sql_create_func(sa, "get_value_for", "sql", "get_value", STR, STR, LNG, SCALE_NONE);
2076 sql_create_func3(sa, "restart", "sql", "restart", STR, STR, LNG, LNG, SCALE_NONE);
2077 for (t = strings; t < numerical; t++) {
2078 sql_create_func(sa, "index", "calc", "index", *t, BIT, BTE, SCALE_NONE);
2079 sql_create_func(sa, "index", "calc", "index", *t, BIT, SHT, SCALE_NONE);
2080 sql_create_func(sa, "index", "calc", "index", *t, BIT, INT, SCALE_NONE);
2081 sql_create_func(sa, "strings", "calc", "strings", *t, NULL, *t, SCALE_NONE);
2082
2083 sql_create_func(sa, "locate", "str", "locate", *t, *t, INT, SCALE_NONE);
2084 sql_create_func3(sa, "locate", "str", "locate", *t, *t, INT, INT, SCALE_NONE);
2085 sql_create_func(sa, "charindex", "str", "locate", *t, *t, INT, SCALE_NONE);
2086 sql_create_func3(sa, "charindex", "str", "locate", *t, *t, INT, INT, SCALE_NONE);
2087 sql_create_func3(sa, "splitpart", "str", "splitpart", *t, *t, INT, *t, INOUT);
2088 sql_create_func(sa, "substring", "str", "substring", *t, INT, *t, INOUT);
2089 sql_create_func3(sa, "substring", "str", "substring", *t, INT, INT, *t, INOUT);
2090 sql_create_func(sa, "substr", "str", "substring", *t, INT, *t, INOUT);
2091 sql_create_func3(sa, "substr", "str", "substring", *t, INT, INT, *t, INOUT);
2092 /*
2093 sql_create_func(sa, "like", "algebra", "like", *t, *t, BIT, SCALE_NONE);
2094 sql_create_func3(sa, "like", "algebra", "like", *t, *t, *t, BIT, SCALE_NONE);
2095 sql_create_func(sa, "ilike", "algebra", "ilike", *t, *t, BIT, SCALE_NONE);
2096 sql_create_func3(sa, "ilike", "algebra", "ilike", *t, *t, *t, BIT, SCALE_NONE);
2097 */
2098 sql_create_func(sa, "not_like", "algebra", "not_like", *t, *t, BIT, SCALE_NONE);
2099 sql_create_func3(sa, "not_like", "algebra", "not_like", *t, *t, *t, BIT, SCALE_NONE);
2100 sql_create_func(sa, "not_ilike", "algebra", "not_ilike", *t, *t, BIT, SCALE_NONE);
2101 sql_create_func3(sa, "not_ilike", "algebra", "not_ilike", *t, *t, *t, BIT, SCALE_NONE);
2102
2103 sql_create_func(sa, "patindex", "pcre", "patindex", *t, *t, INT, SCALE_NONE);
2104 sql_create_func(sa, "truncate", "str", "stringleft", *t, INT, *t, SCALE_NONE);
2105 sql_create_func(sa, "concat", "calc", "+", *t, *t, *t, DIGITS_ADD);
2106 sql_create_func(sa, "ascii", "str", "ascii", *t, NULL, INT, SCALE_NONE);
2107 sql_create_func(sa, "code", "str", "unicode", INT, NULL, *t, SCALE_NONE);
2108 sql_create_func(sa, "length", "str", "length", *t, NULL, INT, SCALE_NONE);
2109 sql_create_func(sa, "right", "str", "stringright", *t, INT, *t, SCALE_NONE);
2110 sql_create_func(sa, "left", "str", "stringleft", *t, INT, *t, SCALE_NONE);
2111 sql_create_func(sa, "upper", "str", "toUpper", *t, NULL, *t, SCALE_NONE);
2112 sql_create_func(sa, "ucase", "str", "toUpper", *t, NULL, *t, SCALE_NONE);
2113 sql_create_func(sa, "lower", "str", "toLower", *t, NULL, *t, SCALE_NONE);
2114 sql_create_func(sa, "lcase", "str", "toLower", *t, NULL, *t, SCALE_NONE);
2115 sql_create_func(sa, "trim", "str", "trim", *t, NULL, *t, SCALE_NONE);
2116 sql_create_func(sa, "trim", "str", "trim", *t, *t, *t, SCALE_NONE);
2117 sql_create_func(sa, "ltrim", "str", "ltrim", *t, NULL, *t, SCALE_NONE);
2118 sql_create_func(sa, "ltrim", "str", "ltrim", *t, *t, *t, SCALE_NONE);
2119 sql_create_func(sa, "rtrim", "str", "rtrim", *t, NULL, *t, SCALE_NONE);
2120 sql_create_func(sa, "rtrim", "str", "rtrim", *t, *t, *t, SCALE_NONE);
2121
2122 sql_create_func(sa, "lpad", "str", "lpad", *t, INT, *t, SCALE_NONE);
2123 sql_create_func3(sa, "lpad", "str", "lpad", *t, INT, *t, *t, SCALE_NONE);
2124 sql_create_func(sa, "rpad", "str", "rpad", *t, INT, *t, SCALE_NONE);
2125 sql_create_func3(sa, "rpad", "str", "rpad", *t, INT, *t, *t, SCALE_NONE);
2126
2127 sql_create_func4(sa, "insert", "str", "insert", *t, INT, INT, *t, *t, SCALE_NONE);
2128 sql_create_func3(sa, "replace", "str", "replace", *t, *t, *t, *t, SCALE_NONE);
2129 sql_create_func(sa, "repeat", "str", "repeat", *t, INT, *t, SCALE_NONE);
2130 sql_create_func(sa, "space", "str", "space", INT, NULL, *t, SCALE_NONE);
2131 sql_create_func(sa, "char_length", "str", "length", *t, NULL, INT, SCALE_NONE);
2132 sql_create_func(sa, "character_length", "str", "length", *t, NULL, INT, SCALE_NONE);
2133 sql_create_func(sa, "octet_length", "str", "nbytes", *t, NULL, INT, SCALE_NONE);
2134
2135 sql_create_func(sa, "soundex", "txtsim", "soundex", *t, NULL, *t, SCALE_NONE);
2136 sql_create_func(sa, "difference", "txtsim", "stringdiff", *t, *t, INT, SCALE_NONE);
2137 sql_create_func(sa, "editdistance", "txtsim", "editdistance", *t, *t, INT, SCALE_FIX);
2138 sql_create_func(sa, "editdistance2", "txtsim", "editdistance2", *t, *t, INT, SCALE_FIX);
2139
2140 sql_create_func(sa, "similarity", "txtsim", "similarity", *t, *t, DBL, SCALE_FIX);
2141 sql_create_func(sa, "qgramnormalize", "txtsim", "qgramnormalize", *t, NULL, *t, SCALE_NONE);
2142
2143 sql_create_func(sa, "levenshtein", "txtsim", "levenshtein", *t, *t, INT, SCALE_FIX);
2144 sres = create_arg(sa, NULL, sql_create_subtype(sa, INT, 0, 0), ARG_OUT);
2145 sql_create_func_(sa, "levenshtein", "txtsim", "levenshtein",
2146 list_append(list_append (list_append (list_append(list_append(sa_list(sa),
2147 create_arg(sa, NULL, sql_create_subtype(sa, *t, 0, 0), ARG_IN)),
2148 create_arg(sa, NULL, sql_create_subtype(sa, *t, 0, 0), ARG_IN)),
2149 create_arg(sa, NULL, sql_create_subtype(sa, INT, 0, 0), ARG_IN)),
2150 create_arg(sa, NULL, sql_create_subtype(sa, INT, 0, 0), ARG_IN)),
2151 create_arg(sa, NULL, sql_create_subtype(sa, INT, 0, 0), ARG_IN)),
2152 sres, FALSE, F_FUNC, SCALE_FIX);
2153 }
2154 sres = create_arg(sa, NULL, sql_create_subtype(sa, TABLE, 0, 0), ARG_OUT);
2155 /* copyfrom fname (arg 12) */
2156 f=sql_create_func_(sa, "copyfrom", "sql", "copy_from",
2157 list_append( list_append( list_append( list_append( list_append( list_append(list_append (list_append (list_append(list_append(list_append(list_append(sa_list(sa),
2158 create_arg(sa, NULL, sql_create_subtype(sa, PTR, 0, 0), ARG_IN)),
2159 create_arg(sa, NULL, sql_create_subtype(sa, STR, 0, 0), ARG_IN)),
2160 create_arg(sa, NULL, sql_create_subtype(sa, STR, 0, 0), ARG_IN)),
2161 create_arg(sa, NULL, sql_create_subtype(sa, STR, 0, 0), ARG_IN)),
2162 create_arg(sa, NULL, sql_create_subtype(sa, STR, 0, 0), ARG_IN)),
2163 create_arg(sa, NULL, sql_create_subtype(sa, STR, 0, 0), ARG_IN)),
2164 create_arg(sa, NULL, sql_create_subtype(sa, LNG, 0, 0), ARG_IN)),
2165 create_arg(sa, NULL, sql_create_subtype(sa, LNG, 0, 0), ARG_IN)),
2166 create_arg(sa, NULL, sql_create_subtype(sa, INT, 0, 0), ARG_IN)),
2167 create_arg(sa, NULL, sql_create_subtype(sa, INT, 0, 0), ARG_IN)),
2168 create_arg(sa, NULL, sql_create_subtype(sa, STR, 0, 0), ARG_IN)),
2169 create_arg(sa, NULL, sql_create_subtype(sa, INT, 0, 0), ARG_IN)), sres, FALSE, F_UNION, SCALE_FIX);
2170 f->varres = 1;
2171
2172 /* bincopyfrom */
2173 f = sql_create_func_(sa, "copyfrom", "sql", "importTable",
2174 list_append(list_append(list_append(sa_list(sa),
2175 create_arg(sa, NULL, sql_create_subtype(sa, STR, 0, 0), ARG_IN)),
2176 create_arg(sa, NULL, sql_create_subtype(sa, STR, 0, 0), ARG_IN)),
2177 create_arg(sa, NULL, sql_create_subtype(sa, INT, 0, 0), ARG_IN)), sres, FALSE, F_UNION, SCALE_FIX);
2178 f->varres = 1;
2179
2180 /* sys_update_schemas, sys_update_tables */
2181 f = sql_create_func_(sa, "sys_update_schemas", "sql", "update_schemas", NULL, NULL, FALSE, F_PROC, SCALE_NONE);
2182 f = sql_create_func_(sa, "sys_update_tables", "sql", "update_tables", NULL, NULL, FALSE, F_PROC, SCALE_NONE);
2183}
2184
2185void
2186types_init(sql_allocator *sa, int debug)
2187{
2188 (void)debug;
2189 aliases = sa_list(sa);
2190 types = sa_list(sa);
2191 localtypes = sa_list(sa);
2192 aggrs = sa_list(sa);
2193 funcs = sa_list(sa);
2194 MT_lock_set(&funcs->ht_lock);
2195 funcs->ht = hash_new(sa, 1024, (fkeyvalue)&base_key);
2196 MT_lock_unset(&funcs->ht_lock);
2197 sqltypeinit( sa );
2198}
2199