hawdb-sql 0.31.0

An embedded Rust graph database for local data-plane workloads
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
// Copyright 2026 Nowledge
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Lower matched syntax directly into relational expressions. Source locations
//! describe contributing tokens, retaining the established expression contract.
use super::super::ExpressionPosition as P;
use super::{syntax, Lowerer};
use crate::ast::*;
use hawdb_core::{HawDBError, Result, Value};
use syntax::{ExpressionKindSyntax as E, ExpressionSyntax as S, LiteralSyntax as L};

fn semantic(message: impl Into<String>) -> HawDBError {
    HawDBError::Semantic(message.into())
}
fn statement_numeric_name(name: &str) -> bool {
    matches!(
        name,
        "bigint"
            | "int8"
            | "int"
            | "integer"
            | "int4"
            | "smallint"
            | "int2"
            | "tinyint"
            | "float"
            | "float8"
            | "double"
            | "double precision"
            | "decimal"
            | "numeric"
            | "dec"
    )
}
fn union(left: SqlSourceSpan, right: SqlSourceSpan) -> SqlSourceSpan {
    if left == SqlSourceSpan::default() {
        return right;
    }
    if right == SqlSourceSpan::default() {
        return left;
    }
    let before =
        |a: SqlSourceLocation, b: SqlSourceLocation| (a.line, a.column) < (b.line, b.column);
    SqlSourceSpan {
        start: if before(left.start, right.start) {
            left.start
        } else {
            right.start
        },
        end: if before(left.end, right.end) {
            right.end
        } else {
            left.end
        },
    }
}
fn expression(kind: ExprKind, span: SqlSourceSpan) -> crate::Expr {
    crate::Expr { kind, span }
}
impl Lowerer<'_> {
    pub(in crate::parser) fn ident(&self, ident: &syntax::Identifier) -> String {
        let raw = &self.input[ident.span.start..ident.span.end];
        if ident.quoted {
            raw[1..raw.len() - 1].replace("\"\"", "\"")
        } else {
            raw.to_ascii_lowercase()
        }
    }
    pub(in crate::parser) fn name(&self, name: &syntax::QualifiedName) -> Result<SqlTableName> {
        let parts: Vec<_> = name.parts.iter().map(|part| self.ident(part)).collect();
        match parts.as_slice() {
            [name] => Ok(SqlTableName {
                schema: None,
                name: name.clone(),
            }),
            [schema, name] => Ok(SqlTableName {
                schema: Some(schema.clone()),
                name: name.clone(),
            }),
            _ => Err(semantic(
                "PostgreSQL SELECT currently supports one- or two-part table names",
            )),
        }
    }
    pub(in crate::parser) fn column(&self, value: &S) -> Result<SqlColumnRef> {
        let E::Column(name) = &value.kind else {
            return Err(semantic("expected a column reference"));
        };
        let parts: Vec<_> = name.parts.iter().map(|part| self.ident(part)).collect();
        match parts.as_slice() {
            [name] => Ok(SqlColumnRef {
                qualifier: None,
                name: name.clone(),
            }),
            [qualifier, name] => Ok(SqlColumnRef {
                qualifier: Some(qualifier.clone()),
                name: name.clone(),
            }),
            _ => Err(semantic(
                "PostgreSQL SELECT currently supports one- or two-part column names",
            )),
        }
    }
    pub(in crate::parser) fn literal(&self, value: &S) -> Result<SqlValue> {
        match &value.kind {
            E::Parameter(position) => Ok(SqlValue::Parameter(*position as usize)),
            E::Literal(L::Null) => Ok(SqlValue::Literal(Value::Null)),
            E::Literal(L::Boolean(v)) => Ok(SqlValue::Literal(Value::Bool(*v))),
            E::Literal(L::Number(span)) => {
                super::super::lower_number(&self.input[span.start..span.end]).map(SqlValue::Literal)
            }
            E::Literal(L::String(span)) => decode_string(&self.input[span.start..span.end])
                .map(|v| SqlValue::Literal(Value::String(v))),
            E::TypedString {
                data_type,
                time_zone,
                value,
            } => self
                .typed_string(data_type, *time_zone, *value)
                .map(SqlValue::Literal),
            E::Parenthesized(inner) => self.literal(inner),
            _ => {
                let expr = self.lower(value, P::Scalar)?;
                if let ExprKind::Value(value) = expr.kind {
                    return Ok(value);
                }
                let mut constant_numeric = true;
                expr.visit(&mut |child| {
                    constant_numeric &= matches!(
                        child.kind,
                        ExprKind::Value(_)
                            | ExprKind::Arithmetic { .. }
                            | ExprKind::Negate(_)
                            | ExprKind::Cast { .. }
                            | ExprKind::TimestampCast { .. }
                            | ExprKind::CurrentTimestamp(_)
                    )
                });
                if !constant_numeric {
                    return Err(semantic("expected a constant numeric expression"));
                }
                Ok(SqlValue::Expression(Box::new(expr)))
            }
        }
    }
    /// Recognizes `CURRENT_TIMESTAMP[(p)]` and `now()` (TIMESTAMPTZ) and
    /// `LOCALTIMESTAMP[(p)]` (TIMESTAMP). Quoted identifiers remain columns.
    fn current_timestamp(
        &self,
        value: &S,
    ) -> Result<Option<hawdb_core::sql_timestamp::TimestampType>> {
        let (name, arguments) = match &value.kind {
            E::Column(name) => (name, None),
            E::Function {
                name,
                arguments,
                distinct: false,
            } => (name, Some(arguments.as_slice())),
            _ => return Ok(None),
        };
        let [part] = name.parts.as_slice() else {
            return Ok(None);
        };
        if part.quoted {
            return Ok(None);
        }
        let name = self.ident(part);
        let zoned = match (name.as_str(), arguments) {
            ("current_timestamp", _) | ("now", Some(_)) => true,
            ("localtimestamp", _) => false,
            _ => return Ok(None),
        };
        let precision = match arguments {
            None => hawdb_core::TimestampPrecision::DEFAULT,
            Some([]) if name != "now" => {
                return Err(semantic(format!(
                    "{} takes no parentheses without a precision",
                    name.to_ascii_uppercase()
                )))
            }
            Some([]) => hawdb_core::TimestampPrecision::DEFAULT,
            Some(
                [S {
                    kind: E::Literal(L::Number(span)),
                    ..
                }],
            ) if name != "now" => self.input[span.start..span.end]
                .parse::<u8>()
                .ok()
                .and_then(hawdb_core::TimestampPrecision::new)
                .ok_or_else(|| {
                    semantic(format!(
                        "{} precision must be an integer between 0 and 6",
                        name.to_ascii_uppercase()
                    ))
                })?,
            Some(_) if name == "now" => return Err(semantic("now() takes no arguments")),
            Some(_) => {
                return Err(semantic(format!(
                    "{} accepts only an integer precision",
                    name.to_ascii_uppercase()
                )))
            }
        };
        Ok(Some(hawdb_core::sql_timestamp::TimestampType {
            zoned,
            precision,
        }))
    }
    /// Lowers `TIMESTAMP 'text'`, `TIMESTAMPTZ 'text'` and
    /// `TIMESTAMP WITH[OUT] TIME ZONE 'text'` to a host temporal value. Other
    /// typed strings (DATE, TIME, INTERVAL) remain unsupported.
    fn typed_string(
        &self,
        data_type: &syntax::Identifier,
        time_zone: Option<syntax::TimeZoneSyntax>,
        value: syntax::Span,
    ) -> Result<Value> {
        let name = if data_type.quoted {
            String::new()
        } else {
            self.ident(data_type)
        };
        let zoned = match (name.as_str(), time_zone) {
            ("timestamp", None | Some(syntax::TimeZoneSyntax::Without)) => false,
            ("timestamp", Some(syntax::TimeZoneSyntax::With)) | ("timestamptz", None) => true,
            _ => {
                return Err(semantic(format!(
                    "unsupported PostgreSQL typed literal {}",
                    &self.input[data_type.span.start..data_type.span.end]
                )))
            }
        };
        let text = decode_string(&self.input[value.start..value.end])?;
        hawdb_core::sql_timestamp::TimestampCast::Timestamp(
            hawdb_core::sql_timestamp::TimestampType {
                zoned,
                precision: hawdb_core::TimestampPrecision::DEFAULT,
            },
        )
        .apply(&Value::String(text))
    }
    /// Recognizes CAST targets TEXT, TIMESTAMP[(p)] [WITH[OUT] TIME ZONE] and
    /// TIMESTAMPTZ[(p)]. Other targets are numeric casts.
    pub(in crate::parser) fn timestamp_cast_type(
        &self,
        name: &syntax::QualifiedName,
        parameters: &[syntax::Span],
        time_zone: Option<syntax::TimeZoneSyntax>,
    ) -> Result<Option<hawdb_core::sql_timestamp::TimestampCast>> {
        use hawdb_core::sql_timestamp::{TimestampCast, TimestampType};
        let [part] = name.parts.as_slice() else {
            return Ok(None);
        };
        // DOUBLE PRECISION spans two words; it is never a timestamp target.
        if part.quoted || self.input[part.span.start..part.span.end].contains(char::is_whitespace) {
            return Ok(None);
        }
        let name = self.ident(part);
        let zoned = match (name.as_str(), time_zone) {
            ("text", None) if parameters.is_empty() => return Ok(Some(TimestampCast::Text)),
            ("timestamp", None | Some(syntax::TimeZoneSyntax::Without)) => false,
            ("timestamp", Some(syntax::TimeZoneSyntax::With)) | ("timestamptz", None) => true,
            (_, Some(_)) => {
                return Err(semantic(
                    "WITH or WITHOUT TIME ZONE requires the TIMESTAMP type",
                ))
            }
            ("text", None) => return Err(semantic("TEXT does not accept type modifiers")),
            _ => return Ok(None),
        };
        let precision = match parameters {
            [] => hawdb_core::TimestampPrecision::DEFAULT,
            [span] => self.input[span.start..span.end]
                .trim()
                .parse::<u8>()
                .ok()
                .and_then(hawdb_core::TimestampPrecision::new)
                .ok_or_else(|| {
                    semantic("TIMESTAMP precision must be an integer between 0 and 6")
                })?,
            _ => return Err(semantic("TIMESTAMP accepts one precision modifier")),
        };
        Ok(Some(TimestampCast::Timestamp(TimestampType {
            zoned,
            precision,
        })))
    }
    pub(in crate::parser) fn numeric_cast_type(
        &self,
        name: &syntax::QualifiedName,
        parameters: &[syntax::Span],
    ) -> Result<hawdb_core::numeric::NumericType> {
        use hawdb_core::numeric::NumericType;
        let [part] = name.parts.as_slice() else {
            if self.statement_tokens.is_some()
                && name
                    .parts
                    .first()
                    .is_some_and(|part| !part.quoted && statement_numeric_name(&self.ident(part)))
            {
                return Err(HawDBError::Parse(
                    "builtin numeric types must be unqualified".into(),
                ));
            }
            return Err(semantic("CAST requires an unqualified numeric type"));
        };
        let name = self.ident(part);
        if let Some(tokens) = self.statement_tokens {
            if part.quoted {
                return Err(semantic("CAST requires a numeric target type"));
            }
            let first = tokens.partition_point(|token| token.span.start < part.span.start);
            let last = tokens.partition_point(|token| token.span.end < part.span.end);
            if last == first + 1
                && tokens[first].is_keyword(self.input, "DOUBLE")
                && tokens[last].is_keyword(self.input, "PRECISION")
            {
                return self.statement_numeric_cast_type("double precision", parameters, tokens);
            }
            return self.statement_numeric_cast_type(&name, parameters, tokens);
        }
        if !matches!(name.as_str(), "decimal" | "numeric" | "dec") && !parameters.is_empty() {
            return Err(semantic("invalid numeric CAST type parameters"));
        }
        Ok(match name.as_str() {
            "bigint" | "int8" => NumericType::BigInt,
            "double precision" | "float8" => NumericType::DoublePrecision,
            "decimal" | "numeric" | "dec" => NumericType::Decimal(self.decimal_type(parameters)?),
            _ => return Err(semantic("CAST requires a numeric target type")),
        })
    }
    // Statement casts retain the numeric aliases accepted by DDL/DML. SELECT
    // has a narrower cast contract. Modifiers come from matched lexer tokens;
    // whitespace and comments between a sign and its digits are not reparsed.
    fn statement_numeric_cast_type(
        &self,
        name: &str,
        parameters: &[syntax::Span],
        tokens: &[syntax::Token],
    ) -> Result<hawdb_core::numeric::NumericType> {
        use hawdb_core::numeric::NumericType;
        if !statement_numeric_name(name) {
            return Err(semantic("CAST requires a numeric target type"));
        }
        if matches!(name, "float8" | "double precision") && !parameters.is_empty() {
            return Err(HawDBError::Parse("unexpected numeric type modifier".into()));
        }
        if matches!(
            name,
            "bigint" | "int8" | "int" | "integer" | "int4" | "smallint" | "int2" | "tinyint"
        ) && parameters.len() > 1
        {
            return Err(HawDBError::Parse(
                "integer CAST types accept one modifier".into(),
            ));
        }
        let invalid = || HawDBError::Parse("numeric type modifiers require integers".into());
        let parameter = |span: syntax::Span, signed: bool| -> Result<(bool, u64)> {
            let first = tokens.partition_point(|token| token.span.start < span.start);
            let last = tokens.partition_point(|token| token.span.end < span.end);
            let sign = tokens[first].kind;
            if first != last
                && !(signed
                    && first + 1 == last
                    && matches!(sign, syntax::TokenKind::Minus | syntax::TokenKind::Plus))
            {
                return Err(invalid());
            }
            if tokens[last].kind != syntax::TokenKind::Number {
                return Err(invalid());
            }
            // The statement dialect tokenizes an immediately following slash
            // as part of a PostgreSQL minus operator, rather than a comment.
            if sign == syntax::TokenKind::Minus
                && self.input.as_bytes().get(tokens[first].span.end) == Some(&b'/')
            {
                return Err(invalid());
            }
            let value = tokens[last]
                .text(self.input)
                .parse::<u64>()
                .map_err(|_| invalid())?;
            Ok((sign == syntax::TokenKind::Minus, value))
        };
        let precision = parameters
            .first()
            .map(|span| parameter(*span, false).map(|(_, value)| value))
            .transpose()?;
        let scale = parameters
            .get(1)
            .map(|span| -> Result<i64> {
                let (negative, magnitude) = parameter(*span, true)?;
                let value = i64::try_from(magnitude).map_err(|_| invalid())?;
                Ok(if negative { -value } else { value })
            })
            .transpose()?;
        match name {
            "bigint" | "int8" if scale.is_none() => Ok(NumericType::BigInt),
            "int" | "integer" | "int4" | "smallint" | "int2" | "tinyint"
                if parameters.is_empty() =>
            {
                Ok(NumericType::BigInt)
            }
            "float8" | "double precision" => Ok(NumericType::DoublePrecision),
            "float"
                if scale.is_none() && precision.is_none_or(|value| (25..=53).contains(&value)) =>
            {
                Ok(NumericType::DoublePrecision)
            }
            "decimal" | "numeric" | "dec" => {
                let precision = u8::try_from(precision.unwrap_or(38))
                    .map_err(|_| semantic("Decimal precision must be 1..38"))?;
                let scale = u8::try_from(scale.unwrap_or(0))
                    .map_err(|_| semantic("Decimal scale must be 0..precision"))?;
                hawdb_core::DecimalType::new(precision, scale)
                    .map(NumericType::Decimal)
                    .map_err(|error| semantic(error.to_string()))
            }
            _ => Err(semantic("unsupported numeric CAST type modifiers")),
        }
    }
    pub(in crate::parser) fn decimal_type(
        &self,
        parameters: &[syntax::Span],
    ) -> Result<hawdb_core::DecimalType> {
        let invalid = || semantic("invalid Decimal type parameters");
        let number = |span: &syntax::Span| {
            self.input[span.start..span.end]
                .parse::<u8>()
                .map_err(|_| invalid())
        };
        let (precision, scale) = match parameters {
            [] => (38, 0),
            [p] => (number(p)?, 0),
            [p, s] => (number(p)?, number(s)?),
            _ => return Err(invalid()),
        };
        hawdb_core::DecimalType::new(precision, scale).map_err(|e| semantic(e.to_string()))
    }
    fn literal_span(&self, value: &S) -> SqlSourceSpan {
        match &value.kind {
            E::Unary { expression, .. } | E::Parenthesized(expression) => {
                self.literal_span(expression)
            }
            _ => self.span(value.span),
        }
    }
    pub(in crate::parser) fn bound(&self, value: &S) -> Result<SqlBound> {
        match self.literal(value)? {
            SqlValue::Literal(Value::Int(v)) if v >= 0 => Ok(SqlBound::Literal(v as u64)),
            SqlValue::Parameter(p) => Ok(SqlBound::Parameter(p)),
            _ => Err(semantic(
                "LIMIT/OFFSET must be non-negative integers or PostgreSQL parameters",
            )),
        }
    }
    pub(in crate::parser) fn lower(&self, value: &S, position: P) -> Result<crate::Expr> {
        use P::{
            Column, Having, HavingScalar, Predicate, PredicateScalar, Scalar, Value as Literal,
        };
        let having = matches!(position, Having | HavingScalar);
        let operand = if having {
            HavingScalar
        } else {
            PredicateScalar
        };
        match position {
            Column => Ok(expression(
                ExprKind::Column(self.column(value)?),
                self.span(value.span),
            )),
            Literal => Ok(expression(
                ExprKind::Value(self.literal(value)?),
                self.literal_span(value),
            )),
            Scalar | HavingScalar | PredicateScalar if self.current_timestamp(value)?.is_some() => {
                let kind = self
                    .current_timestamp(value)?
                    .expect("checked clock function");
                let span = self.span(value.span);
                Ok(expression(
                    ExprKind::Value(SqlValue::Expression(Box::new(expression(
                        ExprKind::CurrentTimestamp(kind),
                        span,
                    )))),
                    span,
                ))
            }
            Scalar | HavingScalar | PredicateScalar => match &value.kind {
                E::Parenthesized(inner) => self.lower(inner, position),
                E::Column(_) => self.lower(value, Column),
                E::Parameter(_) | E::Literal(_) | E::TypedString { .. } => {
                    self.lower(value, Literal)
                }
                E::Unary {
                    operator: syntax::UnaryOperatorSyntax::Plus,
                    expression: inner,
                } => self.lower(inner, position),
                E::Unary {
                    operator: syntax::UnaryOperatorSyntax::Minus,
                    expression: inner,
                } => {
                    if let E::Literal(L::Number(span)) = inner.kind {
                        return Ok(expression(
                            ExprKind::Value(SqlValue::Literal(super::super::lower_number(
                                &format!("-{}", &self.input[span.start..span.end]),
                            )?)),
                            self.span(inner.span),
                        ));
                    }
                    let inner = self.lower(inner, position)?;
                    let span = inner.span;
                    Ok(expression(ExprKind::Negate(Box::new(inner)), span))
                }
                E::Binary {
                    left,
                    operator,
                    right,
                } => {
                    use hawdb_core::numeric::NumericBinaryOp as N;
                    use syntax::BinaryOperatorSyntax as B;
                    let op = match operator {
                        B::Add => N::Add,
                        B::Subtract => N::Subtract,
                        B::Multiply => N::Multiply,
                        B::Divide => N::Divide,
                        B::Modulo => N::Remainder,
                        _ => return Err(semantic("unsupported numeric operator")),
                    };
                    let left = self.lower(left, position)?;
                    let right = self.lower(right, position)?;
                    let span = union(left.span, right.span);
                    Ok(expression(
                        ExprKind::Arithmetic {
                            left: Box::new(left),
                            op,
                            right: Box::new(right),
                        },
                        span,
                    ))
                }
                E::Cast {
                    expression: inner,
                    data_type,
                    type_parameters,
                    time_zone,
                } => {
                    if let Some(target) =
                        self.timestamp_cast_type(data_type, type_parameters, *time_zone)?
                    {
                        let inner = self.lower(inner, position)?;
                        let span = inner.span;
                        // Fold literal operands so that constants reach index
                        // bounds and assignments as native values.
                        if let ExprKind::Value(SqlValue::Literal(value)) = &inner.kind {
                            return Ok(expression(
                                ExprKind::Value(SqlValue::Literal(target.apply(value)?)),
                                span,
                            ));
                        }
                        return Ok(expression(
                            ExprKind::TimestampCast {
                                expression: Box::new(inner),
                                target,
                            },
                            span,
                        ));
                    }
                    let target = self.numeric_cast_type(data_type, type_parameters)?;
                    let inner = self.lower(inner, position)?;
                    let span = inner.span;
                    Ok(expression(
                        ExprKind::Cast {
                            expression: Box::new(inner),
                            target,
                        },
                        span,
                    ))
                }
                E::Function { .. } | E::Filtered { .. } if !matches!(position, PredicateScalar) => {
                    self.function(value, position)
                }
                _ => Err(semantic("unsupported PostgreSQL projection expression")),
            },
            Predicate | Having => match &value.kind {
                E::Parenthesized(inner) => self.lower(inner, position),
                E::Unary {
                    operator: syntax::UnaryOperatorSyntax::Not,
                    expression: inner,
                } => {
                    let inner = self.lower(inner, position)?;
                    let span = inner.span;
                    Ok(expression(ExprKind::Not(Box::new(inner)), span))
                }
                E::Binary {
                    left,
                    operator,
                    right,
                } => {
                    use syntax::BinaryOperatorSyntax as B;
                    if matches!(operator, B::And | B::Or) {
                        let left = self.lower(left, position)?;
                        let right = self.lower(right, position)?;
                        let span = union(left.span, right.span);
                        let kind = if *operator == B::And {
                            ExprKind::And(Box::new(left), Box::new(right))
                        } else {
                            ExprKind::Or(Box::new(left), Box::new(right))
                        };
                        return Ok(expression(kind, span));
                    }
                    let op = match operator {
                        B::Equal => SqlComparisonOp::Eq,
                        B::NotEqual => SqlComparisonOp::NotEq,
                        B::Less => SqlComparisonOp::Lt,
                        B::LessOrEqual => SqlComparisonOp::Lte,
                        B::Greater => SqlComparisonOp::Gt,
                        B::GreaterOrEqual => SqlComparisonOp::Gte,
                        _ => return Err(semantic("unsupported PostgreSQL predicate operator")),
                    };
                    let left = self.lower(left, operand)?;
                    let right = self.lower(right, operand)?;
                    let span = union(left.span, right.span);
                    Ok(expression(
                        ExprKind::Compare {
                            left: Box::new(left),
                            op,
                            right: Box::new(right),
                        },
                        span,
                    ))
                }
                E::InList {
                    expression: left,
                    values,
                    negated,
                } => {
                    let left = self.lower(left, operand)?;
                    let values = values
                        .iter()
                        .map(|v| self.lower(v, operand))
                        .collect::<Result<Vec<_>>>()?;
                    let span = values.iter().fold(left.span, |span, v| union(span, v.span));
                    Ok(expression(
                        ExprKind::InList {
                            left: Box::new(left),
                            values,
                            negated: *negated,
                        },
                        span,
                    ))
                }
                E::Like {
                    expression: left,
                    pattern,
                    escape,
                    negated,
                    case_insensitive,
                } => {
                    let left = self.lower(left, operand)?;
                    let pattern =
                        self.lower(pattern, if having { HavingScalar } else { Literal })?;
                    let escape = match escape.as_deref() {
                        None => SqlLikeEscape::Character('\\'),
                        Some(S {
                            kind: E::Literal(L::String(span)),
                            ..
                        }) => {
                            let raw = decode_string(&self.input[span.start..span.end])?;
                            let mut chars = raw.chars();
                            match (chars.next(), chars.next()) {
                                (None, _) => SqlLikeEscape::Disabled,
                                (Some(c), None) => SqlLikeEscape::Character(c),
                                _ => {
                                    return Err(semantic(
                                        "LIKE ESCAPE must contain at most one Unicode scalar",
                                    ))
                                }
                            }
                        }
                        _ => return Err(semantic("LIKE ESCAPE must be a string literal")),
                    };
                    let span = union(left.span, pattern.span);
                    Ok(expression(
                        ExprKind::Like {
                            left: Box::new(left),
                            pattern: Box::new(pattern),
                            case_insensitive: *case_insensitive,
                            negated: *negated,
                            escape,
                        },
                        span,
                    ))
                }
                E::IsNull {
                    expression: inner,
                    negated,
                } => {
                    let inner = self.lower(inner, operand)?;
                    let span = inner.span;
                    Ok(expression(
                        ExprKind::IsNull {
                            expression: Box::new(inner),
                            negated: *negated,
                        },
                        span,
                    ))
                }
                _ if having => self.lower(value, HavingScalar),
                _ => Err(semantic("unsupported PostgreSQL predicate expression")),
            },
        }
    }
    fn function(&self, value: &S, position: P) -> Result<crate::Expr> {
        let (function, filter) = match &value.kind {
            E::Filtered {
                function,
                predicate,
            } => (function.as_ref(), Some(predicate.as_ref())),
            _ => (value, None),
        };
        let E::Function {
            name,
            arguments,
            distinct,
        } = &function.kind
        else {
            return Err(semantic("FILTER requires a function"));
        };
        let [identifier] = name.parts.as_slice() else {
            return Err(semantic(
                "qualified PostgreSQL function names are not supported",
            ));
        };
        let name = self.ident(identifier);
        let mut span = self.span(identifier.span);
        let arguments = arguments
            .iter()
            .map(|arg| match &arg.kind {
                E::Wildcard(None) => Ok(SqlFunctionArgument::Wildcard),
                E::Wildcard(Some(_)) => Err(semantic(
                    "named and qualified-wildcard PostgreSQL function arguments are not supported",
                )),
                _ => {
                    let arg = self.lower(arg, position)?;
                    span = union(span, arg.span);
                    Ok(SqlFunctionArgument::Expression(arg))
                }
            })
            .collect::<Result<Vec<_>>>()?;
        let filter = filter
            .map(|f| self.lower(f, P::Predicate).map(Box::new))
            .transpose()?;
        if let Some(filter) = &filter {
            span = union(span, filter.span);
        }
        if hawdb_core::VectorDistance::from_function_name(&name).is_some() {
            if *distinct
                || filter.is_some()
                || arguments.len() != 2
                || arguments
                    .iter()
                    .any(|arg| matches!(arg, SqlFunctionArgument::Wildcard))
            {
                return Err(semantic("vector distance requires exactly two scalar arguments without DISTINCT or FILTER"));
            }
        } else {
            match name.as_str() {
                "count" | "sum" => {}
                "min" | "max" | "coalesce" | "octet_length" | "uuidv7" | "version"
                    if filter.is_none() => {}
                "min" | "max" | "coalesce" | "octet_length" | "uuidv7" | "version" => {
                    return Err(semantic(
                        "FILTER is supported only for COUNT and SUM aggregates",
                    ))
                }
                _ => return Err(semantic(format!("unsupported PostgreSQL function {name}"))),
            }
        }
        Ok(expression(
            ExprKind::Function {
                name,
                arguments,
                distinct: *distinct,
                filter,
            },
            span,
        ))
    }
}

