kstone-core 0.1.0

Core storage engine for KeystoneDB - LSM tree, WAL, and DynamoDB-compatible API
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
/// PartiQL parser implementation using sqlparser-rs
///
/// Parses SQL statements and converts them to our simplified AST.
/// Validates DynamoDB-specific constraints (e.g., no JOINs, no subqueries).

use crate::partiql::ast::*;
use crate::{Error, Result};
use sqlparser::ast::{self as sql_ast, Statement};
use sqlparser::dialect::GenericDialect;
use sqlparser::parser::Parser as SqlParser;

/// PartiQL statement parser
pub struct PartiQLParser;

impl PartiQLParser {
    /// Parse a PartiQL statement
    pub fn parse(sql: &str) -> Result<PartiQLStatement> {
        // Validate statement length (1-8192 chars per DynamoDB spec)
        if sql.is_empty() {
            return Err(Error::InvalidQuery("Statement cannot be empty".into()));
        }
        if sql.len() > 8192 {
            return Err(Error::InvalidQuery(format!(
                "Statement too long: {} chars (max 8192)",
                sql.len()
            )));
        }

        // Special handling for INSERT with JSON map
        if sql.trim().to_uppercase().starts_with("INSERT") && sql.contains('{') {
            return Self::parse_insert_with_json_map(sql);
        }

        // Special handling for UPDATE with REMOVE clause
        if sql.trim().to_uppercase().starts_with("UPDATE") && sql.to_uppercase().contains(" REMOVE ") {
            return Self::parse_update_with_remove(sql);
        }

        // Pre-process SQL for DynamoDB compatibility
        // DynamoDB uses VALUE (singular) instead of VALUES (plural)
        let normalized_sql = sql.replace(" VALUE ", " VALUES ");

        // Parse SQL using sqlparser
        let dialect = GenericDialect {};
        let statements = SqlParser::parse_sql(&dialect, &normalized_sql).map_err(|e| {
            Error::InvalidQuery(format!("Failed to parse SQL: {}", e))
        })?;

        // Expect exactly one statement
        if statements.is_empty() {
            return Err(Error::InvalidQuery("No statement found".into()));
        }
        if statements.len() > 1 {
            return Err(Error::InvalidQuery("Multiple statements not supported".into()));
        }

        // Convert to PartiQL AST
        Self::convert_statement(&statements[0])
    }

    /// Special parser for INSERT statements with JSON map syntax
    /// Handles: INSERT INTO table VALUE {'pk': 'value', ...}
    fn parse_insert_with_json_map(sql: &str) -> Result<PartiQLStatement> {
        // Extract table name using regex or simple parsing
        let sql_upper = sql.to_uppercase();

        // Find INSERT INTO ... VALUE
        let into_idx = sql_upper.find("INTO ").ok_or_else(|| {
            Error::InvalidQuery("INSERT requires INTO clause".into())
        })?;
        let value_idx = sql_upper.find(" VALUE ").or_else(|| sql_upper.find(" VALUES ")).ok_or_else(|| {
            Error::InvalidQuery("INSERT requires VALUE clause".into())
        })?;

        // Extract table name (between INTO and VALUE)
        let table_part = sql[into_idx + 5..value_idx].trim();
        let table_name = table_part.split_whitespace().next().ok_or_else(|| {
            Error::InvalidQuery("Could not extract table name".into())
        })?.to_string();

        // Find the JSON map (between { and })
        let brace_start = sql.find('{').ok_or_else(|| {
            Error::InvalidQuery("Expected JSON map starting with {".into())
        })?;
        let brace_end = sql.rfind('}').ok_or_else(|| {
            Error::InvalidQuery("Expected JSON map ending with }".into())
        })?;

        if brace_end <= brace_start {
            return Err(Error::InvalidQuery("Invalid JSON map syntax".into()));
        }

        let json_str = &sql[brace_start..=brace_end];
        let value_map = Self::parse_json_string(json_str)?;

        Ok(PartiQLStatement::Insert(InsertStatement {
            table_name,
            value: value_map,
        }))
    }

    /// Convert sqlparser Statement to PartiQLStatement
    fn convert_statement(stmt: &Statement) -> Result<PartiQLStatement> {
        match stmt {
            Statement::Query(query) => {
                let select_stmt = Self::convert_select(query)?;
                Ok(PartiQLStatement::Select(select_stmt))
            }
            Statement::Insert(insert) => {
                let insert_stmt = Self::convert_insert(insert)?;
                Ok(PartiQLStatement::Insert(insert_stmt))
            }
            Statement::Update {
                table,
                assignments,
                selection,
                ..
            } => {
                let update_stmt = Self::convert_update(table, assignments, selection, &[])?;
                Ok(PartiQLStatement::Update(update_stmt))
            }
            Statement::Delete(delete) => {
                let delete_stmt = Self::convert_delete(delete)?;
                Ok(PartiQLStatement::Delete(delete_stmt))
            }
            _ => Err(Error::InvalidQuery(format!(
                "Unsupported statement type: {:?}",
                stmt
            ))),
        }
    }

