clientSml.c 55.0 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"
18
#include "tname.h"
wmmhello's avatar
wmmhello 已提交
19
//=================================================================================================
wmmhello's avatar
wmmhello 已提交
20 21 22 23 24 25

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

wmmhello's avatar
wmmhello 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40
//=================================================================================================
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];
41 42
  SArray *tags;
  SArray *fields;
wmmhello's avatar
wmmhello 已提交
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 81
} 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*>

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

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

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

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

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

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
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 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
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;

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

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

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

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

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

  return id;
}

wmmhello's avatar
wmmhello 已提交
157
static int32_t smlBuildInvalidDataMsg(SSmlMsgBuf* pBuf, const char *msg1, const char *msg2) {
wmmhello's avatar
wmmhello 已提交
158 159 160 161 162 163
  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 已提交
164 165 166
  return TSDB_CODE_SML_INVALID_DATA;
}

wmmhello's avatar
wmmhello 已提交
167 168
static int32_t smlGenerateSchemaAction(SSchema* pointColField, SHashObj* dbAttrHash, SArray* dbAttrArray, bool isTag, char sTableName[],
                                       SSchemaAction* action, bool* actionNeeded, SSmlHandle* info) {
wmmhello's avatar
wmmhello 已提交
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 196 197 198 199 200 201 202 203 204 205 206 207
//  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 已提交
208 209 210
  return 0;
}

wmmhello's avatar
wmmhello 已提交
211
static int32_t smlBuildColumnDescription(SSmlKv* field, char* buf, int32_t bufSize, int32_t* outBytes) {
wmmhello's avatar
wmmhello 已提交
212
  uint8_t type = field->type;
wmmhello's avatar
wmmhello 已提交
213 214
  char    tname[TSDB_TABLE_NAME_LEN] = {0};
  memcpy(tname, field->key, field->keyLen);
wmmhello's avatar
wmmhello 已提交
215
  if (type == TSDB_DATA_TYPE_BINARY || type == TSDB_DATA_TYPE_NCHAR) {
wmmhello's avatar
wmmhello 已提交
216
    int32_t bytes = field->valueLen;   // todo
wmmhello's avatar
wmmhello 已提交
217
    int out = snprintf(buf, bufSize,"%s %s(%d)",
wmmhello's avatar
wmmhello 已提交
218
                       tname,tDataTypes[field->type].name, bytes);
wmmhello's avatar
wmmhello 已提交
219 220
    *outBytes = out;
  } else {
wmmhello's avatar
wmmhello 已提交
221
    int out = snprintf(buf, bufSize, "%s %s", tname, tDataTypes[type].name);
wmmhello's avatar
wmmhello 已提交
222 223 224 225 226 227
    *outBytes = out;
  }

  return 0;
}

