clientSml.c 56.6 KB
Newer Older
wmmhello's avatar
wmmhello 已提交
1 2 3 4 5 6 7
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "query.h"
#include "taos.h"
wmmhello's avatar
wmmhello 已提交
8 9 10 11
#include "taoserror.h"
#include "tdef.h"
#include "tlog.h"
#include "tmsg.h"
wmmhello's avatar
wmmhello 已提交
12
#include "tstrbuild.h"
wmmhello's avatar
wmmhello 已提交
13 14 15 16
#include "ttime.h"
#include "ttypes.h"
#include "tcommon.h"
#include "catalog.h"
17
#include "clientInt.h"
wmmhello's avatar
wmmhello 已提交
18
//=================================================================================================
wmmhello's avatar
wmmhello 已提交
19 20 21 22 23 24

#define SPACE ' '
#define COMMA ','
#define EQUAL '='
#define QUOTE '"'
#define SLASH '\\'
wmmhello's avatar
wmmhello 已提交
25
#define tsMaxSQLStringLen (1024*1024)
wmmhello's avatar
wmmhello 已提交
26

wmmhello's avatar
wmmhello 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39
//=================================================================================================
typedef TSDB_SML_PROTOCOL_TYPE SMLProtocolType;

typedef enum {
  SCHEMA_ACTION_CREATE_STABLE,
  SCHEMA_ACTION_ADD_COLUMN,
  SCHEMA_ACTION_ADD_TAG,
  SCHEMA_ACTION_CHANGE_COLUMN_SIZE,
  SCHEMA_ACTION_CHANGE_TAG_SIZE,
} ESchemaAction;

typedef struct {
  char sTableName[TSDB_TABLE_NAME_LEN];
40 41
  SArray *tags;
  SArray *fields;
wmmhello's avatar
wmmhello 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
} SCreateSTableActionInfo;

typedef struct {
  char sTableName[TSDB_TABLE_NAME_LEN];
  SSmlKv * field;
} SAlterSTableActionInfo;

typedef struct {
  ESchemaAction action;
  union {
    SCreateSTableActionInfo createSTable;
    SAlterSTableActionInfo alterSTable;
  };
} SSchemaAction;

typedef struct {
  const char* measure;
  const char* tags;
  const char* cols;
  const char* timestamp;

  int32_t measureLen;
  int32_t measureTagsLen;
  int32_t tagsLen;
  int32_t colsLen;
  int32_t timestampLen;
} SSmlLineInfo;

typedef struct {
  const char     *sTableName;   // super table name
  uint8_t        sTableNameLen;
  char           childTableName[TSDB_TABLE_NAME_LEN];
  uint64_t       uid;

  SArray         *tags;

  // colsFormat store cols formated, for quick parse, if info->formatData is true
  SArray         *colsFormat;  // elements are SArray<SSmlKv*>

81
  // cols store cols un formated
wmmhello's avatar
wmmhello 已提交
82 83 84 85
  SArray         *cols;        // elements are SHashObj<cols key string, SSmlKv*> for find by key quickly
} SSmlTableInfo;

typedef struct {
86 87 88 89
  SArray     *tags;       // save the origin order to create table
  SHashObj   *tagHash;    // elements are <key, index in tags>

  SArray     *cols;
wmmhello's avatar
wmmhello 已提交
90
  SHashObj   *fieldHash;
91

wmmhello's avatar
wmmhello 已提交
92 93 94 95 96 97 98 99
  STableMeta *tableMeta;
} SSmlSTableMeta;

typedef struct {
  int32_t   len;
  char      *buf;
} SSmlMsgBuf;

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
typedef struct {
  int32_t code;
  int32_t lineNum;

  int32_t numOfSTables;
  int32_t numOfCTables;
  int32_t numOfCreateSTables;

  int64_t parseTime;
  int64_t schemaTime;
  int64_t insertBindTime;
  int64_t insertRpcTime;
  int64_t endTime;
} SSmlCostInfo;

wmmhello's avatar
wmmhello 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
typedef struct {
  uint64_t          id;

  SMLProtocolType   protocol;
  int8_t            precision;
  bool              dataFormat;     // true means that the name, number and order of keys in each line are the same

  SHashObj          *childTables;
  SHashObj          *superTables;
  SHashObj          *pVgHash;
  void              *exec;

  STscObj           *taos;
  SCatalog          *pCatalog;
  SRequestObj       *pRequest;
  SQuery            *pQuery;

132
  SSmlCostInfo      cost;
wmmhello's avatar
wmmhello 已提交
133 134
  int32_t           affectedRows;
  SSmlMsgBuf        msgBuf;
135 136
  SHashObj          *dumplicateKey;  // for dumplicate key
  SArray            *colsContainer;  // for cols parse, if is dataFormat == false
wmmhello's avatar
wmmhello 已提交
137
} SSmlHandle;
wmmhello's avatar
wmmhello 已提交
138 139 140
//=================================================================================================

static uint64_t linesSmlHandleId = 0;
wmmhello's avatar
wmmhello 已提交
141 142
static const char* TS = "_ts";
static const char* TAG = "_tagNone";
wmmhello's avatar
wmmhello 已提交
143

wmmhello's avatar
wmmhello 已提交
144
//=================================================================================================
wmmhello's avatar
wmmhello 已提交
145

wmmhello's avatar
wmmhello 已提交
146
static uint64_t smlGenId() {
wmmhello's avatar
wmmhello 已提交
147 148 149 150 151 152 153 154 155
  uint64_t id;

  do {
    id = atomic_add_fetch_64(&linesSmlHandleId, 1);
  } while (id == 0);

  return id;
}

wmmhello's avatar
wmmhello 已提交
156
static int32_t smlBuildInvalidDataMsg(SSmlMsgBuf* pBuf, const char *msg1, const char *msg2) {
wmmhello's avatar
wmmhello 已提交
157 158 159 160 161 162
  if(msg1) strncat(pBuf->buf, msg1, pBuf->len);
  int32_t left = pBuf->len - strlen(pBuf->buf);
  if(left > 2 && msg2) {
    strncat(pBuf->buf, ":", left - 1);
    strncat(pBuf->buf, msg2, left - 2);
  }
wmmhello's avatar
wmmhello 已提交
163 164 165
  return TSDB_CODE_SML_INVALID_DATA;
}

wmmhello's avatar
wmmhello 已提交
166
static int smlCompareKv(const void* p1, const void* p2) {
167 168
  SSmlKv* kv1 = *(SSmlKv**)p1;
  SSmlKv* kv2 = *(SSmlKv**)p2;
wmmhello's avatar
wmmhello 已提交
169 170
  int32_t kvLen1 = kv1->keyLen;
  int32_t kvLen2 = kv2->keyLen;
wmmhello's avatar
wmmhello 已提交
171
  int32_t res = strncasecmp(kv1->key, kv2->key, TMIN(kvLen1, kvLen2));
wmmhello's avatar
wmmhello 已提交
172 173 174 175 176 177 178
  if (res != 0) {
    return res;
  } else {
    return kvLen1-kvLen2;
  }
}

wmmhello's avatar
wmmhello 已提交
179
static void smlBuildChildTableName(SSmlTableInfo *tags) {
wmmhello's avatar
wmmhello 已提交
180 181
  int32_t size = taosArrayGetSize(tags->tags);
  ASSERT(size > 0);
wmmhello's avatar
wmmhello 已提交
182
  taosArraySort(tags->tags, smlCompareKv);
wmmhello's avatar
wmmhello 已提交
183

wmmhello's avatar
wmmhello 已提交
184 185 186 187 188 189
  SStringBuilder sb = {0};
  taosStringBuilderAppendStringLen(&sb, tags->sTableName, tags->sTableNameLen);
  for (int j = 0; j < size; ++j) {
    SSmlKv *tagKv = taosArrayGetP(tags->tags, j);
    taosStringBuilderAppendStringLen(&sb, tagKv->key, tagKv->keyLen);
    taosStringBuilderAppendStringLen(&sb, tagKv->value, tagKv->valueLen);
wmmhello's avatar
wmmhello 已提交
190 191 192 193 194 195 196 197
  }
  size_t len = 0;
  char* keyJoined = taosStringBuilderGetResult(&sb, &len);
  T_MD5_CTX context;
  tMD5Init(&context);
  tMD5Update(&context, (uint8_t *)keyJoined, (uint32_t)len);
  tMD5Final(&context);
  uint64_t digest1 = *(uint64_t*)(context.digest);
198 199 200
  //uint64_t digest2 = *(uint64_t*)(context.digest + 8);
  //snprintf(tags->childTableName, TSDB_TABLE_NAME_LEN, "t_%016"PRIx64"%016"PRIx64, digest1, digest2);
  snprintf(tags->childTableName, TSDB_TABLE_NAME_LEN, "t_%016"PRIx64, digest1);
wmmhello's avatar
wmmhello 已提交
201
  taosStringBuilderDestroy(&sb);
wmmhello's avatar
wmmhello 已提交
202
  tags->uid = digest1;
wmmhello's avatar
wmmhello 已提交
203 204
}

wmmhello's avatar
wmmhello 已提交
205 206
static int32_t smlGenerateSchemaAction(SSchema* pointColField, SHashObj* dbAttrHash, SArray* dbAttrArray, bool isTag, char sTableName[],
                                       SSchemaAction* action, bool* actionNeeded, SSmlHandle* info) {
wmmhello's avatar
wmmhello 已提交
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
//  char fieldName[TSDB_COL_NAME_LEN] = {0};
//  strcpy(fieldName, pointColField->name);
//
//  size_t* pDbIndex = taosHashGet(dbAttrHash, fieldName, strlen(fieldName));
//  if (pDbIndex) {
//    SSchema* dbAttr = taosArrayGet(dbAttrArray, *pDbIndex);
//    assert(strcasecmp(dbAttr->name, pointColField->name) == 0);
//    if (pointColField->type != dbAttr->type) {
//      uError("SML:0x%"PRIx64" point type and db type mismatch. key: %s. point type: %d, db type: %d", info->id, pointColField->name,
//               pointColField->type, dbAttr->type);
//      return TSDB_CODE_TSC_INVALID_VALUE;
//    }
//
//    if (IS_VAR_DATA_TYPE(pointColField->type) && (pointColField->bytes > dbAttr->bytes)) {
//      if (isTag) {
//        action->action = SCHEMA_ACTION_CHANGE_TAG_SIZE;
//      } else {
//        action->action = SCHEMA_ACTION_CHANGE_COLUMN_SIZE;
//      }
//      memset(&action->alterSTable, 0,  sizeof(SAlterSTableActionInfo));
//      memcpy(action->alterSTable.sTableName, sTableName, TSDB_TABLE_NAME_LEN);
//      action->alterSTable.field = pointColField;
//      *actionNeeded = true;
//    }
//  } else {
//    if (isTag) {
//      action->action = SCHEMA_ACTION_ADD_TAG;
//    } else {
//      action->action = SCHEMA_ACTION_ADD_COLUMN;
//    }
//    memset(&action->alterSTable, 0, sizeof(SAlterSTableActionInfo));
//    memcpy(action->alterSTable.sTableName, sTableName, TSDB_TABLE_NAME_LEN);
//    action->alterSTable.field = pointColField;
//    *actionNeeded = true;
//  }
//  if (*actionNeeded) {
//    uDebug("SML:0x%" PRIx64 " generate schema action. column name: %s, action: %d", info->id, fieldName,
//             action->action);
//  }
wmmhello's avatar
wmmhello 已提交
246 247 248
  return 0;
}

wmmhello's avatar
wmmhello 已提交
249
static int32_t smlBuildColumnDescription(SSmlKv* field, char* buf, int32_t bufSize, int32_t* outBytes) {
wmmhello's avatar
wmmhello 已提交
250
  uint8_t type = field->type;
wmmhello's avatar
wmmhello 已提交
251 252
  char    tname[TSDB_TABLE_NAME_LEN] = {0};
  memcpy(tname, field->key, field->keyLen);
wmmhello's avatar
wmmhello 已提交
253
  if (type == TSDB_DATA_TYPE_BINARY || type == TSDB_DATA_TYPE_NCHAR) {
wmmhello's avatar
wmmhello 已提交
254
    int32_t bytes = field->valueLen;   // todo
wmmhello's avatar
wmmhello 已提交
255
    int out = snprintf(buf, bufSize,"%s %s(%d)",
wmmhello's avatar
wmmhello 已提交
256
                       tname,tDataTypes[field->type].name, bytes);
wmmhello's avatar
wmmhello 已提交
257 258
    *outBytes = out;
  } else {
wmmhello's avatar
wmmhello 已提交
259
    int out = snprintf(buf, bufSize, "%s %s", tname, tDataTypes[type].name);
wmmhello's avatar
wmmhello 已提交
260 261 262 263 264 265
    *outBytes = out;
  }

  return 0;
}

