s4-server 0.8.3

S4 — Squished S3 — GPU-accelerated transparent compression S3-compatible storage gateway (cargo install s4-server installs the `s4` binary).
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
//! S3 Select — server-side SQL filter on object body (v0.6 #41).
//!
//! Implements the [`SelectObjectContent`][aws-doc] surface as a small,
//! self-contained module. The primary entry point is [`run_select_csv`] /
//! [`run_select_jsonlines`] which take a SQL string and the in-memory body
//! bytes (the caller is responsible for fetching + decompressing +
//! decrypting the object — at the handler level we delegate to S4's
//! existing GET path so SSE-C / SSE-S4 / SSE-KMS / S4 codec all work
//! transparently).
//!
//! ## Supported SQL subset
//!
//! - `SELECT col1, col2 FROM s3object` — projection by header name when
//!   the CSV has a header line.
//! - `SELECT _1, _3 FROM s3object` — positional projection (1-based, AWS
//!   convention; `_1` is the leftmost column).
//! - `SELECT * FROM s3object` — all columns in input order.
//! - `WHERE col = 'value'`, `WHERE col > 100`, `WHERE col LIKE 'foo%'`.
//! - `AND` / `OR` / `NOT` boolean composition.
//! - String / integer / float literals.
//! - Equality / inequality (`=`, `<>`, `<`, `>`, `<=`, `>=`) and `LIKE`.
//!
//! ## Explicitly unsupported (rejected with [`SelectError::UnsupportedFeature`])
//!
//! - Aggregates (`COUNT`, `SUM`, `AVG`, …) and `GROUP BY` / `HAVING`.
//! - `JOIN` / subqueries.
//! - `ORDER BY` / `LIMIT` (Select-on-S3 streams in input order; aggregating
//!   would defeat the streaming model and is outside this v0.6 scope).
//! - Parquet input (Parquet decode is intentionally out of scope; CSV /
//!   JSON Lines are the v0.6 deliverables).
//!
//! ## Output framing
//!
//! [`EventStreamWriter`] emits the AWS event-stream binary protocol —
//! one `Records` frame per non-empty payload, an optional `Stats` frame,
//! and a terminating `End` frame. Each frame is
//! `[total_len BE u32][headers_len BE u32][prelude CRC32][headers][payload][message CRC32]`
//! per the [AWS appendix][aws-events]. The handler in `service.rs` feeds
//! the produced events into `s3s::dto::SelectObjectContentEventStream`,
//! which performs equivalent framing on the wire — `EventStreamWriter`
//! exists primarily so the **frame format itself** can be unit-tested and
//! asserted-on by the integration test without spinning up a full client.
//!
//! [aws-doc]: https://docs.aws.amazon.com/AmazonS3/latest/API/API_SelectObjectContent.html
//! [aws-events]: https://docs.aws.amazon.com/AmazonS3/latest/API/RESTSelectObjectAppendix.html

use sqlparser::ast::{
    BinaryOperator, Expr, GroupByExpr, ObjectName, Query, Select, SelectItem, SetExpr,
    Statement, TableFactor, UnaryOperator, Value,
};
use sqlparser::dialect::GenericDialect;
use sqlparser::parser::Parser;

// =====================================================================
// Errors
// =====================================================================

#[derive(Debug, thiserror::Error)]
pub enum SelectError {
    #[error("SQL parse error: {0}")]
    Parse(String),
    #[error("unsupported SQL feature: {0}")]
    UnsupportedFeature(String),
    #[error("input format error: {0}")]
    InputFormat(String),
    #[error("row evaluation error: {0}")]
    RowEval(String),
}

// =====================================================================
// Input / output formats
// =====================================================================

#[derive(Debug, Clone)]
pub enum SelectInputFormat {
    Csv { has_header: bool, delimiter: char },
    JsonLines,
}

#[derive(Debug, Clone)]
pub enum SelectOutputFormat {
    Csv,
    Json,
}

// =====================================================================
// Parsed query
// =====================================================================

#[derive(Debug, Clone)]
pub struct SelectQuery {
    /// Raw sqlparser SELECT items, validated against the supported
    /// subset at parse time (no aggregates / window funcs / subqueries).
    pub projection: Vec<SelectItem>,
    pub where_clause: Option<Expr>,
    /// Typically the literal `s3object` (case-insensitive). Captured for
    /// completeness; the runtime ignores it because there's only ever one
    /// virtual table in a Select query.
    pub from_alias: String,
}

