reddb-io-server 1.1.2

RedDB server-side engine: storage, runtime, replication, MCP, AI, and the gRPC/HTTP/RedWire/PG-wire dispatchers. Re-exported by the umbrella `reddb` crate.
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
//! Function catalog — static table of built-in scalar / aggregate
//! function signatures used by the Fase 3 expression typer to
//! resolve `Expr::FunctionCall` nodes to a concrete return type.
//!
//! Mirrors PostgreSQL's `pg_proc` catalog with a deliberately
//! narrow row shape: a single (name, arg_types, return_type, kind)
//! entry per overload. Multiple entries may share the same name —
//! the resolver picks the one whose argument types match (after
//! implicit coercion) using the `func_select_candidate` heuristic
//! described in the roadmap (parte 4 of the plan file).
//!
//! The table is `const &[FunctionEntry]` so it lives in the
//! read-only segment and lookups stay cache-friendly. Linear
//! scan is fine for the ~30 entries the catalog covers today.
//! Future weeks can switch to a `HashMap<&'static str, &[…]>`
//! grouped by name when the table grows past ~500 entries.
//!
//! ## Coverage today
//!
//! Aggregates: COUNT, SUM, AVG, MIN, MAX (the five SQL-standard
//! ones). Each has multiple overloads for the numeric category.
//!
//! Scalars covered:
//!
//! - String: UPPER, LOWER, LENGTH, COALESCE
//! - Math:   ABS, ROUND, FLOOR, CEIL
//! - Time:   NOW, CURRENT_TIMESTAMP, CURRENT_DATE, TIME_BUCKET
//! - Geo:    GEO_DISTANCE, GEO_BEARING, HAVERSINE
//! - Misc:   VERIFY_PASSWORD
//!
//! Variadic functions (COALESCE, GREATEST, LEAST, CONCAT) are
//! marked with `variadic: true` and the resolver treats their
//! `arg_types` slice as a description of the *uniform* element
//! type — the catalog can't enumerate every arity, so the typer
//! checks each call-site argument against `arg_types[0]` instead.
//!
//! ## What's NOT in this catalog
//!
//! - User-defined functions (CREATE FUNCTION) — separate runtime
//!   table, queried after the static catalog yields no match.
//! - Polymorphic signatures (anyelement, anyarray) — Fase 3 W4.
//! - Operator functions backing `+`, `-`, `*` — those go in
//!   `pg_operator` equivalent which we haven't built yet.

use super::types::DataType;

/// Function classification — affects resolver behavior and
/// downstream planner cost estimation.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FunctionKind {
    /// Pure scalar function: same input → same output, no side
    /// effects, no row-context dependency. The planner is free
    /// to constant-fold or push-down through joins.
    Scalar,
    /// Aggregate function: consumes a Vec of input values, produces
    /// a single output. Only valid in projection lists / HAVING /
    /// window frames.
    Aggregate,
    /// Window function: like aggregate but evaluates over an
    /// ORDER BY frame. Stays in projection lists only.
    Window,
    /// Side-effecting / time-dependent function: NOW(), RANDOM().
    /// The planner cannot cache results across rows.
    Volatile,
}

/// One signature in the static function catalog.
#[derive(Debug, Clone, Copy)]
pub struct FunctionEntry {
    pub name: &'static str,
    pub arg_types: &'static [DataType],
    pub return_type: DataType,
    pub kind: FunctionKind,
    /// When true, the catalog's `arg_types` describes the element
    /// type of a variadic argument list. Resolver matches each
    /// call-site argument against `arg_types[0]` and ignores
    /// arity entirely.
    pub variadic: bool,
}

const fn entry(
    name: &'static str,
    arg_types: &'static [DataType],
    return_type: DataType,
    kind: FunctionKind,
    variadic: bool,
) -> FunctionEntry {
    FunctionEntry {
        name,
        arg_types,
        return_type,
        kind,
        variadic,
    }
}

// ── Argument-list constants used by multiple entries ──
// These are static slices so the FunctionEntry::arg_types pointer
// stays the same across overloads sharing identical signatures.
// The compiler interns the slice symbols so there's no duplicate
// storage even if multiple entries reference the same array.

