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

wmmhello's avatar
wmmhello 已提交
6
#include "clientSml.h"
wmmhello's avatar
wmmhello 已提交
7 8 9 10 11 12 13 14 15

#include "tdef.h"
#include "ttypes.h"
#include "tmsg.h"
#include "tlog.h"
#include "query.h"
#include "taoserror.h"
#include "taos.h"
#include "ttime.h"
wmmhello's avatar
wmmhello 已提交
16
#include "tstrbuild.h"
wmmhello's avatar
wmmhello 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32


typedef struct  {
  char sTableName[TSDB_TABLE_NAME_LEN];
  SHashObj* tagHash;
  SHashObj* fieldHash;
  SArray* tags; //SArray<SSchema>
  SArray* fields; //SArray<SSchema>
  uint8_t precision;
} SSmlSTableSchema;

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

wmmhello's avatar
wmmhello 已提交
35 36
#define TSNAMELEN 2
#define TAGNAMELEN 3
wmmhello's avatar
wmmhello 已提交
37 38 39
//=================================================================================================

static uint64_t linesSmlHandleId = 0;
wmmhello's avatar
wmmhello 已提交
40 41 42
static const char* TS = "ts";
static const char* TAG = "tag";

wmmhello's avatar
wmmhello 已提交
43 44 45 46 47 48 49 50 51 52 53

uint64_t genLinesSmlId() {
  uint64_t id;

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

  return id;
}

wmmhello's avatar
wmmhello 已提交
54 55 56 57 58 59
static int32_t buildInvalidDataMsg(SMsgBuf* pBuf, const char *msg1, const char *msg2) {
  if(msg1) snprintf(pBuf->buf, pBuf->len, "%s:", msg1);
  if(msg2) strncpy(pBuf->buf, msg2, pBuf->len);
  return TSDB_CODE_SML_INVALID_DATA;
}

wmmhello's avatar
wmmhello 已提交
60
int compareSmlColKv(const void* p1, const void* p2) {
wmmhello's avatar
wmmhello 已提交
61 62
  SSmlKv* kv1 = (SSmlKv *)p1;
  SSmlKv* kv2 = (SSmlKv*)p2;
wmmhello's avatar
wmmhello 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
  int kvLen1 = (int)strlen(kv1->key);
  int kvLen2 = (int)strlen(kv2->key);
  int res = strncasecmp(kv1->key, kv2->key, MIN(kvLen1, kvLen2));
  if (res != 0) {
    return res;
  } else {
    return kvLen1-kvLen2;
  }
}

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];
wmmhello's avatar
wmmhello 已提交
83 84
  SHashObj *tags;
  SHashObj *fields;
wmmhello's avatar
wmmhello 已提交
85 86 87 88
} SCreateSTableActionInfo;

typedef struct {
  char sTableName[TSDB_TABLE_NAME_LEN];
wmmhello's avatar
wmmhello 已提交
89
  SSmlKv * field;
wmmhello's avatar
wmmhello 已提交
90 91 92 93 94 95 96 97 98 99
} SAlterSTableActionInfo;

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

wmmhello's avatar
wmmhello 已提交
100 101 102 103
static int32_t buildSmlChildTableName(TAOS_SML_DATA_POINT_TAGS *tags) {
  int32_t size = taosArrayGetSize(tags->tags);
  ASSERT(size > 0);
  qsort(tags->tags, size, POINTER_BYTES, compareSmlColKv);
wmmhello's avatar
wmmhello 已提交
104

wmmhello's avatar
wmmhello 已提交
105 106 107 108 109 110
  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 已提交
111 112 113 114 115 116 117 118 119
  }
  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);
  uint64_t digest2 = *(uint64_t*)(context.digest + 8);
wmmhello's avatar
wmmhello 已提交
120
  snprintf(tags->childTableName, TSDB_TABLE_NAME_LEN, "t_%016"PRIx64"%016"PRIx64, digest1, digest2);
wmmhello's avatar
wmmhello 已提交
121
  taosStringBuilderDestroy(&sb);
wmmhello's avatar
wmmhello 已提交
122 123
  tags->uid = digest1;
  uDebug("SML: child table name: %s", tags->childTableName);
wmmhello's avatar
wmmhello 已提交
124 125 126 127 128
  return 0;
}

static int32_t generateSchemaAction(SSchema* pointColField, SHashObj* dbAttrHash, SArray* dbAttrArray, bool isTag, char sTableName[],
                                       SSchemaAction* action, bool* actionNeeded, SSmlLinesInfo* info) {
wmmhello's avatar
wmmhello 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
//  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 已提交
168 169 170
  return 0;
}

wmmhello's avatar
wmmhello 已提交
171
static int32_t buildColumnDescription(SSmlKv* field, char* buf, int32_t bufSize, int32_t* outBytes) {
wmmhello's avatar
wmmhello 已提交
172
  uint8_t type = field->type;
wmmhello's avatar
wmmhello 已提交
173 174
  char    tname[TSDB_TABLE_NAME_LEN] = {0};
  memcpy(tname, field->key, field->keyLen);
wmmhello's avatar
wmmhello 已提交
175
  if (type == TSDB_DATA_TYPE_BINARY || type == TSDB_DATA_TYPE_NCHAR) {
wmmhello's avatar
wmmhello 已提交
176
    int32_t bytes = field->valueLen;   // todo
wmmhello's avatar
wmmhello 已提交
177
    int out = snprintf(buf, bufSize,"%s %s(%d)",
wmmhello's avatar
wmmhello 已提交
178
                       tname,tDataTypes[field->type].name, bytes);
wmmhello's avatar
wmmhello 已提交
179 180
    *outBytes = out;
  } else {
wmmhello's avatar
wmmhello 已提交
181
    int out = snprintf(buf, bufSize, "%s %s", tname, tDataTypes[type].name);
wmmhello's avatar
wmmhello 已提交
182 183 184 185 186 187 188 189 190
    *outBytes = out;
  }

  return 0;
}

static int32_t applySchemaAction(TAOS* taos, SSchemaAction* action, SSmlLinesInfo* info) {
  int32_t code = 0;
  int32_t outBytes = 0;
wmmhello's avatar
wmmhello 已提交
191
  char *result = (char *)taosMemoryCalloc(1, tsMaxSQLStringLen+1);
wmmhello's avatar
wmmhello 已提交
192 193 194 195 196 197 198 199 200
  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);
      buildColumnDescription(action->alterSTable.field, result+n, capacity-n, &outBytes);
      TAOS_RES* res = taos_query(taos, result); //TODO async doAsyncQuery
      code = taos_errno(res);
wmmhello's avatar
wmmhello 已提交
201
      const char* errStr = taos_errstr(res);
wmmhello's avatar
wmmhello 已提交
202 203 204 205 206 207 208
      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 已提交
209 210
//      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 已提交
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
        TAOS_RES* res2 = taos_query(taos, "RESET QUERY CACHE");
        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);
      buildColumnDescription(action->alterSTable.field,
                             result+n, capacity-n, &outBytes);
      TAOS_RES* res = taos_query(taos, result); //TODO async doAsyncQuery
      code = taos_errno(res);
wmmhello's avatar
wmmhello 已提交
227
      const char* errStr = taos_errstr(res);
wmmhello's avatar
wmmhello 已提交
228 229 230 231 232 233 234
      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 已提交
235 236
//      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 已提交
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
        TAOS_RES* res2 = taos_query(taos, "RESET QUERY CACHE");
        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);
      buildColumnDescription(action->alterSTable.field, result+n,
                             capacity-n, &outBytes);
      TAOS_RES* res = taos_query(taos, result); //TODO async doAsyncQuery
      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 已提交
258 259
//      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 已提交
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
        TAOS_RES* res2 = taos_query(taos, "RESET QUERY CACHE");
        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);
      buildColumnDescription(action->alterSTable.field, result+n,
                             capacity-n, &outBytes);
      TAOS_RES* res = taos_query(taos, result); //TODO async doAsyncQuery
      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 已提交
