liboxen 0.49.1

Oxen is a fast, unstructured data version control, to help version large machine learning datasets written in Rust.
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
use arrow::array::FixedSizeListArray;
use arrow::array::{Float32Array, Float64Array, ListArray, RecordBatch};
use duckdb;
use polars::frame::DataFrame;

use crate::config::EMBEDDING_CONFIG_FILENAME;
use crate::config::EmbeddingConfig;
use crate::config::embedding_config::{EmbeddingColumn, EmbeddingStatus};
use crate::constants::{EXCLUDE_OXEN_COLS, TABLE_NAME};
use crate::core::db::data_frames::df_db::{self, with_df_db_manager};
use crate::error::OxenError;
use crate::model::Workspace;
use crate::model::data_frame::schema::Field;
use crate::opts::{EmbeddingQueryOpts, PaginateOpts};
use crate::{repositories, util};

use std::path::Path;
use std::path::PathBuf;

fn embedding_config_path(workspace: &Workspace, path: impl AsRef<Path>) -> PathBuf {
    let path = repositories::workspaces::data_frames::duckdb_path(workspace, path);
    let parent = path.parent().unwrap();
    parent.join(EMBEDDING_CONFIG_FILENAME)
}

fn embedding_config(
    workspace: &Workspace,
    path: impl AsRef<Path>,
) -> Result<EmbeddingConfig, OxenError> {
    let embedding_config = embedding_config_path(workspace, path);
    let config_data = util::fs::read_from_path(&embedding_config)?;
    Ok(toml::from_str(&config_data)?)
}

fn write_embedding_size_to_config(
    workspace: &Workspace,
    path: impl AsRef<Path>,
    column_name: impl AsRef<str>,
    vector_length: usize,
) -> Result<(), OxenError> {
    let embedding_config = embedding_config_path(workspace, path);

    // Try to read existing config, create new one if it doesn't exist
    let config_data = util::fs::read_from_path(&embedding_config).unwrap_or_default();
    let mut config: EmbeddingConfig = if config_data.is_empty() {
        EmbeddingConfig::default()
    } else {
        toml::from_str(&config_data)?
    };

    let column = EmbeddingColumn {
        name: column_name.as_ref().to_string(),
        vector_length,
        status: EmbeddingStatus::InProgress,
    };

    config
        .columns
        .insert(column_name.as_ref().to_string(), column);

    let config_str = toml::to_string(&config)?;
    std::fs::write(embedding_config, config_str)?;
    Ok(())
}

fn update_embedding_status(
    workspace: &Workspace,
    path: impl AsRef<Path>,
    column_name: impl AsRef<str>,
    status: EmbeddingStatus,
) -> Result<(), OxenError> {
    let embedding_config = embedding_config_path(workspace, path);
    let config_data = util::fs::read_from_path(&embedding_config)?;
    let mut config: EmbeddingConfig = toml::from_str(&config_data)?;
    config.columns.get_mut(column_name.as_ref()).unwrap().status = status;
    let config_str = toml::to_string(&config)?;
    std::fs::write(embedding_config, config_str)?;
    Ok(())
}

pub fn list_indexed_columns(
    workspace: &Workspace,
    path: impl AsRef<Path>,
) -> Result<Vec<EmbeddingColumn>, OxenError> {
    let Ok(config) = embedding_config(workspace, path) else {
        return Ok(vec![]);
    };
    Ok(config.columns.values().cloned().collect())
}

fn perform_indexing(
    workspace: &Workspace,
    path: impl AsRef<Path>,
    column_name: String,
    vector_length: usize,
) -> Result<(), OxenError> {
    let db_path = repositories::workspaces::data_frames::duckdb_path(workspace, &path);
    with_df_db_manager(&db_path, |manager| {
        manager.with_conn(|conn| {
            // Execute VSS commands separately
            conn.execute("INSTALL vss;", [])?;
            conn.execute("LOAD vss;", [])?;
            conn.execute("SET hnsw_enable_experimental_persistence = true;", [])?;

            // Convert column type
            let sql =
                format!("ALTER TABLE df ALTER COLUMN {column_name} TYPE FLOAT[{vector_length}];");
            log::debug!("Updating column type: {sql}");
            conn.execute(&sql, [])?;
            Ok(())
        })
    })?;

    log::debug!(
        "Completed indexing embeddings for column `{}` on {}",
        column_name,
        path.as_ref().display()
    );
    update_embedding_status(workspace, path, column_name, EmbeddingStatus::Complete)?;

    Ok(())
}

