aboutsummaryrefslogtreecommitdiffstats
path: root/yql/tools/yqlrun/http/sql_tokens_servlet.cpp
blob: aa0fc2fbfca7bca044810ccdeef8672e298b6cd7 (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
#include "sql_tokens_servlet.h"

#include <library/cpp/json/json_reader.h>
#include <yql/essentials/sql/v1/format/sql_format.h>

namespace {

THashSet<TString> ExtractNamesFromJson(const NJson::TJsonValue& json) {
    THashSet<TString> names;
    for (const auto& value : json.GetArraySafe()) {
        names.insert(value["name"].GetStringSafe());
    }
    return names;
}

NJson::TJsonValue LoadJsonResource(TStringBuf filename) {
    TString text;
    Y_ENSURE(NResource::FindExact(filename, &text));
    return NJson::ReadJsonFastTree(text);
}

void OutputJsArray(TStringStream& out, TStringBuf varName, const THashSet<TString>& names) {
    out << varName << " = [";
    for (const auto& name : names) {
        out << '"' << name << "\",";
    }
    out << "];\n";
}

void OutputKnownTokens(TStringStream& out)
{
    // See type_id in the grammar.
    THashSet<TString> compositeTypes = {
        "OPTIONAL",
        "TUPLE",
        "STRUCT",
        "VARIANT",
        "LIST",
        "STREAM",
        "FLOW",
        "DICT",
        "SET",
        "ENUM",
        "RESOURCE",
        "TAGGED",
        "CALLABLE",
    };

    out << "window.sql = {};\n";

    auto kws = NSQLFormat::GetKeywords();
    for (const auto& ty : compositeTypes) {
        kws.erase(ty);
    }
    OutputJsArray(out, "window.sql.keywords", kws);

    THashSet<TString> types = ExtractNamesFromJson(LoadJsonResource("types.json"));
    types.insert(compositeTypes.begin(), compositeTypes.end());
    OutputJsArray(out, "window.sql.types", types);

    THashSet<TString> builtinFuncs = ExtractNamesFromJson(LoadJsonResource("sql_functions.json"));
    OutputJsArray(out, "window.sql.builtinFunctions", builtinFuncs);
}

} // namespace

namespace NYql {
namespace NHttp {

TSqlTokensServlet::TSqlTokensServlet()
{
    TStringStream out;
    OutputKnownTokens(out);
    Script_ = out.Str();
}

void TSqlTokensServlet::DoGet(const TRequest& req, TResponse& resp) const {
    Y_UNUSED(req);

    resp.Body = TBlob::FromString(Script_);
    resp.Headers.AddHeader(THttpInputHeader("Content-Type: text/javascript"));
}

} // namespace NHttp
} // namespace NYql