aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/udfs/common/python/bindings/py_ctx.h
diff options
context:
space:
mode:
authorimunkin <imunkin@yandex-team.com>2024-11-08 10:00:23 +0300
committerimunkin <imunkin@yandex-team.com>2024-11-08 10:12:13 +0300
commita784a2f943d6e15caa6241e2e96d80aac6dbf375 (patch)
tree05f1e5366c916b988a8afb75bdab8ddeee0f6e6d /yql/essentials/udfs/common/python/bindings/py_ctx.h
parentd70137a7b530ccaa52834274913bbb5a3d1ca06e (diff)
downloadydb-a784a2f943d6e15caa6241e2e96d80aac6dbf375.tar.gz
Move yql/udfs/common/ to /yql/essentials YQL-19206
Except the following directories: * clickhouse/client * datetime * knn * roaring commit_hash:c7da95636144d28db109d6b17ddc762e9bacb59f
Diffstat (limited to 'yql/essentials/udfs/common/python/bindings/py_ctx.h')
-rw-r--r--yql/essentials/udfs/common/python/bindings/py_ctx.h120
1 files changed, 120 insertions, 0 deletions
diff --git a/yql/essentials/udfs/common/python/bindings/py_ctx.h b/yql/essentials/udfs/common/python/bindings/py_ctx.h
new file mode 100644
index 0000000000..9e86042908
--- /dev/null
+++ b/yql/essentials/udfs/common/python/bindings/py_ctx.h
@@ -0,0 +1,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