algeon-datafusion 0.1.2

DataFusion physical-plan adapter for native GPU execution.
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
use datafusion::sql::sqlparser::{
    ast::{
        Expr as SqlExpr, Ident, OrderByKind, Query as SqlQuery, Select, SelectItem, SetExpr, Statement, TableFactor,
        Value, ValueWithSpan,
    },
    dialect::GenericDialect,
    parser::Parser,
};

use super::{
    CanonicalField, DataType, ParityComparisonMode, ParityFloatTolerance, ParityStatus, RecordBatch, Schema,
    collect_canonical_rows, compare_canonical_rows, fingerprint_rows, fingerprint_schema_only,
};

/// Result of comparing CPU and native Arrow outputs.
#[derive(Debug, Clone)]
pub struct ParityComparisonResult {
    /// Match, mismatch, or skip status.
    pub status: ParityStatus,
    /// Row-order and float-tolerance mode used for comparison.
    pub mode: ParityComparisonMode,
    /// Mismatch or skip detail when available.
    pub detail: Option<String>,
    /// Stable diagnostic hash of canonical CPU output.
    pub cpu_fingerprint: String,
    /// Stable diagnostic hash of canonical native output.
    pub native_fingerprint: String,
}

/// Compares CPU and native Arrow output using TPC-DS parity rules.
///
/// Schema differences are reported before row comparison. When `ordered` is
/// false, canonical rows are sorted before comparison. Float tolerance applies
/// only to float columns and is recorded in the returned comparison mode.
pub fn compare_record_batches(
    cpu_schema: &Schema,
    cpu_batches: &[RecordBatch],
    native_schema: &Schema,
    native_batches: &[RecordBatch],
    ordered: bool,
    tolerance: ParityFloatTolerance,
) -> ParityComparisonResult {
    compare_record_batches_with_options(
        cpu_schema,
        cpu_batches,
        native_schema,
        native_batches,
        &if ordered { ParityRowOrdering::Ordered } else { ParityRowOrdering::Unordered },
        tolerance,
        ParityCompareOptions::strict_native(),
    )
}

/// Schema comparison switches for native-vs-native and cross-engine oracles.
#[derive(Debug, Clone, Copy)]
pub(super) struct ParityCompareOptions {
    pub(super) allow_decimal_precision_mismatch: bool,
}

impl ParityCompareOptions {
    pub(super) const fn strict_native() -> Self {
        Self { allow_decimal_precision_mismatch: false }
    }

    pub(super) const fn cross_engine_oracle() -> Self {
        Self { allow_decimal_precision_mismatch: true }
    }
}

pub(super) enum ParityRowOrdering {
    Unordered,
    Ordered,
    OrderBy(Vec<ParitySortKey>),
}

pub(super) struct ParitySortKey {
    column: usize,
    descending: bool,
}

pub(super) fn sql_row_ordering(sql: &str, schema: Option<&Schema>) -> ParityRowOrdering {
    let Some(query) = parse_query(sql) else {
        return ParityRowOrdering::Ordered;
    };
    if !query.pipe_operators.is_empty() {
        return ParityRowOrdering::Ordered;
    }
    if query.order_by.is_none() {
        return ParityRowOrdering::Unordered;
    }
    schema
        .and_then(|schema| order_by_keys(&query, schema))
        .map_or(ParityRowOrdering::Ordered, ParityRowOrdering::OrderBy)
}

