diff --git a/include/util/taoserror.h b/include/util/taoserror.h index ba66cfb06a..affa1f0345 100644 --- a/include/util/taoserror.h +++ b/include/util/taoserror.h @@ -765,6 +765,7 @@ int32_t* taosGetErrno(); #define TSDB_CODE_PAR_TAG_IS_PRIMARY_KEY TAOS_DEF_ERROR_CODE(0, 0x2671) #define TSDB_CODE_PAR_SECOND_COL_PK TAOS_DEF_ERROR_CODE(0, 0x2672) #define TSDB_CODE_PAR_COL_PK_TYPE TAOS_DEF_ERROR_CODE(0, 0x2673) +#define TSDB_CODE_PAR_INVALID_PK_OP TAOS_DEF_ERROR_CODE(0, 0x2674) #define TSDB_CODE_PAR_INTERNAL_ERROR TAOS_DEF_ERROR_CODE(0, 0x26FF) //planner diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index a3d64f59b0..86856e30d6 100644 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -927,7 +927,7 @@ static bool isPrimaryKey(STempTableNode* pTable, SNode* pExpr) { return isPrimaryKeyImpl(pExpr); } -static bool hasPkInTable(STableMeta* pTableMeta) { +static bool hasPkInTable(const STableMeta* pTableMeta) { return pTableMeta->tableInfo.numOfColumns>=2 && pTableMeta->schema[1].flags & COL_IS_KEY; } @@ -6766,7 +6766,13 @@ static int32_t checkAlterSuperTableBySchema(STranslateContext* pCxt, SAlterTable : TSDB_CODE_PAR_INVALID_TAG_NAME, pStmt->colName); } - + if (hasPkInTable(pTableMeta) && (pSchema->flags & COL_IS_KEY)) { + if (TSDB_ALTER_TABLE_DROP_COLUMN == pStmt->alterType || + TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES == pStmt->alterType || + TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME == pStmt->alterType) { + return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_PK_OP, pStmt->colName); + } + } if ((TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES == pStmt->alterType || TSDB_ALTER_TABLE_UPDATE_TAG_BYTES == pStmt->alterType) && (!IS_VAR_DATA_TYPE(pSchema->type) || pSchema->type != pStmt->dataType.type || @@ -10959,6 +10965,14 @@ static int32_t rewriteAlterTableImpl(STranslateContext* pCxt, SAlterTableStmt* p return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_ALTER_TABLE); } + const SSchema* pSchema = getNormalColSchema(pTableMeta, pStmt->colName); + if (hasPkInTable(pTableMeta) && pSchema && (pSchema->flags & COL_IS_KEY) && + (TSDB_ALTER_TABLE_DROP_COLUMN == pStmt->alterType || + TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES == pStmt->alterType || + TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME == pStmt->alterType)) { + return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_PK_OP); + } + SVAlterTbReq req = {0}; int32_t code = buildAlterTbReq(pCxt, pStmt, pTableMeta, &req); diff --git a/source/libs/parser/src/parUtil.c b/source/libs/parser/src/parUtil.c index 39f50da9fd..7c82555e63 100644 --- a/source/libs/parser/src/parUtil.c +++ b/source/libs/parser/src/parUtil.c @@ -199,7 +199,9 @@ static char* getSyntaxErrFormat(int32_t errCode) { case TSDB_CODE_PAR_SECOND_COL_PK: return "primary key column must be second column"; case TSDB_CODE_PAR_COL_PK_TYPE: - return "primary key column must be of type int, uint, bigint, ubigint, and varchar"; + return "primary key column must be of type int, uint, bigint, ubigint, and varchar"; + case TSDB_CODE_PAR_INVALID_PK_OP: + return "primary key column can not be added, modified, and dropped"; default: return "Unknown error"; } diff --git a/source/libs/planner/src/planLogicCreater.c b/source/libs/planner/src/planLogicCreater.c index c79fbd01f0..b5236fee9e 100644 --- a/source/libs/planner/src/planLogicCreater.c +++ b/source/libs/planner/src/planLogicCreater.c @@ -1696,6 +1696,11 @@ static int32_t createDeleteScanLogicNode(SLogicPlanContext* pCxt, SDeleteStmt* p } } + STableMeta* pMeta = ((SRealTableNode*)pDelete->pFromTable)->pMeta; + if (TSDB_CODE_SUCCESS == code && hasPkInTable(pMeta)) { + code = addPkCol(pMeta->uid, pMeta->schema + 1, &pScan->pScanCols, pMeta); + } + if (TSDB_CODE_SUCCESS == code && NULL != pDelete->pTagCond) { pScan->pTagCond = nodesCloneNode(pDelete->pTagCond); if (NULL == pScan->pTagCond) { diff --git a/source/util/src/terror.c b/source/util/src/terror.c index 6df9bf0e16..a64c8642db 100644 --- a/source/util/src/terror.c +++ b/source/util/src/terror.c @@ -627,6 +627,7 @@ TAOS_DEFINE_ERROR(TSDB_CODE_PAR_NOT_SUPPORT_MULTI_RESULT, "Operator not suppor TAOS_DEFINE_ERROR(TSDB_CODE_PAR_TAG_IS_PRIMARY_KEY, "tag can not be primary key") TAOS_DEFINE_ERROR(TSDB_CODE_PAR_SECOND_COL_PK, "primary key must be second column") TAOS_DEFINE_ERROR(TSDB_CODE_PAR_COL_PK_TYPE, "primary key column must be of type int, uint, bigint, ubigint, and varchar") +TAOS_DEFINE_ERROR(TSDB_CODE_PAR_INVALID_PK_OP, "primary key column can not be added, modified, and dropped") TAOS_DEFINE_ERROR(TSDB_CODE_PAR_INTERNAL_ERROR, "Parser internal error") //planner