parTestUtil.cpp 9.8 KB
Newer Older
X
Xiaoyu Wang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "parTestUtil.h"

#include <algorithm>
#include <array>
20
#include <thread>
X
Xiaoyu Wang 已提交
21

22 23
#include "catalog.h"
#include "mockCatalogService.h"
X
Xiaoyu Wang 已提交
24 25 26 27 28
#include "parInt.h"

using namespace std;
using namespace testing;

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
namespace ParserTest {

#define DO_WITH_THROW(func, ...)                                                                                     \
  do {                                                                                                               \
    int32_t code__ = func(__VA_ARGS__);                                                                              \
    if (!checkResultCode(#func, code__)) {                                                                           \
      if (TSDB_CODE_SUCCESS != code__) {                                                                             \
        throw runtime_error("sql:[" + stmtEnv_.sql_ + "] " #func " code:" + to_string(code__) +                      \
                            ", strerror:" + string(tstrerror(code__)) + ", msg:" + string(stmtEnv_.msgBuf_.data())); \
      } else {                                                                                                       \
        throw runtime_error("sql:[" + stmtEnv_.sql_ + "] " #func " expect " + to_string(stmtEnv_.expect_) +          \
                            " actual " + to_string(code__));                                                         \
      }                                                                                                              \
    } else if (TSDB_CODE_SUCCESS != code__) {                                                                        \
      throw TerminateFlag();                                                                                         \
    }                                                                                                                \
X
Xiaoyu Wang 已提交
45 46
  } while (0);

47
bool    g_dump = false;
48
bool    g_testAsyncApis = true;
49 50 51 52
int32_t g_logLevel = 131;
int32_t g_skipSql = 0;

void setAsyncFlag(const char* pFlag) { g_testAsyncApis = stoi(pFlag) > 0 ? true : false; }
wafwerar's avatar
wafwerar 已提交
53
void setSkipSqlNum(const char* pNum) { g_skipSql = stoi(pNum); }
X
Xiaoyu Wang 已提交
54

55 56 57 58
struct TerminateFlag : public exception {
  const char* what() const throw() { return "success and terminate"; }
};

59 60 61 62
void setLogLevel(const char* pLogLevel) { g_logLevel = stoi(pLogLevel); }

int32_t getLogLevel() { return g_logLevel; }

X
Xiaoyu Wang 已提交
63 64
class ParserTestBaseImpl {
 public:
X
Xiaoyu Wang 已提交
65 66
  ParserTestBaseImpl(ParserTestBase* pBase) : pBase_(pBase) {}

67 68
  void login(const std::string& user) { caseEnv_.user_ = user; }

X
Xiaoyu Wang 已提交
69 70 71
  void useDb(const string& acctId, const string& db) {
    caseEnv_.acctId_ = acctId;
    caseEnv_.db_ = db;
72
    caseEnv_.nsql_ = g_skipSql;
X
Xiaoyu Wang 已提交
73 74
  }

75
  void run(const string& sql, int32_t expect, ParserStage checkStage) {
76 77 78 79 80
    if (caseEnv_.nsql_ > 0) {
      --(caseEnv_.nsql_);
      return;
    }

81
    reset(expect, checkStage);
X
Xiaoyu Wang 已提交
82 83 84 85 86 87 88
    try {
      SParseContext cxt = {0};
      setParseContext(sql, &cxt);

      SQuery* pQuery = nullptr;
      doParse(&cxt, &pQuery);

89 90
      doAuthenticate(&cxt, pQuery);

X
Xiaoyu Wang 已提交
91 92 93 94
      doTranslate(&cxt, pQuery);

      doCalculateConstant(&cxt, pQuery);

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
      if (g_dump) {
        dump();
      }
    } catch (const TerminateFlag& e) {
      // success and terminate
      return;
    } catch (...) {
      dump();
      throw;
    }

    if (g_testAsyncApis) {
      runAsync(sql, expect, checkStage);
    }
  }

X
Xiaoyu Wang 已提交
111 112
 private:
  struct caseEnv {
113
    string  acctId_;
114
    string  user_;
115 116
    string  db_;
    int32_t nsql_;
117 118

    caseEnv() : user_("wangxiaoyu"), nsql_(0) {}
X
Xiaoyu Wang 已提交
119 120 121 122 123
  };

  struct stmtEnv {
    string            sql_;
    array<char, 1024> msgBuf_;
124 125
    int32_t           expect_;
    string            checkFunc_;
X
Xiaoyu Wang 已提交
126 127 128 129 130 131 132 133
  };

  struct stmtRes {
    string parsedAst_;
    string translatedAst_;
    string calcConstAst_;
  };

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
  bool checkResultCode(const string& pFunc, int32_t resultCode) {
    return !(stmtEnv_.checkFunc_.empty())
               ? (("*" == stmtEnv_.checkFunc_ || stmtEnv_.checkFunc_ == pFunc) ? stmtEnv_.expect_ == resultCode
                                                                               : TSDB_CODE_SUCCESS == resultCode)
               : true;
  }

  string stageFunc(ParserStage stage) {
    switch (stage) {
      case PARSER_STAGE_PARSE:
        return "parse";
      case PARSER_STAGE_TRANSLATE:
        return "translate";
      case PARSER_STAGE_CALC_CONST:
        return "calculateConstant";
      case PARSER_STAGE_ALL:
        return "*";
      default:
        break;
    }
    return "unknown";
  }

  void reset(int32_t expect, ParserStage checkStage) {
X
Xiaoyu Wang 已提交
158 159
    stmtEnv_.sql_.clear();
    stmtEnv_.msgBuf_.fill(0);
160 161
    stmtEnv_.expect_ = expect;
    stmtEnv_.checkFunc_ = stageFunc(checkStage);
X
Xiaoyu Wang 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177

    res_.parsedAst_.clear();
    res_.translatedAst_.clear();
    res_.calcConstAst_.clear();
  }

  void dump() {
    cout << "==========================================sql : [" << stmtEnv_.sql_ << "]" << endl;
    cout << "raw syntax tree : " << endl;
    cout << res_.parsedAst_ << endl;
    cout << "translated syntax tree : " << endl;
    cout << res_.translatedAst_ << endl;
    cout << "optimized syntax tree : " << endl;
    cout << res_.calcConstAst_ << endl;
  }

178
  void setParseContext(const string& sql, SParseContext* pCxt, bool async = false) {
X
Xiaoyu Wang 已提交
179 180 181 182 183
    stmtEnv_.sql_ = sql;
    transform(stmtEnv_.sql_.begin(), stmtEnv_.sql_.end(), stmtEnv_.sql_.begin(), ::tolower);

    pCxt->acctId = atoi(caseEnv_.acctId_.c_str());
    pCxt->db = caseEnv_.db_.c_str();
184 185
    pCxt->pUser = caseEnv_.user_.c_str();
    pCxt->isSuperUser = caseEnv_.user_ == "root";
X
Xiaoyu Wang 已提交
186 187 188 189
    pCxt->pSql = stmtEnv_.sql_.c_str();
    pCxt->sqlLen = stmtEnv_.sql_.length();
    pCxt->pMsg = stmtEnv_.msgBuf_.data();
    pCxt->msgLen = stmtEnv_.msgBuf_.max_size();
190
    pCxt->async = async;
X
Xiaoyu Wang 已提交
191 192 193 194
  }

  void doParse(SParseContext* pCxt, SQuery** pQuery) {
    DO_WITH_THROW(parse, pCxt, pQuery);
X
Xiaoyu Wang 已提交
195
    ASSERT_NE(*pQuery, nullptr);
X
Xiaoyu Wang 已提交
196 197 198
    res_.parsedAst_ = toString((*pQuery)->pRoot);
  }

199 200 201 202 203
  void doCollectMetaKey(SParseContext* pCxt, SQuery* pQuery) {
    DO_WITH_THROW(collectMetaKey, pCxt, pQuery);
    ASSERT_NE(pQuery->pMetaCache, nullptr);
  }

204 205 206 207 208 209 210 211 212 213 214 215
  void doBuildCatalogReq(const SParseMetaCache* pMetaCache, SCatalogReq* pCatalogReq) {
    DO_WITH_THROW(buildCatalogReq, pMetaCache, pCatalogReq);
  }

  void doGetAllMeta(const SCatalogReq* pCatalogReq, SMetaData* pMetaData) {
    DO_WITH_THROW(g_mockCatalogService->catalogGetAllMeta, pCatalogReq, pMetaData);
  }

  void doPutMetaDataToCache(const SCatalogReq* pCatalogReq, const SMetaData* pMetaData, SParseMetaCache* pMetaCache) {
    DO_WITH_THROW(putMetaDataToCache, pCatalogReq, pMetaData, pMetaCache);
  }

216 217
  void doAuthenticate(SParseContext* pCxt, SQuery* pQuery) { DO_WITH_THROW(authenticate, pCxt, pQuery); }

X
Xiaoyu Wang 已提交
218 219
  void doTranslate(SParseContext* pCxt, SQuery* pQuery) {
    DO_WITH_THROW(translate, pCxt, pQuery);
X
Xiaoyu Wang 已提交
220
    checkQuery(pQuery, PARSER_STAGE_TRANSLATE);
X
Xiaoyu Wang 已提交
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
    res_.translatedAst_ = toString(pQuery->pRoot);
  }

  void doCalculateConstant(SParseContext* pCxt, SQuery* pQuery) {
    DO_WITH_THROW(calculateConstant, pCxt, pQuery);
    res_.calcConstAst_ = toString(pQuery->pRoot);
  }

  string toString(const SNode* pRoot) {
    char*   pStr = NULL;
    int32_t len = 0;
    DO_WITH_THROW(nodesNodeToString, pRoot, false, &pStr, &len)
    string str(pStr);
    taosMemoryFreeClear(pStr);
    return str;
  }

X
Xiaoyu Wang 已提交
238 239
  void checkQuery(const SQuery* pQuery, ParserStage stage) { pBase_->checkDdl(pQuery, stage); }

240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
  void runAsync(const string& sql, int32_t expect, ParserStage checkStage) {
    reset(expect, checkStage);
    try {
      SParseContext cxt = {0};
      setParseContext(sql, &cxt, true);

      SQuery* pQuery = nullptr;
      doParse(&cxt, &pQuery);

      doCollectMetaKey(&cxt, pQuery);

      SCatalogReq catalogReq = {0};
      doBuildCatalogReq(pQuery->pMetaCache, &catalogReq);

      string err;
      thread t1([&]() {
        try {
          SMetaData metaData = {0};
          doGetAllMeta(&catalogReq, &metaData);

          doPutMetaDataToCache(&catalogReq, &metaData, pQuery->pMetaCache);

262 263
          doAuthenticate(&cxt, pQuery);

264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
          doTranslate(&cxt, pQuery);

          doCalculateConstant(&cxt, pQuery);
        } catch (const TerminateFlag& e) {
          // success and terminate
        } catch (const runtime_error& e) {
          err = e.what();
        } catch (...) {
          err = "unknown error";
        }
      });

      t1.join();
      if (!err.empty()) {
        throw runtime_error(err);
      }

      if (g_dump) {
        dump();
      }
    } catch (const TerminateFlag& e) {
      // success and terminate
      return;
    } catch (...) {
      dump();
      throw;
    }
  }

X
Xiaoyu Wang 已提交
293 294 295 296
  caseEnv         caseEnv_;
  stmtEnv         stmtEnv_;
  stmtRes         res_;
  ParserTestBase* pBase_;
X
Xiaoyu Wang 已提交
297 298
};

X
Xiaoyu Wang 已提交
299
ParserTestBase::ParserTestBase() : impl_(new ParserTestBaseImpl(this)) {}
X
Xiaoyu Wang 已提交
300 301 302

ParserTestBase::~ParserTestBase() {}

303 304
void ParserTestBase::login(const std::string& user) { return impl_->login(user); }

X
Xiaoyu Wang 已提交
305 306
void ParserTestBase::useDb(const std::string& acctId, const std::string& db) { impl_->useDb(acctId, db); }

307 308 309 310
void ParserTestBase::run(const std::string& sql, int32_t expect, ParserStage checkStage) {
  return impl_->run(sql, expect, checkStage);
}

X
Xiaoyu Wang 已提交
311 312
void ParserTestBase::checkDdl(const SQuery* pQuery, ParserStage stage) { return; }

313
}  // namespace ParserTest