pub fn index(
    workspace: &Workspace,
    path: impl AsRef<Path>,
    column: impl AsRef<str>,
    use_background_thread: bool,
) -> Result<(), OxenError> {
    let path = path.as_ref().to_path_buf();
    let column = column.as_ref();

    let column_name = column.to_string();
    log::debug!(
        "Indexing embeddings for column: {column_name} using background thread: {use_background_thread}"
    );

    let vector_length = get_embedding_length(workspace, &path, column)?;

    if use_background_thread {
        // Clone necessary values for the background thread
        let workspace = workspace.clone();
        let column_name = column_name.clone();
        let path = path.clone();

        // Spawn background thread for VSS setup
        std::thread::spawn(move || {
            if let Err(e) = perform_indexing(&workspace, path, column_name, vector_length) {
                log::error!("Error in background indexing thread: {e}");
            }
        });
    } else {
        perform_indexing(workspace, path, column_name, vector_length)?;
    }

    Ok(())
}

fn get_embedding_length(
    workspace: &Workspace,
    path: impl AsRef<Path>,
    column: impl AsRef<str>,
) -> Result<usize, OxenError> {
    let path = path.as_ref();
    let column = column.as_ref();
    let db_path = repositories::workspaces::data_frames::duckdb_path(workspace, path);
    log::debug!("Embedding index DB Path: {db_path:?}");
    let result_set = with_df_db_manager(&db_path, |manager| {
        manager.with_conn(|conn| {
            // Make sure the existing column is a float vector
            let sql = format!("SELECT {column} FROM df LIMIT 1;");
            let result_set: Vec<RecordBatch> = conn.prepare(&sql)?.query_arrow([])?.collect();
            Ok(result_set)
        })
    })?;
    let Some(item) = result_set.first() else {
        return Err(OxenError::basic_str("No items found"));
    };
    let first_column = item.column(0);
    log::debug!("First column: {first_column:?}");

    // Check if the column is a list of floats/doubles
    let vector_length = match first_column.data_type() {
        arrow::datatypes::DataType::List(field) => match field.data_type() {
            arrow::datatypes::DataType::Float32 => {
                let array = first_column
                    .as_any()
                    .downcast_ref::<ListArray>()
                    .ok_or_else(|| OxenError::basic_str("Failed to downcast to ListArray"))?;
                if let Some(first_value) = array.value(0).as_any().downcast_ref::<Float32Array>() {
                    first_value.len()
                } else {
                    return Err(OxenError::basic_str(
                        "Expected Float32Array inside ListArray",
                    ));
                }
            }
            arrow::datatypes::DataType::Float64 => {
                let array = first_column
                    .as_any()
                    .downcast_ref::<ListArray>()
                    .ok_or_else(|| OxenError::basic_str("Failed to downcast to ListArray"))?;
                if let Some(first_value) = array.value(0).as_any().downcast_ref::<Float64Array>() {
                    first_value.len()
                } else {
                    return Err(OxenError::basic_str(
                        "Expected Float64Array inside ListArray",
                    ));
                }
            }
            _ => {
                return Err(OxenError::basic_str(
                    "Column must be a list of float32 or float64",
                ));
            }
        },
        arrow::datatypes::DataType::FixedSizeList(field, size) => match field.data_type() {
            arrow::datatypes::DataType::Float32 => *size as usize,
            _ => {
                return Err(OxenError::basic_str(
                    "Column FixedSizeList must be a float32 type",
                ));
            }
        },
        _ => return Err(OxenError::basic_str("Column must be a list type")),
    };

    log::debug!("Vector length: {vector_length}");
    // Write the vector length to a file we can use in the query
    write_embedding_size_to_config(workspace, path, column, vector_length)?;
    Ok(vector_length)
}

