sqlite_wasm_reader 0.3.1

A pure Rust SQLite reader library for WASI environments
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
//! SQL query parsing and execution for SELECT statements

use crate::{Error, Result, Value, Row};
use std::collections::HashMap;
use sqlparser::parser::Parser;
use sqlparser::dialect::SQLiteDialect;
use sqlparser::ast::{Statement, Query, SetExpr, Select, SelectItem, TableFactor, Expr as SqlExpr, BinaryOperator, Value as SqlValue};

#[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))]
use alloc::{vec::Vec, string::String, format, boxed::Box};

/// Represents a parsed SELECT query
#[derive(Debug, Clone)]
pub struct SelectQuery {
    /// Columns to select (None means SELECT *)
    pub columns: Option<Vec<String>>,
    /// Table name
    pub table: String,
    /// WHERE clause root expression
    pub where_expr: Option<Expr>,
    /// ORDER BY clause
    pub order_by: Option<OrderBy>,
    /// LIMIT clause
    pub limit: Option<usize>,
}

/// Expression for WHERE clause
#[derive(Debug, Clone)]
pub enum Expr {
    /// Comparison: column op value
    Comparison {
        column: String,
        operator: ComparisonOperator,
        value: Value,
    },
    /// Logical AND
    And(Box<Expr>, Box<Expr>),
    /// Logical OR
    Or(Box<Expr>, Box<Expr>),
    /// Logical NOT
    Not(Box<Expr>),
    /// IS NULL
    IsNull(String),
    /// IS NOT NULL
    IsNotNull(String),
    /// IN (list of values)
    In { column: String, values: Vec<Value> },
    /// BETWEEN (range check)
    Between { column: String, low: Value, high: Value },
}

/// Comparison operators for WHERE clauses
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ComparisonOperator {
    Equal,
    NotEqual,
    LessThan,
    LessThanOrEqual,
    GreaterThan,
    GreaterThanOrEqual,
    Like,
}

/// ORDER BY clause
#[derive(Debug, Clone)]
pub struct OrderBy {
    pub column: String,
    pub ascending: bool,
}

// -----------------------------------------------------------------------------
// Helper constructors & combinators for `Expr`
// -----------------------------------------------------------------------------
impl Expr {
    /// Create `column = value` comparison expression
    pub fn eq(column: impl Into<String>, value: Value) -> Self {
        Expr::Comparison {
            column: column.into(),
            operator: ComparisonOperator::Equal,
            value,
        }
    }

    /// Create `column != value` comparison expression
    pub fn ne(column: impl Into<String>, value: Value) -> Self {
        Expr::Comparison {
            column: column.into(),
            operator: ComparisonOperator::NotEqual,
            value,
        }
    }

    /// Create `column < value` comparison expression
    pub fn lt(column: impl Into<String>, value: Value) -> Self {
        Expr::Comparison {
            column: column.into(),
            operator: ComparisonOperator::LessThan,
            value,
        }
    }

    /// Create `column <= value` comparison expression
    pub fn le(column: impl Into<String>, value: Value) -> Self {
        Expr::Comparison {
            column: column.into(),
            operator: ComparisonOperator::LessThanOrEqual,
            value,
        }
    }

    /// Create `column > value` comparison expression
    pub fn gt(column: impl Into<String>, value: Value) -> Self {
        Expr::Comparison {
            column: column.into(),
            operator: ComparisonOperator::GreaterThan,
            value,
        }
    }

    /// Create `column >= value` comparison expression
    pub fn ge(column: impl Into<String>, value: Value) -> Self {
        Expr::Comparison {
            column: column.into(),
            operator: ComparisonOperator::GreaterThanOrEqual,
            value,
        }
    }

    /// Create `column LIKE value` expression
    pub fn like(column: impl Into<String>, value: Value) -> Self {
        Expr::Comparison {
            column: column.into(),
            operator: ComparisonOperator::Like,
            value,
        }
    }

    /// Create `column IS NULL` expression
    pub fn is_null(column: impl Into<String>) -> Self {
        Expr::IsNull(column.into())
    }

