qql 0.2.1

QQL runtime and executor for Qdrant REST and gRPC
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
use std::time::Duration;

use async_trait::async_trait;
use reqwest::{Client, Method};
use serde::de::DeserializeOwned;
use serde_json::Value;

use qql_core::error::QqlError;
use qql_plan::types::Method as PlanMethod;
use qql_plan::{QueryBatchRequest, UpdateBatchRequest};

use crate::client::{CollectionInfo, QdrantOps};

/// HTTP header for Qdrant 1.19 read affinity (`X-Qdrant-Route-Affinity`).
pub const ROUTE_AFFINITY_HEADER: &str = "X-Qdrant-Route-Affinity";

#[derive(Clone)]
pub struct RestQdrant {
    base_url: String,
    api_key: Option<String>,
    /// Stable value hashed by Qdrant to pin reads to one replica (session /
    /// user id). Sent as [`ROUTE_AFFINITY_HEADER`] on every request.
    route_affinity: Option<String>,
    client: Client,
}

impl RestQdrant {
    /// Construct with a 30s request timeout.
    ///
    /// Never panics (RUN-015 / RUN-010). If the timed client builder fails
    /// (effectively unreachable on stock reqwest), falls back to
    /// [`Client::new`]. Prefer [`Self::with_timeout`] when client-build
    /// failures must surface as errors.
    pub fn new(base_url: impl Into<String>, api_key: Option<String>) -> Self {
        let base_url = base_url.into();
        Self::with_timeout(base_url.clone(), api_key.clone(), Duration::from_secs(30))
            .unwrap_or_else(|_| Self::with_client(base_url, api_key, Client::new()))
    }

    /// Construct with an explicit request timeout. Fallible so library
    /// callers can surface client-build failures without panicking.
    pub fn with_timeout(
        base_url: impl Into<String>,
        api_key: Option<String>,
        timeout: Duration,
    ) -> Result<Self, QqlError> {
        let base_url = base_url.into();
        let client = Client::builder()
            .timeout(timeout)
            .connect_timeout(Duration::from_secs(10).min(timeout))
            .build()
            .map_err(|e| {
                QqlError::transport(
                    "QQL-TRANSPORT",
                    format!("failed to build HTTP client: {e}"),
                    None,
                )
            })?;
        Ok(Self {
            base_url: base_url.trim_end_matches('/').to_string(),
            api_key,
            route_affinity: None,
            client,
        })
    }

    pub fn with_client(base_url: String, api_key: Option<String>, client: Client) -> Self {
        Self {
            base_url: base_url.trim_end_matches('/').to_string(),
            api_key,
            route_affinity: None,
            client,
        }
    }

    /// Pin subsequent reads to a stable replica via `X-Qdrant-Route-Affinity`.
    ///
    /// See Qdrant docs: read affinity / consistency guarantees (v1.19+).
    /// Empty strings are treated as unset.
    pub fn with_route_affinity(mut self, affinity: impl Into<String>) -> Self {
        let value = affinity.into();
        self.route_affinity = if value.is_empty() { None } else { Some(value) };
        self
    }

    pub fn route_affinity(&self) -> Option<&str> {
        self.route_affinity.as_deref()
    }

    fn apply_headers(&self, mut req: reqwest::RequestBuilder) -> reqwest::RequestBuilder {
        if let Some(ref key) = self.api_key {
            req = req.header("api-key", key);
        }
        if let Some(ref affinity) = self.route_affinity {
            req = req.header(ROUTE_AFFINITY_HEADER, affinity);
        }
        req
    }

