aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/udfs/common/json2/sql_exists.h
blob: 8a049b49d4220cbca2db9b9ed1a92c2d65d0f3c9 (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
#pragma once

#include "resource.h"
#include "compile_path.h"

#include <yql/essentials/public/udf/udf_type_builder.h>
#include <yql/essentials/public/udf/udf_value.h>
#include <yql/essentials/public/udf/udf_helpers.h>

#include <util/generic/yexception.h>

namespace NJson2Udf {
    using namespace NKikimr;
    using namespace NUdf;
    using namespace NYql;
    using namespace NJsonPath;

    template <EDataSlot InputType, bool ThrowException>
    class TSqlExists: public TBoxedValue {
    public:
        explicit TSqlExists(TSourcePosition pos)
            : Pos_(pos)
        {
        }

        static TStringRef Name();

        static bool DeclareSignature(
            const TStringRef& name,
            TType* userType,
            IFunctionTypeInfoBuilder& builder,
            bool typesOnly) {
            Y_UNUSED(userType);
            if (name != Name()) {
                return false;
            }

            auto jsonType = builder.Resource(JSON_NODE_RESOURCE_NAME);
            TType* inputType = nullptr;
            if constexpr (InputType == EDataSlot::JsonDocument) {
                inputType = builder.SimpleType<TJsonDocument>();
            } else {
                inputType = jsonType;
            }
            auto inputOptionalType = builder.Optional()->Item(inputType).Build();
            auto jsonPathType = builder.Resource(JSONPATH_RESOURCE_NAME);
            auto dictType = builder.Dict()->Key<TUtf8>().Value(jsonType).Build();
            auto optionalBoolType = builder.Optional()->Item<bool>().Build();

            if constexpr (ThrowException) {
                builder.Args()
                    ->Add(inputOptionalType)
                    .Add(jsonPathType)
                    .Add(dictType)
                    .Done()
                    .Returns(optionalBoolType);
            } else {
                builder.Args()
                    ->Add(inputOptionalType)
                    .Add(jsonPathType)
                    .Add(dictType)
                    .Add(optionalBoolType)
                    .Done()
                    .Returns(optionalBoolType);
            }

            if (!typesOnly) {
                builder.Implementation(new TSqlExists(builder.GetSourcePosition()));
            }
            if constexpr (!ThrowException) {
                builder.IsStrict();
            }
            return true;
        }

    private:
        TUnboxedValue Run(
            const IValueBuilder* valueBuilder,
            const TUnboxedValuePod* args) const final {
            Y_UNUSED(valueBuilder);
            try {
                if (!args[0].HasValue()) {
                    return TUnboxedValuePod();
                }

                TValue jsonDom;
                if constexpr (InputType == EDataSlot::JsonDocument) {
                    jsonDom = TValue(NBinaryJson::TBinaryJsonReader::Make(args[0].AsStringRef())->GetRootCursor());
                } else {
                    jsonDom = TValue(args[0]);
                }

                auto* jsonPathResource = static_cast<TJsonPathResource*>(args[1].AsBoxed().Get());
                const auto& jsonPath = *jsonPathResource->Get();
                const auto variables = DictToVariables(args[2]);

                const auto result = ExecuteJsonPath(jsonPath, jsonDom, variables, valueBuilder);
                if (result.IsError()) {
                    if constexpr (ThrowException) {
                        ythrow yexception() << "Error executing jsonpath:" << Endl << result.GetError() << Endl;
                    } else {
                        return args[3];
                    }
                }

                return TUnboxedValuePod(!result.GetNodes().empty());
            } catch (const std::exception& e) {
                UdfTerminate((TStringBuilder() << Pos_ << " " << e.what()).data());
            }
        }

        TSourcePosition Pos_;
    };

    template <>
    TStringRef TSqlExists<EDataSlot::Json, false>::Name() {
        return "SqlExists";
    }

    template <>
    TStringRef TSqlExists<EDataSlot::Json, true>::Name() {
        return "SqlTryExists";
    }

    template <>
    TStringRef TSqlExists<EDataSlot::JsonDocument, false>::Name() {
        return "JsonDocumentSqlExists";
    }

    template <>
    TStringRef TSqlExists<EDataSlot::JsonDocument, true>::Name() {
        return "JsonDocumentSqlTryExists";
    }
}