From bb87a5b0b71382451e5106213eb9e71112a9c98c Mon Sep 17 00:00:00 2001 From: wangjiaming0909 Date: Tue, 11 Mar 2025 13:18:49 +0800 Subject: [PATCH] fix(decimal): fix taos_fetch_fields_e api --- include/common/ttypes.h | 3 +++ source/client/src/clientImpl.c | 28 ++++++++++-------------- source/client/src/clientStmt.c | 2 +- source/common/src/ttypes.c | 18 +++++++++++++++ source/libs/decimal/test/decimalTest.cpp | 1 + source/libs/parser/src/parInsertStmt.c | 2 +- 6 files changed, 35 insertions(+), 19 deletions(-) diff --git a/include/common/ttypes.h b/include/common/ttypes.h index 9bf1c973b7..0facd5739a 100644 --- a/include/common/ttypes.h +++ b/include/common/ttypes.h @@ -423,6 +423,9 @@ uint8_t getScaleFromTypeMod(int32_t type, STypeMod mod); void fillBytesForDecimalType(int32_t *pBytes, int32_t type, uint8_t precision, uint8_t scale); void extractDecimalTypeInfoFromBytes(int32_t *pBytes, uint8_t *precision, uint8_t *scale); +int32_t calcTypeBytesFromSchemaBytes(int32_t type, int32_t schemaBytes); +int32_t calcSchemaBytesFromTypeBytes(int32_t type, int32_t varTypeBytes); + #ifdef __cplusplus } #endif diff --git a/source/client/src/clientImpl.c b/source/client/src/clientImpl.c index 6d6fcccac0..9682c81139 100644 --- a/source/client/src/clientImpl.c +++ b/source/client/src/clientImpl.c @@ -543,18 +543,12 @@ int32_t setResSchemaInfo(SReqResultInfo* pResInfo, const SSchema* pSchema, int32 } for (int32_t i = 0; i < pResInfo->numOfCols; ++i) { - pResInfo->fields[i].bytes = pSchema[i].bytes; pResInfo->fields[i].type = pSchema[i].type; - pResInfo->userFields[i].bytes = pSchema[i].bytes; pResInfo->userFields[i].type = pSchema[i].type; - - if (pSchema[i].type == TSDB_DATA_TYPE_VARCHAR || pSchema[i].type == TSDB_DATA_TYPE_VARBINARY || - pSchema[i].type == TSDB_DATA_TYPE_GEOMETRY) { - pResInfo->fields[i].bytes = pResInfo->userFields[i].bytes -= VARSTR_HEADER_SIZE; - } else if (pSchema[i].type == TSDB_DATA_TYPE_NCHAR || pSchema[i].type == TSDB_DATA_TYPE_JSON) { - pResInfo->fields[i].bytes = pResInfo->userFields[i].bytes = (pResInfo->userFields[i].bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE; - } else if (IS_DECIMAL_TYPE(pSchema[i].type) && pExtSchema) { + pResInfo->userFields[i].bytes = calcTypeBytesFromSchemaBytes(pSchema[i].type, pSchema[i].bytes); + pResInfo->fields[i].bytes = pResInfo->userFields[i].bytes; + if (IS_DECIMAL_TYPE(pSchema[i].type) && pExtSchema) { decimalFromTypeMod(pExtSchema[i].typeMod, &pResInfo->fields[i].precision, &pResInfo->fields[i].scale); } @@ -1951,10 +1945,10 @@ void doSetOneRowPtr(SReqResultInfo* pResultInfo) { SResultColumn* pCol = &pResultInfo->pCol[i]; int32_t type = pResultInfo->fields[i].type; - int32_t bytes = pResultInfo->fields[i].bytes; + int32_t schemaBytes = calcSchemaBytesFromTypeBytes(type, pResultInfo->fields[i].bytes); if (IS_VAR_DATA_TYPE(type)) { - if (!IS_VAR_NULL_TYPE(type, bytes) && pCol->offset[pResultInfo->current] != -1) { + if (!IS_VAR_NULL_TYPE(type, schemaBytes) && pCol->offset[pResultInfo->current] != -1) { char* pStart = pResultInfo->pCol[i].offset[pResultInfo->current] + pResultInfo->pCol[i].pData; pResultInfo->length[i] = varDataLen(pStart); @@ -1965,8 +1959,8 @@ void doSetOneRowPtr(SReqResultInfo* pResultInfo) { } } else { if (!colDataIsNull_f(pCol->nullbitmap, pResultInfo->current)) { - pResultInfo->row[i] = pResultInfo->pCol[i].pData + bytes * pResultInfo->current; - pResultInfo->length[i] = bytes; + pResultInfo->row[i] = pResultInfo->pCol[i].pData + schemaBytes * pResultInfo->current; + pResultInfo->length[i] = schemaBytes; } else { pResultInfo->row[i] = NULL; pResultInfo->length[i] = 0; @@ -2098,7 +2092,7 @@ static int32_t doConvertUCS4(SReqResultInfo* pResultInfo, int32_t* colLength) { for (int32_t i = 0; i < pResultInfo->numOfCols; ++i) { int32_t type = pResultInfo->fields[i].type; - int32_t bytes = pResultInfo->fields[i].bytes; + int32_t schemaBytes = calcSchemaBytesFromTypeBytes(pResultInfo->fields[i].type, pResultInfo->fields[i].bytes); if (type == TSDB_DATA_TYPE_NCHAR && colLength[i] > 0) { char* p = taosMemoryRealloc(pResultInfo->convertBuf[i], colLength[i]); @@ -2115,11 +2109,11 @@ static int32_t doConvertUCS4(SReqResultInfo* pResultInfo, int32_t* colLength) { char* pStart = pCol->offset[j] + pCol->pData; int32_t len = taosUcs4ToMbsEx((TdUcs4*)varDataVal(pStart), varDataLen(pStart), varDataVal(p), conv); - if (len < 0 || len > bytes || (p + len) >= (pResultInfo->convertBuf[i] + colLength[i])) { + if (len < 0 || len > schemaBytes || (p + len) >= (pResultInfo->convertBuf[i] + colLength[i])) { tscError( "doConvertUCS4 error, invalid data. len:%d, bytes:%d, (p + len):%p, (pResultInfo->convertBuf[i] + " "colLength[i]):%p", - len, bytes, (p + len), (pResultInfo->convertBuf[i] + colLength[i])); + len, schemaBytes, (p + len), (pResultInfo->convertBuf[i] + colLength[i])); taosReleaseConv(idx, conv, C2M, pResultInfo->charsetCxt); return TSDB_CODE_TSC_INTERNAL_ERROR; } @@ -2492,7 +2486,7 @@ int32_t setResultDataPtr(SReqResultInfo* pResultInfo, bool convertUcs4) { } pResultInfo->pCol[i].pData = pStart; - pResultInfo->length[i] = pResultInfo->fields[i].bytes; + pResultInfo->length[i] = calcSchemaBytesFromTypeBytes(pResultInfo->fields[i].type, pResultInfo->fields[i].bytes); pResultInfo->row[i] = pResultInfo->pCol[i].pData; pStart += colLength[i]; diff --git a/source/client/src/clientStmt.c b/source/client/src/clientStmt.c index 5b277e6877..85a678e15b 100644 --- a/source/client/src/clientStmt.c +++ b/source/client/src/clientStmt.c @@ -1862,7 +1862,7 @@ int stmtGetParam(TAOS_STMT* stmt, int idx, int* type, int* bytes) { } *type = pField[idx].type; - *bytes = pField[idx].bytes; + *bytes = calcSchemaBytesFromTypeBytes(pField[idx].type, pField[idx].bytes); _return: diff --git a/source/common/src/ttypes.c b/source/common/src/ttypes.c index a2f3c5f187..7838b46e2e 100644 --- a/source/common/src/ttypes.c +++ b/source/common/src/ttypes.c @@ -292,3 +292,21 @@ void extractDecimalTypeInfoFromBytes(int32_t *pBytes, uint8_t *precision, uint8_ *scale = (uint8_t)(*pBytes & 0xFF); *pBytes >>= 24; } + +int32_t calcTypeBytesFromSchemaBytes(int32_t type, int32_t schemaBytes) { + if (type == TSDB_DATA_TYPE_VARCHAR || type == TSDB_DATA_TYPE_VARBINARY || type == TSDB_DATA_TYPE_GEOMETRY) { + return schemaBytes - VARSTR_HEADER_SIZE; + } else if (type == TSDB_DATA_TYPE_NCHAR || type == TSDB_DATA_TYPE_JSON) { + return (schemaBytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE; + } + return schemaBytes; +} + +int32_t calcSchemaBytesFromTypeBytes(int32_t type, int32_t varTypeBytes) { + if (type == TSDB_DATA_TYPE_VARCHAR || type == TSDB_DATA_TYPE_VARBINARY || type == TSDB_DATA_TYPE_GEOMETRY) { + return varTypeBytes + VARSTR_HEADER_SIZE; + } else if (type == TSDB_DATA_TYPE_NCHAR || type == TSDB_DATA_TYPE_JSON) { + return varTypeBytes * TSDB_NCHAR_SIZE + VARSTR_HEADER_SIZE; + } + return varTypeBytes; +} \ No newline at end of file diff --git a/source/libs/decimal/test/decimalTest.cpp b/source/libs/decimal/test/decimalTest.cpp index 5eb88154be..cbb0d9e074 100644 --- a/source/libs/decimal/test/decimalTest.cpp +++ b/source/libs/decimal/test/decimalTest.cpp @@ -1425,6 +1425,7 @@ TEST_F(DecimalTest, api_taos_fetch_rows) { ASSERT_EQ(fields_e[1].scale, 10); ASSERT_EQ(fields_e[2].type, TSDB_DATA_TYPE_VARCHAR); ASSERT_EQ(fields_e[2].bytes, 255); + taos_free_result(res); res = taos_query(pTaos, sql); diff --git a/source/libs/parser/src/parInsertStmt.c b/source/libs/parser/src/parInsertStmt.c index c61b5e611c..a0a92e4fe2 100644 --- a/source/libs/parser/src/parInsertStmt.c +++ b/source/libs/parser/src/parInsertStmt.c @@ -1052,7 +1052,7 @@ int32_t buildBoundFields(int32_t numOfBound, int16_t* boundColumns, SSchema* pSc schema = &pSchema[boundColumns[i]]; tstrncpy((*fields)[i].name, schema->name, 65); (*fields)[i].type = schema->type; - (*fields)[i].bytes = schema->bytes; + (*fields)[i].bytes = calcTypeBytesFromSchemaBytes(schema->type, schema->bytes); } }