TDengine/source/libs/parser/src/parInsertSql.c

1963 lines
68 KiB
C
Raw Normal View History

/*
* 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/>.
*/
2022-10-24 08:38:16 +00:00
#include "parInsertUtil.h"
2022-03-10 07:36:06 +00:00
#include "parToken.h"
#include "tglobal.h"
#include "ttime.h"
2023-02-05 02:11:26 +00:00
#define NEXT_TOKEN_WITH_PREV(pSql, token) \
do { \
int32_t index = 0; \
token = tStrGetToken(pSql, &index, true, NULL); \
pSql += index; \
2022-03-22 03:50:27 +00:00
} while (0)
2023-02-05 02:11:26 +00:00
#define NEXT_TOKEN_WITH_PREV_EXT(pSql, token, pIgnoreComma) \
do { \
int32_t index = 0; \
token = tStrGetToken(pSql, &index, true, pIgnoreComma); \
pSql += index; \
} while (0)
#define NEXT_TOKEN_KEEP_SQL(pSql, token, index) \
do { \
token = tStrGetToken(pSql, &index, false, NULL); \
} while (0)
2022-11-04 07:21:38 +00:00
#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 {
2022-12-07 11:12:55 +00:00
SParseContext* pComCxt;
SMsgBuf msg;
char tmpTokenBuf[TSDB_MAX_BYTES_PER_ROW];
SBoundColInfo tags; // for stmt
bool missCache;
bool usingDuplicateTable;
bool forceUpdate;
2022-11-04 07:21:38 +00:00
} SInsertParseContext;
2022-04-19 02:18:11 +00:00
typedef int32_t (*_row_append_fn_t)(SMsgBuf* pMsgBuf, const void* value, int32_t len, void* param);
2022-02-28 09:02:43 +00:00
static uint8_t TRUE_VALUE = (uint8_t)TSDB_TRUE;
static uint8_t FALSE_VALUE = (uint8_t)TSDB_FALSE;
2022-11-04 07:21:38 +00:00
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);
}
2022-11-04 07:21:38 +00:00
NEXT_TOKEN(*pSql, token);
if (TK_INTO != token.type) {
return buildSyntaxErrMsg(pMsg, "keyword INTO is expected", token.z);
}
return TSDB_CODE_SUCCESS;
}
2022-11-04 07:21:38 +00:00
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);
}
}
2022-11-04 07:21:38 +00:00
return TSDB_CODE_SUCCESS;
}
2022-06-10 09:07:24 +00:00
2022-11-04 07:21:38 +00:00
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;
}
2022-11-04 07:21:38 +00:00
// 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);
}
}
2022-06-21 07:37:59 +00:00
2022-11-04 07:21:38 +00:00
// 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);
}
}
2022-11-04 07:21:38 +00:00
if (TSDB_CODE_SUCCESS == code) {
code = skipTableOptions(pCxt, pSql);
}
2022-11-04 07:21:38 +00:00
return code;
}
2022-05-09 08:03:56 +00:00
2022-12-20 08:53:08 +00:00
static int32_t parseDuplicateUsingClause(SInsertParseContext* pCxt, SVnodeModifyOpStmt* pStmt, bool* pDuplicate) {
2022-11-04 07:21:38 +00:00
*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);
}
2022-04-13 09:52:26 +00:00
}
2022-11-04 07:21:38 +00:00
2022-04-19 02:18:11 +00:00
return TSDB_CODE_SUCCESS;
2022-04-13 09:52:26 +00:00
}
2022-11-04 07:21:38 +00:00
// pStmt->pSql -> field1_name, ...)
2022-11-27 09:09:02 +00:00
static int32_t parseBoundColumns(SInsertParseContext* pCxt, const char** pSql, bool isTags, SSchema* pSchema,
SBoundColInfo* pBoundInfo) {
bool* pUseCols = taosMemoryCalloc(pBoundInfo->numOfCols, sizeof(bool));
if (NULL == pUseCols) {
return TSDB_CODE_OUT_OF_MEMORY;
2022-11-04 07:21:38 +00:00
}
2022-04-13 09:52:26 +00:00
2022-11-27 09:09:02 +00:00
pBoundInfo->numOfBound = 0;
2022-11-30 08:57:24 +00:00
int16_t lastColIdx = -1; // last column found
2022-11-27 09:09:02 +00:00
int32_t code = TSDB_CODE_SUCCESS;
while (TSDB_CODE_SUCCESS == code) {
SToken token;
2022-11-04 07:21:38 +00:00
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);
2022-11-30 08:57:24 +00:00
int16_t t = lastColIdx + 1;
int16_t index = insFindCol(&token, t, pBoundInfo->numOfCols, pSchema);
2022-11-04 07:21:38 +00:00
if (index < 0 && t > 0) {
index = insFindCol(&token, 0, t, pSchema);
}
if (index < 0) {
2022-11-27 09:09:02 +00:00
code = generateSyntaxErrMsg(&pCxt->msg, TSDB_CODE_PAR_INVALID_COLUMN, token.z);
} else if (pUseCols[index]) {
code = buildSyntaxErrMsg(&pCxt->msg, "duplicated column name", token.z);
} else {
lastColIdx = index;
pUseCols[index] = true;
pBoundInfo->pColIndex[pBoundInfo->numOfBound] = index;
++pBoundInfo->numOfBound;
2022-11-04 07:21:38 +00:00
}
}
2022-11-27 09:09:02 +00:00
if (TSDB_CODE_SUCCESS == code && !isTags && !pUseCols[0]) {
code = buildInvalidOperationMsg(&pCxt->msg, "primary timestamp column can not be null");
2022-11-22 12:55:24 +00:00
}
2022-11-27 09:09:02 +00:00
taosMemoryFree(pUseCols);
2022-11-04 07:21:38 +00:00
2022-11-27 09:09:02 +00:00
return code;
2022-06-04 13:31:07 +00:00
}
2022-11-04 07:21:38 +00:00
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;
if (pToken->type == TK_NOW) {
ts = taosGetTimestamp(timePrec);
} else if (pToken->type == TK_TODAY) {
ts = taosGetTimestampToday(timePrec);
} else if (pToken->type == TK_NK_INTEGER) {
2022-10-13 08:43:56 +00:00
if (TSDB_CODE_SUCCESS != toInteger(pToken->z, pToken->n, 10, &ts)) {
return buildSyntaxErrMsg(pMsgBuf, "invalid timestamp format", pToken->z);
}
2022-04-19 02:18:11 +00:00
} else { // parse the RFC-3339/ISO-8601 timestamp format string
2022-02-24 13:27:13 +00:00
if (taosParseTime(pToken->z, time, pToken->n, timePrec, tsDaylight) != TSDB_CODE_SUCCESS) {
return buildSyntaxErrMsg(pMsgBuf, "invalid timestamp format", pToken->z);
}
return TSDB_CODE_SUCCESS;
}
for (int k = pToken->n; pToken->z[k] != '\0'; k++) {
if (pToken->z[k] == ' ' || pToken->z[k] == '\t') continue;
2022-04-19 02:18:11 +00:00
if (pToken->z[k] == '(' && pToken->z[k + 1] == ')') { // for insert NOW()/TODAY()
*end = pTokenEnd = &pToken->z[k + 2];
k++;
continue;
}
if (pToken->z[k] == ',') {
*end = pTokenEnd;
*time = ts;
return 0;
}
break;
}
/*
* time expression:
* e.g., now+12a, now-5h
*/
index = 0;
2023-02-05 02:11:26 +00:00
SToken token = tStrGetToken(pTokenEnd, &index, false, NULL);
pTokenEnd += index;
2022-11-04 07:21:38 +00:00
if (token.type == TK_NK_MINUS || token.type == TK_NK_PLUS) {
index = 0;
2023-02-05 02:11:26 +00:00
SToken valueToken = tStrGetToken(pTokenEnd, &index, false, NULL);
pTokenEnd += index;
if (valueToken.n < 2) {
2022-11-04 07:21:38 +00:00
return buildSyntaxErrMsg(pMsgBuf, "value expected in timestamp", token.z);
}
char unit = 0;
if (parseAbsoluteDuration(valueToken.z, valueToken.n, &interval, &unit, timePrec) != TSDB_CODE_SUCCESS) {
return TSDB_CODE_TSC_INVALID_OPERATION;
}
2022-11-04 07:21:38 +00:00
if (token.type == TK_NK_PLUS) {
ts += interval;
} else {
ts = ts - interval;
}
*end = pTokenEnd;
}
*time = ts;
return TSDB_CODE_SUCCESS;
}
2022-11-04 07:21:38 +00:00
static int32_t parseTagToken(const char** end, SToken* pToken, SSchema* pSchema, int16_t timePrec, STagVal* val,
SMsgBuf* pMsgBuf) {
2022-05-11 11:03:59 +00:00
int64_t iv;
uint64_t uv;
char* endptr = NULL;
2022-02-28 09:02:43 +00:00
if (isNullValue(pSchema->type, pToken)) {
2022-02-28 09:02:43 +00:00
if (TSDB_DATA_TYPE_TIMESTAMP == pSchema->type && PRIMARYKEY_TIMESTAMP_COL_ID == pSchema->colId) {
2022-04-14 12:14:52 +00:00
return buildSyntaxErrMsg(pMsgBuf, "primary timestamp should not be null", pToken->z);
2022-02-28 09:02:43 +00:00
}
2022-11-04 07:21:38 +00:00
return TSDB_CODE_SUCCESS;
2022-02-28 09:02:43 +00:00
}
2022-11-04 07:21:38 +00:00
// strcpy(val->colName, pSchema->name);
val->cid = pSchema->colId;
val->type = pSchema->type;
2022-08-29 09:23:29 +00:00
2022-02-28 09:02:43 +00:00
switch (pSchema->type) {
case TSDB_DATA_TYPE_BOOL: {
if ((pToken->type == TK_NK_BOOL || pToken->type == TK_NK_STRING) && (pToken->n != 0)) {
2022-02-28 09:02:43 +00:00
if (strncmp(pToken->z, "true", pToken->n) == 0) {
2022-11-04 07:21:38 +00:00
*(int8_t*)(&val->i64) = TRUE_VALUE;
2022-02-28 09:02:43 +00:00
} else if (strncmp(pToken->z, "false", pToken->n) == 0) {
2022-11-04 07:21:38 +00:00
*(int8_t*)(&val->i64) = FALSE_VALUE;
2022-02-28 09:02:43 +00:00
} else {
return buildSyntaxErrMsg(pMsgBuf, "invalid bool data", pToken->z);
}
} else if (pToken->type == TK_NK_INTEGER) {
2022-11-04 07:21:38 +00:00
*(int8_t*)(&val->i64) = ((taosStr2Int64(pToken->z, NULL, 10) == 0) ? FALSE_VALUE : TRUE_VALUE);
} else if (pToken->type == TK_NK_FLOAT) {
2022-11-04 07:21:38 +00:00
*(int8_t*)(&val->i64) = ((taosStr2Double(pToken->z, NULL) == 0) ? FALSE_VALUE : TRUE_VALUE);
2022-02-28 09:02:43 +00:00
} else {
return buildSyntaxErrMsg(pMsgBuf, "invalid bool data", pToken->z);
}
2022-11-04 07:21:38 +00:00
break;
2022-02-28 09:02:43 +00:00
}
case TSDB_DATA_TYPE_TINYINT: {
2022-05-11 11:03:59 +00:00
if (TSDB_CODE_SUCCESS != toInteger(pToken->z, pToken->n, 10, &iv)) {
2022-02-28 09:02:43 +00:00
return buildSyntaxErrMsg(pMsgBuf, "invalid tinyint data", pToken->z);
} else if (!IS_VALID_TINYINT(iv)) {
return buildSyntaxErrMsg(pMsgBuf, "tinyint data overflow", pToken->z);
}
2022-11-04 07:21:38 +00:00
*(int8_t*)(&val->i64) = iv;
break;
2022-02-28 09:02:43 +00:00
}
2022-04-19 02:18:11 +00:00
case TSDB_DATA_TYPE_UTINYINT: {
2022-05-11 11:03:59 +00:00
if (TSDB_CODE_SUCCESS != toUInteger(pToken->z, pToken->n, 10, &uv)) {
2022-02-28 09:02:43 +00:00
return buildSyntaxErrMsg(pMsgBuf, "invalid unsigned tinyint data", pToken->z);
2022-10-17 07:43:42 +00:00
} else if (uv > UINT8_MAX) {
2022-02-28 09:02:43 +00:00
return buildSyntaxErrMsg(pMsgBuf, "unsigned tinyint data overflow", pToken->z);
}
2022-11-04 07:21:38 +00:00
*(uint8_t*)(&val->i64) = uv;
break;
2022-02-28 09:02:43 +00:00
}
case TSDB_DATA_TYPE_SMALLINT: {
2022-05-11 11:03:59 +00:00
if (TSDB_CODE_SUCCESS != toInteger(pToken->z, pToken->n, 10, &iv)) {
2022-02-28 09:02:43 +00:00
return buildSyntaxErrMsg(pMsgBuf, "invalid smallint data", pToken->z);
} else if (!IS_VALID_SMALLINT(iv)) {
return buildSyntaxErrMsg(pMsgBuf, "smallint data overflow", pToken->z);
}
2022-11-04 07:21:38 +00:00
*(int16_t*)(&val->i64) = iv;
break;
2022-02-28 09:02:43 +00:00
}
case TSDB_DATA_TYPE_USMALLINT: {
2022-05-11 11:03:59 +00:00
if (TSDB_CODE_SUCCESS != toUInteger(pToken->z, pToken->n, 10, &uv)) {
2022-02-28 09:02:43 +00:00
return buildSyntaxErrMsg(pMsgBuf, "invalid unsigned smallint data", pToken->z);
2022-10-17 07:43:42 +00:00
} else if (uv > UINT16_MAX) {
2022-02-28 09:02:43 +00:00
return buildSyntaxErrMsg(pMsgBuf, "unsigned smallint data overflow", pToken->z);
}
2022-11-04 07:21:38 +00:00
*(uint16_t*)(&val->i64) = uv;
break;
2022-02-28 09:02:43 +00:00
}
case TSDB_DATA_TYPE_INT: {
2022-05-11 11:03:59 +00:00
if (TSDB_CODE_SUCCESS != toInteger(pToken->z, pToken->n, 10, &iv)) {
2022-02-28 09:02:43 +00:00
return buildSyntaxErrMsg(pMsgBuf, "invalid int data", pToken->z);
} else if (!IS_VALID_INT(iv)) {
return buildSyntaxErrMsg(pMsgBuf, "int data overflow", pToken->z);
}
2022-11-04 07:21:38 +00:00
*(int32_t*)(&val->i64) = iv;
break;
2022-02-28 09:02:43 +00:00
}
case TSDB_DATA_TYPE_UINT: {
2022-05-11 11:03:59 +00:00
if (TSDB_CODE_SUCCESS != toUInteger(pToken->z, pToken->n, 10, &uv)) {
2022-02-28 09:02:43 +00:00
return buildSyntaxErrMsg(pMsgBuf, "invalid unsigned int data", pToken->z);
2022-10-17 07:43:42 +00:00
} else if (uv > UINT32_MAX) {
2022-02-28 09:02:43 +00:00
return buildSyntaxErrMsg(pMsgBuf, "unsigned int data overflow", pToken->z);
}
2022-11-04 07:21:38 +00:00
*(uint32_t*)(&val->i64) = uv;
break;
2022-02-28 09:02:43 +00:00
}
case TSDB_DATA_TYPE_BIGINT: {
2022-05-11 11:03:59 +00:00
if (TSDB_CODE_SUCCESS != toInteger(pToken->z, pToken->n, 10, &iv)) {
2022-02-28 09:02:43 +00:00
return buildSyntaxErrMsg(pMsgBuf, "invalid bigint data", pToken->z);
}
2022-11-04 07:21:38 +00:00
val->i64 = iv;
break;
2022-02-28 09:02:43 +00:00
}
case TSDB_DATA_TYPE_UBIGINT: {
2022-05-11 11:03:59 +00:00
if (TSDB_CODE_SUCCESS != toUInteger(pToken->z, pToken->n, 10, &uv)) {
2022-02-28 09:02:43 +00:00
return buildSyntaxErrMsg(pMsgBuf, "invalid unsigned bigint data", pToken->z);
}
2022-11-04 07:21:38 +00:00
*(uint64_t*)(&val->i64) = uv;
break;
2022-02-28 09:02:43 +00:00
}
case TSDB_DATA_TYPE_FLOAT: {
double dv;
2022-03-05 23:12:08 +00:00
if (TK_NK_ILLEGAL == toDouble(pToken, &dv, &endptr)) {
2022-02-28 09:02:43 +00:00
return buildSyntaxErrMsg(pMsgBuf, "illegal float data", pToken->z);
}
2022-04-19 02:18:11 +00:00
if (((dv == HUGE_VAL || dv == -HUGE_VAL) && errno == ERANGE) || dv > FLT_MAX || dv < -FLT_MAX || isinf(dv) ||
isnan(dv)) {
2022-02-28 09:02:43 +00:00
return buildSyntaxErrMsg(pMsgBuf, "illegal float data", pToken->z);
}
2022-11-04 07:21:38 +00:00
*(float*)(&val->i64) = dv;
break;
2022-02-28 09:02:43 +00:00
}
case TSDB_DATA_TYPE_DOUBLE: {
double dv;
2022-03-05 23:12:08 +00:00
if (TK_NK_ILLEGAL == toDouble(pToken, &dv, &endptr)) {
2022-02-28 09:02:43 +00:00
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);
}
2022-11-04 07:21:38 +00:00
*(double*)(&val->i64) = dv;
break;
2022-02-28 09:02:43 +00:00
}
case TSDB_DATA_TYPE_BINARY: {
// Too long values will raise the invalid sql error message
if (pToken->n + VARSTR_HEADER_SIZE > pSchema->bytes) {
2022-05-26 10:00:44 +00:00
return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_VALUE_TOO_LONG, pSchema->name);
2022-02-28 09:02:43 +00:00
}
val->pData = taosStrdup(pToken->z);
2022-11-04 07:21:38 +00:00
val->nData = pToken->n;
break;
2022-02-28 09:02:43 +00:00
}
case TSDB_DATA_TYPE_NCHAR: {
2022-11-04 07:21:38 +00:00
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);
}
2022-11-04 07:21:38 +00:00
val->pData = p;
val->nData = output;
break;
}
2022-02-28 09:02:43 +00:00
case TSDB_DATA_TYPE_TIMESTAMP: {
2022-11-04 07:21:38 +00:00
if (parseTime(end, pToken, timePrec, &iv, pMsgBuf) != TSDB_CODE_SUCCESS) {
2022-02-28 09:02:43 +00:00
return buildSyntaxErrMsg(pMsgBuf, "invalid timestamp", pToken->z);
}
2022-11-04 07:21:38 +00:00
val->i64 = iv;
break;
2022-02-28 09:02:43 +00:00
}
}
2022-11-04 07:21:38 +00:00
return TSDB_CODE_SUCCESS;
2022-02-28 09:02:43 +00:00
}
2022-11-04 07:21:38 +00:00
// input pStmt->pSql: [(tag1_name, ...)] TAGS (tag1_value, ...) ...
// output pStmt->pSql: TAGS (tag1_value, ...) ...
2022-12-20 08:53:08 +00:00
static int32_t parseBoundTagsClause(SInsertParseContext* pCxt, SVnodeModifyOpStmt* pStmt) {
2022-11-27 09:09:02 +00:00
insInitBoundColsInfo(getNumOfTags(pStmt->pTableMeta), &pCxt->tags);
2022-11-04 07:21:38 +00:00
SToken token;
int32_t index = 0;
NEXT_TOKEN_KEEP_SQL(pStmt->pSql, token, index);
if (TK_NK_LP != token.type) {
return TSDB_CODE_SUCCESS;
}
2022-11-04 07:21:38 +00:00
pStmt->pSql += index;
2022-11-27 09:09:02 +00:00
return parseBoundColumns(pCxt, &pStmt->pSql, true, getTableTagSchema(pStmt->pTableMeta), &pCxt->tags);
2022-11-04 07:21:38 +00:00
}
2022-12-20 08:53:08 +00:00
static int32_t parseTagValue(SInsertParseContext* pCxt, SVnodeModifyOpStmt* pStmt, SSchema* pTagSchema, SToken* pToken,
2022-11-04 07:21:38 +00:00
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);
}
2022-11-04 07:21:38 +00:00
if (isNullValue(pTagSchema->type, pToken)) {
return tTagNew(pTagVals, 1, true, pTag);
} else {
return parseJsontoTagData(pToken->z, pTagVals, pTag, &pCxt->msg);
}
}
2022-11-04 07:21:38 +00:00
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;
}
2022-12-23 09:50:05 +00:00
static int32_t buildCreateTbReq(SVnodeModifyOpStmt* pStmt, STag* pTag, SArray* pTagName) {
2022-11-28 02:43:55 +00:00
pStmt->pCreateTblReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
if (NULL == pStmt->pCreateTblReq) {
return TSDB_CODE_OUT_OF_MEMORY;
}
2022-11-27 09:09:02 +00:00
insBuildCreateTbReq(pStmt->pCreateTblReq, pStmt->targetTableName.tname, pTag, pStmt->pTableMeta->suid,
2022-11-30 02:58:32 +00:00
pStmt->usingTableName.tname, pTagName, pStmt->pTableMeta->tableInfo.numOfTags,
TSDB_DEFAULT_TABLE_TTL);
2022-11-28 02:43:55 +00:00
return TSDB_CODE_SUCCESS;
2022-11-04 07:21:38 +00:00
}
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);
}
2022-11-04 07:21:38 +00:00
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, ...)
2022-12-20 08:53:08 +00:00
static int32_t parseTagsClauseImpl(SInsertParseContext* pCxt, SVnodeModifyOpStmt* pStmt) {
2022-11-04 07:21:38 +00:00
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;
}
2022-11-04 07:21:38 +00:00
if (isParseBindParam) {
code = buildInvalidOperationMsg(&pCxt->msg, "no mix usage for ? and tag values");
break;
}
2022-11-04 07:21:38 +00:00
2022-11-27 09:09:02 +00:00
SSchema* pTagSchema = &pSchema[pCxt->tags.pColIndex[i]];
2022-11-04 07:21:38 +00:00
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);
}
}
2022-11-04 07:21:38 +00:00
if (TSDB_CODE_SUCCESS == code && !isParseBindParam && !isJson) {
code = tTagNew(pTagVals, 1, false, &pTag);
}
2022-11-04 07:21:38 +00:00
if (TSDB_CODE_SUCCESS == code && !isParseBindParam) {
2022-11-28 02:43:55 +00:00
code = buildCreateTbReq(pStmt, pTag, pTagName);
2022-11-04 07:21:38 +00:00
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);
}
2022-11-04 07:21:38 +00:00
}
taosArrayDestroy(pTagVals);
taosArrayDestroy(pTagName);
tTagFree(pTag);
return code;
}
// input pStmt->pSql: TAGS (tag1_value, ...) [table_options] ...
// output pStmt->pSql: [table_options] ...
2022-12-20 08:53:08 +00:00
static int32_t parseTagsClause(SInsertParseContext* pCxt, SVnodeModifyOpStmt* pStmt) {
2022-11-04 07:21:38 +00:00
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);
}
}
2022-11-04 07:21:38 +00:00
return code;
}
2022-12-20 08:53:08 +00:00
static int32_t storeTableMeta(SInsertParseContext* pCxt, SVnodeModifyOpStmt* pStmt) {
2022-11-04 10:01:39 +00:00
pStmt->pTableMeta->suid = pStmt->pTableMeta->uid;
2022-11-04 07:21:38 +00:00
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;
}
2022-11-04 07:21:38 +00:00
char tbFName[TSDB_TABLE_FNAME_LEN];
tNameExtractFullName(&pStmt->targetTableName, tbFName);
return taosHashPut(pStmt->pSubTableHashObj, tbFName, strlen(tbFName), &pBackup, POINTER_BYTES);
}
2022-12-20 08:53:08 +00:00
static int32_t parseTableOptions(SInsertParseContext* pCxt, SVnodeModifyOpStmt* pStmt) {
2022-11-04 07:21:38 +00:00
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);
}
2022-11-27 09:09:02 +00:00
pStmt->pCreateTblReq->ttl = taosStr2Int32(token.z, NULL, 10);
if (pStmt->pCreateTblReq->ttl < 0) {
2022-11-04 07:21:38 +00:00
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);
2022-11-27 09:09:02 +00:00
pStmt->pCreateTblReq->comment = strndup(pCxt->tmpTokenBuf, len);
if (NULL == pStmt->pCreateTblReq->comment) {
2022-11-04 07:21:38 +00:00
return TSDB_CODE_OUT_OF_MEMORY;
}
2022-11-27 09:09:02 +00:00
pStmt->pCreateTblReq->commentLen = len;
2022-11-04 07:21:38 +00:00
} else {
break;
}
} while (1);
return TSDB_CODE_SUCCESS;
}
2022-11-04 07:21:38 +00:00
// input pStmt->pSql:
// 1. [(tag1_name, ...)] ...
// 2. VALUES ... | FILE ...
// output pStmt->pSql:
// 1. [(field1_name, ...)]
// 2. VALUES ... | FILE ...
2022-12-20 08:53:08 +00:00
static int32_t parseUsingClauseBottom(SInsertParseContext* pCxt, SVnodeModifyOpStmt* pStmt) {
2022-11-06 06:26:19 +00:00
if (!pStmt->usingTableProcessing || pCxt->usingDuplicateTable) {
2022-11-04 07:21:38 +00:00
return TSDB_CODE_SUCCESS;
}
2022-05-31 09:49:33 +00:00
2022-11-04 07:21:38 +00:00
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;
}
2022-11-05 04:09:11 +00:00
static int32_t checkAuth(SParseContext* pCxt, SName* pTbName, bool* pMissCache) {
2022-11-04 07:21:38 +00:00
char dbFName[TSDB_DB_FNAME_LEN];
tNameGetFullDbName(pTbName, dbFName);
2022-11-05 04:09:11 +00:00
int32_t code = TSDB_CODE_SUCCESS;
bool pass = true;
bool exists = true;
2022-11-04 07:21:38 +00:00
if (pCxt->async) {
2022-11-05 04:09:11 +00:00
code = catalogChkAuthFromCache(pCxt->pCatalog, pCxt->pUser, dbFName, AUTH_TYPE_WRITE, &pass, &exists);
2022-11-04 07:21:38 +00:00
} else {
2022-11-05 04:09:11 +00:00
SRequestConnInfo conn = {.pTrans = pCxt->pTransporter,
.requestId = pCxt->requestId,
.requestObjRefId = pCxt->requestRid,
.mgmtEps = pCxt->mgmtEpSet};
2022-11-04 07:21:38 +00:00
code = catalogChkAuth(pCxt->pCatalog, &conn, pCxt->pUser, dbFName, AUTH_TYPE_WRITE, &pass);
}
2022-11-05 04:09:11 +00:00
if (TSDB_CODE_SUCCESS == code) {
if (!exists) {
*pMissCache = true;
} else if (!pass) {
code = TSDB_CODE_PAR_PERMISSION_DENIED;
}
2022-11-04 07:21:38 +00:00
}
return code;
}
static int32_t getTableMeta(SInsertParseContext* pCxt, SName* pTbName, bool isStb, STableMeta** pTableMeta,
bool* pMissCache) {
2022-11-05 04:09:11 +00:00
SParseContext* pComCxt = pCxt->pComCxt;
int32_t code = TSDB_CODE_SUCCESS;
2022-11-04 07:21:38 +00:00
if (pComCxt->async) {
if (isStb) {
2022-11-05 04:09:11 +00:00
code = catalogGetCachedSTableMeta(pComCxt->pCatalog, pTbName, pTableMeta);
2022-11-04 07:21:38 +00:00
} else {
2022-11-05 04:09:11 +00:00
code = catalogGetCachedTableMeta(pComCxt->pCatalog, pTbName, pTableMeta);
2022-11-04 07:21:38 +00:00
}
} else {
2022-11-05 04:09:11 +00:00
SRequestConnInfo conn = {.pTrans = pComCxt->pTransporter,
.requestId = pComCxt->requestId,
.requestObjRefId = pComCxt->requestRid,
.mgmtEps = pComCxt->mgmtEpSet};
2022-11-04 07:21:38 +00:00
if (isStb) {
code = catalogGetSTableMeta(pComCxt->pCatalog, &conn, pTbName, pTableMeta);
} else {
code = catalogGetTableMeta(pComCxt->pCatalog, &conn, pTbName, pTableMeta);
2022-05-31 09:49:33 +00:00
}
2022-11-04 07:21:38 +00:00
}
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");
2022-11-22 10:18:31 +00:00
} else if (!isStb && TSDB_SUPER_TABLE == (*pTableMeta)->tableType) {
code = buildInvalidOperationMsg(&pCxt->msg, "insert data into super table is not supported");
2022-11-04 07:21:38 +00:00
}
}
return code;
}
2022-12-20 08:53:08 +00:00
static int32_t getTableVgroup(SParseContext* pCxt, SVnodeModifyOpStmt* pStmt, bool isStb, bool* pMissCache) {
2022-11-05 04:09:11 +00:00
int32_t code = TSDB_CODE_SUCCESS;
SVgroupInfo vg;
bool exists = true;
2022-11-04 07:21:38 +00:00
if (pCxt->async) {
2022-11-05 04:09:11 +00:00
code = catalogGetCachedTableHashVgroup(pCxt->pCatalog, &pStmt->targetTableName, &vg, &exists);
2022-11-04 07:21:38 +00:00
} else {
2022-11-05 04:09:11 +00:00
SRequestConnInfo conn = {.pTrans = pCxt->pTransporter,
.requestId = pCxt->requestId,
.requestObjRefId = pCxt->requestRid,
.mgmtEps = pCxt->mgmtEpSet};
2022-11-04 07:21:38 +00:00
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;
}
2022-12-20 08:53:08 +00:00
static int32_t getTableMetaAndVgroupImpl(SParseContext* pCxt, SVnodeModifyOpStmt* pStmt, bool* pMissCache) {
SVgroupInfo vg;
int32_t code = catalogGetCachedTableVgMeta(pCxt->pCatalog, &pStmt->targetTableName, &vg, &pStmt->pTableMeta);
if (TSDB_CODE_SUCCESS == code) {
if (NULL != pStmt->pTableMeta) {
code = taosHashPut(pStmt->pVgroupsHashObj, (const char*)&vg.vgId, sizeof(vg.vgId), (char*)&vg, sizeof(vg));
}
*pMissCache = (NULL == pStmt->pTableMeta);
}
return code;
}
2022-12-20 08:53:08 +00:00
static int32_t getTableMetaAndVgroup(SInsertParseContext* pCxt, SVnodeModifyOpStmt* pStmt, bool* pMissCache) {
SParseContext* pComCxt = pCxt->pComCxt;
int32_t code = TSDB_CODE_SUCCESS;
if (pComCxt->async) {
2022-12-08 05:53:16 +00:00
code = getTableMetaAndVgroupImpl(pComCxt, pStmt, pMissCache);
} else {
code = getTableMeta(pCxt, &pStmt->targetTableName, false, &pStmt->pTableMeta, pMissCache);
if (TSDB_CODE_SUCCESS == code && !pCxt->missCache) {
code = getTableVgroup(pCxt->pComCxt, pStmt, false, &pCxt->missCache);
}
}
return code;
}
static int32_t collectUseTable(const SName* pName, SHashObj* pTable) {
char fullName[TSDB_TABLE_FNAME_LEN];
tNameExtractFullName(pName, fullName);
return taosHashPut(pTable, fullName, strlen(fullName), pName, sizeof(SName));
}
static int32_t collectUseDatabase(const SName* pName, SHashObj* pDbs) {
char dbFName[TSDB_DB_FNAME_LEN] = {0};
tNameGetFullDbName(pName, dbFName);
return taosHashPut(pDbs, dbFName, strlen(dbFName), dbFName, sizeof(dbFName));
}
2022-12-20 08:53:08 +00:00
static int32_t getTargetTableSchema(SInsertParseContext* pCxt, SVnodeModifyOpStmt* pStmt) {
if (pCxt->forceUpdate) {
pCxt->missCache = true;
return TSDB_CODE_SUCCESS;
}
2022-11-05 04:09:11 +00:00
int32_t code = checkAuth(pCxt->pComCxt, &pStmt->targetTableName, &pCxt->missCache);
if (TSDB_CODE_SUCCESS == code && !pCxt->missCache) {
code = getTableMetaAndVgroup(pCxt, pStmt, &pCxt->missCache);
2022-11-04 07:21:38 +00:00
}
if (TSDB_CODE_SUCCESS == code && !pCxt->pComCxt->async) {
code = collectUseDatabase(&pStmt->targetTableName, pStmt->pDbFNameHashObj);
if (TSDB_CODE_SUCCESS == code) {
code = collectUseTable(&pStmt->targetTableName, pStmt->pTableNameHashObj);
}
}
2022-11-04 07:21:38 +00:00
return code;
}
2022-12-20 08:53:08 +00:00
static int32_t preParseUsingTableName(SInsertParseContext* pCxt, SVnodeModifyOpStmt* pStmt, SToken* pTbName) {
2022-11-04 07:21:38 +00:00
return insCreateSName(&pStmt->usingTableName, pTbName, pCxt->pComCxt->acctId, pCxt->pComCxt->db, &pCxt->msg);
}
2022-12-20 08:53:08 +00:00
static int32_t getUsingTableSchema(SInsertParseContext* pCxt, SVnodeModifyOpStmt* pStmt) {
if (pCxt->forceUpdate) {
pCxt->missCache = true;
return TSDB_CODE_SUCCESS;
}
2022-11-05 04:09:11 +00:00
int32_t code = checkAuth(pCxt->pComCxt, &pStmt->targetTableName, &pCxt->missCache);
if (TSDB_CODE_SUCCESS == code && !pCxt->missCache) {
2022-11-04 07:21:38 +00:00
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);
}
if (TSDB_CODE_SUCCESS == code && !pCxt->pComCxt->async) {
code = collectUseDatabase(&pStmt->usingTableName, pStmt->pDbFNameHashObj);
if (TSDB_CODE_SUCCESS == code) {
code = collectUseTable(&pStmt->usingTableName, pStmt->pTableNameHashObj);
}
}
2022-11-04 07:21:38 +00:00
return code;
}
2022-12-20 08:53:08 +00:00
static int32_t parseUsingTableNameImpl(SInsertParseContext* pCxt, SVnodeModifyOpStmt* pStmt) {
2022-11-04 07:21:38 +00:00
SToken token;
NEXT_TOKEN(pStmt->pSql, token);
int32_t code = preParseUsingTableName(pCxt, pStmt, &token);
if (TSDB_CODE_SUCCESS == code) {
code = getUsingTableSchema(pCxt, pStmt);
}
2022-11-06 06:26:19 +00:00
if (TSDB_CODE_SUCCESS == code && !pCxt->missCache) {
2022-11-04 07:21:38 +00:00
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 ...
2022-12-20 08:53:08 +00:00
static int32_t parseUsingTableName(SInsertParseContext* pCxt, SVnodeModifyOpStmt* pStmt) {
2022-11-04 07:21:38 +00:00
SToken token;
int32_t index = 0;
NEXT_TOKEN_KEEP_SQL(pStmt->pSql, token, index);
if (TK_USING != token.type) {
return getTargetTableSchema(pCxt, pStmt);
}
2022-11-06 06:26:19 +00:00
pStmt->usingTableProcessing = true;
2022-11-04 07:21:38 +00:00
// pStmt->pSql -> stb_name [(tag1_name, ...)
pStmt->pSql += index;
2022-11-06 06:26:19 +00:00
int32_t code = parseDuplicateUsingClause(pCxt, pStmt, &pCxt->usingDuplicateTable);
if (TSDB_CODE_SUCCESS == code && !pCxt->usingDuplicateTable) {
2022-11-04 07:21:38 +00:00
return parseUsingTableNameImpl(pCxt, pStmt);
}
return code;
}
2022-12-20 08:53:08 +00:00
static int32_t preParseTargetTableName(SInsertParseContext* pCxt, SVnodeModifyOpStmt* pStmt, SToken* pTbName) {
2022-11-04 07:21:38 +00:00
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 ...
2022-12-20 08:53:08 +00:00
static int32_t preParseBoundColumnsClause(SInsertParseContext* pCxt, SVnodeModifyOpStmt* pStmt) {
2022-11-04 07:21:38 +00:00
SToken token;
int32_t index = 0;
NEXT_TOKEN_KEEP_SQL(pStmt->pSql, token, index);
if (TK_NK_LP != token.type) {
return TSDB_CODE_SUCCESS;
}
2022-11-04 07:21:38 +00:00
// pStmt->pSql -> field1_name, ...)
pStmt->pSql += index;
pStmt->pBoundCols = pStmt->pSql;
return skipParentheses(pCxt, &pStmt->pSql);
}
2022-12-23 09:50:05 +00:00
static int32_t getTableDataCxt(SInsertParseContext* pCxt, SVnodeModifyOpStmt* pStmt, STableDataCxt** pTableCxt) {
2022-11-04 07:21:38 +00:00
if (pCxt->pComCxt->async) {
2022-12-04 02:09:10 +00:00
return insGetTableDataCxt(pStmt->pTableBlockHashObj, &pStmt->pTableMeta->uid, sizeof(pStmt->pTableMeta->uid),
pStmt->pTableMeta, &pStmt->pCreateTblReq, pTableCxt, false);
2022-11-04 07:21:38 +00:00
}
2022-12-04 02:09:10 +00:00
2022-11-04 07:21:38 +00:00
char tbFName[TSDB_TABLE_FNAME_LEN];
tNameExtractFullName(&pStmt->targetTableName, tbFName);
2022-11-30 08:57:24 +00:00
if (pStmt->usingTableProcessing) {
pStmt->pTableMeta->uid = 0;
}
2022-11-27 09:09:02 +00:00
return insGetTableDataCxt(pStmt->pTableBlockHashObj, tbFName, strlen(tbFName), pStmt->pTableMeta,
2022-11-30 08:57:24 +00:00
&pStmt->pCreateTblReq, pTableCxt, NULL != pCxt->pComCxt->pStmtCb);
2022-11-04 07:21:38 +00:00
}
2022-12-23 09:50:05 +00:00
static int32_t parseBoundColumnsClause(SInsertParseContext* pCxt, SVnodeModifyOpStmt* pStmt, STableDataCxt* pTableCxt) {
2022-11-04 07:21:38 +00:00
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, ...)
2022-11-27 09:09:02 +00:00
return parseBoundColumns(pCxt, &pStmt->pSql, false, getTableColumnSchema(pStmt->pTableMeta),
&pTableCxt->boundColsInfo);
2022-11-04 07:21:38 +00:00
}
if (NULL != pStmt->pBoundCols) {
2022-11-27 09:09:02 +00:00
return parseBoundColumns(pCxt, &pStmt->pBoundCols, false, getTableColumnSchema(pStmt->pTableMeta),
&pTableCxt->boundColsInfo);
2022-11-04 07:21:38 +00:00
}
return TSDB_CODE_SUCCESS;
}
2022-11-30 08:57:24 +00:00
int32_t initTableColSubmitData(STableDataCxt* pTableCxt) {
if (0 == (pTableCxt->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT)) {
return TSDB_CODE_SUCCESS;
}
for (int32_t i = 0; i < pTableCxt->boundColsInfo.numOfBound; ++i) {
2022-12-04 02:09:10 +00:00
SSchema* pSchema = &pTableCxt->pMeta->schema[pTableCxt->boundColsInfo.pColIndex[i]];
2022-11-30 08:57:24 +00:00
SColData* pCol = taosArrayReserve(pTableCxt->pData->aCol, 1);
if (NULL == pCol) {
return TSDB_CODE_OUT_OF_MEMORY;
}
tColDataInit(pCol, pSchema->colId, pSchema->type, 0);
}
return TSDB_CODE_SUCCESS;
}
2022-11-04 07:21:38 +00:00
// input pStmt->pSql:
// 1. [(tag1_name, ...)] ...
// 2. VALUES ... | FILE ...
// output pStmt->pSql: VALUES ... | FILE ...
2022-12-20 08:53:08 +00:00
static int32_t parseSchemaClauseBottom(SInsertParseContext* pCxt, SVnodeModifyOpStmt* pStmt,
2022-12-23 09:50:05 +00:00
STableDataCxt** pTableCxt) {
2022-11-04 07:21:38 +00:00
int32_t code = parseUsingClauseBottom(pCxt, pStmt);
if (TSDB_CODE_SUCCESS == code) {
2022-11-27 09:09:02 +00:00
code = getTableDataCxt(pCxt, pStmt, pTableCxt);
2022-11-04 07:21:38 +00:00
}
if (TSDB_CODE_SUCCESS == code) {
2022-11-27 09:09:02 +00:00
code = parseBoundColumnsClause(pCxt, pStmt, *pTableCxt);
2022-11-04 07:21:38 +00:00
}
2022-11-30 08:57:24 +00:00
if (TSDB_CODE_SUCCESS == code) {
code = initTableColSubmitData(*pTableCxt);
}
2022-11-04 07:21:38 +00:00
return code;
}
// input pStmt->pSql: [(field1_name, ...)] [ USING ... ] VALUES ... | FILE ...
// output pStmt->pSql:
// 1. [(tag1_name, ...)] ...
// 2. VALUES ... | FILE ...
2022-12-20 08:53:08 +00:00
static int32_t parseSchemaClauseTop(SInsertParseContext* pCxt, SVnodeModifyOpStmt* pStmt, SToken* pTbName) {
2022-11-04 07:21:38 +00:00
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,
2022-11-27 09:09:02 +00:00
int16_t timePrec, SColVal* pVal) {
2022-05-31 09:49:33 +00:00
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) {
2022-11-27 09:09:02 +00:00
pVal->value.val = TRUE_VALUE;
2022-05-31 09:49:33 +00:00
} else if (strncmp(pToken->z, "false", pToken->n) == 0) {
2022-11-27 09:09:02 +00:00
pVal->value.val = FALSE_VALUE;
2022-05-31 09:49:33 +00:00
} else {
2022-11-04 07:21:38 +00:00
return buildSyntaxErrMsg(&pCxt->msg, "invalid bool data", pToken->z);
2022-05-31 09:49:33 +00:00
}
} else if (pToken->type == TK_NK_INTEGER) {
2022-11-27 09:09:02 +00:00
pVal->value.val = ((taosStr2Int64(pToken->z, NULL, 10) == 0) ? FALSE_VALUE : TRUE_VALUE);
2022-05-31 09:49:33 +00:00
} else if (pToken->type == TK_NK_FLOAT) {
2022-11-27 09:09:02 +00:00
pVal->value.val = ((taosStr2Double(pToken->z, NULL) == 0) ? FALSE_VALUE : TRUE_VALUE);
2022-05-31 09:49:33 +00:00
} else {
2022-11-04 07:21:38 +00:00
return buildSyntaxErrMsg(&pCxt->msg, "invalid bool data", pToken->z);
2022-05-26 10:00:44 +00:00
}
2022-11-27 09:09:02 +00:00
break;
2022-05-31 09:49:33 +00:00
}
case TSDB_DATA_TYPE_TINYINT: {
2022-11-27 09:09:02 +00:00
if (TSDB_CODE_SUCCESS != toInteger(pToken->z, pToken->n, 10, &pVal->value.val)) {
2022-11-04 07:21:38 +00:00
return buildSyntaxErrMsg(&pCxt->msg, "invalid tinyint data", pToken->z);
2022-11-27 09:09:02 +00:00
} else if (!IS_VALID_TINYINT(pVal->value.val)) {
2022-11-04 07:21:38 +00:00
return buildSyntaxErrMsg(&pCxt->msg, "tinyint data overflow", pToken->z);
2022-05-26 10:00:44 +00:00
}
2022-11-27 09:09:02 +00:00
break;
2022-03-05 23:12:08 +00:00
}
2022-05-31 09:49:33 +00:00
case TSDB_DATA_TYPE_UTINYINT: {
2022-11-27 09:09:02 +00:00
if (TSDB_CODE_SUCCESS != toUInteger(pToken->z, pToken->n, 10, &pVal->value.val)) {
2022-11-04 07:21:38 +00:00
return buildSyntaxErrMsg(&pCxt->msg, "invalid unsigned tinyint data", pToken->z);
2022-11-27 09:09:02 +00:00
} else if (pVal->value.val > UINT8_MAX) {
2022-11-04 07:21:38 +00:00
return buildSyntaxErrMsg(&pCxt->msg, "unsigned tinyint data overflow", pToken->z);
2022-05-31 09:49:33 +00:00
}
2022-11-27 09:09:02 +00:00
break;
2022-05-31 09:49:33 +00:00
}
case TSDB_DATA_TYPE_SMALLINT: {
2022-11-27 09:09:02 +00:00
if (TSDB_CODE_SUCCESS != toInteger(pToken->z, pToken->n, 10, &pVal->value.val)) {
2022-11-04 07:21:38 +00:00
return buildSyntaxErrMsg(&pCxt->msg, "invalid smallint data", pToken->z);
2022-11-27 09:09:02 +00:00
} else if (!IS_VALID_SMALLINT(pVal->value.val)) {
2022-11-04 07:21:38 +00:00
return buildSyntaxErrMsg(&pCxt->msg, "smallint data overflow", pToken->z);
2022-05-31 09:49:33 +00:00
}
2022-11-27 09:09:02 +00:00
break;
2022-05-31 09:49:33 +00:00
}
case TSDB_DATA_TYPE_USMALLINT: {
2022-11-27 09:09:02 +00:00
if (TSDB_CODE_SUCCESS != toUInteger(pToken->z, pToken->n, 10, &pVal->value.val)) {
2022-11-04 07:21:38 +00:00
return buildSyntaxErrMsg(&pCxt->msg, "invalid unsigned smallint data", pToken->z);
2022-11-27 09:09:02 +00:00
} else if (pVal->value.val > UINT16_MAX) {
2022-11-04 07:21:38 +00:00
return buildSyntaxErrMsg(&pCxt->msg, "unsigned smallint data overflow", pToken->z);
2022-05-31 09:49:33 +00:00
}
2022-11-27 09:09:02 +00:00
break;
2022-05-31 09:49:33 +00:00
}
case TSDB_DATA_TYPE_INT: {
2022-11-27 09:09:02 +00:00
if (TSDB_CODE_SUCCESS != toInteger(pToken->z, pToken->n, 10, &pVal->value.val)) {
2022-11-04 07:21:38 +00:00
return buildSyntaxErrMsg(&pCxt->msg, "invalid int data", pToken->z);
2022-11-27 09:09:02 +00:00
} else if (!IS_VALID_INT(pVal->value.val)) {
2022-11-04 07:21:38 +00:00
return buildSyntaxErrMsg(&pCxt->msg, "int data overflow", pToken->z);
2022-05-31 09:49:33 +00:00
}
2022-11-27 09:09:02 +00:00
break;
2022-05-31 09:49:33 +00:00
}
case TSDB_DATA_TYPE_UINT: {
2022-11-27 09:09:02 +00:00
if (TSDB_CODE_SUCCESS != toUInteger(pToken->z, pToken->n, 10, &pVal->value.val)) {
2022-11-04 07:21:38 +00:00
return buildSyntaxErrMsg(&pCxt->msg, "invalid unsigned int data", pToken->z);
2022-11-27 09:09:02 +00:00
} else if (pVal->value.val > UINT32_MAX) {
2022-11-04 07:21:38 +00:00
return buildSyntaxErrMsg(&pCxt->msg, "unsigned int data overflow", pToken->z);
2022-05-31 09:49:33 +00:00
}
2022-11-27 09:09:02 +00:00
break;
2022-05-31 09:49:33 +00:00
}
case TSDB_DATA_TYPE_BIGINT: {
2022-11-27 09:09:02 +00:00
if (TSDB_CODE_SUCCESS != toInteger(pToken->z, pToken->n, 10, &pVal->value.val)) {
2022-11-04 07:21:38 +00:00
return buildSyntaxErrMsg(&pCxt->msg, "invalid bigint data", pToken->z);
2022-05-31 09:49:33 +00:00
}
2022-11-27 09:09:02 +00:00
break;
2022-05-31 09:49:33 +00:00
}
case TSDB_DATA_TYPE_UBIGINT: {
2022-11-27 09:09:02 +00:00
if (TSDB_CODE_SUCCESS != toUInteger(pToken->z, pToken->n, 10, &pVal->value.val)) {
2022-11-04 07:21:38 +00:00
return buildSyntaxErrMsg(&pCxt->msg, "invalid unsigned bigint data", pToken->z);
2022-05-31 09:49:33 +00:00
}
2022-11-27 09:09:02 +00:00
break;
2022-05-31 09:49:33 +00:00
}
case TSDB_DATA_TYPE_FLOAT: {
2022-11-27 09:09:02 +00:00
char* endptr = NULL;
2022-05-31 09:49:33 +00:00
double dv;
if (TK_NK_ILLEGAL == toDouble(pToken, &dv, &endptr)) {
2022-11-04 07:21:38 +00:00
return buildSyntaxErrMsg(&pCxt->msg, "illegal float data", pToken->z);
2022-05-31 09:49:33 +00:00
}
if (((dv == HUGE_VAL || dv == -HUGE_VAL) && errno == ERANGE) || dv > FLT_MAX || dv < -FLT_MAX || isinf(dv) ||
isnan(dv)) {
2022-11-04 07:21:38 +00:00
return buildSyntaxErrMsg(&pCxt->msg, "illegal float data", pToken->z);
2022-05-31 09:49:33 +00:00
}
2022-12-04 02:09:10 +00:00
float f = dv;
memcpy(&pVal->value.val, &f, sizeof(f));
2022-11-27 09:09:02 +00:00
break;
2022-05-31 09:49:33 +00:00
}
case TSDB_DATA_TYPE_DOUBLE: {
2022-11-27 09:09:02 +00:00
char* endptr = NULL;
2022-05-31 09:49:33 +00:00
double dv;
if (TK_NK_ILLEGAL == toDouble(pToken, &dv, &endptr)) {
2022-11-04 07:21:38 +00:00
return buildSyntaxErrMsg(&pCxt->msg, "illegal double data", pToken->z);
2022-05-31 09:49:33 +00:00
}
if (((dv == HUGE_VAL || dv == -HUGE_VAL) && errno == ERANGE) || isinf(dv) || isnan(dv)) {
2022-11-04 07:21:38 +00:00
return buildSyntaxErrMsg(&pCxt->msg, "illegal double data", pToken->z);
2022-05-31 09:49:33 +00:00
}
2022-11-27 09:09:02 +00:00
pVal->value.val = *(int64_t*)&dv;
break;
2022-05-31 09:49:33 +00:00
}
case TSDB_DATA_TYPE_BINARY: {
// Too long values will raise the invalid sql error message
if (pToken->n + VARSTR_HEADER_SIZE > pSchema->bytes) {
2022-11-04 07:21:38 +00:00
return generateSyntaxErrMsg(&pCxt->msg, TSDB_CODE_PAR_VALUE_TOO_LONG, pSchema->name);
2022-04-16 09:47:42 +00:00
}
2022-12-07 02:29:17 +00:00
pVal->value.pData = taosMemoryMalloc(pToken->n);
if (NULL == pVal->value.pData) {
return TSDB_CODE_OUT_OF_MEMORY;
}
memcpy(pVal->value.pData, pToken->z, pToken->n);
2022-11-27 09:09:02 +00:00
pVal->value.nData = pToken->n;
break;
2022-10-17 08:54:12 +00:00
}
2022-11-04 07:21:38 +00:00
case TSDB_DATA_TYPE_NCHAR: {
2022-11-27 09:09:02 +00:00
// if the converted output len is over than pColumnModel->bytes, return error: 'Argument list too long'
int32_t len = 0;
char* pUcs4 = taosMemoryCalloc(1, pSchema->bytes - VARSTR_HEADER_SIZE);
if (NULL == pUcs4) {
return TSDB_CODE_OUT_OF_MEMORY;
}
if (!taosMbsToUcs4(pToken->z, pToken->n, (TdUcs4*)pUcs4, pSchema->bytes - VARSTR_HEADER_SIZE, &len)) {
taosMemoryFree(pUcs4);
2022-11-27 09:09:02 +00:00
if (errno == E2BIG) {
return generateSyntaxErrMsg(&pCxt->msg, TSDB_CODE_PAR_VALUE_TOO_LONG, pSchema->name);
}
char buf[512] = {0};
snprintf(buf, tListLen(buf), "%s", strerror(errno));
return buildSyntaxErrMsg(&pCxt->msg, buf, pToken->z);
}
pVal->value.pData = pUcs4;
pVal->value.nData = len;
break;
}
2022-11-04 07:21:38 +00:00
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);
}
2022-12-07 02:29:17 +00:00
pVal->value.pData = taosMemoryMalloc(pToken->n);
if (NULL == pVal->value.pData) {
return TSDB_CODE_OUT_OF_MEMORY;
}
memcpy(pVal->value.pData, pToken->z, pToken->n);
2022-11-27 09:09:02 +00:00
pVal->value.nData = pToken->n;
break;
}
2022-11-04 07:21:38 +00:00
case TSDB_DATA_TYPE_TIMESTAMP: {
2022-11-27 09:09:02 +00:00
if (parseTime(pSql, pToken, timePrec, &pVal->value.val, &pCxt->msg) != TSDB_CODE_SUCCESS) {
2022-11-04 07:21:38 +00:00
return buildSyntaxErrMsg(&pCxt->msg, "invalid timestamp", pToken->z);
}
2022-11-27 09:09:02 +00:00
break;
2022-11-04 07:21:38 +00:00
}
2022-11-27 09:09:02 +00:00
default:
return TSDB_CODE_FAILED;
2022-04-02 02:37:47 +00:00
}
2022-11-27 16:08:41 +00:00
pVal->flag = CV_FLAG_VALUE;
2022-11-27 09:09:02 +00:00
return TSDB_CODE_SUCCESS;
2022-11-04 07:21:38 +00:00
}
2022-05-10 07:50:41 +00:00
2022-11-04 07:21:38 +00:00
static int32_t parseValueToken(SInsertParseContext* pCxt, const char** pSql, SToken* pToken, SSchema* pSchema,
2022-11-27 09:09:02 +00:00
int16_t timePrec, SColVal* pVal) {
2022-11-04 07:21:38 +00:00
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);
}
2022-11-27 09:09:02 +00:00
pVal->flag = CV_FLAG_NULL;
return TSDB_CODE_SUCCESS;
}
2022-11-04 07:21:38 +00:00
if (TSDB_CODE_SUCCESS == code && IS_NUMERIC_TYPE(pSchema->type) && pToken->n == 0) {
return buildSyntaxErrMsg(&pCxt->msg, "invalid numeric data", pToken->z);
}
2022-11-04 07:21:38 +00:00
if (TSDB_CODE_SUCCESS == code) {
2022-11-27 09:09:02 +00:00
code = parseValueTokenImpl(pCxt, pSql, pToken, pSchema, timePrec, pVal);
2022-04-02 07:27:07 +00:00
}
2022-11-04 07:21:38 +00:00
return code;
}
2022-12-06 12:40:18 +00:00
static void clearColValArray(SArray* pCols) {
int32_t num = taosArrayGetSize(pCols);
for (int32_t i = 0; i < num; ++i) {
SColVal* pCol = taosArrayGet(pCols, i);
2022-12-07 02:29:17 +00:00
if (IS_VAR_DATA_TYPE(pCol->type)) {
2022-12-06 12:40:18 +00:00
taosMemoryFreeClear(pCol->value.pData);
}
}
}
2022-11-27 09:09:02 +00:00
static int parseOneRow(SInsertParseContext* pCxt, const char** pSql, STableDataCxt* pTableCxt, bool* pGotRow,
2022-11-04 07:21:38 +00:00
SToken* pToken) {
2022-11-27 09:09:02 +00:00
SBoundColInfo* pCols = &pTableCxt->boundColsInfo;
bool isParseBindParam = false;
SSchema* pSchemas = getTableColumnSchema(pTableCxt->pMeta);
int32_t code = TSDB_CODE_SUCCESS;
// 1. set the parsed value from sql string
2022-11-04 07:21:38 +00:00
for (int i = 0; i < pCols->numOfBound && TSDB_CODE_SUCCESS == code; ++i) {
2023-02-05 02:11:26 +00:00
const char* pOrigSql = *pSql;
bool ignoreComma = false;
NEXT_TOKEN_WITH_PREV_EXT(*pSql, *pToken, &ignoreComma);
if (ignoreComma) {
code = buildSyntaxErrMsg(&pCxt->msg, "invalid data or symbol", pOrigSql);
2023-02-06 03:07:51 +00:00
break;
2023-02-05 02:11:26 +00:00
}
2022-04-14 12:14:52 +00:00
2022-11-27 09:09:02 +00:00
SSchema* pSchema = &pSchemas[pCols->pColIndex[i]];
SColVal* pVal = taosArrayGet(pTableCxt->pValues, pCols->pColIndex[i]);
2022-04-14 12:14:52 +00:00
2022-11-04 07:21:38 +00:00
if (pToken->type == TK_NK_QUESTION) {
2022-04-14 12:14:52 +00:00
isParseBindParam = true;
2022-11-04 07:21:38 +00:00
if (NULL == pCxt->pComCxt->pStmtCb) {
code = buildSyntaxErrMsg(&pCxt->msg, "? only used in stmt", pToken->z);
2023-02-06 03:07:51 +00:00
break;
}
} else {
if (TK_NK_RP == pToken->type) {
code = generateSyntaxErrMsg(&pCxt->msg, TSDB_CODE_PAR_INVALID_COLUMNS_NUM);
break;
2022-04-14 12:14:52 +00:00
}
2022-06-02 04:34:35 +00:00
2023-02-06 03:07:51 +00:00
if (isParseBindParam) {
code = buildInvalidOperationMsg(&pCxt->msg, "no mix usage for ? and values");
break;
}
2022-04-26 03:50:35 +00:00
2023-02-08 01:51:52 +00:00
if (TSDB_CODE_SUCCESS == code) {
code = parseValueToken(pCxt, pSql, pToken, pSchema, getTableInfo(pTableCxt->pMeta).precision, pVal);
}
2022-11-04 07:21:38 +00:00
}
2022-11-04 07:21:38 +00:00
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);
2022-07-14 08:58:54 +00:00
}
}
}
2022-11-30 11:08:48 +00:00
if (TSDB_CODE_SUCCESS == code && !isParseBindParam) {
2022-11-27 16:08:41 +00:00
SRow** pRow = taosArrayReserve(pTableCxt->pData->aRowP, 1);
2022-11-27 09:09:02 +00:00
code = tRowBuild(pTableCxt->pValues, pTableCxt->pSchema, pRow);
2022-11-30 02:22:43 +00:00
if (TSDB_CODE_SUCCESS == code) {
insCheckTableDataOrder(pTableCxt, TD_ROW_KEY(*pRow));
}
2022-11-04 07:21:38 +00:00
}
2022-07-30 11:23:06 +00:00
2022-11-04 07:21:38 +00:00
if (TSDB_CODE_SUCCESS == code && !isParseBindParam) {
*pGotRow = true;
}
2022-12-06 12:40:18 +00:00
clearColValArray(pTableCxt->pValues);
2022-11-04 07:21:38 +00:00
return code;
}
// pSql -> (field1_value, ...) [(field1_value2, ...) ...]
2022-12-23 09:50:05 +00:00
static int32_t parseValues(SInsertParseContext* pCxt, SVnodeModifyOpStmt* pStmt, STableDataCxt* pTableCxt,
2022-11-27 09:09:02 +00:00
int32_t* pNumOfRows, SToken* pToken) {
int32_t code = TSDB_CODE_SUCCESS;
2022-11-04 07:21:38 +00:00
(*pNumOfRows) = 0;
while (TSDB_CODE_SUCCESS == code) {
int32_t index = 0;
2022-11-04 07:21:38 +00:00
NEXT_TOKEN_KEEP_SQL(pStmt->pSql, *pToken, index);
if (TK_NK_LP != pToken->type) {
break;
}
2022-11-04 07:21:38 +00:00
pStmt->pSql += index;
2022-04-18 11:08:27 +00:00
bool gotRow = false;
2022-11-04 07:21:38 +00:00
if (TSDB_CODE_SUCCESS == code) {
2022-11-27 09:09:02 +00:00
code = parseOneRow(pCxt, &pStmt->pSql, pTableCxt, &gotRow, pToken);
2022-04-18 11:08:27 +00:00
}
2022-11-04 07:21:38 +00:00
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);
}
}
2022-11-04 07:21:38 +00:00
if (TSDB_CODE_SUCCESS == code && gotRow) {
(*pNumOfRows)++;
2022-04-18 11:08:27 +00:00
}
}
2022-11-04 07:21:38 +00:00
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);
}
2022-11-04 07:21:38 +00:00
return code;
}
2022-11-04 07:21:38 +00:00
// VALUES (field1_value, ...) [(field1_value2, ...) ...]
2022-12-23 09:50:05 +00:00
static int32_t parseValuesClause(SInsertParseContext* pCxt, SVnodeModifyOpStmt* pStmt, STableDataCxt* pTableCxt,
2022-11-04 07:21:38 +00:00
SToken* pToken) {
int32_t numOfRows = 0;
2022-11-27 09:09:02 +00:00
int32_t code = parseValues(pCxt, pStmt, pTableCxt, &numOfRows, pToken);
2022-11-04 07:21:38 +00:00
if (TSDB_CODE_SUCCESS == code) {
pStmt->totalRowsNum += numOfRows;
pStmt->totalTbNum += 1;
TSDB_QUERY_SET_TYPE(pStmt->insertType, TSDB_QUERY_TYPE_INSERT);
}
return code;
}
2022-12-23 09:50:05 +00:00
static int32_t parseCsvFile(SInsertParseContext* pCxt, SVnodeModifyOpStmt* pStmt, STableDataCxt* pTableCxt,
2022-11-27 09:09:02 +00:00
int32_t* pNumOfRows) {
int32_t code = TSDB_CODE_SUCCESS;
2022-11-04 07:21:38 +00:00
(*pNumOfRows) = 0;
2022-06-30 07:04:54 +00:00
char* pLine = NULL;
int64_t readLen = 0;
2022-12-14 08:34:56 +00:00
bool firstLine = (pStmt->fileProcessing == false);
2022-11-06 06:26:19 +00:00
pStmt->fileProcessing = false;
2022-11-04 07:21:38 +00:00
while (TSDB_CODE_SUCCESS == code && (readLen = taosGetLineFile(pStmt->fp, &pLine)) != -1) {
2022-06-30 07:04:54 +00:00
if (('\r' == pLine[readLen - 1]) || ('\n' == pLine[readLen - 1])) {
pLine[--readLen] = '\0';
}
if (readLen == 0) {
2022-12-14 08:34:56 +00:00
firstLine = false;
2022-06-30 07:04:54 +00:00
continue;
}
2022-11-04 07:21:38 +00:00
bool gotRow = false;
if (TSDB_CODE_SUCCESS == code) {
SToken token;
strtolower(pLine, pLine);
2022-11-06 06:26:19 +00:00
const char* pRow = pLine;
2022-12-19 03:55:53 +00:00
code = parseOneRow(pCxt, (const char**)&pRow, pTableCxt, &gotRow, &token);
2022-12-14 08:34:56 +00:00
if (code && firstLine) {
firstLine = false;
code = 0;
continue;
}
2022-10-19 05:56:39 +00:00
}
2022-11-04 07:21:38 +00:00
if (TSDB_CODE_SUCCESS == code && gotRow) {
(*pNumOfRows)++;
2022-06-30 07:04:54 +00:00
}
2022-11-27 09:09:02 +00:00
if (TSDB_CODE_SUCCESS == code && (*pNumOfRows) > tsMaxMemUsedByInsert * 1024 * 1024) {
2022-11-06 06:26:19 +00:00
pStmt->fileProcessing = true;
break;
}
2022-12-14 08:34:56 +00:00
firstLine = false;
2022-06-30 07:04:54 +00:00
}
2022-11-14 06:38:51 +00:00
taosMemoryFree(pLine);
2022-06-30 07:04:54 +00:00
2022-11-04 07:21:38 +00:00
if (TSDB_CODE_SUCCESS == code && 0 == (*pNumOfRows) &&
2022-11-06 06:26:19 +00:00
(!TSDB_QUERY_HAS_TYPE(pStmt->insertType, TSDB_QUERY_TYPE_STMT_INSERT)) && !pStmt->fileProcessing) {
2022-11-04 07:21:38 +00:00
code = buildSyntaxErrMsg(&pCxt->msg, "no any data points", NULL);
2022-06-30 07:04:54 +00:00
}
2022-11-04 07:21:38 +00:00
return code;
2022-06-30 07:04:54 +00:00
}
2022-12-23 09:50:05 +00:00
static int32_t parseDataFromFileImpl(SInsertParseContext* pCxt, SVnodeModifyOpStmt* pStmt, STableDataCxt* pTableCxt) {
2022-06-30 07:04:54 +00:00
int32_t numOfRows = 0;
2022-11-27 09:09:02 +00:00
int32_t code = parseCsvFile(pCxt, pStmt, pTableCxt, &numOfRows);
2022-11-04 07:21:38 +00:00
if (TSDB_CODE_SUCCESS == code) {
pStmt->totalRowsNum += numOfRows;
pStmt->totalTbNum += 1;
TSDB_QUERY_SET_TYPE(pStmt->insertType, TSDB_QUERY_TYPE_FILE_INSERT);
2022-11-06 06:26:19 +00:00
if (!pStmt->fileProcessing) {
2022-11-04 07:21:38 +00:00
taosCloseFile(&pStmt->fp);
} else {
parserDebug("0x%" PRIx64 " insert from csv. File is too large, do it in batches.", pCxt->pComCxt->requestId);
}
}
2022-11-06 06:26:19 +00:00
return code;
2022-06-30 07:04:54 +00:00
}
2022-12-20 08:53:08 +00:00
static int32_t parseDataFromFile(SInsertParseContext* pCxt, SVnodeModifyOpStmt* pStmt, SToken* pFilePath,
2022-11-27 09:09:02 +00:00
STableDataCxt* pTableCxt) {
char filePathStr[TSDB_FILENAME_LEN] = {0};
2022-11-04 07:21:38 +00:00
if (TK_NK_STRING == pFilePath->type) {
trimString(pFilePath->z, pFilePath->n, filePathStr, sizeof(filePathStr));
} else {
2022-11-04 07:21:38 +00:00
strncpy(filePathStr, pFilePath->z, pFilePath->n);
}
2022-11-04 07:21:38 +00:00
pStmt->fp = taosOpenFile(filePathStr, TD_FILE_READ | TD_FILE_STREAM);
if (NULL == pStmt->fp) {
return TAOS_SYSTEM_ERROR(errno);
}
2022-11-27 09:09:02 +00:00
return parseDataFromFileImpl(pCxt, pStmt, pTableCxt);
}
2022-12-23 09:50:05 +00:00
static int32_t parseFileClause(SInsertParseContext* pCxt, SVnodeModifyOpStmt* pStmt, STableDataCxt* pTableCxt,
2022-11-04 07:21:38 +00:00
SToken* pToken) {
if (tsUseAdapter) {
return buildInvalidOperationMsg(&pCxt->msg, "proxy mode does not support csv loading");
}
2022-11-04 07:21:38 +00:00
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);
}
2022-11-27 09:09:02 +00:00
return parseDataFromFile(pCxt, pStmt, pToken, pTableCxt);
2021-12-22 05:39:11 +00:00
}
2022-11-04 07:21:38 +00:00
// VALUES (field1_value, ...) [(field1_value2, ...) ...] | FILE csv_file_path
2022-12-23 09:50:05 +00:00
static int32_t parseDataClause(SInsertParseContext* pCxt, SVnodeModifyOpStmt* pStmt, STableDataCxt* pTableCxt) {
2022-11-04 07:21:38 +00:00
SToken token;
NEXT_TOKEN(pStmt->pSql, token);
switch (token.type) {
case TK_VALUES:
2022-11-27 09:09:02 +00:00
return parseValuesClause(pCxt, pStmt, pTableCxt, &token);
2022-11-04 07:21:38 +00:00
case TK_FILE:
2022-11-27 09:09:02 +00:00
return parseFileClause(pCxt, pStmt, pTableCxt, &token);
2022-11-04 07:21:38 +00:00
default:
break;
}
return buildSyntaxErrMsg(&pCxt->msg, "keyword VALUES or FILE is expected", token.z);
2021-12-22 05:39:11 +00:00
}
2022-11-04 07:21:38 +00:00
// input pStmt->pSql:
// 1. [(tag1_name, ...)] ...
// 2. VALUES ... | FILE ...
2022-12-20 08:53:08 +00:00
static int32_t parseInsertTableClauseBottom(SInsertParseContext* pCxt, SVnodeModifyOpStmt* pStmt) {
2022-11-27 09:09:02 +00:00
STableDataCxt* pTableCxt = NULL;
int32_t code = parseSchemaClauseBottom(pCxt, pStmt, &pTableCxt);
2022-08-15 06:45:59 +00:00
if (TSDB_CODE_SUCCESS == code) {
2022-11-27 09:09:02 +00:00
code = parseDataClause(pCxt, pStmt, pTableCxt);
2022-08-15 06:45:59 +00:00
}
return code;
}
2022-12-20 08:53:08 +00:00
static void resetEnvPreTable(SInsertParseContext* pCxt, SVnodeModifyOpStmt* pStmt) {
2022-12-28 02:49:38 +00:00
insDestroyBoundColInfo(&pCxt->tags);
2022-11-06 10:42:53 +00:00
taosMemoryFreeClear(pStmt->pTableMeta);
2022-11-27 09:09:02 +00:00
tdDestroySVCreateTbReq(pStmt->pCreateTblReq);
taosMemoryFreeClear(pStmt->pCreateTblReq);
2022-11-06 10:42:53 +00:00
pCxt->missCache = false;
pCxt->usingDuplicateTable = false;
2022-11-07 03:31:45 +00:00
pStmt->pBoundCols = NULL;
2022-11-06 10:42:53 +00:00
pStmt->usingTableProcessing = false;
pStmt->fileProcessing = false;
}
2022-11-04 07:21:38 +00:00
// input pStmt->pSql: [(field1_name, ...)] [ USING ... ] VALUES ... | FILE ...
2022-12-20 08:53:08 +00:00
static int32_t parseInsertTableClause(SInsertParseContext* pCxt, SVnodeModifyOpStmt* pStmt, SToken* pTbName) {
2022-11-06 10:42:53 +00:00
resetEnvPreTable(pCxt, pStmt);
2022-11-04 07:21:38 +00:00
int32_t code = parseSchemaClauseTop(pCxt, pStmt, pTbName);
if (TSDB_CODE_SUCCESS == code && !pCxt->missCache) {
code = parseInsertTableClauseBottom(pCxt, pStmt);
}
return code;
}
2022-05-24 09:37:53 +00:00
2022-12-20 08:53:08 +00:00
static int32_t checkTableClauseFirstToken(SInsertParseContext* pCxt, SVnodeModifyOpStmt* pStmt, SToken* pTbName,
2022-11-04 07:21:38 +00:00
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);
}
2022-11-04 07:21:38 +00:00
if (0 == pStmt->totalRowsNum && (!TSDB_QUERY_HAS_TYPE(pStmt->insertType, TSDB_QUERY_TYPE_STMT_INSERT))) {
return buildInvalidOperationMsg(&pCxt->msg, "no data in sql");
2022-04-14 12:14:52 +00:00
}
2022-11-04 07:21:38 +00:00
*pHasData = false;
return TSDB_CODE_SUCCESS;
}
2022-04-26 03:50:35 +00:00
2022-11-04 07:21:38 +00:00
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");
}
2022-11-04 07:21:38 +00:00
if (TK_NK_QUESTION == pTbName->type) {
if (NULL == pCxt->pComCxt->pStmtCb) {
return buildSyntaxErrMsg(&pCxt->msg, "? only used in stmt", pTbName->z);
2022-08-10 09:47:28 +00:00
}
2022-05-12 10:59:02 +00:00
2022-11-04 07:21:38 +00:00
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;
}
2022-11-04 07:21:38 +00:00
}
2022-11-04 07:21:38 +00:00
*pHasData = true;
return TSDB_CODE_SUCCESS;
}
2022-12-20 08:53:08 +00:00
static int32_t setStmtInfo(SInsertParseContext* pCxt, SVnodeModifyOpStmt* pStmt) {
2022-12-28 02:49:38 +00:00
SBoundColInfo* tags = taosMemoryMalloc(sizeof(pCxt->tags));
2022-11-04 07:21:38 +00:00
if (NULL == tags) {
2022-12-03 02:03:18 +00:00
return TSDB_CODE_OUT_OF_MEMORY;
2022-11-04 07:21:38 +00:00
}
memcpy(tags, &pCxt->tags, sizeof(pCxt->tags));
2022-11-04 07:21:38 +00:00
SStmtCallback* pStmtCb = pCxt->pComCxt->pStmtCb;
2022-12-04 02:09:10 +00:00
int32_t code = (*pStmtCb->setInfoFn)(pStmtCb->pStmt, pStmt->pTableMeta, tags, &pStmt->targetTableName,
pStmt->usingTableProcessing, pStmt->pVgroupsHashObj, pStmt->pTableBlockHashObj,
pStmt->usingTableName.tname);
2022-11-04 07:21:38 +00:00
memset(&pCxt->tags, 0, sizeof(pCxt->tags));
pStmt->pVgroupsHashObj = NULL;
pStmt->pTableBlockHashObj = NULL;
return code;
}
2022-12-20 08:53:08 +00:00
static int32_t parseInsertBodyBottom(SInsertParseContext* pCxt, SVnodeModifyOpStmt* pStmt) {
2022-11-04 07:21:38 +00:00
if (TSDB_QUERY_HAS_TYPE(pStmt->insertType, TSDB_QUERY_TYPE_STMT_INSERT)) {
return setStmtInfo(pCxt, pStmt);
}
2022-04-14 12:14:52 +00:00
2022-11-04 07:21:38 +00:00
// merge according to vgId
2022-11-27 09:09:02 +00:00
int32_t code = insMergeTableDataCxt(pStmt->pTableBlockHashObj, &pStmt->pVgDataBlocks);
2022-11-04 07:21:38 +00:00
if (TSDB_CODE_SUCCESS == code) {
2022-11-27 09:09:02 +00:00
code = insBuildVgDataBlocks(pStmt->pVgroupsHashObj, pStmt->pVgDataBlocks, &pStmt->pDataBlocks);
2022-11-04 07:21:38 +00:00
}
2022-11-27 09:09:02 +00:00
2022-11-04 07:21:38 +00:00
return code;
}
2022-11-04 07:21:38 +00:00
// tb_name
// [USING stb_name [(tag1_name, ...)] TAGS (tag1_value, ...)]
// [(field1_name, ...)]
// VALUES (field1_value, ...) [(field1_value2, ...) ...] | FILE csv_file_path
// [...];
2022-12-20 08:53:08 +00:00
static int32_t parseInsertBody(SInsertParseContext* pCxt, SVnodeModifyOpStmt* pStmt) {
2022-11-04 07:21:38 +00:00
SToken token;
int32_t code = TSDB_CODE_SUCCESS;
bool hasData = true;
// for each table
2022-11-06 06:26:19 +00:00
while (TSDB_CODE_SUCCESS == code && hasData && !pCxt->missCache && !pStmt->fileProcessing) {
2022-11-04 07:21:38 +00:00
// 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);
}
}
2022-04-26 03:50:35 +00:00
2022-11-04 07:21:38 +00:00
if (TSDB_CODE_SUCCESS == code && !pCxt->missCache) {
code = parseInsertBodyBottom(pCxt, pStmt);
}
return code;
}
2022-04-14 12:14:52 +00:00
2022-11-04 07:21:38 +00:00
static void destroySubTableHashElem(void* p) { taosMemoryFree(*(STableMeta**)p); }
2022-04-26 03:50:35 +00:00
2022-11-22 10:18:31 +00:00
static int32_t createVnodeModifOpStmt(SInsertParseContext* pCxt, bool reentry, SNode** pOutput) {
2022-12-20 08:53:08 +00:00
SVnodeModifyOpStmt* pStmt = (SVnodeModifyOpStmt*)nodesMakeNode(QUERY_NODE_VNODE_MODIFY_STMT);
2022-11-04 07:21:38 +00:00
if (NULL == pStmt) {
return TSDB_CODE_OUT_OF_MEMORY;
2022-04-14 12:14:52 +00:00
}
2022-04-26 03:50:35 +00:00
2022-11-22 10:18:31 +00:00
if (pCxt->pComCxt->pStmtCb) {
2022-11-04 07:21:38 +00:00
TSDB_QUERY_SET_TYPE(pStmt->insertType, TSDB_QUERY_TYPE_STMT_INSERT);
}
2022-11-22 10:18:31 +00:00
pStmt->pSql = pCxt->pComCxt->pSql;
2022-11-27 09:09:02 +00:00
pStmt->freeHashFunc = insDestroyTableDataCxtHashMap;
pStmt->freeArrayFunc = insDestroyVgroupDataCxtList;
2022-11-04 07:21:38 +00:00
2022-11-07 03:31:45 +00:00
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);
}
2022-11-04 07:21:38 +00:00
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);
2022-11-07 03:31:45 +00:00
if ((!reentry && (NULL == pStmt->pVgroupsHashObj || NULL == pStmt->pTableBlockHashObj)) ||
NULL == pStmt->pSubTableHashObj || NULL == pStmt->pTableNameHashObj || NULL == pStmt->pDbFNameHashObj) {
2022-11-04 07:21:38 +00:00
nodesDestroyNode((SNode*)pStmt);
return TSDB_CODE_OUT_OF_MEMORY;
}
2022-11-04 07:21:38 +00:00
taosHashSetFreeFp(pStmt->pSubTableHashObj, destroySubTableHashElem);
*pOutput = (SNode*)pStmt;
return TSDB_CODE_SUCCESS;
}
2022-11-22 10:18:31 +00:00
static int32_t createInsertQuery(SInsertParseContext* pCxt, SQuery** pOutput) {
2022-11-04 07:21:38 +00:00
SQuery* pQuery = (SQuery*)nodesMakeNode(QUERY_NODE_QUERY);
if (NULL == pQuery) {
return TSDB_CODE_OUT_OF_MEMORY;
2022-04-16 07:59:19 +00:00
}
2022-04-26 03:50:35 +00:00
2022-11-04 07:21:38 +00:00
pQuery->execMode = QUERY_EXEC_MODE_SCHEDULE;
pQuery->haveResultSet = false;
pQuery->msgType = TDMT_VND_SUBMIT;
2022-11-07 03:31:45 +00:00
int32_t code = createVnodeModifOpStmt(pCxt, false, &pQuery->pRoot);
2022-11-04 07:21:38 +00:00
if (TSDB_CODE_SUCCESS == code) {
*pOutput = pQuery;
} else {
nodesDestroyNode((SNode*)pQuery);
2022-04-14 12:14:52 +00:00
}
2022-11-04 07:21:38 +00:00
return code;
}
2022-04-14 12:14:52 +00:00
2022-11-04 07:21:38 +00:00
static int32_t checkAuthFromMetaData(const SArray* pUsers) {
if (1 != taosArrayGetSize(pUsers)) {
return TSDB_CODE_FAILED;
}
2022-11-04 07:21:38 +00:00
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;
}
2022-04-26 03:50:35 +00:00
2022-11-04 07:21:38 +00:00
static int32_t getTableMetaFromMetaData(const SArray* pTables, STableMeta** pMeta) {
if (1 != taosArrayGetSize(pTables)) {
return TSDB_CODE_FAILED;
}
2022-12-19 02:25:14 +00:00
taosMemoryFreeClear(*pMeta);
2022-11-04 07:21:38 +00:00
SMetaRes* pRes = taosArrayGet(pTables, 0);
if (TSDB_CODE_SUCCESS == pRes->code) {
*pMeta = tableMetaDup((const STableMeta*)pRes->pRes);
if (NULL == *pMeta) {
2022-05-13 08:35:19 +00:00
return TSDB_CODE_OUT_OF_MEMORY;
}
}
2022-11-04 07:21:38 +00:00
return pRes->code;
}
2022-12-20 08:53:08 +00:00
static int32_t getTableVgroupFromMetaData(const SArray* pTables, SVnodeModifyOpStmt* pStmt, bool isStb) {
2022-11-04 07:21:38 +00:00
if (1 != taosArrayGetSize(pTables)) {
return TSDB_CODE_FAILED;
2022-06-02 04:34:35 +00:00
}
2022-11-04 07:21:38 +00:00
SMetaRes* pRes = taosArrayGet(pTables, 0);
if (TSDB_CODE_SUCCESS != pRes->code) {
return pRes->code;
}
2022-11-04 07:21:38 +00:00
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));
}
2022-06-02 04:34:35 +00:00
2022-11-22 10:18:31 +00:00
static int32_t getTableSchemaFromMetaData(SInsertParseContext* pCxt, const SMetaData* pMetaData,
2022-12-20 08:53:08 +00:00
SVnodeModifyOpStmt* pStmt, bool isStb) {
2022-11-04 07:21:38 +00:00
int32_t code = checkAuthFromMetaData(pMetaData->pUser);
if (TSDB_CODE_SUCCESS == code) {
code = getTableMetaFromMetaData(pMetaData->pTableMeta, &pStmt->pTableMeta);
2022-05-12 10:59:02 +00:00
}
2022-11-22 10:18:31 +00:00
if (TSDB_CODE_SUCCESS == code && !isStb && TSDB_SUPER_TABLE == pStmt->pTableMeta->tableType) {
code = buildInvalidOperationMsg(&pCxt->msg, "insert data into super table is not supported");
}
2023-02-01 02:15:42 +00:00
if (TSDB_CODE_SUCCESS == code && isStb) {
code = storeTableMeta(pCxt, pStmt);
}
2022-11-04 07:21:38 +00:00
if (TSDB_CODE_SUCCESS == code) {
code = getTableVgroupFromMetaData(pMetaData->pTableHash, pStmt, isStb);
}
2022-01-06 23:34:51 +00:00
return code;
}
2022-04-16 11:41:18 +00:00
2022-11-06 06:26:19 +00:00
static void destoryTablesReq(void* p) {
STablesReq* pRes = (STablesReq*)p;
taosArrayDestroy(pRes->pTables);
}
static void clearCatalogReq(SCatalogReq* pCatalogReq) {
2022-11-07 03:31:45 +00:00
if (NULL == pCatalogReq) {
return;
}
2022-11-06 06:26:19 +00:00
taosArrayDestroyEx(pCatalogReq->pTableMeta, destoryTablesReq);
pCatalogReq->pTableMeta = NULL;
taosArrayDestroyEx(pCatalogReq->pTableHash, destoryTablesReq);
pCatalogReq->pTableHash = NULL;
taosArrayDestroy(pCatalogReq->pUser);
pCatalogReq->pUser = NULL;
}
2022-11-22 10:18:31 +00:00
static int32_t setVnodeModifOpStmt(SInsertParseContext* pCxt, SCatalogReq* pCatalogReq, const SMetaData* pMetaData,
2022-12-20 08:53:08 +00:00
SVnodeModifyOpStmt* pStmt) {
2022-11-06 06:26:19 +00:00
clearCatalogReq(pCatalogReq);
2022-11-07 03:31:45 +00:00
if (pStmt->usingTableProcessing) {
2022-11-22 10:18:31 +00:00
return getTableSchemaFromMetaData(pCxt, pMetaData, pStmt, true);
2022-11-07 03:31:45 +00:00
}
2022-11-22 10:18:31 +00:00
return getTableSchemaFromMetaData(pCxt, pMetaData, pStmt, false);
2022-11-07 03:31:45 +00:00
}
2022-11-22 10:18:31 +00:00
static int32_t resetVnodeModifOpStmt(SInsertParseContext* pCxt, SQuery* pQuery) {
2022-11-07 03:31:45 +00:00
nodesDestroyNode(pQuery->pRoot);
int32_t code = createVnodeModifOpStmt(pCxt, true, &pQuery->pRoot);
if (TSDB_CODE_SUCCESS == code) {
2022-12-20 08:53:08 +00:00
SVnodeModifyOpStmt* pStmt = (SVnodeModifyOpStmt*)pQuery->pRoot;
2022-11-07 03:31:45 +00:00
2022-11-22 10:18:31 +00:00
(*pCxt->pComCxt->pStmtCb->getExecInfoFn)(pCxt->pComCxt->pStmtCb->pStmt, &pStmt->pVgroupsHashObj,
&pStmt->pTableBlockHashObj);
2022-11-04 07:21:38 +00:00
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);
}
2022-11-07 03:31:45 +00:00
if (NULL == pStmt->pVgroupsHashObj || NULL == pStmt->pTableBlockHashObj) {
code = TSDB_CODE_OUT_OF_MEMORY;
}
}
2022-11-04 07:21:38 +00:00
2022-11-07 03:31:45 +00:00
return code;
}
2022-11-22 10:18:31 +00:00
static int32_t initInsertQuery(SInsertParseContext* pCxt, SCatalogReq* pCatalogReq, const SMetaData* pMetaData,
2022-11-06 06:26:19 +00:00
SQuery** pQuery) {
2022-11-04 07:21:38 +00:00
if (NULL == *pQuery) {
return createInsertQuery(pCxt, pQuery);
}
2022-11-06 06:26:19 +00:00
2022-11-22 10:18:31 +00:00
if (NULL != pCxt->pComCxt->pStmtCb) {
2022-11-07 03:31:45 +00:00
return resetVnodeModifOpStmt(pCxt, *pQuery);
}
2022-12-20 08:53:08 +00:00
SVnodeModifyOpStmt* pStmt = (SVnodeModifyOpStmt*)(*pQuery)->pRoot;
2022-11-07 03:31:45 +00:00
if (!pStmt->fileProcessing) {
return setVnodeModifOpStmt(pCxt, pCatalogReq, pMetaData, pStmt);
2022-11-06 06:26:19 +00:00
}
return TSDB_CODE_SUCCESS;
}
2022-11-04 07:21:38 +00:00
static int32_t setRefreshMate(SQuery* pQuery) {
2022-12-20 08:53:08 +00:00
SVnodeModifyOpStmt* pStmt = (SVnodeModifyOpStmt*)pQuery->pRoot;
if (taosHashGetSize(pStmt->pTableNameHashObj) > 0) {
taosArrayDestroy(pQuery->pTableList);
pQuery->pTableList = taosArrayInit(taosHashGetSize(pStmt->pTableNameHashObj), sizeof(SName));
SName* pTable = taosHashIterate(pStmt->pTableNameHashObj, NULL);
while (NULL != pTable) {
taosArrayPush(pQuery->pTableList, pTable);
pTable = taosHashIterate(pStmt->pTableNameHashObj, pTable);
}
}
if (taosHashGetSize(pStmt->pDbFNameHashObj) > 0) {
taosArrayDestroy(pQuery->pDbList);
pQuery->pDbList = taosArrayInit(taosHashGetSize(pStmt->pDbFNameHashObj), TSDB_DB_FNAME_LEN);
char* pDb = taosHashIterate(pStmt->pDbFNameHashObj, NULL);
while (NULL != pDb) {
taosArrayPush(pQuery->pDbList, pDb);
pDb = taosHashIterate(pStmt->pDbFNameHashObj, pDb);
}
}
return TSDB_CODE_SUCCESS;
}
2022-11-04 07:21:38 +00:00
// 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
// [...];
2022-12-20 08:53:08 +00:00
static int32_t parseInsertSqlFromStart(SInsertParseContext* pCxt, SVnodeModifyOpStmt* pStmt) {
2022-11-04 07:21:38 +00:00
int32_t code = skipInsertInto(&pStmt->pSql, &pCxt->msg);
if (TSDB_CODE_SUCCESS == code) {
code = parseInsertBody(pCxt, pStmt);
}
return code;
}
2022-12-20 08:53:08 +00:00
static int32_t parseInsertSqlFromCsv(SInsertParseContext* pCxt, SVnodeModifyOpStmt* pStmt) {
2022-11-27 09:09:02 +00:00
STableDataCxt* pTableCxt = NULL;
int32_t code = getTableDataCxt(pCxt, pStmt, &pTableCxt);
2022-11-04 07:21:38 +00:00
if (TSDB_CODE_SUCCESS == code) {
2022-11-27 09:09:02 +00:00
code = parseDataFromFileImpl(pCxt, pStmt, pTableCxt);
}
2022-11-04 07:21:38 +00:00
if (TSDB_CODE_SUCCESS == code) {
if (pStmt->fileProcessing) {
code = parseInsertBodyBottom(pCxt, pStmt);
} else {
code = parseInsertBody(pCxt, pStmt);
}
}
return code;
2022-06-04 13:31:07 +00:00
}
2022-12-20 08:53:08 +00:00
static int32_t parseInsertSqlFromTable(SInsertParseContext* pCxt, SVnodeModifyOpStmt* pStmt) {
2022-11-04 07:21:38 +00:00
int32_t code = parseInsertTableClauseBottom(pCxt, pStmt);
if (TSDB_CODE_SUCCESS == code) {
code = parseInsertBody(pCxt, pStmt);
}
return code;
}
2022-12-20 08:53:08 +00:00
static int32_t parseInsertSqlImpl(SInsertParseContext* pCxt, SVnodeModifyOpStmt* pStmt) {
2022-11-04 07:21:38 +00:00
if (pStmt->pSql == pCxt->pComCxt->pSql || NULL != pCxt->pComCxt->pStmtCb) {
return parseInsertSqlFromStart(pCxt, pStmt);
}
2022-11-04 07:21:38 +00:00
if (pStmt->fileProcessing) {
return parseInsertSqlFromCsv(pCxt, pStmt);
}
2022-11-04 07:21:38 +00:00
return parseInsertSqlFromTable(pCxt, pStmt);
}
2022-11-04 07:21:38 +00:00
static int32_t buildInsertTableReq(SName* pName, SArray** pTables) {
*pTables = taosArrayInit(1, sizeof(SName));
if (NULL == *pTables) {
return TSDB_CODE_OUT_OF_MEMORY;
}
2022-11-04 07:21:38 +00:00
taosArrayPush(*pTables, pName);
return TSDB_CODE_SUCCESS;
}
2022-11-04 07:21:38 +00:00
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;
}
2022-11-04 07:21:38 +00:00
}
2022-11-04 07:21:38 +00:00
STablesReq req = {0};
tNameGetFullDbName(pName, req.dbFName);
buildInsertTableReq(pName, &req.pTables);
taosArrayPush(*pDbs, &req);
2022-11-04 07:21:38 +00:00
return TSDB_CODE_SUCCESS;
}
2022-11-04 07:21:38 +00:00
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;
}
2022-08-10 09:47:28 +00:00
2022-11-04 07:21:38 +00:00
SUserAuthInfo userAuth = {.type = AUTH_TYPE_WRITE};
snprintf(userAuth.user, sizeof(userAuth.user), "%s", pUser);
tNameGetFullDbName(pName, userAuth.dbFName);
taosArrayPush(*pUserAuth, &userAuth);
2022-11-04 07:21:38 +00:00
return TSDB_CODE_SUCCESS;
}
2022-12-20 08:53:08 +00:00
static int32_t buildInsertCatalogReq(SInsertParseContext* pCxt, SVnodeModifyOpStmt* pStmt, SCatalogReq* pCatalogReq) {
2022-11-04 07:21:38 +00:00
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);
}
2022-11-04 07:21:38 +00:00
}
if (TSDB_CODE_SUCCESS == code) {
code = buildInsertDbReq(&pStmt->targetTableName, &pCatalogReq->pTableHash);
}
return code;
}
2022-11-04 07:21:38 +00:00
static int32_t setNextStageInfo(SInsertParseContext* pCxt, SQuery* pQuery, SCatalogReq* pCatalogReq) {
2022-12-20 08:53:08 +00:00
SVnodeModifyOpStmt* pStmt = (SVnodeModifyOpStmt*)pQuery->pRoot;
2022-11-04 07:21:38 +00:00
if (pCxt->missCache) {
parserDebug("0x%" PRIx64 " %d rows of %d tables have been inserted before cache miss", pCxt->pComCxt->requestId,
pStmt->totalRowsNum, pStmt->totalTbNum);
2022-11-06 06:26:19 +00:00
2022-11-04 07:21:38 +00:00
pQuery->execStage = QUERY_EXEC_STAGE_PARSE;
return buildInsertCatalogReq(pCxt, pStmt, pCatalogReq);
}
parserDebug("0x%" PRIx64 " %d rows of %d tables have been inserted", pCxt->pComCxt->requestId, pStmt->totalRowsNum,
pStmt->totalTbNum);
2022-11-06 06:26:19 +00:00
2022-11-04 07:21:38 +00:00
pQuery->execStage = QUERY_EXEC_STAGE_SCHEDULE;
return TSDB_CODE_SUCCESS;
}
2022-11-04 07:21:38 +00:00
int32_t parseInsertSql(SParseContext* pCxt, SQuery** pQuery, SCatalogReq* pCatalogReq, const SMetaData* pMetaData) {
2022-12-07 11:12:55 +00:00
SInsertParseContext context = {.pComCxt = pCxt,
.msg = {.buf = pCxt->pMsg, .len = pCxt->msgLen},
.missCache = false,
.usingDuplicateTable = false,
.forceUpdate = (NULL != pCatalogReq ? pCatalogReq->forceUpdate : false)};
2022-11-04 07:21:38 +00:00
2022-11-22 10:18:31 +00:00
int32_t code = initInsertQuery(&context, pCatalogReq, pMetaData, pQuery);
if (TSDB_CODE_SUCCESS == code) {
2022-12-20 08:53:08 +00:00
code = parseInsertSqlImpl(&context, (SVnodeModifyOpStmt*)(*pQuery)->pRoot);
}
if (TSDB_CODE_SUCCESS == code) {
2022-11-04 07:21:38 +00:00
code = setNextStageInfo(&context, *pQuery, pCatalogReq);
}
if ((TSDB_CODE_SUCCESS == code || NEED_CLIENT_HANDLE_ERROR(code)) &&
QUERY_EXEC_STAGE_SCHEDULE == (*pQuery)->execStage) {
code = setRefreshMate(*pQuery);
}
2022-12-28 02:49:38 +00:00
insDestroyBoundColInfo(&context.tags);
return code;
}