skardi 0.5.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
//! JSON → Arrow conversion against a table's fixed schema.
//!
//! Conversion never infers from data: every field comes from a source-pack
//! [`FieldMapping`], so upstream additive changes are ignored and upstream
//! *breaking* changes fail conversion with the column, page, row, expected
//! type, and the found JSON *kind* (never the value, which may be
//! sensitive).

use std::sync::Arc;

use arrow::array::{
    ArrayRef, BooleanArray, Float64Array, Int64Array, ListBuilder, RecordBatch, StringArray,
    StringBuilder, TimestampMillisecondArray, UInt64Array,
};
use arrow::datatypes::{DataType, Field, Schema, SchemaRef, TimeUnit};
use chrono::DateTime;
use serde_json::Value;

use super::error::OpenConnectorError;
use super::row_path::RowPath;

/// Column type a source-pack field can declare. Deliberately small: it is
/// the contract Skardi maintains, not every Arrow type.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FieldType {
    /// JSON boolean ↔ Arrow Boolean.
    Boolean,
    /// JSON integer ↔ Arrow Int64.
    Int64,
    /// JSON non-negative integer ↔ Arrow UInt64.
    UInt64,
    /// JSON number ↔ Arrow Float64.
    Float64,
    /// JSON string ↔ Arrow Utf8.
    Utf8,
    /// RFC 3339 string or epoch-millis number ↔ Arrow Timestamp(Millisecond, UTC).
    TimestampMillisUtc,
    /// JSON integer epoch **seconds** ↔ Arrow Timestamp(Millisecond, UTC).
    /// For Slack-style APIs whose `created` / `updated` fields are whole
    /// seconds — reading those through [`FieldType::TimestampMillisUtc`]
    /// would silently misparse them as millis (dates in January 1970).
    /// Strictly integers: fractional or string values fail with their kind.
    TimestampSecondsUtc,
    /// JSON array of strings ↔ Arrow List\<Utf8\>.
    Utf8List,
    /// JSON array of objects, each contributing the string under the given
    /// key ↔ Arrow List\<Utf8\> — the design's `$.labels[*].name` /
    /// `$.assignees[*].login` flattening for GitHub-style shapes.
    Utf8ListFromObjectKey(&'static str),
    /// Any JSON value serialized to a JSON string ↔ Arrow Utf8. For
    /// intentionally opaque fields (arbitrary maps, unstable unions). A
    /// present JSON null is SQL NULL (per the shared null rules), not the
    /// string `"null"`.
    Json,
}

impl FieldType {
    /// The Arrow data type this field type maps to.
    pub fn arrow_type(&self) -> DataType {
        match self {
            Self::Boolean => DataType::Boolean,
            Self::Int64 => DataType::Int64,
            Self::UInt64 => DataType::UInt64,
            Self::Float64 => DataType::Float64,
            Self::Utf8 | Self::Json => DataType::Utf8,
            Self::TimestampMillisUtc | Self::TimestampSecondsUtc => {
                DataType::Timestamp(TimeUnit::Millisecond, Some("UTC".into()))
            }
            Self::Utf8List | Self::Utf8ListFromObjectKey(_) => {
                DataType::List(Arc::new(Field::new("item", DataType::Utf8, true)))
            }
        }
    }

    fn label(&self) -> &'static str {
        match self {
            Self::Boolean => "boolean",
            Self::Int64 => "integer",
            Self::UInt64 => "non-negative integer",
            Self::Float64 => "number",
            Self::Utf8 => "string",
            Self::TimestampMillisUtc => "RFC 3339 timestamp or epoch millis",
            Self::TimestampSecondsUtc => "epoch-seconds timestamp",
            Self::Utf8List => "array of strings",
            Self::Utf8ListFromObjectKey(_) => "array of objects each carrying a string key",
            Self::Json => "any JSON value",
        }
    }
}

/// One source-pack field: where in the row JSON it lives, and its type.
#[derive(Debug, Clone, Copy)]
pub struct FieldMapping {
    /// Arrow column name.
    pub name: &'static str,
    /// Row-relative dotted path, e.g. `user.login`. Must point at an object
    /// key; array indexing is out of scope for relational mappings.
    pub path: &'static str,
    /// Column type.
    pub field_type: FieldType,
    /// Whether missing keys / JSON nulls become Arrow nulls (true) or fail
    /// conversion (false).
    pub nullable: bool,
}