pub(super) fn compare_record_batches_with_options(
    cpu_schema: &Schema,
    cpu_batches: &[RecordBatch],
    native_schema: &Schema,
    native_batches: &[RecordBatch],
    ordering: &ParityRowOrdering,
    tolerance: ParityFloatTolerance,
    options: ParityCompareOptions,
) -> ParityComparisonResult {
    let schema_diff = compare_schemas(cpu_schema, native_schema, options);
    let float_tolerant = tolerance.is_enabled() && schema_has_float_column(cpu_schema);
    let mode = ParityComparisonMode::select(!matches!(ordering, ParityRowOrdering::Unordered), float_tolerant);

    if let Some(detail) = schema_diff {
        return ParityComparisonResult {
            status: ParityStatus::Mismatched,
            mode,
            detail: Some(detail),
            cpu_fingerprint: fingerprint_schema_only(cpu_schema),
            native_fingerprint: fingerprint_schema_only(native_schema),
        };
    }

    let cpu_rows = match collect_canonical_rows(cpu_schema, cpu_batches) {
        Ok(rows) => rows,
        Err(detail) => {
            return ParityComparisonResult {
                status: ParityStatus::NotCompared,
                mode,
                detail: Some(format!("cpu canonicalization failed: {detail}")),
                cpu_fingerprint: fingerprint_schema_only(cpu_schema),
                native_fingerprint: fingerprint_schema_only(native_schema),
            };
        },
    };
    let mut native_rows = match collect_canonical_rows(native_schema, native_batches) {
        Ok(rows) => rows,
        Err(detail) => {
            return ParityComparisonResult {
                status: ParityStatus::NotCompared,
                mode,
                detail: Some(format!("native canonicalization failed: {detail}")),
                cpu_fingerprint: fingerprint_rows(cpu_schema, &cpu_rows),
                native_fingerprint: fingerprint_schema_only(native_schema),
            };
        },
    };
    let mut cpu_rows = cpu_rows;
    match ordering {
        ParityRowOrdering::Unordered => {
            cpu_rows.sort();
            native_rows.sort();
        },
        ParityRowOrdering::Ordered => {},
        ParityRowOrdering::OrderBy(keys) => {
            sort_tied_rows(&mut cpu_rows, keys);
            sort_tied_rows(&mut native_rows, keys);
        },
    }

    let cpu_fingerprint = fingerprint_rows(cpu_schema, &cpu_rows);
    let native_fingerprint = fingerprint_rows(cpu_schema, &native_rows);

    if cpu_rows.len() != native_rows.len() {
        return ParityComparisonResult {
            status: ParityStatus::Mismatched,
            mode,
            detail: Some(format!("row count differs: cpu={} native={}", cpu_rows.len(), native_rows.len())),
            cpu_fingerprint,
            native_fingerprint,
        };
    }

    for (index, (cpu_row, native_row)) in cpu_rows.iter().zip(native_rows.iter()).enumerate() {
        if let Some(detail) =
            compare_canonical_rows(cpu_schema, cpu_row, native_row, tolerance, float_tolerant, options)
        {
            return ParityComparisonResult {
                status: ParityStatus::Mismatched,
                mode,
                detail: Some(format!("row {index}: {detail}")),
                cpu_fingerprint,
                native_fingerprint,
            };
        }
    }

    if float_tolerant
        && let ParityRowOrdering::OrderBy(keys) = ordering
        && let Some((row, column)) = float_key_inversion(&native_rows, keys)
    {
        return ParityComparisonResult {
            status: ParityStatus::Mismatched,
            mode,
            detail: Some(format!(
                "row {row}: ORDER BY column {column} ({}) is out of order",
                cpu_schema.field(column).name()
            )),
            cpu_fingerprint,
            native_fingerprint,
        };
    }

    ParityComparisonResult { status: ParityStatus::Matched, mode, detail: None, cpu_fingerprint, native_fingerprint }
}

fn sort_tied_rows(rows: &mut [Vec<CanonicalField>], keys: &[ParitySortKey]) {
    let mut start = 0;
    while start < rows.len() {
        let mut end = start + 1;
        // Approximate float equality is not transitive and cannot define a tie group.
        while end < rows.len()
            && keys.iter().all(|key| {
                !matches!(rows[start][key.column], CanonicalField::Unsupported(_))
                    && rows[start][key.column] == rows[end][key.column]
            })
        {
            end += 1;
        }
        rows[start..end].sort();
        start = end;
    }
}

