plannerTest.cpp 10.7 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 <algorithm>

#include <gtest/gtest.h>

X
Xiaoyu Wang 已提交
20
#include "cmdnodes.h"
X
Xiaoyu Wang 已提交
21
#include "parser.h"
X
Xiaoyu Wang 已提交
22
#include "planInt.h"
X
Xiaoyu Wang 已提交
23 24 25 26

using namespace std;
using namespace testing;

X
Xiaoyu Wang 已提交
27
class PlannerTest : public Test {
X
Xiaoyu Wang 已提交
28
 protected:
X
Xiaoyu Wang 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
  void setDatabase(const string& acctId, const string& db) {
    acctId_ = acctId;
    db_ = db;
  }

  void bind(const char* sql) {
    reset();
    cxt_.acctId = atoi(acctId_.c_str());
    cxt_.db = db_.c_str();
    sqlBuf_ = string(sql);
    transform(sqlBuf_.begin(), sqlBuf_.end(), sqlBuf_.begin(), ::tolower);
    cxt_.sqlLen = strlen(sql);
    cxt_.pSql = sqlBuf_.c_str();
  }

X
bugfix  
Xiaoyu Wang 已提交
44
  bool run(bool streamQuery = false) {
X
Xiaoyu Wang 已提交
45
    int32_t code = qParseQuerySql(&cxt_, &query_);
X
Xiaoyu Wang 已提交
46

X
Xiaoyu Wang 已提交
47
    if (code != TSDB_CODE_SUCCESS) {
X
Xiaoyu Wang 已提交
48 49
      cout << "sql:[" << cxt_.pSql << "] qParseQuerySql code:" << code << ", strerror:" << tstrerror(code)
           << ", msg:" << errMagBuf_ << endl;
X
Xiaoyu Wang 已提交
50 51
      return false;
    }
X
Xiaoyu Wang 已提交
52

X
Xiaoyu Wang 已提交
53
    const string syntaxTreeStr = toString(query_->pRoot, false);
X
Xiaoyu Wang 已提交
54 55

    SLogicNode*  pLogicNode = nullptr;
H
Haojun Liao 已提交
56 57 58 59 60
    SPlanContext cxt = {0};
    cxt.queryId = 1;
    cxt.acctId = 0;
    cxt.streamQuery = streamQuery;

X
Xiaoyu Wang 已提交
61
    setPlanContext(query_, &cxt);
X
Xiaoyu Wang 已提交
62
    code = createLogicPlan(&cxt, &pLogicNode);
X
Xiaoyu Wang 已提交
63
    if (code != TSDB_CODE_SUCCESS) {
X
Xiaoyu Wang 已提交
64
      cout << "sql:[" << cxt_.pSql << "] createLogicPlan code:" << code << ", strerror:" << tstrerror(code) << endl;
X
Xiaoyu Wang 已提交
65 66
      return false;
    }
X
Xiaoyu Wang 已提交
67

X
Xiaoyu Wang 已提交
68
    cout << "====================sql : [" << cxt_.pSql << "]" << endl;
X
bugfix  
Xiaoyu Wang 已提交
69
    cout << "syntax tree : " << endl;
X
Xiaoyu Wang 已提交
70 71
    cout << syntaxTreeStr << endl;
    cout << "unformatted logic plan : " << endl;
X
Xiaoyu Wang 已提交
72 73
    cout << toString((const SNode*)pLogicNode, false) << endl;

74 75 76 77 78 79
    code = optimizeLogicPlan(&cxt, pLogicNode);
    if (code != TSDB_CODE_SUCCESS) {
      cout << "sql:[" << cxt_.pSql << "] optimizeLogicPlan code:" << code << ", strerror:" << tstrerror(code) << endl;
      return false;
    }

X
Xiaoyu Wang 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93
    SLogicSubplan* pLogicSubplan = nullptr;
    code = splitLogicPlan(&cxt, pLogicNode, &pLogicSubplan);
    if (code != TSDB_CODE_SUCCESS) {
      cout << "sql:[" << cxt_.pSql << "] splitLogicPlan code:" << code << ", strerror:" << tstrerror(code) << endl;
      return false;
    }

    SQueryLogicPlan* pLogicPlan = NULL;
    code = scaleOutLogicPlan(&cxt, pLogicSubplan, &pLogicPlan);
    if (code != TSDB_CODE_SUCCESS) {
      cout << "sql:[" << cxt_.pSql << "] createPhysiPlan code:" << code << ", strerror:" << tstrerror(code) << endl;
      return false;
    }

X
Xiaoyu Wang 已提交
94 95
    SArray* pExecNodeList = taosArrayInit(TARRAY_MIN_SIZE, sizeof(SQueryNodeAddr));
    code = createPhysiPlan(&cxt, pLogicPlan, &plan_, pExecNodeList);
X
Xiaoyu Wang 已提交
96 97 98 99
    if (code != TSDB_CODE_SUCCESS) {
      cout << "sql:[" << cxt_.pSql << "] createPhysiPlan code:" << code << ", strerror:" << tstrerror(code) << endl;
      return false;
    }
X
Xiaoyu Wang 已提交
100

X
Xiaoyu Wang 已提交
101
    cout << "unformatted physical plan : " << endl;
102
    cout << toString((const SNode*)plan_, false) << endl;
X
Xiaoyu Wang 已提交
103
    SNode* pNode;
104
    FOREACH(pNode, plan_->pSubplans) {
X
Xiaoyu Wang 已提交
105 106 107 108
      SNode* pSubplan;
      FOREACH(pSubplan, ((SNodeListNode*)pNode)->pNodeList) {
        cout << "unformatted physical subplan : " << endl;
        cout << toString(pSubplan, false) << endl;
X
Xiaoyu Wang 已提交
109
      }
X
Xiaoyu Wang 已提交
110 111
    }

X
Xiaoyu Wang 已提交
112 113 114
    return true;
  }

X
Xiaoyu Wang 已提交
115
 private:
X
Xiaoyu Wang 已提交
116 117
  static const int max_err_len = 1024;

X
Xiaoyu Wang 已提交
118 119 120 121
  void setPlanContext(SQuery* pQuery, SPlanContext* pCxt) {
    if (QUERY_NODE_CREATE_TOPIC_STMT == nodeType(pQuery->pRoot)) {
      pCxt->pAstRoot = ((SCreateTopicStmt*)pQuery->pRoot)->pQuery;
      pCxt->topicQuery = true;
X
bugfix  
Xiaoyu Wang 已提交
122 123 124 125 126
    } else if (QUERY_NODE_CREATE_INDEX_STMT == nodeType(pQuery->pRoot)) {
      SMCreateSmaReq req = {0};
      tDeserializeSMCreateSmaReq(pQuery->pCmdMsg->pMsg, pQuery->pCmdMsg->msgLen, &req);
      nodesStringToNode(req.ast, &pCxt->pAstRoot);
      pCxt->streamQuery = true;
X
Xiaoyu Wang 已提交
127 128 129 130 131 132
    } else if (QUERY_NODE_CREATE_STREAM_STMT == nodeType(pQuery->pRoot)) {
      SCreateStreamStmt* pStmt = (SCreateStreamStmt*)pQuery->pRoot;
      pCxt->pAstRoot = pStmt->pQuery;
      pCxt->streamQuery = true;
      pCxt->triggerType = pStmt->pOptions->triggerType;
      pCxt->watermark = (NULL != pStmt->pOptions->pWatermark ? ((SValueNode*)pStmt->pOptions->pWatermark)->datum.i : 0);
X
Xiaoyu Wang 已提交
133 134 135 136 137
    } else {
      pCxt->pAstRoot = pQuery->pRoot;
    }
  }

X
Xiaoyu Wang 已提交
138 139 140 141 142 143 144
  void reset() {
    memset(&cxt_, 0, sizeof(cxt_));
    memset(errMagBuf_, 0, max_err_len);
    cxt_.pMsg = errMagBuf_;
    cxt_.msgLen = max_err_len;
  }

X
Xiaoyu Wang 已提交
145
  string toString(const SNode* pRoot, bool format = true) {
X
Xiaoyu Wang 已提交
146
    char*   pStr = NULL;
X
Xiaoyu Wang 已提交
147 148 149 150 151 152 153
    int32_t len = 0;
    int32_t code = nodesNodeToString(pRoot, format, &pStr, &len);
    if (code != TSDB_CODE_SUCCESS) {
      cout << "sql:[" << cxt_.pSql << "] toString code:" << code << ", strerror:" << tstrerror(code) << endl;
      return string();
    }
    string str(pStr);
wafwerar's avatar
wafwerar 已提交
154
    taosMemoryFreeClear(pStr);
X
Xiaoyu Wang 已提交
155 156 157
    return str;
  }

X
Xiaoyu Wang 已提交
158 159 160 161
  string        acctId_;
  string        db_;
  char          errMagBuf_[max_err_len];
  string        sqlBuf_;
X
Xiaoyu Wang 已提交
162
  SParseContext cxt_;
X
Xiaoyu Wang 已提交
163 164
  SQuery*       query_;
  SQueryPlan*   plan_;
X
Xiaoyu Wang 已提交
165 166
};