wmmhello's avatar
wmmhello 已提交
266
static int32_t smlApplySchemaAction(SSmlHandle* info, SSchemaAction* action) {
wmmhello's avatar
wmmhello 已提交
267 268
  int32_t code = 0;
  int32_t outBytes = 0;
wmmhello's avatar
wmmhello 已提交
269
  char *result = (char *)taosMemoryCalloc(1, tsMaxSQLStringLen+1);
wmmhello's avatar
wmmhello 已提交
270 271 272 273 274 275
  int32_t capacity = tsMaxSQLStringLen +  1;

  uDebug("SML:0x%"PRIx64" apply schema action. action: %d", info->id, action->action);
  switch (action->action) {
    case SCHEMA_ACTION_ADD_COLUMN: {
      int n = sprintf(result, "alter stable %s add column ", action->alterSTable.sTableName);
wmmhello's avatar
wmmhello 已提交
276 277
      smlBuildColumnDescription(action->alterSTable.field, result+n, capacity-n, &outBytes);
      TAOS_RES* res = taos_query(info->taos, result); //TODO async doAsyncQuery
wmmhello's avatar
wmmhello 已提交
278
      code = taos_errno(res);
wmmhello's avatar
wmmhello 已提交
279
      const char* errStr = taos_errstr(res);
wmmhello's avatar
wmmhello 已提交
280 281 282 283 284 285 286
      char* begin = strstr(errStr, "duplicated column names");
      bool tscDupColNames = (begin != NULL);
      if (code != TSDB_CODE_SUCCESS) {
        uError("SML:0x%"PRIx64" apply schema action. error: %s", info->id, errStr);
      }
      taos_free_result(res);

wmmhello's avatar
wmmhello 已提交
287 288
//      if (code == TSDB_CODE_MND_FIELD_ALREADY_EXIST || code == TSDB_CODE_MND_TAG_ALREADY_EXIST || tscDupColNames) {
      if (code == TSDB_CODE_MND_TAG_ALREADY_EXIST || tscDupColNames) {
wmmhello's avatar
wmmhello 已提交
289
        TAOS_RES* res2 = taos_query(info->taos, "RESET QUERY CACHE");
wmmhello's avatar
wmmhello 已提交
290 291 292 293 294 295 296 297 298 299 300
        code = taos_errno(res2);
        if (code != TSDB_CODE_SUCCESS) {
          uError("SML:0x%" PRIx64 " apply schema action. reset query cache. error: %s", info->id, taos_errstr(res2));
        }
        taos_free_result(res2);
        taosMsleep(500);
      }
      break;
    }
    case SCHEMA_ACTION_ADD_TAG: {
      int n = sprintf(result, "alter stable %s add tag ", action->alterSTable.sTableName);
wmmhello's avatar
wmmhello 已提交
301
      smlBuildColumnDescription(action->alterSTable.field,
wmmhello's avatar
wmmhello 已提交
302
                             result+n, capacity-n, &outBytes);
wmmhello's avatar
wmmhello 已提交
303
      TAOS_RES* res = taos_query(info->taos, result); //TODO async doAsyncQuery
wmmhello's avatar
wmmhello 已提交
304
      code = taos_errno(res);
wmmhello's avatar
wmmhello 已提交
305
      const char* errStr = taos_errstr(res);
wmmhello's avatar
wmmhello 已提交
306 307 308 309 310 311 312
      char* begin = strstr(errStr, "duplicated column names");
      bool tscDupColNames = (begin != NULL);
      if (code != TSDB_CODE_SUCCESS) {
        uError("SML:0x%"PRIx64" apply schema action. error : %s", info->id, taos_errstr(res));
      }
      taos_free_result(res);

wmmhello's avatar
wmmhello 已提交
313 314
//      if (code ==TSDB_CODE_MND_TAG_ALREADY_EXIST || code == TSDB_CODE_MND_FIELD_ALREAY_EXIST || tscDupColNames) {
      if (code ==TSDB_CODE_MND_TAG_ALREADY_EXIST || tscDupColNames) {
wmmhello's avatar
wmmhello 已提交
315
        TAOS_RES* res2 = taos_query(info->taos, "RESET QUERY CACHE");
wmmhello's avatar
wmmhello 已提交
316 317 318 319 320 321 322 323 324 325 326
        code = taos_errno(res2);
        if (code != TSDB_CODE_SUCCESS) {
          uError("SML:0x%" PRIx64 " apply schema action. reset query cache. error: %s", info->id, taos_errstr(res2));
        }
        taos_free_result(res2);
        taosMsleep(500);
      }
      break;
    }
    case SCHEMA_ACTION_CHANGE_COLUMN_SIZE: {
      int n = sprintf(result, "alter stable %s modify column ", action->alterSTable.sTableName);
wmmhello's avatar
wmmhello 已提交
327
      smlBuildColumnDescription(action->alterSTable.field, result+n,
wmmhello's avatar
wmmhello 已提交
328
                             capacity-n, &outBytes);
wmmhello's avatar
wmmhello 已提交
329
      TAOS_RES* res = taos_query(info->taos, result); //TODO async doAsyncQuery
wmmhello's avatar
wmmhello 已提交
330 331 332 333 334 335
      code = taos_errno(res);
      if (code != TSDB_CODE_SUCCESS) {
        uError("SML:0x%"PRIx64" apply schema action. error : %s", info->id, taos_errstr(res));
      }
      taos_free_result(res);

wmmhello's avatar
wmmhello 已提交
336 337
//      if (code == TSDB_CODE_MND_INVALID_COLUMN_LENGTH || code == TSDB_CODE_TSC_INVALID_COLUMN_LENGTH) {
      if (code == TSDB_CODE_TSC_INVALID_COLUMN_LENGTH) {
wmmhello's avatar
wmmhello 已提交
338
        TAOS_RES* res2 = taos_query(info->taos, "RESET QUERY CACHE");
wmmhello's avatar
wmmhello 已提交
339 340 341 342 343 344 345 346 347 348 349
        code = taos_errno(res2);
        if (code != TSDB_CODE_SUCCESS) {
          uError("SML:0x%" PRIx64 " apply schema action. reset query cache. error: %s", info->id, taos_errstr(res2));
        }
        taos_free_result(res2);
        taosMsleep(500);
      }
      break;
    }
    case SCHEMA_ACTION_CHANGE_TAG_SIZE: {
      int n = sprintf(result, "alter stable %s modify tag ", action->alterSTable.sTableName);
wmmhello's avatar
wmmhello 已提交
350
      smlBuildColumnDescription(action->alterSTable.field, result+n,
wmmhello's avatar
wmmhello 已提交
351
                             capacity-n, &outBytes);
wmmhello's avatar
wmmhello 已提交
352
      TAOS_RES* res = taos_query(info->taos, result); //TODO async doAsyncQuery
wmmhello's avatar
wmmhello 已提交
353 354 355 356 357 358
      code = taos_errno(res);
      if (code != TSDB_CODE_SUCCESS) {
        uError("SML:0x%"PRIx64" apply schema action. error : %s", info->id, taos_errstr(res));
      }
      taos_free_result(res);

wmmhello's avatar
wmmhello 已提交
359 360
//      if (code == TSDB_CODE_MND_INVALID_TAG_LENGTH || code == TSDB_CODE_TSC_INVALID_TAG_LENGTH) {
      if (code == TSDB_CODE_TSC_INVALID_TAG_LENGTH) {
wmmhello's avatar
wmmhello 已提交
361
        TAOS_RES* res2 = taos_query(info->taos, "RESET QUERY CACHE");
wmmhello's avatar
wmmhello 已提交
362 363 364 365 366 367 368 369 370 371 372 373
        code = taos_errno(res2);
        if (code != TSDB_CODE_SUCCESS) {
          uError("SML:0x%" PRIx64 " apply schema action. reset query cache. error: %s", info->id, taos_errstr(res2));
        }
        taos_free_result(res2);
        taosMsleep(500);
      }
      break;
    }
    case SCHEMA_ACTION_CREATE_STABLE: {
      int n = sprintf(result, "create stable %s (", action->createSTable.sTableName);
      char* pos = result + n; int freeBytes = capacity - n;
wmmhello's avatar
wmmhello 已提交
374

375
      SArray *cols = action->createSTable.fields;
wmmhello's avatar
wmmhello 已提交
376 377

      for(int i = 0; i < taosArrayGetSize(cols); i++){
378 379
        SSmlKv *kv = taosArrayGetP(cols, i);
        smlBuildColumnDescription(kv, pos, freeBytes, &outBytes);
wmmhello's avatar
wmmhello 已提交
380 381 382
        pos += outBytes; freeBytes -= outBytes;
        *pos = ','; ++pos; --freeBytes;
      }
wmmhello's avatar
wmmhello 已提交
383

wmmhello's avatar
wmmhello 已提交
384 385 386 387 388
      --pos; ++freeBytes;

      outBytes = snprintf(pos, freeBytes, ") tags (");
      pos += outBytes; freeBytes -= outBytes;

389 390 391 392
      cols = action->createSTable.tags;
      for(int i = 0; i < taosArrayGetSize(cols); i++){
        SSmlKv *kv = taosArrayGetP(cols, i);
        smlBuildColumnDescription(kv, pos, freeBytes, &outBytes);
wmmhello's avatar
wmmhello 已提交
393 394 395 396 397
        pos += outBytes; freeBytes -= outBytes;
        *pos = ','; ++pos; --freeBytes;
      }
      pos--; ++freeBytes;
      outBytes = snprintf(pos, freeBytes, ")");
wmmhello's avatar
wmmhello 已提交
398
      TAOS_RES* res = taos_query(info->taos, result);
wmmhello's avatar
wmmhello 已提交
399 400 401 402 403 404
      code = taos_errno(res);
      if (code != TSDB_CODE_SUCCESS) {
        uError("SML:0x%"PRIx64" apply schema action. error : %s", info->id, taos_errstr(res));
      }
      taos_free_result(res);

wmmhello's avatar
wmmhello 已提交
405
      if (code == TSDB_CODE_MND_STB_ALREADY_EXIST) {
wmmhello's avatar
wmmhello 已提交
406
        TAOS_RES* res2 = taos_query(info->taos, "RESET QUERY CACHE");
wmmhello's avatar
wmmhello 已提交
407 408 409 410 411 412 413 414 415 416 417 418 419 420
        code = taos_errno(res2);
        if (code != TSDB_CODE_SUCCESS) {
          uError("SML:0x%" PRIx64 " apply schema action. reset query cache. error: %s", info->id, taos_errstr(res2));
        }
        taos_free_result(res2);
        taosMsleep(500);
      }
      break;
    }

    default:
      break;
  }

wmmhello's avatar
wmmhello 已提交
421
  taosMemoryFreeClear(result);
wmmhello's avatar
wmmhello 已提交
422 423 424 425 426 427
  if (code != 0) {
    uError("SML:0x%"PRIx64 " apply schema action failure. %s", info->id, tstrerror(code));
  }
  return code;
}

wmmhello's avatar
wmmhello 已提交
428
static int32_t smlModifyDBSchemas(SSmlHandle* info) {
wmmhello's avatar
wmmhello 已提交
429 430 431 432
  int32_t code = 0;

  SSmlSTableMeta** tableMetaSml = taosHashIterate(info->superTables, NULL);
  while (tableMetaSml) {
433
    SSmlSTableMeta* sTableData = *tableMetaSml;
wmmhello's avatar
wmmhello 已提交
434 435

    STableMeta *pTableMeta = NULL;
wmmhello's avatar
wmmhello 已提交
436
    SEpSet ep = getEpSet_s(&info->taos->pAppInfo->mgmtEp);
wmmhello's avatar
wmmhello 已提交
437

wmmhello's avatar
wmmhello 已提交
438
    size_t superTableLen = 0;
wmmhello's avatar
wmmhello 已提交
439
    void *superTable = taosHashGetKey(tableMetaSml, &superTableLen);    // todo escape
wmmhello's avatar
wmmhello 已提交
440 441 442
    SName pName = {TSDB_TABLE_NAME_T, info->taos->acctId, {0}, {0}};
    strcpy(pName.dbname, info->pRequest->pDb);
    memcpy(pName.tname, superTable, superTableLen);
wmmhello's avatar
wmmhello 已提交
443

wmmhello's avatar
wmmhello 已提交
444
    code = catalogGetSTableMeta(info->pCatalog, info->taos->pAppInfo->pTransporter, &ep, &pName, &pTableMeta);
wmmhello's avatar
wmmhello 已提交
445

wmmhello's avatar
wmmhello 已提交
446
    if (code == TSDB_CODE_TDB_INVALID_TABLE_ID || code == TSDB_CODE_MND_INVALID_STB) {
wmmhello's avatar
wmmhello 已提交
447 448
      SSchemaAction schemaAction = {0};
      schemaAction.action = SCHEMA_ACTION_CREATE_STABLE;
wmmhello's avatar
wmmhello 已提交
449
      memcpy(schemaAction.createSTable.sTableName, superTable, superTableLen);
450 451
      schemaAction.createSTable.tags = sTableData->tags;
      schemaAction.createSTable.fields = sTableData->cols;
wmmhello's avatar
wmmhello 已提交
452 453 454 455 456 457
      code = smlApplySchemaAction(info, &schemaAction);
      if (code != 0) {
        uError("SML:0x%"PRIx64" smlApplySchemaAction failed. can not create %s", info->id, schemaAction.createSTable.sTableName);
        return code;
      }

wmmhello's avatar
wmmhello 已提交
458
      code = catalogGetSTableMeta(info->pCatalog, info->taos->pAppInfo->pTransporter, &ep, &pName, &pTableMeta);
wmmhello's avatar
wmmhello 已提交
459
      if (code != 0) {
wmmhello's avatar
wmmhello 已提交
460
        uError("SML:0x%"PRIx64" catalogGetSTableMeta failed. super table name %s", info->id, schemaAction.createSTable.sTableName);
wmmhello's avatar
wmmhello 已提交
461 462
        return code;
      }
463
      info->cost.numOfCreateSTables++;
wmmhello's avatar
wmmhello 已提交
464
    }else if (code == TSDB_CODE_SUCCESS) {
wmmhello's avatar
wmmhello 已提交
465 466 467 468
    } else {
      uError("SML:0x%"PRIx64" load table meta error: %s", info->id, tstrerror(code));
      return code;
    }
469
    sTableData->tableMeta = pTableMeta;
wmmhello's avatar
wmmhello 已提交
470

wmmhello's avatar
wmmhello 已提交
471
    tableMetaSml = taosHashIterate(info->superTables, tableMetaSml);
wmmhello's avatar
wmmhello 已提交
472 473 474 475 476 477 478 479 480 481 482
  }
  return 0;
}

