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
|
#include "mkql_scalar_apply.h"
#include <yql/essentials/public/udf/arrow/memory_pool.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/arrow/arrow_defs.h>
#include <yql/essentials/minikql/arrow/arrow_util.h>
#include <yql/essentials/minikql/computation/mkql_computation_node_holders.h>
#include <yql/essentials/minikql/computation/mkql_value_builder.h>
#include <yql/essentials/minikql/mkql_node_cast.h>
#include <yql/essentials/parser/pg_wrapper/interface/utils.h>
namespace NKikimr {
namespace NMiniKQL {
namespace {
class TScalarApplyWrapper : public TMutableComputationNode<TScalarApplyWrapper> {
public:
struct TAccessors {
TAccessors(const TVector<TType*>& argsTypes, TType* returnType, const NUdf::IPgBuilder& pgBuilder)
: PgBuilder(pgBuilder)
{
auto returnItemType = AS_TYPE(TBlockType, returnType)->GetItemType();
ReturnConverter = MakeBlockItemConverter(TTypeInfoHelper(), returnItemType, pgBuilder);
ArgsConverters.reserve(argsTypes.size());
ArgsReaders.reserve(argsTypes.size());
for (auto type : argsTypes) {
ArgsConverters.emplace_back(MakeBlockItemConverter(TTypeInfoHelper(), AS_TYPE(TBlockType, type)->GetItemType(), pgBuilder));
ArgsReaders.emplace_back(MakeBlockReader(TTypeInfoHelper(), AS_TYPE(TBlockType, type)->GetItemType()));
}
}
const NUdf::IPgBuilder& PgBuilder;
std::unique_ptr<IBlockItemConverter> ReturnConverter;
TVector<std::unique_ptr<IBlockItemConverter>> ArgsConverters;
TVector<std::unique_ptr<IBlockReader>> ArgsReaders;
bool ScalarsProcessed = false;
};
struct TKernelState : public arrow::compute::KernelState {
TKernelState(const TVector<TType*>& argsTypes, TType* returnType, const TComputationContext& originalContext)
: Alloc(__LOCATION__)
, TypeEnv(Alloc)
, MemInfo("ScalarApply")
, FunctionRegistry(originalContext.HolderFactory.GetFunctionRegistry()->Clone())
, HolderFactory(Alloc.Ref(), MemInfo, FunctionRegistry.Get())
, ValueBuilder(HolderFactory, NUdf::EValidatePolicy::Exception)
, PgBuilder(NYql::CreatePgBuilder())
, Accessors(argsTypes, returnType, *PgBuilder)
, RandomProvider(CreateDefaultRandomProvider())
, TimeProvider(CreateDefaultTimeProvider())
, Ctx(HolderFactory, &ValueBuilder, TComputationOptsFull(
nullptr, Alloc.Ref(), TypeEnv, *RandomProvider, *TimeProvider, NUdf::EValidatePolicy::Exception, originalContext.SecureParamsProvider, originalContext.CountersProvider),
originalContext.Mutables, *NYql::NUdf::GetYqlMemoryPool())
{
Alloc.Ref().EnableArrowTracking = false;
Alloc.Release();
}
~TKernelState()
{
Alloc.Acquire();
}
TScopedAlloc Alloc;
TTypeEnvironment TypeEnv;
TMemoryUsageInfo MemInfo;
const IFunctionRegistry::TPtr FunctionRegistry;
THolderFactory HolderFactory;
TDefaultValueBuilder ValueBuilder;
std::unique_ptr<NUdf::IPgBuilder> PgBuilder;
TAccessors Accessors;
TIntrusivePtr<IRandomProvider> RandomProvider;
TIntrusivePtr<ITimeProvider> TimeProvider;
TComputationContext Ctx;
};
class TArrowNode : public IArrowKernelComputationNode {
public:
TArrowNode(const TScalarApplyWrapper* parent, TComputationContext& originalContext)
: Parent_(parent)
, OriginalContext_(originalContext)
, ArgsValuesDescr_(ToValueDescr(parent->ArgsTypes_))
, Kernel_(ConvertToInputTypes(parent->ArgsTypes_), ConvertToOutputType(parent->ReturnType_), [parent](arrow::compute::KernelContext* ctx, const arrow::compute::ExecBatch& batch, arrow::Datum* res) {
auto& state = dynamic_cast<TKernelState&>(*ctx->state());
auto guard = Guard(state.Alloc);
TVector<TDatumProvider> providers;
providers.reserve(batch.values.size());
for (const auto& v : batch.values) {
providers.emplace_back(MakeDatumProvider(v));
}
*res = parent->CalculateImpl(providers, state.Accessors, *NYql::NUdf::GetYqlMemoryPool(), state.Ctx);
return arrow::Status::OK();
})
{
Kernel_.null_handling = arrow::compute::NullHandling::COMPUTED_NO_PREALLOCATE;
Kernel_.mem_allocation = arrow::compute::MemAllocation::NO_PREALLOCATE;
Kernel_.init = [parent, ctx = &OriginalContext_](arrow::compute::KernelContext*, const arrow::compute::KernelInitArgs&) {
auto state = std::make_unique<TKernelState>(parent->ArgsTypes_, parent->ReturnType_, *ctx);
return arrow::Result(std::move(state));
};
}
TStringBuf GetKernelName() const final {
return "ScalarApply";
}
const arrow::compute::ScalarKernel& GetArrowKernel() const {
return Kernel_;
}
const std::vector<arrow::ValueDescr>& GetArgsDesc() const {
return ArgsValuesDescr_;
}
const IComputationNode* GetArgument(ui32 index) const {
return Parent_->Args_[index];
}
private:
const TScalarApplyWrapper* Parent_;
const TComputationContext& OriginalContext_;
const std::vector<arrow::ValueDescr> ArgsValuesDescr_;
arrow::compute::ScalarKernel Kernel_;
};
friend class TArrowNode;
TScalarApplyWrapper(TComputationMutables& mutables, const TVector<TType*>& argsTypes, TType* returnType,
TVector<IComputationNode*>&& args, TVector<IComputationExternalNode*>&& lambdaArgs, IComputationNode* lambdaRoot)
: TMutableComputationNode(mutables)
, StateIndex_(mutables.CurValueIndex++)
, ArgsTypes_(argsTypes)
, ReturnType_(returnType)
, Args_(std::move(args))
, LambdaArgs_(std::move(lambdaArgs))
, LambdaRoot_(lambdaRoot)
{
MKQL_ENSURE(Args_.size() == LambdaArgs_.size(), "Mismatch args count");
}
std::unique_ptr<IArrowKernelComputationNode> PrepareArrowKernelComputationNode(TComputationContext& ctx) const final {
return std::make_unique<TArrowNode>(this, ctx);
}
NUdf::TUnboxedValuePod DoCalculate(TComputationContext& ctx) const {
TVector<TDatumProvider> providers;
providers.reserve(Args_.size());
for (auto arg : Args_) {
providers.emplace_back(MakeDatumProvider(arg, ctx));
}
auto& state = GetState(ctx);
return ctx.HolderFactory.CreateArrowBlock(CalculateImpl(providers, state.Accessors, ctx.ArrowMemoryPool, ctx));
}
arrow::Datum CalculateImpl(const TVector<TDatumProvider>& providers, TAccessors& accessors, arrow::MemoryPool& memoryPool,
TComputationContext& ctx) const {
TVector<arrow::Datum> args;
args.reserve(providers.size());
size_t length = 1;
for (const auto& prov : providers) {
args.emplace_back(prov());
if (!args.back().is_scalar()) {
length = args.back().array()->length;
}
}
auto returnItemType = AS_TYPE(TBlockType, ReturnType_)->GetItemType();
if (AS_TYPE(TBlockType, ReturnType_)->GetShape() == TBlockType::EShape::Scalar) {
if (!accessors.ScalarsProcessed) {
for (ui32 j = 0; j < Args_.size(); ++j) {
if (!LambdaArgs_[j]) {
continue;
}
auto item = accessors.ArgsReaders[j]->GetScalarItem(*args[j].scalar());
auto value = accessors.ArgsConverters[j]->MakeValue(item, ctx.HolderFactory);
LambdaArgs_[j]->SetValue(ctx, value);
}
accessors.ScalarsProcessed = true;
}
auto value = LambdaRoot_->GetValue(ctx);
return ConvertScalar(returnItemType, value, memoryPool);
} else {
auto builder = MakeArrayBuilder(TTypeInfoHelper(), returnItemType, memoryPool, length, &accessors.PgBuilder);
for (size_t i = 0; i < length; ++i) {
for (ui32 j = 0; j < Args_.size(); ++j) {
if (!LambdaArgs_[j]) {
continue;
}
if (args[j].is_scalar() && accessors.ScalarsProcessed) {
continue;
}
auto item = args[j].is_scalar() ?
accessors.ArgsReaders[j]->GetScalarItem(*args[j].scalar()) :
accessors.ArgsReaders[j]->GetItem(*args[j].array(), i);
auto value = accessors.ArgsConverters[j]->MakeValue(item, ctx.HolderFactory);
LambdaArgs_[j]->SetValue(ctx, value);
}
accessors.ScalarsProcessed = true;
auto value = LambdaRoot_->GetValue(ctx);
auto item = accessors.ReturnConverter->MakeItem(value);
builder->Add(item);
}
return builder->Build(true);
}
}
private:
void RegisterDependencies() const final {
for (auto arg : Args_) {
this->DependsOn(arg);
}
for (ui32 i = 0; i < Args_.size(); ++i) {
Args_[i]->AddDependence(LambdaArgs_[i]);
this->Own(LambdaArgs_[i]);
}
this->DependsOn(LambdaRoot_);
}
struct TState : public TComputationValue<TState> {
using TComputationValue::TComputationValue;
TState(TMemoryUsageInfo* memInfo, const TVector<TType*>& argsTypes, TType* returnType, const NUdf::IPgBuilder& pgBuilder)
: TComputationValue(memInfo)
, Accessors(argsTypes, returnType, pgBuilder)
{
}
TAccessors Accessors;
};
TState& GetState(TComputationContext& ctx) const {
auto& result = ctx.MutableValues[StateIndex_];
if (!result.HasValue()) {
result = ctx.HolderFactory.Create<TState>(ArgsTypes_, ReturnType_, ctx.Builder->GetPgBuilder());
}
return *static_cast<TState*>(result.AsBoxed().Get());
}
const ui32 StateIndex_;
const TVector<TType*> ArgsTypes_;
TType* const ReturnType_;
const TVector<IComputationNode*> Args_;
const TVector<IComputationExternalNode*> LambdaArgs_;
IComputationNode* const LambdaRoot_;
};
} // namespace
IComputationNode* WrapScalarApply(TCallable& callable, const TComputationNodeFactoryContext& ctx) {
MKQL_ENSURE((callable.GetInputsCount() >= 3) && (callable.GetInputsCount() % 2 == 1), "Bad args count");
auto lambdaRoot = LocateNode(ctx.NodeLocator, callable, callable.GetInputsCount() - 1);
auto argsCount = (callable.GetInputsCount() - 1) / 2;
TVector<IComputationNode*> args(argsCount);
TVector<IComputationExternalNode*> lambdaArgs(argsCount);
TVector<TType*> argsTypes(argsCount);
for (ui32 i = 0; i < argsCount; ++i) {
args[i] = LocateNode(ctx.NodeLocator, callable, i);
lambdaArgs[i] = LocateExternalNode(ctx.NodeLocator, callable, i + argsCount);
argsTypes[i] = callable.GetType()->GetArgumentType(i);
}
return new TScalarApplyWrapper(ctx.Mutables, argsTypes, callable.GetType()->GetReturnType(),
std::move(args), std::move(lambdaArgs), lambdaRoot);
}
}
}
|