plannerTest.cpp 6.9 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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
protected:
  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
      cout << "sql:[" << cxt_.pSql << "] parser code:" << code << ", strerror:" << tstrerror(code) << ", msg:" << errMagBuf_ << endl;
X
Xiaoyu Wang 已提交
49 50
      return false;
    }
X
Xiaoyu Wang 已提交
51

X
Xiaoyu Wang 已提交
52
    const string syntaxTreeStr = toString(query_->pRoot, false);
X
Xiaoyu Wang 已提交
53
  
X
Xiaoyu Wang 已提交
54
    SLogicNode* pLogicNode = nullptr;
H
Haojun Liao 已提交
55 56 57 58 59
    SPlanContext cxt = {0};
    cxt.queryId = 1;
    cxt.acctId = 0;
    cxt.streamQuery = streamQuery;

X
Xiaoyu Wang 已提交
60
    setPlanContext(query_, &cxt);
X
Xiaoyu Wang 已提交
61
    code = createLogicPlan(&cxt, &pLogicNode);
X
Xiaoyu Wang 已提交
62
    if (code != TSDB_CODE_SUCCESS) {
X
Xiaoyu Wang 已提交
63
      cout << "sql:[" << cxt_.pSql << "] createLogicPlan code:" << code << ", strerror:" << tstrerror(code) << endl;
X
Xiaoyu Wang 已提交
64 65
      return false;
    }
X
Xiaoyu Wang 已提交
66
  
X
Xiaoyu Wang 已提交
67
    cout << "====================sql : [" << cxt_.pSql << "]" << endl;
X
bugfix  
Xiaoyu Wang 已提交
68
    cout << "syntax tree : " << endl;
X
Xiaoyu Wang 已提交
69 70
    cout << syntaxTreeStr << endl;
    cout << "unformatted logic plan : " << endl;
X
Xiaoyu Wang 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    cout << toString((const SNode*)pLogicNode, false) << endl;

    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;
    }

    SQueryPlan* pPlan = nullptr;
    code = createPhysiPlan(&cxt, pLogicPlan, &pPlan, NULL);
    if (code != TSDB_CODE_SUCCESS) {
      cout << "sql:[" << cxt_.pSql << "] createPhysiPlan code:" << code << ", strerror:" << tstrerror(code) << endl;
      return false;
    }
  
    cout << "unformatted physical plan : " << endl;
    cout << toString((const SNode*)pPlan, false) << endl;
    SNode* pNode;
    FOREACH(pNode, pPlan->pSubplans) {
      SNode* pSubplan;
      FOREACH(pSubplan, ((SNodeListNode*)pNode)->pNodeList) {
        cout << "unformatted physical subplan : " << endl;
        cout << toString(pSubplan, false) << endl;
X
Xiaoyu Wang 已提交
102
      }
X
Xiaoyu Wang 已提交
103 104
    }

X
Xiaoyu Wang 已提交
105 106 107 108 109 110
    return true;
  }

private:
  static const int max_err_len = 1024;

X
Xiaoyu Wang 已提交
111 112 113 114
  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 已提交
115 116 117 118 119
    } 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 已提交
120 121 122 123 124
    } else {
      pCxt->pAstRoot = pQuery->pRoot;
    }
  }

X
Xiaoyu Wang 已提交
125 126 127 128 129 130 131
  void reset() {
    memset(&cxt_, 0, sizeof(cxt_));
    memset(errMagBuf_, 0, max_err_len);
    cxt_.pMsg = errMagBuf_;
    cxt_.msgLen = max_err_len;
  }

X
Xiaoyu Wang 已提交
132 133 134 135 136 137 138 139 140
  string toString(const SNode* pRoot, bool format = true) {
    char* pStr = NULL;
    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 已提交
141
    taosMemoryFreeClear(pStr);
X
Xiaoyu Wang 已提交
142 143 144
    return str;
  }

X
Xiaoyu Wang 已提交
145 146 147 148 149
  string acctId_;
  string db_;
  char errMagBuf_[max_err_len];
  string sqlBuf_;
  SParseContext cxt_;
X
Xiaoyu Wang 已提交
150
  SQuery* query_;
X
Xiaoyu Wang 已提交
151 152
};

X
Xiaoyu Wang 已提交
153
TEST_F(PlannerTest, simple) {
X
Xiaoyu Wang 已提交
154 155 156 157 158
  setDatabase("root", "test");

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

X
Xiaoyu Wang 已提交
160 161 162 163 164 165 166
TEST_F(PlannerTest, stSimple) {
  setDatabase("root", "test");

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

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

X
Xiaoyu Wang 已提交
170 171
  bind("SELECT count(*) FROM t1");
  ASSERT_TRUE(run());
X
Xiaoyu Wang 已提交
172

X
Xiaoyu Wang 已提交
173
  bind("SELECT c1, max(c3), min(c2), count(*) FROM t1 GROUP BY c1");
X
Xiaoyu Wang 已提交
174 175 176 177 178
  ASSERT_TRUE(run());

  bind("SELECT c1 + c3, c1 + count(*) FROM t1 where c2 = 'abc' GROUP BY c1, c3");
  ASSERT_TRUE(run());

X
Xiaoyu Wang 已提交
179
  bind("SELECT c1 + c3, sum(c4 * c5) FROM t1 where concat(c2, 'wwww') = 'abcwww' GROUP BY c1 + c3");
X
Xiaoyu Wang 已提交
180 181
  ASSERT_TRUE(run());
}
X
Xiaoyu Wang 已提交
182

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

  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");
  ASSERT_TRUE(run());
}
X
Xiaoyu Wang 已提交
189

X
Xiaoyu Wang 已提交
190 191 192 193 194
TEST_F(PlannerTest, interval) {
  setDatabase("root", "test");

  bind("SELECT count(*) FROM t1 interval(10s)");
  ASSERT_TRUE(run());
195 196 197

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

X
Xiaoyu Wang 已提交
200 201 202 203 204 205 206
TEST_F(PlannerTest, sessionWindow) {
  setDatabase("root", "test");

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

X
Xiaoyu Wang 已提交
207 208 209 210 211 212 213 214 215 216 217 218 219
TEST_F(PlannerTest, orderBy) {
  setDatabase("root", "test");

  bind("SELECT * FROM t1 order by c1");
  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 已提交
220 221 222 223
TEST_F(PlannerTest, showTables) {
  setDatabase("root", "test");

  bind("show tables");
X
Xiaoyu Wang 已提交
224 225 226 227 228 229 230 231
  ASSERT_TRUE(run());
}

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

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

X
Xiaoyu Wang 已提交
234 235 236 237
TEST_F(PlannerTest, createTopic) {
  setDatabase("root", "test");

  bind("create topic tp as SELECT * FROM st1");
X
Xiaoyu Wang 已提交
238 239
  ASSERT_TRUE(run());
}
X
bugfix  
Xiaoyu Wang 已提交
240 241 242 243 244 245 246

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

  bind("SELECT sum(c1) FROM st1");
  ASSERT_TRUE(run(true));
}
X
bugfix  
Xiaoyu Wang 已提交
247 248 249 250 251 252 253

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());
}