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
|
#pragma once
#include <yql/essentials/public/udf/udf_type_inspection.h>
#include <yql/essentials/public/udf/udf_value_builder.h>
#include <arrow/type.h>
namespace NYql {
namespace NUdf {
template <typename TTraits, typename... TArgs>
std::unique_ptr<typename TTraits::TResult> MakeTupleArrowTraitsImpl(bool isOptional, TVector<std::unique_ptr<typename TTraits::TResult>>&& children, const TType* type, TArgs&&... args) {
if (isOptional) {
if constexpr (TTraits::PassType) {
return std::make_unique<typename TTraits::template TTuple<true>>(std::move(children), type, std::forward<TArgs>(args)...);
} else {
return std::make_unique<typename TTraits::template TTuple<true>>(std::move(children), std::forward<TArgs>(args)...);
}
} else {
if constexpr (TTraits::PassType) {
return std::make_unique<typename TTraits::template TTuple<false>>(std::move(children), type, std::forward<TArgs>(args)...);
} else {
return std::make_unique<typename TTraits::template TTuple<false>>(std::move(children), std::forward<TArgs>(args)...);
}
}
}
template <typename TTraits, typename T, typename... TArgs>
std::unique_ptr<typename TTraits::TResult> MakeFixedSizeArrowTraitsImpl(bool isOptional, const TType* type, TArgs&&... args) {
if (isOptional) {
if constexpr (TTraits::PassType) {
return std::make_unique<typename TTraits::template TFixedSize<T, true>>(type, std::forward<TArgs>(args)...);
} else {
return std::make_unique<typename TTraits::template TFixedSize<T, true>>(std::forward<TArgs>(args)...);
}
} else {
if constexpr (TTraits::PassType) {
return std::make_unique<typename TTraits::template TFixedSize<T, false>>(type, std::forward<TArgs>(args)...);
} else {
return std::make_unique<typename TTraits::template TFixedSize<T, false>>(std::forward<TArgs>(args)...);
}
}
}
template <typename TTraits, typename T, NKikimr::NUdf::EDataSlot TOriginal, typename... TArgs>
std::unique_ptr<typename TTraits::TResult> MakeStringArrowTraitsImpl(bool isOptional, const TType* type, TArgs&&... args) {
if (isOptional) {
if constexpr (TTraits::PassType) {
return std::make_unique<typename TTraits::template TStrings<T, true, TOriginal>>(type, std::forward<TArgs>(args)...);
} else {
return std::make_unique<typename TTraits::template TStrings<T, true, TOriginal>>(std::forward<TArgs>(args)...);
}
} else {
if constexpr (TTraits::PassType) {
return std::make_unique<typename TTraits::template TStrings<T, false, TOriginal>>(type, std::forward<TArgs>(args)...);
} else {
return std::make_unique<typename TTraits::template TStrings<T, false, TOriginal>>(std::forward<TArgs>(args)...);
}
}
}
template <typename TTraits, typename TTzDate, typename... TArgs>
std::unique_ptr<typename TTraits::TResult> MakeTzDateArrowTraitsImpl(bool isOptional, const TType* type, TArgs&&... args) {
if constexpr (TTraits::PassType) {
return TTraits::template MakeTzDate<TTzDate>(isOptional, type, std::forward<TArgs>(args)...);
} else {
return TTraits::template MakeTzDate<TTzDate>(isOptional, std::forward<TArgs>(args)...);
}
}
template<typename TTraits>
concept CanInstantiateArrowTraitsForDecimal = requires {
typename TTraits::template TFixedSize<NYql::NDecimal::TInt128, true>;
};
template <typename TTraits, typename... TArgs>
std::unique_ptr<typename TTraits::TResult> DispatchByArrowTraits(const ITypeInfoHelper& typeInfoHelper, const TType* type, const IPgBuilder* pgBuilder, TArgs&&... args) {
const TType* unpacked = type;
TOptionalTypeInspector typeOpt(typeInfoHelper, type);
bool isOptional = false;
if (typeOpt) {
unpacked = typeOpt.GetItemType();
isOptional = true;
}
TOptionalTypeInspector unpackedOpt(typeInfoHelper, unpacked);
TPgTypeInspector unpackedPg(typeInfoHelper, unpacked);
if (unpackedOpt || typeOpt && unpackedPg) {
// at least 2 levels of optionals
ui32 nestLevel = 0;
auto currentType = type;
auto previousType = type;
TVector<const TType*> types;
for (;;) {
++nestLevel;
previousType = currentType;
types.push_back(currentType);
TOptionalTypeInspector currentOpt(typeInfoHelper, currentType);
currentType = currentOpt.GetItemType();
TOptionalTypeInspector nexOpt(typeInfoHelper, currentType);
if (!nexOpt) {
break;
}
}
if (TPgTypeInspector(typeInfoHelper, currentType)) {
previousType = currentType;
++nestLevel;
}
auto reader = DispatchByArrowTraits<TTraits>(typeInfoHelper, previousType, pgBuilder, std::forward<TArgs>(args)...);
for (ui32 i = 1; i < nestLevel; ++i) {
if constexpr (TTraits::PassType) {
reader = std::make_unique<typename TTraits::TExtOptional>(std::move(reader), types[nestLevel - 1 - i], std::forward<TArgs>(args)...);
} else {
reader = std::make_unique<typename TTraits::TExtOptional>(std::move(reader), std::forward<TArgs>(args)...);
}
}
return reader;
}
else {
type = unpacked;
}
TStructTypeInspector typeStruct(typeInfoHelper, type);
if (typeStruct) {
TVector<std::unique_ptr<typename TTraits::TResult>> members;
for (ui32 i = 0; i < typeStruct.GetMembersCount(); i++) {
members.emplace_back(DispatchByArrowTraits<TTraits>(typeInfoHelper, typeStruct.GetMemberType(i), pgBuilder, std::forward<TArgs>(args)...));
}
// XXX: Use Tuple block reader for Struct.
return MakeTupleArrowTraitsImpl<TTraits>(isOptional, std::move(members), type, std::forward<TArgs>(args)...);
}
TTupleTypeInspector typeTuple(typeInfoHelper, type);
if (typeTuple) {
TVector<std::unique_ptr<typename TTraits::TResult>> children;
for (ui32 i = 0; i < typeTuple.GetElementsCount(); ++i) {
children.emplace_back(DispatchByArrowTraits<TTraits>(typeInfoHelper, typeTuple.GetElementType(i), pgBuilder, std::forward<TArgs>(args)...));
}
return MakeTupleArrowTraitsImpl<TTraits>(isOptional, std::move(children), type, std::forward<TArgs>(args)...);
}
TDataTypeInspector typeData(typeInfoHelper, type);
if (typeData) {
auto typeId = typeData.GetTypeId();
switch (GetDataSlot(typeId)) {
case NUdf::EDataSlot::Int8:
return MakeFixedSizeArrowTraitsImpl<TTraits, i8>(isOptional, type, std::forward<TArgs>(args)...);
case NUdf::EDataSlot::Bool:
case NUdf::EDataSlot::Uint8:
return MakeFixedSizeArrowTraitsImpl<TTraits, ui8>(isOptional, type, std::forward<TArgs>(args)...);
case NUdf::EDataSlot::Int16:
return MakeFixedSizeArrowTraitsImpl<TTraits, i16>(isOptional, type, std::forward<TArgs>(args)...);
case NUdf::EDataSlot::Uint16:
case NUdf::EDataSlot::Date:
return MakeFixedSizeArrowTraitsImpl<TTraits, ui16>(isOptional, type, std::forward<TArgs>(args)...);
case NUdf::EDataSlot::Int32:
case NUdf::EDataSlot::Date32:
return MakeFixedSizeArrowTraitsImpl<TTraits, i32>(isOptional, type, std::forward<TArgs>(args)...);
case NUdf::EDataSlot::Uint32:
case NUdf::EDataSlot::Datetime:
return MakeFixedSizeArrowTraitsImpl<TTraits, ui32>(isOptional, type, std::forward<TArgs>(args)...);
case NUdf::EDataSlot::Int64:
case NUdf::EDataSlot::Interval:
case NUdf::EDataSlot::Interval64:
case NUdf::EDataSlot::Datetime64:
case NUdf::EDataSlot::Timestamp64:
return MakeFixedSizeArrowTraitsImpl<TTraits, i64>(isOptional, type, std::forward<TArgs>(args)...);
case NUdf::EDataSlot::Uint64:
case NUdf::EDataSlot::Timestamp:
return MakeFixedSizeArrowTraitsImpl<TTraits, ui64>(isOptional, type, std::forward<TArgs>(args)...);
case NUdf::EDataSlot::Float:
return MakeFixedSizeArrowTraitsImpl<TTraits, float>(isOptional, type, std::forward<TArgs>(args)...);
case NUdf::EDataSlot::Double:
return MakeFixedSizeArrowTraitsImpl<TTraits, double>(isOptional, type, std::forward<TArgs>(args)...);
case NUdf::EDataSlot::String:
return MakeStringArrowTraitsImpl<TTraits, arrow::BinaryType, NUdf::EDataSlot::String>(isOptional, type, std::forward<TArgs>(args)...);
case NUdf::EDataSlot::Yson:
return MakeStringArrowTraitsImpl<TTraits, arrow::BinaryType, NUdf::EDataSlot::Yson>(isOptional, type, std::forward<TArgs>(args)...);
case NUdf::EDataSlot::JsonDocument:
return MakeStringArrowTraitsImpl<TTraits, arrow::BinaryType, NUdf::EDataSlot::JsonDocument>(isOptional, type, std::forward<TArgs>(args)...);
case NUdf::EDataSlot::Utf8:
return MakeStringArrowTraitsImpl<TTraits, arrow::StringType, NUdf::EDataSlot::Utf8>(isOptional, type, std::forward<TArgs>(args)...);
case NUdf::EDataSlot::Json:
return MakeStringArrowTraitsImpl<TTraits, arrow::StringType, NUdf::EDataSlot::Json>(isOptional, type, std::forward<TArgs>(args)...);
case NUdf::EDataSlot::TzDate:
return MakeTzDateArrowTraitsImpl<TTraits, TTzDate>(isOptional, type, std::forward<TArgs>(args)...);
case NUdf::EDataSlot::TzDatetime:
return MakeTzDateArrowTraitsImpl<TTraits, TTzDatetime>(isOptional, type, std::forward<TArgs>(args)...);
case NUdf::EDataSlot::TzTimestamp:
return MakeTzDateArrowTraitsImpl<TTraits, TTzTimestamp>(isOptional, type, std::forward<TArgs>(args)...);
case NUdf::EDataSlot::TzDate32:
return MakeTzDateArrowTraitsImpl<TTraits, TTzDate32>(isOptional, type, std::forward<TArgs>(args)...);
case NUdf::EDataSlot::TzDatetime64:
return MakeTzDateArrowTraitsImpl<TTraits, TTzDatetime64>(isOptional, type, std::forward<TArgs>(args)...);
case NUdf::EDataSlot::TzTimestamp64:
return MakeTzDateArrowTraitsImpl<TTraits, TTzTimestamp64>(isOptional, type, std::forward<TArgs>(args)...);
case NUdf::EDataSlot::Decimal: {
if constexpr (CanInstantiateArrowTraitsForDecimal<TTraits>) {
return MakeFixedSizeArrowTraitsImpl<TTraits, NYql::NDecimal::TInt128>(isOptional, type, std::forward<TArgs>(args)...);
} else {
Y_ENSURE(false, "Unsupported data slot");
}
}
case NUdf::EDataSlot::Uuid:
case NUdf::EDataSlot::DyNumber:
Y_ENSURE(false, "Unsupported data slot");
}
}
TResourceTypeInspector resource(typeInfoHelper, type);
if (resource) {
if constexpr (TTraits::PassType) {
return TTraits::MakeResource(isOptional, type, std::forward<TArgs>(args)...);
} else {
return TTraits::MakeResource(isOptional, std::forward<TArgs>(args)...);
}
}
TPgTypeInspector typePg(typeInfoHelper, type);
if (typePg) {
auto desc = typeInfoHelper.FindPgTypeDescription(typePg.GetTypeId());
if constexpr (TTraits::PassType) {
return TTraits::MakePg(*desc, pgBuilder, type, std::forward<TArgs>(args)...);
} else {
return TTraits::MakePg(*desc, pgBuilder, std::forward<TArgs>(args)...);
}
}
Y_ENSURE(false, "Unsupported type");
}
}
}
|