fn float_key_inversion(rows: &[Vec<CanonicalField>], keys: &[ParitySortKey]) -> Option<(usize, usize)> {
    // Value comparison fixes non-float key sequences exactly. Float tolerance can still admit an inversion.
    for (index, pair) in rows.windows(2).enumerate() {
        for key in keys {
            let before = &pair[0][key.column];
            let after = &pair[1][key.column];
            if matches!(before, CanonicalField::Unsupported(_)) {
                break;
            }
            if before == after {
                continue;
            }
            if let (CanonicalField::Float { width, bits: before }, CanonicalField::Float { bits: after, .. }) =
                (before, after)
            {
                let order = match width {
                    32 => f32::from_bits(*before as u32).total_cmp(&f32::from_bits(*after as u32)),
                    _ => f64::from_bits(*before).total_cmp(&f64::from_bits(*after)),
                };
                if (key.descending && order.is_lt()) || (!key.descending && order.is_gt()) {
                    return Some((index + 1, key.column));
                }
            }
            break;
        }
    }
    None
}

pub(super) fn compare_schemas(cpu: &Schema, native: &Schema, options: ParityCompareOptions) -> Option<String> {
    if cpu.fields().len() != native.fields().len() {
        return Some(format!(
            "schema field count differs: cpu={} native={}",
            cpu.fields().len(),
            native.fields().len()
        ));
    }
    for (index, (cpu_field, native_field)) in cpu.fields().iter().zip(native.fields().iter()).enumerate() {
        if !logical_data_types_compatible(cpu_field.data_type(), native_field.data_type(), options) {
            return Some(format!(
                "column {} ({}/{}) type differs: cpu={:?} native={:?}",
                index,
                cpu_field.name(),
                native_field.name(),
                cpu_field.data_type(),
                native_field.data_type(),
            ));
        }
    }
    None
}

pub(super) fn logical_data_types_compatible(cpu: &DataType, native: &DataType, options: ParityCompareOptions) -> bool {
    if cpu == native {
        return true;
    }
    if options.allow_decimal_precision_mismatch {
        match (cpu, native) {
            (DataType::Decimal128(_, cpu_scale), DataType::Decimal128(_, native_scale))
            | (DataType::Decimal256(_, cpu_scale), DataType::Decimal256(_, native_scale))
                if cpu_scale == native_scale =>
            {
                return true;
            },
            _ => {},
        }
    }
    matches!(
        (cpu, native),
        (DataType::Utf8, DataType::Utf8View)
            | (DataType::Utf8View, DataType::Utf8)
            | (DataType::LargeUtf8, DataType::Utf8)
            | (DataType::Utf8, DataType::LargeUtf8)
            | (DataType::LargeUtf8, DataType::Utf8View)
            | (DataType::Utf8View, DataType::LargeUtf8)
            | (DataType::Binary, DataType::BinaryView)
            | (DataType::BinaryView, DataType::Binary)
            | (DataType::LargeBinary, DataType::Binary)
            | (DataType::Binary, DataType::LargeBinary)
            | (DataType::LargeBinary, DataType::BinaryView)
            | (DataType::BinaryView, DataType::LargeBinary)
    )
}

pub(super) fn schema_has_float_column(schema: &Schema) -> bool {
    schema.fields().iter().any(|field| matches!(field.data_type(), DataType::Float32 | DataType::Float64))
}

/// Detect whether the given SQL has a top-level `ORDER BY` clause.
///
/// Unparseable SQL is conservatively treated as ordered. Ordering inside a
/// derived table, window, string or comment does not order the outer result.
pub fn sql_has_top_level_order_by(sql: &str) -> bool {
    parse_query(sql).is_none_or(|query| query.order_by.is_some() || !query.pipe_operators.is_empty())
}

