planTestUtil.cpp 17.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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 "planTestUtil.h"
X
Xiaoyu Wang 已提交
17

wafwerar's avatar
wafwerar 已提交
18
#include <getopt.h>
19 20

#include <algorithm>
X
Xiaoyu Wang 已提交
21
#include <array>
22
#include <chrono>
23 24

#include "cmdnodes.h"
X
Xiaoyu Wang 已提交
25
#include "mockCatalogService.h"
26 27
#include "parser.h"
#include "planInt.h"
28
#include "tglobal.h"
29 30 31 32

using namespace std;
using namespace testing;

X
Xiaoyu Wang 已提交
33 34 35 36 37 38 39 40
#define DO_WITH_THROW(func, ...)                                                                                   \
  do {                                                                                                             \
    int32_t code__ = func(__VA_ARGS__);                                                                            \
    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())); \
    }                                                                                                              \
  } while (0);
41

X
Xiaoyu Wang 已提交
42 43
enum DumpModule {
  DUMP_MODULE_NOTHING = 1,
44
  DUMP_MODULE_SQL,
X
Xiaoyu Wang 已提交
45 46 47 48 49 50 51 52 53 54 55
  DUMP_MODULE_PARSER,
  DUMP_MODULE_LOGIC,
  DUMP_MODULE_OPTIMIZED,
  DUMP_MODULE_SPLIT,
  DUMP_MODULE_SCALED,
  DUMP_MODULE_PHYSICAL,
  DUMP_MODULE_SUBPLAN,
  DUMP_MODULE_ALL
};

DumpModule g_dumpModule = DUMP_MODULE_NOTHING;
56
int32_t    g_skipSql = 0;
57
int32_t    g_limitSql = 0;
58
int32_t    g_logLevel = 131;
59
int32_t    g_queryPolicy = QUERY_POLICY_VNODE;
60
bool       g_useNodeAllocator = false;
X
Xiaoyu Wang 已提交
61 62 63 64

void setDumpModule(const char* pModule) {
  if (NULL == pModule) {
    g_dumpModule = DUMP_MODULE_ALL;
65 66
  } else if (0 == strncasecmp(pModule, "sql", strlen(pModule))) {
    g_dumpModule = DUMP_MODULE_SQL;
X
Xiaoyu Wang 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
  } else if (0 == strncasecmp(pModule, "parser", strlen(pModule))) {
    g_dumpModule = DUMP_MODULE_PARSER;
  } else if (0 == strncasecmp(pModule, "logic", strlen(pModule))) {
    g_dumpModule = DUMP_MODULE_LOGIC;
  } else if (0 == strncasecmp(pModule, "optimized", strlen(pModule))) {
    g_dumpModule = DUMP_MODULE_OPTIMIZED;
  } else if (0 == strncasecmp(pModule, "split", strlen(pModule))) {
    g_dumpModule = DUMP_MODULE_SPLIT;
  } else if (0 == strncasecmp(pModule, "scaled", strlen(pModule))) {
    g_dumpModule = DUMP_MODULE_SCALED;
  } else if (0 == strncasecmp(pModule, "physical", strlen(pModule))) {
    g_dumpModule = DUMP_MODULE_PHYSICAL;
  } else if (0 == strncasecmp(pModule, "subplan", strlen(pModule))) {
    g_dumpModule = DUMP_MODULE_SUBPLAN;
  } else if (0 == strncasecmp(pModule, "all", strlen(pModule))) {
    g_dumpModule = DUMP_MODULE_PHYSICAL;
  }
}
X
Xiaoyu Wang 已提交
85

86 87 88 89 90
void setSkipSqlNum(const char* pArg) { g_skipSql = stoi(pArg); }
void setLimitSqlNum(const char* pArg) { g_limitSql = stoi(pArg); }
void setLogLevel(const char* pArg) { g_logLevel = stoi(pArg); }
void setQueryPolicy(const char* pArg) { g_queryPolicy = stoi(pArg); }
void setUseNodeAllocator(const char* pArg) { g_useNodeAllocator = stoi(pArg); }
91 92 93

