sql-cli 1.67.2

SQL query tool for CSV/JSON with both interactive TUI and non-interactive CLI modes - perfect for exploration and automation
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
// Primary expression parsing
// Handles literals, identifiers, function calls, and parenthesized expressions

use crate::sql::parser::ast::{ColumnRef, SqlExpression, WindowSpec};
use crate::sql::parser::lexer::Token;
use tracing::{debug, trace};

use super::{log_parse_decision, trace_parse_entry, trace_parse_exit, ExpressionParser};

/// Parser context for primary expressions
pub struct PrimaryExpressionContext<'a> {
    pub columns: &'a [String],
    pub in_method_args: bool,
}

impl<'a> Default for PrimaryExpressionContext<'a> {
    fn default() -> Self {
        Self {
            columns: &[],
            in_method_args: false,
        }
    }
}

/// Parse a primary expression (literals, identifiers, functions, parentheses)
/// This is the bottom of the expression hierarchy
pub fn parse_primary<P>(
    parser: &mut P,
    ctx: &PrimaryExpressionContext,
) -> Result<SqlExpression, String>
where
    P: ParsePrimary + ExpressionParser + ?Sized,
{
    trace_parse_entry("parse_primary", ExpressionParser::current_token(parser));

    // Special case: check if a number literal could actually be a column name
    // This handles cases where columns are named with pure numbers like "202204"
    if let Token::NumberLiteral(num_str) = ExpressionParser::current_token(parser) {
        if ctx.columns.iter().any(|col| col == num_str) {
            log_parse_decision(
                "parse_primary",
                ExpressionParser::current_token(parser),
                "Number literal matches column name, treating as column",
            );
            let expr = SqlExpression::Column(ColumnRef::unquoted(num_str.clone()));
            ExpressionParser::advance(parser);
            let result = Ok(expr);
            trace_parse_exit("parse_primary", &result);
            return result;
        }
    }

    let result = match ExpressionParser::current_token(parser) {
        Token::Case => {
            debug!("Parsing CASE expression");
            parser.parse_case_expression()
        }

        Token::DateTime => {
            debug!("Parsing DateTime constructor");
            parse_datetime_constructor(parser)
        }

        Token::Unnest => {
            debug!("Parsing UNNEST expression");
            parse_unnest(parser)
        }

        Token::Identifier(id) => {
            let id_upper = id.to_uppercase();
            let id_clone = id.clone();

            // Check for boolean literals first
            if id_upper == "TRUE" {
                log_parse_decision(
                    "parse_primary",
                    ExpressionParser::current_token(parser),
                    "Boolean literal TRUE",
                );
                ExpressionParser::advance(parser);
                Ok(SqlExpression::BooleanLiteral(true))
            } else if id_upper == "FALSE" {
                log_parse_decision(
                    "parse_primary",
                    ExpressionParser::current_token(parser),
                    "Boolean literal FALSE",
                );
                ExpressionParser::advance(parser);
                Ok(SqlExpression::BooleanLiteral(false))
            } else {
                ExpressionParser::advance(parser);

                // Check for table.column notation or method calls
                if matches!(ExpressionParser::current_token(parser), Token::Dot) {
                    ExpressionParser::advance(parser); // consume dot

                    if let Token::Identifier(next_id) = ExpressionParser::current_token(parser) {
                        let next_id = next_id.clone();
                        ExpressionParser::advance(parser);

                        // Check if this is a method call (followed by parentheses)
                        if matches!(ExpressionParser::current_token(parser), Token::LeftParen) {
                            debug!(object = %id_clone, method = %next_id, "Parsing method call");
                            ExpressionParser::advance(parser); // consume (

                            // Handle empty argument list
                            let args = if matches!(
                                ExpressionParser::current_token(parser),
                                Token::RightParen
                            ) {
                                Vec::new()
                            } else {
                                parser.parse_expression_list()?
                            };
                            ExpressionParser::consume(parser, Token::RightParen)?;

                            log_parse_decision(
                                "parse_primary",
                                &Token::Identifier(next_id.clone()),
                                "Method call",
                            );
                            Ok(SqlExpression::MethodCall {
                                object: id_clone,
                                method: next_id,
                                args,
                            })
                        } else {
                            // It's a qualified column reference
                            let col_ref = ColumnRef::qualified(id_clone, next_id.clone());
                            log_parse_decision(
                                "parse_primary",
                                &Token::Identifier(next_id),
                                "Qualified column reference",
                            );
                            Ok(SqlExpression::Column(col_ref))
                        }
                    } else {
                        Err("Expected identifier after '.'".to_string())
                    }
                // Check if this is a function call
                } else if matches!(ExpressionParser::current_token(parser), Token::LeftParen) {
                    debug!(function = %id_upper, "Parsing function call");
                    ExpressionParser::advance(parser); // consume (
                    let (args, has_distinct) = parser.parse_function_args()?;
                    ExpressionParser::consume(parser, Token::RightParen)?;

                    // Check for OVER clause for window functions
                    if matches!(ExpressionParser::current_token(parser), Token::Over) {
                        debug!(function = %id_upper, "Window function detected");
                        ExpressionParser::advance(parser); // consume OVER
                        ExpressionParser::consume(parser, Token::LeftParen)?;
                        let window_spec = parser.parse_window_spec()?;
                        ExpressionParser::consume(parser, Token::RightParen)?;
                        Ok(SqlExpression::WindowFunction {
                            name: id_upper,
                            args,
                            window_spec,
                        })
                    } else {
                        Ok(SqlExpression::FunctionCall {
                            name: id_upper,
                            args,
                            distinct: has_distinct,
                        })
                    }
                } else {
                    // Otherwise treat as simple column
                    log_parse_decision(
                        "parse_primary",
                        &Token::Identifier(id_clone.clone()),
                        "Column reference",
                    );
                    Ok(SqlExpression::Column(ColumnRef::unquoted(id_clone)))
                }
            }
        }

        Token::QuotedIdentifier(id) => {
            let expr = if ctx.in_method_args {
                // In method arguments, treat quoted identifiers as string literals
                log_parse_decision(
                    "parse_primary",
                    ExpressionParser::current_token(parser),
                    "Quoted identifier in method args - treating as string",
                );
                SqlExpression::StringLiteral(id.clone())
            } else {
                // Otherwise it's a column name like "Customer Id"
                log_parse_decision(
                    "parse_primary",
                    ExpressionParser::current_token(parser),
                    "Quoted identifier as column name",
                );
                SqlExpression::Column(ColumnRef::quoted(id.clone()))
            };
            ExpressionParser::advance(parser);
            Ok(expr)
        }

        Token::StringLiteral(s) => {
            trace!("String literal: {}", s);
            let expr = SqlExpression::StringLiteral(s.clone());
            ExpressionParser::advance(parser);
            Ok(expr)
        }

        Token::NumberLiteral(n) => {
            trace!("Number literal: {}", n);
            let expr = SqlExpression::NumberLiteral(n.clone());
            ExpressionParser::advance(parser);
            Ok(expr)
        }

        Token::Null => {
            trace!("NULL literal");
            ExpressionParser::advance(parser);
            Ok(SqlExpression::Null)
        }

        // Handle LEFT and RIGHT as function names when followed by parentheses
        Token::Left | Token::Right => {
            let func_name = match ExpressionParser::current_token(parser) {
                Token::Left => "LEFT".to_string(),
                Token::Right => "RIGHT".to_string(),
                _ => unreachable!(),
            };

            ExpressionParser::advance(parser);

            // Check if this is a function call
            if matches!(ExpressionParser::current_token(parser), Token::LeftParen) {
                debug!(function = %func_name, "Parsing LEFT/RIGHT function call");
                ExpressionParser::advance(parser); // consume (
                let (args, _has_distinct) = parser.parse_function_args()?;
                ExpressionParser::consume(parser, Token::RightParen)?;

                Ok(SqlExpression::FunctionCall {
                    name: func_name,
                    args,
                    distinct: false,
                })
            } else {
                // If not followed by parenthesis, this is likely a JOIN keyword - error
                Err(format!(
                    "{} keyword unexpected in expression context",
                    func_name
                ))
            }
        }

        Token::LeftParen => {
            debug!("Parsing parenthesized expression or subquery");
            ExpressionParser::advance(parser); // consume (

            // Check if this is a subquery (starts with SELECT)
            if matches!(ExpressionParser::current_token(parser), Token::Select) {
                debug!("Detected subquery - parsing SELECT statement");
                let subquery = parser.parse_subquery()?;
                ExpressionParser::consume(parser, Token::RightParen)?;
                Ok(SqlExpression::ScalarSubquery {
                    query: Box::new(subquery),
                })
            } else {
                // Regular parenthesized expression
                debug!("Regular parenthesized expression");
                let expr = parser.parse_logical_or()?;
                ExpressionParser::consume(parser, Token::RightParen)?;
                Ok(expr)
            }
        }

        Token::Not => {
            debug!("Parsing NOT expression");
            parse_not_expression(parser)
        }

        Token::Star => {
            // Handle * as a literal (like in COUNT(*))
            trace!("Star token as literal");
            ExpressionParser::advance(parser);
            Ok(SqlExpression::StringLiteral("*".to_string()))
        }

        // Handle window-related keywords that can also be column names
        Token::Row => {
            trace!("ROW token treated as identifier in expression context");
            ExpressionParser::advance(parser);
            Ok(SqlExpression::Column(ColumnRef::unquoted(
                "row".to_string(),
            )))
        }

        Token::Rows => {
            trace!("ROWS token treated as identifier in expression context");
            ExpressionParser::advance(parser);
            Ok(SqlExpression::Column(ColumnRef::unquoted(
                "rows".to_string(),
            )))
        }

        Token::Range => {
            trace!("RANGE token treated as identifier in expression context");
            ExpressionParser::advance(parser);
            Ok(SqlExpression::Column(ColumnRef::unquoted(
                "range".to_string(),
            )))
        }

        _ => {
            let err = format!(
                "Unexpected token in primary expression: {:?}",
                ExpressionParser::current_token(parser)
            );
            debug!(error = %err);
            Err(err)
        }
    };

    trace_parse_exit("parse_primary", &result);
    result
}

