From 61d5f6ff2ac09dbbb63e95829302e0d8399e9f5d Mon Sep 17 00:00:00 2001 From: Shuduo Sang Date: Sat, 16 Apr 2022 12:18:07 +0800 Subject: [PATCH] feat(tools): demoapi implemented normal insert/query (#11553) * [TD-13558]: taos shell refactor add taosTools as submodule * add tools/taos-tools * add more client interface for taosTools compile * update taos-tools * update taos-tools * refactor shell * [TD-13558]: taos shell test speed * [TD-13558]: taos -n startup works * taos -n rpc works * taos -n server works * cleanup code since no endPort in 3.0 * update taos-tools * [TD-13558]: taos -C works * improve taos shell -c WIP * update taos-tools * add demoapi.c * adjust show databases result for 3.0 * test: add platform logic * add nchar * adjust taos_fetch_lengths * print fields * remove show databases check from insert cases * fix lua example compile for 3.0 still not work * remove lua.py from smoketest * use get_column_data_offset() to get offset and convert length --- example/src/demoapi.c | 123 +++++++++++++++++++++++++++++++------- tests/pytest/smoketest.sh | 2 +- 2 files changed, 103 insertions(+), 22 deletions(-) diff --git a/example/src/demoapi.c b/example/src/demoapi.c index b1efdb21b9..c38e481b96 100644 --- a/example/src/demoapi.c +++ b/example/src/demoapi.c @@ -30,7 +30,7 @@ fprintf(stderr, "\033[0m"); } while(0) int64_t g_num_of_tb = 2; -int64_t g_num_of_rec = 2; +int64_t g_num_of_rec = 3; static struct argp_option options[] = { {"tables", 't', "NUMBER", 0, "Number of child tables, default is 10000."}, @@ -42,10 +42,18 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state) { switch (key) { case 't': g_num_of_tb = atoll(arg); + if (g_num_of_tb < 1) { + warnPrint("minimal g_num_of_tb is %d\n", 1); + g_num_of_tb = 1; + } break; case 'n': g_num_of_rec = atoll(arg); + if (g_num_of_rec < 2) { + warnPrint("minimal g_num_of_rec is %d\n", 2); + g_num_of_rec = 2; + } break; } @@ -65,15 +73,32 @@ static void prepare_data(TAOS* taos) { usleep(100000); taos_select_db(taos, "test"); - res = taos_query(taos, "create table meters(ts timestamp, f float, n int, b binary(20), c nchar(20)) tags(area int, city binary(20), dist nchar(20));"); + char command[1024] = {0}; + sprintf(command, "%s", "create table meters(ts timestamp, f float, n int, bin1 binary(20), c nchar(20), bin2 binary(20)) tags(area int, city binary(20), dist nchar(20), street binary(20));"); + res = taos_query(taos, command); + if ((res) && (0 == taos_errno(res))) { + okPrint("%s created\n", "meters"); + } else { + errorPrint("%s() LN%d: %s\n", + __func__, __LINE__, taos_errstr(res)); + taos_free_result(res); + exit(1); + } taos_free_result(res); - char command[1024] = {0}; for (int64_t i = 0; i < g_num_of_tb; i ++) { -// sprintf(command, "create table t%"PRId64" using meters tags(%"PRId64", '%s', '%s');", -// i, i, (i%2)?"beijing":"shanghai", (i%2)?"朝阳区":"黄浦区"); - sprintf(command, "create table t%"PRId64" using meters tags(%"PRId64", '%s', '%s');", - i, i, (i%2)?"beijing":"shanghai", (i%2)?"chaoyang":"huangpu"); + sprintf(command, "create table t%"PRId64" using meters " + "tags(%"PRId64", '%s', '%s', '%s');", + i, i, (i%2)?"beijing":"shanghai", + (i%2)?"朝阳区":"黄浦区", + (i%2)?"长安街":"中山路"); +/* sprintf(command, "create table t%"PRId64" using meters " + "tags(%"PRId64", '%s', '%s', '%s');", + i, i, + (i%2)?"beijing":"shanghai", + (i%2)?"chaoyang":"huangpu", + (i%2?"changan street":"jianguo rd")); + */ res = taos_query(taos, command); if ((res) && (0 == taos_errno(res))) { okPrint("t%" PRId64 " created\n", i); @@ -86,11 +111,15 @@ static void prepare_data(TAOS* taos) { int64_t j = 0; int64_t total = 0; int64_t affected; - for (; j < g_num_of_rec -1; j ++) { + for (; j < g_num_of_rec -2; j ++) { sprintf(command, "insert into t%"PRId64" " - "values(%" PRId64 ", %f, %"PRId64", '%c%d', '%c%d')", - i, 1650000000000+j, (float)j, j, 'a'+(int)j%25, rand(), - 'z' - (int)j%25, rand()); + "values(%" PRId64 ", %f, %"PRId64", " + "'%c%d', '%s%c%d', '%c%d')", + i, 1650000000000+j, (float)j, j, + 'a'+(int)j%25, rand(), + "涛思", 'z' - (int)j%25, rand(), + 'b' - (int)j%25, rand() + ); res = taos_query(taos, command); if ((res) && (0 == taos_errno(res))) { affected = taos_affected_rows(res); @@ -101,8 +130,25 @@ static void prepare_data(TAOS* taos) { } taos_free_result(res); } - sprintf(command, "insert into t%"PRId64" values(%" PRId64 ", NULL, NULL, NULL, NULL)", - i, 1650000000000+j+1); + sprintf(command, "insert into t%"PRId64" values(%" PRId64 ", " + "NULL, NULL, NULL, NULL, NULL)", + i, 1650000000000+j); + res = taos_query(taos, command); + if ((res) && (0 == taos_errno(res))) { + affected = taos_affected_rows(res); + total += affected; + } else { + errorPrint("%s() LN%d: %s\n", + __func__, __LINE__, taos_errstr(res)); + } + sprintf(command, "insert into t%"PRId64" " + "values(%" PRId64 ", %f, %"PRId64", " + "'%c%d', '%s%c%d', '%c%d')", + i, 1650000000000+j+1, (float)j, j, + 'a'+(int)j%25, rand(), + "数据", 'z' - (int)j%25, rand(), + 'b' - (int)j%25, rand() + ); res = taos_query(taos, command); if ((res) && (0 == taos_errno(res))) { affected = taos_affected_rows(res); @@ -113,7 +159,8 @@ static void prepare_data(TAOS* taos) { } taos_free_result(res); - printf("insert %"PRId64" records into t%"PRId64", total affected rows: %"PRId64"\n", j, i, total); + okPrint("insert %"PRId64" records into t%"PRId64", " + "total affected rows: %"PRId64"\n", j, i, total); } } @@ -127,29 +174,63 @@ static int print_result(char *tbname, TAOS_RES* res, int block) { printf("fields[%d].name=%s, fields[%d].type=%d, fields[%d].bytes=%d\n", f, fields[f].name, f, fields[f].type, f, fields[f].bytes); } + if (block) { - warnPrint("%s() LN%d, call taos_fetch_block()\n", __func__, __LINE__); + warnPrint("%s", "call taos_fetch_block()\n"); int rows = 0; while ((rows = taos_fetch_block(res, &row))) { + for (int f = 0; f < num_fields; f++) { + if ((fields[f].type != TSDB_DATA_TYPE_VARCHAR) + && (fields[f].type != TSDB_DATA_TYPE_NCHAR) + && (fields[f].type != TSDB_DATA_TYPE_JSON)) { + printf("col%d type is %d, no need get offset\n", + f, fields[f].type); + continue; + } + + int *offsets = taos_get_column_data_offset(res, f); + if (offsets) { + for (int c = 0; c < rows; c++) { + if (offsets[c] != -1) { + int length = *(int16_t*)(row[f] + offsets[c]); + char *buf = calloc(1, length + 1); + strncpy(buf, (char *)(row[f] + offsets[c] + 2), length); + printf("row: %d, col: %d, offset: %d, length: %d, content: %s\n", + c, f, offsets[c], length, buf); + free(buf); + } else { + printf("row: %d, col: %d, offset: -1, means content is NULL\n", + c, f); + } + } + } else { + errorPrint("%s() LN%d: col%d's lengths is NULL\n", + __func__, __LINE__, f); + } + } num_rows += rows; } } else { - warnPrint("%s() LN%d, call taos_fetch_rows()\n", __func__, __LINE__); + warnPrint("%s", "call taos_fetch_rows()\n"); while ((row = taos_fetch_row(res))) { char temp[256] = {0}; taos_print_row(temp, row, fields, num_fields); puts(temp); - num_rows ++; int* lengths = taos_fetch_lengths(res); if (lengths) { for (int c = 0; c < num_fields; c++) { - printf("length of column %d is %d\n", c, lengths[c]); + printf("row: %"PRId64", col: %d, is_null: %s, length of column %d is %d\n", + num_rows, c, + taos_is_null(res, num_rows, c)?"True":"False", + c, lengths[c]); } } else { errorPrint("%s() LN%d: %s's lengths is NULL\n", __func__, __LINE__, tbname); } + + num_rows ++; } } @@ -172,8 +253,8 @@ static void verify_query(TAOS* taos) { int field_count = taos_field_count(res); printf("field_count: %d\n", field_count); int64_t rows = print_result(tbname, res, i % 2); - printf("rows is: %"PRId64"\n", rows); - + okPrint("total query %s result rows is: %"PRId64"\n", + tbname, rows); } else { errorPrint("%s() LN%d: %s\n", __func__, __LINE__, taos_errstr(res)); @@ -207,7 +288,7 @@ int main(int argc, char *argv[]) { verify_query(taos); taos_close(taos); - printf("done\n"); + okPrint("%s", "done\n"); return 0; } diff --git a/tests/pytest/smoketest.sh b/tests/pytest/smoketest.sh index 7ac5d4f6d3..32f5f00c51 100755 --- a/tests/pytest/smoketest.sh +++ b/tests/pytest/smoketest.sh @@ -58,4 +58,4 @@ python3 ./test.py $1 -f client/client.py python3 ./test.py $1 -s && sleep 1 # connector -python3 ./test.py $1 -f connector/lua.py +# python3 ./test.py $1 -f connector/lua.py