int32_t getLogLevel() { return g_logLevel; }

94
class PlannerTestBaseImpl {
X
Xiaoyu Wang 已提交
95
 public:
96
  PlannerTestBaseImpl() : sqlNo_(0), sqlNum_(0) {}
97

X
Xiaoyu Wang 已提交
98 99 100
  void useDb(const string& user, const string& db) {
    caseEnv_.acctId_ = 0;
    caseEnv_.user_ = user;
101
    caseEnv_.db_ = db;
102 103
    caseEnv_.numOfSkipSql_ = g_skipSql;
    caseEnv_.numOfLimitSql_ = g_limitSql;
104 105 106
  }

  void run(const string& sql) {
107
    ++sqlNo_;
108 109 110 111 112
    if (caseEnv_.numOfSkipSql_ > 0) {
      --(caseEnv_.numOfSkipSql_);
      return;
    }
    if (caseEnv_.numOfLimitSql_ > 0 && caseEnv_.numOfLimitSql_ == sqlNum_) {
113 114
      return;
    }
115
    ++sqlNum_;
116

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
    switch (g_queryPolicy) {
      case QUERY_POLICY_VNODE:
      case QUERY_POLICY_HYBRID:
      case QUERY_POLICY_QNODE:
        runImpl(sql, g_queryPolicy);
        break;
      default:
        runImpl(sql, QUERY_POLICY_VNODE);
        runImpl(sql, QUERY_POLICY_HYBRID);
        runImpl(sql, QUERY_POLICY_QNODE);
        break;
    }
  }

  void runImpl(const string& sql, int32_t queryPolicy) {
132 133 134 135 136 137
    SNodeAllocator* pAllocator = NULL;
    if (g_useNodeAllocator) {
      nodesCreateNodeAllocator(32 * 1024, &pAllocator);
      nodesResetThreadLevelAllocator(pAllocator);
    }

138
    reset();
139
    tsQueryPolicy = queryPolicy;
140
    try {
141 142 143
      unique_ptr<SQuery*, void (*)(SQuery**)> query((SQuery**)taosMemoryCalloc(1, sizeof(SQuery*)), _destroyQuery);
      doParseSql(sql, query.get());
      SQuery* pQuery = *(query.get());
144 145 146 147

      SPlanContext cxt = {0};
      setPlanContext(pQuery, &cxt);

X
Xiaoyu Wang 已提交
148 149
      SLogicSubplan* pLogicSubplan = nullptr;
      doCreateLogicPlan(&cxt, &pLogicSubplan);
X
Xiaoyu Wang 已提交
150 151
      unique_ptr<SLogicSubplan, void (*)(SLogicSubplan*)> logicSubplan(pLogicSubplan,
                                                                       (void (*)(SLogicSubplan*))nodesDestroyNode);
152

X
Xiaoyu Wang 已提交
153
      doOptimizeLogicPlan(&cxt, pLogicSubplan);
154

X
Xiaoyu Wang 已提交
155
      doSplitLogicPlan(&cxt, pLogicSubplan);
156 157 158

      SQueryLogicPlan* pLogicPlan = nullptr;
      doScaleOutLogicPlan(&cxt, pLogicSubplan, &pLogicPlan);
X
Xiaoyu Wang 已提交
159 160
      unique_ptr<SQueryLogicPlan, void (*)(SQueryLogicPlan*)> logicPlan(pLogicPlan,
                                                                        (void (*)(SQueryLogicPlan*))nodesDestroyNode);
161 162

      SQueryPlan* pPlan = nullptr;
X
Xiaoyu Wang 已提交
163
      doCreatePhysiPlan(&cxt, pLogicPlan, &pPlan);
X
Xiaoyu Wang 已提交
164
      unique_ptr<SQueryPlan, void (*)(SQueryPlan*)> plan(pPlan, (void (*)(SQueryPlan*))nodesDestroyNode);
X
Xiaoyu Wang 已提交
165

X
Xiaoyu Wang 已提交
166
      dump(g_dumpModule);
167
    } catch (...) {
X
Xiaoyu Wang 已提交
168
      dump(DUMP_MODULE_ALL);
169 170
      nodesDestroyNodeAllocator(pAllocator);
      nodesResetThreadLevelAllocator(NULL);
171 172
      throw;
    }
173 174 175

    nodesDestroyNodeAllocator(pAllocator);
    nodesResetThreadLevelAllocator(NULL);
176 177
  }

178
  void prepare(const string& sql) {
179
    if (caseEnv_.numOfSkipSql_ > 0) {
180 181 182
      return;
    }

183 184 185 186 187 188 189 190 191 192
    reset();
    try {
      doParseSql(sql, &stmtEnv_.pQuery_, true);
    } catch (...) {
      dump(DUMP_MODULE_ALL);
      throw;
    }
  }

