Skip to content
Closed
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
26 changes: 21 additions & 5 deletions datafusion/functions-table/src/generate_series.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub trait SeriesValue: fmt::Debug + Clone + Send + Sync + 'static {
fn should_stop(&self, end: Self, step: &Self::StepType, include_end: bool) -> bool;

/// Advance to the next value in the series
fn advance(&mut self, step: &Self::StepType) -> Result<()>;
fn advance(&mut self, end: &mut Self, step: &Self::StepType) -> Result<()>;

/// Create an Arrow array from a vector of values
fn create_array(&self, values: Vec<Self::ValueType>) -> Result<ArrayRef>;
Expand All @@ -97,8 +97,16 @@ impl SeriesValue for i64 {
reach_end_int64(*self, end, *step, include_end)
}

fn advance(&mut self, step: &Self::StepType) -> Result<()> {
*self += step;
fn advance(&mut self, end: &mut Self, step: &Self::StepType) -> Result<()> {
if let Some(next) = self.checked_add(*step) {
*self = next;
} else {
*end = if *step > 0 {
self.saturating_sub(1)
} else {
self.saturating_add(1)
};
}
Ok(())
}

Expand Down Expand Up @@ -152,7 +160,7 @@ impl SeriesValue for TimestampValue {
}
}

fn advance(&mut self, step: &Self::StepType) -> Result<()> {
fn advance(&mut self, _end: &mut Self, step: &Self::StepType) -> Result<()> {
let tz = self
.parsed_tz
.unwrap_or_else(|| Tz::from_str("+00:00").unwrap());
Expand Down Expand Up @@ -391,7 +399,15 @@ impl<T: SeriesValue> LazyBatchGenerator for GenericSeriesState<T> {
.should_stop(self.end.clone(), &self.step, self.include_end)
{
buf.push(self.current.to_value_type());
self.current.advance(&self.step)?;
if self
.current
.should_stop(self.end.clone(), &self.step, false)
{
self.current.advance(&mut self.end, &self.step)?;
break;
}

self.current.advance(&mut self.end, &self.step)?;
}

if buf.is_empty() {
Expand Down
8 changes: 8 additions & 0 deletions datafusion/sqllogictest/test_files/table_functions.slt
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,14 @@ SELECT * FROM generate_series(1, 2, 3, 4)
statement error DataFusion error: Error during planning: Argument \#1 must be an INTEGER, TIMESTAMP, DATE or NULL, got Utf8
SELECT * FROM generate_series('foo', 'bar')

# Regression test: generate_series with a step that would overflow i64 after the last
# included value must return the reachable values rather than an error, matching
# PostgreSQL/DuckDB behavior.
query I
SELECT * FROM generate_series(9223372036854775806, 9223372036854775807, 2)
----
9223372036854775806

# UDF and UDTF `generate_series` can be used simultaneously
query ? rowsort
SELECT generate_series(1, t1.end) FROM generate_series(3, 5) as t1(end)
Expand Down
Loading