Skip to content

Commit

Permalink
Moves helper functions into functions-window-common package
Browse files Browse the repository at this point in the history
  • Loading branch information
jcsherin committed Oct 11, 2024
1 parent 3b6bc5d commit ab8a83c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 12 deletions.
1 change: 1 addition & 0 deletions datafusion/functions-window-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@
//! [DataFusion]: <https://crates.io/crates/datafusion>
pub mod field;
pub mod partition;
pub mod utils;
42 changes: 42 additions & 0 deletions datafusion/functions-window-common/src/utils.rs
Original file line number Diff line number Diff line change
@@ -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<i64> {
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<ScalarValue>,
dtype: &DataType,
) -> datafusion_common::Result<ScalarValue> {
match default_value {
Some(v) if !v.data_type().is_null() => v.cast_to(dtype),
// If None or Null datatype
_ => ScalarValue::try_from(dtype),
}
}
13 changes: 1 addition & 12 deletions datafusion/physical-plan/src/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -185,18 +186,6 @@ fn get_scalar_value_from_args(
})
}

fn get_signed_integer(value: ScalarValue) -> Result<i64> {
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<u64> {
if value.is_null() {
return Ok(0);
Expand Down

0 comments on commit ab8a83c

Please sign in to comment.