kosiew commented on code in PR #21891:
URL: https://github.com/apache/datafusion/pull/21891#discussion_r3205993127


##########
datafusion/functions-nested/src/range.rs:
##########
@@ -542,32 +538,59 @@ fn retrieve_range_args(
     Some((start, stop, step))
 }
 
-/// Returns an iterator of i64 values from start to stop
-fn gen_range_iter(
+/// Generate integer range values directly into the provided buffer.
+#[inline]
+fn generate_range_values(
     start: i64,
     stop: i64,
-    decreasing: bool,
+    step: i64,
     include_upper: bool,
-) -> Box<dyn Iterator<Item = i64>> {
-    match (decreasing, include_upper) {
-        // Decreasing range, stop is inclusive
-        (true, true) => Box::new((stop..=start).rev()),
-        // Decreasing range, stop is exclusive
-        (true, false) => {
-            if stop == i64::MAX {
-                // start is never greater than stop, and stop is exclusive,
-                // so the decreasing range must be empty.
-                Box::new(std::iter::empty())
-            } else {
-                // Increase the stop value by one to exclude it.
-                // Since stop is not i64::MAX, `stop + 1` will not overflow.
-                Box::new((stop + 1..=start).rev())
+    values: &mut Vec<i64>,
+) {
+    if !include_upper && start == stop {
+        return;
+    }
+
+    if step > 0 {
+        let limit = if include_upper {
+            stop
+        } else {
+            stop.saturating_sub(1)
+        };
+        if start > limit {
+            return;
+        }
+        let count =
+            (start.abs_diff(limit) / step.unsigned_abs()).saturating_add(1) as 
usize;
+        values.reserve(count);
+        let mut current = start;
+        while current <= limit {
+            values.push(current);
+            match current.checked_add(step) {
+                Some(next) => current = next,
+                None => break,
+            }
+        }
+    } else if step < 0 {
+        let limit = if include_upper {
+            stop
+        } else {
+            stop.saturating_add(1)
+        };
+        if start < limit {
+            return;
+        }
+        let count =

Review Comment:
   👍



##########
datafusion/sqllogictest/test_files/array/array_range.slt:
##########
@@ -48,6 +48,16 @@ select
 ----
 [] [] [0] [0]
 
+# Test range with full-span bounds and large steps

Review Comment:
   👍



##########
datafusion/functions-nested/benches/array_range.rs:
##########
@@ -0,0 +1,208 @@
+// 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.
+
+//! Benchmarks for range and generate_series functions.
+
+use arrow::array::{ArrayRef, Int64Array};
+use arrow::datatypes::{DataType, Field};
+use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
+use datafusion_common::{ScalarValue, config::ConfigOptions};
+use datafusion_expr::{ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl};
+use datafusion_functions_nested::range::{Range, gen_series_udf};
+use rand::rngs::StdRng;
+use rand::{Rng, SeedableRng};
+use std::hint::black_box;
+use std::sync::Arc;
+
+const NUM_ROWS: &[usize] = &[100, 1000, 10000];
+const INT_STEPS: &[i64] = &[1, 5, 50];
+const INT_DIRECTIONS: &[(&str, bool)] = &[("increasing", true), ("decreasing", 
false)];
+/// Each row produces at most RANGE_SIZE elements in its Int64 list
+const RANGE_SIZE: i64 = 200;
+const SEED: u64 = 42;
+
+fn criterion_benchmark(c: &mut Criterion) {
+    bench_range_int64(c);
+    bench_generate_series_int64(c);
+}
+
+// ---------------------------------------------------------------------------
+// Int64 – range(start, stop, step)
+// ---------------------------------------------------------------------------
+fn bench_range_int64(c: &mut Criterion) {
+    let mut group = c.benchmark_group("range_int64");
+
+    for &num_rows in NUM_ROWS {
+        for &(direction, increasing) in INT_DIRECTIONS {
+            let (start_array, stop_array) =
+                make_int64_start_stop_arrays(num_rows, RANGE_SIZE, increasing);
+
+            for &step in INT_STEPS {
+                let step = if increasing { step } else { -step };
+                let args = vec![
+                    ColumnarValue::Array(start_array.clone()),
+                    ColumnarValue::Array(stop_array.clone()),
+                    ColumnarValue::Scalar(ScalarValue::Int64(Some(step))),
+                ];
+
+                group.bench_with_input(
+                    BenchmarkId::new(format!("{direction}/step_{step}"), 
num_rows),
+                    &num_rows,
+                    |b, _| {
+                        let udf = Range::new();
+                        b.iter(|| {
+                            black_box(
+                                udf.invoke_with_args(ScalarFunctionArgs {
+                                    args: args.clone(),
+                                    arg_fields: vec![
+                                        Arc::new(Field::new(
+                                            "start",
+                                            DataType::Int64,
+                                            true,
+                                        )),
+                                        Arc::new(Field::new(
+                                            "stop",
+                                            DataType::Int64,
+                                            true,
+                                        )),
+                                        Arc::new(Field::new(
+                                            "step",
+                                            DataType::Int64,
+                                            true,
+                                        )),
+                                    ],
+                                    number_rows: num_rows,
+                                    return_field: Arc::new(Field::new(
+                                        "result",
+                                        
DataType::List(Arc::new(Field::new_list_field(
+                                            DataType::Int64,
+                                            true,
+                                        ))),
+                                        true,
+                                    )),
+                                    config_options: 
Arc::new(ConfigOptions::default()),
+                                })
+                                .unwrap(),
+                            )
+                        })
+                    },
+                );
+            }
+        }
+    }
+
+    group.finish();
+}
+
+// ---------------------------------------------------------------------------
+// Int64 – generate_series(start, stop, step)
+// ---------------------------------------------------------------------------
+fn bench_generate_series_int64(c: &mut Criterion) {
+    let mut group = c.benchmark_group("generate_series_int64");
+
+    for &num_rows in NUM_ROWS {
+        for &(direction, increasing) in INT_DIRECTIONS {
+            let (start_array, stop_array) =
+                make_int64_start_stop_arrays(num_rows, RANGE_SIZE, increasing);
+
+            for &step in INT_STEPS {
+                let step = if increasing { step } else { -step };

Review Comment:
   👍



##########
datafusion/functions-nested/src/range.rs:
##########
@@ -542,32 +538,59 @@ fn retrieve_range_args(
     Some((start, stop, step))
 }
 
-/// Returns an iterator of i64 values from start to stop
-fn gen_range_iter(
+/// Generate integer range values directly into the provided buffer.
+#[inline]
+fn generate_range_values(
     start: i64,
     stop: i64,
-    decreasing: bool,
+    step: i64,
     include_upper: bool,
-) -> Box<dyn Iterator<Item = i64>> {
-    match (decreasing, include_upper) {
-        // Decreasing range, stop is inclusive
-        (true, true) => Box::new((stop..=start).rev()),
-        // Decreasing range, stop is exclusive
-        (true, false) => {
-            if stop == i64::MAX {
-                // start is never greater than stop, and stop is exclusive,
-                // so the decreasing range must be empty.
-                Box::new(std::iter::empty())
-            } else {
-                // Increase the stop value by one to exclude it.
-                // Since stop is not i64::MAX, `stop + 1` will not overflow.
-                Box::new((stop + 1..=start).rev())
+    values: &mut Vec<i64>,
+) {
+    if !include_upper && start == stop {
+        return;
+    }
+
+    if step > 0 {
+        let limit = if include_upper {
+            stop
+        } else {
+            stop.saturating_sub(1)
+        };
+        if start > limit {
+            return;
+        }
+        let count =

Review Comment:
   👍



##########
datafusion/functions-nested/benches/array_range.rs:
##########
@@ -0,0 +1,208 @@
+// 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.
+
+//! Benchmarks for range and generate_series functions.
+
+use arrow::array::{ArrayRef, Int64Array};
+use arrow::datatypes::{DataType, Field};
+use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
+use datafusion_common::{ScalarValue, config::ConfigOptions};
+use datafusion_expr::{ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl};
+use datafusion_functions_nested::range::{Range, gen_series_udf};
+use rand::rngs::StdRng;
+use rand::{Rng, SeedableRng};
+use std::hint::black_box;
+use std::sync::Arc;
+
+const NUM_ROWS: &[usize] = &[100, 1000, 10000];
+const INT_STEPS: &[i64] = &[1, 5, 50];
+const INT_DIRECTIONS: &[(&str, bool)] = &[("increasing", true), ("decreasing", 
false)];

Review Comment:
   👍



##########
datafusion/functions-nested/benches/array_range.rs:
##########
@@ -0,0 +1,208 @@
+// 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.
+
+//! Benchmarks for range and generate_series functions.
+
+use arrow::array::{ArrayRef, Int64Array};
+use arrow::datatypes::{DataType, Field};
+use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
+use datafusion_common::{ScalarValue, config::ConfigOptions};
+use datafusion_expr::{ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl};
+use datafusion_functions_nested::range::{Range, gen_series_udf};
+use rand::rngs::StdRng;
+use rand::{Rng, SeedableRng};
+use std::hint::black_box;
+use std::sync::Arc;
+
+const NUM_ROWS: &[usize] = &[100, 1000, 10000];
+const INT_STEPS: &[i64] = &[1, 5, 50];
+const INT_DIRECTIONS: &[(&str, bool)] = &[("increasing", true), ("decreasing", 
false)];
+/// Each row produces at most RANGE_SIZE elements in its Int64 list
+const RANGE_SIZE: i64 = 200;
+const SEED: u64 = 42;
+
+fn criterion_benchmark(c: &mut Criterion) {
+    bench_range_int64(c);
+    bench_generate_series_int64(c);
+}
+
+// ---------------------------------------------------------------------------
+// Int64 – range(start, stop, step)
+// ---------------------------------------------------------------------------
+fn bench_range_int64(c: &mut Criterion) {
+    let mut group = c.benchmark_group("range_int64");
+
+    for &num_rows in NUM_ROWS {
+        for &(direction, increasing) in INT_DIRECTIONS {
+            let (start_array, stop_array) =
+                make_int64_start_stop_arrays(num_rows, RANGE_SIZE, increasing);
+
+            for &step in INT_STEPS {
+                let step = if increasing { step } else { -step };

Review Comment:
   👍



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to