skardi 0.6.0

High performance query engine for both offline compute and online serving
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
//! Graph values → Arrow, type-directed by the caller-declared columns.
//!
//! Two halves, both backend-shared:
//!
//! - **agtype text → JSON.** AGE serializes results as agtype — JSON with
//!   `::vertex` / `::edge` / `::path` (and `::numeric`) annotations
//!   appended after the value they classify. [`parse_agtype`] strips the
//!   annotations *outside string literals* and parses the remainder as
//!   JSON. The annotations are not needed afterwards: conversion is
//!   type-directed by the DECLARED column type (design §Schema handling —
//!   there is no schema inference anywhere), so a `node` column expects
//!   the vertex object shape and fails with a typed error otherwise.
//! - **JSON → Arrow.** [`build_batch`] converts one buffered batch of rows
//!   against the declared columns. Ad-hoc declared columns are always
//!   nullable (Cypher can produce null in any position); YAML views may
//!   declare `nullable: false` as an author's assertion, and a null met
//!   under it is [`GraphError::NotNullViolation`]. A non-null value of the
//!   wrong JSON kind is [`GraphError::TypeMismatch`] carrying kinds, never
//!   values.
//!
//! The canonical STRUCT shapes (design §Result flattening):
//! - node: `STRUCT<id Utf8, labels List<Utf8>, properties Utf8>` —
//!   properties are JSON text so the schema is independent of which keys
//!   any row happens to carry.
//! - relationship: `STRUCT<id Utf8, start_id Utf8, end_id Utf8, type Utf8,
//!   properties Utf8>`.
//! - path: `STRUCT<nodes List<node>, relationships List<relationship>>` —
//!   two parallel typed lists (an Arrow list has ONE element type and a
//!   path alternates two shapes); relationship *i* connects node *i* to
//!   node *i+1*.

use std::sync::Arc;

use arrow::array::{
    ArrayRef, BooleanBuilder, Float64Builder, Int64Builder, ListArray, ListBuilder, StringBuilder,
    StructArray,
};
use arrow::buffer::{NullBuffer, OffsetBuffer};
use arrow::datatypes::{DataType, Field, Fields, Schema};
use arrow::record_batch::RecordBatch;
use serde_json::Value;

use super::error::{GraphError, json_kind};

/// The declared-type vocabulary — the repo's friendly lowercase names
/// (dynamodb/mongo/seekdb precedent), never Arrow PascalCase. One
/// spelling, shared verbatim by the future YAML views' `type:` fields.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GraphType {
    /// `string` (aliases `str`, `utf8`) → Utf8.
    String,
    /// `int` (aliases `integer`, `bigint`) → Int64.
    Int,
    /// `float` (alias `double`) → Float64.
    Float,
    /// `bool` (alias `boolean`) → Boolean.
    Bool,
    /// `json` → Utf8 carrying the value as JSON text verbatim.
    Json,
    /// `node` → the canonical vertex STRUCT.
    Node,
    /// `relationship` → the canonical edge STRUCT.
    Relationship,
    /// `path` → parallel node/relationship lists.
    Path,
}

/// The accepted set, for error messages — kept in one place so the
/// diagnostic can never drift from the parser.
pub const ACCEPTED_TYPES: &str = "string|str|utf8, int|integer|bigint, float|double, bool|boolean, json, \
     node, relationship, path";

impl GraphType {
    /// Parse one friendly type name.
    pub fn parse(name: &str) -> Option<Self> {
        match name {
            "string" | "str" | "utf8" => Some(Self::String),
            "int" | "integer" | "bigint" => Some(Self::Int),
            "float" | "double" => Some(Self::Float),
            "bool" | "boolean" => Some(Self::Bool),
            "json" => Some(Self::Json),
            "node" => Some(Self::Node),
            "relationship" => Some(Self::Relationship),
            "path" => Some(Self::Path),
            _ => None,
        }
    }

    /// The canonical (post-alias) name, for diagnostics.
    pub fn canonical(&self) -> &'static str {
        match self {
            Self::String => "string",
            Self::Int => "int",
            Self::Float => "float",
            Self::Bool => "bool",
            Self::Json => "json",
            Self::Node => "node",
            Self::Relationship => "relationship",
            Self::Path => "path",
        }
    }

    /// The Arrow type this declared type plans as.
    pub fn arrow_type(&self) -> DataType {
        match self {
            Self::String | Self::Json => DataType::Utf8,
            Self::Int => DataType::Int64,
            Self::Float => DataType::Float64,
            Self::Bool => DataType::Boolean,
            Self::Node => DataType::Struct(node_fields()),
            Self::Relationship => DataType::Struct(relationship_fields()),
            Self::Path => DataType::Struct(path_fields()),
        }
    }
}

/// One declared output column.
#[derive(Debug, Clone)]
pub struct DeclaredColumn {
    pub name: String,
    pub ty: GraphType,
    /// Ad-hoc `cypher_query` columns are always nullable (Cypher can
    /// produce null in any position and the ad-hoc JSON object cannot
    /// declare otherwise, design §Schema handling); YAML views may
    /// declare `false` as an author's assertion, enforced by
    /// [`build_batch`] as [`GraphError::NotNullViolation`].
    pub nullable: bool,
}

