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
use indexmap::IndexSet;
use toasty_core::stmt;
use crate::engine::{
exec,
mir::{self, LogicalPlan},
};
/// Executes a SQL statement against the database.
///
/// Used with SQL-capable drivers to delegate query execution to the database's
/// query engine. The statement may reference inputs from other nodes.
#[derive(Debug)]
pub(crate) struct ExecStatement {
/// Nodes whose outputs are passed as arguments to the statement.
pub(crate) inputs: IndexSet<mir::NodeId>,
/// The SQL statement to execute.
pub(crate) stmt: stmt::Statement,
/// The return type of this operation.
pub(crate) ty: stmt::Type,
/// How this statement's output is interpreted. For a conditional write the
/// statement's leading two columns are probe counts checked against each
/// other; see [`exec::ConditionalOutput`].
pub(crate) conditional: exec::ConditionalOutput,
/// Pagination configuration (None if not paginated)
pub(crate) pagination: Option<exec::PaginationConfig>,
}
impl ExecStatement {
pub(crate) fn to_exec(
&self,
logical_plan: &LogicalPlan,
node: &mir::Node,
var_table: &mut exec::VarDecls,
) -> exec::ExecStatement {
debug_assert!(
{
match &self.stmt {
stmt::Statement::Query(query) => !query.single,
_ => true,
}
},
"as of now, no database can execute single queries"
);
let input_vars = self
.inputs
.iter()
.map(|input| logical_plan[input].var.get().unwrap())
.collect();
let var = var_table.register_var(self.ty.clone());
node.var.set(Some(var));
let output_ty = mir::row_field_types(&self.ty);
exec::ExecStatement {
input: input_vars,
output: exec::ExecStatementOutput {
ty: output_ty,
output: exec::Output {
var,
num_uses: node.num_uses.get(),
},
},
stmt: self.stmt.clone(),
conditional: self.conditional,
pagination: self.pagination.clone(),
}
}
}
impl From<ExecStatement> for mir::Node {
fn from(value: ExecStatement) -> Self {
mir::Operation::ExecStatement(Box::new(value)).into()
}
}