|
1 | | -use datafusion::{prelude::Expr, sql::unparser::expr_to_sql}; |
| 1 | +use datafusion::{ |
| 2 | + arrow::array::RecordBatch, common::utils::get_row_at_idx, prelude::Expr, scalar::ScalarValue, |
| 3 | + sql::unparser::expr_to_sql, |
| 4 | +}; |
| 5 | +use std::collections::HashMap; |
2 | 6 |
|
3 | | -use super::Result; |
| 7 | +use super::{Result, fuzzer_err}; |
4 | 8 |
|
5 | 9 | /// Convert a DataFusion `Expr` into a SQL string using DataFusion's unparser. |
6 | 10 | pub fn to_sql_string(expr: &Expr) -> Result<String> { |
7 | 11 | let unparsed = expr_to_sql(expr)?; |
8 | 12 | Ok(unparsed.to_string()) |
9 | 13 | } |
| 14 | + |
| 15 | +pub(crate) type RowMultiset = HashMap<Vec<ScalarValue>, usize>; |
| 16 | + |
| 17 | +pub(crate) fn batches_to_row_multiset(batches: &[RecordBatch]) -> Result<RowMultiset> { |
| 18 | + let mut multiset: RowMultiset = HashMap::new(); |
| 19 | + let mut expected_num_cols: Option<usize> = None; |
| 20 | + |
| 21 | + for batch in batches { |
| 22 | + if let Some(expected) = expected_num_cols { |
| 23 | + if batch.num_columns() != expected { |
| 24 | + return Err(fuzzer_err(&format!( |
| 25 | + "Mismatched column count across batches: expected {}, got {}", |
| 26 | + expected, |
| 27 | + batch.num_columns() |
| 28 | + ))); |
| 29 | + } |
| 30 | + } else { |
| 31 | + expected_num_cols = Some(batch.num_columns()); |
| 32 | + } |
| 33 | + |
| 34 | + for row_idx in 0..batch.num_rows() { |
| 35 | + let mut row_key = get_row_at_idx(batch.columns(), row_idx) |
| 36 | + .map_err(|e| fuzzer_err(&format!("Failed to extract row {}: {}", row_idx, e)))?; |
| 37 | + row_key.iter_mut().for_each(|v| *v = v.clone().compacted()); |
| 38 | + *multiset.entry(row_key).or_insert(0) += 1; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + Ok(multiset) |
| 43 | +} |
| 44 | + |
| 45 | +pub(crate) fn format_row_multiset_diff(left: &RowMultiset, right: &RowMultiset) -> String { |
| 46 | + let mut lines = Vec::new(); |
| 47 | + for (row, left_count) in left { |
| 48 | + let right_count = right.get(row).copied().unwrap_or(0); |
| 49 | + if *left_count != right_count { |
| 50 | + lines.push(format!( |
| 51 | + "row={:?}, left_count={}, right_count={}", |
| 52 | + row, left_count, right_count |
| 53 | + )); |
| 54 | + } |
| 55 | + } |
| 56 | + for (row, right_count) in right { |
| 57 | + if !left.contains_key(row) { |
| 58 | + lines.push(format!( |
| 59 | + "row={:?}, left_count=0, right_count={}", |
| 60 | + row, right_count |
| 61 | + )); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + lines.sort(); |
| 66 | + let preview = lines.into_iter().take(20).collect::<Vec<_>>(); |
| 67 | + if preview.is_empty() { |
| 68 | + "no row differences".to_string() |
| 69 | + } else { |
| 70 | + preview.join("\n") |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +pub(crate) fn count_total_rows(batches: &[RecordBatch]) -> usize { |
| 75 | + batches.iter().map(RecordBatch::num_rows).sum() |
| 76 | +} |
| 77 | + |
| 78 | +pub(crate) fn validate_batches_value_equivalence( |
| 79 | + left_batches: &[RecordBatch], |
| 80 | + right_batches: &[RecordBatch], |
| 81 | + oracle_name: &str, |
| 82 | +) -> Result<()> { |
| 83 | + let left_multiset = batches_to_row_multiset(left_batches)?; |
| 84 | + let right_multiset = batches_to_row_multiset(right_batches)?; |
| 85 | + |
| 86 | + if left_multiset != right_multiset { |
| 87 | + return Err(fuzzer_err(&format!( |
| 88 | + "{} value equivalence violated:\n{}", |
| 89 | + oracle_name, |
| 90 | + format_row_multiset_diff(&left_multiset, &right_multiset) |
| 91 | + ))); |
| 92 | + } |
| 93 | + |
| 94 | + Ok(()) |
| 95 | +} |
0 commit comments