1/*
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 *
6 * Copyright 1997 - July 2008 CWI, August 2008 - 2019 MonetDB B.V.
7 */
8
9/*
10 * (author) M Kersten, N Nes
11 * SQL support implementation
12 * This module contains the wrappers around the SQL
13 * multi-version-catalog and support routines.
14 */
15#ifndef _SQL_H
16#define _SQL_H
17
18#include "sql_mem.h"
19
20#ifdef WIN32
21#ifndef LIBSQL
22#define sql5_export extern __declspec(dllimport)
23#else
24#define sql5_export extern __declspec(dllexport)
25#endif
26#else
27#define sql5_export extern
28#endif
29
30#include "mal_backend.h"
31#include "sql_mvc.h"
32#include "sql_backend.h"
33#include "mal_session.h"
34
35#include "mal_function.h"
36#include "mal_stack.h"
37#include "mal_interpreter.h"
38
39#include "tablet.h"
40#include "streams.h"
41#include "mtime.h"
42#include <math.h>
43#include "blob.h"
44#include "mkey.h"
45#include "str.h"
46#include "sql_privileges.h"
47#include "sql_decimal.h"
48#include "sql_string.h"
49#include "sql_qc.h"
50#include "sql_env.h"
51#include "sql_statement.h"
52#include "querylog.h"
53
54#include "bat/bat_storage.h"
55#include "bat/bat_utils.h"
56
57extern int sqlcleanup(mvc *c, int err);
58extern sql_rel *sql_symbol2relation(mvc *c, symbol *sym);
59
60extern BAT *mvc_bind(mvc *m, const char *sname, const char *tname, const char *cname, int access);
61extern BAT *mvc_bind_idxbat(mvc *m, const char *sname, const char *tname, const char *iname, int access);
62
63sql5_export str SQLmvc(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
64sql5_export str SQLcommit(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
65sql5_export str SQLabort(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
66sql5_export str SQLshutdown_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
67sql5_export str SQLtransaction2(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
68sql5_export str SQLcatalog(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
69
70sql5_export str mvc_grow_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
71sql5_export str mvc_append_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
72sql5_export str mvc_append_column(sql_trans *t, sql_column *c, BAT *ins);
73
74sql5_export str mvc_update_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
75sql5_export str mvc_bind_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
76sql5_export str mvc_delta_values(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
77sql5_export str mvc_bind_idxbat_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
78sql5_export str mvc_clear_table_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
79sql5_export str mvc_delete_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
80sql5_export str SQLtid(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
81sql5_export str DELTAbat(bat *result, const bat *col, const bat *uid, const bat *uval, const bat *ins);
82sql5_export str DELTAsub(bat *result, const bat *col, const bat *cid, const bat *uid, const bat *uval, const bat *ins);
83sql5_export str DELTAproject(bat *result, const bat *select, const bat *col, const bat *uid, const bat *uval, const bat *ins);
84sql5_export str DELTAbat2(bat *result, const bat *col, const bat *uid, const bat *uval);
85sql5_export str DELTAsub2(bat *result, const bat *col, const bat *cid, const bat *uid, const bat *uval);
86sql5_export str DELTAproject2(bat *result, const bat *select, const bat *col, const bat *uid, const bat *uval);
87
88sql5_export str BATleftproject(bat *result, const bat *col, const bat *l, const bat *r);
89
90sql5_export str mvc_table_result_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
91
92sql5_export str mvc_export_table_wrap( Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
93
94sql5_export str mvc_affected_rows_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
95sql5_export str mvc_export_result_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
96sql5_export str mvc_export_head_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
97sql5_export str mvc_export_chunk_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
98sql5_export str mvc_export_operation_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
99sql5_export str mvc_scalar_value_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
100sql5_export str mvc_row_result_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
101sql5_export str mvc_export_row_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
102sql5_export str mvc_import_table_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
103sql5_export str mvc_bin_import_table_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
104sql5_export str setVariable(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
105sql5_export str getVariable(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
106sql5_export str sql_variables(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
107sql5_export str mvc_logfile(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
108sql5_export str mvc_next_value(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
109sql5_export str mvc_bat_next_value(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
110sql5_export str mvc_get_value(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
111sql5_export str mvc_bat_get_value(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
112sql5_export str mvc_getVersion(lng *r, const int *clientid);
113sql5_export str mvc_restart_seq(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
114sql5_export str mvc_bat_restart_seq(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
115sql5_export str not_unique(bit *ret, const bat *bid);
116sql5_export str SQLshrink(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
117sql5_export str SQLreuse(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
118sql5_export str SQLvacuum(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
119sql5_export str SQLdrop_hash(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
120sql5_export str SQLargRecord(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
121sql5_export str SQLoptimizersUpdate(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
122sql5_export str month_interval_str(int *ret, const str *s, const int *ek, const int *sk);
123sql5_export str second_interval_str(lng *res, const str *s, const int *ek, const int *sk);
124sql5_export str dump_cache(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
125sql5_export str dump_opt_stats(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
126sql5_export str dump_trace(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
127sql5_export str sql_sessions_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
128sql5_export str sql_rt_credentials_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
129sql5_export str sql_storage(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
130sql5_export str sql_querylog_catalog(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
131sql5_export str sql_querylog_calls(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
132sql5_export str sql_querylog_empty(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
133sql5_export str sql_rowid(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
134sql5_export str sql_rank_grp(bat *rid, const bat *bid, const bat *gid, const bat *gpe);
135sql5_export str sql_rank(bat *rid, const bat *bid);
136sql5_export str sql_dense_rank_grp(bat *rid, const bat *bid, const bat *gid, const bat *gpe);
137sql5_export str sql_dense_rank(bat *rid, const bat *bid);
138sql5_export str SQLidentity(oid *rid, const void *i);
139sql5_export str BATSQLidentity(bat *rid, const bat *bid);
140sql5_export str PBATSQLidentity(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
141sql5_export str create_table_or_view(mvc *sql, char* sname, char *tname, sql_table *t, int temp);
142sql5_export str create_table_from_emit(Client cntxt, char *sname, char *tname, sql_emit_col *columns, size_t ncols);
143sql5_export str append_to_table_from_emit(Client cntxt, char *sname, char *tname, sql_emit_col *columns, size_t ncols);
144
145sql5_export str bte_dec_round_wrap(bte *res, const bte *v, const bte *r);
146sql5_export str bte_bat_dec_round_wrap(bat *res, const bat *v, const bte *r);
147sql5_export str bte_round_wrap(bte *res, const bte *v, const int *d, const int *s, const bte *r);
148sql5_export str bte_bat_round_wrap(bat *res, const bat *v, const int *d, const int *s, const bte *r);
149sql5_export str str_2dec_bte(bte *res, const str *val, const int *d, const int *sc);
150sql5_export str str_2num_bte(bte *res, const str *v, const int *len);
151sql5_export str batstr_2dec_bte(bat *res, const bat *val, const int *d, const int *sc);
152sql5_export str batstr_2num_bte(bat *res, const bat *v, const int *len);
153sql5_export str bte_dec2second_interval(lng *res, const int *sc, const bte *dec, const int *ek, const int *sk);
154
155sql5_export str nil_2dec_bte(bte *res, const void *val, const int *d, const int *sc);
156sql5_export str nil_2num_bte(bte *res, const void *v, const int *len);
157sql5_export str batnil_2dec_bte(bat *res, const bat *val, const int *d, const int *sc);
158sql5_export str batnil_2num_bte(bat *res, const bat *v, const int *len);
159
160sql5_export str sht_dec_round_wrap(sht *res, const sht *v, const sht *r);
161sql5_export str sht_bat_dec_round_wrap(bat *res, const bat *v, const sht *r);
162sql5_export str sht_round_wrap(sht *res, const sht *v, const int *d, const int *s, const bte *r);
163sql5_export str sht_bat_round_wrap(bat *res, const bat *v, const int *d, const int *s, const bte *r);
164sql5_export str str_2dec_sht(sht *res, const str *val, const int *d, const int *sc);
165sql5_export str str_2num_sht(sht *res, const str *v, const int *len);
166sql5_export str batstr_2dec_sht(bat *res, const bat *val, const int *d, const int *sc);
167sql5_export str batstr_2num_sht(bat *res, const bat *v, const int *len);
168sql5_export str sht_dec2second_interval(lng *res, const int *sc, const sht *dec, const int *ek, const int *sk);
169
170sql5_export str nil_2dec_sht(sht *res, const void *val, const int *d, const int *sc);
171sql5_export str nil_2num_sht(sht *res, const void *v, const int *len);
172sql5_export str batnil_2dec_sht(bat *res, const bat *val, const int *d, const int *sc);
173sql5_export str batnil_2num_sht(bat *res, const bat *v, const int *len);
174
175sql5_export str int_dec_round_wrap(int *res, const int *v, const int *r);
176sql5_export str int_bat_dec_round_wrap(bat *res, const bat *v, const int *r);
177sql5_export str int_round_wrap(int *res, const int *v, const int *d, const int *s, const bte *r);
178sql5_export str int_bat_round_wrap(bat *res, const bat *v, const int *d, const int *s, const bte *r);
179sql5_export str str_2dec_int(int *res, const str *val, const int *d, const int *sc);
180sql5_export str str_2num_int(int *res, const str *v, const int *len);
181sql5_export str batstr_2dec_int(bat *res, const bat *val, const int *d, const int *sc);
182sql5_export str batstr_2num_int(bat *res, const bat *v, const int *len);
183sql5_export str int_dec2second_interval(lng *res, const int *sc, const int *dec, const int *ek, const int *sk);
184
185sql5_export str nil_2dec_int(int *res, const void *val, const int *d, const int *sc);
186sql5_export str nil_2num_int(int *res, const void *v, const int *len);
187sql5_export str batnil_2dec_int(bat *res, const bat *val, const int *d, const int *sc);
188sql5_export str batnil_2num_int(bat *res, const bat *v, const int *len);
189
190sql5_export str lng_dec_round_wrap(lng *res, const lng *v, const lng *r);
191sql5_export str lng_bat_dec_round_wrap(bat *res, const bat *v, const lng *r);
192sql5_export str lng_round_wrap(lng *res, const lng *v, const int *d, const int *s, const bte *r);
193sql5_export str lng_bat_round_wrap(bat *res, const bat *v, const int *d, const int *s, const bte *r);
194sql5_export str str_2dec_lng(lng *res, const str *val, const int *d, const int *sc);
195sql5_export str str_2num_lng(lng *res, const str *v, const int *len);
196sql5_export str batstr_2dec_lng(bat *res, const bat *val, const int *d, const int *sc);
197sql5_export str batstr_2num_lng(bat *res, const bat *v, const int *len);
198sql5_export str lng_dec2second_interval(lng *res, const int *sc, const lng *dec, const int *ek, const int *sk);
199
200sql5_export str nil_2dec_lng(lng *res, const void *val, const int *d, const int *sc);
201sql5_export str nil_2num_lng(lng *res, const void *v, const int *len);
202sql5_export str batnil_2dec_lng(bat *res, const bat *val, const int *d, const int *sc);
203sql5_export str batnil_2num_lng(bat *res, const bat *v, const int *len);
204
205#ifdef HAVE_HGE
206sql5_export str hge_dec_round_wrap(hge *res, const hge *v, const hge *r);
207sql5_export str hge_bat_dec_round_wrap(bat *res, const bat *v, const hge *r);
208sql5_export str hge_round_wrap(hge *res, const hge *v, const int *d, const int *s, const bte *r);
209sql5_export str hge_bat_round_wrap(bat *res, const bat *v, const int *d, const int *s, const bte *r);
210sql5_export str str_2dec_hge(hge *res, const str *val, const int *d, const int *sc);
211sql5_export str str_2num_hge(hge *res, const str *v, const int *len);
212sql5_export str batstr_2dec_hge(bat *res, const bat *val, const int *d, const int *sc);
213sql5_export str batstr_2num_hge(bat *res, const bat *v, const int *len);
214sql5_export str hge_dec2second_interval(lng *res, const int *sc, const hge *dec, const int *ek, const int *sk);
215
216sql5_export str nil_2dec_hge(hge *res, const void *val, const int *d, const int *sc);
217sql5_export str nil_2num_hge(hge *res, const void *v, const int *len);
218sql5_export str batnil_2dec_hge(bat *res, const bat *val, const int *d, const int *sc);
219sql5_export str batnil_2num_hge(bat *res, const bat *v, const int *len);
220#endif
221
222sql5_export str nil_2time_timestamp(timestamp *res, const void *v, const int *len);
223sql5_export str batnil_2time_timestamp(bat *res, const bat *v, const int *len);
224sql5_export str str_2time_timestamp(timestamp *res, const str *v, const int *len);
225sql5_export str str_2time_timestamptz(timestamp *res, const str *v, const int *len, int *tz);
226sql5_export str batstr_2time_timestamp(bat *res, const bat *v, const int *len);
227sql5_export str batstr_2time_timestamptz(bat *res, const bat *v, const int *len, int *tz);
228sql5_export str timestamp_2time_timestamp(timestamp *res, const timestamp *v, const int *len);
229sql5_export str battimestamp_2time_timestamp(bat *res, const bat *v, const int *len);
230
231sql5_export str nil_2time_daytime(daytime *res, const void *v, const int *len);
232sql5_export str batnil_2time_daytime(bat *res, const bat *v, const int *len);
233sql5_export str str_2time_daytime(daytime *res, const str *v, const int *len);
234sql5_export str str_2time_daytimetz(daytime *res, const str *v, const int *len, int *tz);
235sql5_export str batstr_2time_daytime(bat *res, const bat *v, const int *len);
236sql5_export str batstr_2time_daytimetz(bat *res, const bat *v, const int *len, int *tz);
237sql5_export str daytime_2time_daytime(daytime *res, const daytime *v, const int *len);
238sql5_export str batdaytime_2time_daytime(bat *res, const bat *v, const int *len);
239
240sql5_export str nil_2_timestamp(timestamp *res, const void *val);
241sql5_export str batnil_2_timestamp(bat *res, const bat *val);
242sql5_export str str_2_timestamp(timestamp *res, const str *val);
243sql5_export str batstr_2_timestamp(bat *res, const bat *val);
244
245sql5_export str bat_date_trunc(bat *res, const str *scale, const bat *v);
246sql5_export str date_trunc(timestamp *res, const str *scale, const timestamp *v);
247
248sql5_export str nil_2_daytime(daytime *res, const void *val);
249sql5_export str batnil_2_daytime(bat *res, const bat *val);
250sql5_export str str_2_daytime(daytime *res, const str *val);
251sql5_export str batstr_2_daytime(bat *res, const bat *val);
252
253sql5_export str nil_2_date(date *res, const void *val);
254sql5_export str batnil_2_date(bat *res, const bat *val);
255sql5_export str str_2_date(date *res, const str *val);
256sql5_export str batstr_2_date(bat *res, const bat *val);
257sql5_export str SQLdate_2_str(str *res, const date *val);
258
259sql5_export str str_2_blob(blob * *res, const str *val);
260sql5_export str batstr_2_blob(bat *res, const bat *val);
261sql5_export str SQLblob_2_str(str *res, const blob * val);
262
263
264sql5_export str SQLstr_cast(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
265sql5_export str SQLbatstr_cast(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
266
267sql5_export str flt_dec_round_wrap(flt *res, const flt *v, const flt *r);
268sql5_export str flt_bat_dec_round_wrap(bat *res, const bat *v, const flt *r);
269sql5_export str flt_round_wrap(flt *res, const flt *v, const bte *r);
270sql5_export str flt_bat_round_wrap(bat *res, const bat *v, const bte *r);
271sql5_export str flt_trunc_wrap(flt *res, const flt *v, const int *r);
272
273sql5_export str dbl_dec_round_wrap(dbl *res, const dbl *v, const dbl *r);
274sql5_export str dbl_bat_dec_round_wrap(bat *res, const bat *v, const dbl *r);
275sql5_export str dbl_round_wrap(dbl *res, const dbl *v, const bte *r);
276sql5_export str dbl_bat_round_wrap(bat *res, const bat *v, const bte *r);
277sql5_export str dbl_trunc_wrap(dbl *res, const dbl *v, const int *r);
278
279#define radians(x) ((x) * 3.14159265358979323846 /180.0 )
280#define degrees(x) ((x) * 180.0/3.14159265358979323846 )
281
282sql5_export str SQLcst_alpha_cst(dbl *res, const dbl *decl, const dbl *theta);
283sql5_export str SQLbat_alpha_cst(bat *res, const bat *decl, const dbl *theta);
284sql5_export str SQLcst_alpha_bat(bat *res, const dbl *decl, const bat *theta);
285sql5_export str month_interval(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
286sql5_export str second_interval(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
287sql5_export str second_interval_daytime(lng *res, const daytime *s, const int *ek, const int *sk);
288
289#include "sql_cast.h"
290
291sql5_export str checkSQLContext(Client cntxt);
292sql5_export str getSQLContext(Client cntxt, MalBlkPtr mb, mvc **c, backend **b);
293
294sql5_export void freeVariables(Client c, MalBlkPtr mb, MalStkPtr glb, int start);
295sql5_export str second_interval_2_daytime(daytime *res, const lng *s, const int *d);
296sql5_export str timestamp_2_daytime(daytime *res, const timestamp *v, const int *d);
297sql5_export str date_2_timestamp(timestamp *res, const date *v, const int *d);
298sql5_export str SQLcurrent_daytime(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
299sql5_export str SQLcurrent_timestamp(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
300
301sql5_export str STRindex_int(int *res, const str *src, const bit *u);
302sql5_export str BATSTRindex_int(bat *res, const bat *src, const bit *u);
303sql5_export str STRindex_sht(sht *res, const str *src, const bit *u);
304sql5_export str BATSTRindex_sht(bat *res, const bat *src, const bit *u);
305sql5_export str STRindex_bte(bte *res, const str *src, const bit *u);
306sql5_export str BATSTRindex_bte(bat *res, const bat *src, const bit *u);
307sql5_export str STRstrings(str *res, const str *src);
308sql5_export str BATSTRstrings(bat *res, const bat *src);
309
310sql5_export str SQLflush_log(void *ret);
311sql5_export str SQLsuspend_log_flushing(void *ret);
312sql5_export str SQLresume_log_flushing(void *ret);
313sql5_export str SQLhot_snapshot(void *ret, const str *tarfile);
314
315#endif /* _SQL_H */
316