feat: support alter table add column with comment
This commit is contained in:
parent
dcbef83dea
commit
cb027cc5c5
|
@ -2389,6 +2389,9 @@ typedef struct {
|
||||||
int8_t type;
|
int8_t type;
|
||||||
int8_t flags;
|
int8_t flags;
|
||||||
int32_t bytes;
|
int32_t bytes;
|
||||||
|
bool hasColComment;
|
||||||
|
char* colComment;
|
||||||
|
int32_t colCommentLen;
|
||||||
// TSDB_ALTER_TABLE_DROP_COLUMN
|
// TSDB_ALTER_TABLE_DROP_COLUMN
|
||||||
// TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES
|
// TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES
|
||||||
int8_t colModType;
|
int8_t colModType;
|
||||||
|
|
|
@ -214,6 +214,7 @@ typedef struct SAlterTableStmt {
|
||||||
char newColName[TSDB_COL_NAME_LEN];
|
char newColName[TSDB_COL_NAME_LEN];
|
||||||
STableOptions* pOptions;
|
STableOptions* pOptions;
|
||||||
SDataType dataType;
|
SDataType dataType;
|
||||||
|
char colComment[TSDB_COL_COMMENT_LEN];
|
||||||
SValueNode* pVal;
|
SValueNode* pVal;
|
||||||
} SAlterTableStmt;
|
} SAlterTableStmt;
|
||||||
|
|
||||||
|
|
|
@ -171,8 +171,7 @@ SNode* createDropTableClause(SAstCreateContext* pCxt, bool ignoreNotExists, SNod
|
||||||
SNode* createDropTableStmt(SAstCreateContext* pCxt, SNodeList* pTables);
|
SNode* createDropTableStmt(SAstCreateContext* pCxt, SNodeList* pTables);
|
||||||
SNode* createDropSuperTableStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pRealTable);
|
SNode* createDropSuperTableStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pRealTable);
|
||||||
SNode* createAlterTableModifyOptions(SAstCreateContext* pCxt, SNode* pRealTable, SNode* pOptions);
|
SNode* createAlterTableModifyOptions(SAstCreateContext* pCxt, SNode* pRealTable, SNode* pOptions);
|
||||||
SNode* createAlterTableAddModifyCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pColName,
|
SNode* createAlterTableAddModifyCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SNode* pColDefNode);
|
||||||
SDataType dataType);
|
|
||||||
SNode* createAlterTableDropCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pColName);
|
SNode* createAlterTableDropCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pColName);
|
||||||
SNode* createAlterTableRenameCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pOldColName,
|
SNode* createAlterTableRenameCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pOldColName,
|
||||||
SToken* pNewColName);
|
SToken* pNewColName);
|
||||||
|
|
|
@ -312,17 +312,17 @@ cmd ::= ALTER STABLE alter_table_clause(A).
|
||||||
|
|
||||||
alter_table_clause(A) ::= full_table_name(B) alter_table_options(C). { A = createAlterTableModifyOptions(pCxt, B, C); }
|
alter_table_clause(A) ::= full_table_name(B) alter_table_options(C). { A = createAlterTableModifyOptions(pCxt, B, C); }
|
||||||
alter_table_clause(A) ::=
|
alter_table_clause(A) ::=
|
||||||
full_table_name(B) ADD COLUMN column_name(C) type_name(D). { A = createAlterTableAddModifyCol(pCxt, B, TSDB_ALTER_TABLE_ADD_COLUMN, &C, D); }
|
full_table_name(B) ADD COLUMN column_def(C). { A = createAlterTableAddModifyCol(pCxt, B, TSDB_ALTER_TABLE_ADD_COLUMN, C); }
|
||||||
alter_table_clause(A) ::= full_table_name(B) DROP COLUMN column_name(C). { A = createAlterTableDropCol(pCxt, B, TSDB_ALTER_TABLE_DROP_COLUMN, &C); }
|
alter_table_clause(A) ::= full_table_name(B) DROP COLUMN column_name(C). { A = createAlterTableDropCol(pCxt, B, TSDB_ALTER_TABLE_DROP_COLUMN, &C); }
|
||||||
alter_table_clause(A) ::=
|
alter_table_clause(A) ::=
|
||||||
full_table_name(B) MODIFY COLUMN column_name(C) type_name(D). { A = createAlterTableAddModifyCol(pCxt, B, TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES, &C, D); }
|
full_table_name(B) MODIFY COLUMN column_def(C). { A = createAlterTableAddModifyCol(pCxt, B, TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES, C); }
|
||||||
alter_table_clause(A) ::=
|
alter_table_clause(A) ::=
|
||||||
full_table_name(B) RENAME COLUMN column_name(C) column_name(D). { A = createAlterTableRenameCol(pCxt, B, TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME, &C, &D); }
|
full_table_name(B) RENAME COLUMN column_name(C) column_name(D). { A = createAlterTableRenameCol(pCxt, B, TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME, &C, &D); }
|
||||||
alter_table_clause(A) ::=
|
alter_table_clause(A) ::=
|
||||||
full_table_name(B) ADD TAG column_name(C) type_name(D). { A = createAlterTableAddModifyCol(pCxt, B, TSDB_ALTER_TABLE_ADD_TAG, &C, D); }
|
full_table_name(B) ADD TAG column_def(C). { A = createAlterTableAddModifyCol(pCxt, B, TSDB_ALTER_TABLE_ADD_TAG, C); }
|
||||||
alter_table_clause(A) ::= full_table_name(B) DROP TAG column_name(C). { A = createAlterTableDropCol(pCxt, B, TSDB_ALTER_TABLE_DROP_TAG, &C); }
|
alter_table_clause(A) ::= full_table_name(B) DROP TAG column_name(C). { A = createAlterTableDropCol(pCxt, B, TSDB_ALTER_TABLE_DROP_TAG, &C); }
|
||||||
alter_table_clause(A) ::=
|
alter_table_clause(A) ::=
|
||||||
full_table_name(B) MODIFY TAG column_name(C) type_name(D). { A = createAlterTableAddModifyCol(pCxt, B, TSDB_ALTER_TABLE_UPDATE_TAG_BYTES, &C, D); }
|
full_table_name(B) MODIFY TAG column_def(C). { A = createAlterTableAddModifyCol(pCxt, B, TSDB_ALTER_TABLE_UPDATE_TAG_BYTES, C); }
|
||||||
alter_table_clause(A) ::=
|
alter_table_clause(A) ::=
|
||||||
full_table_name(B) RENAME TAG column_name(C) column_name(D). { A = createAlterTableRenameCol(pCxt, B, TSDB_ALTER_TABLE_UPDATE_TAG_NAME, &C, &D); }
|
full_table_name(B) RENAME TAG column_name(C) column_name(D). { A = createAlterTableRenameCol(pCxt, B, TSDB_ALTER_TABLE_UPDATE_TAG_NAME, &C, &D); }
|
||||||
alter_table_clause(A) ::=
|
alter_table_clause(A) ::=
|
||||||
|
|
|
@ -1333,17 +1333,15 @@ SNode* createAlterTableModifyOptions(SAstCreateContext* pCxt, SNode* pRealTable,
|
||||||
return createAlterTableStmtFinalize(pRealTable, pStmt);
|
return createAlterTableStmtFinalize(pRealTable, pStmt);
|
||||||
}
|
}
|
||||||
|
|
||||||
SNode* createAlterTableAddModifyCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pColName,
|
SNode* createAlterTableAddModifyCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SNode* pColDefNode) {
|
||||||
SDataType dataType) {
|
|
||||||
CHECK_PARSER_STATUS(pCxt);
|
CHECK_PARSER_STATUS(pCxt);
|
||||||
if (!checkColumnName(pCxt, pColName)) {
|
SColumnDefNode* pCol = (SColumnDefNode*)pColDefNode;
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
SAlterTableStmt* pStmt = (SAlterTableStmt*)nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT);
|
SAlterTableStmt* pStmt = (SAlterTableStmt*)nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT);
|
||||||
CHECK_OUT_OF_MEM(pStmt);
|
CHECK_OUT_OF_MEM(pStmt);
|
||||||
pStmt->alterType = alterType;
|
pStmt->alterType = alterType;
|
||||||
COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
|
strcpy(pStmt->colName, pCol->colName);
|
||||||
pStmt->dataType = dataType;
|
strcpy(pStmt->colComment, pCol->comments);
|
||||||
|
pStmt->dataType = pCol->dataType;
|
||||||
return createAlterTableStmtFinalize(pRealTable, pStmt);
|
return createAlterTableStmtFinalize(pRealTable, pStmt);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8863,6 +8863,15 @@ static int32_t buildAddColReq(STranslateContext* pCxt, SAlterTableStmt* pStmt, S
|
||||||
pReq->type = pStmt->dataType.type;
|
pReq->type = pStmt->dataType.type;
|
||||||
pReq->flags = COL_SMA_ON;
|
pReq->flags = COL_SMA_ON;
|
||||||
pReq->bytes = calcTypeBytes(pStmt->dataType);
|
pReq->bytes = calcTypeBytes(pStmt->dataType);
|
||||||
|
if (pStmt->colComment[0]) {
|
||||||
|
pReq->colComment = taosStrdup(pStmt->colComment);
|
||||||
|
if (pReq->colComment == NULL) {
|
||||||
|
return TSDB_CODE_OUT_OF_MEMORY;
|
||||||
|
}
|
||||||
|
pReq->colCommentLen = strlen(pReq->colComment);
|
||||||
|
} else {
|
||||||
|
pReq->colCommentLen = -1;
|
||||||
|
}
|
||||||
return TSDB_CODE_SUCCESS;
|
return TSDB_CODE_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8913,6 +8922,15 @@ static int32_t buildUpdateColReq(STranslateContext* pCxt, SAlterTableStmt* pStmt
|
||||||
return TSDB_CODE_OUT_OF_MEMORY;
|
return TSDB_CODE_OUT_OF_MEMORY;
|
||||||
}
|
}
|
||||||
pReq->colId = pSchema->colId;
|
pReq->colId = pSchema->colId;
|
||||||
|
if (pStmt->colComment[0]) {
|
||||||
|
pReq->colComment = taosStrdup(pStmt->colComment);
|
||||||
|
if (pReq->colComment == NULL) {
|
||||||
|
return TSDB_CODE_OUT_OF_MEMORY;
|
||||||
|
}
|
||||||
|
pReq->colCommentLen = strlen(pReq->colComment);
|
||||||
|
} else {
|
||||||
|
pReq->colCommentLen = -1;
|
||||||
|
}
|
||||||
|
|
||||||
return TSDB_CODE_SUCCESS;
|
return TSDB_CODE_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -215,12 +215,51 @@ class TDTestCase:
|
||||||
tdSql.execute("use comment_test_db")
|
tdSql.execute("use comment_test_db")
|
||||||
tdSql.execute("create table normal_table(ts timestamp, c2 int comment 'c2 comment')")
|
tdSql.execute("create table normal_table(ts timestamp, c2 int comment 'c2 comment')")
|
||||||
tdSql.execute("create stable super_table(ts timestamp comment 'ts', c2 int comment 'c2 comment') tags(tg int comment 'tg comment')")
|
tdSql.execute("create stable super_table(ts timestamp comment 'ts', c2 int comment 'c2 comment') tags(tg int comment 'tg comment')")
|
||||||
tdSql.query('show create table normal_table')
|
|
||||||
create_sql = "create table `normal_table` (`ts` timestamp, `c2` int)"
|
create_sql = "create table `normal_table` (`ts` timestamp, `c2` int)"
|
||||||
|
tdSql.query('show create table normal_table')
|
||||||
tdSql.checkEqual(tdSql.queryResult[0][1].lower(), create_sql)
|
tdSql.checkEqual(tdSql.queryResult[0][1].lower(), create_sql)
|
||||||
tdSql.query('show create table super_table')
|
tdSql.query('show create table super_table')
|
||||||
create_sql = "create stable `super_table` (`ts` timestamp, `c2` int) tags (`tg` int)"
|
create_sql = "create stable `super_table` (`ts` timestamp, `c2` int) tags (`tg` int)"
|
||||||
tdSql.checkEqual(tdSql.queryResult[0][1].lower(), create_sql)
|
tdSql.checkEqual(tdSql.queryResult[0][1].lower(), create_sql)
|
||||||
|
tdSql.execute("drop database comment_test_db")
|
||||||
|
|
||||||
|
def alter_table_with_col_comment(self):
|
||||||
|
tdSql.execute("create database comment_test_db")
|
||||||
|
tdSql.execute("use comment_test_db")
|
||||||
|
tdSql.execute("create table normal_table(ts timestamp, c2 int comment 'c2 comment')")
|
||||||
|
tdSql.execute("create stable super_table(ts timestamp comment 'ts', c2 int comment 'c2 comment') tags(tg int comment 'tg comment')")
|
||||||
|
|
||||||
|
create_sql = "create table `normal_table` (`ts` timestamp, `c2` int, `c3` int)"
|
||||||
|
tdSql.execute("alter table normal_table add column c3 int comment 'c3 comment'", queryTimes=1)
|
||||||
|
tdSql.query("show create table normal_table")
|
||||||
|
tdSql.checkEqual(tdSql.queryResult[0][1].lower(), create_sql)
|
||||||
|
|
||||||
|
create_sql = "create table `normal_table` (`ts` timestamp, `c2` int, `c3` int, `c4` varchar(255))"
|
||||||
|
tdSql.execute("alter table normal_table add column c4 varchar(255) comment 'c4 comment'", queryTimes=1)
|
||||||
|
tdSql.query("show create table normal_table")
|
||||||
|
tdSql.checkEqual(tdSql.queryResult[0][1].lower(), create_sql)
|
||||||
|
|
||||||
|
create_sql = "create table `normal_table` (`ts` timestamp, `c2` int, `c3` int, `c4` varchar(255), `c5` varchar(255))"
|
||||||
|
tdSql.execute("alter table normal_table add column c5 varchar(255)", queryTimes=1)
|
||||||
|
tdSql.query("show create table normal_table")
|
||||||
|
tdSql.checkEqual(tdSql.queryResult[0][1].lower(), create_sql)
|
||||||
|
|
||||||
|
create_sql = "create stable `super_table` (`ts` timestamp, `c2` int, `c3` int) tags (`tg` int) sma(`ts`,`c2`)"
|
||||||
|
tdSql.execute("alter table super_table add column c3 int comment 'c3 comment'", queryTimes=1)
|
||||||
|
tdSql.query("show create table super_table")
|
||||||
|
tdSql.checkEqual(tdSql.queryResult[0][1].lower(), create_sql)
|
||||||
|
|
||||||
|
create_sql = "create stable `super_table` (`ts` timestamp, `c2` int, `c3` int, `c4` varchar(255)) tags (`tg` int) sma(`ts`,`c2`)"
|
||||||
|
tdSql.execute("alter table super_table add column c4 varchar(255) comment 'c4 comment'", queryTimes=1)
|
||||||
|
tdSql.query("show create table super_table")
|
||||||
|
tdSql.checkEqual(tdSql.queryResult[0][1].lower(), create_sql)
|
||||||
|
|
||||||
|
create_sql = "create stable `super_table` (`ts` timestamp, `c2` int, `c3` int, `c4` varchar(256)) tags (`tg` int) sma(`ts`,`c2`)"
|
||||||
|
tdSql.execute("alter table super_table modify column c4 varchar(256) comment 'c4 256 comment'", queryTimes=1)
|
||||||
|
tdSql.query("show create table super_table")
|
||||||
|
tdSql.checkEqual(tdSql.queryResult[0][1].lower(), create_sql)
|
||||||
|
tdSql.execute("drop database comment_test_db")
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
self.check_gitinfo()
|
self.check_gitinfo()
|
||||||
|
@ -231,6 +270,7 @@ class TDTestCase:
|
||||||
self.show_create_sysdb_sql()
|
self.show_create_sysdb_sql()
|
||||||
self.show_create_systb_sql()
|
self.show_create_systb_sql()
|
||||||
self.show_create_table_with_col_comment()
|
self.show_create_table_with_col_comment()
|
||||||
|
self.alter_table_with_col_comment()
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
tdSql.close()
|
tdSql.close()
|
||||||
|
|
Loading…
Reference in New Issue