qql-plan 0.4.2

Typed QQL execution plans and Qdrant route lowering
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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
//! Formula IR types matching the OpenAPI `Expression` schema.
//!
//! [`PlanFormula`] is the plan-owned formula tree: its `Serialize` emits the
//! OpenAPI `Expression` JSON directly (no `serde_json::Value` intermediate),
//! and the gRPC / edge adapters convert it straight into their typed
//! expression messages. [`FormulaDefault`] carries one `DEFAULTS (name = …)`
//! binding.

use alloc::boxed::Box;
use alloc::collections::BTreeMap;
use alloc::string::String;
use alloc::vec::Vec;
use serde::ser::SerializeMap;
use serde::{Serialize, Serializer};

use qql_core::ast::{FormulaExpr, Value};

use crate::filter::lower_filter;
use crate::filter_types::{FieldCondition, FilterClause, FilterExpression, MatchValue};

/// Decay curve family of a [`PlanFormula::Decay`] node.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PlanDecayKind {
    /// Exponential decay (`exp_decay`).
    Exp,
    /// Linear decay (`lin_decay`).
    Lin,
    /// Gaussian decay (`gauss_decay`).
    Gauss,
}

impl PlanDecayKind {
    /// OpenAPI `Expression` key carrying the decay parameters.
    pub fn wire_key(self) -> &'static str {
        match self {
            Self::Exp => "exp_decay",
            Self::Lin => "lin_decay",
            Self::Gauss => "gauss_decay",
        }
    }

    /// Map a parser/backend decay kind spelling to the typed family.
    ///
    /// Unknown spellings fall back to Gaussian, matching the historical
    /// lowering.
    fn from_wire(kind: &str) -> Self {
        match kind.to_ascii_lowercase().as_str() {
            "exp" | "exp_decay" => Self::Exp,
            "lin" | "lin_decay" => Self::Lin,
            _ => Self::Gauss,
        }
    }
}

/// One `DEFAULTS (name = …)` binding value.
///
/// The OpenAPI `FormulaQuery.defaults` object is schemaless
/// (`additionalProperties: true`): numeric variables take numbers, datetime
/// variables take ISO-8601 strings, and the backend coerces per variable.
/// This type carries exactly that value domain without a `serde_json::Value`
/// intermediary.
#[derive(Debug, Clone, PartialEq)]
pub enum FormulaDefault {
    /// JSON `null`.
    Null,
    /// Boolean value.
    Bool(bool),
    /// Integer value.
    Int(i64),
    /// Floating-point value.
    Float(f64),
    /// String value (e.g. an ISO-8601 datetime for a datetime variable).
    String(String),
    /// JSON array.
    List(Vec<FormulaDefault>),
    /// JSON object. Keys are sorted, matching the previous `serde_json::Map`
    /// serialization.
    Object(BTreeMap<String, FormulaDefault>),
}

impl Serialize for FormulaDefault {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        match self {
            Self::Null => serializer.serialize_unit(),
            Self::Bool(value) => serializer.serialize_bool(*value),
            Self::Int(value) => serializer.serialize_i64(*value),
            Self::Float(value) => serializer.serialize_f64(*value),
            Self::String(value) => serializer.serialize_str(value),
            Self::List(values) => values.serialize(serializer),
            Self::Object(entries) => {
                let mut map = serializer.serialize_map(Some(entries.len()))?;
                for (key, value) in entries {
                    map.serialize_entry(key, value)?;
                }
                map.end()
            }
        }
    }
}

