dtcs 0.13.0

Reference implementation of the Data Transformation Contract Standard (DTCS)
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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
//! Runtime expression evaluation.

use crate::analysis::expr::ast::{BinaryOp, Expr, LiteralValue, UnaryOp};
use crate::analysis::expr::parse::parse_expression;
use crate::runtime::conversion::integer_to_decimal;
use crate::runtime::error_mode::{apply_error_mode, ErrorMode};
use crate::runtime::functions::call_function;
use crate::runtime::model::{
    lookup_field, parse_qualified_field_with_interfaces, FieldLookup, Row, RuntimeValue,
};

/// Parse and evaluate an expression string against a single row (unqualified field names).
pub fn eval_expression_on_row(source: &str, row: &Row) -> Result<RuntimeValue, String> {
    eval_expression_on_row_with_mode(source, row, ErrorMode::Fail)
}

/// Parse and evaluate an expression, applying the given plan-level error mode on failure.
pub fn eval_expression_on_row_with_mode(
    source: &str,
    row: &Row,
    mode: ErrorMode,
) -> Result<RuntimeValue, String> {
    let expr =
        parse_expression(source).map_err(|e| format!("expression parse error: {}", e.message))?;
    match evaluate_expr_on_row(&expr, row) {
        Ok(value) => Ok(value),
        Err(err) => apply_error_mode(mode, err),
    }
}

/// Evaluate an expression AST against a single row.
pub fn evaluate_expr_on_row(expr: &Expr, row: &Row) -> Result<RuntimeValue, String> {
    evaluate_expr_on_row_scoped(expr, row, None)
}

fn evaluate_expr_on_row_scoped(
    expr: &Expr,
    row: &Row,
    lambda_params: Option<&[String]>,
) -> Result<RuntimeValue, String> {
    match expr {
        Expr::Literal { value, .. } => Ok(literal_to_runtime(value)),
        Expr::FieldRef { target, scope, .. } => {
            resolve_field_ref(target, scope.as_deref(), row, lambda_params)
        }
        Expr::Unary { op, expr, .. } => {
            let inner = evaluate_expr_on_row_scoped(expr, row, lambda_params)?;
            match op {
                UnaryOp::Negate => match inner {
                    RuntimeValue::Integer(v) => v
                        .checked_neg()
                        .map(RuntimeValue::Integer)
                        .ok_or_else(|| "negate overflow on i64::MIN".to_string()),
                    RuntimeValue::Decimal(v) => Ok(RuntimeValue::Decimal(-v)),
                    other => Err(format!("negate unsupported for {other:?}")),
                },
                UnaryOp::Not => match inner.as_bool() {
                    Some(v) => Ok(RuntimeValue::Boolean(!v)),
                    None => Err("not requires boolean".into()),
                },
            }
        }
        Expr::Binary {
            op, left, right, ..
        } => match op {
            BinaryOp::And => {
                let left_val = evaluate_expr_on_row_scoped(left, row, lambda_params)?;
                let left_bool = left_val
                    .as_bool()
                    .ok_or_else(|| "and requires boolean operands".to_string())?;
                if !left_bool {
                    return Ok(RuntimeValue::Boolean(false));
                }
                let right_val = evaluate_expr_on_row_scoped(right, row, lambda_params)?;
                let right_bool = right_val
                    .as_bool()
                    .ok_or_else(|| "and requires boolean operands".to_string())?;
                Ok(RuntimeValue::Boolean(right_bool))
            }
            BinaryOp::Or => {
                let left_val = evaluate_expr_on_row_scoped(left, row, lambda_params)?;
                let left_bool = left_val
                    .as_bool()
                    .ok_or_else(|| "or requires boolean operands".to_string())?;
                if left_bool {
                    return Ok(RuntimeValue::Boolean(true));
                }
                let right_val = evaluate_expr_on_row_scoped(right, row, lambda_params)?;
                let right_bool = right_val
                    .as_bool()
                    .ok_or_else(|| "or requires boolean operands".to_string())?;
                Ok(RuntimeValue::Boolean(right_bool))
            }
            _ => {
                let left_val = evaluate_expr_on_row_scoped(left, row, lambda_params)?;
                let right_val = evaluate_expr_on_row_scoped(right, row, lambda_params)?;
                evaluate_binary(*op, &left_val, &right_val)
            }
        },
        Expr::Call { callee, args, .. } => {
            if is_lambda_function(callee) {
                return evaluate_lambda_call(callee, args, row);
            }
            let evaluated_args: Result<Vec<_>, _> = args
                .iter()
                .map(|arg| evaluate_expr_on_row_scoped(arg, row, lambda_params))
                .collect();
            call_function(callee, &evaluated_args?)
        }
        Expr::Lambda { .. } => {
            Err("lambda expressions may only be evaluated by a lambda-enabled function".into())
        }
    }
}

