Skip to content

Commit

Permalink
Fix panic with CAST without type name
Browse files Browse the repository at this point in the history
  • Loading branch information
gwenn committed Mar 16, 2024
1 parent 3910b46 commit 49ecded
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/lexer/sql/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,11 @@ fn missing_join_clause() {
);
}

#[test]
fn cast_without_typename() {
parse_cmd(b"SELECT CAST(a AS ) FROM t");
}

#[test]
fn unknown_table_option() {
expect_parser_err_msg(b"CREATE TABLE t(x)o", "unknown table option: o");
Expand Down
4 changes: 3 additions & 1 deletion src/parser/ast/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,9 @@ impl ToTokens for Expr {
s.append(TK_LP, None)?;
expr.to_tokens(s)?;
s.append(TK_AS, None)?;
type_name.to_tokens(s)?;
if let Some(ref type_name) = type_name {
type_name.to_tokens(s)?;
}
s.append(TK_RP, None)
}
Expr::Collate(expr, collation) => {
Expand Down
4 changes: 2 additions & 2 deletions src/parser/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ pub enum Expr {
/// expression
expr: Box<Expr>,
/// `AS` type name
type_name: Type,
type_name: Option<Type>,
},
/// `COLLATE`: expression
Collate(Box<Expr>, String),
Expand Down Expand Up @@ -404,7 +404,7 @@ impl Expr {
Expr::Collate(Box::new(x), from_token(ct, c))
}
/// Constructor
pub fn cast(x: Expr, type_name: Type) -> Expr {
pub fn cast(x: Expr, type_name: Option<Type>) -> Expr {
Expr::Cast {
expr: Box::new(x),
type_name,
Expand Down
2 changes: 1 addition & 1 deletion src/parser/parse.y
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,7 @@ expr(A) ::= expr(X) COLLATE ids(C). {
}
%ifndef SQLITE_OMIT_CAST
expr(A) ::= CAST LP expr(E) AS typetoken(T) RP. {
A = Expr::cast(E, T.unwrap()); // FIXME mandatory ?
A = Expr::cast(E, T);
}
%endif SQLITE_OMIT_CAST

Expand Down

0 comments on commit 49ecded

Please sign in to comment.