/// Parse and validate a S3 Select SQL expression.
///
/// Reject features that have no row-streaming semantics on a single
/// object: aggregates, GROUP BY, HAVING, JOIN, ORDER BY, LIMIT, DISTINCT.
pub fn parse_select(sql: &str) -> Result<SelectQuery, SelectError> {
    let dialect = GenericDialect {};
    let mut statements = Parser::parse_sql(&dialect, sql)
        .map_err(|e| SelectError::Parse(e.to_string()))?;
    if statements.len() != 1 {
        return Err(SelectError::Parse(format!(
            "expected exactly one statement, got {}",
            statements.len()
        )));
    }
    let stmt = statements.pop().expect("len == 1");
    let query = match stmt {
        Statement::Query(q) => *q,
        other => {
            return Err(SelectError::UnsupportedFeature(format!(
                "only SELECT statements are supported, got: {other:?}"
            )));
        }
    };
    let Query {
        body, order_by, limit, offset, fetch, locks, with, ..
    } = query;
    if with.is_some() {
        return Err(SelectError::UnsupportedFeature("CTE / WITH".into()));
    }
    if order_by.is_some() {
        return Err(SelectError::UnsupportedFeature("ORDER BY".into()));
    }
    if limit.is_some() {
        return Err(SelectError::UnsupportedFeature("LIMIT".into()));
    }
    if offset.is_some() {
        return Err(SelectError::UnsupportedFeature("OFFSET".into()));
    }
    if fetch.is_some() {
        return Err(SelectError::UnsupportedFeature("FETCH".into()));
    }
    if !locks.is_empty() {
        return Err(SelectError::UnsupportedFeature("FOR UPDATE / lock clauses".into()));
    }

    let select = match *body {
        SetExpr::Select(s) => *s,
        SetExpr::Query(_) => {
            return Err(SelectError::UnsupportedFeature("nested query".into()));
        }
        SetExpr::SetOperation { .. } => {
            return Err(SelectError::UnsupportedFeature("set operation (UNION/INTERSECT/EXCEPT)".into()));
        }
        other => {
            return Err(SelectError::UnsupportedFeature(format!("unsupported SetExpr: {other:?}")));
        }
    };

    let Select {
        distinct,
        top,
        projection,
        from,
        selection,
        group_by,
        having,
        named_window,
        qualify,
        cluster_by,
        distribute_by,
        sort_by,
        prewhere,
        connect_by,
        ..
    } = select;
    if distinct.is_some() {
        return Err(SelectError::UnsupportedFeature("DISTINCT".into()));
    }
    if top.is_some() {
        return Err(SelectError::UnsupportedFeature("TOP".into()));
    }
    if having.is_some() {
        return Err(SelectError::UnsupportedFeature("HAVING".into()));
    }
    if !named_window.is_empty() {
        return Err(SelectError::UnsupportedFeature("WINDOW".into()));
    }
    if qualify.is_some() {
        return Err(SelectError::UnsupportedFeature("QUALIFY".into()));
    }
    if !cluster_by.is_empty() || !distribute_by.is_empty() || !sort_by.is_empty() {
        return Err(SelectError::UnsupportedFeature(
            "CLUSTER BY / DISTRIBUTE BY / SORT BY".into(),
        ));
    }
    if prewhere.is_some() {
        return Err(SelectError::UnsupportedFeature("PREWHERE".into()));
    }
    if connect_by.is_some() {
        return Err(SelectError::UnsupportedFeature("CONNECT BY".into()));
    }
    match group_by {
        GroupByExpr::Expressions(ref exprs, ref mods) if exprs.is_empty() && mods.is_empty() => {}
        _ => return Err(SelectError::UnsupportedFeature("GROUP BY".into())),
    }

    // Validate projection — reject anything that requires a non-row-local
    // computation (function calls, subqueries, aggregates).
    for item in &projection {
        validate_projection_item(item)?;
    }
    if let Some(ref where_expr) = selection {
        validate_where_expr(where_expr)?;
    }

    // FROM must be a single table reference, optionally aliased.
    let from_alias = match from.as_slice() {
        [twj] if twj.joins.is_empty() => match &twj.relation {
            TableFactor::Table { name, alias, .. } => alias
                .as_ref()
                .map(|a| a.name.value.clone())
                .unwrap_or_else(|| object_name_to_string(name)),
            _ => {
                return Err(SelectError::UnsupportedFeature(
                    "only `FROM s3object` (or aliased single table) is supported".into(),
                ));
            }
        },
        [] => "s3object".to_owned(),
        _ => return Err(SelectError::UnsupportedFeature("JOIN / multiple FROM tables".into())),
    };

    Ok(SelectQuery {
        projection,
        where_clause: selection,
        from_alias,
    })
}

fn object_name_to_string(name: &ObjectName) -> String {
    name.0
        .iter()
        .map(|i| i.value.as_str())
        .collect::<Vec<_>>()
        .join(".")
}

fn validate_projection_item(item: &SelectItem) -> Result<(), SelectError> {
    match item {
        SelectItem::Wildcard(_) => Ok(()),
        SelectItem::QualifiedWildcard(_, _) => Ok(()),
        SelectItem::UnnamedExpr(e) | SelectItem::ExprWithAlias { expr: e, .. } => {
            validate_simple_column_expr(e)
        }
    }
}

fn validate_simple_column_expr(expr: &Expr) -> Result<(), SelectError> {
    match expr {
        Expr::Identifier(_) | Expr::CompoundIdentifier(_) => Ok(()),
        Expr::Function(_) => Err(SelectError::UnsupportedFeature(
            "aggregate / scalar function in projection (only bare column references supported)".into(),
        )),
        Expr::Subquery(_) | Expr::Exists { .. } => {
            Err(SelectError::UnsupportedFeature("subquery in projection".into()))
        }
        _ => Err(SelectError::UnsupportedFeature(format!(
            "unsupported projection expression: {expr}"
        ))),
    }
}

fn validate_where_expr(expr: &Expr) -> Result<(), SelectError> {
    match expr {
        Expr::Identifier(_) | Expr::CompoundIdentifier(_) | Expr::Value(_) => Ok(()),
        Expr::Nested(inner) => validate_where_expr(inner),
        Expr::UnaryOp { op, expr } => match op {
            UnaryOperator::Not | UnaryOperator::Minus | UnaryOperator::Plus => {
                validate_where_expr(expr)
            }
            other => Err(SelectError::UnsupportedFeature(format!(
                "unsupported unary operator in WHERE: {other:?}"
            ))),
        },
        Expr::BinaryOp { op, left, right } => match op {
            BinaryOperator::Eq
            | BinaryOperator::NotEq
            | BinaryOperator::Lt
            | BinaryOperator::LtEq
            | BinaryOperator::Gt
            | BinaryOperator::GtEq
            | BinaryOperator::And
            | BinaryOperator::Or => {
                validate_where_expr(left)?;
                validate_where_expr(right)
            }
            other => Err(SelectError::UnsupportedFeature(format!(
                "unsupported binary operator in WHERE: {other:?}"
            ))),
        },
        Expr::Like { expr, pattern, .. } => {
            validate_where_expr(expr)?;
            validate_where_expr(pattern)
        }
        Expr::IsNull(e) | Expr::IsNotNull(e) => validate_where_expr(e),
        Expr::Function(_) => Err(SelectError::UnsupportedFeature(
            "function call in WHERE".into(),
        )),
        Expr::Subquery(_) | Expr::Exists { .. } | Expr::InSubquery { .. } => {
            Err(SelectError::UnsupportedFeature("subquery in WHERE".into()))
        }
        other => Err(SelectError::UnsupportedFeature(format!(
            "unsupported WHERE expression: {other}"
        ))),
    }
}

// =====================================================================
// Row representation + lookup
// =====================================================================

/// CSV input row. Columns indexed by 0-based position OR by header name
/// (when the InputFormat says `has_header = true`).
pub struct CsvRow<'a> {
    pub fields: Vec<&'a str>,
    pub headers: Option<&'a [String]>,
}

impl CsvRow<'_> {
    /// Look up a column. AWS Select supports both bare `column_name` (when
    /// the CSV has a header) and `_1`, `_2`, ... positional refs. Returns
    /// `None` if the identifier doesn't resolve.
    #[must_use]
    pub fn get(&self, ident: &str) -> Option<&str> {
        if let Some(stripped) = ident.strip_prefix('_')
            && let Ok(n) = stripped.parse::<usize>()
            && n >= 1
        {
            return self.fields.get(n - 1).copied();
        }
        // Header-name lookup. AWS S3 Select treats column names
        // case-insensitively when matched against headers in the file.
        if let Some(headers) = self.headers {
            for (i, h) in headers.iter().enumerate() {
                if h.eq_ignore_ascii_case(ident) {
                    return self.fields.get(i).copied();
                }
            }
        }
        None
    }
}