    /// Convert SELECT query
    fn convert_select(query: &sql_ast::Query) -> Result<SelectStatement> {
        // Validate no unsupported features
        if query.with.is_some() {
            return Err(Error::InvalidQuery("WITH clause not supported".into()));
        }
        // ORDER BY is supported, we'll handle it below
        // LIMIT and OFFSET are now supported
        if query.fetch.is_some() {
            return Err(Error::InvalidQuery("FETCH clause not supported".into()));
        }

        // Extract SELECT body
        let set_expr = match &*query.body {
            sql_ast::SetExpr::Select(select) => select,
            _ => return Err(Error::InvalidQuery("Unsupported query type (no UNION/INTERSECT/EXCEPT)".into())),
        };

        // Validate no unsupported SELECT features
        if !set_expr.cluster_by.is_empty() {
            return Err(Error::InvalidQuery("CLUSTER BY not supported".into()));
        }
        if !set_expr.distribute_by.is_empty() {
            return Err(Error::InvalidQuery("DISTRIBUTE BY not supported".into()));
        }
        if set_expr.group_by != sql_ast::GroupByExpr::Expressions(vec![], vec![]) {
            return Err(Error::InvalidQuery("GROUP BY not supported".into()));
        }
        if set_expr.having.is_some() {
            return Err(Error::InvalidQuery("HAVING clause not supported".into()));
        }
        if !set_expr.named_window.is_empty() {
            return Err(Error::InvalidQuery("Window functions not supported".into()));
        }
        if !set_expr.qualify.is_none() {
            return Err(Error::InvalidQuery("QUALIFY clause not supported".into()));
        }
        if let Some(_top) = &set_expr.top {
            return Err(Error::InvalidQuery("TOP clause not supported".into()));
        }

        // Extract FROM clause (must be single table reference)
        let (table_name, index_name) = if set_expr.from.is_empty() {
            return Err(Error::InvalidQuery("FROM clause required".into()));
        } else if set_expr.from.len() > 1 {
            return Err(Error::InvalidQuery("Multiple tables not supported (no JOINs)".into()));
        } else {
            Self::extract_table_reference(&set_expr.from[0])?
        };

        // Extract SELECT list
        let select_list = Self::convert_select_list(&set_expr.projection)?;

        // Extract WHERE clause
        let where_clause = match &set_expr.selection {
            Some(expr) => Some(Self::convert_where_clause(expr)?),
            None => None,
        };

        // Extract ORDER BY
        let order_by = match &query.order_by {
            Some(order_by_clause) => Some(Self::convert_order_by(&order_by_clause.exprs)?),
            None => None,
        };

        // Extract LIMIT
        let limit = match &query.limit {
            Some(expr) => Some(Self::extract_limit_value(expr)?),
            None => None,
        };

        // Extract OFFSET
        let offset = match &query.offset {
            Some(offset_expr) => Some(Self::extract_offset_value(&offset_expr.value)?),
            None => None,
        };

        Ok(SelectStatement {
            table_name,
            index_name,
            select_list,
            where_clause,
            order_by,
            limit,
            offset,
        })
    }

    /// Extract table name and optional index from FROM clause
    fn extract_table_reference(from: &sql_ast::TableWithJoins) -> Result<(String, Option<String>)> {
        // Check no JOINs
        if !from.joins.is_empty() {
            return Err(Error::InvalidQuery("JOIN not supported".into()));
        }

        // Extract table name
        match &from.relation {
            sql_ast::TableFactor::Table { name, .. } => {
                let table_parts: Vec<&sql_ast::Ident> = name.0.iter().collect();

                if table_parts.is_empty() {
                    return Err(Error::InvalidQuery("Empty table name".into()));
                }

                // Check for "table.index" syntax
                if table_parts.len() == 1 {
                    Ok((table_parts[0].value.clone(), None))
                } else if table_parts.len() == 2 {
                    Ok((table_parts[0].value.clone(), Some(table_parts[1].value.clone())))
                } else {
                    Err(Error::InvalidQuery(format!(
                        "Invalid table reference: expected 'table' or 'table.index', got {:?}",
                        name
                    )))
                }
            }
            _ => Err(Error::InvalidQuery("Unsupported FROM clause (subqueries not allowed)".into())),
        }
    }

    /// Convert SELECT projection list
    fn convert_select_list(projection: &[sql_ast::SelectItem]) -> Result<SelectList> {
        if projection.is_empty() {
            return Err(Error::InvalidQuery("Empty SELECT list".into()));
        }

        // Check for SELECT *
        if projection.len() == 1 {
            if let sql_ast::SelectItem::Wildcard(_) = &projection[0] {
                return Ok(SelectList::All);
            }
        }

        // Extract attribute names
        let mut attributes = Vec::new();
        for item in projection {
            match item {
                sql_ast::SelectItem::UnnamedExpr(expr) => {
                    let attr_name = Self::extract_attribute_name(expr)?;
                    attributes.push(attr_name);
                }
                sql_ast::SelectItem::ExprWithAlias { expr, alias: _ } => {
                    let attr_name = Self::extract_attribute_name(expr)?;
                    attributes.push(attr_name);
                }
                sql_ast::SelectItem::Wildcard(_) => {
                    return Err(Error::InvalidQuery("Cannot mix * with other columns".into()));
                }
                _ => {
                    return Err(Error::InvalidQuery("Unsupported SELECT item".into()));
                }
            }
        }

        Ok(SelectList::Attributes(attributes))
    }

    /// Extract attribute name from expression
    fn extract_attribute_name(expr: &sql_ast::Expr) -> Result<String> {
        match expr {
            sql_ast::Expr::Identifier(ident) => Ok(ident.value.clone()),
            sql_ast::Expr::CompoundIdentifier(parts) => {
                if parts.len() == 1 {
                    Ok(parts[0].value.clone())
                } else {
                    Err(Error::InvalidQuery(format!(
                        "Compound identifiers not supported: {:?}",
                        parts
                    )))
                }
            }
            _ => Err(Error::InvalidQuery(format!(
                "Unsupported expression in SELECT list: {:?}",
                expr
            ))),
        }
    }

    /// Convert WHERE clause expression to WhereClause
    fn convert_where_clause(expr: &sql_ast::Expr) -> Result<WhereClause> {
        let mut conditions = Vec::new();
        Self::extract_conditions(expr, &mut conditions)?;
        Ok(WhereClause { conditions })
    }

