Skip to content

Commit 2064bd1

Browse files
authoredJan 8, 2025
Merge pull request #29492 from taosdata/fix/main/TD-33450
fix: correct error message when CREATE TABLE under system databases
2 parents fe4555f + 0e9c4f5 commit 2064bd1

File tree

5 files changed

+144
-123
lines changed

5 files changed

+144
-123
lines changed
 

‎source/libs/parser/src/parTranslater.c

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7550,6 +7550,11 @@ static int32_t translateDeleteWhere(STranslateContext* pCxt, SDeleteStmt* pDelet
75507550
}
75517551

75527552
static int32_t translateDelete(STranslateContext* pCxt, SDeleteStmt* pDelete) {
7553+
const char* dbName = ((STableNode*)pDelete->pFromTable)->dbName;
7554+
if (IS_SYS_DBNAME(dbName)) {
7555+
return generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_TSC_INVALID_OPERATION,
7556+
"Cannot delete from system database: `%s`", dbName);
7557+
}
75537558
pCxt->pCurrStmt = (SNode*)pDelete;
75547559
int32_t code = translateFrom(pCxt, &pDelete->pFromTable);
75557560
if (TSDB_CODE_SUCCESS == code) {
@@ -8405,6 +8410,10 @@ static int32_t checkCreateDatabase(STranslateContext* pCxt, SCreateDatabaseStmt*
84058410
return generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME,
84068411
"The database name cannot contain '.'");
84078412
}
8413+
if (IS_SYS_DBNAME(pStmt->dbName)) {
8414+
return generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_TSC_INVALID_OPERATION,
8415+
"Cannot create system database: `%s`", pStmt->dbName);
8416+
}
84088417
return checkDatabaseOptions(pCxt, pStmt->dbName, pStmt->pOptions);
84098418
}
84108419

@@ -8594,6 +8603,10 @@ static int32_t translateCreateDatabase(STranslateContext* pCxt, SCreateDatabaseS
85948603
}
85958604

85968605
static int32_t translateDropDatabase(STranslateContext* pCxt, SDropDatabaseStmt* pStmt) {
8606+
if (IS_SYS_DBNAME(pStmt->dbName)) {
8607+
return generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_TSC_INVALID_OPERATION, "Cannot drop system database: `%s`",
8608+
pStmt->dbName);
8609+
}
85978610
SDropDbReq dropReq = {0};
85988611
SName name = {0};
85998612
int32_t code = tNameSetDbName(&name, pCxt->pParseCxt->acctId, pStmt->dbName, strlen(pStmt->dbName));
@@ -8641,6 +8654,10 @@ static int32_t buildAlterDbReq(STranslateContext* pCxt, SAlterDatabaseStmt* pStm
86418654
}
86428655

