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

Lead and Lag window functions should support default value with datatype other than Int64 #9001

Merged
merged 1 commit into from
Jan 26, 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
10 changes: 10 additions & 0 deletions datafusion/common/src/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2364,6 +2364,16 @@ impl ScalarValue {
ScalarValue::try_from_array(&cast_arr, 0)
}

/// Try to cast this value to a ScalarValue of type `data_type`
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

pub fn cast_to(&self, data_type: &DataType) -> Result<Self> {
Copy link
Contributor

@comphead comphead Jan 26, 2024

Choose a reason for hiding this comment

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

I was introducing the same method to overcome unnecessary string conversion in separate PR 👍

let cast_options = CastOptions {
safe: false,
format_options: Default::default(),
};
let cast_arr = cast_with_options(&self.to_array()?, data_type, &cast_options)?;
ScalarValue::try_from_array(&cast_arr, 0)
}

fn eq_array_decimal(
array: &ArrayRef,
index: usize,
Expand Down
14 changes: 3 additions & 11 deletions datafusion/physical-expr/src/window/lead_lag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ use crate::PhysicalExpr;
use arrow::array::ArrayRef;
use arrow::compute::cast;
use arrow::datatypes::{DataType, Field};
use datafusion_common::{
arrow_datafusion_err, exec_err, DataFusionError, Result, ScalarValue,
};
use datafusion_common::{arrow_datafusion_err, DataFusionError, Result, ScalarValue};
use datafusion_expr::PartitionEvaluator;
use std::any::Any;
use std::cmp::min;
Expand Down Expand Up @@ -238,15 +236,9 @@ fn get_default_value(
dtype: &DataType,
) -> Result<ScalarValue> {
match default_value {
Some(v) if v.data_type() == DataType::Int64 => {
ScalarValue::try_from_string(v.to_string(), dtype)
}
Some(v) if !v.data_type().is_null() => exec_err!(
"Unexpected datatype for default value: {}. Expected: Int64",
v.data_type()
),
Some(v) if !v.data_type().is_null() => v.cast_to(dtype),
// If None or Null datatype
_ => Ok(ScalarValue::try_from(dtype)?),
_ => ScalarValue::try_from(dtype),
}
}

Expand Down
14 changes: 14 additions & 0 deletions datafusion/sqllogictest/test_files/window.slt
Original file line number Diff line number Diff line change
Expand Up @@ -4004,3 +4004,17 @@ select lag(a, 1, null) over (order by a) from (select 1 a union all select 2 a)
----
NULL
1

# test LEAD window function with string default value
query T
select lead(a, 1, 'default') over (order by a) from (select '1' a union all select '2' a)
----
2
default

# test LAG window function with string default value
query T
select lag(a, 1, 'default') over (order by a) from (select '1' a union all select '2' a)
----
default
1
Loading