parInsertSql.c 67.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

X
Xiaoyu Wang 已提交
16
#include "parInsertUtil.h"
X
Xiaoyu Wang 已提交
17
#include "parToken.h"
18 19 20
#include "tglobal.h"
#include "ttime.h"

X
Xiaoyu Wang 已提交
21 22 23 24 25
#define NEXT_TOKEN_WITH_PREV(pSql, token)     \
  do {                                        \
    int32_t index = 0;                        \
    token = tStrGetToken(pSql, &index, true); \
    pSql += index;                            \
X
Xiaoyu Wang 已提交
26 27
  } while (0)

X
Xiaoyu Wang 已提交
28 29 30
#define NEXT_TOKEN_KEEP_SQL(pSql, token, index) \
  do {                                          \
    token = tStrGetToken(pSql, &index, false);  \
31 32
  } while (0)

X
Xiaoyu Wang 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45
#define NEXT_VALID_TOKEN(pSql, token)           \
  do {                                          \
    (token).n = tGetToken(pSql, &(token).type); \
    (token).z = (char*)pSql;                    \
    pSql += (token).n;                          \
  } while (TK_NK_SPACE == (token).type)

typedef struct SInsertParseContext {
  SParseContext*     pComCxt;
  SMsgBuf            msg;
  char               tmpTokenBuf[TSDB_MAX_BYTES_PER_ROW];
  SParsedDataColInfo tags;  // for stmt
  bool               missCache;
X
Xiaoyu Wang 已提交
46
  bool               usingDuplicateTable;
47
  bool               forceUpdate;
X
Xiaoyu Wang 已提交
48
} SInsertParseContext;
49

H
refact  
Hongze Cheng 已提交
50
typedef int32_t (*_row_append_fn_t)(SMsgBuf* pMsgBuf, const void* value, int32_t len, void* param);
X
Xiaoyu Wang 已提交
51 52 53 54

static uint8_t TRUE_VALUE = (uint8_t)TSDB_TRUE;
static uint8_t FALSE_VALUE = (uint8_t)TSDB_FALSE;

X
Xiaoyu Wang 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
static bool isNullStr(SToken* pToken) {
  return ((pToken->type == TK_NK_STRING) && (strlen(TSDB_DATA_NULL_STR_L) == pToken->n) &&
          (strncasecmp(TSDB_DATA_NULL_STR_L, pToken->z, pToken->n) == 0));
}

static bool isNullValue(int8_t dataType, SToken* pToken) {
  return TK_NULL == pToken->type || (!IS_STR_DATA_TYPE(dataType) && isNullStr(pToken));
}

static FORCE_INLINE int32_t toDouble(SToken* pToken, double* value, char** endPtr) {
  errno = 0;
  *value = taosStr2Double(pToken->z, endPtr);

  // not a valid integer number, return error
  if ((*endPtr - pToken->z) != pToken->n) {
    return TK_NK_ILLEGAL;
  }

  return pToken->type;
}

