qql-convert 0.4.2

Qdrant REST JSON to QQL statement converter
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
//! Decode point mutation request bodies (upsert, delete, payload/vector writes).

use serde_json::Value;

use crate::ConvertError;
use crate::decode::DecodeCtx;
use crate::decode::payload::{decode_payload, payload_at};
use crate::decode::{filter, vector};
use crate::json::{self, child, index, invalid};
use qql_core::ast::{
    ClearPayloadStmt, DeletePayloadStmt, DeleteStmt, DeleteVectorStmt, PointEntry, PointSelector,
    PointVectors, ShardKey, UpdatePayloadStmt, UpdateVectorPoint, UpdateVectorStmt, UpsertPoint,
    UpsertStmt,
};

/// Decode a `PUT /points` (`PointInsertOperations`) body into `UPSERT`.
pub(crate) fn upsert(body: &Value, ctx: DecodeCtx<'_>) -> Result<UpsertStmt, ConvertError> {
    ctx.opts.reject_read()?;
    let path = "body";
    let obj = json::object(body, path)?;
    json::reject_unknown(
        obj,
        path,
        &[
            "points",
            "batch",
            "shard_key",
            "update_filter",
            "update_mode",
        ],
    )?;
    let shard_key = vector::shard_key_field(obj, path)?;
    let update_filter = match obj.get("update_filter").filter(|v| !v.is_null()) {
        None => None,
        Some(value) => filter::filter_opt(value, &child(path, "update_filter"))?,
    };
    let update_mode = match obj.get("update_mode").filter(|v| !v.is_null()) {
        None => None,
        Some(value) => Some(update_mode_value(value, &child(path, "update_mode"))?),
    };
    let points = if let Some(batch) = obj.get("batch") {
        decode_batch(batch, &child(path, "batch"))?
    } else {
        let points_path = child(path, "points");
        json::required(obj, "points", path)
            .and_then(|v| json::array(v, &points_path))?
            .iter()
            .enumerate()
            .map(|(i, point)| decode_point_struct(point, &index(&points_path, i)))
            .collect::<Result<Vec<_>, _>>()?
    };
    if points.is_empty() {
        return Err(invalid(
            child(path, "points"),
            "upsert requires at least one point",
        ));
    }
    Ok(UpsertStmt {
        collection: ctx.collection.to_string(),
        points,
        embedding: None,
        embed: Vec::new(),
        update_filter,
        update_mode,
        shard_key,
        wait: ctx.opts.wait,
    })
}

/// Decode an OpenAPI `UpdateMode` string into `UpsertUpdateMode`.
fn update_mode_value(
    value: &Value,
    path: &str,
) -> Result<qql_core::ast::UpsertUpdateMode, ConvertError> {
    use qql_core::ast::UpsertUpdateMode;
    match value.as_str() {
        Some("insert_only") => Ok(UpsertUpdateMode::InsertOnly),
        Some("update_only") => Ok(UpsertUpdateMode::UpdateOnly),
        Some("upsert") => Ok(UpsertUpdateMode::Upsert),
        Some(other) => Err(invalid(
            path,
            format!("unknown update_mode '{other}' (expected insert_only, update_only, or upsert)"),
        )),
        None => Err(invalid(
            path,
            format!(
                "expected update_mode string, got {}",
                json::type_name(value)
            ),
        )),
    }
}

/// Decode a `Batch` (`{ids, vectors, payloads?}`) into inline points.
fn decode_batch(value: &Value, path: &str) -> Result<Vec<PointEntry>, ConvertError> {
    let batch = json::object(value, path)?;
    let ids_path = child(path, "ids");
    let ids = json::required(batch, "ids", path)
        .and_then(|v| json::array(v, &ids_path))?
        .iter()
        .enumerate()
        .map(|(i, id)| vector::point_id(id, &index(&ids_path, i)))
        .collect::<Result<Vec<_>, _>>()?;
    if ids.is_empty() {
        return Err(invalid(ids_path, "batch ids must not be empty"));
    }
    let payloads = match batch.get("payloads").filter(|v| !v.is_null()) {
        None => Vec::new(),
        Some(value) => json::array(value, &child(path, "payloads"))?.clone(),
    };
    if !payloads.is_empty() && payloads.len() != ids.len() {
        return Err(invalid(
            child(path, "payloads"),
            format!(
                "payloads length {} does not match ids length {}",
                payloads.len(),
                ids.len()
            ),
        ));
    }
    let vectors = decode_batch_vectors(
        json::required(batch, "vectors", path)?,
        &child(path, "vectors"),
        ids.len(),
    )?;
    ids.into_iter()
        .enumerate()
        .map(|(i, id)| {
            Ok(PointEntry::Inline(UpsertPoint {
                id,
                vectors: Some(vectors[i].clone()),
                payload: payload_at(&payloads, i, &child(path, "payloads"))?,
            }))
        })
        .collect()
}

