summaryrefslogtreecommitdiffstats
path: root/yql/essentials/minikql/comp_nodes/mkql_runtime_feature.cpp
blob: 9e791c4d79a1cb14fc69a86b5a3f717a5365f4a5 (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
#include "mkql_runtime_feature.h"

#include <yql/essentials/minikql/computation/mkql_computation_node_holders.h>
#include <yql/essentials/minikql/mkql_node_cast.h>
#include <yql/essentials/minikql/mkql_string_util.h>
#include <yql/essentials/minikql/runtime_settings/runtime_settings.h>
#include <yql/essentials/minikql/runtime_settings/runtime_settings_configuration.h>

namespace NKikimr::NMiniKQL {

namespace {

TMaybe<TString> GetHostSetting(const NYql::TRuntimeSettings& runtimeSettings, TStringBuf name) {
    TMaybe<TString> result;
    NYql::TRuntimeSettingsConfiguration conf(runtimeSettings);
    conf.SerializeStaticSettings([&](const TString& settingName, const TString& value) {
        if (settingName == name) {
            result = value;
        }
    });
    return result;
}

class THostRuntimeSettingWrapper: public TMutableComputationNode<THostRuntimeSettingWrapper> {
    using TBaseComputation = TMutableComputationNode<THostRuntimeSettingWrapper>;

public:
    THostRuntimeSettingWrapper(TComputationMutables& mutables, IComputationNode* featureName)
        : TBaseComputation(mutables)
        , FeatureName_(featureName)
    {
    }

    NUdf::TUnboxedValuePod DoCalculate(TComputationContext& ctx) const {
        const auto nameValue = FeatureName_->GetValue(ctx);
        const TStringBuf name(nameValue.AsStringRef());
        const auto result = GetHostSetting(ctx.RuntimeSettings, name);
        if (!result.Defined()) {
            return NUdf::TUnboxedValuePod();
        }
        return MakeString(NUdf::TStringRef(*result)).MakeOptional();
    }

private:
    void RegisterDependencies() const final {
        DependsOn(FeatureName_);
    }

    IComputationNode* const FeatureName_;
};

class TUdfRuntimeSettingWrapper: public TMutableComputationNode<TUdfRuntimeSettingWrapper> {
    using TBaseComputation = TMutableComputationNode<TUdfRuntimeSettingWrapper>;

public:
    TUdfRuntimeSettingWrapper(TComputationMutables& mutables, IComputationNode* module,
                              IComputationNode* featureName)
        : TBaseComputation(mutables)
        , Module_(module)
        , FeatureName_(featureName)
    {
    }

    NUdf::TUnboxedValuePod DoCalculate(TComputationContext& ctx) const {
        const auto moduleValue = Module_->GetValue(ctx);
        const auto featureNameValue = FeatureName_->GetValue(ctx);
        const TStringBuf result = ctx.RuntimeSettings.GetUdfSetting(
            moduleValue.AsStringRef(),
            featureNameValue.AsStringRef());
        if (result.empty()) {
            return NUdf::TUnboxedValuePod();
        }
        return MakeString(NUdf::TStringRef(result)).MakeOptional();
    }

private:
    void RegisterDependencies() const final {
        DependsOn(Module_);
        DependsOn(FeatureName_);
    }

    IComputationNode* const Module_;
    IComputationNode* const FeatureName_;
};

} // namespace

IComputationNode* WrapHostRuntimeSetting(TCallable& callable, const TComputationNodeFactoryContext& ctx) {
    MKQL_ENSURE(callable.GetInputsCount() == 1, "Expected 1 arg");
    const auto featureName = LocateNode(ctx.NodeLocator, callable, 0);
    return new THostRuntimeSettingWrapper(ctx.Mutables, featureName);
}

IComputationNode* WrapUdfRuntimeSetting(TCallable& callable, const TComputationNodeFactoryContext& ctx) {
    MKQL_ENSURE(callable.GetInputsCount() == 2, "Expected 2 args");
    const auto module = LocateNode(ctx.NodeLocator, callable, 0);
    const auto featureName = LocateNode(ctx.NodeLocator, callable, 1);
    return new TUdfRuntimeSettingWrapper(ctx.Mutables, module, featureName);
}

} // namespace NKikimr::NMiniKQL