impl From<&Value> for FormulaDefault {
    /// Lower an AST value into the formula-default domain.
    ///
    /// # Invariant
    ///
    /// `Param` / `PositionalParam` panic: `plan()` rejects unbound parameters
    /// before lowering, mirroring the boundary-serialization invariant.
    fn from(value: &Value) -> Self {
        match value {
            Value::Str(value) => Self::String(value.clone()),
            Value::Int(value) => Self::Int(*value),
            // The transports carry no `uint64` scalar (gRPC `Value` is
            // int64/double): values that fit stay exact integers, larger
            // ones widen to `Float` rather than growing the variant set
            // every transport matches on.
            Value::UInt(value) => i64::try_from(*value)
                .map(Self::Int)
                .unwrap_or(Self::Float(*value as f64)),
            Value::Float(value) => Self::Float(*value),
            Value::Bool(value) => Self::Bool(*value),
            Value::Null => Self::Null,
            Value::Dict(entries) => Self::Object(
                entries
                    .iter()
                    .map(|(key, value)| (key.clone(), Self::from(value)))
                    .collect(),
            ),
            Value::List(items) => Self::List(items.iter().map(Self::from).collect()),
            Value::F32Array(values) => Self::List(
                values
                    .iter()
                    .map(|value| Self::Float(*value as f64))
                    .collect(),
            ),
            Value::Param(name, _) => {
                panic!(
                    "invariant violation: unbound parameter :{name} reached formula defaults lowering"
                );
            }
            Value::PositionalParam(idx, _) => {
                panic!(
                    "invariant violation: unbound positional parameter ?{idx} reached formula defaults lowering"
                );
            }
        }
    }
}

/// Transport-neutral formula expression tree (`QUERY FORMULA <expr>`).
///
/// `Serialize` emits the OpenAPI `Expression` wire shape directly.
#[derive(Debug, Clone)]
pub enum PlanFormula {
    /// Numeric literal.
    Constant(f64),
    /// Variable reference: `$score`, `$score[i]`, or a payload key.
    Variable(String),
    /// `left + right`.
    Sum {
        /// Left term.
        left: Box<PlanFormula>,
        /// Right term.
        right: Box<PlanFormula>,
    },
    /// `left - right` (wire: `sum` of `left` and `neg` `right`).
    Sub {
        /// Left term.
        left: Box<PlanFormula>,
        /// Right term.
        right: Box<PlanFormula>,
    },
    /// `left * right`.
    Mul {
        /// Left factor.
        left: Box<PlanFormula>,
        /// Right factor.
        right: Box<PlanFormula>,
    },
    /// `left / right`, with the optional zero-division fallback.
    Div {
        /// Dividend.
        left: Box<PlanFormula>,
        /// Divisor.
        right: Box<PlanFormula>,
        /// Value substituted when the divisor is zero.
        by_zero_default: Option<f64>,
    },
    /// Unary negation.
    Neg(Box<PlanFormula>),
    /// `ABS(x)`.
    Abs(Box<PlanFormula>),
    /// `SQRT(x)`.
    Sqrt(Box<PlanFormula>),
    /// Base-10 logarithm.
    Log10(Box<PlanFormula>),
    /// Natural logarithm.
    Ln(Box<PlanFormula>),
    /// `e^x`.
    Exp(Box<PlanFormula>),
    /// Inverse hyperbolic cosine.
    Acosh(Box<PlanFormula>),
    /// N-ary maximum (at least one operand).
    Max(Vec<PlanFormula>),
    /// N-ary minimum (at least one operand).
    Min(Vec<PlanFormula>),
    /// `base` raised to `exponent`.
    Pow {
        /// Base expression.
        base: Box<PlanFormula>,
        /// Exponent expression.
        exponent: Box<PlanFormula>,
    },
    /// Geographic distance in meters from `(lat, lon)` to a geo payload `field`.
    GeoDistance {
        /// Query latitude in degrees.
        lat: f64,
        /// Query longitude in degrees.
        lon: f64,
        /// Payload geo field to measure against.
        field: String,
    },
    /// Decay curve over `x`, with optional target, scale, and midpoint.
    Decay {
        /// Curve family.
        kind: PlanDecayKind,
        /// Decaying expression.
        x: Box<PlanFormula>,
        /// Decay origin; absent means the backend default (zero).
        target: Option<Box<PlanFormula>>,
        /// Distance from `target` at which the output falls to `midpoint`.
        scale: Option<f64>,
        /// Output value at that distance from `target`.
        midpoint: Option<f64>,
    },
    /// Boolean condition evaluated as `1.0` / `0.0` (OpenAPI `Condition`).
    Condition(FilterExpression),
    /// `CASE WHEN cond THEN then_ ELSE else_ END`, lowered to
    /// `cond * then_ + (1 - cond) * else_`.
    Case {
        /// Boolean condition term (a [`PlanFormula::Condition`]).
        cond: Box<PlanFormula>,
        /// Value produced when the condition holds.
        then_: Box<PlanFormula>,
        /// Value produced otherwise.
        else_: Box<PlanFormula>,
    },
    /// ISO-8601 datetime constant.
    Datetime(String),
    /// Payload datetime field reference.
    DatetimeKey(String),
}