281 282
//      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 已提交
283 284 285 286 287 288 289 290 291 292 293 294 295
        TAOS_RES* res2 = taos_query(taos, "RESET QUERY CACHE");
        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 已提交
296

wmmhello's avatar
wmmhello 已提交
297
      SSmlKv **kv = taosHashIterate(action->createSTable.fields, NULL);
wmmhello's avatar
wmmhello 已提交
298 299
      while(kv){
        buildColumnDescription(*kv, pos, freeBytes, &outBytes);
wmmhello's avatar
wmmhello 已提交
300 301
        pos += outBytes; freeBytes -= outBytes;
        *pos = ','; ++pos; --freeBytes;
wmmhello's avatar
wmmhello 已提交
302
        kv = taosHashIterate(action->createSTable.fields, kv);
wmmhello's avatar
wmmhello 已提交
303 304 305 306 307 308
      }
      --pos; ++freeBytes;

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

wmmhello's avatar
wmmhello 已提交
309 310 311
      kv = taosHashIterate(action->createSTable.tags, NULL);
      while(kv){
        buildColumnDescription(*kv, pos, freeBytes, &outBytes);
wmmhello's avatar
wmmhello 已提交
312 313
        pos += outBytes; freeBytes -= outBytes;
        *pos = ','; ++pos; --freeBytes;
wmmhello's avatar
wmmhello 已提交
314
        kv = taosHashIterate(action->createSTable.tags, kv);
wmmhello's avatar
wmmhello 已提交
315 316 317 318 319 320 321 322 323 324
      }
      pos--; ++freeBytes;
      outBytes = snprintf(pos, freeBytes, ")");
      TAOS_RES* res = taos_query(taos, result);
      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 已提交
325
      if (code == TSDB_CODE_MND_STB_ALREADY_EXIST) {
wmmhello's avatar
wmmhello 已提交
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
        TAOS_RES* res2 = taos_query(taos, "RESET QUERY CACHE");
        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;
  }

  taosMemoryFree(result);
  if (code != 0) {
    uError("SML:0x%"PRIx64 " apply schema action failure. %s", info->id, tstrerror(code));
  }
  return code;
}

static int32_t modifyDBSchemas(TAOS* taos, SSmlLinesInfo* info) {
  int32_t code = 0;

  SSmlSTableMeta** tableMetaSml = taosHashIterate(info->superTables, NULL);
  while (tableMetaSml) {
    SSmlSTableMeta* cTablePoints = *tableMetaSml;

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

wmmhello's avatar
wmmhello 已提交
358 359 360 361 362
    size_t superTableLen = 0;
    void *superTable = taosHashGetKey(tableMetaSml, &superTableLen);
    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 已提交
363

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

wmmhello's avatar
wmmhello 已提交
366
    if (code == TSDB_CODE_TDB_INVALID_TABLE_ID) {
wmmhello's avatar
wmmhello 已提交
367 368
      SSchemaAction schemaAction = {0};
      schemaAction.action = SCHEMA_ACTION_CREATE_STABLE;
wmmhello's avatar
wmmhello 已提交
369 370 371
      memcpy(schemaAction.createSTable.sTableName, superTable, superTableLen);
      schemaAction.createSTable.tags = cTablePoints->tagHash;
      schemaAction.createSTable.fields = cTablePoints->fieldHash;
wmmhello's avatar
wmmhello 已提交
372
      applySchemaAction(taos, &schemaAction, info);
wmmhello's avatar
wmmhello 已提交
373
      code = catalogGetSTableMeta(info->pCatalog, info->taos->pAppInfo->pTransporter, &ep, &pName, &pTableMeta);
wmmhello's avatar
wmmhello 已提交
374
      if (code != 0) {
wmmhello's avatar
wmmhello 已提交
375
        uError("SML:0x%"PRIx64" reconcile point schema failed. can not create %s", info->id, schemaAction.createSTable.sTableName);
wmmhello's avatar
wmmhello 已提交
376 377
        return code;
      }
wmmhello's avatar
wmmhello 已提交
378
    }else if (code == TSDB_CODE_SUCCESS) {
wmmhello's avatar
wmmhello 已提交
379 380 381 382
    } else {
      uError("SML:0x%"PRIx64" load table meta error: %s", info->id, tstrerror(code));
      return code;
    }
wmmhello's avatar
wmmhello 已提交
383
    taosHashPut(info->metaHashObj, superTable, superTableLen, &pTableMeta, POINTER_BYTES);
wmmhello's avatar
wmmhello 已提交
384

wmmhello's avatar
wmmhello 已提交
385
    tableMetaSml = taosHashIterate(info->superTables, tableMetaSml);
wmmhello's avatar
wmmhello 已提交
386 387 388 389
  }
  return 0;
}

wmmhello's avatar
wmmhello 已提交
390
static int32_t applyDataPoints(SSmlLinesInfo* info) {
wmmhello's avatar
wmmhello 已提交
391 392
  int32_t code = TSDB_CODE_SUCCESS;

wmmhello's avatar
wmmhello 已提交
393 394 395
  TAOS_SML_DATA_POINT_TAGS** oneTable = taosHashIterate(info->childTables, NULL);
  while (oneTable) {
    TAOS_SML_DATA_POINT_TAGS* tableData = *oneTable;
wmmhello's avatar
wmmhello 已提交
396

wmmhello's avatar
wmmhello 已提交
397 398 399 400 401 402 403
    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;
    catalogGetTableHashVgroup(info->pCatalog, info->taos->pAppInfo->pTransporter, &ep, &pName, &vg);
    taosHashPut(info->pVgHash, (const char*)&vg.vgId, sizeof(vg.vgId), (char*)&vg, sizeof(vg));
wmmhello's avatar
wmmhello 已提交
404

wmmhello's avatar
wmmhello 已提交
405 406 407
    STableMeta** pMeta = taosHashGet(info->metaHashObj, tableData->sTableName, tableData->sTableNameLen);
    ASSERT (NULL != pMeta && NULL != *pMeta);
    (*pMeta)->vgId = vg.vgId;
wmmhello's avatar
wmmhello 已提交
408
    (*pMeta)->uid = tableData->uid; // one table merge data block together according uid
wmmhello's avatar
wmmhello 已提交
409

wmmhello's avatar
wmmhello 已提交
410 411 412 413
    code = smlBind(info->exec, tableData->tags, tableData->cols, *pMeta, info->msgBuf.buf, info->msgBuf.len);
    if(code != TSDB_CODE_SUCCESS){
      return code;
    }
wmmhello's avatar
wmmhello 已提交
414
    oneTable = taosHashIterate(info->childTables, oneTable);
wmmhello's avatar
wmmhello 已提交
415 416
  }

wmmhello's avatar
wmmhello 已提交
417 418
  smlBuildOutput(info->exec, info->pVgHash);
  launchQueryImpl(info->pRequest, info->pQuery, TSDB_CODE_SUCCESS, true);
wmmhello's avatar
wmmhello 已提交
419

wmmhello's avatar
wmmhello 已提交
420
  info->affectedRows = taos_affected_rows(info->pRequest);
wmmhello's avatar
wmmhello 已提交
421
  return info->pRequest->code;
wmmhello's avatar
wmmhello 已提交
422 423
}