/// Decode a single already matched SQL string token, without a SQL parser or
/// a second tokenizer. PostgreSQL E-byte escapes retain the existing ASCII and
/// NUL admission policy; U& literals use four or six hexadecimal digits.
fn decode_string(raw: &str) -> Result<String> {
    if raw.starts_with('$') {
        return Err(semantic("unsupported PostgreSQL dollar-quoted literal"));
    }
    let (prefix, body) = if raw.starts_with('e') || raw.starts_with('E') {
        (1, &raw[2..raw.len() - 1])
    } else if raw.starts_with("U&") || raw.starts_with("u&") {
        (2, &raw[3..raw.len() - 1])
    } else {
        (0, &raw[1..raw.len() - 1])
    };
    let mut chars = body.chars().peekable();
    let mut out = String::with_capacity(body.len());
    let invalid = || HawDBError::Parse("invalid escaped string literal".into());
    while let Some(c) = chars.next() {
        if c == '\'' && chars.peek() == Some(&'\'') {
            chars.next();
            out.push('\'');
            continue;
        }
        if c != '\\' || prefix == 0 {
            out.push(c);
            continue;
        }
        let next = chars.next().ok_or_else(invalid)?;
        let decoded = if prefix == 2 {
            if next == '\\' {
                '\\'
            } else {
                let (digits, first) = if next == '+' {
                    (6, None)
                } else {
                    (4, Some(next))
                };
                read_hex(&mut chars, digits, first)
                    .and_then(char::from_u32)
                    .ok_or_else(invalid)?
            }
        } else {
            match next {
                'b' => '\u{8}',
                'f' => '\u{c}',
                'n' => '\n',
                'r' => '\r',
                't' => '\t',
                'u' | 'U' => read_hex(&mut chars, if next == 'u' { 4 } else { 8 }, None)
                    .and_then(char::from_u32)
                    .ok_or_else(invalid)?,
                'x' => {
                    let mut value = 0;
                    let mut count = 0;
                    while count < 2 && chars.peek().is_some_and(|c| c.is_ascii_hexdigit()) {
                        value = value * 16 + chars.next().unwrap().to_digit(16).unwrap();
                        count += 1;
                    }
                    if count == 0 {
                        'x'
                    } else {
                        char::from_u32(value)
                            .filter(|_| value < 128)
                            .ok_or_else(invalid)?
                    }
                }
                '0'..='7' => {
                    let mut value = next.to_digit(8).unwrap();
                    let mut count = 1;
                    while count < 3 && chars.peek().is_some_and(|c| matches!(c, '0'..='7')) {
                        value = value * 8 + chars.next().unwrap().to_digit(8).unwrap();
                        count += 1;
                    }
                    let byte = value & 255;
                    char::from_u32(byte)
                        .filter(|_| byte < 128)
                        .ok_or_else(invalid)?
                }
                c => c,
            }
        };
        if prefix == 1 && decoded == '\0' {
            return Err(invalid());
        }
        out.push(decoded);
    }
    Ok(out)
}
fn read_hex(
    chars: &mut impl Iterator<Item = char>,
    digits: usize,
    first: Option<char>,
) -> Option<u32> {
    let mut value = 0;
    let mut consumed = 0;
    if let Some(c) = first {
        value = c.to_digit(16)?;
        consumed = 1;
    }
    while consumed < digits {
        value = value
            .checked_mul(16)?
            .checked_add(chars.next()?.to_digit(16)?)?;
        consumed += 1;
    }
    Some(value)
}