// =====================================================================
// Row evaluation
// =====================================================================

/// Logical value used by the WHERE evaluator. We keep it intentionally
/// small — only the literal kinds the supported subset can produce.
#[derive(Debug, Clone)]
enum Lit<'a> {
    Null,
    Bool(bool),
    Int(i64),
    Float(f64),
    Str(std::borrow::Cow<'a, str>),
}

impl<'a> Lit<'a> {
    fn from_str_value(s: &'a str) -> Lit<'a> {
        Lit::Str(std::borrow::Cow::Borrowed(s))
    }

    fn truthy(&self) -> bool {
        matches!(self, Lit::Bool(true))
    }
}

/// Apply WHERE + projection to a single row. Returns `Ok(Some(values))`
/// for matched rows (one `String` per `SELECT` item, in declaration
/// order), `Ok(None)` if WHERE excluded the row, `Err(...)` only on
/// runtime evaluation problems (a projected column not in the row, etc).
pub fn evaluate_row(
    query: &SelectQuery,
    row: &CsvRow<'_>,
) -> Result<Option<Vec<String>>, SelectError> {
    if let Some(ref w) = query.where_clause {
        let v = eval_expr(w, row)?;
        if !v.truthy() {
            return Ok(None);
        }
    }
    let mut out = Vec::with_capacity(query.projection.len());
    for item in &query.projection {
        match item {
            SelectItem::Wildcard(_) | SelectItem::QualifiedWildcard(_, _) => {
                for f in &row.fields {
                    out.push((*f).to_owned());
                }
            }
            SelectItem::UnnamedExpr(e) | SelectItem::ExprWithAlias { expr: e, .. } => {
                let ident = expr_as_column(e)?;
                let v = row.get(&ident).ok_or_else(|| {
                    SelectError::RowEval(format!("column not found: {ident}"))
                })?;
                out.push(v.to_owned());
            }
        }
    }
    Ok(Some(out))
}

fn expr_as_column(expr: &Expr) -> Result<String, SelectError> {
    match expr {
        Expr::Identifier(i) => Ok(i.value.clone()),
        Expr::CompoundIdentifier(parts) => parts
            .last()
            .map(|p| p.value.clone())
            .ok_or_else(|| SelectError::RowEval("empty compound identifier".into())),
        other => Err(SelectError::UnsupportedFeature(format!(
            "non-column projection: {other}"
        ))),
    }
}

fn eval_expr<'a>(expr: &Expr, row: &'a CsvRow<'a>) -> Result<Lit<'a>, SelectError> {
    match expr {
        Expr::Nested(inner) => eval_expr(inner, row),
        Expr::Identifier(i) => Ok(row
            .get(&i.value)
            .map_or(Lit::Null, Lit::from_str_value)),
        Expr::CompoundIdentifier(parts) => {
            let last = parts
                .last()
                .ok_or_else(|| SelectError::RowEval("empty compound identifier".into()))?;
            Ok(row
                .get(&last.value)
                .map_or(Lit::Null, Lit::from_str_value))
        }
        Expr::Value(v) => value_to_lit(v),
        Expr::UnaryOp { op, expr } => {
            let v = eval_expr(expr, row)?;
            match op {
                UnaryOperator::Not => Ok(Lit::Bool(!v.truthy())),
                UnaryOperator::Minus => match v {
                    Lit::Int(n) => Ok(Lit::Int(-n)),
                    Lit::Float(f) => Ok(Lit::Float(-f)),
                    other => Err(SelectError::RowEval(format!(
                        "cannot negate non-numeric value: {other:?}"
                    ))),
                },
                UnaryOperator::Plus => Ok(v),
                other => Err(SelectError::UnsupportedFeature(format!(
                    "unsupported unary op: {other:?}"
                ))),
            }
        }
        Expr::BinaryOp { op, left, right } => {
            let l = eval_expr(left, row)?;
            let r = eval_expr(right, row)?;
            eval_binary(op, &l, &r)
        }
        Expr::Like { negated, expr, pattern, escape_char } => {
            if escape_char.is_some() {
                return Err(SelectError::UnsupportedFeature(
                    "LIKE ESCAPE clause".into(),
                ));
            }
            let s_val = eval_expr(expr, row)?;
            let p_val = eval_expr(pattern, row)?;
            let s = lit_as_str(&s_val);
            let p = lit_as_str(&p_val);
            let m = like_match(s.as_ref(), p.as_ref());
            Ok(Lit::Bool(if *negated { !m } else { m }))
        }
        Expr::IsNull(e) => Ok(Lit::Bool(matches!(eval_expr(e, row)?, Lit::Null))),
        Expr::IsNotNull(e) => Ok(Lit::Bool(!matches!(eval_expr(e, row)?, Lit::Null))),
        other => Err(SelectError::UnsupportedFeature(format!(
            "unsupported expression in WHERE: {other}"
        ))),
    }
}

fn value_to_lit<'a>(v: &Value) -> Result<Lit<'a>, SelectError> {
    match v {
        Value::Number(s, _) => {
            if let Ok(n) = s.parse::<i64>() {
                Ok(Lit::Int(n))
            } else if let Ok(f) = s.parse::<f64>() {
                Ok(Lit::Float(f))
            } else {
                Err(SelectError::RowEval(format!("invalid number literal: {s}")))
            }
        }
        Value::SingleQuotedString(s) | Value::DoubleQuotedString(s) => {
            Ok(Lit::Str(std::borrow::Cow::Owned(s.clone())))
        }
        Value::Boolean(b) => Ok(Lit::Bool(*b)),
        Value::Null => Ok(Lit::Null),
        other => Err(SelectError::UnsupportedFeature(format!(
            "literal kind not supported: {other:?}"
        ))),
    }
}

fn lit_as_str<'a>(v: &Lit<'a>) -> std::borrow::Cow<'a, str> {
    match v {
        Lit::Null => std::borrow::Cow::Borrowed(""),
        Lit::Bool(b) => std::borrow::Cow::Owned(if *b { "true" } else { "false" }.into()),
        Lit::Int(n) => std::borrow::Cow::Owned(n.to_string()),
        Lit::Float(f) => std::borrow::Cow::Owned(f.to_string()),
        Lit::Str(s) => s.clone(),
    }
}

fn lit_as_f64(v: &Lit<'_>) -> Option<f64> {
    match v {
        Lit::Int(n) => Some(*n as f64),
        Lit::Float(f) => Some(*f),
        Lit::Str(s) => s.parse::<f64>().ok(),
        Lit::Bool(_) | Lit::Null => None,
    }
}

fn eval_binary<'a>(
    op: &BinaryOperator,
    l: &Lit<'a>,
    r: &Lit<'a>,
) -> Result<Lit<'a>, SelectError> {
    use BinaryOperator::*;
    match op {
        And => Ok(Lit::Bool(l.truthy() && r.truthy())),
        Or => Ok(Lit::Bool(l.truthy() || r.truthy())),
        Eq | NotEq | Lt | LtEq | Gt | GtEq => {
            // NULLs propagate to NULL → not-truthy. AWS S3 Select uses the
            // SQL NULL semantics; we collapse to a Bool(false) so they
            // simply don't match.
            if matches!(l, Lit::Null) || matches!(r, Lit::Null) {
                return Ok(Lit::Bool(false));
            }
            // Try numeric comparison first when both sides parse as
            // numbers — covers `col > 100` against CSV string fields.
            let cmp = if let (Some(a), Some(b)) = (lit_as_f64(l), lit_as_f64(r)) {
                a.partial_cmp(&b)
            } else {
                let a = lit_as_str(l);
                let b = lit_as_str(r);
                Some(a.as_ref().cmp(b.as_ref()))
            };
            let ord =
                cmp.ok_or_else(|| SelectError::RowEval("incomparable values (NaN?)".into()))?;
            let res = match op {
                Eq => ord == std::cmp::Ordering::Equal,
                NotEq => ord != std::cmp::Ordering::Equal,
                Lt => ord == std::cmp::Ordering::Less,
                LtEq => ord != std::cmp::Ordering::Greater,
                Gt => ord == std::cmp::Ordering::Greater,
                GtEq => ord != std::cmp::Ordering::Less,
                _ => unreachable!("guarded by outer match"),
            };
            Ok(Lit::Bool(res))
        }
        other => Err(SelectError::UnsupportedFeature(format!(
            "unsupported binary operator: {other:?}"
        ))),
    }
}

/// SQL `LIKE` matcher. Supports `%` (any sequence) and `_` (any single
/// char). Anchored at both ends — `'foo%'` matches `"foobar"` but not
/// `"xfoobar"`.
fn like_match(s: &str, pattern: &str) -> bool {
    let s_bytes: Vec<char> = s.chars().collect();
    let p_bytes: Vec<char> = pattern.chars().collect();
    let (mut si, mut pi) = (0usize, 0usize);
    let (mut star, mut match_si) = (None::<usize>, 0usize);
    while si < s_bytes.len() {
        if pi < p_bytes.len() && (p_bytes[pi] == '_' || p_bytes[pi] == s_bytes[si]) {
            si += 1;
            pi += 1;
        } else if pi < p_bytes.len() && p_bytes[pi] == '%' {
            star = Some(pi);
            match_si = si;
            pi += 1;
        } else if let Some(sp) = star {
            pi = sp + 1;
            match_si += 1;
            si = match_si;
        } else {
            return false;
        }
    }
    while pi < p_bytes.len() && p_bytes[pi] == '%' {
        pi += 1;
    }
    pi == p_bytes.len()
}

// =====================================================================
// CSV / JSON Lines runners
// =====================================================================

/// Run a Select against a CSV-bytes body in-memory. Returns the
/// concatenated output bytes in `output` format (CSV: rfc4180 single CRLF
/// rows / JSON: one JSON-object-per-line).
pub fn run_select_csv(
    sql: &str,
    body: &[u8],
    input: SelectInputFormat,
    output: SelectOutputFormat,
) -> Result<Vec<u8>, SelectError> {
    // v0.8 #51: try the GPU column-scan fast path first. It returns
    // `None` if the query shape doesn't fit (multi-condition WHERE,
    // projection that isn't `*`, no header, etc) or if the GPU
    // surfaces a runtime error — in which case we transparently fall
    // through to the CPU evaluator below. The GPU path only handles
    // CSV-output today; CSV-output is what S3 Select callers use
    // 95%+ of the time, so we gate the fast-path on that to keep the
    // shape narrow.
    if matches!(output, SelectOutputFormat::Csv)
        && let Some(filtered) = select_gpu(sql, body, &input)
    {
        return Ok(filtered);
    }

    let (has_header, delim) = match input {
        SelectInputFormat::Csv { has_header, delimiter } => (has_header, delimiter),
        SelectInputFormat::JsonLines => {
            return Err(SelectError::InputFormat(
                "run_select_csv called with JsonLines input — use run_select_jsonlines".into(),
            ));
        }
    };
    let query = parse_select(sql)?;

    let mut rdr = csv::ReaderBuilder::new()
        .has_headers(has_header)
        .delimiter(delim as u8)
        .flexible(true)
        .from_reader(body);

    let headers_owned: Option<Vec<String>> = if has_header {
        let h = rdr
            .headers()
            .map_err(|e| SelectError::InputFormat(format!("CSV headers: {e}")))?
            .iter()
            .map(|s| s.to_owned())
            .collect();
        Some(h)
    } else {
        None
    };
    let header_slice: Option<&[String]> = headers_owned.as_deref();

    let mut out = Vec::with_capacity(body.len() / 2);
    for record in rdr.records() {
        let record = record
            .map_err(|e| SelectError::InputFormat(format!("CSV record: {e}")))?;
        let fields: Vec<&str> = record.iter().collect();
        let row = CsvRow {
            fields,
            headers: header_slice,
        };
        if let Some(values) = evaluate_row(&query, &row)? {
            write_output_row(&query, &values, &output, &mut out)?;
        }
    }
    Ok(out)
}

/// Run a Select against a JSON-Lines body (`{...}\n{...}\n...`). One row
/// per top-level JSON object. Nested values are stringified for CSV
/// output; for JSON output, the projected fields are re-emitted with
/// their original JSON literal.
pub fn run_select_jsonlines(
    sql: &str,
    body: &[u8],
    output: SelectOutputFormat,
) -> Result<Vec<u8>, SelectError> {
    let query = parse_select(sql)?;
    let text = std::str::from_utf8(body)
        .map_err(|e| SelectError::InputFormat(format!("body is not valid UTF-8: {e}")))?;
    let mut out = Vec::with_capacity(body.len() / 2);
    for (lineno, line) in text.lines().enumerate() {
        let line = line.trim();
        if line.is_empty() {
            continue;
        }
        let v: serde_json::Value = serde_json::from_str(line).map_err(|e| {
            SelectError::InputFormat(format!("JSON parse on line {}: {e}", lineno + 1))
        })?;
        let obj = v.as_object().ok_or_else(|| {
            SelectError::InputFormat(format!(
                "JSON Lines requires top-level object, line {} was not an object",
                lineno + 1
            ))
        })?;
        // Reify the object as ordered (header_name, value_str) pairs so
        // the existing CsvRow evaluator works against it.
        let headers: Vec<String> = obj.keys().cloned().collect();
        let raw_strs: Vec<String> = obj
            .values()
            .map(|jv| match jv {
                serde_json::Value::String(s) => s.clone(),
                other => other.to_string(),
            })
            .collect();
        let fields: Vec<&str> = raw_strs.iter().map(|s| s.as_str()).collect();
        let row = CsvRow {
            fields,
            headers: Some(headers.as_slice()),
        };
        if let Some(values) = evaluate_row(&query, &row)? {
            write_jsonlines_row(&query, &headers, &values, &output, &mut out)?;
        }
    }
    Ok(out)
}

fn write_output_row(
    query: &SelectQuery,
    values: &[String],
    output: &SelectOutputFormat,
    out: &mut Vec<u8>,
) -> Result<(), SelectError> {
    match output {
        SelectOutputFormat::Csv => {
            let mut wtr = csv::WriterBuilder::new()
                .terminator(csv::Terminator::CRLF)
                .from_writer(Vec::new());
            wtr.write_record(values.iter().map(String::as_str))
                .map_err(|e| SelectError::InputFormat(format!("CSV write: {e}")))?;
            wtr.flush()
                .map_err(|e| SelectError::InputFormat(format!("CSV flush: {e}")))?;
            let inner = wtr
                .into_inner()
                .map_err(|e| SelectError::InputFormat(format!("CSV finish: {e}")))?;
            out.extend_from_slice(&inner);
        }
        SelectOutputFormat::Json => {
            let names = projection_names(query, values.len());
            let mut map = serde_json::Map::with_capacity(values.len());
            for (n, v) in names.iter().zip(values.iter()) {
                map.insert(n.clone(), serde_json::Value::String(v.clone()));
            }
            let line = serde_json::to_string(&serde_json::Value::Object(map))
                .map_err(|e| SelectError::InputFormat(format!("JSON serialize: {e}")))?;
            out.extend_from_slice(line.as_bytes());
            out.push(b'\n');
        }
    }
    Ok(())
}

fn write_jsonlines_row(
    query: &SelectQuery,
    headers: &[String],
    values: &[String],
    output: &SelectOutputFormat,
    out: &mut Vec<u8>,
) -> Result<(), SelectError> {
    match output {
        SelectOutputFormat::Csv => write_output_row(query, values, output, out)?,
        SelectOutputFormat::Json => {
            let names = projection_names_with_headers(query, headers, values.len());
            let mut map = serde_json::Map::with_capacity(values.len());
            for (n, v) in names.iter().zip(values.iter()) {
                map.insert(n.clone(), serde_json::Value::String(v.clone()));
            }
            let line = serde_json::to_string(&serde_json::Value::Object(map))
                .map_err(|e| SelectError::InputFormat(format!("JSON serialize: {e}")))?;
            out.extend_from_slice(line.as_bytes());
            out.push(b'\n');
        }
    }
    Ok(())
}

fn projection_names(query: &SelectQuery, fallback_len: usize) -> Vec<String> {
    let mut names = Vec::with_capacity(fallback_len);
    for (i, item) in query.projection.iter().enumerate() {
        match item {
            SelectItem::ExprWithAlias { alias, .. } => names.push(alias.value.clone()),
            SelectItem::UnnamedExpr(e) => match expr_as_column(e) {
                Ok(s) => names.push(s),
                Err(_) => names.push(format!("_{}", i + 1)),
            },
            SelectItem::Wildcard(_) | SelectItem::QualifiedWildcard(_, _) => {
                for j in names.len()..fallback_len {
                    names.push(format!("_{}", j + 1));
                }
                return names;
            }
        }
    }
    while names.len() < fallback_len {
        let n = names.len();
        names.push(format!("_{}", n + 1));
    }
    names
}

fn projection_names_with_headers(
    query: &SelectQuery,
    headers: &[String],
    fallback_len: usize,
) -> Vec<String> {
    let mut names = Vec::with_capacity(fallback_len);
    for (i, item) in query.projection.iter().enumerate() {
        match item {
            SelectItem::Wildcard(_) | SelectItem::QualifiedWildcard(_, _) => {
                for h in headers {
                    names.push(h.clone());
                }
                while names.len() < fallback_len {
                    let n = names.len();
                    names.push(format!("_{}", n + 1));
                }
                return names;
            }
            SelectItem::ExprWithAlias { alias, .. } => names.push(alias.value.clone()),
            SelectItem::UnnamedExpr(e) => match expr_as_column(e) {
                Ok(s) => names.push(s),
                Err(_) => names.push(format!("_{}", i + 1)),
            },
        }
    }
    while names.len() < fallback_len {
        let n = names.len();
        names.push(format!("_{}", n + 1));
    }
    names
}

// =====================================================================
// AWS event-stream framing
// =====================================================================

/// Emits AWS event-stream binary frames for a Select response. Each frame
/// is `[total_len BE u32][headers_len BE u32][prelude CRC32][headers...][payload][message CRC32]`.
///
/// Header value type is fixed at `7` (UTF-8 string). Headers always
/// emitted: `:event-type`, `:message-type`, plus `:content-type` for
/// payload-bearing frames (Records / Stats).
#[derive(Debug, Default)]
pub struct EventStreamWriter {}

impl EventStreamWriter {
    #[must_use]
    pub fn new() -> Self {
        Self {}
    }

    /// Build a `Records` frame. `payload` is the (optionally empty) body
    /// chunk — typically a CSV / JSON-Lines slab of one or more output
    /// rows. AWS allows splitting a logical record across frames.
    pub fn records(&mut self, payload: &[u8]) -> Vec<u8> {
        build_frame(
            &[
                (":event-type", "Records"),
                (":content-type", "application/octet-stream"),
                (":message-type", "event"),
            ],
            Some(payload),
        )
    }

