hamelin_translation 0.3.10

Lowering and IR for Hamelin query language
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
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
//! Intermediate Representation (IR) for lowered Hamelin queries.
//!
//! This module defines the IR types that represent normalized, lowered Hamelin queries
//! suitable for backend translation (DataFusion, Trino, etc.).
//!
//! # Invariants
//!
//! The IR enforces several constraints that make backend translation simpler:
//!
//! - **No compound identifiers in assignments**: All assignments use `SimpleIdentifier`
//! - **No Range type**: Range operators are lowered to struct literals
//! - **No Rows type**: Rows values are handled during lowering
//! - **No LET/DROP commands**: Fused into SELECT
//! - **No WITHIN command**: Converted to WHERE
//! - **No UNNEST (struct)**: Converted to SELECT with field extraction
//! - **No PARSE**: Converted to LET + WHERE
//!
//! # Expression Handling
//!
//! `IRExpression` is a thin wrapper around `TypedExpression`. We don't duplicate
//! the expression tree - we just ensure no banned constructs exist.

mod assignment_tree;
mod freeze;

use assignment_tree::AssignmentTree;
use freeze::FreezeAlgebra;

use std::rc::Rc;

use ordermap::OrderMap;

use hamelin_eval::{eval, Environment};
use hamelin_lib::err::TranslationError;
use hamelin_lib::tree::ast::clause::SortOrder;
use hamelin_lib::tree::ast::identifier::{Identifier, SimpleIdentifier};
use hamelin_lib::tree::ast::node::Span;
use hamelin_lib::tree::typed_ast::clause::{Projections, TypedFromClause};
use hamelin_lib::tree::typed_ast::command::{
    SideEffect, TypedAggCommand, TypedAppendCommand, TypedCommand, TypedCommandKind,
    TypedExplodeCommand, TypedFromCommand, TypedJoinCommand, TypedLimitCommand, TypedLookupCommand,
    TypedSelectCommand, TypedSortCommand, TypedSortExpression, TypedUnionCommand,
    TypedWhereCommand, TypedWindowCommand,
};
use hamelin_lib::tree::typed_ast::context::StatementTranslationContext;
use hamelin_lib::tree::typed_ast::environment::TypeEnvironment;
use hamelin_lib::tree::typed_ast::expression::TypedExpression;
use hamelin_lib::tree::typed_ast::pipeline::TypedPipeline;
use hamelin_lib::tree::typed_ast::query::TypedStatement;
use hamelin_lib::types::Type;

use crate::window_frame::WindowFrame;

/// A lowered Hamelin statement with optional CTEs.
#[derive(Debug, Clone)]
pub struct IRStatement {
    /// WITH clauses (CTEs)
    pub with_clauses: Vec<IRWithClause>,

    /// The main pipeline
    pub pipeline: Rc<IRPipeline>,

    /// Side effect (None for pure queries, some for DML/DDL)
    pub side_effect: SideEffect,
}

/// A WITH clause (Common Table Expression).
#[derive(Debug, Clone)]
pub struct IRWithClause {
    /// The CTE name (always simple identifier)
    pub name: SimpleIdentifier,

    /// The CTE's pipeline
    pub pipeline: Rc<IRPipeline>,
}

/// A lowered pipeline - a sequence of IR commands.
#[derive(Debug, Clone)]
pub struct IRPipeline {
    /// The sequence of commands
    pub commands: Vec<IRCommand>,

    /// Output schema of this pipeline
    pub output_schema: Rc<TypeEnvironment>,
}

/// A lowered command with its output schema.
#[derive(Debug, Clone)]
pub struct IRCommand {
    /// The command kind
    pub kind: IRCommandKind,

    /// Source location for error reporting
    pub span: Span,

    /// Output schema after this command
    pub output_schema: Rc<TypeEnvironment>,
}

/// The kind of IR command.
///
/// Commands that don't appear here have been lowered away:
/// - `LET` → fused into `SELECT`
/// - `DROP` → fused into `SELECT`
/// - `WITHIN` → converted to `WHERE`
/// - `UNNEST` (struct) → converted to `SELECT`
/// - `PARSE` → converted to `LET` + `WHERE`
/// - `NEST` → converted to `SELECT` with struct literal
/// - `UNION` → represented via `IRFromCommand` with multiple inputs
/// - `JOIN` / `LOOKUP` → both lowered to `Join` with appropriate `JoinType`
#[derive(Debug, Clone)]
pub enum IRCommandKind {
    From(IRFromCommand),
    Where(IRWhereCommand),
    Select(IRSelectCommand),
    Agg(IRAggCommand),
    Window(IRWindowCommand),
    Sort(IRSortCommand),
    Limit(IRLimitCommand),
    Explode(IRExplodeCommand),
    Join(IRJoinCommand),
    Append(IRAppendCommand),
}

/// FROM command: union of one or more inputs (tables or WITH references).
///
/// When there are multiple inputs, this represents a UNION ALL operation.
/// The schemas of all inputs must be compatible (handled by lowering).
#[derive(Debug, Clone)]
pub struct IRFromCommand {
    /// The input sources (one or more)
    pub inputs: Vec<IRInput>,
}

/// An input source for FROM.
#[derive(Debug, Clone)]
pub enum IRInput {
    /// Direct table reference (can be catalog.schema.table)
    Table(Identifier),

    /// Reference to a WITH clause, carrying the pipeline for backends that inline CTEs
    With(SimpleIdentifier, Rc<IRPipeline>),
}

/// WHERE command: filter predicate.
#[derive(Debug, Clone)]
pub struct IRWhereCommand {
    /// The filter predicate
    pub predicate: IRExpression,
}

