Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correct error message when CREATE TABLE under system databases #29492

Merged
merged 7 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 47 additions & 10 deletions source/libs/parser/src/parTranslater.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -8594,6 +8603,10 @@ 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));
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -9118,6 +9135,12 @@ static int32_t checkCreateTable(STranslateContext* pCxt, SCreateTableStmt* pStmt
"The table name cannot contain '.'");
}

if (IS_SYS_DBNAME(pStmt->dbName)) {
return generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_TSC_INVALID_OPERATION,
"Cannot create table of system database: `%s`.`%s`", pStmt->dbName,
pStmt->tableName);
}

SDbCfgInfo dbCfg = {0};
int32_t code = getDBCfg(pCxt, pStmt->dbName, &dbCfg);
if (TSDB_CODE_SUCCESS == code && !createStable && NULL != dbCfg.pRetensions) {
Expand Down Expand Up @@ -9864,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,
Expand Down Expand Up @@ -15550,11 +15578,6 @@ static int32_t rewriteDropTableWithOpt(STranslateContext* pCxt, SQuery* pQuery)
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);
}
for (int32_t i = 0; i < TSDB_TABLE_NAME_LEN; i++) {
if (pClause->tableName[i] == '\0') {
break;
Expand Down Expand Up @@ -15585,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);
Expand Down Expand Up @@ -15674,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;
Expand Down Expand Up @@ -15708,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);
}
Expand Down Expand Up @@ -16261,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);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/parallel_test/cases.task
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,7 @@
,,y,system-test,./pytest.sh python3 ./test.py -f 1-insert/test_td29793.py
,,y,system-test,./pytest.sh python3 ./test.py -f 1-insert/insert_timestamp.py
,,y,system-test,./pytest.sh python3 ./test.py -f 1-insert/test_td29157.py
,,y,system-test,./pytest.sh python3 ./test.py -f 1-insert/ddl_in_sysdb.py
,,y,system-test,./pytest.sh python3 ./test.py -f 0-others/show.py
,,y,system-test,./pytest.sh python3 ./test.py -f 0-others/show_tag_index.py
,,y,system-test,./pytest.sh python3 ./test.py -f 0-others/information_schema.py
Expand Down Expand Up @@ -732,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
Expand Down
95 changes: 95 additions & 0 deletions tests/system-test/1-insert/ddl_in_sysdb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@

###################################################################
# 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

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("start to execute %s" % __file__)
tdSql.init(conn.cursor())

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 ddl_systb(self):
tdSql.execute('drop database if exists d0')
tdSql.execute('create database if not exists d0')
tdSql.execute('create stable d0.stb0 (ts timestamp, c0 int) tags(t0 int)')
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 d0.ctb0 using db.stb0 tags(0) {tb} using {tb} tags(1)', expectErrInfo="Corresponding super table not in this db", 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 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)
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'insert into {tb} values (now,1) using stb tags(0) values(now,1)', expectErrInfo="System table not allowed", fullMatched=False)
tdSql.error(f'insert into {db}.{tb} values (now,1) using stb tags(0) 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)
tdSql.execute('drop database if exists d0')

def ddl_in_sysdb(self):
self.ddl_sysdb()
self.ddl_systb()

def run(self):
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())
111 changes: 0 additions & 111 deletions tests/system-test/1-insert/delete_systable.py

This file was deleted.

2 changes: 1 addition & 1 deletion tests/system-test/win-test-file
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading