parInsertTest.cpp 10.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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>

18
#include "mockCatalogService.h"
wafwerar's avatar
wafwerar 已提交
19
#include "os.h"
X
Xiaoyu Wang 已提交
20
#include "parInt.h"
21 22 23 24 25

using namespace std;
using namespace testing;

namespace {
X
Xiaoyu Wang 已提交
26 27
string toString(int32_t code) { return tstrerror(code); }
}  // namespace
28 29 30 31 32 33 34 35 36

// syntax:
// INSERT INTO
//   tb_name
//       [USING stb_name [(tag1_name, ...)] TAGS (tag1_value, ...)]
//       [(field1_name, ...)]
//       VALUES (field1_value, ...) [(field1_value2, ...) ...] | FILE csv_file_path
//   [...];
class InsertTest : public Test {
X
Xiaoyu Wang 已提交
37
 protected:
X
Xiaoyu Wang 已提交
38
  InsertTest() : res_(nullptr) {}
39 40
  ~InsertTest() { reset(); }

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();
H
Haojun Liao 已提交
48
    cxt_.acctId = atoi(acctId_.c_str());
X
Xiaoyu Wang 已提交
49
    cxt_.db = (char*)db_.c_str();
50 51 52 53 54 55 56
    strcpy(sqlBuf_, sql);
    cxt_.sqlLen = strlen(sql);
    sqlBuf_[cxt_.sqlLen] = '\0';
    cxt_.pSql = sqlBuf_;
  }

  int32_t run() {
57
    code_ = parseInsertSql(&cxt_, &res_, nullptr);
58 59 60 61 62 63
    if (code_ != TSDB_CODE_SUCCESS) {
      cout << "code:" << toString(code_) << ", msg:" << errMagBuf_ << endl;
    }
    return code_;
  }

64
  int32_t runAsync() {
X
Xiaoyu Wang 已提交
65
    cxt_.async = true;
66 67
    unique_ptr<SParseMetaCache, void (*)(SParseMetaCache*)> metaCache(new SParseMetaCache(), _destoryParseMetaCache);
    code_ = parseInsertSyntax(&cxt_, &res_, metaCache.get());
68 69 70 71 72
    if (code_ != TSDB_CODE_SUCCESS) {
      cout << "parseInsertSyntax code:" << toString(code_) << ", msg:" << errMagBuf_ << endl;
      return code_;
    }

73 74
    unique_ptr<SCatalogReq, void (*)(SCatalogReq*)> catalogReq(new SCatalogReq(),
                                                               MockCatalogService::destoryCatalogReq);
75
    code_ = buildCatalogReq(metaCache.get(), catalogReq.get());
76 77 78 79 80
    if (code_ != TSDB_CODE_SUCCESS) {
      cout << "buildCatalogReq code:" << toString(code_) << ", msg:" << errMagBuf_ << endl;
      return code_;
    }

81 82
    unique_ptr<SMetaData, void (*)(SMetaData*)> metaData(new SMetaData(), MockCatalogService::destoryMetaData);
    g_mockCatalogService->catalogGetAllMeta(catalogReq.get(), metaData.get());
83

84
    code_ = putMetaDataToCache(catalogReq.get(), metaData.get(), metaCache.get());
85 86 87 88 89
    if (code_ != TSDB_CODE_SUCCESS) {
      cout << "putMetaDataToCache code:" << toString(code_) << ", msg:" << errMagBuf_ << endl;
      return code_;
    }

90
    code_ = parseInsertSql(&cxt_, &res_, metaCache.get());
91 92 93 94 95 96 97 98
    if (code_ != TSDB_CODE_SUCCESS) {
      cout << "parseInsertSql code:" << toString(code_) << ", msg:" << errMagBuf_ << endl;
      return code_;
    }

    return code_;
  }

