Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions datafusion/sql/src/unparser/dialect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ pub trait Dialect: Send + Sync {
/// Return the character used to quote identifiers.
fn identifier_quote_style(&self, _identifier: &str) -> Option<char>;

/// Whether array literals should be rendered with the `ARRAY[...]` keyword.
fn array_keyword(&self) -> bool {
false
}

/// Does the dialect support specifying `NULLS FIRST/LAST` in `ORDER BY` clauses?
fn supports_nulls_first_in_sort(&self) -> bool {
true
Expand Down Expand Up @@ -321,6 +326,10 @@ impl Dialect for DefaultDialect {
pub struct PostgreSqlDialect {}

impl Dialect for PostgreSqlDialect {
fn array_keyword(&self) -> bool {
true
}

fn supports_qualify(&self) -> bool {
false
}
Expand Down
7 changes: 5 additions & 2 deletions datafusion/sql/src/unparser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ impl Unparser<'_> {
.collect::<Result<Vec<_>>>()?;
Ok(ast::Expr::Array(Array {
elem: args,
named: false,
named: self.dialect.array_keyword(),
}))
}

Expand All @@ -615,7 +615,10 @@ impl Unparser<'_> {
elem.push(self.scalar_to_sql(&value)?);
}

Ok(ast::Expr::Array(Array { elem, named: false }))
Ok(ast::Expr::Array(Array {
elem,
named: self.dialect.array_keyword(),
}))
}

fn array_element_to_sql(&self, args: &[Expr]) -> Result<ast::Expr> {
Expand Down
11 changes: 11 additions & 0 deletions datafusion/sql/tests/cases/plan_to_sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2728,6 +2728,17 @@ fn test_unparse_window() -> Result<()> {
Ok(())
}

#[test]
fn test_array_to_sql_postgres() -> Result<(), DataFusionError> {
roundtrip_statement_with_dialect_helper!(
sql: "SELECT [1, 2, 3, 4, 5]",
parser_dialect: GenericDialect {},
unparser_dialect: UnparserPostgreSqlDialect {},
expected: @"SELECT ARRAY[1, 2, 3, 4, 5]",
);
Ok(())
}

#[test]
fn test_like_filter() {
let statement = generate_round_trip_statement(
Expand Down
Loading