Skip to content

Commit f246801

Browse files
authored
Merge 8dc37bc into 71f7291
2 parents 71f7291 + 8dc37bc commit f246801

File tree

13 files changed

+217
-2
lines changed

13 files changed

+217
-2
lines changed

ydb/library/yql/core/common_opt/yql_co_pgselect.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,25 @@
88

99
namespace NYql {
1010

11+
TExprNode::TPtr BuildArrayAggTraits(TExprContext& ctx, TPositionHandle pos, const TExprNode::TPtr& typeNode) {
12+
return ctx.Builder(pos)
13+
.Callable("AggApply")
14+
.Atom(0, "pg_array_agg")
15+
.Callable(1, "ListItemType")
16+
.Add(0, typeNode)
17+
.Seal()
18+
.Lambda(2)
19+
.Param("row")
20+
.Callable("SingleMember")
21+
.Callable(0, "RemoveSystemMembers")
22+
.Arg(0, "row")
23+
.Seal()
24+
.Seal()
25+
.Seal()
26+
.Seal()
27+
.Build();
28+
}
29+
1130
TNodeMap<ui32> GatherSubLinks(const TExprNode::TPtr& root) {
1231
TNodeMap<ui32> subLinks;
1332

@@ -248,6 +267,32 @@ std::pair<TExprNode::TPtr, TExprNode::TPtr> RewriteSubLinksPartial(TPositionHand
248267
.Seal()
249268
.Seal()
250269
.Build();
270+
} else if (linkType == "array") {
271+
auto typeNode = ctx.NewCallable(node->Pos(), "TypeOf", { select });
272+
auto aggTraits = BuildArrayAggTraits(ctx, node->Pos(), typeNode);
273+
auto aggregate = ctx.Builder(node->Pos())
274+
.Callable("Aggregate")
275+
.Add(0, select)
276+
.List(1)
277+
.Seal()
278+
.List(2)
279+
.List(0)
280+
.Atom(0, "a")
281+
.Add(1, aggTraits)
282+
.Seal()
283+
.Seal()
284+
.List(3)
285+
.Seal()
286+
.Seal()
287+
.Build();
288+
289+
return ctx.Builder(node->Pos())
290+
.Callable("SingleMember")
291+
.Callable(0, "ToOptional")
292+
.Add(0, aggregate)
293+
.Seal()
294+
.Seal()
295+
.Build();
251296
} else if (linkType == "any" || linkType == "all") {
252297
const bool useIn = testLambda->Tail().IsCallable("PgResolvedOp")
253298
&& testLambda->Tail().Head().Content() == "="
@@ -347,6 +392,7 @@ std::pair<TExprNode::TPtr, TExprNode::TPtr> RewriteSubLinksPartial(TPositionHand
347392
TExprNode::TPtr someTraits;
348393
TExprNode::TPtr orTraits;
349394
TExprNode::TPtr andTraits;
395+
TExprNode::TPtr aggArrayTraits = BuildArrayAggTraits(ctx, node->Pos(), selectTypeNode);
350396
for (ui32 factoryIndex = 0; factoryIndex < 4; ++factoryIndex)
351397
{
352398
TStringBuf name;
@@ -474,6 +520,17 @@ std::pair<TExprNode::TPtr, TExprNode::TPtr> RewriteSubLinksPartial(TPositionHand
474520
.Add(1, someTraits)
475521
.Seal()
476522
.Build());
523+
} else if (linkType == "array") {
524+
if (sublinkColumns) {
525+
sublinkColumns->push_back(columnName + "_value");
526+
}
527+
528+
aggregateItems.push_back(ctx.Builder(node->Pos())
529+
.List()
530+
.Atom(0, columnName + "_value")
531+
.Add(1, aggArrayTraits)
532+
.Seal()
533+
.Build());
477534
} else if (linkType == "any") {
478535
if (sublinkColumns) {
479536
sublinkColumns->push_back(columnName + "_count");
@@ -578,6 +635,13 @@ std::pair<TExprNode::TPtr, TExprNode::TPtr> RewriteSubLinksPartial(TPositionHand
578635
.Seal()
579636
.Seal()
580637
.Build();
638+
} else if (linkType == "array") {
639+
return ctx.Builder(node->Pos())
640+
.Callable("Member")
641+
.Add(0, originalArg)
642+
.Atom(1, columnName + "_value")
643+
.Seal()
644+
.Build();
581645
} else if (linkType == "any") {
582646
return ctx.Builder(node->Pos())
583647
.Callable("ToPg")

ydb/library/yql/core/type_ann/type_ann_list.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5391,7 +5391,9 @@ namespace {
53915391
} else {
53925392
const NPg::TAggregateDesc& aggDesc = *aggDescPtr;
53935393
const auto& finalDesc = NPg::LookupProc(aggDesc.FinalFuncId ? aggDesc.FinalFuncId : aggDesc.TransFuncId);
5394-
input->SetTypeAnn(ctx.Expr.MakeType<TPgExprType>(finalDesc.ResultType));
5394+
auto resultType = finalDesc.ResultType;
5395+
AdjustReturnType(resultType, aggDesc.ArgTypes, argTypes);
5396+
input->SetTypeAnn(ctx.Expr.MakeType<TPgExprType>(resultType));
53955397
}
53965398
} else {
53975399
ctx.Expr.AddError(TIssue(ctx.Expr.GetPosition(input->Pos()),

ydb/library/yql/core/type_ann/type_ann_pg.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4924,7 +4924,7 @@ IGraphTransformer::TStatus PgSubLinkWrapper(const TExprNode::TPtr& input, TExprN
49244924
}
49254925

49264926
auto linkType = input->Child(0)->Content();
4927-
if (linkType != "exists" && linkType != "any" && linkType != "all" && linkType != "expr") {
4927+
if (linkType != "exists" && linkType != "any" && linkType != "all" && linkType != "expr" && linkType != "array") {
49284928
ctx.Expr.AddError(TIssue(ctx.Expr.GetPosition(input->Pos()),
49294929
TStringBuilder() << "Unknown link type: " << linkType));
49304930
return IGraphTransformer::TStatus::Error;
@@ -5068,6 +5068,16 @@ IGraphTransformer::TStatus PgSubLinkWrapper(const TExprNode::TPtr& input, TExprN
50685068
}
50695069

50705070
if (linkType == "expr") {
5071+
input->SetTypeAnn(valueType);
5072+
} else if (linkType == "array") {
5073+
if (valueType->GetKind() == ETypeAnnotationKind::Pg) {
5074+
auto typeId = valueType->Cast<TPgExprType>()->GetId();
5075+
const auto& desc = NPg::LookupType(typeId);
5076+
if (desc.TypeId != desc.ArrayTypeId) {
5077+
valueType = ctx.Expr.MakeType<TPgExprType>(desc.ArrayTypeId);
5078+
}
5079+
}
5080+
50715081
input->SetTypeAnn(valueType);
50725082
} else {
50735083
auto result = ctx.Expr.MakeType<TPgExprType>(NPg::LookupType("bool").TypeId);

ydb/library/yql/sql/pg/pg_sql.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3319,6 +3319,9 @@ class TConverter : public IPGParseEvents {
33193319
case EXPR_SUBLINK:
33203320
linkType = "expr";
33213321
break;
3322+
case ARRAY_SUBLINK:
3323+
linkType = "array";
3324+
break;
33223325
default:
33233326
AddError(TStringBuilder() << "SublinkExpr: unsupported link type: " << (int)value->subLinkType);
33243327
return nullptr;

ydb/library/yql/tests/sql/dq_file/part14/canondata/result.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2244,6 +2244,28 @@
22442244
}
22452245
],
22462246
"test.test[pg-select_where-default.txt-Results]": [],
2247+
"test.test[pg-sublink_projection_array-default.txt-Analyze]": [
2248+
{
2249+
"checksum": "a3b64a2cf9903b3868a2dd88a18fc46e",
2250+
"size": 922,
2251+
"uri": "https://{canondata_backend}/1942415/4efc96736f3d5e3406745ae6daac7330e100c4f4/resource.tar.gz#test.test_pg-sublink_projection_array-default.txt-Analyze_/plan.txt"
2252+
}
2253+
],
2254+
"test.test[pg-sublink_projection_array-default.txt-Debug]": [
2255+
{
2256+
"checksum": "655e5401783c2b270b8ab1144307a5ea",
2257+
"size": 1057,
2258+
"uri": "https://{canondata_backend}/1942415/4efc96736f3d5e3406745ae6daac7330e100c4f4/resource.tar.gz#test.test_pg-sublink_projection_array-default.txt-Debug_/opt.yql_patched"
2259+
}
2260+
],
2261+
"test.test[pg-sublink_projection_array-default.txt-Plan]": [
2262+
{
2263+
"checksum": "a3b64a2cf9903b3868a2dd88a18fc46e",
2264+
"size": 922,
2265+
"uri": "https://{canondata_backend}/1942415/4efc96736f3d5e3406745ae6daac7330e100c4f4/resource.tar.gz#test.test_pg-sublink_projection_array-default.txt-Plan_/plan.txt"
2266+
}
2267+
],
2268+
"test.test[pg-sublink_projection_array-default.txt-Results]": [],
22472269
"test.test[pg-sublink_where_any_corr-default.txt-Analyze]": [
22482270
{
22492271
"checksum": "a3b64a2cf9903b3868a2dd88a18fc46e",

ydb/library/yql/tests/sql/dq_file/part18/canondata/result.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2015,6 +2015,28 @@
20152015
}
20162016
],
20172017
"test.test[pg-select_table2-default.txt-Results]": [],
2018+
"test.test[pg-sublink_projection_array_corr-default.txt-Analyze]": [
2019+
{
2020+
"checksum": "a3b64a2cf9903b3868a2dd88a18fc46e",
2021+
"size": 922,
2022+
"uri": "https://{canondata_backend}/1880306/d9c0e7be0cd0986dec7319115d94c6ed554b6ac1/resource.tar.gz#test.test_pg-sublink_projection_array_corr-default.txt-Analyze_/plan.txt"
2023+
}
2024+
],
2025+
"test.test[pg-sublink_projection_array_corr-default.txt-Debug]": [
2026+
{
2027+
"checksum": "4db4e4c8a88507f4118cdb7d0355bea1",
2028+
"size": 2460,
2029+
"uri": "https://{canondata_backend}/1880306/d9c0e7be0cd0986dec7319115d94c6ed554b6ac1/resource.tar.gz#test.test_pg-sublink_projection_array_corr-default.txt-Debug_/opt.yql_patched"
2030+
}
2031+
],
2032+
"test.test[pg-sublink_projection_array_corr-default.txt-Plan]": [
2033+
{
2034+
"checksum": "a3b64a2cf9903b3868a2dd88a18fc46e",
2035+
"size": 922,
2036+
"uri": "https://{canondata_backend}/1880306/d9c0e7be0cd0986dec7319115d94c6ed554b6ac1/resource.tar.gz#test.test_pg-sublink_projection_array_corr-default.txt-Plan_/plan.txt"
2037+
}
2038+
],
2039+
"test.test[pg-sublink_projection_array_corr-default.txt-Results]": [],
20182040
"test.test[pg-sublink_projection_exists_corr-default.txt-Analyze]": [
20192041
{
20202042
"checksum": "b4dd508a329723c74293d80f0278c705",

ydb/library/yql/tests/sql/hybrid_file/part0/canondata/result.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1917,6 +1917,20 @@
19171917
"uri": "https://{canondata_backend}/1936947/581aa6d896ffe57e25bdb8006459e912860e61fa/resource.tar.gz#test.test_pg-sublink_projection_any_corr-default.txt-Plan_/plan.txt"
19181918
}
19191919
],
1920+
"test.test[pg-sublink_projection_array_corr-default.txt-Debug]": [
1921+
{
1922+
"checksum": "101ac1aa64971b4a4bff55aaf5ff6637",
1923+
"size": 2459,
1924+
"uri": "https://{canondata_backend}/1880306/9f21ecea8b7e24ea53f2ae352301fe1250fdba57/resource.tar.gz#test.test_pg-sublink_projection_array_corr-default.txt-Debug_/opt.yql_patched"
1925+
}
1926+
],
1927+
"test.test[pg-sublink_projection_array_corr-default.txt-Plan]": [
1928+
{
1929+
"checksum": "a3b64a2cf9903b3868a2dd88a18fc46e",
1930+
"size": 922,
1931+
"uri": "https://{canondata_backend}/1880306/9f21ecea8b7e24ea53f2ae352301fe1250fdba57/resource.tar.gz#test.test_pg-sublink_projection_array_corr-default.txt-Plan_/plan.txt"
1932+
}
1933+
],
19201934
"test.test[pg-sublink_where_all_corr-default.txt-Debug]": [
19211935
{
19221936
"checksum": "134b7f37a718da9933bee6bb3ade2ccc",

ydb/library/yql/tests/sql/hybrid_file/part7/canondata/result.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1777,6 +1777,20 @@
17771777
"uri": "https://{canondata_backend}/1781765/028f42f897160b53900546b39900217bb2eb9fb1/resource.tar.gz#test.test_pg-sublink_order_in_corr-default.txt-Plan_/plan.txt"
17781778
}
17791779
],
1780+
"test.test[pg-sublink_projection_array-default.txt-Debug]": [
1781+
{
1782+
"checksum": "55aa974a2549922efbe6f9e72b851dfd",
1783+
"size": 1056,
1784+
"uri": "https://{canondata_backend}/937458/9a583559753b9ebbe934c023f3a211aa7e017405/resource.tar.gz#test.test_pg-sublink_projection_array-default.txt-Debug_/opt.yql_patched"
1785+
}
1786+
],
1787+
"test.test[pg-sublink_projection_array-default.txt-Plan]": [
1788+
{
1789+
"checksum": "a3b64a2cf9903b3868a2dd88a18fc46e",
1790+
"size": 922,
1791+
"uri": "https://{canondata_backend}/937458/9a583559753b9ebbe934c023f3a211aa7e017405/resource.tar.gz#test.test_pg-sublink_projection_array-default.txt-Plan_/plan.txt"
1792+
}
1793+
],
17801794
"test.test[pg-sublink_where_in-default.txt-Debug]": [
17811795
{
17821796
"checksum": "77392ca5ed9e77686663d38c7be2b298",

ydb/library/yql/tests/sql/sql2yql/canondata/result.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12305,6 +12305,20 @@
1230512305
"uri": "https://{canondata_backend}/1599023/af9c2f81df0601cf266a0926b5ce73b6101b9115/resource.tar.gz#test_sql2yql.test_pg-sublink_projection_any_corr_/sql.yql"
1230612306
}
1230712307
],
12308+
"test_sql2yql.test[pg-sublink_projection_array]": [
12309+
{
12310+
"checksum": "ebead075c75499a4e450b27fde04644e",
12311+
"size": 1596,
12312+
"uri": "https://{canondata_backend}/1880306/fba36789e4743551e5b5168cd9ea1f6dcbfc6a9b/resource.tar.gz#test_sql2yql.test_pg-sublink_projection_array_/sql.yql"
12313+
}
12314+
],
12315+
"test_sql2yql.test[pg-sublink_projection_array_corr]": [
12316+
{
12317+
"checksum": "7b5b52d304637fd42c41b06bf4306444",
12318+
"size": 2516,
12319+
"uri": "https://{canondata_backend}/1936947/fba4a8aeb243783f27d57f7d9d3fa25582d6472b/resource.tar.gz#test_sql2yql.test_pg-sublink_projection_array_corr_/sql.yql"
12320+
}
12321+
],
1230812322
"test_sql2yql.test[pg-sublink_projection_exists_corr]": [
1230912323
{
1231012324
"checksum": "b371d5b2adf9a0572940b0d5f7d7aaef",
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
--!syntax_pg
2+
select array(select z from (values (1),(2)) a(z));
3+
select array(select z from (values (array[1,2]),(array[3,4])) a(z));
4+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
--!syntax_pg
2+
select y,array(select x+u from (values (10),(20)) c(u)) from (values (1,2),(2,3)) a(x,y) order by y;
3+
select y,array(select array[x+u,x+u+1] from (values (10),(20)) c(u)) from (values (1,2),(2,3)) a(x,y) order by y;
4+

ydb/library/yql/tests/sql/yt_native_file/part14/canondata/result.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2184,6 +2184,27 @@
21842184
"uri": "https://{canondata_backend}/1923547/5154c8bd8ef9ead4f609771f831f20c15e795571/resource.tar.gz#test.test_pg-select_where-default.txt-Results_/results.txt"
21852185
}
21862186
],
2187+
"test.test[pg-sublink_projection_array-default.txt-Debug]": [
2188+
{
2189+
"checksum": "32038a9a570f4d5a128ac51e052a377a",
2190+
"size": 997,
2191+
"uri": "https://{canondata_backend}/1946324/dc0c47f9dda1a75015e81fb216992b9c65d266ce/resource.tar.gz#test.test_pg-sublink_projection_array-default.txt-Debug_/opt.yql"
2192+
}
2193+
],
2194+
"test.test[pg-sublink_projection_array-default.txt-Plan]": [
2195+
{
2196+
"checksum": "a3b64a2cf9903b3868a2dd88a18fc46e",
2197+
"size": 922,
2198+
"uri": "https://{canondata_backend}/1946324/dc0c47f9dda1a75015e81fb216992b9c65d266ce/resource.tar.gz#test.test_pg-sublink_projection_array-default.txt-Plan_/plan.txt"
2199+
}
2200+
],
2201+
"test.test[pg-sublink_projection_array-default.txt-Results]": [
2202+
{
2203+
"checksum": "fa2e8fb85a36491df17f2c83001d9704",
2204+
"size": 1340,
2205+
"uri": "https://{canondata_backend}/1946324/dc0c47f9dda1a75015e81fb216992b9c65d266ce/resource.tar.gz#test.test_pg-sublink_projection_array-default.txt-Results_/results.txt"
2206+
}
2207+
],
21872208
"test.test[pg-sublink_where_any_corr-default.txt-Debug]": [
21882209
{
21892210
"checksum": "3678e8f1e569a0cddcac9626e1338430",

ydb/library/yql/tests/sql/yt_native_file/part18/canondata/result.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1695,6 +1695,27 @@
16951695
"uri": "https://{canondata_backend}/1937027/67f47e98bc150e7387e3e30adedc957caafe9c32/resource.tar.gz#test.test_pg-select_table2-default.txt-Results_/results.txt"
16961696
}
16971697
],
1698+
"test.test[pg-sublink_projection_array_corr-default.txt-Debug]": [
1699+
{
1700+
"checksum": "ce9332a8ad6b7557757786bcbfee8111",
1701+
"size": 2399,
1702+
"uri": "https://{canondata_backend}/1889210/217f5bc86115daa345d05d3491a45286f75061f1/resource.tar.gz#test.test_pg-sublink_projection_array_corr-default.txt-Debug_/opt.yql"
1703+
}
1704+
],
1705+
"test.test[pg-sublink_projection_array_corr-default.txt-Plan]": [
1706+
{
1707+
"checksum": "a3b64a2cf9903b3868a2dd88a18fc46e",
1708+
"size": 922,
1709+
"uri": "https://{canondata_backend}/1889210/217f5bc86115daa345d05d3491a45286f75061f1/resource.tar.gz#test.test_pg-sublink_projection_array_corr-default.txt-Plan_/plan.txt"
1710+
}
1711+
],
1712+
"test.test[pg-sublink_projection_array_corr-default.txt-Results]": [
1713+
{
1714+
"checksum": "ad71e811b260b82c9629cc1cf6b3db0d",
1715+
"size": 2140,
1716+
"uri": "https://{canondata_backend}/1889210/217f5bc86115daa345d05d3491a45286f75061f1/resource.tar.gz#test.test_pg-sublink_projection_array_corr-default.txt-Results_/results.txt"
1717+
}
1718+
],
16981719
"test.test[pg-sublink_projection_exists_corr-default.txt-Debug]": [
16991720
{
17001721
"checksum": "cfbf8ae897e45d465f208f603c38dc45",

0 commit comments

Comments
 (0)