skardi 0.4.0

High performance query engine for both offline compute and online serving
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
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
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
use std::{
    any::Any,
    collections::HashMap,
    fmt::{self, Debug, Formatter},
    sync::{Arc, RwLock},
};

use anyhow::Result;
use arrow::{
    array::{
        ArrayRef, RecordBatch, RecordBatchOptions, StringBuilder, UInt64Array, as_boolean_array,
        as_largestring_array, as_string_array,
    },
    datatypes::{DataType, Field, Schema, SchemaRef},
};
use async_trait::async_trait;
use datafusion::{
    catalog::{Session, TableProvider},
    common::cast::{
        as_binary_array, as_float16_array, as_float32_array, as_float64_array, as_int8_array,
        as_int16_array, as_int32_array, as_int64_array, as_large_binary_array, as_uint8_array,
        as_uint16_array, as_uint32_array, as_uint64_array,
    },
    datasource::TableType,
    datasource::sink::{DataSink, DataSinkExec},
    error::DataFusionError,
    execution::{SendableRecordBatchStream, TaskContext},
    logical_expr::{Operator, dml::InsertOp},
    physical_expr::EquivalenceProperties,
    physical_plan::{
        DisplayAs, DisplayFormatType, ExecutionPlan, Partitioning, PlanProperties,
        execution_plan::{Boundedness, EmissionType},
        memory::MemoryStream,
    },
    prelude::Expr,
};
use derivative::Derivative;
use futures::TryStreamExt;
use redis::{Commands, ConnectionLike, Iter};
use uuid::Uuid;

use super::relation::RedisRelation;

/// Enum representing the storage format of data in Redis.
/// Currently supports Hash (each row as a Redis hash). Can be extended to JSON, etc.
#[derive(Debug, Clone)]
pub enum RedisStorage {
    Hash,
}

/// The main TableProvider implementation for Redis.
///
#[derive(Derivative)]
#[derivative(Debug)]
pub struct RedisTable<C>
where
    C: ConnectionLike + Commands + Send + Sync + 'static,
{
    #[derivative(Debug = "ignore")]
    conn: Arc<RwLock<C>>,
    key_space: String,
    table_name: String,
    storage: RedisStorage,
    /// Cached schema. When the table is empty at registration, this starts as a minimal
    /// schema (key column only). On each `schema()` call, if the schema has no data fields,
    /// we re-infer from Redis — once data appears, the schema is cached permanently.
    schema: RwLock<SchemaRef>,
    key_column: Option<String>,
}

impl<C> RedisTable<C>
where
    C: ConnectionLike + Commands + Send + Sync + 'static,
{
    /// Create a new RedisTable by connecting to Redis and retrieving/inferencing the schema.
    /// If the table is empty in Redis, registration still succeeds — using `columns` if
    /// provided, or a minimal schema (just the key column) otherwise. The schema is
    /// re-inferred dynamically when empty, so it picks up new fields after the first INSERT.
    ///
    /// `columns` — optional list of column names to declare the schema for empty tables.
    /// This allows INSERT operations to work before any data exists in Redis.
    pub fn new(
        conn: C,
        key_space: String,
        table_name: String,
        storage: RedisStorage,
        key_column: Option<String>,
        columns: Option<Vec<String>>,
    ) -> Result<Self> {
        let conn = Arc::new(RwLock::new(conn));
        let mut schema = Self::infer_schema(&conn, &key_space, &table_name, &key_column)?;

        // If the table is empty and explicit columns were declared, use them as the schema.
        let has_data_fields = match &key_column {
            Some(_) => schema.fields().len() > 1,
            None => !schema.fields().is_empty(),
        };
        if !has_data_fields {
            if let Some(cols) = columns {
                let mut fields: Vec<Field> = vec![];
                for col in &cols {
                    let nullable = key_column.as_ref() != Some(col);
                    fields.push(Field::new(col, DataType::Utf8, nullable));
                }
                schema = Arc::new(Schema::new(fields));
            }
        }

        Ok(RedisTable {
            conn,
            key_space,
            table_name,
            storage,
            schema: RwLock::new(schema),
            key_column,
        })
    }

    /// Infer the schema from existing Redis data. Returns a minimal schema (just the key
    /// column) if the table is empty — SELECT will return empty results and INSERT will
    /// work using the incoming data's schema.
    fn infer_schema(
        conn: &Arc<RwLock<C>>,
        key_space: &str,
        table_name: &str,
        key_column: &Option<String>,
    ) -> Result<SchemaRef> {
        let pattern = RedisRelation::table_data_key_pattern(key_space, table_name);

        let mut fields: Vec<Field> = vec![];
        let mut inferred_fields: Vec<String> = vec![];

        // TODO: Implement read from schema_key

        let mut conn_write = conn.try_write().map_err(|e| {
            DataFusionError::Execution(format!(
                "failed to acquire write lock of redis connection: {}",
                e
            ))
        })?;
        // Collect the first key from SCAN, then drop the iterator to release the borrow
        // so we can call HGETALL on the same connection.
        let sample_key: Option<String> =
            {
                let mut iter: Iter<String> = conn_write
                    .scan_match(&pattern)
                    .map_err(|e| DataFusionError::Execution(format!("Redis SCAN error: {}", e)))?;
                match iter.next() {
                    Some(result) => Some(result.map_err(|e| {
                        DataFusionError::Execution(format!("Redis SCAN error: {}", e))
                    })?),
                    None => None,
                }
            };
        if let Some(sample_key) = sample_key {
            let entries: Vec<(String, String)> = conn_write
                .hgetall(sample_key)
                .map_err(|e| DataFusionError::Execution(format!("Redis HGETALL error: {}", e)))?;
            // TODO: Implement refer schema from other RedisStorage other than hash, when added
            for (field_name, _v) in entries.iter() {
                inferred_fields.push(field_name.to_string());
            }
        }
        drop(conn_write);

        // Sort field names to have a deterministic order (not strictly necessary, but for consistency).
        inferred_fields.sort();

        // If a key column is expected (used as part of key, not stored as field), include it in schema.
        if let Some(key_col) = key_column {
            // If the key column was not present in the hash fields, add it.
            if !inferred_fields.iter().any(|f| f == key_col) {
                fields.push(Field::new(key_col, DataType::Utf8, false));
            }
        }

        // Add all inferred fields (except maybe the key column if it was part of them).
        for field in &inferred_fields {
            // If key_column is set and equals this field, skip it here because we added it already as non-nullable.
            if let Some(key_col) = key_column {
                if field == key_col {
                    continue;
                }
            }
            // All values are stored as strings in Redis; mark as Utf8 (could refine if schema info available).
            // TODO: Support all dataType not only limit to UTF-8
            fields.push(Field::new(field, DataType::Utf8, true));
        }

        Ok(Arc::new(Schema::new(fields)))
    }

    /// Return the cached schema. If the cached schema has no data fields (empty table),
    /// re-infer from Redis — once data appears, the result is cached permanently.
    fn current_schema(&self) -> SchemaRef {
        let cached = match self.schema.read() {
            Ok(guard) => guard.clone(),
            Err(_) => return Arc::new(Schema::empty()),
        };
        let has_data_fields = match &self.key_column {
            Some(_) => cached.fields().len() > 1,
            None => !cached.fields().is_empty(),
        };
        if has_data_fields {
            return cached;
        }
        // Schema has no data fields — try to re-infer from Redis
        if let Ok(new_schema) = Self::infer_schema(
            &self.conn,
            &self.key_space,
            &self.table_name,
            &self.key_column,
        ) {
            let new_has_data = match &self.key_column {
                Some(_) => new_schema.fields().len() > 1,
                None => !new_schema.fields().is_empty(),
            };
            if new_has_data {
                // Data appeared — cache the real schema permanently
                if let Ok(mut guard) = self.schema.write() {
                    *guard = new_schema.clone();
                }
                return new_schema;
            }
        }
        cached
    }
}

