Skip to content

Commit 61ef7f4

Browse files
committed
sql: render PostgreSQL array literals as ARRAY[...] in unparser
1 parent c17c87c commit 61ef7f4

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

datafusion/sql/src/unparser/dialect.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ pub trait Dialect: Send + Sync {
5151
/// Return the character used to quote identifiers.
5252
fn identifier_quote_style(&self, _identifier: &str) -> Option<char>;
5353

54+
/// Whether array literals should be rendered with the `ARRAY[...]` keyword.
55+
fn array_keyword(&self) -> bool {
56+
false
57+
}
58+
5459
/// Does the dialect support specifying `NULLS FIRST/LAST` in `ORDER BY` clauses?
5560
fn supports_nulls_first_in_sort(&self) -> bool {
5661
true
@@ -321,6 +326,10 @@ impl Dialect for DefaultDialect {
321326
pub struct PostgreSqlDialect {}
322327

323328
impl Dialect for PostgreSqlDialect {
329+
fn array_keyword(&self) -> bool {
330+
true
331+
}
332+
324333
fn supports_qualify(&self) -> bool {
325334
false
326335
}

datafusion/sql/src/unparser/expr.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ impl Unparser<'_> {
604604
.collect::<Result<Vec<_>>>()?;
605605
Ok(ast::Expr::Array(Array {
606606
elem: args,
607-
named: false,
607+
named: self.dialect.array_keyword(),
608608
}))
609609
}
610610

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

618-
Ok(ast::Expr::Array(Array { elem, named: false }))
618+
Ok(ast::Expr::Array(Array {
619+
elem,
620+
named: self.dialect.array_keyword(),
621+
}))
619622
}
620623

621624
fn array_element_to_sql(&self, args: &[Expr]) -> Result<ast::Expr> {

datafusion/sql/tests/cases/plan_to_sql.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2728,6 +2728,17 @@ fn test_unparse_window() -> Result<()> {
27282728
Ok(())
27292729
}
27302730

2731+
#[test]
2732+
fn test_array_to_sql_postgres() -> Result<(), DataFusionError> {
2733+
roundtrip_statement_with_dialect_helper!(
2734+
sql: "SELECT [1, 2, 3, 4, 5]",
2735+
parser_dialect: GenericDialect {},
2736+
unparser_dialect: UnparserPostgreSqlDialect {},
2737+
expected: @"SELECT ARRAY[1, 2, 3, 4, 5]",
2738+
);
2739+
Ok(())
2740+
}
2741+
27312742
#[test]
27322743
fn test_like_filter() {
27332744
let statement = generate_round_trip_statement(

0 commit comments

Comments
 (0)