/// SELECT command: projections with SimpleIdentifier-only assignments.
#[derive(Debug, Clone)]
pub struct IRSelectCommand {
    /// The projection assignments
    pub assignments: Vec<IRAssignment>,
}

/// AGG command: aggregation with SimpleIdentifier-only assignments.
#[derive(Debug, Clone)]
pub struct IRAggCommand {
    /// Aggregate expressions (e.g., sum(x), count(*))
    pub aggregates: Vec<IRAssignment>,

    /// Group by expressions
    pub group_by: Vec<IRAssignment>,

    /// Sort expressions for ordered aggregation
    pub sort_by: Vec<IRSortExpression>,
}

/// WINDOW command: window functions with SimpleIdentifier-only assignments.
#[derive(Debug, Clone)]
pub struct IRWindowCommand {
    /// Window function projections
    pub projections: Vec<IRAssignment>,

    /// Partition by expressions
    pub partition_by: Vec<IRAssignment>,

    /// Sort expressions within each partition
    pub sort_by: Vec<IRSortExpression>,

    /// Window frame specification (constructed during lowering via eval)
    pub frame: Option<WindowFrame>,
}

/// SORT command: ordering.
#[derive(Debug, Clone)]
pub struct IRSortCommand {
    /// Sort expressions
    pub sort_by: Vec<IRSortExpression>,
}

/// A sort expression with direction.
#[derive(Debug, Clone)]
pub struct IRSortExpression {
    /// The expression to sort by
    pub expression: IRExpression,

    /// Sort order (ASC or DESC)
    pub order: SortOrder,
}

/// LIMIT command: row count.
#[derive(Debug, Clone)]
pub struct IRLimitCommand {
    /// The count expression (should be a constant after lowering)
    pub count: IRExpression,
}

/// EXPLODE command: in-place array expansion.
///
/// Transforms a column from `ARRAY<T>` to `T`, creating one row per array element.
/// The column keeps its name but changes type.
///
/// All other transformations (assignments, projections) must be done via
/// preceding LET commands and following SELECT commands.
#[derive(Debug, Clone)]
pub struct IRExplodeCommand {
    /// The array column to explode in-place (must be a simple column reference)
    pub column: SimpleIdentifier,
}

/// The type of join operation.
///
/// This enum is introduced at the IR level (not TypedAST) because:
/// - TypedAST preserves user intent (separate JOIN and LOOKUP commands)
/// - IR introduces lowering concepts (both become JOIN with different types)
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum JoinType {
    /// INNER join - only matching rows from both sides
    Inner,

    /// LEFT join - all rows from left, matching rows from right (or NULL)
    Left,
}

/// JOIN command: combines rows from two tables.
///
/// Both Hamelin JOIN (INNER) and LOOKUP (LEFT) commands lower to this IR node.
/// The right side is always a CTE reference (hoisted by the `lower_joins` pass).
///
/// Missing ON conditions are lowered to `true` (CROSS JOIN semantics).
#[derive(Debug, Clone)]
pub struct IRJoinCommand {
    /// The type of join (Inner or Left)
    pub join_type: JoinType,

    /// The right side CTE reference (always a simple identifier after lowering)
    pub right: SimpleIdentifier,

    /// The join condition (always present - defaults to `true` for CROSS JOIN)
    pub condition: IRExpression,
}

/// APPEND command: write pipeline results to a table (DML).
///
/// This is a data modification command that appends rows to a target table.
/// Optional DISTINCT BY clause for deduplication before insert.
#[derive(Debug, Clone)]
pub struct IRAppendCommand {
    /// Target table to append to
    pub table: Identifier,

    /// Optional DISTINCT BY columns for deduplication
    pub distinct_by: Vec<SimpleIdentifier>,
}

/// Assignment to a simple identifier.
///
/// All assignments in the IR use simple identifiers (never compound).
/// Compound identifier assignments are lowered before reaching the IR.
#[derive(Debug, Clone)]
pub struct IRAssignment {
    /// The target identifier (always simple)
    pub identifier: SimpleIdentifier,

    /// The expression to assign
    pub expression: IRExpression,
}

/// A lowered expression - wrapper around TypedExpression.
///
/// # Invariants
///
/// No Range-related expression kinds exist in the tree.
/// Backends do not need to translate range operators.
#[derive(Debug, Clone)]
pub struct IRExpression(pub Rc<TypedExpression>);

impl IRExpression {
    /// Create a new IR expression from a typed expression.
    ///
    /// # Safety (logical)
    ///
    /// The caller must ensure the expression does not contain banned constructs
    /// (Range operators, etc.). This is enforced by the lowering pass.
    pub fn new(expr: Rc<TypedExpression>) -> Self {
        Self(expr)
    }

    /// Get the inner typed expression.
    pub fn inner(&self) -> &TypedExpression {
        &self.0
    }

    /// Get the resolved type of this expression.
    pub fn resolved_type(&self) -> &Type {
        &self.0.resolved_type
    }

    /// Get the source span for error reporting.
    pub fn span(&self) -> &Span {
        &self.0.ast.span
    }

    /// Freeze the expression by evaluating all constant subexpressions.
    ///
    /// Walks the tree bottom-up, attempting to evaluate each node with an empty
    /// environment. Nodes that can be fully evaluated are replaced with their
    /// literal values. Nodes that cannot be evaluated (e.g., column references)
    /// are kept, but their children may be simplified.
    ///
    /// This is used for query-time binding of functions like `now()`, `today()`, etc.
    pub fn freeze(&self) -> Self {
        let mut alg = FreezeAlgebra;
        let result = self.0.cata(&mut alg);
        match result {
            Ok(value) => IRExpression::new(value.into()),
            Err(expr) => IRExpression::new(expr),
        }
    }
}