#[async_trait]
impl<C> TableProvider for RedisTable<C>
where
    C: ConnectionLike + Commands + Send + Sync + 'static,
{
    fn as_any(&self) -> &dyn Any {
        self
    }
    /// Return the schema of the table (Arrow SchemaRef).
    /// Re-infers the schema from Redis on each call so that new fields added by INSERT
    /// are visible without a server restart.
    fn schema(&self) -> SchemaRef {
        self.current_schema()
    }

    /// Get the type of this table for metadata/catalog purposes.
    fn table_type(&self) -> TableType {
        TableType::Base
    }

    /// Scan the table: create an ExecutionPlan that will read the data from Redis.
    async fn scan(
        &self,
        _state: &dyn Session,
        projection: Option<&Vec<usize>>,
        filters: &[Expr],
        limit: Option<usize>,
    ) -> datafusion::common::Result<Arc<dyn ExecutionPlan>> {
        // TODO: Utilize state
        // Use the current (dynamic) schema so we pick up fields added after registration
        let current_schema = self.current_schema();

        // Determine projected schema if projection push-down is requested
        let projected_schema = if let Some(indicies) = projection {
            let fieds: Vec<Field> = indicies
                .iter()
                .map(|&i| current_schema.field(i).clone())
                .collect();
            Arc::new(Schema::new(fieds))
        } else {
            current_schema.clone()
        };

        // Create the execution plan for scanning Redis
        let properties = PlanProperties::new(
            EquivalenceProperties::new(projected_schema.clone()),
            Partitioning::UnknownPartitioning(1),
            EmissionType::Both,
            Boundedness::Bounded,
        );
        let exec = RedisScanExec {
            conn: self.conn.clone(),
            key_space: self.key_space.clone(),
            table_name: self.table_name.clone(),
            storage: self.storage.clone(),
            projected_schema,
            projection: projection
                .cloned()
                .unwrap_or_else(|| (0..current_schema.fields().len()).collect()),
            key_column: self.key_column.clone(),
            filters: filters.to_owned(),
            limit,
            properties,
        };
        Ok(Arc::new(exec))
    }

    /// Insert into the table: return an ExecutionPlan that will write input data to Redis.
    async fn insert_into(
        &self,
        _state: &dyn Session,
        input: Arc<dyn ExecutionPlan>,
        insert_op: InsertOp,
    ) -> datafusion::common::Result<Arc<dyn ExecutionPlan>> {
        // TODO: Utilize state
        let sink = RedisSink {
            conn: self.conn.clone(),
            key_space: self.key_space.clone(),
            table_name: self.table_name.clone(),
            storage: self.storage.clone(),
            schema: self.current_schema(),
            insert_op,
            key_column: self.key_column.clone(),
        };
        // Wrap in DataSinkExec to execute insertion. The DataSinkExec will handle combining input and writing.
        Ok(Arc::new(DataSinkExec::new(input, Arc::new(sink), None)))
    }

    /// Delete rows matching the given filters.
    async fn delete_from(
        &self,
        _state: &dyn Session,
        filters: Vec<Expr>,
    ) -> datafusion::common::Result<Arc<dyn ExecutionPlan>> {
        let mut conn = self.conn.try_write().map_err(|e| {
            DataFusionError::Execution(format!(
                "failed to acquire write lock of redis connection: {}",
                e
            ))
        })?;
        let current_schema = self.current_schema();
        let keys = resolve_matching_keys(
            &mut *conn,
            &self.key_space,
            &self.table_name,
            &current_schema,
            &self.key_column,
            &filters,
        )?;
        drop(conn);
        Ok(Arc::new(RedisDmlExec::new(
            self.conn.clone(),
            RedisDmlOp::Delete(keys),
        )))
    }

    /// Update rows matching the given filters with the specified assignments.
    async fn update(
        &self,
        _state: &dyn Session,
        assignments: Vec<(String, Expr)>,
        filters: Vec<Expr>,
    ) -> datafusion::common::Result<Arc<dyn ExecutionPlan>> {
        if assignments.is_empty() {
            return Err(DataFusionError::Plan(
                "UPDATE requires at least one assignment".to_string(),
            ));
        }
        let fields = assignments_to_redis_fields(&assignments, &self.key_column)?;
        let mut conn = self.conn.try_write().map_err(|e| {
            DataFusionError::Execution(format!(
                "failed to acquire write lock of redis connection: {}",
                e
            ))
        })?;
        let current_schema = self.current_schema();
        let keys = resolve_matching_keys(
            &mut *conn,
            &self.key_space,
            &self.table_name,
            &current_schema,
            &self.key_column,
            &filters,
        )?;
        drop(conn);
        Ok(Arc::new(RedisDmlExec::new(
            self.conn.clone(),
            RedisDmlOp::Update(keys, fields),
        )))
    }
}

// ─── Filter / Assignment helpers ────────────────────────────────────────────

/// Resolve which Redis keys match the given filter expressions.
///
/// **Fast path**: if the only filter is `key_column = 'literal'`, construct the key directly.
/// **Slow path**: SCAN all keys, HGETALL each, evaluate filters in-memory.
fn resolve_matching_keys<C: ConnectionLike + Commands>(
    conn: &mut C,
    key_space: &str,
    table_name: &str,
    schema: &SchemaRef,
    key_column: &Option<String>,
    filters: &[Expr],
) -> datafusion::common::Result<Vec<String>> {
    let prefix = RedisRelation::prefix(key_space, table_name);

    // Fast path: key_column equality
    if let Some(key_col) = key_column {
        if let Some(literal_val) = extract_key_column_eq(filters, key_col) {
            return Ok(vec![format!("{}:{}", prefix, literal_val)]);
        }
    }

    // Slow path: scan all keys and filter in-memory
    let pattern = RedisRelation::table_data_key_pattern(key_space, table_name);
    let keys: Vec<String> = conn
        .scan_match(&pattern)
        .map_err(|e| DataFusionError::Execution(format!("Redis SCAN error: {}", e)))?
        .collect::<Result<Vec<String>, _>>()
        .map_err(|e| DataFusionError::Execution(format!("Redis SCAN error: {}", e)))?;

    if filters.is_empty() {
        return Ok(keys);
    }

    let mut matched = Vec::new();
    for key in keys {
        let redis_map: HashMap<String, String> = conn
            .hgetall(&key)
            .map_err(|e| DataFusionError::Execution(format!("Redis HGETALL error: {}", e)))?;

        // Reconstruct the full row including key_column if present
        let mut row = redis_map;
        if let Some(key_col) = key_column {
            let id = key
                .strip_prefix(&format!("{}:", prefix))
                .unwrap_or(&key)
                .to_string();
            row.insert(key_col.clone(), id);
        }

        if evaluate_filters(&row, filters, schema)? {
            matched.push(key);
        }
    }

    Ok(matched)
}

/// If filters contain exactly one `Column(key_col) = Literal(string)`, extract the literal value.
fn extract_key_column_eq(filters: &[Expr], key_col: &str) -> Option<String> {
    if filters.len() != 1 {
        return None;
    }
    match &filters[0] {
        Expr::BinaryExpr(binary) if binary.op == Operator::Eq => {
            match (binary.left.as_ref(), binary.right.as_ref()) {
                (Expr::Column(col), Expr::Literal(scalar, _)) if col.name() == key_col => {
                    scalar_to_string(scalar)
                }
                (Expr::Literal(scalar, _), Expr::Column(col)) if col.name() == key_col => {
                    scalar_to_string(scalar)
                }
                _ => None,
            }
        }
        _ => None,
    }
}

/// Evaluate all filter expressions against a row represented as a HashMap.
fn evaluate_filters(
    row: &HashMap<String, String>,
    filters: &[Expr],
    _schema: &SchemaRef,
) -> datafusion::common::Result<bool> {
    for filter in filters {
        if !evaluate_expr(row, filter)? {
            return Ok(false);
        }
    }
    Ok(true)
}

/// Evaluate a single filter expression against a row.
fn evaluate_expr(row: &HashMap<String, String>, expr: &Expr) -> datafusion::common::Result<bool> {
    match expr {
        Expr::BinaryExpr(binary) => match binary.op {
            Operator::And => {
                Ok(evaluate_expr(row, &binary.left)? && evaluate_expr(row, &binary.right)?)
            }
            Operator::Or => {
                Ok(evaluate_expr(row, &binary.left)? || evaluate_expr(row, &binary.right)?)
            }
            Operator::Eq
            | Operator::NotEq
            | Operator::Lt
            | Operator::LtEq
            | Operator::Gt
            | Operator::GtEq => {
                let left_val = resolve_value(row, &binary.left)?;
                let right_val = resolve_value(row, &binary.right)?;
                let cmp = left_val.cmp(&right_val);
                let result = match binary.op {
                    Operator::Eq => cmp == std::cmp::Ordering::Equal,
                    Operator::NotEq => cmp != std::cmp::Ordering::Equal,
                    Operator::Lt => cmp == std::cmp::Ordering::Less,
                    Operator::LtEq => cmp != std::cmp::Ordering::Greater,
                    Operator::Gt => cmp == std::cmp::Ordering::Greater,
                    Operator::GtEq => cmp != std::cmp::Ordering::Less,
                    _ => unreachable!(),
                };
                Ok(result)
            }
            _ => Err(DataFusionError::Plan(format!(
                "Unsupported filter operator for Redis: {:?}",
                binary.op
            ))),
        },
        _ => Err(DataFusionError::Plan(format!(
            "Unsupported filter expression for Redis: {expr}"
        ))),
    }
}

/// Resolve the string value of an expression in the context of a row.
fn resolve_value(row: &HashMap<String, String>, expr: &Expr) -> datafusion::common::Result<String> {
    match expr {
        Expr::Column(col) => Ok(row.get(col.name()).cloned().unwrap_or_default()),
        Expr::Literal(scalar, _) => scalar_to_string(scalar).ok_or_else(|| {
            DataFusionError::Plan(format!("Unsupported literal type for Redis: {scalar}"))
        }),
        _ => Err(DataFusionError::Plan(format!(
            "Unsupported expression in Redis filter: {expr}"
        ))),
    }
}

/// Convert a ScalarValue to a String.
fn scalar_to_string(scalar: &datafusion::common::ScalarValue) -> Option<String> {
    use datafusion::common::ScalarValue;
    match scalar {
        ScalarValue::Utf8(Some(s)) | ScalarValue::LargeUtf8(Some(s)) => Some(s.clone()),
        ScalarValue::Int8(Some(v)) => Some(v.to_string()),
        ScalarValue::Int16(Some(v)) => Some(v.to_string()),
        ScalarValue::Int32(Some(v)) => Some(v.to_string()),
        ScalarValue::Int64(Some(v)) => Some(v.to_string()),
        ScalarValue::UInt8(Some(v)) => Some(v.to_string()),
        ScalarValue::UInt16(Some(v)) => Some(v.to_string()),
        ScalarValue::UInt32(Some(v)) => Some(v.to_string()),
        ScalarValue::UInt64(Some(v)) => Some(v.to_string()),
        ScalarValue::Float32(Some(v)) => Some(v.to_string()),
        ScalarValue::Float64(Some(v)) => Some(v.to_string()),
        ScalarValue::Boolean(Some(v)) => Some(v.to_string()),
        _ => None,
    }
}