  void bindParams(TAOS_MULTI_BIND* pParams, int32_t colIdx) {
193
    if (caseEnv_.numOfSkipSql_ > 0) {
194 195 196
      return;
    }

197 198
    try {
      doBindParams(stmtEnv_.pQuery_, pParams, colIdx);
199 200 201 202 203 204 205
    } catch (...) {
      dump(DUMP_MODULE_ALL);
      throw;
    }
  }

  void exec() {
206 207
    if (caseEnv_.numOfSkipSql_ > 0) {
      --(caseEnv_.numOfSkipSql_);
208 209 210
      return;
    }

211 212
    try {
      doParseBoundSql(stmtEnv_.pQuery_);
213 214 215 216

      SPlanContext cxt = {0};
      setPlanContext(stmtEnv_.pQuery_, &cxt);

X
Xiaoyu Wang 已提交
217 218
      SLogicSubplan* pLogicSubplan = nullptr;
      doCreateLogicPlan(&cxt, &pLogicSubplan);
219 220
      unique_ptr<SLogicSubplan, void (*)(SLogicSubplan*)> logicSubplan(pLogicSubplan,
                                                                       (void (*)(SLogicSubplan*))nodesDestroyNode);
221

X
Xiaoyu Wang 已提交
222
      doOptimizeLogicPlan(&cxt, pLogicSubplan);
223

X
Xiaoyu Wang 已提交
224
      doSplitLogicPlan(&cxt, pLogicSubplan);
225 226 227

      SQueryLogicPlan* pLogicPlan = nullptr;
      doScaleOutLogicPlan(&cxt, pLogicSubplan, &pLogicPlan);
228 229
      unique_ptr<SQueryLogicPlan, void (*)(SQueryLogicPlan*)> logicPlan(pLogicPlan,
                                                                        (void (*)(SQueryLogicPlan*))nodesDestroyNode);
230 231 232

      SQueryPlan* pPlan = nullptr;
      doCreatePhysiPlan(&cxt, pLogicPlan, &pPlan);
233
      unique_ptr<SQueryPlan, void (*)(SQueryPlan*)> plan(pPlan, (void (*)(SQueryPlan*))nodesDestroyNode);
234

235 236
      checkPlanMsg((SNode*)pPlan);

237 238 239 240 241 242 243
      dump(g_dumpModule);
    } catch (...) {
      dump(DUMP_MODULE_ALL);
      throw;
    }
  }

X
Xiaoyu Wang 已提交
244
 private:
245
  struct caseEnv {
X
Xiaoyu Wang 已提交
246 247
    int32_t acctId_;
    string  user_;
248
    string  db_;
249 250
    int32_t numOfSkipSql_;
    int32_t numOfLimitSql_;
251

252
    caseEnv() : numOfSkipSql_(0) {}
253 254 255
  };