// ============================================================================
// Conversion from TypedAST to IR
// ============================================================================

impl IRStatement {
    /// Convert a normalized TypedStatement to IR.
    ///
    /// The statement must have been normalized (all passes run) before calling this.
    /// This validates IR invariants and bails on unexpected constructs.
    pub fn from_typed(
        statement: Rc<TypedStatement>,
        ctx: &mut StatementTranslationContext,
    ) -> Result<Self, Rc<TranslationError>> {
        // Convert WITH clauses
        let mut with_clauses = Vec::new();
        for wc in &statement.with_clauses {
            let name = wc
                .name
                .valid_ref()?
                .clone()
                .try_unwrap_simple()
                .map_err(|id| {
                    ctx.error(format!("CTE name must be simple identifier, got: {}", id))
                        .emit()
                })?;
            let pipeline = IRPipeline::from_typed(wc.pipeline.clone(), ctx)?;
            with_clauses.push(IRWithClause {
                name,
                pipeline: Rc::new(pipeline),
            });
        }

        // Convert main pipeline
        let pipeline = IRPipeline::from_typed(statement.pipeline.clone(), ctx)?;

        Ok(Self {
            with_clauses,
            pipeline: Rc::new(pipeline),
            side_effect: statement.side_effect.clone(),
        })
    }
}

impl IRPipeline {
    /// Convert a normalized TypedPipeline to IR.
    pub fn from_typed(
        pipeline: Rc<TypedPipeline>,
        ctx: &mut StatementTranslationContext,
    ) -> Result<Self, Rc<TranslationError>> {
        let valid = pipeline.valid_ref()?;

        let mut commands = Vec::new();
        for cmd in &valid.commands {
            commands.push(IRCommand::from_typed(cmd, ctx)?);
        }

        Ok(Self {
            commands,
            output_schema: valid.final_schema.clone(),
        })
    }
}

impl IRCommand {
    /// Convert a TypedCommand to IR.
    pub fn from_typed(
        cmd: &Rc<TypedCommand>,
        ctx: &mut StatementTranslationContext,
    ) -> Result<Self, Rc<TranslationError>> {
        let kind = IRCommandKind::from_typed(&cmd.kind, ctx)?;

        Ok(Self {
            kind,
            span: cmd.ast.span,
            output_schema: cmd.output_schema.clone(),
        })
    }
}

impl IRCommandKind {
    /// Convert a TypedCommandKind to IR.
    ///
    /// Commands that should have been lowered away (LET, DROP, WITHIN, etc.)
    /// cause an error.
    fn from_typed(
        kind: &TypedCommandKind,
        ctx: &mut StatementTranslationContext,
    ) -> Result<Self, Rc<TranslationError>> {
        match kind {
            TypedCommandKind::From(from_cmd) => {
                Ok(IRFromCommand::from_typed(from_cmd, ctx)?.into())
            }
            TypedCommandKind::Where(where_cmd) => Ok(IRWhereCommand::from_typed(where_cmd).into()),
            TypedCommandKind::Select(select_cmd) => {
                Ok(IRSelectCommand::from_typed(select_cmd)?.into())
            }
            TypedCommandKind::Agg(agg_cmd) => Ok(IRAggCommand::from_typed(agg_cmd)?.into()),
            TypedCommandKind::Window(window_cmd) => {
                Ok(IRWindowCommand::from_typed(window_cmd)?.into())
            }
            TypedCommandKind::Sort(sort_cmd) => Ok(IRSortCommand::from_typed(sort_cmd).into()),
            TypedCommandKind::Limit(limit_cmd) => Ok(IRLimitCommand::from_typed(limit_cmd).into()),
            TypedCommandKind::Explode(explode_cmd) => {
                Ok(IRExplodeCommand::from_typed(explode_cmd, ctx)?.into())
            }

            // Commands that should have been lowered away
            TypedCommandKind::Let(_) => Err(ctx
                .error("LET command should have been fused into SELECT during normalization")
                .emit()),
            TypedCommandKind::Drop(_) => Err(ctx
                .error("DROP command should have been fused into SELECT during normalization")
                .emit()),
            TypedCommandKind::Within(_) => Err(ctx
                .error("WITHIN command should have been converted to WHERE during normalization")
                .emit()),
            TypedCommandKind::Parse(_) => Err(ctx
                .error("PARSE command should have been lowered to LET + WHERE during normalization")
                .emit()),
            TypedCommandKind::Unnest(_) => Err(ctx
                .error("UNNEST command should have been lowered during normalization")
                .emit()),

            TypedCommandKind::Join(join_cmd) => {
                Ok(IRJoinCommand::from_typed_join(join_cmd, ctx)?.into())
            }
            TypedCommandKind::Lookup(lookup_cmd) => {
                Ok(IRJoinCommand::from_typed_lookup(lookup_cmd, ctx)?.into())
            }
            TypedCommandKind::Append(append_cmd) => {
                Ok(IRAppendCommand::from_typed(append_cmd, ctx)?.into())
            }

            TypedCommandKind::Union(union_cmd) => {
                Ok(IRFromCommand::from_union(union_cmd, ctx)?.into())
            }
            TypedCommandKind::Match(_) => Err(ctx
                .error("MATCH command should have been lowered during normalization")
                .emit()),

            // Commands that should have been lowered away by normalization
            TypedCommandKind::Nest(_) => Err(ctx
                .error("NEST command should have been lowered to SELECT during normalization")
                .emit()),

            TypedCommandKind::Error(err) => Err(err.clone()),
        }
    }
}

