Support EXTRACT function-like operator

The EXTRACT function, for extracting components of a date from a
timestamp, has special syntax: `EXTRACT(<field> FROM <timestamp>)`.
This commit is contained in:
Nikhil Benesch 2019-05-30 01:38:02 -04:00
parent 2f4cd8f6c8
commit d484756182
No known key found for this signature in database
GPG key ID: F7386C5DEADABA7F
3 changed files with 85 additions and 0 deletions

View file

@ -797,6 +797,35 @@ fn parse_cast() {
);
}
#[test]
fn parse_extract() {
let sql = "SELECT EXTRACT(YEAR FROM d)";
let select = verified_only_select(sql);
assert_eq!(
&ASTNode::SQLExtract {
field: SQLDateTimeField::Year,
expr: Box::new(ASTNode::SQLIdentifier("d".to_string())),
},
expr_from_projection(only(&select.projection)),
);
one_statement_parses_to("SELECT EXTRACT(year from d)", "SELECT EXTRACT(YEAR FROM d)");
verified_stmt("SELECT EXTRACT(MONTH FROM d)");
verified_stmt("SELECT EXTRACT(DAY FROM d)");
verified_stmt("SELECT EXTRACT(HOUR FROM d)");
verified_stmt("SELECT EXTRACT(MINUTE FROM d)");
verified_stmt("SELECT EXTRACT(SECOND FROM d)");
let res = parse_sql_statements("SELECT EXTRACT(MILLISECOND FROM d)");
assert_eq!(
ParserError::ParserError(
"Expected Date/time field inside of EXTRACT function, found: MILLISECOND".to_string()
),
res.unwrap_err()
);
}
#[test]
fn parse_create_table() {
let sql = "CREATE TABLE uk_cities (\