//=========================================================================

/*        Field                          Escape charaters
    1: measurement                        Comma,Space
    2: tag_key, tag_value, field_key  Comma,Equal Sign,Space
    3: field_value                    Double quote,Backslash
*/
wmmhello's avatar
wmmhello 已提交
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
//static void escapeSpecialCharacter(uint8_t field, const char **pos) {
//  const char *cur = *pos;
//  if (*cur != '\\') {
//    return;
//  }
//  switch (field) {
//    case 1:
//      switch (*(cur + 1)) {
//        case ',':
//        case ' ':
//          cur++;
//          break;
//        default:
//          break;
//      }
//      break;
//    case 2:
//      switch (*(cur + 1)) {
//        case ',':
//        case ' ':
//        case '=':
//          cur++;
//          break;
//        default:
//          break;
//      }
//      break;
//    case 3:
//      switch (*(cur + 1)) {
//        case '"':
//        case '\\':
//          cur++;
//          break;
//        default:
//          break;
//      }
//      break;
//    default:
//      break;
//  }
//  *pos = cur;
//}
wmmhello's avatar
wmmhello 已提交
525

wmmhello's avatar
wmmhello 已提交
526
static bool smlParseTinyInt(SSmlKv *kvVal, bool *isValid, SSmlMsgBuf *msg) {
wmmhello's avatar
wmmhello 已提交
527 528
  const char *pVal = kvVal->value;
  int32_t len = kvVal->valueLen;
wmmhello's avatar
wmmhello 已提交
529 530 531
  if (len <= 2) {
    return false;
  }
wmmhello's avatar
wmmhello 已提交
532
  const char *signalPos = pVal + len - 2;
wmmhello's avatar
wmmhello 已提交
533
  if (!strncasecmp(signalPos, "i8", 2)) {
wmmhello's avatar
wmmhello 已提交
534 535 536 537
    char *endptr = NULL;
    int64_t result = strtoll(pVal, &endptr, 10);
    if(endptr != signalPos){       // 78ri8
      *isValid = false;
wmmhello's avatar
wmmhello 已提交
538
      smlBuildInvalidDataMsg(msg, "invalid tiny int", endptr);
wmmhello's avatar
wmmhello 已提交
539 540
    }else if(!IS_VALID_TINYINT(result)){
      *isValid = false;
wmmhello's avatar
wmmhello 已提交
541
      smlBuildInvalidDataMsg(msg, "tiny int out of range[-128,127]", endptr);
wmmhello's avatar
wmmhello 已提交
542 543 544 545
    }else{
      kvVal->i = result;
      *isValid = true;
    }
wmmhello's avatar
wmmhello 已提交
546 547 548 549 550
    return true;
  }
  return false;
}

wmmhello's avatar
wmmhello 已提交
551
static bool smlParseTinyUint(SSmlKv *kvVal, bool *isValid, SSmlMsgBuf *msg) {
wmmhello's avatar
wmmhello 已提交
552 553
  const char *pVal = kvVal->value;
  int32_t len = kvVal->valueLen;
wmmhello's avatar
wmmhello 已提交
554 555 556 557 558 559
  if (len <= 2) {
    return false;
  }
  if (pVal[0] == '-') {
    return false;
  }
wmmhello's avatar
wmmhello 已提交
560
  const char *signalPos = pVal + len - 2;
wmmhello's avatar
wmmhello 已提交
561
  if (!strncasecmp(signalPos, "u8", 2)) {
wmmhello's avatar
wmmhello 已提交
562 563 564 565
    char *endptr = NULL;
    int64_t result = strtoll(pVal, &endptr, 10);
    if(endptr != signalPos){       // 78ri8
      *isValid = false;
wmmhello's avatar
wmmhello 已提交
566
      smlBuildInvalidDataMsg(msg, "invalid unsigned tiny int", endptr);
wmmhello's avatar
wmmhello 已提交
567 568
    }else if(!IS_VALID_UTINYINT(result)){
      *isValid = false;
wmmhello's avatar
wmmhello 已提交
569
      smlBuildInvalidDataMsg(msg, "unsigned tiny int out of range[0,255]", endptr);
wmmhello's avatar
wmmhello 已提交
570 571 572 573
    }else{
      kvVal->i = result;
      *isValid = true;
    }
wmmhello's avatar
wmmhello 已提交
574 575 576 577 578
    return true;
  }
  return false;
}

wmmhello's avatar
wmmhello 已提交
579
static bool smlParseSmallInt(SSmlKv *kvVal, bool *isValid, SSmlMsgBuf *msg) {
wmmhello's avatar
wmmhello 已提交
580 581
  const char *pVal = kvVal->value;
  int32_t len = kvVal->valueLen;
wmmhello's avatar
wmmhello 已提交
582 583 584
  if (len <= 3) {
    return false;
  }
wmmhello's avatar
wmmhello 已提交
585
  const char *signalPos = pVal + len - 3;
wmmhello's avatar
wmmhello 已提交
586
  if (!strncasecmp(signalPos, "i16", 3)) {
wmmhello's avatar
wmmhello 已提交
587 588 589 590
    char *endptr = NULL;
    int64_t result = strtoll(pVal, &endptr, 10);
    if(endptr != signalPos){       // 78ri8
      *isValid = false;
wmmhello's avatar
wmmhello 已提交
591
      smlBuildInvalidDataMsg(msg, "invalid small int", endptr);
wmmhello's avatar
wmmhello 已提交
592 593
    }else if(!IS_VALID_SMALLINT(result)){
      *isValid = false;
wmmhello's avatar
wmmhello 已提交
594
      smlBuildInvalidDataMsg(msg, "small int our of range[-32768,32767]", endptr);
wmmhello's avatar
wmmhello 已提交
595 596 597 598
    }else{
      kvVal->i = result;
      *isValid = true;
    }
wmmhello's avatar
wmmhello 已提交
599 600 601 602 603
    return true;
  }
  return false;
}

wmmhello's avatar
wmmhello 已提交
604
static bool smlParseSmallUint(SSmlKv *kvVal, bool *isValid, SSmlMsgBuf *msg) {
wmmhello's avatar
wmmhello 已提交
605 606
  const char *pVal = kvVal->value;
  int32_t len = kvVal->valueLen;
wmmhello's avatar
wmmhello 已提交
607 608 609 610 611 612
  if (len <= 3) {
    return false;
  }
  if (pVal[0] == '-') {
    return false;
  }
wmmhello's avatar
wmmhello 已提交
613
  const char *signalPos = pVal + len - 3;
wmmhello's avatar
wmmhello 已提交
614
  if (strncasecmp(signalPos, "u16", 3) == 0) {
wmmhello's avatar
wmmhello 已提交
615 616 617 618
    char *endptr = NULL;
    int64_t result = strtoll(pVal, &endptr, 10);
    if(endptr != signalPos){       // 78ri8
      *isValid = false;
wmmhello's avatar
wmmhello 已提交
619
      smlBuildInvalidDataMsg(msg, "invalid unsigned small int", endptr);
wmmhello's avatar
wmmhello 已提交
620 621
    }else if(!IS_VALID_USMALLINT(result)){
      *isValid = false;
wmmhello's avatar
wmmhello 已提交
622
      smlBuildInvalidDataMsg(msg, "unsigned small int out of rang[0,65535]", endptr);
wmmhello's avatar
wmmhello 已提交
623 624 625 626
    }else{
      kvVal->i = result;
      *isValid = true;
    }
wmmhello's avatar
wmmhello 已提交
627 628 629 630 631
    return true;
  }
  return false;
}

wmmhello's avatar
wmmhello 已提交
632
static bool smlParseInt(SSmlKv *kvVal, bool *isValid, SSmlMsgBuf *msg) {
wmmhello's avatar
wmmhello 已提交
633 634
  const char *pVal = kvVal->value;
  int32_t len = kvVal->valueLen;
wmmhello's avatar
wmmhello 已提交
635 636 637
  if (len <= 3) {
    return false;
  }
wmmhello's avatar
wmmhello 已提交
638
  const char *signalPos = pVal + len - 3;
wmmhello's avatar
wmmhello 已提交
639
  if (strncasecmp(signalPos, "i32", 3) == 0) {
wmmhello's avatar
wmmhello 已提交
640 641 642 643
    char *endptr = NULL;
    int64_t result = strtoll(pVal, &endptr, 10);
    if(endptr != signalPos){       // 78ri8
      *isValid = false;
wmmhello's avatar
wmmhello 已提交
644
      smlBuildInvalidDataMsg(msg, "invalid int", endptr);
wmmhello's avatar
wmmhello 已提交
645 646
    }else if(!IS_VALID_INT(result)){
      *isValid = false;
wmmhello's avatar
wmmhello 已提交
647
      smlBuildInvalidDataMsg(msg, "int out of range[-2147483648,2147483647]", endptr);
wmmhello's avatar
wmmhello 已提交
648 649 650 651
    }else{
      kvVal->i = result;
      *isValid = true;
    }
wmmhello's avatar
wmmhello 已提交
652 653 654 655 656
    return true;
  }
  return false;
}

wmmhello's avatar
wmmhello 已提交
657
static bool smlParseUint(SSmlKv *kvVal, bool *isValid, SSmlMsgBuf *msg) {
wmmhello's avatar
wmmhello 已提交
658 659
  const char *pVal = kvVal->value;
  int32_t len = kvVal->valueLen;
wmmhello's avatar
wmmhello 已提交
660 661 662 663 664 665
  if (len <= 3) {
    return false;
  }
  if (pVal[0] == '-') {
    return false;
  }
wmmhello's avatar
wmmhello 已提交
666
  const char *signalPos = pVal + len - 3;
wmmhello's avatar
wmmhello 已提交
667
  if (strncasecmp(signalPos, "u32", 3) == 0) {
wmmhello's avatar
wmmhello 已提交
668 669 670 671
    char *endptr = NULL;
    int64_t result = strtoll(pVal, &endptr, 10);
    if(endptr != signalPos){       // 78ri8
      *isValid = false;
wmmhello's avatar
wmmhello 已提交
672
      smlBuildInvalidDataMsg(msg, "invalid unsigned int", endptr);
wmmhello's avatar
wmmhello 已提交
673 674
    }else if(!IS_VALID_UINT(result)){
      *isValid = false;
wmmhello's avatar
wmmhello 已提交
675
      smlBuildInvalidDataMsg(msg, "unsigned int out of range[0,4294967295]", endptr);
wmmhello's avatar
wmmhello 已提交
676 677 678 679
    }else{
      kvVal->i = result;
      *isValid = true;
    }
wmmhello's avatar
wmmhello 已提交
680 681 682 683 684
    return true;
  }
  return false;
}

wmmhello's avatar
wmmhello 已提交
685
static bool smlParseBigInt(SSmlKv *kvVal, bool *isValid, SSmlMsgBuf *msg) {
wmmhello's avatar
wmmhello 已提交
686 687
  const char *pVal = kvVal->value;
  int32_t len = kvVal->valueLen;
wmmhello's avatar
wmmhello 已提交
688
  if (len > 3 && strncasecmp(pVal + len - 3, "i64", 3) == 0) {
wmmhello's avatar
wmmhello 已提交
689
    char *endptr = NULL;
wmmhello's avatar
wmmhello 已提交
690
    errno = 0;
wmmhello's avatar
wmmhello 已提交
691 692 693
    int64_t result = strtoll(pVal, &endptr, 10);
    if(endptr != pVal + len - 3){       // 78ri8
      *isValid = false;
wmmhello's avatar
wmmhello 已提交
694
      smlBuildInvalidDataMsg(msg, "invalid big int", endptr);
wmmhello's avatar
wmmhello 已提交
695
    }else if(errno == ERANGE || !IS_VALID_BIGINT(result)){
wmmhello's avatar
wmmhello 已提交
696
      *isValid = false;
wmmhello's avatar
wmmhello 已提交
697
      smlBuildInvalidDataMsg(msg, "big int out of range[-9223372036854775808,9223372036854775807]", endptr);
wmmhello's avatar
wmmhello 已提交
698 699 700 701 702 703 704
    }else{
      kvVal->i = result;
      *isValid = true;
    }
    return true;
  }else if (len > 1 && pVal[len - 1] == 'i') {
    char *endptr = NULL;
wmmhello's avatar
wmmhello 已提交
705
    errno = 0;
wmmhello's avatar
wmmhello 已提交
706 707 708
    int64_t result = strtoll(pVal, &endptr, 10);
    if(endptr != pVal + len - 1){       // 78ri8
      *isValid = false;
wmmhello's avatar
wmmhello 已提交
709
      smlBuildInvalidDataMsg(msg, "invalid big int", endptr);
wmmhello's avatar
wmmhello 已提交
710
    }else if(errno == ERANGE || !IS_VALID_BIGINT(result)){
wmmhello's avatar
wmmhello 已提交
711
      *isValid = false;
wmmhello's avatar
wmmhello 已提交
712
      smlBuildInvalidDataMsg(msg, "big int out of range[-9223372036854775808,9223372036854775807]", endptr);
wmmhello's avatar
wmmhello 已提交
713 714 715 716
    }else{
      kvVal->i = result;
      *isValid = true;
    }
wmmhello's avatar
wmmhello 已提交
717 718 719 720 721
    return true;
  }
  return false;
}