fn resolve_field_ref(
    target: &str,
    scope: Option<&str>,
    row: &Row,
    lambda_params: Option<&[String]>,
) -> Result<RuntimeValue, String> {
    match scope {
        Some("lambda") => {
            let params = lambda_params.unwrap_or(&[]);
            if params.iter().any(|p| p == target) {
                return Ok(match lookup_field(row, target) {
                    FieldLookup::Missing => RuntimeValue::missing(),
                    FieldLookup::Present(value) => value,
                });
            }
            if let Some((param, rest)) = target.split_once('.') {
                if params.iter().any(|p| p == param) {
                    let base = match lookup_field(row, param) {
                        FieldLookup::Missing => return Ok(RuntimeValue::missing()),
                        FieldLookup::Present(value) => value,
                    };
                    return walk_lambda_path(&base, rest);
                }
            }
            Ok(RuntimeValue::missing())
        }
        None | Some("row") => {
            let field = target.rsplit('.').next().unwrap_or(target);
            Ok(match lookup_field(row, field) {
                FieldLookup::Missing => RuntimeValue::missing(),
                FieldLookup::Present(value) => value,
            })
        }
        Some(other) => Err(format!("unsupported fieldRef scope '{other}'")),
    }
}

fn walk_lambda_path(value: &RuntimeValue, path: &str) -> Result<RuntimeValue, String> {
    let mut current = value.clone();
    for segment in path.split('.') {
        current = match &current {
            RuntimeValue::Null | RuntimeValue::Missing(_) => {
                return Ok(RuntimeValue::missing());
            }
            RuntimeValue::Map(map) => map
                .get(segment)
                .cloned()
                .unwrap_or_else(RuntimeValue::missing),
            RuntimeValue::List(items) => {
                let index: i64 = segment
                    .parse()
                    .map_err(|_| format!("lambda path segment '{segment}' is not a list index"))?;
                match crate::runtime::functions::resolve_list_index(index, items.len()) {
                    Some(resolved) => items[resolved].clone(),
                    None => RuntimeValue::missing(),
                }
            }
            other => {
                return Err(format!(
                    "lambda path segment '{segment}' requires map or list, got {other:?}"
                ));
            }
        };
        if matches!(current, RuntimeValue::Missing(_)) {
            return Ok(current);
        }
    }
    Ok(current)
}

/// Evaluate an expression AST against a row workspace context.
pub fn evaluate_expr(
    expr: &Expr,
    workspaces: &std::collections::BTreeMap<String, Vec<Row>>,
    row_index: usize,
) -> Result<RuntimeValue, String> {
    let interface_ids: Vec<String> = workspaces.keys().cloned().collect();
    evaluate_expr_with_interfaces(expr, workspaces, row_index, &interface_ids)
}