impl PlanFormula {
    /// Lower a formula expression into plan IR.
    ///
    /// Fallible because `CASE WHEN` conditions lower through
    /// [`lower_filter`], which rejects mistyped range bounds.
    pub fn from_expr(expr: &FormulaExpr) -> Result<Self, qql_core::error::QqlError> {
        Ok(match expr {
            FormulaExpr::Constant { value } => Self::Constant(*value),
            FormulaExpr::Variable { name } => Self::Variable(variable_wire_name(name)),
            FormulaExpr::Sum { left, right } => Self::Sum {
                left: Box::new(Self::from_expr(left)?),
                right: Box::new(Self::from_expr(right)?),
            },
            FormulaExpr::Sub { left, right } => Self::Sub {
                left: Box::new(Self::from_expr(left)?),
                right: Box::new(Self::from_expr(right)?),
            },
            FormulaExpr::Mul { left, right } => Self::Mul {
                left: Box::new(Self::from_expr(left)?),
                right: Box::new(Self::from_expr(right)?),
            },
            FormulaExpr::Div {
                left,
                right,
                by_zero_default,
            } => Self::Div {
                left: Box::new(Self::from_expr(left)?),
                right: Box::new(Self::from_expr(right)?),
                by_zero_default: *by_zero_default,
            },
            FormulaExpr::Neg { operand } => Self::Neg(Box::new(Self::from_expr(operand)?)),
            FormulaExpr::Abs { x } => Self::Abs(Box::new(Self::from_expr(x)?)),
            FormulaExpr::Sqrt { x, domain_default } => {
                let inner = Self::from_expr(x)?;
                match domain_default {
                    None => Self::Sqrt(Box::new(inner)),
                    Some(_) => Self::Sqrt(Box::new(Self::Max(vec![Self::Constant(0.0), inner]))),
                }
            }
            FormulaExpr::Log { x, domain_default } => {
                let inner = Self::from_expr(x)?;
                match domain_default {
                    None => Self::Log10(Box::new(inner)),
                    Some(_) => Self::Log10(Box::new(Self::Max(vec![Self::Constant(1e-9), inner]))),
                }
            }
            FormulaExpr::Ln { x, domain_default } => {
                let inner = Self::from_expr(x)?;
                match domain_default {
                    None => Self::Ln(Box::new(inner)),
                    Some(_) => Self::Ln(Box::new(Self::Max(vec![Self::Constant(1e-9), inner]))),
                }
            }
            FormulaExpr::Exp { x } => Self::Exp(Box::new(Self::from_expr(x)?)),
            FormulaExpr::Acosh { x, domain_default } => {
                let inner = Self::from_expr(x)?;
                match domain_default {
                    None => Self::Acosh(Box::new(inner)),
                    Some(_) => Self::Acosh(Box::new(Self::Max(vec![Self::Constant(1.0), inner]))),
                }
            }
            FormulaExpr::Max { args } => {
                Self::Max(args.iter().map(Self::from_expr).collect::<Result<_, _>>()?)
            }
            FormulaExpr::Min { args } => {
                Self::Min(args.iter().map(Self::from_expr).collect::<Result<_, _>>()?)
            }
            FormulaExpr::Pow { base, exponent } => Self::Pow {
                base: Box::new(Self::from_expr(base)?),
                exponent: Box::new(Self::from_expr(exponent)?),
            },
            FormulaExpr::GeoDistance { lat, lon, field } => Self::GeoDistance {
                lat: *lat,
                lon: *lon,
                field: field.clone(),
            },
            FormulaExpr::Decay {
                kind,
                x,
                target,
                scale,
                midpoint,
            } => {
                let target = target
                    .as_ref()
                    .map(|t| Ok(Box::new(Self::from_expr(t)?)))
                    .transpose()?;
                // A bare field identifier decaying against a datetime target is
                // a datetime key, not a numeric variable.
                let x = match (&**x, matches!(target.as_deref(), Some(Self::Datetime(_)))) {
                    (FormulaExpr::Variable { name }, true) => Self::DatetimeKey(name.clone()),
                    _ => Self::from_expr(x)?,
                };
                Self::Decay {
                    kind: PlanDecayKind::from_wire(kind),
                    x: Box::new(x),
                    target,
                    scale: *scale,
                    midpoint: *midpoint,
                }
            }
            FormulaExpr::Case { cond, then_, else_ } => Self::Case {
                cond: Box::new(Self::Condition(lower_filter(cond)?)),
                then_: Box::new(Self::from_expr(then_)?),
                else_: Box::new(Self::from_expr(else_)?),
            },
            FormulaExpr::MatchCondition { field, values } => {
                Self::Condition(match_condition_filter(field, values))
            }
            FormulaExpr::Datetime { value } => Self::Datetime(value.clone()),
            FormulaExpr::DatetimeKey { key } => Self::DatetimeKey(key.clone()),
        })
    }
}

