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

#include "resource.h"

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

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

    class TCompilePath: public TBoxedValue {
    public:
        TCompilePath(TSourcePosition pos)
            : Pos_(pos)
        {
        }

        static const TStringRef& Name() {
            static auto name = TStringRef::Of("CompilePath");
            return name;
        }

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

            auto resourceType = builder.Resource(JSONPATH_RESOURCE_NAME);
            builder.Args()
                ->Add<NUdf::TUtf8>()
                .Done()
                .Returns(resourceType);

            if (!typesOnly) {
                builder.Implementation(new TCompilePath(builder.GetSourcePosition()));
            }
            return true;
        }

    private:
        const size_t MaxParseErrors = 10;

        TUnboxedValue Run(
            const IValueBuilder* valueBuilder,
            const TUnboxedValuePod* args) const final {
            Y_UNUSED(valueBuilder);
            try {
                TIssues issues;
                const auto jsonPath = NJsonPath::ParseJsonPath(args[0].AsStringRef(), issues, MaxParseErrors);
                if (!issues.Empty()) {
                    ythrow yexception() << "Error parsing jsonpath:" << Endl << issues.ToString();
                }

                return TUnboxedValuePod(new TJsonPathResource(jsonPath));
            } catch (const std::exception& e) {
                UdfTerminate((TStringBuilder() << Pos_ << " " << e.what()).data());
            }
        }

        TSourcePosition Pos_;
    };
}