wmmhello's avatar
wmmhello 已提交
228
static int32_t smlApplySchemaAction(SSmlHandle* info, SSchemaAction* action) {
wmmhello's avatar
wmmhello 已提交
229 230
  int32_t code = 0;
  int32_t outBytes = 0;
wmmhello's avatar
wmmhello 已提交
231
  char *result = (char *)taosMemoryCalloc(1, tsMaxSQLStringLen+1);
wmmhello's avatar
wmmhello 已提交
232 233 234 235 236 237
  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 已提交
238 239
      smlBuildColumnDescription(action->alterSTable.field, result+n, capacity-n, &outBytes);
      TAOS_RES* res = taos_query(info->taos, result); //TODO async doAsyncQuery
wmmhello's avatar
wmmhello 已提交
240
      code = taos_errno(res);
wmmhello's avatar
wmmhello 已提交
241
      const char* errStr = taos_errstr(res);
wmmhello's avatar
wmmhello 已提交
242 243 244 245 246
      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 已提交
247
//      if (code == TSDB_CODE_MND_FIELD_ALREADY_EXIST || code == TSDB_CODE_MND_TAG_ALREADY_EXIST || tscDupColNames) {
wmmhello's avatar
wmmhello 已提交
248
      if (code == TSDB_CODE_MND_TAG_ALREADY_EXIST) {
wmmhello's avatar
wmmhello 已提交
249
        TAOS_RES* res2 = taos_query(info->taos, "RESET QUERY CACHE");
wmmhello's avatar
wmmhello 已提交
250 251 252 253 254 255 256 257 258 259 260
        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 已提交
261
      smlBuildColumnDescription(action->alterSTable.field,
wmmhello's avatar
wmmhello 已提交
262
                             result+n, capacity-n, &outBytes);
wmmhello's avatar
wmmhello 已提交
263
      TAOS_RES* res = taos_query(info->taos, result); //TODO async doAsyncQuery
wmmhello's avatar
wmmhello 已提交
264
      code = taos_errno(res);
wmmhello's avatar
wmmhello 已提交
265
      const char* errStr = taos_errstr(res);
wmmhello's avatar
wmmhello 已提交
266 267 268 269 270
      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 已提交
271
//      if (code ==TSDB_CODE_MND_TAG_ALREADY_EXIST || code == TSDB_CODE_MND_FIELD_ALREAY_EXIST || tscDupColNames) {
wmmhello's avatar
wmmhello 已提交
272
      if (code ==TSDB_CODE_MND_TAG_ALREADY_EXIST) {
wmmhello's avatar
wmmhello 已提交
273
        TAOS_RES* res2 = taos_query(info->taos, "RESET QUERY CACHE");
wmmhello's avatar
wmmhello 已提交
274 275 276 277 278 279 280 281 282 283 284
        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 已提交
285
      smlBuildColumnDescription(action->alterSTable.field, result+n,
wmmhello's avatar
wmmhello 已提交
286
                             capacity-n, &outBytes);
wmmhello's avatar
wmmhello 已提交
287
      TAOS_RES* res = taos_query(info->taos, result); //TODO async doAsyncQuery
wmmhello's avatar
wmmhello 已提交
288 289 290 291 292 293
      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 已提交
294 295
//      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 已提交
296
        TAOS_RES* res2 = taos_query(info->taos, "RESET QUERY CACHE");
wmmhello's avatar
wmmhello 已提交
297 298 299 300 301 302 303 304 305 306 307
        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 已提交
308
      smlBuildColumnDescription(action->alterSTable.field, result+n,
wmmhello's avatar
wmmhello 已提交
309
                             capacity-n, &outBytes);
wmmhello's avatar
wmmhello 已提交
310
      TAOS_RES* res = taos_query(info->taos, result); //TODO async doAsyncQuery
wmmhello's avatar
wmmhello 已提交
311 312 313 314 315 316
      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 已提交
317 318
//      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 已提交
319
        TAOS_RES* res2 = taos_query(info->taos, "RESET QUERY CACHE");
wmmhello's avatar
wmmhello 已提交
320 321 322 323 324 325 326 327 328 329 330 331
        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 已提交
332

333
      SArray *cols = action->createSTable.fields;
wmmhello's avatar
wmmhello 已提交
334 335

      for(int i = 0; i < taosArrayGetSize(cols); i++){
wmmhello's avatar
wmmhello 已提交
336
        SSmlKv *kv = (SSmlKv *)taosArrayGetP(cols, i);
337
        smlBuildColumnDescription(kv, pos, freeBytes, &outBytes);
wmmhello's avatar
wmmhello 已提交
338 339 340
        pos += outBytes; freeBytes -= outBytes;
        *pos = ','; ++pos; --freeBytes;
      }
wmmhello's avatar
wmmhello 已提交
341

wmmhello's avatar
wmmhello 已提交
342 343 344 345 346
      --pos; ++freeBytes;

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

347 348
      cols = action->createSTable.tags;
      for(int i = 0; i < taosArrayGetSize(cols); i++){
wmmhello's avatar
wmmhello 已提交
349
        SSmlKv *kv = (SSmlKv *)taosArrayGetP(cols, i);
350
        smlBuildColumnDescription(kv, pos, freeBytes, &outBytes);
wmmhello's avatar
wmmhello 已提交
351 352 353 354 355
        pos += outBytes; freeBytes -= outBytes;
        *pos = ','; ++pos; --freeBytes;
      }
      pos--; ++freeBytes;
      outBytes = snprintf(pos, freeBytes, ")");
wmmhello's avatar
wmmhello 已提交
356
      TAOS_RES* res = taos_query(info->taos, result);
wmmhello's avatar
wmmhello 已提交
357 358 359 360 361 362
      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 已提交
363
      if (code == TSDB_CODE_MND_STB_ALREADY_EXIST) {
wmmhello's avatar
wmmhello 已提交
364
        TAOS_RES* res2 = taos_query(info->taos, "RESET QUERY CACHE");
wmmhello's avatar
wmmhello 已提交
365 366 367 368 369 370 371 372 373 374 375 376 377 378
        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 已提交
379
  taosMemoryFreeClear(result);
wmmhello's avatar
wmmhello 已提交
380 381 382 383 384 385
  if (code != 0) {
    uError("SML:0x%"PRIx64 " apply schema action failure. %s", info->id, tstrerror(code));
  }
  return code;
}

wmmhello's avatar
wmmhello 已提交
386
static int32_t smlModifyDBSchemas(SSmlHandle* info) {
wmmhello's avatar
wmmhello 已提交
387 388
  int32_t code = 0;

wmmhello's avatar
wmmhello 已提交
389
  SSmlSTableMeta** tableMetaSml = (SSmlSTableMeta**)taosHashIterate(info->superTables, NULL);
wmmhello's avatar
wmmhello 已提交
390
  while (tableMetaSml) {
391
    SSmlSTableMeta* sTableData = *tableMetaSml;
wmmhello's avatar
wmmhello 已提交
392 393

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

wmmhello's avatar
wmmhello 已提交
396
    size_t superTableLen = 0;
wmmhello's avatar
wmmhello 已提交
397
    void *superTable = taosHashGetKey(tableMetaSml, &superTableLen);    // todo escape
wmmhello's avatar
wmmhello 已提交
398 399 400
    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 已提交
401

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

D
dapan1121 已提交
404
    if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST || code == TSDB_CODE_MND_INVALID_STB) {
wmmhello's avatar
wmmhello 已提交
405
      SSchemaAction schemaAction = {.action = SCHEMA_ACTION_CREATE_STABLE};
wmmhello's avatar
wmmhello 已提交
406
      memcpy(schemaAction.createSTable.sTableName, superTable, superTableLen);
407 408
      schemaAction.createSTable.tags = sTableData->tags;
      schemaAction.createSTable.fields = sTableData->cols;
wmmhello's avatar
wmmhello 已提交
409 410 411 412 413 414
      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 已提交
415
      code = catalogGetSTableMeta(info->pCatalog, info->taos->pAppInfo->pTransporter, &ep, &pName, &pTableMeta);
wmmhello's avatar
wmmhello 已提交
416
      if (code != 0) {
wmmhello's avatar
wmmhello 已提交
417
        uError("SML:0x%"PRIx64" catalogGetSTableMeta failed. super table name %s", info->id, schemaAction.createSTable.sTableName);
wmmhello's avatar
wmmhello 已提交
418 419
        return code;
      }
420
      info->cost.numOfCreateSTables++;
wmmhello's avatar
wmmhello 已提交
421
    }else if (code == TSDB_CODE_SUCCESS) {
wmmhello's avatar
wmmhello 已提交
422 423 424 425
    } else {
      uError("SML:0x%"PRIx64" load table meta error: %s", info->id, tstrerror(code));
      return code;
    }
426
    sTableData->tableMeta = pTableMeta;
wmmhello's avatar
wmmhello 已提交
427

wmmhello's avatar
wmmhello 已提交
428
    tableMetaSml = (SSmlSTableMeta**)taosHashIterate(info->superTables, tableMetaSml);
wmmhello's avatar
wmmhello 已提交
429 430 431 432 433 434 435 436 437 438 439
  }
  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 已提交
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
//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 已提交
482

wmmhello's avatar
wmmhello 已提交
483
static bool smlParseTinyInt(SSmlKv *kvVal, bool *isValid, SSmlMsgBuf *msg) {
wmmhello's avatar
wmmhello 已提交
484 485
  const char *pVal = kvVal->value;
  int32_t len = kvVal->valueLen;
wmmhello's avatar
wmmhello 已提交
486 487 488
  if (len <= 2) {
    return false;
  }
wmmhello's avatar
wmmhello 已提交
489
  const char *signalPos = pVal + len - 2;
wmmhello's avatar
wmmhello 已提交
490
  if (!strncasecmp(signalPos, "i8", 2)) {
wmmhello's avatar
wmmhello 已提交
491 492 493 494
    char *endptr = NULL;
    int64_t result = strtoll(pVal, &endptr, 10);
    if(endptr != signalPos){       // 78ri8
      *isValid = false;
wmmhello's avatar
wmmhello 已提交
495
      smlBuildInvalidDataMsg(msg, "invalid tiny int", endptr);
wmmhello's avatar
wmmhello 已提交
496 497
    }else if(!IS_VALID_TINYINT(result)){
      *isValid = false;
wmmhello's avatar
wmmhello 已提交
498
      smlBuildInvalidDataMsg(msg, "tiny int out of range[-128,127]", endptr);
wmmhello's avatar
wmmhello 已提交
499 500 501 502
    }else{
      kvVal->i = result;
      *isValid = true;
    }
wmmhello's avatar
wmmhello 已提交
503 504 505 506 507
    return true;
  }
  return false;
}

wmmhello's avatar
wmmhello 已提交
508
static bool smlParseTinyUint(SSmlKv *kvVal, bool *isValid, SSmlMsgBuf *msg) {
wmmhello's avatar
wmmhello 已提交
509 510
  const char *pVal = kvVal->value;
  int32_t len = kvVal->valueLen;
wmmhello's avatar
wmmhello 已提交
511 512 513 514 515 516
  if (len <= 2) {
    return false;
  }
  if (pVal[0] == '-') {
    return false;
  }
wmmhello's avatar
wmmhello 已提交
517
  const char *signalPos = pVal + len - 2;
wmmhello's avatar
wmmhello 已提交
518
  if (!strncasecmp(signalPos, "u8", 2)) {
wmmhello's avatar
wmmhello 已提交
519 520 521 522
    char *endptr = NULL;
    int64_t result = strtoll(pVal, &endptr, 10);
    if(endptr != signalPos){       // 78ri8
      *isValid = false;
wmmhello's avatar
wmmhello 已提交
523
      smlBuildInvalidDataMsg(msg, "invalid unsigned tiny int", endptr);
wmmhello's avatar
wmmhello 已提交
524 525
    }else if(!IS_VALID_UTINYINT(result)){
      *isValid = false;
wmmhello's avatar
wmmhello 已提交
526
      smlBuildInvalidDataMsg(msg, "unsigned tiny int out of range[0,255]", endptr);
wmmhello's avatar
wmmhello 已提交
527 528 529 530
    }else{
      kvVal->i = result;
      *isValid = true;
    }
wmmhello's avatar
wmmhello 已提交
531 532 533 534 535
    return true;
  }
  return false;
}

wmmhello's avatar
wmmhello 已提交
536
static bool smlParseSmallInt(SSmlKv *kvVal, bool *isValid, SSmlMsgBuf *msg) {
wmmhello's avatar
wmmhello 已提交
537 538
  const char *pVal = kvVal->value;
  int32_t len = kvVal->valueLen;
wmmhello's avatar
wmmhello 已提交
539 540 541
  if (len <= 3) {
    return false;
  }
wmmhello's avatar
wmmhello 已提交
542
  const char *signalPos = pVal + len - 3;
wmmhello's avatar
wmmhello 已提交
543
  if (!strncasecmp(signalPos, "i16", 3)) {
wmmhello's avatar
wmmhello 已提交
544 545 546 547
    char *endptr = NULL;
    int64_t result = strtoll(pVal, &endptr, 10);
    if(endptr != signalPos){       // 78ri8
      *isValid = false;
wmmhello's avatar
wmmhello 已提交
548
      smlBuildInvalidDataMsg(msg, "invalid small int", endptr);
wmmhello's avatar
wmmhello 已提交
549 550
    }else if(!IS_VALID_SMALLINT(result)){
      *isValid = false;
wmmhello's avatar
wmmhello 已提交
551
      smlBuildInvalidDataMsg(msg, "small int our of range[-32768,32767]", endptr);
wmmhello's avatar
wmmhello 已提交
552 553 554 555
    }else{
      kvVal->i = result;
      *isValid = true;
    }
wmmhello's avatar
wmmhello 已提交
556 557 558 559 560
    return true;
  }
  return false;
}

wmmhello's avatar
wmmhello 已提交
561
static bool smlParseSmallUint(SSmlKv *kvVal, bool *isValid, SSmlMsgBuf *msg) {
wmmhello's avatar
wmmhello 已提交
562 563
  const char *pVal = kvVal->value;
  int32_t len = kvVal->valueLen;
wmmhello's avatar
wmmhello 已提交
564 565 566 567 568 569
  if (len <= 3) {
    return false;
  }
  if (pVal[0] == '-') {
    return false;
  }
wmmhello's avatar
wmmhello 已提交
570
  const char *signalPos = pVal + len - 3;
wmmhello's avatar
wmmhello 已提交
571
  if (strncasecmp(signalPos, "u16", 3) == 0) {
wmmhello's avatar
wmmhello 已提交
572 573 574 575
    char *endptr = NULL;
    int64_t result = strtoll(pVal, &endptr, 10);
    if(endptr != signalPos){       // 78ri8
      *isValid = false;
wmmhello's avatar
wmmhello 已提交
576
      smlBuildInvalidDataMsg(msg, "invalid unsigned small int", endptr);
wmmhello's avatar
wmmhello 已提交
577 578
    }else if(!IS_VALID_USMALLINT(result)){
      *isValid = false;
wmmhello's avatar
wmmhello 已提交
579
      smlBuildInvalidDataMsg(msg, "unsigned small int out of rang[0,65535]", endptr);
wmmhello's avatar
wmmhello 已提交
580 581 582 583
    }else{
      kvVal->i = result;
      *isValid = true;
    }
wmmhello's avatar
wmmhello 已提交
584 585 586 587 588
    return true;
  }
  return false;
}

wmmhello's avatar
wmmhello 已提交
589
static bool smlParseInt(SSmlKv *kvVal, bool *isValid, SSmlMsgBuf *msg) {
wmmhello's avatar
wmmhello 已提交
590 591
  const char *pVal = kvVal->value;
  int32_t len = kvVal->valueLen;
wmmhello's avatar
wmmhello 已提交
592 593 594
  if (len <= 3) {
    return false;
  }
wmmhello's avatar
wmmhello 已提交
595
  const char *signalPos = pVal + len - 3;
wmmhello's avatar
wmmhello 已提交
596
  if (strncasecmp(signalPos, "i32", 3) == 0) {
wmmhello's avatar
wmmhello 已提交
597 598 599 600
    char *endptr = NULL;
    int64_t result = strtoll(pVal, &endptr, 10);
    if(endptr != signalPos){       // 78ri8
      *isValid = false;
wmmhello's avatar
wmmhello 已提交
601
      smlBuildInvalidDataMsg(msg, "invalid int", endptr);
wmmhello's avatar
wmmhello 已提交
602 603
    }else if(!IS_VALID_INT(result)){
      *isValid = false;
wmmhello's avatar
wmmhello 已提交
604
      smlBuildInvalidDataMsg(msg, "int out of range[-2147483648,2147483647]", endptr);
wmmhello's avatar
wmmhello 已提交
605 606 607 608
    }else{
      kvVal->i = result;
      *isValid = true;
    }
wmmhello's avatar
wmmhello 已提交
609 610 611 612 613
    return true;
  }
  return false;
}

wmmhello's avatar
wmmhello 已提交
614
static bool smlParseUint(SSmlKv *kvVal, bool *isValid, SSmlMsgBuf *msg) {
wmmhello's avatar
wmmhello 已提交
615 616
  const char *pVal = kvVal->value;
  int32_t len = kvVal->valueLen;
wmmhello's avatar
wmmhello 已提交
617 618 619 620 621 622
  if (len <= 3) {
    return false;
  }
  if (pVal[0] == '-') {
    return false;
  }
wmmhello's avatar
wmmhello 已提交
623
  const char *signalPos = pVal + len - 3;
wmmhello's avatar
wmmhello 已提交
624
  if (strncasecmp(signalPos, "u32", 3) == 0) {
wmmhello's avatar
wmmhello 已提交
625 626 627 628
    char *endptr = NULL;
    int64_t result = strtoll(pVal, &endptr, 10);
    if(endptr != signalPos){       // 78ri8
      *isValid = false;
wmmhello's avatar
wmmhello 已提交
629
      smlBuildInvalidDataMsg(msg, "invalid unsigned int", endptr);
wmmhello's avatar
wmmhello 已提交
630 631
    }else if(!IS_VALID_UINT(result)){
      *isValid = false;
wmmhello's avatar
wmmhello 已提交
632
      smlBuildInvalidDataMsg(msg, "unsigned int out of range[0,4294967295]", endptr);
wmmhello's avatar
wmmhello 已提交
633 634 635 636
    }else{
      kvVal->i = result;
      *isValid = true;
    }
wmmhello's avatar
wmmhello 已提交
637 638 639 640 641
    return true;
  }
  return false;
}

wmmhello's avatar
wmmhello 已提交
642
static bool smlParseBigInt(SSmlKv *kvVal, bool *isValid, SSmlMsgBuf *msg) {
wmmhello's avatar
wmmhello 已提交
643 644
  const char *pVal = kvVal->value;
  int32_t len = kvVal->valueLen;
wmmhello's avatar
wmmhello 已提交
645
  if (len > 3 && strncasecmp(pVal + len - 3, "i64", 3) == 0) {
wmmhello's avatar
wmmhello 已提交
646
    char *endptr = NULL;
wmmhello's avatar
wmmhello 已提交
647
    errno = 0;
wmmhello's avatar
wmmhello 已提交
648 649 650
    int64_t result = strtoll(pVal, &endptr, 10);
    if(endptr != pVal + len - 3){       // 78ri8
      *isValid = false;
wmmhello's avatar
wmmhello 已提交
651
      smlBuildInvalidDataMsg(msg, "invalid big int", endptr);
wmmhello's avatar
wmmhello 已提交
652
    }else if(errno == ERANGE || !IS_VALID_BIGINT(result)){
wmmhello's avatar
wmmhello 已提交
653
      *isValid = false;
wmmhello's avatar
wmmhello 已提交
654
      smlBuildInvalidDataMsg(msg, "big int out of range[-9223372036854775808,9223372036854775807]", endptr);
wmmhello's avatar
wmmhello 已提交
655 656 657 658 659 660 661
    }else{
      kvVal->i = result;
      *isValid = true;
    }
    return true;
  }else if (len > 1 && pVal[len - 1] == 'i') {
    char *endptr = NULL;
wmmhello's avatar
wmmhello 已提交
662
    errno = 0;
wmmhello's avatar
wmmhello 已提交
663 664 665
    int64_t result = strtoll(pVal, &endptr, 10);
    if(endptr != pVal + len - 1){       // 78ri8
      *isValid = false;
wmmhello's avatar
wmmhello 已提交
666
      smlBuildInvalidDataMsg(msg, "invalid big int", endptr);
wmmhello's avatar
wmmhello 已提交
667
    }else if(errno == ERANGE || !IS_VALID_BIGINT(result)){
wmmhello's avatar
wmmhello 已提交
668
      *isValid = false;
wmmhello's avatar
wmmhello 已提交
669
      smlBuildInvalidDataMsg(msg, "big int out of range[-9223372036854775808,9223372036854775807]", endptr);
wmmhello's avatar
wmmhello 已提交
670 671 672 673
    }else{
      kvVal->i = result;
      *isValid = true;
    }
wmmhello's avatar
wmmhello 已提交
674 675 676 677 678
    return true;
  }
  return false;
}

wmmhello's avatar
wmmhello 已提交
679
static bool smlParseBigUint(SSmlKv *kvVal, bool *isValid, SSmlMsgBuf *msg) {
wmmhello's avatar
wmmhello 已提交
680 681
  const char *pVal = kvVal->value;
  int32_t len = kvVal->valueLen;
wmmhello's avatar
wmmhello 已提交
682 683 684 685 686 687
  if (len <= 3) {
    return false;
  }
  if (pVal[0] == '-') {
    return false;
  }
wmmhello's avatar
wmmhello 已提交
688
  const char *signalPos = pVal + len - 3;
wmmhello's avatar
wmmhello 已提交
689
  if (strncasecmp(signalPos, "u64", 3) == 0) {
wmmhello's avatar
wmmhello 已提交
690
    char *endptr = NULL;
wmmhello's avatar
wmmhello 已提交
691
    errno = 0;
wmmhello's avatar
wmmhello 已提交
692 693 694
    uint64_t result = strtoull(pVal, &endptr, 10);
    if(endptr != signalPos){       // 78ri8
      *isValid = false;
wmmhello's avatar
wmmhello 已提交
695
      smlBuildInvalidDataMsg(msg, "invalid unsigned big int", endptr);
wmmhello's avatar
wmmhello 已提交
696
    }else if(errno == ERANGE || !IS_VALID_UBIGINT(result)){
wmmhello's avatar
wmmhello 已提交
697
      *isValid = false;
wmmhello's avatar
wmmhello 已提交
698
      smlBuildInvalidDataMsg(msg, "unsigned big int out of range[0,18446744073709551615]", endptr);
wmmhello's avatar
wmmhello 已提交
699 700 701 702
    }else{
      kvVal->u = result;
      *isValid = true;
    }
wmmhello's avatar
wmmhello 已提交
703 704 705 706 707
    return true;
  }
  return false;
}

wmmhello's avatar
wmmhello 已提交
708
static bool smlParseFloat(SSmlKv *kvVal, bool *isValid, SSmlMsgBuf *msg) {
wmmhello's avatar
wmmhello 已提交
709 710 711
  const char *pVal = kvVal->value;
  int32_t len = kvVal->valueLen;
  char *endptr = NULL;
wmmhello's avatar
wmmhello 已提交
712
  errno = 0;
wmmhello's avatar
wmmhello 已提交
713
  float result = strtof(pVal, &endptr);
wmmhello's avatar
wmmhello 已提交
714
  if(endptr == pVal + len && errno != ERANGE && IS_VALID_FLOAT(result)){       // 78
wmmhello's avatar
wmmhello 已提交
715 716 717
    kvVal->f = result;
    *isValid = true;
    return true;
wmmhello's avatar
wmmhello 已提交
718
  }
wmmhello's avatar
wmmhello 已提交
719

wmmhello's avatar
wmmhello 已提交
720
  if (len > 3 && strncasecmp(pVal + len - 3, "f32", 3) == 0) {
wmmhello's avatar
wmmhello 已提交
721 722
    if(endptr != pVal + len - 3){       // 78ri8
      *isValid = false;
wmmhello's avatar
wmmhello 已提交
723
      smlBuildInvalidDataMsg(msg, "invalid float", endptr);
wmmhello's avatar
wmmhello 已提交
724
    }else if(errno == ERANGE || !IS_VALID_FLOAT(result)){
wmmhello's avatar
wmmhello 已提交
725
      *isValid = false;
wmmhello's avatar
wmmhello 已提交
726
      smlBuildInvalidDataMsg(msg, "float out of range[-3.402823466e+38,3.402823466e+38]", endptr);
wmmhello's avatar
wmmhello 已提交
727 728 729 730
    }else{
      kvVal->f = result;
      *isValid = true;
    }
wmmhello's avatar
wmmhello 已提交
731 732 733 734 735
    return true;
  }
  return false;
}

wmmhello's avatar
wmmhello 已提交
736
static bool smlParseDouble(SSmlKv *kvVal, bool *isValid, SSmlMsgBuf *msg) {
wmmhello's avatar
wmmhello 已提交
737 738
  const char *pVal = kvVal->value;
  int32_t len = kvVal->valueLen;
wmmhello's avatar
wmmhello 已提交
739 740 741
  if (len <= 3) {
    return false;
  }
wmmhello's avatar
wmmhello 已提交
742
  const char *signalPos = pVal + len - 3;
wmmhello's avatar
wmmhello 已提交
743
  if (strncasecmp(signalPos, "f64", 3) == 0) {
wmmhello's avatar
wmmhello 已提交
744
    char *endptr = NULL;
wmmhello's avatar
wmmhello 已提交
745
    errno = 0;
wmmhello's avatar
wmmhello 已提交
746 747 748
    double result = strtod(pVal, &endptr);
    if(endptr != signalPos){       // 78ri8
      *isValid = false;
wmmhello's avatar
wmmhello 已提交
749
      smlBuildInvalidDataMsg(msg, "invalid double", endptr);
wmmhello's avatar
wmmhello 已提交
750
    }else if(errno == ERANGE || !IS_VALID_DOUBLE(result)){
wmmhello's avatar
wmmhello 已提交
751
      *isValid = false;
wmmhello's avatar
wmmhello 已提交
752
      smlBuildInvalidDataMsg(msg, "double out of range[-1.7976931348623158e+308,1.7976931348623158e+308]", endptr);
wmmhello's avatar
wmmhello 已提交
753 754 755 756
    }else{
      kvVal->d = result;
      *isValid = true;
    }
wmmhello's avatar
wmmhello 已提交
757 758 759 760 761
    return true;
  }
  return false;
}

wmmhello's avatar
wmmhello 已提交
762
static bool smlParseBool(SSmlKv *kvVal) {
wmmhello's avatar
wmmhello 已提交
763 764 765 766
  const char *pVal = kvVal->value;
  int32_t len = kvVal->valueLen;
  if ((len == 1) && pVal[len - 1] == 't') {
    kvVal->i = true;
wmmhello's avatar
wmmhello 已提交
767 768 769
    return true;
  }

wmmhello's avatar
wmmhello 已提交
770 771
  if ((len == 1) && pVal[len - 1] == 'f') {
    kvVal->i = false;
wmmhello's avatar
wmmhello 已提交
772 773 774
    return true;
  }

wmmhello's avatar
wmmhello 已提交
775
  if((len == 4) && !strncasecmp(pVal, "true", len)) {
wmmhello's avatar
wmmhello 已提交
776
    kvVal->i = true;
wmmhello's avatar
wmmhello 已提交
777 778
    return true;
  }
wmmhello's avatar
wmmhello 已提交
779
  if((len == 5) && !strncasecmp(pVal, "false", len)) {
wmmhello's avatar
wmmhello 已提交
780
    kvVal->i = false;
wmmhello's avatar
wmmhello 已提交
781 782 783 784 785
    return true;
  }
  return false;
}

wmmhello's avatar
wmmhello 已提交
786
static bool smlIsBinary(const char *pVal, uint16_t len) {
wmmhello's avatar
wmmhello 已提交
787 788 789 790 791 792 793 794 795 796
  //binary: "abc"
  if (len < 2) {
    return false;
  }
  if (pVal[0] == '"' && pVal[len - 1] == '"') {
    return true;
  }
  return false;
}

wmmhello's avatar
wmmhello 已提交
797
static bool smlIsNchar(const char *pVal, uint16_t len) {
wmmhello's avatar
wmmhello 已提交
798 799 800 801 802 803 804 805 806 807
  //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 已提交
808
static bool smlParseValue(SSmlKv *pVal, SSmlMsgBuf *msg) {
wmmhello's avatar
wmmhello 已提交
809 810
  // put high probability matching type first
  bool isValid = false;
wmmhello's avatar
wmmhello 已提交
811

wmmhello's avatar
wmmhello 已提交
812
  //binary
wmmhello's avatar
wmmhello 已提交
813
  if (smlIsBinary(pVal->value, pVal->valueLen)) {
wmmhello's avatar
wmmhello 已提交
814 815
    pVal->type = TSDB_DATA_TYPE_BINARY;
    pVal->valueLen -= 2;
wmmhello's avatar
wmmhello 已提交
816 817
    pVal->length = pVal->valueLen;
    pVal->value++;
wmmhello's avatar
wmmhello 已提交
818
    return true;
wmmhello's avatar
wmmhello 已提交
819
  }
wmmhello's avatar
wmmhello 已提交
820
  //nchar
wmmhello's avatar
wmmhello 已提交
821
  if (smlIsNchar(pVal->value, pVal->valueLen)) {
wmmhello's avatar
wmmhello 已提交
822
    pVal->type = TSDB_DATA_TYPE_NCHAR;
wmmhello's avatar
wmmhello 已提交
823 824 825 826 827 828 829 830 831 832
    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 已提交
833 834
    return true;
  }
wmmhello's avatar
wmmhello 已提交
835
  //double
wmmhello's avatar
wmmhello 已提交
836
  if (smlParseDouble(pVal, &isValid, msg)) {
wmmhello's avatar
wmmhello 已提交
837 838 839
    if(!isValid) return false;
    pVal->type = TSDB_DATA_TYPE_DOUBLE;
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
wmmhello's avatar
wmmhello 已提交
840

wmmhello's avatar
wmmhello 已提交
841 842 843
    return true;
  }
  //bool
wmmhello's avatar
wmmhello 已提交
844
  if (smlParseBool(pVal)) {
wmmhello's avatar
wmmhello 已提交
845
    pVal->type = TSDB_DATA_TYPE_BOOL;
wmmhello's avatar
wmmhello 已提交
846 847 848
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
    return true;
  }
wmmhello's avatar
wmmhello 已提交
849

wmmhello's avatar
wmmhello 已提交
850
  if (smlParseTinyInt(pVal, &isValid, msg)) {
wmmhello's avatar
wmmhello 已提交
851
    if(!isValid) return false;
wmmhello's avatar
wmmhello 已提交
852 853 854 855
    pVal->type = TSDB_DATA_TYPE_TINYINT;
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
    return true;
  }
wmmhello's avatar
wmmhello 已提交
856
  if (smlParseTinyUint(pVal, &isValid, msg)) {
wmmhello's avatar
wmmhello 已提交
857
    if(!isValid) return false;
wmmhello's avatar
wmmhello 已提交
858 859 860 861
    pVal->type = TSDB_DATA_TYPE_UTINYINT;
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
    return true;
  }
wmmhello's avatar
wmmhello 已提交
862
  if (smlParseSmallInt(pVal, &isValid, msg)) {
wmmhello's avatar
wmmhello 已提交
863
    if(!isValid) return false;
wmmhello's avatar
wmmhello 已提交
864 865 866 867
    pVal->type = TSDB_DATA_TYPE_SMALLINT;
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
    return true;
  }
wmmhello's avatar
wmmhello 已提交
868
  if (smlParseSmallUint(pVal, &isValid, msg)) {
wmmhello's avatar
wmmhello 已提交
869
    if(!isValid) return false;
wmmhello's avatar
wmmhello 已提交
870 871 872 873
    pVal->type = TSDB_DATA_TYPE_USMALLINT;
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
    return true;
  }
wmmhello's avatar
wmmhello 已提交
874
  if (smlParseInt(pVal, &isValid, msg)) {
wmmhello's avatar
wmmhello 已提交
875
    if(!isValid) return false;
wmmhello's avatar
wmmhello 已提交
876 877 878 879
    pVal->type = TSDB_DATA_TYPE_INT;
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
    return true;
  }
wmmhello's avatar
wmmhello 已提交
880
  if (smlParseUint(pVal, &isValid, msg)) {
wmmhello's avatar
wmmhello 已提交
881
    if(!isValid) return false;
wmmhello's avatar
wmmhello 已提交
882 883 884 885
    pVal->type = TSDB_DATA_TYPE_UINT;
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
    return true;
  }
wmmhello's avatar
wmmhello 已提交
886
  if (smlParseBigInt(pVal, &isValid, msg)) {
wmmhello's avatar
wmmhello 已提交
887
    if(!isValid) return false;
wmmhello's avatar
wmmhello 已提交
888 889 890 891
    pVal->type = TSDB_DATA_TYPE_BIGINT;
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
    return true;
  }
wmmhello's avatar
wmmhello 已提交
892
  if (smlParseBigUint(pVal, &isValid, msg)) {
wmmhello's avatar
wmmhello 已提交
893
    if(!isValid) return false;
wmmhello's avatar
wmmhello 已提交
894 895 896 897 898
    pVal->type = TSDB_DATA_TYPE_UBIGINT;
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
    return true;
  }

wmmhello's avatar
wmmhello 已提交
899
  smlBuildInvalidDataMsg(msg, "invalid data", pVal->value);
wmmhello's avatar
wmmhello 已提交
900 901 902
  return false;
}

wmmhello's avatar
wmmhello 已提交
903
static int32_t smlParseString(const char* sql, SSmlLineInfo *elements, SSmlMsgBuf *msg){
wmmhello's avatar
wmmhello 已提交
904 905 906 907 908 909 910 911
  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 已提交
912 913 914 915
  if (!elements->measure || *sql == COMMA) {
    smlBuildInvalidDataMsg(msg, "invalid data", sql);
    return TSDB_CODE_SML_INVALID_DATA;
  }
wmmhello's avatar
wmmhello 已提交
916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937

  // 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 已提交
938 939 940 941 942 943 944
  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 已提交
945 946 947 948 949 950 951 952 953

  // parse cols
  while (*sql != '\0') {
    if(*sql != SPACE) {
      elements->cols = sql;
      break;
    }
    sql++;
  }
wmmhello's avatar
wmmhello 已提交
954 955 956
  if(!elements->cols) {
    smlBuildInvalidDataMsg(msg, "invalid columns", elements->cols);
    return TSDB_CODE_SML_INVALID_DATA;
957
  }
wmmhello's avatar
wmmhello 已提交
958

wmmhello's avatar
wmmhello 已提交
959
  bool isInQuote = false;
wmmhello's avatar
wmmhello 已提交
960
  while (*sql != '\0') {
wmmhello's avatar
wmmhello 已提交
961 962 963 964
    if(*sql == QUOTE && *(sql - 1) != SLASH){
      isInQuote = !isInQuote;
    }
    if(!isInQuote && *sql == SPACE && *(sql - 1) != SLASH) {
wmmhello's avatar
wmmhello 已提交
965 966 967 968
      break;
    }
    sql++;
  }
969 970 971 972
  if(isInQuote){
    smlBuildInvalidDataMsg(msg, "only one quote", elements->cols);
    return TSDB_CODE_SML_INVALID_DATA;
  }
wmmhello's avatar
wmmhello 已提交
973
  elements->colsLen = sql - elements->cols;
wmmhello's avatar
wmmhello 已提交
974

wmmhello's avatar
wmmhello 已提交
975
  // parse ts,ts can be empty
wmmhello's avatar
wmmhello 已提交
976
  while (*sql != '\0') {
977
    if(*sql != SPACE && elements->timestamp == NULL) {
wmmhello's avatar
wmmhello 已提交
978
      elements->timestamp = sql;
979 980
    }
    if(*sql == SPACE && elements->timestamp != NULL){
wmmhello's avatar
wmmhello 已提交
981 982 983 984
      break;
    }
    sql++;
  }
wmmhello's avatar
wmmhello 已提交
985 986 987
  if(elements->timestamp){
    elements->timestampLen = sql - elements->timestamp;
  }
wmmhello's avatar
wmmhello 已提交
988 989 990 991

  return TSDB_CODE_SUCCESS;
}

992
static int32_t smlParseCols(const char* data, int32_t len, SArray *cols, bool isTag, SHashObj *dumplicateKey, SSmlMsgBuf *msg){
wmmhello's avatar
wmmhello 已提交
993
  if(isTag && len == 0){
wmmhello's avatar
wmmhello 已提交
994
    SSmlKv *kv = (SSmlKv *)taosMemoryCalloc(sizeof(SSmlKv), 1);
wmmhello's avatar
wmmhello 已提交
995
    kv->key = TAG;
wmmhello's avatar
wmmhello 已提交
996
    kv->keyLen = strlen(TAG);
wmmhello's avatar
wmmhello 已提交
997
    kv->value = TAG;
wmmhello's avatar
wmmhello 已提交
998
    kv->valueLen = strlen(TAG);
wmmhello's avatar
wmmhello 已提交
999 1000
    kv->type = TSDB_DATA_TYPE_NCHAR;
    if(cols) taosArrayPush(cols, &kv);
wmmhello's avatar
wmmhello 已提交
1001
    return TSDB_CODE_SUCCESS;
wmmhello's avatar
wmmhello 已提交
1002 1003
  }

wmmhello's avatar
wmmhello 已提交
1004
  for(int i = 0; i < len; i++){
wmmhello's avatar
wmmhello 已提交
1005
    // parse key
wmmhello's avatar
wmmhello 已提交
1006 1007 1008 1009 1010 1011 1012 1013 1014
    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 已提交
1015
    if(keyLen == 0 || keyLen >= TSDB_COL_NAME_LEN){
wmmhello's avatar
wmmhello 已提交
1016
      smlBuildInvalidDataMsg(msg, "invalid key or key is too long than 64", key);
wmmhello's avatar
wmmhello 已提交
1017 1018 1019
      return TSDB_CODE_SML_INVALID_DATA;
    }

1020 1021 1022 1023 1024 1025 1026
    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 已提交
1027
    // parse value
wmmhello's avatar
wmmhello 已提交
1028 1029
    i++;
    const char *value = data + i;
wmmhello's avatar
wmmhello 已提交
1030
    bool isInQuote = false;
wmmhello's avatar
wmmhello 已提交
1031
    while(i < len){
wmmhello's avatar
wmmhello 已提交
1032 1033 1034 1035
      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 已提交
1036 1037 1038 1039
        break;
      }
      i++;
    }
wmmhello's avatar
wmmhello 已提交
1040 1041 1042 1043
    if(isInQuote){
      smlBuildInvalidDataMsg(msg, "only one quote", value);
      return TSDB_CODE_SML_INVALID_DATA;
    }
wmmhello's avatar
wmmhello 已提交
1044
    int32_t valueLen = data + i - value;
wmmhello's avatar
wmmhello 已提交
1045
    if(valueLen == 0){
wmmhello's avatar
wmmhello 已提交
1046
      smlBuildInvalidDataMsg(msg, "invalid value", value);
wmmhello's avatar
wmmhello 已提交
1047 1048
      return TSDB_CODE_SML_INVALID_DATA;
    }
wmmhello's avatar
wmmhello 已提交
1049 1050

    // add kv to SSmlKv
wmmhello's avatar
wmmhello 已提交
1051
    SSmlKv *kv = (SSmlKv *)taosMemoryCalloc(sizeof(SSmlKv), 1);
wmmhello's avatar
wmmhello 已提交
1052 1053 1054 1055
    kv->key = key;
    kv->keyLen = keyLen;
    kv->value = value;
    kv->valueLen = valueLen;
wmmhello's avatar
wmmhello 已提交
1056 1057
    if(isTag){
      kv->type = TSDB_DATA_TYPE_NCHAR;
wmmhello's avatar
wmmhello 已提交
1058
    }else{
wmmhello's avatar
wmmhello 已提交
1059
      if(!smlParseValue(kv, msg)){
wmmhello's avatar
wmmhello 已提交
1060 1061
        return TSDB_CODE_SML_INVALID_DATA;
      }
wmmhello's avatar
wmmhello 已提交
1062
    }
wmmhello's avatar
wmmhello 已提交
1063

wmmhello's avatar
wmmhello 已提交
1064 1065
    if(cols) taosArrayPush(cols, &kv);
  }
wmmhello's avatar
wmmhello 已提交
1066

wmmhello's avatar
wmmhello 已提交
1067 1068 1069
  return TSDB_CODE_SUCCESS;
}

wmmhello's avatar
wmmhello 已提交
1070 1071 1072 1073 1074 1075
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 已提交
1076 1077 1078
  switch (type) {
    case TSDB_TIME_PRECISION_HOURS:
      ts *= (3600 * 1e9);
wmmhello's avatar
wmmhello 已提交
1079
      break;
wmmhello's avatar
wmmhello 已提交
1080 1081
    case TSDB_TIME_PRECISION_MINUTES:
      ts *= (60 * 1e9);
wmmhello's avatar
wmmhello 已提交
1082
      break;
wmmhello's avatar
wmmhello 已提交
1083 1084
    case TSDB_TIME_PRECISION_SECONDS:
      ts *= (1e9);
wmmhello's avatar
wmmhello 已提交
1085
      break;
wmmhello's avatar
wmmhello 已提交
1086
    case TSDB_TIME_PRECISION_MILLI:
wmmhello's avatar
wmmhello 已提交
1087
      ts *= (1e6);
wmmhello's avatar
wmmhello 已提交
1088
      break;
wmmhello's avatar
wmmhello 已提交
1089
    case TSDB_TIME_PRECISION_MICRO:
wmmhello's avatar
wmmhello 已提交
1090 1091
      ts *= (1e3);
      break;
wmmhello's avatar
wmmhello 已提交
1092 1093
    case TSDB_TIME_PRECISION_NANO:
      break;
wmmhello's avatar
wmmhello 已提交
1094 1095
    default:
      ASSERT(0);
wmmhello's avatar
wmmhello 已提交
1096 1097 1098 1099
  }
  if(ts > (double)INT64_MAX || ts < 0){
    return -1;
  }
wmmhello's avatar
wmmhello 已提交
1100 1101

  return (int64_t)ts;
wmmhello's avatar
wmmhello 已提交
1102 1103
}

wmmhello's avatar
wmmhello 已提交
1104
static int64_t smlGetTimeNow(int8_t precision) {
wmmhello's avatar
wmmhello 已提交
1105 1106 1107 1108 1109 1110 1111
  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 已提交
1112 1113 1114
    case TSDB_TIME_PRECISION_MILLI:
    case TSDB_TIME_PRECISION_MICRO:
    case TSDB_TIME_PRECISION_NANO:
wmmhello's avatar
wmmhello 已提交
1115
      return taosGetTimestamp(precision);
wmmhello's avatar
wmmhello 已提交
1116 1117
    default:
      ASSERT(0);
wmmhello's avatar
wmmhello 已提交
1118 1119 1120
  }
}

wmmhello's avatar
wmmhello 已提交
1121
static int8_t smlGetTsTypeByLen(int32_t len) {
wmmhello's avatar
wmmhello 已提交
1122 1123 1124 1125 1126
  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 已提交
1127
    return -1;
wmmhello's avatar
wmmhello 已提交
1128 1129 1130
  }
}

