Merge pull request #12579 from taosdata/feat/alter_table
feat: alter table
This commit is contained in:
commit
6d37249d6f
|
@ -386,7 +386,7 @@ static void *mndBuildVCreateStbReq(SMnode *pMnode, SVgObj *pVgroup, SStbObj *pSt
|
||||||
req.schema.sver = pStb->version;
|
req.schema.sver = pStb->version;
|
||||||
req.schema.pSchema = pStb->pColumns;
|
req.schema.pSchema = pStb->pColumns;
|
||||||
req.schemaTag.nCols = pStb->numOfTags;
|
req.schemaTag.nCols = pStb->numOfTags;
|
||||||
req.schemaTag.nCols = 0;
|
req.schemaTag.sver = 1;
|
||||||
req.schemaTag.pSchema = pStb->pTags;
|
req.schemaTag.pSchema = pStb->pTags;
|
||||||
|
|
||||||
if (req.rollup) {
|
if (req.rollup) {
|
||||||
|
|
|
@ -420,7 +420,8 @@ static int metaAlterTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pAl
|
||||||
// get table entry
|
// get table entry
|
||||||
SDecoder dc = {0};
|
SDecoder dc = {0};
|
||||||
tDecoderInit(&dc, pData, nData);
|
tDecoderInit(&dc, pData, nData);
|
||||||
metaDecodeEntry(&dc, &entry);
|
ret = metaDecodeEntry(&dc, &entry);
|
||||||
|
ASSERT(ret == 0);
|
||||||
|
|
||||||
if (entry.type != TSDB_NORMAL_TABLE) {
|
if (entry.type != TSDB_NORMAL_TABLE) {
|
||||||
terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
|
terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
|
||||||
|
@ -468,11 +469,11 @@ static int metaAlterTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pAl
|
||||||
goto _err;
|
goto _err;
|
||||||
}
|
}
|
||||||
pSchema->sver++;
|
pSchema->sver++;
|
||||||
pSchema->nCols--;
|
|
||||||
tlen = (pSchema->nCols - iCol - 1) * sizeof(SSchema);
|
tlen = (pSchema->nCols - iCol - 1) * sizeof(SSchema);
|
||||||
if (tlen) {
|
if (tlen) {
|
||||||
memmove(pColumn, pColumn + 1, tlen);
|
memmove(pColumn, pColumn + 1, tlen);
|
||||||
}
|
}
|
||||||
|
pSchema->nCols--;
|
||||||
break;
|
break;
|
||||||
case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
|
case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
|
||||||
if (pColumn == NULL) {
|
if (pColumn == NULL) {
|
||||||
|
@ -598,10 +599,41 @@ static int metaUpdateTableTagVal(SMeta *pMeta, int64_t version, SVAlterTbReq *pA
|
||||||
goto _err;
|
goto _err;
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
if (iCol == 0) {
|
||||||
// TODO:
|
// TODO : need to update tag index
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ctbEntry.version = version;
|
||||||
|
SKVRowBuilder kvrb = {0};
|
||||||
|
const SKVRow pOldTag = (const SKVRow)ctbEntry.ctbEntry.pTags;
|
||||||
|
SKVRow pNewTag = NULL;
|
||||||
|
|
||||||
|
tdInitKVRowBuilder(&kvrb);
|
||||||
|
for (int32_t i = 0; i < pTagSchema->nCols; i++) {
|
||||||
|
SSchema *pCol = &pTagSchema->pSchema[i];
|
||||||
|
if (iCol == i) {
|
||||||
|
tdAddColToKVRow(&kvrb, pCol->colId, pAlterTbReq->pTagVal, pAlterTbReq->nTagVal);
|
||||||
|
} else {
|
||||||
|
void *p = tdGetKVRowValOfCol(pOldTag, pCol->colId);
|
||||||
|
if (p) {
|
||||||
|
if (IS_VAR_DATA_TYPE(pCol->type)) {
|
||||||
|
tdAddColToKVRow(&kvrb, pCol->colId, p, varDataTLen(p));
|
||||||
|
} else {
|
||||||
|
tdAddColToKVRow(&kvrb, pCol->colId, p, pCol->bytes);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ctbEntry.ctbEntry.pTags = tdGetKVRowFromBuilder(&kvrb);
|
||||||
|
tdDestroyKVRowBuilder(&kvrb);
|
||||||
|
|
||||||
|
// save to table.db
|
||||||
|
metaSaveToTbDb(pMeta, &ctbEntry);
|
||||||
|
|
||||||
|
// save to uid.idx
|
||||||
|
tdbDbUpsert(pMeta->pUidIdx, &ctbEntry.uid, sizeof(tb_uid_t), &version, sizeof(version), &pMeta->txn);
|
||||||
|
|
||||||
if (ctbEntry.pBuf) taosMemoryFree(ctbEntry.pBuf);
|
if (ctbEntry.pBuf) taosMemoryFree(ctbEntry.pBuf);
|
||||||
if (stbEntry.pBuf) tdbFree(stbEntry.pBuf);
|
if (stbEntry.pBuf) tdbFree(stbEntry.pBuf);
|
||||||
tdbDbcClose(pTbDbc);
|
tdbDbcClose(pTbDbc);
|
||||||
|
|
Loading…
Reference in New Issue