fraiseql-core 2.12.0

Core execution engine for FraiseQL v2 - Compiled GraphQL over SQL
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
//! Vector query builder for pgvector similarity search.
//!
//! This module provides SQL query generation for pgvector operations including:
//! - Similarity search with configurable distance metrics
//! - Vector insert and upsert operations
//! - Proper parameter binding for vector data
//!
//! # Example
//!
//! ```
//! use fraiseql_core::utils::vector::{VectorQueryBuilder, VectorSearchQuery};
//! use fraiseql_core::schema::DistanceMetric;
//!
//! let builder = VectorQueryBuilder::new();
//! let query = VectorSearchQuery {
//!     table: "documents".to_string(),
//!     embedding_column: "embedding".to_string(),
//!     select_columns: vec!["id".to_string(), "content".to_string()],
//!     distance_metric: DistanceMetric::Cosine,
//!     limit: 10,
//!     where_clause: None,
//!     order_by: None,
//!     include_distance: false,
//!     offset: None,
//! };
//!
//! let (sql, _params) = builder.similarity_search(&query, &[0.1, 0.2, 0.3]);
//! assert!(sql.contains("documents"));
//! ```

use serde::{Deserialize, Serialize};

use crate::schema::{DistanceMetric, VectorConfig};

/// A SQL parameter value for vector queries.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub enum VectorParam {
    /// A vector embedding (array of floats).
    Vector(Vec<f32>),
    /// An integer value.
    Int(i64),
    /// A string value.
    String(String),
    /// A JSON value.
    Json(serde_json::Value),
}

impl VectorParam {
    /// Convert to SQL literal string for debugging.
    #[must_use]
    pub fn to_sql_literal(&self) -> String {
        match self {
            VectorParam::Vector(v) => {
                let values: Vec<String> = v.iter().map(std::string::ToString::to_string).collect();
                format!("'[{}]'::vector", values.join(","))
            },
            VectorParam::Int(i) => i.to_string(),
            VectorParam::String(s) => format!("'{}'", s.replace('\'', "''")),
            VectorParam::Json(j) => format!("'{j}'::jsonb"),
        }
    }
}

/// Configuration for a similarity search query.
#[derive(Debug, Clone)]
pub struct VectorSearchQuery {
    /// Table or view to query.
    pub table:            String,
    /// Column containing the vector embedding.
    pub embedding_column: String,
    /// Columns to select (empty = all).
    pub select_columns:   Vec<String>,
    /// Distance metric to use.
    pub distance_metric:  DistanceMetric,
    /// Maximum number of results.
    pub limit:            u32,
    /// Optional WHERE clause (without "WHERE" keyword).
    pub where_clause:     Option<String>,
    /// Optional additional ORDER BY clause (applied after distance ordering).
    pub order_by:         Option<String>,
    /// Whether to include the distance score in results.
    pub include_distance: bool,
    /// Optional offset for pagination.
    pub offset:           Option<u32>,
}

impl Default for VectorSearchQuery {
    fn default() -> Self {
        Self {
            table:            String::new(),
            embedding_column: "embedding".to_string(),
            select_columns:   Vec::new(),
            distance_metric:  DistanceMetric::Cosine,
            limit:            10,
            where_clause:     None,
            order_by:         None,
            include_distance: false,
            offset:           None,
        }
    }
}

impl VectorSearchQuery {
    /// Create a new search query for a table.
    pub fn new(table: impl Into<String>) -> Self {
        Self {
            table: table.into(),
            ..Default::default()
        }
    }

    /// Set the embedding column.
    pub fn with_embedding_column(mut self, column: impl Into<String>) -> Self {
        self.embedding_column = column.into();
        self
    }

    /// Set the columns to select.
    #[must_use = "builder method returns modified builder"]
    pub fn with_select_columns(mut self, columns: Vec<String>) -> Self {
        self.select_columns = columns;
        self
    }

    /// Set the distance metric.
    #[must_use = "builder method returns modified builder"]
    pub const fn with_distance_metric(mut self, metric: DistanceMetric) -> Self {
        self.distance_metric = metric;
        self
    }

    /// Set the result limit.
    #[must_use = "builder method returns modified builder"]
    pub const fn with_limit(mut self, limit: u32) -> Self {
        self.limit = limit;
        self
    }

    /// Set a WHERE clause filter.
    pub fn with_where(mut self, clause: impl Into<String>) -> Self {
        self.where_clause = Some(clause.into());
        self
    }

    /// Include distance score in results.
    #[must_use = "builder method returns modified builder"]
    pub const fn with_distance_score(mut self) -> Self {
        self.include_distance = true;
        self
    }

    /// Set pagination offset.
    #[must_use = "builder method returns modified builder"]
    pub const fn with_offset(mut self, offset: u32) -> Self {
        self.offset = Some(offset);
        self
    }
}