/// Convert UPDATE assignments to Redis hash field-value pairs.
fn assignments_to_redis_fields(
    assignments: &[(String, Expr)],
    key_column: &Option<String>,
) -> datafusion::common::Result<Vec<(String, String)>> {
    let mut fields = Vec::with_capacity(assignments.len());
    for (col, expr) in assignments {
        if let Some(key_col) = key_column {
            if col == key_col {
                return Err(DataFusionError::Plan(format!(
                    "Cannot update key column '{}'; this would require deleting and re-inserting the row",
                    key_col
                )));
            }
        }
        let value = match expr {
            Expr::Literal(scalar, _) => scalar_to_string(scalar).ok_or_else(|| {
                DataFusionError::Plan(format!(
                    "Unsupported literal type for Redis UPDATE: {scalar}"
                ))
            })?,
            _ => {
                return Err(DataFusionError::Plan(format!(
                    "Redis UPDATE only supports literal values, got: {expr}"
                )));
            }
        };
        fields.push((col.clone(), value));
    }
    Ok(fields)
}

// ─── DML execution plan ─────────────────────────────────────────────────────

/// The kind of DML operation to execute.
#[derive(Debug, Clone)]
enum RedisDmlOp {
    /// Delete the given Redis keys.
    Delete(Vec<String>),
    /// Update the given Redis keys with field-value pairs.
    Update(Vec<String>, Vec<(String, String)>),
}

/// A leaf [`ExecutionPlan`] that executes a Redis DML operation and returns
/// a single row `{ count: u64 }` with the number of affected keys.
#[derive(Derivative)]
#[derivative(Debug)]
struct RedisDmlExec<C>
where
    C: ConnectionLike + Commands + Send + Sync + 'static,
{
    #[derivative(Debug = "ignore")]
    conn: Arc<RwLock<C>>,
    op: RedisDmlOp,
    schema: SchemaRef,
    properties: PlanProperties,
}

impl<C> RedisDmlExec<C>
where
    C: ConnectionLike + Commands + Send + Sync + 'static,
{
    fn new(conn: Arc<RwLock<C>>, op: RedisDmlOp) -> Self {
        let schema = Arc::new(Schema::new(vec![Field::new(
            "count",
            DataType::UInt64,
            false,
        )]));
        let properties = PlanProperties::new(
            EquivalenceProperties::new(schema.clone()),
            Partitioning::UnknownPartitioning(1),
            EmissionType::Final,
            Boundedness::Bounded,
        );
        Self {
            conn,
            op,
            schema,
            properties,
        }
    }
}

impl<C> DisplayAs for RedisDmlExec<C>
where
    C: ConnectionLike + Commands + Send + Sync + 'static,
{
    fn fmt_as(&self, t: DisplayFormatType, f: &mut Formatter) -> fmt::Result {
        match t {
            DisplayFormatType::Default => write!(f, "RedisDmlExec(op={:?})", self.op),
            _ => write!(f, "RedisDmlExec"),
        }
    }
}