/// An owned [`FieldMapping`]. Source packs declare columns statically; raw
/// scans (`open_connector_scan`) derive them at planning time from discovered
/// action metadata, so their names cannot be `&'static str`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ColumnSpec {
    /// Arrow column name.
    pub name: String,
    /// Row-relative dotted path (see [`FieldMapping::path`]).
    pub path: String,
    /// Column type.
    pub field_type: FieldType,
    /// Whether missing keys / JSON nulls become Arrow nulls (true) or fail
    /// conversion (false).
    pub nullable: bool,
}

impl From<&FieldMapping> for ColumnSpec {
    fn from(mapping: &FieldMapping) -> Self {
        Self {
            name: mapping.name.to_string(),
            path: mapping.path.to_string(),
            field_type: mapping.field_type,
            nullable: mapping.nullable,
        }
    }
}

/// A column spec with its path pre-parsed.
#[derive(Debug)]
struct CompiledField {
    spec: ColumnSpec,
    path: RowPath,
}

/// Converts rows against one fixed schema. Build once per table; the Arrow
/// schema it produces never changes for the converter's lifetime.
#[derive(Debug)]
pub struct RowConverter {
    fields: Vec<CompiledField>,
    schema: SchemaRef,
}

impl RowConverter {
    /// Compile a converter from static field mappings.
    ///
    /// # Errors
    /// Returns [`OpenConnectorError::InvalidRowPath`] if any mapping path is
    /// not a dotted object-key path.
    pub fn new(mappings: &[FieldMapping]) -> Result<Self, OpenConnectorError> {
        Self::from_columns(mappings.iter().map(ColumnSpec::from).collect())
    }

    /// Compile a converter from owned column specs (the raw-scan path).
    pub fn from_columns(columns: Vec<ColumnSpec>) -> Result<Self, OpenConnectorError> {
        let mut fields = Vec::with_capacity(columns.len());
        let mut arrow_fields = Vec::with_capacity(columns.len());
        for spec in columns {
            // Column paths are row-relative (`user.login`); the `$.` marker
            // belongs to whole-document row paths only. Reject it rather
            // than strip: `$.{path}` would otherwise parse into a literal
            // `"$"` first segment and silently read the wrong key —
            // all-NULL columns instead of a loud error.
            if spec.path.starts_with("$.") {
                return Err(OpenConnectorError::InvalidRowPath {
                    path: spec.path.clone(),
                    reason: format!(
                        "column paths are row-relative object keys \
                         (write '{}', not '{}')",
                        &spec.path[2..],
                        spec.path
                    ),
                });
            }
            let path = RowPath::parse(&format!("$.{}", spec.path))?;
            arrow_fields.push(Field::new(
                &spec.name,
                spec.field_type.arrow_type(),
                spec.nullable,
            ));
            fields.push(CompiledField { spec, path });
        }
        Ok(Self {
            fields,
            schema: Arc::new(Schema::new(arrow_fields)),
        })
    }

    /// The fixed Arrow schema.
    pub fn schema(&self) -> &SchemaRef {
        &self.schema
    }

    /// Convert one page of row objects to a RecordBatch. `page` is 1-based
    /// and only used for error context. An empty page yields an empty batch
    /// with the fixed schema.
    pub fn convert(&self, rows: &[Value], page: usize) -> Result<RecordBatch, OpenConnectorError> {
        let mut columns: Vec<ArrayRef> = Vec::with_capacity(self.fields.len());
        for field in &self.fields {
            columns.push(self.convert_column(field, rows, page)?);
        }
        RecordBatch::try_new(Arc::clone(&self.schema), columns).map_err(|e| {
            OpenConnectorError::ConversionFailed {
                path: "$".to_string(),
                column: "<batch>".to_string(),
                page,
                row: 0,
                expected: "a batch matching the fixed schema".to_string(),
                found: e.to_string(),
            }
        })
    }

