TDengine/source/libs/planner/test/plannerTest.cpp

176 lines
4.9 KiB
C++
Raw Normal View History

2022-02-13 18:31:18 +00:00
/*
* 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>
2022-02-28 09:02:43 +00:00
#include "parser.h"
2022-03-10 07:36:06 +00:00
#include "planInt.h"
2022-02-13 18:31:18 +00:00
using namespace std;
using namespace testing;
2022-02-28 10:06:07 +00:00
class PlannerTest : public Test {
2022-02-13 18:31:18 +00:00
protected:
2022-02-23 06:34:18 +00:00
enum TestTarget {
TEST_LOGIC_PLAN,
TEST_PHYSICAL_PLAN
};
2022-02-13 18:31:18 +00:00
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();
}
2022-02-23 06:34:18 +00:00
bool run(TestTarget target = TEST_PHYSICAL_PLAN) {
2022-02-28 09:02:43 +00:00
int32_t code = qParseQuerySql(&cxt_, &query_);
2022-02-21 09:35:30 +00:00
2022-02-13 18:31:18 +00:00
if (code != TSDB_CODE_SUCCESS) {
2022-02-21 09:35:30 +00:00
cout << "sql:[" << cxt_.pSql << "] parser code:" << code << ", strerror:" << tstrerror(code) << ", msg:" << errMagBuf_ << endl;
2022-02-13 18:31:18 +00:00
return false;
}
2022-02-21 09:35:30 +00:00
2022-02-28 09:02:43 +00:00
const string syntaxTreeStr = toString(query_->pRoot, false);
2022-02-21 09:35:30 +00:00
2022-02-13 18:31:18 +00:00
SLogicNode* pLogicPlan = nullptr;
2022-03-11 06:59:42 +00:00
SPlanContext cxt = { .queryId = 1, .acctId = 0, .pAstRoot = query_->pRoot };
2022-02-28 09:56:38 +00:00
code = createLogicPlan(&cxt, &pLogicPlan);
2022-02-13 18:31:18 +00:00
if (code != TSDB_CODE_SUCCESS) {
2022-02-23 06:34:18 +00:00
cout << "sql:[" << cxt_.pSql << "] logic plan code:" << code << ", strerror:" << tstrerror(code) << endl;
2022-02-13 18:31:18 +00:00
return false;
}
2022-02-21 09:35:30 +00:00
2022-02-25 08:29:40 +00:00
cout << "====================sql : [" << cxt_.pSql << "]" << endl;
2022-02-21 09:35:30 +00:00
cout << "syntax test : " << endl;
cout << syntaxTreeStr << endl;
cout << "unformatted logic plan : " << endl;
cout << toString((const SNode*)pLogicPlan, false) << endl;
2022-02-23 06:34:18 +00:00
if (TEST_PHYSICAL_PLAN == target) {
2022-03-02 02:50:51 +00:00
SQueryPlan* pPlan = nullptr;
code = createPhysiPlan(&cxt, pLogicPlan, &pPlan, NULL);
2022-02-23 06:34:18 +00:00
if (code != TSDB_CODE_SUCCESS) {
cout << "sql:[" << cxt_.pSql << "] physical plan code:" << code << ", strerror:" << tstrerror(code) << endl;
return false;
}
cout << "unformatted physical plan : " << endl;
2022-03-02 02:50:51 +00:00
cout << toString((const SNode*)pPlan, false) << endl;
2022-03-02 10:30:09 +00:00
SNode* pNode;
FOREACH(pNode, pPlan->pSubplans) {
SNode* pSubplan;
FOREACH(pSubplan, ((SNodeListNode*)pNode)->pNodeList) {
cout << "unformatted physical subplan : " << endl;
cout << toString(pSubplan, false) << endl;
}
}
2022-02-23 06:34:18 +00:00
}
2022-02-13 18:31:18 +00:00
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;
}
2022-02-21 09:35:30 +00:00
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();
}
2022-03-04 07:17:04 +00:00
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);
2022-02-21 09:35:30 +00:00
string str(pStr);
tfree(pStr);
return str;
}
2022-02-13 18:31:18 +00:00
string acctId_;
string db_;
char errMagBuf_[max_err_len];
string sqlBuf_;
SParseContext cxt_;
2022-02-28 09:02:43 +00:00
SQuery* query_;
2022-02-13 18:31:18 +00:00
};
2022-02-28 10:06:07 +00:00
TEST_F(PlannerTest, simple) {
2022-02-13 18:31:18 +00:00
setDatabase("root", "test");
bind("SELECT * FROM t1");
ASSERT_TRUE(run());
}
2022-02-21 09:35:30 +00:00
2022-03-09 09:32:12 +00:00
TEST_F(PlannerTest, stSimple) {
setDatabase("root", "test");
bind("SELECT * FROM st1");
ASSERT_TRUE(run());
}
2022-02-28 10:06:07 +00:00
TEST_F(PlannerTest, groupBy) {
2022-02-21 09:35:30 +00:00
setDatabase("root", "test");
2022-02-25 08:29:40 +00:00
bind("SELECT count(*) FROM t1");
ASSERT_TRUE(run());
2022-02-21 09:35:30 +00:00
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());
2022-02-25 08:29:40 +00:00
bind("SELECT c1 + c3, sum(c4 * c5) FROM t1 where concat(c2, 'wwww') = 'abcwww' GROUP BY c1 + c3");
2022-02-21 09:35:30 +00:00
ASSERT_TRUE(run());
}
2022-02-23 06:34:18 +00:00
2022-02-28 10:06:07 +00:00
TEST_F(PlannerTest, subquery) {
2022-02-23 06:34:18 +00:00
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());
}
2022-03-13 19:28:50 +00:00
TEST_F(PlannerTest, interval) {
setDatabase("root", "test");
bind("SELECT count(*) FROM t1 interval(10s)");
ASSERT_TRUE(run());
}