smlTest.cpp 41.0 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
/*
 * 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 <taoserror.h>
#include <tglobal.h>
#include <iostream>

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wwrite-strings"
#pragma GCC diagnostic ignored "-Wunused-function"
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wsign-compare"

#include "../src/clientSml.c"
#include "taos.h"

wmmhello's avatar
wmmhello 已提交
31
int main(int argc, char **argv) {
32 33 34 35
  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}

36
TEST(testCase, smlParseInfluxString_Test) {
wmmhello's avatar
wmmhello 已提交
37
  char       msg[256] = {0};
38 39 40 41 42
  SSmlMsgBuf msgBuf;
  msgBuf.buf = msg;
  msgBuf.len = 256;
  SSmlLineInfo elements = {0};

wmmhello's avatar
wmmhello 已提交
43
  // case 1
wmmhello's avatar
wmmhello 已提交
44 45 46
  char *tmp = "\\,st,t1=3,t2=4,t3=t3 c1=3i64,c3=\"passit hello,c1=2\",c2=false,c4=4f64 1626006833639000000    ,32,c=3";
  char *sql = (char*)taosMemoryCalloc(256, 1);
  memcpy(sql, tmp, strlen(tmp) + 1);
47
  int   ret = smlParseInfluxString(sql, &elements, &msgBuf);
48 49
  ASSERT_EQ(ret, 0);
  ASSERT_EQ(elements.measure, sql);
wmmhello's avatar
wmmhello 已提交
50 51
  ASSERT_EQ(elements.measureLen, strlen(",st"));
  ASSERT_EQ(elements.measureTagsLen, strlen(",st,t1=3,t2=4,t3=t3"));
52 53 54 55 56 57 58 59 60 61 62

  ASSERT_EQ(elements.tags, sql + elements.measureLen + 1);
  ASSERT_EQ(elements.tagsLen, strlen("t1=3,t2=4,t3=t3"));

  ASSERT_EQ(elements.cols, sql + elements.measureTagsLen + 1);
  ASSERT_EQ(elements.colsLen, strlen("c1=3i64,c3=\"passit hello,c1=2\",c2=false,c4=4f64"));

  ASSERT_EQ(elements.timestamp, sql + elements.measureTagsLen + 1 + elements.colsLen + 1);
  ASSERT_EQ(elements.timestampLen, strlen("1626006833639000000"));

  // case 2  false
wmmhello's avatar
wmmhello 已提交
63 64
  tmp = "st,t1=3,t2=4,t3=t3 c1=3i64,c3=\"passit hello,c1=2,c2=false,c4=4f64 1626006833639000000";
  memcpy(sql, tmp, strlen(tmp) + 1);
65
  memset(&elements, 0, sizeof(SSmlLineInfo));
66
  ret = smlParseInfluxString(sql, &elements, &msgBuf);
67 68 69
  ASSERT_NE(ret, 0);

  // case 3  false
wmmhello's avatar
wmmhello 已提交
70 71
  tmp = "st, t1=3,t2=4,t3=t3 c1=3i64,c3=\"passit hello,c1=2,c2=false,c4=4f64 1626006833639000000";
  memcpy(sql, tmp, strlen(tmp) + 1);
72
  memset(&elements, 0, sizeof(SSmlLineInfo));
73
  ret = smlParseInfluxString(sql, &elements, &msgBuf);
74
  ASSERT_EQ(ret, 0);
wmmhello's avatar
wmmhello 已提交
75
  ASSERT_EQ(elements.cols, sql + elements.measureTagsLen + 1);
76 77 78
  ASSERT_EQ(elements.colsLen, strlen("t1=3,t2=4,t3=t3"));

  // case 4  tag is null
wmmhello's avatar
wmmhello 已提交
79 80
  tmp = "st, c1=3i64,c3=\"passit hello,c1=2\",c2=false,c4=4f64 1626006833639000000";
  memcpy(sql, tmp, strlen(tmp) + 1);
81
  memset(&elements, 0, sizeof(SSmlLineInfo));
82
  ret = smlParseInfluxString(sql, &elements, &msgBuf);
83 84 85
  ASSERT_EQ(ret, 0);
  ASSERT_EQ(elements.measure, sql);
  ASSERT_EQ(elements.measureLen, strlen("st"));
wmmhello's avatar
wmmhello 已提交
86
  ASSERT_EQ(elements.measureTagsLen, strlen("st,"));
87

wmmhello's avatar
wmmhello 已提交
88
  ASSERT_EQ(elements.tags, sql + elements.measureTagsLen);
89 90
  ASSERT_EQ(elements.tagsLen, 0);

wmmhello's avatar
wmmhello 已提交
91
  ASSERT_EQ(elements.cols, sql + elements.measureTagsLen + 1);
92 93
  ASSERT_EQ(elements.colsLen, strlen("c1=3i64,c3=\"passit hello,c1=2\",c2=false,c4=4f64"));

wmmhello's avatar
wmmhello 已提交
94
  ASSERT_EQ(elements.timestamp, sql + elements.measureTagsLen + 1 + elements.colsLen + 1);
95 96 97
  ASSERT_EQ(elements.timestampLen, strlen("1626006833639000000"));

  // case 5 tag is null
wmmhello's avatar
wmmhello 已提交
98 99
  tmp = " st   c1=3i64,c3=\"passit hello,c1=2\",c2=false,c4=4f64  1626006833639000000 ";
  memcpy(sql, tmp, strlen(tmp) + 1);
100
  memset(&elements, 0, sizeof(SSmlLineInfo));
101
  ret = smlParseInfluxString(sql, &elements, &msgBuf);
102
  ASSERT_EQ(ret, 0);
wmmhello's avatar
wmmhello 已提交
103
  ASSERT_EQ(elements.measure, sql + 1);
104 105 106 107 108
  ASSERT_EQ(elements.measureLen, strlen("st"));
  ASSERT_EQ(elements.measureTagsLen, strlen("st"));

  ASSERT_EQ(elements.tagsLen, 0);

wmmhello's avatar
wmmhello 已提交
109
  ASSERT_EQ(elements.cols, sql + 1 + elements.measureTagsLen + 3);
110 111
  ASSERT_EQ(elements.colsLen, strlen("c1=3i64,c3=\"passit hello,c1=2\",c2=false,c4=4f64"));

wmmhello's avatar
wmmhello 已提交
112
  ASSERT_EQ(elements.timestamp, sql + 1 + elements.measureTagsLen + 3 + elements.colsLen + 2);
113 114 115
  ASSERT_EQ(elements.timestampLen, strlen("1626006833639000000"));

  // case 6
wmmhello's avatar
wmmhello 已提交
116 117
  tmp = " st   c1=3i64,c3=\"passit hello,c1=2\",c2=false,c4=4f64   ";
  memcpy(sql, tmp, strlen(tmp) + 1);
118
  memset(&elements, 0, sizeof(SSmlLineInfo));
119
  ret = smlParseInfluxString(sql, &elements, &msgBuf);
120 121 122
  ASSERT_EQ(ret, 0);

  // case 7
wmmhello's avatar
wmmhello 已提交
123 124
  tmp = " st   ,   ";
  memcpy(sql, tmp, strlen(tmp) + 1);
125
  memset(&elements, 0, sizeof(SSmlLineInfo));
126
  ret = smlParseInfluxString(sql, &elements, &msgBuf);
wmmhello's avatar
wmmhello 已提交
127
  ASSERT_EQ(ret, 0);
128 129

  // case 8 false
wmmhello's avatar
wmmhello 已提交
130 131
  tmp = ", st   ,   ";
  memcpy(sql, tmp, strlen(tmp) + 1);
132
  memset(&elements, 0, sizeof(SSmlLineInfo));
133
  ret = smlParseInfluxString(sql, &elements, &msgBuf);
134
  ASSERT_NE(ret, 0);
wmmhello's avatar
wmmhello 已提交
135
  taosMemoryFree(sql);
136 137
}

wmmhello's avatar
wmmhello 已提交
138 139 140 141 142 143 144 145 146 147 148 149 150 151
TEST(testCase, smlParseCols_Error_Test) {
  const char *data[] = {
    "c=\"89sd",           // binary, nchar
    "c=j\"89sd\"",
    "c=\"89sd\"k",
    "c=u",                // bool
    "c=truet",
    "c=f64",              // double
    "c=8f64f",
    "c=8ef64",
    "c=f32",              // float
    "c=8f32f",
    "c=8wef32",
    "c=-3.402823466e+39f32",
152
    "c=",                 // double
wmmhello's avatar
wmmhello 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
    "c=8f",
    "c=8we",
    "c=i8",              // tiny int
    "c=-8i8f",
    "c=8wei8",
    "c=-999i8",
    "c=u8",              // u tiny int
    "c=8fu8",
    "c=8weu8",
    "c=999u8",
    "c=-8u8",
    "c=i16",              // small int
    "c=8fi16u",
    "c=8wei16",
    "c=-67787i16",
    "c=u16",              // u small int
    "c=8u16f",
    "c=8weu16",
    "c=-9u16",
    "c=67787u16",
    "c=i32",                // int
    "c=8i32f",
    "c=8wei32",
    "c=2147483649i32",
    "c=u32",                // u int
    "c=8u32f",
    "c=8weu32",
    "c=-4u32",
    "c=42949672958u32",
    "c=i64",                // big int
    "c=8i64i",
    "c=8wei64",
    "c=-9223372036854775809i64",
    "c=i",                  // big int
    "c=8fi",
    "c=8wei",
    "c=9223372036854775808i",
    "c=u64",                // u big int
    "c=8u64f",
    "c=8weu64",
    "c=-3.402823466e+39u64",
    "c=-339u64",
    "c=18446744073709551616u64",
wmmhello's avatar
wmmhello 已提交
196 197
    "c=1,c=2",
    "c=1=2"
wmmhello's avatar
wmmhello 已提交
198 199
  };

200
  SHashObj *dumplicateKey = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
wmmhello's avatar
wmmhello 已提交
201
  for(int i = 0; i < sizeof(data)/sizeof(data[0]); i++){
wmmhello's avatar
wmmhello 已提交
202 203 204 205
    char       msg[256] = {0};
    SSmlMsgBuf msgBuf;
    msgBuf.buf = msg;
    msgBuf.len = 256;
wmmhello's avatar
wmmhello 已提交
206
    int32_t len = strlen(data[i]);
wmmhello's avatar
wmmhello 已提交
207 208
    char *sql = (char*)taosMemoryCalloc(256, 1);
    memcpy(sql, data[i], len + 1);
wmmhello's avatar
wmmhello 已提交
209
    SArray *cols = taosArrayInit(8, POINTER_BYTES);
210
    int32_t ret = smlParseCols(sql, len, cols, NULL, false, dumplicateKey, &msgBuf);
wmmhello's avatar
wmmhello 已提交
211
    printf("i:%d\n",i);
wmmhello's avatar
wmmhello 已提交
212
    ASSERT_NE(ret, TSDB_CODE_SUCCESS);
213
    taosHashClear(dumplicateKey);
wmmhello's avatar
wmmhello 已提交
214
    taosMemoryFree(sql);
wmmhello's avatar
wmmhello 已提交
215 216 217 218 219
    for(int j = 0; j < taosArrayGetSize(cols); j++){
      void *kv = taosArrayGetP(cols, j);
      taosMemoryFree(kv);
    }
    taosArrayDestroy(cols);
wmmhello's avatar
wmmhello 已提交
220
  }
221
  taosHashCleanup(dumplicateKey);
wmmhello's avatar
wmmhello 已提交
222 223
}

wmmhello's avatar
wmmhello 已提交
224 225 226 227 228 229 230
TEST(testCase, smlParseCols_tag_Test) {
  char       msg[256] = {0};
  SSmlMsgBuf msgBuf;
  msgBuf.buf = msg;
  msgBuf.len = 256;

  SArray *cols = taosArrayInit(16, POINTER_BYTES);
wmmhello's avatar
wmmhello 已提交
231
  ASSERT_NE(cols, nullptr);
232
  SHashObj *dumplicateKey = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
wmmhello's avatar
wmmhello 已提交
233 234

  const char *data =
wmmhello's avatar
wmmhello 已提交
235
      "cbin=\"passit helloc\",cnch=L\"iisdfsf\",cbool=false,cf64=4.31f64,cf64_=8.32,cf32=8.23f32,ci8=-34i8,cu8=89u8,ci16=233i16,cu16=898u16,ci32=98289i32,cu32=12323u32,ci64=-89238i64,ci=989i,cu64=8989323u64,cbooltrue=true,cboolt=t,cboolf=f,cnch_=l\"iuwq\"";
wmmhello's avatar
wmmhello 已提交
236
  int32_t len = strlen(data);
237
  int32_t ret = smlParseCols(data, len, cols, NULL, true, dumplicateKey, &msgBuf);
wmmhello's avatar
wmmhello 已提交
238 239 240 241 242
  ASSERT_EQ(ret, TSDB_CODE_SUCCESS);
  int32_t size = taosArrayGetSize(cols);
  ASSERT_EQ(size, 19);

  // nchar
wmmhello's avatar
wmmhello 已提交
243
  SSmlKv *kv = (SSmlKv *)taosArrayGetP(cols, 0);
wmmhello's avatar
wmmhello 已提交
244 245 246
  ASSERT_EQ(strncasecmp(kv->key, "cbin", 4), 0);
  ASSERT_EQ(kv->keyLen, 4);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_NCHAR);
wmmhello's avatar
wmmhello 已提交
247
  ASSERT_EQ(kv->length, 15);
wmmhello's avatar
wmmhello 已提交
248 249 250
  ASSERT_EQ(strncasecmp(kv->value, "\"passit", 7), 0);

  // nchar
wmmhello's avatar
wmmhello 已提交
251
  kv = (SSmlKv *)taosArrayGetP(cols, 3);
wmmhello's avatar
wmmhello 已提交
252 253 254
  ASSERT_EQ(strncasecmp(kv->key, "cf64", 4), 0);
  ASSERT_EQ(kv->keyLen, 4);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_NCHAR);
wmmhello's avatar
wmmhello 已提交
255
  ASSERT_EQ(kv->length, 7);
wmmhello's avatar
wmmhello 已提交
256 257
  ASSERT_EQ(strncasecmp(kv->value, "4.31f64", 7), 0);

wmmhello's avatar
wmmhello 已提交
258 259 260 261
  for(int i = 0; i < size; i++){
    void *tmp = taosArrayGetP(cols, i);
    taosMemoryFree(tmp);
  }
wmmhello's avatar
wmmhello 已提交
262
  taosArrayClear(cols);
263 264

  // test tag is null
wmmhello's avatar
wmmhello 已提交
265 266 267
  data = "t=3e";
  len = 0;
  memset(msgBuf.buf, 0, msgBuf.len);
268
  taosHashClear(dumplicateKey);
269
  ret = smlParseCols(data, len, cols, NULL, true, dumplicateKey, &msgBuf);
wmmhello's avatar
wmmhello 已提交
270 271 272 273 274
  ASSERT_EQ(ret, TSDB_CODE_SUCCESS);
  size = taosArrayGetSize(cols);
  ASSERT_EQ(size, 1);

  // nchar
wmmhello's avatar
wmmhello 已提交
275
  kv = (SSmlKv *)taosArrayGetP(cols, 0);
wmmhello's avatar
wmmhello 已提交
276 277
  ASSERT_EQ(strncasecmp(kv->key, TAG, TAG_LEN), 0);
  ASSERT_EQ(kv->keyLen, TAG_LEN);
wmmhello's avatar
wmmhello 已提交
278
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_NCHAR);
wmmhello's avatar
wmmhello 已提交
279 280
  ASSERT_EQ(kv->length, TAG_LEN);
  ASSERT_EQ(strncasecmp(kv->value, TAG_VALUE, TAG_VALUE_LEN), 0);
wmmhello's avatar
wmmhello 已提交
281
  taosMemoryFree(kv);
282 283 284

  taosArrayDestroy(cols);
  taosHashCleanup(dumplicateKey);
wmmhello's avatar
wmmhello 已提交
285 286
}

wmmhello's avatar
wmmhello 已提交
287 288 289 290 291 292 293
TEST(testCase, smlParseCols_Test) {
  char       msg[256] = {0};
  SSmlMsgBuf msgBuf;
  msgBuf.buf = msg;
  msgBuf.len = 256;

  SArray *cols = taosArrayInit(16, POINTER_BYTES);
wmmhello's avatar
wmmhello 已提交
294
  ASSERT_NE(cols, nullptr);
wmmhello's avatar
wmmhello 已提交
295

296 297
  SHashObj *dumplicateKey = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);

wmmhello's avatar
wmmhello 已提交
298
  const char *data = "cb\\=in=\"pass\\,it hello,c=2\",cnch=L\"ii\\=sdfsf\",cbool=false,cf64=4.31f64,cf64_=8.32,cf32=8.23f32,ci8=-34i8,cu8=89u8,ci16=233i16,cu16=898u16,ci32=98289i32,cu32=12323u32,ci64=-89238i64,ci=989i,cu64=8989323u64,cbooltrue=true,cboolt=t,cboolf=f,cnch_=l\"iuwq\"";
wmmhello's avatar
wmmhello 已提交
299
  int32_t len = strlen(data);
wmmhello's avatar
wmmhello 已提交
300 301
  char *sql = (char*)taosMemoryCalloc(1024, 1);
  memcpy(sql, data, len + 1);
302
  int32_t ret = smlParseCols(sql, len, cols, NULL, false, dumplicateKey, &msgBuf);
wmmhello's avatar
wmmhello 已提交
303 304 305 306 307
  ASSERT_EQ(ret, TSDB_CODE_SUCCESS);
  int32_t size = taosArrayGetSize(cols);
  ASSERT_EQ(size, 19);

  // binary
wmmhello's avatar
wmmhello 已提交
308
  SSmlKv *kv = (SSmlKv *)taosArrayGetP(cols, 0);
wmmhello's avatar
wmmhello 已提交
309
  ASSERT_EQ(strncasecmp(kv->key, "cb=in", 5), 0);
wmmhello's avatar
wmmhello 已提交
310
  ASSERT_EQ(kv->keyLen, 5);
wmmhello's avatar
wmmhello 已提交
311
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_BINARY);
wmmhello's avatar
wmmhello 已提交
312
  ASSERT_EQ(kv->length, 17);
wmmhello's avatar
wmmhello 已提交
313
  ASSERT_EQ(strncasecmp(kv->value, "pass,it ", 8), 0);
wmmhello's avatar
wmmhello 已提交
314 315 316
  taosMemoryFree(kv);

  // nchar
wmmhello's avatar
wmmhello 已提交
317
  kv = (SSmlKv *)taosArrayGetP(cols, 1);
wmmhello's avatar
wmmhello 已提交
318 319 320
  ASSERT_EQ(strncasecmp(kv->key, "cnch", 4), 0);
  ASSERT_EQ(kv->keyLen, 4);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_NCHAR);
wmmhello's avatar
wmmhello 已提交
321
  ASSERT_EQ(kv->length, 8);
wmmhello's avatar
wmmhello 已提交
322
  ASSERT_EQ(strncasecmp(kv->value, "ii=sd", 5), 0);
wmmhello's avatar
wmmhello 已提交
323 324 325
  taosMemoryFree(kv);

  // bool
wmmhello's avatar
wmmhello 已提交
326
  kv = (SSmlKv *)taosArrayGetP(cols, 2);
wmmhello's avatar
wmmhello 已提交
327 328 329
  ASSERT_EQ(strncasecmp(kv->key, "cbool", 5), 0);
  ASSERT_EQ(kv->keyLen, 5);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_BOOL);
wmmhello's avatar
wmmhello 已提交
330
  ASSERT_EQ(kv->length, 1);
wmmhello's avatar
wmmhello 已提交
331 332 333 334
  ASSERT_EQ(kv->i, false);
  taosMemoryFree(kv);

  // double
wmmhello's avatar
wmmhello 已提交
335
  kv = (SSmlKv *)taosArrayGetP(cols, 3);
wmmhello's avatar
wmmhello 已提交
336 337 338
  ASSERT_EQ(strncasecmp(kv->key, "cf64", 4), 0);
  ASSERT_EQ(kv->keyLen, 4);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_DOUBLE);
wmmhello's avatar
wmmhello 已提交
339 340
  ASSERT_EQ(kv->length, 8);
  //ASSERT_EQ(kv->d, 4.31);
341
  printf("4.31 = kv->d:%f\n", kv->d);
wmmhello's avatar
wmmhello 已提交
342 343 344
  taosMemoryFree(kv);

  // float
wmmhello's avatar
wmmhello 已提交
345
  kv = (SSmlKv *)taosArrayGetP(cols, 4);
346
  ASSERT_EQ(strncasecmp(kv->key, "cf64_", 5), 0);
wmmhello's avatar
wmmhello 已提交
347
  ASSERT_EQ(kv->keyLen, 5);
348 349
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_DOUBLE);
  ASSERT_EQ(kv->length, 8);
wmmhello's avatar
wmmhello 已提交
350
  //ASSERT_EQ(kv->f, 8.32);
351
  printf("8.32 = kv->d:%f\n", kv->d);
wmmhello's avatar
wmmhello 已提交
352 353 354
  taosMemoryFree(kv);

  // float
wmmhello's avatar
wmmhello 已提交
355
  kv = (SSmlKv *)taosArrayGetP(cols, 5);
wmmhello's avatar
wmmhello 已提交
356 357 358 359
  ASSERT_EQ(strncasecmp(kv->key, "cf32", 4), 0);
  ASSERT_EQ(kv->keyLen, 4);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_FLOAT);
  ASSERT_EQ(kv->length, 4);
wmmhello's avatar
wmmhello 已提交
360 361
  //ASSERT_EQ(kv->f, 8.23);
  printf("8.23 = kv->f:%f\n", kv->f);
wmmhello's avatar
wmmhello 已提交
362 363 364
  taosMemoryFree(kv);

  // tiny int
wmmhello's avatar
wmmhello 已提交
365
  kv = (SSmlKv *)taosArrayGetP(cols, 6);
wmmhello's avatar
wmmhello 已提交
366 367
  ASSERT_EQ(strncasecmp(kv->key, "ci8", 3), 0);
  ASSERT_EQ(kv->keyLen, 3);
wmmhello's avatar
wmmhello 已提交
368
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_TINYINT);
wmmhello's avatar
wmmhello 已提交
369
  ASSERT_EQ(kv->length, 1);
wmmhello's avatar
wmmhello 已提交
370
  ASSERT_EQ(kv->i, -34);
wmmhello's avatar
wmmhello 已提交
371 372 373
  taosMemoryFree(kv);

  // unsigned tiny int
wmmhello's avatar
wmmhello 已提交
374
  kv = (SSmlKv *)taosArrayGetP(cols, 7);
wmmhello's avatar
wmmhello 已提交
375 376 377 378 379 380 381 382
  ASSERT_EQ(strncasecmp(kv->key, "cu8", 3), 0);
  ASSERT_EQ(kv->keyLen, 3);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_UTINYINT);
  ASSERT_EQ(kv->length, 1);
  ASSERT_EQ(kv->u, 89);
  taosMemoryFree(kv);

  // small int
wmmhello's avatar
wmmhello 已提交
383
  kv = (SSmlKv *)taosArrayGetP(cols, 8);
wmmhello's avatar
wmmhello 已提交
384 385 386 387 388 389 390 391
  ASSERT_EQ(strncasecmp(kv->key, "ci16", 4), 0);
  ASSERT_EQ(kv->keyLen, 4);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_SMALLINT);
  ASSERT_EQ(kv->length, 2);
  ASSERT_EQ(kv->u, 233);
  taosMemoryFree(kv);

  // unsigned smallint
wmmhello's avatar
wmmhello 已提交
392
  kv = (SSmlKv *)taosArrayGetP(cols, 9);
wmmhello's avatar
wmmhello 已提交
393 394 395 396 397 398 399 400
  ASSERT_EQ(strncasecmp(kv->key, "cu16", 4), 0);
  ASSERT_EQ(kv->keyLen, 4);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_USMALLINT);
  ASSERT_EQ(kv->length, 2);
  ASSERT_EQ(kv->u, 898);
  taosMemoryFree(kv);

  // int
wmmhello's avatar
wmmhello 已提交
401
  kv = (SSmlKv *)taosArrayGetP(cols, 10);
wmmhello's avatar
wmmhello 已提交
402 403 404 405 406 407 408 409
  ASSERT_EQ(strncasecmp(kv->key, "ci32", 4), 0);
  ASSERT_EQ(kv->keyLen, 4);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_INT);
  ASSERT_EQ(kv->length, 4);
  ASSERT_EQ(kv->u, 98289);
  taosMemoryFree(kv);

  // unsigned int
wmmhello's avatar
wmmhello 已提交
410
  kv = (SSmlKv *)taosArrayGetP(cols, 11);
wmmhello's avatar
wmmhello 已提交
411 412 413 414 415 416 417 418 419
  ASSERT_EQ(strncasecmp(kv->key, "cu32", 4), 0);
  ASSERT_EQ(kv->keyLen, 4);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_UINT);
  ASSERT_EQ(kv->length, 4);
  ASSERT_EQ(kv->u, 12323);
  taosMemoryFree(kv);


  // bigint
wmmhello's avatar
wmmhello 已提交
420
  kv = (SSmlKv *)taosArrayGetP(cols, 12);
wmmhello's avatar
wmmhello 已提交
421
  ASSERT_EQ(strncasecmp(kv->key, "ci64", 4), 0);
wmmhello's avatar
wmmhello 已提交
422 423
  ASSERT_EQ(kv->keyLen, 4);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_BIGINT);
wmmhello's avatar
wmmhello 已提交
424 425 426 427 428
  ASSERT_EQ(kv->length, 8);
  ASSERT_EQ(kv->i, -89238);
  taosMemoryFree(kv);

  // bigint
wmmhello's avatar
wmmhello 已提交
429
  kv = (SSmlKv *)taosArrayGetP(cols, 13);
wmmhello's avatar
wmmhello 已提交
430 431 432 433 434 435 436 437
  ASSERT_EQ(strncasecmp(kv->key, "ci", 2), 0);
  ASSERT_EQ(kv->keyLen, 2);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_BIGINT);
  ASSERT_EQ(kv->length, 8);
  ASSERT_EQ(kv->i, 989);
  taosMemoryFree(kv);

  // unsigned bigint
wmmhello's avatar
wmmhello 已提交
438
  kv = (SSmlKv *)taosArrayGetP(cols, 14);
wmmhello's avatar
wmmhello 已提交
439 440 441 442 443 444 445 446
  ASSERT_EQ(strncasecmp(kv->key, "cu64", 4), 0);
  ASSERT_EQ(kv->keyLen, 4);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_UBIGINT);
  ASSERT_EQ(kv->length, 8);
  ASSERT_EQ(kv->u, 8989323);
  taosMemoryFree(kv);

  // bool
wmmhello's avatar
wmmhello 已提交
447
  kv = (SSmlKv *)taosArrayGetP(cols, 15);
wmmhello's avatar
wmmhello 已提交
448 449 450 451 452 453 454 455 456
  ASSERT_EQ(strncasecmp(kv->key, "cbooltrue", 9), 0);
  ASSERT_EQ(kv->keyLen, 9);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_BOOL);
  ASSERT_EQ(kv->length, 1);
  ASSERT_EQ(kv->i, true);
  taosMemoryFree(kv);


  // bool
wmmhello's avatar
wmmhello 已提交
457
  kv = (SSmlKv *)taosArrayGetP(cols, 16);
wmmhello's avatar
wmmhello 已提交
458
  ASSERT_EQ(strncasecmp(kv->key, "cboolt", 6), 0);
wmmhello's avatar
wmmhello 已提交
459 460 461 462
  ASSERT_EQ(kv->keyLen, 6);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_BOOL);
  ASSERT_EQ(kv->length, 1);
  ASSERT_EQ(kv->i, true);
wmmhello's avatar
wmmhello 已提交
463 464 465
  taosMemoryFree(kv);

  // bool
wmmhello's avatar
wmmhello 已提交
466
  kv = (SSmlKv *)taosArrayGetP(cols, 17);
wmmhello's avatar
wmmhello 已提交
467 468 469
  ASSERT_EQ(strncasecmp(kv->key, "cboolf", 6), 0);
  ASSERT_EQ(kv->keyLen, 6);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_BOOL);
wmmhello's avatar
wmmhello 已提交
470 471
  ASSERT_EQ(kv->length, 1);
  ASSERT_EQ(kv->i, false);
wmmhello's avatar
wmmhello 已提交
472 473
  taosMemoryFree(kv);

wmmhello's avatar
wmmhello 已提交
474
  // nchar
wmmhello's avatar
wmmhello 已提交
475
  kv = (SSmlKv *)taosArrayGetP(cols, 18);
wmmhello's avatar
wmmhello 已提交
476 477 478 479 480 481 482 483
  ASSERT_EQ(strncasecmp(kv->key, "cnch_", 5), 0);
  ASSERT_EQ(kv->keyLen, 5);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_NCHAR);
  ASSERT_EQ(kv->length, 4);
  ASSERT_EQ(strncasecmp(kv->value, "iuwq", 4), 0);
  taosMemoryFree(kv);

  taosArrayDestroy(cols);
484
  taosHashCleanup(dumplicateKey);
wmmhello's avatar
wmmhello 已提交
485
  taosMemoryFree(sql);
wmmhello's avatar
wmmhello 已提交
486 487
}

488
TEST(testCase, smlProcess_influx_Test) {
wmmhello's avatar
wmmhello 已提交
489
  TAOS *taos = taos_connect("localhost", "root", "taosdata", NULL, 0);
wmmhello's avatar
wmmhello 已提交
490
  ASSERT_NE(taos, nullptr);
wmmhello's avatar
wmmhello 已提交
491

492
  TAOS_RES* pRes = taos_query(taos, "create database if not exists inflx_db");
wmmhello's avatar
wmmhello 已提交
493 494
  taos_free_result(pRes);

495
  pRes = taos_query(taos, "use inflx_db");
wmmhello's avatar
wmmhello 已提交
496 497
  taos_free_result(pRes);

wmmhello's avatar
wmmhello 已提交
498 499
  SRequestObj *request = (SRequestObj *)createRequest((STscObj*)taos, NULL, NULL, TSDB_SQL_INSERT);
  ASSERT_NE(request, nullptr);
wmmhello's avatar
wmmhello 已提交
500

501
  SSmlHandle *info = smlBuildSmlInfo(taos, request, TSDB_SML_LINE_PROTOCOL, TSDB_SML_TIMESTAMP_NANO_SECONDS);
wmmhello's avatar
wmmhello 已提交
502
  ASSERT_NE(info, nullptr);
wmmhello's avatar
wmmhello 已提交
503

wmmhello's avatar
wmmhello 已提交
504
  const char *sql[] = {
505 506 507 508 509
    "readings,name=truck_0,fleet=South,driver=Trish,model=H-2,device_version=v2.3 load_capacity=1500,fuel_capacity=150,nominal_fuel_consumption=12,latitude=52.31854,longitude=4.72037,elevation=124,velocity=0,heading=221,grade=0 1451606401000000000",
    "readings,name=truck_0,fleet=South,driver=Trish,model=H-2,device_version=v2.3 load_capacity=1500,fuel_capacity=150,nominal_fuel_consumption=12,latitude=52.31854,longitude=4.72037,elevation=124,velocity=0,heading=221,grade=0,fuel_consumption=25 1451607402000000000",
    "readings,name=truck_0,fleet=South,driver=Trish,model=H-2,device_version=v2.3 load_capacity=1500,fuel_capacity=150,nominal_fuel_consumption=12,latitude=52.31854,longitude=4.72037,elevation=124,heading=221,grade=0,fuel_consumption=25 1451608403000000000",
    "readings,name=truck_0,fleet=South,driver=Trish,model=H-2,device_version=v2.3 fuel_capacity=150,nominal_fuel_consumption=12,latitude=52.31854,longitude=4.72037,elevation=124,velocity=0,heading=221,grade=0,fuel_consumption=25 1451609404000000000",
    "readings,name=truck_0,fleet=South,driver=Trish,model=H-2,device_version=v2.3 fuel_consumption=25,grade=0 1451619405000000000",
wmmhello's avatar
wmmhello 已提交
510
    "readings,name=truck_1,fleet=South,driver=Albert,model=F-150,device_version=v1.5 load_capacity=2000,fuel_capacity=200,nominal_fuel_consumption=15,latitude=72.45258,longitude=68.83761,elevation=255,velocity=0,heading=181,grade=0,fuel_consumption=25 1451606406000000000",
511 512 513 514 515
    "readings,name=truck_2,driver=Derek,model=F-150,device_version=v1.5 load_capacity=2000,fuel_capacity=200,nominal_fuel_consumption=15,latitude=24.5208,longitude=28.09377,elevation=428,velocity=0,heading=304,grade=0,fuel_consumption=25 1451606407000000000",
    "readings,name=truck_2,fleet=North,driver=Derek,model=F-150 load_capacity=2000,fuel_capacity=200,nominal_fuel_consumption=15,latitude=24.5208,longitude=28.09377,elevation=428,velocity=0,heading=304,grade=0,fuel_consumption=25 1451609408000000000",
    "readings,fleet=South,name=truck_0,driver=Trish,model=H-2,device_version=v2.3 fuel_consumption=25,grade=0 1451629409000000000",
    "stable,t1=t1,t2=t2,t3=t3 c1=1,c2=2,c3=\"kk\",c4=4 1451629501000000000",
    "stable,t2=t2,t1=t1,t3=t3 c1=1,c3=\"\",c4=4 1451629602000000000",
wmmhello's avatar
wmmhello 已提交
516
  };
wmmhello's avatar
wmmhello 已提交
517 518
  int ret = smlProcess(info, (char**)sql, sizeof(sql)/sizeof(sql[0]));
  ASSERT_EQ(ret, 0);
519

520 521
  // case 1
  TAOS_RES *res = taos_query(taos, "select * from t_91e0b182be80332b5c530cbf872f760e");
522 523
  ASSERT_NE(res, nullptr);
  int fieldNum = taos_field_count(res);
524
  ASSERT_EQ(fieldNum, 11);
525
  printf("fieldNum:%d\n", fieldNum);
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590

  TAOS_ROW row = NULL;
  int32_t rowIndex = 0;
  while((row = taos_fetch_row(res)) != NULL) {
    int64_t ts = *(int64_t*)row[0];
    double load_capacity = *(double*)row[1];
    double fuel_capacity = *(double*)row[2];
    double nominal_fuel_consumption = *(double*)row[3];
    double latitude = *(double*)row[4];
    double longitude = *(double*)row[5];
    double elevation = *(double*)row[6];
    double velocity = *(double*)row[7];
    double heading = *(double*)row[8];
    double grade = *(double*)row[9];
    double fuel_consumption = *(double*)row[10];
    if(rowIndex == 0){
      ASSERT_EQ(ts, 1451606407000);
      ASSERT_EQ(load_capacity, 2000);
      ASSERT_EQ(fuel_capacity, 200);
      ASSERT_EQ(nominal_fuel_consumption, 15);
      ASSERT_EQ(latitude, 24.5208);
      ASSERT_EQ(longitude, 28.09377);
      ASSERT_EQ(elevation, 428);
      ASSERT_EQ(velocity, 0);
      ASSERT_EQ(heading, 304);
      ASSERT_EQ(grade, 0);
      ASSERT_EQ(fuel_consumption, 25);
    }else{
      ASSERT_FALSE(1);
    }
    rowIndex++;
  }
  taos_free_result(res);

  // case 2
  res = taos_query(taos, "select * from t_6885c584b98481584ee13dac399e173d");
  ASSERT_NE(res, nullptr);
  fieldNum = taos_field_count(res);
  ASSERT_EQ(fieldNum, 5);
  printf("fieldNum:%d\n", fieldNum);

  rowIndex = 0;
  while((row = taos_fetch_row(res)) != NULL) {
    int *length = taos_fetch_lengths(res);

    int64_t ts = *(int64_t*)row[0];
    double c1 = *(double*)row[1];
    double c4 = *(double*)row[4];
    if(rowIndex == 0){
      ASSERT_EQ(ts, 1451629501000);
      ASSERT_EQ(c1, 1);
      ASSERT_EQ(*(double*)row[2], 2);
      ASSERT_EQ(length[3], 2);
      ASSERT_EQ(memcmp(row[3], "kk", length[3]), 0);
      ASSERT_EQ(c4, 4);
    }else if(rowIndex == 1){
      ASSERT_EQ(ts, 1451629602000);
      ASSERT_EQ(c1, 1);
      ASSERT_EQ(row[2], nullptr);
      ASSERT_EQ(length[3], 0);
      ASSERT_EQ(c4, 4);
    }else{
      ASSERT_FALSE(1);
    }
    rowIndex++;
591 592
  }
  taos_free_result(res);
593 594 595 596 597 598 599 600 601 602 603

  // case 2
  res = taos_query(taos, "show tables");
  ASSERT_NE(res, nullptr);

  row = taos_fetch_row(res);
  int rowNum = taos_affected_rows(res);
  ASSERT_EQ(rowNum, 5);
  taos_free_result(res);


wmmhello's avatar
wmmhello 已提交
604
  destroyRequest(request);
605
  smlDestroyInfo(info);
wmmhello's avatar
wmmhello 已提交
606
}
607

608
// different types
609 610
TEST(testCase, smlParseLine_error_Test) {
  TAOS *taos = taos_connect("localhost", "root", "taosdata", NULL, 0);
wmmhello's avatar
wmmhello 已提交
611
  ASSERT_NE(taos, nullptr);
612 613 614 615 616 617 618

  TAOS_RES* pRes = taos_query(taos, "create database if not exists sml_db");
  taos_free_result(pRes);

  pRes = taos_query(taos, "use sml_db");
  taos_free_result(pRes);

wmmhello's avatar
wmmhello 已提交
619 620
  SRequestObj *request = (SRequestObj *)createRequest((STscObj*)taos, NULL, NULL, TSDB_SQL_INSERT);
  ASSERT_NE(request, nullptr);
621

622
  SSmlHandle *info = smlBuildSmlInfo(taos, request, TSDB_SML_LINE_PROTOCOL, TSDB_SML_TIMESTAMP_NANO_SECONDS);
wmmhello's avatar
wmmhello 已提交
623
  ASSERT_NE(info, nullptr);
624

wmmhello's avatar
wmmhello 已提交
625
  const char *sql[] = {
626 627 628
      "measure,t1=3 c1=8",
      "measure,t2=3 c1=8u8"
  };
629
  int ret = smlProcess(info, (char **)sql, sizeof(sql)/sizeof(sql[0]));
630
  ASSERT_NE(ret, 0);
631 632
  destroyRequest(request);
  smlDestroyInfo(info);
633 634
}

635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
TEST(testCase, smlGetTimestampLen_Test) {
  uint8_t len = smlGetTimestampLen(0);
  ASSERT_EQ(len, 1);

  len = smlGetTimestampLen(1);
  ASSERT_EQ(len, 1);

  len = smlGetTimestampLen(10);
  ASSERT_EQ(len, 2);

  len = smlGetTimestampLen(390);
  ASSERT_EQ(len, 3);

  len = smlGetTimestampLen(-1);
  ASSERT_EQ(len, 1);

  len = smlGetTimestampLen(-10);
  ASSERT_EQ(len, 2);

  len = smlGetTimestampLen(-390);
  ASSERT_EQ(len, 3);
}

TEST(testCase, smlProcess_telnet_Test) {
  TAOS *taos = taos_connect("localhost", "root", "taosdata", NULL, 0);
  ASSERT_NE(taos, nullptr);

662
  TAOS_RES* pRes = taos_query(taos, "create database if not exists telnet_db");
663 664
  taos_free_result(pRes);

665
  pRes = taos_query(taos, "use telnet_db");
666 667 668 669 670
  taos_free_result(pRes);

  SRequestObj *request = (SRequestObj *)createRequest((STscObj*)taos, NULL, NULL, TSDB_SQL_INSERT);
  ASSERT_NE(request, nullptr);

671
  SSmlHandle *info = smlBuildSmlInfo(taos, request, TSDB_SML_TELNET_PROTOCOL, TSDB_SML_TIMESTAMP_NANO_SECONDS);
672 673
  ASSERT_NE(info, nullptr);

wmmhello's avatar
wmmhello 已提交
674
  const char *sql[] = {
wmmhello's avatar
wmmhello 已提交
675 676 677 678
     "sys.if.bytes.out  1479496100 1.3E0 host=web01 interface=eth0",
     "sys.if.bytes.out  1479496101 1.3E1 interface=eth0    host=web01   ",
     "sys.if.bytes.out  1479496102 1.3E3 network=tcp",
     " sys.procs.running   1479496100 42 host=web01   "
679 680 681 682
  };
  int ret = smlProcess(info, (char**)sql, sizeof(sql)/sizeof(sql[0]));
  ASSERT_EQ(ret, 0);

683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
  // case 1
  TAOS_RES *res = taos_query(taos, "select * from t_8c30283b3c4131a071d1e16cf6d7094a");
  ASSERT_NE(res, nullptr);
  int fieldNum = taos_field_count(res);
  ASSERT_EQ(fieldNum, 2);

  TAOS_ROW row = taos_fetch_row(res);
  int64_t ts = *(int64_t*)row[0];
  double c1 = *(double*)row[1];
  ASSERT_EQ(ts, 1479496100000);
  ASSERT_EQ(c1, 42);

  int rowNum = taos_affected_rows(res);
  ASSERT_EQ(rowNum, 1);
  taos_free_result(res);

  // case 2
  res = taos_query(taos, "show tables");
  ASSERT_NE(res, nullptr);

  row = taos_fetch_row(res);
  rowNum = taos_affected_rows(res);
  ASSERT_EQ(rowNum, 3);
  taos_free_result(res);

wmmhello's avatar
wmmhello 已提交
708
  destroyRequest(request);
709 710 711 712 713 714 715
  smlDestroyInfo(info);
}

TEST(testCase, smlProcess_json1_Test) {
  TAOS *taos = taos_connect("localhost", "root", "taosdata", NULL, 0);
  ASSERT_NE(taos, nullptr);

716
  TAOS_RES *pRes = taos_query(taos, "create database if not exists json_db");
717 718
  taos_free_result(pRes);

719
  pRes = taos_query(taos, "use json_db");
720 721 722 723 724
  taos_free_result(pRes);

  SRequestObj *request = (SRequestObj *)createRequest((STscObj *)taos, NULL, NULL, TSDB_SQL_INSERT);
  ASSERT_NE(request, nullptr);

725
  SSmlHandle *info = smlBuildSmlInfo(taos, request, TSDB_SML_JSON_PROTOCOL, TSDB_SML_TIMESTAMP_NANO_SECONDS);
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
  ASSERT_NE(info, nullptr);

  const char *sql =
     "[\n"
     "    {\n"
     "        \"metric\": \"sys.cpu.nice\",\n"
     "        \"timestamp\": 1346846400,\n"
     "        \"value\": 18,\n"
     "        \"tags\": {\n"
     "           \"host\": \"web01\",\n"
     "           \"dc\": \"lga\"\n"
     "        }\n"
     "    },\n"
     "    {\n"
     "        \"metric\": \"sys.cpu.nice\",\n"
     "        \"timestamp\": 1346846400,\n"
     "        \"value\": 9,\n"
     "        \"tags\": {\n"
     "           \"host\": \"web02\",\n"
     "           \"dc\": \"lga\"\n"
     "        }\n"
     "    }\n"
     "]";
wmmhello's avatar
wmmhello 已提交
749
  int ret = smlProcess(info, (char **)(&sql), 1);
750 751
  ASSERT_EQ(ret, 0);

752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
  // case 1
  TAOS_RES *res = taos_query(taos, "select * from t_cb27a7198d637b4f1c6464bd73f756a7");
  ASSERT_NE(res, nullptr);
  int fieldNum = taos_field_count(res);
  ASSERT_EQ(fieldNum, 2);

  TAOS_ROW row = taos_fetch_row(res);
  int64_t ts = *(int64_t*)row[0];
  double c1 = *(double*)row[1];
  ASSERT_EQ(ts, 1346846400000);
  ASSERT_EQ(c1, 18);

  int rowNum = taos_affected_rows(res);
  ASSERT_EQ(rowNum, 1);
  taos_free_result(res);

  // case 2
  res = taos_query(taos, "show tables");
  ASSERT_NE(res, nullptr);

  row = taos_fetch_row(res);
  rowNum = taos_affected_rows(res);
  ASSERT_EQ(rowNum, 2);
  taos_free_result(res);

wmmhello's avatar
wmmhello 已提交
777
  destroyRequest(request);
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
  smlDestroyInfo(info);
}

TEST(testCase, smlProcess_json2_Test) {
  TAOS *taos = taos_connect("localhost", "root", "taosdata", NULL, 0);
  ASSERT_NE(taos, nullptr);

  TAOS_RES *pRes = taos_query(taos, "create database if not exists sml_db");
  taos_free_result(pRes);

  pRes = taos_query(taos, "use sml_db");
  taos_free_result(pRes);

  SRequestObj *request = (SRequestObj *)createRequest((STscObj *)taos, NULL, NULL, TSDB_SQL_INSERT);
  ASSERT_NE(request, nullptr);

794
  SSmlHandle *info = smlBuildSmlInfo(taos, request, TSDB_SML_JSON_PROTOCOL, TSDB_SML_TIMESTAMP_NANO_SECONDS);
795 796 797
  ASSERT_NE(info, nullptr);
  const char *sql =
     "{\n"
798
     "    \"metric\": \"meter_current0\",\n"
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
     "    \"timestamp\": {\n"
     "        \"value\"  : 1346846400,\n"
     "        \"type\"   : \"s\"\n"
     "    },\n"
     "    \"value\": {\n"
     "         \"value\" : 10.3,\n"
     "         \"type\"  : \"i64\"\n"
     "    },\n"
     "    \"tags\": {\n"
     "       \"groupid\": { \n"
     "           \"value\" : 2,\n"
     "           \"type\"  : \"bigint\"\n"
     "       },\n"
     "       \"location\": { \n"
     "           \"value\" : \"北京\",\n"
     "           \"type\"  : \"binary\"\n"
     "       },\n"
     "       \"id\": \"d1001\"\n"
     "    }\n"
     "}";
  int32_t ret = smlProcess(info, (char **)(&sql), -1);
  ASSERT_EQ(ret, 0);
wmmhello's avatar
wmmhello 已提交
821
  destroyRequest(request);
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
  smlDestroyInfo(info);
}

TEST(testCase, smlProcess_json3_Test) {
  TAOS *taos = taos_connect("localhost", "root", "taosdata", NULL, 0);
  ASSERT_NE(taos, nullptr);

  TAOS_RES *pRes = taos_query(taos, "create database if not exists sml_db");
  taos_free_result(pRes);

  pRes = taos_query(taos, "use sml_db");
  taos_free_result(pRes);

  SRequestObj *request = (SRequestObj *)createRequest((STscObj *)taos, NULL, NULL, TSDB_SQL_INSERT);
  ASSERT_NE(request, nullptr);

838
  SSmlHandle *info = smlBuildSmlInfo(taos, request, TSDB_SML_JSON_PROTOCOL, TSDB_SML_TIMESTAMP_NANO_SECONDS);
839 840 841
  ASSERT_NE(info, nullptr);
  const char *sql =
     "{\n"
842
     "    \"metric\": \"meter_current1\",\n"
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
     "    \"timestamp\": {\n"
     "        \"value\"  : 1346846400,\n"
     "        \"type\"   : \"s\"\n"
     "    },\n"
     "    \"value\": {\n"
     "         \"value\" : 10.3,\n"
     "         \"type\"  : \"i64\"\n"
     "    },\n"
     "    \"tags\": {\n"
     "       \"t1\": { \n"
     "           \"value\" : 2,\n"
     "           \"type\"  : \"bigint\"\n"
     "       },\n"
     "       \"t2\": { \n"
     "           \"value\" : 2,\n"
     "           \"type\"  : \"int\"\n"
     "       },\n"
     "       \"t3\": { \n"
     "           \"value\" : 2,\n"
     "           \"type\"  : \"i16\"\n"
     "       },\n"
     "       \"t4\": { \n"
     "           \"value\" : 2,\n"
     "           \"type\"  : \"i8\"\n"
     "       },\n"
     "       \"t5\": { \n"
     "           \"value\" : 2,\n"
     "           \"type\"  : \"f32\"\n"
     "       },\n"
     "       \"t6\": { \n"
     "           \"value\" : 2,\n"
     "           \"type\"  : \"double\"\n"
     "       },\n"
     "       \"t7\": { \n"
     "           \"value\" : \"8323\",\n"
     "           \"type\"  : \"binary\"\n"
     "       },\n"
     "       \"t8\": { \n"
     "           \"value\" : \"北京\",\n"
     "           \"type\"  : \"nchar\"\n"
     "       },\n"
     "       \"t9\": { \n"
     "           \"value\" : true,\n"
     "           \"type\"  : \"bool\"\n"
     "       },\n"
     "       \"id\": \"d1001\"\n"
     "    }\n"
     "}";
  int32_t ret = smlProcess(info, (char **)(&sql), -1);
  ASSERT_EQ(ret, 0);
wmmhello's avatar
wmmhello 已提交
893
  destroyRequest(request);
894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
  smlDestroyInfo(info);
}

TEST(testCase, smlProcess_json4_Test) {
  TAOS *taos = taos_connect("localhost", "root", "taosdata", NULL, 0);
  ASSERT_NE(taos, nullptr);

  TAOS_RES* pRes = taos_query(taos, "create database if not exists sml_db");
  taos_free_result(pRes);

  pRes = taos_query(taos, "use sml_db");
  taos_free_result(pRes);

  SRequestObj *request = (SRequestObj *)createRequest((STscObj*)taos, NULL, NULL, TSDB_SQL_INSERT);
  ASSERT_NE(request, nullptr);

910
  SSmlHandle *info = smlBuildSmlInfo(taos, request, TSDB_SML_JSON_PROTOCOL, TSDB_SML_TIMESTAMP_NANO_SECONDS);
911 912
  ASSERT_NE(info, nullptr);
  const char *sql = "{\n"
913
     "    \"metric\": \"meter_current2\",\n"
914
     "    \"timestamp\": {\n"
915
     "        \"value\"  : 1346846500000,\n"
916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954
     "        \"type\"   : \"ms\"\n"
     "    },\n"
     "    \"value\": \"ni\",\n"
     "    \"tags\": {\n"
     "       \"t1\": { \n"
     "           \"value\" : 20,\n"
     "           \"type\"  : \"i64\"\n"
     "       },\n"
     "       \"t2\": { \n"
     "           \"value\" : 25,\n"
     "           \"type\"  : \"i32\"\n"
     "       },\n"
     "       \"t3\": { \n"
     "           \"value\" : 2,\n"
     "           \"type\"  : \"smallint\"\n"
     "       },\n"
     "       \"t4\": { \n"
     "           \"value\" : 2,\n"
     "           \"type\"  : \"tinyint\"\n"
     "       },\n"
     "       \"t5\": { \n"
     "           \"value\" : 2,\n"
     "           \"type\"  : \"float\"\n"
     "       },\n"
     "       \"t6\": { \n"
     "           \"value\" : 0.2,\n"
     "           \"type\"  : \"f64\"\n"
     "       },\n"
     "       \"t7\": \"nsj\",\n"
     "       \"t8\": { \n"
     "           \"value\" : \"北京\",\n"
     "           \"type\"  : \"nchar\"\n"
     "       },\n"
     "       \"t9\": false,\n"
     "       \"id\": \"d1001\"\n"
     "    }\n"
     "}";
  int32_t ret = smlProcess(info, (char**)(&sql), -1);
  ASSERT_EQ(ret, 0);
wmmhello's avatar
wmmhello 已提交
955
  destroyRequest(request);
956 957
  smlDestroyInfo(info);
}
958 959 960 961 962 963 964 965 966 967 968 969 970 971

TEST(testCase, smlParseTelnetLine_error_Test) {
  TAOS *taos = taos_connect("localhost", "root", "taosdata", NULL, 0);
  ASSERT_NE(taos, nullptr);

  TAOS_RES* pRes = taos_query(taos, "create database if not exists sml_db");
  taos_free_result(pRes);

  pRes = taos_query(taos, "use sml_db");
  taos_free_result(pRes);

  SRequestObj *request = (SRequestObj *)createRequest((STscObj*)taos, NULL, NULL, TSDB_SQL_INSERT);
  ASSERT_NE(request, nullptr);

972
  SSmlHandle *info = smlBuildSmlInfo(taos, request, TSDB_SML_TELNET_PROTOCOL, TSDB_SML_TIMESTAMP_NANO_SECONDS);
973 974 975
  ASSERT_NE(info, nullptr);

  int32_t ret = 0;
wmmhello's avatar
wmmhello 已提交
976
  const char *sql[] = {
977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995
      "sys.procs.running 14794961040 42 host=web01",
      "sys.procs.running 14791040 42 host=web01",
      "sys.procs.running erere 42 host=web01",
      "sys.procs.running 1.6e10 42 host=web01",
      "sys.procs.running 1.47949610 42 host=web01",
      "sys.procs.running 147949610i 42 host=web01",
      "sys.procs.running -147949610 42 host=web01",
      "",
      "   ",
      "sys  ",
      "sys.procs.running 1479496100 42 ",
      "sys.procs.running 1479496100 42 host= ",
      "sys.procs.running 1479496100 42or host=web01",
      "sys.procs.running 1479496100 true host=web01",
      "sys.procs.running 1479496100 \"binary\" host=web01",
      "sys.procs.running 1479496100 L\"rfr\" host=web01",
      "sys.procs.running 1479496100 42 host=web01 cpu= ",
      "sys.procs.running 1479496100 42 host=web01 host=w2",
      "sys.procs.running 1479496100 42 host=web01 host",
wmmhello's avatar
wmmhello 已提交
996 997
      "sys.procs.running 1479496100 42 host=web01=er",
      "sys.procs.running 1479496100 42 host= web01",
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
  };
  for(int i = 0; i < sizeof(sql)/sizeof(sql[0]); i++){
    ret = smlParseTelnetLine(info, (void*)sql[i]);
    ASSERT_NE(ret, 0);
  }

  destroyRequest(request);
  smlDestroyInfo(info);
}

TEST(testCase, smlParseTelnetLine_diff_type_Test) {
  TAOS *taos = taos_connect("localhost", "root", "taosdata", NULL, 0);
  ASSERT_NE(taos, nullptr);

  TAOS_RES* pRes = taos_query(taos, "create database if not exists sml_db");
  taos_free_result(pRes);

  pRes = taos_query(taos, "use sml_db");
  taos_free_result(pRes);

  SRequestObj *request = (SRequestObj *)createRequest((STscObj*)taos, NULL, NULL, TSDB_SQL_INSERT);
  ASSERT_NE(request, nullptr);

1021
  SSmlHandle *info = smlBuildSmlInfo(taos, request, TSDB_SML_TELNET_PROTOCOL, TSDB_SML_TIMESTAMP_NANO_SECONDS);
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
  ASSERT_NE(info, nullptr);

  const char *sql[2] = {
      "sys.procs.running 1479496104000 42 host=web01",
      "sys.procs.running 1479496104000 42u8 host=web01"
  };
  int32_t ret = smlProcess(info, (char**)sql, sizeof(sql)/sizeof(sql[0]));
  ASSERT_NE(ret, 0);

  destroyRequest(request);
  smlDestroyInfo(info);
}

TEST(testCase, smlParseTelnetLine_json_error_Test) {
  TAOS *taos = taos_connect("localhost", "root", "taosdata", NULL, 0);
  ASSERT_NE(taos, nullptr);

  TAOS_RES* pRes = taos_query(taos, "create database if not exists sml_db");
  taos_free_result(pRes);

  pRes = taos_query(taos, "use sml_db");
  taos_free_result(pRes);

  SRequestObj *request = (SRequestObj *)createRequest((STscObj*)taos, NULL, NULL, TSDB_SQL_INSERT);
  ASSERT_NE(request, nullptr);

1048
  SSmlHandle *info = smlBuildSmlInfo(taos, request, TSDB_SML_TELNET_PROTOCOL, TSDB_SML_TIMESTAMP_NANO_SECONDS);
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
  ASSERT_NE(info, nullptr);

  int32_t ret = 0;
  const char *sql[] = {
      "[\n"
      "    {\n"
      "        \"metric\": \"sys.cpu.nice\",\n"
      "        \"timestamp\": 13468464009999333322222223,\n"
      "        \"value\": 18,\n"
      "        \"tags\": {\n"
      "           \"host\": \"web01\",\n"
      "           \"dc\": \"lga\"\n"
      "        }\n"
      "    },\n"
      "]",
      "[\n"
      "    {\n"
      "        \"metric\": \"sys.cpu.nice\",\n"
      "        \"timestamp\": 1346846400i,\n"
      "        \"value\": 18,\n"
      "        \"tags\": {\n"
      "           \"host\": \"web01\",\n"
      "           \"dc\": \"lga\"\n"
      "        }\n"
      "    },\n"
      "]",
      "[\n"
      "    {\n"
      "        \"metric\": \"sys.cpu.nice\",\n"
      "        \"timestamp\": 1346846400,\n"
      "        \"value\": 18,\n"
      "        \"tags\": {\n"
      "           \"groupid\": { \n"
      "                 \"value\" : 2,\n"
      "                 \"type\"  : \"nchar\"\n"
      "             },\n"
      "           \"location\": { \n"
      "                 \"value\" : \"北京\",\n"
      "                 \"type\"  : \"binary\"\n"
      "             },\n"
      "           \"id\": \"d1001\"\n"
      "         }\n"
      "    },\n"
      "]",
  };
  for(int i = 0; i < sizeof(sql)/sizeof(sql[0]); i++){
    ret = smlParseTelnetLine(info, (void*)sql[i]);
    ASSERT_NE(ret, 0);
  }

  destroyRequest(request);
  smlDestroyInfo(info);
}

TEST(testCase, smlParseTelnetLine_diff_json_type1_Test) {
  TAOS *taos = taos_connect("localhost", "root", "taosdata", NULL, 0);
  ASSERT_NE(taos, nullptr);

  TAOS_RES* pRes = taos_query(taos, "create database if not exists sml_db");
  taos_free_result(pRes);

  pRes = taos_query(taos, "use sml_db");
  taos_free_result(pRes);

  SRequestObj *request = (SRequestObj *)createRequest((STscObj*)taos, NULL, NULL, TSDB_SQL_INSERT);
  ASSERT_NE(request, nullptr);

1116
  SSmlHandle *info = smlBuildSmlInfo(taos, request, TSDB_SML_TELNET_PROTOCOL, TSDB_SML_TIMESTAMP_NANO_SECONDS);
1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
  ASSERT_NE(info, nullptr);

  const char *sql[2] = {
      "[\n"
      "    {\n"
      "        \"metric\": \"sys.cpu.nice\",\n"
      "        \"timestamp\": 1346846400,\n"
      "        \"value\": 18,\n"
      "        \"tags\": {\n"
      "           \"host\": \"lga\"\n"
      "        }\n"
      "    },\n"
      "]",
      "[\n"
      "    {\n"
      "        \"metric\": \"sys.cpu.nice\",\n"
      "        \"timestamp\": 1346846400,\n"
      "        \"value\": 18,\n"
      "        \"tags\": {\n"
      "           \"host\": 8\n"
      "        }\n"
      "    },\n"
      "]",
  };
  int32_t ret = smlProcess(info, (char**)sql, sizeof(sql)/sizeof(sql[0]));
  ASSERT_NE(ret, 0);

  destroyRequest(request);
  smlDestroyInfo(info);
}

TEST(testCase, smlParseTelnetLine_diff_json_type2_Test) {
  TAOS *taos = taos_connect("localhost", "root", "taosdata", NULL, 0);
  ASSERT_NE(taos, nullptr);

  TAOS_RES* pRes = taos_query(taos, "create database if not exists sml_db");
  taos_free_result(pRes);

  pRes = taos_query(taos, "use sml_db");
  taos_free_result(pRes);

  SRequestObj *request = (SRequestObj *)createRequest((STscObj*)taos, NULL, NULL, TSDB_SQL_INSERT);
  ASSERT_NE(request, nullptr);

1161
  SSmlHandle *info = smlBuildSmlInfo(taos, request, TSDB_SML_TELNET_PROTOCOL, TSDB_SML_TIMESTAMP_NANO_SECONDS);
1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
  ASSERT_NE(info, nullptr);

  const char *sql[2] = {
      "[\n"
      "    {\n"
      "        \"metric\": \"sys.cpu.nice\",\n"
      "        \"timestamp\": 1346846400,\n"
      "        \"value\": 18,\n"
      "        \"tags\": {\n"
      "           \"host\": \"lga\"\n"
      "        }\n"
      "    },\n"
      "]",
      "[\n"
      "    {\n"
      "        \"metric\": \"sys.cpu.nice\",\n"
      "        \"timestamp\": 1346846400,\n"
      "        \"value\": \"18\",\n"
      "        \"tags\": {\n"
      "           \"host\": \"fff\"\n"
      "        }\n"
      "    },\n"
      "]",
  };
  int32_t ret = smlProcess(info, (char**)sql, sizeof(sql)/sizeof(sql[0]));
  ASSERT_NE(ret, 0);

  destroyRequest(request);
  smlDestroyInfo(info);
}
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205

TEST(testCase, sml_TD15662_Test) {
  TAOS *taos = taos_connect("localhost", "root", "taosdata", NULL, 0);
  ASSERT_NE(taos, nullptr);

  TAOS_RES *pRes = taos_query(taos, "create database if not exists db_15662 precision 'ns'");
  taos_free_result(pRes);

  pRes = taos_query(taos, "use db_15662");
  taos_free_result(pRes);

  SRequestObj *request = (SRequestObj *)createRequest((STscObj *)taos, NULL, NULL, TSDB_SQL_INSERT);
  ASSERT_NE(request, nullptr);

wmmhello's avatar
wmmhello 已提交
1206
  SSmlHandle *info = smlBuildSmlInfo(taos, request, TSDB_SML_LINE_PROTOCOL, TSDB_SML_TIMESTAMP_MILLI_SECONDS);
1207 1208 1209
  ASSERT_NE(info, nullptr);

  const char *sql[] = {
wmmhello's avatar
wmmhello 已提交
1210
      "hetrey,id=sub_table_0123456,t0=f,t1=127i8,t2=32767i16,t3=2147483647i32,t4=9223372036854775807i64,t5=11.12345f32,t6=22.123456789f64,t7=\"binaryTagValue\",t8=L\"ncharTagValue\" c0=f,c1=127i8,c2=32767i16,c3=2147483647i32,c4=9223372036854775807i64,c5=11.12345f32,c6=22.123456789f64,c7=\"binaryColValue\",c8=L\"ncharColValue\",c9=7u64",
1211 1212 1213 1214
  };
  int ret = smlProcess(info, (char **)sql, sizeof(sql) / sizeof(sql[0]));
  ASSERT_EQ(ret, 0);

wmmhello's avatar
wmmhello 已提交
1215 1216
  destroyRequest(request);
  smlDestroyInfo(info);
wmmhello's avatar
wmmhello 已提交
1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
}

TEST(testCase, sml_TD15735_Test) {
  TAOS *taos = taos_connect("localhost", "root", "taosdata", NULL, 0);
  ASSERT_NE(taos, nullptr);

  TAOS_RES* pRes = taos_query(taos, "create database if not exists sml_db");
  taos_free_result(pRes);

  pRes = taos_query(taos, "use sml_db");
  taos_free_result(pRes);

  SRequestObj *request = (SRequestObj *)createRequest((STscObj*)taos, NULL, NULL, TSDB_SQL_INSERT);
  ASSERT_NE(request, nullptr);

  SSmlHandle *info = smlBuildSmlInfo(taos, request, TSDB_SML_TELNET_PROTOCOL, TSDB_SML_TIMESTAMP_NANO_SECONDS);
  ASSERT_NE(info, nullptr);

  const char *sql[1] = {
      "{'metric': 'pekoiw', 'timestamp': {'value': 1626006833639000000, 'type': 'ns'}, 'value': {'value': False, 'type': 'bool'}, 'tags': {'t0': {'value': True, 'type': 'bool'}, 't1': {'value': 127, 'type': 'tinyint'}, 't2': {'value': 32767, 'type': 'smallint'}, 't3': {'value': 2147483647, 'type': 'int'}, 't4': {'value': 9223372036854775807, 'type': 'bigint'}, 't5': {'value': 11.12345027923584, 'type': 'float'}, 't6': {'value': 22.123456789, 'type': 'double'}, 't7': {'value': 'binaryTagValue', 'type': 'binary'}, 't8': {'value': 'ncharTagValue', 'type': 'nchar'}}}",
  };
  int32_t ret = smlProcess(info, (char**)sql, sizeof(sql)/sizeof(sql[0]));
  ASSERT_NE(ret, 0);

  destroyRequest(request);
  smlDestroyInfo(info);
}

TEST(testCase, sml_TD15742_Test) {
  TAOS *taos = taos_connect("localhost", "root", "taosdata", NULL, 0);
  ASSERT_NE(taos, nullptr);

wmmhello's avatar
wmmhello 已提交
1249
  TAOS_RES* pRes = taos_query(taos, "create database if not exists TD15742");
wmmhello's avatar
wmmhello 已提交
1250 1251
  taos_free_result(pRes);

wmmhello's avatar
wmmhello 已提交
1252
  pRes = taos_query(taos, "use TD15742");
wmmhello's avatar
wmmhello 已提交
1253 1254 1255 1256 1257
  taos_free_result(pRes);

  SRequestObj *request = (SRequestObj *)createRequest((STscObj*)taos, NULL, NULL, TSDB_SQL_INSERT);
  ASSERT_NE(request, nullptr);

wmmhello's avatar
wmmhello 已提交
1258
  SSmlHandle *info = smlBuildSmlInfo(taos, request, TSDB_SML_LINE_PROTOCOL, TSDB_SML_TIMESTAMP_MILLI_SECONDS);
wmmhello's avatar
wmmhello 已提交
1259 1260 1261
  ASSERT_NE(info, nullptr);

  const char *sql[] = {
wmmhello's avatar
wmmhello 已提交
1262
      "test_ms,t0=t c0=f 1626006833641",
wmmhello's avatar
wmmhello 已提交
1263 1264 1265 1266 1267 1268
  };
  int ret = smlProcess(info, (char**)sql, sizeof(sql)/sizeof(sql[0]));
  ASSERT_EQ(ret, 0);

  destroyRequest(request);
  smlDestroyInfo(info);
1269
}