/// The vertex STRUCT's fields — one definition feeds the planned type AND
/// the builder so they cannot drift.
pub fn node_fields() -> Fields {
    Fields::from(vec![
        Field::new("id", DataType::Utf8, true),
        Field::new(
            "labels",
            DataType::List(Arc::new(Field::new("item", DataType::Utf8, true))),
            true,
        ),
        Field::new("properties", DataType::Utf8, true),
    ])
}

/// The edge STRUCT's fields.
pub fn relationship_fields() -> Fields {
    Fields::from(vec![
        Field::new("id", DataType::Utf8, true),
        Field::new("start_id", DataType::Utf8, true),
        Field::new("end_id", DataType::Utf8, true),
        Field::new("type", DataType::Utf8, true),
        Field::new("properties", DataType::Utf8, true),
    ])
}

/// The path STRUCT's fields: parallel typed lists.
pub fn path_fields() -> Fields {
    Fields::from(vec![
        Field::new(
            "nodes",
            DataType::List(Arc::new(Field::new(
                "item",
                DataType::Struct(node_fields()),
                true,
            ))),
            true,
        ),
        Field::new(
            "relationships",
            DataType::List(Arc::new(Field::new(
                "item",
                DataType::Struct(relationship_fields()),
                true,
            ))),
            true,
        ),
    ])
}

/// The planned schema for a set of declared columns. Each field carries
/// its declaration's nullable bit — `true` everywhere on the ad-hoc
/// surface, where nulls cannot be declared away (design §Schema
/// handling); YAML views may declare `nullable: false`.
pub fn declared_schema(columns: &[DeclaredColumn]) -> Arc<Schema> {
    Arc::new(Schema::new(
        columns
            .iter()
            .map(|c| Field::new(&c.name, c.ty.arrow_type(), c.nullable))
            .collect::<Vec<_>>(),
    ))
}

/// Strip agtype's `::identifier` annotations outside string literals and
/// parse the remainder as JSON.
///
/// The scanner tracks JSON string state (including escapes), so a literal
/// like `"a::vertex"` survives untouched; only structural annotations —
/// which always follow a value (`}`, `]`, digit, or scalar keyword) — are
/// removed.
pub fn parse_agtype(text: &str) -> Result<Value, String> {
    // BYTES in, BYTES out, one UTF-8 decode at the end. `u8 as char` is
    // Latin-1 semantics and would shred every multi-byte UTF-8 character
    // into mojibake that still parses as JSON — a SILENT corruption
    // (CJK entity names are the common case in knowledge graphs, not an
    // edge case). Byte-level scanning is sound because everything the
    // scanner matches ('"', '\\', ':', the annotation identifiers) is
    // ASCII, and UTF-8 continuation bytes are all >= 0x80 — a multi-byte
    // character can never alias a structural byte.
    let mut cleaned: Vec<u8> = Vec::with_capacity(text.len());
    let bytes = text.as_bytes();
    let mut i = 0;
    let mut in_string = false;
    let mut escaped = false;
    while i < bytes.len() {
        let b = bytes[i];
        if in_string {
            cleaned.push(b);
            if escaped {
                escaped = false;
            } else if b == b'\\' {
                escaped = true;
            } else if b == b'"' {
                in_string = false;
            }
            i += 1;
            continue;
        }
        match b {
            b'"' => {
                in_string = true;
                cleaned.push(b'"');
                i += 1;
            }
            b':' if i + 1 < bytes.len() && bytes[i + 1] == b':' => {
                // `::identifier` — skip the annotation.
                i += 2;
                while i < bytes.len() && (bytes[i].is_ascii_alphanumeric() || bytes[i] == b'_') {
                    i += 1;
                }
            }
            b if b.is_ascii_alphabetic() => {
                // agtype's float specials are bare non-JSON tokens —
                // VERIFIED live: `RETURN 1.0e308 * 10` emits `Infinity`
                // through agtype_out (and `-Infinity`/`NaN` are its
                // siblings). JSON has no spelling for them, so they
                // decode to null — the proportionate outcome for a
                // declared float (AGE's own sqrt(-1.0) already answers
                // SQL NULL), instead of a whole-scan MalformedCell for a
                // legitimate value. Other bare words (true/false/null)
                // pass through untouched; strings are untouched by
                // construction (this arm is outside-string only).
                let start = i;
                while i < bytes.len() && bytes[i].is_ascii_alphabetic() {
                    i += 1;
                }
                let word = &text[start..i];
                if word == "NaN" || word == "Infinity" {
                    // A leading sign belongs to the token (`-Infinity`),
                    // not to the null replacing it.
                    if cleaned.last() == Some(&b'-') {
                        cleaned.pop();
                    }
                    cleaned.extend_from_slice(b"null");
                } else {
                    cleaned.extend_from_slice(word.as_bytes());
                }
            }
            _ => {
                cleaned.push(b);
                i += 1;
            }
        }
    }
    let cleaned = String::from_utf8(cleaned)
        .map_err(|e| format!("agtype text is not valid UTF-8 after annotation strip: {e}"))?;
    serde_json::from_str(&cleaned).map_err(|e| e.to_string())
}