wmmhello's avatar
wmmhello 已提交
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
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 已提交
1188
  int64_t ts = 0;
wmmhello's avatar
wmmhello 已提交
1189 1190
  if(info->protocol == TSDB_SML_LINE_PROTOCOL){
    ts = smlParseInfluxTime(info, data, len);
wmmhello's avatar
wmmhello 已提交
1191
  }else{
wmmhello's avatar
wmmhello 已提交
1192
    ts = smlParseOpenTsdbTime(info, data, len);
wmmhello's avatar
wmmhello 已提交
1193
  }
wmmhello's avatar
wmmhello 已提交
1194
  if(ts == -1)  return TSDB_CODE_TSC_INVALID_TIME_STAMP;
wmmhello's avatar
wmmhello 已提交
1195

wmmhello's avatar
wmmhello 已提交
1196
  // add ts to
wmmhello's avatar
wmmhello 已提交
1197
  SSmlKv *kv = (SSmlKv *)taosMemoryCalloc(sizeof(SSmlKv), 1);
wmmhello's avatar
wmmhello 已提交
1198 1199 1200 1201 1202
  if(!kv){
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  kv->key = TS;
wmmhello's avatar
wmmhello 已提交
1203
  kv->keyLen = strlen(kv->key);
wmmhello's avatar
wmmhello 已提交
1204
  kv->i = ts;
wmmhello's avatar
wmmhello 已提交
1205
  kv->type = TSDB_DATA_TYPE_TIMESTAMP;
wmmhello's avatar
wmmhello 已提交
1206
  kv->length = (int16_t)tDataTypes[kv->type].bytes;
wmmhello's avatar
wmmhello 已提交
1207
  if(cols) taosArrayPush(cols, &kv);
wmmhello's avatar
wmmhello 已提交
1208 1209 1210
  return TSDB_CODE_SUCCESS;
}

