aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/udfs/common/python/bindings/py_ctx.h
blob: 9e86042908f219c6ff104dee3ab3b765b7667001 (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
#pragma once

#include "py_ptr.h"

#include <yql/essentials/public/udf/udf_types.h>
#include <yql/essentials/public/udf/udf_type_builder.h>
#include <yql/essentials/public/udf/udf_type_inspection.h>
#include <yql/essentials/public/udf/udf_value_builder.h>
#include <yql/essentials/public/udf/udf_string.h>

#include <util/generic/ptr.h>
#include <util/generic/intrlist.h>

#include <unordered_map>

namespace NPython {

enum class EBytesDecodeMode {
    Never,
    Strict,
};

class IMemoryLock {
public:
    virtual ~IMemoryLock() = default;
    virtual void Acquire() = 0;
    virtual void Release() = 0;
};

struct TPyCleanupListItemBase: public TIntrusiveListItem<TPyCleanupListItemBase> {
    virtual ~TPyCleanupListItemBase() = default;
    virtual void Cleanup() = 0;
};

template <typename TValueType>
class TPyCleanupListItem: public TPyCleanupListItemBase {
public:
    TPyCleanupListItem() = default;
    virtual ~TPyCleanupListItem() {
        Unlink();
    }

    void Cleanup() override {
        Value = {};
    }

    template <typename TCtx>
    void Set(const TIntrusivePtr<TCtx>& ctx, TValueType val) {
        Value = std::move(val);
        ctx->CleanupList.PushBack(this);
    }

    bool IsSet() const {
        return !!Value;
    }

    const TValueType& Get() const {
        if (!Value) {
            throw yexception() << "Trying to use python wrap object with destroyed yql value";
        }
        return Value;
    }

private:
    TValueType Value;
};

struct TPyContext: public TSimpleRefCount<TPyContext> {
    const NKikimr::NUdf::ITypeInfoHelper::TPtr TypeInfoHelper;
    const NKikimr::NUdf::TStringRef ResourceTag;
    const NKikimr::NUdf::TSourcePosition Pos;
    TIntrusiveList<TPyCleanupListItemBase> CleanupList;

    TPyContext(NKikimr::NUdf::ITypeInfoHelper::TPtr helper, const NKikimr::NUdf::TStringRef& tag, const NKikimr::NUdf::TSourcePosition& pos)
        : TypeInfoHelper(std::move(helper))
        , ResourceTag(tag)
        , Pos(pos)
    {
    }

    void Cleanup() {
        for (auto& o: CleanupList) {
            o.Cleanup();
        }
        CleanupList.Clear();
    }

    ~TPyContext() = default;

    using TPtr = TIntrusivePtr<TPyContext>;
};

struct TPyCastContext: public TSimpleRefCount<TPyCastContext> {
    const NKikimr::NUdf::IValueBuilder *const ValueBuilder;
    const TPyContext::TPtr PyCtx;
    std::unordered_map<const NKikimr::NUdf::TType*, TPyObjectPtr> StructTypes;
    bool LazyInputObjects = true;
    TPyObjectPtr YsonConverterIn;
    TPyObjectPtr YsonConverterOut;
    EBytesDecodeMode BytesDecodeMode = EBytesDecodeMode::Never;
    TPyObjectPtr Decimal;
    std::unordered_map<ui32, TPyObjectPtr> TimezoneNames;
    THolder<IMemoryLock> MemoryLock;

    TPyCastContext(
            const NKikimr::NUdf::IValueBuilder* builder,
            TPyContext::TPtr pyCtx,
            THolder<IMemoryLock> memoryLock = {});

    ~TPyCastContext();

    const TPyObjectPtr& GetTimezoneName(ui32 id);
    const TPyObjectPtr& GetDecimal();

    using TPtr = TIntrusivePtr<TPyCastContext>;
};

using TPyCastContextPtr = TPyCastContext::TPtr;

} // namspace NPython