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
|
#include "mkql_computation_node_ut.h"
#include <yql/essentials/minikql/arrow/arrow_defs.h>
#include <yql/essentials/minikql/computation/mkql_computation_node_holders.h>
#include <yql/essentials/minikql/computation/mkql_computation_node_codegen.h> // Y_IGNORE
#include <arrow/array/builder_primitive.h>
namespace NKikimr {
namespace NMiniKQL {
namespace {
class TTestBlockFlowWrapper: public TStatefulWideFlowCodegeneratorNode<TTestBlockFlowWrapper> {
using TBaseComputation = TStatefulWideFlowCodegeneratorNode<TTestBlockFlowWrapper>;
public:
TTestBlockFlowWrapper(TComputationMutables& mutables, size_t blockSize, size_t blockCount)
: TBaseComputation(mutables, nullptr, EValueRepresentation::Embedded)
, BlockSize(blockSize)
, BlockCount(blockCount)
{
mutables.CurValueIndex += 3U;
}
EFetchResult DoCalculate(NUdf::TUnboxedValue& state, TComputationContext& ctx, NUdf::TUnboxedValue*const* output) const {
return DoCalculateImpl(state, ctx, *output[0], *output[1], *output[2]);
}
#ifndef MKQL_DISABLE_CODEGEN
ICodegeneratorInlineWideNode::TGenerateResult DoGenGetValues(const TCodegenContext& ctx, Value* statePtr, BasicBlock*& block) const {
auto& context = ctx.Codegen.GetContext();
const auto valueType = Type::getInt128Ty(context);
const auto ptrValueType = PointerType::getUnqual(valueType);
const auto statusType = Type::getInt32Ty(context);
const auto atTop = &ctx.Func->getEntryBlock().back();
const auto values0Ptr = GetElementPtrInst::CreateInBounds(valueType, ctx.GetMutables(), {ConstantInt::get(Type::getInt32Ty(context), static_cast<const IComputationNode*>(this)->GetIndex() + 1U)}, "values_0_ptr", atTop);
const auto values1Ptr = GetElementPtrInst::CreateInBounds(valueType, ctx.GetMutables(), {ConstantInt::get(Type::getInt32Ty(context), static_cast<const IComputationNode*>(this)->GetIndex() + 2U)}, "values_1_ptr", atTop);
const auto values2Ptr = GetElementPtrInst::CreateInBounds(valueType, ctx.GetMutables(), {ConstantInt::get(Type::getInt32Ty(context), static_cast<const IComputationNode*>(this)->GetIndex() + 3U)}, "values_2_ptr", atTop);
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", atTop);
const auto doFunc = ConstantInt::get(Type::getInt64Ty(context), GetMethodPtr(&TTestBlockFlowWrapper::DoCalculateImpl));
const auto doType = FunctionType::get(statusType, {self->getType(), ptrValueType, ctx.Ctx->getType(), ptrValueType, ptrValueType, ptrValueType}, false);
const auto doFuncPtr = CastInst::Create(Instruction::IntToPtr, doFunc, PointerType::getUnqual(doType), "function", atTop);
const auto result = CallInst::Create(doType, doFuncPtr, {self, statePtr, ctx.Ctx, values0Ptr, values1Ptr, values2Ptr}, "result", block);
ICodegeneratorInlineWideNode::TGettersList getters{
[values0Ptr, valueType](const TCodegenContext&, BasicBlock*& block) { return new LoadInst(valueType, values0Ptr, "value", block); },
[values1Ptr, valueType](const TCodegenContext&, BasicBlock*& block) { return new LoadInst(valueType, values1Ptr, "value", block); },
[values2Ptr, valueType](const TCodegenContext&, BasicBlock*& block) { return new LoadInst(valueType, values2Ptr, "value", block); }
};
return {result, std::move(getters)};
}
#endif
private:
EFetchResult DoCalculateImpl(NUdf::TUnboxedValue& state, TComputationContext& ctx, NUdf::TUnboxedValue& val1, NUdf::TUnboxedValue& val2, NUdf::TUnboxedValue& val3) const {
if (state.IsInvalid()) {
state = NUdf::TUnboxedValue::Zero();
}
auto index = state.Get<ui64>();
if (index >= BlockCount) {
return EFetchResult::Finish;
}
arrow::UInt64Builder builder(&ctx.ArrowMemoryPool);
ARROW_OK(builder.Reserve(BlockSize));
for (size_t i = 0; i < BlockSize; ++i) {
builder.UnsafeAppend(index * BlockSize + i);
}
std::shared_ptr<arrow::ArrayData> block;
ARROW_OK(builder.FinishInternal(&block));
val1 = ctx.HolderFactory.CreateArrowBlock(std::move(block));
val2 = ctx.HolderFactory.CreateArrowBlock(arrow::Datum(std::make_shared<arrow::UInt64Scalar>(index)));
val3 = ctx.HolderFactory.CreateArrowBlock(arrow::Datum(std::make_shared<arrow::UInt64Scalar>(BlockSize)));
state = NUdf::TUnboxedValuePod(++index);
return EFetchResult::One;
}
void RegisterDependencies() const final {
}
const size_t BlockSize;
const size_t BlockCount;
};
IComputationNode* WrapTestBlockFlow(TCallable& callable, const TComputationNodeFactoryContext& ctx) {
MKQL_ENSURE(callable.GetInputsCount() == 0, "Expected no args");
return new TTestBlockFlowWrapper(ctx.Mutables, 5, 2);
}
TComputationNodeFactory GetNodeFactory() {
return [](TCallable& callable, const TComputationNodeFactoryContext& ctx) -> IComputationNode* {
if (callable.GetType()->GetName() == "TestBlockFlow") {
return WrapTestBlockFlow(callable, ctx);
}
return GetBuiltinFactory()(callable, ctx);
};
} //namespace
template<bool LLVM>
TRuntimeNode MakeFlow(TSetup<LLVM>& setup) {
TProgramBuilder& pb = *setup.PgmBuilder;
TCallableBuilder callableBuilder(*setup.Env, "TestBlockFlow",
pb.NewFlowType(
pb.NewMultiType({
pb.NewBlockType(pb.NewDataType(NUdf::EDataSlot::Uint64), TBlockType::EShape::Many),
pb.NewBlockType(pb.NewDataType(NUdf::EDataSlot::Uint64), TBlockType::EShape::Scalar),
pb.NewBlockType(pb.NewDataType(NUdf::EDataSlot::Uint64), TBlockType::EShape::Scalar),
})));
return TRuntimeNode(callableBuilder.Build(), false);
}
} // namespace
Y_UNIT_TEST_SUITE(TMiniKQLWideTakeSkipBlocks) {
Y_UNIT_TEST_LLVM(TestWideSkipBlocks) {
TSetup<LLVM> setup(GetNodeFactory());
TProgramBuilder& pb = *setup.PgmBuilder;
const auto flow = MakeFlow(setup);
const auto part = pb.WideSkipBlocks(flow, pb.NewDataLiteral<ui64>(7));
const auto plain = pb.WideFromBlocks(part);
const auto singleValueFlow = pb.NarrowMap(plain, [&](TRuntimeNode::TList items) -> TRuntimeNode {
return pb.Add(items[0], items[1]);
});
const auto pgmReturn = pb.ForwardList(singleValueFlow);
const auto graph = setup.BuildGraph(pgmReturn);
const auto iterator = graph->GetValue().GetListIterator();
NUdf::TUnboxedValue item;
UNIT_ASSERT(iterator.Next(item));
UNIT_ASSERT_VALUES_EQUAL(item.Get<ui64>(), 8);
UNIT_ASSERT(iterator.Next(item));
UNIT_ASSERT_VALUES_EQUAL(item.Get<ui64>(), 9);
UNIT_ASSERT(iterator.Next(item));
UNIT_ASSERT_VALUES_EQUAL(item.Get<ui64>(), 10);
UNIT_ASSERT(!iterator.Next(item));
UNIT_ASSERT(!iterator.Next(item));
}
Y_UNIT_TEST_LLVM(TestWideTakeBlocks) {
TSetup<LLVM> setup(GetNodeFactory());
TProgramBuilder& pb = *setup.PgmBuilder;
const auto flow = MakeFlow(setup);
const auto part = pb.WideTakeBlocks(flow, pb.NewDataLiteral<ui64>(4));
const auto plain = pb.WideFromBlocks(part);
const auto singleValueFlow = pb.NarrowMap(plain, [&](TRuntimeNode::TList items) -> TRuntimeNode {
return pb.Add(items[0], items[1]);
});
const auto pgmReturn = pb.ForwardList(singleValueFlow);
const auto graph = setup.BuildGraph(pgmReturn);
const auto iterator = graph->GetValue().GetListIterator();
NUdf::TUnboxedValue item;
UNIT_ASSERT(iterator.Next(item));
UNIT_ASSERT_VALUES_EQUAL(item.Get<ui64>(), 0);
UNIT_ASSERT(iterator.Next(item));
UNIT_ASSERT_VALUES_EQUAL(item.Get<ui64>(), 1);
UNIT_ASSERT(iterator.Next(item));
UNIT_ASSERT_VALUES_EQUAL(item.Get<ui64>(), 2);
UNIT_ASSERT(iterator.Next(item));
UNIT_ASSERT_VALUES_EQUAL(item.Get<ui64>(), 3);
UNIT_ASSERT(!iterator.Next(item));
UNIT_ASSERT(!iterator.Next(item));
}
Y_UNIT_TEST_LLVM(TestWideTakeSkipBlocks) {
TSetup<LLVM> setup(GetNodeFactory());
TProgramBuilder& pb = *setup.PgmBuilder;
const auto flow = MakeFlow(setup);
const auto part = pb.WideTakeBlocks(pb.WideSkipBlocks(flow, pb.NewDataLiteral<ui64>(3)), pb.NewDataLiteral<ui64>(5));
const auto plain = pb.WideFromBlocks(part);
const auto singleValueFlow = pb.NarrowMap(plain, [&](TRuntimeNode::TList items) -> TRuntimeNode {
// 0, 0;
// 1, 0;
// 2, 0;
// 3, 0; -> 3
// 4, 0; -> 4
// 5, 1; -> 6
// 6, 1; -> 7
// 7, 1; -> 8
// 8, 1;
// 9, 1;
// 10, 1;
return pb.Add(items[0], items[1]);
});
const auto pgmReturn = pb.ForwardList(singleValueFlow);
const auto graph = setup.BuildGraph(pgmReturn);
const auto iterator = graph->GetValue().GetListIterator();
NUdf::TUnboxedValue item;
UNIT_ASSERT(iterator.Next(item));
UNIT_ASSERT_VALUES_EQUAL(item.Get<ui64>(), 3);
UNIT_ASSERT(iterator.Next(item));
UNIT_ASSERT_VALUES_EQUAL(item.Get<ui64>(), 4);
UNIT_ASSERT(iterator.Next(item));
UNIT_ASSERT_VALUES_EQUAL(item.Get<ui64>(), 6);
UNIT_ASSERT(iterator.Next(item));
UNIT_ASSERT_VALUES_EQUAL(item.Get<ui64>(), 7);
UNIT_ASSERT(iterator.Next(item));
UNIT_ASSERT_VALUES_EQUAL(item.Get<ui64>(), 8);
UNIT_ASSERT(!iterator.Next(item));
UNIT_ASSERT(!iterator.Next(item));
}
}
} // namespace NMiniKQL
} // namespace NKikimr
|