wmmhello's avatar
wmmhello 已提交
1211
//static int32_t parseSmlCols(const char* data, SArray *cols){
wmmhello's avatar
wmmhello 已提交
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 1245 1246 1247 1248 1249 1250 1251 1252 1253
//  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 已提交
1254
static bool smlUpdateMeta(SSmlSTableMeta* tableMeta, SArray *tags, SArray *cols, SSmlMsgBuf *msg){
wmmhello's avatar
wmmhello 已提交
1255 1256
  if(tags){
    for (int i = 0; i < taosArrayGetSize(tags); ++i) {
wmmhello's avatar
wmmhello 已提交
1257
      SSmlKv *kv = (SSmlKv *)taosArrayGetP(tags, i);
wmmhello's avatar
wmmhello 已提交
1258 1259
      ASSERT(kv->type == TSDB_DATA_TYPE_NCHAR);

wmmhello's avatar
wmmhello 已提交
1260
      uint8_t *index = (uint8_t *)taosHashGet(tableMeta->tagHash, kv->key, kv->keyLen);
1261
      if(index){
wmmhello's avatar
wmmhello 已提交
1262
        SSmlKv **value = (SSmlKv **)taosArrayGet(tableMeta->tags, *index);
wmmhello's avatar
wmmhello 已提交
1263 1264 1265
        ASSERT((*value)->type == TSDB_DATA_TYPE_NCHAR);
        if(kv->valueLen > (*value)->valueLen){    // tags type is nchar
          *value = kv;
wmmhello's avatar
wmmhello 已提交
1266 1267
        }
      }else{
1268 1269 1270 1271 1272
        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 已提交
1273 1274 1275 1276 1277 1278
      }
    }
  }

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

wmmhello's avatar
wmmhello 已提交
1281
      int16_t *index = (int16_t *)taosHashGet(tableMeta->fieldHash, kv->key, kv->keyLen);
1282
      if(index){
wmmhello's avatar
wmmhello 已提交
1283
        SSmlKv **value = (SSmlKv **)taosArrayGet(tableMeta->cols, *index);
wmmhello's avatar
wmmhello 已提交
1284
        if(kv->type != (*value)->type){
wmmhello's avatar
wmmhello 已提交
1285
          smlBuildInvalidDataMsg(msg, "the type is not the same like before", kv->key);
wmmhello's avatar
wmmhello 已提交
1286 1287 1288 1289 1290 1291 1292
          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 已提交
1293 1294
        }
      }else{
1295 1296 1297 1298 1299
        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 已提交
1300 1301 1302
      }
    }
  }