86438656
static int32_t translateAlterDatabase(STranslateContext* pCxt, SAlterDatabaseStmt* pStmt) {
8657+
if (IS_SYS_DBNAME(pStmt->dbName)) {
8658+
return generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_TSC_INVALID_OPERATION, "Cannot alter system database: `%s`",
8659+
pStmt->dbName);
8660+
}
86448661
if (pStmt->pOptions->walLevel == 0) {
86458662
TAOS_CHECK_RETURN(translateGetDbCfg(pCxt, pStmt->dbName, &pStmt->pOptions->pDbCfg));
86468663
if (pStmt->pOptions->pDbCfg->replications > 1) {
@@ -9118,6 +9135,12 @@ static int32_t checkCreateTable(STranslateContext* pCxt, SCreateTableStmt* pStmt
91189135
"The table name cannot contain '.'");
91199136
}
91209137

9138+
if (IS_SYS_DBNAME(pStmt->dbName)) {
9139+
return generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_TSC_INVALID_OPERATION,
9140+
"Cannot create table of system database: `%s`.`%s`", pStmt->dbName,
9141+
pStmt->tableName);
9142+
}
9143+
91219144
SDbCfgInfo dbCfg = {0};
91229145
int32_t code = getDBCfg(pCxt, pStmt->dbName, &dbCfg);
91239146
if (TSDB_CODE_SUCCESS == code && !createStable && NULL != dbCfg.pRetensions) {
@@ -9864,6 +9887,11 @@ static int32_t checkAlterSuperTableBySchema(STranslateContext* pCxt, SAlterTable
98649887
}
98659888

98669889
static int32_t checkAlterSuperTable(STranslateContext* pCxt, SAlterTableStmt* pStmt) {
9890+
if (IS_SYS_DBNAME(pStmt->dbName)) {
9891+
return generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_TSC_INVALID_OPERATION,
9892+
"Cannot alter table of system database: `%s`.`%s`", pStmt->dbName, pStmt->tableName);
9893+
}
9894+
98679895
if (TSDB_ALTER_TABLE_UPDATE_TAG_VAL == pStmt->alterType ||
98689896
TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL == pStmt->alterType) {
98699897
return generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_ALTER_TABLE,
@@ -15550,11 +15578,6 @@ static int32_t rewriteDropTableWithOpt(STranslateContext* pCxt, SQuery* pQuery)
1555015578
char pTableName[TSDB_TABLE_NAME_LEN] = {0};
1555115579
FOREACH(pNode, pStmt->pTables) {
1555215580
SDropTableClause* pClause = (SDropTableClause*)pNode;
15553-
if (IS_SYS_DBNAME(pClause->dbName)) {
15554-
return generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_TSC_INVALID_OPERATION,
15555-
"Cannot drop table of system database: `%s`.`%s`", pClause->dbName,
15556-
pClause->tableName);
15557-
}
1555815581
for (int32_t i = 0; i < TSDB_TABLE_NAME_LEN; i++) {
1555915582
if (pClause->tableName[i] == '\0') {
1556015583
break;
@@ -15585,6 +15608,15 @@ static int32_t rewriteDropTable(STranslateContext* pCxt, SQuery* pQuery) {
1558515608
SNode* pNode;
1558615609
SArray* pTsmas = NULL;
1558715610

15611+
FOREACH(pNode, pStmt->pTables) {
15612+
SDropTableClause* pClause = (SDropTableClause*)pNode;
15613+
if (IS_SYS_DBNAME(pClause->dbName)) {
15614+
return generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_TSC_INVALID_OPERATION,
15615+
"Cannot drop table of system database: `%s`.`%s`", pClause->dbName,
15616+
pClause->tableName);
15617+
}
15618+
}
15619+
1558815620
TAOS_CHECK_RETURN(rewriteDropTableWithOpt(pCxt, pQuery));
1558915621

1559015622
SHashObj* pVgroupHashmap = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_NO_LOCK);
@@ -15674,11 +15706,6 @@ static int32_t rewriteDropSuperTablewithOpt(STranslateContext* pCxt, SQuery* pQu
1567415706
if (!pStmt->withOpt) return code;
1567515707
pCxt->withOpt = true;
1567615708

15677-
if (IS_SYS_DBNAME(pStmt->dbName)) {
15678-
return generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_TSC_INVALID_OPERATION,
15679-
"Cannot drop table of system database: `%s`.`%s`", pStmt->dbName, pStmt->tableName);
15680-
}
15681-
1568215709
for (int32_t i = 0; i < TSDB_TABLE_NAME_LEN; i++) {
1568315710
if (pStmt->tableName[i] == '\0') {
1568415711
break;
@@ -15708,6 +15735,11 @@ static int32_t rewriteDropSuperTablewithOpt(STranslateContext* pCxt, SQuery* pQu
1570815735
}
1570915736

1571015737
static int32_t rewriteDropSuperTable(STranslateContext* pCxt, SQuery* pQuery) {
15738+
SDropSuperTableStmt* pStmt = (SDropSuperTableStmt*)pQuery->pRoot;
15739+
if (IS_SYS_DBNAME(pStmt->dbName)) {
15740+
return generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_TSC_INVALID_OPERATION,
15741+
"Cannot drop table of system database: `%s`.`%s`", pStmt->dbName, pStmt->tableName);
15742+
}
1571115743
TAOS_CHECK_RETURN(rewriteDropSuperTablewithOpt(pCxt, pQuery));
1571215744
TAOS_RETURN(0);
1571315745
}
@@ -16261,6 +16293,11 @@ static int32_t rewriteAlterTableImpl(STranslateContext* pCxt, SAlterTableStmt* p
1626116293
static int32_t rewriteAlterTable(STranslateContext* pCxt, SQuery* pQuery) {
1626216294
SAlterTableStmt* pStmt = (SAlterTableStmt*)pQuery->pRoot;
1626316295

16296+
if (IS_SYS_DBNAME(pStmt->dbName)) {
16297+
return generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_TSC_INVALID_OPERATION,
16298+
"Cannot alter table of system database: `%s`.`%s`", pStmt->dbName, pStmt->tableName);
16299+
}
16300+
1626416301
if (pStmt->dataType.type == TSDB_DATA_TYPE_JSON && pStmt->alterType == TSDB_ALTER_TABLE_ADD_COLUMN) {
1626516302
return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_COL_JSON);
1626616303
}

‎tests/parallel_test/cases.task

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,7 @@
490490
,,y,system-test,./pytest.sh python3 ./test.py -f 1-insert/test_td29793.py
491491
,,y,system-test,./pytest.sh python3 ./test.py -f 1-insert/insert_timestamp.py
492492
,,y,system-test,./pytest.sh python3 ./test.py -f 1-insert/test_td29157.py
493+
,,y,system-test,./pytest.sh python3 ./test.py -f 1-insert/ddl_in_sysdb.py
493494
,,y,system-test,./pytest.sh python3 ./test.py -f 0-others/show.py
494495
,,y,system-test,./pytest.sh python3 ./test.py -f 0-others/show_tag_index.py
495496
,,y,system-test,./pytest.sh python3 ./test.py -f 0-others/information_schema.py
@@ -732,7 +733,6 @@
732733
,,y,system-test,./pytest.sh python3 ./test.py -f 1-insert/tb_100w_data_order.py
733734
,,y,system-test,./pytest.sh python3 ./test.py -f 1-insert/delete_childtable.py
734735
,,y,system-test,./pytest.sh python3 ./test.py -f 1-insert/delete_normaltable.py
735-
,,y,system-test,./pytest.sh python3 ./test.py -f 1-insert/delete_systable.py
736736
,,y,system-test,./pytest.sh python3 ./test.py -f 1-insert/keep_expired.py
737737
,,y,system-test,./pytest.sh python3 ./test.py -f 1-insert/stmt_error.py
738738
,,y,system-test,./pytest.sh python3 ./test.py -f 1-insert/drop.py
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
2+
###################################################################
3+
# Copyright (c) 2016 by TAOS Technologies, Inc.
4+
# All rights reserved.
5+
#
6+
# This file is proprietary and confidential to TAOS Technologies.
7+
# No part of this file may be reproduced, stored, transmitted,
8+
# disclosed or used in any form or by any means other than as
9+
# expressly provided by the written permission from Jianhui Tao
10+
#
11+
###################################################################
12+
13+
# -*- coding: utf-8 -*-
14+
15+
import random
16+
import string
17+
18+
from numpy import logspace
19+
from util import constant
20+
from util.log import *
21+
from util.cases import *
22+
from util.sql import *
23+
from util.common import *
24+
from util.sqlset import TDSetSql
25+
26+
sysdb_tables = {
27+
"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"],
28+
"performance_schema": ["perf_connections", "perf_queries", "perf_consumers", "perf_trans", "perf_apps", "undefined"]
29+
}
30+
31+
class TDTestCase:
32+
def init(self, conn, logSql, replicaVar=1):
33+
self.replicaVar = int(replicaVar)
34+
tdLog.debug("start to execute %s" % __file__)
35+
tdSql.init(conn.cursor())
36+
37+
def ddl_sysdb(self):
38+
for db, _ in sysdb_tables.items():
39+
tdSql.error(f'create database {db}', expectErrInfo="Cannot create system database", fullMatched=False)
40+
tdSql.error(f'create database {db} vgroups 10', expectErrInfo="Cannot create system database", fullMatched=False)
41+
tdSql.error(f'alter database {db} wal_level 0', expectErrInfo="Cannot alter system database", fullMatched=False)
42+
tdSql.error(f'alter database {db} cachemodel \'none\'', expectErrInfo="Cannot alter system database", fullMatched=False)
43+
tdSql.error(f'drop database {db}', expectErrInfo="Cannot drop system database", fullMatched=False)
44+
tdSql.error(f'drop database {db}', expectErrInfo="Cannot drop system database", fullMatched=False)
45+
46+
def ddl_systb(self):
47+
tdSql.execute('drop database if exists d0')
48+
tdSql.execute('create database if not exists d0')
49+
tdSql.execute('create stable d0.stb0 (ts timestamp, c0 int) tags(t0 int)')
50+
for db, tbs in sysdb_tables.items():
51+
tdSql.execute(f'use {db}')
52+
for tb in tbs:
53+
tdSql.error(f'create table {tb} (ts timestamp, c0 int)', expectErrInfo="Cannot create table of system database", fullMatched=False)
54+
tdSql.error(f'create table d0.ctb0 using db.stb0 tags(0) {tb} using {tb} tags(1)', expectErrInfo="Corresponding super table not in this db", fullMatched=False)
55+
tdSql.error(f'create table {db}.{tb} (ts timestamp, c0 int)', expectErrInfo="Cannot create table of system database", fullMatched=False)
56+
tdSql.error(f'create table d0.ctb0 using db.stb0 tags(0) {db}.{tb} using {db}.{tb} tags(1)', expectErrInfo="Corresponding super table not in this db", fullMatched=False)
57+
tdSql.error(f'create stable {tb} (ts timestamp, c0 int) tags(t0 int)', expectErrInfo="Cannot create table of system database", fullMatched=False)
58+
tdSql.error(f'create stable {db}.{tb} (ts timestamp, c0 int) tags(t0 int)', expectErrInfo="Cannot create table of system database", fullMatched=False)
59+
tdSql.error(f'alter table {tb} add column c1 int', expectErrInfo="Cannot alter table of system database", fullMatched=False)
60+
tdSql.error(f'alter table {db}.{tb} add column c1 int', expectErrInfo="Cannot alter table of system database", fullMatched=False)
61+
tdSql.error(f'alter stable {tb} add column c1 int', expectErrInfo="Cannot alter table of system database", fullMatched=False)
62+
tdSql.error(f'alter stable {db}.{tb} add column c1 int', expectErrInfo="Cannot alter table of system database", fullMatched=False)
63+
tdSql.error(f'insert into {tb} values (now,1)', expectErrInfo="System table not allowed", fullMatched=False)
64+
tdSql.error(f'insert into {db}.{tb} values (now,1)', expectErrInfo="System table not allowed", fullMatched=False)
65+
tdSql.error(f'insert into {tb} values (now,1) using stb tags(0) values(now,1)', expectErrInfo="System table not allowed", fullMatched=False)
66+
tdSql.error(f'insert into {db}.{tb} values (now,1) using stb tags(0) values(now,1)', expectErrInfo="System table not allowed", fullMatched=False)
67+
tdSql.error(f'delete from {tb}', expectErrInfo="Cannot delete from system database", fullMatched=False)
68+
tdSql.error(f'delete from {db}.{tb}', expectErrInfo="Cannot delete from system database", fullMatched=False)
69+
tdSql.error(f'delete from {tb} where ts >= 0', expectErrInfo="Cannot delete from system database", fullMatched=False)
70+
tdSql.error(f'delete from {db}.{tb} where ts >= 0', expectErrInfo="Cannot delete from system database", fullMatched=False)
71+
tdSql.error(f'drop table {tb}', expectErrInfo="Cannot drop table of system database", fullMatched=False)
72+
tdSql.error(f'drop table {db}.{tb}', expectErrInfo="Cannot drop table of system database", fullMatched=False)
73+
tdSql.error(f'drop stable {tb}', expectErrInfo="Cannot drop table of system database", fullMatched=False)
74+
tdSql.error(f'drop stable {db}.{tb}', expectErrInfo="Cannot drop table of system database", fullMatched=False)
75+
tdSql.error(f'drop table with {tb}', expectErrInfo="Cannot drop table of system database", fullMatched=False)
76+
tdSql.error(f'drop table with {db}.{tb}', expectErrInfo="Cannot drop table of system database", fullMatched=False)
77+
tdSql.error(f'drop stable with {tb}', expectErrInfo="Cannot drop table of system database", fullMatched=False)
78+
tdSql.error(f'drop stable with {db}.{tb}', expectErrInfo="Cannot drop table of system database", fullMatched=False)
79+
tdSql.execute('drop database if exists d0')
80+
81+
def ddl_in_sysdb(self):
82+
self.ddl_sysdb()
83+
self.ddl_systb()
84+
85+
def run(self):
86+
self.ddl_in_sysdb()
87+
tdDnodes.stoptaosd(1)
88+
tdDnodes.starttaosd(1)
89+
self.ddl_in_sysdb()
90+
def stop(self):
91+
tdSql.close()
92+
tdLog.success("%s successfully executed" % __file__)
93+
94+
tdCases.addWindows(__file__, TDTestCase())
95+
tdCases.addLinux(__file__, TDTestCase())

‎tests/system-test/1-insert/delete_systable.py

Lines changed: 0 additions & 111 deletions
This file was deleted.

‎tests/system-test/win-test-file

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ python3 ./test.py -f 1-insert/update_data.py
562562
python3 ./test.py -f 1-insert/tb_100w_data_order.py
563563
python3 ./test.py -f 1-insert/delete_childtable.py
564564
python3 ./test.py -f 1-insert/delete_normaltable.py
565-
python3 ./test.py -f 1-insert/delete_systable.py
565+
python3 ./test.py -f 1-insert/ddl_in_sysdb.py
566566
python3 ./test.py -f 1-insert/keep_expired.py
567567
python3 ./test.py -f 1-insert/stmt_error.py
568568
python3 ./test.py -f 1-insert/drop.py

0 commit comments

Comments
 (0)