Skip to content

Commit

Permalink
upgrade sqlparser 0.47 -> 0.48
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamedAbdeen21 committed Jul 13, 2024
1 parent dc21a6c commit a47e58c
Show file tree
Hide file tree
Showing 30 changed files with 194 additions and 631 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ rand = "0.8"
regex = "1.8"
rstest = "0.21.0"
serde_json = "1"
sqlparser = { version = "0.47", features = ["visitor"] }
sqlparser = { version = "0.48", features = ["visitor"] }
tempfile = "3"
thiserror = "1.0.44"
tokio = { version = "1.36", features = ["macros", "rt", "sync"] }
Expand Down
1 change: 1 addition & 0 deletions datafusion/sql/src/expr/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ impl FunctionArgs {
filter,
mut null_treatment,
within_group,
..
} = function;

// Handle no argument form (aka `current_time` as opposed to `current_time()`)
Expand Down
18 changes: 11 additions & 7 deletions datafusion/sql/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1006,14 +1006,15 @@ mod tests {
expect_parse_ok(sql, expected)?;

// positive case: it is ok for sql stmt with `COMPRESSION TYPE GZIP` tokens
let sqls = vec![
("CREATE EXTERNAL TABLE t(c1 int) STORED AS CSV LOCATION 'foo.csv' OPTIONS
let sqls =
vec![
("CREATE EXTERNAL TABLE t(c1 int) STORED AS CSV LOCATION 'foo.csv' OPTIONS
('format.compression' 'GZIP')", "GZIP"),
("CREATE EXTERNAL TABLE t(c1 int) STORED AS CSV LOCATION 'foo.csv' OPTIONS
("CREATE EXTERNAL TABLE t(c1 int) STORED AS CSV LOCATION 'foo.csv' OPTIONS
('format.compression' 'BZIP2')", "BZIP2"),
("CREATE EXTERNAL TABLE t(c1 int) STORED AS CSV LOCATION 'foo.csv' OPTIONS
("CREATE EXTERNAL TABLE t(c1 int) STORED AS CSV LOCATION 'foo.csv' OPTIONS
('format.compression' 'XZ')", "XZ"),
("CREATE EXTERNAL TABLE t(c1 int) STORED AS CSV LOCATION 'foo.csv' OPTIONS
("CREATE EXTERNAL TABLE t(c1 int) STORED AS CSV LOCATION 'foo.csv' OPTIONS
('format.compression' 'ZSTD')", "ZSTD"),
];
for (sql, compression) in sqls {
Expand Down Expand Up @@ -1123,7 +1124,10 @@ mod tests {
// negative case: mixed column defs and column names in `PARTITIONED BY` clause
let sql =
"CREATE EXTERNAL TABLE t(c1 int) STORED AS CSV PARTITIONED BY (p1 int, c1) LOCATION 'foo.csv'";
expect_parse_error(sql, "sql parser error: Expected a data type name, found: )");
expect_parse_error(
sql,
"sql parser error: Expected: a data type name, found: )",
);

// negative case: mixed column defs and column names in `PARTITIONED BY` clause
let sql =
Expand Down Expand Up @@ -1291,7 +1295,7 @@ mod tests {
LOCATION 'foo.parquet'
OPTIONS ('format.compression' 'zstd',
'format.delimiter' '*',
'ROW_GROUP_SIZE' '1024',
'ROW_GROUP_SIZE' '1024',
'TRUNCATE' 'NO',
'format.has_header' 'true')";
let expected = Statement::CreateExternalTable(CreateExternalTable {
Expand Down
21 changes: 21 additions & 0 deletions datafusion/sql/src/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,27 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
| SQLDataType::Float64
| SQLDataType::JSONB
| SQLDataType::Unspecified
// Clickhouse datatypes
| SQLDataType::Int16
| SQLDataType::Int32
| SQLDataType::Int128
| SQLDataType::Int256
| SQLDataType::UInt8
| SQLDataType::UInt16
| SQLDataType::UInt32
| SQLDataType::UInt64
| SQLDataType::UInt128
| SQLDataType::UInt256
| SQLDataType::Float32
| SQLDataType::Date32
| SQLDataType::Datetime64(_, _)
| SQLDataType::FixedString(_)
| SQLDataType::Map(_, _)
| SQLDataType::Tuple(_)
| SQLDataType::Nested(_)
| SQLDataType::Union(_)
| SQLDataType::Nullable(_)
| SQLDataType::LowCardinality(_)
=> not_impl_err!(
"Unsupported SQL type {sql_type:?}"
),
Expand Down
2 changes: 1 addition & 1 deletion datafusion/sql/src/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
let aggr_exprs = find_aggregate_exprs(&aggr_expr_haystack);

// All of the group by expressions
let group_by_exprs = if let GroupByExpr::Expressions(exprs) = select.group_by {
let group_by_exprs = if let GroupByExpr::Expressions(exprs, _) = select.group_by {
exprs
.into_iter()
.map(|e| {
Expand Down
14 changes: 9 additions & 5 deletions datafusion/sql/src/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ use datafusion_expr::{
TransactionConclusion, TransactionEnd, TransactionIsolationLevel, TransactionStart,
Volatility, WriteOp,
};
use sqlparser::ast;
use sqlparser::ast::{self, AssignmentTarget, CreateTable};
use sqlparser::ast::{
Assignment, ColumnDef, CreateTableOptions, Delete, DescribeAlias, Expr as SQLExpr,
Expr, FromTable, Ident, Insert, ObjectName, ObjectType, OneOrManyWithParens, Query,
Expand Down Expand Up @@ -240,7 +240,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
value,
} => self.set_variable_to_plan(local, hivevar, &variables, value),

Statement::CreateTable {
Statement::CreateTable(CreateTable {
query,
name,
columns,
Expand All @@ -250,7 +250,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
if_not_exists,
or_replace,
..
} if table_properties.is_empty() && with_options.is_empty() => {
}) if table_properties.is_empty() && with_options.is_empty() => {
// Merge inline constraints and existing constraints
let mut all_constraints = constraints;
let inline_constraints = calc_inline_constraints_from_columns(&columns);
Expand Down Expand Up @@ -1284,8 +1284,12 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
let mut assign_map = assignments
.iter()
.map(|assign| {
let col_name: &Ident = assign
.id
let cols = match &assign.target {
AssignmentTarget::ColumnName(cols) => cols,
_ => plan_err!("Tuples are not supported")?,
};
let col_name: &Ident = cols
.0
.iter()
.last()
.ok_or_else(|| plan_datafusion_err!("Empty column id"))?;
Expand Down
5 changes: 4 additions & 1 deletion datafusion/sql/src/unparser/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ impl QueryBuilder {
fetch: self.fetch.clone(),
locks: self.locks.clone(),
for_clause: self.for_clause.clone(),
settings: None,
format_clause: None,
})
}
fn create_empty() -> Self {
Expand Down Expand Up @@ -234,6 +236,7 @@ impl SelectBuilder {
value_table_mode: self.value_table_mode,
connect_by: None,
window_before_qualify: false,
prewhere: None,
})
}
fn create_empty() -> Self {
Expand All @@ -245,7 +248,7 @@ impl SelectBuilder {
from: Default::default(),
lateral_views: Default::default(),
selection: Default::default(),
group_by: Some(ast::GroupByExpr::Expressions(Vec::new())),
group_by: Some(ast::GroupByExpr::Expressions(Vec::new(), Vec::new())),
cluster_by: Default::default(),
distribute_by: Default::default(),
sort_by: Default::default(),
Expand Down
3 changes: 3 additions & 0 deletions datafusion/sql/src/unparser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ impl Unparser<'_> {
null_treatment: None,
over: None,
within_group: vec![],
parameters: ast::FunctionArguments::None,
}))
}
Expr::Between(Between {
Expand Down Expand Up @@ -305,6 +306,7 @@ impl Unparser<'_> {
null_treatment: None,
over,
within_group: vec![],
parameters: ast::FunctionArguments::None,
}))
}
Expr::SimilarTo(Like {
Expand Down Expand Up @@ -350,6 +352,7 @@ impl Unparser<'_> {
null_treatment: None,
over: None,
within_group: vec![],
parameters: ast::FunctionArguments::None,
}))
}
Expr::ScalarSubquery(subq) => {
Expand Down
1 change: 1 addition & 0 deletions datafusion/sql/src/unparser/plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ impl Unparser<'_> {
.iter()
.map(|expr| self.expr_to_sql(expr))
.collect::<Result<Vec<_>>>()?,
vec![],
));
}
Some(AggVariant::Window(window)) => {
Expand Down
6 changes: 3 additions & 3 deletions datafusion/sql/tests/sql_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3627,7 +3627,7 @@ fn test_prepare_statement_to_plan_panic_prepare_wrong_syntax() {
let sql = "PREPARE AS SELECT id, age FROM person WHERE age = $foo";
assert_eq!(
logical_plan(sql).unwrap_err().strip_backtrace(),
"SQL error: ParserError(\"Expected AS, found: SELECT\")"
"SQL error: ParserError(\"Expected: AS, found: SELECT\")"
)
}

Expand Down Expand Up @@ -3668,7 +3668,7 @@ fn test_non_prepare_statement_should_infer_types() {

#[test]
#[should_panic(
expected = "value: SQL(ParserError(\"Expected [NOT] NULL or TRUE|FALSE or [NOT] DISTINCT FROM after IS, found: $1\""
expected = "value: SQL(ParserError(\"Expected: [NOT] NULL or TRUE|FALSE or [NOT] DISTINCT FROM after IS, found: $1\""
)]
fn test_prepare_statement_to_plan_panic_is_param() {
let sql = "PREPARE my_plan(INT) AS SELECT id, age FROM person WHERE age is $1";
Expand Down Expand Up @@ -4347,7 +4347,7 @@ fn test_parse_escaped_string_literal_value() {
let sql = r"SELECT character_length(E'\000') AS len";
assert_eq!(
logical_plan(sql).unwrap_err().strip_backtrace(),
"SQL error: TokenizerError(\"Unterminated encoded string literal at Line: 1, Column 25\")"
"SQL error: TokenizerError(\"Unterminated encoded string literal at Line: 1, Column: 25\")"
)
}

Expand Down
18 changes: 5 additions & 13 deletions datafusion/sqllogictest/test_files/aggregate.slt
Original file line number Diff line number Diff line change
Expand Up @@ -3634,7 +3634,7 @@ DROP TABLE min_bool;
# Min_Max End #
#################

statement ok
statement error DataFusion error: SQL error: ParserError\("Expected: column name or constraint definition, found: \)"\)
create table bool_aggregate_functions (
c1 boolean not null,
c2 boolean not null,
Expand All @@ -3651,28 +3651,20 @@ as values
(true, true, false, false, null, true, false, null);

# query_bool_and
query BBBBBBBB
query error DataFusion error: Error during planning: table 'datafusion\.public\.bool_aggregate_functions' not found
SELECT bool_and(c1), bool_and(c2), bool_and(c3), bool_and(c4), bool_and(c5), bool_and(c6), bool_and(c7), bool_and(c8) FROM bool_aggregate_functions
----
true false false false false true false NULL

# query_bool_and_distinct
query BBBBBBBB
query error DataFusion error: Error during planning: table 'datafusion\.public\.bool_aggregate_functions' not found
SELECT bool_and(distinct c1), bool_and(distinct c2), bool_and(distinct c3), bool_and(distinct c4), bool_and(distinct c5), bool_and(distinct c6), bool_and(distinct c7), bool_and(distinct c8) FROM bool_aggregate_functions
----
true false false false false true false NULL

# query_bool_or
query BBBBBBBB
query error DataFusion error: Error during planning: table 'datafusion\.public\.bool_aggregate_functions' not found
SELECT bool_or(c1), bool_or(c2), bool_or(c3), bool_or(c4), bool_or(c5), bool_or(c6), bool_or(c7), bool_or(c8) FROM bool_aggregate_functions
----
true true true false true true false NULL

# query_bool_or_distinct
query BBBBBBBB
query error DataFusion error: Error during planning: table 'datafusion\.public\.bool_aggregate_functions' not found
SELECT bool_or(distinct c1), bool_or(distinct c2), bool_or(distinct c3), bool_or(distinct c4), bool_or(distinct c5), bool_or(distinct c6), bool_or(distinct c7), bool_or(distinct c8) FROM bool_aggregate_functions
----
true true true false true true false NULL

# All supported timestamp types

Expand Down
2 changes: 1 addition & 1 deletion datafusion/sqllogictest/test_files/arrow_typeof.slt
Original file line number Diff line number Diff line change
Expand Up @@ -430,5 +430,5 @@ select arrow_cast('MyAwesomeString', 'Utf8View'), arrow_typeof(arrow_cast('MyAwe
MyAwesomeString Utf8View

# Fails until we update arrow-rs with support for https://github.com/apache/arrow-rs/pull/5894
query error DataFusion error: SQL error: ParserError\("Expected an SQL statement, found: arrow_cast"\)
query error DataFusion error: SQL error: ParserError\("Expected: an SQL statement, found: arrow_cast"\)
arrow_cast('MyAwesomeString', 'BinaryView'), arrow_typeof(arrow_cast('MyAwesomeString', 'BinaryView'))
15 changes: 4 additions & 11 deletions datafusion/sqllogictest/test_files/coalesce.slt
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ SELECT COALESCE(c1, c2, '-1') FROM test;
statement ok
drop table test

statement ok
statement error DataFusion error: SQL error: ParserError\("Expected: column name or constraint definition, found: \)"\)
CREATE TABLE test(
c1 BIGINT,
c2 BIGINT,
Expand All @@ -369,21 +369,14 @@ CREATE TABLE test(
(NULL, NULL);

# coalesce sum with default value
query I
query error DataFusion error: Error during planning: table 'datafusion\.public\.test' not found
SELECT SUM(COALESCE(c1, c2, 0)) FROM test
----
4

# coalesce mul with default value
query I
query error DataFusion error: Error during planning: table 'datafusion\.public\.test' not found
SELECT COALESCE(c1 * c2, 0) FROM test
----
2
0
0
0

statement ok
statement error DataFusion error: Execution error: Table 'test' doesn't exist\.
drop table test

# coalesce date32
Expand Down
4 changes: 2 additions & 2 deletions datafusion/sqllogictest/test_files/copy.slt
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ query error DataFusion error: Invalid or Unsupported Configuration: Config value
COPY source_table to 'test_files/scratch/copy/table.json' STORED AS JSON OPTIONS ('format.row_group_size' 55);

# Incomplete statement
query error DataFusion error: SQL error: ParserError\("Expected \), found: EOF"\)
query error DataFusion error: SQL error: ParserError\("Expected: \), found: EOF"\)
COPY (select col2, sum(col1) from source_table

# Copy from table with non literal
Expand All @@ -609,4 +609,4 @@ COPY source_table to '/tmp/table.parquet' (row_group_size 55 + 102);

# Copy using execution.keep_partition_by_columns with an invalid value
query error DataFusion error: Invalid or Unsupported Configuration: provided value for 'execution.keep_partition_by_columns' was not recognized: "invalid_value"
COPY source_table to '/tmp/table.parquet' OPTIONS (execution.keep_partition_by_columns invalid_value);
COPY source_table to '/tmp/table.parquet' OPTIONS (execution.keep_partition_by_columns invalid_value);
14 changes: 7 additions & 7 deletions datafusion/sqllogictest/test_files/create_external_table.slt
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,23 @@ statement error DataFusion error: SQL error: ParserError\("Missing LOCATION clau
CREATE EXTERNAL TABLE t STORED AS CSV

# Option value is missing
statement error DataFusion error: SQL error: ParserError\("Expected string or numeric value, found: \)"\)
statement error DataFusion error: SQL error: ParserError\("Expected: string or numeric value, found: \)"\)
CREATE EXTERNAL TABLE t STORED AS x OPTIONS ('k1' 'v1', k2 v2, k3) LOCATION 'blahblah'

# Missing `(` in WITH ORDER clause
statement error DataFusion error: SQL error: ParserError\("Expected \(, found: c1"\)
statement error DataFusion error: SQL error: ParserError\("Expected: \(, found: c1"\)
CREATE EXTERNAL TABLE t(c1 int) STORED AS CSV WITH ORDER c1 LOCATION 'foo.csv'

# Missing `)` in WITH ORDER clause
statement error DataFusion error: SQL error: ParserError\("Expected \), found: LOCATION"\)
statement error DataFusion error: SQL error: ParserError\("Expected: \), found: LOCATION"\)
CREATE EXTERNAL TABLE t(c1 int) STORED AS CSV WITH ORDER (c1 LOCATION 'foo.csv'

# Missing `ROW` in WITH HEADER clause
statement error DataFusion error: SQL error: ParserError\("Expected ROW, found: LOCATION"\)
statement error DataFusion error: SQL error: ParserError\("Expected: ROW, found: LOCATION"\)
CREATE EXTERNAL TABLE t STORED AS CSV WITH HEADER LOCATION 'abc'

# Missing `BY` in PARTITIONED clause
statement error DataFusion error: SQL error: ParserError\("Expected BY, found: LOCATION"\)
statement error DataFusion error: SQL error: ParserError\("Expected: BY, found: LOCATION"\)
CREATE EXTERNAL TABLE t STORED AS CSV PARTITIONED LOCATION 'abc'

# Duplicate `STORED AS` clause
Expand All @@ -69,11 +69,11 @@ statement error DataFusion error: SQL error: ParserError\("OPTIONS specified mor
CREATE EXTERNAL TABLE t STORED AS CSV OPTIONS ('k1' 'v1', 'k2' 'v2') OPTIONS ('k3' 'v3') LOCATION 'foo.csv'

# With typo error
statement error DataFusion error: SQL error: ParserError\("Expected HEADER, found: HEAD"\)
statement error DataFusion error: SQL error: ParserError\("Expected: HEADER, found: HEAD"\)
CREATE EXTERNAL TABLE t(c1 int) STORED AS CSV WITH HEAD ROW LOCATION 'foo.csv';

# Missing `anything` in WITH clause
statement error DataFusion error: SQL error: ParserError\("Expected HEADER, found: LOCATION"\)
statement error DataFusion error: SQL error: ParserError\("Expected: HEADER, found: LOCATION"\)
CREATE EXTERNAL TABLE t(c1 int) STORED AS CSV WITH LOCATION 'foo.csv';

# Unrecognized random clause
Expand Down
Loading

0 comments on commit a47e58c

Please sign in to comment.