hamelin_lib 0.10.8

Core library 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
use super::expression::{Expression, ExpressionKind};
use crate::{
    antlr::{
        hamelinparser::{IdentifierContextAll, IdentifierContextAttrs, SimpleIdentifierContextAll},
        interval,
    },
    antlr::{parse_identifier, parse_simple_identifier},
    err::TranslationError,
    tree::ast::context::{FromCst, ParseContext},
};
use antlr_rust::tree::ParseTree;
use derive_more::derive::{From, TryUnwrap};
use serde::{Deserialize, Serialize};
use std::{
    fmt::{Display, Formatter},
    hash::{Hash, Hasher},
    ops::{Add, RangeInclusive},
    rc::Rc,
    str::FromStr,
    sync::Arc,
};

use super::node::{Span, Spannable};

/// A pure AST identifier that is either simple or compound (no errors)
#[derive(Debug, Clone, PartialEq, From, TryUnwrap, Hash, Eq)]
pub enum Identifier {
    Simple(SimpleIdentifier),
    Compound(CompoundIdentifier),
}

/// A parsed identifier that may contain an error from CST conversion
#[derive(Debug, Clone, PartialEq, From, TryUnwrap, Hash, Eq)]
pub enum ParsedIdentifier {
    Valid(Identifier),
    Error(Arc<TranslationError>),
}

/// A parsed simple identifier that may contain an error from CST conversion
#[derive(Debug, Clone, PartialEq, From, TryUnwrap, Hash, Eq)]
pub enum ParsedSimpleIdentifier {
    Valid(SimpleIdentifier),
    Error(Arc<TranslationError>),
}

/// Iterator over all strict prefixes of a compound identifier.
/// For `x.y.z`, yields `x`, then `x.y`.
pub struct IdentifierPrefixes<'a> {
    parts: &'a [SimpleIdentifier],
    current_len: usize,
}

impl<'a> Iterator for IdentifierPrefixes<'a> {
    type Item = Identifier;

    fn next(&mut self) -> Option<Self::Item> {
        // Stop when we've yielded all strict prefixes (not including the full identifier)
        if self.current_len >= self.parts.len() {
            return None;
        }

        let prefix = if self.current_len == 1 {
            // Single part - return as Simple
            Identifier::Simple(self.parts[0].clone())
        } else {
            // Multiple parts - return as Compound
            Identifier::Compound(CompoundIdentifier {
                parts: self.parts[0..self.current_len].to_vec(),
            })
        };

        self.current_len += 1;
        Some(prefix)
    }
}

impl Spannable for Identifier {
    fn span(&self) -> Option<RangeInclusive<usize>> {
        match self {
            Identifier::Simple(s) => s.span(),
            Identifier::Compound(c) => c.span(),
        }
    }
}

impl Display for Identifier {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Identifier::Simple(s) => write!(f, "{}", s),
            Identifier::Compound(c) => write!(f, "{}", c),
        }
    }
}

impl Serialize for Identifier {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        match self {
            Self::Simple(s) => serializer.serialize_str(&s.to_string()),
            Self::Compound(c) => serializer.serialize_str(&c.to_string()),
        }
    }
}

impl<'de> Deserialize<'de> for Identifier {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let s = String::deserialize(deserializer)?;
        if s.is_empty() {
            return Err(serde::de::Error::custom("empty identifier"));
        }
        Identifier::parse(&s).map_err(serde::de::Error::custom)
    }
}

impl FromCst<Rc<IdentifierContextAll<'static>>> for ParsedIdentifier {
    fn from_cst_with_context(
        cst: Rc<IdentifierContextAll<'static>>,
        ctx: &mut ParseContext,
    ) -> Self {
        // Parse all simple identifiers, short-circuit on first error
        let simple_idents: Vec<SimpleIdentifier> = match cst
            .simpleIdentifier_all()
            .into_iter()
            .map(|si| ParsedSimpleIdentifier::from_cst_with_context(si.as_ref(), ctx))
            .try_fold(Vec::new(), |mut acc, parsed| {
                parsed.valid().map(|si| {
                    acc.push(si);
                    acc
                })
            }) {
            Ok(idents) => idents,
            Err(e) => {
                ctx.add_error(e.clone());
                return e.into();
            }
        };

        // All parts are valid, build the identifier
        match simple_idents.as_slice() {
            [] => {
                let err = ctx.error("expected identifier").at(cst.as_ref()).emit();
                err.into()
            }
            [single] => Identifier::Simple(single.clone()).into(),
            [first, second, rest @ ..] => {
                CompoundIdentifier::new(first.clone(), second.clone(), rest.to_vec()).into()
            }
        }
    }
}