167
TEST_F(PlannerTest, selectBasic) {
X
Xiaoyu Wang 已提交
168 169 170 171 172
  setDatabase("root", "test");

  bind("SELECT * FROM t1");
  ASSERT_TRUE(run());
}
X
Xiaoyu Wang 已提交
173

X
Xiaoyu Wang 已提交
174 175 176 177 178 179 180
TEST_F(PlannerTest, selectConstant) {
  setDatabase("root", "test");

  bind("SELECT 2-1 FROM t1");
  ASSERT_TRUE(run());
}

181
TEST_F(PlannerTest, selectStableBasic) {
X
Xiaoyu Wang 已提交
182 183 184 185 186 187
  setDatabase("root", "test");

  bind("SELECT * FROM st1");
  ASSERT_TRUE(run());
}

188 189 190
TEST_F(PlannerTest, selectJoin) {
  setDatabase("root", "test");

191 192
  bind("SELECT t1.c1, t2.c2 FROM st1s1 t1, st1s2 t2 where t1.ts = t2.ts");
  ASSERT_TRUE(run());
193

194 195
  bind("SELECT t1.*, t2.* FROM st1s1 t1, st1s2 t2 where t1.ts = t2.ts");
  ASSERT_TRUE(run());
196

X
Xiaoyu Wang 已提交
197 198 199
  bind(
      "SELECT t1.c1, t2.c1 FROM st1s1 t1 join st1s2 t2 on t1.ts = t2.ts where t1.c1 > t2.c1 and t1.c2 = 'abc' and "
      "t2.c2 = 'qwe'");
200 201 202 203
  ASSERT_TRUE(run());
}