/// Normalize the reserved score variable to its wire spelling.
fn variable_wire_name(name: &str) -> String {
    if name == "score" {
        String::from("$score")
    } else {
        String::from(name)
    }
}

/// Build the condition term for `MATCH(field, values)`.
fn match_condition_filter(field: &str, values: &[Value]) -> FilterExpression {
    let r#match = if values.len() == 1 {
        MatchValue::Value {
            value: values[0].clone(),
        }
    } else {
        MatchValue::Any {
            any: values.to_vec(),
        }
    };
    FilterExpression::Single(Box::new(FilterClause::Field(Box::new(FieldCondition {
        key: field.into(),
        r#match: Some(r#match),
        ..Default::default()
    }))))
}

// ── Wire serialization ──────────────────────────────────────────
//
// Every struct below is a one-key OpenAPI object. Fields are declared in
// alphabetical order so direct `Serialize` output matches the key order of
// the previous `serde_json::Map` (BTreeMap-backed) lowering.

#[derive(Serialize)]
struct SumValue<T: Serialize> {
    sum: T,
}

#[derive(Serialize)]
struct MultValue<T: Serialize> {
    mult: T,
}

#[derive(Serialize)]
struct MaxValue<'a> {
    max: &'a [PlanFormula],
}

#[derive(Serialize)]
struct MinValue<'a> {
    min: &'a [PlanFormula],
}

#[derive(Serialize)]
struct NegValue<'a> {
    neg: &'a PlanFormula,
}

#[derive(Serialize)]
struct AbsValue<'a> {
    abs: &'a PlanFormula,
}

#[derive(Serialize)]
struct SqrtValue<'a> {
    sqrt: &'a PlanFormula,
}

#[derive(Serialize)]
struct Log10Value<'a> {
    log10: &'a PlanFormula,
}

#[derive(Serialize)]
struct LnValue<'a> {
    ln: &'a PlanFormula,
}

#[derive(Serialize)]
struct ExpValue<'a> {
    exp: &'a PlanFormula,
}

#[derive(Serialize)]
struct AcoshValue<'a> {
    acosh: &'a PlanFormula,
}

#[derive(Serialize)]
struct PowValue<'a> {
    pow: PowParams<'a>,
}

#[derive(Serialize)]
struct PowParams<'a> {
    base: &'a PlanFormula,
    exponent: &'a PlanFormula,
}

