diff --git a/src/client/src/tscPrepare.c b/src/client/src/tscPrepare.c index f199a5798779523e90e95bfce468a68b8afb6c6d..00d8b60e2f976603c3cc823c478b4698523be0fe 100644 --- a/src/client/src/tscPrepare.c +++ b/src/client/src/tscPrepare.c @@ -163,8 +163,8 @@ static int normalStmtBindParam(STscStmt* stmt, TAOS_BIND* bind) { break; default: - tscDebug("0x%"PRIx64" bind column%d: type mismatch or invalid", stmt->pSql->self, i); - return TSDB_CODE_TSC_INVALID_VALUE; + tscError("0x%"PRIx64" bind column%d: type mismatch or invalid", stmt->pSql->self, i); + return invalidOperationMsg(tscGetErrorMsgPayload(&stmt->pSql->cmd), "bind type mismatch or invalid"); } } @@ -727,6 +727,7 @@ static int doBindParam(STableDataBlocks* pBlock, char* data, SParamInfo* param, #endif if (bind->buffer_type != param->type) { + tscError("column type mismatch"); return TSDB_CODE_TSC_INVALID_VALUE; } @@ -754,6 +755,7 @@ static int doBindParam(STableDataBlocks* pBlock, char* data, SParamInfo* param, case TSDB_DATA_TYPE_BINARY: if ((*bind->length) > (uintptr_t)param->bytes) { + tscError("column length is too big"); return TSDB_CODE_TSC_INVALID_VALUE; } size = (short)*bind->length; @@ -763,6 +765,7 @@ static int doBindParam(STableDataBlocks* pBlock, char* data, SParamInfo* param, case TSDB_DATA_TYPE_NCHAR: { int32_t output = 0; if (!taosMbsToUcs4(bind->buffer, *bind->length, varDataVal(data + param->offset), param->bytes - VARSTR_HEADER_SIZE, &output)) { + tscError("convert nchar failed"); return TSDB_CODE_TSC_INVALID_VALUE; } varDataSetLen(data + param->offset, output); @@ -787,6 +790,7 @@ static int doBindParam(STableDataBlocks* pBlock, char* data, SParamInfo* param, static int doBindBatchParam(STableDataBlocks* pBlock, SParamInfo* param, TAOS_MULTI_BIND* bind, int32_t rowNum) { if (bind->buffer_type != param->type || !isValidDataType(param->type)) { + tscError("column mismatch or invalid"); return TSDB_CODE_TSC_INVALID_VALUE; } @@ -892,8 +896,8 @@ static int insertStmtBindParam(STscStmt* stmt, TAOS_BIND* bind) { int code = doBindParam(pBlock, data, param, &bind[param->idx], 1); if (code != TSDB_CODE_SUCCESS) { - tscDebug("0x%"PRIx64" bind column %d: type mismatch or invalid", pStmt->pSql->self, param->idx); - return code; + tscDebug("0x%"PRIx64" bind column %d: type mismatch or invalid", pStmt->pSql->self, param->idx); + return invalidOperationMsg(tscGetErrorMsgPayload(&stmt->pSql->cmd), "bind column type mismatch or invalid"); } } @@ -957,13 +961,13 @@ static int insertStmtBindParamBatch(STscStmt* stmt, TAOS_MULTI_BIND* bind, int c SParamInfo* param = &pBlock->params[j]; if (bind[param->idx].num != rowNum) { tscError("0x%"PRIx64" param %d: num[%d:%d] not match", pStmt->pSql->self, param->idx, rowNum, bind[param->idx].num); - return TSDB_CODE_TSC_INVALID_VALUE; + return invalidOperationMsg(tscGetErrorMsgPayload(&stmt->pSql->cmd), "bind row num mismatch"); } int code = doBindBatchParam(pBlock, param, &bind[param->idx], pCmd->batchSize); if (code != TSDB_CODE_SUCCESS) { tscError("0x%"PRIx64" bind column %d: type mismatch or invalid", pStmt->pSql->self, param->idx); - return code; + return invalidOperationMsg(tscGetErrorMsgPayload(&stmt->pSql->cmd), "bind column type mismatch or invalid"); } } @@ -974,7 +978,7 @@ static int insertStmtBindParamBatch(STscStmt* stmt, TAOS_MULTI_BIND* bind, int c int code = doBindBatchParam(pBlock, param, bind, pCmd->batchSize); if (code != TSDB_CODE_SUCCESS) { tscError("0x%"PRIx64" bind column %d: type mismatch or invalid", pStmt->pSql->self, param->idx); - return code; + return invalidOperationMsg(tscGetErrorMsgPayload(&stmt->pSql->cmd), "bind column type mismatch or invalid"); } if (colIdx == (pBlock->numOfParams - 1)) { @@ -993,7 +997,7 @@ static int insertStmtUpdateBatch(STscStmt* stmt) { if (pCmd->batchSize > INT16_MAX) { tscError("too many record:%d", pCmd->batchSize); - return TSDB_CODE_TSC_APP_ERROR; + return invalidOperationMsg(tscGetErrorMsgPayload(&stmt->pSql->cmd), "too many records"); } if (taosHashGetSize(pCmd->insertParam.pTableBlockHashList) == 0) { @@ -1057,7 +1061,8 @@ static int insertStmtReset(STscStmt* pStmt) { static int insertStmtExecute(STscStmt* stmt) { SSqlCmd* pCmd = &stmt->pSql->cmd; if (pCmd->batchSize == 0) { - return TSDB_CODE_TSC_INVALID_VALUE; + tscError("no records bind"); + return invalidOperationMsg(tscGetErrorMsgPayload(&stmt->pSql->cmd), "no records bind"); } if (taosHashGetSize(pCmd->insertParam.pTableBlockHashList) == 0) { @@ -1174,7 +1179,7 @@ static int insertBatchStmtExecute(STscStmt* pStmt) { if(pStmt->mtb.nameSet == false) { tscError("0x%"PRIx64" no table name set", pStmt->pSql->self); - return TSDB_CODE_TSC_APP_ERROR; + return invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "no table name set"); } pStmt->pSql->retry = pStmt->pSql->maxRetry + 1; //no retry @@ -1215,7 +1220,8 @@ int stmtParseInsertTbTags(SSqlObj* pSql, STscStmt* pStmt) { int32_t index = 0; SStrToken sToken = tStrGetToken(pCmd->insertParam.sql, &index, false); if (sToken.n == 0) { - return TSDB_CODE_TSC_INVALID_OPERATION; + tscError("table is is expected, sql:%s", pCmd->insertParam.sql); + return tscSQLSyntaxErrMsg(pCmd->payload, "table name is expected", pCmd->insertParam.sql); } if (sToken.n == 1 && sToken.type == TK_QUESTION) { @@ -1237,24 +1243,28 @@ int stmtParseInsertTbTags(SSqlObj* pSql, STscStmt* pStmt) { return TSDB_CODE_SUCCESS; } - if (sToken.n <= 0 || sToken.type != TK_USING) { - return tscSQLSyntaxErrMsg(pCmd->payload, "keywords USING is expected", sToken.z); + if (sToken.n <= 0 || sToken.type != TK_USING) { + tscError("keywords USING is expected, sql:%s", pCmd->insertParam.sql); + return tscSQLSyntaxErrMsg(pCmd->payload, "keywords USING is expected", sToken.z ? sToken.z : pCmd->insertParam.sql); } sToken = tStrGetToken(pCmd->insertParam.sql, &index, false); if (sToken.n <= 0 || ((sToken.type != TK_ID) && (sToken.type != TK_STRING))) { - return tscSQLSyntaxErrMsg(pCmd->payload, "invalid token", sToken.z); + tscError("invalid token, sql:%s", pCmd->insertParam.sql); + return tscSQLSyntaxErrMsg(pCmd->payload, "invalid token", sToken.z ? sToken.z : pCmd->insertParam.sql); } pStmt->mtb.stbname = sToken; sToken = tStrGetToken(pCmd->insertParam.sql, &index, false); if (sToken.n <= 0 || sToken.type != TK_TAGS) { - return tscSQLSyntaxErrMsg(pCmd->payload, "keyword TAGS expected", sToken.z); + tscError("keyword TAGS expected, sql:%s", pCmd->insertParam.sql); + return tscSQLSyntaxErrMsg(pCmd->payload, "keyword TAGS expected", sToken.z ? sToken.z : pCmd->insertParam.sql); } sToken = tStrGetToken(pCmd->insertParam.sql, &index, false); if (sToken.n <= 0 || sToken.type != TK_LP) { - return tscSQLSyntaxErrMsg(pCmd->payload, ") expected", sToken.z); + tscError("( expected, sql:%s", pCmd->insertParam.sql); + return tscSQLSyntaxErrMsg(pCmd->payload, "( expected", sToken.z ? sToken.z : pCmd->insertParam.sql); } pStmt->mtb.tags = taosArrayInit(4, sizeof(SStrToken)); @@ -1264,7 +1274,8 @@ int stmtParseInsertTbTags(SSqlObj* pSql, STscStmt* pStmt) { while (loopCont) { sToken = tStrGetToken(pCmd->insertParam.sql, &index, false); if (sToken.n <= 0) { - return TSDB_CODE_TSC_INVALID_OPERATION; + tscError("unexpected sql end, sql:%s", pCmd->insertParam.sql); + return tscSQLSyntaxErrMsg(pCmd->payload, "unexpected sql end", pCmd->insertParam.sql); } switch (sToken.type) { @@ -1272,7 +1283,8 @@ int stmtParseInsertTbTags(SSqlObj* pSql, STscStmt* pStmt) { loopCont = 0; break; case TK_VALUES: - return TSDB_CODE_TSC_INVALID_OPERATION; + tscError("unexpected token values, sql:%s", pCmd->insertParam.sql); + return tscSQLSyntaxErrMsg(pCmd->payload, "unexpected token", sToken.z); case TK_QUESTION: pStmt->mtb.tagSet = false; //continue default: @@ -1282,12 +1294,14 @@ int stmtParseInsertTbTags(SSqlObj* pSql, STscStmt* pStmt) { } if (taosArrayGetSize(pStmt->mtb.tags) <= 0) { - return TSDB_CODE_TSC_INVALID_OPERATION; + tscError("no tags, sql:%s", pCmd->insertParam.sql); + return tscSQLSyntaxErrMsg(pCmd->payload, "no tags", pCmd->insertParam.sql); } sToken = tStrGetToken(pCmd->insertParam.sql, &index, false); if (sToken.n <= 0 || (sToken.type != TK_VALUES && sToken.type != TK_LP)) { - return TSDB_CODE_TSC_INVALID_OPERATION; + tscError("sql error, sql:%s", pCmd->insertParam.sql); + return tscSQLSyntaxErrMsg(pCmd->payload, "sql error", sToken.z ? sToken.z : pCmd->insertParam.sql); } pStmt->mtb.values = sToken; @@ -1329,8 +1343,8 @@ int stmtGenInsertStatement(SSqlObj* pSql, STscStmt* pStmt, const char* name, TAO } else { if (tags[j].buffer == NULL) { free(str); - tscError("empty"); - return TSDB_CODE_TSC_APP_ERROR; + tscError("empty tag value in params"); + return invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "empty tag value in params"); } ret = converToStr(str + len, tags[j].buffer_type, tags[j].buffer, tags[j].length ? (int32_t)*tags[j].length : -1, &l); @@ -1410,6 +1424,11 @@ TAOS_STMT* taos_stmt_init(TAOS* taos) { return NULL; } + if (TSDB_CODE_SUCCESS != tscAllocPayload(&pSql->cmd, TSDB_DEFAULT_PAYLOAD_SIZE)) { + tscError("%p failed to malloc payload buffer", pSql); + return TSDB_CODE_TSC_OUT_OF_MEMORY; + } + tsem_init(&pSql->rspSem, 0, 0); pSql->signature = pSql; pSql->pTscObj = pObj; @@ -1431,7 +1450,7 @@ int taos_stmt_prepare(TAOS_STMT* stmt, const char* sql, unsigned long length) { if (pStmt->last != STMT_INIT) { tscError("prepare status error, last:%d", pStmt->last); - return TSDB_CODE_TSC_APP_ERROR; + return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), "prepare status error"); } pStmt->last = STMT_PREPARE; @@ -1447,11 +1466,6 @@ int taos_stmt_prepare(TAOS_STMT* stmt, const char* sql, unsigned long length) { pCmd->insertParam.insertType = TSDB_QUERY_TYPE_STMT_INSERT; - if (TSDB_CODE_SUCCESS != tscAllocPayload(pCmd, TSDB_DEFAULT_PAYLOAD_SIZE)) { - tscError("%p failed to malloc payload buffer", pSql); - return TSDB_CODE_TSC_OUT_OF_MEMORY; - } - pSql->sqlstr = realloc(pSql->sqlstr, sqlLen + 1); if (pSql->sqlstr == NULL) { @@ -1489,6 +1503,7 @@ int taos_stmt_prepare(TAOS_STMT* stmt, const char* sql, unsigned long length) { if (code == TSDB_CODE_TSC_ACTION_IN_PROGRESS) { // wait for the callback function to post the semaphore tsem_wait(&pSql->rspSem); + pSql->res.code = code; return pSql->res.code; } @@ -1510,20 +1525,18 @@ int taos_stmt_set_tbname_tags(TAOS_STMT* stmt, const char* name, TAOS_BIND* tags } if (name == NULL) { - terrno = TSDB_CODE_TSC_APP_ERROR; tscError("0x%"PRIx64" name is NULL", pSql->self); - return TSDB_CODE_TSC_APP_ERROR; + return invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "name is NULL"); } if (pStmt->multiTbInsert == false || !tscIsInsertData(pSql->sqlstr)) { - terrno = TSDB_CODE_TSC_APP_ERROR; - tscError("0x%"PRIx64" not multi table insert", pSql->self); - return TSDB_CODE_TSC_APP_ERROR; + tscError("0x%"PRIx64" not multiple table insert", pSql->self); + return invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "not multiple table insert"); } if (pStmt->last == STMT_INIT || pStmt->last == STMT_BIND || pStmt->last == STMT_BIND_COL) { - tscError("0x%"PRIx64" settbname status error, last:%d", pSql->self, pStmt->last); - return TSDB_CODE_TSC_APP_ERROR; + tscError("0x%"PRIx64" set_tbname_tags status error, last:%d", pSql->self, pStmt->last); + return invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "set_tbname_tags status error"); } pStmt->last = STMT_SETTBNAME; @@ -1552,7 +1565,7 @@ int taos_stmt_set_tbname_tags(TAOS_STMT* stmt, const char* name, TAOS_BIND* tags } else { if (tags == NULL) { tscError("No tags set"); - return TSDB_CODE_TSC_APP_ERROR; + return invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "no tags set"); } int32_t ret = stmtGenInsertStatement(pSql, pStmt, name, tags); diff --git a/tests/script/api/batchprepare.c b/tests/script/api/batchprepare.c index 55eb62d9bb4272fe7ab85a96561ebd9c87343ed0..af0611ce738f4f7481bdcee313d5663ed8fae031 100644 --- a/tests/script/api/batchprepare.c +++ b/tests/script/api/batchprepare.c @@ -1860,6 +1860,224 @@ int stmt_funcb_autoctb_e2(TAOS_STMT *stmt) { + + + + +//1 tables 10 records +int stmt_funcb_autoctb_e3(TAOS_STMT *stmt) { + struct { + int64_t *ts; + int8_t b[10]; + int8_t v1[10]; + int16_t v2[10]; + int32_t v4[10]; + int64_t v8[10]; + float f4[10]; + double f8[10]; + char bin[10][40]; + } v = {0}; + + v.ts = malloc(sizeof(int64_t) * 1 * 10); + + int *lb = malloc(10 * sizeof(int)); + + TAOS_BIND *tags = calloc(1, sizeof(TAOS_BIND) * 9 * 1); + TAOS_MULTI_BIND *params = calloc(1, sizeof(TAOS_MULTI_BIND) * 1*10); + +// int one_null = 1; + int one_not_null = 0; + + char* is_null = malloc(sizeof(char) * 10); + char* no_null = malloc(sizeof(char) * 10); + + for (int i = 0; i < 10; ++i) { + lb[i] = 40; + no_null[i] = 0; + is_null[i] = (i % 10 == 2) ? 1 : 0; + v.b[i] = (int8_t)(i % 2); + v.v1[i] = (int8_t)((i+1) % 2); + v.v2[i] = (int16_t)i; + v.v4[i] = (int32_t)(i+1); + v.v8[i] = (int64_t)(i+2); + v.f4[i] = (float)(i+3); + v.f8[i] = (double)(i+4); + memset(v.bin[i], '0'+i%10, 40); + } + + for (int i = 0; i < 10; i+=10) { + params[i+0].buffer_type = TSDB_DATA_TYPE_TIMESTAMP; + params[i+0].buffer_length = sizeof(int64_t); + params[i+0].buffer = &v.ts[10*i/10]; + params[i+0].length = NULL; + params[i+0].is_null = no_null; + params[i+0].num = 10; + + params[i+1].buffer_type = TSDB_DATA_TYPE_BOOL; + params[i+1].buffer_length = sizeof(int8_t); + params[i+1].buffer = v.b; + params[i+1].length = NULL; + params[i+1].is_null = is_null; + params[i+1].num = 10; + + params[i+2].buffer_type = TSDB_DATA_TYPE_TINYINT; + params[i+2].buffer_length = sizeof(int8_t); + params[i+2].buffer = v.v1; + params[i+2].length = NULL; + params[i+2].is_null = is_null; + params[i+2].num = 10; + + params[i+3].buffer_type = TSDB_DATA_TYPE_SMALLINT; + params[i+3].buffer_length = sizeof(int16_t); + params[i+3].buffer = v.v2; + params[i+3].length = NULL; + params[i+3].is_null = is_null; + params[i+3].num = 10; + + params[i+4].buffer_type = TSDB_DATA_TYPE_INT; + params[i+4].buffer_length = sizeof(int32_t); + params[i+4].buffer = v.v4; + params[i+4].length = NULL; + params[i+4].is_null = is_null; + params[i+4].num = 10; + + params[i+5].buffer_type = TSDB_DATA_TYPE_BIGINT; + params[i+5].buffer_length = sizeof(int64_t); + params[i+5].buffer = v.v8; + params[i+5].length = NULL; + params[i+5].is_null = is_null; + params[i+5].num = 10; + + params[i+6].buffer_type = TSDB_DATA_TYPE_FLOAT; + params[i+6].buffer_length = sizeof(float); + params[i+6].buffer = v.f4; + params[i+6].length = NULL; + params[i+6].is_null = is_null; + params[i+6].num = 10; + + params[i+7].buffer_type = TSDB_DATA_TYPE_DOUBLE; + params[i+7].buffer_length = sizeof(double); + params[i+7].buffer = v.f8; + params[i+7].length = NULL; + params[i+7].is_null = is_null; + params[i+7].num = 10; + + params[i+8].buffer_type = TSDB_DATA_TYPE_BINARY; + params[i+8].buffer_length = 40; + params[i+8].buffer = v.bin; + params[i+8].length = lb; + params[i+8].is_null = is_null; + params[i+8].num = 10; + + params[i+9].buffer_type = TSDB_DATA_TYPE_BINARY; + params[i+9].buffer_length = 40; + params[i+9].buffer = v.bin; + params[i+9].length = lb; + params[i+9].is_null = is_null; + params[i+9].num = 10; + + } + + int64_t tts = 1591060628000; + for (int i = 0; i < 10; ++i) { + v.ts[i] = tts + i; + } + + + for (int i = 0; i < 1; ++i) { + tags[i+0].buffer_type = TSDB_DATA_TYPE_INT; + tags[i+0].buffer = v.v4; + tags[i+0].is_null = &one_not_null; + tags[i+0].length = NULL; + + tags[i+1].buffer_type = TSDB_DATA_TYPE_BOOL; + tags[i+1].buffer = v.b; + tags[i+1].is_null = &one_not_null; + tags[i+1].length = NULL; + + tags[i+2].buffer_type = TSDB_DATA_TYPE_TINYINT; + tags[i+2].buffer = v.v1; + tags[i+2].is_null = &one_not_null; + tags[i+2].length = NULL; + + tags[i+3].buffer_type = TSDB_DATA_TYPE_SMALLINT; + tags[i+3].buffer = v.v2; + tags[i+3].is_null = &one_not_null; + tags[i+3].length = NULL; + + tags[i+4].buffer_type = TSDB_DATA_TYPE_BIGINT; + tags[i+4].buffer = v.v8; + tags[i+4].is_null = &one_not_null; + tags[i+4].length = NULL; + + tags[i+5].buffer_type = TSDB_DATA_TYPE_FLOAT; + tags[i+5].buffer = v.f4; + tags[i+5].is_null = &one_not_null; + tags[i+5].length = NULL; + + tags[i+6].buffer_type = TSDB_DATA_TYPE_DOUBLE; + tags[i+6].buffer = v.f8; + tags[i+6].is_null = &one_not_null; + tags[i+6].length = NULL; + + tags[i+7].buffer_type = TSDB_DATA_TYPE_BINARY; + tags[i+7].buffer = v.bin; + tags[i+7].is_null = &one_not_null; + tags[i+7].length = (uintptr_t *)lb; + + tags[i+8].buffer_type = TSDB_DATA_TYPE_NCHAR; + tags[i+8].buffer = v.bin; + tags[i+8].is_null = &one_not_null; + tags[i+8].length = (uintptr_t *)lb; + } + + + unsigned long long starttime = getCurrentTime(); + + char *sql = "insert into ? using stb1 (id1,id2,id3,id4,id5,id6,id7,id8,id9) tags(?,?,?,?,?,?,?,?,?) values(?,?,?,?,?,?,?,?,?,?)"; + int code = taos_stmt_prepare(stmt, sql, 0); + if (code != 0){ + printf("failed to execute taos_stmt_prepare. code:0x%x\n", code); + return -1; + //exit(1); + } + + int id = 0; + for (int zz = 0; zz < 1; zz++) { + char buf[32]; + sprintf(buf, "m%d", zz); + code = taos_stmt_set_tbname_tags(stmt, buf, NULL); + if (code != 0){ + printf("failed to execute taos_stmt_set_tbname_tags. code:0x%x\n", code); + return -1; + } + + taos_stmt_bind_param_batch(stmt, params + id * 10); + taos_stmt_add_batch(stmt); + } + + if (taos_stmt_execute(stmt) != 0) { + printf("failed to execute insert statement.\n"); + exit(1); + } + + ++id; + + unsigned long long endtime = getCurrentTime(); + printf("insert total %d records, used %u seconds, avg:%u useconds\n", 10, (endtime-starttime)/1000000UL, (endtime-starttime)/(10)); + + free(v.ts); + free(lb); + free(params); + free(is_null); + free(no_null); + free(tags); + + return 0; +} + + + //300 tables 60 records int stmt_funcb1(TAOS_STMT *stmt) { struct { @@ -3835,7 +4053,7 @@ void* runcase(void *par) { #endif -#if 1 +#if 1 prepare(taos, 1, 1); stmt = taos_stmt_init(taos); @@ -3900,7 +4118,7 @@ void* runcase(void *par) { #endif -#if 1 +#if 1 prepare(taos, 1, 0); stmt = taos_stmt_init(taos); @@ -3930,6 +4148,21 @@ void* runcase(void *par) { #endif +#if 1 + prepare(taos, 1, 0); + + stmt = taos_stmt_init(taos); + + printf("1t+10r+bm+autoctb+e3 start\n"); + stmt_funcb_autoctb_e3(stmt); + printf("1t+10r+bm+autoctb+e3 end\n"); + printf("check result start\n"); + //check_result(taos, "m0", 1, 0); + printf("check result end\n"); + taos_stmt_close(stmt); + +#endif + #if 1 prepare(taos, 1, 1);