/// Configuration for a vector insert/upsert operation.
#[derive(Debug, Clone)]
pub struct VectorInsertQuery {
    /// Table to insert into.
    pub table:            String,
    /// Columns to insert (in order).
    pub columns:          Vec<String>,
    /// Name of the vector column.
    pub vector_column:    String,
    /// Whether to upsert (ON CONFLICT DO UPDATE).
    pub upsert:           bool,
    /// Conflict column(s) for upsert.
    pub conflict_columns: Vec<String>,
    /// Columns to update on conflict (empty = all non-conflict columns).
    pub update_columns:   Vec<String>,
    /// Whether to return inserted IDs.
    pub returning:        Option<String>,
}

impl Default for VectorInsertQuery {
    fn default() -> Self {
        Self {
            table:            String::new(),
            columns:          Vec::new(),
            vector_column:    "embedding".to_string(),
            upsert:           false,
            conflict_columns: vec!["id".to_string()],
            update_columns:   Vec::new(),
            returning:        Some("id".to_string()),
        }
    }
}

impl VectorInsertQuery {
    /// Create a new insert query.
    pub fn new(table: impl Into<String>) -> Self {
        Self {
            table: table.into(),
            ..Default::default()
        }
    }

    /// Set the columns to insert.
    #[must_use = "builder method returns modified builder"]
    pub fn with_columns(mut self, columns: Vec<String>) -> Self {
        self.columns = columns;
        self
    }

    /// Set the vector column name.
    pub fn with_vector_column(mut self, column: impl Into<String>) -> Self {
        self.vector_column = column.into();
        self
    }

    /// Enable upsert mode.
    #[must_use = "builder method returns modified builder"]
    pub fn with_upsert(mut self, conflict_columns: Vec<String>) -> Self {
        self.upsert = true;
        self.conflict_columns = conflict_columns;
        self
    }

    /// Set columns to update on conflict.
    #[must_use = "builder method returns modified builder"]
    pub fn with_update_columns(mut self, columns: Vec<String>) -> Self {
        self.update_columns = columns;
        self
    }

    /// Set the RETURNING clause.
    pub fn with_returning(mut self, column: impl Into<String>) -> Self {
        self.returning = Some(column.into());
        self
    }
}

/// Builder for pgvector SQL queries.
///
/// This struct generates SQL for vector similarity search and manipulation
/// operations using `PostgreSQL`'s `pgvector` extension.
#[must_use = "call .build() to construct the final value"]
#[derive(Debug, Clone, Default)]
pub struct VectorQueryBuilder {
    /// Parameter placeholder style ($1 vs ?).
    placeholder_style: PlaceholderStyle,
}

/// Style of parameter placeholders in generated SQL.
#[derive(Debug, Clone, Copy, Default)]
#[non_exhaustive]
pub enum PlaceholderStyle {
    /// `PostgreSQL` style: `$1`, `$2`, `$3`
    #[default]
    Dollar,
    /// MySQL/SQLite style: `?`, `?`, `?`
    QuestionMark,
}

impl VectorQueryBuilder {
    /// Create a new vector query builder.
    pub fn new() -> Self {
        Self::default()
    }

    /// Create a builder with question mark placeholders.
    pub const fn with_question_marks() -> Self {
        Self {
            placeholder_style: PlaceholderStyle::QuestionMark,
        }
    }

    /// Generate a parameter placeholder.
    fn placeholder(&self, index: usize) -> String {
        match self.placeholder_style {
            PlaceholderStyle::Dollar => format!("${index}"),
            PlaceholderStyle::QuestionMark => "?".to_string(),
        }
    }

    /// Build a similarity search query.
    ///
    /// Generates SQL like:
    /// ```sql
    /// SELECT id, content, (embedding <=> $1::vector) AS distance
    /// FROM documents
    /// WHERE metadata->>'type' = 'article'
    /// ORDER BY embedding <=> $1::vector
    /// LIMIT 10
    /// ```
    ///
    /// # Arguments
    /// * `query` - The search query configuration
    /// * `query_embedding` - The embedding vector to search for
    ///
    /// # Returns
    /// A tuple of (SQL string, parameter values)
    #[must_use]
    pub fn similarity_search(
        &self,
        query: &VectorSearchQuery,
        query_embedding: &[f32],
    ) -> (String, Vec<VectorParam>) {
        let mut params = Vec::new();
        let mut param_idx = 1;

        // Add the query embedding as the first parameter
        params.push(VectorParam::Vector(query_embedding.to_vec()));
        let embedding_placeholder = format!("{}::vector", self.placeholder(param_idx));
        param_idx += 1;

        let distance_op = query.distance_metric.operator();

        // Build SELECT clause
        let select_clause = if query.select_columns.is_empty() {
            if query.include_distance {
                format!(
                    "*, ({} {} {}) AS distance",
                    query.embedding_column, distance_op, embedding_placeholder
                )
            } else {
                "*".to_string()
            }
        } else {
            let cols = query.select_columns.join(", ");
            if query.include_distance {
                format!(
                    "{}, ({} {} {}) AS distance",
                    cols, query.embedding_column, distance_op, embedding_placeholder
                )
            } else {
                cols
            }
        };

        // Build WHERE clause
        let where_clause = if let Some(ref clause) = query.where_clause {
            format!("\nWHERE {clause}")
        } else {
            String::new()
        };

        // Build ORDER BY clause (always order by distance for similarity search)
        let order_clause = format!(
            "\nORDER BY {} {} {}",
            query.embedding_column, distance_op, embedding_placeholder
        );

        // Build LIMIT clause
        let limit_clause = format!("\nLIMIT {}", self.placeholder(param_idx));
        params.push(VectorParam::Int(i64::from(query.limit)));
        param_idx += 1;

        // Build OFFSET clause
        let offset_clause = if let Some(offset) = query.offset {
            let clause = format!("\nOFFSET {}", self.placeholder(param_idx));
            params.push(VectorParam::Int(i64::from(offset)));
            clause
        } else {
            String::new()
        };

        let sql = format!(
            "SELECT {}\nFROM {}{}{}{}{}",
            select_clause, query.table, where_clause, order_clause, limit_clause, offset_clause
        );

        (sql, params)
    }