    async fn call_body<B: serde::Serialize + ?Sized, T: DeserializeOwned>(
        &self,
        method: Method,
        path: &str,
        body: Option<&B>,
    ) -> Result<T, QqlError> {
        let mut url_buf = String::with_capacity(self.base_url.len() + path.len());
        url_buf.push_str(&self.base_url);
        url_buf.push_str(path);
        let mut req = self.client.request(method, &url_buf);
        req = self.apply_headers(req);
        if let Some(b) = body {
            req = req.json(b);
        }
        let resp = req.send().await.map_err(|error| {
            QqlError::transport(
                "QQL-TRANSPORT",
                format!("HTTP request failed: {error}"),
                None,
            )
            .with_url(url_buf.clone())
        })?;
        let status = resp.status();
        let text = resp.text().await.map_err(|error| {
            QqlError::backend(
                "QQL-BACKEND",
                format!("failed to read response body: {error}"),
                None,
            )
            .with_url(url_buf.clone())
        })?;
        if !status.is_success() {
            let detail = if text.len() > 4096 {
                &text[..4096]
            } else {
                &text
            };
            return Err(QqlError::backend(
                "QQL-BACKEND-HTTP",
                format!("Qdrant returned {status}: {detail}"),
                None,
            )
            .with_status(status.as_u16())
            .with_url(url_buf.clone()));
        }
        let value: Value = serde_json::from_str(&text).map_err(|error| {
            QqlError::backend(
                "QQL-BACKEND-JSON",
                format!("failed to parse Qdrant response: {error}"),
                None,
            )
            .with_url(url_buf.clone())
        })?;
        validate_success_envelope(&value, path)?;
        serde_json::from_value(value).map_err(|error| {
            QqlError::backend(
                "QQL-BACKEND-JSON",
                format!("failed to decode Qdrant response: {error}"),
                None,
            )
            .with_url(url_buf.clone())
        })
    }

    async fn call<T: DeserializeOwned>(
        &self,
        method: Method,
        path: &str,
        body: Option<Value>,
    ) -> Result<T, QqlError> {
        self.call_body(method, path, body.as_ref()).await
    }
}

