plannerTest.cpp 4.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 "parser.h"
X
Xiaoyu Wang 已提交
21
#include "planInt.h"
X
Xiaoyu Wang 已提交
22 23 24 25

using namespace std;
using namespace testing;

X
Xiaoyu Wang 已提交
26
class PlannerTest : public Test {
X
Xiaoyu Wang 已提交
27
protected:
X
Xiaoyu Wang 已提交
28 29 30 31 32
  enum TestTarget {
    TEST_LOGIC_PLAN,
    TEST_PHYSICAL_PLAN
  };

X
Xiaoyu Wang 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
  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
Xiaoyu Wang 已提交
48
  bool run(TestTarget target = TEST_PHYSICAL_PLAN) {
X
Xiaoyu Wang 已提交
49
    int32_t code = qParseQuerySql(&cxt_, &query_);
X
Xiaoyu Wang 已提交
50

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

X
Xiaoyu Wang 已提交
56
    const string syntaxTreeStr = toString(query_->pRoot, false);
X
Xiaoyu Wang 已提交
57
  
X
Xiaoyu Wang 已提交
58
    SLogicNode* pLogicPlan = nullptr;
H
Haojun Liao 已提交
59
    SPlanContext cxt = { .queryId = 1, .acctId = 0, .mgmtEpSet = {0}, .pAstRoot = query_->pRoot };
X
Xiaoyu Wang 已提交
60
    code = createLogicPlan(&cxt, &pLogicPlan);
X
Xiaoyu Wang 已提交
61
    if (code != TSDB_CODE_SUCCESS) {
X
Xiaoyu Wang 已提交
62
      cout << "sql:[" << cxt_.pSql << "] logic plan code:" << code << ", strerror:" << tstrerror(code) << endl;
X
Xiaoyu Wang 已提交
63 64
      return false;
    }
X
Xiaoyu Wang 已提交
65
  
X
Xiaoyu Wang 已提交
66
    cout << "====================sql : [" << cxt_.pSql << "]" << endl;
X
Xiaoyu Wang 已提交
67 68 69 70
    cout << "syntax test : " << endl;
    cout << syntaxTreeStr << endl;
    cout << "unformatted logic plan : " << endl;
    cout << toString((const SNode*)pLogicPlan, false) << endl;
X
Xiaoyu Wang 已提交
71 72

    if (TEST_PHYSICAL_PLAN == target) {
X
Xiaoyu Wang 已提交
73
      SQueryPlan* pPlan = nullptr;
74
      code = createPhysiPlan(&cxt, pLogicPlan, &pPlan, NULL);
X
Xiaoyu Wang 已提交
75 76 77 78 79
      if (code != TSDB_CODE_SUCCESS) {
        cout << "sql:[" << cxt_.pSql << "] physical plan code:" << code << ", strerror:" << tstrerror(code) << endl;
        return false;
      }
      cout << "unformatted physical plan : " << endl;
X
Xiaoyu Wang 已提交
80
      cout << toString((const SNode*)pPlan, false) << endl;
X
Xiaoyu Wang 已提交
81 82 83 84 85 86 87 88
      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 已提交
89 90
    }

X
Xiaoyu Wang 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103
    return true;
  }

private:
  static const int max_err_len = 1024;

  void reset() {
    memset(&cxt_, 0, sizeof(cxt_));
    memset(errMagBuf_, 0, max_err_len);
    cxt_.pMsg = errMagBuf_;
    cxt_.msgLen = max_err_len;
  }

X
Xiaoyu Wang 已提交
104 105 106 107 108 109 110 111
  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();
    }
X
Xiaoyu Wang 已提交
112 113 114 115 116 117 118 119
    SNode* pNode;
    code = nodesStringToNode(pStr, &pNode);
    if (code != TSDB_CODE_SUCCESS) {
      tfree(pStr);
      cout << "sql:[" << cxt_.pSql << "] toObject code:" << code << ", strerror:" << tstrerror(code) << endl;
      return string();
    }
    nodesDestroyNode(pNode);
X
Xiaoyu Wang 已提交
120 121 122 123 124
    string str(pStr);
    tfree(pStr);
    return str;
  }

X
Xiaoyu Wang 已提交
125 126 127 128 129
  string acctId_;
  string db_;
  char errMagBuf_[max_err_len];
  string sqlBuf_;
  SParseContext cxt_;
X
Xiaoyu Wang 已提交
130
  SQuery* query_;
X
Xiaoyu Wang 已提交
131 132
};

X
Xiaoyu Wang 已提交
133
TEST_F(PlannerTest, simple) {
X
Xiaoyu Wang 已提交
134 135 136 137 138
  setDatabase("root", "test");

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

X
Xiaoyu Wang 已提交
140 141 142 143 144 145 146
TEST_F(PlannerTest, stSimple) {
  setDatabase("root", "test");

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

X
Xiaoyu Wang 已提交
147
TEST_F(PlannerTest, groupBy) {
X
Xiaoyu Wang 已提交
148 149
  setDatabase("root", "test");

X
Xiaoyu Wang 已提交
150 151
  bind("SELECT count(*) FROM t1");
  ASSERT_TRUE(run());
X
Xiaoyu Wang 已提交
152 153 154 155 156 157 158

  bind("SELECT c1, count(*) FROM t1 GROUP BY c1");
  ASSERT_TRUE(run());

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

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

X
Xiaoyu Wang 已提交
163
TEST_F(PlannerTest, subquery) {
X
Xiaoyu Wang 已提交
164 165 166 167 168
  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 已提交
169 170 171 172 173 174 175

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

  bind("show tables");
  ASSERT_TRUE(run());
}