aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/public/fastcheck/fastcheck.cpp
blob: 46870d95b35bc5a4b0e2636814a2ddadc67909a3 (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
#include "fastcheck.h"
#include <yql/essentials/ast/yql_ast.h>
#include <yql/essentials/ast/yql_expr.h>
#include <yql/essentials/core/services/mounts/yql_mounts.h>
#include <yql/essentials/core/user_data/yql_user_data.h>
#include <yql/essentials/core/yql_type_annotation.h>
#include <yql/essentials/core/yql_user_data_storage.h>
#include <yql/essentials/sql/sql.h>

namespace NYql {
namespace NFastCheck {

bool CheckProgram(const TString& program, const TOptions& options, TIssues& errors) {
    TAstParseResult astRes;
    if (options.IsSql) {
        NSQLTranslation::TTranslationSettings settings;
        settings.ClusterMapping = options.ClusterMapping;
        settings.SyntaxVersion = options.SyntaxVersion;
        settings.V0Behavior = NSQLTranslation::EV0Behavior::Disable;
        if (options.IsLibrary) {
            settings.Mode = NSQLTranslation::ESqlMode::LIBRARY;
        }

        astRes = SqlToYql(program, settings);
    } else {
        astRes = ParseAst(program);
    }

    if (!astRes.IsOk()) {
        errors = std::move(astRes.Issues);
        return false;
    }

    if (options.IsLibrary) {
        return true;
    }

    if (options.ParseOnly) {
        // parse SQL libs
        for (const auto& x : options.SqlLibs) {
            NSQLTranslation::TTranslationSettings settings;
            settings.ClusterMapping = options.ClusterMapping;
            settings.SyntaxVersion = options.SyntaxVersion;
            settings.V0Behavior = NSQLTranslation::EV0Behavior::Disable;
            settings.File = x.first;
            settings.Mode = NSQLTranslation::ESqlMode::LIBRARY;

            astRes = SqlToYql(x.second, settings);
            if (!astRes.IsOk()) {
                errors = std::move(astRes.Issues);
                return false;
            }
        }

        return true;
    }

    TVector<NUserData::TUserData> userData;
    for (const auto& x : options.SqlLibs) {
        NUserData::TUserData data;
        data.Type_ = NUserData::EType::LIBRARY;
        data.Disposition_ = NUserData::EDisposition::INLINE;
        data.Name_ = x.first;
        data.Content_ = x.second;
        userData.push_back(data);
    }

    TExprContext libCtx;
    libCtx.IssueManager.AddIssues(std::move(astRes.Issues));
    IModuleResolver::TPtr moduleResolver;
    TUserDataTable userDataTable = GetYqlModuleResolver(libCtx, moduleResolver, userData, options.ClusterMapping, {});
    if (!userDataTable) {
        errors = libCtx.IssueManager.GetIssues();
        libCtx.IssueManager.Reset();
        return false;
    }

    auto userDataStorage = MakeIntrusive<TUserDataStorage>(nullptr, userDataTable, nullptr, nullptr);
    if (auto modules = dynamic_cast<TModuleResolver*>(moduleResolver.get())) {
        modules->AttachUserData(userDataStorage);
    }

    TExprContext exprCtx(libCtx.NextUniqueId);
    TExprNode::TPtr exprRoot;
    if (!CompileExpr(*astRes.Root, exprRoot, exprCtx, moduleResolver.get(), nullptr, false, Max<ui32>(), options.SyntaxVersion)) {
        errors = exprCtx.IssueManager.GetIssues();
        exprCtx.IssueManager.Reset();
        return false;
    }

    return true;
}

}
}