static int32_t skipInsertInto(const char** pSql, SMsgBuf* pMsg) {
  SToken token;
  NEXT_TOKEN(*pSql, token);
  if (TK_INSERT != token.type && TK_IMPORT != token.type) {
    return buildSyntaxErrMsg(pMsg, "keyword INSERT is expected", token.z);
81
  }
X
Xiaoyu Wang 已提交
82 83 84
  NEXT_TOKEN(*pSql, token);
  if (TK_INTO != token.type) {
    return buildSyntaxErrMsg(pMsg, "keyword INTO is expected", token.z);
85 86 87 88
  }
  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101
static int32_t skipParentheses(SInsertParseContext* pCxt, const char** pSql) {
  SToken  token;
  int32_t expectRightParenthesis = 1;
  while (1) {
    NEXT_TOKEN(*pSql, token);
    if (TK_NK_LP == token.type) {
      ++expectRightParenthesis;
    } else if (TK_NK_RP == token.type && 0 == --expectRightParenthesis) {
      break;
    }
    if (0 == token.n) {
      return buildSyntaxErrMsg(&pCxt->msg, ") expected", NULL);
    }
102
  }
X
Xiaoyu Wang 已提交
103 104
  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
105

X
Xiaoyu Wang 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118
static int32_t skipTableOptions(SInsertParseContext* pCxt, const char** pSql) {
  do {
    int32_t index = 0;
    SToken  token;
    NEXT_TOKEN_KEEP_SQL(*pSql, token, index);
    if (TK_TTL == token.type || TK_COMMENT == token.type) {
      *pSql += index;
      NEXT_TOKEN_WITH_PREV(*pSql, token);
    } else {
      break;
    }
  } while (1);
  return TSDB_CODE_SUCCESS;
119 120
}

X
Xiaoyu Wang 已提交
121 122 123 124 125 126 127 128 129 130 131 132
// pSql -> stb_name [(tag1_name, ...)] TAGS (tag1_value, ...)
static int32_t ignoreUsingClause(SInsertParseContext* pCxt, const char** pSql) {
  int32_t code = TSDB_CODE_SUCCESS;
  SToken  token;
  NEXT_TOKEN(*pSql, token);

  NEXT_TOKEN(*pSql, token);
  if (TK_NK_LP == token.type) {
    code = skipParentheses(pCxt, pSql);
    if (TSDB_CODE_SUCCESS == code) {
      NEXT_TOKEN(*pSql, token);
    }
133
  }
X
Xiaoyu Wang 已提交
134

X
Xiaoyu Wang 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148
  // pSql -> TAGS (tag1_value, ...)
  if (TSDB_CODE_SUCCESS == code) {
    if (TK_TAGS != token.type) {
      code = buildSyntaxErrMsg(&pCxt->msg, "TAGS is expected", token.z);
    } else {
      NEXT_TOKEN(*pSql, token);
    }
  }
  if (TSDB_CODE_SUCCESS == code) {
    if (TK_NK_LP != token.type) {
      code = buildSyntaxErrMsg(&pCxt->msg, "( is expected", token.z);
    } else {
      code = skipParentheses(pCxt, pSql);
    }
149 150
  }

X
Xiaoyu Wang 已提交
151 152
  if (TSDB_CODE_SUCCESS == code) {
    code = skipTableOptions(pCxt, pSql);
153
  }
X
Xiaoyu Wang 已提交
154 155

  return code;
156
}
D
dapan 已提交
157

X
Xiaoyu Wang 已提交
158 159 160 161 162 163 164 165 166 167 168 169
static int32_t parseDuplicateUsingClause(SInsertParseContext* pCxt, SVnodeModifOpStmt* pStmt, bool* pDuplicate) {
  *pDuplicate = false;

  char tbFName[TSDB_TABLE_FNAME_LEN];
  tNameExtractFullName(&pStmt->targetTableName, tbFName);
  STableMeta** pMeta = taosHashGet(pStmt->pSubTableHashObj, tbFName, strlen(tbFName));
  if (NULL != pMeta) {
    *pDuplicate = true;
    int32_t code = ignoreUsingClause(pCxt, &pStmt->pSql);
    if (TSDB_CODE_SUCCESS == code) {
      return cloneTableMeta(*pMeta, &pStmt->pTableMeta);
    }
D
stmt  
dapan1121 已提交
170
  }
X
Xiaoyu Wang 已提交
171

H
refact  
Hongze Cheng 已提交
172
  return TSDB_CODE_SUCCESS;
D
stmt  
dapan1121 已提交
173 174
}

X
Xiaoyu Wang 已提交
175
// pStmt->pSql -> field1_name, ...)
X
Xiaoyu Wang 已提交
176 177
static int32_t parseBoundColumns(SInsertParseContext* pCxt, const char** pSql, bool isTags,
                                 SParsedDataColInfo* pColList, SSchema* pSchema) {
X
Xiaoyu Wang 已提交
178
  col_id_t nCols = pColList->numOfCols;
D
stmt  
dapan1121 已提交
179

X
Xiaoyu Wang 已提交
180 181 182 183 184 185
  pColList->numOfBound = 0;
  pColList->boundNullLen = 0;
  memset(pColList->boundColumns, 0, sizeof(col_id_t) * nCols);
  for (col_id_t i = 0; i < nCols; ++i) {
    pColList->cols[i].valStat = VAL_STAT_NONE;
  }
D
stmt  
dapan1121 已提交
186

X
Xiaoyu Wang 已提交
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
  SToken   token;
  bool     isOrdered = true;
  col_id_t lastColIdx = -1;  // last column found
  while (1) {
    NEXT_TOKEN(*pSql, token);

    if (TK_NK_RP == token.type) {
      break;
    }

    char tmpTokenBuf[TSDB_COL_NAME_LEN + 2] = {0};  // used for deleting Escape character backstick(`)
    strncpy(tmpTokenBuf, token.z, token.n);
    token.z = tmpTokenBuf;
    token.n = strdequote(token.z);

    col_id_t t = lastColIdx + 1;
    col_id_t index = insFindCol(&token, t, nCols, pSchema);
    if (index < 0 && t > 0) {
      index = insFindCol(&token, 0, t, pSchema);
      isOrdered = false;
    }
    if (index < 0) {
      return generateSyntaxErrMsg(&pCxt->msg, TSDB_CODE_PAR_INVALID_COLUMN, token.z);
    }
    if (pColList->cols[index].valStat == VAL_STAT_HAS) {
      return buildSyntaxErrMsg(&pCxt->msg, "duplicated column name", token.z);
    }
    lastColIdx = index;
    pColList->cols[index].valStat = VAL_STAT_HAS;
    pColList->boundColumns[pColList->numOfBound] = index;
    ++pColList->numOfBound;
    switch (pSchema[t].type) {
      case TSDB_DATA_TYPE_BINARY:
        pColList->boundNullLen += (sizeof(VarDataOffsetT) + VARSTR_HEADER_SIZE + CHAR_BYTES);
        break;
      case TSDB_DATA_TYPE_NCHAR:
        pColList->boundNullLen += (sizeof(VarDataOffsetT) + VARSTR_HEADER_SIZE + TSDB_NCHAR_SIZE);
        break;
      default:
        pColList->boundNullLen += TYPE_BYTES[pSchema[t].type];
        break;
    }
  }

X
Xiaoyu Wang 已提交
231
  if (!isTags && pColList->cols[0].valStat == VAL_STAT_NONE) {
X
Xiaoyu Wang 已提交
232 233 234
    return buildInvalidOperationMsg(&pCxt->msg, "primary timestamp column can not be null");
  }

X
Xiaoyu Wang 已提交
235 236 237 238 239
  pColList->orderStatus = isOrdered ? ORDER_STATUS_ORDERED : ORDER_STATUS_DISORDERED;

  if (!isOrdered) {
    pColList->colIdxInfo = taosMemoryCalloc(pColList->numOfBound, sizeof(SBoundIdxInfo));
    if (NULL == pColList->colIdxInfo) {
S
Shengliang Guan 已提交
240
      return TSDB_CODE_OUT_OF_MEMORY;
X
Xiaoyu Wang 已提交
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
    }
    SBoundIdxInfo* pColIdx = pColList->colIdxInfo;
    for (col_id_t i = 0; i < pColList->numOfBound; ++i) {
      pColIdx[i].schemaColIdx = pColList->boundColumns[i];
      pColIdx[i].boundIdx = i;
    }
    taosSort(pColIdx, pColList->numOfBound, sizeof(SBoundIdxInfo), insSchemaIdxCompar);
    for (col_id_t i = 0; i < pColList->numOfBound; ++i) {
      pColIdx[i].finalIdx = i;
    }
    taosSort(pColIdx, pColList->numOfBound, sizeof(SBoundIdxInfo), insBoundIdxCompar);
  }

  if (pColList->numOfCols > pColList->numOfBound) {
    memset(&pColList->boundColumns[pColList->numOfBound], 0,
           sizeof(col_id_t) * (pColList->numOfCols - pColList->numOfBound));
X
Xiaoyu Wang 已提交
257
  }
X
Xiaoyu Wang 已提交
258

X
Xiaoyu Wang 已提交
259 260 261
  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
262 263 264 265 266
static int parseTime(const char** end, SToken* pToken, int16_t timePrec, int64_t* time, SMsgBuf* pMsgBuf) {
  int32_t     index = 0;
  int64_t     interval;
  int64_t     ts = 0;
  const char* pTokenEnd = *end;
267 268

  if (pToken->type == TK_NOW) {
269
    ts = taosGetTimestamp(timePrec);
270 271
  } else if (pToken->type == TK_TODAY) {
    ts = taosGetTimestampToday(timePrec);
272
  } else if (pToken->type == TK_NK_INTEGER) {
X
Xiaoyu Wang 已提交
273 274 275
    if (TSDB_CODE_SUCCESS != toInteger(pToken->z, pToken->n, 10, &ts)) {
      return buildSyntaxErrMsg(pMsgBuf, "invalid timestamp format", pToken->z);
    }
H
refact  
Hongze Cheng 已提交
276
  } else {  // parse the RFC-3339/ISO-8601 timestamp format string
S
os env  
Shengliang Guan 已提交
277
    if (taosParseTime(pToken->z, time, pToken->n, timePrec, tsDaylight) != TSDB_CODE_SUCCESS) {
278
      return buildSyntaxErrMsg(pMsgBuf, "invalid timestamp format", pToken->z);
279 280 281 282 283 284 285
    }

    return TSDB_CODE_SUCCESS;
  }

  for (int k = pToken->n; pToken->z[k] != '\0'; k++) {
    if (pToken->z[k] == ' ' || pToken->z[k] == '\t') continue;
H
refact  
Hongze Cheng 已提交
286
    if (pToken->z[k] == '(' && pToken->z[k + 1] == ')') {  // for insert NOW()/TODAY()
287 288 289 290
      *end = pTokenEnd = &pToken->z[k + 2];
      k++;
      continue;
    }
291
    if (pToken->z[k] == ',') {
292 293
      *end = pTokenEnd;
      *time = ts;
294 295 296 297 298 299 300 301 302 303 304
      return 0;
    }

    break;
  }

  /*
   * time expression:
   * e.g., now+12a, now-5h
   */
  index = 0;
X
Xiaoyu Wang 已提交
305
  SToken token = tStrGetToken(pTokenEnd, &index, false);
306 307
  pTokenEnd += index;

X
Xiaoyu Wang 已提交
308
  if (token.type == TK_NK_MINUS || token.type == TK_NK_PLUS) {
309
    index = 0;
X
Xiaoyu Wang 已提交
310
    SToken valueToken = tStrGetToken(pTokenEnd, &index, false);
311 312 313
    pTokenEnd += index;

    if (valueToken.n < 2) {
X
Xiaoyu Wang 已提交
314
      return buildSyntaxErrMsg(pMsgBuf, "value expected in timestamp", token.z);
315 316 317 318 319 320 321
    }

    char unit = 0;
    if (parseAbsoluteDuration(valueToken.z, valueToken.n, &interval, &unit, timePrec) != TSDB_CODE_SUCCESS) {
      return TSDB_CODE_TSC_INVALID_OPERATION;
    }

X
Xiaoyu Wang 已提交
322
    if (token.type == TK_NK_PLUS) {
323
      ts += interval;
324
    } else {
325
      ts = ts - interval;
326 327
    }

328
    *end = pTokenEnd;
329 330
  }

331
  *time = ts;
332 333
  return TSDB_CODE_SUCCESS;
}
334

X
Xiaoyu Wang 已提交
335 336
static int32_t parseTagToken(const char** end, SToken* pToken, SSchema* pSchema, int16_t timePrec, STagVal* val,
                             SMsgBuf* pMsgBuf) {
X
Xiaoyu Wang 已提交
337 338 339
  int64_t  iv;
  uint64_t uv;
  char*    endptr = NULL;
X
Xiaoyu Wang 已提交
340

341
  if (isNullValue(pSchema->type, pToken)) {
X
Xiaoyu Wang 已提交
342
    if (TSDB_DATA_TYPE_TIMESTAMP == pSchema->type && PRIMARYKEY_TIMESTAMP_COL_ID == pSchema->colId) {
D
stmt  
dapan1121 已提交
343
      return buildSyntaxErrMsg(pMsgBuf, "primary timestamp should not be null", pToken->z);
X
Xiaoyu Wang 已提交
344 345
    }

X
Xiaoyu Wang 已提交
346
    return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
347 348
  }

X
Xiaoyu Wang 已提交
349 350 351
  //  strcpy(val->colName, pSchema->name);
  val->cid = pSchema->colId;
  val->type = pSchema->type;
X
Xiaoyu Wang 已提交
352

X
Xiaoyu Wang 已提交
353 354
  switch (pSchema->type) {
    case TSDB_DATA_TYPE_BOOL: {
355
      if ((pToken->type == TK_NK_BOOL || pToken->type == TK_NK_STRING) && (pToken->n != 0)) {
X
Xiaoyu Wang 已提交
356
        if (strncmp(pToken->z, "true", pToken->n) == 0) {
X
Xiaoyu Wang 已提交
357
          *(int8_t*)(&val->i64) = TRUE_VALUE;
X
Xiaoyu Wang 已提交
358
        } else if (strncmp(pToken->z, "false", pToken->n) == 0) {
X
Xiaoyu Wang 已提交
359
          *(int8_t*)(&val->i64) = FALSE_VALUE;
X
Xiaoyu Wang 已提交
360 361 362
        } else {
          return buildSyntaxErrMsg(pMsgBuf, "invalid bool data", pToken->z);
        }
363
      } else if (pToken->type == TK_NK_INTEGER) {
X
Xiaoyu Wang 已提交
364
        *(int8_t*)(&val->i64) = ((taosStr2Int64(pToken->z, NULL, 10) == 0) ? FALSE_VALUE : TRUE_VALUE);
365
      } else if (pToken->type == TK_NK_FLOAT) {
X
Xiaoyu Wang 已提交
366
        *(int8_t*)(&val->i64) = ((taosStr2Double(pToken->z, NULL) == 0) ? FALSE_VALUE : TRUE_VALUE);
X
Xiaoyu Wang 已提交
367 368 369
      } else {
        return buildSyntaxErrMsg(pMsgBuf, "invalid bool data", pToken->z);
      }
X
Xiaoyu Wang 已提交
370
      break;
X
Xiaoyu Wang 已提交
371 372 373
    }

    case TSDB_DATA_TYPE_TINYINT: {
X
Xiaoyu Wang 已提交
374
      if (TSDB_CODE_SUCCESS != toInteger(pToken->z, pToken->n, 10, &iv)) {
X
Xiaoyu Wang 已提交
375 376 377 378 379
        return buildSyntaxErrMsg(pMsgBuf, "invalid tinyint data", pToken->z);
      } else if (!IS_VALID_TINYINT(iv)) {
        return buildSyntaxErrMsg(pMsgBuf, "tinyint data overflow", pToken->z);
      }

X
Xiaoyu Wang 已提交
380 381
      *(int8_t*)(&val->i64) = iv;
      break;
X
Xiaoyu Wang 已提交
382 383
    }

H
refact  
Hongze Cheng 已提交
384
    case TSDB_DATA_TYPE_UTINYINT: {
X
Xiaoyu Wang 已提交
385
      if (TSDB_CODE_SUCCESS != toUInteger(pToken->z, pToken->n, 10, &uv)) {
X
Xiaoyu Wang 已提交
386
        return buildSyntaxErrMsg(pMsgBuf, "invalid unsigned tinyint data", pToken->z);
X
Xiaoyu Wang 已提交
387
      } else if (uv > UINT8_MAX) {
X
Xiaoyu Wang 已提交
388 389
        return buildSyntaxErrMsg(pMsgBuf, "unsigned tinyint data overflow", pToken->z);
      }
X
Xiaoyu Wang 已提交
390 391
      *(uint8_t*)(&val->i64) = uv;
      break;
X
Xiaoyu Wang 已提交
392 393 394
    }

    case TSDB_DATA_TYPE_SMALLINT: {
X
Xiaoyu Wang 已提交
395
      if (TSDB_CODE_SUCCESS != toInteger(pToken->z, pToken->n, 10, &iv)) {
X
Xiaoyu Wang 已提交
396 397 398 399
        return buildSyntaxErrMsg(pMsgBuf, "invalid smallint data", pToken->z);
      } else if (!IS_VALID_SMALLINT(iv)) {
        return buildSyntaxErrMsg(pMsgBuf, "smallint data overflow", pToken->z);
      }
X
Xiaoyu Wang 已提交
400 401
      *(int16_t*)(&val->i64) = iv;
      break;
X
Xiaoyu Wang 已提交
402 403 404
    }

    case TSDB_DATA_TYPE_USMALLINT: {
X
Xiaoyu Wang 已提交
405
      if (TSDB_CODE_SUCCESS != toUInteger(pToken->z, pToken->n, 10, &uv)) {
X
Xiaoyu Wang 已提交
406
        return buildSyntaxErrMsg(pMsgBuf, "invalid unsigned smallint data", pToken->z);
X
Xiaoyu Wang 已提交
407
      } else if (uv > UINT16_MAX) {
X
Xiaoyu Wang 已提交
408 409
        return buildSyntaxErrMsg(pMsgBuf, "unsigned smallint data overflow", pToken->z);
      }
X
Xiaoyu Wang 已提交
410 411
      *(uint16_t*)(&val->i64) = uv;
      break;
X
Xiaoyu Wang 已提交
412 413 414
    }

    case TSDB_DATA_TYPE_INT: {
X
Xiaoyu Wang 已提交
415
      if (TSDB_CODE_SUCCESS != toInteger(pToken->z, pToken->n, 10, &iv)) {
X
Xiaoyu Wang 已提交
416 417 418 419
        return buildSyntaxErrMsg(pMsgBuf, "invalid int data", pToken->z);
      } else if (!IS_VALID_INT(iv)) {
        return buildSyntaxErrMsg(pMsgBuf, "int data overflow", pToken->z);
      }
X
Xiaoyu Wang 已提交
420 421
      *(int32_t*)(&val->i64) = iv;
      break;
X
Xiaoyu Wang 已提交
422 423 424
    }

    case TSDB_DATA_TYPE_UINT: {
X
Xiaoyu Wang 已提交
425
      if (TSDB_CODE_SUCCESS != toUInteger(pToken->z, pToken->n, 10, &uv)) {
X
Xiaoyu Wang 已提交
426
        return buildSyntaxErrMsg(pMsgBuf, "invalid unsigned int data", pToken->z);
X
Xiaoyu Wang 已提交
427
      } else if (uv > UINT32_MAX) {
X
Xiaoyu Wang 已提交
428 429
        return buildSyntaxErrMsg(pMsgBuf, "unsigned int data overflow", pToken->z);
      }
X
Xiaoyu Wang 已提交
430 431
      *(uint32_t*)(&val->i64) = uv;
      break;
X
Xiaoyu Wang 已提交
432 433 434
    }

    case TSDB_DATA_TYPE_BIGINT: {
X
Xiaoyu Wang 已提交
435
      if (TSDB_CODE_SUCCESS != toInteger(pToken->z, pToken->n, 10, &iv)) {
X
Xiaoyu Wang 已提交
436 437
        return buildSyntaxErrMsg(pMsgBuf, "invalid bigint data", pToken->z);
      }
X
Xiaoyu Wang 已提交
438 439
      val->i64 = iv;
      break;
X
Xiaoyu Wang 已提交
440 441 442
    }

    case TSDB_DATA_TYPE_UBIGINT: {
X
Xiaoyu Wang 已提交
443
      if (TSDB_CODE_SUCCESS != toUInteger(pToken->z, pToken->n, 10, &uv)) {
X
Xiaoyu Wang 已提交
444 445
        return buildSyntaxErrMsg(pMsgBuf, "invalid unsigned bigint data", pToken->z);
      }
X
Xiaoyu Wang 已提交
446 447
      *(uint64_t*)(&val->i64) = uv;
      break;
X
Xiaoyu Wang 已提交
448 449 450 451
    }

    case TSDB_DATA_TYPE_FLOAT: {
      double dv;
452
      if (TK_NK_ILLEGAL == toDouble(pToken, &dv, &endptr)) {
X
Xiaoyu Wang 已提交
453 454
        return buildSyntaxErrMsg(pMsgBuf, "illegal float data", pToken->z);
      }
H
refact  
Hongze Cheng 已提交
455 456
      if (((dv == HUGE_VAL || dv == -HUGE_VAL) && errno == ERANGE) || dv > FLT_MAX || dv < -FLT_MAX || isinf(dv) ||
          isnan(dv)) {
X
Xiaoyu Wang 已提交
457 458
        return buildSyntaxErrMsg(pMsgBuf, "illegal float data", pToken->z);
      }
X
Xiaoyu Wang 已提交
459 460
      *(float*)(&val->i64) = dv;
      break;
X
Xiaoyu Wang 已提交
461 462 463 464
    }

    case TSDB_DATA_TYPE_DOUBLE: {
      double dv;
465
      if (TK_NK_ILLEGAL == toDouble(pToken, &dv, &endptr)) {
X
Xiaoyu Wang 已提交
466 467 468 469 470
        return buildSyntaxErrMsg(pMsgBuf, "illegal double data", pToken->z);
      }
      if (((dv == HUGE_VAL || dv == -HUGE_VAL) && errno == ERANGE) || isinf(dv) || isnan(dv)) {
        return buildSyntaxErrMsg(pMsgBuf, "illegal double data", pToken->z);
      }
X
Xiaoyu Wang 已提交
471 472 473

      *(double*)(&val->i64) = dv;
      break;
X
Xiaoyu Wang 已提交
474 475 476 477 478
    }

    case TSDB_DATA_TYPE_BINARY: {
      // Too long values will raise the invalid sql error message
      if (pToken->n + VARSTR_HEADER_SIZE > pSchema->bytes) {
D
dapan1121 已提交
479
        return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_VALUE_TOO_LONG, pSchema->name);
X
Xiaoyu Wang 已提交
480
      }
X
Xiaoyu Wang 已提交
481 482 483
      val->pData = strdup(pToken->z);
      val->nData = pToken->n;
      break;
X
Xiaoyu Wang 已提交
484 485 486
    }

    case TSDB_DATA_TYPE_NCHAR: {
X
Xiaoyu Wang 已提交
487 488 489 490 491 492 493 494 495 496 497 498 499 500
      int32_t output = 0;
      void*   p = taosMemoryCalloc(1, pSchema->bytes - VARSTR_HEADER_SIZE);
      if (p == NULL) {
        return TSDB_CODE_OUT_OF_MEMORY;
      }
      if (!taosMbsToUcs4(pToken->z, pToken->n, (TdUcs4*)(p), pSchema->bytes - VARSTR_HEADER_SIZE, &output)) {
        if (errno == E2BIG) {
          taosMemoryFree(p);
          return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_VALUE_TOO_LONG, pSchema->name);
        }
        char buf[512] = {0};
        snprintf(buf, tListLen(buf), " taosMbsToUcs4 error:%s", strerror(errno));
        taosMemoryFree(p);
        return buildSyntaxErrMsg(pMsgBuf, buf, pToken->z);
501
      }
X
Xiaoyu Wang 已提交
502 503 504
      val->pData = p;
      val->nData = output;
      break;
505
    }
X
Xiaoyu Wang 已提交
506
    case TSDB_DATA_TYPE_TIMESTAMP: {
X
Xiaoyu Wang 已提交
507
      if (parseTime(end, pToken, timePrec, &iv, pMsgBuf) != TSDB_CODE_SUCCESS) {
X
Xiaoyu Wang 已提交
508 509 510
        return buildSyntaxErrMsg(pMsgBuf, "invalid timestamp", pToken->z);
      }

X
Xiaoyu Wang 已提交
511 512
      val->i64 = iv;
      break;
X
Xiaoyu Wang 已提交
513 514 515
    }
  }

X
Xiaoyu Wang 已提交
516
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
517 518
}

X
Xiaoyu Wang 已提交
519 520 521 522 523
// input pStmt->pSql:  [(tag1_name, ...)] TAGS (tag1_value, ...) ...
// output pStmt->pSql: TAGS (tag1_value, ...) ...
static int32_t parseBoundTagsClause(SInsertParseContext* pCxt, SVnodeModifOpStmt* pStmt) {
  SSchema* pTagsSchema = getTableTagSchema(pStmt->pTableMeta);
  insSetBoundColumnInfo(&pCxt->tags, pTagsSchema, getNumOfTags(pStmt->pTableMeta));
524

X
Xiaoyu Wang 已提交
525 526 527 528 529
  SToken  token;
  int32_t index = 0;
  NEXT_TOKEN_KEEP_SQL(pStmt->pSql, token, index);
  if (TK_NK_LP != token.type) {
    return TSDB_CODE_SUCCESS;
530 531
  }

X
Xiaoyu Wang 已提交
532
  pStmt->pSql += index;
X
Xiaoyu Wang 已提交
533
  return parseBoundColumns(pCxt, &pStmt->pSql, true, &pCxt->tags, pTagsSchema);
X
Xiaoyu Wang 已提交
534
}
535

X
Xiaoyu Wang 已提交
536 537 538 539 540 541 542 543 544
static int32_t parseTagValue(SInsertParseContext* pCxt, SVnodeModifOpStmt* pStmt, SSchema* pTagSchema, SToken* pToken,
                             SArray* pTagName, SArray* pTagVals, STag** pTag) {
  if (!isNullValue(pTagSchema->type, pToken)) {
    taosArrayPush(pTagName, pTagSchema->name);
  }

  if (pTagSchema->type == TSDB_DATA_TYPE_JSON) {
    if (pToken->n > (TSDB_MAX_JSON_TAG_LEN - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE) {
      return buildSyntaxErrMsg(&pCxt->msg, "json string too long than 4095", pToken->z);
545 546
    }

X
Xiaoyu Wang 已提交
547 548 549 550 551 552
    if (isNullValue(pTagSchema->type, pToken)) {
      return tTagNew(pTagVals, 1, true, pTag);
    } else {
      return parseJsontoTagData(pToken->z, pTagVals, pTag, &pCxt->msg);
    }
  }
553

X
Xiaoyu Wang 已提交
554 555 556 557 558 559 560 561 562 563 564
  STagVal val = {0};
  int32_t code =
      parseTagToken(&pStmt->pSql, pToken, pTagSchema, pStmt->pTableMeta->tableInfo.precision, &val, &pCxt->msg);
  if (TSDB_CODE_SUCCESS == code) {
    taosArrayPush(pTagVals, &val);
  }

  return code;
}

static void buildCreateTbReq(SVnodeModifOpStmt* pStmt, STag* pTag, SArray* pTagName) {
X
Xiaoyu Wang 已提交
565
  insBuildCreateTbReq(&pStmt->createTblReq, pStmt->targetTableName.tname, pTag, pStmt->pTableMeta->suid,
566
                      pStmt->usingTableName.tname, pTagName, pStmt->pTableMeta->tableInfo.numOfTags, TSDB_DEFAULT_TABLE_TTL);
X
Xiaoyu Wang 已提交
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
}

static int32_t checkAndTrimValue(SToken* pToken, char* tmpTokenBuf, SMsgBuf* pMsgBuf) {
  if ((pToken->type != TK_NOW && pToken->type != TK_TODAY && pToken->type != TK_NK_INTEGER &&
       pToken->type != TK_NK_STRING && pToken->type != TK_NK_FLOAT && pToken->type != TK_NK_BOOL &&
       pToken->type != TK_NULL && pToken->type != TK_NK_HEX && pToken->type != TK_NK_OCT &&
       pToken->type != TK_NK_BIN) ||
      (pToken->n == 0) || (pToken->type == TK_NK_RP)) {
    return buildSyntaxErrMsg(pMsgBuf, "invalid data or symbol", pToken->z);
  }

  // Remove quotation marks
  if (TK_NK_STRING == pToken->type) {
    if (pToken->n >= TSDB_MAX_BYTES_PER_ROW) {
      return buildSyntaxErrMsg(pMsgBuf, "too long string", pToken->z);
582
    }
X
Xiaoyu Wang 已提交
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612

    int32_t len = trimString(pToken->z, pToken->n, tmpTokenBuf, TSDB_MAX_BYTES_PER_ROW);
    pToken->z = tmpTokenBuf;
    pToken->n = len;
  }

  return TSDB_CODE_SUCCESS;
}

// pSql -> tag1_value, ...)
static int32_t parseTagsClauseImpl(SInsertParseContext* pCxt, SVnodeModifOpStmt* pStmt) {
  int32_t  code = TSDB_CODE_SUCCESS;
  SSchema* pSchema = getTableTagSchema(pStmt->pTableMeta);
  SArray*  pTagVals = taosArrayInit(pCxt->tags.numOfBound, sizeof(STagVal));
  SArray*  pTagName = taosArrayInit(8, TSDB_COL_NAME_LEN);
  SToken   token;
  bool     isParseBindParam = false;
  bool     isJson = false;
  STag*    pTag = NULL;
  for (int i = 0; TSDB_CODE_SUCCESS == code && i < pCxt->tags.numOfBound; ++i) {
    NEXT_TOKEN_WITH_PREV(pStmt->pSql, token);

    if (token.type == TK_NK_QUESTION) {
      isParseBindParam = true;
      if (NULL == pCxt->pComCxt->pStmtCb) {
        code = buildSyntaxErrMsg(&pCxt->msg, "? only used in stmt", token.z);
        break;
      }

      continue;
613
    }
X
Xiaoyu Wang 已提交
614 615 616 617

    if (isParseBindParam) {
      code = buildInvalidOperationMsg(&pCxt->msg, "no mix usage for ? and tag values");
      break;
618
    }
X
Xiaoyu Wang 已提交
619 620 621 622 623 624

    SSchema* pTagSchema = &pSchema[pCxt->tags.boundColumns[i]];
    isJson = pTagSchema->type == TSDB_DATA_TYPE_JSON;
    code = checkAndTrimValue(&token, pCxt->tmpTokenBuf, &pCxt->msg);
    if (TSDB_CODE_SUCCESS == code) {
      code = parseTagValue(pCxt, pStmt, pTagSchema, &token, pTagName, pTagVals, &pTag);
C
Cary Xu 已提交
625
    }
626 627
  }

X
Xiaoyu Wang 已提交
628 629 630
  if (TSDB_CODE_SUCCESS == code && !isParseBindParam && !isJson) {
    code = tTagNew(pTagVals, 1, false, &pTag);
  }
631

X
Xiaoyu Wang 已提交
632 633 634 635 636 637 638 639 640
  if (TSDB_CODE_SUCCESS == code && !isParseBindParam) {
    buildCreateTbReq(pStmt, pTag, pTagName);
    pTag = NULL;
  }

  for (int i = 0; i < taosArrayGetSize(pTagVals); ++i) {
    STagVal* p = (STagVal*)taosArrayGet(pTagVals, i);
    if (IS_VAR_DATA_TYPE(p->type)) {
      taosMemoryFreeClear(p->pData);
641
    }
X
Xiaoyu Wang 已提交
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
  }
  taosArrayDestroy(pTagVals);
  taosArrayDestroy(pTagName);
  tTagFree(pTag);
  return code;
}

// input pStmt->pSql:  TAGS (tag1_value, ...) [table_options] ...
// output pStmt->pSql: [table_options] ...
static int32_t parseTagsClause(SInsertParseContext* pCxt, SVnodeModifOpStmt* pStmt) {
  SToken token;
  NEXT_TOKEN(pStmt->pSql, token);
  if (TK_TAGS != token.type) {
    return buildSyntaxErrMsg(&pCxt->msg, "TAGS is expected", token.z);
  }

  NEXT_TOKEN(pStmt->pSql, token);
  if (TK_NK_LP != token.type) {
    return buildSyntaxErrMsg(&pCxt->msg, "( is expected", token.z);
  }

  int32_t code = parseTagsClauseImpl(pCxt, pStmt);
  if (TSDB_CODE_SUCCESS == code) {
    NEXT_VALID_TOKEN(pStmt->pSql, token);
    if (TK_NK_COMMA == token.type) {
      code = generateSyntaxErrMsg(&pCxt->msg, TSDB_CODE_PAR_TAGS_NOT_MATCHED);
    } else if (TK_NK_RP != token.type) {
      code = buildSyntaxErrMsg(&pCxt->msg, ") is expected", token.z);
670 671
    }
  }
X
Xiaoyu Wang 已提交
672 673
  return code;
}
674

X
Xiaoyu Wang 已提交
675
static int32_t storeTableMeta(SInsertParseContext* pCxt, SVnodeModifOpStmt* pStmt) {
X
Xiaoyu Wang 已提交
676
  pStmt->pTableMeta->suid = pStmt->pTableMeta->uid;
X
Xiaoyu Wang 已提交
677 678 679 680 681 682
  pStmt->pTableMeta->uid = pStmt->totalTbNum;
  pStmt->pTableMeta->tableType = TSDB_CHILD_TABLE;

  STableMeta* pBackup = NULL;
  if (TSDB_CODE_SUCCESS != cloneTableMeta(pStmt->pTableMeta, &pBackup)) {
    return TSDB_CODE_OUT_OF_MEMORY;
683
  }
684

X
Xiaoyu Wang 已提交
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
  char tbFName[TSDB_TABLE_FNAME_LEN];
  tNameExtractFullName(&pStmt->targetTableName, tbFName);
  return taosHashPut(pStmt->pSubTableHashObj, tbFName, strlen(tbFName), &pBackup, POINTER_BYTES);
}

static int32_t parseTableOptions(SInsertParseContext* pCxt, SVnodeModifOpStmt* pStmt) {
  do {
    int32_t index = 0;
    SToken  token;
    NEXT_TOKEN_KEEP_SQL(pStmt->pSql, token, index);
    if (TK_TTL == token.type) {
      pStmt->pSql += index;
      NEXT_TOKEN_WITH_PREV(pStmt->pSql, token);
      if (TK_NK_INTEGER != token.type) {
        return buildSyntaxErrMsg(&pCxt->msg, "Invalid option ttl", token.z);
      }
      pStmt->createTblReq.ttl = taosStr2Int32(token.z, NULL, 10);
      if (pStmt->createTblReq.ttl < 0) {
        return buildSyntaxErrMsg(&pCxt->msg, "Invalid option ttl", token.z);
      }
    } else if (TK_COMMENT == token.type) {
      pStmt->pSql += index;
      NEXT_TOKEN(pStmt->pSql, token);
      if (TK_NK_STRING != token.type) {
        return buildSyntaxErrMsg(&pCxt->msg, "Invalid option comment", token.z);
      }
      if (token.n >= TSDB_TB_COMMENT_LEN) {
        return buildSyntaxErrMsg(&pCxt->msg, "comment too long", token.z);
      }
      int32_t len = trimString(token.z, token.n, pCxt->tmpTokenBuf, TSDB_TB_COMMENT_LEN);
      pStmt->createTblReq.comment = strndup(pCxt->tmpTokenBuf, len);
      if (NULL == pStmt->createTblReq.comment) {
        return TSDB_CODE_OUT_OF_MEMORY;
      }
      pStmt->createTblReq.commentLen = len;
    } else {
      break;
    }
  } while (1);
724 725 726
  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
727 728 729 730 731 732 733
// input pStmt->pSql:
//   1. [(tag1_name, ...)] ...
//   2. VALUES ... | FILE ...
// output pStmt->pSql:
//   1. [(field1_name, ...)]
//   2. VALUES ... | FILE ...
static int32_t parseUsingClauseBottom(SInsertParseContext* pCxt, SVnodeModifOpStmt* pStmt) {
X
Xiaoyu Wang 已提交
734
  if (!pStmt->usingTableProcessing || pCxt->usingDuplicateTable) {
X
Xiaoyu Wang 已提交
735 736
    return TSDB_CODE_SUCCESS;
  }
wmmhello's avatar
wmmhello 已提交
737

X
Xiaoyu Wang 已提交
738 739 740 741 742 743 744 745 746 747 748
  int32_t code = parseBoundTagsClause(pCxt, pStmt);
  if (TSDB_CODE_SUCCESS == code) {
    code = parseTagsClause(pCxt, pStmt);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = parseTableOptions(pCxt, pStmt);
  }

  return code;
}

X
Xiaoyu Wang 已提交
749
static int32_t checkAuth(SParseContext* pCxt, SName* pTbName, bool* pMissCache) {
X
Xiaoyu Wang 已提交
750 751
  char dbFName[TSDB_DB_FNAME_LEN];
  tNameGetFullDbName(pTbName, dbFName);
X
Xiaoyu Wang 已提交
752 753 754
  int32_t code = TSDB_CODE_SUCCESS;
  bool    pass = true;
  bool    exists = true;
X
Xiaoyu Wang 已提交
755
  if (pCxt->async) {
X
Xiaoyu Wang 已提交
756
    code = catalogChkAuthFromCache(pCxt->pCatalog, pCxt->pUser, dbFName, AUTH_TYPE_WRITE, &pass, &exists);
X
Xiaoyu Wang 已提交
757
  } else {
X
Xiaoyu Wang 已提交
758 759 760 761
    SRequestConnInfo conn = {.pTrans = pCxt->pTransporter,
                             .requestId = pCxt->requestId,
                             .requestObjRefId = pCxt->requestRid,
                             .mgmtEps = pCxt->mgmtEpSet};
X
Xiaoyu Wang 已提交
762 763
    code = catalogChkAuth(pCxt->pCatalog, &conn, pCxt->pUser, dbFName, AUTH_TYPE_WRITE, &pass);
  }
X
Xiaoyu Wang 已提交
764 765 766 767 768 769
  if (TSDB_CODE_SUCCESS == code) {
    if (!exists) {
      *pMissCache = true;
    } else if (!pass) {
      code = TSDB_CODE_PAR_PERMISSION_DENIED;
    }
X
Xiaoyu Wang 已提交
770 771 772 773 774 775
  }
  return code;
}

static int32_t getTableMeta(SInsertParseContext* pCxt, SName* pTbName, bool isStb, STableMeta** pTableMeta,
                            bool* pMissCache) {
X
Xiaoyu Wang 已提交
776 777
  SParseContext* pComCxt = pCxt->pComCxt;
  int32_t        code = TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
778 779
  if (pComCxt->async) {
    if (isStb) {
X
Xiaoyu Wang 已提交
780
      code = catalogGetCachedSTableMeta(pComCxt->pCatalog, pTbName, pTableMeta);
X
Xiaoyu Wang 已提交
781
    } else {
X
Xiaoyu Wang 已提交
782
      code = catalogGetCachedTableMeta(pComCxt->pCatalog, pTbName, pTableMeta);
X
Xiaoyu Wang 已提交
783 784
    }
  } else {
X
Xiaoyu Wang 已提交
785 786 787 788
    SRequestConnInfo conn = {.pTrans = pComCxt->pTransporter,
                             .requestId = pComCxt->requestId,
                             .requestObjRefId = pComCxt->requestRid,
                             .mgmtEps = pComCxt->mgmtEpSet};
X
Xiaoyu Wang 已提交
789 790 791 792
    if (isStb) {
      code = catalogGetSTableMeta(pComCxt->pCatalog, &conn, pTbName, pTableMeta);
    } else {
      code = catalogGetTableMeta(pComCxt->pCatalog, &conn, pTbName, pTableMeta);
wmmhello's avatar
wmmhello 已提交
793
    }
X
Xiaoyu Wang 已提交
794 795 796 797 798 799
  }
  if (TSDB_CODE_SUCCESS == code) {
    if (NULL == *pTableMeta) {
      *pMissCache = true;
    } else if (isStb && TSDB_SUPER_TABLE != (*pTableMeta)->tableType) {
      code = buildInvalidOperationMsg(&pCxt->msg, "create table only from super table is allowed");
X
Xiaoyu Wang 已提交
800 801
    } else if (!isStb && TSDB_SUPER_TABLE == (*pTableMeta)->tableType) {
      code = buildInvalidOperationMsg(&pCxt->msg, "insert data into super table is not supported");
X
Xiaoyu Wang 已提交
802 803 804 805 806 807
    }
  }
  return code;
}

static int32_t getTableVgroup(SParseContext* pCxt, SVnodeModifOpStmt* pStmt, bool isStb, bool* pMissCache) {
X
Xiaoyu Wang 已提交
808 809 810
  int32_t     code = TSDB_CODE_SUCCESS;
  SVgroupInfo vg;
  bool        exists = true;
X
Xiaoyu Wang 已提交
811
  if (pCxt->async) {
X
Xiaoyu Wang 已提交
812
    code = catalogGetCachedTableHashVgroup(pCxt->pCatalog, &pStmt->targetTableName, &vg, &exists);
X
Xiaoyu Wang 已提交
813
  } else {
X
Xiaoyu Wang 已提交
814 815 816 817
    SRequestConnInfo conn = {.pTrans = pCxt->pTransporter,
                             .requestId = pCxt->requestId,
                             .requestObjRefId = pCxt->requestRid,
                             .mgmtEps = pCxt->mgmtEpSet};
X
Xiaoyu Wang 已提交
818 819 820 821 822 823 824 825 826 827 828 829 830
    code = catalogGetTableHashVgroup(pCxt->pCatalog, &conn, &pStmt->targetTableName, &vg);
  }
  if (TSDB_CODE_SUCCESS == code) {
    if (exists) {
      if (isStb) {
        pStmt->pTableMeta->vgId = vg.vgId;
      }
      code = taosHashPut(pStmt->pVgroupsHashObj, (const char*)&vg.vgId, sizeof(vg.vgId), (char*)&vg, sizeof(vg));
    }
    *pMissCache = !exists;
  }
  return code;
}
831

X
Xiaoyu Wang 已提交
832
static int32_t getTargetTableSchema(SInsertParseContext* pCxt, SVnodeModifOpStmt* pStmt) {
833 834 835 836 837
  if (pCxt->forceUpdate) {
    pCxt->missCache = true;
    return TSDB_CODE_SUCCESS;
  }

X
Xiaoyu Wang 已提交
838 839
  int32_t code = checkAuth(pCxt->pComCxt, &pStmt->targetTableName, &pCxt->missCache);
  if (TSDB_CODE_SUCCESS == code && !pCxt->missCache) {
X
Xiaoyu Wang 已提交
840 841 842 843 844 845 846 847 848 849 850 851 852
    code = getTableMeta(pCxt, &pStmt->targetTableName, false, &pStmt->pTableMeta, &pCxt->missCache);
  }
  if (TSDB_CODE_SUCCESS == code && !pCxt->missCache) {
    code = getTableVgroup(pCxt->pComCxt, pStmt, false, &pCxt->missCache);
  }
  return code;
}

static int32_t preParseUsingTableName(SInsertParseContext* pCxt, SVnodeModifOpStmt* pStmt, SToken* pTbName) {
  return insCreateSName(&pStmt->usingTableName, pTbName, pCxt->pComCxt->acctId, pCxt->pComCxt->db, &pCxt->msg);
}

static int32_t getUsingTableSchema(SInsertParseContext* pCxt, SVnodeModifOpStmt* pStmt) {
853 854 855 856 857
  if (pCxt->forceUpdate) {
    pCxt->missCache = true;
    return TSDB_CODE_SUCCESS;
  }

X
Xiaoyu Wang 已提交
858 859
  int32_t code = checkAuth(pCxt->pComCxt, &pStmt->targetTableName, &pCxt->missCache);
  if (TSDB_CODE_SUCCESS == code && !pCxt->missCache) {
X
Xiaoyu Wang 已提交
860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
    code = getTableMeta(pCxt, &pStmt->usingTableName, true, &pStmt->pTableMeta, &pCxt->missCache);
  }
  if (TSDB_CODE_SUCCESS == code && !pCxt->missCache) {
    code = getTableVgroup(pCxt->pComCxt, pStmt, true, &pCxt->missCache);
  }
  return code;
}

static int32_t parseUsingTableNameImpl(SInsertParseContext* pCxt, SVnodeModifOpStmt* pStmt) {
  SToken token;
  NEXT_TOKEN(pStmt->pSql, token);
  int32_t code = preParseUsingTableName(pCxt, pStmt, &token);
  if (TSDB_CODE_SUCCESS == code) {
    code = getUsingTableSchema(pCxt, pStmt);
  }
X
Xiaoyu Wang 已提交
875
  if (TSDB_CODE_SUCCESS == code && !pCxt->missCache) {
X
Xiaoyu Wang 已提交
876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894
    code = storeTableMeta(pCxt, pStmt);
  }
  return code;
}

// input pStmt->pSql:
//   1(care). [USING stb_name [(tag1_name, ...)] TAGS (tag1_value, ...) [table_options]] ...
//   2. VALUES ... | FILE ...
// output pStmt->pSql:
//   1. [(tag1_name, ...)] TAGS (tag1_value, ...) [table_options]] ...
//   2. VALUES ... | FILE ...
static int32_t parseUsingTableName(SInsertParseContext* pCxt, SVnodeModifOpStmt* pStmt) {
  SToken  token;
  int32_t index = 0;
  NEXT_TOKEN_KEEP_SQL(pStmt->pSql, token, index);
  if (TK_USING != token.type) {
    return getTargetTableSchema(pCxt, pStmt);
  }

X
Xiaoyu Wang 已提交
895
  pStmt->usingTableProcessing = true;
X
Xiaoyu Wang 已提交
896 897
  // pStmt->pSql -> stb_name [(tag1_name, ...)
  pStmt->pSql += index;
X
Xiaoyu Wang 已提交
898 899
  int32_t code = parseDuplicateUsingClause(pCxt, pStmt, &pCxt->usingDuplicateTable);
  if (TSDB_CODE_SUCCESS == code && !pCxt->usingDuplicateTable) {
X
Xiaoyu Wang 已提交
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
    return parseUsingTableNameImpl(pCxt, pStmt);
  }
  return code;
}

static int32_t preParseTargetTableName(SInsertParseContext* pCxt, SVnodeModifOpStmt* pStmt, SToken* pTbName) {
  return insCreateSName(&pStmt->targetTableName, pTbName, pCxt->pComCxt->acctId, pCxt->pComCxt->db, &pCxt->msg);
}

// input pStmt->pSql:
//   1(care). [(field1_name, ...)] ...
//   2. [ USING ... ] ...
//   3. VALUES ... | FILE ...
// output pStmt->pSql:
//   1. [ USING ... ] ...
//   2. VALUES ... | FILE ...
static int32_t preParseBoundColumnsClause(SInsertParseContext* pCxt, SVnodeModifOpStmt* pStmt) {
  SToken  token;
  int32_t index = 0;
  NEXT_TOKEN_KEEP_SQL(pStmt->pSql, token, index);
  if (TK_NK_LP != token.type) {
921 922 923
    return TSDB_CODE_SUCCESS;
  }

X
Xiaoyu Wang 已提交
924 925 926 927 928 929 930 931
  // pStmt->pSql -> field1_name, ...)
  pStmt->pSql += index;
  pStmt->pBoundCols = pStmt->pSql;
  return skipParentheses(pCxt, &pStmt->pSql);
}

static int32_t getTableDataBlocks(SInsertParseContext* pCxt, SVnodeModifOpStmt* pStmt, STableDataBlocks** pDataBuf) {
  if (pCxt->pComCxt->async) {
932 933 934 935 936 937
    uint64_t uid = pStmt->pTableMeta->uid;
    if (pStmt->usingTableProcessing) {
      pStmt->pTableMeta->uid = 0;
    }
    
    return insGetDataBlockFromList(pStmt->pTableBlockHashObj, &uid, sizeof(pStmt->pTableMeta->uid),
X
Xiaoyu Wang 已提交
938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959
                                   TSDB_DEFAULT_PAYLOAD_SIZE, sizeof(SSubmitBlk),
                                   getTableInfo(pStmt->pTableMeta).rowSize, pStmt->pTableMeta, pDataBuf, NULL,
                                   &pStmt->createTblReq);
  }
  char tbFName[TSDB_TABLE_FNAME_LEN];
  tNameExtractFullName(&pStmt->targetTableName, tbFName);
  return insGetDataBlockFromList(pStmt->pTableBlockHashObj, tbFName, strlen(tbFName), TSDB_DEFAULT_PAYLOAD_SIZE,
                                 sizeof(SSubmitBlk), getTableInfo(pStmt->pTableMeta).rowSize, pStmt->pTableMeta,
                                 pDataBuf, NULL, &pStmt->createTblReq);
}

static int32_t parseBoundColumnsClause(SInsertParseContext* pCxt, SVnodeModifOpStmt* pStmt,
                                       STableDataBlocks* pDataBuf) {
  SToken  token;
  int32_t index = 0;
  NEXT_TOKEN_KEEP_SQL(pStmt->pSql, token, index);
  if (TK_NK_LP == token.type) {
    pStmt->pSql += index;
    if (NULL != pStmt->pBoundCols) {
      return buildSyntaxErrMsg(&pCxt->msg, "keyword VALUES or FILE is expected", token.z);
    }
    // pStmt->pSql -> field1_name, ...)
X
Xiaoyu Wang 已提交
960 961
    return parseBoundColumns(pCxt, &pStmt->pSql, false, &pDataBuf->boundColumnInfo,
                             getTableColumnSchema(pStmt->pTableMeta));
X
Xiaoyu Wang 已提交
962 963 964
  }

  if (NULL != pStmt->pBoundCols) {
X
Xiaoyu Wang 已提交
965
    return parseBoundColumns(pCxt, &pStmt->pBoundCols, false, &pDataBuf->boundColumnInfo,
X
Xiaoyu Wang 已提交
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
                             getTableColumnSchema(pStmt->pTableMeta));
  }

  return TSDB_CODE_SUCCESS;
}

// input pStmt->pSql:
//   1. [(tag1_name, ...)] ...
//   2. VALUES ... | FILE ...
// output pStmt->pSql: VALUES ... | FILE ...
static int32_t parseSchemaClauseBottom(SInsertParseContext* pCxt, SVnodeModifOpStmt* pStmt,
                                       STableDataBlocks** pDataBuf) {
  int32_t code = parseUsingClauseBottom(pCxt, pStmt);
  if (TSDB_CODE_SUCCESS == code) {
    code = getTableDataBlocks(pCxt, pStmt, pDataBuf);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = parseBoundColumnsClause(pCxt, pStmt, *pDataBuf);
  }
  return code;
}

// input pStmt->pSql: [(field1_name, ...)] [ USING ... ] VALUES ... | FILE ...
// output pStmt->pSql:
//   1. [(tag1_name, ...)] ...
//   2. VALUES ... | FILE ...
static int32_t parseSchemaClauseTop(SInsertParseContext* pCxt, SVnodeModifOpStmt* pStmt, SToken* pTbName) {
  int32_t code = preParseTargetTableName(pCxt, pStmt, pTbName);
  if (TSDB_CODE_SUCCESS == code) {
    // option: [(field1_name, ...)]
    code = preParseBoundColumnsClause(pCxt, pStmt);
  }
  if (TSDB_CODE_SUCCESS == code) {
    // option: [USING stb_name]
    code = parseUsingTableName(pCxt, pStmt);
  }
  return code;
}

static int32_t parseValueTokenImpl(SInsertParseContext* pCxt, const char** pSql, SToken* pToken, SSchema* pSchema,
                                   int16_t timePrec, _row_append_fn_t func, void* param) {
  int64_t  iv;
  uint64_t uv;
  char*    endptr = NULL;
C
Cary Xu 已提交
1010

wmmhello's avatar
wmmhello 已提交
1011 1012 1013 1014
  switch (pSchema->type) {
    case TSDB_DATA_TYPE_BOOL: {
      if ((pToken->type == TK_NK_BOOL || pToken->type == TK_NK_STRING) && (pToken->n != 0)) {
        if (strncmp(pToken->z, "true", pToken->n) == 0) {
X
Xiaoyu Wang 已提交
1015
          return func(&pCxt->msg, &TRUE_VALUE, pSchema->bytes, param);
wmmhello's avatar
wmmhello 已提交
1016
        } else if (strncmp(pToken->z, "false", pToken->n) == 0) {
X
Xiaoyu Wang 已提交
1017
          return func(&pCxt->msg, &FALSE_VALUE, pSchema->bytes, param);
wmmhello's avatar
wmmhello 已提交
1018
        } else {
X
Xiaoyu Wang 已提交
1019
          return buildSyntaxErrMsg(&pCxt->msg, "invalid bool data", pToken->z);
wmmhello's avatar
wmmhello 已提交
1020 1021
        }
      } else if (pToken->type == TK_NK_INTEGER) {
X
Xiaoyu Wang 已提交
1022 1023
        return func(&pCxt->msg, ((taosStr2Int64(pToken->z, NULL, 10) == 0) ? &FALSE_VALUE : &TRUE_VALUE),
                    pSchema->bytes, param);
wmmhello's avatar
wmmhello 已提交
1024
      } else if (pToken->type == TK_NK_FLOAT) {
X
Xiaoyu Wang 已提交
1025 1026
        return func(&pCxt->msg, ((taosStr2Double(pToken->z, NULL) == 0) ? &FALSE_VALUE : &TRUE_VALUE), pSchema->bytes,
                    param);
wmmhello's avatar
wmmhello 已提交
1027
      } else {
X
Xiaoyu Wang 已提交
1028
        return buildSyntaxErrMsg(&pCxt->msg, "invalid bool data", pToken->z);
D
dapan1121 已提交
1029
      }
wmmhello's avatar
wmmhello 已提交
1030
    }
1031

wmmhello's avatar
wmmhello 已提交
1032 1033
    case TSDB_DATA_TYPE_TINYINT: {
      if (TSDB_CODE_SUCCESS != toInteger(pToken->z, pToken->n, 10, &iv)) {
X
Xiaoyu Wang 已提交
1034
        return buildSyntaxErrMsg(&pCxt->msg, "invalid tinyint data", pToken->z);
wmmhello's avatar
wmmhello 已提交
1035
      } else if (!IS_VALID_TINYINT(iv)) {
X
Xiaoyu Wang 已提交
1036
        return buildSyntaxErrMsg(&pCxt->msg, "tinyint data overflow", pToken->z);
D
dapan1121 已提交
1037
      }
wmmhello's avatar
wmmhello 已提交
1038

X
Xiaoyu Wang 已提交
1039 1040
      uint8_t tmpVal = (uint8_t)iv;
      return func(&pCxt->msg, &tmpVal, pSchema->bytes, param);
1041 1042
    }

wmmhello's avatar
wmmhello 已提交
1043 1044
    case TSDB_DATA_TYPE_UTINYINT: {
      if (TSDB_CODE_SUCCESS != toUInteger(pToken->z, pToken->n, 10, &uv)) {
X
Xiaoyu Wang 已提交
1045
        return buildSyntaxErrMsg(&pCxt->msg, "invalid unsigned tinyint data", pToken->z);
X
Xiaoyu Wang 已提交
1046
      } else if (uv > UINT8_MAX) {
X
Xiaoyu Wang 已提交
1047
        return buildSyntaxErrMsg(&pCxt->msg, "unsigned tinyint data overflow", pToken->z);
wmmhello's avatar
wmmhello 已提交
1048
      }
X
Xiaoyu Wang 已提交
1049 1050
      uint8_t tmpVal = (uint8_t)uv;
      return func(&pCxt->msg, &tmpVal, pSchema->bytes, param);
wmmhello's avatar
wmmhello 已提交
1051
    }
1052

wmmhello's avatar
wmmhello 已提交
1053 1054
    case TSDB_DATA_TYPE_SMALLINT: {
      if (TSDB_CODE_SUCCESS != toInteger(pToken->z, pToken->n, 10, &iv)) {
X
Xiaoyu Wang 已提交
1055
        return buildSyntaxErrMsg(&pCxt->msg, "invalid smallint data", pToken->z);
wmmhello's avatar
wmmhello 已提交
1056
      } else if (!IS_VALID_SMALLINT(iv)) {
X
Xiaoyu Wang 已提交
1057
        return buildSyntaxErrMsg(&pCxt->msg, "smallint data overflow", pToken->z);
wmmhello's avatar
wmmhello 已提交
1058
      }
X
Xiaoyu Wang 已提交
1059 1060
      int16_t tmpVal = (int16_t)iv;
      return func(&pCxt->msg, &tmpVal, pSchema->bytes, param);
wmmhello's avatar
wmmhello 已提交
1061
    }
1062

wmmhello's avatar
wmmhello 已提交
1063 1064
    case TSDB_DATA_TYPE_USMALLINT: {
      if (TSDB_CODE_SUCCESS != toUInteger(pToken->z, pToken->n, 10, &uv)) {
X
Xiaoyu Wang 已提交
1065
        return buildSyntaxErrMsg(&pCxt->msg, "invalid unsigned smallint data", pToken->z);
X
Xiaoyu Wang 已提交
1066
      } else if (uv > UINT16_MAX) {
X
Xiaoyu Wang 已提交
1067
        return buildSyntaxErrMsg(&pCxt->msg, "unsigned smallint data overflow", pToken->z);
wmmhello's avatar
wmmhello 已提交
1068
      }
X
Xiaoyu Wang 已提交
1069 1070
      uint16_t tmpVal = (uint16_t)uv;
      return func(&pCxt->msg, &tmpVal, pSchema->bytes, param);
wmmhello's avatar
wmmhello 已提交
1071 1072 1073 1074
    }

    case TSDB_DATA_TYPE_INT: {
      if (TSDB_CODE_SUCCESS != toInteger(pToken->z, pToken->n, 10, &iv)) {
X
Xiaoyu Wang 已提交
1075
        return buildSyntaxErrMsg(&pCxt->msg, "invalid int data", pToken->z);
wmmhello's avatar
wmmhello 已提交
1076
      } else if (!IS_VALID_INT(iv)) {
X
Xiaoyu Wang 已提交
1077
        return buildSyntaxErrMsg(&pCxt->msg, "int data overflow", pToken->z);
wmmhello's avatar
wmmhello 已提交
1078
      }
X
Xiaoyu Wang 已提交
1079 1080
      int32_t tmpVal = (int32_t)iv;
      return func(&pCxt->msg, &tmpVal, pSchema->bytes, param);
wmmhello's avatar
wmmhello 已提交
1081 1082 1083 1084
    }

    case TSDB_DATA_TYPE_UINT: {
      if (TSDB_CODE_SUCCESS != toUInteger(pToken->z, pToken->n, 10, &uv)) {
X
Xiaoyu Wang 已提交
1085
        return buildSyntaxErrMsg(&pCxt->msg, "invalid unsigned int data", pToken->z);
X
Xiaoyu Wang 已提交
1086
      } else if (uv > UINT32_MAX) {
X
Xiaoyu Wang 已提交
1087
        return buildSyntaxErrMsg(&pCxt->msg, "unsigned int data overflow", pToken->z);
wmmhello's avatar
wmmhello 已提交
1088
      }
X
Xiaoyu Wang 已提交
1089 1090
      uint32_t tmpVal = (uint32_t)uv;
      return func(&pCxt->msg, &tmpVal, pSchema->bytes, param);
wmmhello's avatar
wmmhello 已提交
1091 1092 1093 1094
    }

    case TSDB_DATA_TYPE_BIGINT: {
      if (TSDB_CODE_SUCCESS != toInteger(pToken->z, pToken->n, 10, &iv)) {
X
Xiaoyu Wang 已提交
1095
        return buildSyntaxErrMsg(&pCxt->msg, "invalid bigint data", pToken->z);
wmmhello's avatar
wmmhello 已提交
1096
      }
X
Xiaoyu Wang 已提交
1097
      return func(&pCxt->msg, &iv, pSchema->bytes, param);
wmmhello's avatar
wmmhello 已提交
1098 1099 1100 1101
    }

    case TSDB_DATA_TYPE_UBIGINT: {
      if (TSDB_CODE_SUCCESS != toUInteger(pToken->z, pToken->n, 10, &uv)) {
X
Xiaoyu Wang 已提交
1102
        return buildSyntaxErrMsg(&pCxt->msg, "invalid unsigned bigint data", pToken->z);
wmmhello's avatar
wmmhello 已提交
1103
      }
X
Xiaoyu Wang 已提交
1104
      return func(&pCxt->msg, &uv, pSchema->bytes, param);
wmmhello's avatar
wmmhello 已提交
1105 1106 1107 1108 1109
    }

    case TSDB_DATA_TYPE_FLOAT: {
      double dv;
      if (TK_NK_ILLEGAL == toDouble(pToken, &dv, &endptr)) {
X
Xiaoyu Wang 已提交
1110
        return buildSyntaxErrMsg(&pCxt->msg, "illegal float data", pToken->z);
wmmhello's avatar
wmmhello 已提交
1111 1112 1113
      }
      if (((dv == HUGE_VAL || dv == -HUGE_VAL) && errno == ERANGE) || dv > FLT_MAX || dv < -FLT_MAX || isinf(dv) ||
          isnan(dv)) {
X
Xiaoyu Wang 已提交
1114
        return buildSyntaxErrMsg(&pCxt->msg, "illegal float data", pToken->z);
wmmhello's avatar
wmmhello 已提交
1115
      }
X
Xiaoyu Wang 已提交
1116 1117
      float tmpVal = (float)dv;
      return func(&pCxt->msg, &tmpVal, pSchema->bytes, param);
wmmhello's avatar
wmmhello 已提交
1118 1119 1120 1121 1122
    }

    case TSDB_DATA_TYPE_DOUBLE: {
      double dv;
      if (TK_NK_ILLEGAL == toDouble(pToken, &dv, &endptr)) {
X
Xiaoyu Wang 已提交
1123
        return buildSyntaxErrMsg(&pCxt->msg, "illegal double data", pToken->z);
wmmhello's avatar
wmmhello 已提交
1124 1125
      }
      if (((dv == HUGE_VAL || dv == -HUGE_VAL) && errno == ERANGE) || isinf(dv) || isnan(dv)) {
X
Xiaoyu Wang 已提交
1126
        return buildSyntaxErrMsg(&pCxt->msg, "illegal double data", pToken->z);
wmmhello's avatar
wmmhello 已提交
1127
      }
X
Xiaoyu Wang 已提交
1128
      return func(&pCxt->msg, &dv, pSchema->bytes, param);
wmmhello's avatar
wmmhello 已提交
1129 1130 1131 1132 1133
    }

    case TSDB_DATA_TYPE_BINARY: {
      // Too long values will raise the invalid sql error message
      if (pToken->n + VARSTR_HEADER_SIZE > pSchema->bytes) {
X
Xiaoyu Wang 已提交
1134
        return generateSyntaxErrMsg(&pCxt->msg, TSDB_CODE_PAR_VALUE_TOO_LONG, pSchema->name);
D
stmt  
dapan1121 已提交
1135 1136
      }

X
Xiaoyu Wang 已提交
1137
      return func(&pCxt->msg, pToken->z, pToken->n, param);
X
Xiaoyu Wang 已提交
1138
    }
1139

X
Xiaoyu Wang 已提交
1140 1141
    case TSDB_DATA_TYPE_NCHAR: {
      return func(&pCxt->msg, pToken->z, pToken->n, param);
1142
    }
X
Xiaoyu Wang 已提交
1143 1144 1145
    case TSDB_DATA_TYPE_JSON: {
      if (pToken->n > (TSDB_MAX_JSON_TAG_LEN - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE) {
        return buildSyntaxErrMsg(&pCxt->msg, "json string too long than 4095", pToken->z);
1146
      }
X
Xiaoyu Wang 已提交
1147
      return func(&pCxt->msg, pToken->z, pToken->n, param);
1148
    }
X
Xiaoyu Wang 已提交
1149 1150 1151 1152 1153
    case TSDB_DATA_TYPE_TIMESTAMP: {
      int64_t tmpVal;
      if (parseTime(pSql, pToken, timePrec, &tmpVal, &pCxt->msg) != TSDB_CODE_SUCCESS) {
        return buildSyntaxErrMsg(&pCxt->msg, "invalid timestamp", pToken->z);
      }
1154

X
Xiaoyu Wang 已提交
1155 1156
      return func(&pCxt->msg, &tmpVal, pSchema->bytes, param);
    }
X
Xiaoyu Wang 已提交
1157
  }
1158

X
Xiaoyu Wang 已提交
1159 1160
  return TSDB_CODE_FAILED;
}
D
dapan 已提交
1161

X
Xiaoyu Wang 已提交
1162 1163 1164 1165 1166 1167 1168
static int32_t parseValueToken(SInsertParseContext* pCxt, const char** pSql, SToken* pToken, SSchema* pSchema,
                               int16_t timePrec, _row_append_fn_t func, void* param) {
  int32_t code = checkAndTrimValue(pToken, pCxt->tmpTokenBuf, &pCxt->msg);
  if (TSDB_CODE_SUCCESS == code && isNullValue(pSchema->type, pToken)) {
    if (TSDB_DATA_TYPE_TIMESTAMP == pSchema->type && PRIMARYKEY_TIMESTAMP_COL_ID == pSchema->colId) {
      return buildSyntaxErrMsg(&pCxt->msg, "primary timestamp should not be null", pToken->z);
    }
X
Xiaoyu Wang 已提交
1169

X
Xiaoyu Wang 已提交
1170
    return func(&pCxt->msg, NULL, 0, param);
1171 1172
  }

X
Xiaoyu Wang 已提交
1173 1174
  if (TSDB_CODE_SUCCESS == code && IS_NUMERIC_TYPE(pSchema->type) && pToken->n == 0) {
    return buildSyntaxErrMsg(&pCxt->msg, "invalid numeric data", pToken->z);
1175 1176
  }

X
Xiaoyu Wang 已提交
1177 1178
  if (TSDB_CODE_SUCCESS == code) {
    code = parseValueTokenImpl(pCxt, pSql, pToken, pSchema, timePrec, func, param);
X
Xiaoyu Wang 已提交
1179
  }
1180

X
Xiaoyu Wang 已提交
1181
  return code;
1182 1183
}

X
Xiaoyu Wang 已提交
1184 1185 1186 1187 1188 1189 1190 1191
static int parseOneRow(SInsertParseContext* pCxt, const char** pSql, STableDataBlocks* pDataBuf, bool* pGotRow,
                       SToken* pToken) {
  SRowBuilder*        pBuilder = &pDataBuf->rowBuilder;
  STSRow*             row = (STSRow*)(pDataBuf->pData + pDataBuf->size);  // skip the SSubmitBlk header
  SParsedDataColInfo* pCols = &pDataBuf->boundColumnInfo;
  bool                isParseBindParam = false;
  SSchema*            pSchemas = getTableColumnSchema(pDataBuf->pTableMeta);
  SMemParam           param = {.rb = pBuilder};
C
Cary Xu 已提交
1192

X
Xiaoyu Wang 已提交
1193
  int32_t code = tdSRowResetBuf(pBuilder, row);
1194
  // 1. set the parsed value from sql string
X
Xiaoyu Wang 已提交
1195 1196 1197
  for (int i = 0; i < pCols->numOfBound && TSDB_CODE_SUCCESS == code; ++i) {
    NEXT_TOKEN_WITH_PREV(*pSql, *pToken);
    SSchema* pSchema = &pSchemas[pCols->boundColumns[i]];
D
stmt  
dapan1121 已提交
1198

X
Xiaoyu Wang 已提交
1199
    if (pToken->type == TK_NK_QUESTION) {
D
stmt  
dapan1121 已提交
1200
      isParseBindParam = true;
X
Xiaoyu Wang 已提交
1201 1202
      if (NULL == pCxt->pComCxt->pStmtCb) {
        code = buildSyntaxErrMsg(&pCxt->msg, "? only used in stmt", pToken->z);
D
stmt  
dapan1121 已提交
1203 1204 1205 1206
      }
      continue;
    }

X
Xiaoyu Wang 已提交
1207 1208
    if (TSDB_CODE_SUCCESS == code && TK_NK_RP == pToken->type) {
      code = generateSyntaxErrMsg(&pCxt->msg, TSDB_CODE_PAR_INVALID_COLUMNS_NUM);
D
dapan1121 已提交
1209 1210
    }

X
Xiaoyu Wang 已提交
1211 1212
    if (TSDB_CODE_SUCCESS == code && isParseBindParam) {
      code = buildInvalidOperationMsg(&pCxt->msg, "no mix usage for ? and values");
D
stmt  
dapan1121 已提交
1213
    }
X
Xiaoyu Wang 已提交
1214

X
Xiaoyu Wang 已提交
1215 1216 1217 1218 1219 1220
    if (TSDB_CODE_SUCCESS == code) {
      param.schema = pSchema;
      insGetSTSRowAppendInfo(pBuilder->rowType, pCols, i, &param.toffset, &param.colIdx);
      code = parseValueToken(pCxt, pSql, pToken, pSchema, getTableInfo(pDataBuf->pTableMeta).precision, insMemRowAppend,
                             &param);
    }
1221

X
Xiaoyu Wang 已提交
1222 1223 1224 1225
    if (TSDB_CODE_SUCCESS == code && i < pCols->numOfBound - 1) {
      NEXT_VALID_TOKEN(*pSql, *pToken);
      if (TK_NK_COMMA != pToken->type) {
        code = buildSyntaxErrMsg(&pCxt->msg, ", expected", pToken->z);
X
Xiaoyu Wang 已提交
1226 1227
      }
    }
1228 1229
  }

X
Xiaoyu Wang 已提交
1230 1231 1232 1233
  if (TSDB_CODE_SUCCESS == code) {
    TSKEY tsKey = TD_ROW_KEY(row);
    code = insCheckTimestamp(pDataBuf, (const char*)&tsKey);
  }
1234

X
Xiaoyu Wang 已提交
1235
  if (TSDB_CODE_SUCCESS == code && !isParseBindParam) {
C
Cary Xu 已提交
1236
    // set the null value for the columns that do not assign values
X
Xiaoyu Wang 已提交
1237
    if ((pCols->numOfBound < pCols->numOfCols) && TD_IS_TP_ROW(row)) {
1238
      pBuilder->hasNone = true;
1239
    }
D
stmt  
dapan1121 已提交
1240

C
Cary Xu 已提交
1241 1242
    tdSRowEnd(pBuilder);

X
Xiaoyu Wang 已提交
1243
    *pGotRow = true;
X
Xiaoyu Wang 已提交
1244

C
Cary Xu 已提交
1245
#ifdef TD_DEBUG_PRINT_ROW
C
Cary Xu 已提交
1246
    STSchema* pSTSchema = tdGetSTSChemaFromSSChema(schema, spd->numOfCols, 1);
C
Cary Xu 已提交
1247
    tdSRowPrint(row, pSTSchema, __func__);
C
Cary Xu 已提交
1248 1249
    taosMemoryFree(pSTSchema);
#endif
1250 1251
  }

X
Xiaoyu Wang 已提交
1252
  return code;
1253 1254
}

X
Xiaoyu Wang 已提交
1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274
static int32_t allocateMemIfNeed(STableDataBlocks* pDataBlock, int32_t rowSize, int32_t* numOfRows) {
  size_t    remain = pDataBlock->nAllocSize - pDataBlock->size;
  const int factor = 5;
  uint32_t  nAllocSizeOld = pDataBlock->nAllocSize;

  // expand the allocated size
  if (remain < rowSize * factor) {
    while (remain < rowSize * factor) {
      pDataBlock->nAllocSize = (uint32_t)(pDataBlock->nAllocSize * 1.5);
      remain = pDataBlock->nAllocSize - pDataBlock->size;
    }

    char* tmp = taosMemoryRealloc(pDataBlock->pData, (size_t)pDataBlock->nAllocSize);
    if (tmp != NULL) {
      pDataBlock->pData = tmp;
      memset(pDataBlock->pData + pDataBlock->size, 0, pDataBlock->nAllocSize - pDataBlock->size);
    } else {
      // do nothing, if allocate more memory failed
      pDataBlock->nAllocSize = nAllocSizeOld;
      *numOfRows = (int32_t)(pDataBlock->nAllocSize - pDataBlock->headerSize) / rowSize;
S
Shengliang Guan 已提交
1275
      return TSDB_CODE_OUT_OF_MEMORY;
X
Xiaoyu Wang 已提交
1276 1277 1278 1279 1280 1281 1282
    }
  }

  *numOfRows = (int32_t)(pDataBlock->nAllocSize - pDataBlock->headerSize) / rowSize;
  return TSDB_CODE_SUCCESS;
}

1283
// pSql -> (field1_value, ...) [(field1_value2, ...) ...]
X
Xiaoyu Wang 已提交
1284 1285 1286 1287 1288 1289 1290
static int32_t parseValues(SInsertParseContext* pCxt, SVnodeModifOpStmt* pStmt, STableDataBlocks* pDataBuf,
                           int32_t maxRows, int32_t* pNumOfRows, SToken* pToken) {
  int32_t code = insInitRowBuilder(&pDataBuf->rowBuilder, pDataBuf->pTableMeta->sversion, &pDataBuf->boundColumnInfo);

  int32_t extendedRowSize = insGetExtendedRowSize(pDataBuf);
  (*pNumOfRows) = 0;
  while (TSDB_CODE_SUCCESS == code) {
1291
    int32_t index = 0;
X
Xiaoyu Wang 已提交
1292 1293
    NEXT_TOKEN_KEEP_SQL(pStmt->pSql, *pToken, index);
    if (TK_NK_LP != pToken->type) {
1294 1295
      break;
    }
X
Xiaoyu Wang 已提交
1296
    pStmt->pSql += index;
1297

X
Xiaoyu Wang 已提交
1298 1299
    if ((*pNumOfRows) >= maxRows || pDataBuf->size + extendedRowSize >= pDataBuf->nAllocSize) {
      code = allocateMemIfNeed(pDataBuf, extendedRowSize, &maxRows);
1300 1301
    }

D
stmt  
dapan1121 已提交
1302
    bool gotRow = false;
X
Xiaoyu Wang 已提交
1303 1304
    if (TSDB_CODE_SUCCESS == code) {
      code = parseOneRow(pCxt, &pStmt->pSql, pDataBuf, &gotRow, pToken);
D
stmt  
dapan1121 已提交
1305
    }
1306

X
Xiaoyu Wang 已提交
1307 1308 1309 1310 1311 1312 1313
    if (TSDB_CODE_SUCCESS == code) {
      NEXT_VALID_TOKEN(pStmt->pSql, *pToken);
      if (TK_NK_COMMA == pToken->type) {
        code = generateSyntaxErrMsg(&pCxt->msg, TSDB_CODE_PAR_INVALID_COLUMNS_NUM);
      } else if (TK_NK_RP != pToken->type) {
        code = buildSyntaxErrMsg(&pCxt->msg, ") expected", pToken->z);
      }
1314 1315
    }

X
Xiaoyu Wang 已提交
1316 1317 1318
    if (TSDB_CODE_SUCCESS == code && gotRow) {
      pDataBuf->size += extendedRowSize;
      (*pNumOfRows)++;
D
stmt  
dapan1121 已提交
1319
    }
1320 1321
  }

X
Xiaoyu Wang 已提交
1322 1323 1324
  if (TSDB_CODE_SUCCESS == code && 0 == (*pNumOfRows) &&
      (!TSDB_QUERY_HAS_TYPE(pStmt->insertType, TSDB_QUERY_TYPE_STMT_INSERT))) {
    code = buildSyntaxErrMsg(&pCxt->msg, "no any data points", NULL);
1325
  }
X
Xiaoyu Wang 已提交
1326
  return code;
1327 1328
}

X
Xiaoyu Wang 已提交
1329 1330 1331 1332
// VALUES (field1_value, ...) [(field1_value2, ...) ...]
static int32_t parseValuesClause(SInsertParseContext* pCxt, SVnodeModifOpStmt* pStmt, STableDataBlocks* pDataBuf,
                                 SToken* pToken) {
  int32_t maxNumOfRows = 0;
1333
  int32_t numOfRows = 0;
X
Xiaoyu Wang 已提交
1334 1335 1336
  int32_t code = allocateMemIfNeed(pDataBuf, insGetExtendedRowSize(pDataBuf), &maxNumOfRows);
  if (TSDB_CODE_SUCCESS == code) {
    code = parseValues(pCxt, pStmt, pDataBuf, maxNumOfRows, &numOfRows, pToken);
1337
  }
X
Xiaoyu Wang 已提交
1338 1339 1340 1341 1342 1343 1344 1345 1346 1347
  if (TSDB_CODE_SUCCESS == code) {
    code = insSetBlockInfo((SSubmitBlk*)(pDataBuf->pData), pDataBuf, numOfRows, &pCxt->msg);
  }
  if (TSDB_CODE_SUCCESS == code) {
    pDataBuf->numOfTables = 1;
    pStmt->totalRowsNum += numOfRows;
    pStmt->totalTbNum += 1;
    TSDB_QUERY_SET_TYPE(pStmt->insertType, TSDB_QUERY_TYPE_INSERT);
  }
  return code;
1348 1349
}

X
Xiaoyu Wang 已提交
1350 1351 1352
static int32_t parseCsvFile(SInsertParseContext* pCxt, SVnodeModifOpStmt* pStmt, STableDataBlocks* pDataBuf,
                            int maxRows, int32_t* pNumOfRows) {
  int32_t code = insInitRowBuilder(&pDataBuf->rowBuilder, pDataBuf->pTableMeta->sversion, &pDataBuf->boundColumnInfo);
X
Xiaoyu Wang 已提交
1353

X
Xiaoyu Wang 已提交
1354 1355
  int32_t extendedRowSize = insGetExtendedRowSize(pDataBuf);
  (*pNumOfRows) = 0;
X
Xiaoyu Wang 已提交
1356 1357
  char*   pLine = NULL;
  int64_t readLen = 0;
X
Xiaoyu Wang 已提交
1358
  pStmt->fileProcessing = false;
X
Xiaoyu Wang 已提交
1359
  while (TSDB_CODE_SUCCESS == code && (readLen = taosGetLineFile(pStmt->fp, &pLine)) != -1) {
X
Xiaoyu Wang 已提交
1360 1361 1362 1363 1364 1365 1366 1367
    if (('\r' == pLine[readLen - 1]) || ('\n' == pLine[readLen - 1])) {
      pLine[--readLen] = '\0';
    }

    if (readLen == 0) {
      continue;
    }

X
Xiaoyu Wang 已提交
1368 1369
    if ((*pNumOfRows) >= maxRows || pDataBuf->size + extendedRowSize >= pDataBuf->nAllocSize) {
      code = allocateMemIfNeed(pDataBuf, extendedRowSize, &maxRows);
X
Xiaoyu Wang 已提交
1370 1371
    }

X
Xiaoyu Wang 已提交
1372 1373 1374 1375
    bool gotRow = false;
    if (TSDB_CODE_SUCCESS == code) {
      SToken token;
      strtolower(pLine, pLine);
X
Xiaoyu Wang 已提交
1376 1377
      const char* pRow = pLine;
      code = parseOneRow(pCxt, (const char**)&pRow, pDataBuf, &gotRow, &token);
X
Xiaoyu Wang 已提交
1378
    }
X
Xiaoyu Wang 已提交
1379 1380 1381 1382

    if (TSDB_CODE_SUCCESS == code && gotRow) {
      pDataBuf->size += extendedRowSize;
      (*pNumOfRows)++;
X
Xiaoyu Wang 已提交
1383
    }
1384

X
Xiaoyu Wang 已提交
1385
    if (TSDB_CODE_SUCCESS == code && pDataBuf->nAllocSize > tsMaxMemUsedByInsert * 1024 * 1024) {
X
Xiaoyu Wang 已提交
1386
      pStmt->fileProcessing = true;
1387 1388
      break;
    }
X
Xiaoyu Wang 已提交
1389
  }
X
Xiaoyu Wang 已提交
1390
  taosMemoryFree(pLine);
X
Xiaoyu Wang 已提交
1391

X
Xiaoyu Wang 已提交
1392
  if (TSDB_CODE_SUCCESS == code && 0 == (*pNumOfRows) &&
X
Xiaoyu Wang 已提交
1393
      (!TSDB_QUERY_HAS_TYPE(pStmt->insertType, TSDB_QUERY_TYPE_STMT_INSERT)) && !pStmt->fileProcessing) {
X
Xiaoyu Wang 已提交
1394
    code = buildSyntaxErrMsg(&pCxt->msg, "no any data points", NULL);
X
Xiaoyu Wang 已提交
1395
  }
X
Xiaoyu Wang 已提交
1396
  return code;
X
Xiaoyu Wang 已提交
1397 1398
}

X
Xiaoyu Wang 已提交
1399 1400
static int32_t parseDataFromFileImpl(SInsertParseContext* pCxt, SVnodeModifOpStmt* pStmt, STableDataBlocks* pDataBuf) {
  int32_t maxNumOfRows = 0;
X
Xiaoyu Wang 已提交
1401
  int32_t numOfRows = 0;
X
Xiaoyu Wang 已提交
1402 1403 1404
  int32_t code = allocateMemIfNeed(pDataBuf, insGetExtendedRowSize(pDataBuf), &maxNumOfRows);
  if (TSDB_CODE_SUCCESS == code) {
    code = parseCsvFile(pCxt, pStmt, pDataBuf, maxNumOfRows, &numOfRows);
X
Xiaoyu Wang 已提交
1405
  }
X
Xiaoyu Wang 已提交
1406 1407 1408 1409 1410 1411 1412 1413
  if (TSDB_CODE_SUCCESS == code) {
    code = insSetBlockInfo((SSubmitBlk*)(pDataBuf->pData), pDataBuf, numOfRows, &pCxt->msg);
  }
  if (TSDB_CODE_SUCCESS == code) {
    pDataBuf->numOfTables = 1;
    pStmt->totalRowsNum += numOfRows;
    pStmt->totalTbNum += 1;
    TSDB_QUERY_SET_TYPE(pStmt->insertType, TSDB_QUERY_TYPE_FILE_INSERT);
X
Xiaoyu Wang 已提交
1414
    if (!pStmt->fileProcessing) {
X
Xiaoyu Wang 已提交
1415 1416 1417 1418
      taosCloseFile(&pStmt->fp);
    } else {
      parserDebug("0x%" PRIx64 " insert from csv. File is too large, do it in batches.", pCxt->pComCxt->requestId);
    }
1419
  }
X
Xiaoyu Wang 已提交
1420
  return code;
X
Xiaoyu Wang 已提交
1421 1422
}

X
Xiaoyu Wang 已提交
1423 1424
static int32_t parseDataFromFile(SInsertParseContext* pCxt, SVnodeModifOpStmt* pStmt, SToken* pFilePath,
                                 STableDataBlocks* pDataBuf) {
1425
  char filePathStr[TSDB_FILENAME_LEN] = {0};
X
Xiaoyu Wang 已提交
1426 1427
  if (TK_NK_STRING == pFilePath->type) {
    trimString(pFilePath->z, pFilePath->n, filePathStr, sizeof(filePathStr));
1428
  } else {
X
Xiaoyu Wang 已提交
1429
    strncpy(filePathStr, pFilePath->z, pFilePath->n);
1430
  }
X
Xiaoyu Wang 已提交
1431 1432
  pStmt->fp = taosOpenFile(filePathStr, TD_FILE_READ | TD_FILE_STREAM);
  if (NULL == pStmt->fp) {
1433 1434 1435
    return TAOS_SYSTEM_ERROR(errno);
  }

X
Xiaoyu Wang 已提交
1436
  return parseDataFromFileImpl(pCxt, pStmt, pDataBuf);
1437 1438
}

X
Xiaoyu Wang 已提交
1439 1440 1441 1442 1443
static int32_t parseFileClause(SInsertParseContext* pCxt, SVnodeModifOpStmt* pStmt, STableDataBlocks* pDataBuf,
                               SToken* pToken) {
  NEXT_TOKEN(pStmt->pSql, *pToken);
  if (0 == pToken->n || (TK_NK_STRING != pToken->type && TK_NK_ID != pToken->type)) {
    return buildSyntaxErrMsg(&pCxt->msg, "file path is required following keyword FILE", pToken->z);
1444
  }
X
Xiaoyu Wang 已提交
1445
  return parseDataFromFile(pCxt, pStmt, pToken, pDataBuf);
X
Xiaoyu Wang 已提交
1446 1447
}

X
Xiaoyu Wang 已提交
1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460
// VALUES (field1_value, ...) [(field1_value2, ...) ...] | FILE csv_file_path
static int32_t parseDataClause(SInsertParseContext* pCxt, SVnodeModifOpStmt* pStmt, STableDataBlocks* pDataBuf) {
  SToken token;
  NEXT_TOKEN(pStmt->pSql, token);
  switch (token.type) {
    case TK_VALUES:
      return parseValuesClause(pCxt, pStmt, pDataBuf, &token);
    case TK_FILE:
      return parseFileClause(pCxt, pStmt, pDataBuf, &token);
    default:
      break;
  }
  return buildSyntaxErrMsg(&pCxt->msg, "keyword VALUES or FILE is expected", token.z);
X
Xiaoyu Wang 已提交
1461 1462
}

X
Xiaoyu Wang 已提交
1463 1464 1465 1466 1467 1468
// input pStmt->pSql:
//   1. [(tag1_name, ...)] ...
//   2. VALUES ... | FILE ...
static int32_t parseInsertTableClauseBottom(SInsertParseContext* pCxt, SVnodeModifOpStmt* pStmt) {
  STableDataBlocks* pDataBuf = NULL;
  int32_t           code = parseSchemaClauseBottom(pCxt, pStmt, &pDataBuf);
X
Xiaoyu Wang 已提交
1469
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1470
    code = parseDataClause(pCxt, pStmt, pDataBuf);
X
Xiaoyu Wang 已提交
1471 1472 1473 1474
  }
  return code;
}

X
Xiaoyu Wang 已提交
1475 1476 1477 1478 1479 1480
static void resetEnvPreTable(SInsertParseContext* pCxt, SVnodeModifOpStmt* pStmt) {
  destroyBoundColumnInfo(&pCxt->tags);
  taosMemoryFreeClear(pStmt->pTableMeta);
  tdDestroySVCreateTbReq(&pStmt->createTblReq);
  pCxt->missCache = false;
  pCxt->usingDuplicateTable = false;
X
Xiaoyu Wang 已提交
1481
  pStmt->pBoundCols = NULL;
X
Xiaoyu Wang 已提交
1482 1483 1484 1485
  pStmt->usingTableProcessing = false;
  pStmt->fileProcessing = false;
}

X
Xiaoyu Wang 已提交
1486 1487
// input pStmt->pSql: [(field1_name, ...)] [ USING ... ] VALUES ... | FILE ...
static int32_t parseInsertTableClause(SInsertParseContext* pCxt, SVnodeModifOpStmt* pStmt, SToken* pTbName) {
X
Xiaoyu Wang 已提交
1488
  resetEnvPreTable(pCxt, pStmt);
X
Xiaoyu Wang 已提交
1489 1490 1491 1492 1493 1494
  int32_t code = parseSchemaClauseTop(pCxt, pStmt, pTbName);
  if (TSDB_CODE_SUCCESS == code && !pCxt->missCache) {
    code = parseInsertTableClauseBottom(pCxt, pStmt);
  }
  return code;
}
X
Xiaoyu Wang 已提交
1495

X
Xiaoyu Wang 已提交
1496 1497 1498 1499 1500 1501
static int32_t checkTableClauseFirstToken(SInsertParseContext* pCxt, SVnodeModifOpStmt* pStmt, SToken* pTbName,
                                          bool* pHasData) {
  // no data in the sql string anymore.
  if (0 == pTbName->n) {
    if (0 != pTbName->type && '\0' != pStmt->pSql[0]) {
      return buildSyntaxErrMsg(&pCxt->msg, "invalid charactor in SQL", pTbName->z);
1502 1503
    }

X
Xiaoyu Wang 已提交
1504 1505
    if (0 == pStmt->totalRowsNum && (!TSDB_QUERY_HAS_TYPE(pStmt->insertType, TSDB_QUERY_TYPE_STMT_INSERT))) {
      return buildInvalidOperationMsg(&pCxt->msg, "no data in sql");
D
stmt  
dapan1121 已提交
1506 1507
    }

X
Xiaoyu Wang 已提交
1508 1509 1510
    *pHasData = false;
    return TSDB_CODE_SUCCESS;
  }
X
Xiaoyu Wang 已提交
1511

X
Xiaoyu Wang 已提交
1512 1513 1514
  if (TSDB_QUERY_HAS_TYPE(pStmt->insertType, TSDB_QUERY_TYPE_STMT_INSERT) && pStmt->totalTbNum > 0) {
    return buildInvalidOperationMsg(&pCxt->msg, "single table allowed in one stmt");
  }
1515

X
Xiaoyu Wang 已提交
1516 1517 1518
  if (TK_NK_QUESTION == pTbName->type) {
    if (NULL == pCxt->pComCxt->pStmtCb) {
      return buildSyntaxErrMsg(&pCxt->msg, "? only used in stmt", pTbName->z);
X
Xiaoyu Wang 已提交
1519
    }
X
Xiaoyu Wang 已提交
1520

X
Xiaoyu Wang 已提交
1521 1522 1523 1524 1525 1526 1527
    char*   tbName = NULL;
    int32_t code = (*pCxt->pComCxt->pStmtCb->getTbNameFn)(pCxt->pComCxt->pStmtCb->pStmt, &tbName);
    if (TSDB_CODE_SUCCESS == code) {
      pTbName->z = tbName;
      pTbName->n = strlen(tbName);
    } else {
      return code;
1528
    }
X
Xiaoyu Wang 已提交
1529
  }
1530

X
Xiaoyu Wang 已提交
1531 1532 1533
  *pHasData = true;
  return TSDB_CODE_SUCCESS;
}
1534

X
Xiaoyu Wang 已提交
1535 1536 1537
static int32_t setStmtInfo(SInsertParseContext* pCxt, SVnodeModifOpStmt* pStmt) {
  SParsedDataColInfo* tags = taosMemoryMalloc(sizeof(pCxt->tags));
  if (NULL == tags) {
S
Shengliang Guan 已提交
1538
    return TSDB_CODE_OUT_OF_MEMORY;
X
Xiaoyu Wang 已提交
1539 1540
  }
  memcpy(tags, &pCxt->tags, sizeof(pCxt->tags));
1541

X
Xiaoyu Wang 已提交
1542
  SStmtCallback* pStmtCb = pCxt->pComCxt->pStmtCb;
1543
  int32_t code = (*pStmtCb->setInfoFn)(pStmtCb->pStmt, pStmt->pTableMeta, tags, &pStmt->targetTableName, pStmt->usingTableProcessing,
X
Xiaoyu Wang 已提交
1544
                                       pStmt->pVgroupsHashObj, pStmt->pTableBlockHashObj, pStmt->usingTableName.tname);
1545

X
Xiaoyu Wang 已提交
1546 1547 1548 1549 1550
  memset(&pCxt->tags, 0, sizeof(pCxt->tags));
  pStmt->pVgroupsHashObj = NULL;
  pStmt->pTableBlockHashObj = NULL;
  return code;
}
1551

X
Xiaoyu Wang 已提交
1552 1553 1554 1555
static int32_t parseInsertBodyBottom(SInsertParseContext* pCxt, SVnodeModifOpStmt* pStmt) {
  if (TSDB_QUERY_HAS_TYPE(pStmt->insertType, TSDB_QUERY_TYPE_STMT_INSERT)) {
    return setStmtInfo(pCxt, pStmt);
  }
D
stmt  
dapan1121 已提交
1556

X
Xiaoyu Wang 已提交
1557 1558 1559 1560 1561 1562
  // merge according to vgId
  int32_t code = TSDB_CODE_SUCCESS;
  if (taosHashGetSize(pStmt->pTableBlockHashObj) > 0) {
    code = insMergeTableDataBlocks(pStmt->pTableBlockHashObj, &pStmt->pVgDataBlocks);
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1563
    code = insBuildOutput(pStmt->pVgroupsHashObj, pStmt->pVgDataBlocks, &pStmt->pDataBlocks);
X
Xiaoyu Wang 已提交
1564 1565 1566
  }
  return code;
}
1567

X
Xiaoyu Wang 已提交
1568 1569 1570 1571 1572 1573 1574 1575 1576 1577
// tb_name
//     [USING stb_name [(tag1_name, ...)] TAGS (tag1_value, ...)]
//     [(field1_name, ...)]
//     VALUES (field1_value, ...) [(field1_value2, ...) ...] | FILE csv_file_path
// [...];
static int32_t parseInsertBody(SInsertParseContext* pCxt, SVnodeModifOpStmt* pStmt) {
  SToken  token;
  int32_t code = TSDB_CODE_SUCCESS;
  bool    hasData = true;
  // for each table
X
Xiaoyu Wang 已提交
1578
  while (TSDB_CODE_SUCCESS == code && hasData && !pCxt->missCache && !pStmt->fileProcessing) {
X
Xiaoyu Wang 已提交
1579 1580 1581 1582 1583
    // pStmt->pSql -> tb_name ...
    NEXT_TOKEN(pStmt->pSql, token);
    code = checkTableClauseFirstToken(pCxt, pStmt, &token, &hasData);
    if (TSDB_CODE_SUCCESS == code && hasData) {
      code = parseInsertTableClause(pCxt, pStmt, &token);
1584 1585
    }
  }
X
Xiaoyu Wang 已提交
1586

X
Xiaoyu Wang 已提交
1587 1588 1589 1590 1591
  if (TSDB_CODE_SUCCESS == code && !pCxt->missCache) {
    code = parseInsertBodyBottom(pCxt, pStmt);
  }
  return code;
}
D
stmt  
dapan1121 已提交
1592

X
Xiaoyu Wang 已提交
1593
static void destroySubTableHashElem(void* p) { taosMemoryFree(*(STableMeta**)p); }
X
Xiaoyu Wang 已提交
1594

X
Xiaoyu Wang 已提交
1595
static int32_t createVnodeModifOpStmt(SInsertParseContext* pCxt, bool reentry, SNode** pOutput) {
X
Xiaoyu Wang 已提交
1596 1597 1598
  SVnodeModifOpStmt* pStmt = (SVnodeModifOpStmt*)nodesMakeNode(QUERY_NODE_VNODE_MODIF_STMT);
  if (NULL == pStmt) {
    return TSDB_CODE_OUT_OF_MEMORY;
D
stmt  
dapan1121 已提交
1599
  }
X
Xiaoyu Wang 已提交
1600

X
Xiaoyu Wang 已提交
1601
  if (pCxt->pComCxt->pStmtCb) {
X
Xiaoyu Wang 已提交
1602
    TSDB_QUERY_SET_TYPE(pStmt->insertType, TSDB_QUERY_TYPE_STMT_INSERT);
1603
  }
X
Xiaoyu Wang 已提交
1604
  pStmt->pSql = pCxt->pComCxt->pSql;
X
Xiaoyu Wang 已提交
1605 1606 1607
  pStmt->freeHashFunc = insDestroyBlockHashmap;
  pStmt->freeArrayFunc = insDestroyBlockArrayList;

X
Xiaoyu Wang 已提交
1608 1609 1610 1611 1612
  if (!reentry) {
    pStmt->pVgroupsHashObj = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK);
    pStmt->pTableBlockHashObj =
        taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
  }
X
Xiaoyu Wang 已提交
1613 1614 1615
  pStmt->pSubTableHashObj = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR), true, HASH_NO_LOCK);
  pStmt->pTableNameHashObj = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR), true, HASH_NO_LOCK);
  pStmt->pDbFNameHashObj = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR), true, HASH_NO_LOCK);