#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl QdrantOps for RestQdrant {
    async fn list_collections(&self) -> Result<Vec<String>, QqlError> {
        let value: Value = self.call(Method::GET, "/collections", None).await?;
        validate_success_envelope(&value, "list_collections")?;
        let collections = value
            .get("result")
            .and_then(|r| r.get("collections"))
            .and_then(|c| c.as_array())
            .cloned()
            .ok_or_else(|| {
                QqlError::backend(
                    "QQL-BACKEND-ENVELOPE",
                    "list_collections response result.collections missing or not an array",
                    None,
                )
            })?;
        Ok(collections
            .iter()
            .filter_map(|c| c.get("name").and_then(Value::as_str).map(String::from))
            .collect())
    }

    async fn collection_exists(&self, name: &str) -> Result<bool, QqlError> {
        match self
            .call::<Value>(Method::GET, &format!("/collections/{name}"), None)
            .await
        {
            Ok(value) => {
                validate_success_envelope(&value, "collection_exists")?;
                let status_ok = value
                    .get("result")
                    .and_then(|r| r.get("status").or_else(|| r.get("exists")))
                    .is_some();
                Ok(status_ok)
            }
            Err(e) if e.message.contains("404") || e.message.contains("Not found") => Ok(false),
            Err(e) => Err(e),
        }
    }

    async fn get_collection_info(&self, name: &str) -> Result<CollectionInfo, QqlError> {
        let value: Value = self
            .call(Method::GET, &format!("/collections/{name}"), None)
            .await?;
        validate_success_envelope(&value, "get_collection_info")?;
        let result = value.get("result").cloned().unwrap_or(value);

        let schema = crate::backend::schema_from_rest_result(&result);

        let mut info: CollectionInfo = serde_json::from_value(result).map_err(|e| {
            QqlError::backend(
                "QQL-BACKEND-JSON",
                format!("parse collection info: {e}"),
                None,
            )
            .with_collection(name.to_string())
        })?;
        info.schema = schema;
        Ok(info)
    }

    async fn create_collection(
        &self,
        collection_name: &str,
        req: &qql_plan::CreateCollectionRequest,
    ) -> Result<(), QqlError> {
        self.create_collection_planned(collection_name, req).await
    }

    async fn update_collection(
        &self,
        collection_name: &str,
        req: &qql_plan::UpdateCollectionRequest,
    ) -> Result<(), QqlError> {
        let op = qql_plan::PlannedOperation::UpdateCollection {
            collection: collection_name.to_string(),
            request: req.clone(),
        };
        self.execute_planned(&op).await.map(|_| ())
    }

    async fn delete_collection(&self, name: &str) -> Result<(), QqlError> {
        self.call::<Value>(Method::DELETE, &format!("/collections/{name}"), None)
            .await?;
        Ok(())
    }

    async fn create_field_index(
        &self,
        collection_name: &str,
        req: &qql_plan::CreateIndexRequest,
    ) -> Result<(), QqlError> {
        let op = qql_plan::PlannedOperation::CreateIndex {
            collection: collection_name.to_string(),
            request: req.clone(),
        };
        self.execute_planned(&op).await.map(|_| ())
    }

    async fn delete_field_index(
        &self,
        collection_name: &str,
        field_name: &str,
    ) -> Result<(), QqlError> {
        let op = qql_plan::PlannedOperation::DropIndex {
            collection: collection_name.to_string(),
            field: field_name.to_string(),
        };
        self.execute_planned(&op).await.map(|_| ())
    }

    async fn execute_planned(&self, op: &qql_plan::PlannedOperation) -> Result<Value, QqlError> {
        if let qql_plan::PlannedOperation::CreateCollection {
            collection,
            request,
        } = op
        {
            self.create_collection_planned(collection, request).await?;
            return Ok(serde_json::json!({
                "result": true,
                "status": "ok",
                "time": 0.0,
            }));
        }
        let route = qql_plan::plan::to_rest_route(op).map_err(|err| match err {
            qql_plan::RestProjectionError::ClientSideOnly { stmt_type } => QqlError::execution(
                "QQL-REST-CLIENT-SIDE",
                format!("{stmt_type} cannot be executed as a single REST route"),
                None,
            ),
        })?;
        self.execute_http(route).await
    }

    async fn execute_query_batch(
        &self,
        collection: &str,
        batch: &QueryBatchRequest,
    ) -> Result<Vec<Value>, QqlError> {
        let path = format!("/collections/{collection}/points/query/batch");
        let value: Value = self.call_body(Method::POST, &path, Some(batch)).await?;
        result_array(&value, &path)
    }

    async fn execute_update_batch(
        &self,
        collection: &str,
        batch: &UpdateBatchRequest,
    ) -> Result<Vec<Value>, QqlError> {
        let path = format!("/collections/{collection}/points/batch?wait=true");
        let value: Value = self.call_body(Method::POST, &path, Some(batch)).await?;
        result_array(&value, &path)
    }
}

impl RestQdrant {
    /// CREATE COLLECTION with OpenAPI body + optional deferred params / shard keys
    /// (parity with gRPC multi-step create).
    async fn create_collection_planned(
        &self,
        collection: &str,
        req: &qql_plan::types::CreateCollectionRequest,
    ) -> Result<(), QqlError> {
        let body = qql_plan::ddl::create_collection_rest_body(req);
        self.call::<Value>(
            Method::PUT,
            &format!("/collections/{collection}"),
            Some(body),
        )
        .await?;

        if let Some(params_patch) = qql_plan::ddl::create_collection_deferred_params_rest(req) {
            self.call::<Value>(
                Method::PATCH,
                &format!("/collections/{collection}"),
                Some(params_patch),
            )
            .await?;
        }

        if let Some(keys) = &req.shard_keys {
            for key in keys {
                let shard_body = serde_json::json!({
                    "shard_key": key,
                });
                self.call::<Value>(
                    Method::PUT,
                    &format!("/collections/{collection}/shards"),
                    Some(shard_body),
                )
                .await?;
            }
        }
        Ok(())
    }

