diff --git a/docs/examples/node/websocketexample/all_type_stmt.js b/docs/examples/node/websocketexample/all_type_stmt.js old mode 100644 new mode 100755 index f095bee090..2297923e75 --- a/docs/examples/node/websocketexample/all_type_stmt.js +++ b/docs/examples/node/websocketexample/all_type_stmt.js @@ -95,8 +95,8 @@ async function all_type_example() { tagParams.setBoolean([true]); tagParams.setVarchar(["hello"]); tagParams.setNchar(["stmt"]); - tagParams.setGeometry([geometryData]); tagParams.setVarBinary([vbData]); + tagParams.setGeometry([geometryData]); await stmt.setTags(tagParams); @@ -108,8 +108,8 @@ async function all_type_example() { bindParams.setBoolean([true]); bindParams.setVarchar(["hello"]); bindParams.setNchar(["stmt"]); - bindParams.setGeometry([geometryData]); bindParams.setVarBinary([vbData]); + bindParams.setGeometry([geometryData]); await stmt.bind(bindParams); await stmt.batch(); diff --git a/include/common/tdataformat.h b/include/common/tdataformat.h index c0b899b6aa..f71459d6aa 100644 --- a/include/common/tdataformat.h +++ b/include/common/tdataformat.h @@ -155,7 +155,6 @@ int32_t tDecodeTag(SDecoder *pDecoder, STag **ppTag); int32_t tTagToValArray(const STag *pTag, SArray **ppArray); void debugPrintSTag(STag *pTag, const char *tag, int32_t ln); // TODO: remove int32_t parseJsontoTagData(const char *json, SArray *pTagVals, STag **ppTag, void *pMsgBuf); -int32_t parseGeotoTagData(const char *g, SArray *pTagVals, SSchema *pTagSchema); // SColData ================================ typedef struct { @@ -174,7 +173,7 @@ typedef struct { } SColDataCompressInfo; typedef void *(*xMallocFn)(void *, int32_t); -typedef int32_t (*formatGeometryFn)(char *geoStr, int32_t lenght, int32_t buffMaxLen, char **out, int32_t *size); +typedef int32_t (*formatGeometryFn)(char *geoWKB, size_t nGeom); void tColDataDestroy(void *ph); void tColDataInit(SColData *pColData, int16_t cid, int8_t type, int8_t cflag); diff --git a/source/common/src/tdataformat.c b/source/common/src/tdataformat.c index 81651517cd..23986b265d 100644 --- a/source/common/src/tdataformat.c +++ b/source/common/src/tdataformat.c @@ -3058,20 +3058,11 @@ int32_t tColDataAddValueByBind(SColData *pColData, TAOS_MULTI_BIND *pBind, int32 return TSDB_CODE_PAR_VALUE_TOO_LONG; } else { if (pColData->type == TSDB_DATA_TYPE_GEOMETRY) { - int32_t size = 0; - char *out = NULL; - code = fg((char *)pBind->buffer + pBind->buffer_length * i, pBind->length[i], buffMaxLen, &out, &size); - if (code) { - taosMemoryFree(out); - goto _exit; - } - - code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, out, size); - taosMemoryFree(out); - } else { - code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE]( - pColData, (uint8_t *)pBind->buffer + pBind->buffer_length * i, pBind->length[i]); + code = fg((char *)pBind->buffer + pBind->buffer_length * i, (size_t)pBind->length[i]); + if (code) goto _exit; } + code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE]( + pColData, (uint8_t *)pBind->buffer + pBind->buffer_length * i, pBind->length[i]); } } } else { // fixed-length data type @@ -3148,19 +3139,10 @@ int32_t tColDataAddValueByBind2(SColData *pColData, TAOS_STMT2_BIND *pBind, int3 return TSDB_CODE_PAR_VALUE_TOO_LONG; } else { if (pColData->type == TSDB_DATA_TYPE_GEOMETRY) { - int32_t size = 0; - char *out = NULL; - code = fg((char *)buf, pBind->length[i], buffMaxLen, &out, &size); - if (code) { - taosMemoryFree(out); - goto _exit; - } - - code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, out, size); - taosMemoryFree(out); - } else { - code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, buf, pBind->length[i]); + code = fg(buf, pBind->length[i]); + if (code) goto _exit; } + code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, buf, pBind->length[i]); buf += pBind->length[i]; } } diff --git a/source/libs/parser/src/parInsertStmt.c b/source/libs/parser/src/parInsertStmt.c index 8715d1d3ec..b515f9df9d 100644 --- a/source/libs/parser/src/parInsertStmt.c +++ b/source/libs/parser/src/parInsertStmt.c @@ -102,6 +102,21 @@ pStmt = (SVnodeModifyOpStmt*)pQuery->pRoot; } */ +int32_t formatGeometry(char* geoWKB, size_t nGeom) { + int32_t code = TSDB_CODE_SUCCESS; + char* outputWKT = NULL; + + if (TSDB_CODE_SUCCESS != (code = initCtxAsText()) || + TSDB_CODE_SUCCESS != (code = doAsText(geoWKB, nGeom, &outputWKT))) { + code = TSDB_CODE_INVALID_PARA; + goto _exit; + } + +_exit: + geosFreeBuffer(outputWKT); + return code; +} + int32_t qBuildStmtOutput(SQuery* pQuery, SHashObj* pVgHash, SHashObj* pBlockHash) { int32_t code = TSDB_CODE_SUCCESS; SArray* pVgDataBlocks = NULL; @@ -188,12 +203,15 @@ int32_t qBindStmtTagsValue(void* pBlock, void* boundTags, int64_t suid, const ch if (code != TSDB_CODE_SUCCESS) { goto end; } - } else if (pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) { - code = parseGeotoTagData(bind[c].buffer, pTagArray, pTagSchema); } else { STagVal val = {.cid = pTagSchema->colId, .type = pTagSchema->type}; // strcpy(val.colName, pTagSchema->name); - if (pTagSchema->type == TSDB_DATA_TYPE_BINARY || pTagSchema->type == TSDB_DATA_TYPE_VARBINARY) { + if (pTagSchema->type == TSDB_DATA_TYPE_BINARY || pTagSchema->type == TSDB_DATA_TYPE_VARBINARY || + pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) { + if (pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) { + code = formatGeometry(bind[c].buffer, colLen); + if (code != TSDB_CODE_SUCCESS) goto end; + } val.pData = (uint8_t*)bind[c].buffer; val.nData = colLen; } else if (pTagSchema->type == TSDB_DATA_TYPE_NCHAR) { @@ -253,9 +271,6 @@ end: if (p->type == TSDB_DATA_TYPE_NCHAR) { taosMemoryFreeClear(p->pData); } - if (p->type == TSDB_DATA_TYPE_GEOMETRY) { - geosFreeBuffer(p->pData); - } } taosArrayDestroy(pTagArray); taosArrayDestroy(tagName); @@ -309,39 +324,6 @@ int32_t convertStmtNcharCol(SMsgBuf* pMsgBuf, SSchema* pSchema, TAOS_MULTI_BIND* return TSDB_CODE_SUCCESS; } -int32_t formatGeometry(char* geoStr, int32_t lenght, int32_t buffMaxLen, char** out, int32_t* size) { - int32_t code = 0; - unsigned char* output = NULL; - char* tmp = NULL; - code = initCtxGeomFromText(); - if (code != TSDB_CODE_SUCCESS) { - uError("init geom from text failed, code:%d", code); - return code; - } - - code = doGeomFromText(geoStr, &output, (size_t*)size); - - if (code != TSDB_CODE_SUCCESS) { - goto _exit; - } - if (*size > buffMaxLen) { - code = TSDB_CODE_PAR_VALUE_TOO_LONG; - goto _exit; - } - - tmp = taosMemoryCalloc(1, *size); - if (NULL == tmp) { - code = terrno; - goto _exit; - } - memcpy(tmp, output, *size); - *out = tmp; - -_exit: - geosFreeBuffer(output); - return code; -} - int32_t qBindStmtStbColsValue(void* pBlock, SArray* pCols, TAOS_MULTI_BIND* bind, char* msgBuf, int32_t msgBufLen, STSchema** pTSchema, SBindInfo* pBindInfos) { STableDataCxt* pDataBlock = (STableDataCxt*)pBlock; @@ -577,12 +559,15 @@ int32_t qBindStmtTagsValue2(void* pBlock, void* boundTags, int64_t suid, const c if (code != TSDB_CODE_SUCCESS) { goto end; } - } else if (pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) { - code = parseGeotoTagData(bind[c].buffer, pTagArray, pTagSchema); } else { STagVal val = {.cid = pTagSchema->colId, .type = pTagSchema->type}; // strcpy(val.colName, pTagSchema->name); - if (pTagSchema->type == TSDB_DATA_TYPE_BINARY || pTagSchema->type == TSDB_DATA_TYPE_VARBINARY) { + if (pTagSchema->type == TSDB_DATA_TYPE_BINARY || pTagSchema->type == TSDB_DATA_TYPE_VARBINARY || + pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) { + if (pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) { + code = formatGeometry(bind[c].buffer, colLen); + if (code != TSDB_CODE_SUCCESS) goto end; + } val.pData = (uint8_t*)bind[c].buffer; val.nData = colLen; } else if (pTagSchema->type == TSDB_DATA_TYPE_NCHAR) { @@ -642,9 +627,6 @@ end: if (p->type == TSDB_DATA_TYPE_NCHAR) { taosMemoryFreeClear(p->pData); } - if (p->type == TSDB_DATA_TYPE_GEOMETRY) { - geosFreeBuffer(p->pData); - } } taosArrayDestroy(pTagArray); taosArrayDestroy(tagName); diff --git a/source/libs/parser/src/parUtil.c b/source/libs/parser/src/parUtil.c index 33b36c98fb..e35eea9e72 100644 --- a/source/libs/parser/src/parUtil.c +++ b/source/libs/parser/src/parUtil.c @@ -15,7 +15,6 @@ #include "parUtil.h" #include "cJSON.h" -#include "geosWrapper.h" #include "querynodes.h" #include "tarray.h" #include "tlog.h" @@ -519,37 +518,6 @@ end: return retCode; } -int32_t parseGeotoTagData(const char* g, SArray* pTagVals, SSchema* pTagSchema) { - int32_t code = 0; - size_t size = 0; - unsigned char* output = NULL; - STagVal val = {.cid = pTagSchema->colId, .type = pTagSchema->type}; - code = initCtxGeomFromText(); - if (code != TSDB_CODE_SUCCESS) { - return code; - } - code = doGeomFromText(g, &output, &size); - if (code != TSDB_CODE_SUCCESS) { - goto end; - } - if (size > pTagSchema->bytes) { - code = TSDB_CODE_PAR_VALUE_TOO_LONG; - goto end; - } - - val.pData = (uint8_t*)output; - val.nData = (uint32_t)size; - - if (NULL == taosArrayPush(pTagVals, &val)) { - code = terrno; - geosFreeBuffer(output); - goto end; - } - -end: - return code; -} - static int32_t getInsTagsTableTargetNameFromOp(int32_t acctId, SOperatorNode* pOper, SName* pName) { if (OP_TYPE_EQUAL != pOper->opType) { return TSDB_CODE_SUCCESS; diff --git a/tests/script/api/stmt2-geometry-test.c b/tests/script/api/stmt2-geometry-test.c index e1af5879eb..3bda05f5ee 100644 --- a/tests/script/api/stmt2-geometry-test.c +++ b/tests/script/api/stmt2-geometry-test.c @@ -3,6 +3,11 @@ #include #include "taos.h" +int8_t byteArray[21] = {0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x59, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x59, 0x40}; +int8_t worngArray[21] = {0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x59, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x59, 0x40}; + void do_query(TAOS* taos, const char* sql) { printf("[sql]%s\n", sql); TAOS_RES* result = taos_query(taos, sql); @@ -15,8 +20,8 @@ void do_query(TAOS* taos, const char* sql) { taos_free_result(result); } -void execute_test(TAOS* taos, const char* tbname1, const char* tbname2, const char* tag2, const char* col2, - const char* case_desc) { +void execute_test(TAOS* taos, const char* tbname1, const char* tbname2, int8_t* tag2, int8_t* col2, + const char* case_desc, int size) { // prepare stmt TAOS_STMT2_OPTION option = {0, true, false, NULL, NULL}; TAOS_STMT2* stmt = taos_stmt2_init(taos, &option); @@ -36,18 +41,17 @@ void execute_test(TAOS* taos, const char* tbname1, const char* tbname2, const ch } // prepare data - int t1_val = 0; - int64_t ts = 1591060628000; - const char* tbname[2] = {tbname1, tbname2}; - int32_t length[5] = {sizeof(int), 2, sizeof(int64_t), (int32_t)strlen(tag2), (int32_t)strlen(col2)}; + int t1_val = 0; + int64_t ts = 1591060628000; + const char* tbname[2] = {tbname1, tbname2}; + int32_t length[5] = {sizeof(int), 2, sizeof(int64_t), size, 20, sizeof(col2)}; + TAOS_STMT2_BIND tags[2][2] = { - {{TSDB_DATA_TYPE_INT, &t1_val, &length[0], NULL, 2}, {TSDB_DATA_TYPE_GEOMETRY, (void*)tag2, &length[3], NULL, 2}}, - {{TSDB_DATA_TYPE_INT, &t1_val, &length[0], NULL, 2}, - {TSDB_DATA_TYPE_GEOMETRY, (void*)tag2, &length[3], NULL, 2}}}; - TAOS_STMT2_BIND params[2][2] = {{{TSDB_DATA_TYPE_TIMESTAMP, &ts, &length[2], NULL, 1}, - {TSDB_DATA_TYPE_GEOMETRY, (void*)col2, &length[4], NULL, 1}}, - {{TSDB_DATA_TYPE_TIMESTAMP, &ts, &length[2], NULL, 1}, - {TSDB_DATA_TYPE_GEOMETRY, (void*)col2, &length[4], NULL, 1}}}; + {{TSDB_DATA_TYPE_INT, &t1_val, &length[0], NULL, 2}, {TSDB_DATA_TYPE_GEOMETRY, tag2, &length[3], NULL, 2}}, + {{TSDB_DATA_TYPE_INT, &t1_val, &length[0], NULL, 2}, {TSDB_DATA_TYPE_GEOMETRY, tag2, &length[3], NULL, 2}}}; + TAOS_STMT2_BIND params[2][2] = { + {{TSDB_DATA_TYPE_TIMESTAMP, &ts, &length[2], NULL, 1}, {TSDB_DATA_TYPE_GEOMETRY, col2, &length[3], NULL, 1}}, + {{TSDB_DATA_TYPE_TIMESTAMP, &ts, &length[2], NULL, 1}, {TSDB_DATA_TYPE_GEOMETRY, col2, &length[3], NULL, 1}}}; TAOS_STMT2_BIND* tagv[2] = {&tags[0][0], &tags[1][0]}; TAOS_STMT2_BIND* paramv[2] = {¶ms[0][0], ¶ms[1][0]}; @@ -69,21 +73,18 @@ void execute_test(TAOS* taos, const char* tbname1, const char* tbname2, const ch taos_stmt2_close(stmt); } -void test1(TAOS* taos) { - execute_test(taos, "tb11", "tb12", "POINT(1.0 1.0)", "LINESTRING(1.0 1.0, 2.0 2.0, 3.0 3.0)", "[normal]case 1"); -} +void test1(TAOS* taos) { execute_test(taos, "tb11", "tb12", &byteArray[0], &byteArray[0], "[normal]case 1", 21); } void test2(TAOS* taos) { - execute_test(taos, "tb21", "tb22", "hello", "LINESTRING(1.0 1.0, 2.0 2.0, 3.0 3.0)", "[wrong tag]case 2"); + execute_test(taos, "tb21", "tb22", &worngArray[0], &byteArray[0], "[wrong WKB tag]case 2", 21); } void test3(TAOS* taos) { - execute_test(taos, "tb31", "tb32", "POLYGON((0 0, 4 0, 4 4, 0 4, 0 0))", "1", "[wrong col]case 3"); + execute_test(taos, "tb31", "tb32", "POLYGON((0 0, 4 0, 4 4, 0 4, 0 0))", "POLYGON((0 0, 4 0, 4 4, 0 4, 0 0))", + "[wrong WKT col]case 3", sizeof("POLYGON((0 0, 4 0, 4 4, 0 4, 0 0))")); } -void test4(TAOS* taos) { - execute_test(taos, "tb41", "tb42", "POLYGON((0 0, 4 0, 4 4, 0 4, 0 0))", "POINT(1.0 1.0)", "[wrong size]case 4"); -} +void test4(TAOS* taos) { execute_test(taos, "tb41", "tb42", &byteArray[0], &byteArray[0], "[wrong size]case 4", 21); } int main() { TAOS* taos = taos_connect("localhost", "root", "taosdata", "", 0);