Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
26 changes: 25 additions & 1 deletion datafusion/sql/src/unparser/dialect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use super::{
use arrow::array::timezone::Tz;
use arrow::datatypes::TimeUnit;
use chrono::DateTime;
use datafusion_common::Result;
use datafusion_common::{internal_err, Result};
use datafusion_expr::Expr;
use regex::Regex;
use sqlparser::tokenizer::Span;
Expand Down Expand Up @@ -351,6 +351,10 @@ impl Dialect for PostgreSqlDialect {
func_name: &str,
args: &[Expr],
) -> Result<Option<ast::Expr>> {
if func_name == "array_has" {
return self.array_has_to_sql_any(unparser, args);
}

if func_name == "round" {
return Ok(Some(
self.round_to_sql_enforce_numeric(unparser, func_name, args)?,
Expand All @@ -362,6 +366,26 @@ impl Dialect for PostgreSqlDialect {
}

impl PostgreSqlDialect {
fn array_has_to_sql_any(
&self,
unparser: &Unparser,
args: &[Expr],
) -> Result<Option<ast::Expr>> {
let [haystack, needle] = args else {
return internal_err!(
"array_has expected 2 arguments, got {}",
args.len()
);
};
Comment on lines +374 to +376
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Since args must always contain [haystack, needle] in this case, I wonder if setting the else to unreachable!() would be clearer.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vimeh would you like to respond to @nuno-faria before we merge this PR in?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have replaced with an internal_err to match existing patterns I saw.

also went ahead and rebased

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @vimeh.


Ok(Some(ast::Expr::AnyOp {
left: Box::new(unparser.expr_to_sql(needle)?),
compare_op: BinaryOperator::Eq,
right: Box::new(unparser.expr_to_sql(haystack)?),
is_some: false,
}))
}

fn round_to_sql_enforce_numeric(
&self,
unparser: &Unparser,
Expand Down
20 changes: 19 additions & 1 deletion datafusion/sql/src/unparser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1853,7 +1853,7 @@ mod tests {
use datafusion_functions::expr_fn::{get_field, named_struct};
use datafusion_functions_aggregate::count::count_udaf;
use datafusion_functions_aggregate::expr_fn::sum;
use datafusion_functions_nested::expr_fn::{array_element, make_array};
use datafusion_functions_nested::expr_fn::{array_element, array_has, make_array};
use datafusion_functions_nested::map::map;
use datafusion_functions_window::rank::rank_udwf;
use datafusion_functions_window::row_number::row_number_udwf;
Expand Down Expand Up @@ -3074,6 +3074,24 @@ mod tests {
Ok(())
}

#[test]
fn test_postgres_array_has_to_any() -> Result<()> {
let default_dialect: Arc<dyn Dialect> = Arc::new(DefaultDialect {});
let postgres_dialect: Arc<dyn Dialect> = Arc::new(PostgreSqlDialect {});
let expr = array_has(col("items"), lit(1));

for (dialect, expected) in [
(default_dialect, "array_has(\"items\", 1)"),
(postgres_dialect, "1 = ANY(\"items\")"),
] {
let unparser = Unparser::new(dialect.as_ref());
let actual = format!("{}", unparser.expr_to_sql(&expr)?);
assert_eq!(actual, expected);
}

Ok(())
}

#[test]
fn test_window_func_support_window_frame() -> Result<()> {
let default_dialect: Arc<dyn Dialect> =
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 @@ -361,6 +361,17 @@ fn roundtrip_statement_with_dialect_3() -> Result<(), DataFusionError> {
Ok(())
}

#[test]
fn roundtrip_statement_postgres_any_array_expr() -> Result<(), DataFusionError> {
roundtrip_statement_with_dialect_helper!(
sql: "select left from array where 1 = any(left);",
parser_dialect: GenericDialect {},
unparser_dialect: UnparserPostgreSqlDialect {},
expected: @r#"SELECT "array"."left" FROM "array" WHERE 1 = ANY("array"."left")"#,
);
Ok(())
}

#[test]
fn roundtrip_statement_with_dialect_4() -> Result<(), DataFusionError> {
roundtrip_statement_with_dialect_helper!(
Expand Down
Loading