pub fn embedding_from_query(
    conn: &duckdb::Connection,
    workspace: &Workspace,
    path: impl AsRef<Path>,
    query: &EmbeddingQueryOpts,
) -> Result<(Vec<f32>, usize), OxenError> {
    let path = path.as_ref();
    let column = query.column.clone();
    let query = query.query.clone();
    let sql = format!("SELECT {column} FROM df WHERE {query};");
    log::debug!("Executing: {sql}");
    let result_set: Vec<RecordBatch> = conn.prepare(&sql)?.query_arrow([])?.collect();
    // log::debug!("Result set: {:?}", result_set);

    // Read the vector length from the file we wrote in the index function
    let Ok(config) = embedding_config(workspace, path) else {
        return Err(OxenError::basic_str(
            "Must index embeddings before querying",
        ));
    };
    let vector_length = config.columns[&column].vector_length;
    // log::debug!("Vector length: {}", vector_length);
    // Average the embeddings
    let avg_embedding = get_avg_embedding(result_set)?;
    Ok((avg_embedding, vector_length))
}

/// Helper function that contains the common logic for building similarity query SQL
fn build_similarity_query_sql(
    column: &str,
    similarity_column: &str,
    avg_embedding: &[f32],
    vector_length: usize,
    schema: &crate::model::data_frame::schema::Schema,
    exclude_cols: bool,
) -> Result<String, OxenError> {
    let embedding_str = format!(
        "[{}]",
        avg_embedding
            .iter()
            .map(|x| x.to_string())
            .collect::<Vec<String>>()
            .join(",")
    );

    let columns = schema
        .fields
        .iter()
        .map(|f| f.name.as_str())
        .filter(|c| !(EXCLUDE_OXEN_COLS.contains(c) && exclude_cols))
        .collect::<Vec<&str>>();

    let columns_str = columns.join(", ");
    let sql = format!(
        "SELECT {columns_str}, array_cosine_similarity({column}, {embedding_str}::FLOAT[{vector_length}]) as {similarity_column} FROM df ORDER BY {similarity_column} DESC"
    );
    Ok(sql)
}

pub fn similarity_query(
    workspace: &Workspace,
    opts: &EmbeddingQueryOpts,
    exclude_cols: bool,
) -> Result<String, OxenError> {
    let column = opts.column.clone();
    let path = opts.path.clone();
    let similarity_column = opts.name.clone();

    let db_path = repositories::workspaces::data_frames::duckdb_path(workspace, &path);
    log::debug!("Embedding query DB Path: {db_path:?}");
    let (avg_embedding, vector_length) = with_df_db_manager(&db_path, |manager| {
        manager.with_conn(|conn| embedding_from_query(conn, workspace, path.clone(), opts))
    })?;

    let schema = with_df_db_manager(&db_path, |manager| {
        manager.with_conn(|conn| df_db::get_schema(conn, TABLE_NAME))
    })?;

    build_similarity_query_sql(
        &column,
        &similarity_column,
        &avg_embedding,
        vector_length,
        &schema,
        exclude_cols,
    )
}

/// Version of similarity_query that accepts a connection to avoid deadlock issues
pub fn similarity_query_with_conn(
    conn: &duckdb::Connection,
    workspace: &Workspace,
    opts: &EmbeddingQueryOpts,
    exclude_cols: bool,
) -> Result<String, OxenError> {
    let column = opts.column.clone();
    let path = opts.path.clone();
    let similarity_column = opts.name.clone();

    let (avg_embedding, vector_length) = embedding_from_query(conn, workspace, path.clone(), opts)?;
    let schema = df_db::get_schema(conn, TABLE_NAME)?;

    build_similarity_query_sql(
        &column,
        &similarity_column,
        &avg_embedding,
        vector_length,
        &schema,
        exclude_cols,
    )
}

