Skip to content

Commit 5d1d500

Browse files
committed
change simplify signature
Signed-off-by: jayzhan211 <[email protected]>
1 parent adcb2e2 commit 5d1d500

File tree

5 files changed

+24
-26
lines changed

5 files changed

+24
-26
lines changed

datafusion/core/tests/user_defined/user_defined_scalar_functions.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,16 @@ use arrow_schema::{DataType, Field, Schema};
2424
use datafusion::prelude::*;
2525
use datafusion::{execution::registry::FunctionRegistry, test_util};
2626
use datafusion_common::cast::as_float64_array;
27-
use datafusion_common::DFSchemaRef;
27+
use datafusion_common::DFSchema;
2828
use datafusion_common::{
2929
assert_batches_eq, assert_batches_sorted_eq, cast::as_int32_array, not_impl_err,
3030
plan_err, DataFusionError, ExprSchema, Result, ScalarValue,
3131
};
32+
use datafusion_expr::simplify::Simplified;
33+
use datafusion_expr::simplify::SimplifyInfo;
3234
use datafusion_expr::{
3335
create_udaf, create_udf, Accumulator, ColumnarValue, ExprSchemable,
34-
LogicalPlanBuilder, ScalarUDF, ScalarUDFImpl, Signature, Simplified, Volatility,
36+
LogicalPlanBuilder, ScalarUDF, ScalarUDFImpl, Signature, Volatility,
3537
};
3638

3739
use rand::{thread_rng, Rng};
@@ -529,8 +531,9 @@ impl ScalarUDFImpl for CastToI64UDF {
529531
Ok(DataType::Int64)
530532
}
531533
// Wrap with Expr::Cast() to Int64
532-
fn simplify(&self, args: &[Expr], schema: DFSchemaRef) -> Result<Simplified> {
534+
fn simplify(&self, args: &[Expr], info: &dyn SimplifyInfo) -> Result<Simplified> {
533535
let e = args[0].to_owned();
536+
let schema = info.schema().unwrap_or_else(|| DFSchema::empty().into());
534537
let casted_expr = e.cast_to(&DataType::Int64, schema.as_ref())?;
535538
Ok(Simplified::Rewritten(casted_expr))
536539
}

datafusion/expr/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ pub use signature::{
8383
};
8484
pub use table_source::{TableProviderFilterPushDown, TableSource, TableType};
8585
pub use udaf::{AggregateUDF, AggregateUDFImpl};
86-
pub use udf::{ScalarUDF, ScalarUDFImpl, Simplified};
86+
pub use udf::{ScalarUDF, ScalarUDFImpl};
8787
pub use udwf::{WindowUDF, WindowUDFImpl};
8888
pub use window_frame::{WindowFrame, WindowFrameBound, WindowFrameUnits};
8989

datafusion/expr/src/simplify.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,12 @@ impl<'a> SimplifyInfo for SimplifyContext<'a> {
140140
self.props
141141
}
142142
}
143+
144+
/// Was the expression simplified?
145+
pub enum Simplified {
146+
/// The function call was simplified to an entirely new Expr
147+
Rewritten(Expr),
148+
/// the function call could not be simplified, and the arguments
149+
/// are return unmodified
150+
Original,
151+
}

datafusion/expr/src/udf.rs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,20 @@
1717

1818
//! [`ScalarUDF`]: Scalar User Defined Functions
1919
20+
use crate::simplify::{Simplified, SimplifyInfo};
2021
use crate::ExprSchemable;
2122
use crate::{
2223
ColumnarValue, Expr, FuncMonotonicity, ReturnTypeFunction,
2324
ScalarFunctionImplementation, Signature,
2425
};
2526
use arrow::datatypes::DataType;
26-
use datafusion_common::{DFSchemaRef, ExprSchema, Result};
27+
use datafusion_common::{ExprSchema, Result};
2728
use std::any::Any;
2829
use std::fmt;
2930
use std::fmt::Debug;
3031
use std::fmt::Formatter;
3132
use std::sync::Arc;
3233

33-
// TODO(In this PR): Move to simplify.rs
34-
/// Was the expression simplified?
35-
pub enum Simplified {
36-
/// The function call was simplified to an entirely new Expr
37-
Rewritten(Expr),
38-
/// the function call could not be simplified, and the arguments
39-
/// are return unmodified
40-
Original,
41-
}
42-
4334
/// Logical representation of a Scalar User Defined Function.
4435
///
4536
/// A scalar function produces a single row output for each row of input. This
@@ -173,8 +164,8 @@ impl ScalarUDF {
173164
/// Do the function rewrite
174165
///
175166
/// See [`ScalarUDFImpl::simplify`] for more details.
176-
pub fn simplify(&self, args: &[Expr], schema: DFSchemaRef) -> Result<Simplified> {
177-
self.inner.simplify(args, schema)
167+
pub fn simplify(&self, args: &[Expr], info: &dyn SimplifyInfo) -> Result<Simplified> {
168+
self.inner.simplify(args, info)
178169
}
179170

180171
/// Invoke the function on `args`, returning the appropriate result.
@@ -358,7 +349,7 @@ pub trait ScalarUDFImpl: Debug + Send + Sync {
358349
// Do the function rewrite.
359350
// 'args': The arguments of the function
360351
// 'schema': The schema of the function
361-
fn simplify(&self, _args: &[Expr], _schema: DFSchemaRef) -> Result<Simplified> {
352+
fn simplify(&self, _args: &[Expr], _info: &dyn SimplifyInfo) -> Result<Simplified> {
362353
Ok(Simplified::Original)
363354
}
364355
}

datafusion/optimizer/src/simplify_expressions/function_simplifier.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
//! This module implements a rule that do function simplification.
1919
2020
use datafusion_common::tree_node::TreeNodeRewriter;
21-
use datafusion_common::{DFSchema, Result};
21+
use datafusion_common::Result;
22+
use datafusion_expr::simplify::Simplified;
2223
use datafusion_expr::simplify::SimplifyInfo;
23-
use datafusion_expr::Simplified;
2424
use datafusion_expr::{expr::ScalarFunction, Expr, ScalarFunctionDefinition};
2525

2626
pub(super) struct FunctionSimplifier<'a, S> {
@@ -42,12 +42,7 @@ impl<'a, S: SimplifyInfo> TreeNodeRewriter for FunctionSimplifier<'a, S> {
4242
args,
4343
}) = &expr
4444
{
45-
let schema = self
46-
.info
47-
.schema()
48-
.unwrap_or_else(|| DFSchema::empty().into());
49-
50-
let simplified_expr = udf.simplify(args, schema)?;
45+
let simplified_expr = udf.simplify(args, self.info)?;
5146
match simplified_expr {
5247
Simplified::Original => Ok(expr),
5348
Simplified::Rewritten(expr) => Ok(expr),

0 commit comments

Comments
 (0)