X
Xiaoyu Wang 已提交
1616 1617
  if ((!reentry && (NULL == pStmt->pVgroupsHashObj || NULL == pStmt->pTableBlockHashObj)) ||
      NULL == pStmt->pSubTableHashObj || NULL == pStmt->pTableNameHashObj || NULL == pStmt->pDbFNameHashObj) {
X
Xiaoyu Wang 已提交
1618 1619
    nodesDestroyNode((SNode*)pStmt);
    return TSDB_CODE_OUT_OF_MEMORY;
1620
  }
X
Xiaoyu Wang 已提交
1621 1622 1623 1624 1625

  taosHashSetFreeFp(pStmt->pSubTableHashObj, destroySubTableHashElem);

  *pOutput = (SNode*)pStmt;
  return TSDB_CODE_SUCCESS;
1626 1627
}

X
Xiaoyu Wang 已提交
1628
static int32_t createInsertQuery(SInsertParseContext* pCxt, SQuery** pOutput) {
X
Xiaoyu Wang 已提交
1629 1630 1631
  SQuery* pQuery = (SQuery*)nodesMakeNode(QUERY_NODE_QUERY);
  if (NULL == pQuery) {
    return TSDB_CODE_OUT_OF_MEMORY;
D
stmt  
dapan1121 已提交
1632
  }
X
Xiaoyu Wang 已提交
1633

X
Xiaoyu Wang 已提交
1634 1635 1636
  pQuery->execMode = QUERY_EXEC_MODE_SCHEDULE;
  pQuery->haveResultSet = false;
  pQuery->msgType = TDMT_VND_SUBMIT;
1637

X
Xiaoyu Wang 已提交
1638
  int32_t code = createVnodeModifOpStmt(pCxt, false, &pQuery->pRoot);
X
Xiaoyu Wang 已提交
1639 1640 1641 1642
  if (TSDB_CODE_SUCCESS == code) {
    *pOutput = pQuery;
  } else {
    nodesDestroyNode((SNode*)pQuery);
D
stmt  
dapan1121 已提交
1643
  }
X
Xiaoyu Wang 已提交
1644 1645
  return code;
}
D
stmt  
dapan1121 已提交
1646