pub fn nearest_neighbors(
    workspace: &Workspace,
    path: impl AsRef<Path>,
    column: impl AsRef<str>,
    embedding: Vec<f32>,
    pagination: &PaginateOpts,
    exclude_cols: bool,
) -> Result<DataFrame, OxenError> {
    // Time the query
    let start = std::time::Instant::now();
    let db_path = repositories::workspaces::data_frames::duckdb_path(workspace, &path);

    let column = column.as_ref();
    let vector_length = embedding.len();
    let similarity_column = "similarity";
    let (result_set, mut schema) = with_df_db_manager(&db_path, |manager| {
        manager.with_conn(|conn| {
            let schema = df_db::get_schema(conn, TABLE_NAME)?;

            // Build base SQL using helper function
            let base_sql = build_similarity_query_sql(
                column,
                similarity_column,
                &embedding,
                vector_length,
                &schema,
                exclude_cols,
            )?;

            // Add pagination
            let limit = pagination.page_size;
            let page_num = if pagination.page_num > 0 {
                pagination.page_num
            } else {
                1
            };
            let offset = (page_num - 1) * limit;
            let sql = format!("{base_sql} LIMIT {limit} OFFSET {offset}");

            // Print just the first 50 characters of the query
            log::debug!("Executing similarity query: {}", &sql);

            let result_set: Vec<RecordBatch> = conn.prepare(&sql)?.query_arrow([])?.collect();
            Ok((result_set, schema))
        })
    })?;
    log::debug!("Similarity query took: {:?}", start.elapsed());

    schema.fields.push(Field::new(similarity_column, "f32"));

    let start = std::time::Instant::now();
    log::debug!("Serializing similarity query to Polars");
    let df = df_db::record_batches_to_polars_df(result_set)?;
    log::debug!(
        "Serializing similarity query to Polars took: {:?}",
        start.elapsed()
    );
    Ok(df)
}

/// Helper function that contains the common logic for executing similarity queries
fn execute_similarity_query(
    conn: &duckdb::Connection,
    sql: &str,
    similarity_column: &str,
) -> Result<(Vec<RecordBatch>, crate::model::data_frame::schema::Schema), OxenError> {
    let result_set: Vec<RecordBatch> = conn.prepare(sql)?.query_arrow([])?.collect();
    let mut schema = df_db::get_schema(conn, TABLE_NAME)?;
    schema.fields.push(Field::new(similarity_column, "f32"));
    Ok((result_set, schema))
}

/// Version of query that accepts a connection to avoid deadlock issues
pub fn query_with_conn(
    conn: &duckdb::Connection,
    workspace: &Workspace,
    opts: &EmbeddingQueryOpts,
) -> Result<DataFrame, OxenError> {
    let similarity_column = opts.name.clone();

    // Get the base SQL using the connection
    let mut sql = similarity_query_with_conn(conn, workspace, opts, false)?;

    // Add LIMIT to the query, otherwise it will be slow to deserialize
    let limit = opts.pagination.page_size;
    let page_num = if opts.pagination.page_num > 0 {
        opts.pagination.page_num
    } else {
        1
    };
    let offset = (page_num - 1) * limit;
    sql = format!("{sql} LIMIT {limit} OFFSET {offset}");

    // Print just the first 50 characters of the query
    log::debug!("Executing similarity query: {}", &sql[..50]);

    // Time the query
    let start = std::time::Instant::now();
    let (result_set, _schema) = execute_similarity_query(conn, &sql, &similarity_column)?;
    log::debug!("Similarity query took: {:?}", start.elapsed());

    let start = std::time::Instant::now();
    log::debug!("Serializing similarity query to Polars");
    let df = df_db::record_batches_to_polars_df(result_set)?;
    log::debug!(
        "Serializing similarity query to Polars took: {:?}",
        start.elapsed()
    );
    Ok(df)
}

