summaryrefslogtreecommitdiffstats
path: root/yql/essentials/parser/pg_wrapper/comp_factory_utils.h
blob: f31ebedfa9f0d5dd0c23d850e0799aa64d07cf31 (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
#pragma once

#include "pg_include.h"

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

#include <yql/essentials/minikql/computation/mkql_computation_node_holders.h>
#include <yql/essentials/public/udf/udf_value.h>
#include <yql/essentials/parser/pg_catalog/catalog.h>
#include <yql/essentials/minikql/mkql_alloc.h>
#include <yql/essentials/minikql/mkql_node.h>
#include <yql/essentials/parser/pg_wrapper/utils.h>

#include <util/system/types.h>

#include <memory>
#include <optional>
#include <string_view>

#include <util/generic/ptr.h>

namespace NYql {

class TPgArgsExprBuilder {
public:
    TPgArgsExprBuilder();

    void Add(ui32 argOid);
    Node* Build(const NPg::TProcDesc& procDesc);

private:
    TVector<Param> PgArgNodes;
    std::unique_ptr<List, decltype(&free)> PgFuncArgsList;
    FuncExpr PgFuncNode;
};

class TPgConst {
public:
    TPgConst(ui32 typeId, std::string_view value);

    NUdf::TUnboxedValue ExtractConst(i32 typeMod = -1) const;

    ui32 GetTypeId() const {
        return TypeId;
    }

private:
    const ui32 TypeId;
    const TString Value;
    const NPg::TTypeDesc TypeDesc;
    FmgrInfo FInfo;
    ui32 TypeIOParam;
};

class TPgResolvedCallBase {
public:
    TPgResolvedCallBase(std::string_view name,
                        ui32 id,
                        TVector<NKikimr::NMiniKQL::TType*>&& argTypes,
                        NKikimr::NMiniKQL::TType* returnType,
                        bool isList,
                        const NKikimr::NMiniKQL::TStructType* structType);

    TPgResolvedCallBase(std::string_view name,
                        ui32 id,
                        const TVector<std::optional<ui32>>& argTypeIds,
                        ui32 returnTypeOid,
                        bool isList,
                        const NKikimr::NMiniKQL::TStructType* structType);

    TPgResolvedCallBase(std::string_view name,
                        ui32 id,
                        const TVector<ui32>& argTypeIds,
                        ui32 returnTypeOid);

    size_t GetArgCount() const {
        return ArgDesc.size();
    }
    ui32 GetArgTypeId(size_t index) const {
        return ArgDesc[index].TypeId;
    }
    ui32 GetProcId() const {
        return Id;
    }
    ui32 GetReturnTypeId() const {
        return RetTypeDesc.TypeId;
    }
    const FmgrInfo& GetFInfo() const {
        return FInfo;
    }

protected:
    const std::string_view Name;
    const ui32 Id;
    FmgrInfo FInfo;
    const NPg::TProcDesc ProcDesc;
    const NPg::TTypeDesc RetTypeDesc;
    TVector<NPg::TTypeDesc> ArgDesc;
    TVector<NPg::TTypeDesc> StructTypeDesc;

    TPgArgsExprBuilder ArgsExprBuilder;
};

class TFunctionCallInfo {
public:
    TFunctionCallInfo(ui32 numArgs, const FmgrInfo* finfo);
    ~TFunctionCallInfo();

    TFunctionCallInfo(const TFunctionCallInfo&) = delete;
    TFunctionCallInfo& operator=(const TFunctionCallInfo&) = delete;

    FunctionCallInfoBaseData& Ref();

private:
    const ui32 NumArgs = 0;
    ui32 MemSize = 0;
    void* Ptr = nullptr;
    FmgrInfo CopyFmgrInfo;
};

struct TPgResolvedCallState {
    TPgResolvedCallState(ui32 numArgs, const FmgrInfo* finfo);

    TFunctionCallInfo CallInfo;
    NKikimr::NMiniKQL::TUnboxedValueVector Args;
};

template <bool UseContext>
class TPgResolvedCall: public TPgResolvedCallBase {
public:
    TPgResolvedCall(const std::string_view& name,
                    ui32 id,
                    TVector<NKikimr::NMiniKQL::TType*>&& argTypes,
                    NKikimr::NMiniKQL::TType* returnType);

    TPgResolvedCall(const std::string_view& name,
                    ui32 id,
                    const TVector<std::optional<ui32>>& argTypeIds,
                    ui32 returnTypeOid);

    TPgResolvedCall(const std::string_view& name,
                    ui32 id,
                    const TVector<ui32>& argTypeIds,
                    ui32 returnTypeOid);

    THolder<TPgResolvedCallState> CreateState() const;

    using TValueGetter = std::function<NUdf::TUnboxedValue(size_t index)>;

    template <typename TValueGetter>
    NUdf::TUnboxedValue CallFunction(TPgResolvedCallState& state, TValueGetter valueGetter) const requires std::invocable<TValueGetter, size_t> {
        auto& callInfo = state.CallInfo.Ref();
        auto& args = state.Args;
        if constexpr (UseContext) {
            callInfo.context = (Node*)NKikimr::NMiniKQL::TlsAllocState->CurrentContext;
        }

        callInfo.isnull = false;
        for (ui32 i = 0; i < ArgDesc.size(); ++i) {
            args[i] = std::invoke(valueGetter, i);
            auto& value = args[i];
            NullableDatum argDatum = {0, false};
            if (!value) {
                if (this->FInfo.fn_strict) {
                    return NUdf::TUnboxedValuePod();
                }

                argDatum.isnull = true;
            } else {
                argDatum.value = this->ArgDesc[i].PassByValue ? ScalarDatumFromPod(value) : PointerDatumFromPod(value);
            }

            callInfo.args[i] = argDatum;
        }

        const bool needToFree = PrepareVariadicArray(callInfo, this->ProcDesc);
        NUdf::TUnboxedValue res;
        if constexpr (!UseContext) {
            NKikimr::NMiniKQL::TPAllocScope call;
            Y_DEFER {
                // This ensures that there is no dangling pointers references to freed
                // |TPAllocScope| pages that can be allocated and stored inside |callInfo.flinfo->fn_extra|.
                callInfo.flinfo->fn_extra = nullptr;
            };
            res = this->DoCall(callInfo);
        } else {
            res = this->DoCall(callInfo);
        }

        if (needToFree) {
            FreeVariadicArray(callInfo, this->ArgDesc.size());
        }

        return res;
    };

    NUdf::TUnboxedValue CallFunction(TPgResolvedCallState& state, NKikimr::NMiniKQL::TUnboxedValueView values) const;

private:
    NUdf::TUnboxedValue DoCall(FunctionCallInfoBaseData& callInfo) const;
};

extern template class TPgResolvedCall<true>;
extern template class TPgResolvedCall<false>;

struct TPgCastState {
    TPgCastState(const FmgrInfo* finfo1, const FmgrInfo* finfo2)
        : CallInfo1(3, finfo1)
        , CallInfo2(1, finfo2)
    {
    }

    TFunctionCallInfo CallInfo1, CallInfo2;
};

class TPgCast {
public:
    TPgCast(ui32 sourceId, ui32 targetId, bool hasTypeMod);

    NUdf::TUnboxedValue Calculate(NUdf::TUnboxedValue value, i32 typeMod, TPgCastState& state) const;

    TPgCastState CreateState() const {
        return TPgCastState(&FInfo1, &FInfo2);
    }

    const FmgrInfo* GetFInfo1() const {
        return &FInfo1;
    }
    const FmgrInfo* GetFInfo2() const {
        return &FInfo2;
    }

protected:
    Datum ConvertDatum(Datum datum, TPgCastState& state, i32 typeMod) const;

    const ui32 SourceId;
    const ui32 TargetId;
    const NPg::TTypeDesc SourceTypeDesc;
    const NPg::TTypeDesc TargetTypeDesc;
    const bool IsSourceArray;
    const bool IsTargetArray;
    const NPg::TTypeDesc SourceElemDesc;
    const NPg::TTypeDesc TargetElemDesc;
    FmgrInfo FInfo1, FInfo2;
    NPg::TProcDesc Func1Lookup, Func2Lookup;
    bool ConvertArgToCString = false;
    bool ConvertResFromCString = false;
    bool ConvertResFromCString2 = false;
    ui32 TypeIOParam = 0;
    bool ArrayCast = false;
    bool ConvertLength = false;
};

struct TPgResolvedCallWithCastState {
    TPgResolvedCallWithCastState(const TPgResolvedCall<false>& caller, const TVector<TPgCast>& casters);

    TPgResolvedCallState CallState;
    TVector<THolder<TPgCastState>> CastStates;
};

class TPgResolvedCallWithCast {
public:
    static TPgResolvedCallWithCast ForProc(std::string_view name, const TVector<ui32>& inputArgTypeIds);

    static TPgResolvedCallWithCast ForOperator(std::string_view operName, const TVector<ui32>& inputArgTypeIds);

    NUdf::TUnboxedValue CallFunctionWithCast(
        TPgResolvedCallWithCastState& state,
        NKikimr::NMiniKQL::TUnboxedValueView values) const;

    TPgResolvedCallWithCastState CreateState() const;

    ui32 GetReturnTypeId() const {
        return Call->GetReturnTypeId();
    }

    const TPgResolvedCall<false>& GetCall() const {
        return *Call;
    }

    const TVector<TPgCast>& GetCasters() const {
        return Casters;
    }

private:
    TPgResolvedCallWithCast(std::string_view name, ui32 procId,
                            const TVector<ui32>& expectedArgTypeIds,
                            const TVector<ui32>& inputArgTypeIds, ui32 resultType);

    std::unique_ptr<TPgResolvedCall<false>> Call;
    TVector<TPgCast> Casters;
};

} // namespace NYql