1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
use futures::future::{BoxFuture, FutureExt};
use std::collections::HashMap;
use crate::compile::schema;
use crate::compile::sql::{create_table_as, select_star_from};
use crate::types::LazyValue;
use crate::{
ast::Ident,
types,
types::{arrow::EMPTY_RELATION, Arc, Value},
};
use super::SQLParam;
use super::{context::Context, error::*, sql::LazySQLParam};
type TypeRef = schema::Ref<types::Type>;
// This is a type alias for simplicity and to make it easy potentially in the future to allow a
// library user to pass in their own runtime.
pub type Runtime = tokio::runtime::Runtime;
#[cfg(feature = "multi-thread")]
pub fn build() -> Result<Runtime> {
Ok(tokio::runtime::Builder::new_multi_thread()
.enable_io()
.enable_time()
.build()?)
}
#[cfg(not(feature = "multi-thread"))]
pub fn build() -> Result<Runtime> {
Ok(tokio::runtime::Builder::new_current_thread()
.enable_io()
.enable_time()
.build()?)
}
pub fn expensive<F, R>(f: F) -> R
where
F: FnOnce() -> R,
{
match tokio::runtime::Handle::try_current() {
#[cfg(feature = "multi-thread")]
Ok(handle) if handle.runtime_flavor() == tokio::runtime::RuntimeFlavor::MultiThread => {
tokio::task::block_in_place(f)
}
_ => f(),
}
}
pub fn eval<'a>(
ctx: &'a mut Context,
typed_expr: &'a schema::TypedExpr<TypeRef>,
) -> BoxFuture<'a, crate::runtime::Result<Value>> {
async move {
let mut lazy_value = eval_lazy(ctx, typed_expr).await?;
lazy_value.get().await
}
.boxed()
}
pub async fn eval_params_lazy<'a>(
ctx: &'a mut Context,
params: &'a schema::Params<TypeRef>,
) -> Result<HashMap<Ident, LazySQLParam>> {
let mut param_values = HashMap::new();
for (name, param) in params {
let value = eval_lazy(ctx, param).await?;
param_values.insert(
name.clone(),
LazySQLParam::new(name.clone(), value, &*param.type_.read()?),
);
}
Ok(param_values)
}
pub async fn resolve_params(
params: HashMap<Ident, LazySQLParam>,
) -> Result<HashMap<Ident, SQLParam>> {
let mut resolved_params = HashMap::new();
for (name, param) in params.into_iter() {
let value = param.get().await?;
resolved_params.insert(name, value);
}
Ok(resolved_params)
}
pub fn eval_lazy<'a>(
ctx: &'a mut Context,
typed_expr: &'a schema::TypedExpr<TypeRef>,
) -> BoxFuture<'a, crate::runtime::Result<Box<dyn LazyValue>>> {
async move {
Ok(match &*typed_expr.expr.as_ref() {
schema::Expr::Unknown => {
return Err(RuntimeError::new("unresolved extern"));
}
schema::Expr::UncompiledFn(def) => {
return Err(RuntimeError::new(
format!("uncompiled function: {:?}", def).as_str(),
));
}
schema::Expr::SchemaEntry(schema::STypedExpr { .. }) => {
return Err(RuntimeError::new("unresolved schema entry"));
}
schema::Expr::Connection(..) => {
return Err(RuntimeError::new("unresolved connection"));
}
schema::Expr::ContextRef(r) => match ctx.values.get(r) {
Some(v) => v.clone().into(), // Can we avoid this clone??
None => {
return Err(RuntimeError::new(
format!("No such context value {}", r).as_str(),
))
}
},
schema::Expr::Fn(f) => {
use super::functions::*;
let body = match &f.body {
schema::FnBody::Expr(e) => e.clone().into(),
_ => {
return fail!(
"Non-expression function body should have been optimized away"
)
}
};
QSFn::new(typed_expr.type_.clone(), body)?.into()
}
schema::Expr::NativeFn(name) => {
use super::functions::*;
match name.as_str() {
"load" => {
Value::Fn(Arc::new(LoadFileFn::new(&*typed_expr.type_.read()?)?)).into()
}
"__native_identity" => {
Value::Fn(Arc::new(IdentityFn::new(&*typed_expr.type_.read()?)?)).into()
}
_ => return rt_unimplemented!("native function: {}", name),
}
}
schema::Expr::Materialize(schema::MaterializeExpr {
key,
expr,
inlined,
decl_name,
url,
..
}) => {
if let (false, Some(value)) = (inlined, ctx.materializations.get(key)) {
return Ok(value.clone().into());
}
if *inlined {
let table_name = decl_name.into();
let engine = ctx.sql_engine(url.clone()).await?;
if !engine.table_exists(&table_name).await? {
match expr.expr.as_ref() {
schema::Expr::SQL(sql, _) => {
let query = create_table_as(table_name, sql.body.as_query()?, true);
let sql_params = eval_params_lazy(ctx, &sql.names.params).await?;
let _ = ctx
.sql_engine(url.clone())
.await?
.exec(&query, sql_params)
.await?;
}
schema::Expr::Materialize(schema::MaterializeExpr {
decl_name: inner_decl,
..
}) => {
eval(ctx, &expr).await?;
let query =
create_table_as(table_name, select_star_from(inner_decl), true);
let _ = ctx
.sql_engine(url.clone())
.await?
.exec(&query, HashMap::new())
.await?;
}
_ => {
let inner_type = expr.type_.read()?.clone();
let result = eval(ctx, &expr).await?;
let _ = ctx
.sql_engine(url.clone())
.await?
.load(&table_name, result, inner_type, true)
.await?;
}
}
}
// Don't stash the fact that we created the temporary table in the materialization index
// NOTE: For performance sake, we could cache something here (that we created the temp
// table), but for now we assume checking if the table exists is fast enough.
EMPTY_RELATION.clone().into()
} else {
let result = eval(ctx, expr).await?;
ctx.materializations
.entry(key.clone())
.or_insert(result)
.clone()
.into()
}
}
schema::Expr::FnCall(schema::FnCallExpr {
func,
args,
ctx_folder,
}) => {
let mut new_ctx = ctx.clone();
new_ctx.folder = ctx_folder.clone();
let mut arg_values = Vec::new();
for arg in args.iter() {
// Eval the arguments in the calling context. Functions do not currently accept
// lazy parameters, so evaluate them eagerly.
// TODO: Change the function interface to accept lazy parameters
//
arg_values.push(eval(ctx, arg).await?);
}
let fn_val = match eval(&mut new_ctx, func.as_ref()).await? {
Value::Fn(f) => f,
_ => return fail!("Cannot call non-function"),
};
fn_val.execute(&mut new_ctx, arg_values).await?.into()
}
schema::Expr::SQL(e, url) => {
let schema::SQL { body, names } = e.as_ref();
let sql_params = eval_params_lazy(ctx, &names.params).await?;
let query = body.as_statement()?;
let engine = ctx.sql_engine(url.clone()).await?;
// TODO: This ownership model implies some necessary copying (below).
let mut rows = engine.query(&query, sql_params).await?;
// Before returning, we perform some runtime checks that might only be necessary in debug mode:
// - For expressions, validate that the result is a single row and column
// - For expressions and queries, check that the RecordBatch's type matches the
// expected type from the compiler.
let expected_type = typed_expr.type_.read()?;
let value = match body {
schema::SQLBody::Expr(_) => {
if rows.num_batches() != 1 {
return fail!("Expected an expression to have exactly one row");
}
if rows.schema().len() != 1 {
return fail!("Expected an expression to have exactly one column");
}
let value_type = rows.batch(0).records()[0].column(0).type_();
if !ctx.disable_typechecks && *expected_type != value_type {
let target_schema = vec![crate::types::Field::new_nullable(
"value".into(),
expected_type.clone(),
)];
rows = rows.try_cast(&target_schema)?;
}
let row = &rows.batch(0).records()[0];
row.column(0).clone()
}
schema::SQLBody::Query(_) | schema::SQLBody::Table(_) => {
// Validate that the schema matches the expected type. If not, we have a serious problem
// since we may interpret the record batch as a different type than expected.
if !ctx.disable_typechecks && rows.num_batches() > 0 {
let rows_type = crate::types::Type::List(Box::new(
crate::types::Type::Record(rows.schema()),
));
if *expected_type != rows_type {
let target_schema = match &*expected_type {
crate::types::Type::List(t) => match &**t {
crate::types::Type::Record(s) => s,
_ => {
return Err(RuntimeError::type_mismatch(
expected_type.clone(),
rows_type,
));
}
},
_ => {
return Err(RuntimeError::type_mismatch(
expected_type.clone(),
rows_type,
));
}
};
rows = rows.try_cast(&target_schema)?;
}
}
Value::Relation(rows)
}
};
value.into()
}
})
}
.boxed()
}