wmmhello's avatar
wmmhello 已提交
1303
  return true;
wmmhello's avatar
wmmhello 已提交
1304 1305
}

wmmhello's avatar
wmmhello 已提交
1306
static void smlInsertMeta(SSmlSTableMeta* tableMeta, SArray *tags, SArray *cols){
wmmhello's avatar
wmmhello 已提交
1307
  if(tags){
1308
    for (uint8_t i = 0; i < taosArrayGetSize(tags); ++i) {
wmmhello's avatar
wmmhello 已提交
1309
      SSmlKv *kv = (SSmlKv *)taosArrayGetP(tags, i);
1310 1311
      taosArrayPush(tableMeta->tags, &kv);
      taosHashPut(tableMeta->tagHash, kv->key, kv->keyLen, &i, CHAR_BYTES);
wmmhello's avatar
wmmhello 已提交
1312 1313 1314 1315
    }
  }

  if(cols){
1316
    for (int16_t i = 0; i < taosArrayGetSize(cols); ++i) {
wmmhello's avatar
wmmhello 已提交
1317
      SSmlKv *kv = (SSmlKv *)taosArrayGetP(cols, i);
1318 1319
      taosArrayPush(tableMeta->cols, &kv);
      taosHashPut(tableMeta->fieldHash, kv->key, kv->keyLen, &i, SHORT_BYTES);
wmmhello's avatar
wmmhello 已提交
1320 1321 1322 1323
    }
  }
}

