hints_ut.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #include <yql/essentials/parser/lexer_common/hints.h>
  2. #include <yql/essentials/sql/v1/lexer/lexer.h>
  3. #include <library/cpp/testing/unittest/registar.h>
  4. #include <util/string/join.h>
  5. using namespace NSQLTranslation;
  6. using namespace NSQLTranslationV1;
  7. TSQLHints CollectHints(const TString& query, bool antlr4Parser) {
  8. bool ansi = false;
  9. auto lexer = MakeLexer(ansi, antlr4Parser);
  10. UNIT_ASSERT(lexer);
  11. TSQLHints result;
  12. NYql::TIssues issues;
  13. size_t maxErrors = 100;
  14. UNIT_ASSERT(CollectSqlHints(*lexer, query, "", "", result, issues, maxErrors, false));
  15. UNIT_ASSERT(issues.Empty());
  16. return result;
  17. }
  18. TString SerializeHints(const TVector<TSQLHint>& hints) {
  19. return JoinSeq(",", hints);
  20. }
  21. Y_UNIT_TEST_SUITE(TLexerHintsTests) {
  22. Y_UNIT_TEST(Basic) {
  23. TString query = "/*+ some() */ SELECT /*+ foo(one) */ --+ bar(two)";
  24. auto hintsWithPos = CollectHints(query, false);
  25. UNIT_ASSERT(hintsWithPos.size() == 1);
  26. NYql::TPosition pos = hintsWithPos.begin()->first;
  27. TVector<TSQLHint> hints = hintsWithPos.begin()->second;
  28. UNIT_ASSERT_EQUAL(pos.Row, 1);
  29. UNIT_ASSERT_EQUAL(pos.Column, 15);
  30. TStringBuf expected = R"raw("foo":{"one"},"bar":{"two"})raw";
  31. UNIT_ASSERT_NO_DIFF(SerializeHints(hints), expected);
  32. }
  33. Y_UNIT_TEST(Antlr4) {
  34. TString query = "/*+ some() */ SELECT /*+ foo(one) */ --+ bar(two)";
  35. auto hintsWithPos = CollectHints(query, true);
  36. UNIT_ASSERT(hintsWithPos.size() == 1);
  37. NYql::TPosition pos = hintsWithPos.begin()->first;
  38. TVector<TSQLHint> hints = hintsWithPos.begin()->second;
  39. UNIT_ASSERT_EQUAL(pos.Row, 1);
  40. UNIT_ASSERT_EQUAL(pos.Column, 15);
  41. TStringBuf expected = R"raw("foo":{"one"},"bar":{"two"})raw";
  42. UNIT_ASSERT_NO_DIFF(SerializeHints(hints), expected);
  43. }
  44. }