From e46896bfcd7d4596d7000685a247875a8895b26d Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Sat, 23 May 2020 16:12:18 +0800 Subject: [PATCH] [td-225] support hex/bin numeric data to insert --- src/client/src/tscAsync.c | 2 +- src/client/src/tscFunctionImpl.c | 23 ++++++++------- src/client/src/tscParseInsert.c | 50 ++++++++++++++++++-------------- 3 files changed, 42 insertions(+), 33 deletions(-) diff --git a/src/client/src/tscAsync.c b/src/client/src/tscAsync.c index 26de2a51a8..6fba0efd97 100644 --- a/src/client/src/tscAsync.c +++ b/src/client/src/tscAsync.c @@ -48,7 +48,7 @@ void doAsyncQuery(STscObj* pObj, SSqlObj* pSql, void (*fp)(), void* param, const pSql->param = param; pSql->pTscObj = pObj; pSql->maxRetry = TSDB_MAX_REPLICA_NUM; - pSql->fp = fp; + pSql->fp = fp; if (TSDB_CODE_SUCCESS != tscAllocPayload(pCmd, TSDB_DEFAULT_PAYLOAD_SIZE)) { tscError("failed to malloc payload"); diff --git a/src/client/src/tscFunctionImpl.c b/src/client/src/tscFunctionImpl.c index f3e24e43a9..168863d322 100644 --- a/src/client/src/tscFunctionImpl.c +++ b/src/client/src/tscFunctionImpl.c @@ -947,18 +947,21 @@ static void minMax_function(SQLFunctionCtx *pCtx, char *pOutput, int32_t isMin, index = pCtx->preAggVals.statis.maxIndex; } - /** - * NOTE: work around the bug caused by invalid pre-calculated function. - * Here the selectivity + ts will not return correct value. - * - * The following codes of 3 lines will be removed later. - */ - if (index < 0 || index >= pCtx->size + pCtx->startOffset) { - index = 0; + TSKEY key = TSKEY_INITIAL_VAL; + if (pCtx->ptsList != NULL) { + /** + * NOTE: work around the bug caused by invalid pre-calculated function. + * Here the selectivity + ts will not return correct value. + * + * The following codes of 3 lines will be removed later. + */ + if (index < 0 || index >= pCtx->size + pCtx->startOffset) { + index = 0; + } + + key = pCtx->ptsList[index]; } - TSKEY key = pCtx->ptsList[index]; - if (pCtx->inputType >= TSDB_DATA_TYPE_TINYINT && pCtx->inputType <= TSDB_DATA_TYPE_BIGINT) { int64_t val = GET_INT64_VAL(tval); if (pCtx->inputType == TSDB_DATA_TYPE_TINYINT) { diff --git a/src/client/src/tscParseInsert.c b/src/client/src/tscParseInsert.c index 62f27c0960..3e1f0787c3 100644 --- a/src/client/src/tscParseInsert.c +++ b/src/client/src/tscParseInsert.c @@ -42,35 +42,42 @@ enum { static int32_t tscAllocateMemIfNeed(STableDataBlocks *pDataBlock, int32_t rowSize, int32_t * numOfRows); static int32_t tscToInteger(SSQLToken *pToken, int64_t *value, char **endPtr) { -// int32_t numType = isValidNumber(pToken); -// if (TK_ILLEGAL == numType) { -// return numType; -// } - + if (pToken->n == 0) { + return TK_ILLEGAL; + } + int32_t radix = 10; - if (pToken->type == TK_HEX) { - radix = 16; - } else if (pToken->type == TK_OCT) { - radix = 8; - } else if (pToken->type == TK_BIN) { - radix = 2; + + int32_t radixList[3] = {16, 8, 2}; + if (pToken->type == TK_HEX || pToken->type == TK_OCT || pToken->type == TK_BIN) { + radix = radixList[pToken->type - TK_HEX]; } errno = 0; *value = strtoll(pToken->z, endPtr, radix); + + // not a valid integer number, return error + if ((pToken->type == TK_STRING || pToken->type == TK_ID) && ((*endPtr - pToken->z) != pToken->n)) { + return TK_ILLEGAL; + } return pToken->type; } static int32_t tscToDouble(SSQLToken *pToken, double *value, char **endPtr) { -// int32_t numType = isValidNumber(pToken); -// if (TK_ILLEGAL == numType) { -// return numType; -// } - + if (pToken->n == 0) { + return TK_ILLEGAL; + } + errno = 0; *value = strtod(pToken->z, endPtr); - return pToken->type; + + // not a valid integer number, return error + if ((pToken->type == TK_STRING || pToken->type == TK_ID) && ((*endPtr - pToken->z) != pToken->n)) { + return TK_ILLEGAL; + } else { + return pToken->type; + } } int tsParseTime(SSQLToken *pToken, int64_t *time, char **next, char *error, int16_t timePrec) { @@ -422,9 +429,9 @@ int tsParseOneRowData(char **str, STableDataBlocks *pDataBlocks, SSchema schema[ return -1; } - if (((sToken.type != TK_NOW) && (sToken.type != TK_INTEGER) && (sToken.type != TK_STRING) && - (sToken.type != TK_FLOAT) && (sToken.type != TK_BOOL) && (sToken.type != TK_NULL)) || - (sToken.n == 0) || (sToken.type == TK_RP)) { + int16_t type = sToken.type; + if ((type != TK_NOW && type != TK_INTEGER && type != TK_STRING && type != TK_FLOAT && type != TK_BOOL && + type != TK_NULL && type != TK_HEX && type != TK_OCT && type != TK_BIN) || (sToken.n == 0) || (type == TK_RP)) { tscInvalidSQLErrMsg(error, "invalid data or symbol", sToken.z); *code = TSDB_CODE_INVALID_SQL; return -1; @@ -1306,8 +1313,7 @@ int tsParseInsertSql(SSqlObj *pSql) { SQueryInfo *pQueryInfo = NULL; tscGetQueryInfoDetailSafely(pCmd, pCmd->clauseIndex, &pQueryInfo); - uint16_t type = (sToken.type == TK_INSERT)? TSDB_QUERY_TYPE_INSERT:TSDB_QUERY_TYPE_IMPORT; - TSDB_QUERY_SET_TYPE(pQueryInfo->type, type); + TSDB_QUERY_SET_TYPE(pQueryInfo->type, TSDB_QUERY_TYPE_INSERT); sToken = tStrGetToken(pSql->sqlstr, &index, false, 0, NULL); if (sToken.type != TK_INTO) { -- GitLab