    /// Recursively extract AND-connected conditions
    fn extract_conditions(expr: &sql_ast::Expr, conditions: &mut Vec<Condition>) -> Result<()> {
        match expr {
            sql_ast::Expr::BinaryOp { left, op, right } => {
                use sqlparser::ast::BinaryOperator;

                match op {
                    BinaryOperator::And => {
                        // Recursively extract from both sides
                        Self::extract_conditions(left, conditions)?;
                        Self::extract_conditions(right, conditions)?;
                    }
                    BinaryOperator::Or => {
                        return Err(Error::InvalidQuery("OR not supported in WHERE clause (use AND only)".into()));
                    }
                    BinaryOperator::Eq
                    | BinaryOperator::NotEq
                    | BinaryOperator::Lt
                    | BinaryOperator::LtEq
                    | BinaryOperator::Gt
                    | BinaryOperator::GtEq => {
                        // Extract comparison condition
                        let condition = Self::convert_comparison(left, op, right)?;
                        conditions.push(condition);
                    }
                    _ => {
                        return Err(Error::InvalidQuery(format!(
                            "Unsupported operator in WHERE clause: {:?}",
                            op
                        )));
                    }
                }
            }
            sql_ast::Expr::InList { expr, list, negated } => {
                if *negated {
                    return Err(Error::InvalidQuery("NOT IN not supported".into()));
                }
                let condition = Self::convert_in_condition(expr, list)?;
                conditions.push(condition);
            }
            sql_ast::Expr::Between { expr, negated, low, high } => {
                if *negated {
                    return Err(Error::InvalidQuery("NOT BETWEEN not supported".into()));
                }
                let condition = Self::convert_between_condition(expr, low, high)?;
                conditions.push(condition);
            }
            _ => {
                return Err(Error::InvalidQuery(format!(
                    "Unsupported WHERE clause expression: {:?}",
                    expr
                )));
            }
        }
        Ok(())
    }

    /// Convert comparison expression to Condition
    fn convert_comparison(
        left: &sql_ast::Expr,
        op: &sql_ast::BinaryOperator,
        right: &sql_ast::Expr,
    ) -> Result<Condition> {
        use sqlparser::ast::BinaryOperator;

        let attribute = Self::extract_attribute_name(left)?;
        let compare_op = match op {
            BinaryOperator::Eq => CompareOp::Equal,
            BinaryOperator::NotEq => CompareOp::NotEqual,
            BinaryOperator::Lt => CompareOp::LessThan,
            BinaryOperator::LtEq => CompareOp::LessThanOrEqual,
            BinaryOperator::Gt => CompareOp::GreaterThan,
            BinaryOperator::GtEq => CompareOp::GreaterThanOrEqual,
            _ => unreachable!(),
        };
        let value = Self::convert_value(right)?;

        Ok(Condition {
            attribute,
            operator: compare_op,
            value,
        })
    }

    /// Convert IN condition
    fn convert_in_condition(
        expr: &sql_ast::Expr,
        list: &[sql_ast::Expr],
    ) -> Result<Condition> {
        let attribute = Self::extract_attribute_name(expr)?;
        let values: Result<Vec<SqlValue>> = list.iter().map(Self::convert_value).collect();

        Ok(Condition {
            attribute,
            operator: CompareOp::In,
            value: SqlValue::List(values?),
        })
    }

    /// Convert BETWEEN condition
    fn convert_between_condition(
        expr: &sql_ast::Expr,
        low: &sql_ast::Expr,
        high: &sql_ast::Expr,
    ) -> Result<Condition> {
        let attribute = Self::extract_attribute_name(expr)?;
        let low_val = Self::convert_value(low)?;
        let high_val = Self::convert_value(high)?;

        Ok(Condition {
            attribute,
            operator: CompareOp::Between,
            value: SqlValue::List(vec![low_val, high_val]),
        })
    }

    /// Convert SQL expression to SqlValue
    fn convert_value(expr: &sql_ast::Expr) -> Result<SqlValue> {
        match expr {
            sql_ast::Expr::Value(val) => Self::convert_sql_value(val),
            _ => Err(Error::InvalidQuery(format!(
                "Unsupported value expression: {:?}",
                expr
            ))),
        }
    }

    /// Convert sqlparser Value to SqlValue
    fn convert_sql_value(val: &sql_ast::Value) -> Result<SqlValue> {
        match val {
            sql_ast::Value::Number(n, _) => Ok(SqlValue::Number(n.clone())),
            sql_ast::Value::SingleQuotedString(s) | sql_ast::Value::DoubleQuotedString(s) => {
                Ok(SqlValue::String(s.clone()))
            }
            sql_ast::Value::Boolean(b) => Ok(SqlValue::Boolean(*b)),
            sql_ast::Value::Null => Ok(SqlValue::Null),
            _ => Err(Error::InvalidQuery(format!("Unsupported SQL value: {:?}", val))),
        }
    }

    /// Convert ORDER BY clause
    fn convert_order_by(order_by: &[sql_ast::OrderByExpr]) -> Result<OrderBy> {
        if order_by.is_empty() {
            return Err(Error::InvalidQuery("Empty ORDER BY clause".into()));
        }
        if order_by.len() > 1 {
            return Err(Error::InvalidQuery("ORDER BY multiple columns not supported".into()));
        }

        let expr = &order_by[0];
        let attribute = Self::extract_attribute_name(&expr.expr)?;
        let ascending = expr.asc.unwrap_or(true);

        Ok(OrderBy {
            attribute,
            ascending,
        })
    }

