| 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 | #include "monetdb_config.h" |
| 10 | #include "sql.h" |
| 11 | #include "sql_result.h" |
| 12 | #include "sql_cast.h" |
| 13 | #include "sql_gencode.h" |
| 14 | #include "sql_storage.h" |
| 15 | #include "sql_scenario.h" |
| 16 | #include "store_sequence.h" |
| 17 | #include "sql_optimizer.h" |
| 18 | #include "sql_datetime.h" |
| 19 | #include "rel_optimizer.h" |
| 20 | #include "rel_distribute.h" |
| 21 | #include "rel_select.h" |
| 22 | #include "rel_exp.h" |
| 23 | #include "rel_dump.h" |
| 24 | #include "opt_pipes.h" |
| 25 | #include "clients.h" |
| 26 | #include "mal_instruction.h" |
| 27 | |
| 28 | str |
| 29 | nil_2_timestamp(timestamp *res, const void *val) |
| 30 | { |
| 31 | (void) val; |
| 32 | *res = timestamp_nil; |
| 33 | return MAL_SUCCEED; |
| 34 | } |
| 35 | |
| 36 | str |
| 37 | str_2_timestamp(timestamp *res, const str *val) |
| 38 | { |
| 39 | ptr p = NULL; |
| 40 | size_t len = 0; |
| 41 | ssize_t e; |
| 42 | char buf[BUFSIZ]; |
| 43 | |
| 44 | e = ATOMfromstr(TYPE_timestamp, &p, &len, *val, false); |
| 45 | if (e < 0 || !p || (ATOMcmp(TYPE_timestamp, p, ATOMnilptr(TYPE_timestamp)) == 0 && ATOMcmp(TYPE_str, *val, ATOMnilptr(TYPE_str)) != 0)) { |
| 46 | if (p) |
| 47 | GDKfree(p); |
| 48 | snprintf(buf, BUFSIZ, "Conversion of string '%s' failed" , *val? *val:"" ); |
| 49 | throw(SQL, "timestamp" , SQLSTATE(42000) "%s" , buf); |
| 50 | } |
| 51 | *res = *(timestamp *) p; |
| 52 | if (!ATOMextern(TYPE_timestamp)) { |
| 53 | if (p) |
| 54 | GDKfree(p); |
| 55 | } |
| 56 | return MAL_SUCCEED; |
| 57 | } |
| 58 | |
| 59 | str |
| 60 | batnil_2_timestamp(bat *res, const bat *bid) |
| 61 | { |
| 62 | BAT *b, *dst; |
| 63 | BUN p, q; |
| 64 | |
| 65 | if ((b = BATdescriptor(*bid)) == NULL) { |
| 66 | throw(SQL, "batcalc.nil_2_timestamp" , SQLSTATE(HY005) "Cannot access column descriptor" ); |
| 67 | } |
| 68 | dst = COLnew(b->hseqbase, TYPE_timestamp, BATcount(b), TRANSIENT); |
| 69 | if (dst == NULL) { |
| 70 | BBPunfix(b->batCacheid); |
| 71 | throw(SQL, "sql.2_timestamp" , SQLSTATE(HY001) MAL_MALLOC_FAIL); |
| 72 | } |
| 73 | BATloop(b, p, q) { |
| 74 | if (BUNappend(dst, ×tamp_nil, false) != GDK_SUCCEED) { |
| 75 | BBPunfix(b->batCacheid); |
| 76 | BBPreclaim(dst); |
| 77 | throw(SQL, "sql.timestamp" , SQLSTATE(HY001) MAL_MALLOC_FAIL); |
| 78 | } |
| 79 | } |
| 80 | BBPkeepref(*res = dst->batCacheid); |
| 81 | BBPunfix(b->batCacheid); |
| 82 | return MAL_SUCCEED; |
| 83 | } |
| 84 | |
| 85 | str |
| 86 | batstr_2_timestamp(bat *res, const bat *bid) |
| 87 | { |
| 88 | BAT *b, *dst; |
| 89 | BATiter bi; |
| 90 | BUN p, q; |
| 91 | char *msg = NULL; |
| 92 | |
| 93 | if ((b = BATdescriptor(*bid)) == NULL) { |
| 94 | throw(SQL, "batcalc.str_2_timestamp" , SQLSTATE(HY005) "Cannot access column descriptor" ); |
| 95 | } |
| 96 | bi = bat_iterator(b); |
| 97 | dst = COLnew(b->hseqbase, TYPE_timestamp, BATcount(b), TRANSIENT); |
| 98 | if (dst == NULL) { |
| 99 | BBPunfix(b->batCacheid); |
| 100 | throw(SQL, "sql.2_timestamp" , SQLSTATE(HY001) MAL_MALLOC_FAIL); |
| 101 | } |
| 102 | BATloop(b, p, q) { |
| 103 | str v = (str) BUNtvar(bi, p); |
| 104 | timestamp r; |
| 105 | msg = str_2_timestamp(&r, &v); |
| 106 | if (msg) { |
| 107 | BBPunfix(dst->batCacheid); |
| 108 | BBPunfix(b->batCacheid); |
| 109 | return msg; |
| 110 | } |
| 111 | if (BUNappend(dst, &r, false) != GDK_SUCCEED) { |
| 112 | BBPunfix(b->batCacheid); |
| 113 | BBPreclaim(dst); |
| 114 | throw(SQL, "sql.timestamp" , SQLSTATE(HY001) MAL_MALLOC_FAIL); |
| 115 | } |
| 116 | } |
| 117 | BBPkeepref(*res = dst->batCacheid); |
| 118 | BBPunfix(b->batCacheid); |
| 119 | return msg; |
| 120 | } |
| 121 | |
| 122 | str |
| 123 | nil_2_daytime(daytime *res, const void *val) |
| 124 | { |
| 125 | (void) val; |
| 126 | *res = daytime_nil; |
| 127 | return MAL_SUCCEED; |
| 128 | } |
| 129 | |
| 130 | str |
| 131 | str_2_daytime(daytime *res, const str *val) |
| 132 | { |
| 133 | ptr p = NULL; |
| 134 | size_t len = 0; |
| 135 | ssize_t e; |
| 136 | char buf[BUFSIZ]; |
| 137 | |
| 138 | e = ATOMfromstr(TYPE_daytime, &p, &len, *val, false); |
| 139 | if (e < 0 || !p || (ATOMcmp(TYPE_daytime, p, ATOMnilptr(TYPE_daytime)) == 0 && ATOMcmp(TYPE_str, *val, ATOMnilptr(TYPE_str)) != 0)) { |
| 140 | if (p) |
| 141 | GDKfree(p); |
| 142 | snprintf(buf, BUFSIZ, "Conversion of string '%s' failed" , *val? *val:"" ); |
| 143 | throw(SQL, "daytime" , SQLSTATE(42000) "%s" , buf); |
| 144 | } |
| 145 | *res = *(daytime *) p; |
| 146 | if (!ATOMextern(TYPE_daytime)) { |
| 147 | if (p) |
| 148 | GDKfree(p); |
| 149 | } |
| 150 | return MAL_SUCCEED; |
| 151 | } |
| 152 | |
| 153 | str |
| 154 | batnil_2_daytime(bat *res, const bat *bid) |
| 155 | { |
| 156 | BAT *b, *dst; |
| 157 | BUN p, q; |
| 158 | |
| 159 | if ((b = BATdescriptor(*bid)) == NULL) { |
| 160 | throw(SQL, "batcalc.nil_2_daytime" , SQLSTATE(HY005) "Cannot access column descriptor" ); |
| 161 | } |
| 162 | dst = COLnew(b->hseqbase, TYPE_daytime, BATcount(b), TRANSIENT); |
| 163 | if (dst == NULL) { |
| 164 | BBPunfix(b->batCacheid); |
| 165 | throw(SQL, "sql.2_daytime" , SQLSTATE(HY001) MAL_MALLOC_FAIL); |
| 166 | } |
| 167 | daytime r = daytime_nil; |
| 168 | BATloop(b, p, q) { |
| 169 | if (BUNappend(dst, &r, false) != GDK_SUCCEED) { |
| 170 | BBPunfix(b->batCacheid); |
| 171 | BBPreclaim(dst); |
| 172 | throw(SQL, "sql.timestamp" , SQLSTATE(HY001) MAL_MALLOC_FAIL); |
| 173 | } |
| 174 | } |
| 175 | BBPkeepref(*res = dst->batCacheid); |
| 176 | BBPunfix(b->batCacheid); |
| 177 | return MAL_SUCCEED; |
| 178 | } |
| 179 | |
| 180 | str |
| 181 | batstr_2_daytime(bat *res, const bat *bid) |
| 182 | { |
| 183 | BAT *b, *dst; |
| 184 | BATiter bi; |
| 185 | BUN p, q; |
| 186 | char *msg = NULL; |
| 187 | |
| 188 | if ((b = BATdescriptor(*bid)) == NULL) { |
| 189 | throw(SQL, "batcalc.str_2_daytime" , SQLSTATE(HY005) "Cannot access column descriptor" ); |
| 190 | } |
| 191 | bi = bat_iterator(b); |
| 192 | dst = COLnew(b->hseqbase, TYPE_daytime, BATcount(b), TRANSIENT); |
| 193 | if (dst == NULL) { |
| 194 | BBPunfix(b->batCacheid); |
| 195 | throw(SQL, "sql.2_daytime" , SQLSTATE(HY001) MAL_MALLOC_FAIL); |
| 196 | } |
| 197 | BATloop(b, p, q) { |
| 198 | str v = (str) BUNtvar(bi, p); |
| 199 | daytime r; |
| 200 | msg = str_2_daytime(&r, &v); |
| 201 | if (msg) { |
| 202 | BBPunfix(dst->batCacheid); |
| 203 | BBPunfix(b->batCacheid); |
| 204 | return msg; |
| 205 | } |
| 206 | if (BUNappend(dst, &r, false) != GDK_SUCCEED) { |
| 207 | BBPunfix(b->batCacheid); |
| 208 | BBPreclaim(dst); |
| 209 | throw(SQL, "sql.daytime" , SQLSTATE(HY001) MAL_MALLOC_FAIL); |
| 210 | } |
| 211 | } |
| 212 | BBPkeepref(*res = dst->batCacheid); |
| 213 | BBPunfix(b->batCacheid); |
| 214 | return msg; |
| 215 | } |
| 216 | |
| 217 | str |
| 218 | nil_2_date(date *res, const void *val) |
| 219 | { |
| 220 | (void) val; |
| 221 | *res = date_nil; |
| 222 | return MAL_SUCCEED; |
| 223 | } |
| 224 | |
| 225 | str |
| 226 | str_2_date(date *res, const str *val) |
| 227 | { |
| 228 | ptr p = NULL; |
| 229 | size_t len = 0; |
| 230 | ssize_t e; |
| 231 | char buf[BUFSIZ]; |
| 232 | |
| 233 | e = ATOMfromstr(TYPE_date, &p, &len, *val, false); |
| 234 | if (e < 0 || !p || (ATOMcmp(TYPE_date, p, ATOMnilptr(TYPE_date)) == 0 && ATOMcmp(TYPE_str, *val, ATOMnilptr(TYPE_str)) != 0)) { |
| 235 | if (p) |
| 236 | GDKfree(p); |
| 237 | snprintf(buf, BUFSIZ, "Conversion of string '%s' failed" , *val? *val:"" ); |
| 238 | throw(SQL, "date" , SQLSTATE(42000) "%s" , buf); |
| 239 | } |
| 240 | *res = *(date *) p; |
| 241 | if (!ATOMextern(TYPE_date)) { |
| 242 | if (p) |
| 243 | GDKfree(p); |
| 244 | } |
| 245 | return MAL_SUCCEED; |
| 246 | } |
| 247 | |
| 248 | str |
| 249 | SQLdate_2_str(str *res, const date *val) |
| 250 | { |
| 251 | char *p = NULL; |
| 252 | size_t len = 0; |
| 253 | if (date_tostr(&p, &len, val, false) < 0) { |
| 254 | GDKfree(p); |
| 255 | throw(SQL, "date" , GDK_EXCEPTION); |
| 256 | } |
| 257 | *res = p; |
| 258 | return MAL_SUCCEED; |
| 259 | } |
| 260 | |
| 261 | str |
| 262 | batnil_2_date(bat *res, const bat *bid) |
| 263 | { |
| 264 | BAT *b, *dst; |
| 265 | BUN p, q; |
| 266 | |
| 267 | if ((b = BATdescriptor(*bid)) == NULL) { |
| 268 | throw(SQL, "batcalc.nil_2_date" , SQLSTATE(HY005) "Cannot access column descriptor" ); |
| 269 | } |
| 270 | dst = COLnew(b->hseqbase, TYPE_date, BATcount(b), TRANSIENT); |
| 271 | if (dst == NULL) { |
| 272 | BBPunfix(b->batCacheid); |
| 273 | throw(SQL, "sql.2_date" , SQLSTATE(HY001) MAL_MALLOC_FAIL); |
| 274 | } |
| 275 | date r = date_nil; |
| 276 | BATloop(b, p, q) { |
| 277 | if (BUNappend(dst, &r, false) != GDK_SUCCEED) { |
| 278 | BBPunfix(b->batCacheid); |
| 279 | BBPreclaim(dst); |
| 280 | throw(SQL, "sql.date" , SQLSTATE(HY001) MAL_MALLOC_FAIL); |
| 281 | } |
| 282 | } |
| 283 | BBPkeepref(*res = dst->batCacheid); |
| 284 | BBPunfix(b->batCacheid); |
| 285 | return MAL_SUCCEED; |
| 286 | } |
| 287 | |
| 288 | str |
| 289 | batstr_2_date(bat *res, const bat *bid) |
| 290 | { |
| 291 | BAT *b, *dst; |
| 292 | BATiter bi; |
| 293 | BUN p, q; |
| 294 | char *msg = NULL; |
| 295 | |
| 296 | if ((b = BATdescriptor(*bid)) == NULL) { |
| 297 | throw(SQL, "batcalc.str_2_date" , SQLSTATE(HY005) "Cannot access column descriptor" ); |
| 298 | } |
| 299 | bi = bat_iterator(b); |
| 300 | dst = COLnew(b->hseqbase, TYPE_date, BATcount(b), TRANSIENT); |
| 301 | if (dst == NULL) { |
| 302 | BBPunfix(b->batCacheid); |
| 303 | throw(SQL, "sql.2_date" , SQLSTATE(HY001) MAL_MALLOC_FAIL); |
| 304 | } |
| 305 | BATloop(b, p, q) { |
| 306 | str v = (str) BUNtvar(bi, p); |
| 307 | date r; |
| 308 | msg = str_2_date(&r, &v); |
| 309 | if (msg) { |
| 310 | BBPunfix(dst->batCacheid); |
| 311 | BBPunfix(b->batCacheid); |
| 312 | return msg; |
| 313 | } |
| 314 | if (BUNappend(dst, &r, false) != GDK_SUCCEED) { |
| 315 | BBPunfix(b->batCacheid); |
| 316 | BBPreclaim(dst); |
| 317 | throw(SQL, "sql.date" , SQLSTATE(HY001) MAL_MALLOC_FAIL); |
| 318 | } |
| 319 | } |
| 320 | BBPkeepref(*res = dst->batCacheid); |
| 321 | BBPunfix(b->batCacheid); |
| 322 | return msg; |
| 323 | } |
| 324 | |
| 325 | str |
| 326 | str_2_blob(blob **res, const str *val) |
| 327 | { |
| 328 | ptr p = NULL; |
| 329 | size_t len = 0; |
| 330 | ssize_t e; |
| 331 | char buf[BUFSIZ]; |
| 332 | |
| 333 | e = ATOMfromstr(TYPE_blob, &p, &len, *val, false); |
| 334 | if (e < 0 || !p || (ATOMcmp(TYPE_blob, p, ATOMnilptr(TYPE_blob)) == 0 && ATOMcmp(TYPE_str, *val, ATOMnilptr(TYPE_str)) != 0)) { |
| 335 | if (p) |
| 336 | GDKfree(p); |
| 337 | snprintf(buf, BUFSIZ, "Conversion of string '%s' failed" , *val? *val:"" ); |
| 338 | throw(SQL, "blob" , SQLSTATE(42000) "%s" , buf); |
| 339 | } |
| 340 | *res = (blob *) p; |
| 341 | return MAL_SUCCEED; |
| 342 | } |
| 343 | |
| 344 | str |
| 345 | SQLblob_2_str(str *res, const blob *val) |
| 346 | { |
| 347 | char *p = NULL; |
| 348 | size_t len = 0; |
| 349 | if (BLOBtostr(&p, &len, val, false) < 0) { |
| 350 | GDKfree(p); |
| 351 | throw(SQL, "blob" , GDK_EXCEPTION); |
| 352 | } |
| 353 | *res = p; |
| 354 | return MAL_SUCCEED; |
| 355 | } |
| 356 | |
| 357 | str |
| 358 | batstr_2_blob(bat *res, const bat *bid) |
| 359 | { |
| 360 | BAT *b, *dst; |
| 361 | BATiter bi; |
| 362 | BUN p, q; |
| 363 | char *msg = NULL; |
| 364 | |
| 365 | if ((b = BATdescriptor(*bid)) == NULL) { |
| 366 | throw(SQL, "batcalc.str_2_blob" , SQLSTATE(HY005) "Cannot access column descriptor" ); |
| 367 | } |
| 368 | bi = bat_iterator(b); |
| 369 | dst = COLnew(b->hseqbase, TYPE_blob, BATcount(b), TRANSIENT); |
| 370 | if (dst == NULL) { |
| 371 | BBPunfix(b->batCacheid); |
| 372 | throw(SQL, "sql.2_blob" , SQLSTATE(HY001) MAL_MALLOC_FAIL); |
| 373 | } |
| 374 | BATloop(b, p, q) { |
| 375 | str v = (str) BUNtvar(bi, p); |
| 376 | blob *r; |
| 377 | msg = str_2_blob(&r, &v); |
| 378 | if (msg) { |
| 379 | BBPunfix(dst->batCacheid); |
| 380 | BBPunfix(b->batCacheid); |
| 381 | return msg; |
| 382 | } |
| 383 | if (BUNappend(dst, r, false) != GDK_SUCCEED) { |
| 384 | BBPunfix(b->batCacheid); |
| 385 | BBPreclaim(dst); |
| 386 | throw(SQL, "sql.blob" , SQLSTATE(HY001) MAL_MALLOC_FAIL); |
| 387 | } |
| 388 | GDKfree(r); |
| 389 | } |
| 390 | BBPkeepref(*res = dst->batCacheid); |
| 391 | BBPunfix(b->batCacheid); |
| 392 | return msg; |
| 393 | } |
| 394 | |
| 395 | static str |
| 396 | SQLstr_cast_(str *res, mvc *m, sql_class eclass, int d, int s, int has_tz, ptr p, int tpe, int len) |
| 397 | { |
| 398 | char *r = NULL; |
| 399 | int sz = MAX(2, len + 1); /* nil should fit */ |
| 400 | |
| 401 | if (tpe != TYPE_str) { |
| 402 | /* TODO get max size for all from type */ |
| 403 | if (len == 0 && tpe == TYPE_bit) /* should hold false */ |
| 404 | sz = 6; |
| 405 | r = GDKmalloc(sz); |
| 406 | if (r == NULL) |
| 407 | throw(SQL, "str_cast" , SQLSTATE(HY001) MAL_MALLOC_FAIL); |
| 408 | sz = convert2str(m, eclass, d, s, has_tz, p, tpe, &r, sz); |
| 409 | } else { |
| 410 | str v = (str) p; |
| 411 | STRLength(&sz, &v); |
| 412 | if (len == 0 || (sz >= 0 && sz <= len)) { |
| 413 | r = GDKstrdup(v); |
| 414 | if (r == NULL) |
| 415 | throw(SQL, "str_cast" , SQLSTATE(HY001) MAL_MALLOC_FAIL); |
| 416 | } |
| 417 | } |
| 418 | if ((len > 0 && sz > len) || sz < 0) { |
| 419 | if (r) |
| 420 | GDKfree(r); |
| 421 | if (ATOMcmp(tpe, ATOMnilptr(tpe), p) != 0) { |
| 422 | throw(SQL, "str_cast" , SQLSTATE(22001) "value too long for type (var)char(%d)" , len); |
| 423 | } else { |
| 424 | r = GDKstrdup(str_nil); |
| 425 | if (r == NULL) |
| 426 | throw(SQL, "str_cast" , SQLSTATE(HY001) MAL_MALLOC_FAIL); |
| 427 | } |
| 428 | } |
| 429 | *res = r; |
| 430 | return MAL_SUCCEED; |
| 431 | } |
| 432 | |
| 433 | str |
| 434 | SQLstr_cast(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci) |
| 435 | { |
| 436 | str *res = getArgReference_str(stk, pci, 0); |
| 437 | sql_class eclass = (sql_class)*getArgReference_int(stk, pci, 1); |
| 438 | int d = *getArgReference_int(stk, pci, 2); |
| 439 | int s = *getArgReference_int(stk, pci, 3); |
| 440 | int has_tz = *getArgReference_int(stk, pci, 4); |
| 441 | ptr p = getArgReference(stk, pci, 5); |
| 442 | int tpe = getArgType(mb, pci, 5); |
| 443 | int len = *getArgReference_int(stk, pci, 6); |
| 444 | mvc *m = NULL; |
| 445 | str msg; |
| 446 | |
| 447 | if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL) |
| 448 | return msg; |
| 449 | if ((msg = checkSQLContext(cntxt)) != NULL) |
| 450 | return msg; |
| 451 | if (ATOMextern(tpe)) |
| 452 | p = *(ptr *) p; |
| 453 | return SQLstr_cast_(res, m, eclass, d, s, has_tz, p, tpe, len); |
| 454 | } |
| 455 | |
| 456 | /* str SQLbatstr_cast(int *res, int *eclass, int *d1, int *s1, int *has_tz, int *bid, int *digits ); */ |
| 457 | str |
| 458 | SQLbatstr_cast(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci) |
| 459 | { |
| 460 | BAT *b, *dst; |
| 461 | BATiter bi; |
| 462 | BUN p, q; |
| 463 | mvc *m = NULL; |
| 464 | str msg; |
| 465 | char *r = NULL; |
| 466 | bat *res = getArgReference_bat(stk, pci, 0); |
| 467 | sql_class eclass = (sql_class) *getArgReference_int(stk, pci, 1); |
| 468 | int *d1 = getArgReference_int(stk, pci, 2); |
| 469 | int *s1 = getArgReference_int(stk, pci, 3); |
| 470 | int *has_tz = getArgReference_int(stk, pci, 4); |
| 471 | bat *bid = getArgReference_bat(stk, pci, 5); |
| 472 | int *digits = getArgReference_int(stk, pci, 6); |
| 473 | |
| 474 | if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != NULL) |
| 475 | return msg; |
| 476 | if ((msg = checkSQLContext(cntxt)) != NULL) |
| 477 | return msg; |
| 478 | if ((b = BATdescriptor(*bid)) == NULL) { |
| 479 | throw(SQL, "batcalc.str" , SQLSTATE(HY005) "Cannot access column descriptor" ); |
| 480 | } |
| 481 | bi = bat_iterator(b); |
| 482 | dst = COLnew(b->hseqbase, TYPE_str, BATcount(b), TRANSIENT); |
| 483 | if (dst == NULL) { |
| 484 | BBPunfix(b->batCacheid); |
| 485 | throw(SQL, "sql.str_cast" , SQLSTATE(HY001) MAL_MALLOC_FAIL); |
| 486 | } |
| 487 | BATloop(b, p, q) { |
| 488 | ptr v = (ptr) BUNtail(bi, p); |
| 489 | msg = SQLstr_cast_(&r, m, eclass, *d1, *s1, *has_tz, v, b->ttype, *digits); |
| 490 | if (msg) { |
| 491 | BBPunfix(dst->batCacheid); |
| 492 | BBPunfix(b->batCacheid); |
| 493 | return msg; |
| 494 | } |
| 495 | if (BUNappend(dst, r, false) != GDK_SUCCEED) { |
| 496 | BBPunfix(b->batCacheid); |
| 497 | BBPreclaim(dst); |
| 498 | throw(SQL, "sql.str_cast" , SQLSTATE(HY001) MAL_MALLOC_FAIL); |
| 499 | } |
| 500 | GDKfree(r); |
| 501 | r = NULL; |
| 502 | } |
| 503 | BBPkeepref(*res = dst->batCacheid); |
| 504 | BBPunfix(b->batCacheid); |
| 505 | return msg; |
| 506 | } |
| 507 | |
| 508 | /* up casting */ |
| 509 | |
| 510 | #define TP1 bte |
| 511 | #define TP2 bte |
| 512 | #include "sql_cast_impl_int.h" |
| 513 | #undef TP2 |
| 514 | #undef TP1 |
| 515 | |
| 516 | #define TP1 bte |
| 517 | #define TP2 sht |
| 518 | #include "sql_cast_impl_int.h" |
| 519 | #undef TP2 |
| 520 | #undef TP1 |
| 521 | |
| 522 | #define TP1 sht |
| 523 | #define TP2 sht |
| 524 | #include "sql_cast_impl_int.h" |
| 525 | #undef TP2 |
| 526 | #undef TP1 |
| 527 | |
| 528 | #define TP1 bte |
| 529 | #define TP2 int |
| 530 | #include "sql_cast_impl_int.h" |
| 531 | #undef TP2 |
| 532 | #undef TP1 |
| 533 | |
| 534 | #define TP1 sht |
| 535 | #define TP2 int |
| 536 | #include "sql_cast_impl_int.h" |
| 537 | #undef TP2 |
| 538 | #undef TP1 |
| 539 | |
| 540 | #define TP1 int |
| 541 | #define TP2 int |
| 542 | #include "sql_cast_impl_int.h" |
| 543 | #undef TP2 |
| 544 | #undef TP1 |
| 545 | |
| 546 | #define TP1 bte |
| 547 | #define TP2 lng |
| 548 | #include "sql_cast_impl_int.h" |
| 549 | #undef TP2 |
| 550 | #undef TP1 |
| 551 | |
| 552 | #define TP1 sht |
| 553 | #define TP2 lng |
| 554 | #include "sql_cast_impl_int.h" |
| 555 | #undef TP2 |
| 556 | #undef TP1 |
| 557 | |
| 558 | #define TP1 int |
| 559 | #define TP2 lng |
| 560 | #include "sql_cast_impl_int.h" |
| 561 | #undef TP2 |
| 562 | #undef TP1 |
| 563 | |
| 564 | #define TP1 lng |
| 565 | #define TP2 lng |
| 566 | #include "sql_cast_impl_int.h" |
| 567 | #undef TP2 |
| 568 | #undef TP1 |
| 569 | |
| 570 | #ifdef HAVE_HGE |
| 571 | #define TP1 bte |
| 572 | #define TP2 hge |
| 573 | #include "sql_cast_impl_int.h" |
| 574 | #undef TP2 |
| 575 | #undef TP1 |
| 576 | |
| 577 | #define TP1 sht |
| 578 | #define TP2 hge |
| 579 | #include "sql_cast_impl_int.h" |
| 580 | #undef TP2 |
| 581 | #undef TP1 |
| 582 | |
| 583 | #define TP1 int |
| 584 | #define TP2 hge |
| 585 | #include "sql_cast_impl_int.h" |
| 586 | #undef TP2 |
| 587 | #undef TP1 |
| 588 | |
| 589 | #define TP1 lng |
| 590 | #define TP2 hge |
| 591 | #include "sql_cast_impl_int.h" |
| 592 | #undef TP2 |
| 593 | #undef TP1 |
| 594 | |
| 595 | #define TP1 hge |
| 596 | #define TP2 hge |
| 597 | #include "sql_cast_impl_int.h" |
| 598 | #undef TP2 |
| 599 | #undef TP1 |
| 600 | #endif |
| 601 | |
| 602 | /* sql_cast_impl_down_from_flt */ |
| 603 | |
| 604 | #define round_float(x) roundf(x) |
| 605 | |
| 606 | #define TP1 flt |
| 607 | #define TP2 bte |
| 608 | #include "sql_cast_impl_down_from_flt.h" |
| 609 | #undef TP2 |
| 610 | #undef TP1 |
| 611 | |
| 612 | #define TP1 flt |
| 613 | #define TP2 sht |
| 614 | #include "sql_cast_impl_down_from_flt.h" |
| 615 | #undef TP2 |
| 616 | #undef TP1 |
| 617 | |
| 618 | #define TP1 flt |
| 619 | #define TP2 int |
| 620 | #include "sql_cast_impl_down_from_flt.h" |
| 621 | #undef TP2 |
| 622 | #undef TP1 |
| 623 | |
| 624 | #define TP1 flt |
| 625 | #define TP2 lng |
| 626 | #include "sql_cast_impl_down_from_flt.h" |
| 627 | #undef TP2 |
| 628 | #undef TP1 |
| 629 | |
| 630 | #ifdef HAVE_HGE |
| 631 | #define TP1 flt |
| 632 | #define TP2 hge |
| 633 | #include "sql_cast_impl_down_from_flt.h" |
| 634 | #undef TP2 |
| 635 | #undef TP1 |
| 636 | #endif |
| 637 | |
| 638 | #undef round_float |
| 639 | #define round_float(x) round(x) |
| 640 | |
| 641 | #define TP1 dbl |
| 642 | #define TP2 bte |
| 643 | #include "sql_cast_impl_down_from_flt.h" |
| 644 | #undef TP2 |
| 645 | #undef TP1 |
| 646 | |
| 647 | #define TP1 dbl |
| 648 | #define TP2 sht |
| 649 | #include "sql_cast_impl_down_from_flt.h" |
| 650 | #undef TP2 |
| 651 | #undef TP1 |
| 652 | |
| 653 | #define TP1 dbl |
| 654 | #define TP2 int |
| 655 | #include "sql_cast_impl_down_from_flt.h" |
| 656 | #undef TP2 |
| 657 | #undef TP1 |
| 658 | |
| 659 | #define TP1 dbl |
| 660 | #define TP2 lng |
| 661 | #include "sql_cast_impl_down_from_flt.h" |
| 662 | #undef TP2 |
| 663 | #undef TP1 |
| 664 | |
| 665 | #ifdef HAVE_HGE |
| 666 | #define TP1 dbl |
| 667 | #define TP2 hge |
| 668 | #include "sql_cast_impl_down_from_flt.h" |
| 669 | #undef TP2 |
| 670 | #undef TP1 |
| 671 | #endif |
| 672 | |
| 673 | /* sql_cast_impl_up_to_flt */ |
| 674 | |
| 675 | #define TP1 bte |
| 676 | #define TP2 flt |
| 677 | #include "sql_cast_impl_up_to_flt.h" |
| 678 | #undef TP2 |
| 679 | #undef TP1 |
| 680 | |
| 681 | #define TP1 sht |
| 682 | #define TP2 flt |
| 683 | #include "sql_cast_impl_up_to_flt.h" |
| 684 | #undef TP2 |
| 685 | #undef TP1 |
| 686 | |
| 687 | #define TP1 int |
| 688 | #define TP2 flt |
| 689 | #include "sql_cast_impl_up_to_flt.h" |
| 690 | #undef TP2 |
| 691 | #undef TP1 |
| 692 | |
| 693 | #define TP1 lng |
| 694 | #define TP2 flt |
| 695 | #include "sql_cast_impl_up_to_flt.h" |
| 696 | #undef TP2 |
| 697 | #undef TP1 |
| 698 | |
| 699 | #ifdef HAVE_HGE |
| 700 | #define TP1 hge |
| 701 | #define TP2 flt |
| 702 | #include "sql_cast_impl_up_to_flt.h" |
| 703 | #undef TP2 |
| 704 | #undef TP1 |
| 705 | #endif |
| 706 | |
| 707 | #define TP1 bte |
| 708 | #define TP2 dbl |
| 709 | #include "sql_cast_impl_up_to_flt.h" |
| 710 | #undef TP2 |
| 711 | #undef TP1 |
| 712 | |
| 713 | #define TP1 sht |
| 714 | #define TP2 dbl |
| 715 | #include "sql_cast_impl_up_to_flt.h" |
| 716 | #undef TP2 |
| 717 | #undef TP1 |
| 718 | |
| 719 | #define TP1 int |
| 720 | #define TP2 dbl |
| 721 | #include "sql_cast_impl_up_to_flt.h" |
| 722 | #undef TP2 |
| 723 | #undef TP1 |
| 724 | |
| 725 | #define TP1 lng |
| 726 | #define TP2 dbl |
| 727 | #include "sql_cast_impl_up_to_flt.h" |
| 728 | #undef TP2 |
| 729 | #undef TP1 |
| 730 | |
| 731 | #ifdef HAVE_HGE |
| 732 | #define TP1 hge |
| 733 | #define TP2 dbl |
| 734 | #include "sql_cast_impl_up_to_flt.h" |
| 735 | #undef TP2 |
| 736 | #undef TP1 |
| 737 | #endif |
| 738 | |
| 739 | #define TP1 sht |
| 740 | #define TP2 bte |
| 741 | #include "sql_cast_impl_int.h" |
| 742 | #undef TP2 |
| 743 | #undef TP1 |
| 744 | |
| 745 | #define TP1 int |
| 746 | #define TP2 bte |
| 747 | #include "sql_cast_impl_int.h" |
| 748 | #undef TP2 |
| 749 | #undef TP1 |
| 750 | |
| 751 | #define TP1 lng |
| 752 | #define TP2 bte |
| 753 | #include "sql_cast_impl_int.h" |
| 754 | #undef TP2 |
| 755 | #undef TP1 |
| 756 | |
| 757 | #ifdef HAVE_HGE |
| 758 | #define TP1 hge |
| 759 | #define TP2 bte |
| 760 | #include "sql_cast_impl_int.h" |
| 761 | #undef TP2 |
| 762 | #undef TP1 |
| 763 | #endif |
| 764 | |
| 765 | #define TP1 int |
| 766 | #define TP2 sht |
| 767 | #include "sql_cast_impl_int.h" |
| 768 | #undef TP2 |
| 769 | #undef TP1 |
| 770 | |
| 771 | #define TP1 lng |
| 772 | #define TP2 sht |
| 773 | #include "sql_cast_impl_int.h" |
| 774 | #undef TP2 |
| 775 | #undef TP1 |
| 776 | |
| 777 | #ifdef HAVE_HGE |
| 778 | #define TP1 hge |
| 779 | #define TP2 sht |
| 780 | #include "sql_cast_impl_int.h" |
| 781 | #undef TP2 |
| 782 | #undef TP1 |
| 783 | #endif |
| 784 | |
| 785 | #define TP1 lng |
| 786 | #define TP2 int |
| 787 | #include "sql_cast_impl_int.h" |
| 788 | #undef TP2 |
| 789 | #undef TP1 |
| 790 | |
| 791 | #ifdef HAVE_HGE |
| 792 | #define TP1 hge |
| 793 | #define TP2 int |
| 794 | #include "sql_cast_impl_int.h" |
| 795 | #undef TP2 |
| 796 | #undef TP1 |
| 797 | #endif |
| 798 | |
| 799 | #ifdef HAVE_HGE |
| 800 | #define TP1 hge |
| 801 | #define TP2 lng |
| 802 | #include "sql_cast_impl_int.h" |
| 803 | #undef TP2 |
| 804 | #undef TP1 |
| 805 | #endif |
| 806 | |
| 807 | |