    fn convert_column(
        &self,
        field: &CompiledField,
        rows: &[Value],
        page: usize,
    ) -> Result<ArrayRef, OpenConnectorError> {
        let spec = &field.spec;
        let mut cells: Vec<Option<&Value>> = Vec::with_capacity(rows.len());
        for (row_index, row) in rows.iter().enumerate() {
            match field.path.extract(row, page) {
                Ok(value) => cells.push(Some(value)),
                // Absence may become null for a nullable column: either the
                // key is genuinely missing, or a parent on the path is JSON
                // null — providers null out whole objects routinely (GitHub's
                // `commit.author: null`, `issue.user: null`), and a null
                // parent means every leaf under it is absent. A
                // present-but-wrong-shape parent (e.g. `user` changed from
                // object to string) is an upstream *breaking* change and
                // must fail, per this module's contract.
                Err(OpenConnectorError::RowPathNotFound { .. }) if spec.nullable => {
                    cells.push(None)
                }
                Err(OpenConnectorError::RowPathNotFound { .. }) => {
                    return Err(self.failure(field, page, row_index, "missing key"));
                }
                Err(OpenConnectorError::RowPathNotObject { ref found, .. })
                    if found == "null" && spec.nullable =>
                {
                    cells.push(None)
                }
                Err(OpenConnectorError::RowPathNotObject { ref found, .. }) => {
                    return Err(self.failure(field, page, row_index, found));
                }
                Err(e) => return Err(e),
            }
        }

        let fail = |index: usize, found: &str| self.failure(field, page, index, found);
        match spec.field_type {
            FieldType::Boolean => Ok(Arc::new(BooleanArray::from(collect_cells(
                &cells,
                spec,
                |v| v.as_bool(),
                fail,
            )?))),
            FieldType::Int64 => Ok(Arc::new(Int64Array::from(collect_cells(
                &cells,
                spec,
                |v| v.as_i64(),
                fail,
            )?))),
            FieldType::UInt64 => Ok(Arc::new(UInt64Array::from(collect_cells(
                &cells,
                spec,
                |v| v.as_u64(),
                fail,
            )?))),
            FieldType::Float64 => Ok(Arc::new(Float64Array::from(collect_cells(
                &cells,
                spec,
                |v| v.as_f64(),
                fail,
            )?))),
            FieldType::Utf8 => Ok(Arc::new(StringArray::from(collect_cells(
                &cells,
                spec,
                |v| v.as_str().map(str::to_string),
                fail,
            )?))),
            // Opaque values serialize to canonical JSON text, but a present
            // JSON null is SQL NULL like everywhere else — never the 4-char
            // string "null", which would break `IS NULL` and match
            // `= 'null'`. Routing through collect_cells also makes a null in
            // a non-nullable Json column a targeted per-column failure.
            FieldType::Json => Ok(Arc::new(StringArray::from(collect_cells(
                &cells,
                spec,
                |v| Some(v.to_string()),
                fail,
            )?))),
            FieldType::TimestampMillisUtc => Ok(Arc::new(
                TimestampMillisecondArray::from(collect_cells(
                    &cells,
                    spec,
                    parse_timestamp,
                    fail,
                )?)
                .with_timezone("UTC"),
            )),
            FieldType::TimestampSecondsUtc => Ok(Arc::new(
                TimestampMillisecondArray::from(collect_cells_described(
                    &cells,
                    spec,
                    |v| {
                        let seconds = v.as_i64().ok_or_else(|| json_kind(v).to_string())?;
                        seconds.checked_mul(1000).ok_or_else(|| {
                            "an epoch second out of range for millisecond timestamps".to_string()
                        })
                    },
                    fail,
                )?)
                .with_timezone("UTC"),
            )),
            FieldType::Utf8List => self.convert_string_list(field, &cells, page, None),
            FieldType::Utf8ListFromObjectKey(key) => {
                self.convert_string_list(field, &cells, page, Some(key))
            }
        }
    }

