aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/parser/lexer_common/ut/hints_ut.cpp
blob: 0ddce9336449dbe92c081ad29f132d81d558353a (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
#include <yql/essentials/parser/lexer_common/hints.h>
#include <yql/essentials/sql/v1/lexer/lexer.h>

#include <library/cpp/testing/unittest/registar.h>

#include <util/string/join.h>

using namespace NSQLTranslation;
using namespace NSQLTranslationV1;

TSQLHints CollectHints(const TString& query, bool antlr4Parser) {
    bool ansi = false;
    auto lexer = MakeLexer(ansi, antlr4Parser);
    UNIT_ASSERT(lexer);
    TSQLHints result;
    NYql::TIssues issues;
    size_t maxErrors = 100;
    UNIT_ASSERT(CollectSqlHints(*lexer, query, "", "", result, issues, maxErrors, false));
    UNIT_ASSERT(issues.Empty());
    return result;
}

TString SerializeHints(const TVector<TSQLHint>& hints) {
    return JoinSeq(",", hints);
}

Y_UNIT_TEST_SUITE(TLexerHintsTests) {
    Y_UNIT_TEST(Basic) {
        TString query = "/*+ some() */ SELECT /*+ foo(one) */ --+ bar(two)";
        auto hintsWithPos = CollectHints(query, false);
        UNIT_ASSERT(hintsWithPos.size() == 1);
        NYql::TPosition pos = hintsWithPos.begin()->first;
        TVector<TSQLHint> hints = hintsWithPos.begin()->second;

        UNIT_ASSERT_EQUAL(pos.Row, 1);
        UNIT_ASSERT_EQUAL(pos.Column, 15);

        TStringBuf expected = R"raw("foo":{"one"},"bar":{"two"})raw";
        UNIT_ASSERT_NO_DIFF(SerializeHints(hints), expected);
    }
    Y_UNIT_TEST(Antlr4) {
        TString query = "/*+ some() */ SELECT /*+ foo(one) */ --+ bar(two)";
        auto hintsWithPos = CollectHints(query, true);
        UNIT_ASSERT(hintsWithPos.size() == 1);
        NYql::TPosition pos = hintsWithPos.begin()->first;
        TVector<TSQLHint> hints = hintsWithPos.begin()->second;

        UNIT_ASSERT_EQUAL(pos.Row, 1);
        UNIT_ASSERT_EQUAL(pos.Column, 15);

        TStringBuf expected = R"raw("foo":{"one"},"bar":{"two"})raw";
        UNIT_ASSERT_NO_DIFF(SerializeHints(hints), expected);
    }
}