/// Convert one buffered batch of rows (each row = one JSON value per
/// declared column) into a RecordBatch. Batch-atomic: any type mismatch
/// fails the WHOLE batch before anything is emitted (design §Schema
/// handling). `row_base` is the absolute index of the batch's first row,
/// so errors name the result row, not the batch-relative one.
pub fn build_batch(
    columns: &[DeclaredColumn],
    rows: &[Vec<Value>],
    row_base: usize,
) -> Result<RecordBatch, GraphError> {
    // Arity first: a backend row shorter than the declared schema must
    // be a typed error, not an index panic (future Neo4j/Kuzu drivers
    // make this reachable even if the AGE client can't produce it).
    for (i, row) in rows.iter().enumerate() {
        if row.len() != columns.len() {
            return Err(GraphError::RowArityMismatch {
                row: row_base + i,
                expected: columns.len(),
                found: row.len(),
            });
        }
    }
    let mut arrays: Vec<ArrayRef> = Vec::with_capacity(columns.len());
    for (col_idx, col) in columns.iter().enumerate() {
        // A declared `nullable: false` is the author's assertion about
        // the view's Cypher — enforce it BEFORE conversion so the null
        // surfaces as its own typed error, not as a struct field that
        // happens to be empty.
        if !col.nullable {
            for (i, row) in rows.iter().enumerate() {
                if matches!(row[col_idx], Value::Null) {
                    return Err(GraphError::NotNullViolation {
                        column: col.name.clone(),
                        row: row_base + i,
                    });
                }
            }
        }
        let cells = rows.iter().map(|r| &r[col_idx]);
        let array: ArrayRef = match col.ty {
            GraphType::String => {
                let mut b = StringBuilder::new();
                for (i, v) in cells.enumerate() {
                    match v {
                        Value::Null => b.append_null(),
                        Value::String(s) => b.append_value(s),
                        other => return Err(mismatch(col, row_base + i, "string", other)),
                    }
                }
                Arc::new(b.finish())
            }
            GraphType::Int => {
                let mut b = Int64Builder::new();
                for (i, v) in cells.enumerate() {
                    match v {
                        Value::Null => b.append_null(),
                        Value::Number(n) => match n.as_i64() {
                            Some(i) => b.append_value(i),
                            None => return Err(mismatch(col, row_base + i, "int", v)),
                        },
                        other => return Err(mismatch(col, row_base + i, "int", other)),
                    }
                }
                Arc::new(b.finish())
            }
            GraphType::Float => {
                let mut b = Float64Builder::new();
                for (i, v) in cells.enumerate() {
                    match v {
                        Value::Null => b.append_null(),
                        // Integers widen losslessly enough for a declared
                        // float; the reverse (float → int) never does.
                        Value::Number(n) => match n.as_f64() {
                            Some(f) => b.append_value(f),
                            None => return Err(mismatch(col, row_base + i, "float", v)),
                        },
                        other => return Err(mismatch(col, row_base + i, "float", other)),
                    }
                }
                Arc::new(b.finish())
            }
            GraphType::Bool => {
                let mut b = BooleanBuilder::new();
                for (i, v) in cells.enumerate() {
                    match v {
                        Value::Null => b.append_null(),
                        Value::Bool(x) => b.append_value(*x),
                        other => return Err(mismatch(col, row_base + i, "bool", other)),
                    }
                }
                Arc::new(b.finish())
            }
            GraphType::Json => {
                // Verbatim: any JSON kind is legal, null stays SQL NULL.
                let mut b = StringBuilder::new();
                for v in cells {
                    match v {
                        Value::Null => b.append_null(),
                        other => b.append_value(other.to_string()),
                    }
                }
                Arc::new(b.finish())
            }
            GraphType::Node => {
                let mut parts = Vec::with_capacity(rows.len());
                for (i, v) in cells.enumerate() {
                    parts.push(node_parts(col, row_base + i, v)?);
                }
                Arc::new(node_struct_array(&parts))
            }
            GraphType::Relationship => {
                let mut parts = Vec::with_capacity(rows.len());
                for (i, v) in cells.enumerate() {
                    parts.push(relationship_parts(col, row_base + i, v)?);
                }
                Arc::new(relationship_struct_array(&parts))
            }
            GraphType::Path => {
                let mut parts = Vec::with_capacity(rows.len());
                for (i, v) in cells.enumerate() {
                    parts.push(path_parts(col, row_base + i, v)?);
                }
                Arc::new(path_struct_array(&parts))
            }
        };
        arrays.push(array);
    }
    RecordBatch::try_new(declared_schema(columns), arrays).map_err(|e| {
        // Unreachable by construction (builders match the schema); kept as
        // a typed error rather than a panic.
        GraphError::backend("<conversion>", "arrow", &e.to_string())
    })
}