99
  void dumpReslut() {
X
Xiaoyu Wang 已提交
100
    SVnodeModifOpStmt* pStmt = getVnodeModifStmt(res_);
X
Xiaoyu Wang 已提交
101 102 103
    size_t             num = taosArrayGetSize(pStmt->pDataBlocks);
    cout << "payloadType:" << (int32_t)pStmt->payloadType << ", insertType:" << pStmt->insertType
         << ", numOfVgs:" << num << endl;
104
    for (size_t i = 0; i < num; ++i) {
X
Xiaoyu Wang 已提交
105
      SVgDataBlocks* vg = (SVgDataBlocks*)taosArrayGetP(pStmt->pDataBlocks, i);
X
Xiaoyu Wang 已提交
106
      cout << "vgId:" << vg->vg.vgId << ", numOfTables:" << vg->numOfTables << ", dataSize:" << vg->size << endl;
S
Shengliang Guan 已提交
107
      SSubmitReq* submit = (SSubmitReq*)vg->pData;
108
      cout << "length:" << ntohl(submit->length) << ", numOfBlocks:" << ntohl(submit->numOfBlocks) << endl;
X
Xiaoyu Wang 已提交
109
      int32_t     numOfBlocks = ntohl(submit->numOfBlocks);
110 111 112
      SSubmitBlk* blk = (SSubmitBlk*)(submit + 1);
      for (int32_t i = 0; i < numOfBlocks; ++i) {
        cout << "Block:" << i << endl;
C
Cary Xu 已提交
113 114 115
        cout << "\tuid:" << be64toh(blk->uid) << ", tid:" << be64toh(blk->suid) << ", sversion:" << ntohl(blk->sversion)
             << ", dataLen:" << ntohl(blk->dataLen) << ", schemaLen:" << ntohl(blk->schemaLen)
             << ", numOfRows:" << ntohl(blk->numOfRows) << endl;
116 117 118 119 120 121
        blk = (SSubmitBlk*)(blk->data + ntohl(blk->dataLen));
      }
    }
  }

  void checkReslut(int32_t numOfTables, int16_t numOfRows1, int16_t numOfRows2 = -1) {
X
Xiaoyu Wang 已提交
122 123 124 125
    SVnodeModifOpStmt* pStmt = getVnodeModifStmt(res_);
    ASSERT_EQ(pStmt->payloadType, PAYLOAD_TYPE_KV);
    ASSERT_EQ(pStmt->insertType, TSDB_QUERY_TYPE_INSERT);
    size_t num = taosArrayGetSize(pStmt->pDataBlocks);
126 127
    ASSERT_GE(num, 0);
    for (size_t i = 0; i < num; ++i) {
X
Xiaoyu Wang 已提交
128
      SVgDataBlocks* vg = (SVgDataBlocks*)taosArrayGetP(pStmt->pDataBlocks, i);
129 130
      ASSERT_EQ(vg->numOfTables, numOfTables);
      ASSERT_GE(vg->size, 0);
S
Shengliang Guan 已提交
131
      SSubmitReq* submit = (SSubmitReq*)vg->pData;
132 133
      ASSERT_GE(ntohl(submit->length), 0);
      ASSERT_GE(ntohl(submit->numOfBlocks), 0);
X
Xiaoyu Wang 已提交
134
      int32_t     numOfBlocks = ntohl(submit->numOfBlocks);
135 136
      SSubmitBlk* blk = (SSubmitBlk*)(submit + 1);
      for (int32_t i = 0; i < numOfBlocks; ++i) {
C
Cary Xu 已提交
137
        ASSERT_EQ(ntohl(blk->numOfRows), (0 == i ? numOfRows1 : (numOfRows2 > 0 ? numOfRows2 : numOfRows1)));
138 139 140 141 142
        blk = (SSubmitBlk*)(blk->data + ntohl(blk->dataLen));
      }
    }
  }

X
Xiaoyu Wang 已提交
143
 private:
144 145 146
  static const int max_err_len = 1024;
  static const int max_sql_len = 1024 * 1024;

147 148 149 150 151
  static void _destoryParseMetaCache(SParseMetaCache* pMetaCache) {
    destoryParseMetaCache(pMetaCache);
    delete pMetaCache;
  }

