diff --git a/source/common/src/tdatablock.c b/source/common/src/tdatablock.c index 1c63174bf6..e37f80c719 100644 --- a/source/common/src/tdatablock.c +++ b/source/common/src/tdatablock.c @@ -2411,10 +2411,16 @@ _end: void buildCtbNameAddGroupId(const char* stbName, char* ctbName, uint64_t groupId) { char tmp[TSDB_TABLE_NAME_LEN] = {0}; - if (stbName == NULL) { - snprintf(tmp, TSDB_TABLE_NAME_LEN, "_%" PRIu64, groupId); - } else { - snprintf(tmp, TSDB_TABLE_NAME_LEN, "_%s_%" PRIu64, stbName, groupId); + if (stbName == NULL){ + snprintf(tmp, TSDB_TABLE_NAME_LEN, "_%"PRIu64, groupId); + }else{ + int32_t i = strlen(stbName) - 1; + for(; i >= 0; i--){ + if (stbName[i] == '.'){ + break; + } + } + snprintf(tmp, TSDB_TABLE_NAME_LEN, "_%s_%"PRIu64, stbName + i + 1, groupId); } ctbName[TSDB_TABLE_NAME_LEN - strlen(tmp) - 1] = 0; // put stbname + groupId to the end strcat(ctbName, tmp); diff --git a/source/common/test/dataformatTest.cpp b/source/common/test/dataformatTest.cpp index 2dfa706728..e8b7b132f2 100644 --- a/source/common/test/dataformatTest.cpp +++ b/source/common/test/dataformatTest.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wwrite-strings" @@ -475,6 +476,45 @@ TEST(testCase, AllNormTest) { taosMemoryFree(pTSchema); } +TEST(testCase, StreamAllNormTest) { + char ctbName[TSDB_TABLE_NAME_LEN] = {0}; + uint64_t groupId = 12345; + + buildCtbNameAddGroupId(NULL, ctbName, groupId); + + ASSERT_STREQ("_12345", ctbName); +} + +TEST(testCase, StreamWithStbName) { + char stbName[] = "1.table.stb"; + char ctbName[TSDB_TABLE_NAME_LEN] = {0}; + uint64_t groupId = 12345; + + buildCtbNameAddGroupId(stbName, ctbName, groupId); + + ASSERT_STREQ("_stb_12345", ctbName); +} + +TEST(testCase, StreamWithoutDotInStbName) { + char stbName[] = "table"; + char ctbName[TSDB_TABLE_NAME_LEN] = {0}; + uint64_t groupId = 12345; + + buildCtbNameAddGroupId(stbName, ctbName, groupId); + + ASSERT_STREQ("_table_12345", ctbName); +} + +TEST(testCase, StreamWithoutDotInStbName2) { + char stbName[] = ""; + char ctbName[TSDB_TABLE_NAME_LEN] = {0}; + uint64_t groupId = 12345; + + buildCtbNameAddGroupId(stbName, ctbName, groupId); + + ASSERT_STREQ("__12345", ctbName); +} + #if 1 TEST(testCase, NoneTest) { const static int nCols = 14;