iffyio commented on code in PR #2149:
URL:
https://github.com/apache/datafusion-sqlparser-rs/pull/2149#discussion_r2685990664
##########
src/ast/mod.rs:
##########
@@ -1293,14 +1293,39 @@ pub struct LambdaFunction {
pub params: OneOrManyWithParens<Ident>,
/// The body of the lambda function.
pub body: Box<Expr>,
+ /// The syntax style used to write the lambda function.
+ pub syntax: LambdaSyntax,
}
impl fmt::Display for LambdaFunction {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- write!(f, "{} -> {}", self.params, self.body)
+ match self.syntax {
+ LambdaSyntax::Arrow => write!(f, "{} -> {}", self.params,
self.body),
+ LambdaSyntax::LambdaKeyword => {
+ // For lambda keyword syntax, display params without
parentheses
+ // e.g., `lambda x, y : expr` not `lambda (x, y) : expr`
+ write!(f, "lambda ")?;
+ match &self.params {
+ OneOrManyWithParens::One(p) => write!(f, "{p}")?,
+ OneOrManyWithParens::Many(ps) => write!(f, "{}",
display_comma_separated(ps))?,
+ };
+ write!(f, " : {}", self.body)
+ }
+ }
}
}
+/// The syntax style for a lambda function.
+#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Copy)]
+#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
+#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
+pub enum LambdaSyntax {
+ /// Arrow syntax: `param -> expr` or `(param1, param2) -> expr`
+ Arrow,
Review Comment:
Can we add a link to the databricks documentation describing the syntax?
https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-lambda-functions
similarly we can add one to the duckdb docs for the lambdakeyword variant
##########
tests/sqlparser_duckdb.rs:
##########
@@ -870,3 +870,22 @@ fn parse_extract_single_quotes() {
let sql = "SELECT EXTRACT('month' FROM my_timestamp) FROM my_table";
duckdb().verified_stmt(sql);
}
+
+#[test]
+fn test_duckdb_lambda_function() {
+ // Test basic lambda with list_filter
+ let sql = "SELECT [3, 4, 5, 6].list_filter(lambda x : x > 4)";
+ duckdb().verified_stmt(sql);
Review Comment:
```suggestion
duckdb_and_generic().verified_stmt(sql);
```
we can cover the generic dialect in the test cases as well?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]