fn evaluate_expr_with_interfaces(
    expr: &Expr,
    workspaces: &std::collections::BTreeMap<String, Vec<Row>>,
    row_index: usize,
    interface_ids: &[String],
) -> Result<RuntimeValue, String> {
    match expr {
        Expr::Literal { value, .. } => Ok(literal_to_runtime(value)),
        Expr::FieldRef { target, .. } => {
            let qualified = parse_qualified_field_with_interfaces(target, interface_ids)
                .ok_or_else(|| format!("invalid field reference '{target}'"))?;
            let rows = workspaces
                .get(&qualified.interface_id)
                .ok_or_else(|| format!("unknown interface '{}'", qualified.interface_id))?;
            let row = rows
                .get(row_index)
                .ok_or_else(|| format!("row index {row_index} out of range"))?;
            Ok(match lookup_field(row, &qualified.field_name) {
                FieldLookup::Missing => RuntimeValue::missing(),
                FieldLookup::Present(value) => value,
            })
        }
        Expr::Unary { op, expr, .. } => {
            let inner = evaluate_expr_with_interfaces(expr, workspaces, row_index, interface_ids)?;
            match op {
                UnaryOp::Negate => match inner {
                    RuntimeValue::Integer(v) => v
                        .checked_neg()
                        .map(RuntimeValue::Integer)
                        .ok_or_else(|| "negate overflow on i64::MIN".to_string()),
                    RuntimeValue::Decimal(v) => Ok(RuntimeValue::Decimal(-v)),
                    other => Err(format!("negate unsupported for {other:?}")),
                },
                UnaryOp::Not => match inner.as_bool() {
                    Some(v) => Ok(RuntimeValue::Boolean(!v)),
                    None => Err("not requires boolean".into()),
                },
            }
        }
        Expr::Binary {
            op, left, right, ..
        } => match op {
            BinaryOp::And => {
                let left_val =
                    evaluate_expr_with_interfaces(left, workspaces, row_index, interface_ids)?;
                let left_bool = left_val
                    .as_bool()
                    .ok_or_else(|| "and requires boolean operands".to_string())?;
                if !left_bool {
                    return Ok(RuntimeValue::Boolean(false));
                }
                let right_val =
                    evaluate_expr_with_interfaces(right, workspaces, row_index, interface_ids)?;
                let right_bool = right_val
                    .as_bool()
                    .ok_or_else(|| "and requires boolean operands".to_string())?;
                Ok(RuntimeValue::Boolean(right_bool))
            }
            BinaryOp::Or => {
                let left_val =
                    evaluate_expr_with_interfaces(left, workspaces, row_index, interface_ids)?;
                let left_bool = left_val
                    .as_bool()
                    .ok_or_else(|| "or requires boolean operands".to_string())?;
                if left_bool {
                    return Ok(RuntimeValue::Boolean(true));
                }
                let right_val =
                    evaluate_expr_with_interfaces(right, workspaces, row_index, interface_ids)?;
                let right_bool = right_val
                    .as_bool()
                    .ok_or_else(|| "or requires boolean operands".to_string())?;
                Ok(RuntimeValue::Boolean(right_bool))
            }
            _ => {
                let left_val =
                    evaluate_expr_with_interfaces(left, workspaces, row_index, interface_ids)?;
                let right_val =
                    evaluate_expr_with_interfaces(right, workspaces, row_index, interface_ids)?;
                evaluate_binary(*op, &left_val, &right_val)
            }
        },
        Expr::Call { callee, args, .. } => {
            let evaluated_args: Result<Vec<_>, _> = args
                .iter()
                .map(|arg| evaluate_expr_with_interfaces(arg, workspaces, row_index, interface_ids))
                .collect();
            call_function(callee, &evaluated_args?)
        }
        Expr::Lambda { .. } => {
            Err("lambda expressions may only be evaluated by a lambda-enabled function".into())
        }
    }
}

fn is_lambda_function(callee: &str) -> bool {
    matches!(
        callee,
        "dtcs:transform"
            | "dtcs:filter_values"
            | "dtcs:exists"
            | "dtcs:forall"
            | "dtcs:reduce"
            | "dtcs:zip_with"
            | "dtcs:map_filter"
            | "dtcs:transform_keys"
            | "dtcs:transform_values"
    )
}

