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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
|
#include "mkql_block_top.h"
#include <yql/essentials/minikql/computation/mkql_block_reader.h>
#include <yql/essentials/minikql/computation/mkql_block_builder.h>
#include <yql/essentials/minikql/computation/mkql_block_impl.h>
#include <yql/essentials/minikql/computation/mkql_block_impl_codegen.h> // Y_IGNORE
#include <yql/essentials/public/udf/arrow/block_item_comparator.h>
#include <yql/essentials/minikql/arrow/arrow_defs.h>
#include <yql/essentials/minikql/arrow/arrow_util.h>
#include <yql/essentials/minikql/mkql_type_builder.h>
#include <yql/essentials/minikql/computation/mkql_computation_node_holders.h>
#include <yql/essentials/minikql/computation/mkql_computation_node_codegen.h> // Y_IGNORE
#include <yql/essentials/minikql/mkql_node_builder.h>
#include <yql/essentials/minikql/mkql_node_cast.h>
#include <yql/essentials/utils/sort.h>
namespace NKikimr {
namespace NMiniKQL {
namespace {
template <bool Sort, bool HasCount>
class TTopOrSortBlocksWrapper : public TStatefulWideFlowCodegeneratorNode<TTopOrSortBlocksWrapper<Sort, HasCount>> {
using TBaseComputation = TStatefulWideFlowCodegeneratorNode<TTopOrSortBlocksWrapper<Sort, HasCount>>;
using TChunkedArrayIndex = std::vector<IArrayBuilder::TArrayDataItem>;
public:
TTopOrSortBlocksWrapper(TComputationMutables& mutables, IComputationWideFlowNode* flow, TArrayRef<TType* const> wideComponents, IComputationNode* count,
TComputationNodePtrVector&& directions, std::vector<ui32>&& keyIndicies)
: TBaseComputation(mutables, flow, EValueRepresentation::Boxed)
, Flow_(flow)
, Count_(count)
, Directions_(std::move(directions))
, KeyIndicies_(std::move(keyIndicies))
, WideFieldsIndex_(mutables.IncrementWideFieldsIndex(wideComponents.size()))
{
for (ui32 i = 0; i < wideComponents.size() - 1; ++i) {
Columns_.push_back(AS_TYPE(TBlockType, wideComponents[i]));
}
}
EFetchResult DoCalculate(NUdf::TUnboxedValue& state, TComputationContext& ctx, NUdf::TUnboxedValue*const* output) const {
Y_ABORT_UNLESS(output[Columns_.size()]);
auto& s = GetState(state, ctx);
if (!s.Count) {
if (s.IsFinished_)
return EFetchResult::Finish;
if (!s.WritingOutput_) {
for (const auto fields = ctx.WideFields.data() + WideFieldsIndex_;;) {
switch (Flow_->FetchValues(ctx, fields)) {
case EFetchResult::Yield:
return EFetchResult::Yield;
case EFetchResult::One:
s.ProcessInput();
continue;
case EFetchResult::Finish:
break;
}
break;
}
}
if (!s.FillOutput(ctx.HolderFactory))
return EFetchResult::Finish;
}
const auto sliceSize = s.Slice();
for (size_t i = 0; i <= Columns_.size(); ++i) {
if (const auto out = output[i]) {
*out = s.Get(sliceSize, ctx.HolderFactory, i);
}
}
return EFetchResult::One;
}
#ifndef MKQL_DISABLE_CODEGEN
ICodegeneratorInlineWideNode::TGenerateResult DoGenGetValues(const TCodegenContext& ctx, Value* statePtr, BasicBlock*& block) const {
auto& context = ctx.Codegen.GetContext();
const auto width = Columns_.size() + 1U;
const auto valueType = Type::getInt128Ty(context);
const auto statusType = Type::getInt32Ty(context);
const auto indexType = Type::getInt64Ty(context);
const auto flagType = Type::getInt1Ty(context);
const auto arrayType = ArrayType::get(valueType, width);
const auto ptrValuesType = PointerType::getUnqual(arrayType);
TLLVMFieldsStructureState stateFields(context, width);
const auto stateType = StructType::get(context, stateFields.GetFieldsArray());
const auto statePtrType = PointerType::getUnqual(stateType);
const auto atTop = &ctx.Func->getEntryBlock().back();
const auto getFunc = ConstantInt::get(Type::getInt64Ty(context), GetMethodPtr(&TState::Get));
const auto getType = FunctionType::get(valueType, {statePtrType, indexType, ctx.GetFactory()->getType(), indexType}, false);
const auto getPtr = CastInst::Create(Instruction::IntToPtr, getFunc, PointerType::getUnqual(getType), "get", atTop);
const auto heightPtr = new AllocaInst(indexType, 0U, "height_ptr", atTop);
const auto stateOnStack = new AllocaInst(statePtrType, 0U, "state_on_stack", atTop);
new StoreInst(ConstantInt::get(indexType, 0), heightPtr, atTop);
new StoreInst(ConstantPointerNull::get(statePtrType), stateOnStack, atTop);
const auto make = BasicBlock::Create(context, "make", ctx.Func);
const auto main = BasicBlock::Create(context, "main", ctx.Func);
const auto more = BasicBlock::Create(context, "more", ctx.Func);
const auto test = BasicBlock::Create(context, "test", ctx.Func);
const auto read = BasicBlock::Create(context, "read", ctx.Func);
const auto good = BasicBlock::Create(context, "good", ctx.Func);
const auto work = BasicBlock::Create(context, "work", ctx.Func);
const auto fill = BasicBlock::Create(context, "fill", ctx.Func);
const auto over = BasicBlock::Create(context, "over", ctx.Func);
BranchInst::Create(make, main, IsInvalid(statePtr, block), block);
block = make;
llvm::Value* trunc;
if constexpr (HasCount) {
const auto count = GetNodeValue(Count_, ctx, block);
trunc = GetterFor<ui64>(count, context, block);
} else {
trunc = ConstantInt::get(Type::getInt64Ty(context), 0U);
}
const auto dirsType = ArrayType::get(flagType, Directions_.size());
const auto dirs = new AllocaInst(dirsType, 0U, "dirs", block);
for (auto i = 0U; i < Directions_.size(); ++i) {
const auto dir = GetNodeValue(Directions_[i], ctx, block);
const auto cut = GetterFor<bool>(dir, context, block);
const auto ptr = GetElementPtrInst::CreateInBounds(dirsType, dirs, {ConstantInt::get(indexType, 0), ConstantInt::get(indexType, i)}, "ptr", block);
new StoreInst(cut, ptr, block);
}
const auto ptrType = PointerType::getUnqual(StructType::get(context));
const auto self = CastInst::Create(Instruction::IntToPtr, ConstantInt::get(Type::getInt64Ty(context), uintptr_t(this)), ptrType, "self", block);
const auto makeFunc = ConstantInt::get(Type::getInt64Ty(context), GetMethodPtr(&TTopOrSortBlocksWrapper::MakeState));
const auto makeType = FunctionType::get(Type::getVoidTy(context), {self->getType(), ctx.Ctx->getType(), statePtr->getType(), dirs->getType(), trunc->getType()}, false);
const auto makeFuncPtr = CastInst::Create(Instruction::IntToPtr, makeFunc, PointerType::getUnqual(makeType), "function", block);
CallInst::Create(makeType, makeFuncPtr, {self, ctx.Ctx, statePtr, dirs, trunc}, "", block);
BranchInst::Create(main, block);
block = main;
const auto state = new LoadInst(valueType, statePtr, "state", block);
const auto half = CastInst::Create(Instruction::Trunc, state, Type::getInt64Ty(context), "half", block);
const auto stateArg = CastInst::Create(Instruction::IntToPtr, half, statePtrType, "state_arg", block);
const auto countPtr = GetElementPtrInst::CreateInBounds(stateType, stateArg, { stateFields.This(), stateFields.GetCount() }, "count_ptr", block);
const auto count = new LoadInst(indexType, countPtr, "count", block);
const auto none = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ, count, ConstantInt::get(indexType, 0), "none", block);
BranchInst::Create(more, fill, none, block);
block = more;
const auto finishedPtr = GetElementPtrInst::CreateInBounds(stateType, stateArg, { stateFields.This(), stateFields.GetIsFinished() }, "is_finished_ptr", block);
const auto finished = new LoadInst(flagType, finishedPtr, "finished", block);
const auto result = PHINode::Create(statusType, 4U, "result", over);
result->addIncoming(ConstantInt::get(statusType, static_cast<i32>(EFetchResult::Finish)), block);
BranchInst::Create(over, test, finished, block);
block = test;
const auto writingOutputPtr = GetElementPtrInst::CreateInBounds(stateType, stateArg, { stateFields.This(), stateFields.GetWritingOutput() }, "writing_output_ptr", block);
const auto writingOutput = new LoadInst(flagType, writingOutputPtr, "writing_output", block);
BranchInst::Create(work, read, writingOutput, block);
block = read;
const auto getres = GetNodeValues(Flow_, ctx, block);
result->addIncoming(ConstantInt::get(statusType, static_cast<i32>(EFetchResult::Yield)), block);
const auto way = SwitchInst::Create(getres.first, good, 2U, block);
way->addCase(ConstantInt::get(statusType, i32(EFetchResult::Finish)), work);
way->addCase(ConstantInt::get(statusType, i32(EFetchResult::Yield)), over);
block = good;
const auto valuesPtr = GetElementPtrInst::CreateInBounds(stateType, stateArg, { stateFields.This(), stateFields.GetPointer() }, "values_ptr", block);
const auto values = new LoadInst(ptrValuesType, valuesPtr, "values", block);
Value* array = UndefValue::get(arrayType);
for (auto idx = 0U; idx < getres.second.size(); ++idx) {
const auto value = getres.second[idx](ctx, block);
AddRefBoxed(value, ctx, block);
array = InsertValueInst::Create(array, value, {idx}, (TString("value_") += ToString(idx)).c_str(), block);
}
new StoreInst(array, values, block);
const auto processBlockFunc = ConstantInt::get(Type::getInt64Ty(context), GetMethodPtr(&TState::ProcessInput));
const auto processBlockType = FunctionType::get(Type::getVoidTy(context), {statePtrType}, false);
const auto processBlockPtr = CastInst::Create(Instruction::IntToPtr, processBlockFunc, PointerType::getUnqual(processBlockType), "process_inputs_func", block);
CallInst::Create(processBlockType, processBlockPtr, {stateArg}, "", block);
BranchInst::Create(read, block);
block = work;
const auto fillBlockFunc = ConstantInt::get(Type::getInt64Ty(context), GetMethodPtr(&TState::FillOutput));
const auto fillBlockType = FunctionType::get(flagType, {statePtrType, ctx.GetFactory()->getType()}, false);
const auto fillBlockPtr = CastInst::Create(Instruction::IntToPtr, fillBlockFunc, PointerType::getUnqual(fillBlockType), "fill_output_func", block);
const auto hasData = CallInst::Create(fillBlockType, fillBlockPtr, {stateArg, ctx.GetFactory()}, "fill_output", block);
result->addIncoming(ConstantInt::get(statusType, static_cast<i32>(EFetchResult::Finish)), block);
BranchInst::Create(fill, over, hasData, block);
block = fill;
const auto sliceFunc = ConstantInt::get(Type::getInt64Ty(context), GetMethodPtr(&TState::Slice));
const auto sliceType = FunctionType::get(indexType, {statePtrType}, false);
const auto slicePtr = CastInst::Create(Instruction::IntToPtr, sliceFunc, PointerType::getUnqual(sliceType), "slice_func", block);
const auto slice = CallInst::Create(sliceType, slicePtr, {stateArg}, "slice", block);
new StoreInst(slice, heightPtr, block);
new StoreInst(stateArg, stateOnStack, block);
result->addIncoming(ConstantInt::get(statusType, static_cast<i32>(EFetchResult::One)), block);
BranchInst::Create(over, block);
block = over;
ICodegeneratorInlineWideNode::TGettersList getters(width);
for (size_t idx = 0U; idx < getters.size(); ++idx) {
getters[idx] = [idx, getType, getPtr, heightPtr, indexType, statePtrType, stateOnStack](const TCodegenContext& ctx, BasicBlock*& block) {
const auto stateArg = new LoadInst(statePtrType, stateOnStack, "state", block);
const auto heightArg = new LoadInst(indexType, heightPtr, "height", block);
return CallInst::Create(getType, getPtr, {stateArg, heightArg, ctx.GetFactory(), ConstantInt::get(indexType, idx)}, "get", block);
};
}
return {result, std::move(getters)};
}
#endif
private:
void RegisterDependencies() const final {
if (const auto flow = this->FlowDependsOn(Flow_)) {
if constexpr (HasCount) {
this->DependsOn(flow, Count_);
}
for (auto dir : Directions_) {
this->DependsOn(flow, dir);
}
}
}
class TState : public TBlockState {
public:
bool WritingOutput_ = false;
bool IsFinished_ = false;
ui64 OutputLength_ = 0;
ui64 Written_ = 0;
const std::vector<bool> Directions_;
const ui64 Count_;
const std::vector<TBlockType*> Columns_;
const std::vector<ui32> KeyIndicies_;
std::vector<std::vector<arrow::Datum>> SortInput_;
std::vector<ui64> SortPermutation_;
std::vector<TChunkedArrayIndex> SortArrays_;
bool ScalarsFilled_ = false;
TUnboxedValueVector ScalarValues_;
std::vector<std::unique_ptr<IBlockReader>> LeftReaders_;
std::vector<std::unique_ptr<IBlockReader>> RightReaders_;
std::vector<std::unique_ptr<IArrayBuilder>> Builders_;
ui64 BuilderMaxLength_ = 0;
ui64 BuilderLength_ = 0;
std::vector<NUdf::IBlockItemComparator::TPtr> Comparators_; // by key columns only
TState(TMemoryUsageInfo* memInfo, TComputationContext& ctx, const std::vector<ui32>& keyIndicies, const std::vector<TBlockType*>& columns, const bool* directions, ui64 count)
: TBlockState(memInfo, columns.size() + 1U)
, IsFinished_(HasCount && !count)
, Directions_(directions, directions + keyIndicies.size())
, Count_(count)
, Columns_(columns)
, KeyIndicies_(keyIndicies)
, SortInput_(Columns_.size())
, SortArrays_(Columns_.size())
, LeftReaders_(Columns_.size())
, RightReaders_(Columns_.size())
, Builders_(Columns_.size())
, Comparators_(KeyIndicies_.size())
{
for (ui32 i = 0; i < Columns_.size(); ++i) {
if (Columns_[i]->GetShape() == TBlockType::EShape::Scalar) {
continue;
}
LeftReaders_[i] = MakeBlockReader(TTypeInfoHelper(), columns[i]->GetItemType());
RightReaders_[i] = MakeBlockReader(TTypeInfoHelper(), columns[i]->GetItemType());
}
for (ui32 k = 0; k < KeyIndicies_.size(); ++k) {
Comparators_[k] = TBlockTypeHelper().MakeComparator(Columns_[KeyIndicies_[k]]->GetItemType());
}
BuilderMaxLength_ = GetStorageLength();
size_t maxBlockItemSize = 0;
for (ui32 i = 0; i < Columns_.size(); ++i) {
if (Columns_[i]->GetShape() == TBlockType::EShape::Scalar) {
continue;
}
maxBlockItemSize = Max(maxBlockItemSize, CalcMaxBlockItemSize(Columns_[i]->GetItemType()));
};
BuilderMaxLength_ = Max(BuilderMaxLength_, CalcBlockLen(maxBlockItemSize));
for (ui32 i = 0; i < Columns_.size(); ++i) {
if (Columns_[i]->GetShape() == TBlockType::EShape::Scalar) {
continue;
}
Builders_[i] = MakeArrayBuilder(TTypeInfoHelper(), Columns_[i]->GetItemType(), ctx.ArrowMemoryPool, BuilderMaxLength_, &ctx.Builder->GetPgBuilder());
}
}
void Add(const NUdf::TUnboxedValuePod value, size_t idx) {
Values[idx] = value;
}
void ProcessInput() {
const ui64 blockLen = TArrowBlock::From(Values.back()).GetDatum().template scalar_as<arrow::UInt64Scalar>().value;
if (!ScalarsFilled_) {
for (ui32 i = 0; i < Columns_.size(); ++i) {
if (Columns_[i]->GetShape() == TBlockType::EShape::Scalar) {
ScalarValues_[i] = std::move(Values[i]);
}
}
ScalarsFilled_ = true;
}
if constexpr (!HasCount) {
for (ui32 i = 0; i < Columns_.size(); ++i) {
auto datum = TArrowBlock::From(Values[i]).GetDatum();
if (Columns_[i]->GetShape() != TBlockType::EShape::Scalar) {
SortInput_[i].emplace_back(datum);
}
}
OutputLength_ += blockLen;
Values.assign(Values.size(), NUdf::TUnboxedValuePod());
return;
}
// shrink input block
std::optional<std::vector<ui64>> blockIndicies;
if (blockLen > Count_) {
blockIndicies.emplace();
blockIndicies->reserve(blockLen);
for (ui64 row = 0; row < blockLen; ++row) {
blockIndicies->emplace_back(row);
}
std::vector<TChunkedArrayIndex> arrayIndicies(Columns_.size());
for (ui32 i = 0; i < Columns_.size(); ++i) {
if (Columns_[i]->GetShape() != TBlockType::EShape::Scalar) {
auto datum = TArrowBlock::From(Values[i]).GetDatum();
arrayIndicies[i] = MakeChunkedArrayIndex(datum);
}
}
const TBlockLess cmp(KeyIndicies_, *this, arrayIndicies);
NYql::FastNthElement(blockIndicies->begin(), blockIndicies->begin() + Count_, blockIndicies->end(), cmp);
}
// copy all to builders
AddTop(Columns_, blockIndicies, blockLen);
if (BuilderLength_ + Count_ > BuilderMaxLength_) {
CompressBuilders(false);
}
Values.assign(Values.size(), NUdf::TUnboxedValuePod());
}
ui64 GetStorageLength() const {
return 2 * Count_;
}
void CompressBuilders(bool sort) {
Y_ABORT_UNLESS(ScalarsFilled_);
std::vector<TChunkedArrayIndex> arrayIndicies(Columns_.size());
std::vector<arrow::Datum> tmpDatums(Columns_.size());
for (ui32 i = 0; i < Columns_.size(); ++i) {
if (Columns_[i]->GetShape() != TBlockType::EShape::Scalar) {
auto datum = Builders_[i]->Build(false);
arrayIndicies[i] = MakeChunkedArrayIndex(datum);
tmpDatums[i] = std::move(datum);
}
}
std::vector<ui64> blockIndicies;
blockIndicies.reserve(BuilderLength_);
for (ui64 row = 0; row < BuilderLength_; ++row) {
blockIndicies.push_back(row);
}
const ui64 blockLen = Min(BuilderLength_, Count_);
const TBlockLess cmp(KeyIndicies_, *this, arrayIndicies);
if (BuilderLength_ <= Count_) {
if (sort) {
std::sort(blockIndicies.begin(), blockIndicies.end(), cmp);
}
} else {
if (sort) {
NYql::FastPartialSort(blockIndicies.begin(), blockIndicies.begin() + blockLen, blockIndicies.end(), cmp);
} else {
NYql::FastNthElement(blockIndicies.begin(), blockIndicies.begin() + blockLen, blockIndicies.end(), cmp);
}
}
for (ui32 i = 0; i < Columns_.size(); ++i) {
if (Columns_[i]->GetShape() == TBlockType::EShape::Scalar) {
continue;
}
auto& arrayIndex = arrayIndicies[i];
Builders_[i]->AddMany(arrayIndex.data(), arrayIndex.size(), blockIndicies.data(), blockLen);
}
BuilderLength_ = blockLen;
}
void SortAll() {
SortPermutation_.reserve(OutputLength_);
for (ui64 i = 0; i < OutputLength_; ++i) {
SortPermutation_.emplace_back(i);
}
for (ui32 i = 0; i < Columns_.size(); ++i) {
ui64 offset = 0;
for (const auto& datum : SortInput_[i]) {
if (datum.is_scalar()) {
continue;
} else if (datum.is_array()) {
auto arrayData = datum.array();
SortArrays_[i].push_back({ arrayData.get(), offset });
offset += arrayData->length;
} else {
auto chunks = datum.chunks();
for (auto& chunk : chunks) {
auto arrayData = chunk->data();
SortArrays_[i].push_back({ arrayData.get(), offset });
offset += arrayData->length;
}
}
}
}
TBlockLess cmp(KeyIndicies_, *this, SortArrays_);
std::sort(SortPermutation_.begin(), SortPermutation_.end(), cmp);
}
bool FillOutput(const THolderFactory& holderFactory) {
if (WritingOutput_) {
FillSortOutputPart(holderFactory);
} else if constexpr (!HasCount) {
if (!OutputLength_) {
IsFinished_ = true;
return false;
}
SortAll();
WritingOutput_ = true;
FillSortOutputPart(holderFactory);
} else {
IsFinished_ = true;
if (!BuilderLength_) {
return false;
}
if (BuilderLength_ > Count_ || Sort) {
CompressBuilders(Sort);
}
for (ui32 i = 0; i < Columns_.size(); ++i) {
if (Columns_[i]->GetShape() == TBlockType::EShape::Scalar) {
Values[i] = ScalarValues_[i];
} else {
Values[i] = holderFactory.CreateArrowBlock(arrow::Datum(Builders_[i]->Build(true)));
}
}
Values.back() = holderFactory.CreateArrowBlock(arrow::Datum(std::make_shared<arrow::UInt64Scalar>(BuilderLength_)));
}
FillArrays();
return true;
}
void FillSortOutputPart(const THolderFactory& holderFactory) {
auto blockLen = Min(BuilderMaxLength_, OutputLength_ - Written_);
const bool isLast = (Written_ + blockLen == OutputLength_);
for (ui32 i = 0; i < Columns_.size(); ++i) {
if (Columns_[i]->GetShape() == TBlockType::EShape::Scalar) {
Values[i] = ScalarValues_[i];
} else {
Builders_[i]->AddMany(SortArrays_[i].data(), SortArrays_[i].size(), SortPermutation_.data() + Written_, blockLen);
Values[i] = holderFactory.CreateArrowBlock(arrow::Datum(Builders_[i]->Build(isLast)));
}
}
Values.back() = holderFactory.CreateArrowBlock(arrow::Datum(std::make_shared<arrow::UInt64Scalar>(blockLen)));
Written_ += blockLen;
if (Written_ >= OutputLength_)
IsFinished_ = true;
}
void AddTop(const std::vector<TBlockType*>& columns, const std::optional<std::vector<ui64>>& blockIndicies, ui64 blockLen) {
for (ui32 i = 0; i < columns.size(); ++i) {
if (columns[i]->GetShape() == TBlockType::EShape::Scalar) {
continue;
}
const auto& datum = TArrowBlock::From(Values[i]).GetDatum();
auto arrayIndex = MakeChunkedArrayIndex(datum);
if (blockIndicies) {
Builders_[i]->AddMany(arrayIndex.data(), arrayIndex.size(), blockIndicies->data(), Count_);
} else {
Builders_[i]->AddMany(arrayIndex.data(), arrayIndex.size(), ui64(0), blockLen);
}
}
if (blockIndicies) {
BuilderLength_ += Count_;
} else {
BuilderLength_ += blockLen;
}
}
};
#ifndef MKQL_DISABLE_CODEGEN
class TLLVMFieldsStructureState: public TLLVMFieldsStructureBlockState {
private:
using TBase = TLLVMFieldsStructureBlockState;
llvm::IntegerType*const WritingOutputType;
llvm::IntegerType*const IsFinishedType;
protected:
using TBase::Context;
public:
std::vector<llvm::Type*> GetFieldsArray() {
std::vector<llvm::Type*> result = TBase::GetFieldsArray();
result.emplace_back(WritingOutputType);
result.emplace_back(IsFinishedType);
return result;
}
llvm::Constant* GetWritingOutput() {
return ConstantInt::get(Type::getInt32Ty(Context), TBase::GetFieldsCount() + BaseFields);
}
llvm::Constant* GetIsFinished() {
return ConstantInt::get(Type::getInt32Ty(Context), TBase::GetFieldsCount() + BaseFields + 1);
}
TLLVMFieldsStructureState(llvm::LLVMContext& context, size_t width)
: TBase(context, width)
, WritingOutputType(Type::getInt1Ty(Context))
, IsFinishedType(Type::getInt1Ty(Context))
{}
};
#endif
void MakeState(TComputationContext& ctx, NUdf::TUnboxedValue& state, const bool* directions, ui64 count = 0ULL) const {
state = ctx.HolderFactory.Create<TState>(ctx, KeyIndicies_, Columns_, directions, count);
}
TState& GetState(NUdf::TUnboxedValue& state, TComputationContext& ctx) const {
if (state.IsInvalid()) {
std::vector<bool> dirs(Directions_.size());
std::transform(Directions_.cbegin(), Directions_.cend(), dirs.begin(), [&ctx](IComputationNode* dir){ return dir->GetValue(ctx).Get<bool>(); });
if constexpr (HasCount)
MakeState(ctx, state, dirs.data(), Count_->GetValue(ctx).Get<ui64>());
else
MakeState(ctx, state, dirs.data());
auto& s = *static_cast<TState*>(state.AsBoxed().Get());
const auto fields = ctx.WideFields.data() + WideFieldsIndex_;
for (size_t i = 0; i < s.Values.size(); ++i) {
fields[i] = &s.Values[i];
}
return s;
}
return *static_cast<TState*>(state.AsBoxed().Get());
}
static TChunkedArrayIndex MakeChunkedArrayIndex(const arrow::Datum& datum) {
TChunkedArrayIndex result;
if (datum.is_array()) {
result.push_back({datum.array().get(), 0});
} else {
auto chunks = datum.chunks();
ui64 offset = 0;
for (auto& chunk : chunks) {
auto arrayData = chunk->data();
result.push_back({arrayData.get(), offset});
offset += arrayData->length;
}
}
return result;
}
class TBlockLess {
public:
TBlockLess(const std::vector<ui32>& keyIndicies, const TState& state, const std::vector<TChunkedArrayIndex>& arrayIndicies)
: KeyIndicies_(keyIndicies)
, ArrayIndicies_(arrayIndicies)
, State_(state)
{}
bool operator()(ui64 lhs, ui64 rhs) const {
if (KeyIndicies_.size() == 1) {
auto i = KeyIndicies_[0];
auto& arrayIndex = ArrayIndicies_[i];
if (arrayIndex.empty()) {
// scalar
return false;
}
auto leftItem = GetBlockItem(*State_.LeftReaders_[i], arrayIndex, lhs);
auto rightItem = GetBlockItem(*State_.RightReaders_[i], arrayIndex, rhs);
if (State_.Directions_[0]) {
return State_.Comparators_[0]->Less(leftItem, rightItem);
} else {
return State_.Comparators_[0]->Greater(leftItem, rightItem);
}
} else {
for (ui32 k = 0; k < KeyIndicies_.size(); ++k) {
auto i = KeyIndicies_[k];
auto& arrayIndex = ArrayIndicies_[i];
if (arrayIndex.empty()) {
// scalar
continue;
}
auto leftItem = GetBlockItem(*State_.LeftReaders_[i], arrayIndex, lhs);
auto rightItem = GetBlockItem(*State_.RightReaders_[i], arrayIndex, rhs);
auto cmp = State_.Comparators_[k]->Compare(leftItem, rightItem);
if (cmp == 0) {
continue;
}
if (State_.Directions_[k]) {
return cmp < 0;
} else {
return cmp > 0;
}
}
return false;
}
}
private:
static TBlockItem GetBlockItem(IBlockReader& reader, const TChunkedArrayIndex& arrayIndex, ui64 idx) {
Y_DEBUG_ABORT_UNLESS(!arrayIndex.empty());
if (arrayIndex.size() == 1) {
return reader.GetItem(*arrayIndex.front().Data, idx);
}
auto it = LookupArrayDataItem(arrayIndex.data(), arrayIndex.size(), idx);
return reader.GetItem(*it->Data, idx);
}
const std::vector<ui32>& KeyIndicies_;
const std::vector<TChunkedArrayIndex> ArrayIndicies_;
const TState& State_;
};
IComputationWideFlowNode *const Flow_;
IComputationNode *const Count_;
const TComputationNodePtrVector Directions_;
const std::vector<ui32> KeyIndicies_;
std::vector<TBlockType*> Columns_;
const size_t WideFieldsIndex_;
};
template <bool Sort, bool HasCount>
IComputationNode* WrapTopOrSort(TCallable& callable, const TComputationNodeFactoryContext& ctx) {
constexpr ui32 offset = HasCount ? 0 : 1;
const ui32 inputsWithCount = callable.GetInputsCount() + offset;
MKQL_ENSURE(inputsWithCount > 2U && !(inputsWithCount % 2U), "Expected more arguments.");
const auto flowType = AS_TYPE(TFlowType, callable.GetInput(0).GetStaticType());
const auto wideComponents = GetWideComponents(flowType);
MKQL_ENSURE(wideComponents.size() > 0, "Expected at least one column");
const auto wideFlow = dynamic_cast<IComputationWideFlowNode*>(LocateNode(ctx.NodeLocator, callable, 0));
MKQL_ENSURE(wideFlow != nullptr, "Expected wide flow node");
IComputationNode* count = nullptr;
if constexpr (HasCount) {
const auto countType = AS_TYPE(TDataType, callable.GetInput(1).GetStaticType());
MKQL_ENSURE(countType->GetSchemeType() == NUdf::TDataType<ui64>::Id, "Expected ui64");
count = LocateNode(ctx.NodeLocator, callable, 1);
}
TComputationNodePtrVector directions;
std::vector<ui32> keyIndicies;
for (ui32 i = 2; i < inputsWithCount; i += 2) {
ui32 keyIndex = AS_VALUE(TDataLiteral, callable.GetInput(i - offset))->AsValue().Get<ui32>();
MKQL_ENSURE(keyIndex + 1 < wideComponents.size(), "Wrong key index");
keyIndicies.push_back(keyIndex);
directions.push_back(LocateNode(ctx.NodeLocator, callable, i + 1 - offset));
}
return new TTopOrSortBlocksWrapper<Sort, HasCount>(ctx.Mutables, wideFlow, wideComponents, count, std::move(directions), std::move(keyIndicies));
}
} //namespace
IComputationNode* WrapWideTopBlocks(TCallable& callable, const TComputationNodeFactoryContext& ctx) {
return WrapTopOrSort<false, true>(callable, ctx);
}
IComputationNode* WrapWideTopSortBlocks(TCallable& callable, const TComputationNodeFactoryContext& ctx) {
return WrapTopOrSort<true, true>(callable, ctx);
}
IComputationNode* WrapWideSortBlocks(TCallable& callable, const TComputationNodeFactoryContext& ctx) {
return WrapTopOrSort<true, false>(callable, ctx);
}
}
}
|