diff options
author | vvvv <vvvv@ydb.tech> | 2023-11-03 10:24:38 +0300 |
---|---|---|
committer | vvvv <vvvv@ydb.tech> | 2023-11-03 10:47:34 +0300 |
commit | 88b1616c72e91f4a588d32eef6b2ee410eee26fb (patch) | |
tree | 7ea37ca7c976d7b93b97caa341808a1419d4bb41 | |
parent | 24f30520adfc572ffda17b0a28b171d9dcc64e17 (diff) | |
download | ydb-88b1616c72e91f4a588d32eef6b2ee410eee26fb.tar.gz |
YQL-17065 convert of lambda to optional callable
5 files changed, 77 insertions, 3 deletions
diff --git a/ydb/library/yql/core/yql_expr_type_annotation.cpp b/ydb/library/yql/core/yql_expr_type_annotation.cpp index 6ba27a50a3..76ff636413 100644 --- a/ydb/library/yql/core/yql_expr_type_annotation.cpp +++ b/ydb/library/yql/core/yql_expr_type_annotation.cpp @@ -4047,15 +4047,26 @@ IGraphTransformer::TStatus TryConvertTo(TExprNode::TPtr& node, const TTypeAnnota IGraphTransformer::TStatus TrySilentConvertTo(TExprNode::TPtr& node, const TTypeAnnotationNode& expectedType, TExprContext& ctx, TConvertFlags flags) { if (node->Type() == TExprNode::Lambda) { - if (expectedType.GetKind() == ETypeAnnotationKind::Callable) { - auto callableType = expectedType.Cast<TCallableExprType>(); + auto currentType = &expectedType; + ui32 optLevel = 0; + while (currentType->GetKind() == ETypeAnnotationKind::Optional) { + currentType = RemoveOptionalType(currentType); + ++optLevel; + } + + if (currentType->GetKind() == ETypeAnnotationKind::Callable) { + auto callableType = currentType->Cast<TCallableExprType>(); auto lambdaArgsCount = node->Head().ChildrenSize(); if (lambdaArgsCount != callableType->GetArgumentsSize()) { return IGraphTransformer::TStatus::Error; } - auto typeNode = ExpandType(node->Pos(), expectedType, ctx); + auto typeNode = ExpandType(node->Pos(), *currentType, ctx); node = ctx.NewCallable(node->Pos(), "Callable", { typeNode, node }); + for (ui32 i = 0; i < optLevel; ++i) { + node = ctx.NewCallable(node->Pos(), "Just", { node }); + } + return IGraphTransformer::TStatus::Repeat; } diff --git a/ydb/library/yql/tests/sql/dq_file/part1/canondata/result.json b/ydb/library/yql/tests/sql/dq_file/part1/canondata/result.json index f350e96313..e682ec071f 100644 --- a/ydb/library/yql/tests/sql/dq_file/part1/canondata/result.json +++ b/ydb/library/yql/tests/sql/dq_file/part1/canondata/result.json @@ -1645,6 +1645,28 @@ } ], "test.test[expr-int_literals_negative-default.txt-Results]": [], + "test.test[expr-lambda_as_optional_callable-default.txt-Analyze]": [ + { + "checksum": "b4dd508a329723c74293d80f0278c705", + "size": 505, + "uri": "https://storage.yandex-team.ru/get-devtools/1924537/bbed9dde10799cefbbe7c225758f9c4faa83e6e8/resource.tar.gz#test.test_expr-lambda_as_optional_callable-default.txt-Analyze_/plan.txt" + } + ], + "test.test[expr-lambda_as_optional_callable-default.txt-Debug]": [ + { + "checksum": "fb5e8e4ad091e267cd55ac4c215c441e", + "size": 626, + "uri": "https://storage.yandex-team.ru/get-devtools/1924537/bbed9dde10799cefbbe7c225758f9c4faa83e6e8/resource.tar.gz#test.test_expr-lambda_as_optional_callable-default.txt-Debug_/opt.yql_patched" + } + ], + "test.test[expr-lambda_as_optional_callable-default.txt-Plan]": [ + { + "checksum": "b4dd508a329723c74293d80f0278c705", + "size": 505, + "uri": "https://storage.yandex-team.ru/get-devtools/1924537/bbed9dde10799cefbbe7c225758f9c4faa83e6e8/resource.tar.gz#test.test_expr-lambda_as_optional_callable-default.txt-Plan_/plan.txt" + } + ], + "test.test[expr-lambda_as_optional_callable-default.txt-Results]": [], "test.test[expr-list_comp-default.txt-Analyze]": [ { "checksum": "01775e7c945a56ebf0edc2d478f4f68d", diff --git a/ydb/library/yql/tests/sql/sql2yql/canondata/result.json b/ydb/library/yql/tests/sql/sql2yql/canondata/result.json index 01f4d6a8fe..bf5e4afaec 100644 --- a/ydb/library/yql/tests/sql/sql2yql/canondata/result.json +++ b/ydb/library/yql/tests/sql/sql2yql/canondata/result.json @@ -5193,6 +5193,13 @@ "uri": "https://storage.yandex-team.ru/get-devtools/1936997/00f46808be87e2ae2d4ac3ac45675b659c5ace45/resource.tar.gz#test_sql2yql.test_expr-iterable_/sql.yql" } ], + "test_sql2yql.test[expr-lambda_as_optional_callable]": [ + { + "checksum": "46aad5173c0a7f606f23b6d03cfed86c", + "size": 2026, + "uri": "https://storage.yandex-team.ru/get-devtools/1936842/5deec1202f467cec0bbdd3c79aa0551b9c23710b/resource.tar.gz#test_sql2yql.test_expr-lambda_as_optional_callable_/sql.yql" + } + ], "test_sql2yql.test[expr-lds_empty_cast]": [ { "checksum": "6e70223493882613d8644a0c89340d2d", @@ -22175,6 +22182,13 @@ "uri": "https://storage.yandex-team.ru/get-devtools/1925821/aeddf2bb85ad22d5cb1dd10f889edcbbe03627bd/resource.tar.gz#test_sql_format.test_expr-iterable_/formatted.sql" } ], + "test_sql_format.test[expr-lambda_as_optional_callable]": [ + { + "checksum": "ec87334044f958f8a55e2796ab9a5fba", + "size": 296, + "uri": "https://storage.yandex-team.ru/get-devtools/1936842/5deec1202f467cec0bbdd3c79aa0551b9c23710b/resource.tar.gz#test_sql_format.test_expr-lambda_as_optional_callable_/formatted.sql" + } + ], "test_sql_format.test[expr-lds_empty_cast]": [ { "checksum": "07b31a32d89779f0fea33ff1c5ccc68a", diff --git a/ydb/library/yql/tests/sql/suites/expr/lambda_as_optional_callable.sql b/ydb/library/yql/tests/sql/suites/expr/lambda_as_optional_callable.sql new file mode 100644 index 0000000000..5700bc9a71 --- /dev/null +++ b/ydb/library/yql/tests/sql/suites/expr/lambda_as_optional_callable.sql @@ -0,0 +1,6 @@ +$callbackType = Callable<(Int32)->Int32>; +$defaultFunc = Callable($callbackType, ($x)->($x)); +$outerType = CallableType(1, Int32, Int32, Optional<$callbackType>); +$outer = Callable($outerType, ($x,$f)->(($f ?? $defaultFunc)($x))); + +select $outer(1,($y)->($y+1)), $outer(1); diff --git a/ydb/library/yql/tests/sql/yt_native_file/part1/canondata/result.json b/ydb/library/yql/tests/sql/yt_native_file/part1/canondata/result.json index b9e7a89462..13485e26bf 100644 --- a/ydb/library/yql/tests/sql/yt_native_file/part1/canondata/result.json +++ b/ydb/library/yql/tests/sql/yt_native_file/part1/canondata/result.json @@ -1570,6 +1570,27 @@ "uri": "https://storage.yandex-team.ru/get-devtools/1942671/959d92cc08f51bc0c5fab116832a712e17138e0d/resource.tar.gz#test.test_expr-int_literals_negative-default.txt-Results_/results.txt" } ], + "test.test[expr-lambda_as_optional_callable-default.txt-Debug]": [ + { + "checksum": "4e8b3e067d6c28bfb2f3eed35b27fda0", + "size": 556, + "uri": "https://storage.yandex-team.ru/get-devtools/1924537/0da76d781ed66022fe1b1a0496a5d61af2781379/resource.tar.gz#test.test_expr-lambda_as_optional_callable-default.txt-Debug_/opt.yql" + } + ], + "test.test[expr-lambda_as_optional_callable-default.txt-Plan]": [ + { + "checksum": "b4dd508a329723c74293d80f0278c705", + "size": 505, + "uri": "https://storage.yandex-team.ru/get-devtools/1924537/0da76d781ed66022fe1b1a0496a5d61af2781379/resource.tar.gz#test.test_expr-lambda_as_optional_callable-default.txt-Plan_/plan.txt" + } + ], + "test.test[expr-lambda_as_optional_callable-default.txt-Results]": [ + { + "checksum": "ec550ba2f891bb6c210e8c457aeabe3a", + "size": 958, + "uri": "https://storage.yandex-team.ru/get-devtools/1924537/0da76d781ed66022fe1b1a0496a5d61af2781379/resource.tar.gz#test.test_expr-lambda_as_optional_callable-default.txt-Results_/results.txt" + } + ], "test.test[expr-list_comp-default.txt-Debug]": [ { "checksum": "a20604290e524bfc03ba751dc992bb9a", |