fn evaluate_lambda_call(callee: &str, args: &[Expr], row: &Row) -> Result<RuntimeValue, String> {
    match callee {
        "dtcs:reduce" => evaluate_reduce(args, row),
        "dtcs:zip_with" => evaluate_zip_with(args, row),
        "dtcs:map_filter" => evaluate_map_filter(args, row),
        "dtcs:transform_keys" | "dtcs:transform_values" => {
            evaluate_map_transform(callee, args, row)
        }
        _ => evaluate_list_lambda(callee, args, row),
    }
}

fn evaluate_list_lambda(callee: &str, args: &[Expr], row: &Row) -> Result<RuntimeValue, String> {
    if args.len() != 2 {
        return Err(format!("{callee} requires a collection and lambda"));
    }
    let collection = evaluate_expr_on_row(&args[0], row)?;
    let Expr::Lambda {
        parameters, body, ..
    } = &args[1]
    else {
        return Err(format!("{callee} requires a lambda as its second argument"));
    };
    if parameters.is_empty() || parameters.len() > 2 {
        return Err("lambda requires one or two parameters".into());
    }
    let RuntimeValue::List(values) = collection else {
        return Err(format!("{callee} requires a list"));
    };
    if values.len() > 100_000 {
        return Err("lambda evaluation exceeds collection budget".into());
    }
    let mut mapped = Vec::with_capacity(values.len());
    for (index, value) in values.iter().cloned().enumerate() {
        let mut lambda_row = row.clone();
        lambda_row.insert(parameters[0].clone(), value);
        if let Some(index_name) = parameters.get(1) {
            lambda_row.insert(index_name.clone(), RuntimeValue::Integer(index as i64));
        }
        mapped.push(evaluate_expr_on_row_scoped(
            body,
            &lambda_row,
            Some(parameters),
        )?);
    }
    match callee {
        "dtcs:transform" => Ok(RuntimeValue::List(mapped)),
        "dtcs:filter_values" => {
            let mut out = Vec::new();
            for (index, predicate) in mapped.into_iter().enumerate() {
                if predicate.as_bool() == Some(true) {
                    out.push(values[index].clone());
                }
            }
            Ok(RuntimeValue::List(out))
        }
        "dtcs:exists" => Ok(RuntimeValue::Boolean(
            mapped.iter().any(|value| value.as_bool() == Some(true)),
        )),
        "dtcs:forall" => Ok(RuntimeValue::Boolean(
            mapped.iter().all(|value| value.as_bool() == Some(true)),
        )),
        _ => unreachable!(),
    }
}

fn evaluate_reduce(args: &[Expr], row: &Row) -> Result<RuntimeValue, String> {
    if args.len() != 3 {
        return Err("dtcs:reduce requires list, initial, and lambda".into());
    }
    let RuntimeValue::List(values) = evaluate_expr_on_row(&args[0], row)? else {
        return Err("dtcs:reduce requires a list".into());
    };
    let mut acc = evaluate_expr_on_row(&args[1], row)?;
    let Expr::Lambda {
        parameters, body, ..
    } = &args[2]
    else {
        return Err("dtcs:reduce requires a lambda".into());
    };
    if parameters.len() != 2 {
        return Err("dtcs:reduce lambda requires (acc, value)".into());
    }
    for value in values {
        let mut lambda_row = row.clone();
        lambda_row.insert(parameters[0].clone(), acc);
        lambda_row.insert(parameters[1].clone(), value);
        acc = evaluate_expr_on_row_scoped(body, &lambda_row, Some(parameters))?;
    }
    Ok(acc)
}

fn evaluate_zip_with(args: &[Expr], row: &Row) -> Result<RuntimeValue, String> {
    if args.len() != 3 {
        return Err("dtcs:zip_with requires left, right, and lambda".into());
    }
    let RuntimeValue::List(left) = evaluate_expr_on_row(&args[0], row)? else {
        return Err("dtcs:zip_with requires lists".into());
    };
    let RuntimeValue::List(right) = evaluate_expr_on_row(&args[1], row)? else {
        return Err("dtcs:zip_with requires lists".into());
    };
    let Expr::Lambda {
        parameters, body, ..
    } = &args[2]
    else {
        return Err("dtcs:zip_with requires a lambda".into());
    };
    if parameters.len() != 2 {
        return Err("dtcs:zip_with lambda requires two parameters".into());
    }
    let len = left.len().min(right.len());
    let mut out = Vec::with_capacity(len);
    for i in 0..len {
        let mut lambda_row = row.clone();
        lambda_row.insert(parameters[0].clone(), left[i].clone());
        lambda_row.insert(parameters[1].clone(), right[i].clone());
        out.push(evaluate_expr_on_row_scoped(
            body,
            &lambda_row,
            Some(parameters),
        )?);
    }
    Ok(RuntimeValue::List(out))
}