fn mismatch(col: &DeclaredColumn, row: usize, expected: &'static str, v: &Value) -> GraphError {
    GraphError::TypeMismatch {
        column: col.name.clone(),
        row,
        expected,
        found: json_kind(v),
    }
}

/// One vertex, decomposed: (id, label, properties-json). AGE vertex
/// objects carry `id` (number), `label` (string), `properties` (object).
/// Ids are stringified: opaque tokens, stable for the entity's life
/// within one database (design §Backend abstraction).
struct NodeParts {
    id: String,
    label: String,
    properties: String,
}

/// One edge, decomposed.
struct RelParts {
    id: String,
    start_id: String,
    end_id: String,
    rel_type: String,
    properties: String,
}

fn node_parts(
    col: &DeclaredColumn,
    row: usize,
    v: &Value,
) -> Result<Option<NodeParts>, GraphError> {
    match v {
        Value::Null => Ok(None),
        Value::Object(map) => {
            // Endpoint keys are the structural tell of an EDGE: an edge
            // object ({id, label, start_id, end_id, properties}) would
            // otherwise satisfy the node shape completely — id and label
            // present — and a `RETURN r` declared as `node` would decode
            // silently, dropping start_id/end_id on the floor. The
            // reverse direction already fails naturally (a vertex lacks
            // the endpoint keys a relationship column requires); this
            // check closes the hole from the other side, turning a
            // silent wrong answer into the typed error the design
            // promises. Sound because both keys are structural fields,
            // never property names: properties live under their own
            // `properties` sub-object.
            if map.contains_key("start_id") || map.contains_key("end_id") {
                return Err(mismatch(col, row, "node", v));
            }
            let (Some(id), Some(label)) = (map.get("id"), map.get("label").and_then(Value::as_str))
            else {
                return Err(mismatch(col, row, "node", v));
            };
            Ok(Some(NodeParts {
                id: scalar_to_string(id),
                label: label.to_string(),
                properties: map
                    .get("properties")
                    .cloned()
                    .unwrap_or(Value::Null)
                    .to_string(),
            }))
        }
        other => Err(mismatch(col, row, "node", other)),
    }
}

fn relationship_parts(
    col: &DeclaredColumn,
    row: usize,
    v: &Value,
) -> Result<Option<RelParts>, GraphError> {
    match v {
        Value::Null => Ok(None),
        Value::Object(map) => {
            let (Some(id), Some(start), Some(end), Some(label)) = (
                map.get("id"),
                map.get("start_id"),
                map.get("end_id"),
                map.get("label").and_then(Value::as_str),
            ) else {
                return Err(mismatch(col, row, "relationship", v));
            };
            Ok(Some(RelParts {
                id: scalar_to_string(id),
                start_id: scalar_to_string(start),
                end_id: scalar_to_string(end),
                rel_type: label.to_string(),
                properties: map
                    .get("properties")
                    .cloned()
                    .unwrap_or(Value::Null)
                    .to_string(),
            }))
        }
        other => Err(mismatch(col, row, "relationship", other)),
    }
}

/// An AGE path is a JSON array alternating vertex/edge objects
/// (`[v, e, v, …]` — nodes at the even positions, odd total length).
/// A path, decomposed into its two parallel element sequences.
type PathParts = (Vec<NodeParts>, Vec<RelParts>);

fn path_parts(
    col: &DeclaredColumn,
    row: usize,
    v: &Value,
) -> Result<Option<PathParts>, GraphError> {
    match v {
        Value::Null => Ok(None),
        Value::Array(elements) => {
            // A path is ODD-length by construction: n nodes alternate
            // with n-1 edges, and the minimum is one node (the design's
            // zero-hop path). Even lengths AND the empty array are both
            // malformed — an empty array must not be silently accepted
            // as a "0-node path".
            if elements.len().is_multiple_of(2) {
                return Err(mismatch(col, row, "path", v));
            }
            let mut nodes = Vec::with_capacity(elements.len() / 2 + 1);
            let mut rels = Vec::with_capacity(elements.len() / 2);
            for (i, el) in elements.iter().enumerate() {
                if i.is_multiple_of(2) {
                    match node_parts(col, row, el)? {
                        Some(n) => nodes.push(n),
                        None => return Err(mismatch(col, row, "path", v)),
                    }
                } else {
                    match relationship_parts(col, row, el)? {
                        Some(r) => rels.push(r),
                        None => return Err(mismatch(col, row, "path", v)),
                    }
                }
            }
            Ok(Some((nodes, rels)))
        }
        other => Err(mismatch(col, row, "path", other)),
    }
}