#[derive(Serialize)]
struct DivValue<'a> {
    div: DivParams<'a>,
}

#[derive(Serialize)]
struct DivParams<'a> {
    #[serde(skip_serializing_if = "Option::is_none")]
    by_zero_default: Option<f64>,
    left: &'a PlanFormula,
    right: &'a PlanFormula,
}

#[derive(Serialize)]
struct GeoDistanceValue<'a> {
    geo_distance: GeoDistanceParams<'a>,
}

#[derive(Serialize)]
struct GeoDistanceParams<'a> {
    origin: GeoPointRef,
    to: &'a str,
}

#[derive(Serialize)]
struct GeoPointRef {
    lat: f64,
    lon: f64,
}

#[derive(Serialize)]
struct DecayParams<'a> {
    #[serde(skip_serializing_if = "Option::is_none")]
    midpoint: Option<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    scale: Option<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    target: Option<&'a PlanFormula>,
    x: &'a PlanFormula,
}

#[derive(Serialize)]
struct DatetimeValue<'a> {
    datetime: &'a str,
}

#[derive(Serialize)]
struct DatetimeKeyValue<'a> {
    datetime_key: &'a str,
}

impl Serialize for PlanFormula {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        match self {
            Self::Constant(value) => serializer.serialize_f64(*value),
            Self::Variable(name) => serializer.serialize_str(name),
            Self::Sum { left, right } => SumValue {
                sum: (&**left, &**right),
            }
            .serialize(serializer),
            Self::Sub { left, right } => SumValue {
                sum: (&**left, NegValue { neg: right }),
            }
            .serialize(serializer),
            Self::Mul { left, right } => MultValue {
                mult: (&**left, &**right),
            }
            .serialize(serializer),
            Self::Div {
                left,
                right,
                by_zero_default,
            } => DivValue {
                div: DivParams {
                    by_zero_default: *by_zero_default,
                    left,
                    right,
                },
            }
            .serialize(serializer),
            Self::Neg(operand) => NegValue { neg: operand }.serialize(serializer),
            Self::Abs(x) => AbsValue { abs: x }.serialize(serializer),
            Self::Sqrt(x) => SqrtValue { sqrt: x }.serialize(serializer),
            Self::Log10(x) => Log10Value { log10: x }.serialize(serializer),
            Self::Ln(x) => LnValue { ln: x }.serialize(serializer),
            Self::Exp(x) => ExpValue { exp: x }.serialize(serializer),
            Self::Acosh(x) => AcoshValue { acosh: x }.serialize(serializer),
            Self::Max(args) => MaxValue { max: args }.serialize(serializer),
            Self::Min(args) => MinValue { min: args }.serialize(serializer),
            Self::Pow { base, exponent } => PowValue {
                pow: PowParams { base, exponent },
            }
            .serialize(serializer),
            Self::GeoDistance { lat, lon, field } => GeoDistanceValue {
                geo_distance: GeoDistanceParams {
                    origin: GeoPointRef {
                        lat: *lat,
                        lon: *lon,
                    },
                    to: field,
                },
            }
            .serialize(serializer),
            Self::Decay {
                kind,
                x,
                target,
                scale,
                midpoint,
            } => {
                let params = DecayParams {
                    midpoint: *midpoint,
                    scale: *scale,
                    target: target.as_deref(),
                    x,
                };
                let mut map = serializer.serialize_map(Some(1))?;
                map.serialize_entry(kind.wire_key(), &params)?;
                map.end()
            }
            Self::Condition(filter) => filter.serialize(serializer),
            Self::Case { cond, then_, else_ } => {
                let one_minus = SumValue {
                    sum: (1.0f64, NegValue { neg: cond }),
                };
                SumValue {
                    sum: (
                        MultValue {
                            mult: (&**cond, &**then_),
                        },
                        MultValue {
                            mult: (one_minus, &**else_),
                        },
                    ),
                }
                .serialize(serializer)
            }
            Self::Datetime(value) => DatetimeValue { datetime: value }.serialize(serializer),
            Self::DatetimeKey(key) => DatetimeKeyValue { datetime_key: key }.serialize(serializer),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use qql_core::ast::{QueryExpr, Stmt};
    use qql_core::parser::Parser;
    use serde_json::json;

