Skip to content

Commit d95d5c6

Browse files
authored
Fix PG-syntax mixed-types joins (#5556)
1 parent 48e8f3d commit d95d5c6

File tree

12 files changed

+195
-45
lines changed

12 files changed

+195
-45
lines changed

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

+5
Original file line numberDiff line numberDiff line change
@@ -1765,6 +1765,11 @@ std::tuple<TVector<ui32>, TExprNode::TListType> BuildJoinGroups(TPositionHandle
17651765
break;
17661766
}
17671767

1768+
if (left->GetTypeAnn() != right->GetTypeAnn()) {
1769+
bad = true;
1770+
break;
1771+
}
1772+
17681773
bool leftOnLeft;
17691774
if (left->IsCallable("Member") && &left->Head() == &predicate->Head().Head()) {
17701775
auto inputPtr = memberToInput.FindPtr(left->Child(1)->Content());

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

+4
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,10 @@ TExprNode::TPtr DecayCrossJoinIntoInner(TExprNode::TPtr equiJoin, const TExprNod
850850
return equiJoin;
851851
}
852852

853+
if (left->GetTypeAnn() != right->GetTypeAnn()) {
854+
return equiJoin;
855+
}
856+
853857
TSet<ui32> leftInputs, rightInputs;
854858
TSet<TStringBuf> usedFields;
855859
GatherJoinInputs(left, row, parentsMap, backRenameMap, labels, leftInputs, usedFields);

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

+22
Original file line numberDiff line numberDiff line change
@@ -1855,6 +1855,28 @@
18551855
}
18561856
],
18571857
"test.test[pg-aggregate_combine--Results]": [],
1858+
"test.test[pg-join_mixed_types--Analyze]": [
1859+
{
1860+
"checksum": "5218503de8ae47338f8fb364127bedf9",
1861+
"size": 7433,
1862+
"uri": "https://{canondata_backend}/1925842/1afddaadfa4aa57d0e396b052de71d638933dcd4/resource.tar.gz#test.test_pg-join_mixed_types--Analyze_/plan.txt"
1863+
}
1864+
],
1865+
"test.test[pg-join_mixed_types--Debug]": [
1866+
{
1867+
"checksum": "0df0e7f9a4182bb4912483a5c1fe87db",
1868+
"size": 3066,
1869+
"uri": "https://{canondata_backend}/1925842/1afddaadfa4aa57d0e396b052de71d638933dcd4/resource.tar.gz#test.test_pg-join_mixed_types--Debug_/opt.yql_patched"
1870+
}
1871+
],
1872+
"test.test[pg-join_mixed_types--Plan]": [
1873+
{
1874+
"checksum": "5218503de8ae47338f8fb364127bedf9",
1875+
"size": 7433,
1876+
"uri": "https://{canondata_backend}/1925842/1afddaadfa4aa57d0e396b052de71d638933dcd4/resource.tar.gz#test.test_pg-join_mixed_types--Plan_/plan.txt"
1877+
}
1878+
],
1879+
"test.test[pg-join_mixed_types--Results]": [],
18581880
"test.test[pg-order_by_distinct_same_expr_agg_sublink-default.txt-Analyze]": [
18591881
{
18601882
"checksum": "c1f2d837c3623c81dd596a9877913fb8",

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

+14
Original file line numberDiff line numberDiff line change
@@ -1917,6 +1917,20 @@
19171917
"uri": "https://{canondata_backend}/1936842/5461a7f0f4d722c81cba2eff5dd1d41bf3a77f80/resource.tar.gz#test.test_pg-insert--Plan_/plan.txt"
19181918
}
19191919
],
1920+
"test.test[pg-join_mixed_types--Debug]": [
1921+
{
1922+
"checksum": "687483f04fd8a538dd4587f805e558d5",
1923+
"size": 5602,
1924+
"uri": "https://{canondata_backend}/1925842/b16a3fb835f9baf17f7c68e238f1ff3fa411b08e/resource.tar.gz#test.test_pg-join_mixed_types--Debug_/opt.yql_patched"
1925+
}
1926+
],
1927+
"test.test[pg-join_mixed_types--Plan]": [
1928+
{
1929+
"checksum": "569223ae3507b171c9b19d3fceefb315",
1930+
"size": 8853,
1931+
"uri": "https://{canondata_backend}/1925842/b16a3fb835f9baf17f7c68e238f1ff3fa411b08e/resource.tar.gz#test.test_pg-join_mixed_types--Plan_/plan.txt"
1932+
}
1933+
],
19201934
"test.test[pg-join_using6-default.txt-Debug]": [
19211935
{
19221936
"checksum": "4ad19c912ddd72af2c79985283518a08",

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

+7
Original file line numberDiff line numberDiff line change
@@ -11983,6 +11983,13 @@
1198311983
"uri": "https://{canondata_backend}/1881367/79a71c1478c556da1931a7565c12bdd14cc63567/resource.tar.gz#test_sql2yql.test_pg-join_groups_/sql.yql"
1198411984
}
1198511985
],
11986+
"test_sql2yql.test[pg-join_mixed_types]": [
11987+
{
11988+
"checksum": "b364a4a34d7f99a7ffb8ac6b4b3197f3",
11989+
"size": 1691,
11990+
"uri": "https://{canondata_backend}/1936997/14bc828ad4bae6165180fbcaed4d8486bbb77859/resource.tar.gz#test_sql2yql.test_pg-join_mixed_types_/sql.yql"
11991+
}
11992+
],
1198611993
"test_sql2yql.test[pg-join_tree_order]": [
1198711994
{
1198811995
"checksum": "ccdf19008cb38637422026d0ea06e9b8",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{"id1"=0;"id2"=0};
2+
{"id1"=0;"id2"=1};
3+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"_yql_row_spec"={
3+
"Type"=["StructType";[["id1";["PgType";"Int4";];];["id2";["PgType";"Int8";];];];];
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
in Input input_misc_types.txt
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
--!syntax_pg
2+
SELECT t1.* FROM plato."Input" t1 JOIN plato."Input" t2 ON (t1.id1=t2.id2);
3+
SELECT t1.* FROM plato."Input" t1, plato."Input" t2 WHERE (t1.id1=t2.id2);
4+

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

+21
Original file line numberDiff line numberDiff line change
@@ -1794,6 +1794,27 @@
17941794
"uri": "https://{canondata_backend}/1809005/81aef895e3303f900cc0cb6245596fe9a0da6573/resource.tar.gz#test.test_pg-aggregate_combine--Results_/results.txt"
17951795
}
17961796
],
1797+
"test.test[pg-join_mixed_types--Debug]": [
1798+
{
1799+
"checksum": "87186caeeba9fdcbd4baa1e7b1d4415c",
1800+
"size": 5542,
1801+
"uri": "https://{canondata_backend}/1925842/a75b2a850dffc93078af24b6960712b9ee737f6e/resource.tar.gz#test.test_pg-join_mixed_types--Debug_/opt.yql"
1802+
}
1803+
],
1804+
"test.test[pg-join_mixed_types--Plan]": [
1805+
{
1806+
"checksum": "e61d62c499a0a562918ffa9a71d0abfa",
1807+
"size": 8853,
1808+
"uri": "https://{canondata_backend}/1925842/a75b2a850dffc93078af24b6960712b9ee737f6e/resource.tar.gz#test.test_pg-join_mixed_types--Plan_/plan.txt"
1809+
}
1810+
],
1811+
"test.test[pg-join_mixed_types--Results]": [
1812+
{
1813+
"checksum": "613fab0dfa1e220391713e252e265be9",
1814+
"size": 2314,
1815+
"uri": "https://{canondata_backend}/1925842/a75b2a850dffc93078af24b6960712b9ee737f6e/resource.tar.gz#test.test_pg-join_mixed_types--Results_/results.txt"
1816+
}
1817+
],
17971818
"test.test[pg-order_by_distinct_same_expr_agg_sublink-default.txt-Debug]": [
17981819
{
17991820
"checksum": "add068636ef7667f017803290484353e",

ydb/tests/functional/suite_tests/canondata/test_postgres.TestPGSQL.test_sql_suite_plan-jointest_join0.test_/query_2.plan

+54-37
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,10 @@
3030
"Plans": [
3131
{
3232
"CTE Name": "precompute_0_0",
33-
"Node Type": "Limit-CrossJoin-InnerJoin (MapJoin)-ConstantExpr-Filter-TableFullScan-ConstantExpr",
33+
"Node Type": "Limit-Filter-CrossJoin-TableFullScan-ConstantExpr-ConstantExpr-InnerJoin (JoinDict)-ConstantExpr-ConstantExpr-InnerJoin (JoinDict)-ConstantExpr",
3434
"Operators": [
3535
{
3636
"Inputs": [
37-
{
38-
"InternalOperatorId": 1
39-
},
40-
{
41-
"InternalOperatorId": 1
42-
},
43-
{
44-
"InternalOperatorId": 1
45-
},
46-
{
47-
"InternalOperatorId": 1
48-
},
49-
{
50-
"InternalOperatorId": 1
51-
},
5237
{
5338
"InternalOperatorId": 1
5439
},
@@ -69,38 +54,24 @@
6954
"Inputs": [
7055
{
7156
"InternalOperatorId": 2
72-
},
73-
{
74-
"InternalOperatorId": 6
7557
}
7658
],
77-
"Name": "CrossJoin"
59+
"Name": "Filter",
60+
"Predicate": "item.b.Sum0 == item.t.thousand And item.a.f1 + item.b.Sum0 + 999 == item.t.tenthous"
7861
},
7962
{
80-
"Condition": "thousand = Sum0",
8163
"Inputs": [
8264
{
83-
"InternalOperatorId": 4
65+
"InternalOperatorId": 3
8466
},
8567
{
86-
"InternalOperatorId": 3
87-
}
88-
],
89-
"Name": "InnerJoin (MapJoin)"
90-
},
91-
{
92-
"Inputs": [],
93-
"Name": "ToFlow",
94-
"ToFlow": "precompute_0_0"
95-
},
96-
{
97-
"Inputs": [
68+
"InternalOperatorId": 9
69+
},
9870
{
99-
"InternalOperatorId": 5
71+
"InternalOperatorId": 9
10072
}
10173
],
102-
"Name": "Filter",
103-
"Predicate": "Exist(item.thousand)"
74+
"Name": "CrossJoin"
10475
},
10576
{
10677
"Inputs": [],
@@ -116,6 +87,52 @@
11687
"Scan": "Parallel",
11788
"Table": "postgres_jointest/join0.test_plan/tenk1"
11889
},
90+
{
91+
"Inputs": [
92+
{
93+
"InternalOperatorId": 5
94+
}
95+
],
96+
"Name": "ToFlow",
97+
"ToFlow": "precompute_0_0"
98+
},
99+
{
100+
"Inputs": [],
101+
"Member": "precompute_0_0",
102+
"Name": "Member"
103+
},
104+
{
105+
"Inputs": [
106+
{
107+
"InternalOperatorId": 8
108+
},
109+
{
110+
"InternalOperatorId": 10
111+
}
112+
],
113+
"Name": "InnerJoin (JoinDict)"
114+
},
115+
{
116+
"Inputs": [],
117+
"Name": "ToFlow",
118+
"ToFlow": "precompute_0_0"
119+
},
120+
{
121+
"Inputs": [],
122+
"Name": "ToFlow",
123+
"ToFlow": "precompute_0_0"
124+
},
125+
{
126+
"Inputs": [
127+
{
128+
"InternalOperatorId": 8
129+
},
130+
{
131+
"InternalOperatorId": 10
132+
}
133+
],
134+
"Name": "InnerJoin (JoinDict)"
135+
},
119136
{
120137
"Inputs": [],
121138
"Name": "ToFlow",

ydb/tests/functional/suite_tests/canondata/test_postgres.TestPGSQL.test_sql_suite_plan-select.test_/query_12.plan

+55-8
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,61 @@
2929
"PlanNodeType": "Connection",
3030
"Plans": [
3131
{
32-
"Node Type": "Limit-InnerJoin (MapJoin)-Filter-TableFullScan",
32+
"Node Type": "Limit-Filter-CrossJoin-TableFullScan",
3333
"Operators": [
3434
{
3535
"Inputs": [
36+
{
37+
"InternalOperatorId": 1
38+
},
39+
{
40+
"InternalOperatorId": 1
41+
},
42+
{
43+
"InternalOperatorId": 1
44+
},
45+
{
46+
"InternalOperatorId": 1
47+
},
48+
{
49+
"InternalOperatorId": 1
50+
},
51+
{
52+
"InternalOperatorId": 1
53+
},
54+
{
55+
"InternalOperatorId": 1
56+
},
57+
{
58+
"InternalOperatorId": 1
59+
},
60+
{
61+
"InternalOperatorId": 1
62+
},
63+
{
64+
"InternalOperatorId": 1
65+
},
66+
{
67+
"InternalOperatorId": 1
68+
},
69+
{
70+
"InternalOperatorId": 1
71+
},
72+
{
73+
"InternalOperatorId": 1
74+
},
75+
{
76+
"InternalOperatorId": 1
77+
},
78+
{
79+
"InternalOperatorId": 1
80+
},
81+
{
82+
"InternalOperatorId": 1
83+
},
84+
{
85+
"InternalOperatorId": 1
86+
},
3687
{
3788
"InternalOperatorId": 1
3889
}
@@ -41,25 +92,21 @@
4192
"Name": "Limit"
4293
},
4394
{
44-
"Condition": "unique1,stringu1 = i,j",
4595
"Inputs": [
4696
{
4797
"InternalOperatorId": 2
48-
},
49-
{
50-
"Other": "ConstantExpression"
5198
}
5299
],
53-
"Name": "InnerJoin (MapJoin)"
100+
"Name": "Filter",
101+
"Predicate": "item.onek.unique1 == item.v.i And item.onek.stringu1 == item.v.j"
54102
},
55103
{
56104
"Inputs": [
57105
{
58106
"InternalOperatorId": 3
59107
}
60108
],
61-
"Name": "Filter",
62-
"Predicate": "Exist(item.stringu1) And Exist(item.unique1)"
109+
"Name": "CrossJoin"
63110
},
64111
{
65112
"Inputs": [],

0 commit comments

Comments
 (0)