const ARGS_INT: &[DataType] = &[DataType::Integer];
const ARGS_FLOAT: &[DataType] = &[DataType::Float];
const ARGS_BIGINT: &[DataType] = &[DataType::BigInt];
const ARGS_TEXT: &[DataType] = &[DataType::Text];
const ARGS_TWO_TEXT: &[DataType] = &[DataType::Text, DataType::Text];
const ARGS_TEXT_INT: &[DataType] = &[DataType::Text, DataType::Integer];
const ARGS_TEXT_TWO_INT: &[DataType] = &[DataType::Text, DataType::Integer, DataType::Integer];
const ARGS_NONE: &[DataType] = &[];
const ARGS_TWO_FLOATS: &[DataType] = &[DataType::Float, DataType::Float];
const ARGS_GEO_PAIR: &[DataType] = &[DataType::GeoPoint, DataType::GeoPoint];
const ARGS_FOUR_FLOATS: &[DataType] = &[
    DataType::Float,
    DataType::Float,
    DataType::Float,
    DataType::Float,
];
const ARGS_TIME_BUCKET: &[DataType] = &[DataType::Text, DataType::Timestamp];
const ARGS_VERIFY_PWD: &[DataType] = &[DataType::Password, DataType::Text];
const ARGS_MONEY: &[DataType] = &[DataType::Money];
const ARGS_ONE_TEXT: &[DataType] = &[DataType::Text];
const ARGS_TWO_TEXT_ANY: &[DataType] = &[DataType::Text, DataType::Text];

// JSON function signatures (Phase 1.4 PG parity).
//
// RedDB accepts both Text and Json inputs interchangeably — the evaluator
// parses text on the fly. We list the Text overload in the catalog so
// existing text-column calls resolve without a cast; an explicit Json
// overload is included for schemas that actually use the Json type.
const ARGS_JSON_TEXT: &[DataType] = &[DataType::Text];
const ARGS_JSON_JSON: &[DataType] = &[DataType::Json];
const ARGS_JSON_EXTRACT_TEXT: &[DataType] = &[DataType::Text, DataType::Text];
const ARGS_JSON_EXTRACT_JSON: &[DataType] = &[DataType::Json, DataType::Text];
// JSON_SET takes (json, path, value). Value type is Unknown — the evaluator
// coerces any scalar at runtime (int, float, bool, text, or another json).
const ARGS_JSON_SET_TEXT: &[DataType] = &[DataType::Text, DataType::Text, DataType::Unknown];
const ARGS_JSON_SET_JSON: &[DataType] = &[DataType::Json, DataType::Text, DataType::Unknown];

