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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use arrow::array::Array as ArrowArray;
use llkv_runtime::{RuntimeContext, RuntimeStatementResult};
use llkv_sql::SqlEngine;
use llkv_storage::pager::MemPager;
use sqllogictest::{AsyncDB, DBOutput, DefaultColumnType};
/// Format a struct value in DuckDB-compatible format: {'field1': value1, 'field2': value2}
fn format_struct_value(struct_array: &arrow::array::StructArray, row_idx: usize) -> String {
let mut parts = Vec::new();
let field_names = struct_array.column_names();
for (field_idx, column) in struct_array.columns().iter().enumerate() {
let field_name = field_names[field_idx];
let value_str = match column.data_type() {
arrow::datatypes::DataType::Int64 => {
let a = column
.as_any()
.downcast_ref::<arrow::array::Int64Array>()
.unwrap();
if a.is_null(row_idx) {
"NULL".to_string()
} else {
a.value(row_idx).to_string()
}
}
arrow::datatypes::DataType::Int32 => {
let a = column
.as_any()
.downcast_ref::<arrow::array::Int32Array>()
.unwrap();
if a.is_null(row_idx) {
"NULL".to_string()
} else {
a.value(row_idx).to_string()
}
}
arrow::datatypes::DataType::Utf8 => {
let a = column
.as_any()
.downcast_ref::<arrow::array::StringArray>()
.unwrap();
if a.is_null(row_idx) {
"NULL".to_string()
} else {
format!("'{}'", a.value(row_idx))
}
}
arrow::datatypes::DataType::Float64 => {
let a = column
.as_any()
.downcast_ref::<arrow::array::Float64Array>()
.unwrap();
if a.is_null(row_idx) {
"NULL".to_string()
} else {
a.value(row_idx).to_string()
}
}
arrow::datatypes::DataType::Boolean => {
let a = column
.as_any()
.downcast_ref::<arrow::array::BooleanArray>()
.unwrap();
if a.is_null(row_idx) {
"NULL".to_string()
} else if a.value(row_idx) {
"1".to_string()
} else {
"0".to_string()
}
}
arrow::datatypes::DataType::Struct(_) => {
// Recursively format nested struct
let a = column
.as_any()
.downcast_ref::<arrow::array::StructArray>()
.unwrap();
if a.is_null(row_idx) {
"NULL".to_string()
} else {
format_struct_value(a, row_idx)
}
}
_ => "NULL".to_string(),
};
parts.push(format!("'{}': {}", field_name, value_str));
}
format!("{{{}}}", parts.join(", "))
}
pub struct EngineHarness {
engine: SqlEngine<MemPager>,
}
impl EngineHarness {
pub fn new(engine: SqlEngine<MemPager>) -> Self {
let harness = Self { engine };
tracing::debug!("[HARNESS] new() created harness at {:p}", &harness);
harness
}
}
#[derive(Clone)]
pub struct SharedContext {
context: Arc<RuntimeContext<MemPager>>,
}
impl Default for SharedContext {
fn default() -> Self {
Self::new()
}
}
impl SharedContext {
pub fn new() -> Self {
let pager = Arc::new(MemPager::default());
let context = Arc::new(RuntimeContext::new(pager));
Self { context }
}
pub fn make_engine(&self) -> SqlEngine<MemPager> {
SqlEngine::with_context(Arc::clone(&self.context), false)
}
}
#[async_trait::async_trait]
impl AsyncDB for EngineHarness {
type Error = llkv_result::Error;
type ColumnType = DefaultColumnType;
async fn run(&mut self, sql: &str) -> Result<DBOutput<Self::ColumnType>, Self::Error> {
// Log which SQL is being executed by this harness
tracing::debug!("[HARNESS {:p}] run() called, sql=\"{}\"", self, sql.trim());
match self.engine.execute(sql) {
Ok(mut results) => {
tracing::trace!(
"[HARNESS] execute() returned Ok with {} results",
results.len()
);
if results.is_empty() {
return Ok(DBOutput::StatementComplete(0));
}
let result = results.remove(0);
match result {
RuntimeStatementResult::Select { execution, .. } => {
let batches = execution.collect()?;
let mut rows: Vec<Vec<String>> = Vec::new();
for batch in &batches {
for row_idx in 0..batch.num_rows() {
let mut row: Vec<String> = Vec::new();
for col in 0..batch.num_columns() {
let array = batch.column(col);
let val = match array.data_type() {
arrow::datatypes::DataType::Int64 => {
let a = array
.as_any()
.downcast_ref::<arrow::array::Int64Array>()
.unwrap();
if a.is_null(row_idx) {
"NULL".to_string()
} else {
a.value(row_idx).to_string()
}
}
arrow::datatypes::DataType::UInt64 => {
let a = array
.as_any()
.downcast_ref::<arrow::array::UInt64Array>()
.unwrap();
if a.is_null(row_idx) {
"NULL".to_string()
} else {
a.value(row_idx).to_string()
}
}
arrow::datatypes::DataType::Float64 => {
let a = array
.as_any()
.downcast_ref::<arrow::array::Float64Array>()
.unwrap();
if a.is_null(row_idx) {
"NULL".to_string()
} else {
a.value(row_idx).to_string()
}
}
arrow::datatypes::DataType::Utf8 => {
let a = array
.as_any()
.downcast_ref::<arrow::array::StringArray>()
.unwrap();
if a.is_null(row_idx) {
"NULL".to_string()
} else {
a.value(row_idx).to_string()
}
}
arrow::datatypes::DataType::Boolean => {
let a = array
.as_any()
.downcast_ref::<arrow::array::BooleanArray>()
.unwrap();
if a.is_null(row_idx) {
"NULL".to_string()
} else if a.value(row_idx) {
"1".to_string()
} else {
"0".to_string()
}
}
arrow::datatypes::DataType::Struct(_) => {
let a = array
.as_any()
.downcast_ref::<arrow::array::StructArray>()
.unwrap();
if a.is_null(row_idx) {
"NULL".to_string()
} else {
format_struct_value(a, row_idx)
}
}
_ => "".to_string(),
};
row.push(val);
}
rows.push(row);
}
}
let types = if let Some(first) = batches.first() {
(0..first.num_columns())
.map(|col| match first.column(col).data_type() {
arrow::datatypes::DataType::Int64
| arrow::datatypes::DataType::UInt64 => {
DefaultColumnType::Integer
}
arrow::datatypes::DataType::Float64 => {
DefaultColumnType::FloatingPoint
}
arrow::datatypes::DataType::Utf8 => DefaultColumnType::Text,
_ => DefaultColumnType::Any,
})
.collect()
} else {
vec![]
};
Ok(DBOutput::Rows { types, rows })
}
RuntimeStatementResult::Insert { rows_inserted, .. } => {
// Return as a single-row result for compatibility with query directives
Ok(DBOutput::Rows {
types: vec![DefaultColumnType::Integer],
rows: vec![vec![rows_inserted.to_string()]],
})
}
RuntimeStatementResult::Update { rows_updated, .. } => {
// Return as a single-row result for compatibility with query directives
Ok(DBOutput::Rows {
types: vec![DefaultColumnType::Integer],
rows: vec![vec![rows_updated.to_string()]],
})
}
RuntimeStatementResult::Delete { rows_deleted, .. } => {
// Return as a single-row result for compatibility with query directives
Ok(DBOutput::Rows {
types: vec![DefaultColumnType::Integer],
rows: vec![vec![rows_deleted.to_string()]],
})
}
RuntimeStatementResult::CreateTable { .. } => {
Ok(DBOutput::StatementComplete(0))
}
RuntimeStatementResult::CreateIndex { .. } => {
Ok(DBOutput::StatementComplete(0))
}
RuntimeStatementResult::Transaction { .. } => {
Ok(DBOutput::StatementComplete(0))
}
RuntimeStatementResult::NoOp => Ok(DBOutput::StatementComplete(0)),
}
}
Err(e) => {
tracing::trace!("[HARNESS] execute() returned Err: {:?}", e);
Err(e)
}
}
}
async fn shutdown(&mut self) {}
}
pub type HarnessFuture = Pin<Box<dyn Future<Output = Result<EngineHarness, ()>> + Send + 'static>>;
pub type HarnessFactory = Box<dyn Fn() -> HarnessFuture + Send + Sync + 'static>;
pub fn make_factory_factory() -> impl Fn() -> HarnessFactory + Clone {
|| {
tracing::trace!("[FACTORY] make_factory_factory: Creating SharedContext");
let shared = SharedContext::new();
let counter = Arc::new(AtomicUsize::new(0));
let factory: HarnessFactory = Box::new(move || {
let n = counter.fetch_add(1, Ordering::SeqCst);
tracing::debug!(
"[FACTORY] Factory called #{}: Creating new EngineHarness",
n
);
let shared_clone = shared.clone();
Box::pin(async move {
let engine = shared_clone.make_engine();
tracing::debug!(
"[FACTORY] Factory #{}: Created SqlEngine with new Session",
n
);
Ok::<_, ()>(EngineHarness::new(engine))
})
});
factory
}
}