Skip to content

Commit e0afca3

Browse files
lovasoaserprex
authored andcommitted
Fix parsing of datetime functions without parenthesis (apache#930)
1 parent 0993206 commit e0afca3

File tree

2 files changed

+29
-81
lines changed

2 files changed

+29
-81
lines changed

src/parser.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -961,17 +961,18 @@ impl<'a> Parser<'a> {
961961
}
962962

963963
pub fn parse_time_functions(&mut self, name: ObjectName) -> Result<Expr, ParserError> {
964-
let (args, order_by) = if self.consume_token(&Token::LParen) {
965-
self.parse_optional_args_with_orderby()?
964+
let (args, order_by, special) = if self.consume_token(&Token::LParen) {
965+
let (args, order_by) = self.parse_optional_args_with_orderby()?;
966+
(args, order_by, false)
966967
} else {
967-
(vec![], vec![])
968+
(vec![], vec![], true)
968969
};
969970
Ok(Expr::Function(Function {
970971
name,
971972
args,
972973
over: None,
973974
distinct: false,
974-
special: false,
975+
special,
975976
order_by,
976977
}))
977978
}

tests/sqlparser_common.rs

+24-77
Original file line numberDiff line numberDiff line change
@@ -6703,90 +6703,37 @@ fn parse_offset_and_limit() {
67036703

67046704
#[test]
67056705
fn parse_time_functions() {
6706-
let sql = "SELECT CURRENT_TIMESTAMP()";
6707-
let select = verified_only_select(sql);
6708-
assert_eq!(
6709-
&Expr::Function(Function {
6710-
name: ObjectName(vec![Ident::new("CURRENT_TIMESTAMP")]),
6711-
args: vec![],
6712-
over: None,
6713-
distinct: false,
6714-
special: false,
6715-
order_by: vec![],
6716-
}),
6717-
expr_from_projection(&select.projection[0])
6718-
);
6719-
6720-
// Validating Parenthesis
6721-
one_statement_parses_to("SELECT CURRENT_TIMESTAMP", sql);
6722-
6723-
let sql = "SELECT CURRENT_TIME()";
6724-
let select = verified_only_select(sql);
6725-
assert_eq!(
6726-
&Expr::Function(Function {
6727-
name: ObjectName(vec![Ident::new("CURRENT_TIME")]),
6728-
args: vec![],
6729-
over: None,
6730-
distinct: false,
6731-
special: false,
6732-
order_by: vec![],
6733-
}),
6734-
expr_from_projection(&select.projection[0])
6735-
);
6736-
6737-
// Validating Parenthesis
6738-
one_statement_parses_to("SELECT CURRENT_TIME", sql);
6739-
6740-
let sql = "SELECT CURRENT_DATE()";
6741-
let select = verified_only_select(sql);
6742-
assert_eq!(
6743-
&Expr::Function(Function {
6744-
name: ObjectName(vec![Ident::new("CURRENT_DATE")]),
6745-
args: vec![],
6746-
over: None,
6747-
distinct: false,
6748-
special: false,
6749-
order_by: vec![],
6750-
}),
6751-
expr_from_projection(&select.projection[0])
6752-
);
6753-
6754-
// Validating Parenthesis
6755-
one_statement_parses_to("SELECT CURRENT_DATE", sql);
6756-
6757-
let sql = "SELECT LOCALTIME()";
6758-
let select = verified_only_select(sql);
6759-
assert_eq!(
6760-
&Expr::Function(Function {
6761-
name: ObjectName(vec![Ident::new("LOCALTIME")]),
6706+
fn test_time_function(func_name: &'static str) {
6707+
let sql = format!("SELECT {}()", func_name);
6708+
let select = verified_only_select(&sql);
6709+
let select_localtime_func_call_ast = Function {
6710+
name: ObjectName(vec![Ident::new(func_name)]),
67626711
args: vec![],
67636712
over: None,
67646713
distinct: false,
67656714
special: false,
67666715
order_by: vec![],
6767-
}),
6768-
expr_from_projection(&select.projection[0])
6769-
);
6770-
6771-
// Validating Parenthesis
6772-
one_statement_parses_to("SELECT LOCALTIME", sql);
6716+
};
6717+
assert_eq!(
6718+
&Expr::Function(select_localtime_func_call_ast.clone()),
6719+
expr_from_projection(&select.projection[0])
6720+
);
67736721

6774-
let sql = "SELECT LOCALTIMESTAMP()";
6775-
let select = verified_only_select(sql);
6776-
assert_eq!(
6777-
&Expr::Function(Function {
6778-
name: ObjectName(vec![Ident::new("LOCALTIMESTAMP")]),
6779-
args: vec![],
6780-
over: None,
6781-
distinct: false,
6782-
special: false,
6783-
order_by: vec![],
6784-
}),
6785-
expr_from_projection(&select.projection[0])
6786-
);
6722+
// Validating Parenthesis
6723+
let sql_without_parens = format!("SELECT {}", func_name);
6724+
let mut ast_without_parens = select_localtime_func_call_ast.clone();
6725+
ast_without_parens.special = true;
6726+
assert_eq!(
6727+
&Expr::Function(ast_without_parens.clone()),
6728+
expr_from_projection(&verified_only_select(&sql_without_parens).projection[0])
6729+
);
6730+
}
67876731

6788-
// Validating Parenthesis
6789-
one_statement_parses_to("SELECT LOCALTIMESTAMP", sql);
6732+
test_time_function("CURRENT_TIMESTAMP");
6733+
test_time_function("CURRENT_TIME");
6734+
test_time_function("CURRENT_DATE");
6735+
test_time_function("LOCALTIME");
6736+
test_time_function("LOCALTIMESTAMP");
67906737
}
67916738

67926739
#[test]

0 commit comments

Comments
 (0)