/// Decode a `BatchVectorStruct` into one `PointVectors` per batch id.
fn decode_batch_vectors(
    value: &Value,
    path: &str,
    count: usize,
) -> Result<Vec<PointVectors>, ConvertError> {
    match value {
        Value::Array(items) => {
            if items.len() != count {
                return Err(invalid(
                    path,
                    format!(
                        "batch vectors length {} does not match ids length {count}",
                        items.len()
                    ),
                ));
            }
            items
                .iter()
                .enumerate()
                .map(|(i, item)| {
                    Ok(PointVectors::Unnamed(vector::vector_value(
                        item,
                        &index(path, i),
                    )?))
                })
                .collect()
        }
        Value::Object(named) => {
            let mut per_point: Vec<Vec<(String, qql_core::ast::VectorValue)>> =
                vec![Vec::new(); count];
            for (name, column) in named {
                let column_path = child(path, name);
                let values = json::array(column, &column_path)?;
                if values.len() != count {
                    return Err(invalid(
                        column_path,
                        format!(
                            "batch vector column length {} does not match ids length {count}",
                            values.len()
                        ),
                    ));
                }
                for (i, item) in values.iter().enumerate() {
                    per_point[i].push((
                        name.clone(),
                        vector::vector_value(item, &index(&column_path, i))?,
                    ));
                }
            }
            Ok(per_point.into_iter().map(PointVectors::Named).collect())
        }
        other => Err(invalid(
            path,
            format!(
                "expected batch vectors (per-point array or name map), got {}",
                json::type_name(other)
            ),
        )),
    }
}

/// Decode one `PointStruct` (`{id, vector?, payload?}`).
fn decode_point_struct(value: &Value, path: &str) -> Result<PointEntry, ConvertError> {
    let obj = json::object(value, path)?;
    let id = vector::point_id(json::required(obj, "id", path)?, &child(path, "id"))?;
    let vectors = match obj.get("vector").filter(|v| !v.is_null()) {
        None => None,
        Some(value) => Some(vector::point_vectors(value, &child(path, "vector"))?),
    };
    let payload = match obj.get("payload").filter(|v| !v.is_null()) {
        None => Vec::new(),
        Some(value) => decode_payload(value, &child(path, "payload"))?,
    };
    Ok(PointEntry::Inline(UpsertPoint {
        id,
        vectors,
        payload,
    }))
}

/// Decode a `PointsSelector` / `{points, filter}` body into a selector.
fn selector(obj: &json::Obj, path: &str) -> Result<PointSelector, ConvertError> {
    let points = obj.get("points").filter(|v| !v.is_null());
    let filter = filter::filter_field(obj, "filter", path)?;
    match (points, filter) {
        (Some(value), None) => {
            let list_path = child(path, "points");
            let ids = json::array(value, &list_path)?
                .iter()
                .enumerate()
                .map(|(i, id)| vector::point_id(id, &index(&list_path, i)))
                .collect::<Result<Vec<_>, _>>()?;
            if ids.is_empty() {
                return Err(invalid(list_path, "selector must list at least one point"));
            }
            Ok(PointSelector::Ids(ids))
        }
        (None, Some(filter)) => Ok(PointSelector::Filter(filter)),
        (Some(_), Some(_)) => Err(invalid(
            path,
            "selector cannot combine `points` and `filter`",
        )),
        (None, None) => Err(invalid(path, "selector requires `points` or `filter`")),
    }
}

/// Decode a `POST /points/delete` body into `DELETE`.
pub(crate) fn delete(body: &Value, ctx: DecodeCtx<'_>) -> Result<DeleteStmt, ConvertError> {
    ctx.opts.reject_read()?;
    let path = "body";
    let obj = json::object(body, path)?;
    json::reject_unknown(obj, path, &["points", "filter", "shard_key"])?;
    Ok(DeleteStmt {
        collection: ctx.collection.to_string(),
        selector: selector(obj, path)?,
        shard_key: vector::shard_key_field(obj, path)?,
        wait: ctx.opts.wait,
    })
}

/// Decode a `POST /points/payload/clear` body into `CLEAR PAYLOAD`.
pub(crate) fn clear_payload(
    body: &Value,
    ctx: DecodeCtx<'_>,
) -> Result<ClearPayloadStmt, ConvertError> {
    ctx.opts.reject_read()?;
    let path = "body";
    let obj = json::object(body, path)?;
    json::reject_unknown(obj, path, &["points", "filter", "shard_key"])?;
    Ok(ClearPayloadStmt {
        collection: ctx.collection.to_string(),
        selector: selector(obj, path)?,
        shard_key: vector::shard_key_field(obj, path)?,
        wait: ctx.opts.wait,
    })
}