152 153 154 155 156 157
  void reset() {
    memset(&cxt_, 0, sizeof(cxt_));
    memset(errMagBuf_, 0, max_err_len);
    cxt_.pMsg = errMagBuf_;
    cxt_.msgLen = max_err_len;
    code_ = TSDB_CODE_SUCCESS;
158
    qDestroyQuery(res_);
159
    res_ = nullptr;
X
Xiaoyu Wang 已提交
160 161
  }

X
Xiaoyu Wang 已提交
162
  SVnodeModifOpStmt* getVnodeModifStmt(SQuery* pQuery) { return (SVnodeModifOpStmt*)pQuery->pRoot; }
163

X
Xiaoyu Wang 已提交
164 165 166 167
  string        acctId_;
  string        db_;
  char          errMagBuf_[max_err_len];
  char          sqlBuf_[max_sql_len];
168
  SParseContext cxt_;
X
Xiaoyu Wang 已提交
169 170
  int32_t       code_;
  SQuery*       res_;
171 172
};

173
// INSERT INTO tb_name [(field1_name, ...)] VALUES (field1_value, ...)
174 175 176
TEST_F(InsertTest, singleTableSingleRowTest) {
  setDatabase("root", "test");

X
Xiaoyu Wang 已提交
177
  bind("insert into t1 values (now, 1, 'beijing', 3, 4, 5)");
178 179 180
  ASSERT_EQ(run(), TSDB_CODE_SUCCESS);
  dumpReslut();
  checkReslut(1, 1);
181 182 183 184 185 186 187 188 189 190 191

  bind("insert into t1 (ts, c1, c2, c3, c4, c5) values (now, 1, 'beijing', 3, 4, 5)");
  ASSERT_EQ(run(), TSDB_CODE_SUCCESS);

  bind("insert into t1 values (now, 1, 'beijing', 3, 4, 5)");
  ASSERT_EQ(runAsync(), TSDB_CODE_SUCCESS);
  dumpReslut();
  checkReslut(1, 1);

  bind("insert into t1 (ts, c1, c2, c3, c4, c5) values (now, 1, 'beijing', 3, 4, 5)");
  ASSERT_EQ(runAsync(), TSDB_CODE_SUCCESS);
192 193 194 195 196 197
}

// INSERT INTO tb_name VALUES (field1_value, ...)(field1_value, ...)
TEST_F(InsertTest, singleTableMultiRowTest) {
  setDatabase("root", "test");

X
Xiaoyu Wang 已提交
198
  bind(
199 200
      "insert into t1 values (now, 1, 'beijing', 3, 4, 5)(now+1s, 2, 'shanghai', 6, 7, 8)"
      "(now+2s, 3, 'guangzhou', 9, 10, 11)");
201 202 203
  ASSERT_EQ(run(), TSDB_CODE_SUCCESS);
  dumpReslut();
  checkReslut(1, 3);
204 205 206 207 208

  bind(
      "insert into t1 values (now, 1, 'beijing', 3, 4, 5)(now+1s, 2, 'shanghai', 6, 7, 8)"
      "(now+2s, 3, 'guangzhou', 9, 10, 11)");
  ASSERT_EQ(runAsync(), TSDB_CODE_SUCCESS);
209 210 211 212 213 214 215 216 217 218
}

// INSERT INTO tb1_name VALUES (field1_value, ...) tb2_name VALUES (field1_value, ...)
TEST_F(InsertTest, multiTableSingleRowTest) {
  setDatabase("root", "test");

  bind("insert into st1s1 values (now, 1, \"beijing\") st1s2 values (now, 10, \"131028\")");
  ASSERT_EQ(run(), TSDB_CODE_SUCCESS);
  dumpReslut();
  checkReslut(2, 1);
219 220 221

  bind("insert into st1s1 values (now, 1, \"beijing\") st1s2 values (now, 10, \"131028\")");
  ASSERT_EQ(runAsync(), TSDB_CODE_SUCCESS);
222 223 224 225 226 227
}