wmmhello's avatar
wmmhello 已提交
424
int smlInsert(TAOS* taos, SSmlLinesInfo* info) {
wmmhello's avatar
wmmhello 已提交
425 426 427
  uDebug("SML:0x%"PRIx64" taos_sml_insert. number of super tables: %d", info->id, taosHashGetSize(info->superTables));

  uDebug("SML:0x%"PRIx64" modify db schemas", info->id);
wmmhello's avatar
wmmhello 已提交
428
  int32_t code = modifyDBSchemas(taos, info);
wmmhello's avatar
wmmhello 已提交
429 430
  if (code != 0) {
    uError("SML:0x%"PRIx64" error change db schema : %s", info->id, tstrerror(code));
wmmhello's avatar
wmmhello 已提交
431
    return code;
wmmhello's avatar
wmmhello 已提交
432 433 434
  }

  uDebug("SML:0x%"PRIx64" apply data points", info->id);
wmmhello's avatar
wmmhello 已提交
435
  code = applyDataPoints(info);
wmmhello's avatar
wmmhello 已提交
436 437
  if (code != 0) {
    uError("SML:0x%"PRIx64" error apply data points : %s", info->id, tstrerror(code));
wmmhello's avatar
wmmhello 已提交
438
    return code;
wmmhello's avatar
wmmhello 已提交
439 440
  }

wmmhello's avatar
wmmhello 已提交
441
  return TSDB_CODE_SUCCESS;
wmmhello's avatar
wmmhello 已提交
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 482 483 484 485 486 487 488 489 490 491 492 493
}

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

/*        Field                          Escape charaters
    1: measurement                        Comma,Space
    2: tag_key, tag_value, field_key  Comma,Equal Sign,Space
    3: field_value                    Double quote,Backslash
*/
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 已提交
494 495 496
static bool parseTinyInt(SSmlKv *kvVal, bool *isValid, SMsgBuf *msg) {
  const char *pVal = kvVal->value;
  int32_t len = kvVal->valueLen;
wmmhello's avatar
wmmhello 已提交
497 498 499
  if (len <= 2) {
    return false;
  }
wmmhello's avatar
wmmhello 已提交
500 501 502 503 504 505 506 507 508 509 510 511 512 513
  const char *signalPos = pVal + len - 2;
  if (!strcasecmp(signalPos, "i8")) {
    char *endptr = NULL;
    int64_t result = strtoll(pVal, &endptr, 10);
    if(endptr != signalPos){       // 78ri8
      *isValid = false;
      buildInvalidDataMsg(msg, "invalid tiny int", endptr);
    }else if(!IS_VALID_TINYINT(result)){
      *isValid = false;
      buildInvalidDataMsg(msg, "tiny int out of range[-128,127]", endptr);
    }else{
      kvVal->i = result;
      *isValid = true;
    }
wmmhello's avatar
wmmhello 已提交
514 515 516 517 518
    return true;
  }
  return false;
}

wmmhello's avatar
wmmhello 已提交
519 520 521
static bool parseTinyUint(SSmlKv *kvVal, bool *isValid, SMsgBuf *msg) {
  const char *pVal = kvVal->value;
  int32_t len = kvVal->valueLen;
wmmhello's avatar
wmmhello 已提交
522 523 524 525 526 527
  if (len <= 2) {
    return false;
  }
  if (pVal[0] == '-') {
    return false;
  }
wmmhello's avatar
wmmhello 已提交
528 529 530 531 532 533 534 535 536 537 538 539 540 541
  const char *signalPos = pVal + len - 2;
  if (!strcasecmp(signalPos, "u8")) {
    char *endptr = NULL;
    int64_t result = strtoll(pVal, &endptr, 10);
    if(endptr != signalPos){       // 78ri8
      *isValid = false;
      buildInvalidDataMsg(msg, "invalid unsigned tiny int", endptr);
    }else if(!IS_VALID_UTINYINT(result)){
      *isValid = false;
      buildInvalidDataMsg(msg, "unsigned tiny int out of range[0,255]", endptr);
    }else{
      kvVal->i = result;
      *isValid = true;
    }
wmmhello's avatar
wmmhello 已提交
542 543 544 545 546
    return true;
  }
  return false;
}

wmmhello's avatar
wmmhello 已提交
547 548 549
static bool parseSmallInt(SSmlKv *kvVal, bool *isValid, SMsgBuf *msg) {
  const char *pVal = kvVal->value;
  int32_t len = kvVal->valueLen;
wmmhello's avatar
wmmhello 已提交
550 551 552
  if (len <= 3) {
    return false;
  }
wmmhello's avatar
wmmhello 已提交
553 554 555 556 557 558 559 560 561 562 563 564 565 566
  const char *signalPos = pVal + len - 3;
  if (!strcasecmp(signalPos, "i16")) {
    char *endptr = NULL;
    int64_t result = strtoll(pVal, &endptr, 10);
    if(endptr != signalPos){       // 78ri8
      *isValid = false;
      buildInvalidDataMsg(msg, "invalid small int", endptr);
    }else if(!IS_VALID_SMALLINT(result)){
      *isValid = false;
      buildInvalidDataMsg(msg, "small int our of range[-32768,32767]", endptr);
    }else{
      kvVal->i = result;
      *isValid = true;
    }
wmmhello's avatar
wmmhello 已提交
567 568 569 570 571
    return true;
  }
  return false;
}

wmmhello's avatar
wmmhello 已提交
572 573 574
static bool parseSmallUint(SSmlKv *kvVal, bool *isValid, SMsgBuf *msg) {
  const char *pVal = kvVal->value;
  int32_t len = kvVal->valueLen;
wmmhello's avatar
wmmhello 已提交
575 576 577 578 579 580
  if (len <= 3) {
    return false;
  }
  if (pVal[0] == '-') {
    return false;
  }
wmmhello's avatar
wmmhello 已提交
581 582 583 584 585 586 587 588 589 590 591 592 593 594
  const char *signalPos = pVal + len - 3;
  if (strcasecmp(signalPos, "u16") == 0) {
    char *endptr = NULL;
    int64_t result = strtoll(pVal, &endptr, 10);
    if(endptr != signalPos){       // 78ri8
      *isValid = false;
      buildInvalidDataMsg(msg, "invalid unsigned small int", endptr);
    }else if(!IS_VALID_USMALLINT(result)){
      *isValid = false;
      buildInvalidDataMsg(msg, "unsigned small int out of rang[0,65535]", endptr);
    }else{
      kvVal->i = result;
      *isValid = true;
    }
wmmhello's avatar
wmmhello 已提交
595 596 597 598 599
    return true;
  }
  return false;
}

wmmhello's avatar
wmmhello 已提交
600 601 602
static bool parseInt(SSmlKv *kvVal, bool *isValid, SMsgBuf *msg) {
  const char *pVal = kvVal->value;
  int32_t len = kvVal->valueLen;
wmmhello's avatar
wmmhello 已提交
603 604 605
  if (len <= 3) {
    return false;
  }
wmmhello's avatar
wmmhello 已提交
606 607 608 609 610 611 612 613 614 615 616 617 618 619
  const char *signalPos = pVal + len - 3;
  if (strcasecmp(signalPos, "i32") == 0) {
    char *endptr = NULL;
    int64_t result = strtoll(pVal, &endptr, 10);
    if(endptr != signalPos){       // 78ri8
      *isValid = false;
      buildInvalidDataMsg(msg, "invalid int", endptr);
    }else if(!IS_VALID_INT(result)){
      *isValid = false;
      buildInvalidDataMsg(msg, "int out of range[-2147483648,2147483647]", endptr);
    }else{
      kvVal->i = result;
      *isValid = true;
    }
wmmhello's avatar
wmmhello 已提交
620 621 622 623 624
    return true;
  }
  return false;
}