    /// Low-level HTTP dispatch from a pre-built Route.
    async fn execute_http(&self, route: qql_plan::routing::Route) -> Result<Value, QqlError> {
        let method = match route.method {
            PlanMethod::Get => Method::GET,
            PlanMethod::Post => Method::POST,
            PlanMethod::Put => Method::PUT,
            PlanMethod::Patch => Method::PATCH,
            PlanMethod::Delete => Method::DELETE,
        };

        let url = format!("{}{}", self.base_url, route.path);
        let mut builder = match method {
            Method::GET => self.client.get(&url),
            Method::POST => self.client.post(&url),
            Method::PUT => self.client.put(&url),
            Method::PATCH => self.client.patch(&url),
            Method::DELETE => self.client.delete(&url),
            _ => self.client.request(method, &url),
        };
        if !route.query.is_empty() {
            builder = builder.query(&route.query);
        }
        builder = self.apply_headers(builder);
        if let Some(ref body) = route.body {
            builder = builder.json(body);
        }
        let resp = builder.send().await.map_err(|e| {
            QqlError::transport("QQL-TRANSPORT", format!("REST request failed: {e}"), None)
                .with_url(url.clone())
        })?;
        let status = resp.status();
        let text = resp.text().await.map_err(|e| {
            QqlError::transport("QQL-TRANSPORT", format!("REST body read failed: {e}"), None)
        })?;
        if !status.is_success() {
            return Err(QqlError::backend(
                "QQL-BACKEND-HTTP",
                format!("REST {status}: {text}"),
                None,
            )
            .with_status(status.as_u16())
            .with_url(url));
        }
        let value: Value = serde_json::from_str(&text).map_err(|e| {
            QqlError::backend(
                "QQL-BACKEND-JSON",
                format!("invalid JSON response: {e}; body={text}"),
                None,
            )
        })?;
        validate_success_envelope(&value, &route.path)?;
        Ok(value)
    }
}

fn validate_success_envelope(value: &Value, operation: &str) -> Result<(), QqlError> {
    let object = value.as_object().ok_or_else(|| {
        QqlError::backend(
            "QQL-BACKEND-ENVELOPE",
            format!("{operation} returned a non-object JSON response"),
            None,
        )
    })?;

    if !object.contains_key("result") {
        return Err(QqlError::backend(
            "QQL-BACKEND-ENVELOPE",
            format!("{operation} response is missing the result field"),
            None,
        ));
    }

    if object.get("status").and_then(Value::as_str) != Some("ok") {
        return Err(QqlError::backend(
            "QQL-BACKEND-ENVELOPE",
            format!("{operation} response is missing status=ok"),
            None,
        ));
    }

    Ok(())
}

fn result_array(value: &Value, operation: &str) -> Result<Vec<Value>, QqlError> {
    value
        .get("result")
        .and_then(Value::as_array)
        .cloned()
        .ok_or_else(|| {
            QqlError::backend(
                "QQL-BACKEND-ENVELOPE",
                format!("{operation} response result must be an array"),
                None,
            )
        })
}

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

    #[test]
    fn accepts_qdrant_success_envelope() {
        let value = serde_json::json!({
            "result": [],
            "status": "ok",
            "time": 0.001,
        });
        assert!(validate_success_envelope(&value, "test").is_ok());
        assert!(result_array(&value, "test").is_ok());
    }

    #[test]
    fn rejects_missing_result() {
        let value = serde_json::json!({ "status": "ok" });
        let error = validate_success_envelope(&value, "test").unwrap_err();
        assert_eq!(error.code, "QQL-BACKEND-ENVELOPE");
    }

    #[test]
    fn rejects_non_ok_status() {
        let value = serde_json::json!({ "result": [], "status": "error" });
        let error = validate_success_envelope(&value, "test").unwrap_err();
        assert_eq!(error.code, "QQL-BACKEND-ENVELOPE");
    }
}