wmmhello's avatar
wmmhello 已提交
722
static bool smlParseBigUint(SSmlKv *kvVal, bool *isValid, SSmlMsgBuf *msg) {
wmmhello's avatar
wmmhello 已提交
723 724
  const char *pVal = kvVal->value;
  int32_t len = kvVal->valueLen;
wmmhello's avatar
wmmhello 已提交
725 726 727 728 729 730
  if (len <= 3) {
    return false;
  }
  if (pVal[0] == '-') {
    return false;
  }
wmmhello's avatar
wmmhello 已提交
731
  const char *signalPos = pVal + len - 3;
wmmhello's avatar
wmmhello 已提交
732
  if (strncasecmp(signalPos, "u64", 3) == 0) {
wmmhello's avatar
wmmhello 已提交
733
    char *endptr = NULL;
wmmhello's avatar
wmmhello 已提交
734
    errno = 0;
wmmhello's avatar
wmmhello 已提交
735 736 737
    uint64_t result = strtoull(pVal, &endptr, 10);
    if(endptr != signalPos){       // 78ri8
      *isValid = false;
wmmhello's avatar
wmmhello 已提交
738
      smlBuildInvalidDataMsg(msg, "invalid unsigned big int", endptr);
wmmhello's avatar
wmmhello 已提交
739
    }else if(errno == ERANGE || !IS_VALID_UBIGINT(result)){
wmmhello's avatar
wmmhello 已提交
740
      *isValid = false;
wmmhello's avatar
wmmhello 已提交
741
      smlBuildInvalidDataMsg(msg, "unsigned big int out of range[0,18446744073709551615]", endptr);
wmmhello's avatar
wmmhello 已提交
742 743 744 745
    }else{
      kvVal->u = result;
      *isValid = true;
    }
wmmhello's avatar
wmmhello 已提交
746 747 748 749 750
    return true;
  }
  return false;
}

wmmhello's avatar
wmmhello 已提交
751
static bool smlParseFloat(SSmlKv *kvVal, bool *isValid, SSmlMsgBuf *msg) {
wmmhello's avatar
wmmhello 已提交
752 753 754
  const char *pVal = kvVal->value;
  int32_t len = kvVal->valueLen;
  char *endptr = NULL;
wmmhello's avatar
wmmhello 已提交
755
  errno = 0;
wmmhello's avatar
wmmhello 已提交
756
  float result = strtof(pVal, &endptr);
wmmhello's avatar
wmmhello 已提交
757
  if(endptr == pVal + len && errno != ERANGE && IS_VALID_FLOAT(result)){       // 78
wmmhello's avatar
wmmhello 已提交
758 759 760
    kvVal->f = result;
    *isValid = true;
    return true;
wmmhello's avatar
wmmhello 已提交
761
  }
wmmhello's avatar
wmmhello 已提交
762

wmmhello's avatar
wmmhello 已提交
763
  if (len > 3 && strncasecmp(pVal + len - 3, "f32", 3) == 0) {
wmmhello's avatar
wmmhello 已提交
764 765
    if(endptr != pVal + len - 3){       // 78ri8
      *isValid = false;
wmmhello's avatar
wmmhello 已提交
766
      smlBuildInvalidDataMsg(msg, "invalid float", endptr);
wmmhello's avatar
wmmhello 已提交
767
    }else if(errno == ERANGE || !IS_VALID_FLOAT(result)){
wmmhello's avatar
wmmhello 已提交
768
      *isValid = false;
wmmhello's avatar
wmmhello 已提交
769
      smlBuildInvalidDataMsg(msg, "float out of range[-3.402823466e+38,3.402823466e+38]", endptr);
wmmhello's avatar
wmmhello 已提交
770 771 772 773
    }else{
      kvVal->f = result;
      *isValid = true;
    }
wmmhello's avatar
wmmhello 已提交
774 775 776 777 778
    return true;
  }
  return false;
}

wmmhello's avatar
wmmhello 已提交
779
static bool smlParseDouble(SSmlKv *kvVal, bool *isValid, SSmlMsgBuf *msg) {
wmmhello's avatar
wmmhello 已提交
780 781
  const char *pVal = kvVal->value;
  int32_t len = kvVal->valueLen;
wmmhello's avatar
wmmhello 已提交
782 783 784
  if (len <= 3) {
    return false;
  }
wmmhello's avatar
wmmhello 已提交
785
  const char *signalPos = pVal + len - 3;
wmmhello's avatar
wmmhello 已提交
786
  if (strncasecmp(signalPos, "f64", 3) == 0) {
wmmhello's avatar
wmmhello 已提交
787
    char *endptr = NULL;
wmmhello's avatar
wmmhello 已提交
788
    errno = 0;
wmmhello's avatar
wmmhello 已提交
789 790 791
    double result = strtod(pVal, &endptr);
    if(endptr != signalPos){       // 78ri8
      *isValid = false;
wmmhello's avatar
wmmhello 已提交
792
      smlBuildInvalidDataMsg(msg, "invalid double", endptr);
wmmhello's avatar
wmmhello 已提交
793
    }else if(errno == ERANGE || !IS_VALID_DOUBLE(result)){
wmmhello's avatar
wmmhello 已提交
794
      *isValid = false;
wmmhello's avatar
wmmhello 已提交
795
      smlBuildInvalidDataMsg(msg, "double out of range[-1.7976931348623158e+308,1.7976931348623158e+308]", endptr);
wmmhello's avatar
wmmhello 已提交
796 797 798 799
    }else{
      kvVal->d = result;
      *isValid = true;
    }
wmmhello's avatar
wmmhello 已提交
800 801 802 803 804
    return true;
  }
  return false;
}

wmmhello's avatar
wmmhello 已提交
805
static bool smlParseBool(SSmlKv *kvVal) {
wmmhello's avatar
wmmhello 已提交
806 807 808 809
  const char *pVal = kvVal->value;
  int32_t len = kvVal->valueLen;
  if ((len == 1) && pVal[len - 1] == 't') {
    kvVal->i = true;
wmmhello's avatar
wmmhello 已提交
810 811 812
    return true;
  }

wmmhello's avatar
wmmhello 已提交
813 814
  if ((len == 1) && pVal[len - 1] == 'f') {
    kvVal->i = false;
wmmhello's avatar
wmmhello 已提交
815 816 817
    return true;
  }

wmmhello's avatar
wmmhello 已提交
818
  if((len == 4) && !strncasecmp(pVal, "true", len)) {
wmmhello's avatar
wmmhello 已提交
819
    kvVal->i = true;
wmmhello's avatar
wmmhello 已提交
820 821
    return true;
  }
wmmhello's avatar
wmmhello 已提交
822
  if((len == 5) && !strncasecmp(pVal, "false", len)) {
wmmhello's avatar
wmmhello 已提交
823
    kvVal->i = false;
wmmhello's avatar
wmmhello 已提交
824 825 826 827 828
    return true;
  }
  return false;
}

wmmhello's avatar
wmmhello 已提交
829
static bool smlIsBinary(const char *pVal, uint16_t len) {
wmmhello's avatar
wmmhello 已提交
830 831 832 833 834 835 836 837 838 839
  //binary: "abc"
  if (len < 2) {
    return false;
  }
  if (pVal[0] == '"' && pVal[len - 1] == '"') {
    return true;
  }
  return false;
}

wmmhello's avatar
wmmhello 已提交
840
static bool smlIsNchar(const char *pVal, uint16_t len) {
wmmhello's avatar
wmmhello 已提交
841 842 843 844 845 846 847 848 849 850
  //nchar: L"abc"
  if (len < 3) {
    return false;
  }
  if ((pVal[0] == 'l' || pVal[0] == 'L')&& pVal[1] == '"' && pVal[len - 1] == '"') {
    return true;
  }
  return false;
}

wmmhello's avatar
wmmhello 已提交
851
static bool smlParseValue(SSmlKv *pVal, SSmlMsgBuf *msg) {
wmmhello's avatar
wmmhello 已提交
852 853
  // put high probability matching type first
  bool isValid = false;
wmmhello's avatar
wmmhello 已提交
854

wmmhello's avatar
wmmhello 已提交
855
  //binary
wmmhello's avatar
wmmhello 已提交
856
  if (smlIsBinary(pVal->value, pVal->valueLen)) {
wmmhello's avatar
wmmhello 已提交
857 858
    pVal->type = TSDB_DATA_TYPE_BINARY;
    pVal->valueLen -= 2;
wmmhello's avatar
wmmhello 已提交
859 860
    pVal->length = pVal->valueLen;
    pVal->value++;
wmmhello's avatar
wmmhello 已提交
861
    return true;
wmmhello's avatar
wmmhello 已提交
862
  }
wmmhello's avatar
wmmhello 已提交
863
  //nchar
wmmhello's avatar
wmmhello 已提交
864
  if (smlIsNchar(pVal->value, pVal->valueLen)) {
wmmhello's avatar
wmmhello 已提交
865
    pVal->type = TSDB_DATA_TYPE_NCHAR;
wmmhello's avatar
wmmhello 已提交
866 867 868 869 870 871 872 873 874 875
    pVal->valueLen -= 3;
    pVal->length = pVal->valueLen;
    pVal->value += 2;
    return true;
  }
  //float
  if (smlParseFloat(pVal, &isValid, msg)) {
    if(!isValid) return false;
    pVal->type = TSDB_DATA_TYPE_FLOAT;
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
wmmhello's avatar
wmmhello 已提交
876 877
    return true;
  }
wmmhello's avatar
wmmhello 已提交
878
  //double
wmmhello's avatar
wmmhello 已提交
879
  if (smlParseDouble(pVal, &isValid, msg)) {
wmmhello's avatar
wmmhello 已提交
880 881 882
    if(!isValid) return false;
    pVal->type = TSDB_DATA_TYPE_DOUBLE;
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
wmmhello's avatar
wmmhello 已提交
883

wmmhello's avatar
wmmhello 已提交
884 885 886
    return true;
  }
  //bool
wmmhello's avatar
wmmhello 已提交
887
  if (smlParseBool(pVal)) {
wmmhello's avatar
wmmhello 已提交
888
    pVal->type = TSDB_DATA_TYPE_BOOL;
wmmhello's avatar
wmmhello 已提交
889 890 891
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
    return true;
  }
wmmhello's avatar
wmmhello 已提交
892

wmmhello's avatar
wmmhello 已提交
893
  if (smlParseTinyInt(pVal, &isValid, msg)) {
wmmhello's avatar
wmmhello 已提交
894
    if(!isValid) return false;
wmmhello's avatar
wmmhello 已提交
895 896 897 898
    pVal->type = TSDB_DATA_TYPE_TINYINT;
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
    return true;
  }
wmmhello's avatar
wmmhello 已提交
899
  if (smlParseTinyUint(pVal, &isValid, msg)) {
wmmhello's avatar
wmmhello 已提交
900
    if(!isValid) return false;
wmmhello's avatar
wmmhello 已提交
901 902 903 904
    pVal->type = TSDB_DATA_TYPE_UTINYINT;
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
    return true;
  }
wmmhello's avatar
wmmhello 已提交
905
  if (smlParseSmallInt(pVal, &isValid, msg)) {
wmmhello's avatar
wmmhello 已提交
906
    if(!isValid) return false;
wmmhello's avatar
wmmhello 已提交
907 908 909 910
    pVal->type = TSDB_DATA_TYPE_SMALLINT;
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
    return true;
  }
wmmhello's avatar
wmmhello 已提交
911
  if (smlParseSmallUint(pVal, &isValid, msg)) {
wmmhello's avatar
wmmhello 已提交
912
    if(!isValid) return false;
wmmhello's avatar
wmmhello 已提交
913 914 915 916
    pVal->type = TSDB_DATA_TYPE_USMALLINT;
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
    return true;
  }
wmmhello's avatar
wmmhello 已提交
917
  if (smlParseInt(pVal, &isValid, msg)) {
wmmhello's avatar
wmmhello 已提交
918
    if(!isValid) return false;
wmmhello's avatar
wmmhello 已提交
919 920 921 922
    pVal->type = TSDB_DATA_TYPE_INT;
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
    return true;
  }
wmmhello's avatar
wmmhello 已提交
923
  if (smlParseUint(pVal, &isValid, msg)) {
wmmhello's avatar
wmmhello 已提交
924
    if(!isValid) return false;
wmmhello's avatar
wmmhello 已提交
925 926 927 928
    pVal->type = TSDB_DATA_TYPE_UINT;
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
    return true;
  }
wmmhello's avatar
wmmhello 已提交
929
  if (smlParseBigInt(pVal, &isValid, msg)) {
wmmhello's avatar
wmmhello 已提交
930
    if(!isValid) return false;
wmmhello's avatar
wmmhello 已提交
931 932 933 934
    pVal->type = TSDB_DATA_TYPE_BIGINT;
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
    return true;
  }
wmmhello's avatar
wmmhello 已提交
935
  if (smlParseBigUint(pVal, &isValid, msg)) {
wmmhello's avatar
wmmhello 已提交
936
    if(!isValid) return false;
wmmhello's avatar
wmmhello 已提交
937 938 939 940 941
    pVal->type = TSDB_DATA_TYPE_UBIGINT;
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
    return true;
  }

wmmhello's avatar
wmmhello 已提交
942
  smlBuildInvalidDataMsg(msg, "invalid data", pVal->value);
wmmhello's avatar
wmmhello 已提交
943 944 945
  return false;
}