  struct stmtEnv {
X
Xiaoyu Wang 已提交
256
    string            sql_;
257
    array<char, 1024> msgBuf_;
258 259
    SQuery*           pQuery_;

260
    stmtEnv() : pQuery_(nullptr) {}
261
    ~stmtEnv() { qDestroyQuery(pQuery_); }
262 263 264
  };

  struct stmtRes {
X
Xiaoyu Wang 已提交
265
    string         ast_;
266 267
    string         prepareAst_;
    string         boundAst_;
X
Xiaoyu Wang 已提交
268 269 270 271 272
    string         rawLogicPlan_;
    string         optimizedLogicPlan_;
    string         splitLogicPlan_;
    string         scaledLogicPlan_;
    string         physiPlan_;
X
Xiaoyu Wang 已提交
273
    vector<string> physiSubplans_;
274 275
  };

276 277 278 279 280 281 282 283
  static void _destroyQuery(SQuery** pQuery) {
    if (nullptr == pQuery) {
      return;
    }
    qDestroyQuery(*pQuery);
    taosMemoryFree(pQuery);
  }

284 285 286
  void reset() {
    stmtEnv_.sql_.clear();
    stmtEnv_.msgBuf_.fill(0);
287
    qDestroyQuery(stmtEnv_.pQuery_);
288 289

    res_.ast_.clear();
290
    res_.boundAst_.clear();
291 292 293 294 295
    res_.rawLogicPlan_.clear();
    res_.optimizedLogicPlan_.clear();
    res_.splitLogicPlan_.clear();
    res_.scaledLogicPlan_.clear();
    res_.physiPlan_.clear();
296
    res_.physiSubplans_.clear();
297 298
  }

X
Xiaoyu Wang 已提交
299 300 301 302 303
  void dump(DumpModule module) {
    if (DUMP_MODULE_NOTHING == module) {
      return;
    }

304 305
    cout << "========================================== " << sqlNo_ << " sql : [" << stmtEnv_.sql_ << "]" << endl;

X
Xiaoyu Wang 已提交
306
    if (DUMP_MODULE_ALL == module || DUMP_MODULE_PARSER == module) {
307
      if (res_.prepareAst_.empty()) {
X
Xiaoyu Wang 已提交
308
        cout << "+++++++++++++++++++++syntax tree : " << endl;
309 310
        cout << res_.ast_ << endl;
      } else {
X
Xiaoyu Wang 已提交
311
        cout << "+++++++++++++++++++++prepare syntax tree : " << endl;
312
        cout << res_.prepareAst_ << endl;
X
Xiaoyu Wang 已提交
313
        cout << "+++++++++++++++++++++bound syntax tree : " << endl;
314
        cout << res_.boundAst_ << endl;
X
Xiaoyu Wang 已提交
315
        cout << "+++++++++++++++++++++syntax tree : " << endl;
316 317
        cout << res_.ast_ << endl;
      }
X
Xiaoyu Wang 已提交
318 319 320
    }

    if (DUMP_MODULE_ALL == module || DUMP_MODULE_LOGIC == module) {
X
Xiaoyu Wang 已提交
321
      cout << "+++++++++++++++++++++raw logic plan : " << endl;
X
Xiaoyu Wang 已提交
322 323 324 325
      cout << res_.rawLogicPlan_ << endl;
    }

    if (DUMP_MODULE_ALL == module || DUMP_MODULE_OPTIMIZED == module) {
X
Xiaoyu Wang 已提交
326
      cout << "+++++++++++++++++++++optimized logic plan : " << endl;
X
Xiaoyu Wang 已提交
327 328 329 330
      cout << res_.optimizedLogicPlan_ << endl;
    }

    if (DUMP_MODULE_ALL == module || DUMP_MODULE_SPLIT == module) {
X
Xiaoyu Wang 已提交
331
      cout << "+++++++++++++++++++++split logic plan : " << endl;
X
Xiaoyu Wang 已提交
332 333 334 335
      cout << res_.splitLogicPlan_ << endl;
    }

    if (DUMP_MODULE_ALL == module || DUMP_MODULE_SCALED == module) {
X
Xiaoyu Wang 已提交
336
      cout << "+++++++++++++++++++++scaled logic plan : " << endl;
X
Xiaoyu Wang 已提交
337 338 339 340
      cout << res_.scaledLogicPlan_ << endl;
    }

    if (DUMP_MODULE_ALL == module || DUMP_MODULE_PHYSICAL == module) {
X
Xiaoyu Wang 已提交
341
      cout << "+++++++++++++++++++++physical plan : " << endl;
X
Xiaoyu Wang 已提交
342 343 344 345
      cout << res_.physiPlan_ << endl;
    }

    if (DUMP_MODULE_ALL == module || DUMP_MODULE_SUBPLAN == module) {
X
Xiaoyu Wang 已提交
346
      cout << "+++++++++++++++++++++physical subplan : " << endl;
X
Xiaoyu Wang 已提交
347 348 349
      for (const auto& subplan : res_.physiSubplans_) {
        cout << subplan << endl;
      }
X
Xiaoyu Wang 已提交
350
    }
351
  }
X
Xiaoyu Wang 已提交
352

353
  void doParseSql(const string& sql, SQuery** pQuery, bool prepare = false) {
354 355
    stmtEnv_.sql_ = sql;
    transform(stmtEnv_.sql_.begin(), stmtEnv_.sql_.end(), stmtEnv_.sql_.begin(), ::tolower);
X
Xiaoyu Wang 已提交
356

357
    SParseContext cxt = {0};
X
Xiaoyu Wang 已提交
358
    cxt.acctId = caseEnv_.acctId_;
359 360 361 362 363
    cxt.db = caseEnv_.db_.c_str();
    cxt.pSql = stmtEnv_.sql_.c_str();
    cxt.sqlLen = stmtEnv_.sql_.length();
    cxt.pMsg = stmtEnv_.msgBuf_.data();
    cxt.msgLen = stmtEnv_.msgBuf_.max_size();
364
    cxt.svrVer = "3.0.0.0";
365
    cxt.enableSysInfo = true;
X
Xiaoyu Wang 已提交
366 367 368 369
    if (prepare) {
      SStmtCallback stmtCb = {0};
      cxt.pStmtCb = &stmtCb;
    }
X
Xiaoyu Wang 已提交
370

X
Xiaoyu Wang 已提交
371
    DO_WITH_THROW(qParseSql, &cxt, pQuery);
372
    if (prepare) {
373
      res_.prepareAst_ = toString((*pQuery)->pPrepareRoot);
374 375 376 377 378 379 380 381 382 383 384 385 386 387
    } else {
      res_.ast_ = toString((*pQuery)->pRoot);
    }
  }

