Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: move parser into own crate #2747

Merged
merged 8 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 14 additions & 78 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/datasources/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ futures = { workspace = true }
gcp-bigquery-client = "0.18.0"
klickhouse = { version = "0.11.2", features = ["tls"] }
protogen = { path = "../protogen" }
parser = {path = "../parser"}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super-nit: we added spaces in other places for this form.

idle musing: is there a toml linter we could get?

datafusion_ext = { path = "../datafusion_ext" }
mongodb = "2.8.1"
mysql_async = { version = "0.33.0", default-features = false, features = [
Expand Down
18 changes: 18 additions & 0 deletions crates/datasources/src/debug/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ use datafusion_ext::errors::ExtensionError;
use datafusion_ext::functions::VirtualLister;
use errors::DebugError;
use futures::Stream;
use parser::errors::ParserError;
use parser::options::{OptionValue, ParseOptionValue};
use protogen::metastore::types::options::TunnelOptions;
use serde::{Deserialize, Serialize};

Expand All @@ -48,6 +50,22 @@ pub enum DebugTableType {
NeverEnding,
}

impl ParseOptionValue<DebugTableType> for OptionValue {
fn parse_opt(self) -> Result<DebugTableType, ParserError> {
let opt = match self {
Self::QuotedLiteral(s) | Self::UnquotedLiteral(s) => s
.parse()
.map_err(|e: DebugError| ParserError::ParserError(e.to_string()))?,
o => {
return Err(ParserError::ParserError(format!(
"Expected a string, got: {}",
o
)))
}
};
Ok(opt)
}
}
impl fmt::Display for DebugTableType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.as_str())
Expand Down
16 changes: 16 additions & 0 deletions crates/datasources/src/mongodb/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ use mongodb::bson::spec::BinarySubtype;
use mongodb::bson::{bson, Binary, Bson, Document, RawDocumentBuf};
use mongodb::options::{ClientOptions, FindOptions};
use mongodb::{Client, Collection};
use parser::errors::ParserError;
use parser::options::{OptionValue, ParseOptionValue};
use parser::{parser_err, unexpected_type_err};
use tracing::debug;

use crate::bson::array_to_bson;
Expand Down Expand Up @@ -66,6 +69,19 @@ impl FromStr for MongoDbProtocol {
}
}

impl ParseOptionValue<MongoDbProtocol> for OptionValue {
fn parse_opt(self) -> Result<MongoDbProtocol, ParserError> {
let opt = match self {
Self::QuotedLiteral(s) | Self::UnquotedLiteral(s) => {
s.parse().map_err(|e| parser_err!("{e}"))?
}
o => return Err(unexpected_type_err!("mongodb protocol", o)),
};
Ok(opt)
}
}


impl Display for MongoDbProtocol {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let s = match self {
Expand Down
18 changes: 7 additions & 11 deletions crates/metastore/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
[package]
name = "metastore"
version = {workspace = true}
edition = {workspace = true}
version = { workspace = true }
edition = { workspace = true }

[lints]
workspace = true

[dependencies]
ioutil = {path = "../ioutil"}
logutil = {path = "../logutil"}
protogen = {path = "../protogen"}
ioutil = { path = "../ioutil" }
logutil = { path = "../logutil" }
protogen = { path = "../protogen" }
sqlbuiltins = { path = "../sqlbuiltins" }
object_store_util = {path = "../object_store_util"}
pgrepr = {path = "../pgrepr"}
object_store_util = { path = "../object_store_util" }
pgrepr = { path = "../pgrepr" }
async-trait = { workspace = true }
datafusion = { workspace = true }
futures = { workspace = true }
Expand All @@ -26,10 +26,6 @@ tracing = { workspace = true }
uuid = { version = "1.7.0", features = ["v4", "fast-rng", "macro-diagnostics"] }
bytes = "1.4"
once_cell = "1.19.0"
proptest-derive = "0.4"
tower = "0.4"
dashmap = "5.5.0"
catalog = { path = "../catalog" }

[dev-dependencies]
proptest = "1.4"
17 changes: 17 additions & 0 deletions crates/parser/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "parser"
version.workspace = true
edition.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
datafusion.workspace = true
datafusion_ext = { path = "../datafusion_ext" }
protogen = { path = "../protogen" }
prql-compiler = "0.11.3"
universalmind303 marked this conversation as resolved.
Show resolved Hide resolved
thiserror.workspace = true


[lints]
workspace = true
20 changes: 20 additions & 0 deletions crates/parser/src/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
pub use datafusion::sql::sqlparser::parser::ParserError;

#[derive(Debug, thiserror::Error)]
pub enum ParseError {
#[error("SQL statement currently unsupported: {0}")]
UnsupportedSQLStatement(String),
#[error(transparent)]
SqlParser(#[from] ParserError),
#[error("Invalid object name length: {length}, max: {max}")]
InvalidNameLength { length: usize, max: usize },
#[error("Other error: {0}")]
Other(String),
}

pub type Result<T, E = ParseError> = std::result::Result<T, E>;
impl From<ParseError> for ParserError {
fn from(e: ParseError) -> Self {
ParserError::ParserError(e.to_string())
}
}
Loading