fn evaluate_map_filter(args: &[Expr], row: &Row) -> Result<RuntimeValue, String> {
    if args.len() != 2 {
        return Err("dtcs:map_filter requires map and lambda".into());
    }
    let RuntimeValue::Map(map) = evaluate_expr_on_row(&args[0], row)? else {
        return Err("dtcs:map_filter requires a map".into());
    };
    let Expr::Lambda {
        parameters, body, ..
    } = &args[1]
    else {
        return Err("dtcs:map_filter requires a lambda".into());
    };
    if parameters.len() != 2 {
        return Err("dtcs:map_filter lambda requires (key, value)".into());
    }
    let mut out = indexmap::IndexMap::new();
    for (key, value) in map {
        let mut lambda_row = row.clone();
        lambda_row.insert(parameters[0].clone(), RuntimeValue::String(key.clone()));
        lambda_row.insert(parameters[1].clone(), value.clone());
        if evaluate_expr_on_row_scoped(body, &lambda_row, Some(parameters))?.as_bool() == Some(true)
        {
            out.insert(key, value);
        }
    }
    Ok(RuntimeValue::Map(out))
}

fn evaluate_map_transform(callee: &str, args: &[Expr], row: &Row) -> Result<RuntimeValue, String> {
    if args.len() != 2 {
        return Err(format!("{callee} requires map and lambda"));
    }
    let RuntimeValue::Map(map) = evaluate_expr_on_row(&args[0], row)? else {
        return Err(format!("{callee} requires a map"));
    };
    let Expr::Lambda {
        parameters, body, ..
    } = &args[1]
    else {
        return Err(format!("{callee} requires a lambda"));
    };
    if parameters.len() != 2 {
        return Err(format!("{callee} lambda requires (key, value)"));
    }
    let mut out = indexmap::IndexMap::new();
    for (key, value) in map {
        let mut lambda_row = row.clone();
        lambda_row.insert(parameters[0].clone(), RuntimeValue::String(key.clone()));
        lambda_row.insert(parameters[1].clone(), value.clone());
        let result = evaluate_expr_on_row_scoped(body, &lambda_row, Some(parameters))?;
        if callee == "dtcs:transform_keys" {
            let new_key = result
                .as_str()
                .ok_or("dtcs:transform_keys must return string keys")?
                .to_string();
            out.insert(new_key, value);
        } else {
            out.insert(key, result);
        }
    }
    Ok(RuntimeValue::Map(out))
}

fn literal_to_runtime(value: &LiteralValue) -> RuntimeValue {
    match value {
        LiteralValue::Boolean(v) => RuntimeValue::Boolean(*v),
        LiteralValue::String(v) => RuntimeValue::String(v.clone()),
        LiteralValue::Integer(v) => RuntimeValue::Integer(*v),
        LiteralValue::Decimal(v) => RuntimeValue::Decimal(*v),
    }
}

fn values_equal(left: &RuntimeValue, right: &RuntimeValue) -> bool {
    if left == right {
        return true;
    }
    match (left, right) {
        (RuntimeValue::Integer(a), RuntimeValue::Decimal(b)) => {
            integer_to_decimal(*a).is_ok_and(|promoted| promoted == *b)
        }
        (RuntimeValue::Decimal(a), RuntimeValue::Integer(b)) => {
            integer_to_decimal(*b).is_ok_and(|promoted| *a == promoted)
        }
        _ => false,
    }
}