wmmhello's avatar
wmmhello 已提交
625 626 627
static bool parseUint(SSmlKv *kvVal, bool *isValid, SMsgBuf *msg) {
  const char *pVal = kvVal->value;
  int32_t len = kvVal->valueLen;
wmmhello's avatar
wmmhello 已提交
628 629 630 631 632 633
  if (len <= 3) {
    return false;
  }
  if (pVal[0] == '-') {
    return false;
  }
wmmhello's avatar
wmmhello 已提交
634 635 636 637 638 639 640 641 642 643 644 645 646 647
  const char *signalPos = pVal + len - 3;
  if (strcasecmp(signalPos, "u32") == 0) {
    char *endptr = NULL;
    int64_t result = strtoll(pVal, &endptr, 10);
    if(endptr != signalPos){       // 78ri8
      *isValid = false;
      buildInvalidDataMsg(msg, "invalid unsigned int", endptr);
    }else if(!IS_VALID_UINT(result)){
      *isValid = false;
      buildInvalidDataMsg(msg, "unsigned int out of range[0,4294967295]", endptr);
    }else{
      kvVal->i = result;
      *isValid = true;
    }
wmmhello's avatar
wmmhello 已提交
648 649 650 651 652
    return true;
  }
  return false;
}

wmmhello's avatar
wmmhello 已提交
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
static bool parseBigInt(SSmlKv *kvVal, bool *isValid, SMsgBuf *msg) {
  const char *pVal = kvVal->value;
  int32_t len = kvVal->valueLen;
  if (len > 3 && strcasecmp(pVal + len - 3, "i64") == 0) {
    char *endptr = NULL;
    int64_t result = strtoll(pVal, &endptr, 10);
    if(endptr != pVal + len - 3){       // 78ri8
      *isValid = false;
    }else if(!IS_VALID_BIGINT(result)){
      *isValid = false;
    }else{
      kvVal->i = result;
      *isValid = true;
    }
    return true;
  }else if (len > 1 && pVal[len - 1] == 'i') {
    char *endptr = NULL;
    int64_t result = strtoll(pVal, &endptr, 10);
    if(endptr != pVal + len - 1){       // 78ri8
      *isValid = false;
      buildInvalidDataMsg(msg, "invalid big int", endptr);
    }else if(!IS_VALID_BIGINT(result)){
      *isValid = false;
      buildInvalidDataMsg(msg, "big int out of range[-9223372036854775808,9223372036854775807]", endptr);
    }else{
      kvVal->i = result;
      *isValid = true;
    }
wmmhello's avatar
wmmhello 已提交
681 682 683 684 685
    return true;
  }
  return false;
}

wmmhello's avatar
wmmhello 已提交
686 687 688
static bool parseBigUint(SSmlKv *kvVal, bool *isValid, SMsgBuf *msg) {
  const char *pVal = kvVal->value;
  int32_t len = kvVal->valueLen;
wmmhello's avatar
wmmhello 已提交
689 690 691 692 693 694
  if (len <= 3) {
    return false;
  }
  if (pVal[0] == '-') {
    return false;
  }
wmmhello's avatar
wmmhello 已提交
695 696 697 698 699 700 701 702 703 704 705 706 707 708
  const char *signalPos = pVal + len - 3;
  if (strcasecmp(signalPos, "u64") == 0) {
    char *endptr = NULL;
    uint64_t result = strtoull(pVal, &endptr, 10);
    if(endptr != signalPos){       // 78ri8
      *isValid = false;
      buildInvalidDataMsg(msg, "invalid unsigned big int", endptr);
    }else if(!IS_VALID_UBIGINT(result)){
      *isValid = false;
      buildInvalidDataMsg(msg, "unsigned big int out of range[0,18446744073709551615]", endptr);
    }else{
      kvVal->u = result;
      *isValid = true;
    }
wmmhello's avatar
wmmhello 已提交
709 710 711 712 713
    return true;
  }
  return false;
}

wmmhello's avatar
wmmhello 已提交
714 715 716 717 718 719 720 721 722
static bool parseFloat(SSmlKv *kvVal, bool *isValid, SMsgBuf *msg) {
  const char *pVal = kvVal->value;
  int32_t len = kvVal->valueLen;
  char *endptr = NULL;
  float result = strtof(pVal, &endptr);
  if(endptr == pVal + len && IS_VALID_FLOAT(result)){       // 78
    kvVal->f = result;
    *isValid = true;
    return true;
wmmhello's avatar
wmmhello 已提交
723
  }
wmmhello's avatar
wmmhello 已提交
724 725 726 727 728 729 730 731 732 733 734 735

  if (len > 3 && len <strcasecmp(pVal + len - 3, "f32") == 0) {
    if(endptr != pVal + len - 3){       // 78ri8
      *isValid = false;
      buildInvalidDataMsg(msg, "invalid float", endptr);
    }else if(!IS_VALID_FLOAT(result)){
      *isValid = false;
      buildInvalidDataMsg(msg, "float out of range[-3.402823466e+38,3.402823466e+38]", endptr);
    }else{
      kvVal->f = result;
      *isValid = true;
    }
wmmhello's avatar
wmmhello 已提交
736 737 738 739 740
    return true;
  }
  return false;
}

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

wmmhello's avatar
wmmhello 已提交
766 767 768 769
static bool parseBool(SSmlKv *kvVal) {
  const char *pVal = kvVal->value;
  int32_t len = kvVal->valueLen;
  if ((len == 1) && pVal[len - 1] == 't') {
wmmhello's avatar
wmmhello 已提交
770
    //printf("Type is bool(%c)\n", pVal[len - 1]);
wmmhello's avatar
wmmhello 已提交
771
    kvVal->i = true;
wmmhello's avatar
wmmhello 已提交
772 773 774
    return true;
  }

wmmhello's avatar
wmmhello 已提交
775
  if ((len == 1) && pVal[len - 1] == 'f') {
wmmhello's avatar
wmmhello 已提交
776
    //printf("Type is bool(%c)\n", pVal[len - 1]);
wmmhello's avatar
wmmhello 已提交
777
    kvVal->i = false;
wmmhello's avatar
wmmhello 已提交
778 779 780
    return true;
  }

wmmhello's avatar
wmmhello 已提交
781
  if((len == 4) && !strcasecmp(pVal, "true")) {
wmmhello's avatar
wmmhello 已提交
782
    //printf("Type is bool(%s)\n", &pVal[len - 4]);
wmmhello's avatar
wmmhello 已提交
783
    kvVal->i = true;
wmmhello's avatar
wmmhello 已提交
784 785
    return true;
  }
wmmhello's avatar
wmmhello 已提交
786
  if((len == 5) && !strcasecmp(pVal, "false")) {
wmmhello's avatar
wmmhello 已提交
787
    //printf("Type is bool(%s)\n", &pVal[len - 5]);
wmmhello's avatar
wmmhello 已提交
788
    kvVal->i = false;
wmmhello's avatar
wmmhello 已提交
789 790 791 792 793
    return true;
  }
  return false;
}

wmmhello's avatar
wmmhello 已提交
794
static bool isBinary(const char *pVal, uint16_t len) {
wmmhello's avatar
wmmhello 已提交
795 796 797 798 799 800 801 802 803 804 805 806
  //binary: "abc"
  if (len < 2) {
    return false;
  }
  //binary
  if (pVal[0] == '"' && pVal[len - 1] == '"') {
    //printf("Type is binary(%s)\n", pVal);
    return true;
  }
  return false;
}

wmmhello's avatar
wmmhello 已提交
807
static bool isNchar(const char *pVal, uint16_t len) {
wmmhello's avatar
wmmhello 已提交
808 809 810 811 812 813 814 815 816 817 818
  //nchar: L"abc"
  if (len < 3) {
    return false;
  }
  if ((pVal[0] == 'l' || pVal[0] == 'L')&& pVal[1] == '"' && pVal[len - 1] == '"') {
    //printf("Type is nchar(%s)\n", pVal);
    return true;
  }
  return false;
}