    /// Convert INSERT statement
    fn convert_insert(insert: &sql_ast::Insert) -> Result<InsertStatement> {
        // Extract table name
        let table_name = match &insert.table_name {
            sql_ast::ObjectName(parts) => {
                if parts.is_empty() {
                    return Err(Error::InvalidQuery("Empty table name in INSERT".into()));
                }
                parts[0].value.clone()
            }
        };

        // Extract VALUE clause
        // INSERT INTO table VALUE {...} uses the source field
        let value_map = match &insert.source {
            Some(source) => {
                // source is a Box<Query>
                // We expect VALUES clause with a single row
                match &*source.body {
                    sql_ast::SetExpr::Values(values) => {
                        if values.rows.is_empty() {
                            return Err(Error::InvalidQuery("INSERT requires VALUE clause".into()));
                        }
                        if values.rows.len() > 1 {
                            return Err(Error::InvalidQuery(
                                "INSERT can only insert one item at a time".into(),
                            ));
                        }

                        // Parse the single row as a map literal
                        Self::parse_map_literal(&values.rows[0])?
                    }
                    _ => {
                        return Err(Error::InvalidQuery(
                            "INSERT only supports VALUE clause, not SELECT".into(),
                        ));
                    }
                }
            }
            None => return Err(Error::InvalidQuery("INSERT requires VALUE clause".into())),
        };

        Ok(InsertStatement {
            table_name,
            value: value_map,
        })
    }

    /// Parse a map literal from PartiQL VALUE clause
    /// Expects something like: {'pk': 'value', 'name': 'Alice', 'age': 30}
    fn parse_map_literal(row: &[sql_ast::Expr]) -> Result<SqlValue> {
        // Expect a single expression that's a struct/function call representing the map
        // sqlparser might parse {...} differently depending on version

        if row.len() != 1 {
            return Err(Error::InvalidQuery(format!(
                "Expected single map literal in INSERT, got {} expressions",
                row.len()
            )));
        }

        // Try to extract the map from the expression
        // This is a bit tricky - sqlparser might parse it as:
        // - A function call with name being empty and args being the key-value pairs
        // - A composite type
        // - Or something else

        match &row[0] {
            // Check if it's parsed as a function/type constructor
            sql_ast::Expr::Function(func) => {
                // Extract key-value pairs from function arguments
                Self::parse_function_as_map(func)
            }
            // Check if it's a JSON-like expression (newer sqlparser versions)
            sql_ast::Expr::JsonAccess { .. } | sql_ast::Expr::CompositeAccess { .. } => {
                Err(Error::InvalidQuery(
                    "Composite/JSON access expressions not yet supported for INSERT".into(),
                ))
            }
            // Try to handle as a string that can be JSON-parsed
            sql_ast::Expr::Value(sql_ast::Value::SingleQuotedString(s)) => {
                Self::parse_json_string(s)
            }
            _ => {
                Err(Error::InvalidQuery(format!(
                    "Unsupported INSERT VALUE format. Expression type: {:?}",
                    row[0]
                )))
            }
        }
    }

    /// Parse a function expression as a map (for constructs like MAP(...))
    fn parse_function_as_map(func: &sql_ast::Function) -> Result<SqlValue> {
        // If function name is empty or is "MAP", treat arguments as key-value pairs
        let mut map = std::collections::HashMap::new();

        // Access the args based on FunctionArguments enum
        let args_list = match &func.args {
            sql_ast::FunctionArguments::List(args) => &args.args,
            _ => return Err(Error::InvalidQuery("Expected argument list in function".into())),
        };

        for arg in args_list {
            match arg {
                sql_ast::FunctionArg::Unnamed(expr_wrapper) => {
                    let expr = match expr_wrapper {
                        sql_ast::FunctionArgExpr::Expr(e) => e,
                        _ => return Err(Error::InvalidQuery("Expected expression in argument".into())),
                    };

                    // Each arg might be a binary expression like 'key' = 'value'
                    if let sql_ast::Expr::BinaryOp { left, op, right } = expr {
                        if matches!(op, sql_ast::BinaryOperator::Eq) {
                            let key = Self::extract_string_literal(&**left)?;
                            let value = Self::convert_value(&**right)?;
                            map.insert(key, value);
                        } else {
                            return Err(Error::InvalidQuery(
                                "Expected key = value pairs in map".into(),
                            ));
                        }
                    } else {
                        return Err(Error::InvalidQuery(
                            "Expected key = value pairs in map".into(),
                        ));
                    }
                }
                _ => {
                    return Err(Error::InvalidQuery(
                        "Unsupported function argument in map".into(),
                    ));
                }
            }
        }

        Ok(SqlValue::Map(map))
    }

    /// Extract a string literal from an expression
    fn extract_string_literal(expr: &sql_ast::Expr) -> Result<String> {
        match expr {
            sql_ast::Expr::Value(sql_ast::Value::SingleQuotedString(s))
            | sql_ast::Expr::Value(sql_ast::Value::DoubleQuotedString(s)) => Ok(s.clone()),
            sql_ast::Expr::Identifier(ident) => Ok(ident.value.clone()),
            _ => Err(Error::InvalidQuery(format!(
                "Expected string literal, got: {:?}",
                expr
            ))),
        }
    }

    /// Extract LIMIT value from expression
    fn extract_limit_value(expr: &sql_ast::Expr) -> Result<usize> {
        match expr {
            sql_ast::Expr::Value(sql_ast::Value::Number(n, _)) => {
                n.parse::<usize>().map_err(|_| {
                    Error::InvalidQuery(format!("Invalid LIMIT value: {}", n))
                })
            }
            _ => Err(Error::InvalidQuery(format!(
                "LIMIT must be a positive integer, got: {:?}",
                expr
            ))),
        }
    }

    /// Extract OFFSET value from expression
    fn extract_offset_value(expr: &sql_ast::Expr) -> Result<usize> {
        match expr {
            sql_ast::Expr::Value(sql_ast::Value::Number(n, _)) => {
                n.parse::<usize>().map_err(|_| {
                    Error::InvalidQuery(format!("Invalid OFFSET value: {}", n))
                })
            }
            _ => Err(Error::InvalidQuery(format!(
                "OFFSET must be a positive integer, got: {:?}",
                expr
            ))),
        }
    }

