summaryrefslogtreecommitdiffstats
path: root/yql/essentials/minikql/comp_nodes/ut/mkql_list_join_ut.cpp
blob: d008936373e4eebb4add7b19fc5a3a7fd5120f3a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
#include "mkql_computation_node_ut.h"
#include "mkql_program_builder_test_utils.h"

#include <yql/essentials/minikql/computation/mkql_computation_node_impl.h>
#include <yql/essentials/minikql/mkql_node_cast.h>
#include <yql/essentials/minikql/mkql_runtime_version.h>
#include <yql/essentials/minikql/udf_value_test_support/udf_value_comparator_utils.h>
#include <yql/essentials/parser/pg_wrapper/interface/comp_factory.h>

namespace NKikimr::NMiniKQL {

#if MKQL_RUNTIME_VERSION >= 78U

namespace {

using TColumnsVec = TVector<std::pair<const ui32, const ui32>>;

// Builds a ListJoinCore that zips left.a with right.b for the given input rows.
// All=false: Zip  — returns a stream of (i32, i32), length = min(|left|, |right|).
// All=true:  ZipAll — returns a stream of (Maybe<i32>, Maybe<i32>), length = max(|left|, |right|).
using TDefaultInRow = NTest::TStructType<NTest::TStructMember<"left.a", TMaybe<i32>>,
                                         NTest::TStructMember<"right.b", TMaybe<i32>>,
                                         NTest::TStructMember<"key", TMaybe<i32>>,
                                         NTest::TStructMember<"_yql_table_index", i32>>;
template <bool Optional>
using TDefaultOutRow = std::conditional_t<Optional, std::tuple<TMaybe<i32>, TMaybe<i32>>, std::tuple<i32, i32>>;

std::tuple<TStructType*, TStructType*, TStructType*> BuildDefaultStructTypes(TProgramBuilder& pb, const TRuntimeNode& input, TType* payloadType) {
    const auto inputRowType = AS_TYPE(TStructType, AS_TYPE(TListType, input.GetStaticType())->GetItemType());
    const auto leftArgType = AS_TYPE(TStructType, pb.NewStructType({{"a", payloadType}}));
    const auto rightArgType = AS_TYPE(TStructType, pb.NewStructType({{"b", payloadType}}));
    return {inputRowType, leftArgType, rightArgType};
}

std::tuple<TColumnsVec, TColumnsVec, TColumnsVec> BuildDefaultColumnMaps(const TStructType* inputRowType, const TStructType* leftArgType, const TStructType* rightArgType) {
    const TColumnsVec keyColumns = {{inputRowType->GetMemberIndex("key"), 0U}};
    const TColumnsVec leftColumns = {{inputRowType->GetMemberIndex("left.a"), leftArgType->GetMemberIndex("a")}};
    const TColumnsVec rightColumns = {{inputRowType->GetMemberIndex("right.b"), rightArgType->GetMemberIndex("b")}};
    return {keyColumns, leftColumns, rightColumns};
}

template <bool All>
TRuntimeNode BuildZipJoin(TProgramBuilder& pb, TVector<TDefaultInRow>&& rows) {
    const auto list = NTest::ConvertValueToLiteralNode(pb, rows);

    const auto int32Type = pb.NewDataType(NUdf::TDataType<i32>::Id);
    const auto elemType = All ? pb.NewOptionalType(int32Type) : int32Type;
    const auto outputRowType = pb.NewTupleType({elemType, elemType});
    const auto [inputRowType, leftArgType, rightArgType] = BuildDefaultStructTypes(pb, list, int32Type);
    const auto [keyColumns, leftColumns, rightColumns] = BuildDefaultColumnMaps(inputRowType, leftArgType, rightArgType);
    const auto listJoinType = pb.NewStreamType(outputRowType);

    return pb.ListJoinCore(pb.Iterator(list, {}),
                           int32Type, keyColumns,
                           leftColumns, rightColumns,
                           leftArgType, [&pb](const TRuntimeNode leftArg) { return pb.Member(leftArg, "a"); },
                           rightArgType, [&pb](const TRuntimeNode rightArg) { return pb.Member(rightArg, "b"); },
                           listJoinType,
                           [&pb](const TRuntimeNode, const TRuntimeNode leftList, const TRuntimeNode rightList) {
                               TRuntimeNode root;
                               if constexpr (All) {
                                   root = pb.ZipAll({leftList, rightList});
                               } else {
                                   root = pb.Zip({leftList, rightList});
                               }
                               return pb.Iterator(root, {}); });
}

class TUnreachableWrapper: public TMutableComputationNode<TUnreachableWrapper> {
    using TBaseComputation = TMutableComputationNode<TUnreachableWrapper>;

public:
    explicit TUnreachableWrapper(TComputationMutables& mutables)
        : TBaseComputation(mutables)
    {
    }

