qql-plan 0.4.2

Typed QQL execution plans and Qdrant route lowering
Documentation
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
//! Batch grouping, extraction, and validation for multi-statement execution.

use crate::plan::PlannedOperation;
use crate::types::{QueryBatchRequest, UpdateBatchRequest};
use alloc::string::{String, ToString};
use alloc::vec::Vec;
use qql_core::ast::{QueryCollection, QueryExpr, Stmt};
use qql_core::error::QqlError;

/// Batch grouping key identifying contiguous operations that can be combined
/// into a single Qdrant batch RPC.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BatchKey {
    /// Query batch for the named collection.
    Query(String),
    /// Mutation batch for the named collection.
    Mutation(String),
}

/// Batch grouping key for a raw AST statement (before preparation/planning).
///
/// Returns `None` for statements that are never batchable (DDL, SHOW, group
/// queries, point-ID lookups, etc.).
pub fn statement_batch_key(stmt: &Stmt) -> Option<BatchKey> {
    match stmt {
        Stmt::Query(query)
            if query.group.is_none()
                && !matches!(query.expression, QueryExpr::Points { .. })
                && !matches!(query.expression, QueryExpr::CrossRerank { .. }) =>
        {
            match &query.collection {
                QueryCollection::Explicit(collection) => Some(BatchKey::Query(collection.clone())),
                QueryCollection::Inherited => None,
            }
        }
        Stmt::Upsert(stmt) => Some(BatchKey::Mutation(stmt.collection.clone())),
        Stmt::Delete(stmt) => Some(BatchKey::Mutation(stmt.collection.clone())),
        Stmt::UpdatePayload(stmt) => Some(BatchKey::Mutation(stmt.collection.clone())),
        Stmt::ClearPayload(stmt) => Some(BatchKey::Mutation(stmt.collection.clone())),
        Stmt::DeletePayload(stmt) => Some(BatchKey::Mutation(stmt.collection.clone())),
        Stmt::UpdateVector(stmt) => Some(BatchKey::Mutation(stmt.collection.clone())),
        Stmt::DeleteVector(stmt) => Some(BatchKey::Mutation(stmt.collection.clone())),
        // Explicit BATCH blocks are already one unit: they never merge into
        // ambient groups (the executors intercept them before the grouper).
        Stmt::Batch(_) => None,
        _ => None,
    }
}

/// Typed view of one Qdrant batch response item.
///
/// Batch envelopes stay JSON at the REST boundary (per-item results have no
/// OpenAPI struct in this transport-neutral crate), so this borrows the
/// `status` / `error` strings instead of taking ownership.
#[derive(Debug, Clone, Copy)]
struct BatchItem<'item> {
    status: Option<&'item str>,
    error: Option<&'item str>,
}

impl<'item> BatchItem<'item> {
    fn parse(item: &'item serde_json::Value) -> Self {
        Self {
            status: item.get("status").and_then(serde_json::Value::as_str),
            error: item.get("error").and_then(serde_json::Value::as_str),
        }
    }

    fn error_message(self) -> Option<String> {
        if self.status == Some("error") {
            Some(self.error.unwrap_or("batch item failed").to_string())
        } else {
            None
        }
    }
}

/// Detect per-item errors in Qdrant batch endpoint responses.
///
/// Qdrant batch endpoints answer per item; a 200 response can still carry
/// per-item failures (`status: "error"`).
pub fn batch_item_error(item: &serde_json::Value) -> Option<String> {
    BatchItem::parse(item).error_message()
}

/// Verify that a batch response has the expected cardinality.
///
/// Returns `Ok(())` if `received == expected`, or a `QQL-BATCH-CARDINALITY` error otherwise.
pub fn verify_batch_cardinality(
    kind: &str,
    expected: usize,
    received: usize,
) -> Result<(), QqlError> {
    if expected == received {
        Ok(())
    } else {
        Err(QqlError::transport(
            "QQL-BATCH-CARDINALITY",
            alloc::format!("{kind} batch returned {received} results for {expected} operations"),
            None,
        ))
    }
}