X
Xiaoyu Wang 已提交
1647 1648 1649
static int32_t checkAuthFromMetaData(const SArray* pUsers) {
  if (1 != taosArrayGetSize(pUsers)) {
    return TSDB_CODE_FAILED;
1650
  }
1651

X
Xiaoyu Wang 已提交
1652 1653 1654 1655 1656 1657
  SMetaRes* pRes = taosArrayGet(pUsers, 0);
  if (TSDB_CODE_SUCCESS == pRes->code) {
    return (*(bool*)pRes->pRes) ? TSDB_CODE_SUCCESS : TSDB_CODE_PAR_PERMISSION_DENIED;
  }
  return pRes->code;
}
X
Xiaoyu Wang 已提交
1658

X
Xiaoyu Wang 已提交
1659 1660 1661 1662 1663 1664 1665 1666
static int32_t getTableMetaFromMetaData(const SArray* pTables, STableMeta** pMeta) {
  if (1 != taosArrayGetSize(pTables)) {
    return TSDB_CODE_FAILED;
  }
  SMetaRes* pRes = taosArrayGet(pTables, 0);
  if (TSDB_CODE_SUCCESS == pRes->code) {
    *pMeta = tableMetaDup((const STableMeta*)pRes->pRes);
    if (NULL == *pMeta) {
D
dapan1121 已提交
1667 1668 1669
      return TSDB_CODE_OUT_OF_MEMORY;
    }
  }
X
Xiaoyu Wang 已提交
1670 1671
  return pRes->code;
}
1672

