Skip to content

Commit

Permalink
fix: fix name verifying
Browse files Browse the repository at this point in the history
  • Loading branch information
WenyXu committed Nov 27, 2023
1 parent 0badb37 commit a0e4d76
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/common/meta/src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ use crate::DatanodeId;

pub const REMOVED_PREFIX: &str = "__removed";

const NAME_PATTERN: &str = r"[a-zA-Z_:-][a-zA-Z0-9_:\-\.]*";
pub const NAME_PATTERN: &str = r"[a-zA-Z_:-][a-zA-Z0-9_:\-\.]*";

const DATANODE_TABLE_KEY_PREFIX: &str = "__dn_table";
const TABLE_REGION_KEY_PREFIX: &str = "__table_region";
Expand Down
52 changes: 50 additions & 2 deletions src/operator/src/statement/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@ use common_catalog::format_full_table_name;
use common_meta::cache_invalidator::Context;
use common_meta::ddl::ExecutorContext;
use common_meta::key::schema_name::{SchemaNameKey, SchemaNameValue};
use common_meta::key::NAME_PATTERN;
use common_meta::rpc::ddl::{DdlTask, SubmitDdlTaskRequest, SubmitDdlTaskResponse};
use common_meta::rpc::router::{Partition, Partition as MetaPartition};
use common_meta::table_name::TableName;
use common_query::Output;
use common_telemetry::{info, tracing};
use datatypes::prelude::ConcreteDataType;
use datatypes::schema::RawSchema;
use lazy_static::lazy_static;
use partition::partition::{PartitionBound, PartitionDef};
use regex::Regex;
use session::context::QueryContextRef;
use snafu::{ensure, OptionExt, ResultExt};
use sql::ast::Value as SqlValue;
Expand All @@ -41,7 +44,7 @@ use sql::statements::sql_value_to_value;
use sql::MAXVALUE;
use table::dist_table::DistTable;
use table::metadata::{self, RawTableInfo, RawTableMeta, TableId, TableInfo, TableType};
use table::requests::{AlterTableRequest, TableOptions};
use table::requests::{AlterKind, AlterTableRequest, TableOptions};
use table::TableRef;

use super::StatementExecutor;
Expand All @@ -53,6 +56,10 @@ use crate::error::{
};
use crate::expr_factory;

lazy_static! {
static ref NAME_PATTERN_REG: Regex = Regex::new(&format!("^{NAME_PATTERN}$")).unwrap();
}

impl StatementExecutor {
pub fn catalog_manager(&self) -> CatalogManagerRef {
self.catalog_manager.clone()
Expand Down Expand Up @@ -122,6 +129,13 @@ impl StatementExecutor {
};
}

ensure!(
NAME_PATTERN_REG.is_match(&create_table.table_name),
error::UnexpectedSnafu {
violated: format!("Invalid table name: {}", create_table.table_name)
}
);

let table_name = TableName::new(
&create_table.catalog_name,
&create_table.schema_name,
Expand Down Expand Up @@ -213,7 +227,20 @@ impl StatementExecutor {
let request: AlterTableRequest = common_grpc_expr::alter_expr_to_request(table_id, expr)
.context(AlterExprToRequestSnafu)?;

let AlterTableRequest { table_name, .. } = &request;
let AlterTableRequest {
table_name,
alter_kind,
..
} = &request;

if let AlterKind::RenameTable { new_table_name } = alter_kind {
ensure!(
NAME_PATTERN_REG.is_match(new_table_name),
error::UnexpectedSnafu {
violated: format!("Invalid table name: {}", new_table_name)
}
);
}

let _ = table_info
.meta
Expand Down Expand Up @@ -359,6 +386,20 @@ impl StatementExecutor {
database: &str,
create_if_not_exists: bool,
) -> Result<Output> {
ensure!(
NAME_PATTERN_REG.is_match(catalog),
error::UnexpectedSnafu {
violated: format!("Invalid catalog name: {}", catalog)
}
);

ensure!(
NAME_PATTERN_REG.is_match(database),
error::UnexpectedSnafu {
violated: format!("Invalid database name: {}", database)
}
);

// TODO(weny): considers executing it in the procedures.
let schema_key = SchemaNameKey::new(catalog, database);
let exists = self
Expand Down Expand Up @@ -587,6 +628,13 @@ mod test {
use super::*;
use crate::expr_factory;

#[test]
fn test_name_is_match() {
assert!(!NAME_PATTERN_REG.is_match("/adaf"));
assert!(!NAME_PATTERN_REG.is_match("🈲"));
assert!(NAME_PATTERN_REG.is_match("hello"));
}

#[test]
fn test_validate_partition_columns() {
let create_table = CreateTableExpr {
Expand Down
4 changes: 4 additions & 0 deletions tests/cases/standalone/common/alter/rename_table.result
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ SELECT * from t;
| | 1970-01-01T00:00:00.004 |
+---+-------------------------+

ALTER TABLE t RENAME 'Hi👋';

Error: 1002(Unexpected), Unexpected, violated: Invalid table name: Hi👋

ALTER TABLE t RENAME new_table;

Affected Rows: 0
Expand Down
2 changes: 2 additions & 0 deletions tests/cases/standalone/common/alter/rename_table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ INSERT INTO TABLE t VALUES (1, 1), (3, 3), (NULL, 4);

SELECT * from t;

ALTER TABLE t RENAME 'Hi👋';

ALTER TABLE t RENAME new_table;

DESC TABLE t;
Expand Down
4 changes: 4 additions & 0 deletions tests/cases/standalone/common/create/create.result
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ CREATE TABLE test2 (i INTEGER, j TIMESTAMP TIME INDEX);

Error: 4000(TableAlreadyExists), Table already exists: `greptime.public.test2`

CREATE TABLE 'N.~' (i TIMESTAMP TIME INDEX);

Error: 1002(Unexpected), Unexpected, violated: Invalid table name: N.~

DESC TABLE integers;

+--------+----------------------+-----+------+---------+---------------+
Expand Down
2 changes: 2 additions & 0 deletions tests/cases/standalone/common/create/create.sql
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ CREATE TABLE test2 (i INTEGER, j TIMESTAMP TIME INDEX);

CREATE TABLE test2 (i INTEGER, j TIMESTAMP TIME INDEX);

CREATE TABLE 'N.~' (i TIMESTAMP TIME INDEX);

DESC TABLE integers;

DESC TABLE test1;
Expand Down
5 changes: 4 additions & 1 deletion tests/cases/standalone/common/create/create_database.result
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ create database 'illegal-database';

Affected Rows: 1

create database '㊙️database';

Error: 1002(Unexpected), Unexpected, violated: Invalid database name: ㊙️database

show databases;

+--------------------+
Expand All @@ -14,6 +18,5 @@ show databases;
| illegal-database |
| information_schema |
| public |
| test_public_schema |
+--------------------+

2 changes: 2 additions & 0 deletions tests/cases/standalone/common/create/create_database.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ create database illegal-database;

create database 'illegal-database';

create database '㊙️database';

show databases;

0 comments on commit a0e4d76

Please sign in to comment.