    /// Parse a JSON string as SqlValue::Map
    fn parse_json_string(s: &str) -> Result<SqlValue> {
        // DynamoDB uses single quotes, but JSON requires double quotes
        // Convert single quotes to double quotes (simple approach - may need refinement)
        let json_normalized = s.replace('\'', "\"");

        let json_value: serde_json::Value = serde_json::from_str(&json_normalized).map_err(|e| {
            Error::InvalidQuery(format!("Failed to parse JSON: {}", e))
        })?;

        Self::json_to_sql_value(&json_value)
    }

    /// Convert serde_json::Value to SqlValue
    fn json_to_sql_value(value: &serde_json::Value) -> Result<SqlValue> {
        match value {
            serde_json::Value::Null => Ok(SqlValue::Null),
            serde_json::Value::Bool(b) => Ok(SqlValue::Boolean(*b)),
            serde_json::Value::Number(n) => Ok(SqlValue::Number(n.to_string())),
            serde_json::Value::String(s) => Ok(SqlValue::String(s.clone())),
            serde_json::Value::Array(arr) => {
                let items: Result<Vec<SqlValue>> = arr.iter().map(Self::json_to_sql_value).collect();
                Ok(SqlValue::List(items?))
            }
            serde_json::Value::Object(obj) => {
                let mut map = std::collections::HashMap::new();
                for (k, v) in obj {
                    map.insert(k.clone(), Self::json_to_sql_value(v)?);
                }
                Ok(SqlValue::Map(map))
            }
        }
    }

    /// Convert DELETE statement
    fn convert_delete(delete: &sql_ast::Delete) -> Result<DeleteStatement> {
        // Extract table name from DELETE FROM clause
        // FromTable enum has WithFromKeyword and WithoutKeyword variants
        let from_tables = match &delete.from {
            sql_ast::FromTable::WithFromKeyword(tables) => tables,
            sql_ast::FromTable::WithoutKeyword(tables) => tables,
        };

        if from_tables.is_empty() {
            return Err(Error::InvalidQuery("DELETE requires table name".into()));
        }

        // Same extraction logic as SELECT FROM clause
        let (table_name, index_name) = Self::extract_table_reference(&from_tables[0])?;

        // DELETE doesn't support index queries
        if index_name.is_some() {
            return Err(Error::InvalidQuery("DELETE does not support index syntax".into()));
        }

        // Extract WHERE clause (required for DELETE)
        let where_clause = match &delete.selection {
            Some(expr) => Self::convert_where_clause(expr)?,
            None => return Err(Error::InvalidQuery("DELETE requires WHERE clause".into())),
        };

        Ok(DeleteStatement {
            table_name,
            where_clause,
        })
    }

    /// Special parser for UPDATE statements with REMOVE clause
    /// Handles: UPDATE table SET ... REMOVE attr1, attr2 WHERE ...
    /// Also handles: UPDATE table REMOVE attr1, attr2 WHERE ... (REMOVE only)
    fn parse_update_with_remove(sql: &str) -> Result<PartiQLStatement> {
        let sql_upper = sql.to_uppercase();

        // Find REMOVE and WHERE positions
        let remove_idx = sql_upper.find(" REMOVE ").ok_or_else(|| {
            Error::InvalidQuery("Expected REMOVE clause".into())
        })?;
        let where_idx = sql_upper.find(" WHERE ").ok_or_else(|| {
            Error::InvalidQuery("UPDATE requires WHERE clause".into())
        })?;

        if where_idx <= remove_idx {
            return Err(Error::InvalidQuery("REMOVE must come before WHERE".into()));
        }

        // Extract REMOVE attributes (between REMOVE and WHERE)
        let remove_part = &sql[remove_idx + 8..where_idx].trim();
        let remove_attributes: Vec<String> = remove_part
            .split(',')
            .map(|s| s.trim().to_string())
            .filter(|s| !s.is_empty())
            .collect();

        if remove_attributes.is_empty() {
            return Err(Error::InvalidQuery("REMOVE requires at least one attribute".into()));
        }

        // Check if there's a SET clause before REMOVE
        let before_remove = &sql[..remove_idx];
        let has_set = before_remove.to_uppercase().contains(" SET ");

        // Build modified SQL without REMOVE clause for sqlparser
        let sql_without_remove = if has_set {
            // Has SET: just remove REMOVE clause
            format!(
                "{} {}",
                &sql[..remove_idx].trim(),
                &sql[where_idx..].trim()
            )
        } else {
            // No SET: add dummy SET clause so sqlparser accepts it
            // We'll ignore the dummy assignment in convert_update
            format!(
                "{} SET __dummy__ = 0 {}",
                &sql[..remove_idx].trim(),
                &sql[where_idx..].trim()
            )
        };

        // Parse the modified SQL
        let dialect = GenericDialect {};
        let statements = SqlParser::parse_sql(&dialect, &sql_without_remove).map_err(|e| {
            Error::InvalidQuery(format!("Failed to parse UPDATE: {}", e))
        })?;

        if statements.is_empty() {
            return Err(Error::InvalidQuery("No statement found".into()));
        }

        // Extract UPDATE components
        match &statements[0] {
            Statement::Update {
                table,
                assignments,
                selection,
                ..
            } => {
                let update_stmt = Self::convert_update(table, assignments, selection, &remove_attributes)?;
                Ok(PartiQLStatement::Update(update_stmt))
            }
            _ => Err(Error::InvalidQuery("Expected UPDATE statement".into())),
        }
    }