impl IRFromCommand {
    fn from_typed(
        from_cmd: &TypedFromCommand,
        ctx: &mut StatementTranslationContext,
    ) -> Result<Self, Rc<TranslationError>> {
        let mut inputs = Vec::new();
        for clause in &from_cmd.clauses {
            inputs.push(IRInput::from_typed(clause, ctx)?);
        }
        Ok(Self { inputs })
    }

    fn from_union(
        union_cmd: &TypedUnionCommand,
        ctx: &mut StatementTranslationContext,
    ) -> Result<Self, Rc<TranslationError>> {
        let mut inputs = Vec::new();
        for clause in &union_cmd.clauses {
            inputs.push(IRInput::from_typed(clause, ctx)?);
        }
        Ok(Self { inputs })
    }
}

impl IRInput {
    fn from_typed(
        clause: &TypedFromClause,
        ctx: &mut StatementTranslationContext,
    ) -> Result<Self, Rc<TranslationError>> {
        match clause {
            TypedFromClause::Reference(table_ref) => Ok(IRInput::Table(
                table_ref.ast.identifier.valid_ref()?.clone(),
            )),
            TypedFromClause::Alias(_) => Err(ctx
                .error("FROM aliases should have been converted to CTEs during normalization")
                .emit()),
            TypedFromClause::Error(err) => Err(err.clone()),
        }
    }
}

impl IRWhereCommand {
    fn from_typed(where_cmd: &TypedWhereCommand) -> Self {
        Self {
            predicate: IRExpression::new(where_cmd.predicate.clone()),
        }
    }
}

impl IRSelectCommand {
    fn from_typed(select_cmd: &TypedSelectCommand) -> Result<Self, Rc<TranslationError>> {
        let assignments = convert_projections(&select_cmd.projections)?;
        Ok(Self { assignments })
    }
}

impl IRAggCommand {
    fn from_typed(agg_cmd: &TypedAggCommand) -> Result<Self, Rc<TranslationError>> {
        let aggregates = convert_projections(&agg_cmd.aggregates)?;
        let group_by = convert_projections(&agg_cmd.group_by)?;
        let sort_by = convert_sort_expressions(&agg_cmd.sort_by);

        Ok(Self {
            aggregates,
            group_by,
            sort_by,
        })
    }
}

impl IRWindowCommand {
    fn from_typed(window_cmd: &TypedWindowCommand) -> Result<Self, Rc<TranslationError>> {
        let projections = convert_projections(&window_cmd.projections)?;
        let partition_by = convert_projections(&window_cmd.group_by)?;
        let sort_by = convert_sort_expressions(&window_cmd.sort_by);

        // Compute the window frame from the WITHIN expression during lowering
        let frame = window_cmd
            .within
            .as_ref()
            .map(|within_expr| eval_within_to_frame(within_expr))
            .transpose()?;

        Ok(Self {
            projections,
            partition_by,
            sort_by,
            frame,
        })
    }
}

/// Evaluate a WITHIN expression and convert it to a WindowFrame.
///
/// The WITHIN expression must be constant (not reference any columns).
/// Returns an error if evaluation fails or if the value can't be converted to a frame.
fn eval_within_to_frame(
    within_expr: &TypedExpression,
) -> Result<WindowFrame, Rc<TranslationError>> {
    use hamelin_lib::err::Context;
    use hamelin_lib::tree::typed_ast::expression::{TypedErrorExpression, TypedExpressionKind};

    // Check for errors in the expression tree first
    if let Some(err_expr) =
        within_expr.find(&mut |e| matches!(&e.kind, TypedExpressionKind::Error(_)))
    {
        if let TypedExpressionKind::Error(TypedErrorExpression { error }) = &err_expr.kind {
            return Err(error.clone());
        }
    }

    // Check for non-deterministic functions (now(), today(), yesterday(), tomorrow())
    // These are not allowed in WITHIN expressions because window frames must be constant
    if let Some(bad_func_expr) = within_expr.find(&mut |e| {
        matches!(&e.kind, TypedExpressionKind::Apply(apply) if !apply.function_def.is_deterministic())
    }) {
        if let TypedExpressionKind::Apply(apply) = &bad_func_expr.kind {
            let span = within_expr.ast.span.to_range().unwrap_or(0..=0);
            let error = TranslationError::new(Context::new(
                span,
                &format!(
                    "WITHIN expression cannot use non-deterministic function '{}' - window frames must be constant",
                    apply.function_def.name()
                ),
            ));
            return Err(error.into());
        }
    }

    // Try to evaluate the expression with an empty environment
    let empty_env = Environment::default();
    match eval(within_expr, &empty_env) {
        Ok(value) => {
            // Convert the evaluated value to a WindowFrame
            WindowFrame::from_value(value).map_err(|msg| {
                let span = within_expr.ast.span.to_range().unwrap_or(0..=0);
                TranslationError::new(Context::new(
                    span,
                    &format!("Invalid window frame: {}", msg),
                ))
                .into()
            })
        }
        Err(eval_err) => {
            // Evaluation failed - likely because the expression references columns
            let span = within_expr.ast.span.to_range().unwrap_or(0..=0);
            let error = TranslationError::new(Context::new(
                span,
                &format!(
                    "WITHIN expression must be constant (cannot reference columns): {}",
                    eval_err
                ),
            ));
            Err(error.into())
        }
    }
}