wmmhello's avatar
wmmhello 已提交
946
static bool checkDuplicateKey(char *key, SHashObj *pHash, SSmlHandle* info) {
wmmhello's avatar
wmmhello 已提交
947 948 949 950 951
  char *val = NULL;
  val = taosHashGet(pHash, key, strlen(key));
  if (val) {
    uError("SML:0x%"PRIx64" Duplicate key detected:%s", info->id, key);
    return true;
wmmhello's avatar
wmmhello 已提交
952 953
  }

wmmhello's avatar
wmmhello 已提交
954 955
  uint8_t dummy_val = 0;
  taosHashPut(pHash, key, strlen(key), &dummy_val, sizeof(uint8_t));
wmmhello's avatar
wmmhello 已提交
956

wmmhello's avatar
wmmhello 已提交
957
  return false;
wmmhello's avatar
wmmhello 已提交
958 959
}

wmmhello's avatar
wmmhello 已提交
960
static int32_t smlParseString(const char* sql, SSmlLineInfo *elements, SSmlMsgBuf *msg){
wmmhello's avatar
wmmhello 已提交
961 962 963 964 965 966 967 968
  if(!sql) return TSDB_CODE_SML_INVALID_DATA;
  while (*sql != '\0') {           // jump the space at the begining
    if(*sql != SPACE) {
      elements->measure = sql;
      break;
    }
    sql++;
  }
wmmhello's avatar
wmmhello 已提交
969 970 971 972
  if (!elements->measure || *sql == COMMA) {
    smlBuildInvalidDataMsg(msg, "invalid data", sql);
    return TSDB_CODE_SML_INVALID_DATA;
  }
wmmhello's avatar
wmmhello 已提交
973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994

  // parse measure and tag
  while (*sql != '\0') {
    if (elements->measureLen == 0 && *sql == COMMA && *(sql - 1) != SLASH) {  // find the first comma
      elements->measureLen = sql - elements->measure;
      sql++;
      elements->tags = sql;
      continue;
    }

    if (*sql == SPACE && *(sql - 1) != SLASH) {   // find the first space
      if (elements->measureLen == 0) {
        elements->measureLen = sql - elements->measure;
        elements->tags = sql;
      }
      elements->tagsLen = sql - elements->tags;
      elements->measureTagsLen = sql - elements->measure;
      break;
    }

    sql++;
  }
wmmhello's avatar
wmmhello 已提交
995 996 997 998 999 1000 1001
  if(elements->tagsLen == 0){     // measure, cols1=a         measure cols1=a
    elements->measureTagsLen = elements->measureLen;
  }
  if(elements->measureLen == 0) {
    smlBuildInvalidDataMsg(msg, "invalid measure", elements->measure);
    return TSDB_CODE_SML_INVALID_DATA;
  }
wmmhello's avatar
wmmhello 已提交
1002 1003 1004 1005 1006 1007 1008 1009 1010

  // parse cols
  while (*sql != '\0') {
    if(*sql != SPACE) {
      elements->cols = sql;
      break;
    }
    sql++;
  }
wmmhello's avatar
wmmhello 已提交
1011 1012 1013
  if(!elements->cols) {
    smlBuildInvalidDataMsg(msg, "invalid columns", elements->cols);
    return TSDB_CODE_SML_INVALID_DATA;
1014
  }
wmmhello's avatar
wmmhello 已提交
1015

wmmhello's avatar
wmmhello 已提交
1016
  bool isInQuote = false;
wmmhello's avatar
wmmhello 已提交
1017
  while (*sql != '\0') {
wmmhello's avatar
wmmhello 已提交
1018 1019 1020 1021
    if(*sql == QUOTE && *(sql - 1) != SLASH){
      isInQuote = !isInQuote;
    }
    if(!isInQuote && *sql == SPACE && *(sql - 1) != SLASH) {
wmmhello's avatar
wmmhello 已提交
1022 1023 1024 1025
      break;
    }
    sql++;
  }
1026 1027 1028 1029
  if(isInQuote){
    smlBuildInvalidDataMsg(msg, "only one quote", elements->cols);
    return TSDB_CODE_SML_INVALID_DATA;
  }
wmmhello's avatar
wmmhello 已提交
1030
  elements->colsLen = sql - elements->cols;
wmmhello's avatar
wmmhello 已提交
1031

wmmhello's avatar
wmmhello 已提交
1032
  // parse ts,ts can be empty
wmmhello's avatar
wmmhello 已提交
1033
  while (*sql != '\0') {
1034
    if(*sql != SPACE && elements->timestamp == NULL) {
wmmhello's avatar
wmmhello 已提交
1035
      elements->timestamp = sql;
1036 1037
    }
    if(*sql == SPACE && elements->timestamp != NULL){
wmmhello's avatar
wmmhello 已提交
1038 1039 1040 1041
      break;
    }
    sql++;
  }
wmmhello's avatar
wmmhello 已提交
1042 1043 1044
  if(elements->timestamp){
    elements->timestampLen = sql - elements->timestamp;
  }
wmmhello's avatar
wmmhello 已提交
1045 1046 1047 1048

  return TSDB_CODE_SUCCESS;
}

1049
static int32_t smlParseCols(const char* data, int32_t len, SArray *cols, bool isTag, SHashObj *dumplicateKey, SSmlMsgBuf *msg){
wmmhello's avatar
wmmhello 已提交
1050 1051 1052
  if(isTag && len == 0){
    SSmlKv *kv = taosMemoryCalloc(sizeof(SSmlKv), 1);
    kv->key = TAG;
wmmhello's avatar
wmmhello 已提交
1053
    kv->keyLen = strlen(TAG);
wmmhello's avatar
wmmhello 已提交
1054
    kv->value = TAG;
wmmhello's avatar
wmmhello 已提交
1055
    kv->valueLen = strlen(TAG);
wmmhello's avatar
wmmhello 已提交
1056 1057
    kv->type = TSDB_DATA_TYPE_NCHAR;
    if(cols) taosArrayPush(cols, &kv);
wmmhello's avatar
wmmhello 已提交
1058
    return TSDB_CODE_SUCCESS;
wmmhello's avatar
wmmhello 已提交
1059 1060
  }

wmmhello's avatar
wmmhello 已提交
1061
  for(int i = 0; i < len; i++){
wmmhello's avatar
wmmhello 已提交
1062
    // parse key
wmmhello's avatar
wmmhello 已提交
1063 1064 1065 1066 1067 1068 1069 1070 1071
    const char *key = data + i;
    int32_t keyLen = 0;
    while(i < len){
      if(data[i] == EQUAL && i > 0 && data[i-1] != SLASH){
        keyLen = data + i - key;
        break;
      }
      i++;
    }
wmmhello's avatar
wmmhello 已提交
1072
    if(keyLen == 0 || keyLen >= TSDB_COL_NAME_LEN){
wmmhello's avatar
wmmhello 已提交
1073
      smlBuildInvalidDataMsg(msg, "invalid key or key is too long than 64", key);
wmmhello's avatar
wmmhello 已提交
1074 1075 1076
      return TSDB_CODE_SML_INVALID_DATA;
    }

1077 1078 1079 1080 1081 1082 1083
    if(taosHashGet(dumplicateKey, key, keyLen)){
      smlBuildInvalidDataMsg(msg, "dumplicate key", key);
      return TSDB_CODE_SML_INVALID_DATA;
    }else{
      taosHashPut(dumplicateKey, key, keyLen, key, CHAR_BYTES);
    }

wmmhello's avatar
wmmhello 已提交
1084
    // parse value
wmmhello's avatar
wmmhello 已提交
1085 1086
    i++;
    const char *value = data + i;
wmmhello's avatar
wmmhello 已提交
1087
    bool isInQuote = false;
wmmhello's avatar
wmmhello 已提交
1088
    while(i < len){
wmmhello's avatar
wmmhello 已提交
1089 1090 1091 1092
      if(data[i] == QUOTE && data[i-1] != SLASH){
        isInQuote = !isInQuote;
      }
      if(!isInQuote && data[i] == COMMA && i > 0 && data[i-1] != SLASH){
wmmhello's avatar
wmmhello 已提交
1093 1094 1095 1096
        break;
      }
      i++;
    }
wmmhello's avatar
wmmhello 已提交
1097 1098 1099 1100
    if(isInQuote){
      smlBuildInvalidDataMsg(msg, "only one quote", value);
      return TSDB_CODE_SML_INVALID_DATA;
    }
wmmhello's avatar
wmmhello 已提交
1101
    int32_t valueLen = data + i - value;
wmmhello's avatar
wmmhello 已提交
1102
    if(valueLen == 0){
wmmhello's avatar
wmmhello 已提交
1103
      smlBuildInvalidDataMsg(msg, "invalid value", value);
wmmhello's avatar
wmmhello 已提交
1104 1105
      return TSDB_CODE_SML_INVALID_DATA;
    }
wmmhello's avatar
wmmhello 已提交
1106 1107

    // add kv to SSmlKv
wmmhello's avatar
wmmhello 已提交
1108
    SSmlKv *kv = taosMemoryCalloc(sizeof(SSmlKv), 1);
wmmhello's avatar
wmmhello 已提交
1109 1110 1111 1112
    kv->key = key;
    kv->keyLen = keyLen;
    kv->value = value;
    kv->valueLen = valueLen;
wmmhello's avatar
wmmhello 已提交
1113 1114
    if(isTag){
      kv->type = TSDB_DATA_TYPE_NCHAR;
wmmhello's avatar
wmmhello 已提交
1115
    }else{
wmmhello's avatar
wmmhello 已提交
1116
      if(!smlParseValue(kv, msg)){
wmmhello's avatar
wmmhello 已提交
1117 1118
        return TSDB_CODE_SML_INVALID_DATA;
      }
wmmhello's avatar
wmmhello 已提交
1119
    }
wmmhello's avatar
wmmhello 已提交
1120

wmmhello's avatar
wmmhello 已提交
1121 1122
    if(cols) taosArrayPush(cols, &kv);
  }
wmmhello's avatar
wmmhello 已提交
1123

wmmhello's avatar
wmmhello 已提交
1124 1125 1126
  return TSDB_CODE_SUCCESS;
}

wmmhello's avatar
wmmhello 已提交
1127 1128 1129 1130 1131 1132
static int64_t smlGetTimeValue(const char *value, int32_t len, int8_t type) {
  char *endPtr = NULL;
  double ts = (double)strtoll(value, &endPtr, 10);
  if(value + len != endPtr){
    return -1;
  }
wmmhello's avatar
wmmhello 已提交
1133 1134 1135
  switch (type) {
    case TSDB_TIME_PRECISION_HOURS:
      ts *= (3600 * 1e9);
wmmhello's avatar
wmmhello 已提交
1136
      break;
wmmhello's avatar
wmmhello 已提交
1137 1138
    case TSDB_TIME_PRECISION_MINUTES:
      ts *= (60 * 1e9);
wmmhello's avatar
wmmhello 已提交
1139
      break;
wmmhello's avatar
wmmhello 已提交
1140 1141
    case TSDB_TIME_PRECISION_SECONDS:
      ts *= (1e9);
wmmhello's avatar
wmmhello 已提交
1142
      break;
wmmhello's avatar
wmmhello 已提交
1143
    case TSDB_TIME_PRECISION_MILLI:
wmmhello's avatar
wmmhello 已提交
1144
      ts *= (1e6);
wmmhello's avatar
wmmhello 已提交
1145
      break;
wmmhello's avatar
wmmhello 已提交
1146
    case TSDB_TIME_PRECISION_MICRO:
wmmhello's avatar
wmmhello 已提交
1147 1148
      ts *= (1e3);
      break;
wmmhello's avatar
wmmhello 已提交
1149 1150
    case TSDB_TIME_PRECISION_NANO:
      break;
wmmhello's avatar
wmmhello 已提交
1151 1152
    default:
      ASSERT(0);
wmmhello's avatar
wmmhello 已提交
1153 1154 1155 1156
  }
  if(ts > (double)INT64_MAX || ts < 0){
    return -1;
  }
wmmhello's avatar
wmmhello 已提交
1157 1158

  return (int64_t)ts;
wmmhello's avatar
wmmhello 已提交
1159 1160
}