/// Build a `QueryBatchRequest` from a slice of `PlannedOperation` IRs.
pub fn build_query_batch(
    operations: &[PlannedOperation],
) -> Result<(String, QueryBatchRequest), QqlError> {
    if operations.is_empty() {
        return Err(QqlError::execution(
            "QQL-BATCH-INVARIANT",
            "cannot build query batch from empty operations",
            None,
        ));
    }
    let collection = operations[0].collection().unwrap_or_default().to_string();
    let searches = operations
        .iter()
        .map(|operation| match operation {
            PlannedOperation::Query {
                request,
                collection: op_col,
            } => {
                if op_col != &collection {
                    return Err(QqlError::execution(
                        "QQL-BATCH-INVARIANT",
                        "query batch contained multiple collections",
                        None,
                    ));
                }
                Ok(request.clone())
            }
            _ => Err(QqlError::execution(
                "QQL-BATCH-INVARIANT",
                "query batch contained a non-query operation",
                None,
            )),
        })
        .collect::<Result<Vec<_>, _>>()?;
    Ok((collection, QueryBatchRequest { searches }))
}

/// Moving variant of [`build_query_batch`]: destructures an owned operation
/// vector into the wire batch with zero clones.
///
/// Ambient executors own `Vec<PlannedOperation>` (via `BatchGrouper`), so the
/// hot success path moves each `QueryRequest` (vectors, filters, prefetches)
/// instead of cloning it. The borrowed [`build_query_batch`] stays for
/// callers that only borrow their members (explicit `BATCH` blocks, REST
/// projection, WASM error envelopes).
///
/// Build errors are fatal invariants (propagated with `?`, never retried), so
/// consuming the vector on error is safe: the caller returns `Err` directly.
pub fn into_query_batch(
    operations: Vec<PlannedOperation>,
) -> Result<(String, QueryBatchRequest), QqlError> {
    if operations.is_empty() {
        return Err(QqlError::execution(
            "QQL-BATCH-INVARIANT",
            "cannot build query batch from empty operations",
            None,
        ));
    }
    let collection = operations[0].collection().unwrap_or_default().to_string();
    let mut searches = Vec::with_capacity(operations.len());
    for operation in operations {
        match operation {
            PlannedOperation::Query {
                request,
                collection: op_col,
            } => {
                if op_col != collection {
                    return Err(QqlError::execution(
                        "QQL-BATCH-INVARIANT",
                        "query batch contained multiple collections",
                        None,
                    ));
                }
                searches.push(request);
            }
            _ => {
                return Err(QqlError::execution(
                    "QQL-BATCH-INVARIANT",
                    "query batch contained a non-query operation",
                    None,
                ));
            }
        }
    }
    Ok((collection, QueryBatchRequest { searches }))
}

/// Build an `UpdateBatchRequest` from a slice of `PlannedOperation` IRs.
pub fn build_update_batch(
    operations: &[PlannedOperation],
) -> Result<(String, Vec<&'static str>, UpdateBatchRequest), QqlError> {
    if operations.is_empty() {
        return Err(QqlError::execution(
            "QQL-BATCH-INVARIANT",
            "cannot build update batch from empty operations",
            None,
        ));
    }
    let mut updates = Vec::with_capacity(operations.len());
    let mut labels = Vec::with_capacity(operations.len());
    let mut collection = None;

    for operation in operations {
        let Some((current_collection, update)) =
            crate::mutation::planned_to_update_operation(operation)
        else {
            return Err(QqlError::execution(
                "QQL-BATCH-INVARIANT",
                "mutation batch contained a non-mutation operation",
                None,
            ));
        };
        if collection
            .as_ref()
            .is_some_and(|col| col != &current_collection)
        {
            return Err(QqlError::execution(
                "QQL-BATCH-INVARIANT",
                "mutation batch contained multiple collections",
                None,
            ));
        }
        collection = Some(current_collection);
        labels.push(operation.operation_label());
        updates.push(update);
    }

    let collection = collection.unwrap_or_default();
    Ok((
        collection,
        labels,
        UpdateBatchRequest {
            operations: updates,
        },
    ))
}