/// Returns true when every output column is a top-level `ORDER BY` key.
///
/// Keys may refer to output names, projected expressions or 1-based positions.
/// Unresolved keys return false. Partial lists may still determine a total
/// order through uniqueness constraints, which are not available here.
pub fn sql_top_level_order_by_covers_output_columns(sql: &str, schema: &Schema) -> bool {
    parse_query(sql)
        .and_then(|query| order_by_keys(&query, schema))
        .is_some_and(|keys| (0..schema.fields().len()).all(|index| keys.iter().any(|key| key.column == index)))
}

fn parse_query(sql: &str) -> Option<Box<SqlQuery>> {
    let mut statements = Parser::parse_sql(&GenericDialect, sql).ok()?;
    if statements.len() != 1 {
        return None;
    }
    let Statement::Query(mut query) = statements.pop()? else {
        return None;
    };
    // Parentheses around the whole query preserve its result ordering.
    while query.order_by.is_none() && query.pipe_operators.is_empty() {
        let SetExpr::Query(inner) = query.body.as_ref() else {
            break;
        };
        query = inner.clone();
    }
    Some(query)
}

fn order_by_keys(query: &SqlQuery, schema: &Schema) -> Option<Vec<ParitySortKey>> {
    let order_by = query.order_by.as_ref()?;
    if order_by.interpolate.is_some() || !query.pipe_operators.is_empty() {
        return None;
    }
    let OrderByKind::Expressions(keys) = &order_by.kind else {
        return None;
    };
    if keys.is_empty() {
        return None;
    }
    keys.iter()
        .map(|key| {
            if key.with_fill.is_some() {
                return None;
            }
            resolve_order_by_column(&key.expr, query, schema)
                .map(|column| ParitySortKey { column, descending: !key.options.asc.unwrap_or(true) })
        })
        .collect()
}

fn resolve_order_by_column(expr: &SqlExpr, query: &SqlQuery, schema: &Schema) -> Option<usize> {
    match expr {
        SqlExpr::Value(ValueWithSpan { value: Value::Number(position, _), .. }) => {
            return position.parse::<usize>().ok()?.checked_sub(1).filter(|&index| index < schema.fields().len());
        },
        SqlExpr::Identifier(ident) => {
            let mut matches =
                schema.fields().iter().enumerate().filter(|(_, field)| identifier_matches(ident, field.name()));
            if let Some((index, _)) = matches.next() {
                return matches.next().is_none().then_some(index);
            }
        },
        _ => {},
    }
    let SetExpr::Select(select) = query.body.as_ref() else {
        return None;
    };
    // Wildcard expansion changes projection positions, so they cannot index the schema.
    if select.projection.len() != schema.fields().len()
        || select
            .projection
            .iter()
            .any(|item| !matches!(item, SelectItem::UnnamedExpr(_) | SelectItem::ExprWithAlias { .. }))
    {
        return None;
    }
    let unqualified = match expr {
        SqlExpr::CompoundIdentifier(parts) if parts.len() == 2 && is_single_table_qualifier(&parts[0], select) => {
            Some(SqlExpr::Identifier(parts[1].clone()))
        },
        _ => None,
    };
    select.projection.iter().position(|item| {
        let projected = match item {
            SelectItem::UnnamedExpr(expr) | SelectItem::ExprWithAlias { expr, .. } => expr,
            _ => return false,
        };
        projected == expr || unqualified.as_ref().is_some_and(|expr| projected == expr)
    })
}

fn identifier_matches(ident: &Ident, name: &str) -> bool {
    if ident.quote_style.is_some() { ident.value == name } else { ident.value.to_ascii_lowercase() == name }
}

fn is_single_table_qualifier(qualifier: &Ident, select: &Select) -> bool {
    let [table] = select.from.as_slice() else {
        return false;
    };
    if !table.joins.is_empty() {
        return false;
    }
    let TableFactor::Table { name, alias, .. } = &table.relation else {
        return false;
    };
    if let Some(alias) = alias {
        return qualifier == &alias.name;
    }
    name.0.last().and_then(|part| part.as_ident()).is_some_and(|name| name == qualifier)
}