/// Assemble the canonical vertex STRUCT column: explicit child arrays +
/// a validity buffer (arrow's `StructBuilder` erases List child builders,
/// so explicit assembly is both clearer and the only reliable spelling).
fn node_struct_array(items: &[Option<NodeParts>]) -> StructArray {
    let mut ids = StringBuilder::new();
    let mut labels = ListBuilder::new(StringBuilder::new());
    let mut props = StringBuilder::new();
    let mut validity = Vec::with_capacity(items.len());
    for item in items {
        match item {
            Some(n) => {
                ids.append_value(&n.id);
                labels.values().append_value(&n.label);
                labels.append(true);
                props.append_value(&n.properties);
                validity.push(true);
            }
            None => {
                ids.append_null();
                labels.append_null();
                props.append_null();
                validity.push(false);
            }
        }
    }
    StructArray::new(
        node_fields(),
        vec![
            Arc::new(ids.finish()),
            Arc::new(labels.finish()),
            Arc::new(props.finish()),
        ],
        Some(NullBuffer::from(validity)),
    )
}

/// Assemble the canonical edge STRUCT column.
fn relationship_struct_array(items: &[Option<RelParts>]) -> StructArray {
    let mut ids = StringBuilder::new();
    let mut starts = StringBuilder::new();
    let mut ends = StringBuilder::new();
    let mut types = StringBuilder::new();
    let mut props = StringBuilder::new();
    let mut validity = Vec::with_capacity(items.len());
    for item in items {
        match item {
            Some(r) => {
                ids.append_value(&r.id);
                starts.append_value(&r.start_id);
                ends.append_value(&r.end_id);
                types.append_value(&r.rel_type);
                props.append_value(&r.properties);
                validity.push(true);
            }
            None => {
                ids.append_null();
                starts.append_null();
                ends.append_null();
                types.append_null();
                props.append_null();
                validity.push(false);
            }
        }
    }
    StructArray::new(
        relationship_fields(),
        vec![
            Arc::new(ids.finish()),
            Arc::new(starts.finish()),
            Arc::new(ends.finish()),
            Arc::new(types.finish()),
            Arc::new(props.finish()),
        ],
        Some(NullBuffer::from(validity)),
    )
}

/// Assemble the path STRUCT column: two parallel typed lists over
/// flattened node/edge values, offsets per row.
fn path_struct_array(items: &[Option<PathParts>]) -> StructArray {
    let mut flat_nodes: Vec<Option<NodeParts>> = Vec::new();
    let mut flat_rels: Vec<Option<RelParts>> = Vec::new();
    let mut node_lengths = Vec::with_capacity(items.len());
    let mut rel_lengths = Vec::with_capacity(items.len());
    let mut validity = Vec::with_capacity(items.len());
    for item in items {
        match item {
            Some((nodes, rels)) => {
                node_lengths.push(nodes.len());
                rel_lengths.push(rels.len());
                validity.push(true);
                flat_nodes.extend(nodes.iter().map(|n| {
                    Some(NodeParts {
                        id: n.id.clone(),
                        label: n.label.clone(),
                        properties: n.properties.clone(),
                    })
                }));
                flat_rels.extend(rels.iter().map(|r| {
                    Some(RelParts {
                        id: r.id.clone(),
                        start_id: r.start_id.clone(),
                        end_id: r.end_id.clone(),
                        rel_type: r.rel_type.clone(),
                        properties: r.properties.clone(),
                    })
                }));
            }
            None => {
                node_lengths.push(0);
                rel_lengths.push(0);
                validity.push(false);
            }
        }
    }
    let nodes_list = ListArray::new(
        Arc::new(Field::new("item", DataType::Struct(node_fields()), true)),
        OffsetBuffer::from_lengths(node_lengths),
        Arc::new(node_struct_array(&flat_nodes)),
        Some(NullBuffer::from(validity.clone())),
    );
    let rels_list = ListArray::new(
        Arc::new(Field::new(
            "item",
            DataType::Struct(relationship_fields()),
            true,
        )),
        OffsetBuffer::from_lengths(rel_lengths),
        Arc::new(relationship_struct_array(&flat_rels)),
        Some(NullBuffer::from(validity.clone())),
    );
    // The parent STRUCT carries the SAME validity as its child lists —
    // with None here, a NULL path row would read as a valid struct whose
    // lists happen to be null, and `path IS NULL` would answer false.
    StructArray::new(
        path_fields(),
        vec![Arc::new(nodes_list), Arc::new(rels_list)],
        Some(NullBuffer::from(validity)),
    )
}

