Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into feat-multi-catalog
Browse files Browse the repository at this point in the history
  • Loading branch information
dantengsky committed Apr 22, 2022
2 parents e9894e5 + 1c3fcf2 commit 09b199c
Show file tree
Hide file tree
Showing 47 changed files with 523 additions and 456 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ use std::fmt::Formatter;

use sqlparser::ast::Value;

use super::Identifier;
use super::Query;
use crate::parser::ast::display_identifier_vec;
use crate::ast::display_identifier_vec;
use crate::ast::Identifier;
use crate::ast::Query;

#[derive(Debug, Clone, PartialEq)]
#[allow(dead_code)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.

mod ast_visitor;
mod expression;
mod expr;
mod query;
mod statement;

use std::fmt::Display;
use std::fmt::Formatter;

pub use expression::*;
pub use expr::*;
pub use query::*;
pub use statement::*;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
use std::fmt::Display;
use std::fmt::Formatter;

use crate::parser::ast::display_identifier_vec;
use crate::parser::ast::Expr;
use crate::parser::ast::Identifier;
use crate::ast::display_identifier_vec;
use crate::ast::Expr;
use crate::ast::Identifier;

// Root node of a query tree
#[derive(Debug, Clone, PartialEq)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
use std::fmt::Display;
use std::fmt::Formatter;

use super::expression::Literal;
use super::expression::TypeName;
use super::Identifier;
use crate::parser::ast::display_identifier_vec;
use crate::parser::ast::query::Query;
use crate::ast::display_identifier_vec;
use crate::ast::expr::Literal;
use crate::ast::expr::TypeName;
use crate::ast::Identifier;
use crate::ast::Query;

// SQL statement
#[derive(Debug, Clone, PartialEq)]
Expand Down
1 change: 1 addition & 0 deletions common/ast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

pub mod ast;
pub mod parser;
pub mod udfs;
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

use std::ops::Range;

use crate::parser::rule::util::Input;
use crate::parser::token::TokenKind;
use crate::parser::util::Input;

/// This error type accumulates errors and their position when backtracking
/// through a parse tree. This take a deepest error at `alt` combinator.
Expand Down Expand Up @@ -121,3 +121,41 @@ impl<'a> Error<'a> {
.collect()
}
}

pub fn pretty_print_error(source: &str, lables: Vec<(Range<usize>, String)>) -> String {
use codespan_reporting::diagnostic::Diagnostic;
use codespan_reporting::diagnostic::Label;
use codespan_reporting::files::SimpleFile;
use codespan_reporting::term;
use codespan_reporting::term::termcolor::Buffer;
use codespan_reporting::term::Chars;
use codespan_reporting::term::Config;

let mut writer = Buffer::no_color();
let file = SimpleFile::new("SQL", source);
let config = Config {
chars: Chars::ascii(),
before_label_lines: 3,
..Default::default()
};

let lables = lables
.into_iter()
.enumerate()
.map(|(i, (span, msg))| {
if i == 0 {
Label::primary((), span).with_message(msg)
} else {
Label::secondary((), span).with_message(msg)
}
})
.collect();

let diagnostic = Diagnostic::error().with_labels(lables);

term::emit(&mut writer, &config, &file, &diagnostic).unwrap();

std::str::from_utf8(&writer.into_inner())
.unwrap()
.to_string()
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ use pratt::PrattError;
use pratt::PrattParser;
use pratt::Precedence;

use crate::parser::ast::*;
use crate::parser::rule::error::Error;
use crate::parser::rule::error::ErrorKind;
use crate::parser::rule::statement::*;
use crate::parser::rule::util::*;
use crate::ast::*;
use crate::parser::error::Error;
use crate::parser::error::ErrorKind;
use crate::parser::query::*;
use crate::parser::token::*;
use crate::parser::util::*;
use crate::rule;

const BETWEEN_PREC: u32 = 20;
Expand Down
18 changes: 0 additions & 18 deletions common/ast/src/parser/expr/mod.rs

This file was deleted.

56 changes: 10 additions & 46 deletions common/ast/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,30 @@
// See the License for the specific language governing permissions and
// limitations under the License.

pub mod ast;
pub mod error;
pub mod expr;
pub mod rule;
pub mod query;
pub mod statement;
pub mod token;

use std::ops::Range;
pub mod util;

use common_exception::ErrorCode;
use common_exception::Result;
use nom::combinator::map;

use self::rule::statement::statement;
use self::token::TokenKind;
use self::token::Tokenizer;
use crate::parser::ast::Statement;
use crate::ast::Statement;
use crate::parser::error::pretty_print_error;
use crate::parser::statement::statement;
use crate::parser::token::TokenKind;
use crate::parser::token::Tokenizer;
use crate::rule;

/// Parse a SQL string into `Statement`s.
pub fn parse_sql(sql: &str) -> Result<Vec<Statement>> {
let tokens = Tokenizer::new(sql).collect::<Result<Vec<_>>>()?;
let stmt = map(rule! { #statement ~ ";" }, |(stmt, _)| stmt);
let mut stmts = rule! { #stmt+ };

match stmts(tokens.as_slice()) {
Ok((rest, stmts)) if rest[0].kind == TokenKind::EOI => Ok(stmts),
Ok((rest, _)) => Err(ErrorCode::SyntaxException(pretty_print_error(sql, vec![(
Expand All @@ -46,41 +48,3 @@ pub fn parse_sql(sql: &str) -> Result<Vec<Statement>> {
Err(nom::Err::Incomplete(_)) => unreachable!(),
}
}

pub fn pretty_print_error(source: &str, lables: Vec<(Range<usize>, String)>) -> String {
use codespan_reporting::diagnostic::Diagnostic;
use codespan_reporting::diagnostic::Label;
use codespan_reporting::files::SimpleFile;
use codespan_reporting::term;
use codespan_reporting::term::termcolor::Buffer;
use codespan_reporting::term::Chars;
use codespan_reporting::term::Config;

let mut writer = Buffer::no_color();
let file = SimpleFile::new("SQL", source);
let config = Config {
chars: Chars::ascii(),
before_label_lines: 3,
..Default::default()
};

let lables = lables
.into_iter()
.enumerate()
.map(|(i, (span, msg))| {
if i == 0 {
Label::primary((), span).with_message(msg)
} else {
Label::secondary((), span).with_message(msg)
}
})
.collect();

let diagnostic = Diagnostic::error().with_labels(lables);

term::emit(&mut writer, &config, &file, &diagnostic).unwrap();

std::str::from_utf8(&writer.into_inner())
.unwrap()
.to_string()
}
Loading

0 comments on commit 09b199c

Please sign in to comment.