/// Moving variant of [`build_update_batch`]: destructures an owned operation
/// vector into the wire batch with zero clones.
///
/// Uses [`crate::mutation::planned_to_update_operation_owned`] so each
/// request (points, filters, payloads) moves instead of cloning. The borrowed
/// [`build_update_batch`] stays for callers that only borrow their members.
/// See [`into_query_batch`] for why consuming on error is safe.
pub fn into_update_batch(
    operations: Vec<PlannedOperation>,
) -> Result<(String, Vec<&'static str>, UpdateBatchRequest), QqlError> {
    if operations.is_empty() {
        return Err(QqlError::execution(
            "QQL-BATCH-INVARIANT",
            "cannot build update batch from empty operations",
            None,
        ));
    }
    let mut updates = Vec::with_capacity(operations.len());
    let mut labels = Vec::with_capacity(operations.len());
    let mut collection = None;

    for operation in operations {
        let label = operation.operation_label();
        let Some((current_collection, update)) =
            crate::mutation::planned_to_update_operation_owned(operation)
        else {
            return Err(QqlError::execution(
                "QQL-BATCH-INVARIANT",
                "mutation batch contained a non-mutation operation",
                None,
            ));
        };
        if collection
            .as_ref()
            .is_some_and(|col| col != &current_collection)
        {
            return Err(QqlError::execution(
                "QQL-BATCH-INVARIANT",
                "mutation batch contained multiple collections",
                None,
            ));
        }
        collection = Some(current_collection);
        labels.push(label);
        updates.push(update);
    }

    let collection = collection.unwrap_or_default();
    Ok((
        collection,
        labels,
        UpdateBatchRequest {
            operations: updates,
        },
    ))
}

/// State machine orchestrating contiguous batch grouping across statements.
///
/// Shared between native Rust executor (`qql-runtime`) and WASM executor (`qql-wasm`).
#[derive(Debug, Default)]
pub struct BatchGrouper {
    pending: Vec<PlannedOperation>,
    pending_key: Option<BatchKey>,
}

impl BatchGrouper {
    /// Create a new empty batch grouper.
    pub fn new() -> Self {
        Self::default()
    }

    /// Check if an incoming raw AST statement violates the current batch family.
    ///
    /// A statement outside the current batch family is an execution barrier.
    /// Returns any pending batch that must be flushed *before* preparing `stmt`
    /// (because preparation may read or mutate backend state, e.g. UPSERT auto-creation).
    pub fn check_statement_barrier(&mut self, stmt: &Stmt) -> Option<Vec<PlannedOperation>> {
        let statement_key = statement_batch_key(stmt);
        if !self.pending.is_empty() && statement_key != self.pending_key {
            self.pending_key = None;
            Some(core::mem::take(&mut self.pending))
        } else {
            None
        }
    }

    /// Flush any pending batch when preparation or planning fails.
    pub fn flush_on_error(&mut self) -> Option<Vec<PlannedOperation>> {
        self.pending_key = None;
        if self.pending.is_empty() {
            None
        } else {
            Some(core::mem::take(&mut self.pending))
        }
    }

    /// Ingest a successfully planned operation.
    ///
    /// Returns `(flush_group, dispatch_single)`:
    /// - `flush_group`: Prior batch operations that must be flushed before this step.
    /// - `dispatch_single`: A non-batchable operation that must be dispatched individually.
    pub fn push_planned(
        &mut self,
        planned: PlannedOperation,
    ) -> (Option<Vec<PlannedOperation>>, Option<PlannedOperation>) {
        let key = planned.batch_key();
        if key.is_none() {
            self.pending_key = None;
            let flushed = if self.pending.is_empty() {
                None
            } else {
                Some(core::mem::take(&mut self.pending))
            };
            (flushed, Some(planned))
        } else if !self.pending.is_empty() && key != self.pending_key {
            let flushed = core::mem::take(&mut self.pending);
            self.pending_key = key;
            self.pending.push(planned);
            (Some(flushed), None)
        } else {
            self.pending_key = key;
            self.pending.push(planned);
            (None, None)
        }
    }

    /// Flush all remaining operations at the end of statement execution.
    pub fn finish(&mut self) -> Option<Vec<PlannedOperation>> {
        self.pending_key = None;
        if self.pending.is_empty() {
            None
        } else {
            Some(core::mem::take(&mut self.pending))
        }
    }