impl ParsedSimpleIdentifier {
    /// Extract a reference to the valid SimpleIdentifier or return the error
    pub fn valid_ref(&self) -> Result<&SimpleIdentifier, Arc<TranslationError>> {
        match self {
            ParsedSimpleIdentifier::Valid(si) => Ok(si),
            ParsedSimpleIdentifier::Error(err) => Err(err.clone()),
        }
    }

    /// Consume and extract the valid SimpleIdentifier or return the error
    pub fn valid(self) -> Result<SimpleIdentifier, Arc<TranslationError>> {
        match self {
            ParsedSimpleIdentifier::Valid(si) => Ok(si),
            ParsedSimpleIdentifier::Error(err) => Err(err),
        }
    }
}

impl ParsedIdentifier {
    /// Extract a reference to the valid Identifier or return the error
    pub fn valid_ref(&self) -> Result<&Identifier, Arc<TranslationError>> {
        match self {
            ParsedIdentifier::Valid(id) => Ok(id),
            ParsedIdentifier::Error(err) => Err(err.clone()),
        }
    }

    /// Consume and extract the valid Identifier or return the error
    pub fn valid(self) -> Result<Identifier, Arc<TranslationError>> {
        match self {
            ParsedIdentifier::Valid(id) => Ok(id),
            ParsedIdentifier::Error(err) => Err(err),
        }
    }
}

impl Display for ParsedIdentifier {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        self.valid_ref()
            .map(|id| write!(f, "{}", id))
            .unwrap_or_else(|_| write!(f, "<error>"))
    }
}

impl Spannable for ParsedIdentifier {
    fn span(&self) -> Option<RangeInclusive<usize>> {
        self.valid_ref().ok().and_then(|id| id.span())
    }
}

impl Display for ParsedSimpleIdentifier {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        self.valid_ref()
            .map(|id| write!(f, "{}", id))
            .unwrap_or_else(|_| write!(f, "<error>"))
    }
}

impl Spannable for ParsedSimpleIdentifier {
    fn span(&self) -> Option<RangeInclusive<usize>> {
        self.valid_ref().ok().and_then(|id| id.span())
    }
}

// Conversions from Identifier components to ParsedIdentifier
impl From<SimpleIdentifier> for ParsedIdentifier {
    fn from(si: SimpleIdentifier) -> Self {
        ParsedIdentifier::Valid(Identifier::Simple(si))
    }
}

impl From<CompoundIdentifier> for ParsedIdentifier {
    fn from(ci: CompoundIdentifier) -> Self {
        ParsedIdentifier::Valid(Identifier::Compound(ci))
    }
}

impl From<ParsedSimpleIdentifier> for ParsedIdentifier {
    fn from(value: ParsedSimpleIdentifier) -> Self {
        match value {
            ParsedSimpleIdentifier::Valid(si) => ParsedIdentifier::Valid(Identifier::Simple(si)),
            ParsedSimpleIdentifier::Error(err) => ParsedIdentifier::Error(err),
        }
    }
}

impl Identifier {
    pub fn parse(s: impl Into<String>) -> Result<Self, TranslationError> {
        let mut ctx = ParseContext::new();
        let cst = parse_identifier(s.into()).map_err(|errors| {
            errors
                .0
                .into_iter()
                .next()
                .unwrap_or_else(|| ctx.error("cannot parse identifier").emit().as_ref().clone())
        })?;
        match ParsedIdentifier::from_cst_with_context(cst, &mut ctx) {
            ParsedIdentifier::Valid(id) => Ok(id),
            ParsedIdentifier::Error(err) => Err((*err).clone()),
        }
    }