    /// Build a `Stats` frame containing the standard
    /// `BytesScanned` / `BytesProcessed` / `BytesReturned` XML payload.
    pub fn stats(&mut self, scanned: u64, processed: u64, returned: u64) -> Vec<u8> {
        let xml = format!(
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\
<Stats xmlns=\"\">\
<BytesScanned>{scanned}</BytesScanned>\
<BytesProcessed>{processed}</BytesProcessed>\
<BytesReturned>{returned}</BytesReturned>\
</Stats>"
        );
        build_frame(
            &[
                (":event-type", "Stats"),
                (":content-type", "text/xml"),
                (":message-type", "event"),
            ],
            Some(xml.as_bytes()),
        )
    }

    /// Build the terminating `End` frame. Clients must wait for this
    /// before assuming the response stream is complete.
    pub fn end(&mut self) -> Vec<u8> {
        build_frame(
            &[
                (":event-type", "End"),
                (":message-type", "event"),
            ],
            None,
        )
    }
}

fn build_frame(headers: &[(&str, &str)], payload: Option<&[u8]>) -> Vec<u8> {
    let mut header_buf: Vec<u8> = Vec::new();
    for (name, value) in headers {
        let name_bytes = name.as_bytes();
        let value_bytes = value.as_bytes();
        debug_assert!(name_bytes.len() <= u8::MAX as usize, "header name too long");
        debug_assert!(value_bytes.len() <= u16::MAX as usize, "header value too long");
        header_buf.push(name_bytes.len() as u8);
        header_buf.extend_from_slice(name_bytes);
        header_buf.push(7); // value type 7 == UTF-8 string
        header_buf.extend_from_slice(&(value_bytes.len() as u16).to_be_bytes());
        header_buf.extend_from_slice(value_bytes);
    }
    let payload_bytes = payload.unwrap_or(&[]);
    let headers_len: u32 = header_buf.len() as u32;
    let total_len: u32 = 12 + headers_len + payload_bytes.len() as u32 + 4;

    let mut buf: Vec<u8> = Vec::with_capacity(total_len as usize);
    buf.extend_from_slice(&total_len.to_be_bytes());
    buf.extend_from_slice(&headers_len.to_be_bytes());
    let prelude_crc = crc32fast::hash(&buf[..8]);
    buf.extend_from_slice(&prelude_crc.to_be_bytes());
    buf.extend_from_slice(&header_buf);
    buf.extend_from_slice(payload_bytes);
    let message_crc = crc32fast::hash(&buf[..buf.len()]);
    buf.extend_from_slice(&message_crc.to_be_bytes());
    buf
}

// =====================================================================
// GPU column-scan fast path (v0.8 #51)
// =====================================================================
//
// `select_gpu` is the entry point [`run_select_csv`] tries before
// falling back to the per-row CPU evaluator. Its contract is "return
// `Some(filtered_csv_bytes)` when the kernel both _can_ and _did_
// handle the query, otherwise `None`". Failure modes that justify a
// `None` return:
//   - feature `nvcomp-gpu` is off (compile-time dispatch to the no-op
//     fallback further down)
//   - the SQL shape isn't single-column compare (multi-cond AND/OR,
//     function calls, projection that's not `*`, etc)
//   - the input isn't header-bearing CSV (JsonLines / no-header CSV)
//   - the kernel hit a runtime error (no driver, OOB column, body
//     larger than the device budget) — we swallow these and let the
//     CPU path run so the user still gets a correct answer
//
// The CPU path is always the source of truth; the GPU path's output
// must be byte-identical to it for any query the kernel claims to
// support. The bench in `crates/s4-codec/examples/bench_gpu_select.rs`
// asserts that explicitly on a 100M-row CSV.

#[cfg(feature = "nvcomp-gpu")]
mod gpu {
    use super::{Expr, GenericDialect, Parser, SelectInputFormat, Statement, Value};
    use s4_codec::gpu_select::{CompareOp, GpuSelectKernel};
    use sqlparser::ast::{BinaryOperator, SelectItem, SetExpr};
    use std::sync::OnceLock;

    /// One global kernel instance per process — `GpuSelectKernel::new`
    /// is expensive (CUDA context init + NVRTC compile) and the kernel
    /// itself is stateless / thread-safe to call. We hold the result
    /// of the first init attempt so subsequent Selects don't re-pay
    /// the cost (or re-pay the failure log) on a host without CUDA.
    static KERNEL: OnceLock<Option<GpuSelectKernel>> = OnceLock::new();