X
Xiaoyu Wang 已提交
1673 1674 1675
static int32_t getTableVgroupFromMetaData(const SArray* pTables, SVnodeModifOpStmt* pStmt, bool isStb) {
  if (1 != taosArrayGetSize(pTables)) {
    return TSDB_CODE_FAILED;
D
dapan1121 已提交
1676 1677
  }

X
Xiaoyu Wang 已提交
1678 1679 1680
  SMetaRes* pRes = taosArrayGet(pTables, 0);
  if (TSDB_CODE_SUCCESS != pRes->code) {
    return pRes->code;
1681
  }
1682

X
Xiaoyu Wang 已提交
1683 1684 1685 1686 1687 1688 1689
  SVgroupInfo* pVg = pRes->pRes;
  if (isStb) {
    pStmt->pTableMeta->vgId = pVg->vgId;
  }
  return taosHashPut(pStmt->pVgroupsHashObj, (const char*)&pVg->vgId, sizeof(pVg->vgId), (char*)pVg,
                     sizeof(SVgroupInfo));
}
D
dapan1121 已提交
1690

X
Xiaoyu Wang 已提交
1691 1692
static int32_t getTableSchemaFromMetaData(SInsertParseContext* pCxt, const SMetaData* pMetaData,
                                          SVnodeModifOpStmt* pStmt, bool isStb) {
X
Xiaoyu Wang 已提交
1693 1694 1695
  int32_t code = checkAuthFromMetaData(pMetaData->pUser);
  if (TSDB_CODE_SUCCESS == code) {
    code = getTableMetaFromMetaData(pMetaData->pTableMeta, &pStmt->pTableMeta);
X
Xiaoyu Wang 已提交
1696
  }
X
Xiaoyu Wang 已提交
1697 1698 1699
  if (TSDB_CODE_SUCCESS == code && !isStb && TSDB_SUPER_TABLE == pStmt->pTableMeta->tableType) {
    code = buildInvalidOperationMsg(&pCxt->msg, "insert data into super table is not supported");
  }
X
Xiaoyu Wang 已提交
1700 1701
  if (TSDB_CODE_SUCCESS == code) {
    code = getTableVgroupFromMetaData(pMetaData->pTableHash, pStmt, isStb);
1702
  }
X
Xiaoyu Wang 已提交
1703
  return code;
1704
}
D
stmt  
dapan1121 已提交
1705

