liboxen 0.50.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
use std::collections::{HashMap, HashSet};

use crate::constants::DIFF_STATUS_COL;
use crate::error::OxenError;
use crate::model::Schema;
use crate::model::data_frame::schema::Field;
use crate::model::diff::tabular_diff::{
    TabularDiffDupes, TabularDiffMods, TabularDiffParameters, TabularDiffSchemas,
    TabularDiffSummary, TabularSchemaDiff,
};
use crate::model::diff::{AddRemoveModifyCounts, TabularDiff};
use crate::view::compare::{
    TabularCompareFieldBody, TabularCompareFields, TabularCompareTargetBody,
};

use polars::chunked_array::ops::SortMultipleOptions;
use polars::datatypes::AnyValue;
use polars::lazy::dsl::coalesce;
use polars::lazy::dsl::{GetOutput, all, as_struct, col};
use polars::lazy::frame::IntoLazy;
use polars::prelude::ChunkCompareEq;
use polars::prelude::PlSmallStr;
use polars::prelude::SchemaExt;
use polars::prelude::{Column, NamedFrom};
use polars::prelude::{DataFrame, DataFrameJoinOps};
use polars::series::Series;

use super::{SchemaDiff, tabular};

const TARGETS_HASH_COL: &str = "_targets_hash";
const KEYS_HASH_COL: &str = "_keys_hash";

const DIFF_STATUS_ADDED: &str = "added";
const DIFF_STATUS_REMOVED: &str = "removed";
const DIFF_STATUS_MODIFIED: &str = "modified";
const DIFF_STATUS_UNCHANGED: &str = "unchanged";

pub fn diff(
    df_1: &DataFrame,
    df_2: &DataFrame,
    schema_diff: SchemaDiff,
    keys: &[impl AsRef<str>],
    targets: &[impl AsRef<str>],
    display: &[impl AsRef<str>],
) -> Result<TabularDiff, OxenError> {
    if !targets.is_empty() && keys.is_empty() {
        let targets = targets.iter().map(|k| k.as_ref()).collect::<Vec<&str>>();
        return Err(OxenError::basic_str(format!(
            "Must specify at least one key column if specifying target columns. Targets: {targets:?}"
        )));
    }

    let keys: Vec<&str> = keys.iter().map(|k| k.as_ref()).collect();
    let targets: Vec<&str> = targets.iter().map(|k| k.as_ref()).collect();
    let display: Vec<&str> = display.iter().map(|k| k.as_ref()).collect();

    let output_columns = get_output_columns(
        &Schema::from_polars(df_2.schema()),
        keys.clone(),
        targets.clone(),
        display.clone(),
        schema_diff.clone(),
    );

    log::debug!("df_1 is {df_1:?}");
    log::debug!("df_2 is {df_2:?}");
    log::debug!("keys are {keys:?}");
    log::debug!("targets are {targets:?}");
    log::debug!("display are {display:?}");
    log::debug!("output_columns are {output_columns:?}");

    let joined_df = join_hashed_dfs(
        df_1,
        df_2,
        keys.clone(),
        targets.clone(),
        schema_diff.clone(),
    )?;

    log::debug!("joined_df is {joined_df:?}");

    let joined_df = add_diff_status_column(joined_df, keys.clone(), targets.clone())?;
    log::debug!("joined_df after add_diff_status_column is {joined_df:?}");
    let unchanged_vec = vec![DIFF_STATUS_UNCHANGED; joined_df.height()];
    let unchanged_series = Column::Series(
        Series::new(PlSmallStr::from_str(DIFF_STATUS_UNCHANGED), unchanged_vec).into(),
    );
    let mut joined_df = joined_df.filter(
        &joined_df
            .column(DIFF_STATUS_COL)?
            .not_equal(&unchanged_series)?,
    )?;
    log::debug!("joined_df after filter is {joined_df:?}");
    // Once we've joined and calculated group membership based on .left and .right nullity, coalesce keys
    for key in keys.clone() {
        joined_df = joined_df
            .lazy()
            .with_columns([
                coalesce(&[col(format!("{key}.right")), col(format!("{key}.left"))]).alias(key),
            ])
            .collect()?;
    }
    log::debug!("joined_df after coalesce is {joined_df:?}");
    let modifications = calculate_compare_mods(&joined_df)?;

    // Sort by all keys with primitive dtypes
    let joined_df = sort_df_on_keys(joined_df, keys.clone())?;

    let _result_fields =
        prepare_response_fields(&schema_diff, keys.clone(), targets.clone(), display.clone());

    let schema_diff = build_compare_schema_diff(schema_diff, df_1, df_2)?;

    let dupes = TabularDiffDupes {
        left: tabular::n_duped_rows(df_1, &[KEYS_HASH_COL])?,
        right: tabular::n_duped_rows(df_2, &[KEYS_HASH_COL])?,
    };

    let schemas = TabularDiffSchemas {
        left: Schema::from_polars(df_1.schema()),
        right: Schema::from_polars(df_2.schema()),
        diff: Schema::from_polars(joined_df.schema()),
    };

    let diff = TabularDiff {
        summary: TabularDiffSummary {
            modifications: TabularDiffMods {
                row_counts: modifications,
                col_changes: schema_diff,
            },
            schemas,
            dupes,
        },
        contents: joined_df.select(output_columns)?,
        parameters: TabularDiffParameters {
            keys: keys.iter().map(|s| s.to_string()).collect(),
            targets: targets.iter().map(|s| s.to_string()).collect(),
            display: display.iter().map(|s| s.to_string()).collect(),
        },
        filename1: None,
        filename2: None,
    };
    Ok(diff)
}

