Skip to main content

sql_cli/sql/parser/expressions/
primary.rs

1// Primary expression parsing
2// Handles literals, identifiers, function calls, and parenthesized expressions
3
4use crate::sql::parser::ast::{ColumnRef, SqlExpression, WindowSpec};
5use crate::sql::parser::lexer::Token;
6use tracing::{debug, trace};
7
8use super::{log_parse_decision, trace_parse_entry, trace_parse_exit, ExpressionParser};
9
10/// Parser context for primary expressions
11pub struct PrimaryExpressionContext<'a> {
12    pub columns: &'a [String],
13    pub in_method_args: bool,
14}
15
16impl<'a> Default for PrimaryExpressionContext<'a> {
17    fn default() -> Self {
18        Self {
19            columns: &[],
20            in_method_args: false,
21        }
22    }
23}
24
25/// Parse a primary expression (literals, identifiers, functions, parentheses)
26/// This is the bottom of the expression hierarchy
27pub fn parse_primary<P>(
28    parser: &mut P,
29    ctx: &PrimaryExpressionContext,
30) -> Result<SqlExpression, String>
31where
32    P: ParsePrimary + ExpressionParser + ?Sized,
33{
34    trace_parse_entry("parse_primary", ExpressionParser::current_token(parser));
35
36    // Special case: check if a number literal could actually be a column name
37    // This handles cases where columns are named with pure numbers like "202204"
38    if let Token::NumberLiteral(num_str) = ExpressionParser::current_token(parser) {
39        if ctx.columns.iter().any(|col| col == num_str) {
40            log_parse_decision(
41                "parse_primary",
42                ExpressionParser::current_token(parser),
43                "Number literal matches column name, treating as column",
44            );
45            let expr = SqlExpression::Column(ColumnRef::unquoted(num_str.clone()));
46            ExpressionParser::advance(parser);
47            let result = Ok(expr);
48            trace_parse_exit("parse_primary", &result);
49            return result;
50        }
51    }
52
53    let result = match ExpressionParser::current_token(parser) {
54        Token::Case => {
55            debug!("Parsing CASE expression");
56            parser.parse_case_expression()
57        }
58
59        Token::DateTime => {
60            debug!("Parsing DateTime constructor");
61            parse_datetime_constructor(parser)
62        }
63
64        Token::Unnest => {
65            debug!("Parsing UNNEST expression");
66            parse_unnest(parser)
67        }
68
69        Token::Identifier(id) => {
70            let id_upper = id.to_uppercase();
71            let id_clone = id.clone();
72
73            // Check for boolean literals first
74            if id_upper == "TRUE" {
75                log_parse_decision(
76                    "parse_primary",
77                    ExpressionParser::current_token(parser),
78                    "Boolean literal TRUE",
79                );
80                ExpressionParser::advance(parser);
81                Ok(SqlExpression::BooleanLiteral(true))
82            } else if id_upper == "FALSE" {
83                log_parse_decision(
84                    "parse_primary",
85                    ExpressionParser::current_token(parser),
86                    "Boolean literal FALSE",
87                );
88                ExpressionParser::advance(parser);
89                Ok(SqlExpression::BooleanLiteral(false))
90            } else {
91                ExpressionParser::advance(parser);
92
93                // Check for table.column notation or method calls
94                if matches!(ExpressionParser::current_token(parser), Token::Dot) {
95                    ExpressionParser::advance(parser); // consume dot
96
97                    if let Token::Identifier(next_id) = ExpressionParser::current_token(parser) {
98                        let next_id = next_id.clone();
99                        ExpressionParser::advance(parser);
100
101                        // Check if this is a method call (followed by parentheses)
102                        if matches!(ExpressionParser::current_token(parser), Token::LeftParen) {
103                            debug!(object = %id_clone, method = %next_id, "Parsing method call");
104                            ExpressionParser::advance(parser); // consume (
105
106                            // Handle empty argument list
107                            let args = if matches!(
108                                ExpressionParser::current_token(parser),
109                                Token::RightParen
110                            ) {
111                                Vec::new()
112                            } else {
113                                parser.parse_expression_list()?
114                            };
115                            ExpressionParser::consume(parser, Token::RightParen)?;
116
117                            log_parse_decision(
118                                "parse_primary",
119                                &Token::Identifier(next_id.clone()),
120                                "Method call",
121                            );
122                            Ok(SqlExpression::MethodCall {
123                                object: id_clone,
124                                method: next_id,
125                                args,
126                            })
127                        } else {
128                            // It's a qualified column reference
129                            let col_ref = ColumnRef::qualified(id_clone, next_id.clone());
130                            log_parse_decision(
131                                "parse_primary",
132                                &Token::Identifier(next_id),
133                                "Qualified column reference",
134                            );
135                            Ok(SqlExpression::Column(col_ref))
136                        }
137                    } else {
138                        Err("Expected identifier after '.'".to_string())
139                    }
140                // CAST(expr AS type) / TRY_CAST(expr AS type) — the `AS type`
141                // form is not a normal argument list, so intercept it here and
142                // lower it into a two-arg function call CAST(expr, 'TYPE').
143                } else if (id_upper == "CAST" || id_upper == "TRY_CAST")
144                    && matches!(ExpressionParser::current_token(parser), Token::LeftParen)
145                {
146                    parse_cast_expression(parser, &id_upper)
147                // Check if this is a function call
148                } else if matches!(ExpressionParser::current_token(parser), Token::LeftParen) {
149                    debug!(function = %id_upper, "Parsing function call");
150                    ExpressionParser::advance(parser); // consume (
151                    let (args, has_distinct) = parser.parse_function_args()?;
152                    ExpressionParser::consume(parser, Token::RightParen)?;
153
154                    // Check for OVER clause for window functions
155                    if matches!(ExpressionParser::current_token(parser), Token::Over) {
156                        debug!(function = %id_upper, "Window function detected");
157                        ExpressionParser::advance(parser); // consume OVER
158                        ExpressionParser::consume(parser, Token::LeftParen)?;
159                        let window_spec = parser.parse_window_spec()?;
160                        ExpressionParser::consume(parser, Token::RightParen)?;
161                        Ok(SqlExpression::WindowFunction {
162                            name: id_upper,
163                            args,
164                            window_spec,
165                        })
166                    } else {
167                        Ok(SqlExpression::FunctionCall {
168                            name: id_upper,
169                            args,
170                            distinct: has_distinct,
171                        })
172                    }
173                } else {
174                    // Otherwise treat as simple column
175                    log_parse_decision(
176                        "parse_primary",
177                        &Token::Identifier(id_clone.clone()),
178                        "Column reference",
179                    );
180                    Ok(SqlExpression::Column(ColumnRef::unquoted(id_clone)))
181                }
182            }
183        }
184
185        Token::QuotedIdentifier(id) => {
186            let expr = if ctx.in_method_args {
187                // In method arguments, treat quoted identifiers as string literals
188                log_parse_decision(
189                    "parse_primary",
190                    ExpressionParser::current_token(parser),
191                    "Quoted identifier in method args - treating as string",
192                );
193                SqlExpression::StringLiteral(id.clone())
194            } else {
195                // Otherwise it's a column name like "Customer Id"
196                log_parse_decision(
197                    "parse_primary",
198                    ExpressionParser::current_token(parser),
199                    "Quoted identifier as column name",
200                );
201                SqlExpression::Column(ColumnRef::quoted(id.clone()))
202            };
203            ExpressionParser::advance(parser);
204            Ok(expr)
205        }
206
207        Token::StringLiteral(s) => {
208            trace!("String literal: {}", s);
209            let expr = SqlExpression::StringLiteral(s.clone());
210            ExpressionParser::advance(parser);
211            Ok(expr)
212        }
213
214        Token::NumberLiteral(n) => {
215            trace!("Number literal: {}", n);
216            let expr = SqlExpression::NumberLiteral(n.clone());
217            ExpressionParser::advance(parser);
218            Ok(expr)
219        }
220
221        Token::Null => {
222            trace!("NULL literal");
223            ExpressionParser::advance(parser);
224            Ok(SqlExpression::Null)
225        }
226
227        // Handle LEFT and RIGHT as function names when followed by parentheses
228        Token::Left | Token::Right => {
229            let func_name = match ExpressionParser::current_token(parser) {
230                Token::Left => "LEFT".to_string(),
231                Token::Right => "RIGHT".to_string(),
232                _ => unreachable!(),
233            };
234
235            ExpressionParser::advance(parser);
236
237            // Check if this is a function call
238            if matches!(ExpressionParser::current_token(parser), Token::LeftParen) {
239                debug!(function = %func_name, "Parsing LEFT/RIGHT function call");
240                ExpressionParser::advance(parser); // consume (
241                let (args, _has_distinct) = parser.parse_function_args()?;
242                ExpressionParser::consume(parser, Token::RightParen)?;
243
244                Ok(SqlExpression::FunctionCall {
245                    name: func_name,
246                    args,
247                    distinct: false,
248                })
249            } else {
250                // If not followed by parenthesis, this is likely a JOIN keyword - error
251                Err(format!(
252                    "{} keyword unexpected in expression context",
253                    func_name
254                ))
255            }
256        }
257
258        Token::LeftParen => {
259            debug!("Parsing parenthesized expression or subquery");
260            ExpressionParser::advance(parser); // consume (
261
262            // Check if this is a subquery. It starts with SELECT, or with WITH
263            // for a CTE in expression position (P12) — parse_subquery() dispatches
264            // a leading WITH to the CTE parser, so both forms flow through here.
265            if matches!(
266                ExpressionParser::current_token(parser),
267                Token::Select | Token::With
268            ) {
269                debug!("Detected subquery - parsing SELECT/WITH statement");
270                let subquery = parser.parse_subquery()?;
271                ExpressionParser::consume(parser, Token::RightParen)?;
272                Ok(SqlExpression::ScalarSubquery {
273                    query: Box::new(subquery),
274                })
275            } else {
276                // Parenthesized expression, possibly a tuple for tuple IN:
277                // (a, b) IN (SELECT x, y FROM ...)
278                let first = parser.parse_logical_or()?;
279
280                if matches!(ExpressionParser::current_token(parser), Token::Comma) {
281                    // Collect the remaining tuple elements
282                    let mut exprs = vec![first];
283                    while matches!(ExpressionParser::current_token(parser), Token::Comma) {
284                        ExpressionParser::advance(parser); // consume ,
285                        exprs.push(parser.parse_logical_or()?);
286                    }
287                    ExpressionParser::consume(parser, Token::RightParen)?;
288
289                    // Expect IN or NOT IN immediately after
290                    match ExpressionParser::current_token(parser) {
291                        Token::In => {
292                            ExpressionParser::advance(parser); // consume IN
293                            ExpressionParser::consume(parser, Token::LeftParen)?;
294                            if !matches!(
295                                ExpressionParser::current_token(parser),
296                                Token::Select | Token::With
297                            ) {
298                                return Err("Tuple IN requires a subquery on the right".to_string());
299                            }
300                            let subquery = parser.parse_subquery()?;
301                            ExpressionParser::consume(parser, Token::RightParen)?;
302                            Ok(SqlExpression::InSubqueryTuple {
303                                exprs,
304                                subquery: Box::new(subquery),
305                            })
306                        }
307                        Token::Not => {
308                            ExpressionParser::advance(parser); // consume NOT
309                            if !matches!(ExpressionParser::current_token(parser), Token::In) {
310                                return Err("Expected IN after NOT for tuple".to_string());
311                            }
312                            ExpressionParser::advance(parser); // consume IN
313                            ExpressionParser::consume(parser, Token::LeftParen)?;
314                            if !matches!(
315                                ExpressionParser::current_token(parser),
316                                Token::Select | Token::With
317                            ) {
318                                return Err(
319                                    "Tuple NOT IN requires a subquery on the right".to_string()
320                                );
321                            }
322                            let subquery = parser.parse_subquery()?;
323                            ExpressionParser::consume(parser, Token::RightParen)?;
324                            Ok(SqlExpression::NotInSubqueryTuple {
325                                exprs,
326                                subquery: Box::new(subquery),
327                            })
328                        }
329                        _ => Err(
330                            "A tuple (expr, expr, ...) may only appear as the left side of IN / NOT IN"
331                                .to_string(),
332                        ),
333                    }
334                } else {
335                    // Regular parenthesized expression
336                    debug!("Regular parenthesized expression");
337                    ExpressionParser::consume(parser, Token::RightParen)?;
338                    Ok(first)
339                }
340            }
341        }
342
343        Token::Not => {
344            debug!("Parsing NOT expression");
345            parse_not_expression(parser)
346        }
347
348        Token::Star => {
349            // Handle * as a literal (like in COUNT(*))
350            trace!("Star token as literal");
351            ExpressionParser::advance(parser);
352            Ok(SqlExpression::StringLiteral("*".to_string()))
353        }
354
355        // Handle window-related keywords that can also be column names
356        Token::Row => {
357            trace!("ROW token treated as identifier in expression context");
358            ExpressionParser::advance(parser);
359            Ok(SqlExpression::Column(ColumnRef::unquoted(
360                "row".to_string(),
361            )))
362        }
363
364        Token::Rows => {
365            trace!("ROWS token treated as identifier in expression context");
366            ExpressionParser::advance(parser);
367            Ok(SqlExpression::Column(ColumnRef::unquoted(
368                "rows".to_string(),
369            )))
370        }
371
372        Token::Range => {
373            trace!("RANGE token treated as identifier in expression context");
374            ExpressionParser::advance(parser);
375            Ok(SqlExpression::Column(ColumnRef::unquoted(
376                "range".to_string(),
377            )))
378        }
379
380        Token::Minus => {
381            // Unary minus: -expr is parsed as 0 - expr
382            debug!("Parsing unary minus expression");
383            ExpressionParser::advance(parser);
384            let operand = parse_primary(parser, ctx)?;
385            Ok(SqlExpression::BinaryOp {
386                left: Box::new(SqlExpression::NumberLiteral("0".to_string())),
387                op: "-".to_string(),
388                right: Box::new(operand),
389            })
390        }
391
392        _ => {
393            let err = format!(
394                "Unexpected token in primary expression: {:?}",
395                ExpressionParser::current_token(parser)
396            );
397            debug!(error = %err);
398            Err(err)
399        }
400    };
401
402    trace_parse_exit("parse_primary", &result);
403    result
404}
405
406/// Parse `DATETIME(...)`.
407///
408/// `DATETIME` is lexed as a keyword rather than an identifier, because
409/// `CAST(x AS DATETIME)` needs that type spelling reserved. The cost is that it
410/// never reaches the generic function-call arm of `parse_primary`, so it used to
411/// be assembled here out of `NumberLiteral` tokens straight into a
412/// `DateTimeConstructor` node. That made the components *parse-time constants*:
413/// `DATETIME(2024, 1, 15)` worked, but `DATETIME(Year, Month, Day)` failed with
414/// "Expected year in DateTime constructor" before evaluation ever began, and no
415/// amount of casting helped.
416///
417/// The registry already carries a `DATETIME` function taking runtime values
418/// (`functions::date_time::DateTimeConstructor`, 3-7 args, NULL-propagating), so
419/// the fix is simply to stop intercepting: parse an ordinary argument list and
420/// let the registry evaluate it. Both paths format as `%Y-%m-%d %H:%M:%S%.3f`,
421/// so literal calls are unaffected.
422///
423/// The no-argument `DATETIME()` (today at midnight) keeps its own node — the
424/// registry signature requires at least three arguments, so there is nothing to
425/// delegate to.
426fn parse_datetime_constructor<P>(parser: &mut P) -> Result<SqlExpression, String>
427where
428    P: ParsePrimary + ExpressionParser + ?Sized,
429{
430    ExpressionParser::advance(parser); // consume DateTime
431    ExpressionParser::consume(parser, Token::LeftParen)?;
432
433    // DATETIME() with no arguments is today's date
434    if matches!(ExpressionParser::current_token(parser), Token::RightParen) {
435        ExpressionParser::advance(parser); // consume )
436        debug!("DateTime() - today's date");
437        return Ok(SqlExpression::DateTimeToday {
438            hour: None,
439            minute: None,
440            second: None,
441        });
442    }
443
444    let (args, _distinct) = parser.parse_function_args()?;
445    ExpressionParser::consume(parser, Token::RightParen)?;
446
447    debug!(
448        arg_count = args.len(),
449        "DATETIME parsed as registry function call"
450    );
451
452    Ok(SqlExpression::FunctionCall {
453        name: "DATETIME".to_string(),
454        args,
455        distinct: false,
456    })
457}
458
459/// Parse NOT expression
460fn parse_not_expression<P>(parser: &mut P) -> Result<SqlExpression, String>
461where
462    P: ParsePrimary + ExpressionParser + ?Sized,
463{
464    ExpressionParser::advance(parser); // consume NOT
465
466    // Check if this is a NOT IN expression
467    if let Ok(inner_expr) = parser.parse_comparison() {
468        // After parsing the inner expression, check if we're followed by IN
469        if matches!(ExpressionParser::current_token(parser), Token::In) {
470            debug!("NOT IN expression detected");
471            ExpressionParser::advance(parser); // consume IN
472            ExpressionParser::consume(parser, Token::LeftParen)?;
473            let values = parser.parse_expression_list()?;
474            ExpressionParser::consume(parser, Token::RightParen)?;
475
476            Ok(SqlExpression::NotInList {
477                expr: Box::new(inner_expr),
478                values,
479            })
480        } else {
481            // Regular NOT expression
482            debug!("Regular NOT expression");
483            Ok(SqlExpression::Not {
484                expr: Box::new(inner_expr),
485            })
486        }
487    } else {
488        Err("Expected expression after NOT".to_string())
489    }
490}
491
492/// Parse UNNEST expression
493/// Syntax: UNNEST(column_expr, 'delimiter')
494fn parse_unnest<P>(parser: &mut P) -> Result<SqlExpression, String>
495where
496    P: ParsePrimary + ExpressionParser + ?Sized,
497{
498    debug!("parse_unnest: starting");
499    ExpressionParser::advance(parser); // consume UNNEST
500    ExpressionParser::consume(parser, Token::LeftParen)?;
501
502    // Parse the column expression (first argument)
503    let column = parser.parse_logical_or()?;
504    debug!("parse_unnest: parsed column expression");
505
506    // Expect comma
507    ExpressionParser::consume(parser, Token::Comma)?;
508
509    // Parse the delimiter (second argument - must be a string literal)
510    let delimiter = match ExpressionParser::current_token(parser) {
511        Token::StringLiteral(s) => {
512            let delim = s.clone();
513            ExpressionParser::advance(parser);
514            delim
515        }
516        _ => {
517            return Err("UNNEST delimiter must be a string literal".to_string());
518        }
519    };
520
521    debug!(delimiter = %delimiter, "parse_unnest: parsed delimiter");
522
523    ExpressionParser::consume(parser, Token::RightParen)?;
524
525    debug!("parse_unnest: complete");
526    Ok(SqlExpression::Unnest {
527        column: Box::new(column),
528        delimiter,
529    })
530}
531
532/// Parse a CAST / TRY_CAST expression.
533/// Syntax: `CAST(expr AS type)`.
534/// The current token on entry is the opening `(`. The result is lowered into a
535/// `FunctionCall` so it flows through the existing evaluator and AST machinery:
536/// `CAST(expr, 'TYPE')` where the type name is carried as a string literal.
537fn parse_cast_expression<P>(parser: &mut P, func_name: &str) -> Result<SqlExpression, String>
538where
539    P: ParsePrimary + ExpressionParser + ?Sized,
540{
541    ExpressionParser::advance(parser); // consume (
542
543    let inner = parser.parse_logical_or()?;
544
545    ExpressionParser::consume(parser, Token::As)?;
546
547    let type_name = parse_cast_type_name(parser)?;
548
549    ExpressionParser::consume(parser, Token::RightParen)?;
550
551    let name = if func_name.eq_ignore_ascii_case("TRY_CAST") {
552        "TRY_CAST"
553    } else {
554        "CAST"
555    };
556
557    debug!(target = %type_name, "Parsed CAST expression");
558    Ok(SqlExpression::FunctionCall {
559        name: name.to_string(),
560        args: vec![inner, SqlExpression::StringLiteral(type_name)],
561        distinct: false,
562    })
563}
564
565/// Read a SQL type name for CAST, e.g. `INTEGER`, `VARCHAR`, `DOUBLE`,
566/// `TIMESTAMP`. An optional precision/scale specifier such as `DECIMAL(10, 2)`
567/// or `VARCHAR(50)` is consumed and discarded — we coerce within our own type
568/// confines and do not honour width or scale.
569fn parse_cast_type_name<P>(parser: &mut P) -> Result<String, String>
570where
571    P: ParsePrimary + ExpressionParser + ?Sized,
572{
573    let type_name = match ExpressionParser::current_token(parser) {
574        Token::Identifier(id) => id.clone(),
575        // DATETIME is the one type spelling the lexer reserves as a keyword.
576        Token::DateTime => "DATETIME".to_string(),
577        other => {
578            return Err(format!(
579                "Expected a type name after AS in CAST, got {other:?}"
580            ))
581        }
582    };
583    ExpressionParser::advance(parser);
584
585    // Skip an optional (precision) or (precision, scale) specifier.
586    if matches!(ExpressionParser::current_token(parser), Token::LeftParen) {
587        ExpressionParser::advance(parser); // consume (
588        while !matches!(ExpressionParser::current_token(parser), Token::RightParen) {
589            if matches!(ExpressionParser::current_token(parser), Token::Eof) {
590                return Err("Unterminated type specifier in CAST".to_string());
591            }
592            ExpressionParser::advance(parser);
593        }
594        ExpressionParser::consume(parser, Token::RightParen)?;
595    }
596
597    Ok(type_name)
598}
599
600/// Trait that parsers must implement to use primary expression parsing
601pub trait ParsePrimary {
602    fn current_token(&self) -> &Token;
603    fn advance(&mut self);
604    fn consume(&mut self, expected: Token) -> Result<(), String>;
605
606    // These methods are called from parse_primary
607    fn parse_case_expression(&mut self) -> Result<SqlExpression, String>;
608    fn parse_function_args(&mut self) -> Result<(Vec<SqlExpression>, bool), String>;
609    fn parse_window_spec(&mut self) -> Result<WindowSpec, String>;
610    fn parse_logical_or(&mut self) -> Result<SqlExpression, String>;
611    fn parse_comparison(&mut self) -> Result<SqlExpression, String>;
612    fn parse_expression_list(&mut self) -> Result<Vec<SqlExpression>, String>;
613
614    // For subquery parsing (without parenthesis balance validation)
615    fn parse_subquery(&mut self) -> Result<crate::sql::parser::ast::SelectStatement, String>;
616}