aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/sql/settings/translation_settings.cpp
blob: 7d9f4f681e09530c01e5b962944088df00e90594 (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
#include "translation_settings.h"

#include <yql/essentials/core/issue/yql_issue.h>
#include <yql/essentials/utils/utf8.h>

#include <library/cpp/deprecated/split/split_iterator.h>

#include <util/string/split.h>
#include <util/system/env.h>

namespace {
    bool InTestEnvironment() {
        return GetEnv("YQL_LOCAL_ENVIRONMENT") || GetEnv("YA_TEST_RUNNER");
    }

    using namespace NSQLTranslation;
    class TAlwaysDiallowPolicy : public ISqlFeaturePolicy {
    public:
        TAlwaysDiallowPolicy() = default;
        bool Allow() const override {
            return false;
        }
    };

    class TAlwaysAllowPolicy : public ISqlFeaturePolicy {
    public:
        TAlwaysAllowPolicy() = default;
        bool Allow() const override {
            return true;
        }
    };
}

namespace NSQLTranslation {
    ISqlFeaturePolicy::TPtr ISqlFeaturePolicy::MakeAlwaysDisallow() {
        return new TAlwaysDiallowPolicy;
    }

    ISqlFeaturePolicy::TPtr ISqlFeaturePolicy::MakeAlwaysAllow() {
        return new TAlwaysAllowPolicy;
    }

    ISqlFeaturePolicy::TPtr ISqlFeaturePolicy::Make(bool allow) {
        return allow ? MakeAlwaysAllow() : MakeAlwaysDisallow();
    }

    TTranslationSettings::TTranslationSettings()
        : ModuleMapping({{"core", "/lib/yql/core.yql"}})
        , BindingsMode(EBindingsMode::ENABLED)
        , Mode(ESqlMode::QUERY)
        , MaxErrors(SQL_MAX_PARSER_ERRORS)
        , EndOfQueryCommit(true)
        , EnableGenericUdfs(true)
        , SyntaxVersion(1)
        , AnsiLexer(false)
        , Antlr4Parser(false)
        , PgParser(false)
        , InferSyntaxVersion(false)
        , V0Behavior(EV0Behavior::Disable)
        , V0ForceDisable(InTestEnvironment())
        , PGDisable(false)
        , WarnOnV0(true)
        , TestAntlr4(false)
        , V0WarnAsError(ISqlFeaturePolicy::MakeAlwaysDisallow())
        , DqDefaultAuto(ISqlFeaturePolicy::MakeAlwaysDisallow())
        , BlockDefaultAuto(ISqlFeaturePolicy::MakeAlwaysDisallow())
        , AssumeYdbOnClusterWithSlash(false)
    {}


    bool ParseTranslationSettings(const TString& query, TTranslationSettings& settings, NYql::TIssues& issues) {
        if (!NYql::IsUtf8(query)) {
            issues.AddIssue(NYql::YqlIssue(NYql::TPosition(0, 0), NYql::TIssuesIds::DEFAULT_ERROR, "Invalid UTF8 input"));
            return false;
        }

        if (settings.Flags.contains("Antlr4")) {
            settings.Antlr4Parser = true;
        }

        TSplitDelimiters lineDelimiters("\n\r");
        TDelimitersSplit linesSplit(query, lineDelimiters);
        auto lineIterator = linesSplit.Iterator();
        auto lineNumber = 0;

        while (!lineIterator.Eof()) {
            const TString& line = lineIterator.NextString();
            ++lineNumber;

            TVector<TStringBuf> parts;
            TSetDelimiter<const char> partsDelimiters(" \t");
            Split(line, partsDelimiters, parts);

            if (parts.empty()) {
                continue;
            }

            if (!parts[0].StartsWith("--!")) {
                break;
            }

            if (parts.size() > 1) {
                issues.AddIssue(NYql::YqlIssue(NYql::TPosition(0, lineNumber), NYql::TIssuesIds::DEFAULT_ERROR,
                    "Bad translation settings format"));
                return false;
            }

            auto value = TString(parts[0].SubString(3, TString::npos));
            if (value.empty()) {
                continue;
            }

            if ((value == "syntax_v0" || value == "syntax_v1") && !settings.InferSyntaxVersion) {
                issues.AddIssue(NYql::YqlIssue(NYql::TPosition(0, lineNumber), NYql::TIssuesIds::DEFAULT_ERROR,
                    "Explicit syntax version is not allowed"));
                return false;
            }

            if (value == "syntax_v0") {
                if (settings.V0ForceDisable || settings.V0Behavior == NSQLTranslation::EV0Behavior::Disable) {
                    issues.AddIssue(NYql::YqlIssue(NYql::TPosition(0, lineNumber), NYql::TIssuesIds::DEFAULT_ERROR,
                        "V0 syntax is disabled"));
                    return false;
                }

                settings.SyntaxVersion = 0;
            } else if (value == "syntax_v1") {
                settings.SyntaxVersion = 1;
            } else if (value == "ansi_lexer") {
                settings.AnsiLexer = true;
            } else if (value == "antlr4_parser") {
                settings.Antlr4Parser = true;
            } else if (value == "antlr3_parser") {
                settings.Antlr4Parser = false;
            } else if (value == "syntax_pg") {
                settings.PgParser = true;
            } else {
                issues.AddIssue(NYql::YqlIssue(NYql::TPosition(0, lineNumber), NYql::TIssuesIds::DEFAULT_ERROR,
                    TStringBuilder() << "Unknown SQL translation setting: " << value));
                return false;
            }
        }

        return true;
    }

}  // namespace NSQLTranslation