    /// Convert a list column. With `pluck: None` elements must be strings;
    /// with `pluck: Some(key)` elements must be objects carrying a string
    /// under `key` (the flattened value).
    fn convert_string_list(
        &self,
        field: &CompiledField,
        cells: &[Option<&Value>],
        page: usize,
        pluck: Option<&'static str>,
    ) -> Result<ArrayRef, OpenConnectorError> {
        let mut builder = ListBuilder::new(StringBuilder::new());
        for (row_index, cell) in cells.iter().enumerate() {
            let Some(value) = cell else {
                builder.append(false);
                continue;
            };
            if value.is_null() {
                if field.spec.nullable {
                    builder.append(false);
                    continue;
                }
                return Err(self.failure(field, page, row_index, "null"));
            }
            let items = value
                .as_array()
                .ok_or_else(|| self.failure(field, page, row_index, json_kind(value)))?;
            for item in items {
                // A JSON null wherever an item's text would come from is an
                // Arrow null item — the list's item field is declared
                // nullable, and a provider null is data, not shape drift. A
                // *missing* pluck key stays a failure: unlike columns, items
                // carry no pack-declared nullability that authorizes reading
                // absence as null, so a dropped upstream key fails loudly
                // instead of silently yielding all-null lists.
                let text = match pluck {
                    None => match item {
                        Value::Null => None,
                        Value::String(text) => Some(text.as_str()),
                        other => {
                            return Err(self.failure(
                                field,
                                page,
                                row_index,
                                &format!("{} array element", json_kind(other)),
                            ));
                        }
                    },
                    Some(key) => match item {
                        Value::Null => None,
                        Value::Object(object) => match object.get(key) {
                            Some(Value::Null) => None,
                            Some(Value::String(text)) => Some(text.as_str()),
                            Some(other) => {
                                return Err(self.failure(
                                    field,
                                    page,
                                    row_index,
                                    &format!("array element whose '{key}' is {}", json_kind(other)),
                                ));
                            }
                            None => {
                                return Err(self.failure(
                                    field,
                                    page,
                                    row_index,
                                    &format!("array element without key '{key}'"),
                                ));
                            }
                        },
                        other => {
                            return Err(self.failure(
                                field,
                                page,
                                row_index,
                                &format!("{} array element", json_kind(other)),
                            ));
                        }
                    },
                };
                match text {
                    Some(text) => builder.values().append_value(text),
                    None => builder.values().append_null(),
                }
            }
            builder.append(true);
        }
        Ok(Arc::new(builder.finish()))
    }

    fn failure(
        &self,
        field: &CompiledField,
        page: usize,
        row: usize,
        found: &str,
    ) -> OpenConnectorError {
        OpenConnectorError::ConversionFailed {
            path: field.path.as_str().to_string(),
            column: field.spec.name.clone(),
            page,
            row,
            expected: field.spec.field_type.label().to_string(),
            found: found.to_string(),
        }
    }
}

/// Project `cells` through `convert`, mapping JSON null to Arrow null for
/// nullable fields and failing otherwise — for the common case where a type
/// mismatch is the only failure mode, described by the value's JSON kind.
fn collect_cells<T, F, E>(
    cells: &[Option<&Value>],
    spec: &ColumnSpec,
    convert: F,
    fail: E,
) -> Result<Vec<Option<T>>, OpenConnectorError>
where
    F: Fn(&Value) -> Option<T>,
    E: Fn(usize, &str) -> OpenConnectorError,
{
    collect_cells_described(
        cells,
        spec,
        |value| convert(value).ok_or_else(|| json_kind(value).to_string()),
        fail,
    )
}

/// [`collect_cells`] with converter-described failures, for conversions
/// with more than one failure mode — an out-of-range epoch second must not
/// masquerade as "found: number" when the value *is* a number.
fn collect_cells_described<T, F, E>(
    cells: &[Option<&Value>],
    spec: &ColumnSpec,
    convert: F,
    fail: E,
) -> Result<Vec<Option<T>>, OpenConnectorError>
where
    F: Fn(&Value) -> Result<T, String>,
    E: Fn(usize, &str) -> OpenConnectorError,
{
    let mut out = Vec::with_capacity(cells.len());
    for (index, cell) in cells.iter().enumerate() {
        match cell {
            None => out.push(None),
            Some(value) if value.is_null() => {
                if spec.nullable {
                    out.push(None);
                } else {
                    return Err(fail(index, "null"));
                }
            }
            Some(value) => match convert(value) {
                Ok(converted) => out.push(Some(converted)),
                Err(found) => return Err(fail(index, &found)),
            },
        }
    }
    Ok(out)
}

