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