| 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_gencode.h" |
| 13 | #include "sql_storage.h" |
| 14 | #include "sql_scenario.h" |
| 15 | #include "store_sequence.h" |
| 16 | #include "sql_optimizer.h" |
| 17 | #include "sql_datetime.h" |
| 18 | #include "rel_unnest.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 "clients.h" |
| 25 | #include "mal_instruction.h" |
| 26 | |
| 27 | str |
| 28 | batstr_2time_timestamptz(bat *res, const bat *bid, const int *digits, int *tz) |
| 29 | { |
| 30 | BAT *b, *dst; |
| 31 | BATiter bi; |
| 32 | BUN p, q; |
| 33 | char *msg = NULL; |
| 34 | |
| 35 | if ((b = BATdescriptor(*bid)) == NULL) { |
| 36 | throw(SQL, "batcalc.str_2time_timestamp" , SQLSTATE(HY005) "Cannot access column descriptor" ); |
| 37 | } |
| 38 | bi = bat_iterator(b); |
| 39 | dst = COLnew(b->hseqbase, TYPE_timestamp, BATcount(b), TRANSIENT); |
| 40 | if (dst == NULL) { |
| 41 | BBPunfix(b->batCacheid); |
| 42 | throw(SQL, "sql.timestamp" , SQLSTATE(HY001) MAL_MALLOC_FAIL); |
| 43 | } |
| 44 | BATloop(b, p, q) { |
| 45 | char *v = (char *) BUNtvar(bi, p); |
| 46 | timestamp r; |
| 47 | msg = str_2time_timestamptz(&r, &v, digits, tz); |
| 48 | if (msg) { |
| 49 | BBPunfix(dst->batCacheid); |
| 50 | BBPunfix(b->batCacheid); |
| 51 | return msg; |
| 52 | } |
| 53 | if (BUNappend(dst, &r, false) != GDK_SUCCEED) { |
| 54 | BBPunfix(b->batCacheid); |
| 55 | BBPreclaim(dst); |
| 56 | throw(SQL, "sql.timestamp" , SQLSTATE(HY001) MAL_MALLOC_FAIL); |
| 57 | } |
| 58 | } |
| 59 | BBPkeepref(*res = dst->batCacheid); |
| 60 | BBPunfix(b->batCacheid); |
| 61 | return msg; |
| 62 | } |
| 63 | |
| 64 | str |
| 65 | batstr_2time_timestamp(bat *res, const bat *bid, const int *digits) |
| 66 | { |
| 67 | int zero = 0; |
| 68 | return batstr_2time_timestamptz( res, bid, digits, &zero); |
| 69 | } |
| 70 | |
| 71 | str |
| 72 | battimestamp_2time_timestamp(bat *res, const bat *bid, const int *digits) |
| 73 | { |
| 74 | BAT *b, *dst; |
| 75 | BUN p, q; |
| 76 | char *msg = NULL; |
| 77 | |
| 78 | if ((b = BATdescriptor(*bid)) == NULL) { |
| 79 | throw(SQL, "batcalc.timestamp_2time_timestamp" , SQLSTATE(HY005) "Cannot access column descriptor" ); |
| 80 | } |
| 81 | dst = COLnew(b->hseqbase, TYPE_timestamp, BATcount(b), TRANSIENT); |
| 82 | if (dst == NULL) { |
| 83 | BBPunfix(b->batCacheid); |
| 84 | throw(SQL, "sql.timestamp" , SQLSTATE(HY001) MAL_MALLOC_FAIL); |
| 85 | } |
| 86 | const timestamp *v = (const timestamp *) Tloc(b, 0); |
| 87 | BATloop(b, p, q) { |
| 88 | timestamp r; |
| 89 | msg = timestamp_2time_timestamp(&r, v, digits); |
| 90 | if (msg) { |
| 91 | BBPunfix(dst->batCacheid); |
| 92 | BBPunfix(b->batCacheid); |
| 93 | return msg; |
| 94 | } |
| 95 | if (BUNappend(dst, &r, false) != GDK_SUCCEED) { |
| 96 | BBPunfix(b->batCacheid); |
| 97 | BBPreclaim(dst); |
| 98 | throw(SQL, "sql.timestamp" , SQLSTATE(HY001) MAL_MALLOC_FAIL); |
| 99 | } |
| 100 | v++; |
| 101 | } |
| 102 | BBPkeepref(*res = dst->batCacheid); |
| 103 | BBPunfix(b->batCacheid); |
| 104 | return msg; |
| 105 | } |
| 106 | |
| 107 | str |
| 108 | batnil_2time_timestamp(bat *res, const bat *bid, const int *digits) |
| 109 | { |
| 110 | BAT *b, *dst; |
| 111 | BUN p, q; |
| 112 | char *msg = NULL; |
| 113 | |
| 114 | if ((b = BATdescriptor(*bid)) == NULL) { |
| 115 | throw(SQL, "batcalc.nil_2time_timestamp" , SQLSTATE(HY005) "Cannot access column descriptor" ); |
| 116 | } |
| 117 | dst = COLnew(b->hseqbase, TYPE_timestamp, BATcount(b), TRANSIENT); |
| 118 | if (dst == NULL) { |
| 119 | BBPunfix(b->batCacheid); |
| 120 | throw(SQL, "sql.timestamp" , SQLSTATE(HY001) MAL_MALLOC_FAIL); |
| 121 | } |
| 122 | BATloop(b, p, q) { |
| 123 | timestamp r; |
| 124 | msg = nil_2time_timestamp(&r, NULL, digits); |
| 125 | if (msg) { |
| 126 | BBPunfix(dst->batCacheid); |
| 127 | BBPunfix(b->batCacheid); |
| 128 | return msg; |
| 129 | } |
| 130 | if (BUNappend(dst, &r, false) != GDK_SUCCEED) { |
| 131 | BBPunfix(b->batCacheid); |
| 132 | BBPreclaim(dst); |
| 133 | throw(SQL, "sql.timestamp" , SQLSTATE(HY001) MAL_MALLOC_FAIL); |
| 134 | } |
| 135 | } |
| 136 | BBPkeepref(*res = dst->batCacheid); |
| 137 | BBPunfix(b->batCacheid); |
| 138 | return msg; |
| 139 | } |
| 140 | |
| 141 | str |
| 142 | batstr_2time_daytimetz(bat *res, const bat *bid, const int *digits, int *tz) |
| 143 | { |
| 144 | BAT *b, *dst; |
| 145 | BATiter bi; |
| 146 | BUN p, q; |
| 147 | char *msg = NULL; |
| 148 | |
| 149 | if ((b = BATdescriptor(*bid)) == NULL) { |
| 150 | throw(SQL, "batcalc.str_2time_daytime" , SQLSTATE(HY005) "Cannot access column descriptor" ); |
| 151 | } |
| 152 | bi = bat_iterator(b); |
| 153 | dst = COLnew(b->hseqbase, TYPE_daytime, BATcount(b), TRANSIENT); |
| 154 | if (dst == NULL) { |
| 155 | BBPunfix(b->batCacheid); |
| 156 | throw(SQL, "sql.daytime" , SQLSTATE(HY001) MAL_MALLOC_FAIL); |
| 157 | } |
| 158 | BATloop(b, p, q) { |
| 159 | char *v = (char *) BUNtvar(bi, p); |
| 160 | daytime r; |
| 161 | msg = str_2time_daytimetz(&r, &v, digits, tz); |
| 162 | if (msg) { |
| 163 | BBPunfix(dst->batCacheid); |
| 164 | BBPunfix(b->batCacheid); |
| 165 | return msg; |
| 166 | } |
| 167 | if (BUNappend(dst, &r, false) != GDK_SUCCEED) { |
| 168 | BBPunfix(b->batCacheid); |
| 169 | BBPreclaim(dst); |
| 170 | throw(SQL, "sql.daytime" , SQLSTATE(HY001) MAL_MALLOC_FAIL); |
| 171 | } |
| 172 | } |
| 173 | BBPkeepref(*res = dst->batCacheid); |
| 174 | BBPunfix(b->batCacheid); |
| 175 | return msg; |
| 176 | } |
| 177 | |
| 178 | str |
| 179 | batstr_2time_daytime(bat *res, const bat *bid, const int *digits) |
| 180 | { |
| 181 | int zero = 0; |
| 182 | return batstr_2time_daytimetz( res, bid, digits, &zero); |
| 183 | } |
| 184 | |
| 185 | str |
| 186 | batdaytime_2time_daytime(bat *res, const bat *bid, const int *digits) |
| 187 | { |
| 188 | BAT *b, *dst; |
| 189 | BUN p, q; |
| 190 | char *msg = NULL; |
| 191 | |
| 192 | if ((b = BATdescriptor(*bid)) == NULL) { |
| 193 | throw(SQL, "batcalc.daytime_2time_daytime" , SQLSTATE(HY005) "Cannot access column descriptor" ); |
| 194 | } |
| 195 | dst = COLnew(b->hseqbase, TYPE_daytime, BATcount(b), TRANSIENT); |
| 196 | if (dst == NULL) { |
| 197 | BBPunfix(b->batCacheid); |
| 198 | throw(SQL, "sql.daytime" , SQLSTATE(HY001) MAL_MALLOC_FAIL); |
| 199 | } |
| 200 | const daytime *v = (const daytime *) Tloc(b, 0); |
| 201 | BATloop(b, p, q) { |
| 202 | daytime r; |
| 203 | msg = daytime_2time_daytime(&r, v, digits); |
| 204 | if (msg) { |
| 205 | BBPunfix(dst->batCacheid); |
| 206 | BBPunfix(b->batCacheid); |
| 207 | return msg; |
| 208 | } |
| 209 | if (BUNappend(dst, &r, false) != GDK_SUCCEED) { |
| 210 | BBPunfix(b->batCacheid); |
| 211 | BBPreclaim(dst); |
| 212 | throw(SQL, "sql.daytime" , SQLSTATE(HY001) MAL_MALLOC_FAIL); |
| 213 | } |
| 214 | v++; |
| 215 | } |
| 216 | BBPkeepref(*res = dst->batCacheid); |
| 217 | BBPunfix(b->batCacheid); |
| 218 | return msg; |
| 219 | } |
| 220 | |
| 221 | str |
| 222 | batnil_2time_daytime(bat *res, const bat *bid, const int *digits) |
| 223 | { |
| 224 | BAT *b, *dst; |
| 225 | BUN p, q; |
| 226 | char *msg = NULL; |
| 227 | |
| 228 | if ((b = BATdescriptor(*bid)) == NULL) { |
| 229 | throw(SQL, "batcalc.nil_2time_daytime" , SQLSTATE(HY005) "Cannot access column descriptor" ); |
| 230 | } |
| 231 | dst = COLnew(b->hseqbase, TYPE_daytime, BATcount(b), TRANSIENT); |
| 232 | if (dst == NULL) { |
| 233 | BBPunfix(b->batCacheid); |
| 234 | throw(SQL, "sql.daytime" , SQLSTATE(HY001) MAL_MALLOC_FAIL); |
| 235 | } |
| 236 | BATloop(b, p, q) { |
| 237 | daytime r; |
| 238 | msg = nil_2time_daytime(&r, NULL, digits); |
| 239 | if (msg) { |
| 240 | BBPunfix(dst->batCacheid); |
| 241 | BBPunfix(b->batCacheid); |
| 242 | return msg; |
| 243 | } |
| 244 | if (BUNappend(dst, &r, false) != GDK_SUCCEED) { |
| 245 | BBPunfix(b->batCacheid); |
| 246 | BBPreclaim(dst); |
| 247 | throw(SQL, "sql.daytime" , SQLSTATE(HY001) MAL_MALLOC_FAIL); |
| 248 | } |
| 249 | } |
| 250 | BBPkeepref(*res = dst->batCacheid); |
| 251 | BBPunfix(b->batCacheid); |
| 252 | return msg; |
| 253 | } |
| 254 | |
| 255 | |