From 8d938c3ac8266e33e4c85d4c95b7708dd0afd146 Mon Sep 17 00:00:00 2001 From: Ning Sun Date: Wed, 19 Oct 2022 14:13:26 +0800 Subject: [PATCH] feat: use our forked sqlparser to parse Geometry(POINT) syntax --- Cargo.lock | 3 +- Cargo.toml | 3 ++ src/datatypes/src/types/geometry.rs | 11 ++++--- src/query/Cargo.toml | 1 - src/sql/Cargo.toml | 2 +- src/sql/src/statements.rs | 45 +++++++++++++++++++++++++++-- 6 files changed, 53 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3f795815c0..b66c95a267 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5156,8 +5156,7 @@ dependencies = [ [[package]] name = "sqlparser" version = "0.15.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "adbbea2526ad0d02ad9414a07c396078a5b944bbf9ca4fbab8f01bb4cb579081" +source = "git+https://github.com/sunng87/sqlparser-rs.git?branch=feature/argument-for-custom-type-for-v015#1933b40f285d1eda49e38885bb2741af525a2ebe" dependencies = [ "log", ] diff --git a/Cargo.toml b/Cargo.toml index 7deaf5eee7..d0bd09a6d5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,3 +30,6 @@ members = [ "src/table-engine", "test-util", ] + +[patch.crates-io] +sqlparser = { git = "https://github.com/sunng87/sqlparser-rs.git", branch = "feature/argument-for-custom-type-for-v015" } diff --git a/src/datatypes/src/types/geometry.rs b/src/datatypes/src/types/geometry.rs index 197e9fc726..5dff58d876 100644 --- a/src/datatypes/src/types/geometry.rs +++ b/src/datatypes/src/types/geometry.rs @@ -7,8 +7,6 @@ use crate::prelude::LogicalTypeId; use crate::value::GeometryValue; use crate::vectors::geometry::GeometryVectorBuilder; -const GEOMETRY_TYPE_NAME: &str = "Geometry"; - #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub enum GeometryType { Point, @@ -22,7 +20,7 @@ impl Default for GeometryType { impl DataType for GeometryType { fn name(&self) -> &str { - GEOMETRY_TYPE_NAME + Self::GEOMETRY_TYPE_NAME } fn logical_type_id(&self) -> crate::type_id::LogicalTypeId { @@ -54,7 +52,8 @@ impl DataType for GeometryType { } impl GeometryType { - pub fn name() -> &'static str { - GEOMETRY_TYPE_NAME - } + pub const GEOMETRY_TYPE_NAME: &'static str = "Geometry"; + pub const GEOMETRY_SUBTYPE_POINT_NAME: &'static str = "POINT"; + pub const GEOMETRY_SUBTYPE_LINESTRING_NAME: &'static str = "LINESTRING"; + pub const GEOMETRY_SUBTYPE_POLYGON_NAME: &'static str = "POLYGON"; } diff --git a/src/query/Cargo.toml b/src/query/Cargo.toml index d0d8ea983e..2031c0edc5 100644 --- a/src/query/Cargo.toml +++ b/src/query/Cargo.toml @@ -46,4 +46,3 @@ streaming-stats = "0.2" test-util = { path = "../../test-util" } tokio = { version = "1.0", features = ["full"] } tokio-stream = "0.1" - diff --git a/src/sql/Cargo.toml b/src/sql/Cargo.toml index 5365b5d179..b9a8c7e6f0 100644 --- a/src/sql/Cargo.toml +++ b/src/sql/Cargo.toml @@ -9,5 +9,5 @@ common-error = { path = "../common/error" } common-time = { path = "../common/time" } datatypes = { path = "../datatypes" } snafu = { version = "0.7", features = ["backtraces"] } -sqlparser = "0.15.0" +sqlparser = { git = "https://github.com/sunng87/sqlparser-rs.git", branch = "feature/argument-for-custom-type-for-v015" } table-engine = { path = "../table-engine" } diff --git a/src/sql/src/statements.rs b/src/sql/src/statements.rs index 1266e86fb5..9f61d0acc6 100644 --- a/src/sql/src/statements.rs +++ b/src/sql/src/statements.rs @@ -256,8 +256,6 @@ fn sql_data_type_to_concrete_data_type(data_type: &SqlDataType) -> Result { if type_name.value.eq_ignore_ascii_case(DateTimeType::name()) { Ok(ConcreteDataType::datetime_datatype()) - } else if type_name.value.eq_ignore_ascii_case(GeometryType::name()) { - Ok(ConcreteDataType::geometry_datatype(GeometryType::Point)) } else { error::SqlTypeNotSupportedSnafu { t: data_type.clone(), @@ -270,6 +268,30 @@ fn sql_data_type_to_concrete_data_type(data_type: &SqlDataType) -> Result match &obj_name.0[..] { + [type_name] => { + if type_name + .value + .eq_ignore_ascii_case(GeometryType::GEOMETRY_TYPE_NAME) + { + if let Some(subtype) = args.get(0) { + let subtype = subtype.to_uppercase(); + if subtype == GeometryType::GEOMETRY_SUBTYPE_POINT_NAME { + return Ok(ConcreteDataType::Geometry(GeometryType::Point)); + } + } + } + + error::SqlTypeNotSupportedSnafu { + t: data_type.clone(), + } + .fail() + } + _ => error::SqlTypeNotSupportedSnafu { + t: data_type.clone(), + } + .fail(), + }, SqlDataType::Timestamp => Ok(ConcreteDataType::timestamp_millis_datatype()), _ => error::SqlTypeNotSupportedSnafu { t: data_type.clone(), @@ -326,6 +348,25 @@ mod tests { SqlDataType::Timestamp, ConcreteDataType::timestamp_millis_datatype(), ); + check_type( + SqlDataType::CustomWithArgs(ObjectName(vec!["GEOMETRY".into()]), vec!["POINT".into()]), + ConcreteDataType::geometry_datatype(GeometryType::Point), + ); + + assert!( + sql_data_type_to_concrete_data_type(&SqlDataType::CustomWithArgs( + ObjectName(vec!["GEOMETRY".into()]), + vec![] + )) + .is_err() + ); + assert!( + sql_data_type_to_concrete_data_type(&SqlDataType::CustomWithArgs( + ObjectName(vec!["GEOMETRY".into()]), + vec!["DUMMY_TYPE".into()] + )) + .is_err() + ); } #[test]