fn evaluate_binary(
    op: BinaryOp,
    left: &RuntimeValue,
    right: &RuntimeValue,
) -> Result<RuntimeValue, String> {
    match op {
        BinaryOp::Add => match (left, right) {
            (RuntimeValue::Integer(a), RuntimeValue::Integer(b)) => a
                .checked_add(*b)
                .map(RuntimeValue::Integer)
                .ok_or_else(|| "integer overflow".into()),
            (RuntimeValue::Decimal(a), RuntimeValue::Decimal(b)) => {
                Ok(RuntimeValue::Decimal(a + b))
            }
            (RuntimeValue::String(a), RuntimeValue::String(b)) => {
                Ok(RuntimeValue::String(format!("{a}{b}")))
            }
            (RuntimeValue::Integer(a), RuntimeValue::Decimal(b)) => {
                Ok(RuntimeValue::Decimal(integer_to_decimal(*a)? + b))
            }
            (RuntimeValue::Decimal(a), RuntimeValue::Integer(b)) => {
                Ok(RuntimeValue::Decimal(a + integer_to_decimal(*b)?))
            }
            _ => Err("add type mismatch".into()),
        },
        BinaryOp::Sub => match (left, right) {
            (RuntimeValue::Integer(a), RuntimeValue::Integer(b)) => a
                .checked_sub(*b)
                .map(RuntimeValue::Integer)
                .ok_or_else(|| "integer overflow".into()),
            (RuntimeValue::Decimal(a), RuntimeValue::Decimal(b)) => {
                Ok(RuntimeValue::Decimal(a - b))
            }
            (RuntimeValue::Integer(a), RuntimeValue::Decimal(b)) => {
                Ok(RuntimeValue::Decimal(integer_to_decimal(*a)? - b))
            }
            (RuntimeValue::Decimal(a), RuntimeValue::Integer(b)) => {
                Ok(RuntimeValue::Decimal(a - integer_to_decimal(*b)?))
            }
            _ => Err("sub type mismatch".into()),
        },
        BinaryOp::Mul => match (left, right) {
            (RuntimeValue::Integer(a), RuntimeValue::Integer(b)) => a
                .checked_mul(*b)
                .map(RuntimeValue::Integer)
                .ok_or_else(|| "integer overflow".into()),
            (RuntimeValue::Decimal(a), RuntimeValue::Decimal(b)) => {
                Ok(RuntimeValue::Decimal(a * b))
            }
            (RuntimeValue::Integer(a), RuntimeValue::Decimal(b)) => {
                Ok(RuntimeValue::Decimal(integer_to_decimal(*a)? * b))
            }
            (RuntimeValue::Decimal(a), RuntimeValue::Integer(b)) => {
                Ok(RuntimeValue::Decimal(a * integer_to_decimal(*b)?))
            }
            _ => Err("mul type mismatch".into()),
        },
        BinaryOp::Div => match (left, right) {
            (RuntimeValue::Integer(a), RuntimeValue::Integer(b)) if *b != 0 => a
                .checked_div(*b)
                .map(RuntimeValue::Integer)
                .ok_or_else(|| "integer division overflow".into()),
            (RuntimeValue::Decimal(a), RuntimeValue::Decimal(b)) if *b != 0.0 => {
                Ok(RuntimeValue::Decimal(a / b))
            }
            (RuntimeValue::Integer(a), RuntimeValue::Decimal(b)) if *b != 0.0 => {
                Ok(RuntimeValue::Decimal(integer_to_decimal(*a)? / b))
            }
            (RuntimeValue::Decimal(a), RuntimeValue::Integer(b)) if *b != 0 => {
                Ok(RuntimeValue::Decimal(a / integer_to_decimal(*b)?))
            }
            _ => Err("div type mismatch or division by zero".into()),
        },
        BinaryOp::Mod => match (left, right) {
            (RuntimeValue::Integer(a), RuntimeValue::Integer(b)) if *b != 0 => {
                Ok(RuntimeValue::Integer(a % b))
            }
            (RuntimeValue::Decimal(a), RuntimeValue::Decimal(b)) if *b != 0.0 => {
                Ok(RuntimeValue::Decimal(a % b))
            }
            _ => Err("mod type mismatch or division by zero".into()),
        },
        BinaryOp::Eq => Ok(RuntimeValue::Boolean(values_equal(left, right))),
        BinaryOp::Neq => Ok(RuntimeValue::Boolean(!values_equal(left, right))),
        BinaryOp::NullSafeEq => Ok(RuntimeValue::Boolean(null_safe_equal(left, right))),
        BinaryOp::Lt | BinaryOp::Lte | BinaryOp::Gt | BinaryOp::Gte => {
            compare_ordered(op, left, right).map(RuntimeValue::Boolean)
        }
        BinaryOp::Between => {
            Err("between is ternary; use `value between lo and hi` (parsed as dtcs:between)".into())
        }
        BinaryOp::In => match right {
            RuntimeValue::List(items) => Ok(RuntimeValue::Boolean(
                items.iter().any(|item| values_equal(left, item)),
            )),
            RuntimeValue::String(haystack) => match left.as_str() {
                Some(needle) => Ok(RuntimeValue::Boolean(haystack.contains(needle))),
                None if left.is_null() || left.is_missing() => Ok(RuntimeValue::Null),
                None => Err("in with string right-hand side requires string left".into()),
            },
            _ => Err("in requires list or string on the right".into()),
        },
        BinaryOp::Contains => match left {
            RuntimeValue::List(items) => Ok(RuntimeValue::Boolean(
                items.iter().any(|item| values_equal(item, right)),
            )),
            RuntimeValue::String(haystack) => match right.as_str() {
                Some(needle) => Ok(RuntimeValue::Boolean(haystack.contains(needle))),
                None if right.is_null() || right.is_missing() => Ok(RuntimeValue::Null),
                None => Err("contains with string left-hand side requires string right".into()),
            },
            _ => Err("contains requires list or string on the left".into()),
        },
        BinaryOp::And | BinaryOp::Or => unreachable!("handled by short-circuit evaluation"),
    }
}

