-
Notifications
You must be signed in to change notification settings - Fork 693
Snowflake: Lambda functions #2192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -827,6 +827,7 @@ define_keywords!( | |
| RECEIVE, | ||
| RECLUSTER, | ||
| RECURSIVE, | ||
| REDUCE, | ||
| REF, | ||
| REFERENCES, | ||
| REFERENCING, | ||
|
|
@@ -1051,6 +1052,7 @@ define_keywords!( | |
| TRACE, | ||
| TRAILING, | ||
| TRANSACTION, | ||
| TRANSFORM, | ||
|
Comment on lines
830
to
1055
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it looks like both no longer need to be keywords in the diff? |
||
| TRANSIENT, | ||
| TRANSLATE, | ||
| TRANSLATE_REGEX, | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -1603,10 +1603,34 @@ impl<'a> Parser<'a> { | |||||||
| value: self.parse_introduced_string_expr()?.into(), | ||||||||
| }) | ||||||||
| } | ||||||||
| // An unreserved word (likely an identifier) is followed by an arrow, | ||||||||
| // which indicates a lambda function with a single, untyped parameter. | ||||||||
| // For example: `a -> a * 2`. | ||||||||
| Token::Arrow if self.dialect.supports_lambda_functions() => { | ||||||||
| self.expect_token(&Token::Arrow)?; | ||||||||
| Ok(Expr::Lambda(LambdaFunction { | ||||||||
| params: OneOrManyWithParens::One(w.to_ident(w_span)), | ||||||||
| params: OneOrManyWithParens::One(LambdaFunctionParameter { | ||||||||
| name: w.to_ident(w_span), | ||||||||
| data_type: None, | ||||||||
| }), | ||||||||
| body: Box::new(self.parse_expr()?), | ||||||||
| syntax: LambdaSyntax::Arrow, | ||||||||
| })) | ||||||||
| } | ||||||||
| // An unreserved word (likely an identifier) that is followed by another word (likley a data type) | ||||||||
| // which is then followed by an arrow, which indicates a lambda function with a single, typed parameter. | ||||||||
| // For example: `a INT -> a * 2`. | ||||||||
| Token::Word(_) | ||||||||
| if self.peek_nth_token_ref(1).token == Token::Arrow | ||||||||
| && self.dialect.supports_lambda_functions() => | ||||||||
|
Comment on lines
+1624
to
+1625
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
thinking probably cleaner/quicker to first check the dialect before looking to parse any further? |
||||||||
| { | ||||||||
| let data_type = self.parse_data_type()?; | ||||||||
| self.expect_token(&Token::Arrow)?; | ||||||||
| Ok(Expr::Lambda(LambdaFunction { | ||||||||
| params: OneOrManyWithParens::One(LambdaFunctionParameter { | ||||||||
| name: w.to_ident(w_span), | ||||||||
| data_type: Some(data_type), | ||||||||
| }), | ||||||||
| body: Box::new(self.parse_expr()?), | ||||||||
| syntax: LambdaSyntax::Arrow, | ||||||||
| })) | ||||||||
|
|
@@ -2192,7 +2216,7 @@ impl<'a> Parser<'a> { | |||||||
| return Ok(None); | ||||||||
| } | ||||||||
| self.maybe_parse(|p| { | ||||||||
| let params = p.parse_comma_separated(|p| p.parse_identifier())?; | ||||||||
| let params = p.parse_comma_separated(|p| p.parse_lambda_function_parameter())?; | ||||||||
| p.expect_token(&Token::RParen)?; | ||||||||
| p.expect_token(&Token::Arrow)?; | ||||||||
| let expr = p.parse_expr()?; | ||||||||
|
|
@@ -2204,7 +2228,7 @@ impl<'a> Parser<'a> { | |||||||
| }) | ||||||||
| } | ||||||||
|
|
||||||||
| /// Parses a lambda expression using the `LAMBDA` keyword syntax. | ||||||||
| /// Parses a lambda expression following the `LAMBDA` keyword syntax. | ||||||||
| /// | ||||||||
| /// Syntax: `LAMBDA <params> : <expr>` | ||||||||
| /// | ||||||||
|
|
@@ -2214,30 +2238,49 @@ impl<'a> Parser<'a> { | |||||||
| /// | ||||||||
| /// 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 = self.parse_lambda_function_parameters()?; | ||||||||
| // 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, | ||||||||
| })) | ||||||||
| } | ||||||||
|
|
||||||||
| /// Parses the parameters of a lambda function with optional typing. | ||||||||
| fn parse_lambda_function_parameters( | ||||||||
| &mut self, | ||||||||
| ) -> Result<OneOrManyWithParens<LambdaFunctionParameter>, 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())?; | ||||||||
| let params = self.parse_comma_separated(|p| p.parse_lambda_function_parameter())?; | ||||||||
| 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())?; | ||||||||
| let params = self.parse_comma_separated(|p| p.parse_lambda_function_parameter())?; | ||||||||
| 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, | ||||||||
| })) | ||||||||
| Ok(params) | ||||||||
| } | ||||||||
|
|
||||||||
| /// Parses a single parameter of a lambda function, with optional typing. | ||||||||
| fn parse_lambda_function_parameter(&mut self) -> Result<LambdaFunctionParameter, ParserError> { | ||||||||
| let name = self.parse_identifier()?; | ||||||||
| let data_type = match self.peek_token().token { | ||||||||
| Token::Word(_) => self.maybe_parse(|p| p.parse_data_type())?, | ||||||||
| _ => None, | ||||||||
| }; | ||||||||
| Ok(LambdaFunctionParameter { name, data_type }) | ||||||||
| } | ||||||||
|
|
||||||||
| /// Tries to parse the body of an [ODBC escaping sequence] | ||||||||
|
|
||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add a link to the docs containing the data_type syntax?