TEST_F(PlannerTest, selectGroupBy) {
X
Xiaoyu Wang 已提交
204 205
  setDatabase("root", "test");

X
Xiaoyu Wang 已提交
206 207
  bind("SELECT count(*) FROM t1");
  ASSERT_TRUE(run());
X
Xiaoyu Wang 已提交
208

209 210
  bind("SELECT c1, max(c3), min(c3), count(*) FROM t1 GROUP BY c1");
  ASSERT_TRUE(run());
X
Xiaoyu Wang 已提交
211

212 213
  bind("SELECT c1 + c3, c1 + count(*) FROM t1 where c2 = 'abc' GROUP BY c1, c3");
  ASSERT_TRUE(run());
X
Xiaoyu Wang 已提交
214

215 216
  bind("SELECT c1 + c3, sum(c4 * c5) FROM t1 where concat(c2, 'wwww') = 'abcwww' GROUP BY c1 + c3");
  ASSERT_TRUE(run());
X
Xiaoyu Wang 已提交
217 218 219

  bind("SELECT sum(ceil(c1)) FROM t1 GROUP BY ceil(c1)");
  ASSERT_TRUE(run());
X
Xiaoyu Wang 已提交
220
}
X
Xiaoyu Wang 已提交
221

222
TEST_F(PlannerTest, selectSubquery) {
X
Xiaoyu Wang 已提交
223 224
  setDatabase("root", "test");

X
Xiaoyu Wang 已提交
225 226 227
  bind(
      "SELECT count(*) FROM (SELECT c1 + c3 a, c1 + count(*) b FROM t1 where c2 = 'abc' GROUP BY c1, c3) where a > 100 "
      "group by b");
X
Xiaoyu Wang 已提交
228 229
  ASSERT_TRUE(run());
}
X
Xiaoyu Wang 已提交
230

