Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
31 changes: 31 additions & 0 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,21 @@ pub enum Expr {
},
/// Scalar function call e.g. `LEFT(foo, 5)`
Function(Function),
/// CompositeFunction (function chain)
///
/// Syntax:
///
/// `<arbitrary-expr>.<arbitrary-expr>.<arbitrary-expr>...`
///
/// > `arbitrary-expr` can be any expression including a function call.
///
/// Example:
///
/// ```sql
/// SELECT (SELECT ',' + name FROM sys.objects FOR XML PATH(''), TYPE).value('.','NVARCHAR(MAX)')
/// SELECT CONVERT(XML,'<Book>abc</Book>').value('.','NVARCHAR(MAX)')
/// ```
CompositeFunction(CompositeFunction),
/// `CASE [<operand>] WHEN <condition> THEN <result> ... [ELSE <result>] END`
///
/// Note we only recognize a complete single expression as `<condition>`,
Expand Down Expand Up @@ -1464,6 +1479,7 @@ impl fmt::Display for Expr {
write!(f, " '{}'", &value::escape_single_quote_string(value))
}
Expr::Function(fun) => write!(f, "{fun}"),
Expr::CompositeFunction(fun) => write!(f, "{fun}"),
Expr::Case {
operand,
conditions,
Expand Down Expand Up @@ -5593,6 +5609,21 @@ impl fmt::Display for FunctionArgumentClause {
}
}

/// A composite function call
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub struct CompositeFunction {
pub left: Box<Expr>,
pub right: Function,
}

impl fmt::Display for CompositeFunction {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}.{}", self.left, self.right,)
}
}

#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
Expand Down
51 changes: 46 additions & 5 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1012,7 +1012,7 @@ impl<'a> Parser<'a> {
}

let next_token = self.next_token();
let expr = match next_token.token {
let mut expr = match next_token.token {
Token::Word(w) => match w.keyword {
Keyword::TRUE | Keyword::FALSE | Keyword::NULL => {
self.prev_token();
Expand Down Expand Up @@ -1271,10 +1271,22 @@ impl<'a> Parser<'a> {
)
}
};
Ok(Expr::CompositeAccess {
expr: Box::new(expr),
key,
})
if self.consume_token(&Token::LParen) {
self.prev_token();
let func = match self.parse_function(ObjectName(vec![key]))? {
Expr::Function(func) => func,
_ => unreachable!(),
};
Ok(Expr::CompositeFunction(CompositeFunction {
left: Box::new(expr),
right: func,
}))
} else {
Ok(Expr::CompositeAccess {
expr: Box::new(expr),
key,
})
}
}
}
Token::Placeholder(_) | Token::Colon | Token::AtSign => {
Expand All @@ -1288,6 +1300,35 @@ impl<'a> Parser<'a> {
_ => self.expected("an expression", next_token),
}?;

// Composite function chain
while matches!(
expr,
Expr::Function(_)
| Expr::CompositeFunction { .. }
| Expr::Cast { .. }
| Expr::Convert { .. }
) && self.consume_token(&Token::Period)
{
let tok = self.next_token();
let name = match tok.token {
Token::Word(word) => word.to_ident(),
_ => {
return parser_err!(format!("Expected identifier, found: {tok}"), tok.location)
}
};
if self.consume_token(&Token::LParen) {
self.prev_token();
let func = match self.parse_function(ObjectName(vec![name]))? {
Expr::Function(func) => func,
_ => unreachable!(),
};
expr = Expr::CompositeFunction(CompositeFunction {
left: Box::new(expr),
right: func,
});
}
}

if self.parse_keyword(Keyword::COLLATE) {
Ok(Expr::Collate {
expr: Box::new(expr),
Expand Down
Loading