clientSmlLine.c 21.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "clientSml.h"

// comma ,
X
Xiaoyu Wang 已提交
24
#define IS_COMMA(sql) (*(sql) == COMMA && *((sql)-1) != SLASH)
25
// space
X
Xiaoyu Wang 已提交
26
#define IS_SPACE(sql) (*(sql) == SPACE && *((sql)-1) != SLASH)
27
// equal =
X
Xiaoyu Wang 已提交
28
#define IS_EQUAL(sql) (*(sql) == EQUAL && *((sql)-1) != SLASH)
29
// quote "
30
// #define IS_QUOTE(sql) (*(sql) == QUOTE && *((sql)-1) != SLASH)
31 32
// SLASH

33
#define IS_SLASH_LETTER_IN_FIELD_VALUE(sql) (*((sql)-1) == SLASH && (*(sql) == QUOTE || *(sql) == SLASH))
34

35
#define IS_SLASH_LETTER_IN_TAG_FIELD_KEY(sql) \
wmmhello's avatar
wmmhello 已提交
36
  (*((sql)-1) == SLASH && (*(sql) == COMMA || *(sql) == SPACE || *(sql) == EQUAL))
37

38 39 40 41 42 43
#define PROCESS_SLASH_IN_FIELD_VALUE(key, keyLen)  \
  for (int i = 1; i < keyLen; ++i) {               \
    if (IS_SLASH_LETTER_IN_FIELD_VALUE(key + i)) { \
      MOVE_FORWARD_ONE(key + i, keyLen - i);       \
      keyLen--;                                    \
    }                                              \
wmmhello's avatar
wmmhello 已提交
44 45
  }

46 47 48 49 50 51
#define PROCESS_SLASH_IN_TAG_FIELD_KEY(key, keyLen)  \
  for (int i = 1; i < keyLen; ++i) {                 \
    if (IS_SLASH_LETTER_IN_TAG_FIELD_KEY(key + i)) { \
      MOVE_FORWARD_ONE(key + i, keyLen - i);         \
      keyLen--;                                      \
    }                                                \
52 53 54 55 56
  }

#define BINARY_ADD_LEN 2  // "binary"   2 means " "
#define NCHAR_ADD_LEN  3  // L"nchar"   3 means L" "

X
Xiaoyu Wang 已提交
57
uint8_t smlPrecisionConvert[7] = {TSDB_TIME_PRECISION_NANO,    TSDB_TIME_PRECISION_HOURS, TSDB_TIME_PRECISION_MINUTES,
58 59 60 61 62 63
                                  TSDB_TIME_PRECISION_SECONDS, TSDB_TIME_PRECISION_MILLI, TSDB_TIME_PRECISION_MICRO,
                                  TSDB_TIME_PRECISION_NANO};