impl<C> ExecutionPlan for RedisDmlExec<C>
where
    C: ConnectionLike + Commands + Send + Sync + 'static,
{
    fn name(&self) -> &str {
        "RedisDmlExec"
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn schema(&self) -> SchemaRef {
        self.schema.clone()
    }

    fn properties(&self) -> &PlanProperties {
        &self.properties
    }

    fn children(&self) -> Vec<&Arc<dyn ExecutionPlan>> {
        vec![]
    }

    fn with_new_children(
        self: Arc<Self>,
        _children: Vec<Arc<dyn ExecutionPlan>>,
    ) -> datafusion::error::Result<Arc<dyn ExecutionPlan>> {
        Ok(self)
    }

    fn execute(
        &self,
        _partition: usize,
        _context: Arc<TaskContext>,
    ) -> datafusion::error::Result<SendableRecordBatchStream> {
        let mut conn = self.conn.try_write().map_err(|e| {
            DataFusionError::Execution(format!(
                "failed to acquire write lock of redis connection: {}",
                e
            ))
        })?;

        let affected = match &self.op {
            RedisDmlOp::Delete(keys) => {
                if keys.is_empty() {
                    0u64
                } else {
                    let _: () = conn.del(keys).map_err(|e| {
                        DataFusionError::Execution(format!("Redis DEL error: {}", e))
                    })?;
                    keys.len() as u64
                }
            }
            RedisDmlOp::Update(keys, fields) => {
                let field_refs: Vec<(&str, &str)> = fields
                    .iter()
                    .map(|(k, v)| (k.as_str(), v.as_str()))
                    .collect();
                for key in keys {
                    let _: () = conn.hset_multiple(key, &field_refs).map_err(|e| {
                        DataFusionError::Execution(format!("Redis HSET error: {}", e))
                    })?;
                }
                keys.len() as u64
            }
        };

        let batch = create_count_batch(affected)?;
        Ok(Box::pin(MemoryStream::try_new(
            vec![batch],
            self.schema.clone(),
            None,
        )?))
    }
}

/// Create a single-row RecordBatch with `{ count: u64 }`.
fn create_count_batch(count: u64) -> datafusion::common::Result<RecordBatch> {
    let schema = Arc::new(Schema::new(vec![Field::new(
        "count",
        DataType::UInt64,
        false,
    )]));
    let array: UInt64Array = vec![count].into();
    RecordBatch::try_new(schema, vec![Arc::new(array)]).map_err(DataFusionError::from)
}

// ─── Scan execution plan ────────────────────────────────────────────────────

/// ExecutionPlan for scanning a Redis table (reads Redis hashes and outputs Arrow RecordBatches).
#[derive(Derivative)]
#[derivative(Debug)]
struct RedisScanExec<C>
where
    C: ConnectionLike + Commands + Send + Sync + 'static,
{
    #[derivative(Debug = "ignore")]
    conn: Arc<RwLock<C>>,
    key_space: String,
    table_name: String,
    storage: RedisStorage,
    projected_schema: SchemaRef,
    projection: Vec<usize>,
    key_column: Option<String>,
    filters: Vec<Expr>,
    limit: Option<usize>,
    properties: PlanProperties,
}

impl<C> RedisScanExec<C>
where
    C: ConnectionLike + Commands + Send + Sync + 'static,
{
    /// Helper to gather all record batches from Redis for a given partition (node).
    fn fetch_partition(&self, _partition_idx: usize) -> datafusion::common::Result<RecordBatch> {
        // TODO: Utilize partition_idx
        let pattern = RedisRelation::table_data_key_pattern(&self.key_space, &self.table_name);

        // Prepare builders for each projected column
        let mut builders: Vec<StringBuilder> = self
            .projected_schema
            .fields()
            .iter()
            .map(|_| StringBuilder::new())
            .collect();

        // Scan through all keys for this partition
        let mut conn_write = self.conn.try_write().map_err(|e| {
            DataFusionError::Execution(format!(
                "failed to acquire write lock of redis connection {}",
                e
            ))
        })?;
        let keys: Vec<String> = conn_write
            .scan_match(&pattern)
            .map_err(|e| DataFusionError::Execution(format!("Redis SCAN error: {}", e)))?
            .collect::<Result<Vec<String>, _>>()
            .map_err(|e| DataFusionError::Execution(format!("Redis SCAN error: {}", e)))?;

        let mut count: usize = 0;
        for key in &keys {
            // Optionally apply a limit to stop early
            if let Some(max) = self.limit {
                if count >= max {
                    break;
                }
            }

            // Fetch the hash fields for this key
            let redis_map: HashMap<String, String> = conn_write
                .hgetall(key)
                .map_err(|e| DataFusionError::Execution(format!("Redis HGETALL error: {}", e)))?;
            // Extract the key column value from the Redis key suffix (e.g. "mydb:products:PROD001" → "PROD001")
            let key_col_value = self
                .key_column
                .as_ref()
                .map(|_| RedisRelation::table_key(&pattern, key).to_string());
            // TODO: Add other RedisStorage support other than hash
            for (j, field) in self.projected_schema.fields().iter().enumerate() {
                let field_name = field.name();
                let cell_value = if let Some(val) = redis_map.get(field_name) {
                    val.to_string()
                } else if self.key_column.as_deref() == Some(field_name) {
                    // Field is the key column — value is extracted from the Redis key suffix
                    key_col_value.clone().unwrap_or_default()
                } else {
                    "".to_string()
                };
                builders[j].append_value(cell_value);
            }
            count += 1;
        }

        // Finish building arrays and create a RecordBatch
        let arrays: Vec<ArrayRef> = builders
            .into_iter()
            .map(|mut b| Arc::new(b.finish()) as ArrayRef)
            .collect();

        if arrays.is_empty() {
            // DataFusion pushes an empty projection for `count(*)`-style queries
            // where only the row count matters. `RecordBatch::try_new` rejects a
            // zero-column batch unless we supply the row count explicitly, so
            // pass `count` through so aggregates see the real input cardinality.
            let options = RecordBatchOptions::new().with_row_count(Some(count));
            RecordBatch::try_new_with_options(self.projected_schema.clone(), arrays, &options)
                .map_err(|e| {
                    DataFusionError::Execution(format!("Error building RecordBatch: {}", e))
                })
        } else {
            RecordBatch::try_new(self.projected_schema.clone(), arrays).map_err(|e| {
                DataFusionError::Execution(format!("Error building RecordBatch: {}", e))
            })
        }
    }
}

impl<C> ExecutionPlan for RedisScanExec<C>
where
    C: ConnectionLike + Commands + Send + Sync + 'static,
{
    fn as_any(&self) -> &dyn Any {
        self
    }

    fn schema(&self) -> SchemaRef {
        self.projected_schema.clone()
    }

    fn name(&self) -> &str {
        "redis execution"
    }

    fn properties(&self) -> &PlanProperties {
        &self.properties
    }

    fn children(&self) -> Vec<&Arc<dyn ExecutionPlan>> {
        vec![]
    }

    fn with_new_children(
        self: Arc<Self>,
        _children: Vec<Arc<dyn ExecutionPlan>>,
    ) -> datafusion::error::Result<Arc<dyn ExecutionPlan>> {
        Ok(Arc::new(RedisScanExec {
            conn: self.conn.clone(),
            key_space: self.key_space.clone(),
            table_name: self.table_name.clone(),
            storage: self.storage.clone(),
            projected_schema: Arc::clone(&self.projected_schema),
            projection: self.projection.clone(),
            key_column: self.key_column.clone(),
            filters: self.filters.clone(),
            limit: self.limit,
            properties: self.properties.clone(),
        }))
    }

    fn execute(
        &self,
        partition: usize,
        _context: Arc<TaskContext>,
    ) -> datafusion::error::Result<SendableRecordBatchStream> {
        // Fetch the data for the given partition (synchronously, in this implementation).
        // TODO: Utilize context
        let batch = self.fetch_partition(partition)?;
        let schema = self.schema();
        let output = vec![batch];
        // TODO: Find proper ways to create SendableRecordBatchStream, find proper projections
        Ok(Box::pin(MemoryStream::try_new(output, schema, None)?))
    }
}

impl<C> DisplayAs for RedisScanExec<C>
where
    C: ConnectionLike + Commands + Send + Sync + 'static,
{
    fn fmt_as(&self, t: DisplayFormatType, f: &mut Formatter) -> fmt::Result {
        match t {
            DisplayFormatType::Default => write!(
                f,
                "RedisScanExec: table={}, projected_cols={:?}, filters={:?}, limit={:?}",
                self.table_name, self.projection, self.filters, self.limit
            ),
            _ => write!(f, "RedisScanExec"),
        }
    }
}

// ─── DataSink for INSERT ────────────────────────────────────────────────────

/// DataSink implementation for writing data into Redis.
#[derive(Derivative)]
#[derivative(Debug)]
struct RedisSink<C>
where
    C: ConnectionLike + Commands + Send + Sync + 'static,
{
    #[derivative(Debug = "ignore")]
    conn: Arc<RwLock<C>>,
    key_space: String,
    table_name: String,
    storage: RedisStorage,
    schema: SchemaRef,
    insert_op: InsertOp,
    key_column: Option<String>,
}

impl<C> DisplayAs for RedisSink<C>
where
    C: ConnectionLike + Commands + Send + Sync + 'static,
{
    fn fmt_as(&self, t: DisplayFormatType, f: &mut Formatter<'_>) -> fmt::Result {
        match t {
            // a "compact" or default one‐liner
            DisplayFormatType::Default => {
                write!(f, "RedisSink({})", self.table_name)
            }

            // the "verbose" form, dumping every field
            DisplayFormatType::Verbose => {
                write!(
                    f,
                    "RedisSink {{ \
                     key_space:    {:?}, \
                     table:     \"{}\", \
                     storage:   {:?}, \
                     schema:    {:?}, \
                     insert_op: {:?}, \
                     key_col:   {:?} \
                     }}",
                    self.key_space,
                    self.table_name,
                    self.storage,
                    self.schema,
                    self.insert_op,
                    self.key_column
                )
            }

            // tree render format (new in datafusion 50)
            DisplayFormatType::TreeRender => {
                write!(f, "RedisSink({})", self.table_name)
            }
        }
    }
}

#[async_trait]
impl<C> DataSink for RedisSink<C>
where
    C: ConnectionLike + Commands + Send + Sync + 'static,
{
    fn as_any(&self) -> &dyn Any {
        self
    }

    fn schema(&self) -> &SchemaRef {
        &self.schema
    }

    /// Consume a stream of RecordBatches and write all rows to Redis. Returns the count of rows written.
    async fn write_all(
        &self,
        mut data: SendableRecordBatchStream,
        _context: &Arc<TaskContext>,
    ) -> datafusion::common::Result<u64> {
        // TODO: Utilize context, intergrate underneath scaler, aggregate, window function, etc

        let prefix = RedisRelation::prefix(&self.key_space, &self.table_name);

        // If Overwrite or Replace, delete existing keys for this table first
        if self.insert_op == InsertOp::Overwrite || self.insert_op == InsertOp::Replace {
            let pattern = RedisRelation::table_data_key_pattern(&self.key_space, &self.table_name);
            let mut conn_write = self.conn.try_write().map_err(|e| {
                DataFusionError::Execution(format!(
                    "failed to acquire write lock of redis connection: {}",
                    e
                ))
            })?;
            // Use SCAN to find keys and delete them
            let mut iter: Iter<String> = conn_write
                .scan_match(&pattern)
                .map_err(|e| DataFusionError::Execution(format!("Redis SCAN error: {}", e)))?;
            let mut del_keys: Vec<String> = vec![];
            // TODO: Try to add a batch size here, not delete everything at once.
            while let Some(k_result) = iter.next() {
                let k = k_result
                    .map_err(|e| DataFusionError::Execution(format!("Redis SCAN error: {}", e)))?;
                del_keys.push(k);
            }
            if !del_keys.is_empty() {
                let _: () = conn_write
                    .del(&del_keys)
                    .map_err(|e| DataFusionError::Execution(format!("Redis SCAN error: {}", e)))?;
            }
        }

        // Iterate through all record batches and write each row
        let mut total_rows: u64 = 0;
        while let Some(batch) = data.try_next().await? {
            // TODO: Implement write with other RdisStorage type, when added
            let records = batch;
            let num_rows = records.num_rows();
            let num_cols = records.num_columns();
            // Pre-extract arrays for efficiency
            let columns: Vec<ArrayRef> = (0..num_cols).map(|i| records.column(i).clone()).collect();
            // Determine index of key_column if any (to avoid storing it as field if it's the key).
            let key_col_index = self
                .key_column
                .as_ref()
                .and_then(|col_name| records.schema().index_of(col_name).ok());

            for row_idx in 0..num_rows {
                // Determine the Redis key for this row
                let id_value = if let Some(idx) = key_col_index {
                    // Use the value of the key column as the ID
                    array_value_to_string(&columns[idx], row_idx)?
                } else {
                    Uuid::new_v4().to_string()
                };
                let redis_key = format!("{}:{}", prefix, id_value);
                let mut fileds = vec![];
                let schema = records.schema();
                for col_idx in 0..num_cols {
                    if Some(col_idx) == key_col_index {
                        continue;
                    }
                    let field_name = schema.field(col_idx).name();
                    let value_str = array_value_to_string(&columns[col_idx], row_idx)?;
                    fileds.push((field_name.as_str(), value_str))
                }
                let mut conn_write = self.conn.try_write().map_err(|e| {
                    DataFusionError::Execution(format!(
                        "failed to acquire write lock of redis connection: {}",
                        e
                    ))
                })?;
                let _: () = conn_write.hset_multiple(&redis_key, &fileds).map_err(|e| {
                    DataFusionError::Execution(format!("Failed to HSET row to Redis: {}", e))
                })?;
            }
            total_rows += num_rows as u64;
        }

        Ok(total_rows)
    }
}

// ─── Utilities ──────────────────────────────────────────────────────────────

/// Helper function to convert a value at a given row of an Arrow array to a string.
/// TODO: Move this to a common place instead of only for redis
fn array_value_to_string(array: &ArrayRef, row: usize) -> datafusion::common::Result<String> {
    if array.is_null(row) {
        return Ok("".to_string());
    }
    // Match on common data types and convert to string
    let str = match array.data_type() {
        DataType::Utf8 => {
            let str_arr = as_string_array(array);
            str_arr.value(row).to_string()
        }
        DataType::LargeUtf8 => {
            let str_arr = as_largestring_array(array);
            str_arr.value(row).to_string()
        }
        DataType::Int64 => {
            let int_arr = as_int64_array(array).inspect_err(|e| {
                DataFusionError::Execution(format!("DataType conversion error {}", e));
            })?;
            int_arr.value(row).to_string()
        }
        DataType::Int32 => {
            let int_arr = as_int32_array(array).inspect_err(|e| {
                DataFusionError::Execution(format!("DataType conversion error {}", e));
            })?;
            int_arr.value(row).to_string()
        }
        DataType::Int16 => {
            let int_arr = as_int16_array(array).inspect_err(|e| {
                DataFusionError::Execution(format!("DataType conversion error {}", e));
            })?;
            int_arr.value(row).to_string()
        }
        DataType::Int8 => {
            let int_arr = as_int8_array(array).inspect_err(|e| {
                DataFusionError::Execution(format!("DataType conversion error {}", e));
            })?;
            int_arr.value(row).to_string()
        }
        DataType::UInt64 => {
            let int_arr = as_uint64_array(array).inspect_err(|e| {
                DataFusionError::Execution(format!("DataType conversion error {}", e));
            })?;
            int_arr.value(row).to_string()
        }
        DataType::UInt32 => {
            let int_arr = as_uint32_array(array).inspect_err(|e| {
                DataFusionError::Execution(format!("DataType conversion error {}", e));
            })?;
            int_arr.value(row).to_string()
        }
        DataType::UInt16 => {
            let int_arr = as_uint16_array(array).inspect_err(|e| {
                DataFusionError::Execution(format!("DataType conversion error {}", e));
            })?;
            int_arr.value(row).to_string()
        }
        DataType::UInt8 => {
            let int_arr = as_uint8_array(array).inspect_err(|e| {
                DataFusionError::Execution(format!("DataType conversion error {}", e));
            })?;
            int_arr.value(row).to_string()
        }
        DataType::Float64 => {
            let float_arr = as_float64_array(array).inspect_err(|e| {
                DataFusionError::Execution(format!("DataType conversion error {}", e));
            })?;
            float_arr.value(row).to_string()
        }
        DataType::Float32 => {
            let float_arr = as_float32_array(array).inspect_err(|e| {
                DataFusionError::Execution(format!("DataType conversion error {}", e));
            })?;
            float_arr.value(row).to_string()
        }
        DataType::Float16 => {
            let float_arr = as_float16_array(array).inspect_err(|e| {
                DataFusionError::Execution(format!("DataType conversion error {}", e));
            })?;
            float_arr.value(row).to_string()
        }
        DataType::Boolean => {
            let bool_arr = as_boolean_array(array);
            bool_arr.value(row).to_string()
        }
        DataType::Binary => {
            let bin_array = as_binary_array(array).inspect_err(|e| {
                DataFusionError::Execution(format!("DataType conversion error {}", e));
            })?;
            let bytes = bin_array.value(row);
            hex::encode(bytes)
        }
        DataType::LargeBinary => {
            let bin_array = as_large_binary_array(array).inspect_err(|e| {
                DataFusionError::Execution(format!("DataType conversion error {}", e));
            })?;
            let bytes = bin_array.value(row);
            hex::encode(bytes)
        }
        _other => {
            // Fallback: use debug representation of ScalarValue
            // TODO: Add conversion for all other types
            format!("{:?}", array.slice(row, 1).as_ref())
        }
    };

    Ok(str)
}

// ─── Registration ───────────────────────────────────────────────────────

/// Register a Redis hash table as a DataFusion table.
///
/// # Options
/// * `key_space` - Namespace prefix for Redis keys (required)
/// * `table` - Table name within the key space (required)
/// * `key_column` - Column to use as the Redis key suffix (optional)
pub fn register_redis_tables(
    session_ctx: &mut datafusion::prelude::SessionContext,
    name: &str,
    connection_string: &str,
    options: Option<&HashMap<String, String>>,
) -> Result<()> {
    tracing::info!(
        "Registering Redis table: {} with connection: {}",
        name,
        connection_string
    );

    let opts = options.ok_or_else(|| {
        anyhow::anyhow!(
            "Redis data source '{}' requires options (key_space, table)",
            name
        )
    })?;

    let key_space = opts.get("key_space").ok_or_else(|| {
        anyhow::anyhow!("Redis data source '{}' requires 'key_space' option", name)
    })?;

    let table = opts
        .get("table")
        .ok_or_else(|| anyhow::anyhow!("Redis data source '{}' requires 'table' option", name))?;

    let key_column = opts.get("key_column").cloned();
    let columns = opts.get("columns").map(|s| {
        s.split(',')
            .map(|c| c.trim().to_string())
            .filter(|c| !c.is_empty())
            .collect::<Vec<String>>()
    });

    let client = redis::Client::open(connection_string)
        .map_err(|e| anyhow::anyhow!("Failed to create Redis client for '{}': {}", name, e))?;

    let conn = client
        .get_connection()
        .map_err(|e| anyhow::anyhow!("Failed to connect to Redis for '{}': {}", name, e))?;

    let redis_table = RedisTable::new(
        conn,
        key_space.clone(),
        table.clone(),
        RedisStorage::Hash,
        key_column,
        columns,
    )?;

    session_ctx
        .register_table(name, Arc::new(redis_table))
        .map_err(|e| anyhow::anyhow!("Failed to register Redis table '{}': {}", name, e))?;

    tracing::info!("Successfully registered Redis table: {}", name);
    Ok(())
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;

    use super::*;
    use arrow::{
        array::{Float64Array, Int32Array, StringArray, UInt64Array},
        datatypes::{DataType, Field, Schema},
        record_batch::RecordBatch,
        util::pretty,
    };
    use datafusion::{prelude::SessionContext, test_util::bounded_stream};
    use redis::Value;
    use redis_test::{MockCmd, MockRedisConnection};

    /// Mock for the SCAN + HGETALL calls in the "person" read test.
    fn make_person_mock(prefix: &str) -> MockRedisConnection {
        // SCAN → return two keys as BulkStrings in an Array
        let scan = MockCmd::new(
            redis::cmd("SCAN")
                .arg(0)
                .arg("MATCH")
                .arg(format!("{prefix}:*"))
                .clone(),
            Ok(Value::Array(vec![
                Value::BulkString(format!("{prefix}:1").into_bytes()),
                Value::BulkString(format!("{prefix}:2").into_bytes()),
            ])),
        );

        let scan_dup = MockCmd::new(
            redis::cmd("SCAN")
                .arg(0)
                .arg("MATCH")
                .arg(format!("{prefix}:*"))
                .clone(),
            Ok(Value::Array(vec![
                Value::BulkString(format!("{prefix}:1").into_bytes()),
                Value::BulkString(format!("{prefix}:2").into_bytes()),
            ])),
        );

        // HGETALL itest:person:1 → alternating BulkString entries
        let h1 = MockCmd::new(
            redis::cmd("HGETALL").arg(format!("{prefix}:1")).clone(),
            Ok(Value::Array(vec![
                Value::BulkString(b"id".to_vec()),
                Value::BulkString(b"1".to_vec()),
                Value::BulkString(b"name".to_vec()),
                Value::BulkString(b"Alice".to_vec()),
                Value::BulkString(b"age".to_vec()),
                Value::BulkString(b"30".to_vec()),
                Value::BulkString(b"city".to_vec()),
                Value::BulkString(b"seattle".to_vec()),
            ])),
        );

        let h1_dup = MockCmd::new(
            redis::cmd("HGETALL").arg(format!("{prefix}:1")).clone(),
            Ok(Value::Array(vec![
                Value::BulkString(b"id".to_vec()),
                Value::BulkString(b"1".to_vec()),
                Value::BulkString(b"name".to_vec()),
                Value::BulkString(b"Alice".to_vec()),
                Value::BulkString(b"age".to_vec()),
                Value::BulkString(b"30".to_vec()),
                Value::BulkString(b"city".to_vec()),
                Value::BulkString(b"seattle".to_vec()),
            ])),
        );

        // HGETALL itest:person:2
        let h2 = MockCmd::new(
            redis::cmd("HGETALL").arg(format!("{prefix}:2")).clone(),
            Ok(Value::Array(vec![
                Value::BulkString(b"id".to_vec()),
                Value::BulkString(b"2".to_vec()),
                Value::BulkString(b"name".to_vec()),
                Value::BulkString(b"Bob".to_vec()),
                Value::BulkString(b"age".to_vec()),
                Value::BulkString(b"35".to_vec()),
                Value::BulkString(b"city".to_vec()),
                Value::BulkString(b"Denver".to_vec()),
            ])),
        );

        MockRedisConnection::new(vec![scan, h1, scan_dup, h1_dup, h2])
    }

    #[tokio::test]
    async fn manual_write_then_table_read_mock() -> Result<()> {
        let keyspace = "itest";
        let table = "person";
        let prefix = format!("{keyspace}:{table}");

        // inject our mock
        let mock_conn = make_person_mock(&prefix);
        let rtable = RedisTable::new(
            mock_conn,
            keyspace.to_string(),
            table.to_string(),
            RedisStorage::Hash,
            None,
            None,
        )?;

        let ctx = SessionContext::new();
        ctx.register_table(table, Arc::new(rtable))?;

        let df = ctx
            .sql("SELECT id, name, age, city FROM person ORDER BY id")
            .await?;
        let batches = df.collect().await?;
        println!("{}", pretty::pretty_format_batches(&batches).unwrap());

        assert_eq!(batches.len(), 1);
        let batch = &batches[0];
        assert_eq!(batch.num_rows(), 2);
        let names = batch
            .column(1)
            .as_any()
            .downcast_ref::<StringArray>()
            .unwrap();
        let ages = batch
            .column(2)
            .as_any()
            .downcast_ref::<StringArray>()
            .unwrap();
        assert_eq!(names.value(0), "Alice");
        assert_eq!(names.value(1), "Bob");
        assert_eq!(ages.value(0), "30");
        assert_eq!(ages.value(1), "35");
        Ok(())
    }

    /// Mock the SCAN (empty) + HSET calls in the "orders" write test.
    fn make_orders_write_mock(prefix: &str) -> MockRedisConnection {
        // start with an empty SCAN so purge_prefix thinks nothing to delete
        let scan = MockCmd::new(
            redis::cmd("SCAN")
                .arg(0)
                .arg("MATCH")
                .arg(format!("{prefix}:*"))
                .clone(),
            Ok(Value::Array(vec![])),
        );
        // HSET prefix:A1 ...
        let h1 = MockCmd::new(
            redis::cmd("HMSET")
                .arg(format!("{prefix}:A1"))
                .arg("item")
                .arg("Widget")
                .arg("qty")
                .arg("10")
                .arg("price")
                .arg("19.99")
                .clone(),
            Ok(Value::Okay),
        );
        // HSET prefix:A2 ...
        let h2 = MockCmd::new(
            redis::cmd("HMSET")
                .arg(format!("{prefix}:A2"))
                .arg("item")
                .arg("Gadget")
                .arg("qty")
                .arg("20")
                .arg("price")
                .arg("29.95")
                .clone(),
            Ok(Value::Okay),
        );
        MockRedisConnection::new(vec![scan, h1, h2])
    }

    /// Mock the HGETALL calls for manual verification in the "orders" test.
    fn make_orders_read_mock(prefix: &str) -> MockRedisConnection {
        // HGETALL prefix:A1
        let h1 = MockCmd::new(
            redis::cmd("HGETALL").arg(format!("{prefix}:A1")).clone(),
            Ok(Value::Array(vec![
                Value::BulkString(b"item".to_vec()),
                Value::BulkString(b"Widget".to_vec()),
                Value::BulkString(b"qty".to_vec()),
                Value::BulkString(b"10".to_vec()),
                Value::BulkString(b"price".to_vec()),
                Value::BulkString(b"19.99".to_vec()),
            ])),
        );
        // HGETALL prefix:A2
        let h2 = MockCmd::new(
            redis::cmd("HGETALL").arg(format!("{prefix}:A2")).clone(),
            Ok(Value::Array(vec![
                Value::BulkString(b"item".to_vec()),
                Value::BulkString(b"Gadget".to_vec()),
                Value::BulkString(b"qty".to_vec()),
                Value::BulkString(b"20".to_vec()),
                Value::BulkString(b"price".to_vec()),
                Value::BulkString(b"29.95".to_vec()),
            ])),
        );
        MockRedisConnection::new(vec![h1, h2])
    }

    #[tokio::test]
    async fn table_write_then_manual_read_mock() -> Result<()> {
        let keyspace = "itest";
        let table = "orders";
        let prefix = format!("{keyspace}:{table}");

        let schema = Arc::new(Schema::new(vec![
            Field::new("order_id", DataType::Utf8, false),
            Field::new("item", DataType::Utf8, true),
            Field::new("qty", DataType::Int32, true),
            Field::new("price", DataType::Float64, true),
        ]));
        let batch = RecordBatch::try_new(
            schema.clone(),
            vec![
                Arc::new(StringArray::from(vec!["A1", "A2"])) as _,
                Arc::new(StringArray::from(vec!["Widget", "Gadget"])) as _,
                Arc::new(Int32Array::from(vec![10, 20])) as _,
                Arc::new(Float64Array::from(vec![19.99, 29.95])) as _,
            ],
        )?;

        // 1) write
        let write_conn = make_orders_write_mock(&prefix);
        let sink = RedisSink {
            conn: Arc::new(RwLock::new(write_conn)),
            key_space: keyspace.to_string(),
            table_name: table.into(),
            storage: RedisStorage::Hash,
            schema: schema.clone(),
            insert_op: InsertOp::Overwrite,
            key_column: Some("order_id".into()),
        };
        let stream = bounded_stream(batch, 1);
        let rows_written = sink
            .write_all(stream, &Arc::new(TaskContext::default()))
            .await?;
        assert_eq!(rows_written, 2);

        // 2) read back manually
        let mut read_conn = make_orders_read_mock(&prefix);
        let k1: HashMap<String, String> = read_conn.hgetall(format!("{prefix}:A1"))?;
        assert_eq!(k1.get("item").unwrap(), "Widget");
        assert_eq!(k1.get("qty").unwrap(), "10");
        assert_eq!(k1.get("price").unwrap(), "19.99");

        let k2: HashMap<String, String> = read_conn.hgetall(format!("{prefix}:A2"))?;
        assert_eq!(k2.get("item").unwrap(), "Gadget");
        assert_eq!(k2.get("qty").unwrap(), "20");
        assert_eq!(k2.get("price").unwrap(), "29.95");

        Ok(())
    }

    // ─── DELETE tests ───────────────────────────────────────────────────────

    #[tokio::test]
    async fn test_delete_by_key_column() -> Result<()> {
        let keyspace = "itest";
        let table = "person";
        let prefix = format!("{keyspace}:{table}");

        // Mock: DEL itest:person:1 -> returns 1
        let del = MockCmd::new(
            redis::cmd("DEL").arg(format!("{prefix}:1")).clone(),
            Ok(Value::Int(1)),
        );
        let mock_conn = MockRedisConnection::new(vec![del]);

        let schema = Arc::new(Schema::new(vec![
            Field::new("id", DataType::Utf8, false),
            Field::new("name", DataType::Utf8, true),
        ]));

        let conn = Arc::new(RwLock::new(mock_conn));

        // Use the fast path: key_column = "id", filter = id = '1'
        let key_column = Some("id".to_string());
        let filters = vec![Expr::BinaryExpr(datafusion::logical_expr::BinaryExpr {
            left: Box::new(Expr::Column(datafusion::common::Column::new_unqualified(
                "id",
            ))),
            op: Operator::Eq,
            right: Box::new(Expr::Literal(
                datafusion::common::ScalarValue::Utf8(Some("1".to_string())),
                None,
            )),
        })];

        let keys = {
            let mut conn_guard = conn.try_write().unwrap();
            resolve_matching_keys(
                &mut *conn_guard,
                keyspace,
                table,
                &schema,
                &key_column,
                &filters,
            )?
        };

        assert_eq!(keys, vec![format!("{prefix}:1")]);

        let exec = RedisDmlExec::new(conn, RedisDmlOp::Delete(keys));
        let batch = exec.execute(0, Arc::new(TaskContext::default()))?;

        // Collect the stream
        use futures::TryStreamExt;
        let batches: Vec<RecordBatch> = batch.try_collect().await?;
        assert_eq!(batches.len(), 1);
        let count_arr = batches[0]
            .column(0)
            .as_any()
            .downcast_ref::<UInt64Array>()
            .unwrap();
        assert_eq!(count_arr.value(0), 1);

        Ok(())
    }

    #[tokio::test]
    async fn test_delete_all_rows() -> Result<()> {
        let keyspace = "itest";
        let table = "person";
        let prefix = format!("{keyspace}:{table}");

        // Mock: SCAN returns 2 keys, then DEL both
        let scan = MockCmd::new(
            redis::cmd("SCAN")
                .arg(0)
                .arg("MATCH")
                .arg(format!("{prefix}:*"))
                .clone(),
            Ok(Value::Array(vec![
                Value::BulkString(format!("{prefix}:1").into_bytes()),
                Value::BulkString(format!("{prefix}:2").into_bytes()),
            ])),
        );
        let del = MockCmd::new(
            redis::cmd("DEL")
                .arg(format!("{prefix}:1"))
                .arg(format!("{prefix}:2"))
                .clone(),
            Ok(Value::Int(2)),
        );
        let mock_conn = MockRedisConnection::new(vec![scan, del]);

        let schema = Arc::new(Schema::new(vec![
            Field::new("id", DataType::Utf8, false),
            Field::new("name", DataType::Utf8, true),
        ]));

        let conn = Arc::new(RwLock::new(mock_conn));
        let key_column: Option<String> = None;
        let filters: Vec<Expr> = vec![];

        let keys = {
            let mut conn_guard = conn.try_write().unwrap();
            resolve_matching_keys(
                &mut *conn_guard,
                keyspace,
                table,
                &schema,
                &key_column,
                &filters,
            )?
        };

        assert_eq!(keys.len(), 2);

        let exec = RedisDmlExec::new(conn, RedisDmlOp::Delete(keys));
        let batch = exec.execute(0, Arc::new(TaskContext::default()))?;
        let batches: Vec<RecordBatch> = batch.try_collect().await?;
        let count_arr = batches[0]
            .column(0)
            .as_any()
            .downcast_ref::<UInt64Array>()
            .unwrap();
        assert_eq!(count_arr.value(0), 2);

        Ok(())
    }

    // ─── UPDATE tests ───────────────────────────────────────────────────────

    #[tokio::test]
    async fn test_update_by_key_column() -> Result<()> {
        let keyspace = "itest";
        let table = "person";
        let prefix = format!("{keyspace}:{table}");

        // Mock: HMSET itest:person:1 name "Updated"
        let hset = MockCmd::new(
            redis::cmd("HMSET")
                .arg(format!("{prefix}:1"))
                .arg("name")
                .arg("Updated")
                .clone(),
            Ok(Value::Okay),
        );
        let mock_conn = MockRedisConnection::new(vec![hset]);

        let schema = Arc::new(Schema::new(vec![
            Field::new("id", DataType::Utf8, false),
            Field::new("name", DataType::Utf8, true),
        ]));

        let conn = Arc::new(RwLock::new(mock_conn));
        let key_column = Some("id".to_string());

        // Filter: id = '1'
        let filters = vec![Expr::BinaryExpr(datafusion::logical_expr::BinaryExpr {
            left: Box::new(Expr::Column(datafusion::common::Column::new_unqualified(
                "id",
            ))),
            op: Operator::Eq,
            right: Box::new(Expr::Literal(
                datafusion::common::ScalarValue::Utf8(Some("1".to_string())),
                None,
            )),
        })];

        let keys = {
            let mut conn_guard = conn.try_write().unwrap();
            resolve_matching_keys(
                &mut *conn_guard,
                keyspace,
                table,
                &schema,
                &key_column,
                &filters,
            )?
        };
        assert_eq!(keys, vec![format!("{prefix}:1")]);

        // Assignment: name = 'Updated'
        let assignments = vec![(
            "name".to_string(),
            Expr::Literal(
                datafusion::common::ScalarValue::Utf8(Some("Updated".to_string())),
                None,
            ),
        )];
        let fields = assignments_to_redis_fields(&assignments, &key_column)?;
        assert_eq!(fields, vec![("name".to_string(), "Updated".to_string())]);

        let exec = RedisDmlExec::new(conn, RedisDmlOp::Update(keys, fields));
        let batch = exec.execute(0, Arc::new(TaskContext::default()))?;
        let batches: Vec<RecordBatch> = batch.try_collect().await?;
        let count_arr = batches[0]
            .column(0)
            .as_any()
            .downcast_ref::<UInt64Array>()
            .unwrap();
        assert_eq!(count_arr.value(0), 1);

        Ok(())
    }

    #[tokio::test]
    async fn test_update_key_column_rejected() -> Result<()> {
        let key_column = Some("id".to_string());
        let assignments = vec![(
            "id".to_string(),
            Expr::Literal(
                datafusion::common::ScalarValue::Utf8(Some("new_id".to_string())),
                None,
            ),
        )];

        let result = assignments_to_redis_fields(&assignments, &key_column);
        assert!(result.is_err());
        let err_msg = result.unwrap_err().to_string();
        assert!(err_msg.contains("Cannot update key column"));

        Ok(())
    }

    // ─── Integration test helpers ────────────────────────────────────────

    /// Register a Redis table from the CI docker service.
    fn register_ci_table(ctx: &mut SessionContext, table: &str) {
        register_ci_table_with_options(ctx, table, None);
    }

    /// Register a Redis table from the CI docker service with optional extra options.
    fn register_ci_table_with_options(
        ctx: &mut SessionContext,
        table: &str,
        extra_options: Option<&HashMap<String, String>>,
    ) {
        let mut options = HashMap::new();
        options.insert("key_space".to_string(), "mydb".to_string());
        options.insert("table".to_string(), table.to_string());
        options.insert("key_column".to_string(), "product_id".to_string());
        if let Some(extra) = extra_options {
            options.extend(extra.clone());
        }
        register_redis_tables(ctx, table, "redis://127.0.0.1:6379", Some(&options))
            .unwrap_or_else(|e| panic!("register {} failed: {}", table, e));
    }

    /// Remove all CI Redis keys for a registered table so tests can start from an empty table.
    fn clear_ci_table(table: &str) {
        let client = redis::Client::open("redis://127.0.0.1:6379").expect("create redis client");
        let mut conn = client.get_connection().expect("connect to redis");
        let pattern = format!("mydb:{table}:*");
        let keys: Vec<String> = conn.keys(&pattern).expect("scan redis keys");
        if !keys.is_empty() {
            let _: usize = conn.del(keys).expect("cleanup redis keys");
        }
    }

    async fn ci_query_all(ctx: &SessionContext, sql: &str) -> Vec<RecordBatch> {
        let df = ctx.sql(sql).await.expect("parse sql");
        df.collect().await.expect("collect results")
    }

    fn ci_total_rows(batches: &[RecordBatch]) -> usize {
        batches.iter().map(|b| b.num_rows()).sum()
    }

    // ─── Scan tests (integration) ───────────────────────────────────────

    #[tokio::test]
    #[ignore]
    async fn test_scan_all_rows_live() {
        let mut ctx = SessionContext::new();
        register_ci_table(&mut ctx, "products");

        let batches = ci_query_all(
            &ctx,
            "SELECT product_id, name, category, price, in_stock FROM products ORDER BY product_id",
        )
        .await;
        assert!(ci_total_rows(&batches) >= 5);
    }

    #[tokio::test]
    #[ignore]
    async fn test_scan_with_projection_live() {
        let mut ctx = SessionContext::new();
        register_ci_table(&mut ctx, "products");

        let batches = ci_query_all(&ctx, "SELECT name FROM products ORDER BY product_id").await;
        assert!(ci_total_rows(&batches) >= 5);
        assert_eq!(batches[0].num_columns(), 1);
    }

    #[tokio::test]
    #[ignore]
    async fn test_scan_with_filter_live() {
        let mut ctx = SessionContext::new();
        register_ci_table(&mut ctx, "products");

        let batches = ci_query_all(
            &ctx,
            "SELECT product_id, name FROM products WHERE product_id = 'PROD001'",
        )
        .await;
        assert_eq!(ci_total_rows(&batches), 1);

        let names = batches[0]
            .column(1)
            .as_any()
            .downcast_ref::<StringArray>()
            .unwrap();
        assert_eq!(names.value(0), "Laptop");
    }

    #[tokio::test]
    #[ignore]
    async fn test_scan_with_limit_live() {
        let mut ctx = SessionContext::new();
        register_ci_table(&mut ctx, "products");

        let batches = ci_query_all(&ctx, "SELECT product_id FROM products LIMIT 2").await;
        assert_eq!(ci_total_rows(&batches), 2);
    }

    // ─── Insert test (integration) ──────────────────────────────────────

    #[tokio::test]
    #[ignore]
    async fn test_insert_into_live() {
        let mut ctx = SessionContext::new();
        register_ci_table(&mut ctx, "products");

        ctx.sql(
            "INSERT INTO products (product_id, name, category, price, in_stock)
             VALUES ('PROD_INS_TEST', 'TestInsert', 'TestCat', '49.99', 'true')",
        )
        .await
        .expect("parse insert")
        .collect()
        .await
        .expect("execute insert");

        let batches = ci_query_all(
            &ctx,
            "SELECT product_id, name FROM products WHERE product_id = 'PROD_INS_TEST'",
        )
        .await;
        assert_eq!(ci_total_rows(&batches), 1);
    }

    /// Multi-row `INSERT INTO ... VALUES (...), (...), (...)` — the shape the
    /// server-side renderer emits when a pipeline parameter is the
    /// array-of-arrays form `{"rows": [[..], [..]]}`. DataFusion materializes
    /// the VALUES list into a batch with N rows, and `RedisSink` writes one
    /// hash per row keyed on `product_id`. Verifies all N keys land.
    #[tokio::test]
    #[ignore]
    async fn test_insert_multi_row_values_live() {
        let mut ctx = SessionContext::new();
        register_ci_table(&mut ctx, "products");

        // Pre-clean so a re-run starts from a known state.
        for id in ["PROD_RBATCH_1", "PROD_RBATCH_2", "PROD_RBATCH_3"] {
            ctx.sql(&format!("DELETE FROM products WHERE product_id = '{id}'"))
                .await
                .unwrap()
                .collect()
                .await
                .unwrap();
        }

        ctx.sql(
            "INSERT INTO products (product_id, name, category, price, in_stock) VALUES \
             ('PROD_RBATCH_1', 'RB1', 'RBatchCat', '1.0', 'true'), \
             ('PROD_RBATCH_2', 'RB2', 'RBatchCat', '2.0', 'true'), \
             ('PROD_RBATCH_3', 'RB3', 'RBatchCat', '3.0', 'false')",
        )
        .await
        .expect("parse multi-row insert")
        .collect()
        .await
        .expect("execute multi-row insert");

        let batches = ci_query_all(
            &ctx,
            "SELECT product_id FROM products WHERE category = 'RBatchCat' ORDER BY product_id",
        )
        .await;
        assert_eq!(ci_total_rows(&batches), 3);
    }

    // ─── Delete tests (integration) ─────────────────────────────────────

    #[tokio::test]
    #[ignore]
    async fn test_delete_with_filter_live() {
        let mut ctx = SessionContext::new();
        register_ci_table(&mut ctx, "products");

        // Insert a row to delete
        ctx.sql(
            "INSERT INTO products (product_id, name, category, price, in_stock)
             VALUES ('PROD_DEL_TEST', 'DeleteMe', 'Test', '1.0', 'true')",
        )
        .await
        .unwrap()
        .collect()
        .await
        .unwrap();

        let before = ci_query_all(
            &ctx,
            "SELECT product_id FROM products WHERE product_id = 'PROD_DEL_TEST'",
        )
        .await;
        assert_eq!(ci_total_rows(&before), 1);

        ctx.sql("DELETE FROM products WHERE product_id = 'PROD_DEL_TEST'")
            .await
            .expect("parse delete")
            .collect()
            .await
            .expect("execute delete");

        let after = ci_query_all(
            &ctx,
            "SELECT product_id FROM products WHERE product_id = 'PROD_DEL_TEST'",
        )
        .await;
        assert_eq!(ci_total_rows(&after), 0);
    }

    #[tokio::test]
    #[ignore]
    async fn test_delete_no_matching_rows_live() {
        let mut ctx = SessionContext::new();
        register_ci_table(&mut ctx, "products");

        let before = ci_query_all(
            &ctx,
            "SELECT product_id FROM products WHERE product_id = 'PROD001'",
        )
        .await;
        assert_eq!(ci_total_rows(&before), 1);

        ctx.sql("DELETE FROM products WHERE product_id = 'NONEXISTENT'")
            .await
            .expect("parse delete")
            .collect()
            .await
            .expect("execute delete");

        let after = ci_query_all(
            &ctx,
            "SELECT product_id FROM products WHERE product_id = 'PROD001'",
        )
        .await;
        assert_eq!(ci_total_rows(&after), 1);
    }

    // ─── Update tests (integration) ─────────────────────────────────────

    #[tokio::test]
    #[ignore]
    async fn test_update_single_column_live() {
        let mut ctx = SessionContext::new();
        register_ci_table(&mut ctx, "products");

        ctx.sql("UPDATE products SET price = '899.99' WHERE product_id = 'PROD001'")
            .await
            .expect("parse update")
            .collect()
            .await
            .expect("execute update");

        let batches = ci_query_all(
            &ctx,
            "SELECT price FROM products WHERE product_id = 'PROD001'",
        )
        .await;
        assert_eq!(ci_total_rows(&batches), 1);

        let prices = batches[0]
            .column(0)
            .as_any()
            .downcast_ref::<StringArray>()
            .unwrap();
        assert_eq!(prices.value(0), "899.99");
    }

    #[tokio::test]
    #[ignore]
    async fn test_update_no_matching_rows_live() {
        let mut ctx = SessionContext::new();
        register_ci_table(&mut ctx, "products");

        let before = ci_query_all(
            &ctx,
            "SELECT name FROM products WHERE product_id = 'PROD001'",
        )
        .await;
        assert_eq!(ci_total_rows(&before), 1);
        let before_name = before[0]
            .column(0)
            .as_any()
            .downcast_ref::<StringArray>()
            .unwrap()
            .value(0)
            .to_string();

        ctx.sql("UPDATE products SET price = '0.0' WHERE product_id = 'NONEXISTENT'")
            .await
            .expect("parse update")
            .collect()
            .await
            .expect("execute update");

        let after = ci_query_all(
            &ctx,
            "SELECT name FROM products WHERE product_id = 'PROD001'",
        )
        .await;
        assert_eq!(ci_total_rows(&after), 1);
        let after_name = after[0]
            .column(0)
            .as_any()
            .downcast_ref::<StringArray>()
            .unwrap()
            .value(0)
            .to_string();
        assert_eq!(before_name, after_name);
    }

    // ─── Combined DML test (integration) ────────────────────────────────

    #[tokio::test]
    #[ignore]
    async fn test_insert_update_delete_round_trip_live() {
        let mut ctx = SessionContext::new();
        register_ci_table(&mut ctx, "products");

        // 1. Insert
        ctx.sql(
            "INSERT INTO products (product_id, name, category, price, in_stock)
             VALUES ('PROD_RT_TEST', 'RoundTrip', 'Test', '10.0', 'true')",
        )
        .await
        .unwrap()
        .collect()
        .await
        .unwrap();
        let after_insert = ci_query_all(
            &ctx,
            "SELECT product_id FROM products WHERE product_id = 'PROD_RT_TEST'",
        )
        .await;
        assert_eq!(ci_total_rows(&after_insert), 1);

        // 2. Update
        ctx.sql("UPDATE products SET name = 'RoundTripUpdated', price = '20.0' WHERE product_id = 'PROD_RT_TEST'")
            .await
            .unwrap()
            .collect()
            .await
            .unwrap();
        let batches = ci_query_all(
            &ctx,
            "SELECT name, price FROM products WHERE product_id = 'PROD_RT_TEST'",
        )
        .await;
        assert_eq!(ci_total_rows(&batches), 1);
        let names = batches[0]
            .column(0)
            .as_any()
            .downcast_ref::<StringArray>()
            .unwrap();
        assert_eq!(names.value(0), "RoundTripUpdated");

        // 3. Delete
        ctx.sql("DELETE FROM products WHERE product_id = 'PROD_RT_TEST'")
            .await
            .unwrap()
            .collect()
            .await
            .unwrap();
        let after_delete = ci_query_all(
            &ctx,
            "SELECT product_id FROM products WHERE product_id = 'PROD_RT_TEST'",
        )
        .await;
        assert_eq!(ci_total_rows(&after_delete), 0);
    }

    // ─── Non-key column filter test (integration) ───────────────────────

    #[tokio::test]
    #[ignore]
    async fn test_filter_by_category_live() {
        let mut ctx = SessionContext::new();
        register_ci_table(&mut ctx, "products");

        let batches = ci_query_all(
            &ctx,
            "SELECT product_id, name FROM products WHERE category = 'Electronics' ORDER BY product_id",
        )
        .await;
        // PROD001..PROD004 are Electronics
        assert_eq!(ci_total_rows(&batches), 4);
    }

    #[tokio::test]
    #[ignore]
    async fn test_filter_by_in_stock_live() {
        let mut ctx = SessionContext::new();
        register_ci_table(&mut ctx, "products");

        let batches = ci_query_all(
            &ctx,
            "SELECT product_id FROM products WHERE in_stock = 'false'",
        )
        .await;
        // Only PROD003 (Monitor) is out of stock
        assert_eq!(ci_total_rows(&batches), 1);
    }

    // ─── Aggregation test (integration) ─────────────────────────────────

    #[tokio::test]
    #[ignore]
    async fn test_aggregation_query_live() {
        let mut ctx = SessionContext::new();
        register_ci_table(&mut ctx, "products");

        let batches = ci_query_all(
            &ctx,
            "SELECT category, COUNT(*) as cnt
             FROM products
             GROUP BY category
             ORDER BY category",
        )
        .await;
        assert!(ci_total_rows(&batches) >= 2); // at least Electronics and Furniture
    }

    /// Regression test for #97 (Redis half): projection pushdown emits
    /// `Some([])` for `count(*)`, and `fetch_partition` previously built a
    /// zero-column batch via `RecordBatch::try_new`, which Arrow rejects with
    /// "must either specify a row count or at least one column". Uses a
    /// dedicated table so the bare `count(*)` has a known value regardless of
    /// what other parallel tests do to `products`.
    #[tokio::test]
    #[ignore]
    async fn test_count_star_pushdown_live() {
        let table_name = "count_star_scratch_live";
        clear_ci_table(table_name);

        let mut ctx = SessionContext::new();
        let mut extra_options = HashMap::new();
        extra_options.insert("columns".to_string(), "product_id,name,price".to_string());
        register_ci_table_with_options(&mut ctx, table_name, Some(&extra_options));

        ctx.sql(&format!(
            "INSERT INTO {table_name} (product_id, name, price)
             VALUES ('A', 'a', '1.0'), ('B', 'b', '2.0'), ('C', 'c', '3.0')"
        ))
        .await
        .expect("parse insert")
        .collect()
        .await
        .expect("execute insert");

        let batches = ci_query_all(&ctx, &format!("SELECT count(*) FROM {table_name}")).await;
        assert_eq!(ci_total_rows(&batches), 1);
        let counts = batches[0]
            .column(0)
            .as_any()
            .downcast_ref::<arrow::array::Int64Array>()
            .unwrap();
        assert_eq!(counts.value(0), 3);

        clear_ci_table(table_name);
    }

    #[tokio::test]
    #[ignore]
    async fn test_empty_table_declared_schema_live() {
        let table_name = "products_schema_live";
        clear_ci_table(table_name);

        let mut ctx = SessionContext::new();
        let mut extra_options = HashMap::new();
        extra_options.insert(
            "columns".to_string(),
            "product_id,name,category,price,in_stock".to_string(),
        );
        register_ci_table_with_options(&mut ctx, table_name, Some(&extra_options));

        let catalog = ctx.catalog("datafusion").unwrap();
        let schema = catalog.schema("public").unwrap();
        let table = schema.table(table_name).await.unwrap().unwrap();
        let table_schema = table.schema();
        let field_names: Vec<&str> = table_schema
            .fields()
            .iter()
            .map(|f| f.name().as_str())
            .collect();
        assert_eq!(
            field_names,
            vec!["product_id", "name", "category", "price", "in_stock"]
        );

        ctx.sql(
            "INSERT INTO products_schema_live (product_id, name, category, price, in_stock)
             VALUES ('PROD_SCHEMA', 'SchemaProduct', 'TestCat', '12.34', 'true')",
        )
        .await
        .expect("parse insert")
        .collect()
        .await
        .expect("execute insert");

        let batches = ci_query_all(
            &ctx,
            "SELECT product_id, name, price
             FROM products_schema_live
             WHERE product_id = 'PROD_SCHEMA'",
        )
        .await;
        assert_eq!(ci_total_rows(&batches), 1);

        let product_ids = batches[0]
            .column(0)
            .as_any()
            .downcast_ref::<StringArray>()
            .unwrap();
        let names = batches[0]
            .column(1)
            .as_any()
            .downcast_ref::<StringArray>()
            .unwrap();
        assert_eq!(product_ids.value(0), "PROD_SCHEMA");
        assert_eq!(names.value(0), "SchemaProduct");

        ctx.sql("DELETE FROM products_schema_live WHERE product_id = 'PROD_SCHEMA'")
            .await
            .unwrap()
            .collect()
            .await
            .unwrap();

        clear_ci_table(table_name);
    }
}