    /// Create `column IS NOT NULL` expression
    pub fn is_not_null(column: impl Into<String>) -> Self {
        Expr::IsNotNull(column.into())
    }

    /// Create `column IN (values...)` expression
    pub fn in_values(column: impl Into<String>, values: Vec<Value>) -> Self {
        Expr::In {
            column: column.into(),
            values,
        }
    }

    /// Create `column BETWEEN low AND high` expression
    pub fn between(column: impl Into<String>, low: Value, high: Value) -> Self {
        Expr::Between {
            column: column.into(),
            low,
            high,
        }
    }

    /// Logical AND: `self AND other`
    pub fn and(self, other: Expr) -> Self {
        Expr::And(Box::new(self), Box::new(other))
    }

    /// Logical OR: `self OR other`
    pub fn or(self, other: Expr) -> Self {
        Expr::Or(Box::new(self), Box::new(other))
    }

    /// Logical NOT: `NOT self`
    pub fn not(self) -> Self {
        Expr::Not(Box::new(self))
    }
}

impl SelectQuery {
    /// Parse a SELECT SQL statement using sqlparser
    pub fn parse(sql: &str) -> Result<Self> {
        let dialect = SQLiteDialect {};
        let statements = Parser::parse_sql(&dialect, sql)
            .map_err(|e| Error::QueryError(format!("SQL parse error: {}", e)))?;

        if statements.len() != 1 {
            return Err(Error::QueryError("Expected a single SELECT statement".to_string()));
        }

        if let Statement::Query(query) = &statements[0] {
            Self::from_sqlparser_query(query)
        } else {
            Err(Error::QueryError("Only SELECT statements are supported".to_string()))
        }
    }

    fn from_sqlparser_query(query: &Query) -> Result<Self> {
        let select = if let SetExpr::Select(select) = &*query.body {
            select
        } else {
            return Err(Error::QueryError("Unsupported query type".to_string()));
        };

        let table = Self::parse_table_name(select)?;
        let columns = Self::parse_columns(&select.projection)?;
        let where_expr = if let Some(expr) = &select.selection {
            Some(Self::parse_where_expr(expr)?)
        } else {
            None
        };

        let order_by = Self::parse_order_by(query.order_by.as_ref())?;
        let limit = Self::parse_limit(query.limit_clause.as_ref())?;

        Ok(SelectQuery {
            columns,
            table,
            where_expr,
            order_by,
            limit,
        })
    }

    fn parse_table_name(select: &Select) -> Result<String> {
        if select.from.len() != 1 {
            return Err(Error::QueryError("Query must involve exactly one table".to_string()));
        }
        let table = &select.from[0];
        if !table.joins.is_empty() {
            return Err(Error::QueryError("JOINs are not supported".to_string()));
        }
        if let TableFactor::Table { name, .. } = &table.relation {
            Ok(name.0.iter().map(|i| i.to_string()).collect::<Vec<_>>().join("."))
        } else {
            Err(Error::QueryError("Unsupported table factor".to_string()))
        }
    }

    fn parse_columns(projection: &[SelectItem]) -> Result<Option<Vec<String>>> {
        if projection.len() == 1 {
            if let SelectItem::Wildcard(_) = &projection[0] {
                return Ok(None);
            }
        }

        let mut columns = Vec::new();
        for item in projection {
            if let SelectItem::UnnamedExpr(SqlExpr::Identifier(ident)) = item {
                columns.push(ident.value.clone());
            } else {
                return Err(Error::QueryError("Unsupported column expression".to_string()));
            }
        }
        Ok(Some(columns))
    }

