Skip to content

Commit

Permalink
refactor: avoid loop
Browse files Browse the repository at this point in the history
  • Loading branch information
tshauck committed Aug 9, 2024
1 parent 58c8d71 commit 9c13836
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions datafusion/functions-nested/src/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use datafusion_expr::{
ColumnarValue, ScalarUDFImpl, Signature, TypeSignature, Volatility,
};
use std::any::Any;
use std::iter::from_fn;
use std::sync::Arc;

make_udf_expr_and_func!(
Expand Down Expand Up @@ -342,15 +343,15 @@ fn gen_range_date(args: &[ArrayRef], include_upper: bool) -> Result<ArrayRef> {
}
let mut new_date = start;

let mut values: Vec<Option<i32>> = vec![];
loop {
if neg && new_date < stop || !neg && new_date > stop {
break;
let values = from_fn(|| {
if (neg && new_date < stop) || (!neg && new_date > stop) {
None
} else {
let current_date = new_date;
new_date = Date32Type::add_month_day_nano(new_date, step);
Some(Some(current_date))
}

values.push(Some(new_date));
new_date = Date32Type::add_month_day_nano(new_date, step);
}
});

list_builder.append_value(values);
list_builder.append(true);
Expand Down

0 comments on commit 9c13836

Please sign in to comment.