Merge pull request #22427 from taosdata/feature/rebuildIndex0

rebuild index at tag0
This commit is contained in:
Haojun Liao 2023-08-16 09:47:43 +08:00 committed by GitHub
commit 63bc50a723
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 302 additions and 125 deletions

View File

@ -32,6 +32,8 @@ void taosSeedRand(uint32_t seed);
uint32_t taosRand(void); uint32_t taosRand(void);
uint32_t taosRandR(uint32_t* pSeed); uint32_t taosRandR(uint32_t* pSeed);
void taosRandStr(char* str, int32_t size); void taosRandStr(char* str, int32_t size);
void taosRandStr2(char* str, int32_t size);
uint32_t taosSafeRand(void); uint32_t taosSafeRand(void);
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -142,7 +142,8 @@ int32_t colDataSetVal(SColumnInfoData* pColumnInfoData, uint32_t rowIndex, const
return 0; return 0;
} }
int32_t colDataReassignVal(SColumnInfoData* pColumnInfoData, uint32_t dstRowIdx, uint32_t srcRowIdx, const char* pData) { int32_t colDataReassignVal(SColumnInfoData* pColumnInfoData, uint32_t dstRowIdx, uint32_t srcRowIdx,
const char* pData) {
int32_t type = pColumnInfoData->info.type; int32_t type = pColumnInfoData->info.type;
if (IS_VAR_DATA_TYPE(type)) { if (IS_VAR_DATA_TYPE(type)) {
int32_t dataLen = 0; int32_t dataLen = 0;
@ -164,7 +165,6 @@ int32_t colDataReassignVal(SColumnInfoData* pColumnInfoData, uint32_t dstRowIdx,
return 0; return 0;
} }
static int32_t colDataReserve(SColumnInfoData* pColumnInfoData, size_t newSize) { static int32_t colDataReserve(SColumnInfoData* pColumnInfoData, size_t newSize) {
if (!IS_VAR_DATA_TYPE(pColumnInfoData->info.type)) { if (!IS_VAR_DATA_TYPE(pColumnInfoData->info.type)) {
return TSDB_CODE_SUCCESS; return TSDB_CODE_SUCCESS;
@ -190,7 +190,8 @@ static int32_t colDataReserve(SColumnInfoData* pColumnInfoData, size_t newSize)
static int32_t doCopyNItems(struct SColumnInfoData* pColumnInfoData, int32_t currentRow, const char* pData, static int32_t doCopyNItems(struct SColumnInfoData* pColumnInfoData, int32_t currentRow, const char* pData,
int32_t itemLen, int32_t numOfRows, bool trimValue) { int32_t itemLen, int32_t numOfRows, bool trimValue) {
if (pColumnInfoData->info.bytes < itemLen) { if (pColumnInfoData->info.bytes < itemLen) {
uWarn("column/tag actual data len %d is bigger than schema len %d, trim it:%d", itemLen, pColumnInfoData->info.bytes, trimValue); uWarn("column/tag actual data len %d is bigger than schema len %d, trim it:%d", itemLen,
pColumnInfoData->info.bytes, trimValue);
if (trimValue) { if (trimValue) {
itemLen = pColumnInfoData->info.bytes; itemLen = pColumnInfoData->info.bytes;
} else { } else {
@ -230,8 +231,8 @@ static int32_t doCopyNItems(struct SColumnInfoData* pColumnInfoData, int32_t cur
return TSDB_CODE_SUCCESS; return TSDB_CODE_SUCCESS;
} }
int32_t colDataSetNItems(SColumnInfoData* pColumnInfoData, uint32_t currentRow, const char* pData, int32_t colDataSetNItems(SColumnInfoData* pColumnInfoData, uint32_t currentRow, const char* pData, uint32_t numOfRows,
uint32_t numOfRows, bool trimValue) { bool trimValue) {
int32_t len = pColumnInfoData->info.bytes; int32_t len = pColumnInfoData->info.bytes;
if (IS_VAR_DATA_TYPE(pColumnInfoData->info.type)) { if (IS_VAR_DATA_TYPE(pColumnInfoData->info.type)) {
len = varDataTLen(pData); len = varDataTLen(pData);
@ -698,8 +699,7 @@ int32_t blockDataFromBuf(SSDataBlock* pBlock, const char* buf) {
return TSDB_CODE_SUCCESS; return TSDB_CODE_SUCCESS;
} }
static bool colDataIsNNull(const SColumnInfoData* pColumnInfoData, int32_t startIndex, static bool colDataIsNNull(const SColumnInfoData* pColumnInfoData, int32_t startIndex, uint32_t nRows) {
uint32_t nRows) {
if (!pColumnInfoData->hasNull) { if (!pColumnInfoData->hasNull) {
return false; return false;
} }
@ -880,7 +880,6 @@ int32_t dataBlockCompar(const void* p1, const void* p2, const void* param) {
} }
static int32_t blockDataAssign(SColumnInfoData* pCols, const SSDataBlock* pDataBlock, const int32_t* index) { static int32_t blockDataAssign(SColumnInfoData* pCols, const SSDataBlock* pDataBlock, const int32_t* index) {
size_t numOfCols = taosArrayGetSize(pDataBlock->pDataBlock); size_t numOfCols = taosArrayGetSize(pDataBlock->pDataBlock);
for (int32_t i = 0; i < numOfCols; ++i) { for (int32_t i = 0; i < numOfCols; ++i) {
SColumnInfoData* pDst = &pCols[i]; SColumnInfoData* pDst = &pCols[i];
@ -1131,6 +1130,7 @@ static int32_t doEnsureCapacity(SColumnInfoData* pColumn, const SDataBlockInfo*
if (tmp == NULL) { if (tmp == NULL) {
return TSDB_CODE_OUT_OF_MEMORY; return TSDB_CODE_OUT_OF_MEMORY;
} }
// memset(tmp, 0, numOfRows * pColumn->info.bytes);
// copy back the existed data // copy back the existed data
if (pColumn->pData != NULL) { if (pColumn->pData != NULL) {
@ -1474,7 +1474,7 @@ size_t blockDataGetCapacityInRow(const SSDataBlock* pBlock, size_t pageSize, int
int end = nRows; int end = nRows;
while (start <= end) { while (start <= end) {
int mid = start + (end - start) / 2; int mid = start + (end - start) / 2;
//data size + var data type columns offset + fixed data type columns bitmap len // data size + var data type columns offset + fixed data type columns bitmap len
int midSize = rowSize * mid + numVarCols * sizeof(int32_t) * mid + numFixCols * BitmapLen(mid); int midSize = rowSize * mid + numVarCols * sizeof(int32_t) * mid + numFixCols * BitmapLen(mid);
if (midSize > payloadSize) { if (midSize > payloadSize) {
result = mid; result = mid;
@ -1772,7 +1772,7 @@ static char* formatTimestamp(char* buf, int64_t val, int precision) {
// for debug // for debug
char* dumpBlockData(SSDataBlock* pDataBlock, const char* flag, char** pDataBuf) { char* dumpBlockData(SSDataBlock* pDataBlock, const char* flag, char** pDataBuf) {
int32_t size = 2048*1024; int32_t size = 2048 * 1024;
*pDataBuf = taosMemoryCalloc(size, 1); *pDataBuf = taosMemoryCalloc(size, 1);
char* dumpBuf = *pDataBuf; char* dumpBuf = *pDataBuf;
char pBuf[128] = {0}; char pBuf[128] = {0};
@ -1780,8 +1780,8 @@ char* dumpBlockData(SSDataBlock* pDataBlock, const char* flag, char** pDataBuf)
int32_t rows = pDataBlock->info.rows; int32_t rows = pDataBlock->info.rows;
int32_t len = 0; int32_t len = 0;
len += snprintf(dumpBuf + len, size - len, len += snprintf(dumpBuf + len, size - len,
"===stream===%s|block type %d|child id %d|group id:%" PRIu64 "|uid:%" PRId64 "===stream===%s|block type %d|child id %d|group id:%" PRIu64 "|uid:%" PRId64 "|rows:%" PRId64
"|rows:%" PRId64 "|version:%" PRIu64 "|cal start:%" PRIu64 "|cal end:%" PRIu64 "|tbl:%s\n", "|version:%" PRIu64 "|cal start:%" PRIu64 "|cal end:%" PRIu64 "|tbl:%s\n",
flag, (int32_t)pDataBlock->info.type, pDataBlock->info.childId, pDataBlock->info.id.groupId, flag, (int32_t)pDataBlock->info.type, pDataBlock->info.childId, pDataBlock->info.id.groupId,
pDataBlock->info.id.uid, pDataBlock->info.rows, pDataBlock->info.version, pDataBlock->info.id.uid, pDataBlock->info.rows, pDataBlock->info.version,
pDataBlock->info.calWin.skey, pDataBlock->info.calWin.ekey, pDataBlock->info.parTbName); pDataBlock->info.calWin.skey, pDataBlock->info.calWin.ekey, pDataBlock->info.parTbName);
@ -2181,7 +2181,8 @@ int32_t blockEncode(const SSDataBlock* pBlock, char* data, int32_t numOfCols) {
} }
colSizes[col] = htonl(colSizes[col]); colSizes[col] = htonl(colSizes[col]);
// uError("blockEncode col bytes:%d, type:%d, size:%d, htonl size:%d", pColRes->info.bytes, pColRes->info.type, htonl(colSizes[col]), colSizes[col]); // uError("blockEncode col bytes:%d, type:%d, size:%d, htonl size:%d", pColRes->info.bytes, pColRes->info.type,
// htonl(colSizes[col]), colSizes[col]);
} }
*actualLen = dataLen; *actualLen = dataLen;
@ -2283,7 +2284,7 @@ const char* blockDecode(SSDataBlock* pBlock, const char* pData) {
} }
void trimDataBlock(SSDataBlock* pBlock, int32_t totalRows, const bool* pBoolList) { void trimDataBlock(SSDataBlock* pBlock, int32_t totalRows, const bool* pBoolList) {
// int32_t totalRows = pBlock->info.rows; // int32_t totalRows = pBlock->info.rows;
int32_t bmLen = BitmapLen(totalRows); int32_t bmLen = BitmapLen(totalRows);
char* pBitmap = NULL; char* pBitmap = NULL;
int32_t maxRows = 0; int32_t maxRows = 0;
@ -2310,7 +2311,8 @@ void trimDataBlock(SSDataBlock* pBlock, int32_t totalRows, const bool* pBoolList
if (colDataIsNull_var(pDst, j)) { if (colDataIsNull_var(pDst, j)) {
colDataSetNull_var(pDst, numOfRows); colDataSetNull_var(pDst, numOfRows);
} else { } else {
// fix address sanitizer error. p1 may point to memory that will change during realloc of colDataSetVal, first copy it to p2 // fix address sanitizer error. p1 may point to memory that will change during realloc of colDataSetVal, first
// copy it to p2
char* p1 = colDataGetVarData(pDst, j); char* p1 = colDataGetVarData(pDst, j);
int32_t len = 0; int32_t len = 0;
if (pDst->info.type == TSDB_DATA_TYPE_JSON) { if (pDst->info.type == TSDB_DATA_TYPE_JSON) {

View File

@ -79,9 +79,12 @@ int32_t mndInitIdx(SMnode *pMnode) {
return sdbSetTable(pMnode->pSdb, table); return sdbSetTable(pMnode->pSdb, table);
} }
static int32_t mndFindSuperTableTagId(const SStbObj *pStb, const char *tagName) { static int32_t mndFindSuperTableTagId(const SStbObj *pStb, const char *tagName, int8_t *hasIdx) {
for (int32_t tag = 0; tag < pStb->numOfTags; tag++) { for (int32_t tag = 0; tag < pStb->numOfTags; tag++) {
if (strcasecmp(pStb->pTags[tag].name, tagName) == 0) { if (strcasecmp(pStb->pTags[tag].name, tagName) == 0) {
if (IS_IDX_ON(&pStb->pTags[tag])) {
*hasIdx = 1;
}
return tag; return tag;
} }
} }
@ -597,7 +600,8 @@ static int32_t mndSetUpdateIdxStbCommitLogs(SMnode *pMnode, STrans *pTrans, SStb
pNew->updateTime = taosGetTimestampMs(); pNew->updateTime = taosGetTimestampMs();
pNew->lock = 0; pNew->lock = 0;
int32_t tag = mndFindSuperTableTagId(pOld, tagName); int8_t hasIdx = 0;
int32_t tag = mndFindSuperTableTagId(pOld, tagName, &hasIdx);
if (tag < 0) { if (tag < 0) {
terrno = TSDB_CODE_MND_TAG_NOT_EXIST; terrno = TSDB_CODE_MND_TAG_NOT_EXIST;
return -1; return -1;
@ -612,14 +616,14 @@ static int32_t mndSetUpdateIdxStbCommitLogs(SMnode *pMnode, STrans *pTrans, SStb
SSchema *pTag = pNew->pTags + tag; SSchema *pTag = pNew->pTags + tag;
if (on == 1) { if (on == 1) {
if (IS_IDX_ON(pTag)) { if (hasIdx && tag != 0) {
terrno = TSDB_CODE_MND_TAG_INDEX_ALREADY_EXIST; terrno = TSDB_CODE_MND_TAG_INDEX_ALREADY_EXIST;
return -1; return -1;
} else { } else {
SSCHMEA_SET_IDX_ON(pTag); SSCHMEA_SET_IDX_ON(pTag);
} }
} else { } else {
if (!IS_IDX_ON(pTag)) { if (hasIdx == 0) {
terrno = TSDB_CODE_MND_SMA_NOT_EXIST; terrno = TSDB_CODE_MND_SMA_NOT_EXIST;
} else { } else {
SSCHMEA_SET_IDX_OFF(pTag); SSCHMEA_SET_IDX_OFF(pTag);
@ -667,7 +671,42 @@ _OVER:
mndTransDrop(pTrans); mndTransDrop(pTrans);
return code; return code;
} }
int8_t mndCheckIndexNameByTagName(SMnode *pMnode, SIdxObj *pIdxObj) {
// build index on first tag, and no index name;
int8_t exist = 0;
SDbObj *pDb = NULL;
if (strlen(pIdxObj->db) > 0) {
pDb = mndAcquireDb(pMnode, pIdxObj->db);
if (pDb == NULL) return 0;
}
SSmaAndTagIter *pIter = NULL;
SIdxObj *pIdx = NULL;
SSdb *pSdb = pMnode->pSdb;
while (1) {
pIter = sdbFetch(pSdb, SDB_IDX, pIter, (void **)&pIdx);
if (pIter == NULL) break;
if (NULL != pDb && pIdx->dbUid != pDb->uid) {
sdbRelease(pSdb, pIdx);
continue;
}
if (pIdxObj->stbUid != pIdx->stbUid) {
sdbRelease(pSdb, pIdx);
continue;
}
if (strncmp(pIdxObj->colName, pIdx->colName, TSDB_COL_NAME_LEN) == 0) {
sdbCancelFetch(pSdb, pIter);
sdbRelease(pSdb, pIdx);
exist = 1;
break;
}
sdbRelease(pSdb, pIdx);
}
mndReleaseDb(pMnode, pDb);
return exist;
}
static int32_t mndAddIndex(SMnode *pMnode, SRpcMsg *pReq, SCreateTagIndexReq *req, SDbObj *pDb, SStbObj *pStb) { static int32_t mndAddIndex(SMnode *pMnode, SRpcMsg *pReq, SCreateTagIndexReq *req, SDbObj *pDb, SStbObj *pStb) {
int32_t code = -1; int32_t code = -1;
SIdxObj idxObj = {0}; SIdxObj idxObj = {0};
@ -681,11 +720,20 @@ static int32_t mndAddIndex(SMnode *pMnode, SRpcMsg *pReq, SCreateTagIndexReq *re
idxObj.stbUid = pStb->uid; idxObj.stbUid = pStb->uid;
idxObj.dbUid = pStb->dbUid; idxObj.dbUid = pStb->dbUid;
int32_t tag = mndFindSuperTableTagId(pStb, req->colName); int8_t hasIdx = 0;
int32_t tag = mndFindSuperTableTagId(pStb, req->colName, &hasIdx);
if (tag < 0) { if (tag < 0) {
terrno = TSDB_CODE_MND_TAG_NOT_EXIST; terrno = TSDB_CODE_MND_TAG_NOT_EXIST;
return -1; return -1;
} else if (tag == 0) { }
int8_t exist = 0;
if (tag == 0 && hasIdx == 1) {
exist = mndCheckIndexNameByTagName(pMnode, &idxObj);
if (exist) {
terrno = TSDB_CODE_MND_TAG_INDEX_ALREADY_EXIST;
return -1;
}
} else if (hasIdx == 1) {
terrno = TSDB_CODE_MND_TAG_INDEX_ALREADY_EXIST; terrno = TSDB_CODE_MND_TAG_INDEX_ALREADY_EXIST;
return -1; return -1;
} }
@ -695,11 +743,11 @@ static int32_t mndAddIndex(SMnode *pMnode, SRpcMsg *pReq, SCreateTagIndexReq *re
return -1; return -1;
} }
SSchema *pTag = pStb->pTags + tag; // SSchema *pTag = pStb->pTags + tag;
if (IS_IDX_ON(pTag)) { // if (IS_IDX_ON(pTag)) {
terrno = TSDB_CODE_MND_TAG_INDEX_ALREADY_EXIST; // terrno = TSDB_CODE_MND_TAG_INDEX_ALREADY_EXIST;
return -1; // return -1;
} // }
code = mndAddIndexImpl(pMnode, pReq, pDb, pStb, &idxObj); code = mndAddIndexImpl(pMnode, pReq, pDb, pStb, &idxObj);
return code; return code;
@ -806,8 +854,8 @@ int32_t mndDropIdxsByStb(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *p
if (pIdx->stbUid == pStb->uid) { if (pIdx->stbUid == pStb->uid) {
if (mndSetDropIdxCommitLogs(pMnode, pTrans, pIdx) != 0) { if (mndSetDropIdxCommitLogs(pMnode, pTrans, pIdx) != 0) {
sdbCancelFetch(pSdb, pIter);
sdbRelease(pSdb, pIdx); sdbRelease(pSdb, pIdx);
sdbCancelFetch(pSdb, pIdx);
return -1; return -1;
} }
} }

View File

@ -18,6 +18,7 @@
#include "mndDb.h" #include "mndDb.h"
#include "mndDnode.h" #include "mndDnode.h"
#include "mndIndex.h" #include "mndIndex.h"
#include "mndIndexComm.h"
#include "mndInfoSchema.h" #include "mndInfoSchema.h"
#include "mndMnode.h" #include "mndMnode.h"
#include "mndPerfSchema.h" #include "mndPerfSchema.h"
@ -822,7 +823,7 @@ int32_t mndBuildStbFromReq(SMnode *pMnode, SStbObj *pDst, SMCreateStbReq *pCreat
return -1; return -1;
} }
if(pDst->nextColId < 0 || pDst->nextColId >= 0x7fff - pDst->numOfColumns - pDst->numOfTags){ if (pDst->nextColId < 0 || pDst->nextColId >= 0x7fff - pDst->numOfColumns - pDst->numOfTags) {
terrno = TSDB_CODE_MND_FIELD_VALUE_OVERFLOW; terrno = TSDB_CODE_MND_FIELD_VALUE_OVERFLOW;
return -1; return -1;
} }
@ -857,11 +858,39 @@ static int32_t mndCreateStb(SMnode *pMnode, SRpcMsg *pReq, SMCreateStbReq *pCrea
SStbObj stbObj = {0}; SStbObj stbObj = {0};
int32_t code = -1; int32_t code = -1;
char fullIdxName[TSDB_INDEX_FNAME_LEN * 2] = {0};
STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_DB_INSIDE, pReq, "create-stb"); STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_DB_INSIDE, pReq, "create-stb");
if (pTrans == NULL) goto _OVER; if (pTrans == NULL) goto _OVER;
mInfo("trans:%d, used to create stb:%s", pTrans->id, pCreate->name); mInfo("trans:%d, used to create stb:%s", pTrans->id, pCreate->name);
if (mndBuildStbFromReq(pMnode, &stbObj, pCreate, pDb) != 0) goto _OVER; if (mndBuildStbFromReq(pMnode, &stbObj, pCreate, pDb) != 0) goto _OVER;
char randStr[24] = {0};
taosRandStr2(randStr, tListLen(randStr) - 1);
SSchema *pSchema = &(stbObj.pTags[0]);
sprintf(fullIdxName, "%s.%s_%s", pDb->name, pSchema->name, randStr);
SSIdx idx = {0};
if (mndAcquireGlobalIdx(pMnode, fullIdxName, SDB_IDX, &idx) == 0 && idx.pIdx != NULL) {
terrno = TSDB_CODE_MND_TAG_INDEX_ALREADY_EXIST;
mndReleaseIdx(pMnode, idx.pIdx);
goto _OVER;
}
SIdxObj idxObj = {0};
memcpy(idxObj.name, fullIdxName, TSDB_INDEX_FNAME_LEN);
memcpy(idxObj.stb, stbObj.name, TSDB_TABLE_FNAME_LEN);
memcpy(idxObj.db, stbObj.db, TSDB_DB_FNAME_LEN);
memcpy(idxObj.colName, pSchema->name, TSDB_COL_NAME_LEN);
idxObj.createdTime = taosGetTimestampMs();
idxObj.uid = mndGenerateUid(fullIdxName, strlen(fullIdxName));
idxObj.stbUid = stbObj.uid;
idxObj.dbUid = stbObj.dbUid;
if (mndSetCreateIdxCommitLogs(pMnode, pTrans, &idxObj) < 0) goto _OVER;
if (mndAddStbToTrans(pMnode, pTrans, pDb, &stbObj) < 0) goto _OVER; if (mndAddStbToTrans(pMnode, pTrans, pDb, &stbObj) < 0) goto _OVER;
if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER; if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;
code = 0; code = 0;
@ -956,7 +985,7 @@ static int32_t mndBuildStbFromAlter(SStbObj *pStb, SStbObj *pDst, SMCreateStbReq
return -1; return -1;
} }
if(pDst->nextColId < 0 || pDst->nextColId >= 0x7fff - pDst->numOfColumns - pDst->numOfTags){ if (pDst->nextColId < 0 || pDst->nextColId >= 0x7fff - pDst->numOfColumns - pDst->numOfTags) {
terrno = TSDB_CODE_MND_FIELD_VALUE_OVERFLOW; terrno = TSDB_CODE_MND_FIELD_VALUE_OVERFLOW;
return -1; return -1;
} }
@ -1188,7 +1217,7 @@ static int32_t mndAddSuperTableTag(const SStbObj *pOld, SStbObj *pNew, SArray *p
return -1; return -1;
} }
if(pNew->nextColId < 0 || pNew->nextColId >= 0x7fff - ntags){ if (pNew->nextColId < 0 || pNew->nextColId >= 0x7fff - ntags) {
terrno = TSDB_CODE_MND_FIELD_VALUE_OVERFLOW; terrno = TSDB_CODE_MND_FIELD_VALUE_OVERFLOW;
return -1; return -1;
} }
@ -1478,7 +1507,8 @@ static int32_t mndAlterStbTagBytes(SMnode *pMnode, const SStbObj *pOld, SStbObj
SSchema *pTag = pNew->pTags + tag; SSchema *pTag = pNew->pTags + tag;
if (!(pTag->type == TSDB_DATA_TYPE_BINARY || pTag->type == TSDB_DATA_TYPE_NCHAR || pTag->type == TSDB_DATA_TYPE_GEOMETRY)) { if (!(pTag->type == TSDB_DATA_TYPE_BINARY || pTag->type == TSDB_DATA_TYPE_NCHAR ||
pTag->type == TSDB_DATA_TYPE_GEOMETRY)) {
terrno = TSDB_CODE_MND_INVALID_STB_OPTION; terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
return -1; return -1;
} }
@ -1506,7 +1536,7 @@ static int32_t mndAddSuperTableColumn(const SStbObj *pOld, SStbObj *pNew, SArray
return -1; return -1;
} }
if(pNew->nextColId < 0 || pNew->nextColId >= 0x7fff - ncols){ if (pNew->nextColId < 0 || pNew->nextColId >= 0x7fff - ncols) {
terrno = TSDB_CODE_MND_FIELD_VALUE_OVERFLOW; terrno = TSDB_CODE_MND_FIELD_VALUE_OVERFLOW;
return -1; return -1;
} }
@ -1598,7 +1628,8 @@ static int32_t mndAlterStbColumnBytes(SMnode *pMnode, const SStbObj *pOld, SStbO
} }
SSchema *pCol = pNew->pColumns + col; SSchema *pCol = pNew->pColumns + col;
if (!(pCol->type == TSDB_DATA_TYPE_BINARY || pCol->type == TSDB_DATA_TYPE_NCHAR || pCol->type == TSDB_DATA_TYPE_GEOMETRY)) { if (!(pCol->type == TSDB_DATA_TYPE_BINARY || pCol->type == TSDB_DATA_TYPE_NCHAR ||
pCol->type == TSDB_DATA_TYPE_GEOMETRY)) {
terrno = TSDB_CODE_MND_INVALID_STB_OPTION; terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
return -1; return -1;
} }
@ -3182,7 +3213,6 @@ static int32_t mndRetrieveStbCol(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pB
SSdb *pSdb = pMnode->pSdb; SSdb *pSdb = pMnode->pSdb;
SStbObj *pStb = NULL; SStbObj *pStb = NULL;
int32_t numOfRows = 0; int32_t numOfRows = 0;
if (!pShow->sysDbRsp) { if (!pShow->sysDbRsp) {
numOfRows = buildSysDbColsInfo(pBlock, pShow->db, pShow->filterTb); numOfRows = buildSysDbColsInfo(pBlock, pShow->db, pShow->filterTb);

View File

@ -17,8 +17,8 @@
#include "osMemory.h" #include "osMemory.h"
#include "tencode.h" #include "tencode.h"
void _metaReaderInit(SMetaReader* pReader, void* pVnode, int32_t flags, SStoreMeta* pAPI) { void _metaReaderInit(SMetaReader *pReader, void *pVnode, int32_t flags, SStoreMeta *pAPI) {
SMeta* pMeta = ((SVnode*)pVnode)->pMeta; SMeta *pMeta = ((SVnode *)pVnode)->pMeta;
metaReaderDoInit(pReader, pMeta, flags); metaReaderDoInit(pReader, pMeta, flags);
pReader->pAPI = pAPI; pReader->pAPI = pAPI;
} }
@ -143,7 +143,7 @@ tb_uid_t metaGetTableEntryUidByName(SMeta *pMeta, const char *name) {
int metaGetTableNameByUid(void *pVnode, uint64_t uid, char *tbName) { int metaGetTableNameByUid(void *pVnode, uint64_t uid, char *tbName) {
int code = 0; int code = 0;
SMetaReader mr = {0}; SMetaReader mr = {0};
metaReaderDoInit(&mr, ((SVnode*)pVnode)->pMeta, 0); metaReaderDoInit(&mr, ((SVnode *)pVnode)->pMeta, 0);
code = metaReaderGetTableEntryByUid(&mr, uid); code = metaReaderGetTableEntryByUid(&mr, uid);
if (code < 0) { if (code < 0) {
metaReaderClear(&mr); metaReaderClear(&mr);
@ -195,7 +195,7 @@ int metaGetTableUidByName(void *pVnode, char *tbName, uint64_t *uid) {
int metaGetTableTypeByName(void *pVnode, char *tbName, ETableType *tbType) { int metaGetTableTypeByName(void *pVnode, char *tbName, ETableType *tbType) {
int code = 0; int code = 0;
SMetaReader mr = {0}; SMetaReader mr = {0};
metaReaderDoInit(&mr, ((SVnode*)pVnode)->pMeta, 0); metaReaderDoInit(&mr, ((SVnode *)pVnode)->pMeta, 0);
code = metaGetTableEntryByName(&mr, tbName); code = metaGetTableEntryByName(&mr, tbName);
if (code == 0) *tbType = mr.me.type; if (code == 0) *tbType = mr.me.type;
@ -244,7 +244,7 @@ SMTbCursor *metaOpenTbCursor(void *pVnode) {
return NULL; return NULL;
} }
SVnode* pVnodeObj = pVnode; SVnode *pVnodeObj = pVnode;
// tdbTbcMoveToFirst((TBC *)pTbCur->pDbc); // tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
pTbCur->pMeta = pVnodeObj->pMeta; pTbCur->pMeta = pVnodeObj->pMeta;
pTbCur->paused = 1; pTbCur->paused = 1;
@ -1131,7 +1131,7 @@ int32_t metaFilterTtl(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
pCursor->type = param->type; pCursor->type = param->type;
metaRLock(pMeta); metaRLock(pMeta);
//ret = tdbTbcOpen(pMeta->pTtlIdx, &pCursor->pCur, NULL); // ret = tdbTbcOpen(pMeta->pTtlIdx, &pCursor->pCur, NULL);
END: END:
if (pCursor->pMeta) metaULock(pCursor->pMeta); if (pCursor->pMeta) metaULock(pCursor->pMeta);
@ -1186,7 +1186,7 @@ int32_t metaFilterTableIds(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
ret = -1; ret = -1;
for (int i = 0; i < oStbEntry.stbEntry.schemaTag.nCols; i++) { for (int i = 0; i < oStbEntry.stbEntry.schemaTag.nCols; i++) {
SSchema *schema = oStbEntry.stbEntry.schemaTag.pSchema + i; SSchema *schema = oStbEntry.stbEntry.schemaTag.pSchema + i;
if (schema->colId == param->cid && param->type == schema->type && (IS_IDX_ON(schema) || i == 0)) { if (schema->colId == param->cid && param->type == schema->type && (IS_IDX_ON(schema))) {
ret = 0; ret = 0;
} }
} }

View File

@ -450,12 +450,13 @@ int metaAddIndexToSTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
goto _err; goto _err;
} }
if (IS_IDX_ON(pNew) && !IS_IDX_ON(pOld)) { if (IS_IDX_ON(pNew) && !IS_IDX_ON(pOld)) {
if (diffIdx != -1) goto _err; // if (diffIdx != -1) goto _err;
diffIdx = i; diffIdx = i;
break;
} }
} }
if (diffIdx == -1 || diffIdx == 0) { if (diffIdx == -1) {
goto _err; goto _err;
} }
@ -586,7 +587,7 @@ int metaDropIndexFromSTable(SMeta *pMeta, int64_t version, SDropIndexReq *pReq)
for (int i = 0; i < oStbEntry.stbEntry.schemaTag.nCols; i++) { for (int i = 0; i < oStbEntry.stbEntry.schemaTag.nCols; i++) {
SSchema *schema = oStbEntry.stbEntry.schemaTag.pSchema + i; SSchema *schema = oStbEntry.stbEntry.schemaTag.pSchema + i;
if (0 == strncmp(schema->name, pReq->colName, sizeof(pReq->colName))) { if (0 == strncmp(schema->name, pReq->colName, sizeof(pReq->colName))) {
if (i != 0 || IS_IDX_ON(schema)) { if (IS_IDX_ON(schema)) {
pCol = schema; pCol = schema;
} }
break; break;
@ -2094,7 +2095,7 @@ static int metaUpdateTagIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry) {
} else { } else {
for (int i = 0; i < pTagSchema->nCols; i++) { for (int i = 0; i < pTagSchema->nCols; i++) {
pTagColumn = &pTagSchema->pSchema[i]; pTagColumn = &pTagSchema->pSchema[i];
if (i != 0 && !IS_IDX_ON(pTagColumn)) continue; if (!IS_IDX_ON(pTagColumn)) continue;
STagVal tagVal = {.cid = pTagColumn->colId}; STagVal tagVal = {.cid = pTagColumn->colId};
tTagGet((const STag *)pCtbEntry->ctbEntry.pTags, &tagVal); tTagGet((const STag *)pCtbEntry->ctbEntry.pTags, &tagVal);

View File

@ -864,7 +864,7 @@ static void setColumnInfoBySchema(const SRealTableNode* pTable, const SSchema* p
pCol->tableType = pTable->pMeta->tableType; pCol->tableType = pTable->pMeta->tableType;
pCol->colId = pColSchema->colId; pCol->colId = pColSchema->colId;
pCol->colType = (tagFlag >= 0 ? COLUMN_TYPE_TAG : COLUMN_TYPE_COLUMN); pCol->colType = (tagFlag >= 0 ? COLUMN_TYPE_TAG : COLUMN_TYPE_COLUMN);
pCol->hasIndex = ((0 == tagFlag) || (pColSchema != NULL && IS_IDX_ON(pColSchema))); pCol->hasIndex = (pColSchema != NULL && IS_IDX_ON(pColSchema));
pCol->node.resType.type = pColSchema->type; pCol->node.resType.type = pColSchema->type;
pCol->node.resType.bytes = pColSchema->bytes; pCol->node.resType.bytes = pColSchema->bytes;
if (TSDB_DATA_TYPE_TIMESTAMP == pCol->node.resType.type) { if (TSDB_DATA_TYPE_TIMESTAMP == pCol->node.resType.type) {

View File

@ -84,3 +84,12 @@ void taosRandStr(char* str, int32_t size) {
str[i] = set[taosRand() % len]; str[i] = set[taosRand() % len];
} }
} }
void taosRandStr2(char* str, int32_t size) {
const char* set = "abcdefghijklmnopqrstuvwxyz0123456789";
int32_t len = 36;
for (int32_t i = 0; i < size; ++i) {
str[i] = set[taosRand() % len];
}
}

View File

@ -1204,6 +1204,9 @@
,,y,script,./test.sh -f tsim/tag/drop_tag.sim ,,y,script,./test.sh -f tsim/tag/drop_tag.sim
,,y,script,./test.sh -f tsim/tag/tbNameIn.sim ,,y,script,./test.sh -f tsim/tag/tbNameIn.sim
,,y,script,./test.sh -f tmp/monitor.sim ,,y,script,./test.sh -f tmp/monitor.sim
,,y,script,./test.sh -f tsim/tagindex/add_index.sim
,,n,script,./test.sh -f tsim/tagindex/sma_and_tag_index.sim
#develop test #develop test
,,n,develop-test,python3 ./test.py -f 2-query/table_count_scan.py ,,n,develop-test,python3 ./test.py -f 2-query/table_count_scan.py

View File

@ -52,19 +52,35 @@ sql create sma index sma_index_name1 on stb function(max(c1),max(c2),min(c1)) in
print --> show sma print --> show sma
sql show indexes from stb from d1; sql show indexes from stb from d1;
if $rows != 1 then if $rows != 2 then
return -1 return -1
endi endi
if $data[0][0] != sma_index_name1 then
if $data[0][6] == tag_index then
if $data[1][0] != sma_index_name1 then
return -1 return -1
endi endi
if $data[0][1] != d1 then if $data[1][1] != d1 then
return -1 return -1
endi endi
if $data[0][2] != stb then if $data[1][2] != stb then
return -1 return -1
endi
else
if $data[0][0] != sma_index_name1 then
return -1
endi
if $data[0][1] != d1 then
return -1
endi
if $data[0][2] != stb then
return -1
endi
endi endi
print --> drop stb print --> drop stb
sql drop table stb; sql drop table stb;
@ -78,17 +94,30 @@ sql create sma index sma_index_name1 on stb function(max(c1),max(c2),min(c1)) in
print --> show sma print --> show sma
sql show indexes from stb from d1; sql show indexes from stb from d1;
if $rows != 1 then if $rows != 2 then
return -1 return -1
endi endi
if $data[0][0] != sma_index_name1 then
if $data[0][6] == tag_index then
if $data[1][0] != sma_index_name1 then
return -1 return -1
endi endi
if $data[0][1] != d1 then if $data[1][1] != d1 then
return -1 return -1
endi endi
if $data[0][2] != stb then if $data[1][2] != stb then
return -1 return -1
endi
else
if $data[0][0] != sma_index_name1 then
return -1
endi
if $data[0][1] != d1 then
return -1
endi
if $data[0][2] != stb then
return -1
endi
endi endi
print --> drop stb print --> drop stb

View File

@ -7,7 +7,7 @@ print ======== step0
$dbPrefix = ta_3_db $dbPrefix = ta_3_db
$tbPrefix = ta_3_tb $tbPrefix = ta_3_tb
$mtPrefix = ta_3_mt $mtPrefix = ta_3_mt
$tbNum = 500 $tbNum = 50
$rowNum = 20 $rowNum = 20
$totalNum = 200 $totalNum = 200
@ -48,12 +48,16 @@ while $i < $tbNum
$i = $i + 1 $i = $i + 1
endw endw
sql_error create index ti1 on $mtPrefix (t1)
sql create index ti2 on $mtPrefix (t2) sql create index ti2 on $mtPrefix (t2)
sql create index ti5 on $mtPrefix (t5) sql create index ti5 on $mtPrefix (t5)
print ==== test name conflict print ==== test name conflict
# #
sql_error create index ti1 on $mtPrefix(t1)
sql_error create index ti11 on $mtPrefix(t1)
sql_error create index ti3 on $mtPrefix(t2) sql_error create index ti3 on $mtPrefix(t2)
sql_error create index ti2 on $mtPrefix(t2) sql_error create index ti2 on $mtPrefix(t2)
@ -73,6 +77,15 @@ while $i < $tbNum
$i = $i + 1 $i = $i + 1
endw endw
$i = 0
while $i < $tbNum
sql select * from $mtPrefix where t1= $i ;
if $rows != 1 then
return -1
endi
$i = $i + 1
endw
print ===== test operator great equal print ===== test operator great equal
@ -250,7 +263,7 @@ endw
print === show index print === show index
sql select * from information_schema.ins_indexes sql select * from information_schema.ins_indexes
if $rows != 1 then if $rows != 2 then
return -1 return -1
endi endi
@ -259,12 +272,41 @@ print === drop index ti2
sql drop index ti2 sql drop index ti2
print === drop not exist index print === drop not exist index
sql select * from information_schema.ins_indexes
if $rows != 1 then
return -1
endi
sql drop index $data[0][0]
if $rows != 0 then
return -1
endi
sql_error drop index t2 sql_error drop index t2
sql_error drop index t3 sql_error drop index t3
sql create index ti0 on $mtPrefix (t1)
$i = $interval
while $i < $limit
sql select * from $mtPrefix where t1 <= $i ;
$tmp = $i - $interval
$tmp = $tmp + 1
if $rows != $tmp then
return -1
endi
$i = $i + 1
endw
sql_error create index ti0 on $mtPrefix (t1) sql_error create index ti0 on $mtPrefix (t1)
sql_error create index ti2 on $mtPrefix (t1)
sql_error create index t2i on ta_3_tb17 (t2) sql_error create index t2i on ta_3_tb17 (t2)

View File

@ -69,7 +69,7 @@ sql create sma index smat2i on $mtPrefix function(max(c1)) interval(6m,10s) slid
sql select * from information_schema.ins_indexes sql select * from information_schema.ins_indexes
if $rows != 2 then if $rows != 3 then
return -1 return -1
endi endi
@ -84,7 +84,7 @@ while $i < 5
endw endw
sql select * from information_schema.ins_indexes sql select * from information_schema.ins_indexes
if $rows != 6 then if $rows != 7 then
return -1 return -1
endi endi
@ -114,13 +114,13 @@ sql use $dbPrefix
sql create table if not exists $mtPrefix (ts timestamp, c1 int) tags (t1 int, t2 int, t3 int, t4 int, t5 int) sql create table if not exists $mtPrefix (ts timestamp, c1 int) tags (t1 int, t2 int, t3 int, t4 int, t5 int)
sql create index tagt2i on $mtPrefix (t2) sql create index tagt2i on $mtPrefix (t2)
sql select * from information_schema.ins_indexes sql select * from information_schema.ins_indexes
if $rows != 1 then if $rows != 2 then
return -1 return -1
endi endi
sql alter table $mtPrefix drop tag t2 sql alter table $mtPrefix drop tag t2
sql select * from information_schema.ins_indexes sql select * from information_schema.ins_indexes
if $rows != 0 then if $rows != 1 then
return -1 return -1
endi endi
@ -128,18 +128,22 @@ endi
print ==== rename tag name, and update index colName print ==== rename tag name, and update index colName
sql create index tagt3i on $mtPrefix (t3) sql create index tagt3i on $mtPrefix (t3)
sql select * from information_schema.ins_indexes sql select * from information_schema.ins_indexes
if $rows != 1 then if $rows != 2 then
return -1 return -1
endi endi
sql alter table $mtPrefix rename tag t3 txxx sql alter table $mtPrefix rename tag t3 txxx
sql select * from information_schema.ins_indexes sql select * from information_schema.ins_indexes
if $rows != 1 then if $rows != 2 then
return -1 return -1
endi endi
if $data05 != txxx then if $data05 == txxx then
return -1 print "manual created index"
elif $data15 == txxx then
print "auto created index at tag0"
else
return -1;
endi endi
@ -153,7 +157,7 @@ sql create table if not exists $mtPrefix (ts timestamp, c1 int) tags (t1 int, t2
sql create index tagt3i on $mtPrefix (t3) sql create index tagt3i on $mtPrefix (t3)
sql select * from information_schema.ins_indexes sql select * from information_schema.ins_indexes
if $rows != 2 then if $rows != 4 then
return -1 return -1
endi endi

View File

@ -59,14 +59,18 @@ class TDTestCase:
tdSql.checkData(1, 2, 2) tdSql.checkData(1, 2, 2)
def check_indexes(self): def check_indexes(self):
tdSql.checkRows(1) tdSql.checkRows(2)
for i in range(2):
col_name = tdSql.getData(i, 5)
if col_name == "t0":
continue
tdSql.checkCols(7) tdSql.checkCols(7)
tdSql.checkData(0, 0, 'idx1') tdSql.checkData(i, 0, 'idx1')
tdSql.checkData(0, 1, 'db') tdSql.checkData(i, 1, 'db')
tdSql.checkData(0, 2, 'stb') tdSql.checkData(i, 2, 'stb')
tdSql.checkData(0, 3, None) tdSql.checkData(i, 3, None)
tdSql.checkData(0, 5, 't1') tdSql.checkData(i, 5, 't1')
tdSql.checkData(0, 6, 'tag_index') tdSql.checkData(i, 6, 'tag_index')
def run(self): def run(self):
tdSql.execute(f'create database db') tdSql.execute(f'create database db')

View File

@ -118,12 +118,15 @@ class TDTestCase:
def show_tagidx(self, stbname): def show_tagidx(self, stbname):
sql = f'select index_name,column_name from information_schema.ins_indexes where db_name="db"' sql = f'select index_name,column_name from information_schema.ins_indexes where db_name="db"'
tdSql.query(sql) tdSql.query(sql)
rows = len(self.tag_dict.keys())-1 rows = len(self.tag_dict.keys())
tdSql.checkRows(rows) tdSql.checkRows(rows)
for i in range(rows): for i in range(rows):
col_name = tdSql.getData(i, 1) col_name = tdSql.getData(i, 1)
idx_name = f'idx_{col_name}' idx_name = f'idx_{col_name}'
# skip first tag
if col_name == "t1":
continue
tdSql.checkData(i, 0, idx_name) tdSql.checkData(i, 0, idx_name)
tdLog.info(f' show {rows} tag indexs ok.') tdLog.info(f' show {rows} tag indexs ok.')
@ -201,7 +204,7 @@ class TDTestCase:
# check idx result is 0 # check idx result is 0
sql = f'select index_name,column_name from information_schema.ins_indexes where db_name="db"' sql = f'select index_name,column_name from information_schema.ins_indexes where db_name="db"'
tdSql.query(sql) tdSql.query(sql)
tdSql.checkRows(0) tdSql.checkRows(1)
tdLog.info(f' drop {cnt} tag indexs ok.') tdLog.info(f' drop {cnt} tag indexs ok.')
# create long name idx # create long name idx