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

Support internal cast for BuiltinScalarFunction::MakeArray #6607

Merged
merged 7 commits into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
89 changes: 33 additions & 56 deletions datafusion-cli/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions datafusion/core/tests/sqllogictests/test_files/array.slt
Original file line number Diff line number Diff line change
Expand Up @@ -300,3 +300,8 @@ query II rowsort
select array_ndims(make_array()), array_ndims(make_array(make_array()))
----
1 2

query ?
select make_array(1, 2.0)
----
[1.0, 2.0]
44 changes: 39 additions & 5 deletions datafusion/optimizer/src/analyzer/type_coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ use datafusion_expr::type_coercion::{is_datetime, is_numeric, is_utf8_or_large_u
use datafusion_expr::utils::from_plan;
use datafusion_expr::{
aggregate_function, is_false, is_not_false, is_not_true, is_not_unknown, is_true,
is_unknown, type_coercion, AggregateFunction, Expr, LogicalPlan, Operator,
Projection, WindowFrame, WindowFrameBound, WindowFrameUnits,
is_unknown, type_coercion, AggregateFunction, BuiltinScalarFunction, Expr,
LogicalPlan, Operator, Projection, WindowFrame, WindowFrameBound, WindowFrameUnits,
};
use datafusion_expr::{ExprSchemable, Signature};

Expand Down Expand Up @@ -387,13 +387,14 @@ impl TreeNodeRewriter for TypeCoercionRewriter {
Ok(expr)
}
Expr::ScalarFunction(ScalarFunction { fun, args }) => {
let nex_expr = coerce_arguments_for_signature(
let new_args = coerce_arguments_for_signature(
args.as_slice(),
&self.schema,
&fun.signature(),
)?;
let expr = Expr::ScalarFunction(ScalarFunction::new(fun, nex_expr));
Ok(expr)
let new_args =
coerce_arguments_for_fun(new_args.as_slice(), &self.schema, &fun)?;
Ok(Expr::ScalarFunction(ScalarFunction::new(fun, new_args)))
}
Expr::AggregateFunction(expr::AggregateFunction {
fun,
Expand Down Expand Up @@ -602,6 +603,39 @@ fn coerce_arguments_for_signature(
.collect::<Result<Vec<_>>>()
}

fn coerce_arguments_for_fun(
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

expressions: &[Expr],
schema: &DFSchema,
fun: &BuiltinScalarFunction,
) -> Result<Vec<Expr>> {
if expressions.is_empty() {
return Ok(vec![]);
}

if *fun == BuiltinScalarFunction::MakeArray {
// Find the final data type for the function arguments
let current_types = expressions
.iter()
.map(|e| e.get_type(schema))
.collect::<Result<Vec<_>>>()?;

let new_type = current_types
.iter()
.skip(1)
.fold(current_types.first().unwrap().clone(), |acc, x| {
comparison_coercion(&acc, x).unwrap_or(acc)
});

return expressions
.iter()
.enumerate()
.map(|(_, expr)| cast_expr(expr, &new_type, schema))
.collect();
}

Ok(expressions.to_vec())
}

/// Cast `expr` to the specified type, if possible
fn cast_expr(expr: &Expr, to_type: &DataType, schema: &DFSchema) -> Result<Expr> {
expr.clone().cast_to(to_type, schema)
Expand Down