wmmhello's avatar
wmmhello 已提交
1324
static SSmlTableInfo* smlBuildTableInfo(bool format){
wmmhello's avatar
wmmhello 已提交
1325
  SSmlTableInfo *tag = (SSmlTableInfo *)taosMemoryCalloc(sizeof(SSmlTableInfo), 1);
wmmhello's avatar
wmmhello 已提交
1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351
  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:
wmmhello's avatar
wmmhello 已提交
1352
  taosMemoryFree(tag);
wmmhello's avatar
wmmhello 已提交
1353 1354 1355 1356 1357 1358 1359 1360 1361
  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++){
wmmhello's avatar
wmmhello 已提交
1362 1363
      SHashObj *kvHash = (SHashObj *)taosArrayGetP(tag->cols, i);
      void** p1 = (void**)taosHashIterate(kvHash, NULL);
wmmhello's avatar
wmmhello 已提交
1364
      while (p1) {
wmmhello's avatar
wmmhello 已提交
1365 1366
        taosMemoryFree(*p1);
        p1 = (void**)taosHashIterate(kvHash, p1);
wmmhello's avatar
wmmhello 已提交
1367 1368 1369 1370 1371
      }
      taosHashCleanup(kvHash);
    }
  }
  taosArrayDestroy(tag->tags);
wmmhello's avatar
wmmhello 已提交
1372
  taosMemoryFree(tag);
wmmhello's avatar
wmmhello 已提交
1373 1374 1375 1376 1377
}

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

1381 1382 1383 1384 1385 1386
  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++){
wmmhello's avatar
wmmhello 已提交
1387
    SSmlKv *kv = (SSmlKv *)taosArrayGetP(cols, i);
1388
    taosHashPut(kvHash, kv->key, kv->keyLen, &kv, POINTER_BYTES);   // todo key need escape, like \=, because find by schema name later
wmmhello's avatar
wmmhello 已提交
1389
  }
1390 1391
  taosArrayPush(oneTable->cols, &kvHash);

1392
  return TSDB_CODE_SUCCESS;
wmmhello's avatar
wmmhello 已提交
1393 1394 1395
}

static SSmlSTableMeta* smlBuildSTableMeta(){
wmmhello's avatar
wmmhello 已提交
1396
  SSmlSTableMeta* meta = (SSmlSTableMeta*)taosMemoryCalloc(sizeof(SSmlSTableMeta), 1);
wmmhello's avatar
wmmhello 已提交
1397 1398 1399
  if(!meta){
    return NULL;
  }
1400
  meta->tagHash = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
wmmhello's avatar
wmmhello 已提交
1401 1402 1403 1404 1405
  if (meta->tagHash == NULL) {
    uError("SML:smlBuildSTableMeta failed to allocate memory");
    goto cleanup;
  }

1406
  meta->fieldHash = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
wmmhello's avatar
wmmhello 已提交
1407 1408 1409 1410
  if (meta->fieldHash == NULL) {
    uError("SML:smlBuildSTableMeta failed to allocate memory");
    goto cleanup;
  }
1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422

  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 已提交
1423 1424 1425
  return meta;

cleanup:
wmmhello's avatar
wmmhello 已提交
1426
  taosMemoryFree(meta);
wmmhello's avatar
wmmhello 已提交
1427 1428 1429 1430 1431 1432
  return NULL;
}

