diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index a15d3675ae..26b96da4ce 100755 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -7550,6 +7550,11 @@ static int32_t translateDeleteWhere(STranslateContext* pCxt, SDeleteStmt* pDelet } static int32_t translateDelete(STranslateContext* pCxt, SDeleteStmt* pDelete) { + const char* dbName = ((STableNode*)pDelete->pFromTable)->dbName; + if (IS_SYS_DBNAME(dbName)) { + return generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_TSC_INVALID_OPERATION, + "Cannot delete from system database: `%s`", dbName); + } pCxt->pCurrStmt = (SNode*)pDelete; int32_t code = translateFrom(pCxt, &pDelete->pFromTable); if (TSDB_CODE_SUCCESS == code) { @@ -8405,6 +8410,10 @@ static int32_t checkCreateDatabase(STranslateContext* pCxt, SCreateDatabaseStmt* return generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, "The database name cannot contain '.'"); } + if (IS_SYS_DBNAME(pStmt->dbName)) { + return generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_TSC_INVALID_OPERATION, + "Cannot create system database: `%s`", pStmt->dbName); + } return checkDatabaseOptions(pCxt, pStmt->dbName, pStmt->pOptions); } @@ -8594,9 +8603,13 @@ static int32_t translateCreateDatabase(STranslateContext* pCxt, SCreateDatabaseS } static int32_t translateDropDatabase(STranslateContext* pCxt, SDropDatabaseStmt* pStmt) { + if (IS_SYS_DBNAME(pStmt->dbName)) { + return generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_TSC_INVALID_OPERATION, "Cannot drop system database: `%s`", + pStmt->dbName); + } SDropDbReq dropReq = {0}; SName name = {0}; - int32_t code = tNameSetDbName(&name, pCxt->pParseCxt->acctId, pStmt->dbName, strlen(pStmt->dbName)); + int32_t code = tNameSetDbName(&name, pCxt->pParseCxt->acctId, pStmt->dbName, strlen(pStmt->dbName)); if (TSDB_CODE_SUCCESS != code) return code; (void)tNameGetFullDbName(&name, dropReq.db); dropReq.ignoreNotExists = pStmt->ignoreNotExists; @@ -8641,6 +8654,10 @@ static int32_t buildAlterDbReq(STranslateContext* pCxt, SAlterDatabaseStmt* pStm } static int32_t translateAlterDatabase(STranslateContext* pCxt, SAlterDatabaseStmt* pStmt) { + if (IS_SYS_DBNAME(pStmt->dbName)) { + return generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_TSC_INVALID_OPERATION, "Cannot alter system database: `%s`", + pStmt->dbName); + } if (pStmt->pOptions->walLevel == 0) { TAOS_CHECK_RETURN(translateGetDbCfg(pCxt, pStmt->dbName, &pStmt->pOptions->pDbCfg)); if (pStmt->pOptions->pDbCfg->replications > 1) { @@ -9870,6 +9887,11 @@ static int32_t checkAlterSuperTableBySchema(STranslateContext* pCxt, SAlterTable } static int32_t checkAlterSuperTable(STranslateContext* pCxt, SAlterTableStmt* pStmt) { + if (IS_SYS_DBNAME(pStmt->dbName)) { + return generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_TSC_INVALID_OPERATION, + "Cannot alter table of system database: `%s`.`%s`", pStmt->dbName, pStmt->tableName); + } + if (TSDB_ALTER_TABLE_UPDATE_TAG_VAL == pStmt->alterType || TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL == pStmt->alterType) { return generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_ALTER_TABLE, @@ -15549,21 +15571,11 @@ int32_t serializeVgroupsDropTableBatch(SHashObj* pVgroupHashmap, SArray** pOut) static int32_t rewriteDropTableWithOpt(STranslateContext* pCxt, SQuery* pQuery) { int32_t code = TSDB_CODE_SUCCESS; SDropTableStmt* pStmt = (SDropTableStmt*)pQuery->pRoot; + if (!pStmt->withOpt) return code; + pCxt->withOpt = true; + SNode* pNode = NULL; char pTableName[TSDB_TABLE_NAME_LEN] = {0}; - - FOREACH(pNode, pStmt->pTables) { - SDropTableClause* pClause = (SDropTableClause*)pNode; - if (IS_SYS_DBNAME(pClause->dbName)) { - return generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_TSC_INVALID_OPERATION, - "Cannot drop table of system database: `%s`.`%s`", pClause->dbName, - pClause->tableName); - } - } - - if (!pStmt->withOpt) return code; - - pCxt->withOpt = true; FOREACH(pNode, pStmt->pTables) { SDropTableClause* pClause = (SDropTableClause*)pNode; for (int32_t i = 0; i < TSDB_TABLE_NAME_LEN; i++) { @@ -15596,6 +15608,15 @@ static int32_t rewriteDropTable(STranslateContext* pCxt, SQuery* pQuery) { SNode* pNode; SArray* pTsmas = NULL; + FOREACH(pNode, pStmt->pTables) { + SDropTableClause* pClause = (SDropTableClause*)pNode; + if (IS_SYS_DBNAME(pClause->dbName)) { + return generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_TSC_INVALID_OPERATION, + "Cannot drop table of system database: `%s`.`%s`", pClause->dbName, + pClause->tableName); + } + } + TAOS_CHECK_RETURN(rewriteDropTableWithOpt(pCxt, pQuery)); SHashObj* pVgroupHashmap = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_NO_LOCK); @@ -15685,11 +15706,6 @@ static int32_t rewriteDropSuperTablewithOpt(STranslateContext* pCxt, SQuery* pQu if (!pStmt->withOpt) return code; pCxt->withOpt = true; - if (IS_SYS_DBNAME(pStmt->dbName)) { - return generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_TSC_INVALID_OPERATION, - "Cannot drop table of system database: `%s`.`%s`", pStmt->dbName, pStmt->tableName); - } - for (int32_t i = 0; i < TSDB_TABLE_NAME_LEN; i++) { if (pStmt->tableName[i] == '\0') { break; @@ -15719,6 +15735,11 @@ static int32_t rewriteDropSuperTablewithOpt(STranslateContext* pCxt, SQuery* pQu } static int32_t rewriteDropSuperTable(STranslateContext* pCxt, SQuery* pQuery) { + SDropSuperTableStmt* pStmt = (SDropSuperTableStmt*)pQuery->pRoot; + if (IS_SYS_DBNAME(pStmt->dbName)) { + return generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_TSC_INVALID_OPERATION, + "Cannot drop table of system database: `%s`.`%s`", pStmt->dbName, pStmt->tableName); + } TAOS_CHECK_RETURN(rewriteDropSuperTablewithOpt(pCxt, pQuery)); TAOS_RETURN(0); } @@ -16272,6 +16293,11 @@ static int32_t rewriteAlterTableImpl(STranslateContext* pCxt, SAlterTableStmt* p static int32_t rewriteAlterTable(STranslateContext* pCxt, SQuery* pQuery) { SAlterTableStmt* pStmt = (SAlterTableStmt*)pQuery->pRoot; + if (IS_SYS_DBNAME(pStmt->dbName)) { + return generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_TSC_INVALID_OPERATION, + "Cannot alter table of system database: `%s`.`%s`", pStmt->dbName, pStmt->tableName); + } + if (pStmt->dataType.type == TSDB_DATA_TYPE_JSON && pStmt->alterType == TSDB_ALTER_TABLE_ADD_COLUMN) { return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_COL_JSON); } diff --git a/tests/parallel_test/cases.task b/tests/parallel_test/cases.task index c4ebb8d78b..825e5037ab 100644 --- a/tests/parallel_test/cases.task +++ b/tests/parallel_test/cases.task @@ -733,7 +733,6 @@ ,,y,system-test,./pytest.sh python3 ./test.py -f 1-insert/tb_100w_data_order.py ,,y,system-test,./pytest.sh python3 ./test.py -f 1-insert/delete_childtable.py ,,y,system-test,./pytest.sh python3 ./test.py -f 1-insert/delete_normaltable.py -,,y,system-test,./pytest.sh python3 ./test.py -f 1-insert/delete_systable.py ,,y,system-test,./pytest.sh python3 ./test.py -f 1-insert/keep_expired.py ,,y,system-test,./pytest.sh python3 ./test.py -f 1-insert/stmt_error.py ,,y,system-test,./pytest.sh python3 ./test.py -f 1-insert/drop.py diff --git a/tests/system-test/1-insert/ddl_in_sysdb.py b/tests/system-test/1-insert/ddl_in_sysdb.py index d2707c0702..4065f593f5 100644 --- a/tests/system-test/1-insert/ddl_in_sysdb.py +++ b/tests/system-test/1-insert/ddl_in_sysdb.py @@ -1,43 +1,87 @@ -import taos -import sys -import datetime -import inspect -import random -from util.dnodes import TDDnode -from util.dnodes import tdDnodes +################################################################### +# Copyright (c) 2016 by TAOS Technologies, Inc. +# All rights reserved. +# +# This file is proprietary and confidential to TAOS Technologies. +# No part of this file may be reproduced, stored, transmitted, +# disclosed or used in any form or by any means other than as +# expressly provided by the written permission from Jianhui Tao +# +################################################################### + +# -*- coding: utf-8 -*- + +import random +import string + +from numpy import logspace +from util import constant from util.log import * -from util.sql import * from util.cases import * +from util.sql import * +from util.common import * +from util.sqlset import TDSetSql + +sysdb_tables = { + "information_schema": ["ins_dnodes", "ins_mnodes", "ins_modules", "ins_qnodes", "ins_snodes", "ins_cluster", "ins_databases", "ins_functions", "ins_indexes", "ins_stables", "ins_tables", "ins_tags", "ins_columns", "ins_users", "ins_grants", "ins_vgroups", "ins_configs", "ins_dnode_variables", "ins_topics", "ins_subscriptions", "ins_streams", "ins_streams_tasks", "ins_vnodes", "ins_user_privileges", "undefined"], + "performance_schema": ["perf_connections", "perf_queries", "perf_consumers", "perf_trans", "perf_apps", "undefined"] + } class TDTestCase: def init(self, conn, logSql, replicaVar=1): self.replicaVar = int(replicaVar) - tdLog.debug(f"start to excute {__file__}") - tdSql.init(conn.cursor(), True) + tdLog.debug("start to execute %s" % __file__) + tdSql.init(conn.cursor()) - self.testcasePath = os.path.split(__file__)[0] - self.testcasePath = self.testcasePath.replace('\\', '//') - self.sysdb = "information_schema" + def ddl_sysdb(self): + for db, _ in sysdb_tables.items(): + tdSql.error(f'create database {db}', expectErrInfo="Cannot create system database", fullMatched=False) + tdSql.error(f'create database {db} vgroups 10', expectErrInfo="Cannot create system database", fullMatched=False) + tdSql.error(f'alter database {db} wal_level 0', expectErrInfo="Cannot alter system database", fullMatched=False) + tdSql.error(f'alter database {db} cachemodel \'none\'', expectErrInfo="Cannot alter system database", fullMatched=False) + tdSql.error(f'drop database {db}', expectErrInfo="Cannot drop system database", fullMatched=False) + tdSql.error(f'drop database {db}', expectErrInfo="Cannot drop system database", fullMatched=False) - def test_ddl(self): - tdSql.execute(f"use {self.sysdb}") - tdSql.error(f'create stable ins_test (ts timestamp, a int) tags (b int);', expectErrInfo="Cannot create table of system database", fullMatched=False) - tdSql.error(f'create table ins_test (ts timestamp, a int);', expectErrInfo="Cannot create table of system database", fullMatched=False) - tdSql.error(f'drop table ins_users;', expectErrInfo="Cannot drop table of system database", fullMatched=False) + def ddl_systb(self): + for db, tbs in sysdb_tables.items(): + tdSql.execute(f'use {db}') + for tb in tbs: + tdSql.error(f'create table {tb} (ts timestamp, c0 int)', expectErrInfo="Cannot create table of system database", fullMatched=False) + tdSql.error(f'create table {db}.{tb} (ts timestamp, c0 int)', expectErrInfo="Cannot create table of system database", fullMatched=False) + tdSql.error(f'create stable {tb} (ts timestamp, c0 int) tags(t0 int)', expectErrInfo="Cannot create table of system database", fullMatched=False) + tdSql.error(f'create stable {db}.{tb} (ts timestamp, c0 int) tags(t0 int)', expectErrInfo="Cannot create table of system database", fullMatched=False) + tdSql.error(f'alter table {tb} add column c1 int', expectErrInfo="Cannot alter table of system database", fullMatched=False) + tdSql.error(f'alter table {db}.{tb} add column c1 int', expectErrInfo="Cannot alter table of system database", fullMatched=False) + tdSql.error(f'alter stable {tb} add column c1 int', expectErrInfo="Cannot alter table of system database", fullMatched=False) + tdSql.error(f'alter stable {db}.{tb} add column c1 int', expectErrInfo="Cannot alter table of system database", fullMatched=False) + tdSql.error(f'insert into {tb} values (now,1)', expectErrInfo="System table not allowed", fullMatched=False) + tdSql.error(f'insert into {db}.{tb} values (now,1)', expectErrInfo="System table not allowed", fullMatched=False) + tdSql.error(f'delete from {tb}', expectErrInfo="Cannot delete from system database", fullMatched=False) + tdSql.error(f'delete from {db}.{tb}', expectErrInfo="Cannot delete from system database", fullMatched=False) + tdSql.error(f'delete from {tb} where ts >= 0', expectErrInfo="Cannot delete from system database", fullMatched=False) + tdSql.error(f'delete from {db}.{tb} where ts >= 0', expectErrInfo="Cannot delete from system database", fullMatched=False) + tdSql.error(f'drop table {tb}', expectErrInfo="Cannot drop table of system database", fullMatched=False) + tdSql.error(f'drop table {db}.{tb}', expectErrInfo="Cannot drop table of system database", fullMatched=False) + tdSql.error(f'drop stable {tb}', expectErrInfo="Cannot drop table of system database", fullMatched=False) + tdSql.error(f'drop stable {db}.{tb}', expectErrInfo="Cannot drop table of system database", fullMatched=False) + tdSql.error(f'drop table with {tb}', expectErrInfo="Cannot drop table of system database", fullMatched=False) + tdSql.error(f'drop table with {db}.{tb}', expectErrInfo="Cannot drop table of system database", fullMatched=False) + tdSql.error(f'drop stable with {tb}', expectErrInfo="Cannot drop table of system database", fullMatched=False) + tdSql.error(f'drop stable with {db}.{tb}', expectErrInfo="Cannot drop table of system database", fullMatched=False) + + def ddl_in_sysdb(self): + self.ddl_sysdb() + self.ddl_systb() def run(self): - tdSql.prepare() - - startTime_all = time.time() - self.test_ddl() - endTime_all = time.time() - print("total time %ds" % (endTime_all - startTime_all)) - + self.ddl_in_sysdb() + tdDnodes.stoptaosd(1) + tdDnodes.starttaosd(1) + self.ddl_in_sysdb() def stop(self): tdSql.close() tdLog.success("%s successfully executed" % __file__) - tdCases.addWindows(__file__, TDTestCase()) tdCases.addLinux(__file__, TDTestCase()) diff --git a/tests/system-test/1-insert/delete_systable.py b/tests/system-test/1-insert/delete_systable.py deleted file mode 100644 index 40422a7515..0000000000 --- a/tests/system-test/1-insert/delete_systable.py +++ /dev/null @@ -1,111 +0,0 @@ - -################################################################### -# Copyright (c) 2016 by TAOS Technologies, Inc. -# All rights reserved. -# -# This file is proprietary and confidential to TAOS Technologies. -# No part of this file may be reproduced, stored, transmitted, -# disclosed or used in any form or by any means other than as -# expressly provided by the written permission from Jianhui Tao -# -################################################################### - -# -*- coding: utf-8 -*- - -import random -import string - -from numpy import logspace -from util import constant -from util.log import * -from util.cases import * -from util.sql import * -from util.common import * -from util.sqlset import TDSetSql - -info_schema_db = "information_schema" -perf_schema_db = "performance_schema" - -info_schema_tables = [ - "ins_dnodes", - "ins_mnodes", - "ins_modules", - "ins_qnodes", - "ins_snodes", - "ins_cluster", - "ins_databases", - "ins_functions", - "ins_indexes", - "ins_stables", - "ins_tables", - "ins_tags", - "ins_columns", - "ins_users", - "ins_grants", - "ins_vgroups", - "ins_configs", - "ins_dnode_variables", - "ins_topics", - "ins_subscriptions", - "ins_streams", - "ins_streams_tasks", - "ins_vnodes", - "ins_user_privileges" -] - -perf_schema_tables = [ - "perf_connections", - "perf_queries", - "perf_consumers", - "perf_trans", - "perf_apps" -] - -class TDTestCase: - def init(self, conn, logSql, replicaVar=1): - self.replicaVar = int(replicaVar) - tdLog.debug("start to execute %s" % __file__) - tdSql.init(conn.cursor()) - - def delete_systb(self): - tdSql.execute(f'use {info_schema_db}') - for i in info_schema_tables: - tdSql.error(f'delete from {i}') - tdSql.error(f'delete from {info_schema_db}.{i}') - tdSql.error(f'delete from {i} where ts >= 0') - tdSql.error(f'delete from {info_schema_db}.{i} where ts >= 0') - - tdSql.execute(f'use {perf_schema_db}') - for i in perf_schema_tables: - tdSql.error(f'delete from {i}') - tdSql.error(f'delete from {perf_schema_db}.{i}') - tdSql.error(f'delete from {i} where ts >= 0') - tdSql.error(f'delete from {perf_schema_db}.{i} where ts >= 0') - - def drop_systb(self): - tdSql.execute(f'use {info_schema_db}') - for i in info_schema_tables: - tdSql.error(f'drop table {i}') - tdSql.error(f'drop {info_schema_db}.{i}') - tdSql.error(f'drop database {info_schema_db}') - - tdSql.execute(f'use {perf_schema_db}') - for i in perf_schema_tables: - tdSql.error(f'drop table {i}') - tdSql.error(f'drop table {perf_schema_db}.{i}') - tdSql.error(f'drop database {perf_schema_db}') - - def delete_from_systb(self): - self.delete_systb() - self.drop_systb() - def run(self): - self.delete_from_systb() - tdDnodes.stoptaosd(1) - tdDnodes.starttaosd(1) - self.delete_from_systb() - def stop(self): - tdSql.close() - tdLog.success("%s successfully executed" % __file__) - -tdCases.addWindows(__file__, TDTestCase()) -tdCases.addLinux(__file__, TDTestCase()) diff --git a/tests/system-test/win-test-file b/tests/system-test/win-test-file index 1127499ec8..b3e7e5b4d3 100644 --- a/tests/system-test/win-test-file +++ b/tests/system-test/win-test-file @@ -562,7 +562,7 @@ python3 ./test.py -f 1-insert/update_data.py python3 ./test.py -f 1-insert/tb_100w_data_order.py python3 ./test.py -f 1-insert/delete_childtable.py python3 ./test.py -f 1-insert/delete_normaltable.py -python3 ./test.py -f 1-insert/delete_systable.py +python3 ./test.py -f 1-insert/ddl_in_sysdb.py python3 ./test.py -f 1-insert/keep_expired.py python3 ./test.py -f 1-insert/stmt_error.py python3 ./test.py -f 1-insert/drop.py