    /// Convert UPDATE statement
    fn convert_update(
        table: &sql_ast::TableWithJoins,
        assignments: &[sql_ast::Assignment],
        selection: &Option<sql_ast::Expr>,
        remove_attributes: &[String],
    ) -> Result<UpdateStatement> {
        // Extract table name
        let (table_name, index_name) = Self::extract_table_reference(table)?;

        // UPDATE doesn't support index queries
        if index_name.is_some() {
            return Err(Error::InvalidQuery("UPDATE does not support index syntax".into()));
        }

        // Extract WHERE clause (required for UPDATE)
        let where_clause = match selection {
            Some(expr) => Self::convert_where_clause(expr)?,
            None => return Err(Error::InvalidQuery("UPDATE requires WHERE clause".into())),
        };

        // Convert SET assignments, filtering out dummy assignments
        let set_assignments: Vec<SetAssignment> = assignments
            .iter()
            .map(Self::convert_assignment)
            .collect::<Result<Vec<_>>>()?
            .into_iter()
            .filter(|a| a.attribute != "__dummy__")
            .collect();

        Ok(UpdateStatement {
            table_name,
            where_clause,
            set_assignments,
            remove_attributes: remove_attributes.to_vec(),
        })
    }

    /// Convert a SET assignment
    fn convert_assignment(assignment: &sql_ast::Assignment) -> Result<SetAssignment> {
        // Extract attribute name from target
        let attribute = match &assignment.target {
            sql_ast::AssignmentTarget::ColumnName(name) => {
                match name {
                    sql_ast::ObjectName(parts) => {
                        if parts.is_empty() {
                            return Err(Error::InvalidQuery("Empty attribute name".into()));
                        }
                        parts[0].value.clone()
                    }
                }
            }
            _ => return Err(Error::InvalidQuery("Unsupported assignment target".into())),
        };

        // Convert value expression
        let value = Self::convert_set_value(&assignment.value)?;

        Ok(SetAssignment { attribute, value })
    }

