parserInsertTest.cpp 6.5 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>

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

using namespace std;
using namespace testing;

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

// 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 已提交
36
 protected:
37 38 39 40 41 42 43
  void setDatabase(const string& acctId, const string& db) {
    acctId_ = acctId;
    db_ = db;
  }

  void bind(const char* sql) {
    reset();
H
Haojun Liao 已提交
44
    cxt_.acctId = atoi(acctId_.c_str());
X
Xiaoyu Wang 已提交
45
    cxt_.db = (char*)db_.c_str();
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
    strcpy(sqlBuf_, sql);
    cxt_.sqlLen = strlen(sql);
    sqlBuf_[cxt_.sqlLen] = '\0';
    cxt_.pSql = sqlBuf_;
  }

  int32_t run() {
    code_ = parseInsertSql(&cxt_, &res_);
    if (code_ != TSDB_CODE_SUCCESS) {
      cout << "code:" << toString(code_) << ", msg:" << errMagBuf_ << endl;
    }
    return code_;
  }

  void dumpReslut() {
X
Xiaoyu Wang 已提交
61
    SVnodeModifOpStmt* pStmt = getVnodeModifStmt(res_);
X
Xiaoyu Wang 已提交
62 63 64
    size_t             num = taosArrayGetSize(pStmt->pDataBlocks);
    cout << "payloadType:" << (int32_t)pStmt->payloadType << ", insertType:" << pStmt->insertType
         << ", numOfVgs:" << num << endl;
65
    for (size_t i = 0; i < num; ++i) {
X
Xiaoyu Wang 已提交
66
      SVgDataBlocks* vg = (SVgDataBlocks*)taosArrayGetP(pStmt->pDataBlocks, i);
X
Xiaoyu Wang 已提交
67
      cout << "vgId:" << vg->vg.vgId << ", numOfTables:" << vg->numOfTables << ", dataSize:" << vg->size << endl;
S
Shengliang Guan 已提交
68
      SSubmitReq* submit = (SSubmitReq*)vg->pData;
69
      cout << "length:" << ntohl(submit->length) << ", numOfBlocks:" << ntohl(submit->numOfBlocks) << endl;
X
Xiaoyu Wang 已提交
70
      int32_t     numOfBlocks = ntohl(submit->numOfBlocks);
71 72 73
      SSubmitBlk* blk = (SSubmitBlk*)(submit + 1);
      for (int32_t i = 0; i < numOfBlocks; ++i) {
        cout << "Block:" << i << endl;
X
Xiaoyu Wang 已提交
74 75 76
        cout << "\tuid:" << be64toh(blk->uid) << ", tid:" << be64toh(blk->suid) << ", padding:" << ntohl(blk->padding)
             << ", sversion:" << ntohl(blk->sversion) << ", dataLen:" << ntohl(blk->dataLen)
             << ", schemaLen:" << ntohl(blk->schemaLen) << ", numOfRows:" << ntohs(blk->numOfRows) << endl;
77 78 79 80 81 82
        blk = (SSubmitBlk*)(blk->data + ntohl(blk->dataLen));
      }
    }
  }

  void checkReslut(int32_t numOfTables, int16_t numOfRows1, int16_t numOfRows2 = -1) {
X
Xiaoyu Wang 已提交
83 84 85 86
    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);
87 88
    ASSERT_GE(num, 0);
    for (size_t i = 0; i < num; ++i) {
X
Xiaoyu Wang 已提交
89
      SVgDataBlocks* vg = (SVgDataBlocks*)taosArrayGetP(pStmt->pDataBlocks, i);
90 91
      ASSERT_EQ(vg->numOfTables, numOfTables);
      ASSERT_GE(vg->size, 0);
S
Shengliang Guan 已提交
92
      SSubmitReq* submit = (SSubmitReq*)vg->pData;
93 94
      ASSERT_GE(ntohl(submit->length), 0);
      ASSERT_GE(ntohl(submit->numOfBlocks), 0);
X
Xiaoyu Wang 已提交
95
      int32_t     numOfBlocks = ntohl(submit->numOfBlocks);
96 97 98 99 100 101 102 103
      SSubmitBlk* blk = (SSubmitBlk*)(submit + 1);
      for (int32_t i = 0; i < numOfBlocks; ++i) {
        ASSERT_EQ(ntohs(blk->numOfRows), (0 == i ? numOfRows1 : (numOfRows2 > 0 ? numOfRows2 : numOfRows1)));
        blk = (SSubmitBlk*)(blk->data + ntohl(blk->dataLen));
      }
    }
  }

X
Xiaoyu Wang 已提交
104
 private:
105 106 107 108 109 110 111 112 113
  static const int max_err_len = 1024;
  static const int max_sql_len = 1024 * 1024;

  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;
X
Xiaoyu Wang 已提交
114 115
  }

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

X
Xiaoyu Wang 已提交
118 119 120 121
  string        acctId_;
  string        db_;
  char          errMagBuf_[max_err_len];
  char          sqlBuf_[max_sql_len];
122
  SParseContext cxt_;
X
Xiaoyu Wang 已提交
123 124
  int32_t       code_;
  SQuery*       res_;
125 126 127 128 129 130
};

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

X
Xiaoyu Wang 已提交
131
  bind("insert into t1 values (now, 1, 'beijing', 3, 4, 5)");
132 133 134 135 136 137 138 139 140
  ASSERT_EQ(run(), TSDB_CODE_SUCCESS);
  dumpReslut();
  checkReslut(1, 1);
}

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

X
Xiaoyu Wang 已提交
141 142 143
  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)");
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
  ASSERT_EQ(run(), TSDB_CODE_SUCCESS);
  dumpReslut();
  checkReslut(1, 3);
}

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

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

X
Xiaoyu Wang 已提交
163 164 165
  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\")");
166 167 168 169 170
  ASSERT_EQ(run(), TSDB_CODE_SUCCESS);
  dumpReslut();
  checkReslut(2, 3, 2);
}

X
Xiaoyu Wang 已提交
171
// INSERT INTO
X
Xiaoyu Wang 已提交
172 173 174 175 176
//    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 已提交
177 178 179
  bind(
      "insert into st1s1 using st1 tags(1, 'wxy') values (now, 1, \"beijing\")(now+1s, 2, \"shanghai\")(now+2s, 3, "
      "\"guangzhou\")");
X
Xiaoyu Wang 已提交
180 181 182 183 184
  ASSERT_EQ(run(), TSDB_CODE_SUCCESS);
  dumpReslut();
  checkReslut(1, 3);
}

185 186 187 188 189 190 191 192
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);
}