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

@@ -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),