/// Ids arrive as agtype integers; stringify without float detours.
fn scalar_to_string(v: &Value) -> String {
    match v {
        Value::String(s) => s.clone(),
        other => other.to_string(),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use arrow::array::{Array, StringArray, StructArray};

    #[test]
    fn agtype_annotations_strip_outside_strings_only() {
        let text = r#"{"id": 1, "label": "A", "properties": {"note": "x::vertex"}}::vertex"#;
        let v = parse_agtype(text).expect("parses");
        assert_eq!(v["properties"]["note"], "x::vertex", "string survives");
        assert_eq!(v["id"], 1);

        // Numeric annotation mid-array.
        let v = parse_agtype(r#"[1.5::numeric, "a", {"x": 2}::vertex]"#).expect("parses");
        assert_eq!(v[0], 1.5);
        assert_eq!(v[2]["x"], 2);
    }

    #[test]
    fn non_ascii_string_values_survive_byte_faithfully() {
        // Latin-1 `u8 as char` shredding would turn 颱風 into mojibake
        // that STILL parses as JSON — this pins the byte-faithful path
        // (CJK, emoji, and a combining accent, inside and outside
        // annotated objects).
        let text = r#"{"id": 1, "label": "城市", "properties": {"name": "颱風", "note": "café ☔"}}::vertex"#;
        let v = parse_agtype(text).expect("parses");
        assert_eq!(v["label"], "城市");
        assert_eq!(v["properties"]["name"], "颱風");
        assert_eq!(v["properties"]["note"], "café ☔");

        let v = parse_agtype(r#"["中文", 1.5::numeric, "🎈"]"#).expect("parses");
        assert_eq!(v[0], "中文");
        assert_eq!(v[2], "🎈");
    }

    #[test]
    fn agtype_float_specials_decode_to_null_outside_strings_only() {
        // Verified live: AGE emits bare `Infinity` for float overflow
        // (1.0e308 * 10). JSON has no spelling for the specials, so they
        // decode to null — proportionate for a declared float — while
        // string CONTENT is untouched and ordinary words pass through.
        let v = parse_agtype(r#"[Infinity, -Infinity, NaN, 1.5, true]"#).expect("parses");
        assert_eq!(v[0], Value::Null);
        assert_eq!(v[1], Value::Null, "the sign is consumed with the token");
        assert_eq!(v[2], Value::Null);
        assert_eq!(v[3], 1.5);
        assert_eq!(v[4], true);

        let v =
            parse_agtype(r#"{"note": "to Infinity and beyond", "x": Infinity}"#).expect("parses");
        assert_eq!(v["note"], "to Infinity and beyond", "strings untouched");
        assert_eq!(v["x"], Value::Null);
    }

    #[test]
    fn declared_types_parse_with_aliases_and_reject_pascal_case() {
        assert_eq!(GraphType::parse("utf8"), Some(GraphType::String));
        assert_eq!(GraphType::parse("bigint"), Some(GraphType::Int));
        assert_eq!(GraphType::parse("double"), Some(GraphType::Float));
        assert_eq!(GraphType::parse("path"), Some(GraphType::Path));
        // Arrow PascalCase is NOT the vocabulary (design: the repo's
        // friendly lowercase names, one spelling everywhere).
        assert_eq!(GraphType::parse("Utf8"), None);
        assert_eq!(GraphType::parse("Int64"), None);
    }

    fn col(name: &str, ty: GraphType) -> DeclaredColumn {
        DeclaredColumn {
            name: name.to_string(),
            ty,
            nullable: true,
        }
    }

    #[test]
    fn scalars_convert_and_nulls_pass_through() {
        let columns = vec![
            col("s", GraphType::String),
            col("i", GraphType::Int),
            col("f", GraphType::Float),
            col("b", GraphType::Bool),
            col("j", GraphType::Json),
        ];
        let rows = vec![
            vec![
                serde_json::json!("hi"),
                serde_json::json!(7),
                serde_json::json!(1.5),
                serde_json::json!(true),
                serde_json::json!({"k": [1, 2]}),
            ],
            vec![
                Value::Null,
                Value::Null,
                serde_json::json!(2), // int widens into a declared float
                Value::Null,
                Value::Null,
            ],
        ];
        let batch = build_batch(&columns, &rows, 0).expect("converts");
        assert_eq!(batch.num_rows(), 2);
        let s = batch
            .column(0)
            .as_any()
            .downcast_ref::<StringArray>()
            .unwrap();
        assert_eq!(s.value(0), "hi");
        assert!(s.is_null(1));
        let j = batch
            .column(4)
            .as_any()
            .downcast_ref::<StringArray>()
            .unwrap();
        assert_eq!(j.value(0), r#"{"k":[1,2]}"#);
    }

    #[test]
    fn type_mismatch_names_column_row_and_kinds_never_values() {
        let columns = vec![col("n", GraphType::Int)];
        let rows = vec![
            vec![serde_json::json!(1)],
            vec![serde_json::json!("secret-value")],
        ];
        let err = build_batch(&columns, &rows, 10).unwrap_err();
        let msg = err.to_string();
        assert!(msg.contains("'n'"), "{msg}");
        assert!(msg.contains("row 11"), "absolute row index: {msg}");
        assert!(msg.contains("a string"), "{msg}");
        assert!(!msg.contains("secret-value"), "values never leak: {msg}");
    }

    #[test]
    fn a_float_does_not_pass_as_a_declared_int() {
        let columns = vec![col("n", GraphType::Int)];
        let rows = vec![vec![serde_json::json!(1.5)]];
        let err = build_batch(&columns, &rows, 0).unwrap_err();
        assert!(err.to_string().contains("declared 'int'"), "{err}");
    }

    #[test]
    fn node_converts_to_the_canonical_struct() {
        let columns = vec![col("v", GraphType::Node)];
        let vertex = parse_agtype(
            r#"{"id": 844424930131969, "label": "Person", "properties": {"name": "redacted"}}::vertex"#,
        )
        .unwrap();
        let rows = vec![vec![vertex], vec![Value::Null]];
        let batch = build_batch(&columns, &rows, 0).expect("converts");
        let s = batch
            .column(0)
            .as_any()
            .downcast_ref::<StructArray>()
            .unwrap();
        let ids = s
            .column_by_name("id")
            .unwrap()
            .as_any()
            .downcast_ref::<StringArray>()
            .unwrap();
        assert_eq!(ids.value(0), "844424930131969", "id is stringified");
        assert!(s.is_null(1), "null node row is a null struct");
        let props = s
            .column_by_name("properties")
            .unwrap()
            .as_any()
            .downcast_ref::<StringArray>()
            .unwrap();
        assert_eq!(props.value(0), r#"{"name":"redacted"}"#);
    }

    #[test]
    fn path_splits_into_parallel_typed_lists() {
        let columns = vec![col("p", GraphType::Path)];
        let path = parse_agtype(
            r#"[{"id": 1, "label": "A", "properties": {}}::vertex, \
                {"id": 9, "label": "KNOWS", "start_id": 1, "end_id": 2, "properties": {}}::edge, \
                {"id": 2, "label": "B", "properties": {}}::vertex]::path"#
                .replace("\\\n", "")
                .as_str(),
        )
        .unwrap();
        let rows = vec![vec![path]];
        let batch = build_batch(&columns, &rows, 0).expect("converts");
        let s = batch
            .column(0)
            .as_any()
            .downcast_ref::<StructArray>()
            .unwrap();
        let nodes = s.column_by_name("nodes").unwrap();
        let rels = s.column_by_name("relationships").unwrap();
        let nodes = nodes
            .as_any()
            .downcast_ref::<arrow::array::ListArray>()
            .unwrap();
        let rels = rels
            .as_any()
            .downcast_ref::<arrow::array::ListArray>()
            .unwrap();
        assert_eq!(nodes.value(0).len(), 2, "nodes is one longer");
        assert_eq!(rels.value(0).len(), 1);
    }

    #[test]
    fn relationships_convert_to_the_canonical_struct_with_null_rows() {
        let columns = vec![col("r", GraphType::Relationship)];
        let edge = parse_agtype(
            r#"{"id": 9, "label": "KNOWS", "start_id": 1, "end_id": 2, "properties": {"since": 2019}}::edge"#,
        )
        .unwrap();
        let rows = vec![vec![edge], vec![Value::Null]];
        let batch = build_batch(&columns, &rows, 0).expect("converts");
        let s = batch
            .column(0)
            .as_any()
            .downcast_ref::<StructArray>()
            .unwrap();
        assert!(s.is_null(1), "null relationship row is a null struct");
        let types = s
            .column_by_name("type")
            .unwrap()
            .as_any()
            .downcast_ref::<StringArray>()
            .unwrap();
        assert_eq!(types.value(0), "KNOWS");
        let starts = s
            .column_by_name("start_id")
            .unwrap()
            .as_any()
            .downcast_ref::<StringArray>()
            .unwrap();
        assert_eq!(starts.value(0), "1", "endpoint ids stringify");
    }

    #[test]
    fn every_wrong_kind_is_a_typed_mismatch_per_declared_type() {
        // One wrong-kind row per scalar declared type, plus the malformed
        // struct shapes — each must name the declared type, never a value.
        let cases: Vec<(GraphType, Value, &str)> = vec![
            (GraphType::String, serde_json::json!(7), "string"),
            (GraphType::Bool, serde_json::json!("yes"), "bool"),
            (GraphType::Float, serde_json::json!("1.5"), "float"),
            (GraphType::Node, serde_json::json!({"id": 1}), "node"), // no label
            (
                // An EDGE object satisfies the node keys (id + label) —
                // the endpoint keys are what must reject it, or a
                // `RETURN r` declared `node` decodes silently with
                // start_id/end_id dropped.
                GraphType::Node,
                serde_json::json!({"id": 9, "label": "KNOWS", "start_id": 1,
                                   "end_id": 2, "properties": {}}),
                "node",
            ),
            (
                GraphType::Relationship,
                serde_json::json!({"id": 1, "label": "K"}), // no endpoints
                "relationship",
            ),
            (GraphType::Path, serde_json::json!("not-a-list"), "path"),
        ];
        for (ty, bad, name) in cases {
            let columns = vec![col("c", ty)];
            let err = build_batch(&columns, &[vec![bad]], 0).unwrap_err();
            assert!(
                err.to_string().contains(&format!("declared '{name}'")),
                "{name}: {err}"
            );
        }
    }

    #[test]
    fn canonical_names_and_arrow_types_cover_every_variant() {
        for (ty, canonical) in [
            (GraphType::String, "string"),
            (GraphType::Int, "int"),
            (GraphType::Float, "float"),
            (GraphType::Bool, "bool"),
            (GraphType::Json, "json"),
            (GraphType::Node, "node"),
            (GraphType::Relationship, "relationship"),
            (GraphType::Path, "path"),
        ] {
            assert_eq!(ty.canonical(), canonical);
            // Round-trip: the canonical name parses back to the same type.
            assert_eq!(GraphType::parse(canonical), Some(ty));
            // And every variant plans a concrete Arrow type.
            let _ = ty.arrow_type();
        }
    }

    #[test]
    fn a_path_with_a_malformed_element_is_a_typed_mismatch() {
        // An edge slot holding a non-edge object fails as 'relationship'
        // (the element conversion), with the path row named.
        let columns = vec![col("p", GraphType::Path)];
        let rows = vec![vec![serde_json::json!([
            {"id": 1, "label": "A", "properties": {}},
            {"id": 9}, // not an edge
            {"id": 2, "label": "B", "properties": {}}
        ])]];
        let err = build_batch(&columns, &rows, 3).unwrap_err();
        let msg = err.to_string();
        assert!(msg.contains("declared 'relationship'"), "{msg}");
        assert!(msg.contains("row 3"), "{msg}");
    }

    #[test]
    fn a_null_path_row_is_a_null_struct() {
        // With a None parent validity the null row would read as a VALID
        // struct whose child lists are null — and SQL `path IS NULL`
        // would answer false. The parent must carry the row validity.
        let columns = vec![col("p", GraphType::Path)];
        let rows = vec![
            vec![serde_json::json!([{"id":1,"label":"A","properties":{}}])],
            vec![Value::Null],
        ];
        let batch = build_batch(&columns, &rows, 0).expect("converts");
        let paths = batch
            .column(0)
            .as_any()
            .downcast_ref::<StructArray>()
            .unwrap();
        assert!(!paths.is_null(0));
        assert!(paths.is_null(1), "NULL path row must be a NULL struct");
    }

    #[test]
    fn a_short_row_is_a_typed_arity_error_not_a_panic() {
        // A backend row narrower than the declared schema (reachable via
        // future drivers) must surface as a typed error with identity.
        let columns = vec![col("a", GraphType::Int), col("b", GraphType::Int)];
        let rows = vec![
            vec![serde_json::json!(1), serde_json::json!(2)],
            vec![serde_json::json!(3)], // one column short
        ];
        let err = build_batch(&columns, &rows, 5).unwrap_err();
        let msg = err.to_string();
        assert!(msg.contains("row 6"), "{msg}");
        assert!(
            msg.contains("carries 1 columns but 2 were declared"),
            "{msg}"
        );
    }

    #[test]
    fn a_null_under_nullable_false_is_a_typed_violation_with_identity() {
        // YAML views may assert `nullable: false`; a null met under it is
        // NotNullViolation naming the column and the ABSOLUTE row — never
        // a value (there is none to leak, but the discipline holds).
        let columns = vec![DeclaredColumn {
            name: "n".to_string(),
            ty: GraphType::Int,
            nullable: false,
        }];
        let rows = vec![vec![serde_json::json!(1)], vec![Value::Null]];
        let err = build_batch(&columns, &rows, 10).unwrap_err();
        assert!(matches!(err, GraphError::NotNullViolation { .. }), "{err}");
        let msg = err.to_string();
        assert!(msg.contains("'n'"), "{msg}");
        assert!(msg.contains("row 11"), "absolute row index: {msg}");
        assert!(msg.contains("nullable: false"), "{msg}");
        // The same null under a nullable declaration keeps passing.
        build_batch(&[col("n", GraphType::Int)], &rows, 10).expect("nullable passes");
    }

    #[test]
    fn declared_schema_carries_each_columns_nullable_bit() {
        let columns = vec![
            col("opt", GraphType::String),
            DeclaredColumn {
                name: "req".to_string(),
                ty: GraphType::Int,
                nullable: false,
            },
        ];
        let schema = declared_schema(&columns);
        assert!(schema.field(0).is_nullable());
        assert!(!schema.field(1).is_nullable());
    }

    #[test]
    fn even_length_and_empty_paths_are_typed_mismatches() {
        let columns = vec![col("p", GraphType::Path)];
        let rows = vec![vec![
            serde_json::json!([{"id":1,"label":"A","properties":{}},
                                                {"id":2,"label":"B","properties":{}}]),
        ]];
        let err = build_batch(&columns, &rows, 0).unwrap_err();
        assert!(err.to_string().contains("declared 'path'"), "{err}");

        // The empty array is malformed too — an AGE path always carries
        // at least one node, and silently accepting [] as a "0-node
        // path" would hide upstream corruption.
        let rows = vec![vec![serde_json::json!([])]];
        let err = build_batch(&columns, &rows, 0).unwrap_err();
        assert!(err.to_string().contains("declared 'path'"), "{err}");

        // The zero-hop minimum stays legal: one node, no edges.
        let rows = vec![vec![
            serde_json::json!([{"id":1,"label":"A","properties":{}}]),
        ]];
        build_batch(&columns, &rows, 0).expect("a single-node path is the legal minimum");
    }
}