// INSERT INTO tb1_name VALUES (field1_value, ...) tb2_name VALUES (field1_value, ...)
TEST_F(InsertTest, multiTableMultiRowTest) {
  setDatabase("root", "test");

X
Xiaoyu Wang 已提交
228 229 230
  bind(
      "insert into st1s1 values (now, 1, \"beijing\")(now+1s, 2, \"shanghai\")(now+2s, 3, \"guangzhou\")"
      " st1s2 values (now, 10, \"131028\")(now+1s, 20, \"132028\")");
231 232 233
  ASSERT_EQ(run(), TSDB_CODE_SUCCESS);
  dumpReslut();
  checkReslut(2, 3, 2);
234 235 236 237 238

  bind(
      "insert into st1s1 values (now, 1, \"beijing\")(now+1s, 2, \"shanghai\")(now+2s, 3, \"guangzhou\")"
      " st1s2 values (now, 10, \"131028\")(now+1s, 20, \"132028\")");
  ASSERT_EQ(runAsync(), TSDB_CODE_SUCCESS);
239 240
}

X
Xiaoyu Wang 已提交
241
// INSERT INTO
X
Xiaoyu Wang 已提交
242 243 244 245 246
//    tb1_name USING st1_name [(tag1_name, ...)] TAGS (tag1_value, ...) VALUES (field1_value, ...)
//    tb2_name USING st2_name [(tag1_name, ...)] TAGS (tag1_value, ...) VALUES (field1_value, ...)
TEST_F(InsertTest, autoCreateTableTest) {
  setDatabase("root", "test");

X
Xiaoyu Wang 已提交
247
  bind(
X
Xiaoyu Wang 已提交
248 249
      "insert into st1s1 using st1 tags(1, 'wxy', now) "
      "values (now, 1, \"beijing\")(now+1s, 2, \"shanghai\")(now+2s, 3, \"guangzhou\")");
X
Xiaoyu Wang 已提交
250 251 252
  ASSERT_EQ(run(), TSDB_CODE_SUCCESS);
  dumpReslut();
  checkReslut(1, 3);
253 254 255 256 257 258 259

  bind(
      "insert into st1s1 using st1 (tag1, tag2) tags(1, 'wxy') values (now, 1, \"beijing\")"
      "(now+1s, 2, \"shanghai\")(now+2s, 3, \"guangzhou\")");
  ASSERT_EQ(run(), TSDB_CODE_SUCCESS);

  bind(
X
Xiaoyu Wang 已提交
260 261
      "insert into st1s1 using st1 tags(1, 'wxy', now) "
      "values (now, 1, \"beijing\")(now+1s, 2, \"shanghai\")(now+2s, 3, \"guangzhou\")");
262 263 264 265 266 267
  ASSERT_EQ(runAsync(), TSDB_CODE_SUCCESS);

  bind(
      "insert into st1s1 using st1 (tag1, tag2) tags(1, 'wxy') values (now, 1, \"beijing\")"
      "(now+1s, 2, \"shanghai\")(now+2s, 3, \"guangzhou\")");
  ASSERT_EQ(runAsync(), TSDB_CODE_SUCCESS);
268 269 270 271 272

  bind(
      "insert into st1s1 using st1 tags(1, 'wxy', now) values (now, 1, \"beijing\")"
      "st1s1 using st1 tags(1, 'wxy', now) values (now+1s, 2, \"shanghai\")");
  ASSERT_EQ(run(), TSDB_CODE_SUCCESS);
X
Xiaoyu Wang 已提交
273 274
}

275 276 277 278 279 280 281
TEST_F(InsertTest, toleranceTest) {
  setDatabase("root", "test");

  bind("insert into");
  ASSERT_NE(run(), TSDB_CODE_SUCCESS);
  bind("insert into t");
  ASSERT_NE(run(), TSDB_CODE_SUCCESS);
282 283 284 285 286

  bind("insert into");
  ASSERT_NE(runAsync(), TSDB_CODE_SUCCESS);
  bind("insert into t");
  ASSERT_NE(runAsync(), TSDB_CODE_SUCCESS);
287
}