From 3e880b599a36a6c1eca73b7427b0faea7d7a1eed Mon Sep 17 00:00:00 2001 From: Nickolay Ponomarev Date: Mon, 27 Jul 2020 21:23:48 +0300 Subject: [PATCH 1/4] Use consistent style for Display impls --- src/ast/mod.rs | 84 ++++++++++++++++++++------------------------------ 1 file changed, 33 insertions(+), 51 deletions(-) diff --git a/src/ast/mod.rs b/src/ast/mod.rs index 41bff69d..fa05b171 100644 --- a/src/ast/mod.rs +++ b/src/ast/mod.rs @@ -299,7 +299,7 @@ impl fmt::Display for Expr { results, else_result, } => { - f.write_str("CASE")?; + write!(f, "CASE")?; if let Some(operand) = operand { write!(f, " {}", operand)?; } @@ -310,7 +310,7 @@ impl fmt::Display for Expr { if let Some(else_result) = else_result { write!(f, " ELSE {}", else_result)?; } - f.write_str(" END") + write!(f, " END") } Expr::Exists(s) => write!(f, "EXISTS ({})", s), Expr::Subquery(s) => write!(f, "({})", s), @@ -626,8 +626,7 @@ impl fmt::Display for Statement { } => { write!(f, "UPDATE {}", table_name)?; if !assignments.is_empty() { - write!(f, " SET ")?; - write!(f, "{}", display_comma_separated(assignments))?; + write!(f, " SET {}", display_comma_separated(assignments))?; } if let Some(selection) = selection { write!(f, " WHERE {}", selection)?; @@ -652,26 +651,19 @@ impl fmt::Display for Statement { materialized, with_options, } => { - write!(f, "CREATE")?; - - if *or_replace { - write!(f, " OR REPLACE")?; - } - - if *materialized { - write!(f, " MATERIALIZED")?; - } - - write!(f, " VIEW {}", name)?; - + write!( + f, + "CREATE {or_replace}{materialized}VIEW {name}", + or_replace = if *or_replace { "OR REPLACE " } else { "" }, + materialized = if *materialized { "MATERIALIZED " } else { "" }, + name = name + )?; if !with_options.is_empty() { write!(f, " WITH ({})", display_comma_separated(with_options))?; } - if !columns.is_empty() { write!(f, " ({})", display_comma_separated(columns))?; } - write!(f, " AS {}", query) } Statement::CreateTable { @@ -716,7 +708,6 @@ impl fmt::Display for Statement { if *without_rowid { write!(f, " WITHOUT ROWID")?; } - if *external { write!( f, @@ -757,22 +748,15 @@ impl fmt::Display for Statement { columns, unique, if_not_exists, - } => { - write!( - f, - "CREATE{}INDEX{}{} ON {}({}", - if *unique { " UNIQUE " } else { " " }, - if *if_not_exists { - " IF NOT EXISTS " - } else { - " " - }, - name, - table_name, - display_separated(columns, ",") - )?; - write!(f, ");") - } + } => write!( + f, + "CREATE {unique}INDEX {if_not_exists}{name} ON {table_name}({columns});", + unique = if *unique { "UNIQUE " } else { "" }, + if_not_exists = if *if_not_exists { "IF NOT EXISTS " } else { "" }, + name = name, + table_name = table_name, + columns = display_separated(columns, ",") + ), Statement::AlterTable { name, operation } => { write!(f, "ALTER TABLE {} {}", name, operation) } @@ -793,13 +777,13 @@ impl fmt::Display for Statement { local, variable, value, - } => { - f.write_str("SET ")?; - if *local { - f.write_str("LOCAL ")?; - } - write!(f, "{} = {}", variable, value) - } + } => write!( + f, + "SET{local} {variable} = {value}", + local = if *local { " LOCAL" } else { "" }, + variable = variable, + value = value + ), Statement::ShowVariable { variable } => write!(f, "SHOW {}", variable), Statement::ShowColumns { extended, @@ -807,14 +791,13 @@ impl fmt::Display for Statement { table_name, filter, } => { - f.write_str("SHOW ")?; - if *extended { - f.write_str("EXTENDED ")?; - } - if *full { - f.write_str("FULL ")?; - } - write!(f, "COLUMNS FROM {}", table_name)?; + write!( + f, + "SHOW {extended}{full}COLUMNS FROM {table_name}", + extended = if *extended { "EXTENDED " } else { "" }, + full = if *full { "FULL " } else { "" }, + table_name = table_name, + )?; if let Some(filter) = filter { write!(f, " {}", filter)?; } @@ -843,7 +826,6 @@ impl fmt::Display for Statement { Statement::CreateSchema { schema_name } => write!(f, "CREATE SCHEMA {}", schema_name), Statement::Assert { condition, message } => { write!(f, "ASSERT {}", condition)?; - if let Some(m) = message { write!(f, " AS {}", m)?; } From 9371652446b2148fb04f9a0cf8adebdb86d63ea9 Mon Sep 17 00:00:00 2001 From: Nickolay Ponomarev Date: Tue, 28 Jul 2020 23:49:32 +0300 Subject: [PATCH 2/4] Fix "unused stmt" warning in tests, with default features --- tests/sqlparser_postgres.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/sqlparser_postgres.rs b/tests/sqlparser_postgres.rs index f45913a5..2b950c0d 100644 --- a/tests/sqlparser_postgres.rs +++ b/tests/sqlparser_postgres.rs @@ -474,14 +474,12 @@ fn parse_execute() { ); let stmt = pg_and_generic().verified_stmt("EXECUTE a(1, 't')"); - - #[cfg(feature = "bigdecimal")] assert_eq!( stmt, Statement::Execute { name: "a".into(), parameters: vec![ - Expr::Value(Value::Number(bigdecimal::BigDecimal::from(1))), + Expr::Value(number("1")), Expr::Value(Value::SingleQuotedString("t".to_string())) ], } From 9a2d86dcb5bc32b0fcdb993d8d22e670871fe018 Mon Sep 17 00:00:00 2001 From: Nickolay Ponomarev Date: Wed, 29 Jul 2020 00:04:40 +0300 Subject: [PATCH 3/4] Change CREATE INDEX serialization to not end with a semicolon --- src/ast/mod.rs | 2 +- tests/sqlparser_common.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ast/mod.rs b/src/ast/mod.rs index fa05b171..332fce63 100644 --- a/src/ast/mod.rs +++ b/src/ast/mod.rs @@ -750,7 +750,7 @@ impl fmt::Display for Statement { if_not_exists, } => write!( f, - "CREATE {unique}INDEX {if_not_exists}{name} ON {table_name}({columns});", + "CREATE {unique}INDEX {if_not_exists}{name} ON {table_name}({columns})", unique = if *unique { "UNIQUE " } else { "" }, if_not_exists = if *if_not_exists { "IF NOT EXISTS " } else { "" }, name = name, diff --git a/tests/sqlparser_common.rs b/tests/sqlparser_common.rs index 5889cc35..54da1bc0 100644 --- a/tests/sqlparser_common.rs +++ b/tests/sqlparser_common.rs @@ -3152,7 +3152,7 @@ fn ensure_multiple_dialects_are_tested() { #[test] fn parse_create_index() { - let sql = "CREATE UNIQUE INDEX IF NOT EXISTS idx_name ON test(name,age);"; + let sql = "CREATE UNIQUE INDEX IF NOT EXISTS idx_name ON test(name,age)"; let ident_vec = vec![Ident::new("name"), Ident::new("age")]; match verified_stmt(sql) { Statement::CreateIndex { From a6e30b3fad3739993ea54e36d2dfb9031d2e91f1 Mon Sep 17 00:00:00 2001 From: Nickolay Ponomarev Date: Wed, 29 Jul 2020 00:14:22 +0300 Subject: [PATCH 4/4] Fix typo in JSONFILE serialization Closes https://github.com/ballista-compute/sqlparser-rs/issues/237 --- src/ast/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ast/mod.rs b/src/ast/mod.rs index 332fce63..3c2f5c4f 100644 --- a/src/ast/mod.rs +++ b/src/ast/mod.rs @@ -923,7 +923,7 @@ impl fmt::Display for FileFormat { PARQUET => "PARQUET", AVRO => "AVRO", RCFILE => "RCFILE", - JSONFILE => "TEXTFILE", + JSONFILE => "JSONFILE", }) } }