    fn kernel() -> Option<&'static GpuSelectKernel> {
        KERNEL
            .get_or_init(|| match GpuSelectKernel::new() {
                Ok(k) => Some(k),
                Err(e) => {
                    tracing::debug!(
                        target: "s4_server::select::gpu",
                        ?e,
                        "GpuSelectKernel init failed; falling back to CPU permanently"
                    );
                    None
                }
            })
            .as_ref()
    }

    /// Attempt the GPU path. Returns `Some(filtered_csv)` on success,
    /// `None` if the query shape doesn't fit the kernel's supported
    /// subset or the GPU surfaced an error.
    pub(super) fn try_select_gpu(
        sql: &str,
        body: &[u8],
        input: &SelectInputFormat,
    ) -> Option<Vec<u8>> {
        // Only header-bearing CSV qualifies — column-name lookup is
        // how we resolve the WHERE column to an index.
        let SelectInputFormat::Csv {
            has_header: true,
            delimiter: ',',
        } = input
        else {
            return None;
        };

        let (col_name, op, literal) = parse_simple_predicate(sql)?;

        // Resolve column name → index by reading the header row only.
        // The kernel itself does the per-row column extraction; this
        // host-side header parse is a single linear scan over the
        // first ~kilobyte of the body.
        let col_idx = resolve_header_column(body, &col_name)?;

        let kernel = kernel()?;
        match kernel.scan_csv(body, col_idx, op, literal.as_bytes()) {
            Ok(out) => Some(out),
            Err(e) => {
                tracing::debug!(
                    target: "s4_server::select::gpu",
                    ?e,
                    "GPU scan failed; falling back to CPU"
                );
                None
            }
        }
    }

    /// Narrow the parsed SQL down to the single `WHERE col OP literal`
    /// shape the kernel handles. Anything else (AND/OR composition,
    /// function calls, multi-column projection that isn't `*`)
    /// returns `None` so the caller falls back to CPU.
    fn parse_simple_predicate(sql: &str) -> Option<(String, CompareOp, String)> {
        let mut stmts = Parser::parse_sql(&GenericDialect {}, sql).ok()?;
        if stmts.len() != 1 {
            return None;
        }
        let Statement::Query(query) = stmts.pop()? else {
            return None;
        };
        if query.order_by.is_some() || query.limit.is_some() || query.with.is_some() {
            return None;
        }
        let SetExpr::Select(select) = *query.body else {
            return None;
        };
        // Projection must be `*` or a single bare wildcard / identifier
        // — the kernel emits whole rows verbatim, so we can't apply a
        // narrowing projection cheaply on the GPU side. (CPU path will
        // do the projection if the user asked for one.)
        let projection_is_star = select.projection.len() == 1
            && matches!(select.projection[0], SelectItem::Wildcard(_));
        if !projection_is_star {
            return None;
        }
        let where_expr = select.selection?;

        // Narrow WHERE to `Identifier OP Literal`.
        let Expr::BinaryOp { op, left, right } = where_expr else {
            return None;
        };
        let col_name = match *left {
            Expr::Identifier(i) => i.value,
            _ => return None,
        };
        let (cmp_op, literal_str) = match (op, *right) {
            (BinaryOperator::Eq, Expr::Value(v)) => (CompareOp::Equal, value_as_str(&v)?),
            (BinaryOperator::NotEq, Expr::Value(v)) => {
                (CompareOp::NotEqual, value_as_str(&v)?)
            }
            (BinaryOperator::Gt, Expr::Value(v)) => {
                (CompareOp::GreaterThan, value_as_str(&v)?)
            }
            (BinaryOperator::Lt, Expr::Value(v)) => {
                (CompareOp::LessThan, value_as_str(&v)?)
            }
            _ => return None,
        };

        Some((col_name, cmp_op, literal_str))
    }

    fn value_as_str(v: &Value) -> Option<String> {
        match v {
            Value::SingleQuotedString(s) | Value::DoubleQuotedString(s) => Some(s.clone()),
            Value::Number(s, _) => Some(s.clone()),
            _ => None,
        }
    }

    /// Find the 0-based index of `col_name` (case-insensitive) in the
    /// header row of `body`. Returns `None` if the column isn't
    /// present. We only scan the first line — the kernel handles the
    /// rest.
    fn resolve_header_column(body: &[u8], col_name: &str) -> Option<usize> {
        let nl = body.iter().position(|&b| b == b'\n').unwrap_or(body.len());
        let mut header = &body[..nl];
        if header.last() == Some(&b'\r') {
            header = &header[..header.len() - 1];
        }
        let header_str = std::str::from_utf8(header).ok()?;
        for (i, h) in header_str.split(',').enumerate() {
            if h.eq_ignore_ascii_case(col_name) {
                return Some(i);
            }
        }
        None
    }
}

/// GPU acceleration entry point — replaced the v0.6 #41 stub in
/// v0.8 #51. Returns `Some(filtered_csv_bytes)` when the GPU path
/// both _can_ and _did_ handle the query (single-column compare on
/// header-bearing CSV); `None` means the caller should run the CPU
/// path. The CPU path is always the source of truth for output
/// formatting / projection / multi-condition WHERE / JSON Lines.
#[must_use]
pub fn select_gpu(
    sql: &str,
    body: &[u8],
    input: &SelectInputFormat,
) -> Option<Vec<u8>> {
    #[cfg(feature = "nvcomp-gpu")]
    {
        gpu::try_select_gpu(sql, body, input)
    }
    #[cfg(not(feature = "nvcomp-gpu"))]
    {
        let _ = (sql, body, input);
        None
    }
}

// =====================================================================
// Unit tests
// =====================================================================

#[cfg(test)]
mod tests {
    use super::*;

    fn csv_input() -> SelectInputFormat {
        SelectInputFormat::Csv {
            has_header: true,
            delimiter: ',',
        }
    }

    #[test]
    fn parse_select_happy_path() {
        let q = parse_select("SELECT name, age FROM s3object WHERE age > 30").unwrap();
        assert_eq!(q.projection.len(), 2);
        assert!(q.where_clause.is_some());
        assert_eq!(q.from_alias.to_lowercase(), "s3object");
    }

    #[test]
    fn parse_select_rejects_group_by() {
        let err =
            parse_select("SELECT name, COUNT(*) FROM s3object GROUP BY name").unwrap_err();
        match err {
            SelectError::UnsupportedFeature(_) => {}
            other => panic!("expected UnsupportedFeature, got {other:?}"),
        }
    }

    #[test]
    fn parse_select_rejects_join() {
        let err = parse_select("SELECT a.x FROM s3object a JOIN other b ON a.id = b.id")
            .unwrap_err();
        assert!(matches!(err, SelectError::UnsupportedFeature(_)));
    }

    #[test]
    fn parse_select_rejects_order_by() {
        let err = parse_select("SELECT name FROM s3object ORDER BY name").unwrap_err();
        assert!(matches!(err, SelectError::UnsupportedFeature(_)));
    }

    #[test]
    fn evaluate_row_eq_match() {
        let q = parse_select("SELECT name FROM s3object WHERE name = 'alice'").unwrap();
        let headers = vec!["name".to_owned(), "age".to_owned()];
        let row = CsvRow {
            fields: vec!["alice", "30"],
            headers: Some(&headers),
        };
        let r = evaluate_row(&q, &row).unwrap();
        assert_eq!(r, Some(vec!["alice".to_owned()]));

        let row2 = CsvRow {
            fields: vec!["bob", "30"],
            headers: Some(&headers),
        };
        assert_eq!(evaluate_row(&q, &row2).unwrap(), None);
    }

    #[test]
    fn evaluate_row_int_compare() {
        let q = parse_select("SELECT age FROM s3object WHERE age > 100").unwrap();
        let headers = vec!["name".to_owned(), "age".to_owned()];
        let big = CsvRow {
            fields: vec!["x", "200"],
            headers: Some(&headers),
        };
        let small = CsvRow {
            fields: vec!["x", "50"],
            headers: Some(&headers),
        };
        assert!(evaluate_row(&q, &big).unwrap().is_some());
        assert!(evaluate_row(&q, &small).unwrap().is_none());
    }

    #[test]
    fn evaluate_row_like_pattern() {
        let q = parse_select("SELECT name FROM s3object WHERE name LIKE 'foo%'").unwrap();
        let headers = vec!["name".to_owned()];
        let yes = CsvRow {
            fields: vec!["foobar"],
            headers: Some(&headers),
        };
        let no = CsvRow {
            fields: vec!["xfoobar"],
            headers: Some(&headers),
        };
        assert!(evaluate_row(&q, &yes).unwrap().is_some());
        assert!(evaluate_row(&q, &no).unwrap().is_none());
    }

