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
|
#include "mkql_value_builder.h"
#include "mkql_validate.h"
#include <yql/essentials/minikql/mkql_node_cast.h>
#include <yql/essentials/minikql/mkql_string_util.h>
#include <yql/essentials/minikql/mkql_type_builder.h>
#include <yql/essentials/parser/pg_wrapper/interface/utils.h>
#include <library/cpp/yson/node/node_io.h>
#include <arrow/chunked_array.h>
#include <arrow/array/array_base.h>
#include <arrow/array/util.h>
#include <arrow/c/bridge.h>
#include <util/system/env.h>
namespace NKikimr {
namespace NMiniKQL {
///////////////////////////////////////////////////////////////////////////////
// TDefaultValueBuilder
///////////////////////////////////////////////////////////////////////////////
TDefaultValueBuilder::TDefaultValueBuilder(const THolderFactory& holderFactory, NUdf::EValidatePolicy policy)
: HolderFactory_(holderFactory)
, Policy_(policy)
, PgBuilder_(NYql::CreatePgBuilder())
{}
void TDefaultValueBuilder::SetSecureParamsProvider(const NUdf::ISecureParamsProvider* provider) {
SecureParamsProvider_ = provider;
}
void TDefaultValueBuilder::RethrowAtTerminate() {
Rethrow_ = true;
}
void TDefaultValueBuilder::SetCalleePositionHolder(const NUdf::TSourcePosition*& position) {
CalleePositionPtr_ = &position;
}
void TDefaultValueBuilder::Terminate(const char* message) const {
TStringBuf reason = (message ? TStringBuf(message) : TStringBuf("(unknown)"));
TString fullMessage = TStringBuilder() <<
"Terminate was called, reason(" << reason.size() << "): " << reason << Endl;
HolderFactory_.CleanupModulesOnTerminate();
if (Policy_ == NUdf::EValidatePolicy::Exception) {
if (Rethrow_ && std::current_exception()) {
throw;
}
Rethrow_ = true;
ythrow yexception() << fullMessage;
}
Cerr << fullMessage << Flush;
_exit(1);
}
NUdf::TUnboxedValue TDefaultValueBuilder::NewStringNotFilled(ui32 size) const
{
return MakeStringNotFilled(size);
}
NUdf::TUnboxedValue TDefaultValueBuilder::NewString(const NUdf::TStringRef& ref) const
{
return MakeString(ref);
}
NUdf::TUnboxedValue TDefaultValueBuilder::ConcatStrings(NUdf::TUnboxedValuePod first, NUdf::TUnboxedValuePod second) const
{
return ::NKikimr::NMiniKQL::ConcatStrings(first, second);
}
NUdf::TUnboxedValue TDefaultValueBuilder::AppendString(NUdf::TUnboxedValuePod value, const NUdf::TStringRef& ref) const
{
return ::NKikimr::NMiniKQL::AppendString(value, ref);
}
NUdf::TUnboxedValue TDefaultValueBuilder::PrependString(const NUdf::TStringRef& ref, NUdf::TUnboxedValuePod value) const
{
return ::NKikimr::NMiniKQL::PrependString(ref, value);
}
NUdf::TUnboxedValue TDefaultValueBuilder::SubString(NUdf::TUnboxedValuePod value, ui32 offset, ui32 size) const
{
return ::NKikimr::NMiniKQL::SubString(value, offset, size);
}
NUdf::TUnboxedValue TDefaultValueBuilder::NewList(NUdf::TUnboxedValue* items, ui64 count) const {
if (items == nullptr || count == 0) {
return HolderFactory_.GetEmptyContainerLazy();
}
NUdf::TUnboxedValue* inplace = nullptr;
auto array = HolderFactory_.CreateDirectArrayHolder(count, inplace);
std::copy_n(std::make_move_iterator(items), count, inplace);
return array;
}
NUdf::TUnboxedValue TDefaultValueBuilder::ReverseList(const NUdf::TUnboxedValuePod& list) const
{
return HolderFactory_.ReverseList(this, list);
}
NUdf::TUnboxedValue TDefaultValueBuilder::SkipList(const NUdf::TUnboxedValuePod& list, ui64 count) const
{
return HolderFactory_.SkipList(this, list, count);
}
NUdf::TUnboxedValue TDefaultValueBuilder::TakeList(const NUdf::TUnboxedValuePod& list, ui64 count) const
{
return HolderFactory_.TakeList(this, list, count);
}
NUdf::TUnboxedValue TDefaultValueBuilder::ToIndexDict(const NUdf::TUnboxedValuePod& list) const
{
return HolderFactory_.ToIndexDict(this, list);
}
NUdf::TUnboxedValue TDefaultValueBuilder::NewArray32(ui32 count, NUdf::TUnboxedValue*& itemsPtr) const {
return HolderFactory_.CreateDirectArrayHolder(count, itemsPtr);
}
NUdf::TUnboxedValue TDefaultValueBuilder::NewArray64(ui64 count, NUdf::TUnboxedValue*& itemsPtr) const {
return HolderFactory_.CreateDirectArrayHolder(count, itemsPtr);
}
NUdf::TUnboxedValue TDefaultValueBuilder::NewVariant(ui32 index, NUdf::TUnboxedValue&& value) const {
return HolderFactory_.CreateVariantHolder(value.Release(), index);
}
NUdf::IDictValueBuilder::TPtr TDefaultValueBuilder::NewDict(const NUdf::TType* dictType, ui32 flags) const
{
return HolderFactory_.NewDict(dictType, flags);
}
bool TDefaultValueBuilder::MakeDate(ui32 year, ui32 month, ui32 day, ui16& value) const {
return ::NKikimr::NMiniKQL::MakeDate(year, month, day, value);
}
bool TDefaultValueBuilder::SplitDate(ui16 value, ui32& year, ui32& month, ui32& day) const {
return ::NKikimr::NMiniKQL::SplitDate(value, year, month, day);
}
bool TDefaultValueBuilder::MakeDatetime(ui32 year, ui32 month, ui32 day, ui32 hour, ui32 minute, ui32 second, ui32& value, ui16 tzId) const
{
return ::NKikimr::NMiniKQL::MakeTzDatetime(year, month, day, hour, minute, second, value, tzId);
}
bool TDefaultValueBuilder::SplitDatetime(ui32 value, ui32& year, ui32& month, ui32& day, ui32& hour, ui32& minute, ui32& second, ui16 tzId) const
{
return ::NKikimr::NMiniKQL::SplitTzDatetime(value, year, month, day, hour, minute, second, tzId);
}
bool TDefaultValueBuilder::FullSplitDate(ui16 value, ui32& year, ui32& month, ui32& day,
ui32& dayOfYear, ui32& weekOfYear, ui32& dayOfWeek, ui16 tzId) const {
ui32 unusedWeekOfYearIso8601 = 0;
return ::NKikimr::NMiniKQL::SplitTzDate(value, year, month, day, dayOfYear, weekOfYear, unusedWeekOfYearIso8601, dayOfWeek, tzId);
}
bool TDefaultValueBuilder::FullSplitDate2(ui16 value, ui32& year, ui32& month, ui32& day,
ui32& dayOfYear, ui32& weekOfYear, ui32& weekOfYearIso8601, ui32& dayOfWeek, ui16 tzId) const {
return ::NKikimr::NMiniKQL::SplitTzDate(value, year, month, day, dayOfYear, weekOfYear, weekOfYearIso8601, dayOfWeek, tzId);
}
bool TDefaultValueBuilder::FullSplitDatetime(ui32 value, ui32& year, ui32& month, ui32& day, ui32& hour, ui32& minute, ui32& second,
ui32& dayOfYear, ui32& weekOfYear, ui32& dayOfWeek, ui16 tzId) const {
ui32 unusedWeekOfYearIso8601 = 0;
return ::NKikimr::NMiniKQL::SplitTzDatetime(value, year, month, day, hour, minute, second, dayOfYear, weekOfYear, unusedWeekOfYearIso8601, dayOfWeek, tzId);
}
bool TDefaultValueBuilder::FullSplitDatetime2(ui32 value, ui32& year, ui32& month, ui32& day, ui32& hour, ui32& minute, ui32& second,
ui32& dayOfYear, ui32& weekOfYear, ui32& weekOfYearIso8601, ui32& dayOfWeek, ui16 tzId) const {
return ::NKikimr::NMiniKQL::SplitTzDatetime(value, year, month, day, hour, minute, second, dayOfYear, weekOfYear, weekOfYearIso8601, dayOfWeek, tzId);
}
bool TDefaultValueBuilder::EnrichDate(ui16 date, ui32& dayOfYear, ui32& weekOfYear, ui32& dayOfWeek) const {
ui32 unusedWeekOfYearIso8601 = 0;
return ::NKikimr::NMiniKQL::EnrichDate(date, dayOfYear, weekOfYear, unusedWeekOfYearIso8601, dayOfWeek);
}
bool TDefaultValueBuilder::EnrichDate2(ui16 date, ui32& dayOfYear, ui32& weekOfYear, ui32& weekOfYearIso8601, ui32& dayOfWeek) const {
return ::NKikimr::NMiniKQL::EnrichDate(date, dayOfYear, weekOfYear, weekOfYearIso8601, dayOfWeek);
}
bool TDefaultValueBuilder::GetTimezoneShift(ui32 year, ui32 month, ui32 day, ui32 hour, ui32 minute, ui32 second, ui16 tzId, i32& value) const
{
return ::NKikimr::NMiniKQL::GetTimezoneShift(year, month, day, hour, minute, second, tzId, value);
}
const NUdf::TSourcePosition* TDefaultValueBuilder::CalleePosition() const {
return *CalleePositionPtr_;
}
NUdf::TUnboxedValue TDefaultValueBuilder::Run(const NUdf::TSourcePosition& callee, const NUdf::IBoxedValue& value, const NUdf::TUnboxedValuePod* args) const {
const auto prev = *CalleePositionPtr_;
*CalleePositionPtr_ = &callee;
const auto ret = NUdf::TBoxedValueAccessor::Run(value, this, args);
*CalleePositionPtr_ = prev;
return ret;
}
void TDefaultValueBuilder::ExportArrowBlock(NUdf::TUnboxedValuePod value, ui32 chunk, ArrowArray* out) const {
const auto& datum = TArrowBlock::From(value).GetDatum();
std::shared_ptr<arrow::Array> arr;
if (datum.is_scalar()) {
if (chunk != 0) {
UdfTerminate("Bad chunk index");
}
auto arrRes = arrow::MakeArrayFromScalar(*datum.scalar(), 1);
if (!arrRes.status().ok()) {
UdfTerminate(arrRes.status().ToString().c_str());
}
arr = std::move(arrRes).ValueOrDie();
} else if (datum.is_array()) {
if (chunk != 0) {
UdfTerminate("Bad chunk index");
}
arr = datum.make_array();
} else if (datum.is_arraylike()) {
const auto& chunks = datum.chunks();
if (chunk >= chunks.size()) {
UdfTerminate("Bad chunk index");
}
arr = chunks[chunk];
} else {
UdfTerminate("Unexpected kind of arrow::Datum");
}
auto status = arrow::ExportArray(*arr, out);
if (!status.ok()) {
UdfTerminate(status.ToString().c_str());
}
}
NUdf::TUnboxedValue TDefaultValueBuilder::ImportArrowBlock(ArrowArray* arrays, ui32 chunkCount, bool isScalar, const NUdf::IArrowType& type) const {
const auto dataType = static_cast<const TArrowType&>(type).GetType();
if (isScalar) {
if (chunkCount != 1) {
UdfTerminate("Bad chunkCount value");
}
auto arrRes = arrow::ImportArray(arrays, dataType);
auto arr = std::move(arrRes).ValueOrDie();
if (arr->length() != 1) {
UdfTerminate("Expected array with one element");
}
auto scalarRes = arr->GetScalar(0);
if (!scalarRes.status().ok()) {
UdfTerminate(scalarRes.status().ToString().c_str());
}
auto scalar = std::move(scalarRes).ValueOrDie();
return HolderFactory_.CreateArrowBlock(std::move(scalar));
} else {
if (chunkCount < 1) {
UdfTerminate("Bad chunkCount value");
}
TVector<std::shared_ptr<arrow::Array>> imported(chunkCount);
for (ui32 i = 0; i < chunkCount; ++i) {
auto arrRes = arrow::ImportArray(arrays + i, dataType);
if (!arrRes.status().ok()) {
UdfTerminate(arrRes.status().ToString().c_str());
}
imported[i] = std::move(arrRes).ValueOrDie();
}
if (chunkCount == 1) {
return HolderFactory_.CreateArrowBlock(imported.front());
} else {
return HolderFactory_.CreateArrowBlock(arrow::ChunkedArray::Make(std::move(imported), dataType).ValueOrDie());
}
}
}
ui32 TDefaultValueBuilder::GetArrowBlockChunks(NUdf::TUnboxedValuePod value, bool& isScalar, ui64& length) const {
const auto& datum = TArrowBlock::From(value).GetDatum();
isScalar = false;
length = datum.length();
if (datum.is_scalar()) {
isScalar = true;
return 1;
} else if (datum.is_array()) {
return 1;
} else if (datum.is_arraylike()) {
return datum.chunks().size();
} else {
UdfTerminate("Unexpected kind of arrow::Datum");
}
}
bool TDefaultValueBuilder::FindTimezoneName(ui32 id, NUdf::TStringRef& name) const {
auto res = ::NKikimr::NMiniKQL::FindTimezoneIANAName(id);
if (!res) {
return false;
}
name = *res;
return true;
}
bool TDefaultValueBuilder::FindTimezoneId(const NUdf::TStringRef& name, ui32& id) const {
auto res = ::NKikimr::NMiniKQL::FindTimezoneId(name);
if (!res) {
return false;
}
id = *res;
return true;
}
bool TDefaultValueBuilder::GetSecureParam(NUdf::TStringRef key, NUdf::TStringRef& value) const {
if (SecureParamsProvider_)
return SecureParamsProvider_->GetSecureParam(key, value);
return false;
}
bool TDefaultValueBuilder::SplitTzDate32(i32 date, i32& year, ui32& month, ui32& day,
ui32& dayOfYear, ui32& weekOfYear, ui32& weekOfYearIso8601, ui32& dayOfWeek, ui16 timezoneId) const
{
return ::NKikimr::NMiniKQL::SplitTzDate32(date, year, month, day, dayOfYear, weekOfYear, weekOfYearIso8601, dayOfWeek, timezoneId);
}
bool TDefaultValueBuilder::SplitTzDatetime64(i64 datetime, i32& year, ui32& month, ui32& day,
ui32& hour, ui32& minute, ui32& second,
ui32& dayOfYear, ui32& weekOfYear, ui32& weekOfYearIso8601, ui32& dayOfWeek, ui16 timezoneId) const
{
return ::NKikimr::NMiniKQL::SplitTzDatetime64(
datetime, year, month, day, hour, minute, second,
dayOfYear, weekOfYear, weekOfYearIso8601, dayOfWeek, timezoneId);
}
bool TDefaultValueBuilder::MakeTzDate32(i32 year, ui32 month, ui32 day, i32& date, ui16 timezoneId) const {
return ::NKikimr::NMiniKQL::MakeTzDate32(year, month, day, date, timezoneId);
}
bool TDefaultValueBuilder::MakeTzDatetime64(i32 year, ui32 month, ui32 day,
ui32 hour, ui32 minute, ui32 second, i64& datetime, ui16 timezoneId) const
{
return ::NKikimr::NMiniKQL::MakeTzDatetime64(year, month, day, hour, minute, second, datetime, timezoneId);
}
NUdf::IListValueBuilder::TPtr TDefaultValueBuilder::NewListBuilder() const {
return HolderFactory_.NewList();
}
} // namespace NMiniKQL
} // namespace Nkikimr
|