static int64_t smlParseInfluxTime(SSmlHandle *info, const char *data, int32_t len) {
  uint8_t toPrecision = info->currSTableMeta ? info->currSTableMeta->tableInfo.precision : TSDB_TIME_PRECISION_NANO;

X
Xiaoyu Wang 已提交
64 65
  if (unlikely(len == 0 || (len == 1 && data[0] == '0'))) {
    return taosGetTimestampNs() / smlFactorNS[toPrecision];
66 67 68 69 70 71 72 73 74 75 76 77
  }

  uint8_t fromPrecision = smlPrecisionConvert[info->precision];

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

wmmhello's avatar
wmmhello 已提交
78
int32_t smlParseValue(SSmlKv *pVal, SSmlMsgBuf *msg) {
X
Xiaoyu Wang 已提交
79
  if (pVal->value[0] == '"') {  // binary
wmmhello's avatar
wmmhello 已提交
80 81 82 83 84 85 86 87
    if (pVal->length >= 2 && pVal->value[pVal->length - 1] == '"') {
      pVal->type = TSDB_DATA_TYPE_BINARY;
      pVal->length -= BINARY_ADD_LEN;
      if (pVal->length > TSDB_MAX_BINARY_LEN - VARSTR_HEADER_SIZE) {
        return TSDB_CODE_PAR_INVALID_VAR_COLUMN_LEN;
      }
      pVal->value += (BINARY_ADD_LEN - 1);
      return TSDB_CODE_SUCCESS;
88
    }
wmmhello's avatar
wmmhello 已提交
89
    return TSDB_CODE_TSC_INVALID_VALUE;
90
  }
wmmhello's avatar
wmmhello 已提交
91

X
Xiaoyu Wang 已提交
92 93
  if (pVal->value[0] == 'l' || pVal->value[0] == 'L') {  // nchar
    if (pVal->value[1] == '"' && pVal->value[pVal->length - 1] == '"' && pVal->length >= 3) {
wmmhello's avatar
wmmhello 已提交
94 95 96 97 98 99 100
      pVal->type = TSDB_DATA_TYPE_NCHAR;
      pVal->length -= NCHAR_ADD_LEN;
      if (pVal->length > (TSDB_MAX_NCHAR_LEN - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE) {
        return TSDB_CODE_PAR_INVALID_VAR_COLUMN_LEN;
      }
      pVal->value += (NCHAR_ADD_LEN - 1);
      return TSDB_CODE_SUCCESS;
101
    }
wmmhello's avatar
wmmhello 已提交
102
    return TSDB_CODE_TSC_INVALID_VALUE;
103 104
  }

X
Xiaoyu Wang 已提交
105 106 107 108
  if (pVal->value[0] == 't' || pVal->value[0] == 'T') {
    if (pVal->length == 1 ||
        (pVal->length == 4 && (pVal->value[1] == 'r' || pVal->value[1] == 'R') &&
         (pVal->value[2] == 'u' || pVal->value[2] == 'U') && (pVal->value[3] == 'e' || pVal->value[3] == 'E'))) {
wmmhello's avatar
wmmhello 已提交
109 110 111 112 113 114
      pVal->i = TSDB_TRUE;
      pVal->type = TSDB_DATA_TYPE_BOOL;
      pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
      return TSDB_CODE_SUCCESS;
    }
    return TSDB_CODE_TSC_INVALID_VALUE;
115
  }
wmmhello's avatar
wmmhello 已提交
116

X
Xiaoyu Wang 已提交
117 118 119 120 121
  if (pVal->value[0] == 'f' || pVal->value[0] == 'F') {
    if (pVal->length == 1 ||
        (pVal->length == 5 && (pVal->value[1] == 'a' || pVal->value[1] == 'A') &&
         (pVal->value[2] == 'l' || pVal->value[2] == 'L') && (pVal->value[3] == 's' || pVal->value[3] == 'S') &&
         (pVal->value[4] == 'e' || pVal->value[4] == 'E'))) {
wmmhello's avatar
wmmhello 已提交
122 123 124 125 126 127 128 129
      pVal->i = TSDB_FALSE;
      pVal->type = TSDB_DATA_TYPE_BOOL;
      pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
      return TSDB_CODE_SUCCESS;
    }
    return TSDB_CODE_TSC_INVALID_VALUE;
  }

130 131 132 133 134 135 136 137 138
  // number
  if (smlParseNumber(pVal, msg)) {
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
    return TSDB_CODE_SUCCESS;
  }

  return TSDB_CODE_TSC_INVALID_VALUE;
}

X
Xiaoyu Wang 已提交
139 140 141
static int32_t smlParseTagKv(SSmlHandle *info, char **sql, char *sqlEnd, SSmlLineInfo *currElement, bool isSameMeasure,
                             bool isSameCTable) {
  if (isSameCTable) {
142 143 144 145 146
    return TSDB_CODE_SUCCESS;
  }

  int     cnt = 0;
  SArray *preLineKV = info->preLineTagKV;
X
Xiaoyu Wang 已提交
147 148 149 150
  if (info->dataFormat) {
    if (unlikely(!isSameMeasure)) {
      SSmlSTableMeta **tmp =
          (SSmlSTableMeta **)taosHashGet(info->superTables, currElement->measure, currElement->measureLen);
151

152
      SSmlSTableMeta *sMeta = NULL;
X
Xiaoyu Wang 已提交
153
      if (unlikely(tmp == NULL)) {
154
        char *measure = currElement->measure;
wmmhello's avatar
wmmhello 已提交
155
        int   measureLen = currElement->measureLen;
156 157
        if (currElement->measureEscaped) {
          measure = (char *)taosMemoryMalloc(currElement->measureLen);
wmmhello's avatar
wmmhello 已提交
158 159 160 161
          memcpy(measure, currElement->measure, currElement->measureLen);
          PROCESS_SLASH_IN_MEASUREMENT(measure, measureLen);
        }
        STableMeta *pTableMeta = smlGetMeta(info, measure, measureLen);
162
        if (currElement->measureEscaped) {
wmmhello's avatar
wmmhello 已提交
163 164
          taosMemoryFree(measure);
        }
X
Xiaoyu Wang 已提交
165
        if (pTableMeta == NULL) {
166
          info->dataFormat = false;
X
Xiaoyu Wang 已提交
167
          info->reRun = true;
168 169
          return TSDB_CODE_SUCCESS;
        }
170
        sMeta = smlBuildSTableMeta(info->dataFormat);
wmmhello's avatar
wmmhello 已提交
171
        if(sMeta == NULL){
wmmhello's avatar
wmmhello 已提交
172
          taosMemoryFreeClear(pTableMeta);
wmmhello's avatar
wmmhello 已提交
173 174
          return TSDB_CODE_OUT_OF_MEMORY;
        }
wmmhello's avatar
wmmhello 已提交
175
        sMeta->tableMeta = pTableMeta;
176
        taosHashPut(info->superTables, currElement->measure, currElement->measureLen, &sMeta, POINTER_BYTES);
177 178
        for (int i = pTableMeta->tableInfo.numOfColumns;
             i < pTableMeta->tableInfo.numOfTags + pTableMeta->tableInfo.numOfColumns; i++) {
179
          SSchema *tag = pTableMeta->schema + i;
180 181 182 183
          SSmlKv   kv = {.key = tag->name,
                         .keyLen = strlen(tag->name),
                         .type = tag->type,
                         .length = (tag->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE};
184 185
          taosArrayPush(sMeta->tags, &kv);
        }
186
        tmp = &sMeta;
187
      }
188 189
      info->currSTableMeta = (*tmp)->tableMeta;
      info->maxTagKVs = (*tmp)->tags;
190 191
    }
  }
wmmhello's avatar
wmmhello 已提交
192
  taosArrayClearEx(preLineKV, freeSSmlKv);
193 194 195 196 197 198 199 200

  while (*sql < sqlEnd) {
    if (unlikely(IS_SPACE(*sql))) {
      break;
    }

    // parse key
    const char *key = *sql;
X
Xiaoyu Wang 已提交
201
    size_t      keyLen = 0;
wmmhello's avatar
wmmhello 已提交
202 203
    bool        keyEscaped = false;
    size_t      keyLenEscaped = 0;
204
    while (*sql < sqlEnd) {
205
      if (unlikely(IS_SPACE(*sql) || IS_COMMA(*sql))) {
206 207 208 209 210 211 212 213
        smlBuildInvalidDataMsg(&info->msgBuf, "invalid data", *sql);
        return TSDB_CODE_SML_INVALID_DATA;
      }
      if (unlikely(IS_EQUAL(*sql))) {
        keyLen = *sql - key;
        (*sql)++;
        break;
      }
wmmhello's avatar
wmmhello 已提交
214 215 216
      if (IS_SLASH_LETTER_IN_TAG_FIELD_KEY(*sql)) {
        keyLenEscaped++;
        keyEscaped = true;
217 218 219 220
      }
      (*sql)++;
    }

wmmhello's avatar
wmmhello 已提交
221
    if (unlikely(IS_INVALID_COL_LEN(keyLen - keyLenEscaped))) {
222 223 224 225 226 227
      smlBuildInvalidDataMsg(&info->msgBuf, "invalid key or key is too long than 64", key);
      return TSDB_CODE_TSC_INVALID_COLUMN_LENGTH;
    }

    // parse value
    const char *value = *sql;
X
Xiaoyu Wang 已提交
228
    size_t      valueLen = 0;
wmmhello's avatar
wmmhello 已提交
229 230
    bool        valueEscaped = false;
    size_t      valueLenEscaped = 0;
231 232 233 234
    while (*sql < sqlEnd) {
      // parse value
      if (unlikely(IS_SPACE(*sql) || IS_COMMA(*sql))) {
        break;
X
Xiaoyu Wang 已提交
235
      } else if (unlikely(IS_EQUAL(*sql))) {
236 237 238 239
        smlBuildInvalidDataMsg(&info->msgBuf, "invalid data", *sql);
        return TSDB_CODE_SML_INVALID_DATA;
      }

240
      if (IS_SLASH_LETTER_IN_TAG_FIELD_KEY(*sql)) {
wmmhello's avatar
wmmhello 已提交
241 242
        valueLenEscaped++;
        valueEscaped = true;
243 244 245 246 247 248 249 250 251 252 253
      }

      (*sql)++;
    }
    valueLen = *sql - value;

    if (unlikely(valueLen == 0)) {
      smlBuildInvalidDataMsg(&info->msgBuf, "invalid value", value);
      return TSDB_CODE_SML_INVALID_DATA;
    }

wmmhello's avatar
wmmhello 已提交
254
    if (unlikely(valueLen - valueLenEscaped > (TSDB_MAX_NCHAR_LEN - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE)) {
255 256 257
      return TSDB_CODE_PAR_INVALID_VAR_COLUMN_LEN;
    }

258 259
    if (keyEscaped) {
      char *tmp = (char *)taosMemoryMalloc(keyLen);
wmmhello's avatar
wmmhello 已提交
260 261 262 263
      memcpy(tmp, key, keyLen);
      PROCESS_SLASH_IN_TAG_FIELD_KEY(tmp, keyLen);
      key = tmp;
    }
264 265
    if (valueEscaped) {
      char *tmp = (char *)taosMemoryMalloc(valueLen);
wmmhello's avatar
wmmhello 已提交
266 267 268 269
      memcpy(tmp, value, valueLen);
      PROCESS_SLASH_IN_TAG_FIELD_KEY(tmp, valueLen);
      value = tmp;
    }
270 271 272 273 274 275 276
    SSmlKv kv = {.key = key,
                 .keyLen = keyLen,
                 .type = TSDB_DATA_TYPE_NCHAR,
                 .value = value,
                 .length = valueLen,
                 .keyEscaped = keyEscaped,
                 .valueEscaped = valueEscaped};
wmmhello's avatar
wmmhello 已提交
277
    taosArrayPush(preLineKV, &kv);
X
Xiaoyu Wang 已提交
278 279
    if (info->dataFormat) {
      if (unlikely(cnt + 1 > info->currSTableMeta->tableInfo.numOfTags)) {
280
        info->dataFormat = false;
X
Xiaoyu Wang 已提交
281
        info->reRun = true;
282 283 284
        return TSDB_CODE_SUCCESS;
      }

285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
      if (unlikely(cnt >= taosArrayGetSize(info->maxTagKVs))) {
        info->dataFormat = false;
        info->reRun = true;
        return TSDB_CODE_SUCCESS;
      }
      SSmlKv *maxKV = (SSmlKv *)taosArrayGet(info->maxTagKVs, cnt);

      if (unlikely(!IS_SAME_KEY)) {
        info->dataFormat = false;
        info->reRun = true;
        return TSDB_CODE_SUCCESS;
      }

      if (unlikely(kv.length > maxKV->length)) {
        maxKV->length = kv.length;
        info->needModifySchema = true;
301 302 303 304
      }
    }

    cnt++;
X
Xiaoyu Wang 已提交
305
    if (IS_SPACE(*sql)) {
306 307 308 309 310
      break;
    }
    (*sql)++;
  }

X
Xiaoyu Wang 已提交
311
  void *oneTable = taosHashGet(info->childTables, currElement->measure, currElement->measureTagsLen);
312 313 314 315 316 317 318 319 320
  if ((oneTable != NULL)) {
    return TSDB_CODE_SUCCESS;
  }

  SSmlTableInfo *tinfo = smlBuildTableInfo(1, currElement->measure, currElement->measureLen);
  if (unlikely(!tinfo)) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
  tinfo->tags = taosArrayDup(preLineKV, NULL);
321
  for (size_t i = 0; i < taosArrayGetSize(preLineKV); i++) {
wmmhello's avatar
wmmhello 已提交
322
    SSmlKv *kv = (SSmlKv *)taosArrayGet(preLineKV, i);
323 324
    if (kv->keyEscaped) kv->key = NULL;
    if (kv->valueEscaped) kv->value = NULL;
wmmhello's avatar
wmmhello 已提交
325
  }
326 327

  smlSetCTableName(tinfo);
328
  getTableUid(info, currElement, tinfo);
X
Xiaoyu Wang 已提交
329
  if (info->dataFormat) {
330 331
    info->currSTableMeta->uid = tinfo->uid;
    tinfo->tableDataCtx = smlInitTableDataCtx(info->pQuery, info->currSTableMeta);
X
Xiaoyu Wang 已提交
332
    if (tinfo->tableDataCtx == NULL) {
wmmhello's avatar
wmmhello 已提交
333
      smlDestroyTableInfo(&tinfo);
334 335 336 337 338
      smlBuildInvalidDataMsg(&info->msgBuf, "smlInitTableDataCtx error", NULL);
      return TSDB_CODE_SML_INVALID_DATA;
    }
  }

339
  taosHashPut(info->childTables, currElement->measure, currElement->measureTagsLen, &tinfo, POINTER_BYTES);
340 341 342 343

  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
344 345
static int32_t smlParseColKv(SSmlHandle *info, char **sql, char *sqlEnd, SSmlLineInfo *currElement, bool isSameMeasure,
                             bool isSameCTable) {
346
  int cnt = 0;
X
Xiaoyu Wang 已提交
347 348 349 350
  if (info->dataFormat) {
    if (unlikely(!isSameCTable)) {
      SSmlTableInfo **oneTable =
          (SSmlTableInfo **)taosHashGet(info->childTables, currElement->measure, currElement->measureTagsLen);
351 352 353 354
      if (unlikely(oneTable == NULL)) {
        smlBuildInvalidDataMsg(&info->msgBuf, "child table should inside", currElement->measure);
        return TSDB_CODE_SML_INVALID_DATA;
      }
355
      info->currTableDataCtx = (*oneTable)->tableDataCtx;
356 357
    }

X
Xiaoyu Wang 已提交
358 359 360 361
    if (unlikely(!isSameMeasure)) {
      SSmlSTableMeta **tmp =
          (SSmlSTableMeta **)taosHashGet(info->superTables, currElement->measure, currElement->measureLen);
      if (unlikely(tmp == NULL)) {
362
        char *measure = currElement->measure;
wmmhello's avatar
wmmhello 已提交
363
        int   measureLen = currElement->measureLen;
364 365
        if (currElement->measureEscaped) {
          measure = (char *)taosMemoryMalloc(currElement->measureLen);
wmmhello's avatar
wmmhello 已提交
366 367 368 369
          memcpy(measure, currElement->measure, currElement->measureLen);
          PROCESS_SLASH_IN_MEASUREMENT(measure, measureLen);
        }
        STableMeta *pTableMeta = smlGetMeta(info, measure, measureLen);
370
        if (currElement->measureEscaped) {
wmmhello's avatar
wmmhello 已提交
371 372
          taosMemoryFree(measure);
        }
X
Xiaoyu Wang 已提交
373
        if (pTableMeta == NULL) {
374
          info->dataFormat = false;
X
Xiaoyu Wang 已提交
375
          info->reRun = true;
376 377
          return TSDB_CODE_SUCCESS;
        }
378
        *tmp = smlBuildSTableMeta(info->dataFormat);
wmmhello's avatar
wmmhello 已提交
379
        if(*tmp == NULL){
wmmhello's avatar
wmmhello 已提交
380
          taosMemoryFreeClear(pTableMeta);
wmmhello's avatar
wmmhello 已提交
381 382
          return TSDB_CODE_OUT_OF_MEMORY;
        }
383 384 385
        (*tmp)->tableMeta = pTableMeta;
        taosHashPut(info->superTables, currElement->measure, currElement->measureLen, tmp, POINTER_BYTES);

386
        for (int i = 0; i < pTableMeta->tableInfo.numOfColumns; i++) {
387
          SSchema *tag = pTableMeta->schema + i;
388 389
          SSmlKv   kv = {.key = tag->name, .keyLen = strlen(tag->name), .type = tag->type};
          if (tag->type == TSDB_DATA_TYPE_NCHAR) {
390
            kv.length = (tag->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
391
          } else if (tag->type == TSDB_DATA_TYPE_BINARY) {
392 393 394 395
            kv.length = tag->bytes - VARSTR_HEADER_SIZE;
          }
          taosArrayPush((*tmp)->cols, &kv);
        }
396
      }
397
      info->currSTableMeta = (*tmp)->tableMeta;
398
      info->masColKVs = (*tmp)->cols;
399 400 401 402 403 404 405 406 407 408
    }
  }

  while (*sql < sqlEnd) {
    if (unlikely(IS_SPACE(*sql))) {
      break;
    }

    // parse key
    const char *key = *sql;
X
Xiaoyu Wang 已提交
409
    size_t      keyLen = 0;
wmmhello's avatar
wmmhello 已提交
410 411
    bool        keyEscaped = false;
    size_t      keyLenEscaped = 0;
412
    while (*sql < sqlEnd) {
413
      if (unlikely(IS_SPACE(*sql) || IS_COMMA(*sql))) {
414 415 416 417 418 419 420 421
        smlBuildInvalidDataMsg(&info->msgBuf, "invalid data", *sql);
        return TSDB_CODE_SML_INVALID_DATA;
      }
      if (unlikely(IS_EQUAL(*sql))) {
        keyLen = *sql - key;
        (*sql)++;
        break;
      }
wmmhello's avatar
wmmhello 已提交
422 423 424
      if (IS_SLASH_LETTER_IN_TAG_FIELD_KEY(*sql)) {
        keyLenEscaped++;
        keyEscaped = true;
425 426 427 428
      }
      (*sql)++;
    }

wmmhello's avatar
wmmhello 已提交
429
    if (unlikely(IS_INVALID_COL_LEN(keyLen - keyLenEscaped))) {
430 431 432 433 434 435
      smlBuildInvalidDataMsg(&info->msgBuf, "invalid key or key is too long than 64", key);
      return TSDB_CODE_TSC_INVALID_COLUMN_LENGTH;
    }

    // parse value
    const char *value = *sql;
X
Xiaoyu Wang 已提交
436
    size_t      valueLen = 0;
wmmhello's avatar
wmmhello 已提交
437 438
    bool        valueEscaped = false;
    size_t      valueLenEscaped = 0;
wmmhello's avatar
wmmhello 已提交
439
    int         quoteNum = 0;
wmmhello's avatar
wmmhello 已提交
440
    const char *escapeChar = NULL;
441 442
    while (*sql < sqlEnd) {
      // parse value
wmmhello's avatar
wmmhello 已提交
443
      if (unlikely(*(*sql) == QUOTE && (*(*sql - 1) != SLASH || (*sql - 1) == escapeChar))) {
wmmhello's avatar
wmmhello 已提交
444
        quoteNum++;
445
        (*sql)++;
wmmhello's avatar
wmmhello 已提交
446 447
        if(quoteNum > 2){
          break;
448
        }
449 450
        continue;
      }
wmmhello's avatar
wmmhello 已提交
451 452
      if (quoteNum % 2 == 0 && (unlikely(IS_SPACE(*sql) || IS_COMMA(*sql)))) {
        break;
453
      }
wmmhello's avatar
wmmhello 已提交
454 455 456 457
      if (IS_SLASH_LETTER_IN_FIELD_VALUE(*sql) && (*sql - 1) != escapeChar) {
        escapeChar = *sql;
        valueEscaped = true;
        valueLenEscaped++;
458 459 460 461 462 463
      }

      (*sql)++;
    }
    valueLen = *sql - value;

wmmhello's avatar
wmmhello 已提交
464 465
    if (unlikely(quoteNum != 0 && quoteNum != 2)) {
      smlBuildInvalidDataMsg(&info->msgBuf, "unbalanced quotes", value);
466 467 468 469 470 471
      return TSDB_CODE_SML_INVALID_DATA;
    }
    if (unlikely(valueLen == 0)) {
      smlBuildInvalidDataMsg(&info->msgBuf, "invalid value", value);
      return TSDB_CODE_SML_INVALID_DATA;
    }
wmmhello's avatar
wmmhello 已提交
472

473 474 475 476 477 478 479
    SSmlKv  kv = {.key = key, .keyLen = keyLen, .value = value, .length = valueLen};
    int32_t ret = smlParseValue(&kv, &info->msgBuf);
    if (ret != TSDB_CODE_SUCCESS) {
      smlBuildInvalidDataMsg(&info->msgBuf, "smlParseValue error", value);
      return ret;
    }

480 481
    if (keyEscaped) {
      char *tmp = (char *)taosMemoryMalloc(kv.keyLen);
482 483
      memcpy(tmp, key, kv.keyLen);
      PROCESS_SLASH_IN_TAG_FIELD_KEY(tmp, kv.keyLen);
484 485
      kv.key = tmp;
      kv.keyEscaped = keyEscaped;
486 487
    }

488 489
    if (valueEscaped) {
      char *tmp = (char *)taosMemoryMalloc(kv.length);
490 491 492 493
      memcpy(tmp, kv.value, kv.length);
      PROCESS_SLASH_IN_FIELD_VALUE(tmp, kv.length);
      kv.value = tmp;
      kv.valueEscaped = valueEscaped;
494 495
    }

X
Xiaoyu Wang 已提交
496 497 498
    if (info->dataFormat) {
      // cnt begin 0, add ts so + 2
      if (unlikely(cnt + 2 > info->currSTableMeta->tableInfo.numOfColumns)) {
499
        info->dataFormat = false;
X
Xiaoyu Wang 已提交
500
        info->reRun = true;
wmmhello's avatar
wmmhello 已提交
501
        freeSSmlKv(&kv);
502 503 504 505 506
        return TSDB_CODE_SUCCESS;
      }
      // bind data
      ret = smlBuildCol(info->currTableDataCtx, info->currSTableMeta->schema, &kv, cnt + 1);
      if (unlikely(ret != TSDB_CODE_SUCCESS)) {
wmmhello's avatar
wmmhello 已提交
507
        uDebug("smlBuildCol error, retry");
508
        info->dataFormat = false;
X
Xiaoyu Wang 已提交
509
        info->reRun = true;
wmmhello's avatar
wmmhello 已提交
510
        freeSSmlKv(&kv);
511 512
        return TSDB_CODE_SUCCESS;
      }
513 514 515
      if (cnt >= taosArrayGetSize(info->masColKVs)) {
        info->dataFormat = false;
        info->reRun = true;
wmmhello's avatar
wmmhello 已提交
516
        freeSSmlKv(&kv);
517 518 519 520 521 522
        return TSDB_CODE_SUCCESS;
      }
      SSmlKv *maxKV = (SSmlKv *)taosArrayGet(info->masColKVs, cnt);
      if (kv.type != maxKV->type) {
        info->dataFormat = false;
        info->reRun = true;
wmmhello's avatar
wmmhello 已提交
523
        freeSSmlKv(&kv);
524 525 526 527 528
        return TSDB_CODE_SUCCESS;
      }
      if (unlikely(!IS_SAME_KEY)) {
        info->dataFormat = false;
        info->reRun = true;
wmmhello's avatar
wmmhello 已提交
529
        freeSSmlKv(&kv);
530 531
        return TSDB_CODE_SUCCESS;
      }
532

533 534 535
      if (unlikely(IS_VAR_DATA_TYPE(kv.type) && kv.length > maxKV->length)) {
        maxKV->length = kv.length;
        info->needModifySchema = true;
536
      }
wmmhello's avatar
wmmhello 已提交
537
      freeSSmlKv(&kv);
X
Xiaoyu Wang 已提交
538 539
    } else {
      if (currElement->colArray == NULL) {
X
Xiaoyu Wang 已提交
540
        currElement->colArray = taosArrayInit_s(sizeof(SSmlKv), 1);
541
      }
X
Xiaoyu Wang 已提交
542
      taosArrayPush(currElement->colArray, &kv);  // reserve for timestamp
543 544 545
    }

    cnt++;
X
Xiaoyu Wang 已提交
546
    if (IS_SPACE(*sql)) {
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
      break;
    }
    (*sql)++;
  }

  return TSDB_CODE_SUCCESS;
}

int32_t smlParseInfluxString(SSmlHandle *info, char *sql, char *sqlEnd, SSmlLineInfo *elements) {
  if (!sql) return TSDB_CODE_SML_INVALID_DATA;
  JUMP_SPACE(sql, sqlEnd)
  if (unlikely(*sql == COMMA)) return TSDB_CODE_SML_INVALID_DATA;
  elements->measure = sql;

  // parse measure
wmmhello's avatar
wmmhello 已提交
562
  size_t measureLenEscaped = 0;
563
  while (sql < sqlEnd) {
wmmhello's avatar
wmmhello 已提交
564 565 566 567
    if (unlikely((sql != elements->measure) && IS_SLASH_LETTER_IN_MEASUREMENT(sql))) {
      elements->measureEscaped = true;
      measureLenEscaped++;
      sql++;
568 569 570 571 572 573 574 575 576 577 578 579
      continue;
    }
    if (unlikely(IS_COMMA(sql))) {
      break;
    }

    if (unlikely(IS_SPACE(sql))) {
      break;
    }
    sql++;
  }
  elements->measureLen = sql - elements->measure;
wmmhello's avatar
wmmhello 已提交
580
  if (unlikely(IS_INVALID_TABLE_LEN(elements->measureLen - measureLenEscaped))) {
581 582 583 584 585
    smlBuildInvalidDataMsg(&info->msgBuf, "measure is empty or too large than 192", NULL);
    return TSDB_CODE_TSC_INVALID_TABLE_ID_LENGTH;
  }

  // to get measureTagsLen before
X
Xiaoyu Wang 已提交
586 587
  const char *tmp = sql;
  while (tmp < sqlEnd) {
588 589 590 591 592 593 594 595 596
    if (unlikely(IS_SPACE(tmp))) {
      break;
    }
    tmp++;
  }
  elements->measureTagsLen = tmp - elements->measure;

  bool isSameCTable = false;
  bool isSameMeasure = false;
X
Xiaoyu Wang 已提交
597
  if (IS_SAME_CHILD_TABLE) {
598 599
    isSameCTable = true;
    isSameMeasure = true;
X
Xiaoyu Wang 已提交
600
  } else if (info->dataFormat) {
601 602 603 604 605 606 607
    isSameMeasure = IS_SAME_SUPER_TABLE;
  }
  // parse tag
  if (*sql == COMMA) sql++;
  elements->tags = sql;

  int ret = smlParseTagKv(info, &sql, sqlEnd, elements, isSameMeasure, isSameCTable);
X
Xiaoyu Wang 已提交
608
  if (unlikely(ret != TSDB_CODE_SUCCESS)) {
609 610
    return ret;
  }
X
Xiaoyu Wang 已提交
611
  if (unlikely(info->reRun)) {
612 613 614 615 616 617 618 619 620 621 622
    return TSDB_CODE_SUCCESS;
  }

  sql = elements->measure + elements->measureTagsLen;
  elements->tagsLen = sql - elements->tags;

  // parse cols
  JUMP_SPACE(sql, sqlEnd)
  elements->cols = sql;

  ret = smlParseColKv(info, &sql, sqlEnd, elements, isSameMeasure, isSameCTable);
X
Xiaoyu Wang 已提交
623
  if (unlikely(ret != TSDB_CODE_SUCCESS)) {
624 625 626
    return ret;
  }

X
Xiaoyu Wang 已提交
627
  if (unlikely(info->reRun)) {
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
    return TSDB_CODE_SUCCESS;
  }

  elements->colsLen = sql - elements->cols;
  if (unlikely(elements->colsLen == 0)) {
    smlBuildInvalidDataMsg(&info->msgBuf, "cols is empty", NULL);
    return TSDB_CODE_SML_INVALID_DATA;
  }

  // parse timestamp
  JUMP_SPACE(sql, sqlEnd)
  elements->timestamp = sql;
  while (sql < sqlEnd) {
    if (unlikely(isspace(*sql))) {
      break;
    }
    sql++;
  }
  elements->timestampLen = sql - elements->timestamp;

  int64_t ts = smlParseInfluxTime(info, elements->timestamp, elements->timestampLen);
  if (unlikely(ts <= 0)) {
    uError("SML:0x%" PRIx64 " smlParseTS error:%" PRId64, info->id, ts);
    return TSDB_CODE_INVALID_TIMESTAMP;
  }
  // add ts to
X
Xiaoyu Wang 已提交
654 655 656 657
  SSmlKv kv = {.key = TS,
               .keyLen = TS_LEN,
               .type = TSDB_DATA_TYPE_TIMESTAMP,
               .i = ts,
wmmhello's avatar
wmmhello 已提交
658 659 660
               .length = (size_t)tDataTypes[TSDB_DATA_TYPE_TIMESTAMP].bytes,
               .keyEscaped = false,
               .valueEscaped = false};
X
Xiaoyu Wang 已提交
661
  if (info->dataFormat) {
wmmhello's avatar
wmmhello 已提交
662
    uDebug("SML:0x%" PRIx64 " smlParseInfluxString format true, ts:%" PRId64, info->id, ts);
wmmhello's avatar
wmmhello 已提交
663
    ret = smlBuildCol(info->currTableDataCtx, info->currSTableMeta->schema, &kv, 0);
664 665 666
    if (ret != TSDB_CODE_SUCCESS) {
      return ret;
    }
wmmhello's avatar
wmmhello 已提交
667
    ret = smlBuildRow(info->currTableDataCtx);
668 669 670
    if (ret != TSDB_CODE_SUCCESS) {
      return ret;
    }
wmmhello's avatar
wmmhello 已提交
671
    clearColValArray(info->currTableDataCtx->pValues);
X
Xiaoyu Wang 已提交
672
  } else {
wmmhello's avatar
wmmhello 已提交
673
    uDebug("SML:0x%" PRIx64 " smlParseInfluxString format false, ts:%" PRId64, info->id, ts);
674 675 676 677 678 679
    taosArraySet(elements->colArray, 0, &kv);
  }
  info->preLine = *elements;

  return ret;
}