231
TEST_F(PlannerTest, selectInterval) {
X
Xiaoyu Wang 已提交
232 233
  setDatabase("root", "test");

X
Xiaoyu Wang 已提交
234
  bind("SELECT count(*) FROM t1 interval(10s)");
X
Xiaoyu Wang 已提交
235 236
  ASSERT_TRUE(run());

X
Xiaoyu Wang 已提交
237 238
  bind("SELECT _wstartts, _wduration, _wendts, count(*) FROM t1 interval(10s)");
  ASSERT_TRUE(run());
X
Xiaoyu Wang 已提交
239

X
Xiaoyu Wang 已提交
240 241
  bind("SELECT count(*) FROM t1 interval(10s) fill(linear)");
  ASSERT_TRUE(run());
242

X
Xiaoyu Wang 已提交
243 244
  bind("SELECT count(*), sum(c1) FROM t1 interval(10s) fill(value, 10, 20)");
  ASSERT_TRUE(run());
X
Xiaoyu Wang 已提交
245
}
X
Xiaoyu Wang 已提交
246

247
TEST_F(PlannerTest, selectSessionWindow) {
X
Xiaoyu Wang 已提交
248 249 250 251 252 253
  setDatabase("root", "test");

  bind("SELECT count(*) FROM t1 session(ts, 10s)");
  ASSERT_TRUE(run());
}

254
TEST_F(PlannerTest, selectStateWindow) {
255 256 257 258 259 260 261 262 263
  setDatabase("root", "test");

  bind("SELECT count(*) FROM t1 state_window(c1)");
  ASSERT_TRUE(run());

  bind("SELECT count(*) FROM t1 state_window(c1 + 10)");
  ASSERT_TRUE(run());
}

264
TEST_F(PlannerTest, selectPartitionBy) {
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
  setDatabase("root", "test");

  bind("SELECT * FROM t1 partition by c1");
  ASSERT_TRUE(run());

  bind("SELECT count(*) FROM t1 partition by c1");
  ASSERT_TRUE(run());

  bind("SELECT count(*) FROM t1 partition by c1 group by c2");
  ASSERT_TRUE(run());

  bind("SELECT count(*) FROM st1 partition by tag1, tag2 interval(10s)");
  ASSERT_TRUE(run());
}

280
TEST_F(PlannerTest, selectOrderBy) {
X
Xiaoyu Wang 已提交
281 282
  setDatabase("root", "test");

283
  bind("SELECT c1 FROM t1 order by c1");
X
Xiaoyu Wang 已提交
284 285 286 287 288 289 290
  ASSERT_TRUE(run());

  bind("SELECT c1 FROM t1 order by c2");
  ASSERT_TRUE(run());

  bind("SELECT * FROM t1 order by c1 + 10, c2");
  ASSERT_TRUE(run());
X
Xiaoyu Wang 已提交
291 292 293

  bind("SELECT * FROM t1 order by c1 desc nulls first");
  ASSERT_TRUE(run());
X
Xiaoyu Wang 已提交
294 295
}

296
TEST_F(PlannerTest, selectGroupByOrderBy) {
297 298 299 300 301 302 303 304 305
  setDatabase("root", "test");

  bind("select count(*), sum(c1) from t1 order by sum(c1)");
  ASSERT_TRUE(run());

  bind("select count(*), sum(c1) a from t1 order by a");
  ASSERT_TRUE(run());
}