wmmhello's avatar
wmmhello 已提交
819 820 821 822 823 824 825 826
static bool convertSmlValue(SSmlKv *pVal, SMsgBuf *msg) {
  // put high probability matching type first
  bool isValid = false;
  if (parseFloat(pVal, &isValid, msg)) {
    if(!isValid) return false;
    pVal->type = TSDB_DATA_TYPE_FLOAT;
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
    return true;
wmmhello's avatar
wmmhello 已提交
827
  }
wmmhello's avatar
wmmhello 已提交
828 829 830 831 832 833 834
  //binary
  if (isBinary(pVal->value, pVal->valueLen)) {
    pVal->type = TSDB_DATA_TYPE_BINARY;
    pVal->length = pVal->valueLen - 2;
    pVal->valueLen -= 2;
    pVal->value = pVal->value++;
    return true;
wmmhello's avatar
wmmhello 已提交
835
  }
wmmhello's avatar
wmmhello 已提交
836 837
  //nchar
  if (isNchar(pVal->value, pVal->valueLen)) {
wmmhello's avatar
wmmhello 已提交
838
    pVal->type = TSDB_DATA_TYPE_NCHAR;
wmmhello's avatar
wmmhello 已提交
839 840
    pVal->length = pVal->valueLen - 3;
    pVal->value = pVal->value+2;
wmmhello's avatar
wmmhello 已提交
841 842
    return true;
  }
wmmhello's avatar
wmmhello 已提交
843 844 845 846
  if (parseDouble(pVal, &isValid, msg)) {
    if(!isValid) return false;
    pVal->type = TSDB_DATA_TYPE_DOUBLE;
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
wmmhello's avatar
wmmhello 已提交
847

wmmhello's avatar
wmmhello 已提交
848 849 850 851 852
    return true;
  }
  //bool
  if (parseBool(pVal)) {
    pVal->type = TSDB_DATA_TYPE_BOOL;
wmmhello's avatar
wmmhello 已提交
853 854 855
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
    return true;
  }
wmmhello's avatar
wmmhello 已提交
856 857 858

  if (parseTinyInt(pVal, &isValid, msg)) {
    if(!isValid) return false;
wmmhello's avatar
wmmhello 已提交
859 860 861 862
    pVal->type = TSDB_DATA_TYPE_TINYINT;
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
    return true;
  }
wmmhello's avatar
wmmhello 已提交
863 864
  if (parseTinyUint(pVal, &isValid, msg)) {
    if(!isValid) return false;
wmmhello's avatar
wmmhello 已提交
865 866 867 868
    pVal->type = TSDB_DATA_TYPE_UTINYINT;
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
    return true;
  }
wmmhello's avatar
wmmhello 已提交
869 870
  if (parseSmallInt(pVal, &isValid, msg)) {
    if(!isValid) return false;
wmmhello's avatar
wmmhello 已提交
871 872 873 874
    pVal->type = TSDB_DATA_TYPE_SMALLINT;
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
    return true;
  }
wmmhello's avatar
wmmhello 已提交
875 876
  if (parseSmallUint(pVal, &isValid, msg)) {
    if(!isValid) return false;
wmmhello's avatar
wmmhello 已提交
877 878 879 880
    pVal->type = TSDB_DATA_TYPE_USMALLINT;
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
    return true;
  }
wmmhello's avatar
wmmhello 已提交
881 882
  if (parseInt(pVal, &isValid, msg)) {
    if(!isValid) return false;
wmmhello's avatar
wmmhello 已提交
883 884 885 886
    pVal->type = TSDB_DATA_TYPE_INT;
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
    return true;
  }
wmmhello's avatar
wmmhello 已提交
887 888
  if (parseUint(pVal, &isValid, msg)) {
    if(!isValid) return false;
wmmhello's avatar
wmmhello 已提交
889 890 891 892
    pVal->type = TSDB_DATA_TYPE_UINT;
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
    return true;
  }
wmmhello's avatar
wmmhello 已提交
893 894
  if (parseBigInt(pVal, &isValid, msg)) {
    if(!isValid) return false;
wmmhello's avatar
wmmhello 已提交
895 896 897 898
    pVal->type = TSDB_DATA_TYPE_BIGINT;
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
    return true;
  }
wmmhello's avatar
wmmhello 已提交
899 900
  if (parseBigUint(pVal, &isValid, msg)) {
    if(!isValid) return false;
wmmhello's avatar
wmmhello 已提交
901 902 903 904 905
    pVal->type = TSDB_DATA_TYPE_UBIGINT;
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
    return true;
  }

wmmhello's avatar
wmmhello 已提交
906
  buildInvalidDataMsg(msg, "invalid data", pVal->value);
wmmhello's avatar
wmmhello 已提交
907 908 909
  return false;
}

wmmhello's avatar
wmmhello 已提交
910 911 912 913 914 915
bool checkDuplicateKey(char *key, SHashObj *pHash, SSmlLinesInfo* info) {
  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 已提交
916 917
  }

wmmhello's avatar
wmmhello 已提交
918 919
  uint8_t dummy_val = 0;
  taosHashPut(pHash, key, strlen(key), &dummy_val, sizeof(uint8_t));
wmmhello's avatar
wmmhello 已提交
920

wmmhello's avatar
wmmhello 已提交
921
  return false;
wmmhello's avatar
wmmhello 已提交
922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967
}

int32_t parseSml(const char* sql, TAOS_PARSE_ELEMENTS *elements){
  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++;
  }
  if (!elements->measure || *sql == COMMA) return TSDB_CODE_SML_INVALID_DATA;

  // 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++;
  }
  if(elements->measureLen == 0) return TSDB_CODE_SML_INVALID_DATA;

  // parse cols
  while (*sql != '\0') {
    if(*sql != SPACE) {
      elements->cols = sql;
      break;
    }
    sql++;
  }
  if(!elements->cols) return TSDB_CODE_SML_INVALID_DATA;

wmmhello's avatar
wmmhello 已提交
968
  bool isInQuote = false;
wmmhello's avatar
wmmhello 已提交
969
  while (*sql != '\0') {
wmmhello's avatar
wmmhello 已提交
970 971 972 973
    if(*sql == QUOTE && *(sql - 1) != SLASH){
      isInQuote = !isInQuote;
    }
    if(!isInQuote && *sql == SPACE && *(sql - 1) != SLASH) {
wmmhello's avatar
wmmhello 已提交
974 975 976 977
      break;
    }
    sql++;
  }
wmmhello's avatar
wmmhello 已提交
978
  elements->colsLen = sql - elements->cols;
wmmhello's avatar
wmmhello 已提交
979

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

  return TSDB_CODE_SUCCESS;
}

wmmhello's avatar
wmmhello 已提交
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
bool parseSmlCols(const char* data, int32_t len, SArray *cols, bool isTag, SMsgBuf *msg){
  if(isTag && len == 0){
    SSmlKv *kv = taosMemoryCalloc(sizeof(SSmlKv), 1);
    kv->key = TAG;
    kv->keyLen = TAGNAMELEN;
    kv->value = TAG;
    kv->valueLen = TAGNAMELEN;
    kv->type = TSDB_DATA_TYPE_NCHAR;
    if(cols) taosArrayPush(cols, &kv);
    return true;
  }

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

wmmhello's avatar
wmmhello 已提交
1023
    // parse value
wmmhello's avatar
wmmhello 已提交
1024 1025 1026 1027 1028 1029 1030 1031
    i++;
    const char *value = data + i;
    while(i < len){
      if(data[i] == COMMA && i > 0 && data[i-1] != SLASH){
        break;
      }
      i++;
    }
wmmhello's avatar
wmmhello 已提交
1032
    int32_t valueLen = data + i - value;
wmmhello's avatar
wmmhello 已提交
1033
    if(valueLen == 0){
wmmhello's avatar
wmmhello 已提交
1034
      buildInvalidDataMsg(msg, "invalid value", value);
wmmhello's avatar
wmmhello 已提交
1035 1036
      return TSDB_CODE_SML_INVALID_DATA;
    }
wmmhello's avatar
wmmhello 已提交
1037 1038

    // add kv to SSmlKv
wmmhello's avatar
wmmhello 已提交
1039
    SSmlKv *kv = taosMemoryCalloc(sizeof(SSmlKv), 1);
wmmhello's avatar
wmmhello 已提交
1040 1041 1042 1043
    kv->key = key;
    kv->keyLen = keyLen;
    kv->value = value;
    kv->valueLen = valueLen;
wmmhello's avatar
wmmhello 已提交
1044 1045
    if(isTag){
      kv->type = TSDB_DATA_TYPE_NCHAR;
wmmhello's avatar
wmmhello 已提交
1046 1047 1048 1049
    }else{
      if(!convertSmlValue(kv, msg)){
        return TSDB_CODE_SML_INVALID_DATA;
      }
wmmhello's avatar
wmmhello 已提交
1050
    }
wmmhello's avatar
wmmhello 已提交
1051

wmmhello's avatar
wmmhello 已提交
1052 1053
    if(cols) taosArrayPush(cols, &kv);
  }