impl IRSortCommand {
    fn from_typed(sort_cmd: &TypedSortCommand) -> Self {
        Self {
            sort_by: convert_sort_expressions(&sort_cmd.expressions),
        }
    }
}

impl IRLimitCommand {
    fn from_typed(limit_cmd: &TypedLimitCommand) -> Self {
        Self {
            count: IRExpression::new(limit_cmd.count.clone()),
        }
    }
}

impl IRExplodeCommand {
    fn from_typed(
        explode_cmd: &TypedExplodeCommand,
        ctx: &mut StatementTranslationContext,
    ) -> Result<Self, Rc<TranslationError>> {
        // After normalization, EXPLODE should be in canonical form: EXPLODE col = col
        let column = explode_cmd
            .identifier
            .valid_ref()?
            .clone()
            .try_unwrap_simple()
            .map_err(|id| {
                ctx.error(format!(
                    "EXPLODE identifier must be simple after normalization, got: {}",
                    id
                ))
                .emit()
            })?;

        Ok(Self { column })
    }
}

impl IRJoinCommand {
    /// Convert a TypedJoinCommand to IRJoinCommand (INNER join).
    fn from_typed_join(
        join_cmd: &TypedJoinCommand,
        ctx: &mut StatementTranslationContext,
    ) -> Result<Self, Rc<TranslationError>> {
        Self::from_typed_inner(JoinType::Inner, &join_cmd.right, &join_cmd.condition, ctx)
    }

    /// Convert a TypedLookupCommand to IRJoinCommand (LEFT join).
    fn from_typed_lookup(
        lookup_cmd: &TypedLookupCommand,
        ctx: &mut StatementTranslationContext,
    ) -> Result<Self, Rc<TranslationError>> {
        Self::from_typed_inner(
            JoinType::Left,
            &lookup_cmd.right,
            &lookup_cmd.condition,
            ctx,
        )
    }

    /// Shared implementation for both Join and Lookup.
    fn from_typed_inner(
        join_type: JoinType,
        right: &hamelin_lib::tree::typed_ast::clause::TypedTableAlias,
        condition: &Option<Rc<TypedExpression>>,
        ctx: &mut StatementTranslationContext,
    ) -> Result<Self, Rc<TranslationError>> {
        // After lower_joins pass, the right side should reference a CTE (simple identifier)
        let right_id = right
            .ast
            .table
            .identifier
            .valid_ref()?
            .clone()
            .try_unwrap_simple()
            .map_err(|id| {
                ctx.error(format!(
                    "JOIN right side must be simple identifier after lowering, got: {}",
                    id
                ))
                .emit()
            })?;

        // After lower_joins pass, condition is always present (defaults to `true`)
        let condition = condition.as_ref().ok_or_else(|| {
            ctx.error("JOIN condition should be present after lower_joins normalization")
                .emit()
        })?;

        Ok(Self {
            join_type,
            right: right_id,
            condition: IRExpression::new(condition.clone()),
        })
    }
}

impl IRAppendCommand {
    fn from_typed(
        append_cmd: &TypedAppendCommand,
        ctx: &mut StatementTranslationContext,
    ) -> Result<Self, Rc<TranslationError>> {
        let table = append_cmd.table.ast.identifier.valid_ref()?.clone();

        // Convert DISTINCT BY selections to simple identifiers
        let mut distinct_by = Vec::new();
        for selection in &append_cmd.distinct_by {
            let id = selection
                .ast
                .identifier
                .valid_ref()?
                .clone()
                .try_unwrap_simple()
                .map_err(|id| {
                    ctx.error(format!(
                        "APPEND DISTINCT BY must use simple identifiers, got: {}",
                        id
                    ))
                    .emit()
                })?;
            distinct_by.push(id);
        }

        Ok(Self { table, distinct_by })
    }
}

// ============================================================================
// Helper functions
// ============================================================================

/// Convert Projections to Vec<IRAssignment>, packing compound identifiers into struct literals.
///
/// Compound identifier assignments like `a.b.c = expr` are grouped and packed into
/// nested struct literals: `a = {b: {c: expr}}`.
fn convert_projections(
    projections: &Projections,
) -> Result<Vec<IRAssignment>, Rc<TranslationError>> {
    // Group assignments by root identifier, preserving order
    let mut groups: OrderMap<SimpleIdentifier, AssignmentTree> = OrderMap::new();

    for assignment in &projections.assignments {
        let identifier = assignment.identifier.valid_ref()?;
        let expression = assignment.expression.clone();

        match identifier {
            Identifier::Simple(simple) => {
                groups
                    .entry(simple.clone())
                    .or_default()
                    .insert_leaf(expression);
            }
            Identifier::Compound(compound) => {
                let root = compound.first();
                let path = &compound.parts[1..];
                groups
                    .entry(root)
                    .or_default()
                    .insert_at_path(path, expression);
            }
        }
    }

    // Convert groups to IRAssignments
    Ok(groups
        .into_iter()
        .map(|(identifier, tree)| IRAssignment {
            identifier,
            expression: tree.into_ir_expression(),
        })
        .collect())
}

/// Convert TypedSortExpressions to IRSortExpressions.
fn convert_sort_expressions(exprs: &[TypedSortExpression]) -> Vec<IRSortExpression> {
    exprs
        .iter()
        .map(|e| IRSortExpression {
            expression: IRExpression::new(e.expression.clone()),
            order: e.order.clone(),
        })
        .collect()
}

// ============================================================================
// From implementations for IRCommandKind
// ============================================================================

impl From<IRFromCommand> for IRCommandKind {
    fn from(cmd: IRFromCommand) -> Self {
        IRCommandKind::From(cmd)
    }
}