X
Xiaoyu Wang 已提交
1706 1707 1708 1709 1710 1711
static void destoryTablesReq(void* p) {
  STablesReq* pRes = (STablesReq*)p;
  taosArrayDestroy(pRes->pTables);
}

static void clearCatalogReq(SCatalogReq* pCatalogReq) {
X
Xiaoyu Wang 已提交
1712 1713 1714 1715
  if (NULL == pCatalogReq) {
    return;
  }

X
Xiaoyu Wang 已提交
1716 1717 1718 1719 1720 1721 1722 1723
  taosArrayDestroyEx(pCatalogReq->pTableMeta, destoryTablesReq);
  pCatalogReq->pTableMeta = NULL;
  taosArrayDestroyEx(pCatalogReq->pTableHash, destoryTablesReq);
  pCatalogReq->pTableHash = NULL;
  taosArrayDestroy(pCatalogReq->pUser);
  pCatalogReq->pUser = NULL;
}

X
Xiaoyu Wang 已提交
1724
static int32_t setVnodeModifOpStmt(SInsertParseContext* pCxt, SCatalogReq* pCatalogReq, const SMetaData* pMetaData,
X
Xiaoyu Wang 已提交
1725 1726 1727
                                   SVnodeModifOpStmt* pStmt) {
  clearCatalogReq(pCatalogReq);

X
Xiaoyu Wang 已提交
1728
  if (pStmt->usingTableProcessing) {
X
Xiaoyu Wang 已提交
1729
    return getTableSchemaFromMetaData(pCxt, pMetaData, pStmt, true);
X
Xiaoyu Wang 已提交
1730
  }
X
Xiaoyu Wang 已提交
1731
  return getTableSchemaFromMetaData(pCxt, pMetaData, pStmt, false);
X
Xiaoyu Wang 已提交
1732 1733
}