/// The static function catalog. Append-only; removing a row is a
/// breaking change that may invalidate cached plans referencing
/// the function. Each block is grouped by category for readability.
pub const FUNCTION_CATALOG: &[FunctionEntry] = &[
    // ─────────────────────────────────────────────────────────────
    // Aggregate functions
    // ─────────────────────────────────────────────────────────────
    //
    // COUNT(*) and COUNT(col) are both modelled as `Integer →
    // Integer` here; the parser distinguishes the star form via
    // a separate Projection variant so the catalog doesn't need
    // a magic asterisk overload.
    entry(
        "COUNT",
        ARGS_INT,
        DataType::Integer,
        FunctionKind::Aggregate,
        false,
    ),
    entry(
        "COUNT",
        ARGS_TEXT,
        DataType::Integer,
        FunctionKind::Aggregate,
        false,
    ),
    entry(
        "COUNT",
        ARGS_FLOAT,
        DataType::Integer,
        FunctionKind::Aggregate,
        false,
    ),
    entry(
        "SUM",
        ARGS_INT,
        DataType::Integer,
        FunctionKind::Aggregate,
        false,
    ),
    entry(
        "SUM",
        ARGS_BIGINT,
        DataType::BigInt,
        FunctionKind::Aggregate,
        false,
    ),
    entry(
        "SUM",
        ARGS_FLOAT,
        DataType::Float,
        FunctionKind::Aggregate,
        false,
    ),
    entry(
        "AVG",
        ARGS_INT,
        DataType::Float,
        FunctionKind::Aggregate,
        false,
    ),
    entry(
        "AVG",
        ARGS_FLOAT,
        DataType::Float,
        FunctionKind::Aggregate,
        false,
    ),
    entry(
        "MIN",
        ARGS_INT,
        DataType::Integer,
        FunctionKind::Aggregate,
        false,
    ),
    entry(
        "MIN",
        ARGS_FLOAT,
        DataType::Float,
        FunctionKind::Aggregate,
        false,
    ),
    entry(
        "MIN",
        ARGS_TEXT,
        DataType::Text,
        FunctionKind::Aggregate,
        false,
    ),
    entry(
        "MAX",
        ARGS_INT,
        DataType::Integer,
        FunctionKind::Aggregate,
        false,
    ),
    entry(
        "MAX",
        ARGS_FLOAT,
        DataType::Float,
        FunctionKind::Aggregate,
        false,
    ),
    entry(
        "MAX",
        ARGS_TEXT,
        DataType::Text,
        FunctionKind::Aggregate,
        false,
    ),
    entry(
        "STDDEV",
        ARGS_FLOAT,
        DataType::Float,
        FunctionKind::Aggregate,
        false,
    ),
    entry(
        "VARIANCE",
        ARGS_FLOAT,
        DataType::Float,
        FunctionKind::Aggregate,
        false,
    ),
    entry(
        "GROUP_CONCAT",
        ARGS_TWO_TEXT,
        DataType::Text,
        FunctionKind::Aggregate,
        false,
    ),
    entry(
        "STRING_AGG",
        ARGS_TWO_TEXT,
        DataType::Text,
        FunctionKind::Aggregate,
        false,
    ),
    // ─────────────────────────────────────────────────────────────
    // Scalar — string
    // ─────────────────────────────────────────────────────────────
    entry(
        "UPPER",
        ARGS_TEXT,
        DataType::Text,
        FunctionKind::Scalar,
        false,
    ),
    entry(
        "LOWER",
        ARGS_TEXT,
        DataType::Text,
        FunctionKind::Scalar,
        false,
    ),
    entry(
        "LENGTH",
        ARGS_TEXT,
        DataType::Integer,
        FunctionKind::Scalar,
        false,
    ),
    entry(
        "CHAR_LENGTH",
        ARGS_TEXT,
        DataType::Integer,
        FunctionKind::Scalar,
        false,
    ),
    entry(
        "CHARACTER_LENGTH",
        ARGS_TEXT,
        DataType::Integer,
        FunctionKind::Scalar,
        false,
    ),
    entry(
        "OCTET_LENGTH",
        ARGS_TEXT,
        DataType::Integer,
        FunctionKind::Scalar,
        false,
    ),
    entry(
        "BIT_LENGTH",
        ARGS_TEXT,
        DataType::Integer,
        FunctionKind::Scalar,
        false,
    ),
    entry(
        "SUBSTRING",
        ARGS_TWO_TEXT,
        DataType::Text,
        FunctionKind::Scalar,
        false,
    ),
    entry(
        "SUBSTRING",
        ARGS_TEXT_INT,
        DataType::Text,
        FunctionKind::Scalar,
        false,
    ),
    entry(
        "SUBSTRING",
        ARGS_TEXT_TWO_INT,
        DataType::Text,
        FunctionKind::Scalar,
        false,
    ),
    entry(
        "SUBSTR",
        ARGS_TEXT_INT,
        DataType::Text,
        FunctionKind::Scalar,
        false,
    ),
    entry(
        "SUBSTR",
        ARGS_TEXT_TWO_INT,
        DataType::Text,
        FunctionKind::Scalar,
        false,
    ),
    entry(
        "POSITION",
        ARGS_TWO_TEXT,
        DataType::Integer,
        FunctionKind::Scalar,
        false,
    ),
    entry(
        "TRIM",
        ARGS_TEXT,
        DataType::Text,
        FunctionKind::Scalar,
        false,
    ),
    entry(
        "TRIM",
        ARGS_TWO_TEXT,
        DataType::Text,
        FunctionKind::Scalar,
        false,
    ),
    entry(
        "LTRIM",
        ARGS_TEXT,
        DataType::Text,
        FunctionKind::Scalar,
        false,
    ),
    entry(
        "LTRIM",
        ARGS_TWO_TEXT,
        DataType::Text,
        FunctionKind::Scalar,
        false,
    ),
    entry(
        "RTRIM",
        ARGS_TEXT,
        DataType::Text,
        FunctionKind::Scalar,
        false,
    ),
    entry(
        "RTRIM",
        ARGS_TWO_TEXT,
        DataType::Text,
        FunctionKind::Scalar,
        false,
    ),
    entry(
        "BTRIM",
        ARGS_TEXT,
        DataType::Text,
        FunctionKind::Scalar,
        false,
    ),
    entry(
        "BTRIM",
        ARGS_TWO_TEXT,
        DataType::Text,
        FunctionKind::Scalar,
        false,
    ),
    entry(
        "CONCAT",
        ARGS_TEXT,
        DataType::Text,
        FunctionKind::Scalar,
        true,
    ),
    entry(
        "CONCAT_WS",
        ARGS_TEXT,
        DataType::Text,
        FunctionKind::Scalar,
        true,
    ),
    entry(
        "REVERSE",
        ARGS_TEXT,
        DataType::Text,
        FunctionKind::Scalar,
        false,
    ),
    entry(
        "LEFT",
        ARGS_TEXT_INT,
        DataType::Text,
        FunctionKind::Scalar,
        false,
    ),
    entry(
        "RIGHT",
        ARGS_TEXT_INT,
        DataType::Text,
        FunctionKind::Scalar,
        false,
    ),
    entry(
        "QUOTE_LITERAL",
        ARGS_TEXT,
        DataType::Text,
        FunctionKind::Scalar,
        false,
    ),
    // COALESCE is variadic over a uniform element type. The
    // resolver matches each call-site arg against arg_types[0]
    // (any concrete type), and the return type is propagated
    // from the first non-null argument's type at typing time.
    entry(
        "COALESCE",
        ARGS_TEXT,
        DataType::Text,
        FunctionKind::Scalar,
        true,
    ),
    // ─────────────────────────────────────────────────────────────
    // Scalar — math
    // ─────────────────────────────────────────────────────────────
    entry(
        "ABS",
        ARGS_INT,
        DataType::Integer,
        FunctionKind::Scalar,
        false,
    ),
    entry(
        "ABS",
        ARGS_FLOAT,
        DataType::Float,
        FunctionKind::Scalar,
        false,
    ),
    entry(
        "ROUND",
        ARGS_FLOAT,
        DataType::Float,
        FunctionKind::Scalar,
        false,
    ),
    entry(
        "FLOOR",
        ARGS_FLOAT,
        DataType::Float,
        FunctionKind::Scalar,
        false,
    ),
    entry(
        "CEIL",
        ARGS_FLOAT,
        DataType::Float,
        FunctionKind::Scalar,
        false,
    ),
    // ─────────────────────────────────────────────────────────────
    // Scalar — time
    // ─────────────────────────────────────────────────────────────
    //
    // NOW / CURRENT_TIMESTAMP / CURRENT_DATE are no-arg volatile
    // scalars that read the wall clock at evaluation time. The
    // planner must not constant-fold them across rows.
    entry(
        "NOW",
        ARGS_NONE,
        DataType::TimestampMs,
        FunctionKind::Volatile,
        false,
    ),
    entry(
        "CURRENT_TIMESTAMP",
        ARGS_NONE,
        DataType::TimestampMs,
        FunctionKind::Volatile,
        false,
    ),
    entry(
        "CURRENT_DATE",
        ARGS_NONE,
        DataType::Date,
        FunctionKind::Volatile,
        false,
    ),
    // Phase 2.5.3 multi-tenancy + PG session identity scalars. Marked
    // Volatile so the planner cannot constant-fold them across rows
    // — the thread-local value is hot-swappable per statement.
    entry(
        "CURRENT_TENANT",
        ARGS_NONE,
        DataType::Text,
        FunctionKind::Volatile,
        false,
    ),
    entry(
        "CURRENT_USER",
        ARGS_NONE,
        DataType::Text,
        FunctionKind::Volatile,
        false,
    ),
    entry(
        "SESSION_USER",
        ARGS_NONE,
        DataType::Text,
        FunctionKind::Volatile,
        false,
    ),
    entry(
        "CURRENT_ROLE",
        ARGS_NONE,
        DataType::Text,
        FunctionKind::Volatile,
        false,
    ),
    entry(
        "TIME_BUCKET",
        ARGS_TIME_BUCKET,
        DataType::TimestampMs,
        FunctionKind::Scalar,
        false,
    ),
    // ─────────────────────────────────────────────────────────────
    // Scalar — geo
    // ─────────────────────────────────────────────────────────────
    entry(
        "GEO_DISTANCE",
        ARGS_GEO_PAIR,
        DataType::Float,
        FunctionKind::Scalar,
        false,
    ),
    entry(
        "GEO_DISTANCE",
        ARGS_FOUR_FLOATS,
        DataType::Float,
        FunctionKind::Scalar,
        false,
    ),
    entry(
        "GEO_BEARING",
        ARGS_FOUR_FLOATS,
        DataType::Float,
        FunctionKind::Scalar,
        false,
    ),
    entry(
        "HAVERSINE",
        ARGS_FOUR_FLOATS,
        DataType::Float,
        FunctionKind::Scalar,
        false,
    ),
    entry(
        "VINCENTY",
        ARGS_FOUR_FLOATS,
        DataType::Float,
        FunctionKind::Scalar,
        false,
    ),
    // ─────────────────────────────────────────────────────────────
    // Scalar — security
    // ─────────────────────────────────────────────────────────────
    //
    // VERIFY_PASSWORD takes a hashed Password column + a candidate
    // plaintext Text and returns Boolean. Marked Volatile because
    // the underlying argon2id verify is intentionally slow and the
    // planner should not cache results.
    entry(
        "VERIFY_PASSWORD",
        ARGS_VERIFY_PWD,
        DataType::Boolean,
        FunctionKind::Volatile,
        false,
    ),
    entry(
        "MONEY",
        ARGS_ONE_TEXT,
        DataType::Money,
        FunctionKind::Scalar,
        false,
    ),
    entry(
        "MONEY",
        ARGS_TWO_TEXT_ANY,
        DataType::Money,
        FunctionKind::Scalar,
        false,
    ),
    entry(
        "MONEY_ASSET",
        ARGS_MONEY,
        DataType::AssetCode,
        FunctionKind::Scalar,
        false,
    ),
    entry(
        "MONEY_MINOR",
        ARGS_MONEY,
        DataType::BigInt,
        FunctionKind::Scalar,
        false,
    ),
    entry(
        "MONEY_SCALE",
        ARGS_MONEY,
        DataType::Integer,
        FunctionKind::Scalar,
        false,
    ),
    // Two-floats variant used by some places (legacy dual-arg form).
    entry(
        "POWER",
        ARGS_TWO_FLOATS,
        DataType::Float,
        FunctionKind::Scalar,
        false,
    ),
    // ─────────────────────────────────────────────────────────────
    // Scalar — JSON (Phase 1.4 PG parity)
    // ─────────────────────────────────────────────────────────────
    //
    // JSON_EXTRACT(json_or_text, path) → Text
    //   Returns the value at `path` serialised as JSON text. Scalar strings
    //   come back with surrounding quotes; scalar numbers/booleans come back
    //   as bare tokens. Returns NULL when the path does not resolve.
    entry(
        "JSON_EXTRACT",
        ARGS_JSON_EXTRACT_TEXT,
        DataType::Text,
        FunctionKind::Scalar,
        false,
    ),
    entry(
        "JSON_EXTRACT",
        ARGS_JSON_EXTRACT_JSON,
        DataType::Text,
        FunctionKind::Scalar,
        false,
    ),
    // JSON_EXTRACT_TEXT(json_or_text, path) → Text
    //   Same as JSON_EXTRACT but strings come back unquoted (PG `->>` style).
    entry(
        "JSON_EXTRACT_TEXT",
        ARGS_JSON_EXTRACT_TEXT,
        DataType::Text,
        FunctionKind::Scalar,
        false,
    ),
    entry(
        "JSON_EXTRACT_TEXT",
        ARGS_JSON_EXTRACT_JSON,
        DataType::Text,
        FunctionKind::Scalar,
        false,
    ),
    // JSON_SET(json_or_text, path, value) → Json
    //   Immutable update — returns a new JSON blob with `value` written at `path`.
    //   Creates missing parent objects/arrays on the path.
    entry(
        "JSON_SET",
        ARGS_JSON_SET_TEXT,
        DataType::Json,
        FunctionKind::Scalar,
        false,
    ),
    entry(
        "JSON_SET",
        ARGS_JSON_SET_JSON,
        DataType::Json,
        FunctionKind::Scalar,
        false,
    ),
    // JSON_ARRAY_LENGTH(json) → Integer
    entry(
        "JSON_ARRAY_LENGTH",
        ARGS_JSON_TEXT,
        DataType::Integer,
        FunctionKind::Scalar,
        false,
    ),
    entry(
        "JSON_ARRAY_LENGTH",
        ARGS_JSON_JSON,
        DataType::Integer,
        FunctionKind::Scalar,
        false,
    ),
    // JSON_TYPEOF(json) → Text ("null" | "boolean" | "number" | "string" | "array" | "object")
    entry(
        "JSON_TYPEOF",
        ARGS_JSON_TEXT,
        DataType::Text,
        FunctionKind::Scalar,
        false,
    ),
    entry(
        "JSON_TYPEOF",
        ARGS_JSON_JSON,
        DataType::Text,
        FunctionKind::Scalar,
        false,
    ),
    // JSON_VALID(text) → Boolean — returns true if the argument parses.
    entry(
        "JSON_VALID",
        ARGS_TEXT,
        DataType::Boolean,
        FunctionKind::Scalar,
        false,
    ),
    // JSON_ARRAY(any*) → Json — variadic array constructor.
    // Resolver uses arg_types[0] as the template; Unknown means "any scalar".
    entry(
        "JSON_ARRAY",
        &[DataType::Unknown],
        DataType::Json,
        FunctionKind::Scalar,
        true,
    ),
    // JSON_OBJECT(key, value, ...) → Json — variadic k/v pair object constructor.
    entry(
        "JSON_OBJECT",
        &[DataType::Unknown],
        DataType::Json,
        FunctionKind::Scalar,
        true,
    ),
];

/// Look up a function by name, returning the slice of overloads
/// (possibly empty). The resolver walks this slice and picks the
/// best match using its own coercion logic.
pub fn lookup(name: &str) -> Vec<&'static FunctionEntry> {
    FUNCTION_CATALOG
        .iter()
        .filter(|e| e.name.eq_ignore_ascii_case(name))
        .collect()
}

/// Resolve a function call to the best-matching overload. Returns
/// `None` when no overload matches the call-site argument types
/// (after implicit coercion).
///
/// **As of issue #82, this function delegates to
/// `coercion_spine::resolve_function`**. The spine owns the
/// coercion-aware overload-picking rule that used to live inline
/// here; the catalog itself is now a pure registry of `pg_proc`-style
/// rows. Callers that need the per-argument implicit-cast list
/// (which the runtime evaluator must apply before dispatch) should
/// call `coercion_spine::resolve_function` directly — this wrapper
/// only returns the entry to preserve the existing call-site
/// signature.
pub fn resolve(name: &str, arg_types: &[DataType]) -> Option<&'static FunctionEntry> {
    super::coercion_spine::resolve_function(name, arg_types).map(|(entry, _)| entry)
}