    /// Attempt to infer an identifier from an expression's AST structure.
    /// Returns None if the expression doesn't have an inferable identifier
    /// (caller should fall back to source text).
    ///
    /// This is used during CST→AST parsing to desugar bare expressions into
    /// assignments by inferring the field name from the expression structure.
    pub fn infer_from_expression(expr: &Expression) -> Option<ParsedIdentifier> {
        // First, check if we can strip off one wrapper operation
        let current = match &expr.kind {
            // Zero-arg function call: use the function name as identifier
            ExpressionKind::FunctionCall(func_call)
                if func_call.positional_args.is_empty() && func_call.named_args.is_empty() =>
            {
                return Some(match func_call.name.valid_ref() {
                    Ok(simple_id) => SimpleIdentifier::with_arc_name(simple_id.name.clone()).into(),
                    Err(err) => err.into(),
                });
            }
            // Timestamp truncation: unwrap to inner expression
            ExpressionKind::TsTrunc(tstrunc) => tstrunc.expression.as_ref(),
            // Cast: unwrap to inner expression
            ExpressionKind::Cast(cast) => cast.expression.as_ref(),
            // No wrapper to strip
            _ => expr,
        };

        // Collect field lookup chain by walking inward
        let mut fields = Vec::new();
        let mut inner = current;
        while let ExpressionKind::FieldLookup(field_lookup) = &inner.kind {
            fields.push(field_lookup.field_identifier.clone());
            inner = field_lookup.value.as_ref();
        }

        // Must be a field reference at the core
        match &inner.kind {
            ExpressionKind::FieldReference(col_ref) => {
                // Reverse fields since we collected them outside-in
                fields.reverse();
                match col_ref.field_name.valid_ref() {
                    Ok(field_name) => {
                        if fields.is_empty() {
                            // Just a field reference with no field lookup
                            Some(SimpleIdentifier::with_arc_name(field_name.name.clone()).into())
                        } else {
                            // Build compound identifier: base field + all lookups
                            let mut parts =
                                vec![SimpleIdentifier::with_arc_name(field_name.name.clone())];
                            for field in fields {
                                match field.valid() {
                                    Ok(simple_id) => parts.push(simple_id),
                                    Err(err) => return Some(err.into()),
                                }
                            }
                            Some(CompoundIdentifier { parts }.into())
                        }
                    }
                    Err(err) => Some(err.into()),
                }
            }
            _ => None, // Cannot infer - caller should use source text fallback
        }
    }

    /// Returns the first component of this identifier.
    pub fn first(&self) -> SimpleIdentifier {
        match self {
            Identifier::Simple(s) => s.first(),
            Identifier::Compound(c) => c.first(),
        }
    }

    /// Returns an iterator over all strict prefixes of this identifier.
    /// For example, `x.y.z` yields `x`, then `x.y`.
    /// Simple identifiers have no prefixes and return an empty iterator.
    pub fn prefixes(&self) -> IdentifierPrefixes<'_> {
        IdentifierPrefixes {
            parts: match self {
                Identifier::Compound(c) => &c.parts,
                _ => &[],
            },
            current_len: 1,
        }
    }

    /// Returns the rest of this identifier after removing the first component.
    /// Manually dispatches to the concrete type implementations.
    pub fn rest(&self) -> Option<Identifier> {
        match self {
            Identifier::Simple(s) => s.rest(),
            Identifier::Compound(c) => Some(c.rest()),
        }
    }

    /// Returns the last component of this identifier.
    pub fn last(&self) -> SimpleIdentifier {
        match self {
            Identifier::Simple(s) => s.last(),
            Identifier::Compound(c) => c.last(),
        }
    }

    /// Check if this identifier has the given prefix (without doing the stripping work).
    /// Returns true if `prefix` is a strict prefix of `self`.
    /// Returns false if they are equal or if prefix doesn't match.
    pub fn has_prefix(&self, prefix: &Identifier) -> bool {
        match self {
            Identifier::Simple(s) => s.has_prefix(prefix),
            Identifier::Compound(c) => c.has_prefix(prefix),
        }
    }

    /// Strip a prefix from this identifier, returning the suffix.
    /// Manually dispatches to the concrete type implementations.
    pub fn strip_prefix(&self, prefix: &Identifier) -> Option<Identifier> {
        match self {
            Identifier::Simple(s) => s.strip_prefix(prefix),
            Identifier::Compound(c) => c.strip_prefix(prefix),
        }
    }

    /// Returns a slice of SimpleIdentifier references for each segment of this identifier.
    /// For simple identifiers, returns a single-element slice.
    /// For compound identifiers, returns a slice of all parts.
    pub fn segments(&self) -> &[SimpleIdentifier] {
        match self {
            Identifier::Simple(s) => std::slice::from_ref(s),
            Identifier::Compound(c) => &c.parts,
        }
    }

    /// Build an identifier from a vec of segments.
    /// Returns `None` for empty, `Simple` for one, `Compound` for 2+.
    pub fn from_segments(segments: Vec<SimpleIdentifier>) -> Option<Identifier> {
        match segments.as_slice() {
            [] => None,
            [single] => Some(single.clone().into()),
            [first, second, rest @ ..] => {
                Some(CompoundIdentifier::new(first.clone(), second.clone(), rest.to_vec()).into())
            }
        }
    }

    /// Prepend a prefix identifier to this identifier, producing a compound identifier.
    pub fn prepend(self, prefix: Identifier) -> CompoundIdentifier {
        let mut parts = prefix.segments().to_vec();
        parts.extend(self.segments().iter().cloned());
        // We always have at least 2 parts (prefix has ≥1, self has ≥1)
        CompoundIdentifier { parts }
    }
}

/// A simple (unqualified) identifier
#[derive(Debug, Clone)]
pub struct SimpleIdentifier {
    pub span: Span,
    /// The identifier name, stored as Arc<str> for memory efficiency.
    /// When parsed with an interner, identical names share the same allocation.
    name: Arc<str>,
}