    fn parse_where_expr(expr: &SqlExpr) -> Result<Expr> {
        match expr {
            SqlExpr::BinaryOp { left, op, right } => match op {
                BinaryOperator::And => Ok(Expr::And(
                    Box::new(Self::parse_where_expr(left)?),
                    Box::new(Self::parse_where_expr(right)?),
                )),
                BinaryOperator::Or => Ok(Expr::Or(
                    Box::new(Self::parse_where_expr(left)?),
                    Box::new(Self::parse_where_expr(right)?),
                )),
                BinaryOperator::Eq
                | BinaryOperator::NotEq
                | BinaryOperator::Lt
                | BinaryOperator::LtEq
                | BinaryOperator::Gt
                | BinaryOperator::GtEq => Self::parse_comparison_expr(expr),
                _ => Err(Error::QueryError(format!("Unsupported operator: {:?}", op))),
            },
            SqlExpr::IsNull(expr) => {
                if let SqlExpr::Identifier(ident) = &**expr {
                    Ok(Expr::IsNull(ident.value.clone()))
                } else {
                    Err(Error::QueryError("Expected column name before IS NULL".to_string()))
                }
            },
            SqlExpr::IsNotNull(expr) => {
                if let SqlExpr::Identifier(ident) = &**expr {
                    Ok(Expr::IsNotNull(ident.value.clone()))
                } else {
                    Err(Error::QueryError("Expected column name before IS NOT NULL".to_string()))
                }
            },
            SqlExpr::Like { negated, expr, pattern, .. } => {
                if *negated {
                    return Err(Error::QueryError("NOT LIKE is not supported".to_string()));
                }
                if let (SqlExpr::Identifier(ident), value_expr) = (&**expr, &**pattern) {
                    let value = Self::parse_sql_value(value_expr)?;
                    Ok(Expr::Comparison {
                        column: ident.value.clone(),
                        operator: ComparisonOperator::Like,
                        value,
                    })
                } else {
                    Err(Error::QueryError("Expected column LIKE 'pattern'".to_string()))
                }
            },
            SqlExpr::InList { expr, list, negated } => {
                if *negated {
                    return Err(Error::QueryError("NOT IN is not supported".to_string()));
                }
                if let SqlExpr::Identifier(ident) = &**expr {
                    let mut values = Vec::new();
                    for item in list {
                        values.push(Self::parse_sql_value(item)?);
                    }
                    Ok(Expr::In {
                        column: ident.value.clone(),
                        values,
                    })
                } else {
                    Err(Error::QueryError("Expected column name before IN".to_string()))
                }
            },
            SqlExpr::Between { expr, negated, low, high } => {
                if *negated {
                    return Err(Error::QueryError("NOT BETWEEN is not supported".to_string()));
                }
                if let SqlExpr::Identifier(ident) = &**expr {
                    let low_value = Self::parse_sql_value(low)?;
                    let high_value = Self::parse_sql_value(high)?;
                    Ok(Expr::Between {
                        column: ident.value.clone(),
                        low: low_value,
                        high: high_value,
                    })
                } else {
                    Err(Error::QueryError("Expected column name before BETWEEN".to_string()))
                }
            },
            SqlExpr::Nested(expr) => Self::parse_where_expr(expr),
            _ => Err(Error::QueryError(format!("Unsupported expression: {:?}", expr))),
        }
    }

    fn parse_comparison_expr(expr: &SqlExpr) -> Result<Expr> {
        if let SqlExpr::BinaryOp { left, op, right } = expr {
            let operator = match op {
                BinaryOperator::Eq => ComparisonOperator::Equal,
                BinaryOperator::NotEq => ComparisonOperator::NotEqual,
                BinaryOperator::Lt => ComparisonOperator::LessThan,
                BinaryOperator::LtEq => ComparisonOperator::LessThanOrEqual,
                BinaryOperator::Gt => ComparisonOperator::GreaterThan,
                BinaryOperator::GtEq => ComparisonOperator::GreaterThanOrEqual,
                _ => return Err(Error::QueryError(format!("Unsupported comparison operator: {:?}", op))),
            };

            // Handle both column = value and value = column
            match (&**left, &**right) {
                (SqlExpr::Identifier(ident), value) => {
                    let value = Self::parse_sql_value(value)?;
                    Ok(Expr::Comparison {
                        column: ident.value.clone(),
                        operator,
                        value,
                    })
                },
                (value, SqlExpr::Identifier(ident)) => {
                    // For non-equality operators, we need to reverse the operator
                    let operator = match operator {
                        ComparisonOperator::LessThan => ComparisonOperator::GreaterThan,
                        ComparisonOperator::LessThanOrEqual => ComparisonOperator::GreaterThanOrEqual,
                        ComparisonOperator::GreaterThan => ComparisonOperator::LessThan,
                        ComparisonOperator::GreaterThanOrEqual => ComparisonOperator::LessThanOrEqual,
                        _ => operator, // For =, != the order doesn't matter
                    };
                    let value = Self::parse_sql_value(value)?;
                    Ok(Expr::Comparison {
                        column: ident.value.clone(),
                        operator,
                        value,
                    })
                },
                _ => Err(Error::QueryError("Expected column = value comparison".to_string())),
            }
        } else {
            Err(Error::QueryError("Expected comparison expression".to_string()))
        }
    }