/// Decode a `POST /points/payload` (`SetPayload`) body into `UPDATE … SET PAYLOAD`.
pub(crate) fn update_payload(
    body: &Value,
    ctx: DecodeCtx<'_>,
) -> Result<UpdatePayloadStmt, ConvertError> {
    ctx.opts.reject_read()?;
    let path = "body";
    let obj = json::object(body, path)?;
    json::reject_unknown(
        obj,
        path,
        &["payload", "points", "filter", "shard_key", "key"],
    )?;
    let key = match obj.get("key").filter(|v| !v.is_null()) {
        None => None,
        Some(value) => Some(json::string_at(value, &child(path, "key"))?.to_string()),
    };
    let payload = decode_payload(
        json::required(obj, "payload", path)?,
        &child(path, "payload"),
    )?;
    Ok(UpdatePayloadStmt {
        collection: ctx.collection.to_string(),
        selector: selector(obj, path)?,
        payload,
        key,
        overwrite: false,
        shard_key: vector::shard_key_field(obj, path)?,
        wait: ctx.opts.wait,
    })
}

/// Decode a `POST /points/payload/delete` body into `DELETE PAYLOAD`.
pub(crate) fn delete_payload(
    body: &Value,
    ctx: DecodeCtx<'_>,
) -> Result<DeletePayloadStmt, ConvertError> {
    ctx.opts.reject_read()?;
    let path = "body";
    let obj = json::object(body, path)?;
    json::reject_unknown(obj, path, &["keys", "points", "filter", "shard_key"])?;
    let keys_path = child(path, "keys");
    let keys = json::required(obj, "keys", path)
        .and_then(|v| json::array(v, &keys_path))?
        .iter()
        .enumerate()
        .map(|(i, key)| Ok(json::string_at(key, &index(&keys_path, i))?.to_string()))
        .collect::<Result<Vec<String>, ConvertError>>()?;
    if keys.is_empty() {
        return Err(invalid(
            keys_path,
            "keys must list at least one payload key",
        ));
    }
    Ok(DeletePayloadStmt {
        collection: ctx.collection.to_string(),
        keys,
        selector: selector(obj, path)?,
        shard_key: vector::shard_key_field(obj, path)?,
        wait: ctx.opts.wait,
    })
}

/// Decode a `POST /points/vectors/delete` body into `DELETE VECTOR`.
pub(crate) fn delete_vectors(
    body: &Value,
    ctx: DecodeCtx<'_>,
) -> Result<DeleteVectorStmt, ConvertError> {
    ctx.opts.reject_read()?;
    let path = "body";
    let obj = json::object(body, path)?;
    json::reject_unknown(obj, path, &["vector", "points", "filter", "shard_key"])?;
    let names_path = child(path, "vector");
    let vector_names = json::required(obj, "vector", path)
        .and_then(|v| json::array(v, &names_path))?
        .iter()
        .enumerate()
        .map(|(i, name)| Ok(json::string_at(name, &index(&names_path, i))?.to_string()))
        .collect::<Result<Vec<String>, ConvertError>>()?;
    if vector_names.is_empty() {
        return Err(invalid(
            names_path,
            "vector must list at least one vector name",
        ));
    }
    Ok(DeleteVectorStmt {
        collection: ctx.collection.to_string(),
        selector: selector(obj, path)?,
        vector_names,
        shard_key: vector::shard_key_field(obj, path)?,
        wait: ctx.opts.wait,
    })
}

/// Decode a `PUT /points/vectors` (`UpdateVectors`) body into one
/// `UPDATE … SET VECTOR` statement. Named maps and multiple points stay on
/// the statement; the formatter picks compact `WHERE id =` vs `VALUES`.
pub(crate) fn update_vectors(
    body: &Value,
    ctx: DecodeCtx<'_>,
) -> Result<UpdateVectorStmt, ConvertError> {
    ctx.opts.reject_read()?;
    let path = "body";
    let obj = json::object(body, path)?;
    json::reject_unknown(obj, path, &["points", "shard_key", "update_filter"])?;
    if obj.contains_key("update_filter") {
        return Err(invalid(
            child(path, "update_filter"),
            "update_filter has no QQL representation",
        ));
    }
    let shard_key: Option<ShardKey> = vector::shard_key_field(obj, path)?;
    let points_path = child(path, "points");
    let points = json::required(obj, "points", path).and_then(|v| json::array(v, &points_path))?;
    if points.is_empty() {
        return Err(invalid(points_path, "points must not be empty"));
    }
    let mut decoded = Vec::with_capacity(points.len());
    for (i, point) in points.iter().enumerate() {
        let point_path = index(&points_path, i);
        let point = json::object(point, &point_path)?;
        decoded.push(UpdateVectorPoint {
            id: vector::point_id(
                json::required(point, "id", &point_path)?,
                &child(&point_path, "id"),
            )?,
            vectors: vector::point_vectors(
                json::required(point, "vector", &point_path)?,
                &child(&point_path, "vector"),
            )?,
        });
    }
    Ok(UpdateVectorStmt {
        collection: ctx.collection.to_string(),
        points: decoded,
        shard_key,
        wait: ctx.opts.wait,
    })
}