  void doBindParams(SQuery* pQuery, TAOS_MULTI_BIND* pParams, int32_t colIdx) {
    DO_WITH_THROW(qStmtBindParams, pQuery, pParams, colIdx);
    if (colIdx < 0 || pQuery->placeholderNum == colIdx + 1) {
      res_.boundAst_ = toString(pQuery->pRoot);
    }
  }

  void doParseBoundSql(SQuery* pQuery) {
    SParseContext cxt = {0};
X
Xiaoyu Wang 已提交
388
    cxt.acctId = caseEnv_.acctId_;
389 390 391 392 393
    cxt.db = caseEnv_.db_.c_str();
    cxt.pSql = stmtEnv_.sql_.c_str();
    cxt.sqlLen = stmtEnv_.sql_.length();
    cxt.pMsg = stmtEnv_.msgBuf_.data();
    cxt.msgLen = stmtEnv_.msgBuf_.max_size();
X
Xiaoyu Wang 已提交
394
    cxt.pUser = caseEnv_.user_.c_str();
395 396 397

    DO_WITH_THROW(qStmtParseQuerySql, &cxt, pQuery);
    res_.ast_ = toString(pQuery->pRoot);
398 399
  }

X
Xiaoyu Wang 已提交
400 401 402
  void doCreateLogicPlan(SPlanContext* pCxt, SLogicSubplan** pLogicSubplan) {
    DO_WITH_THROW(createLogicPlan, pCxt, pLogicSubplan);
    res_.rawLogicPlan_ = toString((SNode*)(*pLogicSubplan));
403 404
  }

X
Xiaoyu Wang 已提交
405 406 407
  void doOptimizeLogicPlan(SPlanContext* pCxt, SLogicSubplan* pLogicSubplan) {
    DO_WITH_THROW(optimizeLogicPlan, pCxt, pLogicSubplan);
    res_.optimizedLogicPlan_ = toString((SNode*)pLogicSubplan);
408 409
  }

X
Xiaoyu Wang 已提交
410 411 412
  void doSplitLogicPlan(SPlanContext* pCxt, SLogicSubplan* pLogicSubplan) {
    DO_WITH_THROW(splitLogicPlan, pCxt, pLogicSubplan);
    res_.splitLogicPlan_ = toString((SNode*)(pLogicSubplan));
413 414 415 416 417 418 419
  }

