diff --git a/source/dnode/vnode/src/meta/metaEntry2.c b/source/dnode/vnode/src/meta/metaEntry2.c index cc0859f883..b180f52927 100644 --- a/source/dnode/vnode/src/meta/metaEntry2.c +++ b/source/dnode/vnode/src/meta/metaEntry2.c @@ -1319,11 +1319,6 @@ static int32_t metaHandleNormalTableUpdate(SMeta *pMeta, const SMetaEntry *pEntr tsdbCacheInvalidateSchema(pMeta->pVnode->pTsdb, 0, entry.uid, pSchema->version); } } - { // for update column bytes - - if (!TSDB_CACHE_NO(pMeta->pVnode->config)) { - tsdbCacheInvalidateSchema(pMeta->pVnode->pTsdb, 0, entry.uid, pSchema->version); - } } #endif tsdbCacheInvalidateSchema(pMeta->pVnode->pTsdb, 0, pEntry->uid, pEntry->ntbEntry.schemaRow.version); diff --git a/source/dnode/vnode/src/meta/metaTable.c b/source/dnode/vnode/src/meta/metaTable.c index 202b0454a9..145f506c7e 100644 --- a/source/dnode/vnode/src/meta/metaTable.c +++ b/source/dnode/vnode/src/meta/metaTable.c @@ -2992,7 +2992,7 @@ int metaAlterTable(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMeta case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES: return metaAlterTableColumnBytes(pMeta, version, pReq, pMetaRsp); case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME: - return metaAlterTableColumn(pMeta, version, pReq, pMetaRsp); + return metaAlterTableColumnName(pMeta, version, pReq, pMetaRsp); case TSDB_ALTER_TABLE_UPDATE_TAG_VAL: return metaUpdateTableTagVal(pMeta, version, pReq); case TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL: diff --git a/source/dnode/vnode/src/meta/metaTable2.c b/source/dnode/vnode/src/meta/metaTable2.c index fce2de7c5d..97d5e8903e 100644 --- a/source/dnode/vnode/src/meta/metaTable2.c +++ b/source/dnode/vnode/src/meta/metaTable2.c @@ -812,8 +812,92 @@ int32_t metaDropTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, S int32_t metaAlterTableColumnName(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) { int32_t code = TSDB_CODE_SUCCESS; - // TODO - return code; + + // check request + code = metaCheckAlterTableColumnReq(pMeta, version, pReq); + if (code) { + TAOS_RETURN(code); + } + + if (NULL == pReq->colNewName) { + metaError("vgId:%d, %s failed at %s:%d since invalid new column name, version:%" PRId64, TD_VID(pMeta->pVnode), + __func__, __FILE__, __LINE__, version); + TAOS_RETURN(TSDB_CODE_INVALID_MSG); + } + + // fetch old entry + SMetaEntry *pEntry = NULL; + code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry); + if (code) { + metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, + __FILE__, __LINE__, pReq->tbName, version); + TAOS_RETURN(code); + } + + if (pEntry->version >= version) { + metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64, + TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version); + metaFetchEntryFree(&pEntry); + TAOS_RETURN(TSDB_CODE_INVALID_PARA); + } + + // search the column to update + SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow; + SSchema *pColumn = NULL; + int32_t iColumn = 0; + for (int32_t i = 0; i < pSchema->nCols; i++) { + if (pSchema->pSchema[i].colId == pReq->colId) { + pColumn = &pSchema->pSchema[i]; + iColumn = i; + break; + } + } + + if (NULL == pColumn) { + metaError("vgId:%d, %s failed at %s:%d since column id %" PRId64 " not found in table %s, version:%" PRId64, + TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, pReq->tbName, version); + metaFetchEntryFree(&pEntry); + TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS); + } + + if (tqCheckColModifiable(pMeta->pVnode->pTq, pEntry->uid, pColumn->colId) != 0) { + metaError("vgId:%d, %s failed at %s:%d since column %s is not modifiable, version:%" PRId64, TD_VID(pMeta->pVnode), + __func__, __FILE__, __LINE__, pColumn->name, version); + metaFetchEntryFree(&pEntry); + TAOS_RETURN(TSDB_CODE_VND_COL_SUBSCRIBED); + } + + // do update column name + pEntry->version = version; + tstrncpy(pColumn->name, pReq->colNewName, TSDB_COL_NAME_LEN); + pSchema->version++; + + // do handle entry + code = metaHandleEntry2(pMeta, pEntry); + if (code) { + metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode), + __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version); + metaFetchEntryFree(&pEntry); + TAOS_RETURN(code); + } else { + metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName, + pEntry->uid, version); + } + + // build response + if (metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp) < 0) { + metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode), + __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version); + } else { + for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) { + SColCmpr *p = &pEntry->colCmpr.pColCmpr[i]; + pRsp->pSchemaExt[i].colId = p->id; + pRsp->pSchemaExt[i].compress = p->alg; + } + } + + metaFetchEntryFree(&pEntry); + TAOS_RETURN(code); } int32_t metaAlterTableColumnBytes(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {