aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/parser/pg_wrapper/ut/codegen_ut.cpp
blob: 50320e27ddc378b6528428084e5c139b5ae5397a (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
#include "../pg_compat.h"

#include <library/cpp/testing/unittest/registar.h>
#include <library/cpp/resource/resource.h>
#include <yql/essentials/minikql/codegen/codegen.h>
#include <yql/essentials/minikql/arrow/arrow_defs.h>

#include <arrow/compute/kernel.h>
#include <arrow/array/builder_primitive.h>
#include <arrow/array/builder_binary.h>

#include <llvm/IR/Module.h>

#include <yql/essentials/parser/pg_wrapper/arrow.h>

extern "C" {
#include <yql/essentials/parser/pg_wrapper/postgresql/src/backend/catalog/pg_collation_d.h>
#include <yql/essentials/parser/pg_wrapper/postgresql/src/backend/utils/fmgrprotos.h>
}

#include <yql/essentials/parser/pg_catalog/catalog.h>
#include <yql/essentials/minikql/arrow/arrow_util.h>

#include <util/datetime/cputimer.h>

using namespace NYql;
using namespace NYql::NCodegen;

extern "C" {
Y_PRAGMA_DIAGNOSTIC_PUSH
Y_PRAGMA("GCC diagnostic ignored \"-Wreturn-type-c-linkage\"")
#include <yql/essentials/parser/pg_wrapper/pg_kernels_fwd.inc>
Y_PRAGMA_DIAGNOSTIC_POP
}

enum class EKernelFlavor {
    Indirect,
    DefArg,
    Cpp,
    BitCode,
    Ideal
};

Y_UNIT_TEST_SUITE(TPgCodegen) {
    void PgFuncImpl(EKernelFlavor flavor, bool constArg, bool fixed) {
        const TString& name = fixed ? "date_eq" : "textout";
        ICodegen::TPtr codegen;
        TExecFunc execFunc;        
        switch (flavor) {
        case EKernelFlavor::Indirect: {
            if (fixed) {
                execFunc = MakeIndirectExec<true, true>(&date_eq);
            } else {
                execFunc = MakeIndirectExec<true, false>(&textout);
            }
            break;
        }
        case EKernelFlavor::DefArg: {
            if (fixed) {
                execFunc = TGenericExec<TPgDirectFunc<&date_eq>, true, true, TDefaultArgsPolicy>({});
            } else {
                execFunc = TGenericExec<TPgDirectFunc<&textout>, true, false, TDefaultArgsPolicy>({});
            }
            break;
        }
        case EKernelFlavor::Cpp: {
            execFunc = fixed ? arrow_date_eq() : arrow_textout();
            break;
        }
        case EKernelFlavor::BitCode: {
            codegen = ICodegen::Make(ETarget::Native);
            auto bitcode = NResource::Find(fixed ? "/llvm_bc/PgFuncs1" : "/llvm_bc/PgFuncs17");
            codegen->LoadBitCode(bitcode, "Funcs");
            auto func = codegen->GetModule().getFunction(std::string("arrow_" + name));
            Y_ENSURE(func);
            codegen->AddGlobalMapping("GetPGKernelState", (const void*)&GetPGKernelState);
            codegen->Verify();
            codegen->ExportSymbol(func);
            codegen->Compile();
            //codegen->ShowGeneratedFunctions(&Cerr);
            typedef TExecFunc (*TFunc)();
            auto funcPtr = (TFunc)codegen->GetPointerToFunction(func);
            execFunc = funcPtr();
            break;
        }
        case EKernelFlavor::Ideal: {
            if (fixed) {
                execFunc = [](arrow::compute::KernelContext* ctx, const arrow::compute::ExecBatch& batch, arrow::Datum* res) {
                    size_t length = batch.values[0].length();
                    //NUdf::TFixedSizeArrayBuilder<ui64, true> builder(NKikimr::NMiniKQL::TTypeInfoHelper(), arrow::uint64(), *arrow::default_memory_pool(), length);
                    NUdf::TTypedBufferBuilder<ui64> dataBuilder(arrow::default_memory_pool());
                    NUdf::TTypedBufferBuilder<ui8> nullBuilder(arrow::default_memory_pool());
                    dataBuilder.Reserve(length);
                    nullBuilder.Reserve(length);
                    auto out = dataBuilder.MutableData();
                    auto outNulls = nullBuilder.MutableData();
                    NUdf::TFixedSizeBlockReader<ui64, false> reader1;                    
                    NUdf::TFixedSizeBlockReader<ui64, false> reader2;                    
                    const auto& array1 = *batch.values[0].array();
                    const auto ptr1 = array1.GetValues<ui64>(1);                
                    if (batch.values[1].is_array()) {
                        const auto& array2 = *batch.values[1].array();
                        const auto ptr2 = array2.GetValues<ui64>(1);
                        for (size_t i = 0; i < length; ++i) {
                            //auto x = reader1.GetItem(array1, i).As<ui64>();
                            //auto y = reader2.GetItem(array2, i).As<ui64>();
                            auto x = ptr1[i];
                            auto y = ptr2[i];
                            out[i] = x == y ? 1 : 0;
                            outNulls[i] = false;
                        }
                    } else {
                        ui64 yConst = reader2.GetScalarItem(*batch.values[1].scalar()).As<ui64>();
                        for (size_t i = 0; i < length; ++i) {
                            auto x = ptr1[i];
                            out[i] = x == yConst ? 1 : 0;
                            outNulls[i] = false;
                        }
                    }

                    std::shared_ptr<arrow::Buffer> nulls;
                    nulls = nullBuilder.Finish();
                    nulls = NUdf::MakeDenseBitmap(nulls->data(), length, arrow::default_memory_pool());
                    std::shared_ptr<arrow::Buffer> data = dataBuilder.Finish();

                    *res = arrow::ArrayData::Make(arrow::uint64(), length ,{ data, nulls});
                    return arrow::Status::OK();
                };
            } else {
                execFunc = [](arrow::compute::KernelContext* ctx, const arrow::compute::ExecBatch& batch, arrow::Datum* res) {
                    size_t length = batch.values[0].length();
                    NUdf::TStringArrayBuilder<arrow::BinaryType, true, NUdf::EPgStringType::None> builder(NKikimr::NMiniKQL::TTypeInfoHelper(), arrow::binary(), *ctx->memory_pool(), length);
                    NUdf::TStringBlockReader<arrow::BinaryType, true> reader;
                    const auto& array = *batch.values[0].array();
                    for (size_t i = 0; i < length; ++i) {
                        auto item = reader.GetItem(array, i);
                        if (!item) {
                            builder.Add(NUdf::TBlockItem{});
                        } else {
                            auto s = item.AsStringRef();
                            size_t len = s.Size() - VARHDRSZ - sizeof(void*);
                            const char* ptr = s.Data() + VARHDRSZ + sizeof(void*);
                            builder.Add(NUdf::TBlockItem{NUdf::TStringRef(ptr, len)});
                        }
                    }

                    *res = builder.Build(true);
                    return arrow::Status::OK();
                };
            };

            break;
        }
        }

        Y_ENSURE(execFunc);
        arrow::compute::ExecContext execContent;
        arrow::compute::KernelContext kernelCtx(&execContent);
        TPgKernelState state;
        kernelCtx.SetState(&state);
        FmgrInfo finfo;
        Zero(state.flinfo);
        state.ProcDesc = fixed ? &NPg::LookupProc("date_eq", { 0, 0 }) : &NPg::LookupProc("textout", { 0 });
        fmgr_info(state.ProcDesc->ProcId, &state.flinfo);
        state.context = nullptr;
        state.resultinfo = nullptr;
        state.fncollation = DEFAULT_COLLATION_OID;
        state.Name = name;
        if (fixed) {
            state.TypeLen = 1;
            state.IsFixedResult = true;
            state.IsFixedArg.push_back(true);
            state.IsFixedArg.push_back(true);
        } else {
            state.TypeLen = -2;
            state.IsFixedResult = false;
            state.IsFixedArg.push_back(false);
        }

#ifdef NDEBUG
        const size_t N = 10000;
#else
        const size_t N = 1000;
#endif
        std::vector<arrow::Datum> batchArgs;
        if (fixed) {
            arrow::UInt64Builder builder;
            ARROW_OK(builder.Reserve(N));
            for (size_t i = 0; i < N; ++i) {
                builder.UnsafeAppend(i);
            }

            std::shared_ptr<arrow::ArrayData> out;
            ARROW_OK(builder.FinishInternal(&out));
            arrow::Datum arg1(out), arg2;
            if (constArg) {
                Cout << "with const arg\n";
                arg2 = NKikimr::NMiniKQL::MakeScalarDatum<ui64>(0);
            } else {
                arg2 = out;
            }

            batchArgs.push_back(arg1);
            batchArgs.push_back(arg2);
        } else {
            arrow::BinaryBuilder builder;
            ARROW_OK(builder.Reserve(N));
            for (size_t i = 0; i < N; ++i) {
                std::string s(sizeof(void*) + VARHDRSZ + 500, 'A' + i % 26);
                NUdf::ZeroMemoryContext(s.data() + sizeof(void*));
                auto t = (text*)(s.data() + sizeof(void*));
                SET_VARSIZE(t, VARHDRSZ + 500);
                ARROW_OK(builder.Append(s));
            }

            std::shared_ptr<arrow::ArrayData> out;
            ARROW_OK(builder.FinishInternal(&out));
            arrow::Datum arg1(out);
            batchArgs.push_back(arg1);
        }

        arrow::compute::ExecBatch batch(std::move(batchArgs), N);

        {
            Cout << "begin...\n";            
            TSimpleTimer timer;
            for (size_t count = 0; count < (fixed ? 10000 : 1000); ++count) {
                arrow::Datum res;
                ARROW_OK(execFunc(&kernelCtx, batch, &res));
                Y_ENSURE(res.length() == N);
            }

            Cout << "done, elapsed: " << timer.Get() << "\n";
        }
    }

    Y_UNIT_TEST(PgFixedFuncIdeal) {
        PgFuncImpl(EKernelFlavor::Ideal, false, true);
        PgFuncImpl(EKernelFlavor::Ideal, true, true);
    }

    Y_UNIT_TEST(PgFixedFuncDefArg) {
        PgFuncImpl(EKernelFlavor::DefArg, false, true);
        PgFuncImpl(EKernelFlavor::DefArg, true, true);
    }

    Y_UNIT_TEST(PgFixedFuncIndirect) {
        PgFuncImpl(EKernelFlavor::Indirect, false, true);
        PgFuncImpl(EKernelFlavor::Indirect, true, true);
    }    

#if !defined(USE_SLOW_PG_KERNELS)
    Y_UNIT_TEST(PgFixedFuncCpp) {
        PgFuncImpl(EKernelFlavor::Cpp, false, true);
        PgFuncImpl(EKernelFlavor::Cpp, true, true);
    }

    Y_UNIT_TEST(PgFixedFuncBC) {
        PgFuncImpl(EKernelFlavor::BitCode, false, true);
        PgFuncImpl(EKernelFlavor::BitCode, true, true);
    }
#endif    

    Y_UNIT_TEST(PgStrFuncIdeal) {
        PgFuncImpl(EKernelFlavor::Ideal, false, false);
    }        

    Y_UNIT_TEST(PgStrFuncDefArg) {
        PgFuncImpl(EKernelFlavor::DefArg, false, false);
    }    

    Y_UNIT_TEST(PgStrFuncIndirect) {
        PgFuncImpl(EKernelFlavor::Indirect, false, false);
    }

#if !defined(USE_SLOW_PG_KERNELS)
    Y_UNIT_TEST(PgStrFuncCpp) {
        PgFuncImpl(EKernelFlavor::Cpp, false, false);
    }

    Y_UNIT_TEST(PgStrFuncBC) {
        PgFuncImpl(EKernelFlavor::BitCode, false, false);
    }
#endif

}