impl From<IRWhereCommand> for IRCommandKind {
    fn from(cmd: IRWhereCommand) -> Self {
        IRCommandKind::Where(cmd)
    }
}

impl From<IRSelectCommand> for IRCommandKind {
    fn from(cmd: IRSelectCommand) -> Self {
        IRCommandKind::Select(cmd)
    }
}

impl From<IRAggCommand> for IRCommandKind {
    fn from(cmd: IRAggCommand) -> Self {
        IRCommandKind::Agg(cmd)
    }
}

impl From<IRWindowCommand> for IRCommandKind {
    fn from(cmd: IRWindowCommand) -> Self {
        IRCommandKind::Window(cmd)
    }
}

impl From<IRSortCommand> for IRCommandKind {
    fn from(cmd: IRSortCommand) -> Self {
        IRCommandKind::Sort(cmd)
    }
}

impl From<IRLimitCommand> for IRCommandKind {
    fn from(cmd: IRLimitCommand) -> Self {
        IRCommandKind::Limit(cmd)
    }
}

impl From<IRExplodeCommand> for IRCommandKind {
    fn from(cmd: IRExplodeCommand) -> Self {
        IRCommandKind::Explode(cmd)
    }
}

impl From<IRJoinCommand> for IRCommandKind {
    fn from(cmd: IRJoinCommand) -> Self {
        IRCommandKind::Join(cmd)
    }
}