pub fn query(workspace: &Workspace, opts: &EmbeddingQueryOpts) -> Result<DataFrame, OxenError> {
    let path = opts.path.clone();
    let similarity_column = opts.name.clone();

    let db_path = repositories::workspaces::data_frames::duckdb_path(workspace, &path);
    let mut sql = with_df_db_manager(&db_path, |manager| {
        manager.with_conn(|conn| similarity_query_with_conn(conn, workspace, opts, false))
    })?;

    // Add LIMIT to the query, otherwise it will be slow to deserialize
    let limit = opts.pagination.page_size;
    let page_num = if opts.pagination.page_num > 0 {
        opts.pagination.page_num
    } else {
        1
    };
    let offset = (page_num - 1) * limit;
    sql = format!("{sql} LIMIT {limit} OFFSET {offset}");

    // Print just the first 50 characters of the query
    log::debug!("Executing similarity query: {}", &sql[..50]);
    // Time the query
    let start = std::time::Instant::now();
    let (result_set, _schema) = with_df_db_manager(&db_path, |manager| {
        manager.with_conn(|conn| execute_similarity_query(conn, &sql, &similarity_column))
    })?;
    log::debug!("Similarity query took: {:?}", start.elapsed());

    let start = std::time::Instant::now();
    log::debug!("Serializing similarity query to Polars");
    let df = df_db::record_batches_to_polars_df(result_set)?;
    log::debug!(
        "Serializing similarity query to Polars took: {:?}",
        start.elapsed()
    );
    Ok(df)
}

fn get_avg_embedding(result_set: Vec<RecordBatch>) -> Result<Vec<f32>, OxenError> {
    let mut embeddings: Vec<Vec<f32>> = Vec::new();
    let mut vector_length = 0;
    for batch in result_set {
        let first_column = batch.column(0);
        match first_column.data_type() {
            arrow::datatypes::DataType::List(field) => match field.data_type() {
                arrow::datatypes::DataType::Float32 => {
                    let array = first_column
                        .as_any()
                        .downcast_ref::<ListArray>()
                        .ok_or_else(|| OxenError::basic_str("Failed to downcast to ListArray"))?;
                    if let Some(first_value) =
                        array.value(0).as_any().downcast_ref::<Float32Array>()
                    {
                        embeddings.push(first_value.values().to_vec());
                        if vector_length == 0 {
                            vector_length = first_value.len();
                        } else if first_value.len() != vector_length {
                            return Err(OxenError::basic_str(
                                "All embeddings must be the same length",
                            ));
                        }
                    } else {
                        return Err(OxenError::basic_str(
                            "Expected Float32Array inside ListArray",
                        ));
                    }
                }
                _ => {
                    return Err(OxenError::basic_str(
                        "Expected arrow::datatypes::DataType::Float32 inside List",
                    ));
                }
            },
            arrow::datatypes::DataType::FixedSizeList(field, _) => match field.data_type() {
                arrow::datatypes::DataType::Float32 => {
                    let array = first_column
                        .as_any()
                        .downcast_ref::<FixedSizeListArray>()
                        .ok_or_else(|| {
                            OxenError::basic_str("Failed to downcast to FixedSizeListArray")
                        })?;
                    if let Some(first_value) =
                        array.value(0).as_any().downcast_ref::<Float32Array>()
                    {
                        embeddings.push(first_value.values().to_vec());
                        if vector_length == 0 {
                            vector_length = first_value.len();
                        } else if first_value.len() != vector_length {
                            return Err(OxenError::basic_str(
                                "All embeddings must be the same length",
                            ));
                        }
                    }
                }
                _ => {
                    return Err(OxenError::basic_str(
                        "Column FixedSizeList must be a float32 type",
                    ));
                }
            },
            _ => {
                return Err(OxenError::basic_str(
                    "Expected arrow::datatypes::DataType::List inside as data type",
                ));
            }
        }
    }

    if embeddings.is_empty() {
        return Err(OxenError::NoRowsFound);
    }

    if vector_length == 0 {
        return Err(OxenError::basic_str(
            "Vector's must have a length greater than 0",
        ));
    }

    // Average the embeddings along the columns
    let mut avg_embedding = vec![0.0; vector_length];
    for i in 0..vector_length {
        let sum: f32 = embeddings.iter().map(|v| v[i]).sum();
        avg_embedding[i] = sum / embeddings.len() as f32;
    }

    Ok(avg_embedding)
}