Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Postgres: enforce required NUMERIC type for round scalar function #34

Merged
merged 3 commits into from
Oct 3, 2024
Merged
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
116 changes: 115 additions & 1 deletion datafusion/sql/src/unparser/dialect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@
use std::sync::Arc;

use arrow_schema::TimeUnit;
use datafusion_expr::Expr;
use regex::Regex;
use sqlparser::{
ast::{self, Ident, ObjectName, TimezoneInfo},
ast::{self, Function, Ident, ObjectName, TimezoneInfo},
keywords::ALL_KEYWORDS,
};

use datafusion_common::Result;

use super::{utils::date_part_to_sql, Unparser};

/// `Dialect` to use for Unparsing
///
/// The default dialect tries to avoid quoting identifiers unless necessary (e.g. `a` instead of `"a"`)
Expand Down Expand Up @@ -108,6 +113,15 @@ pub trait Dialect: Send + Sync {
fn supports_column_alias_in_table_alias(&self) -> bool {
true
}

fn scalar_function_to_sql_overrides(
&self,
_unparser: &Unparser,
_func_name: &str,
_args: &[Expr],
) -> Result<Option<ast::Expr>> {
Ok(None)
}
}

/// `IntervalStyle` to use for unparsing
Expand Down Expand Up @@ -171,6 +185,67 @@ impl Dialect for PostgreSqlDialect {
fn float64_ast_dtype(&self) -> sqlparser::ast::DataType {
sqlparser::ast::DataType::DoublePrecision
}

fn scalar_function_to_sql_overrides(
&self,
unparser: &Unparser,
func_name: &str,
args: &[Expr],
) -> Result<Option<ast::Expr>> {
if func_name == "round" {
return Ok(Some(
self.round_to_sql_enforce_numeric(unparser, func_name, args)?,
));
}

Ok(None)
}
}

impl PostgreSqlDialect {
fn round_to_sql_enforce_numeric(
&self,
unparser: &Unparser,
func_name: &str,
args: &[Expr],
) -> Result<ast::Expr> {
let mut args = unparser.function_args_to_sql(args)?;

// Enforce the first argument to be Numeric
if let Some(ast::FunctionArg::Unnamed(ast::FunctionArgExpr::Expr(expr))) =
args.first_mut()
{
if let ast::Expr::Cast { data_type, .. } = expr {
// Don't create an additional cast wrapper if we can update the existing one
*data_type = ast::DataType::Numeric(ast::ExactNumberInfo::None);
} else {
// Wrap the expression in a new cast
*expr = ast::Expr::Cast {
kind: ast::CastKind::Cast,
expr: Box::new(expr.clone()),
data_type: ast::DataType::Numeric(ast::ExactNumberInfo::None),
format: None,
};
}
}

Ok(ast::Expr::Function(Function {
name: ast::ObjectName(vec![Ident {
value: func_name.to_string(),
quote_style: None,
}]),
args: ast::FunctionArguments::List(ast::FunctionArgumentList {
duplicate_treatment: None,
args,
clauses: vec![],
}),
filter: None,
null_treatment: None,
over: None,
within_group: vec![],
parameters: ast::FunctionArguments::None,
}))
}
}

pub struct MySqlDialect {}
Expand Down Expand Up @@ -211,6 +286,19 @@ impl Dialect for MySqlDialect {
) -> ast::DataType {
ast::DataType::Datetime(None)
}

fn scalar_function_to_sql_overrides(
&self,
unparser: &Unparser,
func_name: &str,
args: &[Expr],
) -> Result<Option<ast::Expr>> {
if func_name == "date_part" {
return date_part_to_sql(unparser, self.date_field_extract_style(), args);
}

Ok(None)
}
}

pub struct SqliteDialect {}
Expand All @@ -231,6 +319,19 @@ impl Dialect for SqliteDialect {
fn supports_column_alias_in_table_alias(&self) -> bool {
false
}

fn scalar_function_to_sql_overrides(
&self,
unparser: &Unparser,
func_name: &str,
args: &[Expr],
) -> Result<Option<ast::Expr>> {
if func_name == "date_part" {
sgrebnov marked this conversation as resolved.
Show resolved Hide resolved
return date_part_to_sql(unparser, self.date_field_extract_style(), args);
}

Ok(None)
}
}

pub struct CustomDialect {
Expand Down Expand Up @@ -339,6 +440,19 @@ impl Dialect for CustomDialect {
fn supports_column_alias_in_table_alias(&self) -> bool {
self.supports_column_alias_in_table_alias
}

fn scalar_function_to_sql_overrides(
&self,
unparser: &Unparser,
func_name: &str,
args: &[Expr],
) -> Result<Option<ast::Expr>> {
if func_name == "date_part" {
return date_part_to_sql(unparser, self.date_field_extract_style(), args);
}

Ok(None)
}
}

/// `CustomDialectBuilder` to build `CustomDialect` using builder pattern
Expand Down
Loading