diff --git a/datafusion/functions-window-common/src/lib.rs b/datafusion/functions-window-common/src/lib.rs index 53f9eb1c9ac6..ee3eaf46fc50 100644 --- a/datafusion/functions-window-common/src/lib.rs +++ b/datafusion/functions-window-common/src/lib.rs @@ -20,3 +20,4 @@ //! [DataFusion]: pub mod field; pub mod partition; +pub mod utils; diff --git a/datafusion/functions-window-common/src/utils.rs b/datafusion/functions-window-common/src/utils.rs new file mode 100644 index 000000000000..944027d3c377 --- /dev/null +++ b/datafusion/functions-window-common/src/utils.rs @@ -0,0 +1,42 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use datafusion_common::arrow::datatypes::DataType; +use datafusion_common::{exec_err, ScalarValue}; + +pub fn get_signed_integer(value: ScalarValue) -> datafusion_common::Result { + if value.is_null() { + return Ok(0); + } + + if !value.data_type().is_integer() { + return exec_err!("Expected an integer value"); + } + + value.cast_to(&DataType::Int64)?.try_into() +} + +pub fn get_casted_value( + default_value: Option, + dtype: &DataType, +) -> datafusion_common::Result { + match default_value { + Some(v) if !v.data_type().is_null() => v.cast_to(dtype), + // If None or Null datatype + _ => ScalarValue::try_from(dtype), + } +} diff --git a/datafusion/physical-plan/src/windows/mod.rs b/datafusion/physical-plan/src/windows/mod.rs index 4050b44569c8..209cc1339059 100644 --- a/datafusion/physical-plan/src/windows/mod.rs +++ b/datafusion/physical-plan/src/windows/mod.rs @@ -53,6 +53,7 @@ mod window_agg_exec; pub use bounded_window_agg_exec::BoundedWindowAggExec; use datafusion_functions_window_common::field::WindowUDFFieldArgs; use datafusion_functions_window_common::partition::PartitionEvaluatorArgs; +use datafusion_functions_window_common::utils::get_signed_integer; use datafusion_physical_expr::expressions::Column; pub use datafusion_physical_expr::window::{ BuiltInWindowExpr, PlainAggregateWindowExpr, WindowExpr, @@ -185,18 +186,6 @@ fn get_scalar_value_from_args( }) } -fn get_signed_integer(value: ScalarValue) -> Result { - if value.is_null() { - return Ok(0); - } - - if !value.data_type().is_integer() { - return exec_err!("Expected an integer value"); - } - - value.cast_to(&DataType::Int64)?.try_into() -} - fn get_unsigned_integer(value: ScalarValue) -> Result { if value.is_null() { return Ok(0);