smlTest.cpp 40.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
/*
 * 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
  ASSERT_EQ(ret, TSDB_CODE_SUCCESS);
  size = taosArrayGetSize(cols);
272
  ASSERT_EQ(size, 0);
273 274 275

  taosArrayDestroy(cols);
  taosHashCleanup(dumplicateKey);
wmmhello's avatar
wmmhello 已提交
276 277
}

wmmhello's avatar
wmmhello 已提交
278 279 280 281 282 283 284
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 已提交
285
  ASSERT_NE(cols, nullptr);
wmmhello's avatar
wmmhello 已提交
286

287 288
  SHashObj *dumplicateKey = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);

wmmhello's avatar
wmmhello 已提交
289
  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 已提交
290
  int32_t len = strlen(data);
wmmhello's avatar
wmmhello 已提交
291 292
  char *sql = (char*)taosMemoryCalloc(1024, 1);
  memcpy(sql, data, len + 1);
293
  int32_t ret = smlParseCols(sql, len, cols, NULL, false, dumplicateKey, &msgBuf);
wmmhello's avatar
wmmhello 已提交
294 295 296 297 298
  ASSERT_EQ(ret, TSDB_CODE_SUCCESS);
  int32_t size = taosArrayGetSize(cols);
  ASSERT_EQ(size, 19);

  // binary
wmmhello's avatar
wmmhello 已提交
299
  SSmlKv *kv = (SSmlKv *)taosArrayGetP(cols, 0);
wmmhello's avatar
wmmhello 已提交
300
  ASSERT_EQ(strncasecmp(kv->key, "cb=in", 5), 0);
wmmhello's avatar
wmmhello 已提交
301
  ASSERT_EQ(kv->keyLen, 5);
wmmhello's avatar
wmmhello 已提交
302
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_BINARY);
wmmhello's avatar
wmmhello 已提交
303
  ASSERT_EQ(kv->length, 17);
wmmhello's avatar
wmmhello 已提交
304
  ASSERT_EQ(strncasecmp(kv->value, "pass,it ", 8), 0);
wmmhello's avatar
wmmhello 已提交
305 306 307
  taosMemoryFree(kv);

  // nchar
wmmhello's avatar
wmmhello 已提交
308
  kv = (SSmlKv *)taosArrayGetP(cols, 1);
wmmhello's avatar
wmmhello 已提交
309 310 311
  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 已提交
312
  ASSERT_EQ(kv->length, 8);
wmmhello's avatar
wmmhello 已提交
313
  ASSERT_EQ(strncasecmp(kv->value, "ii=sd", 5), 0);
wmmhello's avatar
wmmhello 已提交
314 315 316
  taosMemoryFree(kv);

  // bool
wmmhello's avatar
wmmhello 已提交
317
  kv = (SSmlKv *)taosArrayGetP(cols, 2);
wmmhello's avatar
wmmhello 已提交
318 319 320
  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 已提交
321
  ASSERT_EQ(kv->length, 1);
wmmhello's avatar
wmmhello 已提交
322 323 324 325
  ASSERT_EQ(kv->i, false);
  taosMemoryFree(kv);

  // double
wmmhello's avatar
wmmhello 已提交
326
  kv = (SSmlKv *)taosArrayGetP(cols, 3);
wmmhello's avatar
wmmhello 已提交
327 328 329
  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 已提交
330 331
  ASSERT_EQ(kv->length, 8);
  //ASSERT_EQ(kv->d, 4.31);
332
  printf("4.31 = kv->d:%f\n", kv->d);
wmmhello's avatar
wmmhello 已提交
333 334 335
  taosMemoryFree(kv);

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

  // float
wmmhello's avatar
wmmhello 已提交
346
  kv = (SSmlKv *)taosArrayGetP(cols, 5);
wmmhello's avatar
wmmhello 已提交
347 348 349 350
  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 已提交
351 352
  //ASSERT_EQ(kv->f, 8.23);
  printf("8.23 = kv->f:%f\n", kv->f);
wmmhello's avatar
wmmhello 已提交
353 354 355
  taosMemoryFree(kv);

  // tiny int
wmmhello's avatar
wmmhello 已提交
356
  kv = (SSmlKv *)taosArrayGetP(cols, 6);
wmmhello's avatar
wmmhello 已提交
357 358
  ASSERT_EQ(strncasecmp(kv->key, "ci8", 3), 0);
  ASSERT_EQ(kv->keyLen, 3);
wmmhello's avatar
wmmhello 已提交
359
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_TINYINT);
wmmhello's avatar
wmmhello 已提交
360
  ASSERT_EQ(kv->length, 1);
wmmhello's avatar
wmmhello 已提交
361
  ASSERT_EQ(kv->i, -34);
wmmhello's avatar
wmmhello 已提交
362 363 364
  taosMemoryFree(kv);

  // unsigned tiny int
wmmhello's avatar
wmmhello 已提交
365
  kv = (SSmlKv *)taosArrayGetP(cols, 7);
wmmhello's avatar
wmmhello 已提交
366 367 368 369 370 371 372 373
  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 已提交
374
  kv = (SSmlKv *)taosArrayGetP(cols, 8);
wmmhello's avatar
wmmhello 已提交
375 376 377 378 379 380 381 382
  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 已提交
383
  kv = (SSmlKv *)taosArrayGetP(cols, 9);
wmmhello's avatar
wmmhello 已提交
384 385 386 387 388 389 390 391
  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 已提交
392
  kv = (SSmlKv *)taosArrayGetP(cols, 10);
wmmhello's avatar
wmmhello 已提交
393 394 395 396 397 398 399 400
  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 已提交
401
  kv = (SSmlKv *)taosArrayGetP(cols, 11);
wmmhello's avatar
wmmhello 已提交
402 403 404 405 406 407 408 409 410
  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 已提交
411
  kv = (SSmlKv *)taosArrayGetP(cols, 12);
wmmhello's avatar
wmmhello 已提交
412
  ASSERT_EQ(strncasecmp(kv->key, "ci64", 4), 0);
wmmhello's avatar
wmmhello 已提交
413 414
  ASSERT_EQ(kv->keyLen, 4);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_BIGINT);
wmmhello's avatar
wmmhello 已提交
415 416 417 418 419
  ASSERT_EQ(kv->length, 8);
  ASSERT_EQ(kv->i, -89238);
  taosMemoryFree(kv);

  // bigint
wmmhello's avatar
wmmhello 已提交
420
  kv = (SSmlKv *)taosArrayGetP(cols, 13);
wmmhello's avatar
wmmhello 已提交
421 422 423 424 425 426 427 428
  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 已提交
429
  kv = (SSmlKv *)taosArrayGetP(cols, 14);
wmmhello's avatar
wmmhello 已提交
430 431 432 433 434 435 436 437
  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 已提交
438
  kv = (SSmlKv *)taosArrayGetP(cols, 15);
wmmhello's avatar
wmmhello 已提交
439 440 441 442 443 444 445 446 447
  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 已提交
448
  kv = (SSmlKv *)taosArrayGetP(cols, 16);
wmmhello's avatar
wmmhello 已提交
449
  ASSERT_EQ(strncasecmp(kv->key, "cboolt", 6), 0);
wmmhello's avatar
wmmhello 已提交
450 451 452 453
  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 已提交
454 455 456
  taosMemoryFree(kv);

  // bool
wmmhello's avatar
wmmhello 已提交
457
  kv = (SSmlKv *)taosArrayGetP(cols, 17);
wmmhello's avatar
wmmhello 已提交
458 459 460
  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 已提交
461 462
  ASSERT_EQ(kv->length, 1);
  ASSERT_EQ(kv->i, false);
wmmhello's avatar
wmmhello 已提交
463 464
  taosMemoryFree(kv);

wmmhello's avatar
wmmhello 已提交
465
  // nchar
wmmhello's avatar
wmmhello 已提交
466
  kv = (SSmlKv *)taosArrayGetP(cols, 18);
wmmhello's avatar
wmmhello 已提交
467 468 469 470 471 472 473 474
  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);
475
  taosHashCleanup(dumplicateKey);
wmmhello's avatar
wmmhello 已提交
476
  taosMemoryFree(sql);
wmmhello's avatar
wmmhello 已提交
477 478
}

479
TEST(testCase, smlProcess_influx_Test) {
wmmhello's avatar
wmmhello 已提交
480
  TAOS *taos = taos_connect("localhost", "root", "taosdata", NULL, 0);
wmmhello's avatar
wmmhello 已提交
481
  ASSERT_NE(taos, nullptr);
wmmhello's avatar
wmmhello 已提交
482

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

486
  pRes = taos_query(taos, "use inflx_db");
wmmhello's avatar
wmmhello 已提交
487 488
  taos_free_result(pRes);

wmmhello's avatar
wmmhello 已提交
489 490
  SRequestObj *request = (SRequestObj *)createRequest((STscObj*)taos, NULL, NULL, TSDB_SQL_INSERT);
  ASSERT_NE(request, nullptr);
wmmhello's avatar
wmmhello 已提交
491

492
  SSmlHandle *info = smlBuildSmlInfo(taos, request, TSDB_SML_LINE_PROTOCOL, TSDB_SML_TIMESTAMP_NANO_SECONDS);
wmmhello's avatar
wmmhello 已提交
493
  ASSERT_NE(info, nullptr);
wmmhello's avatar
wmmhello 已提交
494

wmmhello's avatar
wmmhello 已提交
495
  const char *sql[] = {
496 497 498 499 500
    "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 已提交
501
    "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",
502 503 504 505 506
    "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 已提交
507
  };
wmmhello's avatar
wmmhello 已提交
508 509
  int ret = smlProcess(info, (char**)sql, sizeof(sql)/sizeof(sql[0]));
  ASSERT_EQ(ret, 0);
510

511 512
  // case 1
  TAOS_RES *res = taos_query(taos, "select * from t_91e0b182be80332b5c530cbf872f760e");
513 514
  ASSERT_NE(res, nullptr);
  int fieldNum = taos_field_count(res);
515
  ASSERT_EQ(fieldNum, 11);
516
  printf("fieldNum:%d\n", fieldNum);
517 518 519 520 521 522 523 524 525 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

  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++;
582 583
  }
  taos_free_result(res);
584 585 586 587 588 589 590 591 592 593 594

  // 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 已提交
595
  destroyRequest(request);
596
  smlDestroyInfo(info);
wmmhello's avatar
wmmhello 已提交
597
}
598

599
// different types
600 601
TEST(testCase, smlParseLine_error_Test) {
  TAOS *taos = taos_connect("localhost", "root", "taosdata", NULL, 0);
wmmhello's avatar
wmmhello 已提交
602
  ASSERT_NE(taos, nullptr);
603 604 605 606 607 608 609

  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 已提交
610 611
  SRequestObj *request = (SRequestObj *)createRequest((STscObj*)taos, NULL, NULL, TSDB_SQL_INSERT);
  ASSERT_NE(request, nullptr);
612

613
  SSmlHandle *info = smlBuildSmlInfo(taos, request, TSDB_SML_LINE_PROTOCOL, TSDB_SML_TIMESTAMP_NANO_SECONDS);
wmmhello's avatar
wmmhello 已提交
614
  ASSERT_NE(info, nullptr);
615

wmmhello's avatar
wmmhello 已提交
616
  const char *sql[] = {
617 618 619
      "measure,t1=3 c1=8",
      "measure,t2=3 c1=8u8"
  };
620
  int ret = smlProcess(info, (char **)sql, sizeof(sql)/sizeof(sql[0]));
621
  ASSERT_NE(ret, 0);
622 623
  destroyRequest(request);
  smlDestroyInfo(info);
624 625
}

626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
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);

653
  TAOS_RES* pRes = taos_query(taos, "create database if not exists telnet_db");
654 655
  taos_free_result(pRes);

656
  pRes = taos_query(taos, "use telnet_db");
657 658 659 660 661
  taos_free_result(pRes);

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

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

wmmhello's avatar
wmmhello 已提交
665
  const char *sql[] = {
wmmhello's avatar
wmmhello 已提交
666 667 668 669
     "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   "
670 671 672 673
  };
  int ret = smlProcess(info, (char**)sql, sizeof(sql)/sizeof(sql[0]));
  ASSERT_EQ(ret, 0);

674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
  // 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 已提交
699
  destroyRequest(request);
700 701 702 703 704 705 706
  smlDestroyInfo(info);
}

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

707
  TAOS_RES *pRes = taos_query(taos, "create database if not exists json_db");
708 709
  taos_free_result(pRes);

710
  pRes = taos_query(taos, "use json_db");
711 712 713 714 715
  taos_free_result(pRes);

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

716
  SSmlHandle *info = smlBuildSmlInfo(taos, request, TSDB_SML_JSON_PROTOCOL, TSDB_SML_TIMESTAMP_NANO_SECONDS);
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
  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 已提交
740
  int ret = smlProcess(info, (char **)(&sql), 1);
741 742
  ASSERT_EQ(ret, 0);

743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
  // 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 已提交
768
  destroyRequest(request);
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
  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);

785
  SSmlHandle *info = smlBuildSmlInfo(taos, request, TSDB_SML_JSON_PROTOCOL, TSDB_SML_TIMESTAMP_NANO_SECONDS);
786 787 788
  ASSERT_NE(info, nullptr);
  const char *sql =
     "{\n"
789
     "    \"metric\": \"meter_current0\",\n"
790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811
     "    \"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 已提交
812
  destroyRequest(request);
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
  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);

829
  SSmlHandle *info = smlBuildSmlInfo(taos, request, TSDB_SML_JSON_PROTOCOL, TSDB_SML_TIMESTAMP_NANO_SECONDS);
830 831 832
  ASSERT_NE(info, nullptr);
  const char *sql =
     "{\n"
833
     "    \"metric\": \"meter_current1\",\n"
834 835 836 837 838 839 840 841 842 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
     "    \"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 已提交
884
  destroyRequest(request);
885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900
  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);

901
  SSmlHandle *info = smlBuildSmlInfo(taos, request, TSDB_SML_JSON_PROTOCOL, TSDB_SML_TIMESTAMP_NANO_SECONDS);
902 903
  ASSERT_NE(info, nullptr);
  const char *sql = "{\n"
904
     "    \"metric\": \"meter_current2\",\n"
905
     "    \"timestamp\": {\n"
906
     "        \"value\"  : 1346846500000,\n"
907 908 909 910 911 912 913 914 915 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
     "        \"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 已提交
946
  destroyRequest(request);
947 948
  smlDestroyInfo(info);
}
949 950 951 952 953 954 955 956 957 958 959 960 961 962

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

963
  SSmlHandle *info = smlBuildSmlInfo(taos, request, TSDB_SML_TELNET_PROTOCOL, TSDB_SML_TIMESTAMP_NANO_SECONDS);
964 965 966
  ASSERT_NE(info, nullptr);

  int32_t ret = 0;
wmmhello's avatar
wmmhello 已提交
967
  const char *sql[] = {
968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986
      "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 已提交
987 988
      "sys.procs.running 1479496100 42 host=web01=er",
      "sys.procs.running 1479496100 42 host= web01",
989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
  };
  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);

1012
  SSmlHandle *info = smlBuildSmlInfo(taos, request, TSDB_SML_TELNET_PROTOCOL, TSDB_SML_TIMESTAMP_NANO_SECONDS);
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
  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);

1039
  SSmlHandle *info = smlBuildSmlInfo(taos, request, TSDB_SML_TELNET_PROTOCOL, TSDB_SML_TIMESTAMP_NANO_SECONDS);
1040 1041 1042 1043 1044 1045 1046 1047 1048 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
  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);

1107
  SSmlHandle *info = smlBuildSmlInfo(taos, request, TSDB_SML_TELNET_PROTOCOL, TSDB_SML_TIMESTAMP_NANO_SECONDS);
1108 1109 1110 1111 1112 1113 1114 1115 1116 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
  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);

1152
  SSmlHandle *info = smlBuildSmlInfo(taos, request, TSDB_SML_TELNET_PROTOCOL, TSDB_SML_TIMESTAMP_NANO_SECONDS);
1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
  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);
}
1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196

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 已提交
1197
  SSmlHandle *info = smlBuildSmlInfo(taos, request, TSDB_SML_LINE_PROTOCOL, TSDB_SML_TIMESTAMP_MILLI_SECONDS);
1198 1199 1200
  ASSERT_NE(info, nullptr);

  const char *sql[] = {
1201 1202
      "hetrey c0=f,c1=127i8 1626006833639",
      "hetrey,t1=r c0=f,c1=127i8 1626006833640",
1203 1204 1205 1206
  };
  int ret = smlProcess(info, (char **)sql, sizeof(sql) / sizeof(sql[0]));
  ASSERT_EQ(ret, 0);

wmmhello's avatar
wmmhello 已提交
1207 1208
  destroyRequest(request);
  smlDestroyInfo(info);
wmmhello's avatar
wmmhello 已提交
1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240
}

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 已提交
1241
  TAOS_RES* pRes = taos_query(taos, "create database if not exists TD15742");
wmmhello's avatar
wmmhello 已提交
1242 1243
  taos_free_result(pRes);

wmmhello's avatar
wmmhello 已提交
1244
  pRes = taos_query(taos, "use TD15742");
wmmhello's avatar
wmmhello 已提交
1245 1246 1247 1248 1249
  taos_free_result(pRes);

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

wmmhello's avatar
wmmhello 已提交
1250
  SSmlHandle *info = smlBuildSmlInfo(taos, request, TSDB_SML_LINE_PROTOCOL, TSDB_SML_TIMESTAMP_MILLI_SECONDS);
wmmhello's avatar
wmmhello 已提交
1251 1252 1253
  ASSERT_NE(info, nullptr);

  const char *sql[] = {
wmmhello's avatar
wmmhello 已提交
1254
      "test_ms,t0=t c0=f 1626006833641",
wmmhello's avatar
wmmhello 已提交
1255 1256 1257 1258 1259 1260
  };
  int ret = smlProcess(info, (char**)sql, sizeof(sql)/sizeof(sql[0]));
  ASSERT_EQ(ret, 0);

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