    #[test]
    fn run_select_csv_end_to_end_filters_rows() {
        let body = b"name,age\nalice,30\nbob,40\ncarol,50\n";
        let out = run_select_csv(
            "SELECT name FROM s3object WHERE age > 35",
            body,
            csv_input(),
            SelectOutputFormat::Csv,
        )
        .unwrap();
        let s = std::str::from_utf8(&out).unwrap();
        let lines: Vec<&str> = s.split("\r\n").filter(|l| !l.is_empty()).collect();
        assert_eq!(lines, vec!["bob", "carol"]);
    }

    #[test]
    fn run_select_jsonlines_filter() {
        let body = b"{\"name\":\"alice\",\"age\":\"30\"}\n\
                     {\"name\":\"bob\",\"age\":\"40\"}\n\
                     {\"name\":\"carol\",\"age\":\"50\"}\n";
        let out = run_select_jsonlines(
            "SELECT name FROM s3object WHERE age > 35",
            body,
            SelectOutputFormat::Json,
        )
        .unwrap();
        let s = std::str::from_utf8(&out).unwrap();
        let lines: Vec<&str> = s.lines().filter(|l| !l.is_empty()).collect();
        assert_eq!(lines.len(), 2);
        assert!(lines[0].contains("bob"));
        assert!(lines[1].contains("carol"));
    }

    #[test]
    fn positional_column_ref() {
        let body = b"alice,30\nbob,40\n";
        let out = run_select_csv(
            "SELECT _1 FROM s3object WHERE _2 > 35",
            body,
            SelectInputFormat::Csv {
                has_header: false,
                delimiter: ',',
            },
            SelectOutputFormat::Csv,
        )
        .unwrap();
        let s = std::str::from_utf8(&out).unwrap();
        let lines: Vec<&str> = s.split("\r\n").filter(|l| !l.is_empty()).collect();
        assert_eq!(lines, vec!["bob"]);
    }

    #[test]
    fn and_or_combination() {
        let body = b"name,age,city\n\
                     alice,30,nyc\n\
                     bob,40,nyc\n\
                     carol,50,sf\n\
                     dan,25,sf\n";
        let out = run_select_csv(
            "SELECT name FROM s3object WHERE (city = 'nyc' AND age > 35) OR name = 'dan'",
            body,
            csv_input(),
            SelectOutputFormat::Csv,
        )
        .unwrap();
        let s = std::str::from_utf8(&out).unwrap();
        let mut lines: Vec<&str> = s.split("\r\n").filter(|l| !l.is_empty()).collect();
        lines.sort_unstable();
        assert_eq!(lines, vec!["bob", "dan"]);
    }

    #[test]
    fn event_stream_records_frame_format() {
        let mut w = EventStreamWriter::new();
        let frame = w.records(b"hello,world\r\n");
        let total =
            u32::from_be_bytes([frame[0], frame[1], frame[2], frame[3]]) as usize;
        assert_eq!(total, frame.len());
        let headers_len =
            u32::from_be_bytes([frame[4], frame[5], frame[6], frame[7]]) as usize;
        let prelude_crc =
            u32::from_be_bytes([frame[8], frame[9], frame[10], frame[11]]);
        assert_eq!(prelude_crc, crc32fast::hash(&frame[..8]));
        let msg_crc = u32::from_be_bytes([
            frame[total - 4],
            frame[total - 3],
            frame[total - 2],
            frame[total - 1],
        ]);
        assert_eq!(msg_crc, crc32fast::hash(&frame[..total - 4]));
        let hdr_region = &frame[12..12 + headers_len];
        let s = String::from_utf8_lossy(hdr_region);
        assert!(s.contains(":event-type"));
        assert!(s.contains("Records"));
        let payload = &frame[12 + headers_len..total - 4];
        assert_eq!(payload, b"hello,world\r\n");
    }

    #[test]
    fn event_stream_end_frame_no_payload() {
        let mut w = EventStreamWriter::new();
        let frame = w.end();
        let total =
            u32::from_be_bytes([frame[0], frame[1], frame[2], frame[3]]) as usize;
        let headers_len =
            u32::from_be_bytes([frame[4], frame[5], frame[6], frame[7]]) as usize;
        assert_eq!(total - 4 - 12 - headers_len, 0);
        let s = String::from_utf8_lossy(&frame[12..12 + headers_len]);
        assert!(s.contains("End"));
    }

    #[test]
    fn event_stream_stats_xml_payload() {
        let mut w = EventStreamWriter::new();
        let frame = w.stats(1024, 800, 64);
        let total =
            u32::from_be_bytes([frame[0], frame[1], frame[2], frame[3]]) as usize;
        let headers_len =
            u32::from_be_bytes([frame[4], frame[5], frame[6], frame[7]]) as usize;
        let payload = &frame[12 + headers_len..total - 4];
        let xml = std::str::from_utf8(payload).unwrap();
        assert!(xml.contains("<BytesScanned>1024</BytesScanned>"));
        assert!(xml.contains("<BytesProcessed>800</BytesProcessed>"));
        assert!(xml.contains("<BytesReturned>64</BytesReturned>"));
    }

    #[test]
    fn gpu_no_where_falls_through() {
        // No WHERE clause → kernel can't accelerate (it needs a
        // single-column predicate). `select_gpu` must return None
        // regardless of whether the feature is built in or not.
        let v = select_gpu(
            "SELECT * FROM s3object",
            b"name,age\nalice,30\n",
            &csv_input(),
        );
        assert!(
            v.is_none(),
            "queries without a WHERE predicate must fall through to CPU"
        );
    }

    #[test]
    fn gpu_jsonlines_falls_through() {
        // JSON Lines isn't supported by the kernel — must always
        // fall through, even when the feature is on.
        let v = select_gpu(
            "SELECT * FROM s3object WHERE country = 'Japan'",
            b"{\"country\":\"Japan\"}\n",
            &SelectInputFormat::JsonLines,
        );
        assert!(
            v.is_none(),
            "JSON Lines input must always fall through to CPU"
        );
    }

    #[test]
    fn like_match_basics() {
        assert!(like_match("foobar", "foo%"));
        assert!(!like_match("xfoobar", "foo%"));
        assert!(like_match("abc", "_b_"));
        assert!(like_match("anything", "%"));
        assert!(like_match("", ""));
        assert!(!like_match("a", ""));
    }
}