wmmhello's avatar
wmmhello 已提交
1054

wmmhello's avatar
wmmhello 已提交
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
  return TSDB_CODE_SUCCESS;
}

static int64_t getTimeStampValue(const char *value, int32_t type) {
  double ts = (double)strtoll(value, NULL, 10);
  switch (type) {
    case TSDB_TIME_PRECISION_HOURS:
      ts *= (3600 * 1e9);
    case TSDB_TIME_PRECISION_MINUTES:
      ts *= (60 * 1e9);
    case TSDB_TIME_PRECISION_SECONDS:
      ts *= (1e9);
    case TSDB_TIME_PRECISION_MICRO:
      ts *= (1e6);
    case TSDB_TIME_PRECISION_MILLI:
      ts *= (1e3);
    default:
      break;
  }
  if(ts > (double)INT64_MAX || ts < 0){
    return -1;
  }else{
    return (int64_t)ts;
  }
}

static int64_t getTimeStampNow(int32_t precision) {
  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;
    default:
      return taosGetTimestamp(precision);
  }
}

wmmhello's avatar
wmmhello 已提交
1095
static bool isValidateTimeStamp(const char *pVal, int32_t len) {
wmmhello's avatar
wmmhello 已提交
1096 1097
  for (int i = 0; i < len; ++i) {
    if (!isdigit(pVal[i])) {
wmmhello's avatar
wmmhello 已提交
1098
      return false;
wmmhello's avatar
wmmhello 已提交
1099
    }
wmmhello's avatar
wmmhello 已提交
1100
  }
wmmhello's avatar
wmmhello 已提交
1101
  return true;
wmmhello's avatar
wmmhello 已提交
1102 1103
}

wmmhello's avatar
wmmhello 已提交
1104 1105 1106 1107 1108 1109
static int32_t getTsType(int32_t len) {
  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 已提交
1110
    return -1;
wmmhello's avatar
wmmhello 已提交
1111 1112 1113
  }
}