  void doScaleOutLogicPlan(SPlanContext* pCxt, SLogicSubplan* pLogicSubplan, SQueryLogicPlan** pLogicPlan) {
    DO_WITH_THROW(scaleOutLogicPlan, pCxt, pLogicSubplan, pLogicPlan);
    res_.scaledLogicPlan_ = toString((SNode*)(*pLogicPlan));
  }

X
Xiaoyu Wang 已提交
420
  void doCreatePhysiPlan(SPlanContext* pCxt, SQueryLogicPlan* pLogicPlan, SQueryPlan** pPlan) {
X
Xiaoyu Wang 已提交
421 422 423
    unique_ptr<SArray, void (*)(SArray*)> execNodeList((SArray*)taosArrayInit(TARRAY_MIN_SIZE, sizeof(SQueryNodeAddr)),
                                                       (void (*)(SArray*))taosArrayDestroy);
    DO_WITH_THROW(createPhysiPlan, pCxt, pLogicPlan, pPlan, execNodeList.get());
424
    res_.physiPlan_ = toString((SNode*)(*pPlan));
X
Xiaoyu Wang 已提交
425 426 427
    SNode* pNode;
    FOREACH(pNode, (*pPlan)->pSubplans) {
      SNode* pSubplan;
X
Xiaoyu Wang 已提交
428
      FOREACH(pSubplan, ((SNodeListNode*)pNode)->pNodeList) { res_.physiSubplans_.push_back(toString(pSubplan)); }
X
Xiaoyu Wang 已提交
429
    }
430 431 432
  }

  void setPlanContext(SQuery* pQuery, SPlanContext* pCxt) {
433
    pCxt->queryId = 1;
X
Xiaoyu Wang 已提交
434
    pCxt->pUser = caseEnv_.user_.c_str();
435
    if (QUERY_NODE_CREATE_TOPIC_STMT == nodeType(pQuery->pRoot)) {
436 437 438 439 440
      SCreateTopicStmt* pStmt = (SCreateTopicStmt*)pQuery->pRoot;
      pCxt->pAstRoot = pStmt->pQuery;
      pStmt->pQuery = nullptr;
      nodesDestroyNode(pQuery->pRoot);
      pQuery->pRoot = pCxt->pAstRoot;
441 442 443 444
      pCxt->topicQuery = true;
    } else if (QUERY_NODE_CREATE_INDEX_STMT == nodeType(pQuery->pRoot)) {
      SMCreateSmaReq req = {0};
      tDeserializeSMCreateSmaReq(pQuery->pCmdMsg->pMsg, pQuery->pCmdMsg->msgLen, &req);
X
Xiaoyu Wang 已提交
445
      g_mockCatalogService->createSmaIndex(&req);
446
      nodesStringToNode(req.ast, &pCxt->pAstRoot);
447 448 449
      tFreeSMCreateSmaReq(&req);
      nodesDestroyNode(pQuery->pRoot);
      pQuery->pRoot = pCxt->pAstRoot;
450 451 452 453
      pCxt->streamQuery = true;
    } else if (QUERY_NODE_CREATE_STREAM_STMT == nodeType(pQuery->pRoot)) {
      SCreateStreamStmt* pStmt = (SCreateStreamStmt*)pQuery->pRoot;
      pCxt->pAstRoot = pStmt->pQuery;
454
      pStmt->pQuery = nullptr;
455 456 457
      pCxt->streamQuery = true;
      pCxt->triggerType = pStmt->pOptions->triggerType;
      pCxt->watermark = (NULL != pStmt->pOptions->pWatermark ? ((SValueNode*)pStmt->pOptions->pWatermark)->datum.i : 0);
458 459
      nodesDestroyNode(pQuery->pRoot);
      pQuery->pRoot = pCxt->pAstRoot;
460 461 462 463 464 465
    } else {
      pCxt->pAstRoot = pQuery->pRoot;
    }
  }