impl SimpleIdentifier {
    /// Create a new simple identifier without interning.
    ///
    /// This allocates a fresh `Arc<str>` for the name. For memory-efficient
    /// parsing with string deduplication, use `Pipeline::parse_with()` with
    /// an interner instead.
    pub fn new(name: impl Into<String>) -> Self {
        let s: String = name.into();
        Self {
            span: Span::NONE,
            name: Arc::from(s.as_str()),
        }
    }

    /// Create a simple identifier with a pre-existing `Arc<str>` name.
    ///
    /// This is used internally when we already have an interned or shared
    /// string and want to preserve the sharing.
    pub(crate) fn with_arc_name(name: Arc<str>) -> Self {
        Self {
            span: Span::NONE,
            name,
        }
    }

    /// Get the name as a string slice.
    pub fn as_str(&self) -> &str {
        &self.name
    }

    /// Get the name as a string slice.
    ///
    /// This is an ergonomic alias for `as_str()` that mirrors the field-access
    /// style of the old `pub name: String` field, easing the migration from
    /// `ident.name` to `ident.name()`.
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Check if this identifier can be represented as an unquoted Hamelin identifier.
    ///
    /// Returns `true` if the name round-trips through the Hamelin parser without
    /// needing backtick quoting. This catches reserved words (like `select`, `let`)
    /// that the character-level check in `Display` would miss.
    pub fn is_clean(&self) -> bool {
        parse_simple_identifier(self.name.to_string()).is_ok()
    }
}

impl AsRef<str> for SimpleIdentifier {
    fn as_ref(&self) -> &str {
        &self.name
    }
}

impl PartialEq<str> for SimpleIdentifier {
    fn eq(&self, other: &str) -> bool {
        &*self.name == other
    }
}

impl PartialEq<&str> for SimpleIdentifier {
    fn eq(&self, other: &&str) -> bool {
        &*self.name == *other
    }
}

impl SimpleIdentifier {
    pub fn parse(s: impl Into<String>) -> Result<Self, TranslationError> {
        let mut ctx = ParseContext::new();
        match parse_simple_identifier(s.into()) {
            Ok(cst) => {
                match ParsedSimpleIdentifier::from_cst_with_context(cst.as_ref(), &mut ctx) {
                    ParsedSimpleIdentifier::Valid(identifier) => Ok(identifier),
                    ParsedSimpleIdentifier::Error(err) => Err((*err).clone()),
                }
            }
            Err(parse_errors) => {
                let err =
                    parse_errors.0.into_iter().next().unwrap_or_else(|| {
                        ctx.error("cant parse identifier").emit().as_ref().clone()
                    });
                Err(err)
            }
        }
    }
}

impl Spannable for SimpleIdentifier {
    fn span(&self) -> Option<RangeInclusive<usize>> {
        self.span.to_range()
    }
}

impl FromCst<&SimpleIdentifierContextAll<'static>> for ParsedSimpleIdentifier {
    fn from_cst_with_context(
        cst: &SimpleIdentifierContextAll<'static>,
        ctx: &mut ParseContext,
    ) -> Self {
        match cst {
            SimpleIdentifierContextAll::BackQuotedIdentifierContext(parser_ctx) => {
                let text = parser_ctx.get_text();
                match text.get(1..text.len() - 1) {
                    Some(inner) => {
                        let unescaped = inner.replace("``", "`");
                        SimpleIdentifier {
                            span: interval(parser_ctx).into(),
                            name: ctx.intern(&unescaped),
                        }
                        .into()
                    }
                    None => {
                        let err = ctx
                            .error("invalid backtick identifier")
                            .at(parser_ctx)
                            .emit();
                        err.into()
                    }
                }
            }
            SimpleIdentifierContextAll::UnquotedIdentifierContext(parser_ctx) => SimpleIdentifier {
                span: interval(parser_ctx).into(),
                name: ctx.intern(&parser_ctx.get_text()),
            }
            .into(),
            SimpleIdentifierContextAll::Error(parser_ctx) => {
                let err = ctx.error("parse_error").at(parser_ctx).emit();
                err.into()
            }
        }
    }
}

impl FromStr for Identifier {
    type Err = TranslationError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::parse(s)
    }
}

impl FromStr for SimpleIdentifier {
    type Err = TranslationError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::parse(s)
    }
}

impl From<&str> for SimpleIdentifier {
    fn from(s: &str) -> Self {
        SimpleIdentifier {
            span: Span::NONE,
            name: Arc::from(s),
        }
    }
}

impl PartialEq for SimpleIdentifier {
    fn eq(&self, other: &Self) -> bool {
        self.name == other.name
    }
}