    fn parse_sql_value(sql_value: &SqlExpr) -> Result<Value> {
        match sql_value {
            SqlExpr::Value(value_with_span) => match &value_with_span.value {
                SqlValue::Number(s, _) => {
                    if s.contains('.') {
                        s.parse::<f64>().map(Value::Real).map_err(|_| Error::QueryError("Invalid float value".to_string()))
                    } else {
                        s.parse::<i64>().map(Value::Integer).map_err(|_| Error::QueryError("Invalid integer value".to_string()))
                    }
                }
                SqlValue::SingleQuotedString(s) => Ok(Value::Text(s.clone())),
                SqlValue::DoubleQuotedString(s) => Ok(Value::Text(s.clone())),
                SqlValue::Null => Ok(Value::Null),
                _ => Err(Error::QueryError("Unsupported value type".to_string())),
            },
            SqlExpr::Identifier(ident) => Ok(Value::Text(ident.value.clone())),
            _ => Err(Error::QueryError(format!("Expected a literal value, found {:?}", sql_value))),
        }
    }

    fn parse_order_by(order_by: Option<&sqlparser::ast::OrderBy>) -> Result<Option<OrderBy>> {
        if let Some(order_by) = order_by {
            // In sqlparser 0.57.0, OrderBy has a 'kind' field
            match &order_by.kind {
                sqlparser::ast::OrderByKind::Expressions(expressions) => {
                    // Take the first expression for now (we could extend this to support multiple later)
                    if let Some(order_expr) = expressions.first() {
                        // Extract column name from the expression
                        let column = match &order_expr.expr {
                            sqlparser::ast::Expr::Identifier(ident) => ident.value.clone(),
                            _ => return Err(Error::QueryError("Unsupported ORDER BY expression".to_string())),
                        };
                        
                        // Extract sort direction
                        let ascending = order_expr.options.asc.unwrap_or(true);
                        
                        Ok(Some(OrderBy { column, ascending }))
                    } else {
                        Ok(None)
                    }
                }
                _ => Err(Error::QueryError("Unsupported ORDER BY kind".to_string())),
            }
        } else {
            Ok(None)
        }
    }

    fn parse_limit(limit_clause: Option<&sqlparser::ast::LimitClause>) -> Result<Option<usize>> {
        if let Some(limit_clause) = limit_clause {
            // In sqlparser 0.57.0, LimitClause can be different types
            match limit_clause {
                sqlparser::ast::LimitClause::LimitOffset { limit, .. } => {
                    if let Some(limit_expr) = limit {
                        // Extract the limit value
                        let limit_value = Self::parse_sql_value(limit_expr)?;
                        match limit_value {
                            Value::Integer(n) => Ok(Some(n as usize)),
                            _ => Err(Error::QueryError("LIMIT must be an integer".to_string())),
                        }
                    } else {
                        Ok(None)
                    }
                }
                _ => Err(Error::QueryError("Unsupported LIMIT clause type".to_string())),
            }
        } else {
            Ok(None)
        }
    }
}

impl SelectQuery {
    /// Execute the query against the provided rows
    pub fn execute(&self, mut rows: Vec<Row>, all_columns: &[String]) -> Result<Vec<Row>> {
        // Apply WHERE conditions
        rows = self.apply_where_conditions(rows)?;
        
        // Apply ORDER BY
        if let Some(ref order_by) = self.order_by {
            rows = self.apply_order_by(rows, order_by)?;
        }
        
        // Apply column selection
        rows = self.apply_column_selection(rows, all_columns)?;
        
        // Apply LIMIT
        if let Some(limit) = self.limit {
            rows.truncate(limit);
        }
        
        Ok(rows)
    }
    
    /// Apply WHERE conditions to filter rows
    fn apply_where_conditions(&self, rows: Vec<Row>) -> Result<Vec<Row>> {
        if self.where_expr.is_none() {
            return Ok(rows);
        }
        
        let total_rows = rows.len();
        let filtered_rows: Vec<Row> = rows
            .into_iter()
            .filter(|row| {
                self.evaluate_expr(row, &self.where_expr.as_ref().unwrap())
            })
            .collect();
        
        // Add debug logging for WHERE clause filtering
        crate::logging::log_debug(&format!(
            "WHERE clause filtered {} rows from {} total rows", 
            filtered_rows.len(), 
            total_rows
        ));
        
        Ok(filtered_rows)
    }
    
    /// Evaluate a WHERE expression against a row
    pub fn evaluate_expr(&self, row: &Row, expr: &Expr) -> bool {
        match expr {
            Expr::Comparison { column, operator, value } => {
                let row_value = match row.get(column.as_str()) {
                    Some(value) => value,
                    None => return false, // Column doesn't exist
                };
                
                match operator {
                    ComparisonOperator::Equal => self.values_equal(row_value, value),
                    ComparisonOperator::NotEqual => !self.values_equal(row_value, value),
                    ComparisonOperator::LessThan => self.value_less_than(row_value, value),
                    ComparisonOperator::LessThanOrEqual => {
                        self.value_less_than(row_value, value) || 
                        self.values_equal(row_value, value)
                    },
                    ComparisonOperator::GreaterThan => {
                        !self.value_less_than(row_value, value) && 
                        !self.values_equal(row_value, value)
                    },
                    ComparisonOperator::GreaterThanOrEqual => {
                        !self.value_less_than(row_value, value)
                    },
                    ComparisonOperator::Like => self.value_like(row_value, value),
                }
            },
            Expr::And(left, right) => self.evaluate_expr(row, left) && self.evaluate_expr(row, right),
            Expr::Or(left, right) => self.evaluate_expr(row, left) || self.evaluate_expr(row, right),
            Expr::Not(expr) => !self.evaluate_expr(row, expr),
            Expr::IsNull(column) => row.get(column.as_str()).map_or(false, |v| v.is_null()),
            Expr::IsNotNull(column) => row.get(column.as_str()).map_or(false, |v| !v.is_null()),
            Expr::In { column, values } => {
                let row_value = row.get(column.as_str()).cloned().unwrap_or(Value::Null);
                values.iter().any(|v| self.values_equal(&row_value, v))
            },
            Expr::Between { column, low, high } => {
                let row_value = match row.get(column.as_str()) {
                    Some(value) => value,
                    None => return false, // Column doesn't exist
                };
                
                // Check if row_value >= low AND row_value <= high
                (self.values_equal(row_value, low) || !self.value_less_than(row_value, low)) &&
                (self.values_equal(row_value, high) || self.value_less_than(row_value, high))
            },
        }
    }
    
    /// Compare two values for equality
    fn values_equal(&self, a: &Value, b: &Value) -> bool {
        match (a, b) {
            (Value::Null, Value::Null) => true,
            (Value::Integer(a), Value::Integer(b)) => a == b,
            (Value::Real(a), Value::Real(b)) => (a - b).abs() < f64::EPSILON,
            (Value::Text(a), Value::Text(b)) => a == b,
            (Value::Blob(a), Value::Blob(b)) => a == b,
            // Type coercion
            (Value::Integer(a), Value::Real(b)) => (*a as f64 - b).abs() < f64::EPSILON,
            (Value::Real(a), Value::Integer(b)) => (a - *b as f64).abs() < f64::EPSILON,
            _ => false,
        }
    }
    
    /// Check if value a is less than value b
    fn value_less_than(&self, a: &Value, b: &Value) -> bool {
        match (a, b) {
            (Value::Integer(a), Value::Integer(b)) => a < b,
            (Value::Real(a), Value::Real(b)) => a < b,
            (Value::Text(a), Value::Text(b)) => a < b,
            (Value::Integer(a), Value::Real(b)) => (*a as f64) < *b,
            (Value::Real(a), Value::Integer(b)) => *a < (*b as f64),
            _ => false,
        }
    }
    
    /// Check if value matches LIKE pattern (improved implementation)
    fn value_like(&self, value: &Value, pattern: &Value) -> bool {
        match (value, pattern) {
            (Value::Text(text), Value::Text(pattern)) => {
                // Improved LIKE implementation with % wildcard
                if pattern.contains('%') {
                    let pattern_parts: Vec<&str> = pattern.split('%').collect();
                    
                    // Handle simple cases like 'prefix%', '%suffix', '%middle%'
                    match pattern_parts.len() {
                        2 => {
                            let prefix = pattern_parts[0];
                            let suffix = pattern_parts[1];
                            
                            // Handle 'prefix%' case (suffix is empty)
                            if suffix.is_empty() {
                                return text.starts_with(prefix);
                            }
                            // Handle '%suffix' case (prefix is empty)
                            if prefix.is_empty() {
                                return text.ends_with(suffix);
                            }
                            // Handle 'prefix%suffix' case
                            return text.starts_with(prefix) && text.ends_with(suffix) && text.len() >= prefix.len() + suffix.len();
                        },
                        1 => {
                            // No % found, exact match
                            return text == pattern;
                        },
                        3 => {
                            // Handle '%middle%' case
                            let prefix = pattern_parts[0];
                            let middle = pattern_parts[1];
                            let suffix = pattern_parts[2];
                            
                            if prefix.is_empty() && suffix.is_empty() {
                                // Pattern is '%middle%' - check if text contains middle
                                return text.contains(middle);
                            }
                            // More complex patterns - fall back to basic matching
                            return text.starts_with(prefix) && text.contains(middle) && text.ends_with(suffix);
                        },
                        _ => {
                            // Multiple % wildcards - more complex pattern
                            // For now, do a simple contains check for each non-empty part
                            for part in pattern_parts {
                                if !part.is_empty() && !text.contains(part) {
                                    return false;
                                }
                            }
                            return true;
                        }
                    }
                } else {
                    // No wildcards, exact match
                    text == pattern
                }
            },
            _ => false,
        }
    }
    
    /// Apply ORDER BY to sort rows
    fn apply_order_by(&self, mut rows: Vec<Row>, order_by: &OrderBy) -> Result<Vec<Row>> {
        rows.sort_by(|a, b| {
            let val_a = a.get(order_by.column.as_str());
            let val_b = b.get(order_by.column.as_str());
            
            let cmp = match (val_a, val_b) {
                (Some(a), Some(b)) => self.compare_values(a, b),
                (Some(_), None) => std::cmp::Ordering::Greater,
                (None, Some(_)) => std::cmp::Ordering::Less,
                (None, None) => std::cmp::Ordering::Equal,
            };
            
            if order_by.ascending {
                cmp
            } else {
                cmp.reverse()
            }
        });
        
        Ok(rows)
    }
    
    /// Compare two values for ordering
    fn compare_values(&self, a: &Value, b: &Value) -> std::cmp::Ordering {
        match (a, b) {
            (Value::Null, Value::Null) => std::cmp::Ordering::Equal,
            (Value::Null, _) => std::cmp::Ordering::Less,
            (_, Value::Null) => std::cmp::Ordering::Greater,
            (Value::Integer(a), Value::Integer(b)) => a.cmp(b),
            (Value::Real(a), Value::Real(b)) => a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal),
            (Value::Text(a), Value::Text(b)) => a.cmp(b),
            (Value::Integer(a), Value::Real(b)) => (*a as f64).partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal),
            (Value::Real(a), Value::Integer(b)) => a.partial_cmp(&(*b as f64)).unwrap_or(std::cmp::Ordering::Equal),
            _ => std::cmp::Ordering::Equal,
        }
    }
    
    /// Apply column selection (SELECT specific columns or *)
    fn apply_column_selection(&self, rows: Vec<Row>, all_columns: &[String]) -> Result<Vec<Row>> {
        match &self.columns {
            None => Ok(rows), // SELECT * - return all columns
            Some(selected_columns) => {
                let mut result_rows = Vec::new();
                
                for row in rows {
                    let mut new_row = HashMap::new();
                    
                    for column in selected_columns {
                        if !all_columns.contains(column) {
                            return Err(Error::ColumnNotFound(column.clone()));
                        }
                        
                        let value = row.get(column.as_str()).cloned().unwrap_or(Value::Null);
                        new_row.insert(column.clone(), value);
                    }
                    
                    result_rows.push(new_row);
                }
                
                Ok(result_rows)
            }
        }
    }
}

impl SelectQuery {
    /// Create a new `SelectQuery` for the given `table` with default values (SELECT *)
    pub fn new(table: impl Into<String>) -> Self {
        Self {
            columns: None,
            table: table.into(),
            where_expr: None,
            order_by: None,
            limit: None,
        }
    }

    /// Specify the columns to select (equivalent to the projection in SQL).
    /// Passing an empty vector is the same as `SELECT *`.
    pub fn select_columns(mut self, columns: Vec<String>) -> Self {
        if columns.is_empty() {
            self.columns = None;
        } else {
            self.columns = Some(columns);
        }
        self
    }

    /// Attach a WHERE expression to the query.
    pub fn with_where(mut self, expr: Expr) -> Self {
        self.where_expr = Some(expr);
        self
    }

    /// Attach an ORDER BY clause to the query.
    pub fn with_order_by(mut self, column: impl Into<String>, ascending: bool) -> Self {
        self.order_by = Some(OrderBy { column: column.into(), ascending });
        self
    }

