Support qualified function names

...e.g. `db.schema.func()`
This commit is contained in:
Nickolay Ponomarev 2019-04-20 22:20:45 +03:00
parent d4de248c73
commit 4a5dc8dd4b
3 changed files with 17 additions and 13 deletions

View file

@ -54,7 +54,7 @@ pub enum ASTNode {
/// Qualified wildcard, e.g. `alias.*` or `schema.table.*`.
/// (Same caveats apply to SQLQualifiedWildcard as to SQLWildcard.)
SQLQualifiedWildcard(Vec<SQLIdent>),
/// Multi part identifier e.g. `myschema.dbo.mytable`
/// Multi-part identifier, e.g. `table_alias.column` or `schema.table.col`
SQLCompoundIdentifier(Vec<SQLIdent>),
/// `IS NULL` expression
SQLIsNull(Box<ASTNode>),
@ -100,9 +100,8 @@ pub enum ASTNode {
/// SQLValue
SQLValue(Value),
/// Scalar function call e.g. `LEFT(foo, 5)`
/// TODO: this can be a compound SQLObjectName as well (for UDFs)
SQLFunction {
name: SQLIdent,
name: SQLObjectName,
args: Vec<ASTNode>,
over: Option<SQLWindowSpec>,
},
@ -176,7 +175,7 @@ impl ToString for ASTNode {
}
ASTNode::SQLValue(v) => v.to_string(),
ASTNode::SQLFunction { name, args, over } => {
let mut s = format!("{}({})", name, comma_separated_string(args));
let mut s = format!("{}({})", name.to_string(), comma_separated_string(args));
if let Some(o) = over {
s += &format!(" OVER ({})", o.to_string())
}

View file

@ -175,10 +175,10 @@ impl Parser {
expr: Box::new(self.parse_subexpr(p)?),
})
}
// another SQLWord:
// Here `w` is a word, check if it's a part of a multi-part
// identifier, a function call, or a simple identifier:
_ => match self.peek_token() {
Some(Token::LParen) => self.parse_function(w.as_sql_ident()),
Some(Token::Period) => {
Some(Token::LParen) | Some(Token::Period) => {
let mut id_parts: Vec<SQLIdent> = vec![w.as_sql_ident()];
let mut ends_with_wildcard = false;
while self.consume_token(&Token::Period) {
@ -198,7 +198,12 @@ impl Parser {
if ends_with_wildcard {
Ok(ASTNode::SQLQualifiedWildcard(id_parts))
} else {
Ok(ASTNode::SQLCompoundIdentifier(id_parts))
if self.consume_token(&Token::LParen) {
self.prev_token();
self.parse_function(SQLObjectName(id_parts))
} else {
Ok(ASTNode::SQLCompoundIdentifier(id_parts))
}
}
}
_ => Ok(ASTNode::SQLIdentifier(w.as_sql_ident())),
@ -237,7 +242,7 @@ impl Parser {
}
}
pub fn parse_function(&mut self, name: SQLIdent) -> Result<ASTNode, ParserError> {
pub fn parse_function(&mut self, name: SQLObjectName) -> Result<ASTNode, ParserError> {
self.expect_token(&Token::LParen)?;
let args = if self.consume_token(&Token::RParen) {
vec![]

View file

@ -119,7 +119,7 @@ fn parse_select_count_wildcard() {
let select = verified_only_select(sql);
assert_eq!(
&ASTNode::SQLFunction {
name: "COUNT".to_string(),
name: SQLObjectName(vec!["COUNT".to_string()]),
args: vec![ASTNode::SQLWildcard],
over: None,
},
@ -533,7 +533,7 @@ fn parse_scalar_function_in_projection() {
let select = verified_only_select(sql);
assert_eq!(
&ASTNode::SQLFunction {
name: String::from("sqrt"),
name: SQLObjectName(vec![String::from("sqrt")]),
args: vec![ASTNode::SQLIdentifier(String::from("id"))],
over: None,
},
@ -555,7 +555,7 @@ fn parse_window_functions() {
assert_eq!(4, select.projection.len());
assert_eq!(
&ASTNode::SQLFunction {
name: "row_number".to_string(),
name: SQLObjectName(vec!["row_number".to_string()]),
args: vec![],
over: Some(SQLWindowSpec {
partition_by: vec![],
@ -636,7 +636,7 @@ fn parse_delimited_identifiers() {
);
assert_eq!(
&ASTNode::SQLFunction {
name: r#""myfun""#.to_string(),
name: SQLObjectName(vec![r#""myfun""#.to_string()]),
args: vec![],
over: None,
},