impl Eq for SimpleIdentifier {}

impl Hash for SimpleIdentifier {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.name.hash(state);
    }
}

impl Display for SimpleIdentifier {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        if self.is_clean() {
            write!(f, "{}", self.name)
        } else {
            // Escape backticks by doubling them, then wrap in backticks
            let escaped = self.name.replace('`', "``");
            write!(f, "`{}`", escaped)
        }
    }
}

impl Serialize for SimpleIdentifier {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.serialize_str(&self.to_string())
    }
}

impl<'de> Deserialize<'de> for SimpleIdentifier {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let s = String::deserialize(deserializer)?;
        SimpleIdentifier::parse(s).map_err(serde::de::Error::custom)
    }
}

impl SimpleIdentifier {
    /// Returns the simple identifier itself
    pub fn first(&self) -> SimpleIdentifier {
        self.clone()
    }

    /// Simple identifiers have no rest
    pub fn rest(&self) -> Option<Identifier> {
        None
    }

    /// Returns the simple identifier itself
    pub fn last(&self) -> SimpleIdentifier {
        self.clone()
    }

    /// Check if this simple identifier has the given prefix
    /// Always returns false since simple identifiers can't have prefixes
    pub fn has_prefix(&self, _prefix: &Identifier) -> bool {
        false
    }

    /// Strip prefix - returns None since simple identifiers can't have prefixes
    pub fn strip_prefix(&self, _prefix: &Identifier) -> Option<Identifier> {
        // Only returns None - either they're equal (not a child) or different (not a prefix)
        None
    }

    /// Prepend a prefix identifier so that this identifier becomes the rightmost segment.
    ///
    /// Equivalent to [`Identifier::prepend`] on `self.into()`, but reads naturally when
    /// building paths from the outer field inward (e.g. `items.prepend(data.into())` for
    /// `data.items`).
    pub fn prepend(self, prefix: Identifier) -> CompoundIdentifier {
        let id: Identifier = self.into();
        id.prepend(prefix)
    }
}

/// A compound (qualified) identifier like `table.column` or `schema.table.column`
#[derive(Debug, Clone, PartialEq, Hash, Eq)]
pub struct CompoundIdentifier {
    parts: Vec<SimpleIdentifier>,
}

impl CompoundIdentifier {
    pub fn new(
        first: SimpleIdentifier,
        second: SimpleIdentifier,
        rest: Vec<SimpleIdentifier>,
    ) -> Self {
        let mut parts = vec![first, second];
        parts.extend(rest);
        Self { parts }
    }

    /// Create a two-part compound identifier.
    pub fn from_two(first: impl Into<String>, second: impl Into<String>) -> Self {
        Self {
            parts: vec![SimpleIdentifier::new(first), SimpleIdentifier::new(second)],
        }
    }
}

impl Spannable for CompoundIdentifier {
    fn span(&self) -> Option<RangeInclusive<usize>> {
        // Combine spans of all parts
        let first_span = self.parts.first()?.span()?;
        let last_span = self.parts.last()?.span()?;
        Some(*first_span.start()..=*last_span.end())
    }
}

impl Display for CompoundIdentifier {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        // Join parts with dots
        for (i, part) in self.parts.iter().enumerate() {
            if i > 0 {
                write!(f, ".")?;
            }
            write!(f, "{}", part)?;
        }
        Ok(())
    }
}

impl CompoundIdentifier {
    /// Returns all parts as a slice.
    pub fn parts(&self) -> &[SimpleIdentifier] {
        &self.parts
    }

    /// Consumes self and returns the parts vec.
    pub fn into_parts(self) -> Vec<SimpleIdentifier> {
        self.parts
    }

    /// Returns the parts after the first as a slice.
    ///
    /// A CompoundIdentifier always has at least 2 parts, so this always has at least 1 element.
    pub fn rest_parts(&self) -> &[SimpleIdentifier] {
        &self.parts[1..]
    }

    /// Returns the first part as a simple identifier
    pub fn first(&self) -> SimpleIdentifier {
        // CompoundIdentifier always has at least 2 parts, so this is safe
        self.parts[0].clone()
    }

    /// Returns the remaining parts after the first.
    ///
    /// A CompoundIdentifier always has at least 2 parts, so rest always has at least 1 part.
    pub fn rest(&self) -> Identifier {
        match &self.parts[1..] {
            [single] => single.clone().into(),
            parts => CompoundIdentifier {
                parts: parts.to_vec(),
            }
            .into(),
        }
    }

    /// Returns the last part as a simple identifier
    pub fn last(&self) -> SimpleIdentifier {
        // CompoundIdentifier always has at least 2 parts, so this is safe
        self.parts[self.parts.len() - 1].clone()
    }

