aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/providers/common/config/yql_dispatch.cpp
blob: 5a8b2573b1a3febfd00b0835272f7129ba97353c (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include "yql_dispatch.h"

#include <yql/essentials/core/yql_expr_type_annotation.h>
#include <yql/essentials/ast/yql_expr.h>
#include <yql/essentials/utils/log/log.h>

#include <library/cpp/string_utils/levenshtein_diff/levenshtein_diff.h>

#include <util/string/split.h>
#include <util/random/random.h>
#include <util/datetime/base.h>

namespace NYql {

namespace NPrivate {

template <>
TParser<TString> GetDefaultParser<TString>() {
    return [] (const TString& str) {
        return str;
    };
}

template<>
TParser<bool> GetDefaultParser<bool>() {
    // Special handle of empty value to properly set setting for empty pragmas like yt.UseFeautre;
    return [] (const TString& str) {
        return str ? FromString<bool>(str) : true;
    };
}

template <>
TParser<TGUID> GetDefaultParser<TGUID>() {
    return [] (const TString& str) {
        TGUID guid;
        if (!GetGuid(str, guid)) {
            throw yexception() << "Bad GUID format";
        }
        return guid;
    };
}

template<>
TParser<NSize::TSize> GetDefaultParser<NSize::TSize>() {
    return [] (const TString& str) -> NSize::TSize {
        return NSize::ParseSize(str); // Support suffixes k, m, g, ...
    };
}

template<>
TParser<TInstant> GetDefaultParser<TInstant>() {
    return [] (const TString& str) {
        TInstant val;
        if (!TInstant::TryParseIso8601(str, val)
            && !TInstant::TryParseRfc822(str, val)
            && !TInstant::TryParseHttp(str, val)
            && !TInstant::TryParseX509(str, val)
        ) {
            throw yexception() << "Bad date/time format";
        }
        return val;
    };
}

#define YQL_DEFINE_PRIMITIVE_SETTING_PARSER(type)                       \
    template<>                                                          \
    TParser<type> GetDefaultParser<type>() {                            \
        return [] (const TString& s) { return FromString<type>(s); };   \
    }

#define YQL_DEFINE_CONTAINER_SETTING_PARSER(type)                       \
    template <>                                                         \
    TParser<type> GetDefaultParser<type>() {                            \
        return [] (const TString& str) {                                \
            type res;                                                   \
            StringSplitter(str).SplitBySet(",;| ").AddTo(&res);         \
            for (auto& s: res) {                                        \
                if (s.empty()) {                                        \
                    throw yexception() << "Empty value item";           \
                }                                                       \
            }                                                           \
            return res;                                                 \
        };                                                              \
    }

YQL_PRIMITIVE_SETTING_PARSER_TYPES(YQL_DEFINE_PRIMITIVE_SETTING_PARSER)
YQL_CONTAINER_SETTING_PARSER_TYPES(YQL_DEFINE_CONTAINER_SETTING_PARSER)

} // NPrivate

namespace NCommon {

bool TSettingDispatcher::IsRuntime(const TString& name) {
    auto normalizedName = NormalizeName(name);
    if (auto handler = Handlers.Value(normalizedName, TSettingHandler::TPtr())) {
        return handler->IsRuntime();
    }
    return false;
}

bool TSettingDispatcher::Dispatch(const TString& cluster, const TString& name, const TMaybe<TString>& value, EStage stage, const TErrorCallback& errorCallback) {
    auto normalizedName = NormalizeName(name);
    if (auto handler = Handlers.Value(normalizedName, TSettingHandler::TPtr())) {
        if (cluster != ALL_CLUSTERS) {
            if (!handler->IsRuntime()) {
                return errorCallback(TStringBuilder() << "Static setting " << name.Quote() << " cannot be set for specific cluster", true);
            }
            if (!ValidClusters.contains(cluster)) {
                TStringBuilder nearClusterMsg;
                for (auto& item: ValidClusters) {
                    if (NLevenshtein::Distance(cluster, item) < DefaultMistypeDistance) {
                        nearClusterMsg << ", did you mean " << item.Quote() << '?';
                        break;
                    }
                }
                return errorCallback(TStringBuilder() << "Unknown cluster name " << cluster.Quote()
                    << " for setting " << name.Quote() << nearClusterMsg, true);
            }
        }
        if (!value && !handler->IsRuntime()) {
            return errorCallback(TStringBuilder() << "Static setting " << name.Quote() << " cannot be reset to default", true);
        }

        bool validateOnly = true;
        switch (stage) {
        case EStage::RUNTIME:
            validateOnly = !handler->IsRuntime();
            break;
        case EStage::STATIC:
            validateOnly = handler->IsRuntime();
            break;
        case EStage::CONFIG:
            validateOnly = false;
            break;
        }

        return handler->Handle(cluster, value, validateOnly, errorCallback);
    } else {
        // ignore unknown names in config
        if (stage == EStage::CONFIG) {
            return true;
        }

        TStringBuilder nearHandlerMsg;
        for (auto& item: Handlers) {
            if (NLevenshtein::Distance(normalizedName, item.first) < DefaultMistypeDistance) {
                nearHandlerMsg << ", did you mean " << item.second->GetDisplayName().Quote() << '?';
                break;
            }
        }
        return errorCallback(TStringBuilder() << "Unknown setting name " << name.Quote() << nearHandlerMsg, true);
    }
}

void TSettingDispatcher::FreezeDefaults() {
    for (auto& item: Handlers) {
        item.second->FreezeDefault();
    }
}

void TSettingDispatcher::Restore() {
    for (auto& item: Handlers) {
        item.second->Restore(ALL_CLUSTERS);
    }
}

void TSettingDispatcher::Enumerate(std::function<void(std::string_view)> callback) {
    for (const auto& name : Names) {
        callback(name);
    }
}

TSettingDispatcher::TErrorCallback TSettingDispatcher::GetDefaultErrorCallback() {
    return [] (const TString& msg, bool isError) -> bool {
        if (isError) {
            YQL_LOG(ERROR) << msg;
            throw yexception() << msg;
        }
        YQL_LOG(WARN) << msg;
        return true;
    };
}

TSettingDispatcher::TErrorCallback TSettingDispatcher::GetErrorCallback(TPositionHandle pos, TExprContext& ctx) {
    return [pos, &ctx](const TString& msg, bool isError) -> bool {
        if (isError) {
            ctx.AddError(YqlIssue(ctx.GetPosition(pos), TIssuesIds::DEFAULT_ERROR, msg));
            return false;
        } else {
            return ctx.AddWarning(YqlIssue(ctx.GetPosition(pos), TIssuesIds::YQL_PRAGMA_WARNING_MSG, msg));
        }
    };
}


} // NCommon
} // NYql