Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parser support for timestampadd #857

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion dask_planner/src/dialect.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
use core::{iter::Peekable, str::Chars};

use datafusion_sql::sqlparser::dialect::Dialect;
use datafusion_sql::sqlparser::{
ast::{Expr, Function, FunctionArg, FunctionArgExpr, Ident, ObjectName, Value},
dialect::Dialect,
parser::{Parser, ParserError},
tokenizer::Token,
};

#[derive(Debug)]
pub struct DaskDialect {}
Expand Down Expand Up @@ -37,4 +42,46 @@ impl Dialect for DaskDialect {
fn supports_filter_during_aggregation(&self) -> bool {
true
}

/// override expression parsing
fn parse_prefix(&self, parser: &mut Parser) -> Option<Result<Expr, ParserError>> {
fn parse_expr(parser: &mut Parser) -> Result<Option<Expr>, ParserError> {
match parser.peek_token() {
Token::Word(w) if w.value.to_lowercase() == "timestampadd" => {
// TIMESTAMPADD(YEAR, 2, d)
parser.next_token(); // skip timestampadd
parser.expect_token(&Token::LParen)?;
let time_unit = parser.next_token();
parser.expect_token(&Token::Comma)?;
let n = parser.parse_expr()?;
parser.expect_token(&Token::Comma)?;
let expr = parser.parse_expr()?;
parser.expect_token(&Token::RParen)?;

// convert to function args
let args = vec![
FunctionArg::Unnamed(FunctionArgExpr::Expr(Expr::Value(
Value::SingleQuotedString(time_unit.to_string()),
))),
FunctionArg::Unnamed(FunctionArgExpr::Expr(n)),
FunctionArg::Unnamed(FunctionArgExpr::Expr(expr)),
];

Ok(Some(Expr::Function(Function {
name: ObjectName(vec![Ident::new("TIMESTAMPADD")]),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For sake of consistency with other custom functions, could we give this an all lowercase name?

Suggested change
name: ObjectName(vec![Ident::new("TIMESTAMPADD")]),
name: ObjectName(vec![Ident::new("timestampadd")]),

args,
over: None,
distinct: false,
special: false,
Comment on lines +50 to +75
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is so cool! Definitely more convenient than having to go through SQLParser and DataFusion, I can already think of a couple more functions that I'll do this for.

})))
}
_ => Ok(None),
}
}
match parse_expr(parser) {
Ok(Some(expr)) => Some(Ok(expr)),
Ok(None) => None,
Err(e) => Some(Err(e)),
}
}
}
17 changes: 17 additions & 0 deletions dask_planner/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1236,6 +1236,23 @@ impl<'a> DaskParser<'a> {
mod test {
use crate::parser::{DaskParser, DaskStatement};

#[test]
fn timestampadd() {
let sql = "SELECT TIMESTAMPADD(YEAR, 2, d) FROM t";
let statements = DaskParser::parse_sql(sql).unwrap();
assert_eq!(1, statements.len());
let actual = format!("{:?}", statements[0]);
let expected = "projection: [\
UnnamedExpr(Function(Function { name: ObjectName([Ident { value: \"TIMESTAMPADD\", quote_style: None }]), \
args: [\
Unnamed(Expr(Value(SingleQuotedString(\"YEAR\")))), \
Unnamed(Expr(Value(Number(\"2\", false)))), \
Unnamed(Expr(Identifier(Ident { value: \"d\", quote_style: None })))\
], over: None, distinct: false, special: false }))\
]";
assert!(actual.contains(expected));
}

#[test]
fn create_model() {
let sql = r#"CREATE MODEL my_model WITH (
Expand Down