fn sort_df_on_keys(df: DataFrame, keys: Vec<&str>) -> Result<DataFrame, OxenError> {
    let mut sort_cols = vec![];
    for key in keys.iter() {
        if let Ok(col) = df.column(key)
            && col.dtype().is_primitive()
        {
            sort_cols.push(*key);
        }
    }

    if !sort_cols.is_empty() {
        return Ok(df.sort(
            sort_cols,
            SortMultipleOptions::new().with_order_descending(false),
        )?);
    }
    Ok(df)
}

fn build_compare_schema_diff(
    schema_diff: SchemaDiff,
    df_1: &DataFrame,
    df_2: &DataFrame,
) -> Result<TabularSchemaDiff, OxenError> {
    let added_cols = schema_diff
        .added_cols
        .iter()
        .map(|col| {
            let dtype = df_2.column(col)?;
            Ok(Field {
                name: col.clone(),
                dtype: dtype.dtype().to_string(),
                metadata: None,
                changes: None,
            })
        })
        .collect::<Result<Vec<Field>, OxenError>>()?;

    let removed_cols = schema_diff
        .removed_cols
        .iter()
        .map(|col| {
            let dtype = df_1.column(col)?;
            Ok(Field {
                name: col.clone(),
                dtype: dtype.dtype().to_string(),
                metadata: None,
                changes: None,
            })
        })
        .collect::<Result<Vec<Field>, OxenError>>()?;

    Ok(TabularSchemaDiff {
        added: added_cols,
        removed: removed_cols,
    })
}

fn get_output_columns(
    df2_schema: &Schema, // For consistent column ordering
    keys: Vec<&str>,
    targets: Vec<&str>,
    display: Vec<&str>,
    schema_diff: SchemaDiff,
) -> Vec<String> {
    // Keys in df2 order. Targets in df2 order, then any additional. Then display in df2, then any additional.
    let df2_cols_set: HashSet<&str> = df2_schema.fields.iter().map(|f| f.name.as_str()).collect();

    // Get the column index of each column in df2schema
    let mut col_indices: HashMap<&str, usize> = HashMap::new();
    for (i, col) in df2_schema.fields.iter().enumerate() {
        col_indices.insert(col.name.as_str(), i);
    }

    let mut out_columns = vec![];

    let ordered_keys = order_columns_by_schema(keys, &df2_cols_set, &col_indices);
    let ordered_targets = order_columns_by_schema(targets, &df2_cols_set, &col_indices);
    let ordered_display = order_columns_by_schema(display, &df2_cols_set, &col_indices);

    for key in ordered_keys.iter() {
        out_columns.push(key.to_string());
    }

    for target in ordered_targets.iter() {
        if schema_diff.added_cols.contains(&target.to_string()) {
            out_columns.push(format!("{target}.right"));
        } else if schema_diff.removed_cols.contains(&target.to_string()) {
            out_columns.push(format!("{target}.left"));
        } else {
            out_columns.push(format!("{target}.left"));
            out_columns.push(format!("{target}.right"))
        };
    }

    for col in ordered_display.iter() {
        if col.ends_with(".left") {
            let stripped = col.trim_end_matches(".left");
            if schema_diff.removed_cols.contains(&stripped.to_string())
                || schema_diff.unchanged_cols.contains(&stripped.to_string())
            {
                out_columns.push(col.to_string());
            }
        }
        if col.ends_with(".right") {
            let stripped = col.trim_end_matches(".right");
            if schema_diff.added_cols.contains(&stripped.to_string())
                || schema_diff.unchanged_cols.contains(&stripped.to_string())
            {
                out_columns.push(col.to_string());
            }
        }
    }

    out_columns.push(DIFF_STATUS_COL.to_string());
    out_columns
}