static void smlDestroySTableMeta(SSmlSTableMeta *meta){
  taosHashCleanup(meta->tagHash);
  taosHashCleanup(meta->fieldHash);
1433 1434
  taosArrayDestroy(meta->tags);
  taosArrayDestroy(meta->cols);
wmmhello's avatar
wmmhello 已提交
1435 1436 1437 1438 1439 1440
  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 已提交
1441
  if(ret != TSDB_CODE_SUCCESS){
wmmhello's avatar
wmmhello 已提交
1442
    uError("SML:0x%"PRIx64" smlParseString failed", info->id);
wmmhello's avatar
wmmhello 已提交
1443 1444 1445
    return ret;
  }

1446 1447 1448 1449 1450 1451 1452 1453 1454
  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 已提交
1455
  }
wmmhello's avatar
wmmhello 已提交
1456

wmmhello's avatar
wmmhello 已提交
1457
  ret = smlParseTS(info, elements.timestamp, elements.timestampLen, cols);
wmmhello's avatar
wmmhello 已提交
1458
  if(ret != TSDB_CODE_SUCCESS){
wmmhello's avatar
wmmhello 已提交
1459
    uError("SML:0x%"PRIx64" smlParseTS failed", info->id);
wmmhello's avatar
wmmhello 已提交
1460 1461
    return ret;
  }
1462
  ret = smlParseCols(elements.cols, elements.colsLen, cols, false, info->dumplicateKey, &info->msgBuf);
wmmhello's avatar
wmmhello 已提交
1463
  if(ret != TSDB_CODE_SUCCESS){
wmmhello's avatar
wmmhello 已提交
1464
    uError("SML:0x%"PRIx64" smlParseCols parse cloums fields failed", info->id);
wmmhello's avatar
wmmhello 已提交
1465 1466
    return ret;
  }
wmmhello's avatar
wmmhello 已提交
1467
  if(taosArrayGetSize(cols) > TSDB_MAX_COLUMNS){
wmmhello's avatar
wmmhello 已提交
1468
    smlBuildInvalidDataMsg(&info->msgBuf, "too many columns than 4096", NULL);
wmmhello's avatar
wmmhello 已提交
1469 1470
    return TSDB_CODE_SML_INVALID_DATA;
  }
wmmhello's avatar
wmmhello 已提交
1471

wmmhello's avatar
wmmhello 已提交
1472
  SSmlTableInfo **oneTable = (SSmlTableInfo **)taosHashGet(info->childTables, elements.measure, elements.measureTagsLen);
wmmhello's avatar
wmmhello 已提交
1473
  if(oneTable){
wmmhello's avatar
wmmhello 已提交
1474
    SSmlSTableMeta** tableMeta = (SSmlSTableMeta**)taosHashGet(info->superTables, elements.measure, elements.measureLen);
wmmhello's avatar
wmmhello 已提交
1475
    ASSERT(tableMeta);
wmmhello's avatar
wmmhello 已提交
1476
    ret = smlUpdateMeta(*tableMeta, NULL, cols, &info->msgBuf);    // update meta cols
wmmhello's avatar
wmmhello 已提交
1477
    if(!ret){
wmmhello's avatar
wmmhello 已提交
1478
      uError("SML:0x%"PRIx64" smlUpdateMeta cols failed", info->id);
wmmhello's avatar
wmmhello 已提交
1479 1480
      return TSDB_CODE_SML_INVALID_DATA;
    }
wmmhello's avatar
wmmhello 已提交
1481 1482 1483 1484
    ret = smlDealCols(*oneTable, info->dataFormat, cols);
    if(ret != TSDB_CODE_SUCCESS){
      return ret;
    }
wmmhello's avatar
wmmhello 已提交
1485
  }else{
1486 1487
    SSmlTableInfo *tinfo = smlBuildTableInfo(info->dataFormat);
    if(!tinfo){
wmmhello's avatar
wmmhello 已提交
1488 1489
      return TSDB_CODE_TSC_OUT_OF_MEMORY;
    }
1490
    ret = smlDealCols(tinfo, info->dataFormat, cols);
wmmhello's avatar
wmmhello 已提交
1491 1492
    if(ret != TSDB_CODE_SUCCESS){
      return ret;
wmmhello's avatar
wmmhello 已提交
1493 1494
    }

1495
    ret = smlParseCols(elements.tags, elements.tagsLen, tinfo->tags, true, info->dumplicateKey, &info->msgBuf);
wmmhello's avatar
wmmhello 已提交
1496
    if(ret != TSDB_CODE_SUCCESS){
wmmhello's avatar
wmmhello 已提交
1497
      uError("SML:0x%"PRIx64" smlParseCols parse tag fields failed", info->id);
wmmhello's avatar
wmmhello 已提交
1498 1499 1500
      return ret;
    }

1501
    if(taosArrayGetSize(tinfo->tags) > TSDB_MAX_TAGS){
wmmhello's avatar
wmmhello 已提交
1502
      smlBuildInvalidDataMsg(&info->msgBuf, "too many tags than 128", NULL);
wmmhello's avatar
wmmhello 已提交
1503 1504 1505
      return TSDB_CODE_SML_INVALID_DATA;
    }

1506 1507
    tinfo->sTableName = elements.measure;
    tinfo->sTableNameLen = elements.measureLen;
1508 1509 1510 1511
    RandTableName rName = {.tags=tinfo->tags, .sTableName=tinfo->sTableName, .sTableNameLen=tinfo->sTableNameLen,
                            .childTableName=tinfo->childTableName};
    buildChildTableName(&rName);
    tinfo->uid = rName.uid;
wmmhello's avatar
wmmhello 已提交
1512

wmmhello's avatar
wmmhello 已提交
1513
    SSmlSTableMeta** tableMeta = (SSmlSTableMeta**)taosHashGet(info->superTables, elements.measure, elements.measureLen);
wmmhello's avatar
wmmhello 已提交
1514
    if(tableMeta){  // update meta
1515
      ret = smlUpdateMeta(*tableMeta, tinfo->tags, cols, &info->msgBuf);
wmmhello's avatar
wmmhello 已提交
1516
      if(!ret){
wmmhello's avatar
wmmhello 已提交
1517
        uError("SML:0x%"PRIx64" smlUpdateMeta failed", info->id);
wmmhello's avatar
wmmhello 已提交
1518 1519
        return TSDB_CODE_SML_INVALID_DATA;
      }
wmmhello's avatar
wmmhello 已提交
1520
    }else{
wmmhello's avatar
wmmhello 已提交
1521
      SSmlSTableMeta *meta = smlBuildSTableMeta();
1522
      smlInsertMeta(meta, tinfo->tags, cols);
wmmhello's avatar
wmmhello 已提交
1523 1524 1525
      taosHashPut(info->superTables, elements.measure, elements.measureLen, &meta, POINTER_BYTES);
    }

1526
    taosHashPut(info->childTables, elements.measure, elements.measureTagsLen, &tinfo, POINTER_BYTES);
wmmhello's avatar
wmmhello 已提交
1527
  }
1528 1529 1530 1531 1532

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

wmmhello's avatar
wmmhello 已提交
1536
static void smlDestroyInfo(SSmlHandle* info){
wmmhello's avatar
wmmhello 已提交
1537 1538
  if(!info) return;
  qDestroyQuery(info->pQuery);
wmmhello's avatar
wmmhello 已提交
1539 1540 1541
  smlDestroyHandle(info->exec);

  // destroy info->childTables
wmmhello's avatar
wmmhello 已提交
1542
  void** p1 = (void**)taosHashIterate(info->childTables, NULL);
wmmhello's avatar
wmmhello 已提交
1543
  while (p1) {
wmmhello's avatar
wmmhello 已提交
1544 1545
    smlDestroyBuildTableInfo((SSmlTableInfo*)(*p1), info->dataFormat);
    p1 = (void**)taosHashIterate(info->childTables, p1);
wmmhello's avatar
wmmhello 已提交
1546
  }
wmmhello's avatar
wmmhello 已提交
1547
  taosHashCleanup(info->childTables);
wmmhello's avatar
wmmhello 已提交
1548 1549

  // destroy info->superTables
wmmhello's avatar
wmmhello 已提交
1550
  p1 = (void**)taosHashIterate(info->superTables, NULL);
wmmhello's avatar
wmmhello 已提交
1551
  while (p1) {
wmmhello's avatar
wmmhello 已提交
1552 1553
    smlDestroySTableMeta((SSmlSTableMeta*)(*p1));
    p1 = (void**)taosHashIterate(info->superTables, p1);
wmmhello's avatar
wmmhello 已提交
1554
  }
wmmhello's avatar
wmmhello 已提交
1555
  taosHashCleanup(info->superTables);
wmmhello's avatar
wmmhello 已提交
1556 1557

  // destroy info->pVgHash
wmmhello's avatar
wmmhello 已提交
1558
  taosHashCleanup(info->pVgHash);
1559
  taosHashCleanup(info->dumplicateKey);
wmmhello's avatar
wmmhello 已提交
1560 1561

  taosMemoryFreeClear(info);
wmmhello's avatar
wmmhello 已提交
1562
}
wmmhello's avatar
wmmhello 已提交
1563 1564

