phyPlanTests.cpp 5.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
/*
 * 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 <gtest/gtest.h>

#include "plannerInt.h"
#include "mockCatalogService.h"

using namespace std;
using namespace testing;

void* myCalloc(size_t nmemb, size_t size) {
  if (void* p = calloc(nmemb, size)) {
    return p;
  }
  throw bad_alloc();
}

class PhyPlanTest : public Test {
protected:
  void pushScan(const string& db, const string& table, int32_t scanOp) {
    shared_ptr<MockTableMeta> meta = mockCatalogService->getTableMeta(db, table);
    EXPECT_TRUE(meta);
X
Xiaoyu Wang 已提交
36
    unique_ptr<SQueryPlanNode> scan((SQueryPlanNode*)myCalloc(1, sizeof(SQueryPlanNode)));
37 38 39 40 41 42 43 44 45 46 47
    scan->info.type = scanOp;
    scan->numOfCols = meta->schema->tableInfo.numOfColumns;
    scan->pSchema = (SSchema*)myCalloc(1, sizeof(SSchema) * scan->numOfCols);
    memcpy(scan->pSchema, meta->schema->schema, sizeof(SSchema) * scan->numOfCols);
    //todo 'pExpr' 'numOfExpr'
    scan->pExtInfo = createScanExtInfo(meta);
    pushNode(scan.release());
  }

  int32_t run() {
    SQueryDag* dag = nullptr;
48 49
    uint64_t requestId = 20;
    int32_t code = createDag(logicPlan_.get(), nullptr, &dag, requestId);
50 51 52 53
    dag_.reset(dag);
    return code;
  }

54 55 56 57
  int32_t run(const string& db, const string& sql) {
    SParseContext cxt;
    buildParseContext(db, sql, &cxt);
    SQueryNode* query;
X
Xiaoyu Wang 已提交
58
    int32_t code = qParseQuerySql(&cxt, &query);
59 60 61 62 63
    if (TSDB_CODE_SUCCESS != code) {
      cout << "error no:" << code << ", msg:" << cxt.pMsg << endl;
      return code;
    }
    SQueryDag* dag = nullptr;
64 65
    uint64_t requestId = 20;
    code = qCreateQueryDag(query, &dag, requestId);
66 67 68 69
    dag_.reset(dag);
    return code;
  }

70 71 72 73 74 75 76 77
  void explain() {
    size_t level = taosArrayGetSize(dag_->pSubplans);
    for (size_t i = 0; i < level; ++i) {
      std::cout << "level " << i  << ":" << std::endl;
      const SArray* subplans = (const SArray*)taosArrayGetP(dag_->pSubplans, i);
      size_t num = taosArrayGetSize(subplans);
      for (size_t j = 0; j < num; ++j) {
        std::cout << "no " << j << ":" << std::endl;
78
        int32_t len = 0;
79
        char* str = nullptr;
80 81
        ASSERT_EQ(TSDB_CODE_SUCCESS, qSubPlanToString((const SSubplan*)taosArrayGetP(subplans, j), &str, &len));
        std::cout << "len:" << len << std::endl;
82 83 84 85 86 87
        std::cout << str << std::endl;
        free(str);
      }
    }
  }

L
Liu Jicong 已提交
88
  SQueryDag* result() {
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    return dag_.get();
  }

private:
  void pushNode(SQueryPlanNode* node) {
    if (logicPlan_) {
      // todo
    } else {
      logicPlan_.reset(node);
    }
  }

  void copySchemaMeta(STableMeta** dst, const STableMeta* src) {
    int32_t size = sizeof(STableMeta) + sizeof(SSchema) * (src->tableInfo.numOfTags + src->tableInfo.numOfColumns);
    *dst = (STableMeta*)myCalloc(1, size);
    memcpy(*dst, src, size);
  }

  void copyStorageMeta(SVgroupsInfo** dst, const std::vector<SVgroupInfo>& src) {
    *dst = (SVgroupsInfo*)myCalloc(1, sizeof(SVgroupsInfo) + sizeof(SVgroupMsg) * src.size());
    (*dst)->numOfVgroups = src.size();
    for (int32_t i = 0; i < src.size(); ++i) {
      (*dst)->vgroups[i].vgId = src[i].vgId;
      (*dst)->vgroups[i].numOfEps = src[i].numOfEps;
      memcpy((*dst)->vgroups[i].epAddr, src[i].epAddr, src[i].numOfEps);
    }
  }

  SQueryTableInfo* createScanExtInfo(shared_ptr<MockTableMeta>& meta) {
    SQueryTableInfo* info = (SQueryTableInfo*)myCalloc(1, sizeof(SQueryTableInfo));
    info->pMeta = (STableMetaInfo*)myCalloc(1, sizeof(STableMetaInfo));
    copySchemaMeta(&info->pMeta->pTableMeta, meta->schema.get());
    copyStorageMeta(&info->pMeta->vgroupList, meta->vgs);
    return info;
  }

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
  void buildParseContext(const string& db, const string& sql, SParseContext* pCxt) {
    static string _db;
    static string _sql;
    static const int32_t _msgMaxLen = 4096;
    static char _msg[_msgMaxLen];
  
    _db = db;
    _sql = sql;
    memset(_msg, 0, _msgMaxLen);
  
    pCxt->ctx.acctId = 1;
    pCxt->ctx.db = _db.c_str();
    pCxt->ctx.requestId = 1;
    pCxt->pSql = _sql.c_str();
    pCxt->sqlLen = _sql.length();
    pCxt->pMsg = _msg;
    pCxt->msgLen = _msgMaxLen;
  }

144 145 146 147 148 149 150
  shared_ptr<MockTableMeta> meta_;
  unique_ptr<SQueryPlanNode> logicPlan_;
  unique_ptr<SQueryDag> dag_;
};

// select * from table
TEST_F(PhyPlanTest, tableScanTest) {
151
  pushScan("test", "t1", QNODE_TABLESCAN);
152 153
  ASSERT_EQ(run(), TSDB_CODE_SUCCESS);
  explain();
L
Liu Jicong 已提交
154
  SQueryDag* dag = result();
155 156 157
  // todo check
}

L
Liu Jicong 已提交
158 159 160 161 162 163 164
TEST_F(PhyPlanTest, serializeTest) {
  pushScan("test", "t1", QNODE_TABLESCAN);
  ASSERT_EQ(run(), TSDB_CODE_SUCCESS);
  SQueryDag* dag = result();
  cout << qDagToString(dag) << endl;
}

165 166
// select * from supertable
TEST_F(PhyPlanTest, superTableScanTest) {
167
  pushScan("test", "st1", QNODE_TABLESCAN);
168 169
  ASSERT_EQ(run(), TSDB_CODE_SUCCESS);
  explain();
L
Liu Jicong 已提交
170
  SQueryDag* dag = result();
171 172
  // todo check
}
173 174 175 176 177

// insert into t values(...)
TEST_F(PhyPlanTest, insertTest) {
  ASSERT_EQ(run("test", "insert into t1 values (now, 1, \"beijing\")"), TSDB_CODE_SUCCESS);
  explain();
L
Liu Jicong 已提交
178
  SQueryDag* dag = result();
179 180
  // todo check
}