X
Xiaoyu Wang 已提交
1734
static int32_t resetVnodeModifOpStmt(SInsertParseContext* pCxt, SQuery* pQuery) {
X
Xiaoyu Wang 已提交
1735 1736 1737 1738 1739 1740
  nodesDestroyNode(pQuery->pRoot);

  int32_t code = createVnodeModifOpStmt(pCxt, true, &pQuery->pRoot);
  if (TSDB_CODE_SUCCESS == code) {
    SVnodeModifOpStmt* pStmt = (SVnodeModifOpStmt*)pQuery->pRoot;

X
Xiaoyu Wang 已提交
1741 1742
    (*pCxt->pComCxt->pStmtCb->getExecInfoFn)(pCxt->pComCxt->pStmtCb->pStmt, &pStmt->pVgroupsHashObj,
                                             &pStmt->pTableBlockHashObj);
X
Xiaoyu Wang 已提交
1743 1744 1745 1746 1747 1748
    if (NULL == pStmt->pVgroupsHashObj) {
      pStmt->pVgroupsHashObj = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK);
    }
    if (NULL == pStmt->pTableBlockHashObj) {
      pStmt->pTableBlockHashObj =
          taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
1749
    }
X
Xiaoyu Wang 已提交
1750 1751 1752
    if (NULL == pStmt->pVgroupsHashObj || NULL == pStmt->pTableBlockHashObj) {
      code = TSDB_CODE_OUT_OF_MEMORY;
    }
1753
  }