static SSmlHandle* smlBuildSmlInfo(TAOS* taos, SRequestObj* request, SMLProtocolType protocol, int8_t precision, bool dataFormat){
1565
  int32_t code = TSDB_CODE_SUCCESS;
wmmhello's avatar
wmmhello 已提交
1566
  SSmlHandle* info = (SSmlHandle*)taosMemoryCalloc(1, sizeof(SSmlHandle));
wmmhello's avatar
wmmhello 已提交
1567 1568 1569
  if (NULL == info) {
    return NULL;
  }
wmmhello's avatar
wmmhello 已提交
1570
  info->id          = smlGenId();
wmmhello's avatar
wmmhello 已提交
1571

wmmhello's avatar
wmmhello 已提交
1572
  info->pQuery      = (SQuery *)taosMemoryCalloc(1, sizeof(SQuery));
wmmhello's avatar
wmmhello 已提交
1573
  if (NULL == info->pQuery) {
wmmhello's avatar
wmmhello 已提交
1574
    uError("SML:0x%"PRIx64" create info->pQuery error", info->id);
wmmhello's avatar
wmmhello 已提交
1575 1576
    goto cleanup;
  }
wmmhello's avatar
wmmhello 已提交
1577
  info->pQuery->execMode      = QUERY_EXEC_MODE_SCHEDULE;
wmmhello's avatar
wmmhello 已提交
1578
  info->pQuery->haveResultSet = false;
wmmhello's avatar
wmmhello 已提交
1579 1580 1581 1582 1583 1584
  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 已提交
1585 1586
  ((SVnodeModifOpStmt*)(info->pQuery->pRoot))->payloadType = PAYLOAD_TYPE_KV;

wmmhello's avatar
wmmhello 已提交
1587
  info->taos        = (STscObj *)taos;
1588
  code = catalogGetHandle(info->taos->pAppInfo->clusterId, &info->pCatalog);
wmmhello's avatar
wmmhello 已提交
1589 1590
  if(code != TSDB_CODE_SUCCESS){
    uError("SML:0x%"PRIx64" get catalog error %d", info->id, code);
wmmhello's avatar
wmmhello 已提交
1591 1592 1593
    goto cleanup;
  }

wmmhello's avatar
wmmhello 已提交
1594 1595 1596 1597 1598 1599 1600 1601
  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);
1602 1603 1604
  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 已提交
1605

1606 1607 1608 1609 1610 1611 1612 1613
  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 已提交
1614
  if(NULL == info->exec || NULL == info->childTables
1615 1616
      || NULL == info->superTables || NULL == info->pVgHash
      || NULL == info->dumplicateKey){
wmmhello's avatar
wmmhello 已提交
1617 1618 1619
    uError("SML:0x%"PRIx64" create info failed", info->id);
    goto cleanup;
  }
wmmhello's avatar
wmmhello 已提交
1620

wmmhello's avatar
wmmhello 已提交
1621
  return info;
wmmhello's avatar
wmmhello 已提交
1622 1623 1624 1625
cleanup:
  smlDestroyInfo(info);
  return NULL;
}
wmmhello's avatar
wmmhello 已提交
1626
static int32_t smlInsertData(SSmlHandle* info) {
wmmhello's avatar
wmmhello 已提交
1627 1628
  int32_t code = TSDB_CODE_SUCCESS;

wmmhello's avatar
wmmhello 已提交
1629
  SSmlTableInfo** oneTable = (SSmlTableInfo**)taosHashIterate(info->childTables, NULL);
wmmhello's avatar
wmmhello 已提交
1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644
  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));

wmmhello's avatar
wmmhello 已提交
1645
    SSmlSTableMeta** pMeta = (SSmlSTableMeta** )taosHashGet(info->superTables, tableData->sTableName, tableData->sTableNameLen);
wmmhello's avatar
wmmhello 已提交
1646 1647
    ASSERT (NULL != pMeta && NULL != *pMeta);

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

1652
    code = smlBindData(info->exec, tableData->tags, tableData->colsFormat, (*pMeta)->cols,
1653
                       tableData->cols, info->dataFormat, (*pMeta)->tableMeta, tableData->childTableName, info->msgBuf.buf, info->msgBuf.len);
wmmhello's avatar
wmmhello 已提交
1654 1655 1656
    if(code != TSDB_CODE_SUCCESS){
      return code;
    }
wmmhello's avatar
wmmhello 已提交
1657
    oneTable = (SSmlTableInfo**)taosHashIterate(info->childTables, oneTable);
wmmhello's avatar
wmmhello 已提交
1658
  }
wmmhello's avatar
wmmhello 已提交
1659 1660

  smlBuildOutput(info->exec, info->pVgHash);
1661 1662
  info->cost.insertRpcTime = taosGetTimestampUs();

wmmhello's avatar
wmmhello 已提交
1663 1664 1665 1666 1667 1668
  launchQueryImpl(info->pRequest, info->pQuery, TSDB_CODE_SUCCESS, true);

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

1669
static void smlPrintStatisticInfo(SSmlHandle *info){
1670
  uError("SML:0x%"PRIx64" smlInsertLines result, code:%d,lineNum:%d,stable num:%d,ctable num:%d,create stable num:%d \
wmmhello's avatar
wmmhello 已提交
1671
        parse cost:%"PRId64",schema cost:%"PRId64",bind cost:%"PRId64",rpc cost:%"PRId64",total cost:%"PRId64"", info->id, info->cost.code,
1672 1673 1674 1675 1676 1677
         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 已提交
1678 1679 1680
static int smlInsertLines(SSmlHandle *info, char* lines[], int numLines) {
  int32_t code = TSDB_CODE_SUCCESS;

wmmhello's avatar
wmmhello 已提交
1681
  if (numLines <= 0 || numLines > 65536) {
wmmhello's avatar
wmmhello 已提交
1682
    uError("SML:0x%"PRIx64" smlInsertLines numLines should be between 1 and 65536. numLines: %d", info->id, numLines);
wmmhello's avatar
wmmhello 已提交
1683
    code = TSDB_CODE_TSC_APP_ERROR;
wmmhello's avatar
wmmhello 已提交
1684 1685
    goto cleanup;
  }
wmmhello's avatar
wmmhello 已提交
1686

1687
  info->cost.parseTime = taosGetTimestampUs();
wmmhello's avatar
wmmhello 已提交
1688
  for (int32_t i = 0; i < numLines; ++i) {
wmmhello's avatar
wmmhello 已提交
1689
    code = smlParseLine(info, lines[i]);
wmmhello's avatar
wmmhello 已提交
1690
    if (code != TSDB_CODE_SUCCESS) {
wmmhello's avatar
wmmhello 已提交
1691
      uError("SML:0x%"PRIx64" smlParseLine failed. line %d : %s", info->id, i, lines[i]);
wmmhello's avatar
wmmhello 已提交
1692 1693 1694
      goto cleanup;
    }
  }
wmmhello's avatar
wmmhello 已提交
1695

1696 1697 1698 1699 1700
  info->cost.lineNum = numLines;
  info->cost.numOfSTables = taosHashGetSize(info->superTables);
  info->cost.numOfCTables = taosHashGetSize(info->childTables);

  info->cost.schemaTime = taosGetTimestampUs();
wmmhello's avatar
wmmhello 已提交
1701 1702 1703 1704 1705
  code = smlModifyDBSchemas(info);
  if (code != 0) {
    uError("SML:0x%"PRIx64" smlModifyDBSchemas error : %s", info->id, tstrerror(code));
    goto cleanup;
  }
wmmhello's avatar
wmmhello 已提交
1706

1707
  info->cost.insertBindTime = taosGetTimestampUs();
wmmhello's avatar
wmmhello 已提交
1708 1709 1710
  code = smlInsertData(info);
  if (code != 0) {
    uError("SML:0x%"PRIx64" smlInsertData error : %s", info->id, tstrerror(code));
wmmhello's avatar
wmmhello 已提交
1711 1712
    goto cleanup;
  }
1713
  info->cost.endTime = taosGetTimestampUs();
wmmhello's avatar
wmmhello 已提交
1714 1715

cleanup:
1716
  info->cost.code = code;
1717
  smlPrintStatisticInfo(info);
wmmhello's avatar
wmmhello 已提交
1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741
  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 已提交
1742
TAOS_RES* taos_schemaless_insert(TAOS* taos, char* lines[], int numLines, int protocol, int precision) {
wmmhello's avatar
wmmhello 已提交
1743
  SRequestObj* request = (SRequestObj*)createRequest((STscObj *)taos, NULL, NULL, TSDB_SQL_INSERT);
wmmhello's avatar
wmmhello 已提交
1744
  if(!request){
1745
    return NULL;
wmmhello's avatar
wmmhello 已提交
1746 1747
  }

wmmhello's avatar
wmmhello 已提交
1748
  SSmlHandle* info = smlBuildSmlInfo(taos, request, (SMLProtocolType)protocol, precision, true);
wmmhello's avatar
wmmhello 已提交
1749
  if(!info){
1750
    return (TAOS_RES*)request;
wmmhello's avatar
wmmhello 已提交
1751 1752
  }

wmmhello's avatar
wmmhello 已提交
1753
  switch (protocol) {
wmmhello's avatar
wmmhello 已提交
1754
    case TSDB_SML_LINE_PROTOCOL:{
wmmhello's avatar
wmmhello 已提交
1755
      smlInsertLines(info, lines, numLines);
wmmhello's avatar
wmmhello 已提交
1756
      break;
wmmhello's avatar
wmmhello 已提交
1757
    }
wmmhello's avatar
wmmhello 已提交
1758 1759 1760 1761 1762 1763 1764 1765 1766
    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;
  }
1767
  smlDestroyInfo(info);
wmmhello's avatar
wmmhello 已提交
1768

wmmhello's avatar
wmmhello 已提交
1769
  return (TAOS_RES*)request;
wmmhello's avatar
wmmhello 已提交
1770
}
1771