fn order_columns_by_schema<'a>(
    columns: Vec<&'a str>,
    df2_cols_set: &HashSet<&'a str>,
    col_indices: &HashMap<&'a str, usize>,
) -> Vec<&'a str> {
    let mut ordered_columns: Vec<&'a str> = columns
        .iter()
        .filter(|col| df2_cols_set.contains(*col))
        .cloned()
        .collect();

    ordered_columns.sort_by_key(|col| *col_indices.get(col).unwrap_or(&usize::MAX));

    ordered_columns
}

fn join_hashed_dfs(
    left_df: &DataFrame,
    right_df: &DataFrame,
    keys: Vec<&str>,
    targets: Vec<&str>,
    schema_diff: SchemaDiff,
) -> Result<DataFrame, OxenError> {
    log::debug!("left_df: {left_df:?}");
    log::debug!("right_df: {right_df:?}");

    let mut joined_df = left_df.full_join(right_df, [KEYS_HASH_COL], [KEYS_HASH_COL])?;
    log::debug!("joined_df: {joined_df:?}");

    let mut cols_to_rename = targets.clone();
    for key in keys.iter() {
        cols_to_rename.push(key);
    }
    // TODO: maybe set logic?
    for col in schema_diff.unchanged_cols.iter() {
        if !cols_to_rename.contains(&col.as_str()) {
            cols_to_rename.push(col);
        }
    }

    if !targets.is_empty() {
        cols_to_rename.push(TARGETS_HASH_COL);
    }

    for col in schema_diff.added_cols.iter() {
        if joined_df.schema().contains(col) {
            // TODO: it seems like a bug was introduced in Polars 0.46.0 that
            // broke rename. `lazy().rename(...).collect()` seems to work around
            // the issue. It might be fixed in Polars 0.47.1, but that version
            // introduces several async issues for us.
            // See: https://github.com/pola-rs/polars/pull/22380
            joined_df = joined_df
                .lazy()
                .rename([col.as_str()], [&format!("{col}.right")], false)
                .collect()?;
        }
    }

    for col in schema_diff.removed_cols.iter() {
        if joined_df.schema().contains(col) {
            joined_df = joined_df
                .lazy()
                .rename([col.as_str()], [&format!("{col}.left")], false)
                .collect()?;
        }
    }

    for target in cols_to_rename.iter() {
        let left_before = target.to_string();
        let left_after = format!("{target}.left");
        let right_before = format!("{target}_right");
        let right_after = format!("{target}.right");
        // Rename conditionally for asymetric targets
        if joined_df.schema().contains(&left_before) {
            joined_df = joined_df
                .lazy()
                .rename([&left_before], [&left_after], false)
                .collect()?;
        }
        if joined_df.schema().contains(&right_before) {
            joined_df = joined_df
                .lazy()
                .rename([&right_before], [&right_after], false)
                .collect()?;
        }
    }

    Ok(joined_df)
}

fn add_diff_status_column(
    joined_df: DataFrame,
    keys: Vec<&str>,
    targets: Vec<&str>,
) -> Result<DataFrame, OxenError> {
    // Columns required for determining group membership in the closure
    let col_names = if !keys.is_empty() {
        vec![
            format!("{}.left", keys[0]),
            format!("{}.right", keys[0]),
            format!("{}.left", TARGETS_HASH_COL),
            format!("{}.right", TARGETS_HASH_COL),
        ]
    } else {
        vec![
            format!("{}.left", TARGETS_HASH_COL),
            format!("{}.right", TARGETS_HASH_COL),
        ]
    };

    let mut field_names = vec![];
    for col_name in &col_names {
        if joined_df
            .schema()
            .iter_fields()
            .any(|field| field.name() == col_name)
        {
            field_names.push(col(col_name));
        }
    }

    // For pulling into the closure
    let has_targets = !targets.is_empty();
    let joined_df = joined_df
        .lazy()
        .select([
            all(),
            as_struct(field_names)
                .apply(
                    move |s| {
                        let ca = s.struct_()?;
                        let s_a = &ca.fields_as_series();

                        let num_rows = s_a[0].len();
                        let num_columns = s_a.len();
                        let mut results = vec![];
                        for i in 0..num_rows {
                            // log::debug!("row: {:?}", i);
                            let mut row = vec![];
                            for j_elem in s_a.iter().take(num_columns) {
                                let elem = j_elem.get(i).unwrap();
                                // log::debug!("  elem: {:?}", elem);
                                row.push(elem);
                            }
                            let key_left = row.first();
                            let key_right = row.get(1);
                            let target_hash_left = row.get(2);
                            let target_hash_right = row.get(3);

                            results.push(test_function(
                                key_left,
                                key_right,
                                target_hash_left,
                                target_hash_right,
                                has_targets,
                            ));
                        }
                        Ok(Some(Column::Series(
                            Series::new(PlSmallStr::from_str(""), results).into(),
                        )))
                    },
                    GetOutput::from_type(polars::prelude::DataType::String),
                )
                .alias(DIFF_STATUS_COL),
        ])
        .collect()?;

    Ok(joined_df)
}