    /// Convert SET value (literal or arithmetic expression)
    fn convert_set_value(expr: &sql_ast::Expr) -> Result<SetValue> {
        match expr {
            // Literal value
            sql_ast::Expr::Value(v) => {
                let sql_value = Self::convert_sql_value(v)?;
                Ok(SetValue::Literal(sql_value))
            }
            // Binary operation (attribute +/- value)
            sql_ast::Expr::BinaryOp { left, op, right } => {
                // Left side should be an identifier (attribute name)
                let attribute = match &**left {
                    sql_ast::Expr::Identifier(ident) => ident.value.clone(),
                    _ => return Err(Error::InvalidQuery(
                        "Arithmetic expression left side must be an attribute".into()
                    )),
                };

                // Right side should be a literal value
                let value = match &**right {
                    sql_ast::Expr::Value(v) => Self::convert_sql_value(v)?,
                    _ => return Err(Error::InvalidQuery(
                        "Arithmetic expression right side must be a literal".into()
                    )),
                };

                // Check operator
                match op {
                    sql_ast::BinaryOperator::Plus => Ok(SetValue::Add { attribute, value }),
                    sql_ast::BinaryOperator::Minus => Ok(SetValue::Subtract { attribute, value }),
                    _ => Err(Error::InvalidQuery(format!(
                        "Unsupported arithmetic operator in SET: {:?}",
                        op
                    ))),
                }
            }
            _ => Err(Error::InvalidQuery(format!(
                "Unsupported SET value expression: {:?}",
                expr
            ))),
        }
    }
}

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

    #[test]
    fn test_parse_simple_select() {
        let sql = "SELECT * FROM users WHERE pk = 'user#123'";
        let stmt = PartiQLParser::parse(sql).unwrap();

        match stmt {
            PartiQLStatement::Select(select) => {
                assert_eq!(select.table_name, "users");
                assert_eq!(select.index_name, None);
                assert_eq!(select.select_list, SelectList::All);

                let where_clause = select.where_clause.unwrap();
                assert_eq!(where_clause.conditions.len(), 1);
                assert_eq!(where_clause.conditions[0].attribute, "pk");
                assert_eq!(where_clause.conditions[0].operator, CompareOp::Equal);
            }
            _ => panic!("Expected SELECT statement"),
        }
    }

    #[test]
    fn test_parse_select_with_index() {
        let sql = "SELECT * FROM users.email_index WHERE pk = 'org#acme'";
        let stmt = PartiQLParser::parse(sql).unwrap();

        match stmt {
            PartiQLStatement::Select(select) => {
                assert_eq!(select.table_name, "users");
                assert_eq!(select.index_name, Some("email_index".to_string()));
            }
            _ => panic!("Expected SELECT statement"),
        }
    }

    #[test]
    fn test_parse_select_with_attributes() {
        let sql = "SELECT name, age FROM users WHERE pk = 'user#123'";
        let stmt = PartiQLParser::parse(sql).unwrap();

        match stmt {
            PartiQLStatement::Select(select) => {
                match select.select_list {
                    SelectList::Attributes(attrs) => {
                        assert_eq!(attrs, vec!["name", "age"]);
                    }
                    _ => panic!("Expected attribute list"),
                }
            }
            _ => panic!("Expected SELECT statement"),
        }
    }

    #[test]
    fn test_parse_select_with_order_by() {
        let sql = "SELECT * FROM users WHERE pk = 'user#123' ORDER BY sk DESC";
        let stmt = PartiQLParser::parse(sql).unwrap();

        match stmt {
            PartiQLStatement::Select(select) => {
                let order_by = select.order_by.unwrap();
                assert_eq!(order_by.attribute, "sk");
                assert_eq!(order_by.ascending, false);
            }
            _ => panic!("Expected SELECT statement"),
        }
    }

    #[test]
    fn test_parse_select_with_in() {
        let sql = "SELECT * FROM users WHERE pk IN ('user#1', 'user#2')";
        let stmt = PartiQLParser::parse(sql).unwrap();

        match stmt {
            PartiQLStatement::Select(select) => {
                let where_clause = select.where_clause.unwrap();
                assert_eq!(where_clause.conditions[0].operator, CompareOp::In);
            }
            _ => panic!("Expected SELECT statement"),
        }
    }

    #[test]
    fn test_parse_select_with_between() {
        let sql = "SELECT * FROM users WHERE pk = 'user#123' AND age BETWEEN 18 AND 65";
        let stmt = PartiQLParser::parse(sql).unwrap();

        match stmt {
            PartiQLStatement::Select(select) => {
                let where_clause = select.where_clause.unwrap();
                assert_eq!(where_clause.conditions.len(), 2);

                // Find BETWEEN condition
                let between_cond = where_clause.conditions.iter()
                    .find(|c| c.operator == CompareOp::Between)
                    .unwrap();
                assert_eq!(between_cond.attribute, "age");
            }
            _ => panic!("Expected SELECT statement"),
        }
    }

    #[test]
    fn test_reject_join() {
        let sql = "SELECT * FROM users JOIN orders ON users.pk = orders.user_id";
        let result = PartiQLParser::parse(sql);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("JOIN"));
    }

    #[test]
    fn test_reject_or() {
        let sql = "SELECT * FROM users WHERE pk = 'user#123' OR pk = 'user#456'";
        let result = PartiQLParser::parse(sql);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("OR"));
    }

    #[test]
    fn test_reject_group_by() {
        let sql = "SELECT COUNT(*) FROM users GROUP BY status";
        let result = PartiQLParser::parse(sql);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("GROUP BY"));
    }

    #[test]
    fn test_reject_too_long() {
        let sql = "SELECT * FROM users WHERE pk = '".to_string() + &"x".repeat(10000) + "'";
        let result = PartiQLParser::parse(&sql);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("too long"));
    }

    // DELETE tests
    #[test]
    fn test_parse_delete_with_pk() {
        let sql = "DELETE FROM users WHERE pk = 'user#123'";
        let stmt = PartiQLParser::parse(sql).unwrap();

        match stmt {
            PartiQLStatement::Delete(delete) => {
                assert_eq!(delete.table_name, "users");
                assert_eq!(delete.where_clause.conditions.len(), 1);
                assert_eq!(delete.where_clause.conditions[0].attribute, "pk");
                assert_eq!(delete.where_clause.conditions[0].operator, CompareOp::Equal);
            }
            _ => panic!("Expected DELETE statement"),
        }
    }

    #[test]
    fn test_parse_delete_with_pk_and_sk() {
        let sql = "DELETE FROM users WHERE pk = 'user#123' AND sk = 'profile'";
        let stmt = PartiQLParser::parse(sql).unwrap();

        match stmt {
            PartiQLStatement::Delete(delete) => {
                assert_eq!(delete.table_name, "users");
                assert_eq!(delete.where_clause.conditions.len(), 2);

                let pk_cond = delete.where_clause.get_condition("pk").unwrap();
                assert_eq!(pk_cond.operator, CompareOp::Equal);

                let sk_cond = delete.where_clause.get_condition("sk").unwrap();
                assert_eq!(sk_cond.operator, CompareOp::Equal);
            }
            _ => panic!("Expected DELETE statement"),
        }
    }

    #[test]
    fn test_reject_delete_without_where() {
        let sql = "DELETE FROM users";
        let result = PartiQLParser::parse(sql);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("WHERE"));
    }

    // INSERT tests
    #[test]
    fn test_parse_insert_simple() {
        let sql = "INSERT INTO users VALUE {'pk': 'user#123', 'name': 'Alice', 'age': 30}";
        let stmt = PartiQLParser::parse(sql).unwrap();

        match stmt {
            PartiQLStatement::Insert(insert) => {
                assert_eq!(insert.table_name, "users");
                match &insert.value {
                    SqlValue::Map(map) => {
                        assert_eq!(map.len(), 3);
                        assert_eq!(map.get("pk"), Some(&SqlValue::String("user#123".to_string())));
                        assert_eq!(map.get("name"), Some(&SqlValue::String("Alice".to_string())));
                        assert_eq!(map.get("age"), Some(&SqlValue::Number("30".to_string())));
                    }
                    _ => panic!("Expected Map value"),
                }
            }
            _ => panic!("Expected INSERT statement"),
        }
    }

    #[test]
    fn test_parse_insert_with_sk() {
        let sql = "INSERT INTO users VALUE {'pk': 'user#123', 'sk': 'profile', 'email': 'alice@example.com'}";
        let stmt = PartiQLParser::parse(sql).unwrap();

        match stmt {
            PartiQLStatement::Insert(insert) => {
                assert_eq!(insert.table_name, "users");
                match &insert.value {
                    SqlValue::Map(map) => {
                        assert!(map.contains_key("pk"));
                        assert!(map.contains_key("sk"));
                        assert_eq!(map.get("sk"), Some(&SqlValue::String("profile".to_string())));
                    }
                    _ => panic!("Expected Map value"),
                }
            }
            _ => panic!("Expected INSERT statement"),
        }
    }

    #[test]
    fn test_parse_insert_nested_values() {
        let sql = r#"INSERT INTO users VALUE {'pk': 'user#123', 'profile': {'name': 'Alice', 'age': 30}, 'tags': ['admin', 'active']}"#;
        let stmt = PartiQLParser::parse(sql).unwrap();

        match stmt {
            PartiQLStatement::Insert(insert) => {
                match &insert.value {
                    SqlValue::Map(map) => {
                        // Check nested map
                        match map.get("profile") {
                            Some(SqlValue::Map(profile)) => {
                                assert_eq!(profile.get("name"), Some(&SqlValue::String("Alice".to_string())));
                            }
                            _ => panic!("Expected nested map for profile"),
                        }

                        // Check list
                        match map.get("tags") {
                            Some(SqlValue::List(tags)) => {
                                assert_eq!(tags.len(), 2);
                            }
                            _ => panic!("Expected list for tags"),
                        }
                    }
                    _ => panic!("Expected Map value"),
                }
            }
            _ => panic!("Expected INSERT statement"),
        }
    }

    #[test]
    fn test_parse_insert_various_types() {
        let sql = "INSERT INTO items VALUE {'pk': 'item#1', 'price': 29.99, 'active': true, 'description': null}";
        let stmt = PartiQLParser::parse(sql).unwrap();

        match stmt {
            PartiQLStatement::Insert(insert) => {
                match &insert.value {
                    SqlValue::Map(map) => {
                        // Number
                        match map.get("price") {
                            Some(SqlValue::Number(n)) => assert_eq!(n, "29.99"),
                            _ => panic!("Expected number for price"),
                        }

                        // Boolean
                        assert_eq!(map.get("active"), Some(&SqlValue::Boolean(true)));

                        // Null
                        assert_eq!(map.get("description"), Some(&SqlValue::Null));
                    }
                    _ => panic!("Expected Map value"),
                }
            }
            _ => panic!("Expected INSERT statement"),
        }
    }

    #[test]
    fn test_reject_insert_without_map() {
        let sql = "INSERT INTO users VALUE 'not a map'";
        let result = PartiQLParser::parse(sql);
        assert!(result.is_err());
    }

    // UPDATE tests
    #[test]
    fn test_parse_update_simple() {
        let sql = "UPDATE users SET name = 'Alice', age = 30 WHERE pk = 'user#123'";
        let stmt = PartiQLParser::parse(sql).unwrap();

        match stmt {
            PartiQLStatement::Update(update) => {
                assert_eq!(update.table_name, "users");
                assert_eq!(update.set_assignments.len(), 2);
                assert_eq!(update.remove_attributes.len(), 0);

                // Check first assignment
                assert_eq!(update.set_assignments[0].attribute, "name");
                match &update.set_assignments[0].value {
                    SetValue::Literal(SqlValue::String(s)) => assert_eq!(s, "Alice"),
                    _ => panic!("Expected string literal"),
                }

                // Check WHERE clause
                assert!(update.where_clause.has_condition("pk"));
            }
            _ => panic!("Expected UPDATE statement"),
        }
    }

    #[test]
    fn test_parse_update_with_arithmetic() {
        let sql = "UPDATE users SET age = age + 1, count = count - 5 WHERE pk = 'user#123'";
        let stmt = PartiQLParser::parse(sql).unwrap();

        match stmt {
            PartiQLStatement::Update(update) => {
                assert_eq!(update.set_assignments.len(), 2);

                // Check Add operation
                match &update.set_assignments[0].value {
                    SetValue::Add { attribute, value } => {
                        assert_eq!(attribute, "age");
                        match value {
                            SqlValue::Number(n) => assert_eq!(n, "1"),
                            _ => panic!("Expected number"),
                        }
                    }
                    _ => panic!("Expected Add operation"),
                }

                // Check Subtract operation
                match &update.set_assignments[1].value {
                    SetValue::Subtract { attribute, value } => {
                        assert_eq!(attribute, "count");
                        match value {
                            SqlValue::Number(n) => assert_eq!(n, "5"),
                            _ => panic!("Expected number"),
                        }
                    }
                    _ => panic!("Expected Subtract operation"),
                }
            }
            _ => panic!("Expected UPDATE statement"),
        }
    }

    #[test]
    fn test_parse_update_with_remove() {
        let sql = "UPDATE users SET name = 'Alice' REMOVE tags, metadata WHERE pk = 'user#123'";
        let stmt = PartiQLParser::parse(sql).unwrap();

        match stmt {
            PartiQLStatement::Update(update) => {
                assert_eq!(update.table_name, "users");
                assert_eq!(update.set_assignments.len(), 1);
                assert_eq!(update.remove_attributes.len(), 2);
                assert_eq!(update.remove_attributes[0], "tags");
                assert_eq!(update.remove_attributes[1], "metadata");
            }
            _ => panic!("Expected UPDATE statement"),
        }
    }

    #[test]
    fn test_parse_update_remove_only() {
        let sql = "UPDATE users REMOVE tags, metadata WHERE pk = 'user#123' AND sk = 'profile'";
        let stmt = PartiQLParser::parse(sql).unwrap();

        match stmt {
            PartiQLStatement::Update(update) => {
                assert_eq!(update.set_assignments.len(), 0);
                assert_eq!(update.remove_attributes.len(), 2);
                assert_eq!(update.where_clause.conditions.len(), 2);
            }
            _ => panic!("Expected UPDATE statement"),
        }
    }

    #[test]
    fn test_reject_update_without_where() {
        let sql = "UPDATE users SET name = 'Alice'";
        let result = PartiQLParser::parse(sql);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("WHERE"));
    }

    #[test]
    fn test_parse_select_with_limit() {
        let sql = "SELECT * FROM users WHERE pk = 'user#123' LIMIT 10";
        let stmt = PartiQLParser::parse(sql).unwrap();

        match stmt {
            PartiQLStatement::Select(select) => {
                assert_eq!(select.table_name, "users");
                assert_eq!(select.limit, Some(10));
                assert_eq!(select.offset, None);
            }
            _ => panic!("Expected SELECT statement"),
        }
    }

    #[test]
    fn test_parse_select_with_offset() {
        let sql = "SELECT * FROM users WHERE pk = 'user#123' OFFSET 5";
        let stmt = PartiQLParser::parse(sql).unwrap();

        match stmt {
            PartiQLStatement::Select(select) => {
                assert_eq!(select.table_name, "users");
                assert_eq!(select.limit, None);
                assert_eq!(select.offset, Some(5));
            }
            _ => panic!("Expected SELECT statement"),
        }
    }

    #[test]
    fn test_parse_select_with_limit_and_offset() {
        let sql = "SELECT * FROM users WHERE pk = 'user#123' LIMIT 20 OFFSET 10";
        let stmt = PartiQLParser::parse(sql).unwrap();

        match stmt {
            PartiQLStatement::Select(select) => {
                assert_eq!(select.table_name, "users");
                assert_eq!(select.limit, Some(20));
                assert_eq!(select.offset, Some(10));
            }
            _ => panic!("Expected SELECT statement"),
        }
    }
}