Skip to content

Commit e75ed5b

Browse files
vimehalamb
andauthored
feat(sql): unparse array_has as ANY for Postgres (apache#20654)
## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes apache#123` indicates that this PR will close issue apache#123. --> - Closes apache#20653 . ## Rationale for this change <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> PostgreSQL does not have an `array_has()` function. When the SQL unparser emits `array_has(haystack, needle)` for the Postgres dialect, it produces invalid SQL. The idiomatic equivalent is `needle = ANY(haystack)`. ## What changes are included in this PR? <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> Adds a `scalar_function_to_sql_overrides` entry in `PostgreSqlDialect` that converts `array_has(haystack, needle)` to `needle = ANY(haystack)` via `ast::Expr::AnyOp`. ## Are these changes tested? <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> Yep, with a unit test comparing default vs Postgres dialect output and a roundtrip integration test for `= ANY()` in a WHERE clause. ## Are there any user-facing changes? <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. --> <!-- If there are any breaking changes to public APIs, please add the `api change` label. --> No breaking changes. `array_has` expressions are now unparsed as valid Postgres syntax when using `PostgreSqlDialect`. --------- Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>
1 parent fee90be commit e75ed5b

3 files changed

Lines changed: 52 additions & 2 deletions

File tree

datafusion/sql/src/unparser/dialect.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use super::{
2424
use arrow::array::timezone::Tz;
2525
use arrow::datatypes::TimeUnit;
2626
use chrono::DateTime;
27-
use datafusion_common::Result;
27+
use datafusion_common::{Result, internal_err};
2828
use datafusion_expr::Expr;
2929
use regex::Regex;
3030
use sqlparser::tokenizer::Span;
@@ -351,6 +351,10 @@ impl Dialect for PostgreSqlDialect {
351351
func_name: &str,
352352
args: &[Expr],
353353
) -> Result<Option<ast::Expr>> {
354+
if func_name == "array_has" {
355+
return self.array_has_to_sql_any(unparser, args);
356+
}
357+
354358
if func_name == "round" {
355359
return Ok(Some(
356360
self.round_to_sql_enforce_numeric(unparser, func_name, args)?,
@@ -362,6 +366,23 @@ impl Dialect for PostgreSqlDialect {
362366
}
363367

364368
impl PostgreSqlDialect {
369+
fn array_has_to_sql_any(
370+
&self,
371+
unparser: &Unparser,
372+
args: &[Expr],
373+
) -> Result<Option<ast::Expr>> {
374+
let [haystack, needle] = args else {
375+
return internal_err!("array_has expected 2 arguments, got {}", args.len());
376+
};
377+
378+
Ok(Some(ast::Expr::AnyOp {
379+
left: Box::new(unparser.expr_to_sql(needle)?),
380+
compare_op: BinaryOperator::Eq,
381+
right: Box::new(unparser.expr_to_sql(haystack)?),
382+
is_some: false,
383+
}))
384+
}
385+
365386
fn round_to_sql_enforce_numeric(
366387
&self,
367388
unparser: &Unparser,

datafusion/sql/src/unparser/expr.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1853,7 +1853,7 @@ mod tests {
18531853
use datafusion_functions::expr_fn::{get_field, named_struct};
18541854
use datafusion_functions_aggregate::count::count_udaf;
18551855
use datafusion_functions_aggregate::expr_fn::sum;
1856-
use datafusion_functions_nested::expr_fn::{array_element, make_array};
1856+
use datafusion_functions_nested::expr_fn::{array_element, array_has, make_array};
18571857
use datafusion_functions_nested::map::map;
18581858
use datafusion_functions_window::rank::rank_udwf;
18591859
use datafusion_functions_window::row_number::row_number_udwf;
@@ -3074,6 +3074,24 @@ mod tests {
30743074
Ok(())
30753075
}
30763076

3077+
#[test]
3078+
fn test_postgres_array_has_to_any() -> Result<()> {
3079+
let default_dialect: Arc<dyn Dialect> = Arc::new(DefaultDialect {});
3080+
let postgres_dialect: Arc<dyn Dialect> = Arc::new(PostgreSqlDialect {});
3081+
let expr = array_has(col("items"), lit(1));
3082+
3083+
for (dialect, expected) in [
3084+
(default_dialect, "array_has(\"items\", 1)"),
3085+
(postgres_dialect, "1 = ANY(\"items\")"),
3086+
] {
3087+
let unparser = Unparser::new(dialect.as_ref());
3088+
let actual = format!("{}", unparser.expr_to_sql(&expr)?);
3089+
assert_eq!(actual, expected);
3090+
}
3091+
3092+
Ok(())
3093+
}
3094+
30773095
#[test]
30783096
fn test_window_func_support_window_frame() -> Result<()> {
30793097
let default_dialect: Arc<dyn Dialect> =

datafusion/sql/tests/cases/plan_to_sql.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,17 @@ fn roundtrip_statement_with_dialect_3() -> Result<(), DataFusionError> {
361361
Ok(())
362362
}
363363

364+
#[test]
365+
fn roundtrip_statement_postgres_any_array_expr() -> Result<(), DataFusionError> {
366+
roundtrip_statement_with_dialect_helper!(
367+
sql: "select left from array where 1 = any(left);",
368+
parser_dialect: GenericDialect {},
369+
unparser_dialect: UnparserPostgreSqlDialect {},
370+
expected: @r#"SELECT "array"."left" FROM "array" WHERE 1 = ANY("array"."left")"#,
371+
);
372+
Ok(())
373+
}
374+
364375
#[test]
365376
fn roundtrip_statement_with_dialect_4() -> Result<(), DataFusionError> {
366377
roundtrip_statement_with_dialect_helper!(

0 commit comments

Comments
 (0)