wmmhello's avatar
wmmhello 已提交
1114 1115
static int32_t parseSmlTS(const char* data, int32_t len, SArray *tags, SSmlLinesInfo* info){
  int64_t ts = 0;
wmmhello's avatar
wmmhello 已提交
1116
  if(data == NULL){
wmmhello's avatar
wmmhello 已提交
1117 1118 1119
    if(info->protocol != TSDB_SML_LINE_PROTOCOL){
      buildInvalidDataMsg(&info->msgBuf, "timestamp can not be null", NULL);
      return TSDB_CODE_TSC_INVALID_TIME_STAMP;
wmmhello's avatar
wmmhello 已提交
1120
    }
wmmhello's avatar
wmmhello 已提交
1121
    ts = getTimeStampNow(info->tsType);
wmmhello's avatar
wmmhello 已提交
1122 1123 1124
  }else{
    int ret = isValidateTimeStamp(data, len);
    if(!ret){
wmmhello's avatar
wmmhello 已提交
1125 1126
      buildInvalidDataMsg(&info->msgBuf, "timestamp must be digit", data);
      return TSDB_CODE_TSC_INVALID_TIME_STAMP;
wmmhello's avatar
wmmhello 已提交
1127
    }
wmmhello's avatar
wmmhello 已提交
1128 1129
    int32_t tsType = -1;
    if(info->protocol != TSDB_SML_LINE_PROTOCOL){
wmmhello's avatar
wmmhello 已提交
1130
      tsType = getTsType(len);
wmmhello's avatar
wmmhello 已提交
1131 1132 1133
      if (tsType == -1) {
        buildInvalidDataMsg(&info->msgBuf, "timestamp precision can only be seconds(10 digits) or milli seconds(13 digits)", data);
        return TSDB_CODE_TSC_INVALID_TIME_STAMP;
wmmhello's avatar
wmmhello 已提交
1134
      }
wmmhello's avatar
wmmhello 已提交
1135 1136
    }else{
      tsType = info->tsType;
wmmhello's avatar
wmmhello 已提交
1137
    }
wmmhello's avatar
wmmhello 已提交
1138 1139 1140 1141
    ts = getTimeStampValue(data, tsType);
    if(ts == -1){
      buildInvalidDataMsg(&info->msgBuf, "invalid timestamp", data);
      return TSDB_CODE_TSC_INVALID_TIME_STAMP;
wmmhello's avatar
wmmhello 已提交
1142 1143 1144 1145
    }
  }

  SSmlKv *kv = taosMemoryCalloc(sizeof(SSmlKv), 1);
wmmhello's avatar
wmmhello 已提交
1146 1147 1148 1149 1150 1151 1152
  if(!kv){
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  kv->key = TS;
  kv->keyLen = TSNAMELEN;
  kv->i = ts;
wmmhello's avatar
wmmhello 已提交
1153
  kv->type = TSDB_DATA_TYPE_TIMESTAMP;
wmmhello's avatar
wmmhello 已提交
1154
  kv->length = (int16_t)tDataTypes[kv->type].bytes;
wmmhello's avatar
wmmhello 已提交
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 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
  if(tags) taosArrayPush(tags, &kv);
  return TSDB_CODE_SUCCESS;
}

//int32_t parseSmlCols(const char* data, SArray *cols){
//  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 已提交
1202
bool updateMeta(SSmlSTableMeta* tableMeta, SArray *tags, SArray *cols, SMsgBuf *msg){
wmmhello's avatar
wmmhello 已提交
1203 1204
  if(tags){
    for (int i = 0; i < taosArrayGetSize(tags); ++i) {
wmmhello's avatar
wmmhello 已提交
1205
      SSmlKv *kv = taosArrayGetP(tags, i);
wmmhello's avatar
wmmhello 已提交
1206 1207
      ASSERT(kv->type == TSDB_DATA_TYPE_NCHAR);

wmmhello's avatar
wmmhello 已提交
1208
      SSmlKv **value = taosHashGet(tableMeta->tagHash, kv->key, kv->keyLen);
wmmhello's avatar
wmmhello 已提交
1209
      if(value){
wmmhello's avatar
wmmhello 已提交
1210 1211 1212
        ASSERT((*value)->type == TSDB_DATA_TYPE_NCHAR);
        if(kv->valueLen > (*value)->valueLen){    // tags type is nchar
          *value = kv;
wmmhello's avatar
wmmhello 已提交
1213 1214 1215 1216 1217 1218 1219 1220 1221
        }
      }else{
        taosHashPut(tableMeta->tagHash, kv->key, kv->keyLen, &kv, POINTER_BYTES);
      }
    }
  }

  if(cols){
    for (int i = 1; i < taosArrayGetSize(cols); ++i) {  //jump timestamp
wmmhello's avatar
wmmhello 已提交
1222 1223
      SSmlKv *kv = taosArrayGetP(cols, i);
      SSmlKv **value = taosHashGet(tableMeta->fieldHash, kv->key, kv->keyLen);
wmmhello's avatar
wmmhello 已提交
1224 1225
      if(value){
        if(kv->type != (*value)->type){
wmmhello's avatar
wmmhello 已提交
1226 1227 1228 1229 1230 1231 1232 1233
          buildInvalidDataMsg(msg, "the type is not the same like before", kv->key);
          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 已提交
1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244
        }
      }else{
        taosHashPut(tableMeta->fieldHash, kv->key, kv->keyLen, &kv, POINTER_BYTES);
      }
    }
  }
}

void insertMeta(SSmlSTableMeta* tableMeta, SArray *tags, SArray *cols){
  if(tags){
    for (int i = 0; i < taosArrayGetSize(tags); ++i) {
wmmhello's avatar
wmmhello 已提交
1245
      SSmlKv *kv = taosArrayGetP(tags, i);
wmmhello's avatar
wmmhello 已提交
1246 1247 1248 1249 1250 1251
      taosHashPut(tableMeta->tagHash, kv->key, kv->keyLen, &kv, POINTER_BYTES);
    }
  }

  if(cols){
    for (int i = 0; i < taosArrayGetSize(cols); ++i) {
wmmhello's avatar
wmmhello 已提交
1252
      SSmlKv *kv = taosArrayGetP(cols, i);
wmmhello's avatar
wmmhello 已提交
1253 1254 1255 1256 1257
      taosHashPut(tableMeta->fieldHash, kv->key, kv->keyLen, &kv, POINTER_BYTES);
    }
  }
}

wmmhello's avatar
wmmhello 已提交
1258
static int32_t smlParseLine(const char* sql, SSmlLinesInfo* info) {
wmmhello's avatar
wmmhello 已提交
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
  TAOS_PARSE_ELEMENTS elements = {0};
  int ret = parseSml(sql, &elements);
  if(ret != TSDB_CODE_SUCCESS){
    return ret;
  }

  SArray *cols = taosArrayInit(16, POINTER_BYTES);
  if (cols == NULL) {
    uError("SML:0x%"PRIx64" taos_insert_lines failed to allocate memory", info->id);
    return TSDB_CODE_TSC_OUT_OF_MEMORY;
  }
wmmhello's avatar
wmmhello 已提交
1270

wmmhello's avatar
wmmhello 已提交
1271 1272 1273 1274 1275
  ret = parseSmlTS(elements.timestamp, elements.timestampLen, cols, info);
  if(ret != TSDB_CODE_SUCCESS){
    return ret;
  }
  ret = parseSmlCols(elements.cols, elements.colsLen, cols, false, &info->msgBuf);
wmmhello's avatar
wmmhello 已提交
1276 1277 1278
  if(ret != TSDB_CODE_SUCCESS){
    return ret;
  }
wmmhello's avatar
wmmhello 已提交
1279 1280 1281 1282
  if(taosArrayGetSize(cols) > TSDB_MAX_COLUMNS){
    buildInvalidDataMsg(&info->msgBuf, "too many columns than 4096", NULL);
    return TSDB_CODE_SML_INVALID_DATA;
  }
wmmhello's avatar
wmmhello 已提交
1283 1284 1285 1286 1287

  TAOS_SML_DATA_POINT_TAGS** oneTable = taosHashGet(info->childTables, elements.measure, elements.measureTagsLen);
  if(oneTable){
    SSmlSTableMeta** tableMeta = taosHashGet(info->superTables, elements.measure, elements.measureLen);
    ASSERT(tableMeta);
wmmhello's avatar
wmmhello 已提交
1288 1289 1290 1291
    ret = updateMeta(*tableMeta, NULL, cols, &info->msgBuf);    // update meta
    if(!ret){
      return TSDB_CODE_SML_INVALID_DATA;
    }
wmmhello's avatar
wmmhello 已提交
1292 1293 1294
    taosArrayPush((*oneTable)->cols, &cols);
  }else{
    TAOS_SML_DATA_POINT_TAGS *tag = taosMemoryCalloc(sizeof(TAOS_SML_DATA_POINT_TAGS), 1);
wmmhello's avatar
wmmhello 已提交
1295 1296 1297
    if(!tag){
      return TSDB_CODE_OUT_OF_MEMORY;
    }
wmmhello's avatar
wmmhello 已提交
1298 1299 1300 1301 1302 1303 1304
    tag->cols = taosArrayInit(16, POINTER_BYTES);
    if (tag->cols == NULL) {
      uError("SML:0x%"PRIx64" taos_insert_lines failed to allocate memory", info->id);
      return TSDB_CODE_TSC_OUT_OF_MEMORY;
    }
    taosArrayPush(tag->cols, &cols);

wmmhello's avatar
wmmhello 已提交
1305 1306 1307 1308 1309 1310
    tag->colsColumn = taosArrayInit(16, POINTER_BYTES);
    if (tag->cols == NULL) {
      uError("SML:0x%"PRIx64" taos_insert_lines failed to allocate memory", info->id);
      return TSDB_CODE_TSC_OUT_OF_MEMORY;
    }

wmmhello's avatar
wmmhello 已提交
1311 1312 1313 1314 1315
    tag->tags = taosArrayInit(16, POINTER_BYTES);
    if (tag->tags == NULL) {
      uError("SML:0x%"PRIx64" taos_insert_lines failed to allocate memory", info->id);
      return TSDB_CODE_TSC_OUT_OF_MEMORY;
    }
wmmhello's avatar
wmmhello 已提交
1316
    ret = parseSmlCols(elements.tags, elements.tagsLen, tag->tags, true, &info->msgBuf);
wmmhello's avatar
wmmhello 已提交
1317 1318 1319 1320
    if(ret != TSDB_CODE_SUCCESS){
      return ret;
    }

wmmhello's avatar
wmmhello 已提交
1321 1322 1323 1324 1325 1326 1327 1328 1329
    if(taosArrayGetSize(tag->tags) > TSDB_MAX_TAGS){
      buildInvalidDataMsg(&info->msgBuf, "too many tags than 128", NULL);
      return TSDB_CODE_SML_INVALID_DATA;
    }

    tag->sTableName = elements.measure;
    tag->sTableNameLen = elements.measureLen;
    buildSmlChildTableName(tag);

wmmhello's avatar
wmmhello 已提交
1330 1331
    SSmlSTableMeta** tableMeta = taosHashGet(info->superTables, elements.measure, elements.measureLen);
    if(tableMeta){  // update meta
wmmhello's avatar
wmmhello 已提交
1332 1333 1334 1335
      ret = updateMeta(*tableMeta, tag->tags, cols, &info->msgBuf);
      if(!ret){
        return TSDB_CODE_SML_INVALID_DATA;
      }
wmmhello's avatar
wmmhello 已提交
1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346
    }else{
      SSmlSTableMeta* meta = taosMemoryCalloc(sizeof(SSmlSTableMeta), 1);
      insertMeta(meta, tag->tags, cols);
      taosHashPut(info->superTables, elements.measure, elements.measureLen, &meta, POINTER_BYTES);
    }

    taosHashPut(info->childTables, elements.measure, elements.measureTagsLen, &tag, POINTER_BYTES);
  }
  return TSDB_CODE_SUCCESS;
}

wmmhello's avatar
wmmhello 已提交
1347 1348 1349 1350 1351 1352 1353 1354 1355
static void smlDestroyInfo(SSmlLinesInfo* info){
  if(!info) return;
  qDestroyQuery(info->pQuery);
  tscSmlDestroyHandle(info->exec);
  taosHashCleanup(info->childTables);
  taosHashCleanup(info->superTables);
  taosHashCleanup(info->metaHashObj);
  taosHashCleanup(info->pVgHash);
  taosMemoryFree(info);
wmmhello's avatar
wmmhello 已提交
1356
}
wmmhello's avatar
wmmhello 已提交
1357
static SSmlLinesInfo* smlBuildInfo(TAOS* taos, SRequestObj* request, SMLProtocolType protocol, int32_t tsType){
wmmhello's avatar
wmmhello 已提交
1358
  SSmlLinesInfo* info = taosMemoryMalloc(sizeof(SSmlLinesInfo));
wmmhello's avatar
wmmhello 已提交
1359 1360 1361
  if (NULL == info) {
    return NULL;
  }
wmmhello's avatar
wmmhello 已提交
1362 1363
  info->id = genLinesSmlId();
  info->tsType = tsType;
wmmhello's avatar
wmmhello 已提交
1364
  info->taos = taos;
wmmhello's avatar
wmmhello 已提交
1365 1366
  info->protocol = protocol;

wmmhello's avatar
wmmhello 已提交
1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381
  info->pQuery = taosMemoryCalloc(1, sizeof(SQuery));
  if (NULL == info->pQuery) {
    goto cleanup;
  }
  info->pQuery->execMode = QUERY_EXEC_MODE_SCHEDULE;
  info->pQuery->haveResultSet = false;
  info->pQuery->msgType = TDMT_VND_SUBMIT;
  info->pQuery->pRoot = (SNode*)nodesMakeNode(QUERY_NODE_VNODE_MODIF_STMT);
  ((SVnodeModifOpStmt*)(info->pQuery->pRoot))->payloadType = PAYLOAD_TYPE_KV;

  info->exec = tscSmlInitHandle(info->pQuery);

  int32_t code = catalogGetHandle(info->taos->pAppInfo->clusterId, &info->pCatalog);
  if(code != TSDB_CODE_SUCCESS){
    uError("SML:0x%"PRIx64" get catalog error %d", info->id, code);
wmmhello's avatar
wmmhello 已提交
1382 1383
    goto cleanup;
  }
wmmhello's avatar
wmmhello 已提交
1384
  info->pRequest = request;
wmmhello's avatar
wmmhello 已提交
1385 1386
  info->msgBuf.buf = info->pRequest->msgBuf;
  info->msgBuf.len = ERROR_MSG_BUF_DEFAULT_SIZE;
wmmhello's avatar
wmmhello 已提交
1387 1388 1389

  info->childTables = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, false);
  info->superTables = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, false);
