refactor: remove source and location in snafu display (#2428)

* refactor: remove source pt 1

* refactor: remove source pt 2

* refactor: remove source pt 3

* refactor: remove location pt 1

* refactor: remove location pt 2

* chore: remove rustc files

* chore: fix error case

* chore: fix test

* chore: fix test

* chore: fix cr issue

Co-authored-by: fys <40801205+fengys1996@users.noreply.github.com>

---------

Co-authored-by: fys <40801205+fengys1996@users.noreply.github.com>
This commit is contained in:
shuiyisong
2023-09-21 10:55:24 +08:00
committed by GitHub
parent 9863e501f1
commit 20f4f7971a
51 changed files with 680 additions and 1031 deletions

View File

@@ -36,11 +36,10 @@ pub enum Error {
Unsupported { sql: String, keyword: String },
#[snafu(display(
"Unexpected token while parsing SQL statement: {}, expected: '{}', found: {}, source: {}",
"Unexpected token while parsing SQL statement: {}, expected: '{}', found: {}",
sql,
expected,
actual,
source
))]
Unexpected {
sql: String,
@@ -57,7 +56,7 @@ pub enum Error {
UnsupportedDefaultValue { column_name: String, expr: Expr },
// Syntax error from sql parser.
#[snafu(display("Syntax error, source: {}, sql: {}", source, sql))]
#[snafu(display("Syntax error, sql: {}", sql))]
Syntax { sql: String, source: ParserError },
#[snafu(display("Missing time index constraint"))]
@@ -96,7 +95,7 @@ pub enum Error {
#[snafu(display("Invalid table name: {}", name))]
InvalidTableName { name: String },
#[snafu(display("Invalid default constraint, column: {}, source: {}", column, source))]
#[snafu(display("Invalid default constraint, column: {}", column))]
InvalidDefault {
column: String,
location: Location,
@@ -114,16 +113,13 @@ pub enum Error {
#[snafu(display("Invalid table option key: {}", key))]
InvalidTableOption { key: String, location: Location },
#[snafu(display("Failed to serialize column default constraint, source: {}", source))]
#[snafu(display("Failed to serialize column default constraint"))]
SerializeColumnDefaultConstraint {
location: Location,
source: datatypes::error::Error,
},
#[snafu(display(
"Failed to convert data type to gRPC data type defined in proto, source: {}",
source
))]
#[snafu(display("Failed to convert data type to gRPC data type defined in proto"))]
ConvertToGrpcDataType {
location: Location,
source: api::error::Error,

View File

@@ -98,6 +98,7 @@ impl<'a> ParserContext<'a> {
mod tests {
use std::assert_matches::assert_matches;
use snafu::ErrorCompat;
use sqlparser::ast::{ColumnOption, DataType};
use super::*;
@@ -214,9 +215,8 @@ mod tests {
fn test_parse_alter_drop_column() {
let sql = "ALTER TABLE my_metric_1 DROP a";
let result = ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}).unwrap_err();
assert!(result
.to_string()
.contains("expect keyword COLUMN after ALTER TABLE DROP"));
let err = result.iter_chain().last().unwrap().to_string();
assert!(err.contains("expect keyword COLUMN after ALTER TABLE DROP"));
let sql = "ALTER TABLE my_metric_1 DROP COLUMN a";
let mut result = ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}).unwrap();
@@ -245,9 +245,8 @@ mod tests {
fn test_parse_alter_rename_table() {
let sql = "ALTER TABLE test_table table_t";
let result = ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}).unwrap_err();
assert!(result
.to_string()
.contains("expect keyword ADD or DROP or RENAME after ALTER TABLE"));
let err = result.iter_chain().last().unwrap().to_string();
assert!(err.contains("expect keyword ADD or DROP or RENAME after ALTER TABLE"));
let sql = "ALTER TABLE test_table RENAME table_t";
let mut result = ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}).unwrap();

View File

@@ -837,6 +837,7 @@ mod tests {
use std::collections::HashMap;
use common_catalog::consts::FILE_ENGINE;
use snafu::ErrorCompat;
use sqlparser::ast::ColumnOption::NotNull;
use super::*;
@@ -1400,6 +1401,9 @@ ENGINE=mito";
let result = ParserContext::create_with_dialect(sql, &GreptimeDbDialect {});
assert!(result
.unwrap_err()
.iter_chain()
.last()
.unwrap()
.to_string()
.contains("sql parser error: Expected BY, found: RANGE"));
@@ -1414,6 +1418,9 @@ ENGINE=mito";
let result = ParserContext::create_with_dialect(sql, &GreptimeDbDialect {});
assert!(result
.unwrap_err()
.iter_chain()
.last()
.unwrap()
.to_string()
.contains("sql parser error: Expected LESS, found: THAN"));
@@ -1428,6 +1435,9 @@ ENGINE=mito";
let result = ParserContext::create_with_dialect(sql, &GreptimeDbDialect {});
assert!(result
.unwrap_err()
.iter_chain()
.last()
.unwrap()
.to_string()
.contains("Expected a concrete value, found: MAXVALU"));
}
@@ -1537,10 +1547,8 @@ ENGINE=mito";
fn test_invalid_column_name() {
let sql = "create table foo(user string, i timestamp time index)";
let result = ParserContext::create_with_dialect(sql, &GreptimeDbDialect {});
assert!(result
.unwrap_err()
.to_string()
.contains("Cannot use keyword 'user' as column name"));
let err = result.unwrap_err().iter_chain().last().unwrap().to_string();
assert!(err.contains("Cannot use keyword 'user' as column name"));
// If column name is quoted, it's valid even same with keyword.
let sql = r#"

View File

@@ -33,6 +33,8 @@ impl<'a> ParserContext<'a> {
#[cfg(test)]
mod tests {
use snafu::ErrorCompat;
use crate::dialect::GreptimeDbDialect;
use crate::parser::ParserContext;
@@ -53,6 +55,9 @@ mod tests {
assert!(result.is_err());
assert!(result
.unwrap_err()
.iter_chain()
.last()
.unwrap()
.to_string()
.contains("Expected an expression"));
}

View File

@@ -166,6 +166,8 @@ impl<'a> ParserContext<'a> {
#[cfg(test)]
mod tests {
use snafu::ErrorCompat;
use super::*;
use crate::dialect::GreptimeDbDialect;
#[test]
@@ -284,11 +286,21 @@ mod tests {
// Invalid duration
let sql = "TQL EVAL (1676887657, 1676887659, 1m) http_requests_total{environment=~'staging|testing|development',method!='GET'} @ 1609746000 offset 5m";
let result = ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}).unwrap_err();
assert!(result.to_string().contains("Expected ), found: m"));
assert!(result
.iter_chain()
.last()
.unwrap()
.to_string()
.contains("Expected ), found: m"));
// missing end
let sql = "TQL EVAL (1676887657, '1m') http_requests_total{environment=~'staging|testing|development',method!='GET'} @ 1609746000 offset 5m";
let result = ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}).unwrap_err();
assert!(result.to_string().contains("Expected ,, found: )"));
assert!(result
.iter_chain()
.last()
.unwrap()
.to_string()
.contains("Expected ,, found: )"));
}
}