/// Parse DateTime constructor
fn parse_datetime_constructor<P>(parser: &mut P) -> Result<SqlExpression, String>
where
    P: ParsePrimary + ExpressionParser + ?Sized,
{
    ExpressionParser::advance(parser); // consume DateTime
    ExpressionParser::consume(parser, Token::LeftParen)?;

    // Check if empty parentheses for DateTime() - today's date
    if matches!(ExpressionParser::current_token(parser), Token::RightParen) {
        ExpressionParser::advance(parser); // consume )
        debug!("DateTime() - today's date");
        return Ok(SqlExpression::DateTimeToday {
            hour: None,
            minute: None,
            second: None,
        });
    }

    // Parse year
    let year = if let Token::NumberLiteral(n) = ExpressionParser::current_token(parser) {
        n.parse::<i32>().map_err(|_| "Invalid year")?
    } else {
        return Err("Expected year in DateTime constructor".to_string());
    };
    ExpressionParser::advance(parser);
    ExpressionParser::consume(parser, Token::Comma)?;

    // Parse month
    let month = if let Token::NumberLiteral(n) = ExpressionParser::current_token(parser) {
        n.parse::<u32>().map_err(|_| "Invalid month")?
    } else {
        return Err("Expected month in DateTime constructor".to_string());
    };
    ExpressionParser::advance(parser);
    ExpressionParser::consume(parser, Token::Comma)?;

    // Parse day
    let day = if let Token::NumberLiteral(n) = ExpressionParser::current_token(parser) {
        n.parse::<u32>().map_err(|_| "Invalid day")?
    } else {
        return Err("Expected day in DateTime constructor".to_string());
    };
    ExpressionParser::advance(parser);

    // Check for optional time components
    let mut hour = None;
    let mut minute = None;
    let mut second = None;

    if matches!(ExpressionParser::current_token(parser), Token::Comma) {
        ExpressionParser::advance(parser); // consume comma

        // Parse hour
        if let Token::NumberLiteral(n) = ExpressionParser::current_token(parser) {
            hour = Some(n.parse::<u32>().map_err(|_| "Invalid hour")?);
            ExpressionParser::advance(parser);

            // Check for minute
            if matches!(ExpressionParser::current_token(parser), Token::Comma) {
                ExpressionParser::advance(parser); // consume comma

                if let Token::NumberLiteral(n) = ExpressionParser::current_token(parser) {
                    minute = Some(n.parse::<u32>().map_err(|_| "Invalid minute")?);
                    ExpressionParser::advance(parser);

                    // Check for second
                    if matches!(ExpressionParser::current_token(parser), Token::Comma) {
                        ExpressionParser::advance(parser); // consume comma

                        if let Token::NumberLiteral(n) = ExpressionParser::current_token(parser) {
                            second = Some(n.parse::<u32>().map_err(|_| "Invalid second")?);
                            ExpressionParser::advance(parser);
                        }
                    }
                }
            }
        }
    }

    ExpressionParser::consume(parser, Token::RightParen)?;

    debug!(year = year, month = month, day = day, hour = ?hour, minute = ?minute, second = ?second, "DateTime constructor parsed");

    Ok(SqlExpression::DateTimeConstructor {
        year,
        month,
        day,
        hour,
        minute,
        second,
    })
}