306
TEST_F(PlannerTest, selectDistinct) {
307 308 309 310 311 312 313 314 315 316 317 318
  setDatabase("root", "test");

  bind("SELECT distinct c1 FROM t1");
  ASSERT_TRUE(run());

  bind("SELECT distinct c1, c2 + 10 FROM t1");
  ASSERT_TRUE(run());

  bind("SELECT distinct c1 + 10 a FROM t1 order by a");
  ASSERT_TRUE(run());
}

319
TEST_F(PlannerTest, selectLimit) {
320 321 322 323 324 325 326 327 328 329 330 331
  setDatabase("root", "test");

  bind("SELECT * FROM t1 limit 2");
  ASSERT_TRUE(run());

  bind("SELECT * FROM t1 limit 5 offset 2");
  ASSERT_TRUE(run());

  bind("SELECT * FROM t1 limit 2, 5");
  ASSERT_TRUE(run());
}

332
TEST_F(PlannerTest, selectSlimit) {
333 334 335 336 337 338 339 340 341 342 343 344
  setDatabase("root", "test");

  bind("SELECT * FROM t1 partition by c1 slimit 2");
  ASSERT_TRUE(run());

  bind("SELECT * FROM t1 partition by c1 slimit 5 soffset 2");
  ASSERT_TRUE(run());

  bind("SELECT * FROM t1 partition by c1 slimit 2, 5");
  ASSERT_TRUE(run());
}

X
Xiaoyu Wang 已提交
345 346 347 348
TEST_F(PlannerTest, showTables) {
  setDatabase("root", "test");

  bind("show tables");
X
Xiaoyu Wang 已提交
349
  ASSERT_TRUE(run());
X
Xiaoyu Wang 已提交
350 351 352 353 354

  setDatabase("root", "information_schema");

  bind("show tables");
  ASSERT_TRUE(run());
X
Xiaoyu Wang 已提交
355 356 357 358 359 360 361
}

TEST_F(PlannerTest, showStables) {
  setDatabase("root", "test");

  bind("show stables");
  ASSERT_TRUE(run());
X
Xiaoyu Wang 已提交
362 363
}

X
Xiaoyu Wang 已提交
364 365 366 367
TEST_F(PlannerTest, createTopic) {
  setDatabase("root", "test");

  bind("create topic tp as SELECT * FROM st1");
X
Xiaoyu Wang 已提交
368 369
  ASSERT_TRUE(run());
}
X
bugfix  
Xiaoyu Wang 已提交
370

X
Xiaoyu Wang 已提交
371
TEST_F(PlannerTest, createStream) {
X
bugfix  
Xiaoyu Wang 已提交
372 373
  setDatabase("root", "test");

X
Xiaoyu Wang 已提交
374 375 376
  bind(
      "create stream if not exists s1 trigger window_close watermark 10s into st1 as select count(*) from t1 "
      "interval(10s)");
X
Xiaoyu Wang 已提交
377
  ASSERT_TRUE(run());
X
bugfix  
Xiaoyu Wang 已提交
378
}
X
bugfix  
Xiaoyu Wang 已提交
379 380 381 382 383 384 385

TEST_F(PlannerTest, createSmaIndex) {
  setDatabase("root", "test");

  bind("create sma index index1 on t1 function(max(c1), min(c3 + 10), sum(c4)) INTERVAL(10s)");
  ASSERT_TRUE(run());
}
386 387 388 389 390 391 392 393 394 395 396 397 398

TEST_F(PlannerTest, explain) {
  setDatabase("root", "test");

  bind("explain SELECT * FROM t1");
  ASSERT_TRUE(run());

  bind("explain analyze SELECT * FROM t1");
  ASSERT_TRUE(run());

  bind("explain analyze verbose true ratio 0.01 SELECT * FROM t1");
  ASSERT_TRUE(run());
}