Skip to content
Open
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
27 changes: 26 additions & 1 deletion src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
/// Lambda keyword syntax: `lambda param : expr` or `lambda param1, param2 : expr`
LambdaKeyword,
}

/// Encapsulates the common pattern in SQL where either one unparenthesized item
/// such as an identifier or expression is permitted, or multiple of the same
/// item in a parenthesized list. For accessing items regardless of the form,
Expand Down
1 change: 1 addition & 0 deletions src/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,7 @@ define_keywords!(
KEY_BLOCK_SIZE,
KILL,
LAG,
LAMBDA,
LANGUAGE,
LARGE,
LAST,
Expand Down
41 changes: 41 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1516,6 +1516,9 @@ impl<'a> Parser<'a> {
Keyword::MAP if *self.peek_token_ref() == Token::LBrace && self.dialect.support_map_literal_syntax() => {
Ok(Some(self.parse_duckdb_map_literal()?))
}
Keyword::LAMBDA if self.dialect.supports_lambda_functions() => {
Ok(Some(self.parse_lambda_expr()?))
}
_ if self.dialect.supports_geometric_types() => match w.keyword {
Keyword::CIRCLE => Ok(Some(self.parse_geometric_type(GeometricTypeKind::Circle)?)),
Keyword::BOX => Ok(Some(self.parse_geometric_type(GeometricTypeKind::GeometricBox)?)),
Expand Down Expand Up @@ -1568,6 +1571,7 @@ impl<'a> Parser<'a> {
Ok(Expr::Lambda(LambdaFunction {
params: OneOrManyWithParens::One(w.clone().into_ident(w_span)),
body: Box::new(self.parse_expr()?),
syntax: LambdaSyntax::Arrow,
}))
}
_ => Ok(Expr::Identifier(w.clone().into_ident(w_span))),
Expand Down Expand Up @@ -2108,10 +2112,47 @@ impl<'a> Parser<'a> {
Ok(Expr::Lambda(LambdaFunction {
params: OneOrManyWithParens::Many(params),
body: Box::new(expr),
syntax: LambdaSyntax::Arrow,
}))
})
}

/// Parses a lambda expression using the `LAMBDA` keyword syntax.
///
/// Syntax: `LAMBDA <params> : <expr>`
///
/// Examples:
/// - `LAMBDA x : x + 1`
/// - `LAMBDA x, i : x > i`
///
/// See <https://duckdb.org/docs/stable/sql/functions/lambda>
fn parse_lambda_expr(&mut self) -> Result<Expr, ParserError> {
// Parse the parameters: either a single identifier or comma-separated identifiers
let params = if self.consume_token(&Token::LParen) {
// Parenthesized parameters: (x, y)
let params = self.parse_comma_separated(|p| p.parse_identifier())?;
self.expect_token(&Token::RParen)?;
OneOrManyWithParens::Many(params)
} else {
// Unparenthesized parameters: x or x, y
let params = self.parse_comma_separated(|p| p.parse_identifier())?;
if params.len() == 1 {
OneOrManyWithParens::One(params.into_iter().next().unwrap())
} else {
OneOrManyWithParens::Many(params)
}
};
// Expect the colon separator
self.expect_token(&Token::Colon)?;
// Parse the body expression
let body = self.parse_expr()?;
Ok(Expr::Lambda(LambdaFunction {
params,
body: Box::new(body),
syntax: LambdaSyntax::LambdaKeyword,
}))
}

/// Tries to parse the body of an [ODBC escaping sequence]
/// i.e. without the enclosing braces
/// Currently implemented:
Expand Down
3 changes: 2 additions & 1 deletion tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15619,7 +15619,8 @@ fn test_lambdas() {
},
],
else_result: Some(Box::new(Expr::value(number("1")))),
})
}),
syntax: LambdaSyntax::Arrow,
})
]
)),
Expand Down
6 changes: 4 additions & 2 deletions tests/sqlparser_databricks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ fn test_databricks_exists() {
),
Expr::Lambda(LambdaFunction {
params: OneOrManyWithParens::One(Ident::new("x")),
body: Box::new(Expr::IsNull(Box::new(Expr::Identifier(Ident::new("x")))))
body: Box::new(Expr::IsNull(Box::new(Expr::Identifier(Ident::new("x"))))),
syntax: LambdaSyntax::Arrow,
})
]
),
Expand Down Expand Up @@ -141,7 +142,8 @@ fn test_databricks_lambdas() {
},
],
else_result: Some(Box::new(Expr::value(number("1"))))
})
}),
syntax: LambdaSyntax::Arrow,
})
]
)),
Expand Down
19 changes: 19 additions & 0 deletions tests/sqlparser_duckdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

// Test lambda with arrow syntax (also supported by DuckDB)
let sql_arrow = "SELECT list_filter([1, 2, 3], x -> x > 1)";
duckdb().verified_stmt(sql_arrow);

// Test lambda with multiple parameters (with index)
let sql_multi = "SELECT list_filter([1, 3, 1, 5], lambda x, i : x > i)";
duckdb().verified_stmt(sql_multi);

// Test lambda in list_transform
let sql_transform = "SELECT list_transform([1, 2, 3], lambda x : x * 2)";
duckdb().verified_stmt(sql_transform);
}
Loading