mirror of
https://github.com/GreptimeTeam/greptimedb.git
synced 2025-12-22 22:20:02 +00:00
refactor: Rename errors::ParserError to error::Error
This commit is contained in:
@@ -19,7 +19,7 @@ pub enum InnerError {
|
||||
|
||||
// The sql error already contains the SQL.
|
||||
#[snafu(display("Cannot parse SQL, source: {}", source))]
|
||||
ParseSql { source: sql::errors::ParserError },
|
||||
ParseSql { source: sql::error::Error },
|
||||
|
||||
#[snafu(display("Cannot plan SQL: {}, source: {}", sql, source))]
|
||||
PlanSql {
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
use common_error::prelude::*;
|
||||
use sqlparser::parser::ParserError as SpParserError;
|
||||
use sqlparser::parser::ParserError;
|
||||
|
||||
/// SQL parser errors.
|
||||
// Now the error in parser does not contains backtrace to avoid generating backtrace
|
||||
// every time the parser parses an invalid SQL.
|
||||
#[derive(Debug, Snafu)]
|
||||
#[snafu(visibility(pub))]
|
||||
pub enum ParserError {
|
||||
pub enum Error {
|
||||
#[snafu(display("SQL statement is not supported: {}, keyword: {}", sql, keyword))]
|
||||
Unsupported { sql: String, keyword: String },
|
||||
|
||||
@@ -21,19 +21,21 @@ pub enum ParserError {
|
||||
sql: String,
|
||||
expected: String,
|
||||
actual: String,
|
||||
source: SpParserError,
|
||||
source: ParserError,
|
||||
},
|
||||
|
||||
// Syntax error from sql parser.
|
||||
#[snafu(display("Syntax error, sql: {}, source: {}", sql, source))]
|
||||
SpSyntax { sql: String, source: SpParserError },
|
||||
Syntax { sql: String, source: ParserError },
|
||||
}
|
||||
|
||||
impl ErrorExt for ParserError {
|
||||
impl ErrorExt for Error {
|
||||
fn status_code(&self) -> StatusCode {
|
||||
use Error::*;
|
||||
|
||||
match self {
|
||||
Self::Unsupported { .. } => StatusCode::Unsupported,
|
||||
Self::Unexpected { .. } | Self::SpSyntax { .. } => StatusCode::InvalidSyntax,
|
||||
Unsupported { .. } => StatusCode::Unsupported,
|
||||
Unexpected { .. } | Syntax { .. } => StatusCode::InvalidSyntax,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,21 +50,21 @@ mod tests {
|
||||
|
||||
use super::*;
|
||||
|
||||
fn raise_sp_error() -> Result<(), SpParserError> {
|
||||
Err(SpParserError::ParserError("parser error".to_string()))
|
||||
fn raise_sp_error() -> Result<(), ParserError> {
|
||||
Err(ParserError::ParserError("parser error".to_string()))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_syntax_error() {
|
||||
let err = raise_sp_error()
|
||||
.context(SpSyntaxSnafu { sql: "" })
|
||||
.context(SyntaxSnafu { sql: "" })
|
||||
.err()
|
||||
.unwrap();
|
||||
assert_matches!(
|
||||
err,
|
||||
ParserError::SpSyntax {
|
||||
Error::Syntax {
|
||||
sql: _,
|
||||
source: SpParserError::ParserError { .. }
|
||||
source: ParserError::ParserError { .. }
|
||||
}
|
||||
);
|
||||
assert_eq!(StatusCode::InvalidSyntax, err.status_code());
|
||||
@@ -80,7 +82,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_unsupported_error() {
|
||||
let err = ParserError::Unsupported {
|
||||
let err = Error::Unsupported {
|
||||
sql: "".to_string(),
|
||||
keyword: "".to_string(),
|
||||
};
|
||||
@@ -4,7 +4,7 @@ extern crate core;
|
||||
|
||||
pub mod ast;
|
||||
pub mod dialect;
|
||||
pub mod errors;
|
||||
pub mod error;
|
||||
pub mod parser;
|
||||
pub mod parsers;
|
||||
pub mod statements;
|
||||
|
||||
@@ -4,12 +4,12 @@ use sqlparser::keywords::Keyword;
|
||||
use sqlparser::parser::Parser;
|
||||
use sqlparser::tokenizer::{Token, Tokenizer};
|
||||
|
||||
use crate::errors;
|
||||
use crate::error::{self, Error};
|
||||
use crate::statements::show_database::SqlShowDatabase;
|
||||
use crate::statements::show_kind::ShowKind;
|
||||
use crate::statements::statement::Statement;
|
||||
|
||||
pub type Result<T> = std::result::Result<T, errors::ParserError>;
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
/// GrepTime SQL parser context, a simple wrapper for Datafusion SQL parser.
|
||||
pub struct ParserContext<'a> {
|
||||
@@ -87,10 +87,11 @@ impl<'a> ParserContext<'a> {
|
||||
|
||||
/// Raises an "unsupported statement" error.
|
||||
pub fn unsupported<T>(&self, keyword: String) -> Result<T> {
|
||||
Err(errors::ParserError::Unsupported {
|
||||
sql: self.sql.to_string(),
|
||||
error::UnsupportedSnafu {
|
||||
sql: self.sql,
|
||||
keyword,
|
||||
})
|
||||
}
|
||||
.fail()
|
||||
}
|
||||
|
||||
/// Parses SHOW statements
|
||||
@@ -137,7 +138,7 @@ impl<'a> ParserContext<'a> {
|
||||
ShowKind::Like(
|
||||
self.parser
|
||||
.parse_identifier()
|
||||
.context(errors::UnexpectedSnafu {
|
||||
.context(error::UnexpectedSnafu {
|
||||
sql: self.sql,
|
||||
expected: "LIKE",
|
||||
actual: tok.to_string(),
|
||||
@@ -146,7 +147,7 @@ impl<'a> ParserContext<'a> {
|
||||
),
|
||||
))),
|
||||
Keyword::WHERE => Ok(Statement::ShowDatabases(SqlShowDatabase::new(
|
||||
ShowKind::Where(self.parser.parse_expr().context(errors::UnexpectedSnafu {
|
||||
ShowKind::Where(self.parser.parse_expr().context(error::UnexpectedSnafu {
|
||||
sql: self.sql.to_string(),
|
||||
expected: "some valid expression".to_string(),
|
||||
actual: self.peek_token_as_string(),
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use snafu::ResultExt;
|
||||
use sqlparser::ast::Statement as SpStatement;
|
||||
|
||||
use crate::errors;
|
||||
use crate::error;
|
||||
use crate::parser::ParserContext;
|
||||
use crate::parser::Result;
|
||||
use crate::statements::insert::Insert;
|
||||
@@ -14,13 +14,13 @@ impl<'a> ParserContext<'a> {
|
||||
let spstatement = self
|
||||
.parser
|
||||
.parse_insert()
|
||||
.context(errors::SpSyntaxSnafu { sql: self.sql })?;
|
||||
.context(error::SyntaxSnafu { sql: self.sql })?;
|
||||
|
||||
match spstatement {
|
||||
SpStatement::Insert { .. } => {
|
||||
Ok(Statement::Insert(Box::new(Insert { inner: spstatement })))
|
||||
}
|
||||
unexp => errors::UnsupportedSnafu {
|
||||
unexp => error::UnsupportedSnafu {
|
||||
sql: self.sql.to_string(),
|
||||
keyword: unexp.to_string(),
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use snafu::prelude::*;
|
||||
|
||||
use crate::errors;
|
||||
use crate::error;
|
||||
use crate::parser::ParserContext;
|
||||
use crate::parser::Result;
|
||||
use crate::statements::query::Query;
|
||||
@@ -12,7 +12,7 @@ impl<'a> ParserContext<'a> {
|
||||
let spquery = self
|
||||
.parser
|
||||
.parse_query()
|
||||
.context(errors::SpSyntaxSnafu { sql: self.sql })?;
|
||||
.context(error::SyntaxSnafu { sql: self.sql })?;
|
||||
|
||||
Ok(Statement::Query(Box::new(Query::try_from(spquery)?)))
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use sqlparser::ast::Query as SpQuery;
|
||||
|
||||
use crate::errors::ParserError;
|
||||
use crate::error::Error;
|
||||
|
||||
/// Query statement instance.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
@@ -10,7 +10,7 @@ pub struct Query {
|
||||
|
||||
/// Automatically converts from sqlparser Query instance to SqlQuery.
|
||||
impl TryFrom<SpQuery> for Query {
|
||||
type Error = ParserError;
|
||||
type Error = Error;
|
||||
|
||||
fn try_from(q: SpQuery) -> Result<Self, Self::Error> {
|
||||
Ok(Query { inner: q })
|
||||
@@ -18,7 +18,7 @@ impl TryFrom<SpQuery> for Query {
|
||||
}
|
||||
|
||||
impl TryFrom<Query> for SpQuery {
|
||||
type Error = ParserError;
|
||||
type Error = Error;
|
||||
|
||||
fn try_from(value: Query) -> Result<Self, Self::Error> {
|
||||
Ok(value.inner)
|
||||
|
||||
Reference in New Issue
Block a user