    /// Check if this compound identifier has the given prefix
    pub fn has_prefix(&self, prefix: &Identifier) -> bool {
        match prefix {
            Identifier::Simple(prefix_simple) => {
                // Prefix is simple - check if our first part matches and we have more parts
                self.parts.len() > 1 && self.parts[0] == *prefix_simple
            }
            Identifier::Compound(prefix_compound) => {
                // Prefix is compound - check if it matches the beginning of our parts
                let prefix_len = prefix_compound.parts().len();

                // We must have more parts than the prefix for it to be a true prefix
                if self.parts.len() <= prefix_len {
                    return false;
                }

                // Check if all prefix parts match
                for i in 0..prefix_len {
                    if self.parts[i] != prefix_compound.parts()[i] {
                        return false;
                    }
                }

                true
            }
        }
    }

    /// Strip a prefix from this compound identifier
    pub fn strip_prefix(&self, prefix: &Identifier) -> Option<Identifier> {
        let mut current: Identifier = self.clone().into();
        let mut prefix_current = prefix.clone();

        // Iteratively match first components
        loop {
            let c = current.first();
            let p = prefix_current.first();

            if c == p {
                // They match - check if prefix has more parts
                match prefix_current.rest() {
                    None => {
                        // Prefix exhausted - return the rest of current
                        return current.rest();
                    }
                    Some(prefix_rest) => {
                        // Prefix has more parts - advance both
                        match current.rest() {
                            None => return None, // Current exhausted, but prefix has more
                            Some(current_rest) => {
                                current = current_rest;
                                prefix_current = prefix_rest;
                            }
                        }
                    }
                }
            } else {
                // First components don't match
                return None;
            }
        }
    }
}

// ============================================================================
// Add trait implementation - combines identifiers into compound identifiers
// ============================================================================

impl Add for Identifier {
    type Output = Identifier;

    /// Combine two identifiers into a compound identifier.
    /// This is useful for building nested field paths like `prefix + field_name`.
    fn add(self, suffix: Identifier) -> Identifier {
        suffix.prepend(self).into()
    }
}

// ============================================================================
// Conversions from SQL identifiers to tree AST identifiers
// ============================================================================

use crate::sql::expression::identifier::{
    CompoundIdentifier as SqlCompoundIdentifier, Identifier as SqlIdentifier,
    SimpleIdentifier as SqlSimpleIdentifier,
};

impl From<SqlSimpleIdentifier> for SimpleIdentifier {
    fn from(sql_ident: SqlSimpleIdentifier) -> Self {
        SimpleIdentifier::new(sql_ident.name)
    }
}

impl From<SqlCompoundIdentifier> for CompoundIdentifier {
    fn from(sql_ident: SqlCompoundIdentifier) -> Self {
        CompoundIdentifier {
            parts: sql_ident.into_vec().into_iter().map(|p| p.into()).collect(),
        }
    }
}

impl From<SqlIdentifier> for Identifier {
    fn from(sql_ident: SqlIdentifier) -> Self {
        match sql_ident {
            SqlIdentifier::Simple(simple) => Identifier::Simple(simple.into()),
            SqlIdentifier::Compound(compound) => Identifier::Compound(compound.into()),
        }
    }
}