fn null_safe_equal(left: &RuntimeValue, right: &RuntimeValue) -> bool {
    match (left, right) {
        (RuntimeValue::Null, RuntimeValue::Null)
        | (RuntimeValue::Missing(_), RuntimeValue::Missing(_)) => true,
        (RuntimeValue::Null, _)
        | (_, RuntimeValue::Null)
        | (RuntimeValue::Missing(_), _)
        | (_, RuntimeValue::Missing(_)) => false,
        _ => values_equal(left, right),
    }
}

fn compare_ordered(
    op: BinaryOp,
    left: &RuntimeValue,
    right: &RuntimeValue,
) -> Result<bool, String> {
    use BinaryOp::{Gt, Gte, Lt, Lte};
    let ordering = match (left, right) {
        (RuntimeValue::Integer(a), RuntimeValue::Integer(b)) => a.cmp(b),
        (RuntimeValue::Decimal(a), RuntimeValue::Decimal(b)) => a
            .partial_cmp(b)
            .ok_or_else(|| "decimal comparison failed".to_string())?,
        (RuntimeValue::Integer(a), RuntimeValue::Decimal(b)) => integer_to_decimal(*a)?
            .partial_cmp(b)
            .ok_or_else(|| "decimal comparison failed".to_string())?,
        (RuntimeValue::Decimal(a), RuntimeValue::Integer(b)) => a
            .partial_cmp(&integer_to_decimal(*b)?)
            .ok_or_else(|| "decimal comparison failed".to_string())?,
        (RuntimeValue::String(a), RuntimeValue::String(b)) => a.cmp(b),
        (RuntimeValue::Boolean(a), RuntimeValue::Boolean(b)) => a.cmp(b),
        _ => return Err("comparison type mismatch".into()),
    };
    Ok(match op {
        Lt => ordering == std::cmp::Ordering::Less,
        Lte => ordering != std::cmp::Ordering::Greater,
        Gt => ordering == std::cmp::Ordering::Greater,
        Gte => ordering != std::cmp::Ordering::Less,
        _ => return Err("invalid comparison operator".into()),
    })
}