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

[WIP] Introduce the "parser" feature to gate the SQL text processing and leaving only AST and other support types #1691

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
11 changes: 10 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ path = "src/lib.rs"
default = ["std", "recursive-protection"]
std = []
recursive-protection = ["std", "recursive"]
parser = []
# Enable JSON output in the `cli` example:
json_example = ["serde_json", "serde"]
visitor = ["sqlparser_derive"]
Expand All @@ -63,4 +64,12 @@ pretty_assertions = "1"

[package.metadata.docs.rs]
# Document these features on docs.rs
features = ["serde", "visitor"]
features = ["parser", "serde", "visitor"]

[[example]]
name = "cli"
required-features = ["parser"]

[[example]]
name = "parse_select"
required-features = ["parser"]
2 changes: 1 addition & 1 deletion sqlparser_bench/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ authors = ["Dandandan <[email protected]>"]
edition = "2018"

[dependencies]
sqlparser = { path = "../" }
sqlparser = { path = "../", features = ["parser"] }

[dev-dependencies]
criterion = "0.5"
Expand Down
1 change: 1 addition & 0 deletions src/ast/helpers/stmt_create_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,7 @@ impl TryFrom<Statement> for CreateTableBuilder {

/// Helper return type when parsing configuration for a `CREATE TABLE` statement.
#[derive(Default)]
#[cfg(feature = "parser")]
pub(crate) struct CreateTableConfiguration {
pub partition_by: Option<Box<Expr>>,
pub cluster_by: Option<WrappedCollection<Vec<Ident>>>,
Expand Down
7 changes: 7 additions & 0 deletions src/ast/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,13 @@ fn union_spans<I: Iterator<Item = Span>>(iter: I) -> Span {
///
/// # Example
/// ```
/// # #[cfg(feature = "parser")]
/// # use sqlparser::parser::{Parser, ParserError};
/// # use sqlparser::ast::Spanned;
/// # #[cfg(feature = "parser")]
/// # use sqlparser::dialect::GenericDialect;
/// # use sqlparser::tokenizer::Location;
/// # #[cfg(feature = "parser")]
/// # fn main() -> Result<(), ParserError> {
/// let dialect = GenericDialect {};
/// let sql = r#"SELECT *
Expand All @@ -78,6 +81,9 @@ fn union_spans<I: Iterator<Item = Span>>(iter: I) -> Span {
/// assert_eq!(span.end, Location::new(2, 15));
/// # Ok(())
/// # }
/// #
/// # #[cfg(not(feature = "parser"))]
/// # fn main() {}
/// ```
///
pub trait Spanned {
Expand Down Expand Up @@ -2167,6 +2173,7 @@ impl Spanned for TableObject {
}

#[cfg(test)]
#[cfg(feature = "parser")]
pub mod tests {
use crate::dialect::{Dialect, GenericDialect, SnowflakeDialect};
use crate::parser::Parser;
Expand Down
17 changes: 15 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,23 @@
//! # Example parsing SQL text
//!
//! ```
//! # #[cfg(feature = "parser")]
//! use sqlparser::dialect::GenericDialect;
//! # #[cfg(feature = "parser")]
//! use sqlparser::parser::Parser;
//!
//! # #[cfg(feature = "parser")]
//! let dialect = GenericDialect {}; // or AnsiDialect
//!
//! let sql = "SELECT a, b, 123, myfunc(b) \
//! FROM table_1 \
//! WHERE a > b AND b < 100 \
//! ORDER BY a DESC, b";
//!
//! # #[cfg(feature = "parser")]
//! let ast = Parser::parse_sql(&dialect, sql).unwrap();
//!
//! # #[cfg(feature = "parser")]
//! println!("AST: {:?}", ast);
//! ```
//!
Expand All @@ -54,13 +59,17 @@
//! useful for tools that analyze and manipulate SQL.
//!
//! ```
//! # #[cfg(feature = "parser")]
//! # use sqlparser::dialect::GenericDialect;
//! # #[cfg(feature = "parser")]
//! # use sqlparser::parser::Parser;
//! let sql = "SELECT a FROM table_1";
//!
//! # #[cfg(feature = "parser")]
//! // parse to a Vec<Statement>
//! let ast = Parser::parse_sql(&GenericDialect, sql).unwrap();
//!
//! # #[cfg(feature = "parser")]
//! // The original SQL text can be generated from the AST
//! assert_eq!(ast[0].to_string(), sql);
//! ```
Expand Down Expand Up @@ -141,13 +150,17 @@ extern crate pretty_assertions;

pub mod ast;
#[macro_use]
#[cfg(feature = "parser")]
pub mod dialect;
pub mod keywords;
pub mod parser;
pub mod tokenizer;

#[doc(hidden)]
// This is required to make utilities accessible by both the crate-internal
// unit-tests and by the integration tests <https://stackoverflow.com/a/44541071/1026>
// External users are not supposed to rely on this module.
// External users are not supposed to rely on these modules.
#[doc(hidden)]
#[cfg(feature = "parser")]
pub mod test_dialect_utils;
#[doc(hidden)]
pub mod test_utils;
23 changes: 21 additions & 2 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,36 @@ use alloc::{
vec,
vec::Vec,
};
#[allow(unused_imports)]
use core::{
fmt::{self, Display},
str::FromStr,
};
#[allow(unused_imports)]
use helpers::attached_token::AttachedToken;

#[allow(unused_imports)]
use log::debug;

#[cfg(feature = "parser")]
use recursion::RecursionCounter;
#[allow(unused_imports)]
use IsLateral::*;
#[allow(unused_imports)]
use IsOptional::*;

#[cfg(feature = "parser")]
use crate::ast::helpers::stmt_create_table::{CreateTableBuilder, CreateTableConfiguration};
#[allow(unused_imports)]
use crate::ast::Statement::CreatePolicy;
use crate::ast::*;
#[cfg(feature = "parser")]
use crate::dialect::*;
#[allow(unused_imports)]
use crate::keywords::{Keyword, ALL_KEYWORDS};
use crate::tokenizer::*;

#[cfg(feature = "parser")]
mod alter;

#[derive(Debug, Clone, PartialEq, Eq)]
Expand All @@ -49,13 +60,15 @@ pub enum ParserError {
}

// Use `Parser::expected` instead, if possible
#[allow(unused_macros)]
macro_rules! parser_err {
($MSG:expr, $loc:expr) => {
Err(ParserError::ParserError(format!("{}{}", $MSG, $loc)))
};
}

#[cfg(feature = "std")]
#[cfg(feature = "parser")]
/// Implementation [`RecursionCounter`] if std is available
mod recursion {
use std::cell::Cell;
Expand Down Expand Up @@ -184,9 +197,11 @@ impl fmt::Display for ParserError {
impl std::error::Error for ParserError {}

// By default, allow expressions up to this deep before erroring
#[allow(unused)]
const DEFAULT_REMAINING_DEPTH: usize = 50;

// A constant EOF token that can be referenced.
#[allow(unused)]
const EOF_TOKEN: TokenWithSpan = TokenWithSpan {
token: Token::EOF,
span: Span {
Expand All @@ -207,8 +222,10 @@ const EOF_TOKEN: TokenWithSpan = TokenWithSpan {
/// child type.
///
/// See [Parser::parse_data_type] for details
#[cfg(feature = "parser")]
struct MatchedTrailingBracket(bool);

#[cfg(feature = "parser")]
impl From<bool> for MatchedTrailingBracket {
fn from(value: bool) -> Self {
Self(value)
Expand Down Expand Up @@ -264,6 +281,7 @@ impl ParserOptions {
}

#[derive(Copy, Clone)]
#[allow(unused)]
enum ParserState {
/// The default state of the parser.
Normal,
Expand Down Expand Up @@ -309,8 +327,7 @@ enum ParserState {
/// "foo"
/// ]
/// ```
///
///
#[cfg(feature = "parser")]
pub struct Parser<'a> {
/// The tokens
tokens: Vec<TokenWithSpan>,
Expand All @@ -328,6 +345,7 @@ pub struct Parser<'a> {
recursion_counter: RecursionCounter,
}

#[cfg(feature = "parser")]
impl<'a> Parser<'a> {
/// Create a parser for a [`Dialect`]
///
Expand Down Expand Up @@ -14271,6 +14289,7 @@ impl Word {
}

#[cfg(test)]
#[cfg(feature = "parser")]
mod tests {
use crate::test_utils::{all_dialects, TestedDialects};

Expand Down
Loading