Skip to content

Commit ae2d032

Browse files
authored
Fixed IN with tuple input in some cases (#10605)
1 parent 93e2e23 commit ae2d032

File tree

21 files changed

+314
-9
lines changed

21 files changed

+314
-9
lines changed

ydb/library/yql/core/peephole_opt/yql_opt_peephole_physical.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1974,13 +1974,12 @@ TExprNode::TPtr ExpandSqlIn(const TExprNode::TPtr& input, TExprContext& ctx) {
19741974
auto collection = input->HeadPtr();
19751975
auto lookup = input->ChildPtr(1);
19761976
auto options = input->ChildPtr(2);
1977+
const bool nullableCollectionItems = IsSqlInCollectionItemsNullable(NNodes::TCoSqlIn(input));
19771978

19781979
const bool ansiIn = HasSetting(*options, "ansi");
19791980
const bool tableSource = HasSetting(*options, "tableSource");
19801981
static const size_t MaxCollectionItemsToExpandAsOrChain = 5;
1981-
const bool hasOptionals = collection->GetTypeAnn()->HasOptionalOrNull() ||
1982-
lookup->GetTypeAnn()->HasOptionalOrNull();
1983-
if (ansiIn || !hasOptionals) {
1982+
if (ansiIn || !nullableCollectionItems) {
19841983
const size_t collectionSize = collection->ChildrenSize();
19851984
if ((collection->IsCallable("AsList") || collection->IsList()) &&
19861985
collectionSize <= MaxCollectionItemsToExpandAsOrChain &&
@@ -2068,7 +2067,6 @@ TExprNode::TPtr ExpandSqlIn(const TExprNode::TPtr& input, TExprContext& ctx) {
20682067
.Build();
20692068
}
20702069

2071-
const bool nullableCollectionItems = IsSqlInCollectionItemsNullable(NNodes::TCoSqlIn(input));
20722070
if (ansiIn && (nullableCollectionItems || lookupType->HasOptionalOrNull())) {
20732071
YQL_CLOG(DEBUG, CorePeepHole) << "ANSI IN: with nullable items in collection or lookup";
20742072
YQL_ENSURE(dict);

ydb/library/yql/core/yql_expr_type_annotation.cpp

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4924,21 +4924,53 @@ bool IsSqlInCollectionItemsNullable(const NNodes::TCoSqlIn& node) {
49244924
collectionType = collectionType->Cast<TOptionalExprType>()->GetItemType();
49254925
}
49264926

4927+
auto lookupType = node.Lookup().Ref().GetTypeAnn();
4928+
49274929
const auto collectionKind = collectionType->GetKind();
49284930
bool result = false;
49294931
switch (collectionKind) {
49304932
case ETypeAnnotationKind::Tuple:
49314933
{
49324934
const auto tupleType = collectionType->Cast<TTupleExprType>();
4933-
result = AnyOf(tupleType->GetItems(), [](const auto& item) { return item->HasOptionalOrNull(); } );
4935+
for (const auto& item : tupleType->GetItems()) {
4936+
if (item->HasOptionalOrNull()) {
4937+
result = true;
4938+
break;
4939+
}
4940+
4941+
auto cmp = CanCompare<true>(lookupType, item);
4942+
if (cmp == ECompareOptions::Optional || cmp == ECompareOptions::Null) {
4943+
result = true;
4944+
break;
4945+
}
4946+
}
4947+
49344948
break;
49354949
}
4936-
case ETypeAnnotationKind::Dict:
4937-
result = collectionType->Cast<TDictExprType>()->GetKeyType()->HasOptionalOrNull();
4950+
case ETypeAnnotationKind::Dict: {
4951+
if (collectionType->Cast<TDictExprType>()->GetKeyType()->HasOptionalOrNull()) {
4952+
result = true;
4953+
} else {
4954+
auto cmp = CanCompare<true>(lookupType, collectionType->Cast<TDictExprType>()->GetKeyType());
4955+
if (cmp == ECompareOptions::Optional || cmp == ECompareOptions::Null) {
4956+
result = true;
4957+
}
4958+
}
4959+
49384960
break;
4939-
case ETypeAnnotationKind::List:
4940-
result = collectionType->Cast<TListExprType>()->GetItemType()->HasOptionalOrNull();
4961+
}
4962+
case ETypeAnnotationKind::List: {
4963+
if (collectionType->Cast<TListExprType>()->GetItemType()->HasOptionalOrNull()) {
4964+
result = true;
4965+
} else {
4966+
auto cmp = CanCompare<true>(lookupType, collectionType->Cast<TListExprType>()->GetItemType());
4967+
if (cmp == ECompareOptions::Optional || cmp == ECompareOptions::Null) {
4968+
result = true;
4969+
}
4970+
}
4971+
49414972
break;
4973+
}
49424974
case ETypeAnnotationKind::EmptyDict:
49434975
case ETypeAnnotationKind::EmptyList:
49444976
case ETypeAnnotationKind::Null:

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,28 @@
10171017
}
10181018
],
10191019
"test.test[in-in_with_nulls_and_optionals_extra-default.txt-Results]": [],
1020+
"test.test[in-large_in_YQL-19183--Analyze]": [
1021+
{
1022+
"checksum": "b4dd508a329723c74293d80f0278c705",
1023+
"size": 505,
1024+
"uri": "https://{canondata_backend}/1881367/03ce4da085261f32ea1c441399858f72350f0970/resource.tar.gz#test.test_in-large_in_YQL-19183--Analyze_/plan.txt"
1025+
}
1026+
],
1027+
"test.test[in-large_in_YQL-19183--Debug]": [
1028+
{
1029+
"checksum": "6cd461af12a4f4f8bf6d3b148d669f25",
1030+
"size": 594,
1031+
"uri": "https://{canondata_backend}/1881367/03ce4da085261f32ea1c441399858f72350f0970/resource.tar.gz#test.test_in-large_in_YQL-19183--Debug_/opt.yql_patched"
1032+
}
1033+
],
1034+
"test.test[in-large_in_YQL-19183--Plan]": [
1035+
{
1036+
"checksum": "b4dd508a329723c74293d80f0278c705",
1037+
"size": 505,
1038+
"uri": "https://{canondata_backend}/1881367/03ce4da085261f32ea1c441399858f72350f0970/resource.tar.gz#test.test_in-large_in_YQL-19183--Plan_/plan.txt"
1039+
}
1040+
],
1041+
"test.test[in-large_in_YQL-19183--Results]": [],
10201042
"test.test[insert-append_sorted-to_sorted-Analyze]": [
10211043
{
10221044
"checksum": "c81430c2e09d6c34d9891ba4daf09f31",

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
@@ -1145,6 +1145,28 @@
11451145
"uri": "file://test.test_flatten_by-flatten_dict--Results_/extracted"
11461146
}
11471147
],
1148+
"test.test[in-small_in_YQL-19183-ansi-Analyze]": [
1149+
{
1150+
"checksum": "b4dd508a329723c74293d80f0278c705",
1151+
"size": 505,
1152+
"uri": "https://{canondata_backend}/1903885/7dffac89ce1ad5b85a289c1c8f6a474e7e3a9362/resource.tar.gz#test.test_in-small_in_YQL-19183-ansi-Analyze_/plan.txt"
1153+
}
1154+
],
1155+
"test.test[in-small_in_YQL-19183-ansi-Debug]": [
1156+
{
1157+
"checksum": "b5a738dd6b40400b6bf4fff3c1e67b6c",
1158+
"size": 421,
1159+
"uri": "https://{canondata_backend}/1903885/7dffac89ce1ad5b85a289c1c8f6a474e7e3a9362/resource.tar.gz#test.test_in-small_in_YQL-19183-ansi-Debug_/opt.yql_patched"
1160+
}
1161+
],
1162+
"test.test[in-small_in_YQL-19183-ansi-Plan]": [
1163+
{
1164+
"checksum": "b4dd508a329723c74293d80f0278c705",
1165+
"size": 505,
1166+
"uri": "https://{canondata_backend}/1903885/7dffac89ce1ad5b85a289c1c8f6a474e7e3a9362/resource.tar.gz#test.test_in-small_in_YQL-19183-ansi-Plan_/plan.txt"
1167+
}
1168+
],
1169+
"test.test[in-small_in_YQL-19183-ansi-Results]": [],
11481170
"test.test[insert-append_missing_null-default.txt-Analyze]": [
11491171
{
11501172
"checksum": "b591e16de5b80ad56b57d82981336873",

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,6 +1210,28 @@
12101210
}
12111211
],
12121212
"test.test[in-in_with_tuple-default.txt-Results]": [],
1213+
"test.test[in-large_in_YQL-19183-ansi-Analyze]": [
1214+
{
1215+
"checksum": "b4dd508a329723c74293d80f0278c705",
1216+
"size": 505,
1217+
"uri": "https://{canondata_backend}/1881367/e98bbd650c45a3f4f6bc628cf8be62baa88c6183/resource.tar.gz#test.test_in-large_in_YQL-19183-ansi-Analyze_/plan.txt"
1218+
}
1219+
],
1220+
"test.test[in-large_in_YQL-19183-ansi-Debug]": [
1221+
{
1222+
"checksum": "42f246b79cfdf472d0e1b1b2a2906325",
1223+
"size": 541,
1224+
"uri": "https://{canondata_backend}/1881367/e98bbd650c45a3f4f6bc628cf8be62baa88c6183/resource.tar.gz#test.test_in-large_in_YQL-19183-ansi-Debug_/opt.yql_patched"
1225+
}
1226+
],
1227+
"test.test[in-large_in_YQL-19183-ansi-Plan]": [
1228+
{
1229+
"checksum": "b4dd508a329723c74293d80f0278c705",
1230+
"size": 505,
1231+
"uri": "https://{canondata_backend}/1881367/e98bbd650c45a3f4f6bc628cf8be62baa88c6183/resource.tar.gz#test.test_in-large_in_YQL-19183-ansi-Plan_/plan.txt"
1232+
}
1233+
],
1234+
"test.test[in-large_in_YQL-19183-ansi-Results]": [],
12131235
"test.test[insert-after_group_by-default.txt-Analyze]": [
12141236
{
12151237
"checksum": "96a53f8e3a065123498005c527be5309",

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,6 +1082,28 @@
10821082
}
10831083
],
10841084
"test.test[in-in_immediate_subquery-default.txt-Results]": [],
1085+
"test.test[in-small_in_YQL-19183--Analyze]": [
1086+
{
1087+
"checksum": "b4dd508a329723c74293d80f0278c705",
1088+
"size": 505,
1089+
"uri": "https://{canondata_backend}/1903885/36b7eb9d918e0ee90b18e7dfac3ec36336c26b5e/resource.tar.gz#test.test_in-small_in_YQL-19183--Analyze_/plan.txt"
1090+
}
1091+
],
1092+
"test.test[in-small_in_YQL-19183--Debug]": [
1093+
{
1094+
"checksum": "fe842b7683cf32fe6a3cad94653770f3",
1095+
"size": 474,
1096+
"uri": "https://{canondata_backend}/1903885/36b7eb9d918e0ee90b18e7dfac3ec36336c26b5e/resource.tar.gz#test.test_in-small_in_YQL-19183--Debug_/opt.yql_patched"
1097+
}
1098+
],
1099+
"test.test[in-small_in_YQL-19183--Plan]": [
1100+
{
1101+
"checksum": "b4dd508a329723c74293d80f0278c705",
1102+
"size": 505,
1103+
"uri": "https://{canondata_backend}/1903885/36b7eb9d918e0ee90b18e7dfac3ec36336c26b5e/resource.tar.gz#test.test_in-small_in_YQL-19183--Plan_/plan.txt"
1104+
}
1105+
],
1106+
"test.test[in-small_in_YQL-19183--Results]": [],
10851107
"test.test[insert-keepmeta-view_fail-Results]": [
10861108
{
10871109
"uri": "file://test.test_insert-keepmeta-view_fail-Results_/extracted"

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,6 +1245,20 @@
12451245
"uri": "https://{canondata_backend}/1130705/2dbc543e7e2156e1086b7eff9aaab72ade9022c4/resource.tar.gz#test.test_in-in_with_list_dict-default.txt-Plan_/plan.txt"
12461246
}
12471247
],
1248+
"test.test[in-large_in_YQL-19183--Debug]": [
1249+
{
1250+
"checksum": "9cf3548e7c262c9a9f1d9fd7924cfb92",
1251+
"size": 593,
1252+
"uri": "https://{canondata_backend}/1881367/943a50aaa7841517b3581cb3efc1c4693dfe6c56/resource.tar.gz#test.test_in-large_in_YQL-19183--Debug_/opt.yql_patched"
1253+
}
1254+
],
1255+
"test.test[in-large_in_YQL-19183--Plan]": [
1256+
{
1257+
"checksum": "b4dd508a329723c74293d80f0278c705",
1258+
"size": 505,
1259+
"uri": "https://{canondata_backend}/1881367/943a50aaa7841517b3581cb3efc1c4693dfe6c56/resource.tar.gz#test.test_in-large_in_YQL-19183--Plan_/plan.txt"
1260+
}
1261+
],
12481262
"test.test[insert-merge_publish--Debug]": [
12491263
{
12501264
"checksum": "64ebc7e15210475430faab9682fa1c41",

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,6 +1231,20 @@
12311231
"uri": "https://{canondata_backend}/1937001/601e94a23ec26980c16840b1ec99d6084037513f/resource.tar.gz#test.test_in-in_with_tuple-default.txt-Plan_/plan.txt"
12321232
}
12331233
],
1234+
"test.test[in-small_in_YQL-19183-ansi-Debug]": [
1235+
{
1236+
"checksum": "8e81ddca5e2463a69f21ae340d099230",
1237+
"size": 420,
1238+
"uri": "https://{canondata_backend}/1903885/c99336662dd85cc4dbf2e30aa3726a822664376a/resource.tar.gz#test.test_in-small_in_YQL-19183-ansi-Debug_/opt.yql_patched"
1239+
}
1240+
],
1241+
"test.test[in-small_in_YQL-19183-ansi-Plan]": [
1242+
{
1243+
"checksum": "b4dd508a329723c74293d80f0278c705",
1244+
"size": 505,
1245+
"uri": "https://{canondata_backend}/1903885/c99336662dd85cc4dbf2e30aa3726a822664376a/resource.tar.gz#test.test_in-small_in_YQL-19183-ansi-Plan_/plan.txt"
1246+
}
1247+
],
12341248
"test.test[in-yql-14677-default.txt-Debug]": [
12351249
{
12361250
"checksum": "e73c4edd15205a000c7c8527253ea6ad",

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,6 +1343,20 @@
13431343
"uri": "https://{canondata_backend}/1923547/94f377eaa1d93890e1345ac4940cc6fa07bddd4f/resource.tar.gz#test.test_in-in_noansi-default.txt-Plan_/plan.txt"
13441344
}
13451345
],
1346+
"test.test[in-small_in_YQL-19183--Debug]": [
1347+
{
1348+
"checksum": "90279bdf10e6c5fe33509b2eaf53540c",
1349+
"size": 473,
1350+
"uri": "https://{canondata_backend}/1903885/f9d45bc250f07f42a2353007c7f2648896a84384/resource.tar.gz#test.test_in-small_in_YQL-19183--Debug_/opt.yql_patched"
1351+
}
1352+
],
1353+
"test.test[in-small_in_YQL-19183--Plan]": [
1354+
{
1355+
"checksum": "b4dd508a329723c74293d80f0278c705",
1356+
"size": 505,
1357+
"uri": "https://{canondata_backend}/1903885/f9d45bc250f07f42a2353007c7f2648896a84384/resource.tar.gz#test.test_in-small_in_YQL-19183--Plan_/plan.txt"
1358+
}
1359+
],
13461360
"test.test[insert-append_missing_null-default.txt-Debug]": [
13471361
{
13481362
"checksum": "22d432d1951015310a3bbf0503113106",

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,6 +1091,20 @@
10911091
"uri": "https://{canondata_backend}/1889210/796baf28896eb5aaad8828a0b6000e7d17563447/resource.tar.gz#test.test_in-in_with_nulls_and_optionals-default.txt-Plan_/plan.txt"
10921092
}
10931093
],
1094+
"test.test[in-large_in_YQL-19183-ansi-Debug]": [
1095+
{
1096+
"checksum": "e3ea538b42e9ed7b18bd731120e7fd16",
1097+
"size": 540,
1098+
"uri": "https://{canondata_backend}/1903885/4a384ef3fd6e8cf628d678d9322eef7d381022a7/resource.tar.gz#test.test_in-large_in_YQL-19183-ansi-Debug_/opt.yql_patched"
1099+
}
1100+
],
1101+
"test.test[in-large_in_YQL-19183-ansi-Plan]": [
1102+
{
1103+
"checksum": "b4dd508a329723c74293d80f0278c705",
1104+
"size": 505,
1105+
"uri": "https://{canondata_backend}/1903885/4a384ef3fd6e8cf628d678d9322eef7d381022a7/resource.tar.gz#test.test_in-large_in_YQL-19183-ansi-Plan_/plan.txt"
1106+
}
1107+
],
10941108
"test.test[insert-append_after_replace-default.txt-Debug]": [
10951109
{
10961110
"checksum": "e053a922f685828f341eb8d8e7a948d4",

0 commit comments

Comments
 (0)