[TD-4034]fix schema change,free not exist column data

This commit is contained in:
lichuang 2021-05-20 09:29:31 +08:00
parent b2e3024217
commit f182495628
2 changed files with 28 additions and 19 deletions

View File

@ -73,8 +73,8 @@ typedef struct {
} STsdbCfg;
#define CACHE_NO_LAST(c) ((c)->cacheLastRow == 0)
#define CACHE_LAST_ROW(c) ((c)->cacheLastRow == 1)
#define CACHE_LAST_NULL_COLUMN(c) ((c)->cacheLastRow == 2)
#define CACHE_LAST_ROW(c) (((c)->cacheLastRow & 1) > 0)
#define CACHE_LAST_NULL_COLUMN(c) (((c)->cacheLastRow & 2) > 0)
// --------- TSDB REPOSITORY USAGE STATISTICS
typedef struct {

View File

@ -660,43 +660,52 @@ int tsdbUpdateLastColSchema(STable *pTable, STSchema *pNewSchema) {
TSDB_WLOCK_TABLE(pTable);
int16_t oldIdx = 0;
for (int16_t i = 0; i < numOfCols; ++i) {
STColumn *pCol = schemaColAt(pNewSchema, i);
int16_t idx = tsdbGetLastColumnsIndexByColId(pTable, pCol->colId);
SDataCol* pDataCol = &(lastCols[i]);
if (idx != -1) {
// move col data to new last column array
SDataCol* pOldDataCol = &(pTable->lastCols[idx]);
memcpy(pDataCol, pOldDataCol, sizeof(SDataCol));
} else {
// init new colid data
pDataCol->colId = pCol->colId;
pDataCol->bytes = 0;
pDataCol->pData = NULL;
}
// free dropped column data
while (oldIdx < idx && oldIdx < pTable->lastColNum) {
SDataCol* pOldDataCol = &(pTable->lastCols[oldIdx]);
if (pOldDataCol->bytes != 0) {
tfree(pOldDataCol->pData);
pOldDataCol->bytes = 0;
}
++oldIdx;
}
if (idx != -1 && oldIdx == idx) {
oldIdx += 1;
}
}
// free old schema last column datas
tfree(pTable->lastCols);
SDataCol *oldLastCols = pTable->lastCols;
int16_t oldLastColNum = pTable->lastColNum;
pTable->lastColSVersion = schemaVersion(pNewSchema);
pTable->lastCols = lastCols;
pTable->lastColNum = numOfCols;
if (oldLastCols == NULL) {
TSDB_WUNLOCK_TABLE(pTable);
return 0;
}
// free old schema last column datas
for (int16_t i = 0; i < oldLastColNum; ++i) {
SDataCol* pDataCol = &(oldLastCols[i]);
if (pDataCol->bytes == 0) {
continue;
}
int16_t idx = tsdbGetLastColumnsIndexByColId(pTable, pDataCol->colId);
if (idx != -1) {
continue;
}
// free not exist column data
tfree(pDataCol->pData);
}
TSDB_WUNLOCK_TABLE(pTable);
tfree(oldLastCols);
return 0;
}