summaryrefslogtreecommitdiffstats
path: root/yql/essentials/parser/pg_wrapper/comp_factory_utils.cpp
blob: 06b75438043f8b3ea48174a190dd96d7e6dd0b01 (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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
#include "comp_factory_utils.h"

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

#include <yql/essentials/parser/pg_catalog/catalog.h>
#include <yql/essentials/parser/pg_wrapper/utils.h>
#include <yql/essentials/minikql/mkql_type_ops.h>
#include <yql/essentials/minikql/mkql_node_cast.h>
#include <yql/essentials/minikql/defs.h>

namespace NYql {

using namespace NKikimr::NMiniKQL;

TPgArgsExprBuilder::TPgArgsExprBuilder()
    : PgFuncArgsList(nullptr, &free)
{
}

void TPgArgsExprBuilder::Add(ui32 argOid) {
    PgArgNodes.emplace_back();
    auto& p = PgArgNodes.back();
    Zero(p);
    p.xpr.type = T_Param;
    p.paramkind = PARAM_EXTERN;
    p.paramtype = argOid;
    p.paramcollid = DEFAULT_COLLATION_OID;
    p.paramtypmod = -1;
    p.paramid = PgArgNodes.size();
}

Node* TPgArgsExprBuilder::Build(const NPg::TProcDesc& procDesc) {
    PgFuncArgsList.reset((List*)malloc(offsetof(List, initial_elements) + PgArgNodes.size() * sizeof(ListCell)));
    PgFuncArgsList->type = T_List;
    PgFuncArgsList->elements = PgFuncArgsList->initial_elements;
    PgFuncArgsList->length = PgFuncArgsList->max_length = PgArgNodes.size();
    for (size_t i = 0; i < PgArgNodes.size(); ++i) {
        PgFuncArgsList->elements[i].ptr_value = &PgArgNodes[i];
    }

    Zero(PgFuncNode);
    PgFuncNode.xpr.type = T_FuncExpr;
    PgFuncNode.funcid = procDesc.ProcId;
    PgFuncNode.funcresulttype = procDesc.ResultType;
    PgFuncNode.funcretset = procDesc.ReturnSet;
    PgFuncNode.funcvariadic = procDesc.VariadicArgType && procDesc.VariadicArgType != procDesc.VariadicType;
    PgFuncNode.args = PgFuncArgsList.get();
    return (Node*)&PgFuncNode;
}

TPgResolvedCallBase::TPgResolvedCallBase(std::string_view name,
                                         ui32 id,
                                         TVector<TType*>&& argTypes,
                                         TType* returnType,
                                         bool isList,
                                         const TStructType* structType)
    : TPgResolvedCallBase(name, id,
                          [&argTypes]() {
                              TVector<std::optional<ui32>> result(Reserve(argTypes.size()));
                              for (const auto* argType : argTypes) {
                                  if (argType->IsPg()) {
                                      result.push_back(static_cast<const TPgType*>(argType)->GetTypeId());
                                  } else {
                                      result.push_back(std::nullopt);
                                  }
                              }
                              return result;
                          }(),
                          returnType->IsStruct() ? RECORDOID : AS_TYPE(TPgType, returnType)->GetTypeId(),
                          isList, structType)
{
}

TPgResolvedCallBase::TPgResolvedCallBase(std::string_view name,
                                         ui32 id,
                                         const TVector<std::optional<ui32>>& argTypeIds,
                                         ui32 returnTypeOid,
                                         bool isList,
                                         const TStructType* structType)
    : Name(name)
    , Id(id)
    , ProcDesc(NPg::LookupProc(id))
    , RetTypeDesc(NPg::LookupType(returnTypeOid))
{
    Zero(FInfo);
    Y_ENSURE(Id);
    GetPgFuncAddr(Id, FInfo);
    Y_ENSURE(FInfo.fn_retset == isList);
    Y_ENSURE(FInfo.fn_addr);
    ArgDesc.reserve(argTypeIds.size());
    for (ui32 i = 0; i < argTypeIds.size(); ++i) {
        auto type = [&]() {
            if (argTypeIds[i]) {
                return *argTypeIds[i];
            }
            return ProcDesc.ArgTypes[i];
        }();
        ArgDesc.emplace_back(NPg::LookupType(type));
    }
    Y_ENSURE(ArgDesc.size() == argTypeIds.size());
    Y_ENSURE(ArgDesc.size() <= FUNC_MAX_ARGS);
    for (size_t i = 0; i < ArgDesc.size(); ++i) {
        ArgsExprBuilder.Add(ArgDesc[i].TypeId);
    }

    FInfo.fn_expr = ArgsExprBuilder.Build(ProcDesc);

    if (structType) {
        StructTypeDesc.reserve(structType->GetMembersCount());
        for (ui32 i = 0; i < structType->GetMembersCount(); ++i) {
            auto itemType = structType->GetMemberType(i);
            auto type = AS_TYPE(NKikimr::NMiniKQL::TPgType, itemType)->GetTypeId();
            StructTypeDesc.emplace_back(NPg::LookupType(type));
        }
    }
}

TPgResolvedCallBase::TPgResolvedCallBase(std::string_view name,
                                         ui32 id,
                                         const TVector<ui32>& argTypeIds,
                                         ui32 returnTypeOid)
    : TPgResolvedCallBase(name, id,
                          [&argTypeIds]() {
                              TVector<std::optional<ui32>> result;
                              result.reserve(argTypeIds.size());
                              for (ui32 typeId : argTypeIds) {
                                  result.push_back(typeId);
                              }
                              return result;
                          }(),
                          returnTypeOid, /*isList=*/false, /*structType=*/nullptr)
{
}

TPgConst::TPgConst(ui32 typeId, std::string_view value)
    : TypeId(typeId)
    , Value(value)
    , TypeDesc(NPg::LookupType(typeId))
{
    Zero(FInfo);
    ui32 inFuncId = TypeDesc.InFuncId;
    if (TypeDesc.TypeId == TypeDesc.ArrayTypeId) {
        inFuncId = NPg::LookupProc("array_in", {0, 0, 0}).ProcId;
    }

    Y_ENSURE(inFuncId);
    GetPgFuncAddr(inFuncId, FInfo);
    Y_ENSURE(!FInfo.fn_retset);
    Y_ENSURE(FInfo.fn_addr);
    Y_ENSURE(FInfo.fn_nargs >= 1 && FInfo.fn_nargs <= 3);
    TypeIOParam = MakeTypeIOParam(TypeDesc);
}

NUdf::TUnboxedValue TPgConst::ExtractConst(i32 typeMod) const {
    LOCAL_FCINFO(callInfo, 3);
    Zero(*callInfo);
    FmgrInfo copyFmgrInfo = FInfo;
    callInfo->flinfo = &copyFmgrInfo;
    callInfo->nargs = 3;
    callInfo->fncollation = DEFAULT_COLLATION_OID;
    callInfo->isnull = false;
    callInfo->args[0] = {(Datum)Value.c_str(), false};
    callInfo->args[1] = {ObjectIdGetDatum(TypeIOParam), false};
    callInfo->args[2] = {Int32GetDatum(typeMod), false};

    NKikimr::NMiniKQL::TPAllocScope call;
    auto ret = FInfo.fn_addr(callInfo);
    Y_ENSURE(!callInfo->isnull);
    return AnyDatumToPod(ret, TypeDesc.PassByValue);
}

TFunctionCallInfo::TFunctionCallInfo(ui32 numArgs, const FmgrInfo* finfo)
    : NumArgs(numArgs)
    , CopyFmgrInfo(*finfo)
{
    if (!finfo->fn_addr) {
        return;
    }

    MemSize = SizeForFunctionCallInfo(numArgs);
    Ptr = TWithDefaultMiniKQLAlloc::AllocWithSize(MemSize);
    auto& callInfo = Ref();
    Zero(callInfo);
    callInfo.flinfo = &CopyFmgrInfo; // client may mutate fn_extra
    callInfo.nargs = NumArgs;
    callInfo.fncollation = DEFAULT_COLLATION_OID;
}

FunctionCallInfoBaseData& TFunctionCallInfo::Ref() {
    Y_ENSURE(Ptr);
    return *(FunctionCallInfoBaseData*)Ptr;
}

TFunctionCallInfo::~TFunctionCallInfo() {
    if (Ptr) {
        TWithDefaultMiniKQLAlloc::FreeWithSize(Ptr, MemSize);
    }
}

TPgResolvedCallState::TPgResolvedCallState(ui32 numArgs, const FmgrInfo* finfo)
    : CallInfo(numArgs, finfo)
    , Args(numArgs)
{
}

template <bool UseContext>
TPgResolvedCall<UseContext>::TPgResolvedCall(const std::string_view& name,
                                             ui32 id,
                                             TVector<NKikimr::NMiniKQL::TType*>&& argTypes,
                                             NKikimr::NMiniKQL::TType* returnType)
    : TPgResolvedCallBase(name, id, std::move(argTypes), returnType, false, nullptr)
{
}

template <bool UseContext>
TPgResolvedCall<UseContext>::TPgResolvedCall(const std::string_view& name,
                                             ui32 id,
                                             const TVector<std::optional<ui32>>& argTypeIds,
                                             ui32 returnTypeOid)
    : TPgResolvedCallBase(name, id, argTypeIds, returnTypeOid, /*isList=*/false, /*structType=*/nullptr)
{
}

template <bool UseContext>
TPgResolvedCall<UseContext>::TPgResolvedCall(const std::string_view& name,
                                             ui32 id,
                                             const TVector<ui32>& argTypeIds,
                                             ui32 returnTypeOid)
    : TPgResolvedCallBase(name, id, argTypeIds, returnTypeOid)
{
}

template <bool UseContext>
NUdf::TUnboxedValue TPgResolvedCall<UseContext>::DoCall(FunctionCallInfoBaseData& callInfo) const {
    auto ret = this->FInfo.fn_addr(&callInfo);
    if (callInfo.isnull) {
        return NUdf::TUnboxedValue();
    }

    return AnyDatumToPod(ret, this->RetTypeDesc.PassByValue);
}

template <bool UseContext>
NUdf::TUnboxedValue TPgResolvedCall<UseContext>::CallFunction(TPgResolvedCallState& state, NKikimr::NMiniKQL::TUnboxedValueView values) const {
    Y_ENSURE(values.size() == ArgDesc.size());
    return CallFunction(state, [&](size_t index) { return values[index]; });
}

template <bool UseContext>
THolder<TPgResolvedCallState> TPgResolvedCall<UseContext>::CreateState() const {
    return MakeHolder<TPgResolvedCallState>(GetArgCount(), &FInfo);
}

TPgCast::TPgCast(ui32 sourceId, ui32 targetId, bool hasTypeMod)
    : SourceId(sourceId)
    , TargetId(targetId)
    , SourceTypeDesc(SourceId ? NPg::LookupType(SourceId) : NPg::TTypeDesc())
    , TargetTypeDesc(NPg::LookupType(targetId))
    , IsSourceArray(SourceId && SourceTypeDesc.TypeId == SourceTypeDesc.ArrayTypeId)
    , IsTargetArray(TargetTypeDesc.TypeId == TargetTypeDesc.ArrayTypeId)
    , SourceElemDesc(SourceId ? NPg::LookupType(IsSourceArray ? SourceTypeDesc.ElementTypeId : SourceTypeDesc.TypeId) : NPg::TTypeDesc())
    , TargetElemDesc(NPg::LookupType(IsTargetArray ? TargetTypeDesc.ElementTypeId : TargetTypeDesc.TypeId))
{
    TypeIOParam = MakeTypeIOParam(TargetTypeDesc);

    Zero(FInfo1);
    Zero(FInfo2);
    if (hasTypeMod && SourceId == TargetId && NPg::HasCast(TargetElemDesc.TypeId, TargetElemDesc.TypeId)) {
        const auto& cast = NPg::LookupCast(TargetElemDesc.TypeId, TargetElemDesc.TypeId);

        Y_ENSURE(cast.FunctionId);
        GetPgFuncAddr(cast.FunctionId, FInfo1);
        Y_ENSURE(!FInfo1.fn_retset);
        Y_ENSURE(FInfo1.fn_addr);
        Y_ENSURE(FInfo1.fn_nargs >= 2 && FInfo1.fn_nargs <= 3);
        ConvertLength = true;
        ArrayCast = IsSourceArray;
        return;
    }

    if (SourceId == 0 || SourceId == TargetId) {
        return;
    }

    ui32 funcId;
    ui32 funcId2 = 0;
    if (!NPg::HasCast(SourceElemDesc.TypeId, TargetElemDesc.TypeId) || (IsSourceArray != IsTargetArray)) {
        ArrayCast = IsSourceArray && IsTargetArray;
        if (IsSourceArray && !IsTargetArray) {
            Y_ENSURE(TargetTypeDesc.Category == 'S' || TargetId == UNKNOWNOID);
            funcId = NPg::LookupProc("array_out", {0}).ProcId;
        } else if (IsTargetArray && !IsSourceArray) {
            Y_ENSURE(SourceElemDesc.Category == 'S' || SourceId == UNKNOWNOID);
            funcId = NPg::LookupProc("array_in", {0, 0, 0}).ProcId;
        } else if (SourceElemDesc.Category == 'S' || SourceId == UNKNOWNOID) {
            funcId = TargetElemDesc.InFuncId;
        } else {
            Y_ENSURE(TargetTypeDesc.Category == 'S' || TargetId == UNKNOWNOID);
            funcId = SourceElemDesc.OutFuncId;
        }
    } else {
        Y_ENSURE(IsSourceArray == IsTargetArray);
        ArrayCast = IsSourceArray;

        const auto& cast = NPg::LookupCast(SourceElemDesc.TypeId, TargetElemDesc.TypeId);
        switch (cast.Method) {
            case NPg::ECastMethod::Binary:
                return;
            case NPg::ECastMethod::Function: {
                Y_ENSURE(cast.FunctionId);
                funcId = cast.FunctionId;
                break;
            }
            case NPg::ECastMethod::InOut: {
                funcId = SourceElemDesc.OutFuncId;
                funcId2 = TargetElemDesc.InFuncId;
                break;
            }
        }
    }

    Y_ENSURE(funcId);
    GetPgFuncAddr(funcId, FInfo1);
    Y_ENSURE(!FInfo1.fn_retset);
    Y_ENSURE(FInfo1.fn_addr);
    Y_ENSURE(FInfo1.fn_nargs >= 1 && FInfo1.fn_nargs <= 3);
    Func1Lookup = NPg::LookupProc(funcId);
    Y_ENSURE(Func1Lookup.ArgTypes.size() >= 1 && Func1Lookup.ArgTypes.size() <= 3);
    if (NPg::LookupType(Func1Lookup.ArgTypes[0]).TypeLen == -2 && SourceElemDesc.Category == 'S') {
        ConvertArgToCString = true;
    }

    if (funcId2) {
        Y_ENSURE(funcId2);
        GetPgFuncAddr(funcId2, FInfo2);
        Y_ENSURE(!FInfo2.fn_retset);
        Y_ENSURE(FInfo2.fn_addr);
        Y_ENSURE(FInfo2.fn_nargs == 1);
        Func2Lookup = NPg::LookupProc(funcId2);
        Y_ENSURE(Func2Lookup.ArgTypes.size() == 1);
    }

    if (!funcId2) {
        if (NPg::LookupType(Func1Lookup.ResultType).TypeLen == -2 && TargetElemDesc.Category == 'S') {
            ConvertResFromCString = true;
        }
    } else {
        const auto& Func2ArgType = NPg::LookupType(Func2Lookup.ArgTypes[0]);
        if (NPg::LookupType(Func1Lookup.ResultType).TypeLen == -2 && Func2ArgType.Category == 'S') {
            ConvertResFromCString = true;
        }

        if (NPg::LookupType(Func2Lookup.ResultType).TypeLen == -2 && TargetElemDesc.Category == 'S') {
            ConvertResFromCString2 = true;
        }
    }
}

NUdf::TUnboxedValue TPgCast::Calculate(NUdf::TUnboxedValue value, i32 typeMod, TPgCastState& state) const {
    if (!value) {
        return value;
    }

    if (!FInfo1.fn_addr) {
        // binary compatible
        if (!ArrayCast) {
            return value.Release();
        } else {
            // clone array with new target type in the header
            auto datum = PointerDatumFromPod(value);
            ArrayType* arr = DatumGetArrayTypePCopy(datum);
            ARR_ELEMTYPE(arr) = TargetElemDesc.TypeId;
            return PointerDatumToPod(PointerGetDatum(arr));
        }
    }

    TPAllocScope call;
    if (ArrayCast) {
        auto arr = (ArrayType*)DatumGetPointer(PointerDatumFromPod(value));
        auto ndim = ARR_NDIM(arr);
        auto dims = ARR_DIMS(arr);
        auto lb = ARR_LBOUND(arr);
        auto nitems = ArrayGetNItems(ndim, dims);

        Datum* elems = (Datum*)TWithDefaultMiniKQLAlloc::AllocWithSize(nitems * sizeof(Datum));
        Y_DEFER {
            TWithDefaultMiniKQLAlloc::FreeWithSize(elems, nitems * sizeof(Datum));
        };

        bool* nulls = (bool*)TWithDefaultMiniKQLAlloc::AllocWithSize(nitems);
        Y_DEFER {
            TWithDefaultMiniKQLAlloc::FreeWithSize(nulls, nitems);
        };

        array_iter iter;
        array_iter_setup(&iter, (AnyArrayType*)arr);
        for (ui32 i = 0; i < nitems; ++i) {
            bool isNull;
            auto datum = array_iter_next(&iter, &isNull, i, SourceElemDesc.TypeLen,
                                         SourceElemDesc.PassByValue, SourceElemDesc.TypeAlign);
            if (isNull) {
                nulls[i] = true;
                continue;
            } else {
                nulls[i] = false;
                elems[i] = ConvertDatum(datum, state, typeMod);
            }
        }

        auto ret = construct_md_array(elems, nulls, ndim, dims, lb, TargetElemDesc.TypeId,
                                      TargetElemDesc.TypeLen, TargetElemDesc.PassByValue, TargetElemDesc.TypeAlign);

        return PointerDatumToPod(PointerGetDatum(ret));
    } else {
        auto datum = SourceTypeDesc.PassByValue ? ScalarDatumFromPod(value) : PointerDatumFromPod(value);
        auto ret = ConvertDatum(datum, state, typeMod);
        return AnyDatumToPod(ret, TargetTypeDesc.PassByValue);
    }
}

Datum TPgCast::ConvertDatum(Datum datum, TPgCastState& state, i32 typeMod) const {
    auto& callInfo1 = state.CallInfo1.Ref();
    callInfo1.isnull = false;
    NullableDatum argDatum = {datum, false};
    void* freeCString = nullptr;
    Y_DEFER {
        if (freeCString) {
            pfree(freeCString);
        }
    };

    if (ConvertArgToCString) {
        argDatum.value = (Datum)MakeCString(GetVarBuf((const text*)argDatum.value));
        freeCString = (void*)argDatum.value;
    }

    callInfo1.args[0] = argDatum;
    if (ConvertLength) {
        callInfo1.args[1] = {Int32GetDatum(typeMod), false};
        callInfo1.args[2] = {BoolGetDatum(true), false};
    } else {
        if (FInfo1.fn_nargs == 2) {
            callInfo1.args[1] = {Int32GetDatum(typeMod), false};
        } else {
            callInfo1.args[1] = {ObjectIdGetDatum(TypeIOParam), false};
            callInfo1.args[2] = {Int32GetDatum(typeMod), false};
        }
    }

    void* freeMem = nullptr;
    void* freeMem2 = nullptr;
    Y_DEFER {
        if (freeMem) {
            pfree(freeMem);
        }

        if (freeMem2) {
            pfree(freeMem2);
        }
    };

    {
        auto ret = FInfo1.fn_addr(&callInfo1);
        Y_ENSURE(!callInfo1.isnull);

        if (ConvertResFromCString) {
            freeMem = (void*)ret;
            ret = (Datum)MakeVar((const char*)ret);
        }

        if (FInfo2.fn_addr) {
            auto& callInfo2 = state.CallInfo1.Ref();
            callInfo2.isnull = false;
            NullableDatum argDatum2 = {ret, false};
            callInfo2.args[0] = argDatum2;

            auto ret2 = FInfo2.fn_addr(&callInfo2);
            pfree((void*)ret);

            Y_ENSURE(!callInfo2.isnull);
            ret = ret2;
        }

        if (ConvertResFromCString2) {
            freeMem2 = (void*)ret;
            ret = (Datum)MakeVar((const char*)ret);
        }

        return ret;
    }
}

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

TPgResolvedCallWithCastState::TPgResolvedCallWithCastState(
    const TPgResolvedCall<false>& caller,
    const TVector<TPgCast>& casters)
    : CallState(caller.GetArgCount(), &caller.GetFInfo())
{
    CastStates.reserve(casters.size());
    for (const auto& caster : casters) {
        CastStates.push_back(MakeHolder<TPgCastState>(caster.GetFInfo1(), caster.GetFInfo2()));
    }
}

TPgResolvedCallWithCast::TPgResolvedCallWithCast(
    std::string_view name,
    ui32 procId,
    const TVector<ui32>& expectedArgTypeIds,
    const TVector<ui32>& inputArgTypeIds,
    ui32 resultType)
{
    Call = std::make_unique<TPgResolvedCall<false>>(name, procId, expectedArgTypeIds, resultType);

    Casters.reserve(inputArgTypeIds.size());
    for (size_t i = 0; i < inputArgTypeIds.size(); ++i) {
        Casters.emplace_back(inputArgTypeIds[i], expectedArgTypeIds[i], false);
    }
}

TPgResolvedCallWithCast TPgResolvedCallWithCast::ForProc(
    std::string_view name,
    const TVector<ui32>& inputArgTypeIds)
{
    auto procOrType = NPg::LookupProcWithCasts(TString(name), inputArgTypeIds);

    const auto* procPtr = std::get_if<const NPg::TProcDesc*>(&procOrType);
    MKQL_ENSURE(procPtr, "Type cast case not supported, use TPgCast directly");

    const auto& proc = **procPtr;
    MKQL_ENSURE(inputArgTypeIds.size() <= proc.ArgTypes.size(), "Variadic functions not supported");

    TVector<ui32> expectedArgTypeIds;
    expectedArgTypeIds.reserve(inputArgTypeIds.size());
    for (size_t i = 0; i < inputArgTypeIds.size(); ++i) {
        expectedArgTypeIds.push_back(proc.ArgTypes[i]);
    }

    return TPgResolvedCallWithCast(name, proc.ProcId, expectedArgTypeIds, inputArgTypeIds, proc.ResultType);
}

TPgResolvedCallWithCast TPgResolvedCallWithCast::ForOperator(
    std::string_view operName,
    const TVector<ui32>& inputArgTypeIds)
{
    const auto& oper = NPg::LookupOper(TString(operName), inputArgTypeIds);
    TVector<ui32> expectedArgTypeIds = {oper.LeftType, oper.RightType};
    return TPgResolvedCallWithCast(operName, oper.ProcId, expectedArgTypeIds, inputArgTypeIds, oper.ResultType);
}

TPgResolvedCallWithCastState TPgResolvedCallWithCast::CreateState() const {
    return TPgResolvedCallWithCastState(*Call, Casters);
}

NUdf::TUnboxedValue TPgResolvedCallWithCast::CallFunctionWithCast(
    TPgResolvedCallWithCastState& state,
    NKikimr::NMiniKQL::TUnboxedValueView values) const {
    Y_ENSURE(values.size() == Casters.size());

    return Call->CallFunction(state.CallState, [&](size_t i) -> NUdf::TUnboxedValue {
        return Casters[i].Calculate(values[i], -1, *state.CastStates[i]);
    });
}

} // namespace NYql