impl Lowerer<'_> {
    pub(in crate::parser) fn is_text_search_call(
        &self,
        name: &syntax::QualifiedName,
    ) -> Result<bool> {
        let name = self.name(name)?;
        Ok(name.schema.is_none() && name.name == "text_search")
    }

    /// `text_search(index, query, top_k => k)`. Arguments are literals or
    /// positional parameters; the runtime validates their types after binding.
    pub(in crate::parser) fn text_search_call(
        &self,
        args: &[syntax::TableFunctionArgumentSyntax],
    ) -> Result<SqlTextSearch> {
        const USAGE: &str = "text_search requires index and query arguments and top_k => k";
        let [index, query, options @ ..] = args else {
            return Err(semantic(USAGE));
        };
        if index.name.is_some() || query.name.is_some() {
            return Err(semantic(USAGE));
        }
        let mut top_k = None;
        for arg in options {
            let Some(name) = &arg.name else {
                return Err(semantic("text_search options must be named"));
            };
            let slot = match self.ident(name).as_str() {
                "top_k" => &mut top_k,
                _ => return Err(semantic("unknown text_search option")),
            };
            if slot.replace(self.literal(&arg.expression)?).is_some() {
                return Err(semantic("duplicate text_search option"));
            }
        }
        Ok(SqlTextSearch {
            index: self.literal(&index.expression)?,
            query: self.literal(&query.expression)?,
            top_k: top_k.ok_or_else(|| semantic(USAGE))?,
        })
    }

    pub(in crate::parser) fn vector_call(
        &self,
        name: &syntax::QualifiedName,
        args: &[syntax::TableFunctionArgumentSyntax],
    ) -> Result<SqlVectorIndexSearch> {
        let name = self.name(name)?;
        if name.schema.is_some() || name.name != "vector_index_search" {
            return Err(semantic("unsupported SQL table function"));
        }
        let [index, embedding, options @ ..] = args else {
            return Err(semantic(
                "vector_index_search requires index and embedding arguments",
            ));
        };
        if index.name.is_some() || embedding.name.is_some() {
            return Err(semantic(
                "vector_index_search requires index and embedding arguments",
            ));
        }
        let (mut candidates, mut top_k, mut mode, mut max_work) = (None, None, None, None);
        for arg in options {
            let Some(name) = &arg.name else {
                return Err(semantic("vector index options must be named"));
            };
            let slot = match self.ident(name).as_str() {
                "candidates" => &mut candidates,
                "top_k" => &mut top_k,
                "mode" => &mut mode,
                "max_work" => &mut max_work,
                _ => return Err(semantic("unknown vector index option")),
            };
            if slot.replace(self.literal(&arg.expression)?).is_some() {
                return Err(semantic("duplicate vector index option"));
            }
        }
        use hawdb_core::VectorIndexQueryMode as Mode;
        let mode = match mode {
            None => Mode::AnnWindow,
            Some(SqlValue::Literal(Value::String(mode))) => Mode::parse(&mode)?,
            _ => return Err(semantic("vector index mode must be a string literal")),
        };
        let required = || {
            semantic(if mode == Mode::Exact {
                "exact vector search requires explicit top_k"
            } else {
                "vector index requires explicit candidates and top_k"
            })
        };
        match mode {
            Mode::AnnWindow | Mode::FilteredAnn if candidates.is_none() => return Err(required()),
            Mode::Exact if candidates.is_some() => {
                return Err(semantic("exact vector search does not accept candidates"))
            }
            _ => {}
        }
        if mode.requires_eligible_domain() != max_work.is_some() {
            return Err(semantic(
                "max_work is required for exact and filtered_ann vector search only",
            ));
        }
        Ok(SqlVectorIndexSearch {
            index: self.literal(&index.expression)?,
            embedding: self.literal(&embedding.expression)?,
            mode,
            max_work,
            candidates,
            top_k: top_k.ok_or_else(required)?,
        })
    }
}