    /// Build a single vector insert query.
    ///
    /// Generates SQL like:
    /// ```sql
    /// INSERT INTO documents (id, content, embedding)
    /// VALUES ($1, $2, $3::vector)
    /// RETURNING id
    /// ```
    #[must_use]
    pub fn insert_one(
        &self,
        query: &VectorInsertQuery,
        values: &[VectorParam],
    ) -> (String, Vec<VectorParam>) {
        let columns = query.columns.join(", ");

        let placeholders: Vec<String> = values
            .iter()
            .enumerate()
            .map(|(i, v)| {
                let ph = self.placeholder(i + 1);
                if matches!(v, VectorParam::Vector(_)) {
                    format!("{ph}::vector")
                } else {
                    ph
                }
            })
            .collect();

        let values_clause = placeholders.join(", ");

        let returning_clause = if let Some(ref col) = query.returning {
            format!("\nRETURNING {col}")
        } else {
            String::new()
        };

        let sql = if query.upsert {
            let conflict_cols = query.conflict_columns.join(", ");

            // Determine which columns to update
            let update_cols: Vec<&String> = if query.update_columns.is_empty() {
                // Update all non-conflict columns
                query.columns.iter().filter(|c| !query.conflict_columns.contains(c)).collect()
            } else {
                query.update_columns.iter().collect()
            };

            let update_clause: String = update_cols
                .iter()
                .map(|c| format!("{c} = EXCLUDED.{c}"))
                .collect::<Vec<_>>()
                .join(", ");

            format!(
                "INSERT INTO {} ({})\nVALUES ({})\nON CONFLICT ({}) DO UPDATE SET {}{}",
                query.table, columns, values_clause, conflict_cols, update_clause, returning_clause
            )
        } else {
            format!(
                "INSERT INTO {} ({})\nVALUES ({}){}",
                query.table, columns, values_clause, returning_clause
            )
        };

        (sql, values.to_vec())
    }

    /// Build a batch vector insert query.
    ///
    /// Generates SQL like:
    /// ```sql
    /// INSERT INTO documents (id, content, embedding)
    /// VALUES
    ///   ($1, $2, $3::vector),
    ///   ($4, $5, $6::vector),
    ///   ($7, $8, $9::vector)
    /// RETURNING id
    /// ```
    #[must_use]
    pub fn insert_batch(
        &self,
        query: &VectorInsertQuery,
        rows: &[Vec<VectorParam>],
    ) -> (String, Vec<VectorParam>) {
        if rows.is_empty() {
            return (String::new(), Vec::new());
        }

        let columns = query.columns.join(", ");
        let cols_per_row = query.columns.len();

        let mut all_params = Vec::new();
        let mut values_clauses = Vec::new();

        for (row_idx, row) in rows.iter().enumerate() {
            let base_idx = row_idx * cols_per_row + 1;
            let placeholders: Vec<String> = row
                .iter()
                .enumerate()
                .map(|(i, v)| {
                    let ph = self.placeholder(base_idx + i);
                    if matches!(v, VectorParam::Vector(_)) {
                        format!("{ph}::vector")
                    } else {
                        ph
                    }
                })
                .collect();

            values_clauses.push(format!("({})", placeholders.join(", ")));
            all_params.extend(row.clone());
        }

        let returning_clause = if let Some(ref col) = query.returning {
            format!("\nRETURNING {col}")
        } else {
            String::new()
        };

        let sql = format!(
            "INSERT INTO {} ({})\nVALUES\n  {}{}",
            query.table,
            columns,
            values_clauses.join(",\n  "),
            returning_clause
        );

        (sql, all_params)
    }

    /// Build a query to create a vector index.
    ///
    /// Generates SQL like:
    /// ```sql
    /// CREATE INDEX ON documents USING hnsw (embedding vector_cosine_ops)
    /// ```
    #[must_use]
    pub fn create_index(&self, config: &VectorConfig, table: &str, column: &str) -> Option<String> {
        config.index_type.index_sql(table, column, config.distance_metric)
    }
}