    fn lower(source: &str) -> serde_json::Value {
        let stmt = Parser::parse(source).unwrap();
        let Stmt::Query(query) = stmt else {
            panic!("expected query");
        };
        let QueryExpr::Formula { expression, .. } = &query.expression else {
            panic!("expected formula");
        };
        serde_json::to_value(PlanFormula::from_expr(expression.as_ref()).unwrap()).unwrap()
    }

    #[test]
    fn arithmetic_and_function_shapes_match_openapi_expression() {
        assert_eq!(
            lower("QUERY FORMULA score * 2 FROM docs;"),
            json!({"mult": ["$score", 2.0]})
        );
        assert_eq!(
            lower("QUERY FORMULA score - 2 FROM docs;"),
            json!({"sum": ["$score", {"neg": 2.0}]})
        );
        assert_eq!(
            lower("QUERY FORMULA (score / views [DEFAULT = 1.0]) FROM docs;"),
            json!({"div": {"by_zero_default": 1.0, "left": "$score", "right": "views"}})
        );
        assert_eq!(
            lower("QUERY FORMULA ABS(score) + SQRT(bonus) FROM docs;"),
            json!({"sum": [{"abs": "$score"}, {"sqrt": "bonus"}]})
        );
        assert_eq!(
            lower("QUERY FORMULA LOG(score) + LN(bonus) FROM docs;"),
            json!({"sum": [{"log10": "$score"}, {"ln": "bonus"}]})
        );
        assert_eq!(
            lower("QUERY FORMULA EXP(score) + ACOSH(bonus) FROM docs;"),
            json!({"sum": [{"exp": "$score"}, {"acosh": "bonus"}]})
        );
        assert_eq!(
            lower("QUERY FORMULA MAX(score, bonus) + MIN(rank) FROM docs;"),
            json!({"sum": [{"max": ["$score", "bonus"]}, {"min": ["rank"]}]})
        );
        assert_eq!(
            lower("QUERY FORMULA POW(score, 2) FROM docs;"),
            json!({"pow": {"base": "$score", "exponent": 2.0}})
        );
    }

    #[test]
    fn bounded_functions_with_domain_default_desugar_to_max_clamps() {
        assert_eq!(
            lower("QUERY FORMULA ACOSH(score) [DEFAULT = 0.0] FROM docs;"),
            json!({"acosh": {"max": [1.0, "$score"]}})
        );
        assert_eq!(
            lower("QUERY FORMULA SQRT(bonus) [DEFAULT = 0.0] FROM docs;"),
            json!({"sqrt": {"max": [0.0, "bonus"]}})
        );
        assert_eq!(
            lower("QUERY FORMULA LOG(views) [DEFAULT = 0.0] FROM docs;"),
            json!({"log10": {"max": [1e-9, "views"]}})
        );
        assert_eq!(
            lower("QUERY FORMULA LN(views) [DEFAULT = 0.0] FROM docs;"),
            json!({"ln": {"max": [1e-9, "views"]}})
        );
    }

    #[test]
    fn geo_distance_and_datetime_shapes() {
        assert_eq!(
            lower("QUERY FORMULA GEO_DISTANCE(52.5, 13.4, location) FROM docs;"),
            json!({"geo_distance": {"origin": {"lat": 52.5, "lon": 13.4}, "to": "location"}})
        );
        assert_eq!(
            lower("QUERY FORMULA DATETIME('2024-01-01T00:00:00Z') FROM docs;"),
            json!({"datetime": "2024-01-01T00:00:00Z"})
        );
        assert_eq!(
            lower("QUERY FORMULA DATETIME_KEY('created_at') FROM docs;"),
            json!({"datetime_key": "created_at"})
        );
    }