X
Xiaoyu Wang 已提交
1754

X
Xiaoyu Wang 已提交
1755
  return code;
1756 1757
}

X
Xiaoyu Wang 已提交
1758
static int32_t initInsertQuery(SInsertParseContext* pCxt, SCatalogReq* pCatalogReq, const SMetaData* pMetaData,
X
Xiaoyu Wang 已提交
1759
                               SQuery** pQuery) {
X
Xiaoyu Wang 已提交
1760 1761 1762
  if (NULL == *pQuery) {
    return createInsertQuery(pCxt, pQuery);
  }
X
Xiaoyu Wang 已提交
1763

X
Xiaoyu Wang 已提交
1764
  if (NULL != pCxt->pComCxt->pStmtCb) {
X
Xiaoyu Wang 已提交
1765 1766 1767 1768 1769 1770 1771
    return resetVnodeModifOpStmt(pCxt, *pQuery);
  }

  SVnodeModifOpStmt* pStmt = (SVnodeModifOpStmt*)(*pQuery)->pRoot;

  if (!pStmt->fileProcessing) {
    return setVnodeModifOpStmt(pCxt, pCatalogReq, pMetaData, pStmt);
X
Xiaoyu Wang 已提交
1772 1773 1774
  }

  return TSDB_CODE_SUCCESS;
1775 1776
}

X
Xiaoyu Wang 已提交
1777 1778 1779 1780 1781 1782
static int32_t setRefreshMate(SQuery* pQuery) {
  SVnodeModifOpStmt* pStmt = (SVnodeModifOpStmt*)pQuery->pRoot;
  SName*             pTable = taosHashIterate(pStmt->pTableNameHashObj, NULL);
  while (NULL != pTable) {
    taosArrayPush(pQuery->pTableList, pTable);
    pTable = taosHashIterate(pStmt->pTableNameHashObj, pTable);
1783 1784
  }

X
Xiaoyu Wang 已提交
1785 1786 1787 1788
  char* pDb = taosHashIterate(pStmt->pDbFNameHashObj, NULL);
  while (NULL != pDb) {
    taosArrayPush(pQuery->pDbList, pDb);
    pDb = taosHashIterate(pStmt->pDbFNameHashObj, pDb);
1789 1790 1791 1792 1793
  }

  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805
// INSERT INTO
//   tb_name
//       [USING stb_name [(tag1_name, ...)] TAGS (tag1_value, ...) [table_options]]
//       [(field1_name, ...)]
//       VALUES (field1_value, ...) [(field1_value2, ...) ...] | FILE csv_file_path
//   [...];
static int32_t parseInsertSqlFromStart(SInsertParseContext* pCxt, SVnodeModifOpStmt* pStmt) {
  int32_t code = skipInsertInto(&pStmt->pSql, &pCxt->msg);
  if (TSDB_CODE_SUCCESS == code) {
    code = parseInsertBody(pCxt, pStmt);
  }
  return code;
1806 1807
}

X
Xiaoyu Wang 已提交
1808 1809 1810 1811 1812
static int32_t parseInsertSqlFromCsv(SInsertParseContext* pCxt, SVnodeModifOpStmt* pStmt) {
  STableDataBlocks* pDataBuf = NULL;
  int32_t           code = getTableDataBlocks(pCxt, pStmt, &pDataBuf);
  if (TSDB_CODE_SUCCESS == code) {
    code = parseDataFromFileImpl(pCxt, pStmt, pDataBuf);
1813 1814
  }

X
Xiaoyu Wang 已提交
1815 1816 1817 1818 1819 1820 1821 1822 1823
  if (TSDB_CODE_SUCCESS == code) {
    if (pStmt->fileProcessing) {
      code = parseInsertBodyBottom(pCxt, pStmt);
    } else {
      code = parseInsertBody(pCxt, pStmt);
    }
  }

  return code;
X
Xiaoyu Wang 已提交
1824 1825
}

X
Xiaoyu Wang 已提交
1826 1827 1828 1829 1830 1831 1832
static int32_t parseInsertSqlFromTable(SInsertParseContext* pCxt, SVnodeModifOpStmt* pStmt) {
  int32_t code = parseInsertTableClauseBottom(pCxt, pStmt);
  if (TSDB_CODE_SUCCESS == code) {
    code = parseInsertBody(pCxt, pStmt);
  }
  return code;
}
1833

X
Xiaoyu Wang 已提交
1834 1835 1836 1837
static int32_t parseInsertSqlImpl(SInsertParseContext* pCxt, SVnodeModifOpStmt* pStmt) {
  if (pStmt->pSql == pCxt->pComCxt->pSql || NULL != pCxt->pComCxt->pStmtCb) {
    return parseInsertSqlFromStart(pCxt, pStmt);
  }
1838

X
Xiaoyu Wang 已提交
1839 1840 1841
  if (pStmt->fileProcessing) {
    return parseInsertSqlFromCsv(pCxt, pStmt);
  }
1842

X
Xiaoyu Wang 已提交
1843 1844
  return parseInsertSqlFromTable(pCxt, pStmt);
}
1845

X
Xiaoyu Wang 已提交
1846 1847 1848 1849 1850
static int32_t buildInsertTableReq(SName* pName, SArray** pTables) {
  *pTables = taosArrayInit(1, sizeof(SName));
  if (NULL == *pTables) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
1851

X
Xiaoyu Wang 已提交
1852 1853 1854
  taosArrayPush(*pTables, pName);
  return TSDB_CODE_SUCCESS;
}
1855

X
Xiaoyu Wang 已提交
1856 1857 1858 1859 1860
static int32_t buildInsertDbReq(SName* pName, SArray** pDbs) {
  if (NULL == *pDbs) {
    *pDbs = taosArrayInit(1, sizeof(STablesReq));
    if (NULL == *pDbs) {
      return TSDB_CODE_OUT_OF_MEMORY;
1861
    }
X
Xiaoyu Wang 已提交
1862
  }
1863

X
Xiaoyu Wang 已提交
1864 1865 1866 1867
  STablesReq req = {0};
  tNameGetFullDbName(pName, req.dbFName);
  buildInsertTableReq(pName, &req.pTables);
  taosArrayPush(*pDbs, &req);
1868

X
Xiaoyu Wang 已提交
1869 1870
  return TSDB_CODE_SUCCESS;
}
1871

X
Xiaoyu Wang 已提交
1872 1873 1874 1875 1876
static int32_t buildInsertUserAuthReq(const char* pUser, SName* pName, SArray** pUserAuth) {
  *pUserAuth = taosArrayInit(1, sizeof(SUserAuthInfo));
  if (NULL == *pUserAuth) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
X
Xiaoyu Wang 已提交
1877

X
Xiaoyu Wang 已提交
1878 1879 1880 1881
  SUserAuthInfo userAuth = {.type = AUTH_TYPE_WRITE};
  snprintf(userAuth.user, sizeof(userAuth.user), "%s", pUser);
  tNameGetFullDbName(pName, userAuth.dbFName);
  taosArrayPush(*pUserAuth, &userAuth);
1882

X
Xiaoyu Wang 已提交
1883 1884 1885 1886 1887 1888 1889 1890 1891 1892
  return TSDB_CODE_SUCCESS;
}

static int32_t buildInsertCatalogReq(SInsertParseContext* pCxt, SVnodeModifOpStmt* pStmt, SCatalogReq* pCatalogReq) {
  int32_t code = buildInsertUserAuthReq(pCxt->pComCxt->pUser, &pStmt->targetTableName, &pCatalogReq->pUser);
  if (TSDB_CODE_SUCCESS == code) {
    if (0 == pStmt->usingTableName.type) {
      code = buildInsertDbReq(&pStmt->targetTableName, &pCatalogReq->pTableMeta);
    } else {
      code = buildInsertDbReq(&pStmt->usingTableName, &pCatalogReq->pTableMeta);
1893
    }
X
Xiaoyu Wang 已提交
1894 1895 1896 1897 1898 1899
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = buildInsertDbReq(&pStmt->targetTableName, &pCatalogReq->pTableHash);
  }
  return code;
}
1900

X
Xiaoyu Wang 已提交
1901 1902
static int32_t setNextStageInfo(SInsertParseContext* pCxt, SQuery* pQuery, SCatalogReq* pCatalogReq) {
  if (pCxt->missCache) {
X
Xiaoyu Wang 已提交
1903 1904 1905
    parserDebug("0x%" PRIx64 " %d rows have been inserted before cache miss", pCxt->pComCxt->requestId,
                ((SVnodeModifOpStmt*)pQuery->pRoot)->totalRowsNum);

X
Xiaoyu Wang 已提交
1906 1907
    pQuery->execStage = QUERY_EXEC_STAGE_PARSE;
    return buildInsertCatalogReq(pCxt, (SVnodeModifOpStmt*)pQuery->pRoot, pCatalogReq);
1908 1909
  }

X
Xiaoyu Wang 已提交
1910 1911 1912
  parserDebug("0x%" PRIx64 " %d rows have been inserted", pCxt->pComCxt->requestId,
              ((SVnodeModifOpStmt*)pQuery->pRoot)->totalRowsNum);

X
Xiaoyu Wang 已提交
1913
  pQuery->execStage = QUERY_EXEC_STAGE_SCHEDULE;
1914 1915 1916
  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
1917 1918 1919 1920
int32_t parseInsertSql(SParseContext* pCxt, SQuery** pQuery, SCatalogReq* pCatalogReq, const SMetaData* pMetaData) {
  SInsertParseContext context = {
      .pComCxt = pCxt,
      .msg = {.buf = pCxt->pMsg, .len = pCxt->msgLen},
X
Xiaoyu Wang 已提交
1921 1922
      .missCache = false,
      .usingDuplicateTable = false,
1923
      .forceUpdate = (NULL != pCatalogReq ? pCatalogReq->forceUpdate : false)
X
Xiaoyu Wang 已提交
1924 1925
  };

X
Xiaoyu Wang 已提交
1926
  int32_t code = initInsertQuery(&context, pCatalogReq, pMetaData, pQuery);
1927
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1928
    code = parseInsertSqlImpl(&context, (SVnodeModifOpStmt*)(*pQuery)->pRoot);
1929 1930
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1931 1932 1933 1934 1935
    code = setNextStageInfo(&context, *pQuery, pCatalogReq);
  }
  if ((TSDB_CODE_SUCCESS == code || NEED_CLIENT_HANDLE_ERROR(code)) &&
      QUERY_EXEC_STAGE_SCHEDULE == (*pQuery)->execStage) {
    code = setRefreshMate(*pQuery);
1936
  }
X
Xiaoyu Wang 已提交
1937
  destroyBoundColumnInfo(&context.tags);
1938 1939
  return code;
}