From a9a4196c961f47bcd3e1b48b7f63966de73dc4d0 Mon Sep 17 00:00:00 2001 From: Vamshi Maskuri <117595548+varshith257@users.noreply.github.com> Date: Thu, 31 Oct 2024 01:14:56 +0530 Subject: [PATCH 1/6] feat!: add sqlparser ident --- crates/proof-of-sql-parser/Cargo.toml | 1 + crates/proof-of-sql-parser/src/identifier.rs | 24 +++++++++++++++ crates/proof-of-sql-parser/src/resource_id.rs | 29 +++++++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/crates/proof-of-sql-parser/Cargo.toml b/crates/proof-of-sql-parser/Cargo.toml index 228833b7e..29c23bc56 100644 --- a/crates/proof-of-sql-parser/Cargo.toml +++ b/crates/proof-of-sql-parser/Cargo.toml @@ -21,6 +21,7 @@ chrono = { workspace = true, features = ["serde"] } lalrpop-util = { workspace = true, features = ["lexer", "unicode"] } serde = { workspace = true, features = ["serde_derive", "alloc"] } snafu = { workspace = true } +sqlparser = { workspace = true } [build-dependencies] lalrpop = { workspace = true } diff --git a/crates/proof-of-sql-parser/src/identifier.rs b/crates/proof-of-sql-parser/src/identifier.rs index b11df6862..fea7f2758 100644 --- a/crates/proof-of-sql-parser/src/identifier.rs +++ b/crates/proof-of-sql-parser/src/identifier.rs @@ -2,6 +2,7 @@ use crate::{sql::IdentifierParser, ParseError, ParseResult}; use alloc::{format, string::ToString}; use arrayvec::ArrayString; use core::{cmp::Ordering, fmt, ops::Deref, str::FromStr}; +use sqlparser::ast::Ident; /// Top-level unique identifier. #[derive(Debug, PartialEq, Eq, Clone, Hash, Ord, PartialOrd, Copy)] @@ -71,6 +72,18 @@ impl fmt::Display for Identifier { } } + +// TryFrom for Identifier +impl TryFrom for Identifier { + type Error = ParseError; + + fn try_from(ident: Ident) -> ParseResult { + // Convert Ident's value to Identifier + Identifier::try_new(ident.value) + } +} + + impl PartialEq for Identifier { fn eq(&self, other: &str) -> bool { other.eq_ignore_ascii_case(&self.name) @@ -97,6 +110,7 @@ impl AsRef for Identifier { } } + #[cfg(test)] mod tests { use super::*; @@ -278,4 +292,14 @@ mod tests { Identifier::new("t".repeat(64)); Identifier::new("茶".repeat(21)); } + + #[test] + fn try_from_ident() { + let ident = Ident::new("ValidIdentifier"); + let identifier = Identifier::try_from(ident).unwrap(); + assert_eq!(identifier.name(), "valididentifier"); + + let invalid_ident = Ident::new("INVALID$IDENTIFIER"); + assert!(Identifier::try_from(invalid_ident).is_err()); + } } diff --git a/crates/proof-of-sql-parser/src/resource_id.rs b/crates/proof-of-sql-parser/src/resource_id.rs index 2fe0d0848..10045d03b 100644 --- a/crates/proof-of-sql-parser/src/resource_id.rs +++ b/crates/proof-of-sql-parser/src/resource_id.rs @@ -3,11 +3,13 @@ use crate::{impl_serde_from_str, sql::ResourceIdParser, Identifier, ParseError, use alloc::{ format, string::{String, ToString}, + vec::Vec, }; use core::{ fmt::{self, Display}, str::FromStr, }; +use sqlparser::ast::Ident; /// Unique resource identifier, like `schema.object_name`. #[derive(Debug, PartialEq, Eq, Clone, Hash, Copy)] @@ -110,6 +112,22 @@ impl FromStr for ResourceId { } impl_serde_from_str!(ResourceId); +impl TryFrom> for ResourceId { + type Error = ParseError; + + fn try_from(identifiers: Vec) -> ParseResult { + if identifiers.len() != 2 { + return Err(ParseError::ResourceIdParseError { + error: "Expected exactly two identifiers for ResourceId".to_string(), + }); + } + + let schema = Identifier::try_from(identifiers[0].clone())?; + let object_name = Identifier::try_from(identifiers[1].clone())?; + Ok(ResourceId::new(schema, object_name)) + } +} + #[cfg(test)] mod tests { use super::*; @@ -233,4 +251,15 @@ mod tests { serde_json::from_str(r#""good_identifier.bad!identifier"#); assert!(deserialized.is_err()); } + + #[test] + fn test_try_from_vec_ident() { + let identifiers = alloc::vec![Ident::new("schema_name"), Ident::new("object_name")]; + let resource_id = ResourceId::try_from(identifiers).unwrap(); + assert_eq!(resource_id.schema().name(), "schema_name"); + assert_eq!(resource_id.object_name().name(), "object_name"); + + let invalid_identifiers = alloc::vec![Ident::new("only_one_ident")]; + assert!(ResourceId::try_from(invalid_identifiers).is_err()); + } } From 9945cd4c389efda75455d11bc4f71e3e06477c3e Mon Sep 17 00:00:00 2001 From: Vamshi Maskuri <117595548+varshith257@users.noreply.github.com> Date: Thu, 31 Oct 2024 01:14:56 +0530 Subject: [PATCH 2/6] feat!: add sqlparser ident --- crates/proof-of-sql-parser/Cargo.toml | 1 + crates/proof-of-sql-parser/src/identifier.rs | 24 +++++++++++++++ crates/proof-of-sql-parser/src/resource_id.rs | 29 +++++++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/crates/proof-of-sql-parser/Cargo.toml b/crates/proof-of-sql-parser/Cargo.toml index 228833b7e..29c23bc56 100644 --- a/crates/proof-of-sql-parser/Cargo.toml +++ b/crates/proof-of-sql-parser/Cargo.toml @@ -21,6 +21,7 @@ chrono = { workspace = true, features = ["serde"] } lalrpop-util = { workspace = true, features = ["lexer", "unicode"] } serde = { workspace = true, features = ["serde_derive", "alloc"] } snafu = { workspace = true } +sqlparser = { workspace = true } [build-dependencies] lalrpop = { workspace = true } diff --git a/crates/proof-of-sql-parser/src/identifier.rs b/crates/proof-of-sql-parser/src/identifier.rs index b11df6862..fea7f2758 100644 --- a/crates/proof-of-sql-parser/src/identifier.rs +++ b/crates/proof-of-sql-parser/src/identifier.rs @@ -2,6 +2,7 @@ use crate::{sql::IdentifierParser, ParseError, ParseResult}; use alloc::{format, string::ToString}; use arrayvec::ArrayString; use core::{cmp::Ordering, fmt, ops::Deref, str::FromStr}; +use sqlparser::ast::Ident; /// Top-level unique identifier. #[derive(Debug, PartialEq, Eq, Clone, Hash, Ord, PartialOrd, Copy)] @@ -71,6 +72,18 @@ impl fmt::Display for Identifier { } } + +// TryFrom for Identifier +impl TryFrom for Identifier { + type Error = ParseError; + + fn try_from(ident: Ident) -> ParseResult { + // Convert Ident's value to Identifier + Identifier::try_new(ident.value) + } +} + + impl PartialEq for Identifier { fn eq(&self, other: &str) -> bool { other.eq_ignore_ascii_case(&self.name) @@ -97,6 +110,7 @@ impl AsRef for Identifier { } } + #[cfg(test)] mod tests { use super::*; @@ -278,4 +292,14 @@ mod tests { Identifier::new("t".repeat(64)); Identifier::new("茶".repeat(21)); } + + #[test] + fn try_from_ident() { + let ident = Ident::new("ValidIdentifier"); + let identifier = Identifier::try_from(ident).unwrap(); + assert_eq!(identifier.name(), "valididentifier"); + + let invalid_ident = Ident::new("INVALID$IDENTIFIER"); + assert!(Identifier::try_from(invalid_ident).is_err()); + } } diff --git a/crates/proof-of-sql-parser/src/resource_id.rs b/crates/proof-of-sql-parser/src/resource_id.rs index 2fe0d0848..10045d03b 100644 --- a/crates/proof-of-sql-parser/src/resource_id.rs +++ b/crates/proof-of-sql-parser/src/resource_id.rs @@ -3,11 +3,13 @@ use crate::{impl_serde_from_str, sql::ResourceIdParser, Identifier, ParseError, use alloc::{ format, string::{String, ToString}, + vec::Vec, }; use core::{ fmt::{self, Display}, str::FromStr, }; +use sqlparser::ast::Ident; /// Unique resource identifier, like `schema.object_name`. #[derive(Debug, PartialEq, Eq, Clone, Hash, Copy)] @@ -110,6 +112,22 @@ impl FromStr for ResourceId { } impl_serde_from_str!(ResourceId); +impl TryFrom> for ResourceId { + type Error = ParseError; + + fn try_from(identifiers: Vec) -> ParseResult { + if identifiers.len() != 2 { + return Err(ParseError::ResourceIdParseError { + error: "Expected exactly two identifiers for ResourceId".to_string(), + }); + } + + let schema = Identifier::try_from(identifiers[0].clone())?; + let object_name = Identifier::try_from(identifiers[1].clone())?; + Ok(ResourceId::new(schema, object_name)) + } +} + #[cfg(test)] mod tests { use super::*; @@ -233,4 +251,15 @@ mod tests { serde_json::from_str(r#""good_identifier.bad!identifier"#); assert!(deserialized.is_err()); } + + #[test] + fn test_try_from_vec_ident() { + let identifiers = alloc::vec![Ident::new("schema_name"), Ident::new("object_name")]; + let resource_id = ResourceId::try_from(identifiers).unwrap(); + assert_eq!(resource_id.schema().name(), "schema_name"); + assert_eq!(resource_id.object_name().name(), "object_name"); + + let invalid_identifiers = alloc::vec![Ident::new("only_one_ident")]; + assert!(ResourceId::try_from(invalid_identifiers).is_err()); + } } From 92116a8d1a6cde811178c950608c065648bc7936 Mon Sep 17 00:00:00 2001 From: Vamshi Maskuri <117595548+varshith257@users.noreply.github.com> Date: Thu, 31 Oct 2024 01:20:19 +0530 Subject: [PATCH 3/6] fix: fmt --- crates/proof-of-sql-parser/src/identifier.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/crates/proof-of-sql-parser/src/identifier.rs b/crates/proof-of-sql-parser/src/identifier.rs index fea7f2758..87ccc9de4 100644 --- a/crates/proof-of-sql-parser/src/identifier.rs +++ b/crates/proof-of-sql-parser/src/identifier.rs @@ -72,18 +72,16 @@ impl fmt::Display for Identifier { } } - // TryFrom for Identifier impl TryFrom for Identifier { type Error = ParseError; fn try_from(ident: Ident) -> ParseResult { // Convert Ident's value to Identifier - Identifier::try_new(ident.value) + Identifier::try_new(ident.value) } } - impl PartialEq for Identifier { fn eq(&self, other: &str) -> bool { other.eq_ignore_ascii_case(&self.name) @@ -110,7 +108,6 @@ impl AsRef for Identifier { } } - #[cfg(test)] mod tests { use super::*; @@ -298,7 +295,7 @@ mod tests { let ident = Ident::new("ValidIdentifier"); let identifier = Identifier::try_from(ident).unwrap(); assert_eq!(identifier.name(), "valididentifier"); - + let invalid_ident = Ident::new("INVALID$IDENTIFIER"); assert!(Identifier::try_from(invalid_ident).is_err()); } From 1dd5910736feec57c3546b71358d13d17ae5c661 Mon Sep 17 00:00:00 2001 From: Vamshi Maskuri <117595548+varshith257@users.noreply.github.com> Date: Thu, 31 Oct 2024 01:29:55 +0530 Subject: [PATCH 4/6] fix: fmt --- crates/proof-of-sql-parser/src/identifier.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/crates/proof-of-sql-parser/src/identifier.rs b/crates/proof-of-sql-parser/src/identifier.rs index fea7f2758..87ccc9de4 100644 --- a/crates/proof-of-sql-parser/src/identifier.rs +++ b/crates/proof-of-sql-parser/src/identifier.rs @@ -72,18 +72,16 @@ impl fmt::Display for Identifier { } } - // TryFrom for Identifier impl TryFrom for Identifier { type Error = ParseError; fn try_from(ident: Ident) -> ParseResult { // Convert Ident's value to Identifier - Identifier::try_new(ident.value) + Identifier::try_new(ident.value) } } - impl PartialEq for Identifier { fn eq(&self, other: &str) -> bool { other.eq_ignore_ascii_case(&self.name) @@ -110,7 +108,6 @@ impl AsRef for Identifier { } } - #[cfg(test)] mod tests { use super::*; @@ -298,7 +295,7 @@ mod tests { let ident = Ident::new("ValidIdentifier"); let identifier = Identifier::try_from(ident).unwrap(); assert_eq!(identifier.name(), "valididentifier"); - + let invalid_ident = Ident::new("INVALID$IDENTIFIER"); assert!(Identifier::try_from(invalid_ident).is_err()); } From 8730ae8f4aeb7f35f020218eb483ef850192baf1 Mon Sep 17 00:00:00 2001 From: Vamshi Maskuri <117595548+varshith257@users.noreply.github.com> Date: Thu, 31 Oct 2024 02:25:36 +0530 Subject: [PATCH 5/6] refactor: ResourceId conversion to use ObjectName structure --- crates/proof-of-sql-parser/src/resource_id.rs | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/crates/proof-of-sql-parser/src/resource_id.rs b/crates/proof-of-sql-parser/src/resource_id.rs index 10045d03b..f78d716f3 100644 --- a/crates/proof-of-sql-parser/src/resource_id.rs +++ b/crates/proof-of-sql-parser/src/resource_id.rs @@ -9,7 +9,7 @@ use core::{ fmt::{self, Display}, str::FromStr, }; -use sqlparser::ast::Ident; +use sqlparser::ast::{Ident, ObjectName}; /// Unique resource identifier, like `schema.object_name`. #[derive(Debug, PartialEq, Eq, Clone, Hash, Copy)] @@ -112,18 +112,18 @@ impl FromStr for ResourceId { } impl_serde_from_str!(ResourceId); -impl TryFrom> for ResourceId { +impl TryFrom for ResourceId { type Error = ParseError; - fn try_from(identifiers: Vec) -> ParseResult { - if identifiers.len() != 2 { + fn try_from(object_name: ObjectName) -> ParseResult { + if object_name.0.len() != 2 { return Err(ParseError::ResourceIdParseError { error: "Expected exactly two identifiers for ResourceId".to_string(), }); } - let schema = Identifier::try_from(identifiers[0].clone())?; - let object_name = Identifier::try_from(identifiers[1].clone())?; + let schema = Identifier::try_from(object_name.0[0].clone())?; + let object_name = Identifier::try_from(object_name.0[1].clone())?; Ok(ResourceId::new(schema, object_name)) } } @@ -253,13 +253,16 @@ mod tests { } #[test] - fn test_try_from_vec_ident() { - let identifiers = alloc::vec![Ident::new("schema_name"), Ident::new("object_name")]; - let resource_id = ResourceId::try_from(identifiers).unwrap(); + fn test_try_from_object_name() { + let object_name = ObjectName(alloc::vec![ + Ident::new("schema_name"), + Ident::new("object_name") + ]); + let resource_id = ResourceId::try_from(object_name).unwrap(); assert_eq!(resource_id.schema().name(), "schema_name"); assert_eq!(resource_id.object_name().name(), "object_name"); - let invalid_identifiers = alloc::vec![Ident::new("only_one_ident")]; - assert!(ResourceId::try_from(invalid_identifiers).is_err()); + let invalid_object_name = ObjectName(alloc::vec![Ident::new("invalid_schema")]); + assert!(ResourceId::try_from(invalid_object_name).is_err()); } } From eeaf042e573afa1d5e511ec72604e48789d57000 Mon Sep 17 00:00:00 2001 From: Vamshi Maskuri <117595548+varshith257@users.noreply.github.com> Date: Thu, 31 Oct 2024 02:29:55 +0530 Subject: [PATCH 6/6] revert: ResourceId conversion to use ObjectName structure --- crates/proof-of-sql-parser/src/resource_id.rs | 25 ++++++++----------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/crates/proof-of-sql-parser/src/resource_id.rs b/crates/proof-of-sql-parser/src/resource_id.rs index f78d716f3..10045d03b 100644 --- a/crates/proof-of-sql-parser/src/resource_id.rs +++ b/crates/proof-of-sql-parser/src/resource_id.rs @@ -9,7 +9,7 @@ use core::{ fmt::{self, Display}, str::FromStr, }; -use sqlparser::ast::{Ident, ObjectName}; +use sqlparser::ast::Ident; /// Unique resource identifier, like `schema.object_name`. #[derive(Debug, PartialEq, Eq, Clone, Hash, Copy)] @@ -112,18 +112,18 @@ impl FromStr for ResourceId { } impl_serde_from_str!(ResourceId); -impl TryFrom for ResourceId { +impl TryFrom> for ResourceId { type Error = ParseError; - fn try_from(object_name: ObjectName) -> ParseResult { - if object_name.0.len() != 2 { + fn try_from(identifiers: Vec) -> ParseResult { + if identifiers.len() != 2 { return Err(ParseError::ResourceIdParseError { error: "Expected exactly two identifiers for ResourceId".to_string(), }); } - let schema = Identifier::try_from(object_name.0[0].clone())?; - let object_name = Identifier::try_from(object_name.0[1].clone())?; + let schema = Identifier::try_from(identifiers[0].clone())?; + let object_name = Identifier::try_from(identifiers[1].clone())?; Ok(ResourceId::new(schema, object_name)) } } @@ -253,16 +253,13 @@ mod tests { } #[test] - fn test_try_from_object_name() { - let object_name = ObjectName(alloc::vec![ - Ident::new("schema_name"), - Ident::new("object_name") - ]); - let resource_id = ResourceId::try_from(object_name).unwrap(); + fn test_try_from_vec_ident() { + let identifiers = alloc::vec![Ident::new("schema_name"), Ident::new("object_name")]; + let resource_id = ResourceId::try_from(identifiers).unwrap(); assert_eq!(resource_id.schema().name(), "schema_name"); assert_eq!(resource_id.object_name().name(), "object_name"); - let invalid_object_name = ObjectName(alloc::vec![Ident::new("invalid_schema")]); - assert!(ResourceId::try_from(invalid_object_name).is_err()); + let invalid_identifiers = alloc::vec![Ident::new("only_one_ident")]; + assert!(ResourceId::try_from(invalid_identifiers).is_err()); } }