impl From<SqlSimpleIdentifier> for Identifier {
    fn from(sql_ident: SqlSimpleIdentifier) -> Self {
        Identifier::Simple(sql_ident.into())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{tree::builder::*, types::STRING};
    // Helper function to convert Expression to Identifier using parse-time inference
    fn identifier_from_expr(expr: &Expression) -> Identifier {
        match Identifier::infer_from_expression(expr) {
            Some(ParsedIdentifier::Valid(identifier)) => identifier,
            Some(ParsedIdentifier::Error(e)) => panic!("expected valid identifier, got {:?}", e),
            None => panic!("could not infer identifier from expression"),
        }
    }

    #[test]
    fn test_simple_identifier_creation() {
        use crate::tree::builder::identifier::ident;
        let id: Identifier = ident("test").into();
        match id {
            Identifier::Simple(simple) => {
                assert!(simple == "test");
                assert!(simple.span.is_none());
            }
            _ => panic!("Expected simple identifier"),
        }
    }

    #[test]
    fn test_compound_identifier_creation() {
        use crate::tree::builder::identifier::ident;
        let id: Identifier = ident("schema").dot("table").into();
        match id {
            Identifier::Compound(compound) => {
                assert_eq!(compound.parts().len(), 2);
                assert!(compound.parts()[0] == "schema");
                assert!(compound.parts()[1] == "table");
            }
            _ => panic!("Expected compound identifier"),
        }
    }

    #[test]
    fn test_compound_identifier_with_three_parts() {
        use crate::tree::builder::identifier::ident;
        let id: Identifier = ident("catalog").dot("schema").dot("table").into();
        match id {
            Identifier::Compound(compound) => {
                assert_eq!(compound.parts().len(), 3);
                assert!(compound.parts()[0] == "catalog");
                assert!(compound.parts()[1] == "schema");
                assert!(compound.parts()[2] == "table");
            }
            _ => panic!("Expected compound identifier"),
        }
    }

    #[test]
    fn test_identifier_from_column_reference() {
        let expr = field_ref("foo").build();
        let ident = identifier_from_expr(&expr);
        match ident {
            Identifier::Simple(s) => assert!(s == "foo"),
            _ => panic!("Expected Simple identifier"),
        }
    }

    #[test]
    fn test_identifier_from_field_access_two_levels() {
        let expr = field(field_ref("foo"), "bar").build();
        let ident = identifier_from_expr(&expr);
        match ident {
            Identifier::Compound(c) => {
                assert_eq!(c.parts.len(), 2);
                assert!(c.parts()[0] == "foo");
                assert!(c.parts()[1] == "bar");
            }
            _ => panic!("Expected Compound identifier"),
        }
    }

    #[test]
    fn test_identifier_from_field_access_three_levels() {
        let expr = field(field(field_ref("foo"), "bar"), "baz").build();
        let ident = identifier_from_expr(&expr);
        match ident {
            Identifier::Compound(c) => {
                assert_eq!(c.parts.len(), 3);
                assert!(c.parts()[0] == "foo");
                assert!(c.parts()[1] == "bar");
                assert!(c.parts()[2] == "baz");
            }
            _ => panic!("Expected Compound identifier"),
        }
    }

    #[test]
    fn test_identifier_from_zero_arg_function() {
        let expr = call("now").build();
        let ident = identifier_from_expr(&expr);
        match ident {
            Identifier::Simple(s) => assert!(s == "now"),
            _ => panic!("Expected Simple identifier from zero-arg function"),
        }
    }

    #[test]
    fn test_identifier_from_cast() {
        let expr = cast(field_ref("foo"), STRING).build();
        let ident = identifier_from_expr(&expr);
        match ident {
            Identifier::Simple(s) => assert!(s == "foo"),
            _ => panic!("Expected Simple identifier from cast"),
        }
    }

    #[test]
    fn test_identifier_from_cast_with_field_access() {
        let expr = cast(field(field_ref("foo"), "bar"), STRING).build();
        let ident = identifier_from_expr(&expr);
        match ident {
            Identifier::Compound(c) => {
                assert_eq!(c.parts.len(), 2);
                assert!(c.parts()[0] == "foo");
                assert!(c.parts()[1] == "bar");
            }
            _ => panic!("Expected Compound identifier from cast of field access"),
        }
    }

    #[test]
    fn test_identifier_first() {
        use crate::tree::builder::identifier::ident;

        // Simple identifier
        let id: Identifier = ident("x").into();
        let first = id.first();
        assert!(first == "x");

        // Compound identifier
        let id: Identifier = ident("x").dot("y").into();
        let first = id.first();
        assert!(first == "x");
    }

    #[test]
    fn test_identifier_rest() {
        use crate::tree::builder::identifier::ident;

        // Simple identifier - no rest
        let id: Identifier = ident("x").into();
        assert_eq!(id.rest(), None);

        // Compound identifier with 2 parts
        let id: Identifier = ident("x").dot("y").into();
        match id.rest() {
            Some(Identifier::Simple(s)) => assert!(s == "y"),
            _ => panic!("Expected Simple identifier"),
        }

        // Compound identifier with 3 parts
        let id: Identifier = ident("x").dot("y").dot("z").into();
        match id.rest() {
            Some(Identifier::Compound(c)) => {
                assert_eq!(c.parts.len(), 2);
                assert!(c.parts()[0] == "y");
                assert!(c.parts()[1] == "z");
            }
            _ => panic!("Expected Compound identifier"),
        }
    }

    #[test]
    fn test_identifier_last() {
        use crate::tree::builder::identifier::ident;

        // Simple identifier
        let id: Identifier = ident("x").into();
        let last = id.last();
        assert!(last == "x");

        // Compound identifier
        let id: Identifier = ident("x").dot("y").into();
        let last = id.last();
        assert!(last == "y");

        // Compound identifier with 3 parts
        let id: Identifier = ident("x").dot("y").dot("z").into();
        let last = id.last();
        assert!(last == "z");
    }

    #[test]
    fn test_identifier_has_prefix() {
        use crate::tree::builder::identifier::ident;

        // x.y.z has prefix x.y
        let id: Identifier = ident("x").dot("y").dot("z").into();
        let prefix: Identifier = ident("x").dot("y").into();
        assert!(id.has_prefix(&prefix));

        // x.y has prefix x
        let id: Identifier = ident("x").dot("y").into();
        let prefix: Identifier = ident("x").into();
        assert!(id.has_prefix(&prefix));

        // x doesn't have prefix x (equal, not a child)
        let id: Identifier = ident("x").into();
        let prefix: Identifier = ident("x").into();
        assert!(!id.has_prefix(&prefix));

        // x doesn't have prefix y (not a prefix)
        let id: Identifier = ident("x").into();
        let prefix: Identifier = ident("y").into();
        assert!(!id.has_prefix(&prefix));

        // x doesn't have prefix x.y (prefix is longer)
        let id: Identifier = ident("x").into();
        let prefix: Identifier = ident("x").dot("y").into();
        assert!(!id.has_prefix(&prefix));

        // x.y.z has prefix x
        let id: Identifier = ident("x").dot("y").dot("z").into();
        let prefix: Identifier = ident("x").into();
        assert!(id.has_prefix(&prefix));

        // x.y doesn't have prefix x.z (different branch)
        let id: Identifier = ident("x").dot("y").into();
        let prefix: Identifier = ident("x").dot("z").into();
        assert!(!id.has_prefix(&prefix));
    }

    #[test]
    fn test_identifier_strip_prefix() {
        use crate::tree::builder::identifier::ident;

        // x.y.z with prefix x.y -> z
        let id: Identifier = ident("x").dot("y").dot("z").into();
        let prefix: Identifier = ident("x").dot("y").into();
        match id.strip_prefix(&prefix) {
            Some(Identifier::Simple(s)) => assert!(s == "z"),
            _ => panic!("Expected Some(Simple(z))"),
        }

        // x.y with prefix x -> y
        let id: Identifier = ident("x").dot("y").into();
        let prefix: Identifier = ident("x").into();
        match id.strip_prefix(&prefix) {
            Some(Identifier::Simple(s)) => assert!(s == "y"),
            _ => panic!("Expected Some(Simple(y))"),
        }

        // x with prefix x -> None (equal, not a child)
        let id: Identifier = ident("x").into();
        let prefix: Identifier = ident("x").into();
        assert_eq!(id.strip_prefix(&prefix), None);

        // x with prefix y -> None (not a prefix)
        let id: Identifier = ident("x").into();
        let prefix: Identifier = ident("y").into();
        assert_eq!(id.strip_prefix(&prefix), None);

        // x with prefix x.y -> None (prefix is longer)
        let id: Identifier = ident("x").into();
        let prefix: Identifier = ident("x").dot("y").into();
        assert_eq!(id.strip_prefix(&prefix), None);
    }

    #[test]
    fn test_identifier_add() {
        use crate::tree::builder::identifier::ident;

        // Simple + Simple -> Compound
        let prefix: Identifier = ident("x").into();
        let suffix: Identifier = ident("y").into();
        let result = prefix + suffix;
        match result {
            Identifier::Compound(c) => {
                assert_eq!(c.parts.len(), 2);
                assert!(c.parts()[0] == "x");
                assert!(c.parts()[1] == "y");
            }
            _ => panic!("Expected Compound identifier"),
        }

        // Simple + Compound -> Compound
        let prefix: Identifier = ident("x").into();
        let suffix: Identifier = ident("y").dot("z").into();
        let result = prefix + suffix;
        match result {
            Identifier::Compound(c) => {
                assert_eq!(c.parts.len(), 3);
                assert!(c.parts()[0] == "x");
                assert!(c.parts()[1] == "y");
                assert!(c.parts()[2] == "z");
            }
            _ => panic!("Expected Compound identifier"),
        }

        // Compound + Simple -> Compound
        let prefix: Identifier = ident("x").dot("y").into();
        let suffix: Identifier = ident("z").into();
        let result = prefix + suffix;
        match result {
            Identifier::Compound(c) => {
                assert_eq!(c.parts.len(), 3);
                assert!(c.parts()[0] == "x");
                assert!(c.parts()[1] == "y");
                assert!(c.parts()[2] == "z");
            }
            _ => panic!("Expected Compound identifier"),
        }

        // Compound + Compound -> Compound
        let prefix: Identifier = ident("x").dot("y").into();
        let suffix: Identifier = ident("z").dot("a").into();
        let result = prefix + suffix;
        match result {
            Identifier::Compound(c) => {
                assert_eq!(c.parts.len(), 4);
                assert!(c.parts()[0] == "x");
                assert!(c.parts()[1] == "y");
                assert!(c.parts()[2] == "z");
                assert!(c.parts()[3] == "a");
            }
            _ => panic!("Expected Compound identifier"),
        }
    }
}