wmmhello's avatar
wmmhello 已提交
1161
static int64_t smlGetTimeNow(int8_t precision) {
wmmhello's avatar
wmmhello 已提交
1162 1163 1164 1165 1166 1167 1168
  switch (precision) {
    case TSDB_TIME_PRECISION_HOURS:
      return taosGetTimestampMs()/1000/3600;
    case TSDB_TIME_PRECISION_MINUTES:
      return taosGetTimestampMs()/1000/60;
    case TSDB_TIME_PRECISION_SECONDS:
      return taosGetTimestampMs()/1000;
wmmhello's avatar
wmmhello 已提交
1169 1170 1171
    case TSDB_TIME_PRECISION_MILLI:
    case TSDB_TIME_PRECISION_MICRO:
    case TSDB_TIME_PRECISION_NANO:
wmmhello's avatar
wmmhello 已提交
1172
      return taosGetTimestamp(precision);
wmmhello's avatar
wmmhello 已提交
1173 1174
    default:
      ASSERT(0);
wmmhello's avatar
wmmhello 已提交
1175 1176 1177
  }
}

wmmhello's avatar
wmmhello 已提交
1178
static int8_t smlGetTsTypeByLen(int32_t len) {
wmmhello's avatar
wmmhello 已提交
1179 1180 1181 1182 1183
  if (len == TSDB_TIME_PRECISION_SEC_DIGITS) {
    return TSDB_TIME_PRECISION_SECONDS;
  } else if (len == TSDB_TIME_PRECISION_MILLI_DIGITS) {
    return TSDB_TIME_PRECISION_MILLI_DIGITS;
  } else {
wmmhello's avatar
wmmhello 已提交
1184
    return -1;
wmmhello's avatar
wmmhello 已提交
1185 1186 1187
  }
}

wmmhello's avatar
wmmhello 已提交
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 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 1241 1242 1243 1244
static int8_t smlGetTsTypeByPrecision(int8_t precision) {
  switch (precision) {
    case TSDB_SML_TIMESTAMP_HOURS:
      return TSDB_TIME_PRECISION_HOURS;
    case TSDB_SML_TIMESTAMP_MILLI_SECONDS:
      return TSDB_TIME_PRECISION_MILLI;
    case TSDB_SML_TIMESTAMP_NANO_SECONDS:
    case TSDB_SML_TIMESTAMP_NOT_CONFIGURED:
      return TSDB_TIME_PRECISION_NANO;
    case TSDB_SML_TIMESTAMP_MICRO_SECONDS:
      return TSDB_TIME_PRECISION_MICRO;
    case TSDB_SML_TIMESTAMP_SECONDS:
      return TSDB_TIME_PRECISION_SECONDS;
    case TSDB_SML_TIMESTAMP_MINUTES:
      return TSDB_TIME_PRECISION_MINUTES;
    default:
      return -1;
  }
}

static int64_t smlParseInfluxTime(SSmlHandle* info, const char* data, int32_t len){
  int8_t tsType = smlGetTsTypeByPrecision(info->precision);
  if (tsType == -1) {
    smlBuildInvalidDataMsg(&info->msgBuf, "invalid timestamp precision", NULL);
    return -1;
  }
  if(!data){
    return smlGetTimeNow(tsType);
  }

  int64_t ts = smlGetTimeValue(data, len, tsType);
  if(ts == -1){
    smlBuildInvalidDataMsg(&info->msgBuf, "invalid timestamp", data);
    return -1;
  }
  return ts;
}

static int64_t smlParseOpenTsdbTime(SSmlHandle* info, const char* data, int32_t len){
  if(!data){
    smlBuildInvalidDataMsg(&info->msgBuf, "timestamp can not be null", NULL);
    return -1;
  }
  int8_t tsType = smlGetTsTypeByLen(len);
  if (tsType == -1) {
    smlBuildInvalidDataMsg(&info->msgBuf, "timestamp precision can only be seconds(10 digits) or milli seconds(13 digits)", data);
    return -1;
  }
  int64_t ts = smlGetTimeValue(data, len, tsType);
  if(ts == -1){
    smlBuildInvalidDataMsg(&info->msgBuf, "invalid timestamp", data);
    return -1;
  }
  return ts;
}

