adriangb commented on code in PR #2458:
URL:
https://github.com/apache/datafusion-sqlparser-rs/pull/2458#discussion_r3889857013
##########
tests/sqlparser_derive_dialect.rs:
##########
@@ -121,3 +125,65 @@ fn test_identifier_quote_style_overrides() {
None
);
}
+
+#[test]
+fn test_lambda_keyword_syntax_on_postgres_derivative() {
+ // A PostgreSQL derivative can opt into the `LAMBDA` keyword spelling of
+ // lambda functions without giving up `->` as JSON member access. The two
+ // meet in a single expression below: a lambda whose body is a JSON access.
+ derive_dialect!(
+ LambdaPostgreSqlDialect,
+ PostgreSqlDialect,
+ overrides = { supports_lambda_keyword_syntax = true }
+ );
+ let dialect = LambdaPostgreSqlDialect::new();
+
+ // Only the keyword spelling is enabled; the arrow spelling stays off.
+ assert!(dialect.supports_lambda_keyword_syntax());
+ assert!(!dialect.supports_lambda_functions());
+
+ let sql = "SELECT transform(xs, lambda x : (x -> 'a')::INT + 1)";
+ let ast = Parser::parse_sql(&dialect, sql).unwrap();
+ assert_eq!(sql, ast[0].to_string());
+
+ // Round-tripping alone would not distinguish a JSON access from a nested
+ // lambda, since both print as `x -> 'a'`, so check the parsed shape.
+ let Statement::Query(query) = &ast[0] else {
+ panic!("unexpected statement {}", ast[0]);
+ };
+ let Expr::Function(func) =
+ expr_from_projection(only(&query.body.as_select().unwrap().projection))
+ else {
+ panic!("expected a function call");
+ };
+ let FunctionArguments::List(args) = &func.args else {
+ panic!("expected an argument list");
+ };
+ let [_, FunctionArg::Unnamed(FunctionArgExpr::Expr(Expr::Lambda(lambda)))]
= &args.args[..]
+ else {
+ panic!("expected the second argument to be a lambda");
+ };
+
+ // The lambda came from the `LAMBDA` keyword, not from `->`.
+ assert_eq!(LambdaSyntax::LambdaKeyword, lambda.syntax);
+
+ // And the `->` in its body is still JSON member access.
+ let Expr::BinaryOp {
+ left,
+ op: BinaryOperator::Plus,
+ ..
+ } = lambda.body.as_ref()
+ else {
+ panic!("expected the lambda body to be an addition");
+ };
+ let Expr::Cast { expr, .. } = left.as_ref() else {
+ panic!("expected the left operand to be a cast");
+ };
+ let Expr::Nested(json_access) = expr.as_ref() else {
+ panic!("expected the cast operand to be parenthesized");
+ };
+ let Expr::BinaryOp { op, .. } = json_access.as_ref() else {
+ panic!("expected `->` to stay a binary operator");
+ };
+ assert_eq!(&BinaryOperator::Arrow, op);
+}
Review Comment:
Thanks, added but in `sqlparser_custom_dialect.rs`
--
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]