fix: store raw objname internally (#2719)

* fix: store raw objname internally

Signed-off-by: tison <wander4096@gmail.com>

* add a utility

Signed-off-by: tison <wander4096@gmail.com>

* add a sqlness test case

Signed-off-by: tison <wander4096@gmail.com>

* cargo clippy

Signed-off-by: tison <wander4096@gmail.com>

---------

Signed-off-by: tison <wander4096@gmail.com>
This commit is contained in:
tison
2023-11-10 10:56:30 +08:00
committed by GitHub
parent af7107565a
commit 8fd0766754
7 changed files with 59 additions and 6 deletions

View File

@@ -43,6 +43,7 @@ use snafu::{OptionExt, ResultExt};
use sql::statements::copy::{CopyDatabaseArgument, CopyTable, CopyTableArgument};
use sql::statements::statement::Statement;
use sql::statements::OptionMap;
use sql::util::format_raw_object_name;
use sqlparser::ast::ObjectName;
use table::engine::TableReference;
use table::requests::{CopyDatabaseRequest, CopyDirection, CopyTableRequest};
@@ -162,7 +163,7 @@ impl StatementExecutor {
Statement::CreateDatabase(stmt) => {
self.create_database(
query_ctx.current_catalog(),
&stmt.name.to_string(),
&format_raw_object_name(&stmt.name),
stmt.if_not_exists,
)
.await

View File

@@ -17,6 +17,7 @@ use common_query::Output;
use session::context::QueryContextRef;
use snafu::{OptionExt, ResultExt};
use sql::statements::describe::DescribeTable;
use sql::util::format_raw_object_name;
use crate::error::{
CatalogSnafu, DescribeStatementSnafu, ExternalSnafu, Result, TableNotFoundSnafu,
@@ -40,7 +41,7 @@ impl StatementExecutor {
.await
.context(CatalogSnafu)?
.with_context(|| TableNotFoundSnafu {
table_name: stmt.name().to_string(),
table_name: format_raw_object_name(stmt.name()),
})?;
query::sql::describe_table(table).context(DescribeStatementSnafu)

View File

@@ -39,6 +39,7 @@ mod tests {
use crate::dialect::GreptimeDbDialect;
use crate::parser::ParserContext;
use crate::statements::statement::Statement;
use crate::util::format_raw_object_name;
#[test]
pub fn test_describe_table() {
@@ -49,7 +50,7 @@ mod tests {
assert_matches!(&stmts[0], Statement::DescribeTable { .. });
match &stmts[0] {
Statement::DescribeTable(show) => {
assert_eq!(show.name.to_string(), "test");
assert_eq!(format_raw_object_name(&show.name), "test");
}
_ => {
unreachable!();
@@ -66,7 +67,7 @@ mod tests {
assert_matches!(&stmts[0], Statement::DescribeTable { .. });
match &stmts[0] {
Statement::DescribeTable(show) => {
assert_eq!(show.name.to_string(), "test_schema.test");
assert_eq!(format_raw_object_name(&show.name), "test_schema.test");
}
_ => {
unreachable!();
@@ -83,7 +84,10 @@ mod tests {
assert_matches!(&stmts[0], Statement::DescribeTable { .. });
match &stmts[0] {
Statement::DescribeTable(show) => {
assert_eq!(show.name.to_string(), "test_catalog.test_schema.test");
assert_eq!(
format_raw_object_name(&show.name),
"test_catalog.test_schema.test"
);
}
_ => {
unreachable!();

View File

@@ -13,10 +13,11 @@
// limitations under the License.
use std::collections::HashMap;
use std::fmt::{Display, Formatter};
use std::sync::LazyLock;
use regex::Regex;
use sqlparser::ast::{SqlOption, Value};
use sqlparser::ast::{ObjectName, SqlOption, Value};
static SQL_SECRET_PATTERNS: LazyLock<Vec<Regex>> = LazyLock::new(|| {
vec![
@@ -25,6 +26,27 @@ static SQL_SECRET_PATTERNS: LazyLock<Vec<Regex>> = LazyLock::new(|| {
]
});
/// Format an [ObjectName] without any quote of its idents.
pub fn format_raw_object_name(name: &ObjectName) -> String {
struct Inner<'a> {
name: &'a ObjectName,
}
impl<'a> Display for Inner<'a> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let mut delim = "";
for ident in self.name.0.iter() {
write!(f, "{delim}")?;
delim = ".";
write!(f, "{}", ident.value)?;
}
Ok(())
}
}
format!("{}", Inner { name })
}
pub fn parse_option_string(value: Value) -> Option<String> {
match value {
Value::SingleQuotedString(v) | Value::DoubleQuotedString(v) => Some(v),

View File

@@ -0,0 +1,19 @@
create database illegal-database;
Error: 1001(Unsupported), SQL statement is not supported: create database illegal-database;, keyword: -
create database 'illegal-database';
Affected Rows: 1
show databases;
+--------------------+
| Schemas |
+--------------------+
| illegal-database |
| information_schema |
| public |
| test_public_schema |
+--------------------+

View File

@@ -0,0 +1,5 @@
create database illegal-database;
create database 'illegal-database';
show databases;

View File

@@ -3,6 +3,7 @@ show databases;
+-----------------------+
| Schemas |
+-----------------------+
| illegal-database |
| information_schema |
| public |
| test_public_schema |