    /// Attach a LIMIT clause to the query.
    pub fn with_limit(mut self, limit: usize) -> Self {
        self.limit = Some(limit);
        self
    }
}

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

    #[test]
    fn test_parse_simple_select() {
        let query = SelectQuery::parse("SELECT * FROM users").unwrap();
        assert_eq!(query.table, "users");
        assert!(query.columns.is_none());
        assert!(query.where_expr.is_none());
    }

    #[test]
    fn test_parse_select_with_columns() {
        let query = SelectQuery::parse("SELECT name, email FROM users").unwrap();
        assert_eq!(query.table, "users");
        assert_eq!(query.columns.as_ref().unwrap(), &vec!["name".to_string(), "email".to_string()]);
    }

    #[test]
    fn test_parse_select_with_where() {
        let query = SelectQuery::parse("SELECT * FROM users WHERE age > 18").unwrap();
        assert_eq!(query.table, "users");
        assert!(query.where_expr.is_some());
        let expr = query.where_expr.as_ref().unwrap();
        if let Expr::Comparison { column, operator, .. } = expr {
            assert_eq!(column, "age");
            assert_eq!(operator, &ComparisonOperator::GreaterThan);
        } else {
            panic!("Expected Comparison expr");
        }
    }

    #[test]
    fn test_parse_select_with_order_by() {
        let query = SelectQuery::parse("SELECT * FROM users ORDER BY name ASC").unwrap();
        assert_eq!(query.table, "users");
        assert!(query.order_by.is_some());
        let order_by = query.order_by.unwrap();
        assert_eq!(order_by.column, "name");
        assert!(order_by.ascending);
    }

    #[test]
    fn test_parse_select_with_limit() {
        let query = SelectQuery::parse("SELECT * FROM users LIMIT 10").unwrap();
        assert_eq!(query.table, "users");
        assert_eq!(query.limit, Some(10));
    }

    #[test]
    fn test_like_pattern_matching() {
        let query = SelectQuery::parse("SELECT * FROM users").unwrap();
        
        // Test prefix pattern 'f%'
        assert!(query.value_like(&Value::Text("foo".to_string()), &Value::Text("f%".to_string())));
        assert!(query.value_like(&Value::Text("ff736190-1479-4681-b9b2-78757cd55821".to_string()), &Value::Text("f%".to_string())));
        assert!(query.value_like(&Value::Text("fa18fc4d-11dc-466b-84cd-d6793ff93774".to_string()), &Value::Text("f%".to_string())));
        assert!(!query.value_like(&Value::Text("bar".to_string()), &Value::Text("f%".to_string())));
        
        // Test suffix pattern '%bar'
        assert!(query.value_like(&Value::Text("foobar".to_string()), &Value::Text("%bar".to_string())));
        assert!(!query.value_like(&Value::Text("foo".to_string()), &Value::Text("%bar".to_string())));
        
        // Test contains pattern '%middle%'
        assert!(query.value_like(&Value::Text("foo middle bar".to_string()), &Value::Text("%middle%".to_string())));
        assert!(!query.value_like(&Value::Text("foo bar".to_string()), &Value::Text("%middle%".to_string())));
        
        // Test exact match (no wildcards)
        assert!(query.value_like(&Value::Text("exact".to_string()), &Value::Text("exact".to_string())));
        assert!(!query.value_like(&Value::Text("different".to_string()), &Value::Text("exact".to_string())));
    }

    #[test]
    fn test_parse_select_with_between() {
        let query = SelectQuery::parse("SELECT * FROM users WHERE age BETWEEN 18 AND 65").unwrap();
        assert_eq!(query.table, "users");
        assert!(query.where_expr.is_some());
        
        let expr = query.where_expr.as_ref().unwrap();
        if let Expr::Between { column, low, high } = expr {
            assert_eq!(column, "age");
            assert_eq!(low, &Value::Integer(18));
            assert_eq!(high, &Value::Integer(65));
        } else {
            panic!("Expected Between expr, got {:?}", expr);
        }
    }

    #[test]
    fn test_between_evaluation() {
        let query = SelectQuery::parse("SELECT * FROM users").unwrap();
        let mut row = std::collections::HashMap::new();
        
        // Test integer BETWEEN
        row.insert("age".to_string(), Value::Integer(25));
        let expr = Expr::Between {
            column: "age".to_string(),
            low: Value::Integer(18),
            high: Value::Integer(65),
        };
        assert!(query.evaluate_expr(&row, &expr));
        
        // Test value outside range (too low)
        row.insert("age".to_string(), Value::Integer(15));
        assert!(!query.evaluate_expr(&row, &expr));
        
        // Test value outside range (too high)
        row.insert("age".to_string(), Value::Integer(70));
        assert!(!query.evaluate_expr(&row, &expr));
        
        // Test boundary values
        row.insert("age".to_string(), Value::Integer(18));
        assert!(query.evaluate_expr(&row, &expr));
        
        row.insert("age".to_string(), Value::Integer(65));
        assert!(query.evaluate_expr(&row, &expr));
        
        // Test float BETWEEN
        row.insert("score".to_string(), Value::Real(85.5));
        let expr = Expr::Between {
            column: "score".to_string(),
            low: Value::Real(80.0),
            high: Value::Real(90.0),
        };
        assert!(query.evaluate_expr(&row, &expr));
        
        // Test mixed types (integer value, real bounds)
        row.insert("score".to_string(), Value::Integer(85));
        assert!(query.evaluate_expr(&row, &expr));
    }

    #[test]
    fn test_between_builder_api() {
        let query = SelectQuery::new("users")
            .with_where(Expr::between("age", Value::Integer(18), Value::Integer(65)));
        
        assert_eq!(query.table, "users");
        assert!(query.where_expr.is_some());
        
        if let Some(Expr::Between { column, low, high }) = &query.where_expr {
            assert_eq!(column, "age");
            assert_eq!(low, &Value::Integer(18));
            assert_eq!(high, &Value::Integer(65));
        } else {
            panic!("Expected Between expr");
        }
    }
}