diff --git a/src/client/src/tscServer.c b/src/client/src/tscServer.c index 21f6f9d39006e0e6572f4bc764343029e1e0c391..c04871471205a066e5c4f5fadda866823a7cac0c 100644 --- a/src/client/src/tscServer.c +++ b/src/client/src/tscServer.c @@ -1348,11 +1348,11 @@ int tscBuildAlterTableMsg(SSqlObj *pSql, SSqlInfo *pInfo) { strcpy(pAlterTableMsg->tableId, pTableMetaInfo->name); pAlterTableMsg->type = htons(pAlterInfo->type); - pAlterTableMsg->numOfCols = htons(tscNumOfFields(pQueryInfo)); + pAlterTableMsg->numOfCols = tscNumOfFields(pQueryInfo); memcpy(pAlterTableMsg->tagVal, pAlterInfo->tagData.data, TSDB_MAX_TAGS_LEN); SSchema *pSchema = pAlterTableMsg->schema; - for (int i = 0; i < tscNumOfFields(pQueryInfo); ++i) { + for (int i = 0; i < pAlterTableMsg->numOfCols; ++i) { TAOS_FIELD *pField = tscFieldInfoGetField(&pQueryInfo->fieldsInfo, i); pSchema->type = pField->type; diff --git a/src/inc/taoserror.h b/src/inc/taoserror.h index 5084199ac83bae38451e265067a757a3039a6685..c9071f4cb0c14e689583b25c1c7e2939ec4a6fb6 100644 --- a/src/inc/taoserror.h +++ b/src/inc/taoserror.h @@ -97,6 +97,13 @@ TAOS_DEFINE_ERROR(TSDB_CODE_INVALID_TABLE, 0, 203, "invalid table n TAOS_DEFINE_ERROR(TSDB_CODE_NOT_SUPER_TABLE, 0, 204, "no super table") // operation only available for super table TAOS_DEFINE_ERROR(TSDB_CODE_NOT_ACTIVE_TABLE, 0, 205, "not active table") TAOS_DEFINE_ERROR(TSDB_CODE_TABLE_ID_MISMATCH, 0, 206, "table id mismatch") +TAOS_DEFINE_ERROR(TSDB_CODE_TAG_ALREAY_EXIST, 0, 207, "tag already exist") +TAOS_DEFINE_ERROR(TSDB_CODE_TAG_NOT_EXIST, 0, 208, "tag not exist") +TAOS_DEFINE_ERROR(TSDB_CODE_FIELD_ALREAY_EXIST, 0, 209, "field already exist") +TAOS_DEFINE_ERROR(TSDB_CODE_FIELD_NOT_EXIST, 0, 210, "field not exist") +TAOS_DEFINE_ERROR(TSDB_CODE_COL_NAME_TOO_LONG, 0, 211, "column name too long") +TAOS_DEFINE_ERROR(TSDB_CODE_TOO_MANY_TAGS, 0, 211, "too many tags") + // dnode & mnode TAOS_DEFINE_ERROR(TSDB_CODE_NO_ENOUGH_DNODES, 0, 250, "no enough dnodes") diff --git a/src/inc/taosmsg.h b/src/inc/taosmsg.h index 16e147c5affc293bd848ae0d877609b3fd519064..8e6e762017ec972c127a3fa399fe668bf128e0fc 100644 --- a/src/inc/taosmsg.h +++ b/src/inc/taosmsg.h @@ -238,7 +238,7 @@ typedef struct { typedef struct SSchema { uint8_t type; - char name[TSDB_COL_NAME_LEN]; + char name[TSDB_COL_NAME_LEN + 1]; int16_t colId; int16_t bytes; } SSchema; @@ -256,14 +256,14 @@ typedef struct { uint64_t uid; uint64_t superTableUid; uint64_t createdTime; - char tableId[TSDB_TABLE_ID_LEN]; - char superTableId[TSDB_TABLE_ID_LEN]; + char tableId[TSDB_TABLE_ID_LEN + 1]; + char superTableId[TSDB_TABLE_ID_LEN + 1]; char data[]; } SMDCreateTableMsg; typedef struct { - char tableId[TSDB_TABLE_ID_LEN]; - char db[TSDB_DB_NAME_LEN]; + char tableId[TSDB_TABLE_ID_LEN + 1]; + char db[TSDB_DB_NAME_LEN + 1]; int8_t igExists; int16_t numOfTags; int16_t numOfColumns; @@ -274,13 +274,13 @@ typedef struct { } SCMCreateTableMsg; typedef struct { - char tableId[TSDB_TABLE_ID_LEN]; + char tableId[TSDB_TABLE_ID_LEN + 1]; int8_t igNotExists; } SCMDropTableMsg; typedef struct { - char tableId[TSDB_TABLE_ID_LEN]; - char db[TSDB_DB_NAME_LEN]; + char tableId[TSDB_TABLE_ID_LEN + 1]; + char db[TSDB_DB_NAME_LEN + 1]; int16_t type; /* operation type */ char tagVal[TSDB_MAX_BYTES_PER_ROW]; int8_t numOfCols; /* number of schema */ @@ -515,8 +515,8 @@ typedef struct { } SVnodeLoad; typedef struct { - char acct[TSDB_USER_LEN]; - char db[TSDB_DB_NAME_LEN]; + char acct[TSDB_USER_LEN + 1]; + char db[TSDB_DB_NAME_LEN + 1]; uint32_t vgId; int32_t maxSessions; int32_t cacheBlockSize; @@ -713,8 +713,8 @@ typedef struct { typedef struct STableMetaMsg { int32_t contLen; - char tableId[TSDB_TABLE_ID_LEN]; // table id - char stableId[TSDB_TABLE_ID_LEN]; // stable name if it is created according to super table + char tableId[TSDB_TABLE_ID_LEN + 1]; // table id + char stableId[TSDB_TABLE_ID_LEN + 1]; // stable name if it is created according to super table uint8_t numOfTags; uint8_t precision; uint8_t tableType; diff --git a/src/mnode/src/mgmtTable.c b/src/mnode/src/mgmtTable.c index 1813be6522c1791e96746da7620334ff9bc83729..57e905361c8345c08520bf1eb21666672091fb34 100644 --- a/src/mnode/src/mgmtTable.c +++ b/src/mnode/src/mgmtTable.c @@ -78,6 +78,8 @@ static void mgmtGetChildTableMeta(SQueuedMsg *pMsg); static void mgmtProcessAlterTableMsg(SQueuedMsg *queueMsg); static void mgmtProcessAlterTableRsp(SRpcMsg *rpcMsg); +static int32_t mgmtFindSuperTableColumnIndex(SSuperTableObj *pStable, char *colName); + static void mgmtDestroyChildTable(SChildTableObj *pTable) { tfree(pTable->schema); tfree(pTable->sql); @@ -757,11 +759,11 @@ static void mgmtProcessDropSuperTableMsg(SQueuedMsg *pMsg) { SSuperTableObj *pStable = (SSuperTableObj *)pMsg->pTable; if (pStable->numOfTables != 0) { mgmtDropAllChildTablesInStable(pStable); - for (int32_t vg = 0; vg < pTable->vgLen; ++vg) { - int32_t vgId = pTable->vgList[vg]; + for (int32_t vg = 0; vg < pStable->vgLen; ++vg) { + int32_t vgId = pStable->vgList[vg]; if (vgId == 0) break; - SMDDropSTableMsg *pDrop = rpcMalloc(sizeof(SMDDropSTableMsg)); + SMDDropSTableMsg *pDrop = rpcMallocCont(sizeof(SMDDropSTableMsg)); pDrop->vgId = htonl(vgId); pDrop->uid = htobe64(pStable->uid); mgmtExtractTableName(pStable->info.tableId, pDrop->tableId); @@ -801,31 +803,33 @@ static int32_t mgmtFindSuperTableTagIndex(SSuperTableObj *pStable, const char *t static int32_t mgmtAddSuperTableTag(SSuperTableObj *pStable, SSchema schema[], int32_t ntags) { if (pStable->numOfTags + ntags > TSDB_MAX_TAGS) { - return TSDB_CODE_APP_ERROR; + mError("stable:%s, add tag, too many tags", pStable->info.tableId); + return TSDB_CODE_TOO_MANY_TAGS; } - // check if schemas have the same name - for (int32_t i = 1; i < ntags; i++) { - for (int32_t j = 0; j < i; j++) { - if (strcasecmp(schema[i].name, schema[j].name) == 0) { - return TSDB_CODE_APP_ERROR; - } + for (int32_t i = 0; i < ntags; i++) { + if (mgmtFindSuperTableColumnIndex(pStable, schema[i].name) > 0) { + mError("stable:%s, add tag, column:%s already exist", pStable->info.tableId, schema[i].name); + return TSDB_CODE_TAG_ALREAY_EXIST; + } + + if (mgmtFindSuperTableTagIndex(pStable, schema[i].name) > 0) { + mError("stable:%s, add tag, tag:%s already exist", pStable->info.tableId, schema[i].name); + return TSDB_CODE_FIELD_ALREAY_EXIST; } } int32_t schemaSize = sizeof(SSchema) * (pStable->numOfTags + pStable->numOfColumns); pStable->schema = realloc(pStable->schema, schemaSize + sizeof(SSchema) * ntags); - memmove(pStable->schema + sizeof(SSchema) * (pStable->numOfColumns + ntags), - pStable->schema + sizeof(SSchema) * pStable->numOfColumns, sizeof(SSchema) * pStable->numOfTags); - memcpy(pStable->schema + sizeof(SSchema) * pStable->numOfColumns, schema, sizeof(SSchema) * ntags); + memcpy(pStable->schema + pStable->numOfColumns + pStable->numOfTags, schema, sizeof(SSchema) * ntags); - SSchema *tschema = (SSchema *) (pStable->schema + sizeof(SSchema) * pStable->numOfColumns); + SSchema *tschema = (SSchema *)(pStable->schema + pStable->numOfColumns + pStable->numOfTags); for (int32_t i = 0; i < ntags; i++) { tschema[i].colId = pStable->nextColId++; } - pStable->numOfColumns += ntags; + pStable->numOfTags += ntags; pStable->sversion++; SSdbOper oper = { @@ -840,25 +844,22 @@ static int32_t mgmtAddSuperTableTag(SSuperTableObj *pStable, SSchema schema[], i return TSDB_CODE_SDB_ERROR; } - mPrint("table %s, succeed to add tag %s", pStable->info.tableId, schema[0].name); + mPrint("stable %s, succeed to add tag %s", pStable->info.tableId, schema[0].name); return TSDB_CODE_SUCCESS; } static int32_t mgmtDropSuperTableTag(SSuperTableObj *pStable, char *tagName) { int32_t col = mgmtFindSuperTableTagIndex(pStable, tagName); - if (col <= 0 || col >= pStable->numOfTags) { - return TSDB_CODE_APP_ERROR; + if (col < 0) { + mError("stable:%s, drop tag, tag:%s not exist", pStable->info.tableId, tagName); + return TSDB_CODE_TAG_NOT_EXIST; } - memmove(pStable->schema + sizeof(SSchema) * col, pStable->schema + sizeof(SSchema) * (col + 1), - sizeof(SSchema) * (pStable->numOfColumns + pStable->numOfTags - col - 1)); - + memmove(pStable->schema + pStable->numOfColumns + col, pStable->schema + pStable->numOfColumns + col + 1, + sizeof(SSchema) * (pStable->numOfTags - col - 1)); pStable->numOfTags--; pStable->sversion++; - int32_t schemaSize = sizeof(SSchema) * (pStable->numOfTags + pStable->numOfColumns); - pStable->schema = realloc(pStable->schema, schemaSize); - SSdbOper oper = { .type = SDB_OPER_GLOBAL, .table = tsSuperTableSdb, @@ -871,27 +872,29 @@ static int32_t mgmtDropSuperTableTag(SSuperTableObj *pStable, char *tagName) { return TSDB_CODE_SDB_ERROR; } - mPrint("table %s, succeed to drop tag %s", pStable->info.tableId, tagName); + mPrint("stable %s, succeed to drop tag %s", pStable->info.tableId, tagName); return TSDB_CODE_SUCCESS; } static int32_t mgmtModifySuperTableTagName(SSuperTableObj *pStable, char *oldTagName, char *newTagName) { int32_t col = mgmtFindSuperTableTagIndex(pStable, oldTagName); if (col < 0) { - // Tag name does not exist - mError("table:%s, failed to modify table tag, oldName: %s, newName: %s", pStable->info.tableId, oldTagName, newTagName); - return TSDB_CODE_INVALID_MSG_TYPE; + mError("stable:%s, failed to modify table tag, oldName: %s, newName: %s", pStable->info.tableId, oldTagName, newTagName); + return TSDB_CODE_TAG_NOT_EXIST; } // int32_t rowSize = 0; uint32_t len = strlen(newTagName); + if (len >= TSDB_COL_NAME_LEN) { + return TSDB_CODE_COL_NAME_TOO_LONG; + } - if (col >= pStable->numOfTags || len >= TSDB_COL_NAME_LEN || mgmtFindSuperTableTagIndex(pStable, newTagName) >= 0) { - return TSDB_CODE_APP_ERROR; + if (mgmtFindSuperTableTagIndex(pStable, newTagName) >= 0) { + return TSDB_CODE_TAG_ALREAY_EXIST; } // update - SSchema *schema = (SSchema *) (pStable->schema + (pStable->numOfColumns + col) * sizeof(SSchema)); + SSchema *schema = (SSchema *) (pStable->schema + pStable->numOfColumns + col); strncpy(schema->name, newTagName, TSDB_COL_NAME_LEN); SSdbOper oper = { @@ -906,15 +909,15 @@ static int32_t mgmtModifySuperTableTagName(SSuperTableObj *pStable, char *oldTag return TSDB_CODE_SDB_ERROR; } - mPrint("table %s, succeed to modify tag %s to %s", pStable->info.tableId, oldTagName, newTagName); + mPrint("stable %s, succeed to modify tag %s to %s", pStable->info.tableId, oldTagName, newTagName); return TSDB_CODE_SUCCESS; } static int32_t mgmtFindSuperTableColumnIndex(SSuperTableObj *pStable, char *colName) { SSchema *schema = (SSchema *) pStable->schema; - for (int32_t i = 0; i < pStable->numOfColumns; i++) { - if (strcasecmp(schema[i].name, colName) == 0) { - return i; + for (int32_t col = 0; col < pStable->numOfColumns; col++) { + if (strcasecmp(schema[col].name, colName) == 0) { + return col; } } @@ -923,21 +926,28 @@ static int32_t mgmtFindSuperTableColumnIndex(SSuperTableObj *pStable, char *colN static int32_t mgmtAddSuperTableColumn(SDbObj *pDb, SSuperTableObj *pStable, SSchema schema[], int32_t ncols) { if (ncols <= 0) { + mError("stable:%s, add column, ncols:%d <= 0", pStable->info.tableId); return TSDB_CODE_APP_ERROR; } for (int32_t i = 0; i < ncols; i++) { if (mgmtFindSuperTableColumnIndex(pStable, schema[i].name) > 0) { - return TSDB_CODE_APP_ERROR; + mError("stable:%s, add column, column:%s already exist", pStable->info.tableId, schema[i].name); + return TSDB_CODE_FIELD_ALREAY_EXIST; + } + + if (mgmtFindSuperTableTagIndex(pStable, schema[i].name) > 0) { + mError("stable:%s, add column, tag:%s already exist", pStable->info.tableId, schema[i].name); + return TSDB_CODE_TAG_ALREAY_EXIST; } } int32_t schemaSize = sizeof(SSchema) * (pStable->numOfTags + pStable->numOfColumns); pStable->schema = realloc(pStable->schema, schemaSize + sizeof(SSchema) * ncols); - memmove(pStable->schema + sizeof(SSchema) * (pStable->numOfColumns + ncols), - pStable->schema + sizeof(SSchema) * pStable->numOfColumns, sizeof(SSchema) * pStable->numOfTags); - memcpy(pStable->schema + sizeof(SSchema) * pStable->numOfColumns, schema, sizeof(SSchema) * ncols); + memmove(pStable->schema + pStable->numOfColumns + ncols, pStable->schema + pStable->numOfColumns, + sizeof(SSchema) * pStable->numOfTags); + memcpy(pStable->schema + pStable->numOfColumns, schema, sizeof(SSchema) * ncols); SSchema *tschema = (SSchema *) (pStable->schema + sizeof(SSchema) * pStable->numOfColumns); for (int32_t i = 0; i < ncols; i++) { @@ -965,17 +975,18 @@ static int32_t mgmtAddSuperTableColumn(SDbObj *pDb, SSuperTableObj *pStable, SSc return TSDB_CODE_SDB_ERROR; } - mPrint("table %s, succeed to add column", pStable->info.tableId); + mPrint("stable %s, succeed to add column", pStable->info.tableId); return TSDB_CODE_SUCCESS; } static int32_t mgmtDropSuperTableColumn(SDbObj *pDb, SSuperTableObj *pStable, char *colName) { int32_t col = mgmtFindSuperTableColumnIndex(pStable, colName); - if (col < 0) { - return TSDB_CODE_APP_ERROR; + if (col <= 0) { + mError("stable:%s, drop column, column:%s not exist", pStable->info.tableId, colName); + return TSDB_CODE_FIELD_NOT_EXIST; } - memmove(pStable->schema + sizeof(SSchema) * col, pStable->schema + sizeof(SSchema) * (col + 1), + memmove(pStable->schema + col, pStable->schema + col + 1, sizeof(SSchema) * (pStable->numOfColumns + pStable->numOfTags - col - 1)); pStable->numOfColumns--; @@ -1002,7 +1013,7 @@ static int32_t mgmtDropSuperTableColumn(SDbObj *pDb, SSuperTableObj *pStable, ch return TSDB_CODE_SDB_ERROR; } - mPrint("table %s, succeed to delete column", pStable->info.tableId); + mPrint("stable %s, succeed to delete column", pStable->info.tableId); return TSDB_CODE_SUCCESS; } @@ -1490,9 +1501,9 @@ static int32_t mgmtModifyChildTableTagValue(SChildTableObj *pTable, char *tagNam static int32_t mgmtFindNormalTableColumnIndex(SChildTableObj *pTable, char *colName) { SSchema *schema = (SSchema *) pTable->schema; - for (int32_t i = 0; i < pTable->numOfColumns; i++) { - if (strcasecmp(schema[i].name, colName) == 0) { - return i; + for (int32_t col = 0; col < pTable->numOfColumns; col++) { + if (strcasecmp(schema[col].name, colName) == 0) { + return col; } } @@ -1501,21 +1512,23 @@ static int32_t mgmtFindNormalTableColumnIndex(SChildTableObj *pTable, char *colN static int32_t mgmtAddNormalTableColumn(SDbObj *pDb, SChildTableObj *pTable, SSchema schema[], int32_t ncols) { if (ncols <= 0) { + mError("table:%s, add column, ncols:%d <= 0", pTable->info.tableId); return TSDB_CODE_APP_ERROR; } for (int32_t i = 0; i < ncols; i++) { if (mgmtFindNormalTableColumnIndex(pTable, schema[i].name) > 0) { - return TSDB_CODE_APP_ERROR; + mError("table:%s, add column, column:%s already exist", pTable->info.tableId, schema[i].name); + return TSDB_CODE_FIELD_ALREAY_EXIST; } } int32_t schemaSize = pTable->numOfColumns * sizeof(SSchema); pTable->schema = realloc(pTable->schema, schemaSize + sizeof(SSchema) * ncols); - memcpy(pTable->schema + schemaSize, schema, sizeof(SSchema) * ncols); + memcpy(pTable->schema + pTable->numOfColumns, schema, sizeof(SSchema) * ncols); - SSchema *tschema = (SSchema *) (pTable->schema + sizeof(SSchema) * pTable->numOfColumns); + SSchema *tschema = (SSchema *) (pTable->schema + pTable->numOfColumns); for (int32_t i = 0; i < ncols; i++) { tschema[i].colId = pTable->nextColId++; } @@ -1525,7 +1538,7 @@ static int32_t mgmtAddNormalTableColumn(SDbObj *pDb, SChildTableObj *pTable, SSc SAcctObj *pAcct = mgmtGetAcct(pDb->acct); if (pAcct != NULL) { - pAcct->acctInfo.numOfTimeSeries += ncols; + pAcct->acctInfo.numOfTimeSeries += ncols; mgmtDecAcctRef(pAcct); } @@ -1547,13 +1560,12 @@ static int32_t mgmtAddNormalTableColumn(SDbObj *pDb, SChildTableObj *pTable, SSc static int32_t mgmtDropNormalTableColumn(SDbObj *pDb, SChildTableObj *pTable, char *colName) { int32_t col = mgmtFindNormalTableColumnIndex(pTable, colName); - if (col < 0) { - return TSDB_CODE_APP_ERROR; + if (col <= 0) { + mError("table:%s, drop column, column:%s not exist", pTable->info.tableId, colName); + return TSDB_CODE_FIELD_NOT_EXIST; } - memmove(pTable->schema + sizeof(SSchema) * col, pTable->schema + sizeof(SSchema) * (col + 1), - sizeof(SSchema) * (pTable->numOfColumns - col - 1)); - + memmove(pTable->schema + col, pTable->schema + col + 1, sizeof(SSchema) * (pTable->numOfColumns - col - 1)); pTable->numOfColumns--; pTable->sversion++; @@ -1575,7 +1587,7 @@ static int32_t mgmtDropNormalTableColumn(SDbObj *pDb, SChildTableObj *pTable, ch return TSDB_CODE_SDB_ERROR; } - mPrint("table %s, succeed to add column %s", pTable->info.tableId, colName); + mPrint("table %s, succeed to drop column %s", pTable->info.tableId, colName); return TSDB_CODE_SUCCESS; } @@ -2096,7 +2108,8 @@ static void mgmtProcessAlterTableMsg(SQueuedMsg *pMsg) { return; } - pAlter->numOfCols = htons(pAlter->numOfCols); + pAlter->type = htons(pAlter->type); + if (pAlter->numOfCols > 2) { mError("table:%s, error numOfCols:%d in alter table", pAlter->tableId, pAlter->numOfCols); mgmtSendSimpleResp(pMsg->thandle, TSDB_CODE_APP_ERROR);