spg-engine 7.37.23

Execution engine for SPG: glues spg-sql parsing to spg-storage. Foreign keys, joins, vectors, cold tier.
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
//! v7.17.0 Phase 7 — INET / CIDR text helpers.
//!
//! SPG stores network address types as Text. The host() / network() /
//! masklen() helpers parse the textual `addr[/mask]` form and return
//! the constituent pieces, matching PG's contract for the dominant
//! customer surface (Django ORM / Rails ORM normalisation). The
//! `inet_op_bool_result` arm backs the INET/CIDR containment operators.
//! Split out of `eval.rs` (cut 24).

use alloc::format;
use alloc::string::ToString;
use alloc::vec::Vec;

use spg_sql::ast::BinOp;
use spg_storage::Value;

use super::EvalError;

/// A network-address function argument: accept the legacy TEXT form OR a
/// real INET / CIDR value (rendered to its canonical text first). Fixes the
/// inet builtins erroring when handed an actual inet/cidr column value.
fn inet_arg_text(v: &Value<'_>) -> Option<alloc::string::String> {
    match v {
        Value::Text(s) => Some(s.to_string()),
        Value::Inet { family, bits, addr } | Value::Cidr { family, bits, addr } => {
            Some(crate::conversions::format_inet(*family, *bits, addr))
        }
        _ => None,
    }
}

pub(super) fn inet_host(args: &[Value<'_>]) -> Result<Value<'static>, EvalError> {
    let s = match args {
        [Value::Text(s)] => s.clone(),
        [Value::Inet { family, bits, addr }] | [Value::Cidr { family, bits, addr }] => {
            alloc::borrow::Cow::Owned(crate::conversions::format_inet(*family, *bits, addr))
        }
        [Value::Null] => return Ok(Value::Null),
        _ => {
            return Err(EvalError::TypeMismatch {
                detail: alloc::format!("host() takes one TEXT arg, got {} args", args.len()),
            });
        }
    };
    let host = s.split('/').next().unwrap_or("").to_string();
    Ok(Value::text(host))
}

pub(super) fn inet_network(args: &[Value<'_>]) -> Result<Value<'static>, EvalError> {
    let s = match args {
        [Value::Text(s)] => s.clone(),
        [Value::Inet { family, bits, addr }] | [Value::Cidr { family, bits, addr }] => {
            alloc::borrow::Cow::Owned(crate::conversions::format_inet(*family, *bits, addr))
        }
        [Value::Null] => return Ok(Value::Null),
        _ => {
            return Err(EvalError::TypeMismatch {
                detail: alloc::format!("network() takes one TEXT arg, got {} args", args.len()),
            });
        }
    };
    // For a `host/mask` form return the masked-network address.
    let mut split = s.splitn(2, '/');
    let host = split.next().unwrap_or("").to_string();
    let mask: u32 = split.next().and_then(|m| m.parse().ok()).unwrap_or(32);
    if !host.contains('.') {
        // IPv6 / MACADDR — return the input unmasked.
        return Ok(Value::text(s));
    }
    // v7.39 (read01 round 103) — bit-level masking: zero every host bit, not
    // just whole trailing octets. The old octet-granular path left `/25`,
    // `/26`, … unmasked (network('192.168.1.130/25') returned .130 instead of
    // PG's .128).
    let Some(addr) = ipv4_to_u32(&host) else {
        return Ok(Value::text(s));
    };
    let mask = mask.min(32);
    let masked = if mask == 0 {
        0
    } else {
        addr & (u32::MAX << (32 - mask))
    };
    Ok(Value::text(alloc::format!(
        "{}.{}.{}.{}/{mask}",
        (masked >> 24) & 0xFF,
        (masked >> 16) & 0xFF,
        (masked >> 8) & 0xFF,
        masked & 0xFF
    )))
}

/// v7.39 (read01 round 103) — parse a dotted-quad IPv4 host into a u32, or
/// `None` if it isn't four 0..=255 octets.
fn ipv4_to_u32(host: &str) -> Option<u32> {
    let octets: Vec<&str> = host.split('.').collect();
    if octets.len() != 4 {
        return None;
    }
    let mut addr: u32 = 0;
    for oct in &octets {
        let b: u32 = oct.parse().ok()?;
        if b > 255 {
            return None;
        }
        addr = (addr << 8) | b;
    }
    Some(addr)
}

/// v7.37.17 (17.6 siblings) — family(inet) returns 4 for IPv4,
/// 6 for IPv6.
pub(super) fn inet_family(args: &[Value<'_>]) -> Result<Value<'static>, EvalError> {
    let s = match args {
        [Value::Text(s)] => s.clone(),
        [Value::Inet { family, bits, addr }] | [Value::Cidr { family, bits, addr }] => {
            alloc::borrow::Cow::Owned(crate::conversions::format_inet(*family, *bits, addr))
        }
        [Value::Null] => return Ok(Value::Null),
        _ => {
            return Err(EvalError::TypeMismatch {
                detail: alloc::format!("family() takes one TEXT arg, got {} args", args.len()),
            });
        }
    };
    let host = s.split('/').next().unwrap_or("");
    if host.contains(':') {
        Ok(Value::Int(6))
    } else {
        Ok(Value::Int(4))
    }
}

/// v7.37.17 (17.6 siblings) — netmask(inet) builds the dotted-quad
/// netmask from the prefix length (IPv4 only for now).
pub(super) fn inet_netmask(args: &[Value<'_>]) -> Result<Value<'static>, EvalError> {
    let s = match args {
        [Value::Text(s)] => s.clone(),
        [Value::Inet { family, bits, addr }] | [Value::Cidr { family, bits, addr }] => {
            alloc::borrow::Cow::Owned(crate::conversions::format_inet(*family, *bits, addr))
        }
        [Value::Null] => return Ok(Value::Null),
        _ => {
            return Err(EvalError::TypeMismatch {
                detail: alloc::format!("netmask() takes one TEXT arg, got {} args", args.len()),
            });
        }
    };
    let mut split = s.splitn(2, '/');
    let host = split.next().unwrap_or("");
    let mask: u32 = split.next().and_then(|m| m.parse().ok()).unwrap_or(32);
    if host.contains(':') {
        // IPv6 — punt to text passthrough until full v6 support.
        return Ok(Value::text(s));
    }
    let mask = mask.min(32);
    let bits: u32 = if mask == 0 {
        0
    } else {
        u32::MAX << (32 - mask)
    };
    Ok(Value::text(alloc::format!(
        "{}.{}.{}.{}",
        (bits >> 24) & 0xFF,
        (bits >> 16) & 0xFF,
        (bits >> 8) & 0xFF,
        bits & 0xFF
    )))
}

/// v7.37.17 (17.6 siblings) — hostmask(inet) — the complement of
/// netmask (IPv4).
pub(super) fn inet_hostmask(args: &[Value<'_>]) -> Result<Value<'static>, EvalError> {
    let s = match args {
        [Value::Text(s)] => s.clone(),
        [Value::Inet { family, bits, addr }] | [Value::Cidr { family, bits, addr }] => {
            alloc::borrow::Cow::Owned(crate::conversions::format_inet(*family, *bits, addr))
        }
        [Value::Null] => return Ok(Value::Null),
        _ => {
            return Err(EvalError::TypeMismatch {
                detail: alloc::format!("hostmask() takes one TEXT arg, got {} args", args.len()),
            });
        }
    };
    let mut split = s.splitn(2, '/');
    let host = split.next().unwrap_or("");
    let mask: u32 = split.next().and_then(|m| m.parse().ok()).unwrap_or(32);
    if host.contains(':') {
        return Ok(Value::text(s));
    }
    let mask = mask.min(32);
    let bits: u32 = if mask == 0 {
        u32::MAX
    } else {
        !(u32::MAX << (32 - mask))
    };
    Ok(Value::text(alloc::format!(
        "{}.{}.{}.{}",
        (bits >> 24) & 0xFF,
        (bits >> 16) & 0xFF,
        (bits >> 8) & 0xFF,
        bits & 0xFF
    )))
}

/// v7.37.17 (17.6 siblings) — broadcast(inet) — network address
/// with host bits set to 1 (IPv4).
pub(super) fn inet_broadcast(args: &[Value<'_>]) -> Result<Value<'static>, EvalError> {
    let s = match args {
        [Value::Text(s)] => s.clone(),
        [Value::Inet { family, bits, addr }] | [Value::Cidr { family, bits, addr }] => {
            alloc::borrow::Cow::Owned(crate::conversions::format_inet(*family, *bits, addr))
        }
        [Value::Null] => return Ok(Value::Null),
        _ => {
            return Err(EvalError::TypeMismatch {
                detail: alloc::format!("broadcast() takes one TEXT arg, got {} args", args.len()),
            });
        }
    };
    let mut split = s.splitn(2, '/');
    let host = split.next().unwrap_or("");
    let mask: u32 = split.next().and_then(|m| m.parse().ok()).unwrap_or(32);
    if host.contains(':') {
        return Ok(Value::text(s));
    }
    let octets: Vec<u32> = host.split('.').filter_map(|o| o.parse().ok()).collect();
    if octets.len() != 4 {
        return Ok(Value::text(s));
    }
    let addr = (octets[0] << 24) | (octets[1] << 16) | (octets[2] << 8) | octets[3];
    let mask = mask.min(32);
    let host_bits: u32 = if mask == 0 {
        u32::MAX
    } else {
        !(u32::MAX << (32 - mask))
    };
    let bcast = addr | host_bits;
    Ok(Value::text(alloc::format!(
        "{}.{}.{}.{}/{mask}",
        (bcast >> 24) & 0xFF,
        (bcast >> 16) & 0xFF,
        (bcast >> 8) & 0xFF,
        bcast & 0xFF
    )))
}

/// v7.37.17 (17.6 siblings) — inet_merge(a, b) — the smallest
/// network that includes both arguments (IPv4; IPv6 mixed input
/// errors like PG's "cannot merge addresses from different
/// families" when families differ, text passthrough of the first
/// arg when both are IPv6).
pub(super) fn inet_merge(args: &[Value<'_>]) -> Result<Value<'static>, EvalError> {
    // v7.39 (read01 inet family) — typed rewrite: the smallest network
    // containing both arguments (longest common prefix capped by both
    // masks), PG inet_merge.
    let unpack = |v: &Value<'_>| -> Option<(u8, u8, [u8; 16])> {
        match v {
            Value::Inet { family, bits, addr } | Value::Cidr { family, bits, addr } => {
                Some((*family, *bits, *addr))
            }
            // An unknown text literal coerces to inet, like PG.
            Value::Text(s) => crate::conversions::parse_inet_text(s),
            _ => None,
        }
    };
    if args.iter().any(|v| matches!(v, Value::Null)) {
        return Ok(Value::Null);
    }
    let (Some((fa, ba, aa)), Some((fb, bb, ab))) =
        (args.first().and_then(unpack), args.get(1).and_then(unpack))
    else {
        return Err(EvalError::TypeMismatch {
            detail: "inet_merge() takes 2 inet/cidr args".into(),
        });
    };
    if fa != fb {
        return Err(EvalError::TypeMismatch {
            detail: "cannot merge addresses from different families".into(),
        });
    }
    let nbits = u16::from(ba.min(bb));
    let mut common: u16 = 0;
    'outer: for byte in 0..16usize {
        for bit in 0..8u16 {
            let pos = (byte as u16) * 8 + bit;
            if pos >= nbits {
                break 'outer;
            }
            let mask = 0x80u8 >> bit;
            if (aa[byte] & mask) != (ab[byte] & mask) {
                break 'outer;
            }
            common = pos + 1;
        }
    }
    let mut addr = [0u8; 16];
    for byte in 0..16usize {
        let bit_base = (byte as u16) * 8;
        let keep = common.saturating_sub(bit_base).min(8) as u8;
        let mask: u8 = if keep == 0 { 0 } else { 0xffu8 << (8 - keep) };
        addr[byte] = aa[byte] & mask;
    }
    #[allow(clippy::cast_possible_truncation)]
    Ok(Value::Cidr {
        family: fa,
        bits: common as u8,
        addr,
    })
}

/// v7.37.17 (17.6 siblings) — macaddr8_set7bit(macaddr8) — sets
/// the 7th bit (0x02, locally-administered) of the first byte,
/// converting an EUI-64 into a modified EUI-64 for IPv6 autoconf.
pub(super) fn macaddr8_set7bit(args: &[Value<'_>]) -> Result<Value<'static>, EvalError> {
    // v7.38 (read01) — PG's macaddr8_set7bit takes (and returns) a macaddr8.
    // An unadorned string literal is the unknown-type form PG casts for you.
    let mut out: [u8; 8] = match args {
        [Value::Macaddr8(b)] => *b,
        [Value::Null] => return Ok(Value::Null),
        [Value::Text(s)] => {
            let bytes: Vec<u8> = s
                .split([':', '-'])
                .filter_map(|part| u8::from_str_radix(part, 16).ok())
                .collect();
            <[u8; 8]>::try_from(bytes.as_slice()).map_err(|_| EvalError::TypeMismatch {
                detail: alloc::format!("macaddr8_set7bit(): invalid macaddr8 '{s}'"),
            })?
        }
        _ => {
            return Err(EvalError::TypeMismatch {
                detail: alloc::format!(
                    "macaddr8_set7bit() takes one macaddr8 arg, got {} args",
                    args.len()
                ),
            });
        }
    };
    out[0] |= 0x02;
    Ok(Value::Macaddr8(out))
}

/// v7.37.17 (17.6 siblings) — inet_same_family(a, b).
pub(super) fn inet_same_family(args: &[Value<'_>]) -> Result<Value<'static>, EvalError> {
    // v7.39 (read01 inet family) — typed rewrite (values arrive as
    // Value::Inet/Cidr since the typed-inet work; text stays accepted).
    let fam = |v: &Value<'_>| -> Option<u8> {
        match v {
            Value::Inet { family, .. } | Value::Cidr { family, .. } => Some(*family),
            Value::Text(s) => Some(if s.split('/').next().unwrap_or("").contains(':') {
                6
            } else {
                4
            }),
            _ => None,
        }
    };
    if args.iter().any(|v| matches!(v, Value::Null)) {
        return Ok(Value::Null);
    }
    match (args.first().and_then(fam), args.get(1).and_then(fam)) {
        (Some(a), Some(b)) => Ok(Value::Bool(a == b)),
        _ => Err(EvalError::TypeMismatch {
            detail: "inet_same_family() takes 2 inet args".into(),
        }),
    }
}

pub(super) fn inet_masklen(args: &[Value<'_>]) -> Result<Value<'static>, EvalError> {
    let s = match args {
        [Value::Text(s)] => s.clone(),
        [Value::Inet { family, bits, addr }] | [Value::Cidr { family, bits, addr }] => {
            alloc::borrow::Cow::Owned(crate::conversions::format_inet(*family, *bits, addr))
        }
        [Value::Null] => return Ok(Value::Null),
        _ => {
            return Err(EvalError::TypeMismatch {
                detail: alloc::format!("masklen() takes one TEXT arg, got {} args", args.len()),
            });
        }
    };
    let mask: i32 = s
        .split_once('/')
        .and_then(|(_, m)| m.parse().ok())
        .unwrap_or(32);
    Ok(Value::Int(mask))
}

/// `set_masklen(inet|cidr, n)` — change the prefix length, keeping the address
/// and the argument's type. PG clamps to the family's maximum.
pub(super) fn inet_set_masklen(args: &[Value<'_>]) -> Result<Value<'static>, EvalError> {
    if args.iter().any(|v| matches!(v, Value::Null)) {
        return Ok(Value::Null);
    }
    let (family, addr, is_cidr) = match args.first() {
        Some(Value::Inet { family, addr, .. }) => (*family, *addr, false),
        Some(Value::Cidr { family, addr, .. }) => (*family, *addr, true),
        _ => {
            return Err(EvalError::TypeMismatch {
                detail: "set_masklen() first arg must be inet/cidr".into(),
            });
        }
    };
    let n = match args.get(1) {
        Some(Value::SmallInt(v)) => i64::from(*v),
        Some(Value::Int(v)) => i64::from(*v),
        Some(Value::BigInt(v)) => *v,
        _ => {
            return Err(EvalError::TypeMismatch {
                detail: "set_masklen() second arg must be an integer".into(),
            });
        }
    };
    let max = if family == 4 { 32 } else { 128 };
    #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
    let bits = n.clamp(0, max) as u8;
    if is_cidr {
        // v7.39 (read01 inet_cidr_ntop.c) — the cidr variant ZEROES the
        // bits right of the new mask (PG cidr_set_masklen); the inet
        // variant keeps the host part.
        let mut addr = addr;
        let nbytes = if family == 4 { 4 } else { 16 };
        for byte in 0..nbytes {
            let bit_base = (byte as u16) * 8;
            let keep = u16::from(bits).saturating_sub(bit_base).min(8) as u8;
            let mask: u8 = if keep == 0 { 0 } else { 0xffu8 << (8 - keep) };
            addr[byte] &= mask;
        }
        Ok(Value::Cidr { family, bits, addr })
    } else {
        Ok(Value::Inet { family, bits, addr })
    }
}

/// `abbrev(cidr)` — the shortest text form, dropping octets past the prefix
/// (`192.168.1.0/24` → `192.168.1/24`, `10.0.0.0/8` → `10/8`). IPv6 falls
/// back to the full text form.
pub(super) fn inet_abbrev(args: &[Value<'_>]) -> Result<Value<'static>, EvalError> {
    let (family, bits, addr, is_cidr) = match args.first() {
        Some(Value::Cidr { family, bits, addr }) => (*family, *bits, *addr, true),
        Some(Value::Inet { family, bits, addr }) => (*family, *bits, *addr, false),
        Some(Value::Null) => return Ok(Value::Null),
        _ => {
            return Err(EvalError::TypeMismatch {
                detail: "abbrev() arg must be inet/cidr".into(),
            });
        }
    };
    // abbrev(inet) keeps the full canonical text (PG: `192.168.1.0/24`
    // stays intact, a /32 host drops the mask); only abbrev(cidr) drops
    // the octets past the network prefix.
    if !is_cidr || family != 4 {
        return Ok(Value::text(crate::conversions::format_inet(
            family, bits, &addr,
        )));
    }
    let sig = ((usize::from(bits) + 7) / 8).max(1);
    let parts: alloc::vec::Vec<alloc::string::String> = addr[0..sig]
        .iter()
        .map(alloc::string::ToString::to_string)
        .collect();
    Ok(Value::text(alloc::format!("{}/{}", parts.join("."), bits)))
}

// ─── v7.17.0 Phase 3.P0-47 — INET / CIDR containment + overlap ────────
//
// SPG stores INET / CIDR as Text (Phase 7 design); these helpers parse
// the textual `addr[/mask]` form into a (family, bytes, prefix_bits)
// triple and implement PG's network-comparison operators on that
// representation.
//
// PG semantics:
//   * `<<`  — strictly contained-in (LHS ⊊ RHS)
//   * `<<=` — contained-in-or-equal (LHS ⊆ RHS)
//   * `>>`, `>>=` — mirrors of the above
//   * `&&`  — overlap (either LHS ⊆ RHS or RHS ⊆ LHS)
//
// NULL on either side → NULL (3VL). Mixed family (v4 vs v6) is never
// contained / never overlaps but is not an error — same as PG.

/// Parsed inet network: address bytes (4 for v4, 16 for v6) and the
/// network prefix length in bits.
struct InetNet {
    bytes: [u8; 16],
    /// 4 for IPv4, 16 for IPv6.
    family_bytes: u8,
    /// 0..=32 for v4, 0..=128 for v6.
    prefix_bits: u8,
}

fn parse_inet_text(s: &str) -> Option<InetNet> {
    let mut split = s.splitn(2, '/');
    let host = split.next()?;
    let mask_str = split.next();
    if host.contains(':') {
        let bytes = parse_ipv6(host)?;
        let prefix_bits = match mask_str {
            Some(m) => m.parse::<u8>().ok().filter(|&n| n <= 128)?,
            None => 128,
        };
        let mut out = [0u8; 16];
        out.copy_from_slice(&bytes);
        Some(InetNet {
            bytes: out,
            family_bytes: 16,
            prefix_bits,
        })
    } else {
        let bytes = parse_ipv4(host)?;
        let prefix_bits = match mask_str {
            Some(m) => m.parse::<u8>().ok().filter(|&n| n <= 32)?,
            None => 32,
        };
        let mut out = [0u8; 16];
        out[..4].copy_from_slice(&bytes);
        Some(InetNet {
            bytes: out,
            family_bytes: 4,
            prefix_bits,
        })
    }
}

fn parse_ipv4(s: &str) -> Option<[u8; 4]> {
    let parts: Vec<&str> = s.split('.').collect();
    if parts.len() != 4 {
        return None;
    }
    let mut out = [0u8; 4];
    for (i, p) in parts.iter().enumerate() {
        out[i] = p.parse::<u8>().ok()?;
    }
    Some(out)
}

fn parse_ipv6(s: &str) -> Option<[u8; 16]> {
    // Split on the `::` shorthand at most once.
    let (head, tail) = match s.find("::") {
        Some(idx) => (&s[..idx], Some(&s[idx + 2..])),
        None => (s, None),
    };
    let head_groups: Vec<&str> = if head.is_empty() {
        Vec::new()
    } else {
        head.split(':').collect()
    };
    let tail_groups: Vec<&str> = match tail {
        Some(t) if !t.is_empty() => t.split(':').collect(),
        _ => Vec::new(),
    };
    let head_len = head_groups.len();
    let tail_len = tail_groups.len();
    // Without `::` we need exactly 8 groups; with `::` we need ≤ 7.
    if tail.is_none() {
        if head_len != 8 {
            return None;
        }
    } else if head_len + tail_len > 7 {
        return None;
    }
    let mut words = [0u16; 8];
    for (i, g) in head_groups.iter().enumerate() {
        words[i] = u16::from_str_radix(g, 16).ok()?;
    }
    let tail_start = 8 - tail_len;
    for (i, g) in tail_groups.iter().enumerate() {
        words[tail_start + i] = u16::from_str_radix(g, 16).ok()?;
    }
    let mut out = [0u8; 16];
    for (i, w) in words.iter().enumerate() {
        out[i * 2] = (w >> 8) as u8;
        out[i * 2 + 1] = (w & 0xff) as u8;
    }
    Some(out)
}

/// Compare the first `prefix_bits` bits of `a` and `b`. Returns true if
/// they match. `prefix_bits` must not exceed the family size.
fn network_prefix_eq(a: &InetNet, b: &InetNet, prefix_bits: u8) -> bool {
    let full_bytes = (prefix_bits / 8) as usize;
    if a.bytes[..full_bytes] != b.bytes[..full_bytes] {
        return false;
    }
    let extra = prefix_bits % 8;
    if extra == 0 {
        return true;
    }
    let mask: u8 = 0xff << (8 - extra);
    (a.bytes[full_bytes] & mask) == (b.bytes[full_bytes] & mask)
}

/// True iff network `a` is fully contained in network `b` (a ⊆ b).
fn inet_contained_eq(a: &InetNet, b: &InetNet) -> bool {
    if a.family_bytes != b.family_bytes {
        return false;
    }
    if a.prefix_bits < b.prefix_bits {
        return false;
    }
    network_prefix_eq(a, b, b.prefix_bits)
}

/// True iff a and b are exactly the same network (same family + same
/// prefix + same masked address).
fn inet_networks_equal(a: &InetNet, b: &InetNet) -> bool {
    if a.family_bytes != b.family_bytes {
        return false;
    }
    if a.prefix_bits != b.prefix_bits {
        return false;
    }
    network_prefix_eq(a, b, a.prefix_bits)
}

pub(super) fn inet_op_bool_result(
    op: BinOp,
    l: &Value,
    r: &Value,
) -> Result<Value<'static>, EvalError> {
    if matches!(l, Value::Null) || matches!(r, Value::Null) {
        return Ok(Value::Null);
    }
    // v7.37.5 ζ-A — INET/CIDR are first-class typed Values. Accept
    // both the typed shape (no re-parse) and the legacy Text shape
    // (re-parse on demand, since old columns / `'1.2.3.4'::text`
    // literals still flow through here).
    let to_inet = |v: &Value| -> Result<InetNet, EvalError> {
        match v {
            Value::Inet { family, bits, addr } | Value::Cidr { family, bits, addr } => {
                Ok(InetNet {
                    bytes: *addr,
                    family_bytes: *family,
                    prefix_bits: *bits,
                })
            }
            Value::Text(s) => parse_inet_text(s).ok_or_else(|| EvalError::TypeMismatch {
                detail: format!("invalid inet text: {s:?}"),
            }),
            _ => Err(EvalError::TypeMismatch {
                detail: format!(
                    "inet operator requires INET/CIDR/TEXT operands, got {}",
                    crate::conversions::pg_type_name_for_error_opt(v.data_type())
                ),
            }),
        }
    };
    let a = to_inet(l)?;
    let b = to_inet(r)?;
    let result = match op {
        BinOp::InetContainedByEq => inet_contained_eq(&a, &b),
        BinOp::InetContainedBy => inet_contained_eq(&a, &b) && !inet_networks_equal(&a, &b),
        BinOp::InetContainsEq => inet_contained_eq(&b, &a),
        BinOp::InetContains => inet_contained_eq(&b, &a) && !inet_networks_equal(&a, &b),
        BinOp::InetOverlap => inet_contained_eq(&a, &b) || inet_contained_eq(&b, &a),
        _ => unreachable!("inet_op_bool_result called with non-inet op"),
    };
    Ok(Value::Bool(result))
}

/// v7.37.17 (17.6 siblings) — MySQL INET_ATON(text) → the IPv4
/// address as a BigInt. Invalid input → NULL (MySQL semantics, not
/// an error).
pub(super) fn mysql_inet_aton(args: &[Value<'_>]) -> Result<Value<'static>, EvalError> {
    let s = match args {
        [Value::Null] => return Ok(Value::Null),
        [Value::Text(s)] => s,
        _ => {
            return Err(EvalError::TypeMismatch {
                detail: alloc::format!("inet_aton() takes one TEXT arg, got {} args", args.len()),
            });
        }
    };
    match parse_ipv4(s) {
        Some(b) => Ok(Value::BigInt(
            (i64::from(b[0]) << 24)
                | (i64::from(b[1]) << 16)
                | (i64::from(b[2]) << 8)
                | i64::from(b[3]),
        )),
        None => Ok(Value::Null),
    }
}

/// v7.37.17 (17.6 siblings) — MySQL INET_NTOA(n) → dotted-quad text.
/// Out-of-range input → NULL (MySQL semantics).
pub(super) fn mysql_inet_ntoa(args: &[Value<'_>]) -> Result<Value<'static>, EvalError> {
    let n = match args {
        [Value::Null] => return Ok(Value::Null),
        [Value::Int(n)] => i64::from(*n),
        [Value::SmallInt(n)] => i64::from(*n),
        [Value::BigInt(n)] => *n,
        _ => {
            return Err(EvalError::TypeMismatch {
                detail: alloc::format!(
                    "inet_ntoa() takes one integer arg, got {}",
                    args.first().map_or_else(
                        || alloc::string::String::from("no arguments"),
                        |a| crate::conversions::pg_type_name_for_error_opt(a.data_type()),
                    )
                ),
            });
        }
    };
    if !(0..=0xFFFF_FFFF).contains(&n) {
        return Ok(Value::Null);
    }
    let n = n as u32;
    Ok(Value::text(alloc::format!(
        "{}.{}.{}.{}",
        (n >> 24) & 0xFF,
        (n >> 16) & 0xFF,
        (n >> 8) & 0xFF,
        n & 0xFF
    )))
}

/// RFC 5952 text rendering for a 16-byte IPv6 address: lowercase
/// hex, longest zero run (length ≥ 2) compressed to `::`.
fn format_ipv6(bytes: &[u8; 16]) -> alloc::string::String {
    let mut groups = [0u16; 8];
    for (i, g) in groups.iter_mut().enumerate() {
        *g = (u16::from(bytes[i * 2]) << 8) | u16::from(bytes[i * 2 + 1]);
    }
    // Longest run of zero groups (first among ties), min length 2.
    let (mut best_start, mut best_len) = (usize::MAX, 0usize);
    let mut i = 0;
    while i < 8 {
        if groups[i] == 0 {
            let start = i;
            while i < 8 && groups[i] == 0 {
                i += 1;
            }
            let len = i - start;
            if len > best_len {
                best_start = start;
                best_len = len;
            }
        } else {
            i += 1;
        }
    }
    let mut out = alloc::string::String::new();
    if best_len >= 2 {
        for (idx, g) in groups.iter().enumerate().take(best_start) {
            if idx > 0 {
                out.push(':');
            }
            out.push_str(&alloc::format!("{g:x}"));
        }
        out.push_str("::");
        for (idx, g) in groups.iter().enumerate().skip(best_start + best_len) {
            if idx > best_start + best_len {
                out.push(':');
            }
            out.push_str(&alloc::format!("{g:x}"));
        }
    } else {
        for (idx, g) in groups.iter().enumerate() {
            if idx > 0 {
                out.push(':');
            }
            out.push_str(&alloc::format!("{g:x}"));
        }
    }
    out
}

/// v7.37.17 (17.6 siblings) — MySQL INET6_ATON(text) → the address
/// as VARBINARY: 16 bytes for IPv6, 4 bytes for IPv4 (MySQL keeps
/// the shorter form for v4 input). Invalid → NULL.
pub(super) fn mysql_inet6_aton(args: &[Value<'_>]) -> Result<Value<'static>, EvalError> {
    let s = match args {
        [Value::Null] => return Ok(Value::Null),
        [Value::Text(s)] => s,
        _ => {
            return Err(EvalError::TypeMismatch {
                detail: alloc::format!("inet6_aton() takes one TEXT arg, got {} args", args.len()),
            });
        }
    };
    if let Some(b) = parse_ipv4(s) {
        return Ok(Value::Bytes(alloc::borrow::Cow::Owned(b.to_vec())));
    }
    match parse_ipv6(s) {
        Some(b) => Ok(Value::Bytes(alloc::borrow::Cow::Owned(b.to_vec()))),
        None => Ok(Value::Null),
    }
}

/// v7.37.17 (17.6 siblings) — MySQL INET6_NTOA(varbinary) → text.
/// 4-byte input renders dotted-quad; 16-byte renders RFC 5952
/// compressed IPv6. Anything else → NULL.
pub(super) fn mysql_inet6_ntoa(args: &[Value<'_>]) -> Result<Value<'static>, EvalError> {
    let bytes = match args {
        [Value::Null] => return Ok(Value::Null),
        [Value::Bytes(b)] => b.as_ref(),
        _ => {
            return Err(EvalError::TypeMismatch {
                detail: alloc::format!(
                    "inet6_ntoa() takes one binary arg, got {}",
                    args.first().map_or_else(
                        || alloc::string::String::from("no arguments"),
                        |a| crate::conversions::pg_type_name_for_error_opt(a.data_type()),
                    )
                ),
            });
        }
    };
    match bytes.len() {
        4 => Ok(Value::text(alloc::format!(
            "{}.{}.{}.{}",
            bytes[0],
            bytes[1],
            bytes[2],
            bytes[3]
        ))),
        16 => {
            let mut b = [0u8; 16];
            b.copy_from_slice(bytes);
            Ok(Value::text(format_ipv6(&b)))
        }
        _ => Ok(Value::Null),
    }
}

/// v7.37.17 (17.6 siblings) — MySQL IS_IPV4(text) / IS_IPV6(text).
pub(super) fn mysql_is_ipv4(args: &[Value<'_>]) -> Result<Value<'static>, EvalError> {
    match args {
        [Value::Null] => Ok(Value::Null),
        [Value::Text(s)] => Ok(Value::Bool(parse_ipv4(s).is_some())),
        _ => Err(EvalError::TypeMismatch {
            detail: alloc::format!("is_ipv4() takes one TEXT arg, got {} args", args.len()),
        }),
    }
}

pub(super) fn mysql_is_ipv6(args: &[Value<'_>]) -> Result<Value<'static>, EvalError> {
    match args {
        [Value::Null] => Ok(Value::Null),
        [Value::Text(s)] => Ok(Value::Bool(!s.is_empty() && parse_ipv6(s).is_some())),
        _ => Err(EvalError::TypeMismatch {
            detail: alloc::format!("is_ipv6() takes one TEXT arg, got {} args", args.len()),
        }),
    }
}