You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "lovasoa (via GitHub)" <gi...@apache.org> on 2026/01/05 15:26:03 UTC

[PR] Add support for DuckDB `LAMBDA` keyword syntax [datafusion-sqlparser-rs]

lovasoa opened a new pull request, #2149:
URL: https://github.com/apache/datafusion-sqlparser-rs/pull/2149

   ## Summary
   
   This PR adds support for parsing DuckDB's `LAMBDA` keyword syntax for lambda functions.
   
   DuckDB supports two syntaxes for lambda functions:
   - Arrow syntax: `x -> x + 1` (already supported)
   - Lambda keyword syntax: `lambda x : x + 1` (added in this PR)
   
   ### Changes
   
   - Added `LAMBDA` keyword to keywords list
   - Added `LambdaSyntax` enum to track which syntax style was used (for correct round-tripping)
   - Added `parse_lambda_expr()` to parse `lambda <params> : <expr>` syntax
   - Updated `LambdaFunction` struct to include `syntax` field
   - Added tests for DuckDB lambda functions
   
   ### Examples
   
   ```sql
   -- Single parameter
   SELECT list_filter([1, 2, 3], lambda x : x > 1)
   
   -- Multiple parameters (with index)
   SELECT list_filter([1, 3, 1, 5], lambda x, i : x > i)
   
   -- Method call syntax
   SELECT [3, 4, 5, 6].list_filter(lambda x : x > 4)
   ```
   
   ## Test plan
   
   - [x] Added `test_duckdb_lambda_function` test covering single param, multi-param, and method call syntax
   - [x] All existing lambda tests pass (Databricks, ClickHouse, common)
   - [x] All DuckDB tests pass
   
   ---
   
   > [!NOTE]
   > This code was generated by an LLM (Claude) and reviewed by @lovasoa.
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Add support for DuckDB `LAMBDA` keyword syntax [datafusion-sqlparser-rs]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #2149:
URL: https://github.com/apache/datafusion-sqlparser-rs/pull/2149#issuecomment-3743856681

   (thanks for the review @iffyio )


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Add support for DuckDB `LAMBDA` keyword syntax [datafusion-sqlparser-rs]

Posted by "lovasoa (via GitHub)" <gi...@apache.org>.
lovasoa commented on PR #2149:
URL: https://github.com/apache/datafusion-sqlparser-rs/pull/2149#issuecomment-3744375535

   @iffyio @alamb thanks for the review! I addressed your comments.


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Add support for DuckDB `LAMBDA` keyword syntax [datafusion-sqlparser-rs]

Posted by "lovasoa (via GitHub)" <gi...@apache.org>.
lovasoa commented on PR #2149:
URL: https://github.com/apache/datafusion-sqlparser-rs/pull/2149#issuecomment-3724055602

   Hi @iffyio, @alamb ! Do you think you could have a look at this ? The old duckdb lambda syntax is deprecated, so I suppose this is going to pop out more and more. This comes from a bug report in sqlpage.


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Add support for DuckDB `LAMBDA` keyword syntax [datafusion-sqlparser-rs]

Posted by "iffyio (via GitHub)" <gi...@apache.org>.
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Add support for DuckDB `LAMBDA` keyword syntax [datafusion-sqlparser-rs]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb merged PR #2149:
URL: https://github.com/apache/datafusion-sqlparser-rs/pull/2149


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org