fn calculate_compare_mods(joined_df: &DataFrame) -> Result<AddRemoveModifyCounts, OxenError> {
    // TODO: for reasons which are unclear to me it is ridiculously unclear how
    // to use the polars DSL to get the added, removed, and modified rows as a scalary without
    // filtering down into sub-dataframes or cloning them. This is a workaround for now.

    let mut added_rows = 0;
    let mut removed_rows = 0;
    let mut modified_rows = 0;

    for row in joined_df.column(DIFF_STATUS_COL)?.str()?.into_iter() {
        match row {
            Some("added") => added_rows += 1,
            Some("removed") => removed_rows += 1,
            Some("modified") => modified_rows += 1,
            _ => (),
        }
    }
    Ok(AddRemoveModifyCounts {
        added: added_rows,
        removed: removed_rows,
        modified: modified_rows,
    })
}

fn test_function(
    key_left: Option<&AnyValue>,
    key_right: Option<&AnyValue>,
    target_hash_left: Option<&AnyValue>,
    target_hash_right: Option<&AnyValue>,
    has_targets: bool,
) -> String {
    // TODO better error handling
    if let Some(AnyValue::Null) = key_left {
        return DIFF_STATUS_ADDED.to_string();
    }

    if let Some(AnyValue::Null) = key_right {
        return DIFF_STATUS_REMOVED.to_string();
    }

    if !has_targets {
        return DIFF_STATUS_UNCHANGED.to_string();
    }
    if let Some(target_hash_left) = target_hash_left
        && let Some(target_hash_right) = target_hash_right
        && target_hash_left != target_hash_right
    {
        return DIFF_STATUS_MODIFIED.to_string();
    }
    DIFF_STATUS_UNCHANGED.to_string()
}

fn prepare_response_fields(
    schema_diff: &SchemaDiff,
    keys: Vec<&str>,
    targets: Vec<&str>,
    display: Vec<&str>,
) -> TabularCompareFields {
    let res_keys = keys
        .iter()
        .map(|key| TabularCompareFieldBody {
            left: key.to_string(),
            right: key.to_string(),
            alias_as: None,
            compare_method: None,
        })
        .collect::<Vec<TabularCompareFieldBody>>();

    let mut res_targets: Vec<TabularCompareTargetBody> = vec![];

    for target in targets.iter() {
        if schema_diff.added_cols.contains(&target.to_string()) {
            res_targets.push(TabularCompareTargetBody {
                left: None,
                right: Some(target.to_string()),
                compare_method: None,
            });
        } else if schema_diff.removed_cols.contains(&target.to_string()) {
            res_targets.push(TabularCompareTargetBody {
                left: Some(target.to_string()),
                right: None,
                compare_method: None,
            });
        } else {
            res_targets.push(TabularCompareTargetBody {
                left: Some(target.to_string()),
                right: Some(target.to_string()),
                compare_method: None,
            });
        }
    }

    let mut res_display: Vec<TabularCompareTargetBody> = vec![];
    for disp in display.iter() {
        if schema_diff.added_cols.contains(&disp.to_string()) {
            res_display.push(TabularCompareTargetBody {
                left: None,
                right: Some(disp.to_string()),
                compare_method: None,
            });
        } else if schema_diff.removed_cols.contains(&disp.to_string()) {
            res_display.push(TabularCompareTargetBody {
                left: Some(disp.to_string()),
                right: None,
                compare_method: None,
            });
        } else {
            res_display.push(TabularCompareTargetBody {
                left: Some(disp.to_string()),
                right: Some(disp.to_string()),
                compare_method: None,
            });
        }
    }

    TabularCompareFields {
        keys: res_keys,
        targets: res_targets,
        display: res_display,
    }
}