wmmhello's avatar
wmmhello 已提交
1390 1391
  info->metaHashObj = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR), true, false);
  info->pVgHash     = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, false);
wmmhello's avatar
wmmhello 已提交
1392

wmmhello's avatar
wmmhello 已提交
1393
  return info;
wmmhello's avatar
wmmhello 已提交
1394

wmmhello's avatar
wmmhello 已提交
1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410
cleanup:
  smlDestroyInfo(info);
  return NULL;
}

int sml_insert_lines(TAOS* taos, SRequestObj* request, char* lines[], int numLines, SMLProtocolType protocol, int32_t tsType) {
  int32_t code = TSDB_CODE_SUCCESS;

  SSmlLinesInfo* info = smlBuildInfo(taos, request, protocol, tsType);
  if(!info){
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto cleanup;
  }
  if (numLines <= 0 || numLines > 65536) {
    uError("SML:0x%"PRIx64" taos_insert_lines numLines should be between 1 and 65536. numLines: %d", info->id, numLines);
    code = TSDB_CODE_TSC_APP_ERROR;
wmmhello's avatar
wmmhello 已提交
1411 1412
    goto cleanup;
  }
wmmhello's avatar
wmmhello 已提交
1413 1414 1415 1416 1417 1418 1419 1420
  for (int32_t i = 0; i < numLines; ++i) {
    code = smlParseLine(lines[i], info);
    if (code != TSDB_CODE_SUCCESS) {
      uError("SML:0x%"PRIx64" data point line parse failed. line %d : %s", info->id, i, lines[i]);
      goto cleanup;
    }
  }
  uDebug("SML:0x%"PRIx64" data point line parse success. tables %d", info->id, taosHashGetSize(info->childTables));
wmmhello's avatar
wmmhello 已提交
1421

wmmhello's avatar
wmmhello 已提交
1422 1423
  code = smlInsert(taos, info);
  if (code != TSDB_CODE_SUCCESS) {
wmmhello's avatar
wmmhello 已提交
1424 1425 1426 1427 1428 1429 1430
    uError("SML:0x%"PRIx64" taos_sml_insert error: %s", info->id, tstrerror((code)));
    goto cleanup;
  }

  uDebug("SML:0x%"PRIx64" taos_insert_lines finish inserting %d lines. code: %d", info->id, numLines, code);

cleanup:
wmmhello's avatar
wmmhello 已提交
1431
  smlDestroyInfo(info);
wmmhello's avatar
wmmhello 已提交
1432 1433 1434
  return code;
}

wmmhello's avatar
wmmhello 已提交
1435
static int32_t convertPrecisionType(int precision) {
wmmhello's avatar
wmmhello 已提交
1436 1437
  switch (precision) {
    case TSDB_SML_TIMESTAMP_HOURS:
wmmhello's avatar
wmmhello 已提交
1438
      return TSDB_TIME_PRECISION_HOURS;
wmmhello's avatar
wmmhello 已提交
1439
    case TSDB_SML_TIMESTAMP_MILLI_SECONDS:
wmmhello's avatar
wmmhello 已提交
1440
      return TSDB_TIME_PRECISION_MILLI;
wmmhello's avatar
wmmhello 已提交
1441
    case TSDB_SML_TIMESTAMP_NANO_SECONDS:
wmmhello's avatar
wmmhello 已提交
1442 1443
    case TSDB_SML_TIMESTAMP_NOT_CONFIGURED:
      return TSDB_TIME_PRECISION_NANO;
wmmhello's avatar
wmmhello 已提交
1444
    case TSDB_SML_TIMESTAMP_MICRO_SECONDS:
wmmhello's avatar
wmmhello 已提交
1445
      return TSDB_TIME_PRECISION_MICRO;
wmmhello's avatar
wmmhello 已提交
1446
    case TSDB_SML_TIMESTAMP_SECONDS:
wmmhello's avatar
wmmhello 已提交
1447
      return TSDB_TIME_PRECISION_SECONDS;
wmmhello's avatar
wmmhello 已提交
1448
    case TSDB_SML_TIMESTAMP_MINUTES:
wmmhello's avatar
wmmhello 已提交
1449
      return TSDB_TIME_PRECISION_MINUTES;
wmmhello's avatar
wmmhello 已提交
1450
    default:
wmmhello's avatar
wmmhello 已提交
1451
      return -1;
wmmhello's avatar
wmmhello 已提交
1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478
  }
}

/**
 * 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.
 *
 */

TAOS_RES* taos_schemaless_insert(TAOS* taos, char* lines[], int numLines, int protocol, int precision) {
  int code = TSDB_CODE_SUCCESS;

wmmhello's avatar
wmmhello 已提交
1479
  SRequestObj* request = createRequest(taos, NULL, NULL, TSDB_SQL_INSERT);
wmmhello's avatar
wmmhello 已提交
1480
  switch (protocol) {
wmmhello's avatar
wmmhello 已提交
1481
    case TSDB_SML_LINE_PROTOCOL:{
wmmhello's avatar
wmmhello 已提交
1482 1483 1484 1485 1486 1487 1488
      int32_t tsType = convertPrecisionType(precision);
      if(tsType == -1){
        request->code = TSDB_CODE_SML_INVALID_PRECISION_TYPE;
        goto end;
      }

      code = sml_insert_lines(taos, request, lines, numLines, protocol, tsType);
wmmhello's avatar
wmmhello 已提交
1489
      break;
wmmhello's avatar
wmmhello 已提交
1490
    }
wmmhello's avatar
wmmhello 已提交
1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501
    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:
      code = TSDB_CODE_SML_INVALID_PROTOCOL_TYPE;
      break;
  }

wmmhello's avatar
wmmhello 已提交
1502 1503
end:
  return (TAOS_RES*)request;
wmmhello's avatar
wmmhello 已提交
1504
}