impl From<IRAppendCommand> for IRCommandKind {
    fn from(cmd: IRAppendCommand) -> Self {
        IRCommandKind::Append(cmd)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use hamelin_eval::{eval, value::Value, Environment};
    use hamelin_lib::tree::{
        ast::{pipeline::Pipeline, IntoTyped, TypeCheckExecutor},
        builder::{ident, pipeline, select_command},
        typed_ast::expression::TypedExpressionKind,
    };
    use hamelin_lib::types::{struct_type::Struct, INT, STRING};
    use pretty_assertions::assert_eq;
    use rstest::rstest;

    /// Test helper to extract the IRSelectCommand from a simple pipeline
    fn get_ir_select(pipeline: Pipeline) -> IRSelectCommand {
        let typed = pipeline.typed_with().typed();
        let select_cmd = typed.valid_ref().unwrap().commands[0].clone();
        if let TypedCommandKind::Select(select) = &select_cmd.kind {
            IRSelectCommand::from_typed(select).unwrap()
        } else {
            panic!("Expected SELECT command");
        }
    }

    /// Test helper to extract identifier and check expression type from assignment
    fn assignment_info(assignment: &IRAssignment) -> (String, &Type) {
        (
            assignment.identifier.to_string(),
            assignment.expression.resolved_type(),
        )
    }

    #[rstest]
    // Simple assignments pass through unchanged
    #[case::simple_assignments(
        pipeline()
            .command(select_command()
                .named_field("a", 1)
                .named_field("b", "hello")
                .build())
            .build(),
        vec![("a", INT.clone()), ("b", STRING.clone())]
    )]
    fn test_simple_assignments_no_packing(
        #[case] input: Pipeline,
        #[case] expected: Vec<(&str, Type)>,
    ) {
        let ir_select = get_ir_select(input);
        assert_eq!(ir_select.assignments.len(), expected.len());
        for (assignment, (expected_name, expected_type)) in
            ir_select.assignments.iter().zip(expected.iter())
        {
            let (name, resolved) = assignment_info(assignment);
            assert_eq!(name, *expected_name);
            assert_eq!(resolved, expected_type);
        }
    }

    #[rstest]
    // Compound identifiers with same root get packed into struct literal
    #[case::compound_same_root_packs_to_struct(
        pipeline()
            .command(select_command()
                .named_field(ident("x").dot("a"), 1)
                .named_field(ident("x").dot("b"), "hello")
                .build())
            .build(),
        "x",
        Struct::default().with_str("a", INT).with_str("b", STRING).into()
    )]
    // Single compound identifier gets packed
    #[case::single_compound_packs(
        pipeline()
            .command(select_command()
                .named_field(ident("user").dot("id"), 42)
                .build())
            .build(),
        "user",
        Struct::default().with_str("id", INT).into()
    )]
    fn test_compound_identifiers_pack_to_struct(
        #[case] input: Pipeline,
        #[case] expected_name: &str,
        #[case] expected_type: Type,
    ) {
        let ir_select = get_ir_select(input);
        assert_eq!(ir_select.assignments.len(), 1);
        let (name, resolved) = assignment_info(&ir_select.assignments[0]);
        assert_eq!(name, expected_name);
        assert_eq!(resolved, &expected_type);
    }

    #[rstest]
    // Deep nesting: a.b.c = 1 becomes a = {b: {c: 1}}
    #[case::deep_nesting(
        pipeline()
            .command(select_command()
                .named_field(ident("a").dot("b").dot("c"), 1)
                .build())
            .build(),
        "a",
        Struct::default().with_str("b",
            Struct::default().with_str("c", INT).into()).into()
    )]
    fn test_deep_nesting_packs_correctly(
        #[case] input: Pipeline,
        #[case] expected_name: &str,
        #[case] expected_type: Type,
    ) {
        let ir_select = get_ir_select(input);
        assert_eq!(ir_select.assignments.len(), 1);
        let (name, resolved) = assignment_info(&ir_select.assignments[0]);
        assert_eq!(name, expected_name);
        assert_eq!(resolved, &expected_type);
    }

    #[rstest]
    // Mixed: simple and compound with different roots stay separate
    #[case::mixed_simple_and_compound(
        pipeline()
            .command(select_command()
                .named_field("simple", 1)
                .named_field(ident("nested").dot("field"), 2)
                .build())
            .build(),
        vec![
            ("simple", INT.clone()),
            ("nested", Struct::default().with_str("field", INT).into()),
        ]
    )]
    // Multiple different roots each become their own struct
    #[case::multiple_roots(
        pipeline()
            .command(select_command()
                .named_field(ident("a").dot("x"), 1)
                .named_field(ident("b").dot("y"), 2)
                .build())
            .build(),
        vec![
            ("a", Struct::default().with_str("x", INT).into()),
            ("b", Struct::default().with_str("y", INT).into()),
        ]
    )]
    fn test_mixed_assignments(#[case] input: Pipeline, #[case] expected: Vec<(&str, Type)>) {
        let ir_select = get_ir_select(input);
        assert_eq!(ir_select.assignments.len(), expected.len());
        for (assignment, (expected_name, expected_type)) in
            ir_select.assignments.iter().zip(expected.iter())
        {
            let (name, resolved) = assignment_info(assignment);
            assert_eq!(name, *expected_name);
            assert_eq!(resolved, expected_type);
        }
    }

    #[test]
    fn test_order_preserved() {
        // Order of assignments should be preserved based on first occurrence of root
        let input = pipeline()
            .command(
                select_command()
                    .named_field(ident("z").dot("first"), 1)
                    .named_field(ident("a").dot("second"), 2)
                    .named_field(ident("z").dot("third"), 3)
                    .build(),
            )
            .build();

        let ir_select = get_ir_select(input);
        assert_eq!(ir_select.assignments.len(), 2);

        // z should come first (first occurrence), then a
        assert_eq!(ir_select.assignments[0].identifier.to_string(), "z");
        assert_eq!(ir_select.assignments[1].identifier.to_string(), "a");

        // z should have both fields packed
        let z_type = ir_select.assignments[0].expression.resolved_type();
        let expected_z = Struct::default()
            .with_str("first", INT)
            .with_str("third", INT);
        assert_eq!(z_type, &Type::from(expected_z));
    }

    #[test]
    fn test_expressions_preserved_in_packed_structs() {
        // Expressions should be preserved correctly in packed structs
        // Use literals since column_ref without FROM context gets Unknown type
        let input = pipeline()
            .command(
                select_command()
                    .named_field(ident("nested").dot("int_field"), 42)
                    .named_field(ident("nested").dot("str_field"), "hello")
                    .build(),
            )
            .build();

        let ir_select = get_ir_select(input);
        assert_eq!(ir_select.assignments.len(), 1);

        // The nested struct should have both fields with correct types
        let nested = &ir_select.assignments[0];
        assert_eq!(nested.identifier.to_string(), "nested");
        let expected_type: Type = Struct::default()
            .with_str("int_field", INT)
            .with_str("str_field", STRING)
            .into();
        assert_eq!(nested.expression.resolved_type(), &expected_type);
    }

    #[test]
    fn test_freeze_resolves_now_in_within() {
        use crate::lower::lower;
        use hamelin_lib::tree::builder::{call, hours, string};

        // Build: SELECT timestamp = ts("2024-01-15T12:00:00Z") | WITHIN -5h
        let input = pipeline()
            .command(
                select_command()
                    .named_field("timestamp", call("ts").arg(string("2024-01-15T12:00:00Z")))
                    .build(),
            )
            .within(hours(-5))
            .build();

        // Type check and lower
        let typed = input.typed_with().typed();
        let query = hamelin_lib::tree::builder::query()
            .main(Rc::new(typed))
            .build();
        let typed_query = query.typed_with().typed();
        let ir = lower(Rc::new(typed_query)).expect("lowering should succeed");

        // The IR should have: FROM (implicit) -> SELECT -> WHERE
        // WITHIN -5h becomes WHERE timestamp >= now() + -5h AND timestamp <= now()
        // After freeze, now() should become ts("...") with resolved timestamps
        let where_cmd = ir
            .pipeline
            .commands
            .iter()
            .find(|cmd| matches!(cmd.kind, IRCommandKind::Where(_)))
            .expect("should have WHERE command");

        let IRCommandKind::Where(where_cmd) = &where_cmd.kind else {
            panic!("expected WHERE command");
        };

        // Freeze the predicate
        let frozen = where_cmd.predicate.freeze();

        // Check the TypedExpressionKind for now() and ts() calls
        fn has_now_apply(expr: &TypedExpression) -> bool {
            match &expr.kind {
                TypedExpressionKind::Apply(apply) => {
                    if apply.function_def.name() == "now" {
                        return true;
                    }
                    apply.parameter_binding.iter().any(|arg| has_now_apply(arg))
                }
                TypedExpressionKind::ArrayLiteral(arr) => {
                    arr.elements.iter().any(|e| has_now_apply(e))
                }
                TypedExpressionKind::TupleLiteral(tup) => {
                    tup.elements.iter().any(|e| has_now_apply(e))
                }
                TypedExpressionKind::StructLiteral(s) => {
                    s.fields.iter().any(|(_, e)| has_now_apply(e))
                }
                TypedExpressionKind::VariantIndexAccess(v) => has_now_apply(&v.value),
                TypedExpressionKind::FieldLookup(f) => has_now_apply(&f.value),
                TypedExpressionKind::Cast(c) => has_now_apply(&c.value),
                TypedExpressionKind::TsTrunc(t) => has_now_apply(&t.expression),
                TypedExpressionKind::BroadcastApply(b) => {
                    b.parameter_binding.iter().any(|arg| has_now_apply(arg))
                }
                TypedExpressionKind::ColumnReference(_)
                | TypedExpressionKind::Leaf
                | TypedExpressionKind::Lambda(_)
                | TypedExpressionKind::Error(_) => false,
            }
        }

        fn has_ts_apply(expr: &TypedExpression) -> bool {
            match &expr.kind {
                TypedExpressionKind::Apply(apply) => {
                    if apply.function_def.name() == "ts" {
                        return true;
                    }
                    apply.parameter_binding.iter().any(|arg| has_ts_apply(arg))
                }
                TypedExpressionKind::ArrayLiteral(arr) => {
                    arr.elements.iter().any(|e| has_ts_apply(e))
                }
                TypedExpressionKind::TupleLiteral(tup) => {
                    tup.elements.iter().any(|e| has_ts_apply(e))
                }
                TypedExpressionKind::StructLiteral(s) => {
                    s.fields.iter().any(|(_, e)| has_ts_apply(e))
                }
                TypedExpressionKind::VariantIndexAccess(v) => has_ts_apply(&v.value),
                TypedExpressionKind::FieldLookup(f) => has_ts_apply(&f.value),
                TypedExpressionKind::Cast(c) => has_ts_apply(&c.value),
                TypedExpressionKind::TsTrunc(t) => has_ts_apply(&t.expression),
                TypedExpressionKind::BroadcastApply(b) => {
                    b.parameter_binding.iter().any(|arg| has_ts_apply(arg))
                }
                TypedExpressionKind::ColumnReference(_)
                | TypedExpressionKind::Leaf
                | TypedExpressionKind::Lambda(_)
                | TypedExpressionKind::Error(_) => false,
            }
        }

        // Before freeze: should have now() calls
        assert!(
            has_now_apply(where_cmd.predicate.inner()),
            "before freeze should contain now() calls"
        );

        // After freeze: no now() calls, but should have ts() calls
        assert!(
            !has_now_apply(frozen.inner()),
            "frozen expression should not contain now() calls"
        );
        assert!(
            has_ts_apply(frozen.inner()),
            "frozen expression should contain ts() calls for resolved timestamps"
        );

        // Extract and verify the actual timestamp values are correct
        // The frozen expression should be: timestamp >= ts1 AND timestamp <= ts2
        // where ts1 ≈ now - 5h and ts2 ≈ now
        fn collect_ts_timestamps(expr: &TypedExpression) -> Vec<chrono::DateTime<chrono::Utc>> {
            let mut timestamps = Vec::new();
            collect_ts_timestamps_impl(expr, &mut timestamps);
            timestamps
        }

        fn collect_ts_timestamps_impl(
            expr: &TypedExpression,
            out: &mut Vec<chrono::DateTime<chrono::Utc>>,
        ) {
            match &expr.kind {
                TypedExpressionKind::Apply(apply) => {
                    if apply.function_def.name() == "ts" {
                        // Eval the ts() call to get the timestamp value
                        let env = Environment::default();
                        if let Ok(Value::Timestamp(ts)) = eval(expr, &env) {
                            out.push(*ts.instant());
                        }
                    }
                    for arg in apply.parameter_binding.iter() {
                        collect_ts_timestamps_impl(arg, out);
                    }
                }
                TypedExpressionKind::ArrayLiteral(arr) => {
                    for e in &arr.elements {
                        collect_ts_timestamps_impl(e, out);
                    }
                }
                TypedExpressionKind::TupleLiteral(tup) => {
                    for e in &tup.elements {
                        collect_ts_timestamps_impl(e, out);
                    }
                }
                TypedExpressionKind::StructLiteral(s) => {
                    for (_, e) in &s.fields {
                        collect_ts_timestamps_impl(e, out);
                    }
                }
                TypedExpressionKind::VariantIndexAccess(v) => {
                    collect_ts_timestamps_impl(&v.value, out)
                }
                TypedExpressionKind::FieldLookup(f) => collect_ts_timestamps_impl(&f.value, out),
                TypedExpressionKind::Cast(c) => collect_ts_timestamps_impl(&c.value, out),
                TypedExpressionKind::TsTrunc(t) => collect_ts_timestamps_impl(&t.expression, out),
                TypedExpressionKind::BroadcastApply(b) => {
                    for arg in b.parameter_binding.iter() {
                        collect_ts_timestamps_impl(arg, out);
                    }
                }
                TypedExpressionKind::ColumnReference(_)
                | TypedExpressionKind::Leaf
                | TypedExpressionKind::Lambda(_)
                | TypedExpressionKind::Error(_) => {}
            }
        }

        let timestamps = collect_ts_timestamps(frozen.inner());
        assert_eq!(
            timestamps.len(),
            2,
            "should have exactly 2 timestamp literals"
        );

        let now = chrono::Utc::now();
        let five_hours = chrono::Duration::hours(5);
        let one_minute = chrono::Duration::minutes(1);

        // One timestamp should be close to now - 5h, the other close to now
        let (earlier, later) = if timestamps[0] < timestamps[1] {
            (timestamps[0], timestamps[1])
        } else {
            (timestamps[1], timestamps[0])
        };

        let expected_earlier = now - five_hours;
        let expected_later = now;

        assert!(
            (earlier - expected_earlier).abs() < one_minute,
            "earlier timestamp {:?} should be within 1 minute of now - 5h ({:?})",
            earlier,
            expected_earlier
        );
        assert!(
            (later - expected_later).abs() < one_minute,
            "later timestamp {:?} should be within 1 minute of now ({:?})",
            later,
            expected_later
        );
    }
}