static int32_t smlParseTS(SSmlHandle* info, const char* data, int32_t len, SArray *cols){
wmmhello's avatar
wmmhello 已提交
1245
  int64_t ts = 0;
wmmhello's avatar
wmmhello 已提交
1246 1247
  if(info->protocol == TSDB_SML_LINE_PROTOCOL){
    ts = smlParseInfluxTime(info, data, len);
wmmhello's avatar
wmmhello 已提交
1248
  }else{
wmmhello's avatar
wmmhello 已提交
1249
    ts = smlParseOpenTsdbTime(info, data, len);
wmmhello's avatar
wmmhello 已提交
1250
  }
wmmhello's avatar
wmmhello 已提交
1251
  if(ts == -1)  return TSDB_CODE_TSC_INVALID_TIME_STAMP;
wmmhello's avatar
wmmhello 已提交
1252

wmmhello's avatar
wmmhello 已提交
1253
  // add ts to
wmmhello's avatar
wmmhello 已提交
1254
  SSmlKv *kv = taosMemoryCalloc(sizeof(SSmlKv), 1);
wmmhello's avatar
wmmhello 已提交
1255 1256 1257 1258 1259
  if(!kv){
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  kv->key = TS;
wmmhello's avatar
wmmhello 已提交
1260
  kv->keyLen = strlen(kv->key);
wmmhello's avatar
wmmhello 已提交
1261
  kv->i = ts;
wmmhello's avatar
wmmhello 已提交
1262
  kv->type = TSDB_DATA_TYPE_TIMESTAMP;
wmmhello's avatar
wmmhello 已提交
1263
  kv->length = (int16_t)tDataTypes[kv->type].bytes;
wmmhello's avatar
wmmhello 已提交
1264
  if(cols) taosArrayPush(cols, &kv);
wmmhello's avatar
wmmhello 已提交
1265 1266 1267
  return TSDB_CODE_SUCCESS;
}

wmmhello's avatar
wmmhello 已提交
1268
//static int32_t parseSmlCols(const char* data, SArray *cols){
wmmhello's avatar
wmmhello 已提交
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
//  while(*data != '\0'){
//    if(*data == EQUAL) return TSDB_CODE_SML_INVALID_DATA;
//    const char *key = data;
//    int32_t keyLen = 0;
//    while(*data != '\0'){
//      if(*data == EQUAL && *(data-1) != SLASH){
//        keyLen = data - key;
//        data ++;
//        break;
//      }
//      data++;
//    }
//    if(keyLen == 0){
//      return TSDB_CODE_SML_INVALID_DATA;
//    }
//
//    if(*data == COMMA) return TSDB_CODE_SML_INVALID_DATA;
//    const char *value = data;
//    int32_t valueLen = 0;
//    while(*data != '\0'){
//      if(*data == COMMA && *(data-1) != SLASH){
//        valueLen = data - value;
//        data ++;
//        break;
//      }
//      data++;
//    }
//    if(valueLen == 0){
//      return TSDB_CODE_SML_INVALID_DATA;
//    }
//
//    TAOS_SML_KV *kv = taosMemoryCalloc(sizeof(TAOS_SML_KV), 1);
//    kv->key = key;
//    kv->keyLen = keyLen;
//    kv->value = value;
//    kv->valueLen = valueLen;
//    kv->type = TSDB_DATA_TYPE_NCHAR;
//    if(cols) taosArrayPush(cols, &kv);
//  }
//  return TSDB_CODE_SUCCESS;
//}

wmmhello's avatar
wmmhello 已提交
1311
static bool smlUpdateMeta(SSmlSTableMeta* tableMeta, SArray *tags, SArray *cols, SSmlMsgBuf *msg){
wmmhello's avatar
wmmhello 已提交
1312 1313
  if(tags){
    for (int i = 0; i < taosArrayGetSize(tags); ++i) {
wmmhello's avatar
wmmhello 已提交
1314
      SSmlKv *kv = taosArrayGetP(tags, i);
wmmhello's avatar
wmmhello 已提交
1315 1316
      ASSERT(kv->type == TSDB_DATA_TYPE_NCHAR);

1317 1318 1319
      uint8_t *index = taosHashGet(tableMeta->tagHash, kv->key, kv->keyLen);
      if(index){
        SSmlKv **value = taosArrayGet(tableMeta->tags, *index);
wmmhello's avatar
wmmhello 已提交
1320 1321 1322
        ASSERT((*value)->type == TSDB_DATA_TYPE_NCHAR);
        if(kv->valueLen > (*value)->valueLen){    // tags type is nchar
          *value = kv;
wmmhello's avatar
wmmhello 已提交
1323 1324
        }
      }else{
1325 1326 1327 1328 1329
        size_t tmp = taosArrayGetSize(tableMeta->tags);
        ASSERT(tmp <= UINT8_MAX);
        uint8_t size = tmp;
        taosArrayPush(tableMeta->tags, &kv);
        taosHashPut(tableMeta->tagHash, kv->key, kv->keyLen, &size, CHAR_BYTES);
wmmhello's avatar
wmmhello 已提交
1330 1331 1332 1333 1334 1335
      }
    }
  }

  if(cols){
    for (int i = 1; i < taosArrayGetSize(cols); ++i) {  //jump timestamp
wmmhello's avatar
wmmhello 已提交
1336
      SSmlKv *kv = taosArrayGetP(cols, i);
1337 1338 1339 1340

      int16_t *index = taosHashGet(tableMeta->fieldHash, kv->key, kv->keyLen);
      if(index){
        SSmlKv **value = taosArrayGet(tableMeta->cols, *index);
wmmhello's avatar
wmmhello 已提交
1341
        if(kv->type != (*value)->type){
wmmhello's avatar
wmmhello 已提交
1342
          smlBuildInvalidDataMsg(msg, "the type is not the same like before", kv->key);
wmmhello's avatar
wmmhello 已提交
1343 1344 1345 1346 1347 1348 1349
          return false;
        }else{
          if(IS_VAR_DATA_TYPE(kv->type)){     // update string len, if bigger
            if(kv->valueLen > (*value)->valueLen){
              *value = kv;
            }
          }
wmmhello's avatar
wmmhello 已提交
1350 1351
        }
      }else{
1352 1353 1354 1355 1356
        size_t tmp = taosArrayGetSize(tableMeta->cols);
        ASSERT(tmp <= INT16_MAX);
        int16_t size = tmp;
        taosArrayPush(tableMeta->cols, &kv);
        taosHashPut(tableMeta->fieldHash, kv->key, kv->keyLen, &size, SHORT_BYTES);
wmmhello's avatar
wmmhello 已提交
1357 1358 1359
      }
    }
  }
wmmhello's avatar
wmmhello 已提交
1360
  return true;
wmmhello's avatar
wmmhello 已提交
1361 1362
}

wmmhello's avatar
wmmhello 已提交
1363
static void smlInsertMeta(SSmlSTableMeta* tableMeta, SArray *tags, SArray *cols){
wmmhello's avatar
wmmhello 已提交
1364
  if(tags){
1365
    for (uint8_t i = 0; i < taosArrayGetSize(tags); ++i) {
wmmhello's avatar
wmmhello 已提交
1366
      SSmlKv *kv = taosArrayGetP(tags, i);
1367 1368
      taosArrayPush(tableMeta->tags, &kv);
      taosHashPut(tableMeta->tagHash, kv->key, kv->keyLen, &i, CHAR_BYTES);
wmmhello's avatar
wmmhello 已提交
1369 1370 1371 1372
    }
  }

  if(cols){
1373
    for (int16_t i = 0; i < taosArrayGetSize(cols); ++i) {
wmmhello's avatar
wmmhello 已提交
1374
      SSmlKv *kv = taosArrayGetP(cols, i);
1375 1376
      taosArrayPush(tableMeta->cols, &kv);
      taosHashPut(tableMeta->fieldHash, kv->key, kv->keyLen, &i, SHORT_BYTES);
wmmhello's avatar
wmmhello 已提交
1377 1378 1379 1380
    }
  }
}

wmmhello's avatar
wmmhello 已提交
1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435
static SSmlTableInfo* smlBuildTableInfo(bool format){
  SSmlTableInfo *tag = taosMemoryCalloc(sizeof(SSmlTableInfo), 1);
  if(!tag){
    return NULL;
  }

  if(format){
    tag->colsFormat = taosArrayInit(16, POINTER_BYTES);
    if (tag->colsFormat == NULL) {
      uError("SML:smlParseLine failed to allocate memory");
      goto cleanup;
    }
  }else{
    tag->cols = taosArrayInit(16, POINTER_BYTES);
    if (tag->cols == NULL) {
      uError("SML:smlParseLine failed to allocate memory");
      goto cleanup;
    }
  }

  tag->tags = taosArrayInit(16, POINTER_BYTES);
  if (tag->tags == NULL) {
    uError("SML:smlParseLine failed to allocate memory");
    goto cleanup;
  }
  return tag;

cleanup:
  taosMemoryFreeClear(tag);
  return NULL;
}

static void smlDestroyBuildTableInfo(SSmlTableInfo *tag, bool format){
  if(format){
    taosArrayDestroy(tag->colsFormat);
  }else{
    tag->cols = taosArrayInit(16, POINTER_BYTES);
    for(size_t i = 0; i < taosArrayGetSize(tag->cols); i++){
      SHashObj *kvHash = taosArrayGetP(tag->cols, i);
      void** p1 = taosHashIterate(kvHash, NULL);
      while (p1) {
        SSmlKv* kv = *p1;
        taosMemoryFreeClear(kv);
        p1 = taosHashIterate(kvHash, p1);
      }
      taosHashCleanup(kvHash);
    }
  }
  taosArrayDestroy(tag->tags);
  taosMemoryFreeClear(tag);
}

static int32_t smlDealCols(SSmlTableInfo* oneTable, bool dataFormat, SArray *cols){
  if(dataFormat){
    taosArrayPush(oneTable->colsFormat, &cols);
1436 1437
    return TSDB_CODE_SUCCESS;
  }
wmmhello's avatar
wmmhello 已提交
1438

1439 1440 1441 1442 1443 1444 1445 1446
  SHashObj *kvHash = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
  if(!kvHash){
    uError("SML:smlDealCols failed to allocate memory");
    return TSDB_CODE_TSC_OUT_OF_MEMORY;
  }
  for(size_t i = 0; i < taosArrayGetSize(cols); i++){
    SSmlKv *kv = taosArrayGetP(cols, i);
    taosHashPut(kvHash, kv->key, kv->keyLen, &kv, POINTER_BYTES);   // todo key need escape, like \=, because find by schema name later
wmmhello's avatar
wmmhello 已提交
1447
  }
1448 1449
  taosArrayPush(oneTable->cols, &kvHash);

1450
  return TSDB_CODE_SUCCESS;
wmmhello's avatar
wmmhello 已提交
1451 1452 1453 1454 1455 1456 1457
}

static SSmlSTableMeta* smlBuildSTableMeta(){
  SSmlSTableMeta* meta = taosMemoryCalloc(sizeof(SSmlSTableMeta), 1);
  if(!meta){
    return NULL;
  }
1458
  meta->tagHash = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
wmmhello's avatar
wmmhello 已提交
1459 1460 1461 1462 1463
  if (meta->tagHash == NULL) {
    uError("SML:smlBuildSTableMeta failed to allocate memory");
    goto cleanup;
  }

1464
  meta->fieldHash = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
wmmhello's avatar
wmmhello 已提交
1465 1466 1467 1468
  if (meta->fieldHash == NULL) {
    uError("SML:smlBuildSTableMeta failed to allocate memory");
    goto cleanup;
  }
1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480

  meta->tags = taosArrayInit(32, POINTER_BYTES);
  if (meta->tags == NULL) {
    uError("SML:smlBuildSTableMeta failed to allocate memory");
    goto cleanup;
  }

  meta->cols = taosArrayInit(32, POINTER_BYTES);
  if (meta->cols == NULL) {
    uError("SML:smlBuildSTableMeta failed to allocate memory");
    goto cleanup;
  }
wmmhello's avatar
wmmhello 已提交
1481 1482 1483 1484 1485 1486 1487 1488 1489 1490
  return meta;

cleanup:
  taosMemoryFreeClear(meta);
  return NULL;
}

static void smlDestroySTableMeta(SSmlSTableMeta *meta){
  taosHashCleanup(meta->tagHash);
  taosHashCleanup(meta->fieldHash);
1491 1492
  taosArrayDestroy(meta->tags);
  taosArrayDestroy(meta->cols);
wmmhello's avatar
wmmhello 已提交
1493 1494 1495 1496 1497 1498
  taosMemoryFree(meta->tableMeta);
}

static int32_t smlParseLine(SSmlHandle* info, const char* sql) {
  SSmlLineInfo elements = {0};
  int ret = smlParseString(sql, &elements, &info->msgBuf);
wmmhello's avatar
wmmhello 已提交
1499
  if(ret != TSDB_CODE_SUCCESS){
wmmhello's avatar
wmmhello 已提交
1500
    uError("SML:0x%"PRIx64" smlParseString failed", info->id);
wmmhello's avatar
wmmhello 已提交
1501 1502 1503
    return ret;
  }

1504 1505 1506 1507 1508 1509 1510 1511 1512
  SArray *cols = NULL;
  if(info->dataFormat){   // if dataFormat, cols need new memory to save data
    cols = taosArrayInit(16, POINTER_BYTES);
    if (cols == NULL) {
      uError("SML:0x%"PRIx64" smlParseLine failed to allocate memory", info->id);
      return TSDB_CODE_TSC_OUT_OF_MEMORY;
    }
  }else{      // if dataFormat is false, cols do not need to save data, there is another new memory to save data
    cols = info->colsContainer;
wmmhello's avatar
wmmhello 已提交
1513
  }
wmmhello's avatar
wmmhello 已提交
1514

wmmhello's avatar
wmmhello 已提交
1515
  ret = smlParseTS(info, elements.timestamp, elements.timestampLen, cols);
wmmhello's avatar
wmmhello 已提交
1516
  if(ret != TSDB_CODE_SUCCESS){
wmmhello's avatar
wmmhello 已提交
1517
    uError("SML:0x%"PRIx64" smlParseTS failed", info->id);
wmmhello's avatar
wmmhello 已提交
1518 1519
    return ret;
  }
1520
  ret = smlParseCols(elements.cols, elements.colsLen, cols, false, info->dumplicateKey, &info->msgBuf);
wmmhello's avatar
wmmhello 已提交
1521
  if(ret != TSDB_CODE_SUCCESS){
wmmhello's avatar
wmmhello 已提交
1522
    uError("SML:0x%"PRIx64" smlParseCols parse cloums fields failed", info->id);
wmmhello's avatar
wmmhello 已提交
1523 1524
    return ret;
  }
wmmhello's avatar
wmmhello 已提交
1525
  if(taosArrayGetSize(cols) > TSDB_MAX_COLUMNS){
wmmhello's avatar
wmmhello 已提交
1526
    smlBuildInvalidDataMsg(&info->msgBuf, "too many columns than 4096", NULL);
wmmhello's avatar
wmmhello 已提交
1527 1528
    return TSDB_CODE_SML_INVALID_DATA;
  }
wmmhello's avatar
wmmhello 已提交
1529

wmmhello's avatar
wmmhello 已提交
1530
  SSmlTableInfo **oneTable = taosHashGet(info->childTables, elements.measure, elements.measureTagsLen);
wmmhello's avatar
wmmhello 已提交
1531 1532 1533
  if(oneTable){
    SSmlSTableMeta** tableMeta = taosHashGet(info->superTables, elements.measure, elements.measureLen);
    ASSERT(tableMeta);
wmmhello's avatar
wmmhello 已提交
1534
    ret = smlUpdateMeta(*tableMeta, NULL, cols, &info->msgBuf);    // update meta cols
wmmhello's avatar
wmmhello 已提交
1535
    if(!ret){
wmmhello's avatar
wmmhello 已提交
1536
      uError("SML:0x%"PRIx64" smlUpdateMeta cols failed", info->id);
wmmhello's avatar
wmmhello 已提交
1537 1538
      return TSDB_CODE_SML_INVALID_DATA;
    }
wmmhello's avatar
wmmhello 已提交
1539 1540 1541 1542
    ret = smlDealCols(*oneTable, info->dataFormat, cols);
    if(ret != TSDB_CODE_SUCCESS){
      return ret;
    }
wmmhello's avatar
wmmhello 已提交
1543
  }else{
1544 1545
    SSmlTableInfo *tinfo = smlBuildTableInfo(info->dataFormat);
    if(!tinfo){
wmmhello's avatar
wmmhello 已提交
1546 1547
      return TSDB_CODE_TSC_OUT_OF_MEMORY;
    }
1548
    ret = smlDealCols(tinfo, info->dataFormat, cols);
wmmhello's avatar
wmmhello 已提交
1549 1550
    if(ret != TSDB_CODE_SUCCESS){
      return ret;
wmmhello's avatar
wmmhello 已提交
1551 1552
    }

1553
    ret = smlParseCols(elements.tags, elements.tagsLen, tinfo->tags, true, info->dumplicateKey, &info->msgBuf);
wmmhello's avatar
wmmhello 已提交
1554
    if(ret != TSDB_CODE_SUCCESS){
wmmhello's avatar
wmmhello 已提交
1555
      uError("SML:0x%"PRIx64" smlParseCols parse tag fields failed", info->id);
wmmhello's avatar
wmmhello 已提交
1556 1557 1558
      return ret;
    }

1559
    if(taosArrayGetSize(tinfo->tags) > TSDB_MAX_TAGS){
wmmhello's avatar
wmmhello 已提交
1560
      smlBuildInvalidDataMsg(&info->msgBuf, "too many tags than 128", NULL);
wmmhello's avatar
wmmhello 已提交
1561 1562 1563
      return TSDB_CODE_SML_INVALID_DATA;
    }

1564 1565 1566
    tinfo->sTableName = elements.measure;
    tinfo->sTableNameLen = elements.measureLen;
    smlBuildChildTableName(tinfo);
1567
    //uDebug("SML:0x%"PRIx64" child table name: %s", info->id, tinfo->childTableName);
wmmhello's avatar
wmmhello 已提交
1568

wmmhello's avatar
wmmhello 已提交
1569 1570
    SSmlSTableMeta** tableMeta = taosHashGet(info->superTables, elements.measure, elements.measureLen);
    if(tableMeta){  // update meta
1571
      ret = smlUpdateMeta(*tableMeta, tinfo->tags, cols, &info->msgBuf);
wmmhello's avatar
wmmhello 已提交
1572
      if(!ret){
wmmhello's avatar
wmmhello 已提交
1573
        uError("SML:0x%"PRIx64" smlUpdateMeta failed", info->id);
wmmhello's avatar
wmmhello 已提交
1574 1575
        return TSDB_CODE_SML_INVALID_DATA;
      }
wmmhello's avatar
wmmhello 已提交
1576
    }else{
wmmhello's avatar
wmmhello 已提交
1577
      SSmlSTableMeta *meta = smlBuildSTableMeta();
1578
      smlInsertMeta(meta, tinfo->tags, cols);
wmmhello's avatar
wmmhello 已提交
1579 1580 1581
      taosHashPut(info->superTables, elements.measure, elements.measureLen, &meta, POINTER_BYTES);
    }

1582
    taosHashPut(info->childTables, elements.measure, elements.measureTagsLen, &tinfo, POINTER_BYTES);
wmmhello's avatar
wmmhello 已提交
1583
  }
1584 1585 1586 1587 1588

  if(!info->dataFormat){
    taosArrayClear(info->colsContainer);
  }
  taosHashClear(info->dumplicateKey);
wmmhello's avatar
wmmhello 已提交
1589 1590 1591
  return TSDB_CODE_SUCCESS;
}

wmmhello's avatar
wmmhello 已提交
1592
static void smlDestroyInfo(SSmlHandle* info){
wmmhello's avatar
wmmhello 已提交
1593 1594
  if(!info) return;
  qDestroyQuery(info->pQuery);
wmmhello's avatar
wmmhello 已提交
1595 1596 1597 1598 1599 1600 1601 1602 1603
  smlDestroyHandle(info->exec);

  // destroy info->childTables
  void** p1 = taosHashIterate(info->childTables, NULL);
  while (p1) {
    SSmlTableInfo* oneTable = *p1;
    smlDestroyBuildTableInfo(oneTable, info->dataFormat);
    p1 = taosHashIterate(info->childTables, p1);
  }
wmmhello's avatar
wmmhello 已提交
1604
  taosHashCleanup(info->childTables);
wmmhello's avatar
wmmhello 已提交
1605 1606 1607 1608 1609 1610 1611 1612

  // destroy info->superTables
  p1 = taosHashIterate(info->superTables, NULL);
  while (p1) {
    SSmlSTableMeta* oneTable = *p1;
    smlDestroySTableMeta(oneTable);
    p1 = taosHashIterate(info->superTables, p1);
  }
wmmhello's avatar
wmmhello 已提交
1613
  taosHashCleanup(info->superTables);
wmmhello's avatar
wmmhello 已提交
1614 1615

  // destroy info->pVgHash
wmmhello's avatar
wmmhello 已提交
1616
  taosHashCleanup(info->pVgHash);
1617
  taosHashCleanup(info->dumplicateKey);
wmmhello's avatar
wmmhello 已提交
1618 1619

  taosMemoryFreeClear(info);
wmmhello's avatar
wmmhello 已提交
1620
}
wmmhello's avatar
wmmhello 已提交
1621 1622

static SSmlHandle* smlBuildSmlInfo(TAOS* taos, SRequestObj* request, SMLProtocolType protocol, int8_t precision, bool dataFormat){
1623
  int32_t code = TSDB_CODE_SUCCESS;
wmmhello's avatar
wmmhello 已提交
1624
  SSmlHandle* info = taosMemoryMalloc(sizeof(SSmlHandle));
wmmhello's avatar
wmmhello 已提交
1625 1626 1627
  if (NULL == info) {
    return NULL;
  }
wmmhello's avatar
wmmhello 已提交
1628
  info->id          = smlGenId();
wmmhello's avatar
wmmhello 已提交
1629

wmmhello's avatar
wmmhello 已提交
1630
  info->pQuery      = taosMemoryCalloc(1, sizeof(SQuery));
wmmhello's avatar
wmmhello 已提交
1631
  if (NULL == info->pQuery) {
wmmhello's avatar
wmmhello 已提交
1632
    uError("SML:0x%"PRIx64" create info->pQuery error", info->id);
wmmhello's avatar
wmmhello 已提交
1633 1634
    goto cleanup;
  }
wmmhello's avatar
wmmhello 已提交
1635
  info->pQuery->execMode      = QUERY_EXEC_MODE_SCHEDULE;
wmmhello's avatar
wmmhello 已提交
1636
  info->pQuery->haveResultSet = false;
wmmhello's avatar
wmmhello 已提交
1637 1638 1639 1640 1641 1642
  info->pQuery->msgType       = TDMT_VND_SUBMIT;
  info->pQuery->pRoot         = (SNode*)nodesMakeNode(QUERY_NODE_VNODE_MODIF_STMT);
  if(NULL == info->pQuery->pRoot){
    uError("SML:0x%"PRIx64" create info->pQuery->pRoot error", info->id);
    goto cleanup;
  }
wmmhello's avatar
wmmhello 已提交
1643 1644
  ((SVnodeModifOpStmt*)(info->pQuery->pRoot))->payloadType = PAYLOAD_TYPE_KV;

wmmhello's avatar
wmmhello 已提交
1645
  info->taos        = taos;
1646
  code = catalogGetHandle(info->taos->pAppInfo->clusterId, &info->pCatalog);
wmmhello's avatar
wmmhello 已提交
1647 1648
  if(code != TSDB_CODE_SUCCESS){
    uError("SML:0x%"PRIx64" get catalog error %d", info->id, code);
wmmhello's avatar
wmmhello 已提交
1649 1650 1651
    goto cleanup;
  }

wmmhello's avatar
wmmhello 已提交
1652 1653 1654 1655 1656 1657 1658 1659
  info->precision   = precision;
  info->protocol    = protocol;
  info->dataFormat  = dataFormat;
  info->pRequest    = request;
  info->msgBuf.buf  = info->pRequest->msgBuf;
  info->msgBuf.len  = ERROR_MSG_BUF_DEFAULT_SIZE;

  info->exec        = smlInitHandle(info->pQuery);
1660 1661 1662
  info->childTables = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
  info->superTables = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
  info->pVgHash     = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK);
wmmhello's avatar
wmmhello 已提交
1663

1664 1665 1666 1667 1668 1669 1670 1671
  info->dumplicateKey = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
  if(!dataFormat){
    info->colsContainer = taosArrayInit(32, POINTER_BYTES);
    if(NULL == info->colsContainer){
      uError("SML:0x%"PRIx64" create info failed", info->id);
      goto cleanup;
    }
  }
wmmhello's avatar
wmmhello 已提交
1672
  if(NULL == info->exec || NULL == info->childTables
1673 1674
      || NULL == info->superTables || NULL == info->pVgHash
      || NULL == info->dumplicateKey){
wmmhello's avatar
wmmhello 已提交
1675 1676 1677
    uError("SML:0x%"PRIx64" create info failed", info->id);
    goto cleanup;
  }
wmmhello's avatar
wmmhello 已提交
1678

wmmhello's avatar
wmmhello 已提交
1679
  return info;
wmmhello's avatar
wmmhello 已提交
1680 1681 1682 1683
cleanup:
  smlDestroyInfo(info);
  return NULL;
}
wmmhello's avatar
wmmhello 已提交
1684
static int32_t smlInsertData(SSmlHandle* info) {
wmmhello's avatar
wmmhello 已提交
1685 1686
  int32_t code = TSDB_CODE_SUCCESS;

wmmhello's avatar
wmmhello 已提交
1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705
  SSmlTableInfo** oneTable = taosHashIterate(info->childTables, NULL);
  while (oneTable) {
    SSmlTableInfo* tableData = *oneTable;

    SName pName = {TSDB_TABLE_NAME_T, info->taos->acctId, {0}, {0}};
    strcpy(pName.dbname, info->pRequest->pDb);
    memcpy(pName.tname, tableData->childTableName, strlen(tableData->childTableName));
    SEpSet ep = getEpSet_s(&info->taos->pAppInfo->mgmtEp);
    SVgroupInfo vg;
    code = catalogGetTableHashVgroup(info->pCatalog, info->taos->pAppInfo->pTransporter, &ep, &pName, &vg);
    if (code != 0) {
      uError("SML:0x%"PRIx64" catalogGetTableHashVgroup failed. table name: %s", info->id, tableData->childTableName);
      return code;
    }
    taosHashPut(info->pVgHash, (const char*)&vg.vgId, sizeof(vg.vgId), (char*)&vg, sizeof(vg));

    SSmlSTableMeta** pMeta = taosHashGet(info->superTables, tableData->sTableName, tableData->sTableNameLen);
    ASSERT (NULL != pMeta && NULL != *pMeta);

1706
    // use tablemeta of stable to save vgid and uid of child table
wmmhello's avatar
wmmhello 已提交
1707 1708 1709
    (*pMeta)->tableMeta->vgId = vg.vgId;
    (*pMeta)->tableMeta->uid = tableData->uid; // one table merge data block together according uid

1710
    code = smlBindData(info->exec, tableData->tags, tableData->colsFormat, (*pMeta)->cols,
1711
                       tableData->cols, info->dataFormat, (*pMeta)->tableMeta, tableData->childTableName, info->msgBuf.buf, info->msgBuf.len);
wmmhello's avatar
wmmhello 已提交
1712 1713 1714 1715
    if(code != TSDB_CODE_SUCCESS){
      return code;
    }
    oneTable = taosHashIterate(info->childTables, oneTable);
wmmhello's avatar
wmmhello 已提交
1716
  }
wmmhello's avatar
wmmhello 已提交
1717 1718

  smlBuildOutput(info->exec, info->pVgHash);
1719 1720
  info->cost.insertRpcTime = taosGetTimestampUs();

wmmhello's avatar
wmmhello 已提交
1721 1722 1723 1724 1725 1726
  launchQueryImpl(info->pRequest, info->pQuery, TSDB_CODE_SUCCESS, true);

  info->affectedRows = taos_affected_rows(info->pRequest);
  return info->pRequest->code;
}