/// RFC 3339 string or epoch-millis number → epoch millis.
fn parse_timestamp(value: &Value) -> Option<i64> {
    if let Some(text) = value.as_str() {
        let parsed = DateTime::parse_from_rfc3339(text).ok()?;
        return Some(parsed.timestamp_millis());
    }
    value.as_i64()
}
/// Short human-readable kind of a JSON value, for error messages.
fn json_kind(value: &Value) -> &'static str {
    match value {
        Value::Null => "null",
        Value::Bool(_) => "boolean",
        Value::Number(_) => "number",
        Value::String(_) => "string",
        Value::Array(_) => "array",
        Value::Object(_) => "object",
    }
}

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

    const FIELDS: &[FieldMapping] = &[
        FieldMapping {
            name: "id",
            path: "id",
            field_type: FieldType::UInt64,
            nullable: false,
        },
        FieldMapping {
            name: "title",
            path: "title",
            field_type: FieldType::Utf8,
            nullable: false,
        },
        FieldMapping {
            name: "author_login",
            path: "user.login",
            field_type: FieldType::Utf8,
            nullable: true,
        },
        FieldMapping {
            name: "score",
            path: "score",
            field_type: FieldType::Float64,
            nullable: true,
        },
        FieldMapping {
            name: "labels",
            path: "labels",
            field_type: FieldType::Utf8List,
            nullable: true,
        },
        FieldMapping {
            name: "created_at",
            path: "created_at",
            field_type: FieldType::TimestampMillisUtc,
            nullable: true,
        },
        FieldMapping {
            name: "raw",
            path: "raw",
            field_type: FieldType::Json,
            nullable: true,
        },
    ];

    fn converter() -> RowConverter {
        RowConverter::new(FIELDS).unwrap()
    }

    fn row(id: u64, title: &str) -> Value {
        json!({
            "id": id,
            "title": title,
            "user": {"login": "octocat"},
            "score": 9.5,
            "labels": ["bug", "p1"],
            "created_at": "2026-01-01T00:00:00Z",
            "raw": {"nested": [1, 2]},
            "extra_upstream_field": "ignored"
        })
    }

    #[test]
    fn converts_full_rows_and_ignores_extra_fields() {
        let batch = converter().convert(&[row(1, "a"), row(2, "b")], 1).unwrap();
        assert_eq!(batch.num_rows(), 2);
        assert_eq!(batch.num_columns(), 7);
        let ids = batch
            .column(0)
            .as_any()
            .downcast_ref::<UInt64Array>()
            .unwrap();
        assert_eq!(ids.value(1), 2);
        let authors = batch
            .column(2)
            .as_any()
            .downcast_ref::<StringArray>()
            .unwrap();
        assert_eq!(authors.value(0), "octocat");
    }

    #[test]
    fn missing_required_key_fails_with_context() {
        let mut value = row(1, "a");
        value.as_object_mut().unwrap().remove("title");
        let err = converter().convert(&[value], 2).unwrap_err();
        assert!(matches!(
            err,
            OpenConnectorError::ConversionFailed { ref column, page: 2, row: 0, ref found, .. }
                if column == "title" && found == "missing key"
        ));
    }

    #[test]
    fn missing_nullable_key_becomes_null() {
        let mut value = row(1, "a");
        value.as_object_mut().unwrap().remove("score");
        let batch = converter().convert(&[value], 1).unwrap();
        let scores = batch
            .column(3)
            .as_any()
            .downcast_ref::<Float64Array>()
            .unwrap();
        assert!(scores.is_null(0));
    }

    #[test]
    fn nullable_column_fails_on_shape_mismatch() {
        // `user` changed from object to string upstream: a nullable
        // user.login column must NOT quietly become all-null — this is a
        // breaking change and has to fail conversion.
        let mut value = row(1, "a");
        value["user"] = json!("octocat");
        let err = converter().convert(&[value], 1).unwrap_err();
        match err {
            OpenConnectorError::ConversionFailed { column, found, .. } => {
                assert_eq!(column, "author_login");
                assert_eq!(found, "a string");
            }
            other => panic!("expected ConversionFailed, got {other}"),
        }
    }

    #[test]
    fn null_parent_object_nulls_for_nullable_column() {
        // Providers null out whole objects (GitHub `commit.author: null`,
        // `issue.user: null`): a JSON-null parent means every leaf under it
        // is absent — NULL for a nullable column, not a structural failure.
        let mut value = row(1, "a");
        value["user"] = Value::Null;
        let batch = converter().convert(&[value], 1).unwrap();
        let authors = batch
            .column(2)
            .as_any()
            .downcast_ref::<StringArray>()
            .unwrap();
        assert!(authors.is_null(0));
    }

    #[test]
    fn null_parent_object_fails_for_required_column() {
        let converter = RowConverter::new(&[FieldMapping {
            name: "author_login",
            path: "user.login",
            field_type: FieldType::Utf8,
            nullable: false,
        }])
        .unwrap();
        let err = converter
            .convert(&[serde_json::json!({"user": null})], 1)
            .unwrap_err();
        assert!(matches!(
            err,
            OpenConnectorError::ConversionFailed { ref column, ref found, .. }
                if column == "author_login" && found == "null"
        ));
    }

    #[test]
    fn object_list_plucks_the_declared_string_key() {
        // The design's `$.labels[*].name` flattening: an array of objects
        // becomes List<Utf8> of one declared key.
        let converter = RowConverter::new(&[FieldMapping {
            name: "labels",
            path: "labels",
            field_type: FieldType::Utf8ListFromObjectKey("name"),
            nullable: true,
        }])
        .unwrap();
        let batch = converter
            .convert(
                &[
                    serde_json::json!({"labels": [{"name": "bug", "color": "red"}, {"name": "p1"}]}),
                    serde_json::json!({"labels": []}),
                    serde_json::json!({"labels": null}),
                    serde_json::json!({}),
                ],
                1,
            )
            .unwrap();
        let lists = batch
            .column(0)
            .as_any()
            .downcast_ref::<arrow::array::ListArray>()
            .unwrap();
        let first = lists.value(0);
        let first = first.as_any().downcast_ref::<StringArray>().unwrap();
        assert_eq!(first.value(0), "bug");
        assert_eq!(first.value(1), "p1");
        assert_eq!(lists.value(1).len(), 0, "empty array stays an empty list");
        assert!(lists.is_null(2), "JSON null becomes NULL");
        assert!(lists.is_null(3), "absent key becomes NULL");
    }

    #[test]
    fn object_list_shape_mismatches_fail_with_the_specific_kind() {
        let converter = RowConverter::new(&[FieldMapping {
            name: "labels",
            path: "labels",
            field_type: FieldType::Utf8ListFromObjectKey("name"),
            nullable: true,
        }])
        .unwrap();
        for (bad, found) in [
            // A dropped upstream key is shape drift and must fail loudly —
            // it must never silently become an all-null list.
            (
                serde_json::json!({"labels": [{"color": "red"}]}),
                "array element without key 'name'",
            ),
            (
                serde_json::json!({"labels": [{"name": 42}]}),
                "array element whose 'name' is number",
            ),
            (
                serde_json::json!({"labels": ["bug"]}),
                "string array element",
            ),
        ] {
            let err = converter.convert(&[bad], 1).unwrap_err();
            assert!(
                matches!(
                    err,
                    OpenConnectorError::ConversionFailed { found: ref f, .. } if f == found
                ),
                "expected found={found}, got {err}"
            );
        }
    }

    #[test]
    fn json_null_list_elements_become_null_items() {
        // The item field is declared nullable (List<Utf8> with nullable
        // items), so an explicit provider null — a null element, or a pluck
        // key holding null — must convert to an Arrow null item, not fail.
        let converter = RowConverter::new(&[
            FieldMapping {
                name: "tags",
                path: "tags",
                field_type: FieldType::Utf8List,
                nullable: true,
            },
            FieldMapping {
                name: "labels",
                path: "labels",
                field_type: FieldType::Utf8ListFromObjectKey("name"),
                nullable: true,
            },
        ])
        .unwrap();
        let batch = converter
            .convert(
                &[serde_json::json!({
                    "tags": ["a", null, "b"],
                    "labels": [{"name": null}, {"name": "bug"}, null]
                })],
                1,
            )
            .unwrap();

        let tags = batch
            .column(0)
            .as_any()
            .downcast_ref::<arrow::array::ListArray>()
            .unwrap()
            .value(0);
        let tags = tags.as_any().downcast_ref::<StringArray>().unwrap();
        assert_eq!(tags.value(0), "a");
        assert!(tags.is_null(1), "null element is a null item");
        assert_eq!(tags.value(2), "b");

        let labels = batch
            .column(1)
            .as_any()
            .downcast_ref::<arrow::array::ListArray>()
            .unwrap()
            .value(0);
        let labels = labels.as_any().downcast_ref::<StringArray>().unwrap();
        assert!(labels.is_null(0), "pluck key holding null is a null item");
        assert_eq!(labels.value(1), "bug");
        assert!(labels.is_null(2), "null element is a null item");
    }

    #[test]
    fn missing_parent_object_still_nulls_for_nullable_column() {
        // A genuinely-absent parent (no `user` key at all) is absence, not a
        // shape change — nullable user.login becomes null.
        let mut value = row(1, "a");
        value.as_object_mut().unwrap().remove("user");
        let batch = converter().convert(&[value], 1).unwrap();
        let authors = batch
            .column(2)
            .as_any()
            .downcast_ref::<StringArray>()
            .unwrap();
        assert!(authors.is_null(0));
    }

    #[test]
    fn null_in_required_field_fails() {
        let mut value = row(1, "a");
        value["title"] = Value::Null;
        let err = converter().convert(&[value], 1).unwrap_err();
        assert!(matches!(
            err,
            OpenConnectorError::ConversionFailed { ref column, ref found, .. }
                if column == "title" && found == "null"
        ));
    }

    #[test]
    fn wrong_type_fails_with_kind_not_value() {
        let mut value = row(1, "a");
        value["id"] = json!("not-a-number");
        let err = converter().convert(&[value], 1).unwrap_err();
        match err {
            OpenConnectorError::ConversionFailed { found, column, .. } => {
                assert_eq!(column, "id");
                assert_eq!(found, "string");
                assert!(!found.contains("not-a-number"), "error must not echo data");
            }
            other => panic!("expected ConversionFailed, got {other}"),
        }
    }

    #[test]
    fn float_for_int_fails() {
        let mut value = row(1, "a");
        value["id"] = json!(1.5);
        let err = converter().convert(&[value], 1).unwrap_err();
        assert!(matches!(err, OpenConnectorError::ConversionFailed { .. }));
    }

    #[test]
    fn timestamps_parse_rfc3339_and_epoch() {
        let mut value = row(1, "a");
        let batch = converter().convert(&[value.clone()], 1).unwrap();
        let ts = batch
            .column(5)
            .as_any()
            .downcast_ref::<TimestampMillisecondArray>()
            .unwrap();
        assert_eq!(ts.value(0), 1767225600000);

        value["created_at"] = json!(1767225600000i64);
        let batch = converter().convert(&[value], 1).unwrap();
        let ts = batch
            .column(5)
            .as_any()
            .downcast_ref::<TimestampMillisecondArray>()
            .unwrap();
        assert_eq!(ts.value(0), 1767225600000);
    }

    #[test]
    fn epoch_seconds_convert_to_millis_and_reject_other_shapes() {
        // Slack-style `created`/`updated` are whole epoch seconds; the
        // seconds variant scales to millis, and anything that is not an
        // integer fails with its kind — a fractional or string timestamp
        // must never be misread.
        let converter = RowConverter::new(&[FieldMapping {
            name: "created",
            path: "created",
            field_type: FieldType::TimestampSecondsUtc,
            nullable: true,
        }])
        .unwrap();

        let batch = converter
            .convert(
                &[
                    serde_json::json!({"created": 1735689600}), // 2025-01-01T00:00:00Z
                    serde_json::json!({"created": null}),
                    serde_json::json!({}),
                ],
                1,
            )
            .unwrap();
        let created = batch
            .column(0)
            .as_any()
            .downcast_ref::<TimestampMillisecondArray>()
            .unwrap();
        assert_eq!(created.value(0), 1_735_689_600_000);
        assert!(created.is_null(1), "JSON null stays NULL");
        assert!(created.is_null(2), "absent key stays NULL");

        for (bad, found) in [
            (serde_json::json!({"created": 1735689600.5}), "number"),
            (
                serde_json::json!({"created": "2025-01-01T00:00:00Z"}),
                "string",
            ),
            // Overflow is its own diagnosis — "found: number" would
            // contradict the expected type, which is also a number.
            (
                serde_json::json!({"created": i64::MAX}),
                "an epoch second out of range for millisecond timestamps",
            ),
        ] {
            let err = converter.convert(&[bad], 1).unwrap_err();
            assert!(
                matches!(
                    err,
                    OpenConnectorError::ConversionFailed { found: ref f, ref expected, .. }
                        if f == found && expected == "epoch-seconds timestamp"
                ),
                "got {err}"
            );
        }
    }

    #[test]
    fn non_string_list_element_fails() {
        let mut value = row(1, "a");
        value["labels"] = json!(["bug", 42]);
        let err = converter().convert(&[value], 1).unwrap_err();
        assert!(matches!(err, OpenConnectorError::ConversionFailed { .. }));
    }

    #[test]
    fn opaque_field_serializes_json() {
        let batch = converter().convert(&[row(1, "a")], 1).unwrap();
        let raw = batch
            .column(6)
            .as_any()
            .downcast_ref::<StringArray>()
            .unwrap();
        assert!(raw.value(0).contains("nested"));
    }

    #[test]
    fn json_null_becomes_arrow_null_not_the_string_null() {
        // A present JSON null in a nullable Json column is SQL NULL — never
        // the 4-char string "null", which would make `IS NULL` miss and
        // `= 'null'` match. Raw scans type every object/array field as Json,
        // so nullable nested SaaS fields (assignee: null) hit this arm.
        let mut with_null = row(1, "a");
        with_null["raw"] = Value::Null;
        let mut absent = row(2, "b");
        absent.as_object_mut().unwrap().remove("raw");

        let batch = converter().convert(&[with_null, absent], 1).unwrap();
        let raw = batch
            .column(6)
            .as_any()
            .downcast_ref::<StringArray>()
            .unwrap();
        assert!(raw.is_null(0), "present JSON null must be Arrow null");
        assert!(raw.is_null(1), "absent key stays Arrow null");
    }

    #[test]
    fn json_null_fails_for_non_nullable_json_column() {
        // Same null discipline as every other type: a required opaque field
        // fails with a targeted per-column error, not a batch-level one.
        let converter = RowConverter::new(&[FieldMapping {
            name: "raw",
            path: "raw",
            field_type: FieldType::Json,
            nullable: false,
        }])
        .unwrap();
        let err = converter
            .convert(&[serde_json::json!({"raw": null})], 3)
            .unwrap_err();
        assert!(matches!(
            err,
            OpenConnectorError::ConversionFailed { ref column, page: 3, row: 0, ref found, .. }
                if column == "raw" && found == "null"
        ));
    }

    #[test]
    fn empty_page_yields_empty_batch_with_schema() {
        let converter = converter();
        let batch = converter.convert(&[], 1).unwrap();
        assert_eq!(batch.num_rows(), 0);
        assert_eq!(batch.schema().fields().len(), 7);
    }

    #[test]
    fn owned_columns_build_the_same_converter_as_static_mappings() {
        // The raw-scan path derives ColumnSpecs at planning time; they must
        // produce exactly the schema and conversion the static path does.
        let owned = RowConverter::from_columns(FIELDS.iter().map(ColumnSpec::from).collect())
            .expect("owned converter");
        assert_eq!(owned.schema(), converter().schema());

        let batch = owned.convert(&[row(7, "seven")], 1).unwrap();
        let ids = batch
            .column(0)
            .as_any()
            .downcast_ref::<UInt64Array>()
            .unwrap();
        assert_eq!(ids.value(0), 7);
    }

    #[test]
    fn dollar_prefixed_column_paths_are_rejected_not_silently_misread() {
        // `$.user.login` would survive the `$.{path}` concatenation as a
        // literal `"$"` first segment and read the wrong key — nullable
        // columns would silently go all-NULL. Reject it with the corrected
        // spelling instead.
        let err = RowConverter::new(&[FieldMapping {
            name: "author",
            path: "$.user.login",
            field_type: FieldType::Utf8,
            nullable: true,
        }])
        .unwrap_err();
        assert!(matches!(
            err,
            OpenConnectorError::InvalidRowPath { ref path, ref reason }
                if path == "$.user.login"
                    && reason.contains("write 'user.login', not '$.user.login'")
        ));
    }

    #[test]
    fn invalid_mapping_path_is_rejected() {
        let err = RowConverter::new(&[FieldMapping {
            name: "x",
            path: "a..b",
            field_type: FieldType::Utf8,
            nullable: false,
        }])
        .unwrap_err();
        assert!(matches!(err, OpenConnectorError::InvalidRowPath { .. }));
    }
}