aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/minikql/mkql_node_builder_ut.cpp
blob: 91d71a5b3a2856c89727a03ce70028d4c2544bff (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
#include "mkql_node_builder.h"
#include "mkql_node_printer.h"
#include <yql/essentials/public/udf/udf_type_inspection.h>
#include "mkql_type_builder.h"

#include <library/cpp/testing/unittest/registar.h>

namespace NKikimr {
namespace NMiniKQL {

Y_UNIT_TEST_SUITE(TMiniKQLNodeBuilderTest) {
    Y_UNIT_TEST(TestDataBuild) {
        TScopedAlloc alloc(__LOCATION__);
        TTypeEnvironment env(alloc);
        auto d2 = BuildDataLiteral(NUdf::TUnboxedValuePod((ui32)456), NUdf::EDataSlot::Uint32, env);
        UNIT_ASSERT_EQUAL(d2->GetType()->GetSchemeType(), NUdf::TDataType<ui32>::Id);
    }

    Y_UNIT_TEST(TestTupleBuilder) {
        TScopedAlloc alloc(__LOCATION__);
        TTypeEnvironment env(alloc);
        auto builder = TTupleLiteralBuilder(env);
        const auto tuple = builder
            .Add(TRuntimeNode(BuildDataLiteral(NUdf::TUnboxedValuePod(true), NUdf::EDataSlot::Bool, env), true))
            .Add(TRuntimeNode(BuildDataLiteral(NUdf::TUnboxedValuePod(132), NUdf::EDataSlot::Uint32, env), true))
            .Build();
        UNIT_ASSERT_EQUAL(tuple->GetType()->GetKind(), TType::EKind::Tuple);
        UNIT_ASSERT_VALUES_EQUAL(tuple->GetValuesCount(), 2);
        builder.Clear();
        UNIT_ASSERT_VALUES_EQUAL(builder.Build()->GetValuesCount(), 0);
    }

    Y_UNIT_TEST(TestStructTypeBuilder) {
        TScopedAlloc alloc(__LOCATION__);
        TTypeEnvironment env(alloc);
        auto type = TStructTypeBuilder(env)
            .Add("Field1", env.GetVoidLazy()->GetGenericType())
            .Add("Field2", TDataType::Create(NUdf::TDataType<ui32>::Id, env))
            .Build();
        UNIT_ASSERT_EQUAL(type->GetKind(), TType::EKind::Struct);
    }

    Y_UNIT_TEST(TestStructLiteralBuilder) {
        TScopedAlloc alloc(__LOCATION__);
        TTypeEnvironment env(alloc);
        auto structObj = TStructLiteralBuilder(env)
            .Add("Field1", TRuntimeNode(env.GetVoidLazy(), true))
            .Add("Field2", TRuntimeNode(BuildDataLiteral(NUdf::TUnboxedValuePod((ui32)234), NUdf::EDataSlot::Uint32, env), true))
            .Build();
        UNIT_ASSERT_EQUAL(structObj->GetType()->GetKind(), TType::EKind::Struct);

        auto structObj2 = TStructLiteralBuilder(env)
            .Add("Field2", TRuntimeNode(BuildDataLiteral(NUdf::TUnboxedValuePod((ui32)234), NUdf::EDataSlot::Uint32, env), true))
            .Add("Field1", TRuntimeNode(env.GetVoidLazy(), true))
            .Build();
        UNIT_ASSERT(structObj->GetType()->IsSameType(*structObj2->GetType()));
    }

    Y_UNIT_TEST(TestListLiteralBuilder) {
        TScopedAlloc alloc(__LOCATION__);
        TTypeEnvironment env(alloc);
        auto list = TListLiteralBuilder(env, TDataType::Create(NUdf::TDataType<ui32>::Id, env))
            .Add(TRuntimeNode(BuildDataLiteral(NUdf::TUnboxedValuePod((ui32)132), NUdf::EDataSlot::Uint32, env), true))
            .Add(TRuntimeNode(BuildDataLiteral(NUdf::TUnboxedValuePod((ui32)234), NUdf::EDataSlot::Uint32, env), true))
            .Build();
        UNIT_ASSERT_EQUAL(list->GetType()->GetKind(), TType::EKind::List);
        UNIT_ASSERT_EQUAL(list->GetItemsCount(), 2);
    }

    Y_UNIT_TEST(TestCallableTypeBuilder) {
        TScopedAlloc alloc(__LOCATION__);
        TTypeEnvironment env(alloc);
        auto callableType = TCallableTypeBuilder(env, "func", TDataType::Create(NUdf::TDataType<ui32>::Id, env))
            .Add(env.GetVoidLazy()->GetType())
            .Add(env.GetTypeOfVoidLazy())
            .Build();

        UNIT_ASSERT_EQUAL(callableType->GetKind(), TType::EKind::Callable);
        UNIT_ASSERT_EQUAL(callableType->GetName(), "func");
        UNIT_ASSERT_EQUAL(callableType->GetArgumentsCount(), 2);
    }

    Y_UNIT_TEST(TestCallableTypeBuilderWithNamesAndFlags) {
        TScopedAlloc alloc(__LOCATION__);
        TTypeEnvironment env(alloc);
        auto callableType = TCallableTypeBuilder(env, "func", TDataType::Create(NUdf::TDataType<ui32>::Id, env))
            .Add(env.GetVoidLazy()->GetType())
            .Add(env.GetTypeOfVoidLazy())
            .SetArgumentName("Arg2")
            .SetArgumentFlags(NUdf::ICallablePayload::TArgumentFlags::AutoMap)
            .Add(env.GetListOfVoidLazy()->GetType())
            .SetArgumentName("Arg3")
            .Build();

        UNIT_ASSERT_EQUAL(callableType->GetKind(), TType::EKind::Callable);
        UNIT_ASSERT_EQUAL(callableType->GetName(), "func");
        UNIT_ASSERT_EQUAL(callableType->GetArgumentsCount(), 3);

        TTypeInfoHelper typeHelper;
        NUdf::TCallableTypeInspector callableInspector(typeHelper, callableType);
        UNIT_ASSERT(callableInspector);
        UNIT_ASSERT_EQUAL(callableInspector.GetArgsCount(), 3);
        UNIT_ASSERT_EQUAL(callableInspector.GetArgumentName(0), NUdf::TStringRef());
        UNIT_ASSERT_EQUAL(callableInspector.GetArgumentName(1), NUdf::TStringRef("Arg2"));
        UNIT_ASSERT_EQUAL(callableInspector.GetArgumentName(2), NUdf::TStringRef("Arg3"));

        UNIT_ASSERT_EQUAL(callableInspector.GetArgumentFlags(0), 0);
        UNIT_ASSERT_EQUAL(callableInspector.GetArgumentFlags(1), NUdf::ICallablePayload::TArgumentFlags::AutoMap);
        UNIT_ASSERT_EQUAL(callableInspector.GetArgumentFlags(2), 0);
    }

    Y_UNIT_TEST(TestCallableTypeBuilderBadOrderArgNames) {
        TScopedAlloc alloc(__LOCATION__);
        TTypeEnvironment env(alloc);
        UNIT_ASSERT_EXCEPTION(TCallableTypeBuilder(env, "func", TDataType::Create(NUdf::TDataType<ui32>::Id, env))
            .Add(env.GetVoidLazy()->GetType())
            .Add(env.GetTypeOfVoidLazy())
            .SetArgumentName("Arg2")
            .SetArgumentFlags(NUdf::ICallablePayload::TArgumentFlags::AutoMap)
            .Add(env.GetListOfVoidLazy()->GetType())
            .Build(), yexception);
    }

    Y_UNIT_TEST(TestCallableTypeBuilderDuplicateArgNames) {
        TScopedAlloc alloc(__LOCATION__);
        TTypeEnvironment env(alloc);
        UNIT_ASSERT_EXCEPTION(TCallableTypeBuilder(env, "func", TDataType::Create(NUdf::TDataType<ui32>::Id, env))
            .Add(env.GetVoidLazy()->GetType())
            .Add(env.GetTypeOfVoidLazy())
            .SetArgumentName("Arg2")
            .SetArgumentFlags(NUdf::ICallablePayload::TArgumentFlags::AutoMap)
            .Add(env.GetListOfVoidLazy()->GetType())
            .SetArgumentName("Arg2")
            .Build(), yexception);
    }

    Y_UNIT_TEST(TestDictLiteralBuilder) {
        TScopedAlloc alloc(__LOCATION__);
        TTypeEnvironment env(alloc);
        auto dict = TDictLiteralBuilder(env, TDataType::Create(NUdf::TDataType<ui32>::Id, env),
            TDataType::Create(NUdf::TDataType<char*>::Id, env)).Add(
                TRuntimeNode(BuildDataLiteral(NUdf::TUnboxedValuePod((ui32)132), NUdf::EDataSlot::Uint32, env), true),
                TRuntimeNode(BuildDataLiteral(NUdf::TUnboxedValuePod::Embedded("abc"), NUdf::TDataType<char*>::Id, env), true)
            ).Add(
                TRuntimeNode(BuildDataLiteral(NUdf::TUnboxedValuePod((ui32)234), NUdf::EDataSlot::Uint32, env), true),
                TRuntimeNode(BuildDataLiteral(NUdf::TUnboxedValuePod::Embedded("def"), NUdf::TDataType<char*>::Id, env), true)
            ).Build();

        UNIT_ASSERT_EQUAL(dict->GetType()->GetKind(), TType::EKind::Dict);
        UNIT_ASSERT_EQUAL(dict->GetItemsCount(), 2);
    }

    Y_UNIT_TEST(TestCallableBuilder) {
        TScopedAlloc alloc(__LOCATION__);
        TTypeEnvironment env(alloc);
        auto callable1 = TCallableBuilder(env, "func1", TDataType::Create(NUdf::TDataType<ui32>::Id, env))
            .Add(TRuntimeNode(env.GetEmptyStructLazy(), true))
            .Add(TRuntimeNode(env.GetVoidLazy(), true))
            .Build();

        UNIT_ASSERT_EQUAL(callable1->GetType()->GetKind(), TType::EKind::Callable);
        UNIT_ASSERT_EQUAL(callable1->GetInputsCount(), 2);

        auto callable2 = TCallableBuilder(env, "func2", TDataType::Create(NUdf::TDataType<ui32>::Id, env))
            .Add(TRuntimeNode(callable1, false))
            .Add(TRuntimeNode(env.GetVoidLazy(), true))
            .Build();

        UNIT_ASSERT_EQUAL(callable2->GetType()->GetKind(), TType::EKind::Callable);
        UNIT_ASSERT_EQUAL(callable2->GetType()->GetArgumentsCount(), 2);
        UNIT_ASSERT_EQUAL(callable2->GetType()->GetArgumentType(0)->GetKind(), TType::EKind::Data);
        UNIT_ASSERT_EQUAL(callable2->GetType()->GetArgumentType(1)->GetKind(), TType::EKind::Void);
        UNIT_ASSERT_EQUAL(callable2->GetInputsCount(), 2);
    }

    Y_UNIT_TEST(TestCallableBuilderWithNamesAndFlags) {
        TScopedAlloc alloc(__LOCATION__);
        TTypeEnvironment env(alloc);
        auto callable1 = TCallableBuilder(env, "func1", TDataType::Create(NUdf::TDataType<ui32>::Id, env))
            .Add(TRuntimeNode(env.GetEmptyStructLazy(), true))
            .Add(TRuntimeNode(env.GetVoidLazy(), true))
            .SetArgumentName("Arg2")
            .SetArgumentFlags(NUdf::ICallablePayload::TArgumentFlags::AutoMap)
            .Add(TRuntimeNode(env.GetEmptyTupleLazy(), true))
            .SetArgumentName("Arg3")
            .Build();

        UNIT_ASSERT_EQUAL(callable1->GetType()->GetKind(), TType::EKind::Callable);
        UNIT_ASSERT_EQUAL(callable1->GetInputsCount(), 3);

        TTypeInfoHelper typeHelper;
        NUdf::TCallableTypeInspector callableInspector(typeHelper, callable1->GetType());
        UNIT_ASSERT(callableInspector);
        UNIT_ASSERT_EQUAL(callableInspector.GetArgsCount(), 3);
        UNIT_ASSERT_EQUAL(callableInspector.GetArgumentName(0), NUdf::TStringRef());
        UNIT_ASSERT_EQUAL(callableInspector.GetArgumentName(1), NUdf::TStringRef("Arg2"));
        UNIT_ASSERT_EQUAL(callableInspector.GetArgumentName(2), NUdf::TStringRef("Arg3"));

        UNIT_ASSERT_EQUAL(callableInspector.GetArgumentFlags(0), 0);
        UNIT_ASSERT_EQUAL(callableInspector.GetArgumentFlags(1), NUdf::ICallablePayload::TArgumentFlags::AutoMap);
        UNIT_ASSERT_EQUAL(callableInspector.GetArgumentFlags(2), 0);
    }
}

}
}