    NUdf::TUnboxedValuePod DoCalculate(TComputationContext&) const {
        THROW yexception() << "Unreachable";
    }

private:
    void RegisterDependencies() const final {
    }
};

IComputationNode* WrapUnreachable(TCallable&, const TComputationNodeFactoryContext& ctx) {
    return new TUnreachableWrapper(ctx.Mutables);
}

TComputationNodeFactory GetUnreachableFactory() {
    return [](TCallable& callable, const TComputationNodeFactoryContext& ctx) -> IComputationNode* {
        if (callable.GetType()->GetName() == "Unreachable") {
            return WrapUnreachable(callable, ctx);
        }
        return GetBuiltinFactory()(callable, ctx);
    };
}

TRuntimeNode Unreachable(TProgramBuilder& pb) {
    TCallableBuilder callableBuilder(pb.GetTypeEnvironment(), "Unreachable", pb.NewVoidType());
    return TRuntimeNode(callableBuilder.Build(), false);
}

TComputationNodeFactory GetFactoryWithPg() {
    return [](TCallable& callable, const TComputationNodeFactoryContext& ctx) -> IComputationNode* {
        if (auto* pgCallable = NYql::GetPgFactory()(callable, ctx)) {
            return pgCallable;
        }
        return GetBuiltinFactory()(callable, ctx);
    };
}

} // namespace

Y_UNIT_TEST_SUITE(TMiniKQLListJoinCoreTest) {

Y_UNIT_TEST_QUAD(EmptyLeftList, LLVM, All) {
    // Only right-side rows. Zip: empty output. ZipAll: left slots become Nothing.
    TSetup<LLVM> setup;
    TProgramBuilder& pb = *setup.PgmBuilder;

    const auto listJoin = BuildZipJoin<All>(pb, TVector<TDefaultInRow>{
                                                    {{{TMaybe<i32>{}}, {i32(3)}, {i32(9)}, {1}}},
                                                    {{{TMaybe<i32>{}}, {i32(4)}, {i32(9)}, {1}}},
                                                });

    const auto graph = setup.BuildGraph(listJoin);

    using TOutRow = TDefaultOutRow<All>;
    if constexpr (All) {
        const TVector<TOutRow> expected{
            {{TMaybe<i32>{}}, {i32(3)}},
            {{TMaybe<i32>{}}, {i32(4)}},
        };
        AssertUnboxedValueElementEqual(graph->GetValue(), NYql::NUdf::TUnboxedValueComparatorStreamView<TOutRow>(expected));
    } else {
        const TVector<TOutRow> expected{};
        AssertUnboxedValueElementEqual(graph->GetValue(), NYql::NUdf::TUnboxedValueComparatorStreamView<TOutRow>(expected));
    }
}

Y_UNIT_TEST_QUAD(EmptyRightList, LLVM, All) {
    // Only left-side rows. Zip: empty output. ZipAll: right slots become Nothing.
    TSetup<LLVM> setup;
    TProgramBuilder& pb = *setup.PgmBuilder;

    const auto listJoin = BuildZipJoin<All>(pb, TVector<TDefaultInRow>{
                                                    {{{i32(1)}, {TMaybe<i32>{}}, {i32(9)}, {0}}},
                                                    {{{i32(2)}, {TMaybe<i32>{}}, {i32(9)}, {0}}},
                                                });

    const auto graph = setup.BuildGraph(listJoin);

    using TOutRow = TDefaultOutRow<All>;
    if constexpr (All) {
        const TVector<TOutRow> expected{
            {{i32(1)}, {TMaybe<i32>{}}},
            {{i32(2)}, {TMaybe<i32>{}}},
        };
        AssertUnboxedValueElementEqual(graph->GetValue(), NYql::NUdf::TUnboxedValueComparatorStreamView<TOutRow>(expected));
    } else {
        const TVector<TOutRow> expected{};
        AssertUnboxedValueElementEqual(graph->GetValue(), NYql::NUdf::TUnboxedValueComparatorStreamView<TOutRow>(expected));
    }
}

Y_UNIT_TEST_QUAD(EmptyStream, LLVM, All) {
    // No rows at all. Both Zip and ZipAll of two empty lists produce no output.
    TSetup<LLVM> setup;
    TProgramBuilder& pb = *setup.PgmBuilder;

    const auto listJoin = BuildZipJoin<All>(pb, TVector<TDefaultInRow>{});

    const auto graph = setup.BuildGraph(listJoin);

    using TOutRow = TDefaultOutRow<All>;
    const TVector<TOutRow> expected{};
    AssertUnboxedValueElementEqual(graph->GetValue(), NYql::NUdf::TUnboxedValueComparatorStreamView<TOutRow>(expected));
}

Y_UNIT_TEST_LLVM(InterleavedRows) {
    // Left and right rows appear alternating in the stream. Accumulation must be
    // order-independent: left=[1,2], right=[3,4] regardless of arrival order.
    TSetup<LLVM> setup;
    TProgramBuilder& pb = *setup.PgmBuilder;

    const auto listJoin = BuildZipJoin<false>(pb, TVector<TDefaultInRow>{
                                                      {{{i32(1)}, {TMaybe<i32>{}}, {i32(9)}, {0}}}, // left
                                                      {{{TMaybe<i32>{}}, {i32(3)}, {i32(9)}, {1}}}, // right
                                                      {{{i32(2)}, {TMaybe<i32>{}}, {i32(9)}, {0}}}, // left
                                                      {{{TMaybe<i32>{}}, {i32(4)}, {i32(9)}, {1}}}, // right
                                                  });

    const auto graph = setup.BuildGraph(listJoin);

    using TOutRow = TDefaultOutRow<false>;
    const TVector<TOutRow> expected{{1, 3}, {2, 4}};
    AssertUnboxedValueElementEqual(graph->GetValue(), NYql::NUdf::TUnboxedValueComparatorStreamView<TOutRow>(expected));
}

Y_UNIT_TEST(InvalidTableIndex) {
    // A row with _yql_table_index=2 must throw during Fetch.
    TSetup<false> setup;
    TProgramBuilder& pb = *setup.PgmBuilder;

    const auto listJoin = BuildZipJoin<false>(pb, TVector<TDefaultInRow>{
                                                      {{{i32(1)}, {TMaybe<i32>{}}, {i32(9)}, {2}}},
                                                  });

    const auto graph = setup.BuildGraph(listJoin);
    auto stream = graph->GetValue();
    NUdf::TUnboxedValue result;
    UNIT_ASSERT_EXCEPTION_CONTAINS(stream.Fetch(result), yexception, "Bad table index: 2");
}

Y_UNIT_TEST(InvalidColumnsMapping) {
    // Mismap inputRowType members types with leftArgType and rightArgType
    // member types to check EnrichNeedUnwrap sentinel branch.
    TSetup<false> setup;
    TProgramBuilder& pb = *setup.PgmBuilder;

    const auto list = NTest::ConvertValueToLiteralNode(pb, TVector<TDefaultInRow>{
                                                               {{{i32(23)}, {TMaybe<i32>{}}, {9}, {0}}},
                                                           });

    const auto int32Type = pb.NewDataType(NUdf::TDataType<i32>::Id);
    const auto uint32Type = pb.NewDataType(NUdf::TDataType<ui32>::Id);
    const auto [inputRowType, leftArgType, rightArgType] = BuildDefaultStructTypes(pb, list, uint32Type);
    const auto [keyColumns, leftColumns, rightColumns] = BuildDefaultColumnMaps(inputRowType, leftArgType, rightArgType);
    const auto listJoinType = pb.NewStreamType(pb.NewTupleType({uint32Type, uint32Type}));

    const auto listJoin = pb.ListJoinCore(pb.Iterator(list, {}),
                                          int32Type, keyColumns,
                                          leftColumns, rightColumns,
                                          leftArgType, [&pb](const TRuntimeNode arg) { return pb.Member(arg, "a"); },
                                          rightArgType, [&pb](const TRuntimeNode arg) { return pb.Member(arg, "b"); },
                                          listJoinType,
                                          [&pb](const TRuntimeNode, const TRuntimeNode leftList, const TRuntimeNode rightList) {
                                              const auto root = pb.Zip({leftList, rightList});
                                              return pb.Iterator(root, {}); });

    UNIT_ASSERT_EXCEPTION_CONTAINS(setup.BuildGraph(listJoin), yexception, "Bad column mapping:");
}

Y_UNIT_TEST_LLVM(ColumnReordering) {
    // Both left and right output columns are mapped in reverse order relative to input.
    // Left:  input[a=9,  b=24] -> output {x=b=24, y=a=9} (a->y, b->x).
    // Right: input[c=42, d=73] -> output {p=d=73, q=c=42} (c->q, d->p).
    // Verifies MakeListItem uses itemsPtr[outColumn], not itemsPtr[inColumn].
    TSetup<LLVM> setup;
    TProgramBuilder& pb = *setup.PgmBuilder;

    using TInRow = NTest::TStructType<NTest::TStructMember<"left.a", TMaybe<i32>>,
                                      NTest::TStructMember<"left.b", TMaybe<i32>>,
                                      NTest::TStructMember<"right.c", TMaybe<i32>>,
                                      NTest::TStructMember<"right.d", TMaybe<i32>>,
                                      NTest::TStructMember<"key", i32>,
                                      NTest::TStructMember<"_yql_table_index", i32>>;
    const auto list = NTest::ConvertValueToLiteralNode(pb, TVector<TInRow>{
                                                               {{{i32(9)}, {i32(24)}, {TMaybe<i32>{}}, {TMaybe<i32>{}}, {0}, {0}}},  // left:  a=9,  b=24
                                                               {{{TMaybe<i32>{}}, {TMaybe<i32>{}}, {i32(42)}, {i32(73)}, {0}, {1}}}, // right: c=42, d=73
                                                           });

    const auto int32Type = pb.NewDataType(NUdf::TDataType<i32>::Id);
    // Left output struct: {x (idx=0), y (idx=1)}; a->y (outCol=1), b->x (outCol=0).
    const auto leftItemType = AS_TYPE(TStructType, pb.NewStructType({{"x", int32Type}, {"y", int32Type}}));
    // Right output struct: {p (idx=0), q (idx=1)}; c->q (outCol=1), d->p (outCol=0).
    const auto rightItemType = AS_TYPE(TStructType, pb.NewStructType({{"p", int32Type}, {"q", int32Type}}));
    const auto inputRowType = AS_TYPE(TStructType, AS_TYPE(TListType, list.GetStaticType())->GetItemType());

    const TColumnsVec keyColumns = {{inputRowType->GetMemberIndex("key"), 0U}};
    const TColumnsVec leftColumns = {
        {inputRowType->GetMemberIndex("left.a"), leftItemType->GetMemberIndex("y")},
        {inputRowType->GetMemberIndex("left.b"), leftItemType->GetMemberIndex("x")},
    };
    const TColumnsVec rightColumns = {
        {inputRowType->GetMemberIndex("right.c"), rightItemType->GetMemberIndex("q")},
        {inputRowType->GetMemberIndex("right.d"), rightItemType->GetMemberIndex("p")},
    };
    // Output: (left.x, left.y, right.p, right.q) = (24, 9, 73, 42).
    const auto listJoinType = pb.NewStreamType(pb.NewTupleType({int32Type, int32Type, int32Type, int32Type}));

    const auto listJoin = pb.ListJoinCore(pb.Iterator(list, {}),
                                          int32Type, keyColumns,
                                          leftColumns, rightColumns,
                                          leftItemType, [&pb](const TRuntimeNode arg) { return pb.NewTuple({pb.Member(arg, "x"), pb.Member(arg, "y")}); },
                                          rightItemType, [&pb](const TRuntimeNode arg) { return pb.NewTuple({pb.Member(arg, "p"), pb.Member(arg, "q")}); },
                                          listJoinType,
                                          [&pb](const TRuntimeNode, const TRuntimeNode leftList, const TRuntimeNode rightList) {
                                              const auto root = pb.Map(pb.Zip({leftList, rightList}), [&pb](const auto pair) {
                                                 const auto l = pb.Nth(pair, 0);
                                                 const auto r = pb.Nth(pair, 1);
                                                 return pb.NewTuple({pb.Nth(l, 0), pb.Nth(l, 1), pb.Nth(r, 0), pb.Nth(r, 1)});
                                              });
                                              return pb.Iterator(root , {}); });

    const auto graph = setup.BuildGraph(listJoin);

    using TOutRow = std::tuple<i32, i32, i32, i32>;
    const TVector<TOutRow> expected{{24, 9, 73, 42}};
    AssertUnboxedValueElementEqual(graph->GetValue(), NYql::NUdf::TUnboxedValueComparatorStreamView<TOutRow>(expected));
}

Y_UNIT_TEST_LLVM(MultipleColumnsPerSide) {
    // Left side has columns {a, c}, right side has columns {a, c} (same field name, same type).
    // Verifies that all columns from both sides are correctly extracted and placed.
    TSetup<LLVM> setup;
    TProgramBuilder& pb = *setup.PgmBuilder;

    using TInRow = NTest::TStructType<NTest::TStructMember<"left.a", TMaybe<i32>>,
                                      NTest::TStructMember<"left.c", TMaybe<i32>>,
                                      NTest::TStructMember<"right.a", TMaybe<i32>>,
                                      NTest::TStructMember<"right.c", TMaybe<i32>>,
                                      NTest::TStructMember<"key", i32>,
                                      NTest::TStructMember<"_yql_table_index", i32>>;
    const auto list = NTest::ConvertValueToLiteralNode(pb, TVector<TInRow>{
                                                               {{{1}, {3}, {TMaybe<i32>{}}, {TMaybe<i32>{}}, {0}, {0}}}, // left:  a=1, c=3
                                                               {{{TMaybe<i32>{}}, {TMaybe<i32>{}}, {2}, {4}, {0}, {1}}}, // right: a=2, c=4
                                                           });

    const auto int32Type = pb.NewDataType(NUdf::TDataType<i32>::Id);
    const auto leftArgType = AS_TYPE(TStructType, pb.NewStructType({{"la", int32Type}, {"lc", int32Type}}));
    const auto rightArgType = AS_TYPE(TStructType, pb.NewStructType({{"ra", int32Type}, {"rc", int32Type}}));
    const auto inputRowType = AS_TYPE(TStructType, AS_TYPE(TListType, list.GetStaticType())->GetItemType());

    const TColumnsVec keyColumns = {{inputRowType->GetMemberIndex("key"), 0U}};
    const TColumnsVec leftColumns = {
        {inputRowType->GetMemberIndex("left.a"), leftArgType->GetMemberIndex("la")},
        {inputRowType->GetMemberIndex("left.c"), leftArgType->GetMemberIndex("lc")},
    };
    const TColumnsVec rightColumns = {
        {inputRowType->GetMemberIndex("right.a"), rightArgType->GetMemberIndex("ra")},
        {inputRowType->GetMemberIndex("right.c"), rightArgType->GetMemberIndex("rc")},
    };
    const auto listJoinType = pb.NewStreamType(pb.NewTupleType({int32Type, int32Type}));

    const auto listJoin = pb.ListJoinCore(pb.Iterator(list, {}),
                                          int32Type, keyColumns,
                                          leftColumns, rightColumns,
                                          leftArgType, [&pb](const TRuntimeNode arg) { return pb.NewTuple({pb.Member(arg, "la"), pb.Member(arg, "lc")}); },
                                          rightArgType, [&pb](const TRuntimeNode arg) { return pb.NewTuple({pb.Member(arg, "ra"), pb.Member(arg, "rc")}); },
                                          listJoinType,
                                          [&pb](const TRuntimeNode, const TRuntimeNode leftList, const TRuntimeNode rightList) {
                                              const auto root = pb.Extend({leftList, rightList});
                                              return pb.Iterator(root, {}); });

    const auto graph = setup.BuildGraph(listJoin);

    using TOutRow = TDefaultOutRow<false>;
    const TVector<TOutRow> expected{{1, 3}, {2, 4}};
    AssertUnboxedValueElementEqual(graph->GetValue(), NYql::NUdf::TUnboxedValueComparatorStreamView<TOutRow>(expected));
}

Y_UNIT_TEST_LLVM(KeyUsedInLambda) {
    // The key value is read inside the lambda and emitted in every output row.
    // Verifies that KeyArg_ receives the correct value from InitializeKey.
    TSetup<LLVM> setup;
    TProgramBuilder& pb = *setup.PgmBuilder;

    const auto list = NTest::ConvertValueToLiteralNode(pb, TVector<TDefaultInRow>{
                                                               {{{i32(1)}, {TMaybe<i32>{}}, {9}, {0}}},
                                                               {{{i32(2)}, {TMaybe<i32>{}}, {9}, {0}}},
                                                               {{{TMaybe<i32>{}}, {i32(3)}, {9}, {1}}},
                                                               {{{TMaybe<i32>{}}, {i32(4)}, {9}, {1}}},
                                                           });

    const auto int32Type = pb.NewDataType(NUdf::TDataType<i32>::Id);
    const auto [inputRowType, leftArgType, rightArgType] = BuildDefaultStructTypes(pb, list, int32Type);
    const auto [keyColumns, leftColumns, rightColumns] = BuildDefaultColumnMaps(inputRowType, leftArgType, rightArgType);
    // Output: (key, a, b).
    const auto listJoinType = pb.NewStreamType(pb.NewTupleType({int32Type, int32Type, int32Type}));

    const auto listJoin = pb.ListJoinCore(pb.Iterator(list, {}),
                                          int32Type, keyColumns,
                                          leftColumns, rightColumns,
                                          leftArgType, [&pb](const TRuntimeNode arg) { return pb.Member(arg, "a"); },
                                          rightArgType, [&pb](const TRuntimeNode arg) { return pb.Member(arg, "b"); },
                                          listJoinType,
                                          [&pb](const TRuntimeNode key, const TRuntimeNode leftList, const TRuntimeNode rightList) {
                                              const auto root = pb.Map(pb.Zip({leftList, rightList}), [&pb, key](const auto pair) {
                                                  return pb.NewTuple({key, pb.Nth(pair, 0), pb.Nth(pair, 1)});
                                              });
                                              return pb.Iterator(root, {}); });

    const auto graph = setup.BuildGraph(listJoin);

    using TOutRow = std::tuple<i32, i32, i32>;
    const TVector<TOutRow> expected{{9, 1, 3}, {9, 2, 4}};
    AssertUnboxedValueElementEqual(graph->GetValue(), NYql::NUdf::TUnboxedValueComparatorStreamView<TOutRow>(expected));
}

Y_UNIT_TEST_LLVM(MultiColumnKey) {
    // Key is composed of two columns (key1, key2). Exercises the InitializeKey
    // branch that builds a tuple array instead of returning a scalar directly.
    TSetup<LLVM> setup;
    TProgramBuilder& pb = *setup.PgmBuilder;

    using TInRow = NTest::TStructType<NTest::TStructMember<"left.a", TMaybe<i32>>,
                                      NTest::TStructMember<"right.b", TMaybe<i32>>,
                                      NTest::TStructMember<"key1", i32>,
                                      NTest::TStructMember<"key2", i32>,
                                      NTest::TStructMember<"_yql_table_index", i32>>;
    const auto list = NTest::ConvertValueToLiteralNode(pb, TVector<TInRow>{
                                                               {{{i32(1)}, {TMaybe<i32>{}}, {5}, {7}, {0}}},
                                                               {{{i32(2)}, {TMaybe<i32>{}}, {5}, {7}, {0}}},
                                                               {{{TMaybe<i32>{}}, {i32(3)}, {5}, {7}, {1}}},
                                                               {{{TMaybe<i32>{}}, {i32(4)}, {5}, {7}, {1}}},
                                                           });

    const auto int32Type = pb.NewDataType(NUdf::TDataType<i32>::Id);
    const auto [inputRowType, leftArgType, rightArgType] = BuildDefaultStructTypes(pb, list, int32Type);
    // Two-column key maps to a tuple (key1, key2).
    const auto keyType = AS_TYPE(TTupleType, pb.NewTupleType({int32Type, int32Type}));
    const TColumnsVec keyColumns = {
        {inputRowType->GetMemberIndex("key1"), 0},
        {inputRowType->GetMemberIndex("key2"), 1},
    };
    const TColumnsVec leftColumns = {{inputRowType->GetMemberIndex("left.a"), leftArgType->GetMemberIndex("a")}};
    const TColumnsVec rightColumns = {{inputRowType->GetMemberIndex("right.b"), rightArgType->GetMemberIndex("b")}};
    // Output: (key1, key2, a, b).
    const auto listJoinType = pb.NewStreamType(pb.NewTupleType({int32Type, int32Type, int32Type, int32Type}));

    const auto listJoin = pb.ListJoinCore(pb.Iterator(list, {}),
                                          keyType, keyColumns,
                                          leftColumns, rightColumns,
                                          leftArgType, [&pb](const TRuntimeNode arg) { return pb.Member(arg, "a"); },
                                          rightArgType, [&pb](const TRuntimeNode arg) { return pb.Member(arg, "b"); },
                                          listJoinType,
                                          [&pb](const TRuntimeNode key, const TRuntimeNode leftList, const TRuntimeNode rightList) {
                                              const auto root = pb.Map(pb.Zip({leftList, rightList}), [&pb, key](const auto pair) {
                                                  return pb.NewTuple({pb.Nth(key, 0), pb.Nth(key, 1), pb.Nth(pair, 0), pb.Nth(pair, 1)});
                                              });
                                              return pb.Iterator(root, {}); });

    const auto graph = setup.BuildGraph(listJoin);

    using TOutRow = std::tuple<i32, i32, i32, i32>;
    const TVector<TOutRow> expected{{5, 7, 1, 3}, {5, 7, 2, 4}};
    AssertUnboxedValueElementEqual(graph->GetValue(), NYql::NUdf::TUnboxedValueComparatorStreamView<TOutRow>(expected));
}

Y_UNIT_TEST_LLVM(CrossProductJoin) {
    // Lambda uses FlatMap to produce the full cross product of left x right rows.
    // 2 left rows x 3 right rows = 6 output rows.
    TSetup<LLVM> setup;
    TProgramBuilder& pb = *setup.PgmBuilder;

    const auto list = NTest::ConvertValueToLiteralNode(pb, TVector<TDefaultInRow>{
                                                               {{{i32(1)}, {TMaybe<i32>{}}, {0}, {0}}},
                                                               {{{i32(2)}, {TMaybe<i32>{}}, {0}, {0}}},
                                                               {{{TMaybe<i32>{}}, {i32(3)}, {0}, {1}}},
                                                               {{{TMaybe<i32>{}}, {i32(4)}, {0}, {1}}},
                                                               {{{TMaybe<i32>{}}, {i32(5)}, {0}, {1}}},
                                                           });

    const auto int32Type = pb.NewDataType(NUdf::TDataType<i32>::Id);
    const auto [inputRowType, leftArgType, rightArgType] = BuildDefaultStructTypes(pb, list, int32Type);
    const auto [keyColumns, leftColumns, rightColumns] = BuildDefaultColumnMaps(inputRowType, leftArgType, rightArgType);
    const auto listJoinType = pb.NewStreamType(pb.NewTupleType({int32Type, int32Type}));

    const auto listJoin = pb.ListJoinCore(pb.Iterator(list, {}),
                                          int32Type, keyColumns,
                                          leftColumns, rightColumns,
                                          leftArgType, [&pb](const TRuntimeNode arg) { return pb.Member(arg, "a"); },
                                          rightArgType, [&pb](const TRuntimeNode arg) { return pb.Member(arg, "b"); },
                                          listJoinType,
                                          [&pb](const TRuntimeNode, const TRuntimeNode leftList, const TRuntimeNode rightList) {
                                              const auto root = pb.FlatMap(leftList, [&pb, rightList](const TRuntimeNode a) {
                                                  return pb.Map(rightList, [&pb, a](const TRuntimeNode b) {
                                                      return pb.NewTuple({a, b});
                                                  });
                                              });
                                              return pb.Iterator(root, {}); });

    const auto graph = setup.BuildGraph(listJoin);

    using TOutRow = TDefaultOutRow<false>;
    const TVector<TOutRow> expected{{1, 3}, {1, 4}, {1, 5}, {2, 3}, {2, 4}, {2, 5}};
    AssertUnboxedValueElementEqual(graph->GetValue(), NYql::NUdf::TUnboxedValueComparatorStreamView<TOutRow>(expected));
}

Y_UNIT_TEST_LLVM(ArgmapTransform) {
    // Argmap lambdas apply arithmetic transforms: left increments (a+1), right doubles (b*2).
    // Verifies MakeListItem stores the premap output, not the raw premap arg.
    TSetup<LLVM> setup;
    TProgramBuilder& pb = *setup.PgmBuilder;

    const auto list = NTest::ConvertValueToLiteralNode(pb, TVector<TDefaultInRow>{
                                                               {{{42}, {TMaybe<i32>{}}, {9}, {0}}}, // left:  a=42 -> premap: 42+1=43
                                                               {{{TMaybe<i32>{}}, {73}, {9}, {1}}}, // right: a=73 -> premap: 73*2=146
                                                           });

    const auto int32Type = pb.NewDataType(NUdf::TDataType<i32>::Id);
    const auto [inputRowType, leftArgType, rightArgType] = BuildDefaultStructTypes(pb, list, int32Type);
    const auto [keyColumns, leftColumns, rightColumns] = BuildDefaultColumnMaps(inputRowType, leftArgType, rightArgType);
    const auto listJoinType = pb.NewStreamType(pb.NewTupleType({int32Type, int32Type}));

    const auto listJoin = pb.ListJoinCore(pb.Iterator(list, {}),
                                          int32Type, keyColumns,
                                          leftColumns, rightColumns,
                                          leftArgType, [&pb](const TRuntimeNode arg) { return pb.Increment(pb.Member(arg, "a")); },
                                          rightArgType, [&pb](const TRuntimeNode arg) { return pb.Mul(pb.Member(arg, "b"), pb.NewDataLiteral<i32>(2)); },
                                          listJoinType,
                                          [&pb](const TRuntimeNode, const TRuntimeNode leftList, const TRuntimeNode rightList) {
                                              const auto root = pb.Zip({leftList, rightList});
                                              return pb.Iterator(root, {}); });

    const auto graph = setup.BuildGraph(listJoin);

    using TOutRow = TDefaultOutRow<false>;
    const TVector<TOutRow> expected{{43, 146}};
    AssertUnboxedValueElementEqual(graph->GetValue(), NYql::NUdf::TUnboxedValueComparatorStreamView<TOutRow>(expected));
}

Y_UNIT_TEST_LLVM(SpecialArgmapLambdas) {
    // Left uses identity premap: list items are the premap arg structs themselves.
    // Right uses constant premap: list items are always 42, ignoring the arg content.
    TSetup<LLVM> setup;
    TProgramBuilder& pb = *setup.PgmBuilder;

    const auto list = NTest::ConvertValueToLiteralNode(pb, TVector<TDefaultInRow>{
                                                               {{{i32(23)}, {TMaybe<i32>{}}, {9}, {0}}}, // left:  a=23
                                                               {{{i32(24)}, {TMaybe<i32>{}}, {9}, {0}}}, // left:  a=24
                                                               {{{TMaybe<i32>{}}, {i32(42)}, {9}, {1}}}, // right: b=42 (ignored by premap)
                                                               {{{TMaybe<i32>{}}, {i32(42)}, {9}, {1}}}, // right: b=42 (ignored by premap)
                                                           });

    const auto int32Type = pb.NewDataType(NUdf::TDataType<i32>::Id);
    const auto [inputRowType, leftArgType, rightArgType] = BuildDefaultStructTypes(pb, list, int32Type);
    const auto [keyColumns, leftColumns, rightColumns] = BuildDefaultColumnMaps(inputRowType, leftArgType, rightArgType);
    const auto listJoinType = pb.NewStreamType(pb.NewTupleType({int32Type, int32Type}));

    const auto listJoin = pb.ListJoinCore(pb.Iterator(list, {}),
                                          int32Type, keyColumns,
                                          leftColumns, rightColumns,
                                          leftArgType, [](const TRuntimeNode arg) { return arg; },
                                          rightArgType, [&pb](const TRuntimeNode) { return pb.NewDataLiteral<i32>(73); },
                                          listJoinType,
                                          [&pb](const TRuntimeNode, const TRuntimeNode leftList, const TRuntimeNode rightList) {
                                              const auto root = pb.Zip({rightList, pb.Map(leftList, [&pb](const auto item) { return pb.Member(item, "a"); })});
                                              return pb.Iterator(root, {}); });

    const auto graph = setup.BuildGraph(listJoin);

    using TOutRow = TDefaultOutRow<false>;
    const TVector<TOutRow> expected{{73, 23}, {73, 24}};
    AssertUnboxedValueElementEqual(graph->GetValue(), NYql::NUdf::TUnboxedValueComparatorStreamView<TOutRow>(expected));
}

Y_UNIT_TEST_LLVM(FinishImmediate) {
    // Stream returns Finish before any row is produced (KeyValue_ stays Invalid).
    // The new early-return path fires: Finish is propagated directly without
    // invoking the join lambda, even though the lambda would return non-empty output.
    TSetup<LLVM> setup(GetUnreachableFactory());
    TProgramBuilder& pb = *setup.PgmBuilder;

    const auto list = NTest::ConvertValueToLiteralNode(pb, TVector<TDefaultInRow>{});

    const auto int32Type = pb.NewDataType(NUdf::TDataType<i32>::Id);
    const auto [inputRowType, leftArgType, rightArgType] = BuildDefaultStructTypes(pb, list, int32Type);
    const auto [keyColumns, leftColumns, rightColumns] = BuildDefaultColumnMaps(inputRowType, leftArgType, rightArgType);
    const auto listJoinType = pb.NewStreamType(int32Type);
    const auto unreachableArgmapLambda = [&pb](const TRuntimeNode) -> TRuntimeNode { return Unreachable(pb); };
    const auto unreachableJoinLambda = [&pb, &listJoinType](const TRuntimeNode, const TRuntimeNode, const TRuntimeNode) -> TRuntimeNode {
        const auto listItemType = AS_TYPE(TStreamType, listJoinType)->GetItemType();
        return pb.Seq({Unreachable(pb), pb.Iterator(pb.NewEmptyList(listItemType), {})}, listJoinType);
    };

    const auto listJoin = pb.ListJoinCore(pb.Iterator(list, {}),
                                          int32Type, keyColumns,
                                          leftColumns, rightColumns,
                                          leftArgType, unreachableArgmapLambda,
                                          rightArgType, unreachableArgmapLambda,
                                          listJoinType, unreachableJoinLambda);

    const auto graph = setup.BuildGraph(listJoin);

    const TVector<i32> expected{};
    AssertUnboxedValueElementEqual(graph->GetValue(), NYql::NUdf::TUnboxedValueComparatorStreamView<i32>(expected));
}

Y_UNIT_TEST_LLVM(CheckOptionalWithNoUnwrap) {
    // Test whether nested optionals are handled properly (i.e. NeedUnwrap is false).
    TSetup<LLVM> setup;
    TProgramBuilder& pb = *setup.PgmBuilder;

    using TInnerOpt = TMaybe<i32>;
    using TOuterOpt = TMaybe<TInnerOpt>;
    using TInRow = NTest::TStructType<NTest::TStructMember<"left.a", TOuterOpt>,
                                      NTest::TStructMember<"right.b", TOuterOpt>,
                                      NTest::TStructMember<"key", i32>,
                                      NTest::TStructMember<"_yql_table_index", i32>>;
    const auto list = NTest::ConvertValueToLiteralNode(pb, TVector<TInRow>{
                                                               {{{TOuterOpt{42}}, {TOuterOpt{}}, {9}, {0}}},          // left: 42??
                                                               {{{TOuterOpt{}}, {TOuterOpt{TInnerOpt{}}}, {9}, {1}}}, // right: Just(Nothing(Int32)
                                                               {{{TOuterOpt{73}}, {TOuterOpt{}}, {9}, {0}}},          // left: 73??
                                                               {{{TOuterOpt{}}, {TOuterOpt{}}, {9}, {1}}},            // right: Nothing(Int32?)
                                                           });

    const auto int32Type = pb.NewDataType(NUdf::TDataType<i32>::Id);
    const auto innerOptType = pb.NewOptionalType(int32Type);
    const auto outerOptType = pb.NewOptionalType(innerOptType);
    const auto leftArgType = AS_TYPE(TStructType, pb.NewStructType({{"a", outerOptType}}));
    const auto rightArgType = AS_TYPE(TStructType, pb.NewStructType({{"b", outerOptType}}));
    const auto inputRowType = AS_TYPE(TStructType, AS_TYPE(TListType, list.GetStaticType())->GetItemType());
    const auto [keyColumns, leftColumns, rightColumns] = BuildDefaultColumnMaps(inputRowType, leftArgType, rightArgType);
    const auto listJoinType = pb.NewStreamType(pb.NewTupleType({outerOptType, outerOptType}));

    const auto listJoin = pb.ListJoinCore(pb.Iterator(list, {}),
                                          int32Type, keyColumns,
                                          leftColumns, rightColumns,
                                          leftArgType, [&pb](const TRuntimeNode arg) { return pb.Member(arg, "a"); },
                                          rightArgType, [&pb](const TRuntimeNode arg) { return pb.Member(arg, "b"); },
                                          listJoinType,
                                          [&pb](const TRuntimeNode, const TRuntimeNode leftList, const TRuntimeNode rightList) {
                                              const auto root = pb.Zip({leftList, rightList});
                                              return pb.Iterator(root, {}); });

    const auto graph = setup.BuildGraph(listJoin);

    using TOutRow = std::tuple<TOuterOpt, TOuterOpt>;
    const TVector<TOutRow> expected{{TOuterOpt{42}, TOuterOpt{TInnerOpt{}}}, {73, TOuterOpt{}}};
    AssertUnboxedValueElementEqual(graph->GetValue(), NYql::NUdf::TUnboxedValueComparatorStreamView<TOutRow>(expected));
}

Y_UNIT_TEST_LLVM(CheckPgTypeWithNoUnwrap) {
    // Test whether PGtypes are handled properly (i.e. NeedUnwrap is false).
    TSetup<LLVM> setup(GetFactoryWithPg());
    TProgramBuilder& pb = *setup.PgmBuilder;

    using TInRow = NTest::TStructType<NTest::TStructMember<"left.a", NTest::TPgInt>,
                                      NTest::TStructMember<"right.b", NTest::TPgInt>,
                                      NTest::TStructMember<"key", i32>,
                                      NTest::TStructMember<"_yql_table_index", i32>>;
    const auto list = NTest::ConvertValueToLiteralNode(pb, TVector<TInRow>{
                                                               {{{NTest::TPgInt(23)}, {NTest::TPgInt()}, {9}, {0}}}, // left: 23
                                                               {{{NTest::TPgInt(24)}, {NTest::TPgInt()}, {9}, {0}}}, // left: 24
                                                               {{{NTest::TPgInt()}, {NTest::TPgInt(42)}, {9}, {1}}}, // right: 42
                                                               {{{NTest::TPgInt()}, {NTest::TPgInt(73)}, {9}, {1}}}, // right: 73
                                                           });

    const auto int32Type = pb.NewDataType(NUdf::TDataType<i32>::Id);
    const auto pgIntType = pb.NewPgType(NYql::NPg::LookupType("int4").TypeId);
    const auto leftArgType = AS_TYPE(TStructType, pb.NewStructType({{"a", pgIntType}}));
    const auto rightArgType = AS_TYPE(TStructType, pb.NewStructType({{"b", pgIntType}}));
    const auto inputRowType = AS_TYPE(TStructType, AS_TYPE(TListType, list.GetStaticType())->GetItemType());
    const auto [keyColumns, leftColumns, rightColumns] = BuildDefaultColumnMaps(inputRowType, leftArgType, rightArgType);
    const auto listJoinType = pb.NewStreamType(pb.NewTupleType({int32Type, int32Type}));

    const auto listJoin = pb.ListJoinCore(pb.Iterator(list, {}),
                                          int32Type, keyColumns,
                                          leftColumns, rightColumns,
                                          leftArgType, [&pb, &int32Type](const TRuntimeNode arg) { return pb.FromPg(pb.Member(arg, "a"), int32Type); },
                                          rightArgType, [&pb, &int32Type](const TRuntimeNode arg) { return pb.FromPg(pb.Member(arg, "b"), int32Type); },
                                          listJoinType,
                                          [&pb](const TRuntimeNode, const TRuntimeNode leftList, const TRuntimeNode rightList) {
                                              const auto root = pb.Zip({leftList, rightList});
                                              return pb.Iterator(root, {}); });

    const auto graph = setup.BuildGraph(listJoin);

    using TOutRow = std::tuple<i32, i32>;
    const TVector<TOutRow> expected{{23, 42}, {24, 73}};
    AssertUnboxedValueElementEqual(graph->GetValue(), NYql::NUdf::TUnboxedValueComparatorStreamView<TOutRow>(expected));
}

} // Y_UNIT_TEST_SUITE(TMiniKQLListJoinCoreTest)

#endif // MKQL_RUNTIME_VERSION >= 78U

} // namespace NKikimr::NMiniKQL