aboutsummaryrefslogtreecommitdiffstats
path: root/ydb/library/yql/minikql/mkql_node_builder.cpp
blob: fff28a7143086e216d2bde1cb52171d66ddb8057 (plain) (blame)
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
#include "mkql_node_builder.h"
#include "mkql_node_printer.h"

#include <util/generic/algorithm.h>

namespace NKikimr {
namespace NMiniKQL {

using namespace NDetail;

namespace {
    TNode* BuildCallableTypePayload(const TVector<TStringBuf>& argNames, 
        const TVector<ui64>& argFlags, TStringBuf payload, const TTypeEnvironment& env) { 
        TStructTypeBuilder itemTypeBuilder(env);
        itemTypeBuilder.Add("Name", TDataType::Create(NUdf::TDataType<char*>::Id, env));
        itemTypeBuilder.Add("Flags", TDataType::Create(NUdf::TDataType<ui64>::Id, env));
        TType* itemType = itemTypeBuilder.Build();

        bool startedNames = false;
        THashSet<TStringBuf> usedNames; 
        TListLiteralBuilder argsListBuilder(env, itemType);
        for (ui32 i = 0; i < argNames.size(); ++i) {
            TStructLiteralBuilder itemBuilder(env);
            auto name = argNames[i];
            auto flags = argFlags[i];
            bool hasName = name.size() > 0;
            if (startedNames) {
                MKQL_ENSURE(hasName, "Named arguments already started");
            } else {
                startedNames = hasName;
            }

            if (hasName) {
                MKQL_ENSURE(usedNames.insert(name).second, "Duplication of argument name: " << name);
            }

            itemBuilder.Add("Name", TRuntimeNode(BuildDataLiteral(name, NUdf::EDataSlot::String, env), true));
            itemBuilder.Add("Flags", TRuntimeNode(BuildDataLiteral(NUdf::TUnboxedValuePod(flags), NUdf::EDataSlot::Uint64, env), true));
            argsListBuilder.Add(TRuntimeNode(itemBuilder.Build(), true));
        }

        TRuntimeNode argsList = TRuntimeNode(argsListBuilder.Build(), true);
        TStructLiteralBuilder payloadBuilder(env);
        payloadBuilder.Add("Args", argsList);
        payloadBuilder.Add("Payload", TRuntimeNode(BuildDataLiteral(payload, NUdf::EDataSlot::String, env), true));
        return payloadBuilder.Build();
    }
}

TDataLiteral* BuildDataLiteral(const NUdf::TUnboxedValuePod& value, NUdf::TDataTypeId type, const TTypeEnvironment& env) {
    return TDataLiteral::Create(value, TDataType::Create(type, env), env);
}

TDataLiteral* BuildDataLiteral(const NUdf::TStringRef& data, NUdf::TDataTypeId type, const TTypeEnvironment& env) {
    return BuildDataLiteral(env.NewStringValue(data), type, env);
}

TOptionalLiteral* BuildOptionalLiteral(TRuntimeNode value, const TTypeEnvironment& env) {
    auto type = TOptionalType::Create(value.GetStaticType(), env);
    return TOptionalLiteral::Create(value, type, env);
}

TOptionalLiteral* BuildEmptyOptionalLiteral(TType *itemType, const TTypeEnvironment& env) {
    auto type = TOptionalType::Create(itemType, env);
    return TOptionalLiteral::Create(type, env);
}

TOptionalLiteral* BuildEmptyOptionalDataLiteral(NUdf::TDataTypeId schemeType, const TTypeEnvironment& env) {
    return BuildEmptyOptionalLiteral(TDataType::Create(schemeType, env), env);
}

TType* UnpackOptional(TRuntimeNode data, bool& isOptional) {
    return UnpackOptional(data.GetStaticType(), isOptional);
}

TType* UnpackOptional(TType* type, bool& isOptional) {
    isOptional = type->IsOptional();
    if (!isOptional)
        return type;

    auto optType = static_cast<TOptionalType*>(type);
    return optType->GetItemType();
}

TDataType* UnpackOptionalData(TRuntimeNode data, bool& isOptional) {
    return UnpackOptionalData(data.GetStaticType(), isOptional);
}

TDataType* UnpackOptionalData(TType* type, bool& isOptional) {
    auto unpackedType = UnpackOptional(type, isOptional);
    MKQL_ENSURE(unpackedType->IsData(),
        "Expected data or optional of data, actual: " << PrintNode(type, true));

    return static_cast<TDataType*>(unpackedType);
}

TStructTypeBuilder::TStructTypeBuilder(const TTypeEnvironment& env)
    : Env(&env)
{
}

void TStructTypeBuilder::Reserve(ui32 size) {
    Members.reserve(size);
}

TStructTypeBuilder& TStructTypeBuilder::Add(const TStringBuf& name, TType* type, ui32* index) {
    Members.push_back(TStructMember(Env->InternName(name).Str(), type, index));
    return *this;
}

TStructType* TStructTypeBuilder::Build() {
    if (Members.empty())
        return Env->GetEmptyStruct()->GetType();

    Sort(Members.begin(), Members.end());
    return TStructType::Create(Members.size(), Members.data(), *Env);
}

void TStructTypeBuilder::FillIndexes() {
    ui32 index = 0;
    for (const TStructMember& member: Members) {
        if (member.Index) {
            *(member.Index) = index++;
        }
    }
}

void TStructTypeBuilder::Clear() {
    Members.clear();
}

TStructLiteralBuilder::TStructLiteralBuilder(const TTypeEnvironment& env)
    : Env(&env)
{
}

void TStructLiteralBuilder::Reserve(ui32 size) {
    Members.reserve(size);
    Values.reserve(size);
}

TStructLiteralBuilder& TStructLiteralBuilder::Add(const TStringBuf& name, TRuntimeNode value) {
    TType* valueType = value.GetStaticType();
    Members.push_back(TStructMember(Env->InternName(name).Str(), valueType));
    Values.push_back(value);
    return *this;
}

TStructLiteral* TStructLiteralBuilder::Build() {
    Y_VERIFY_DEBUG(Members.size() == Values.size());
    if (Members.empty())
        return Env->GetEmptyStruct();

    TVector<std::pair<TStringBuf, ui32>> sortedIndicies(Members.size()); 
    for (ui32 i = 0, e = Members.size(); i < e; ++i) {
        sortedIndicies[i] = std::make_pair(Members[i].Name, i);
    }

    Sort(sortedIndicies.begin(), sortedIndicies.end(),
        [](const std::pair<TStringBuf, ui32>& x, const std::pair<TStringBuf, ui32>& y) {
            return x.first < y.first;
    });

    TVector<TStructMember> sortedMembers(Members.size()); 
    TVector<TRuntimeNode> sortedValues(Members.size()); 

    for (ui32 i = 0, e = Members.size(); i < e; ++i) {
        sortedMembers[i] = Members[sortedIndicies[i].second];
        sortedValues[i] = Values[sortedIndicies[i].second];
    }

    auto type = TStructType::Create(sortedMembers.size(), sortedMembers.data(), *Env);
    return TStructLiteral::Create(sortedValues.size(), sortedValues.data(), type, *Env);
}

void TStructLiteralBuilder::Clear() {
    Members.clear();
}

TListLiteralBuilder::TListLiteralBuilder(const TTypeEnvironment& env, TType* type)
    : Env(&env)
    , Type(type)
{}

TListLiteralBuilder& TListLiteralBuilder::Add(TRuntimeNode item) {
    Items.push_back(item);
    return *this;
}

TListLiteral* TListLiteralBuilder::Build() {
    auto type = TListType::Create(Type, *Env);
    return TListLiteral::Create(Items.data(), Items.size(), type, *Env);
}

void TListLiteralBuilder::Clear() {
    Items.clear();
}

TDictLiteralBuilder::TDictLiteralBuilder(const TTypeEnvironment& env, TType* keyType, TType* payloadType)
    : Env(&env)
    , KeyType(keyType)
    , PayloadType(payloadType)
{
}

void TDictLiteralBuilder::Reserve(ui32 size) {
    Items.reserve(size);
}

TDictLiteralBuilder& TDictLiteralBuilder::Add(TRuntimeNode key, TRuntimeNode payload) {
    Items.push_back(std::make_pair(key, payload));
    return *this;
}

TDictLiteral* TDictLiteralBuilder::Build() {
    auto type = TDictType::Create(KeyType, PayloadType, *Env);
    return TDictLiteral::Create(Items.size(), Items.data(), type, *Env);
}

void TDictLiteralBuilder::Clear() {
    Items.clear();
}

TCallableTypeBuilder::TCallableTypeBuilder(const TTypeEnvironment& env, const TStringBuf& name, TType* returnType)
    : Env(&env)
    , Name(Env->InternName(name))
    , ReturnType(returnType)
    , OptionalArgsCount(0)
    , HasPayload(false)
{}

void TCallableTypeBuilder::Reserve(ui32 size) {
    Arguments.reserve(size);
    ArgNames.reserve(size);
    ArgFlags.reserve(size);
}

TCallableTypeBuilder& TCallableTypeBuilder::Add(TType *type) {
    Arguments.push_back(type);
    ArgNames.emplace_back();
    ArgFlags.emplace_back();
    return *this;
}

TCallableTypeBuilder& TCallableTypeBuilder::SetArgumentName(const TStringBuf& name) {
    MKQL_ENSURE(!ArgNames.empty(), "No arguments");
    HasPayload = true;
    ArgNames.back() = name;
    return *this;
}

TCallableTypeBuilder& TCallableTypeBuilder::SetArgumentFlags(ui64 flags) {
    MKQL_ENSURE(!ArgFlags.empty(), "No arguments");
    HasPayload = true;
    ArgFlags.back() = flags;
    return *this;
}

TCallableTypeBuilder& TCallableTypeBuilder::SetOptionalArgs(ui32 count) {
    OptionalArgsCount = count;
    return *this;
}

TCallableTypeBuilder& TCallableTypeBuilder::SetPayload(const TStringBuf& data) {
    HasPayload = true;
    FuncPayload = data;
    return *this;
}

TCallableType* TCallableTypeBuilder::Build() {
    TNode* payload = nullptr;
    if (HasPayload) {
        payload = BuildCallableTypePayload(ArgNames, ArgFlags, FuncPayload, *Env);
    }

    auto ret = TCallableType::Create(ReturnType, Name.Str(), Arguments.size(), Arguments.data(), payload, *Env);
    ret->SetOptionalArgumentsCount(OptionalArgsCount);
    return ret;
}

void TCallableTypeBuilder::Clear() {
    Arguments.clear();
    ArgNames.clear();
    ArgFlags.clear();
    OptionalArgsCount = 0;
    HasPayload = false;
    FuncPayload = TStringBuf();
}

TCallableBuilder::TCallableBuilder(const TTypeEnvironment& env, const TStringBuf& name, TType* returnType, bool disableMerge)
    : Env(&env)
    , Name(Env->InternName(name))
    , ReturnType(returnType)
    , DisableMerge(disableMerge)
    , OptionalArgsCount(0)
    , HasPayload(false)
{}

void TCallableBuilder::Reserve(ui32 size) {
    Arguments.reserve(size);
    Inputs.reserve(size);
    ArgNames.reserve(size);
    ArgFlags.reserve(size);
}

TCallableBuilder& TCallableBuilder::Add(TRuntimeNode input) {
    TType* inputType = input.GetStaticType();
    Arguments.push_back(inputType);
    Inputs.push_back(input);
    ArgNames.emplace_back();
    ArgFlags.emplace_back();
    return *this;
}

TCallableBuilder& TCallableBuilder::SetOptionalArgs(ui32 count) {
    OptionalArgsCount = count;
    return *this;
}

TCallableBuilder& TCallableBuilder::SetTypePayload(const TStringBuf& data) {
    HasPayload = true;
    FuncPayload = data;
    return *this;
}

TCallableBuilder& TCallableBuilder::SetArgumentName(const TStringBuf& name) {
    MKQL_ENSURE(!ArgNames.empty(), "No arguments");
    HasPayload = true;
    ArgNames.back() = name;
    return *this;
}

TCallableBuilder& TCallableBuilder::SetArgumentFlags(ui64 flags) {
    MKQL_ENSURE(!ArgFlags.empty(), "No arguments");
    HasPayload = true;
    ArgFlags.back() = flags;
    return *this;
}

TCallable* TCallableBuilder::Build() {
    TNode* payload = nullptr;
    if (HasPayload) {
        payload = BuildCallableTypePayload(ArgNames, ArgFlags, FuncPayload, *Env);
    }

    auto type = TCallableType::Create(ReturnType, Name.Str(), Arguments.size(), Arguments.data(), payload, *Env);
    type->SetOptionalArgumentsCount(OptionalArgsCount);
    if (DisableMerge)
        type->DisableMerge();
    return TCallable::Create(Inputs.size(), Inputs.data(), type, *Env);
}

void TCallableBuilder::Clear() {
    Arguments.clear();
    Inputs.clear();
    ArgNames.clear();
    ArgFlags.clear();
    OptionalArgsCount = 0;
    FuncPayload = TStringBuf();
}

}
}