    #[test]
    fn decay_datetime_target_infers_datetime_key() {
        assert_eq!(
            lower(
                "QUERY FORMULA EXP_DECAY(judgment_date, TARGET = '2026-09-04T00:00:00Z', \
                 SCALE = 630720000, MIDPOINT = 0.5) FROM docs;"
            ),
            json!({
                "exp_decay": {
                    "midpoint": 0.5,
                    "scale": 630720000.0,
                    "target": {"datetime": "2026-09-04T00:00:00Z"},
                    "x": {"datetime_key": "judgment_date"}
                }
            })
        );
        // A non-datetime target keeps the numeric variable form.
        assert_eq!(
            lower("QUERY FORMULA LIN_DECAY(score, TARGET = 0.5) FROM docs;"),
            json!({"lin_decay": {"target": 0.5, "x": "$score"}})
        );
        assert_eq!(
            lower("QUERY FORMULA GAUSS_DECAY(payload_key) FROM docs;"),
            json!({"gauss_decay": {"x": "payload_key"}})
        );
    }

    #[test]
    fn score_variable_normalizes_to_wire_spelling() {
        assert_eq!(
            lower("QUERY FORMULA $score + score FROM docs;"),
            json!({"sum": ["$score", "$score"]})
        );
        assert_eq!(lower("QUERY FORMULA score FROM docs;"), json!("$score"));
    }

    #[test]
    fn match_conditions_lower_to_key_match_objects() {
        assert_eq!(
            lower("QUERY FORMULA MATCH(is_superhost, true) FROM docs;"),
            json!({"key": "is_superhost", "match": {"value": true}})
        );
        assert_eq!(
            lower("QUERY FORMULA MATCH_ANY(tags, ['a', 'b']) FROM docs;"),
            json!({"key": "tags", "match": {"any": ["a", "b"]}})
        );
    }

    #[test]
    fn case_lowers_to_condition_weighted_terms() {
        let value = lower(
            "QUERY FORMULA CASE WHEN status = 'active' THEN $score * 2 ELSE $score END FROM docs;",
        );
        let cond = json!({"key": "status", "match": {"value": "active"}});
        assert_eq!(value["sum"][0]["mult"][0], cond);
        assert_eq!(value["sum"][0]["mult"][1], json!({"mult": ["$score", 2.0]}));
        assert_eq!(value["sum"][1]["mult"][0]["sum"][0], json!(1.0));
        assert_eq!(value["sum"][1]["mult"][0]["sum"][1]["neg"], cond);
        assert_eq!(value["sum"][1]["mult"][1], json!("$score"));

        let compound =
            lower("QUERY FORMULA CASE WHEN a = 1 AND b = 2 THEN $score ELSE 0 END FROM docs;");
        assert_eq!(
            compound["sum"][0]["mult"][0],
            json!({"must": [
                {"key": "a", "match": {"value": 1}},
                {"key": "b", "match": {"value": 2}}
            ]})
        );
    }

    #[test]
    fn ast_values_lower_to_typed_defaults() {
        let defaults: BTreeMap<String, FormulaDefault> = [
            ("score".to_string(), FormulaDefault::Float(0.0)),
            ("boost".to_string(), FormulaDefault::Int(2)),
            (
                "start".to_string(),
                FormulaDefault::String("2024-01-01T00:00:00Z".into()),
            ),
            (
                "nested".to_string(),
                FormulaDefault::Object(BTreeMap::from([(
                    "a".to_string(),
                    FormulaDefault::List(vec![FormulaDefault::Int(1), FormulaDefault::Null]),
                )])),
            ),
        ]
        .into();
        assert_eq!(
            serde_json::to_value(defaults).unwrap(),
            json!({
                "score": 0.0,
                "boost": 2,
                "start": "2024-01-01T00:00:00Z",
                "nested": {"a": [1, null]}
            })
        );

        assert_eq!(
            FormulaDefault::from(&Value::F32Array(vec![0.5, 1.0])),
            FormulaDefault::List(vec![FormulaDefault::Float(0.5), FormulaDefault::Float(1.0)])
        );
    }
}