    /// Check if there are pending operations waiting to be flushed.
    pub fn has_pending(&self) -> bool {
        !self.pending.is_empty()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::plan::plan;

    fn planned(source: &str) -> PlannedOperation {
        let stmt = qql_core::parser::Parser::parse(source).expect("test stmt must parse");
        plan(&stmt).expect("test stmt must plan")
    }

    #[test]
    fn into_query_batch_matches_borrowed_build() {
        let ops = alloc::vec![
            planned("QUERY [0.1, 0.2] FROM docs LIMIT 2;"),
            planned("QUERY [0.3, 0.4] FROM docs LIMIT 3;"),
        ];
        let (borrowed_collection, borrowed) =
            build_query_batch(&ops).expect("borrowed build must succeed");
        let (owned_collection, owned) = into_query_batch(ops).expect("owned build must succeed");
        assert_eq!(owned_collection, borrowed_collection);
        assert_eq!(owned_collection, "docs");
        let borrowed_json = serde_json::to_value(&borrowed).expect("must serialize");
        let owned_json = serde_json::to_value(&owned).expect("must serialize");
        assert_eq!(owned_json, borrowed_json);
        assert_eq!(owned.searches.len(), 2);
    }

    #[test]
    fn into_update_batch_matches_borrowed_build() {
        let ops = alloc::vec![
            planned("UPSERT INTO docs VALUES {id: 1, vector: [0.1, 0.2]};"),
            planned("DELETE PAYLOAD a FROM docs WHERE id = 1;"),
        ];
        let (borrowed_collection, borrowed_labels, borrowed) =
            build_update_batch(&ops).expect("borrowed build must succeed");
        let (owned_collection, owned_labels, owned) =
            into_update_batch(ops).expect("owned build must succeed");
        assert_eq!(owned_collection, borrowed_collection);
        assert_eq!(owned_labels, borrowed_labels);
        let borrowed_json = serde_json::to_value(&borrowed).expect("must serialize");
        let owned_json = serde_json::to_value(&owned).expect("must serialize");
        assert_eq!(owned_json, borrowed_json);
    }

    #[test]
    fn into_batches_reject_empty_and_heterogeneous() {
        assert!(into_query_batch(Vec::new()).is_err());
        assert!(into_update_batch(Vec::new()).is_err());
        // Query builder rejects mutations and vice versa.
        let mutation = planned("DELETE PAYLOAD a FROM docs WHERE id = 1;");
        assert!(into_query_batch(alloc::vec![mutation]).is_err());
        let query = planned("QUERY [0.1, 0.2] FROM docs LIMIT 1;");
        assert!(into_update_batch(alloc::vec![query]).is_err());
    }

    #[test]
    fn into_batches_reject_multiple_collections() {
        let q1 = planned("QUERY [0.1, 0.2] FROM col_a LIMIT 1;");
        let q2 = planned("QUERY [0.1, 0.2] FROM col_b LIMIT 1;");
        assert!(into_query_batch(alloc::vec![q1.clone(), q2.clone()]).is_err());
        assert!(build_query_batch(&[q1, q2]).is_err());

        let m1 = planned("UPSERT INTO col_a VALUES {id: 1, vector: [0.1, 0.2]};");
        let m2 = planned("UPSERT INTO col_b VALUES {id: 2, vector: [0.1, 0.2]};");
        assert!(into_update_batch(alloc::vec![m1.clone(), m2.clone()]).is_err());
        assert!(build_update_batch(&[m1, m2]).is_err());
    }

    #[test]
    fn update_operation_owned_roundtrip_preserves_variant_and_collection() {
        let ops = alloc::vec![
            planned("UPSERT INTO docs VALUES {id: 1, vector: [0.1, 0.2]};"),
            planned("DELETE FROM docs WHERE id = 1;"),
            planned("UPDATE docs SET PAYLOAD = {a: 1} WHERE id = 1;"),
            planned("UPDATE docs SET PAYLOAD = {a: 1} OVERWRITE WHERE id = 1;"),
            planned("CLEAR PAYLOAD FROM docs WHERE id = 1;"),
            planned("DELETE PAYLOAD a FROM docs WHERE id = 1;"),
            planned("UPDATE docs SET VECTOR dense = [0.1, 0.2] WHERE id = 1;"),
            planned("DELETE VECTOR default FROM docs WHERE id = 1;"),
        ];
        assert_eq!(ops.len(), 8);
        let (collection, labels, batch) = into_update_batch(ops).expect("must build");
        assert_eq!(labels.len(), batch.operations.len());
        assert_eq!(labels.len(), 8);
        for (label, wire_op) in labels.iter().zip(batch.operations) {
            let rebuilt = crate::mutation::update_operation_into_planned(&collection, wire_op);
            assert_eq!(rebuilt.collection().unwrap(), collection);
            assert_eq!(&rebuilt.operation_label(), label);
        }
    }
}