/// Parse NOT expression
fn parse_not_expression<P>(parser: &mut P) -> Result<SqlExpression, String>
where
    P: ParsePrimary + ExpressionParser + ?Sized,
{
    ExpressionParser::advance(parser); // consume NOT

    // Check if this is a NOT IN expression
    if let Ok(inner_expr) = parser.parse_comparison() {
        // After parsing the inner expression, check if we're followed by IN
        if matches!(ExpressionParser::current_token(parser), Token::In) {
            debug!("NOT IN expression detected");
            ExpressionParser::advance(parser); // consume IN
            ExpressionParser::consume(parser, Token::LeftParen)?;
            let values = parser.parse_expression_list()?;
            ExpressionParser::consume(parser, Token::RightParen)?;

            Ok(SqlExpression::NotInList {
                expr: Box::new(inner_expr),
                values,
            })
        } else {
            // Regular NOT expression
            debug!("Regular NOT expression");
            Ok(SqlExpression::Not {
                expr: Box::new(inner_expr),
            })
        }
    } else {
        Err("Expected expression after NOT".to_string())
    }
}

/// Parse UNNEST expression
/// Syntax: UNNEST(column_expr, 'delimiter')
fn parse_unnest<P>(parser: &mut P) -> Result<SqlExpression, String>
where
    P: ParsePrimary + ExpressionParser + ?Sized,
{
    debug!("parse_unnest: starting");
    ExpressionParser::advance(parser); // consume UNNEST
    ExpressionParser::consume(parser, Token::LeftParen)?;

    // Parse the column expression (first argument)
    let column = parser.parse_logical_or()?;
    debug!("parse_unnest: parsed column expression");

    // Expect comma
    ExpressionParser::consume(parser, Token::Comma)?;

    // Parse the delimiter (second argument - must be a string literal)
    let delimiter = match ExpressionParser::current_token(parser) {
        Token::StringLiteral(s) => {
            let delim = s.clone();
            ExpressionParser::advance(parser);
            delim
        }
        _ => {
            return Err("UNNEST delimiter must be a string literal".to_string());
        }
    };

    debug!(delimiter = %delimiter, "parse_unnest: parsed delimiter");

    ExpressionParser::consume(parser, Token::RightParen)?;

    debug!("parse_unnest: complete");
    Ok(SqlExpression::Unnest {
        column: Box::new(column),
        delimiter,
    })
}

/// Trait that parsers must implement to use primary expression parsing
pub trait ParsePrimary {
    fn current_token(&self) -> &Token;
    fn advance(&mut self);
    fn consume(&mut self, expected: Token) -> Result<(), String>;

    // These methods are called from parse_primary
    fn parse_case_expression(&mut self) -> Result<SqlExpression, String>;
    fn parse_function_args(&mut self) -> Result<(Vec<SqlExpression>, bool), String>;
    fn parse_window_spec(&mut self) -> Result<WindowSpec, String>;
    fn parse_logical_or(&mut self) -> Result<SqlExpression, String>;
    fn parse_comparison(&mut self) -> Result<SqlExpression, String>;
    fn parse_expression_list(&mut self) -> Result<Vec<SqlExpression>, String>;

    // For subquery parsing (without parenthesis balance validation)
    fn parse_subquery(&mut self) -> Result<crate::sql::parser::ast::SelectStatement, String>;
}