Skip to content

fix: wrong result of range function #8313

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 6 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
15 changes: 12 additions & 3 deletions datafusion/physical-expr/src/array_expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -741,13 +741,22 @@ pub fn gen_range(args: &[ArrayRef]) -> Result<ArrayRef> {
let mut offsets = vec![0];
for (idx, stop) in stop_array.iter().enumerate() {
let stop = stop.unwrap_or(0);
let start = start_array.as_ref().map(|arr| arr.value(idx)).unwrap_or(0);
let mut start = start_array.as_ref().map(|arr| arr.value(idx)).unwrap_or(0);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
let mut start = start_array.as_ref().map(|arr| arr.value(idx)).unwrap_or(0);
let start = start_array.as_ref().map(|arr| arr.value(idx)).unwrap_or(0);

let step = step_array.as_ref().map(|arr| arr.value(idx)).unwrap_or(1);
if step == 0 {
return exec_err!("step can't be 0 for function range(start [, stop, step]");
}
let value = (start..stop).step_by(step as usize);
values.extend(value);
let value;
if step < 0 {
while stop < start {
values.push(start);
start += step;
}
} else {
value = (start..stop).step_by(step as usize);
values.extend(value.clone());
}

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
let value;
if step < 0 {
while stop < start {
values.push(start);
start += step;
}
} else {
value = (start..stop).step_by(step as usize);
values.extend(value.clone());
}
if step < 0 {
// Decreasing range
values.extend((stop..start).rev().step_by((-step) as usize));
} else {
// Increasing range
values.extend((start..stop).step_by(step as usize));
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Emm, this is good advice, but it should be
values.extend((stop+1..start+1).rev().step_by((-step) as usize));
Because the stop of the range is not taken. 🤔️

Copy link
Member

Choose a reason for hiding this comment

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

Yes, and I think we should add a test case for it 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, I added ut for this

Copy link
Member

Choose a reason for hiding this comment

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

Would you mind putting the test in sqllogoictest?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

offsets.push(values.len() as i32);
}
let arr = Arc::new(ListArray::try_new(
Expand Down
2 changes: 1 addition & 1 deletion datafusion/sqllogictest/test_files/array.slt
Original file line number Diff line number Diff line change
Expand Up @@ -2752,7 +2752,7 @@ select range(5),
range(1, -5, 1)
;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
;
range(1, -5, -1)
;

Copy link
Member

Choose a reason for hiding this comment

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

move the test to the sqllogistest, overall LGTM 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated~

----
[0, 1, 2, 3, 4] [2, 3, 4] [2, 5, 8] [1] []
[0, 1, 2, 3, 4] [2, 3, 4] [2, 5, 8] [] []

query ???
select generate_series(5),
Expand Down