1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745
int32_t numOfSTables;
int32_t numOfCTables;
int32_t numOfCreateSTables;

int64_t parseTime;
int64_t schemaTime;
int64_t insertBindTime;
int64_t insertRpcTime;
int64_t endTime;

static void printStatisticInfo(SSmlHandle *info){
  uError("SML:0x%"PRIx64" smlInsertLines result, code:%d,lineNum:%d,stable num:%d,ctable num:%d,create stable num:%d \
        parse cost:%lld,schema cost:%lld,bind cost:%lld,rpc cost:%lld,total cost:%lld", info->id, info->cost.code,
         info->cost.lineNum, info->cost.numOfSTables, info->cost.numOfCTables, info->cost.numOfCreateSTables,
         info->cost.schemaTime-info->cost.parseTime, info->cost.insertBindTime-info->cost.schemaTime,
         info->cost.insertRpcTime-info->cost.insertBindTime, info->cost.endTime-info->cost.insertRpcTime,
         info->cost.endTime-info->cost.parseTime);
}

wmmhello's avatar
wmmhello 已提交
1746 1747 1748
static int smlInsertLines(SSmlHandle *info, char* lines[], int numLines) {
  int32_t code = TSDB_CODE_SUCCESS;

wmmhello's avatar
wmmhello 已提交
1749
  if (numLines <= 0 || numLines > 65536) {
wmmhello's avatar
wmmhello 已提交
1750
    uError("SML:0x%"PRIx64" smlInsertLines numLines should be between 1 and 65536. numLines: %d", info->id, numLines);
wmmhello's avatar
wmmhello 已提交
1751
    code = TSDB_CODE_TSC_APP_ERROR;
wmmhello's avatar
wmmhello 已提交
1752 1753
    goto cleanup;
  }
wmmhello's avatar
wmmhello 已提交
1754

1755
  info->cost.parseTime = taosGetTimestampUs();
wmmhello's avatar
wmmhello 已提交
1756
  for (int32_t i = 0; i < numLines; ++i) {
wmmhello's avatar
wmmhello 已提交
1757
    code = smlParseLine(info, lines[i]);
wmmhello's avatar
wmmhello 已提交
1758
    if (code != TSDB_CODE_SUCCESS) {
wmmhello's avatar
wmmhello 已提交
1759
      uError("SML:0x%"PRIx64" smlParseLine failed. line %d : %s", info->id, i, lines[i]);
wmmhello's avatar
wmmhello 已提交
1760 1761 1762
      goto cleanup;
    }
  }
wmmhello's avatar
wmmhello 已提交
1763

1764 1765 1766 1767 1768
  info->cost.lineNum = numLines;
  info->cost.numOfSTables = taosHashGetSize(info->superTables);
  info->cost.numOfCTables = taosHashGetSize(info->childTables);

  info->cost.schemaTime = taosGetTimestampUs();
wmmhello's avatar
wmmhello 已提交
1769 1770 1771 1772 1773
  code = smlModifyDBSchemas(info);
  if (code != 0) {
    uError("SML:0x%"PRIx64" smlModifyDBSchemas error : %s", info->id, tstrerror(code));
    goto cleanup;
  }
wmmhello's avatar
wmmhello 已提交
1774

1775
  info->cost.insertBindTime = taosGetTimestampUs();
wmmhello's avatar
wmmhello 已提交
1776 1777 1778
  code = smlInsertData(info);
  if (code != 0) {
    uError("SML:0x%"PRIx64" smlInsertData error : %s", info->id, tstrerror(code));
wmmhello's avatar
wmmhello 已提交
1779 1780
    goto cleanup;
  }
1781
  info->cost.endTime = taosGetTimestampUs();
wmmhello's avatar
wmmhello 已提交
1782 1783

cleanup:
1784 1785
  info->cost.code = code;
  printStatisticInfo(info);
wmmhello's avatar
wmmhello 已提交
1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809
  return code;
}

/**
 * taos_schemaless_insert() parse and insert data points into database according to
 * different protocol.
 *
 * @param $lines input array may contain multiple lines, each line indicates a data point.
 *               If protocol=2 is used input array should contain single JSON
 *               string(e.g. char *lines[] = {"$JSON_string"}). If need to insert
 *               multiple data points in JSON format, should include them in $JSON_string
 *               as a JSON array.
 * @param $numLines indicates how many data points in $lines.
 *                  If protocol = 2 is used this param will be ignored as $lines should
 *                  contain single JSON string.
 * @param $protocol indicates which protocol to use for parsing:
 *                  0 - influxDB line protocol
 *                  1 - OpenTSDB telnet line protocol
 *                  2 - OpenTSDB JSON format protocol
 * @return return zero for successful insertion. Otherwise return none-zero error code of
 *         failure reason.
 *
 */

wmmhello's avatar
wmmhello 已提交
1810
TAOS_RES* taos_schemaless_insert(TAOS* taos, char* lines[], int numLines, int protocol, int precision) {
wmmhello's avatar
wmmhello 已提交
1811
  SRequestObj* request = createRequest(taos, NULL, NULL, TSDB_SQL_INSERT);
wmmhello's avatar
wmmhello 已提交
1812
  if(!request){
1813
    return NULL;
wmmhello's avatar
wmmhello 已提交
1814 1815
  }

1816
  SSmlHandle* info = smlBuildSmlInfo(taos, request, protocol, precision, true);
wmmhello's avatar
wmmhello 已提交
1817
  if(!info){
1818
    return (TAOS_RES*)request;
wmmhello's avatar
wmmhello 已提交
1819 1820
  }

wmmhello's avatar
wmmhello 已提交
1821
  switch (protocol) {
wmmhello's avatar
wmmhello 已提交
1822
    case TSDB_SML_LINE_PROTOCOL:{
wmmhello's avatar
wmmhello 已提交
1823
      smlInsertLines(info, lines, numLines);
wmmhello's avatar
wmmhello 已提交
1824
      break;
wmmhello's avatar
wmmhello 已提交
1825
    }
wmmhello's avatar
wmmhello 已提交
1826 1827 1828 1829 1830 1831 1832 1833 1834
    case TSDB_SML_TELNET_PROTOCOL:
      //code = taos_insert_telnet_lines(taos, lines, numLines, protocol, tsType, &affected_rows);
      break;
    case TSDB_SML_JSON_PROTOCOL:
      //code = taos_insert_json_payload(taos, *lines, protocol, tsType, &affected_rows);
      break;
    default:
      break;
  }
1835
  smlDestroyInfo(info);
wmmhello's avatar
wmmhello 已提交
1836

wmmhello's avatar
wmmhello 已提交
1837 1838
end:
  return (TAOS_RES*)request;
wmmhello's avatar
wmmhello 已提交
1839
}
1840