  string toString(const SNode* pRoot) {
X
Xiaoyu Wang 已提交
466
    char*   pStr = NULL;
467 468 469 470 471 472 473
    int32_t len = 0;
    DO_WITH_THROW(nodesNodeToString, pRoot, false, &pStr, &len)
    string str(pStr);
    taosMemoryFreeClear(pStr);
    return str;
  }

474
  void checkPlanMsg(const SNode* pRoot) {
475 476 477
    char*   pStr = NULL;
    int32_t len = 0;
    DO_WITH_THROW(nodesNodeToMsg, pRoot, &pStr, &len)
478

479
    string  copyStr(pStr, len);
480 481 482
    SNode*  pNode = NULL;
    char*   pNewStr = NULL;
    int32_t newlen = 0;
483
    DO_WITH_THROW(nodesMsgToNode, copyStr.c_str(), len, &pNode)
484 485 486
    DO_WITH_THROW(nodesNodeToMsg, pNode, &pNewStr, &newlen)
    if (newlen != len || 0 != memcmp(pStr, pNewStr, len)) {
      cout << "nodesNodeToMsg error!!!!!!!!!!!!!! len = " << len << ", newlen = " << newlen << endl;
487 488 489 490
      taosMemoryFreeClear(pNewStr);
      DO_WITH_THROW(nodesNodeToString, pRoot, false, &pNewStr, &newlen)
      cout << "orac node: " << pNewStr << endl;
      taosMemoryFreeClear(pNewStr);
491
      DO_WITH_THROW(nodesNodeToString, pNode, false, &pNewStr, &newlen)
492
      cout << "new node: " << pNewStr << endl;
493
    }
494
    nodesDestroyNode(pNode);
495 496
    taosMemoryFreeClear(pNewStr);

497 498 499
    taosMemoryFreeClear(pStr);
  }

500 501 502
  caseEnv caseEnv_;
  stmtEnv stmtEnv_;
  stmtRes res_;
503
  int32_t sqlNo_;
504
  int32_t sqlNum_;
505 506
};

X
Xiaoyu Wang 已提交
507
PlannerTestBase::PlannerTestBase() : impl_(new PlannerTestBaseImpl()) {}
508

X
Xiaoyu Wang 已提交
509
PlannerTestBase::~PlannerTestBase() {}
510

X
Xiaoyu Wang 已提交
511
void PlannerTestBase::useDb(const std::string& user, const std::string& db) { impl_->useDb(user, db); }
512

X
Xiaoyu Wang 已提交
513
void PlannerTestBase::run(const std::string& sql) { return impl_->run(sql); }
514 515 516 517 518 519 520 521

void PlannerTestBase::prepare(const std::string& sql) { return impl_->prepare(sql); }

void PlannerTestBase::bindParams(TAOS_MULTI_BIND* pParams, int32_t colIdx) {
  return impl_->bindParams(pParams, colIdx);
}

void PlannerTestBase::exec() { return impl_->exec(); }