diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index 78bac40..46d2716 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -1304,7 +1304,6 @@ generate_series_step_int4(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; generate_series_fctx *fctx; - int32 result; MemoryContext oldcontext; /* stuff done only on the first call of the function */ @@ -1343,25 +1342,34 @@ generate_series_step_int4(PG_FUNCTION_ARGS) funcctx->user_fctx = fctx; MemoryContextSwitchTo(oldcontext); + + /* ensure first value in series should exist */ + if ((fctx->step > 0 && fctx->current + fctx->step <= fctx->finish) || + (fctx->step < 0 && fctx->current + fctx->step >= fctx->finish)) + /* send off first value in series */ + SRF_RETURN_NEXT(funcctx, Int32GetDatum(start)); } /* stuff done on every call of the function */ funcctx = SRF_PERCALL_SETUP(); /* - * get the saved state and use current as the result for this iteration + * get the saved state */ fctx = funcctx->user_fctx; - result = fctx->current; - if ((fctx->step > 0 && fctx->current <= fctx->finish) || - (fctx->step < 0 && fctx->current >= fctx->finish)) + if ((fctx->step > 0 && + fctx->current < fctx->current + fctx->step && + fctx->current + fctx->step <= fctx->finish) || + (fctx->step < 0 && + fctx->current > fctx->current + fctx->step && + fctx->current + fctx->step >= fctx->finish)) { /* increment current in preparation for next iteration */ fctx->current += fctx->step; - /* do when there is more left to send */ - SRF_RETURN_NEXT(funcctx, Int32GetDatum(result)); + /* send off current value in series */ + SRF_RETURN_NEXT(funcctx, Int32GetDatum(fctx->current)); } else /* do when there is no more left */ diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index bbab90c..4e85195 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -1337,7 +1337,6 @@ generate_series_step_int8(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; generate_series_fctx *fctx; - int64 result; MemoryContext oldcontext; /* stuff done only on the first call of the function */ @@ -1376,25 +1375,35 @@ generate_series_step_int8(PG_FUNCTION_ARGS) funcctx->user_fctx = fctx; MemoryContextSwitchTo(oldcontext); + + /* ensure first value in series should exist */ + if ((fctx->step > 0 && fctx->current + fctx->step <= fctx->finish) || + (fctx->step < 0 && fctx->current + fctx->step >= fctx->finish)) + /* send off first value in series */ + SRF_RETURN_NEXT(funcctx, Int64GetDatum(start)); + } /* stuff done on every call of the function */ funcctx = SRF_PERCALL_SETUP(); /* - * get the saved state and use current as the result for this iteration + * get the saved state */ fctx = funcctx->user_fctx; - result = fctx->current; - if ((fctx->step > 0 && fctx->current <= fctx->finish) || - (fctx->step < 0 && fctx->current >= fctx->finish)) + if ((fctx->step > 0 && + fctx->current < fctx->current + fctx->step && + fctx->current + fctx->step <= fctx->finish) || + (fctx->step < 0 && + fctx->current > fctx->current + fctx->step && + fctx->current + fctx->step >= fctx->finish)) { /* increment current in preparation for next iteration */ fctx->current += fctx->step; - /* do when there is more left to send */ - SRF_RETURN_NEXT(funcctx, Int64GetDatum(result)); + /* send off current value in series */ + SRF_RETURN_NEXT(funcctx, Int64GetDatum(fctx->current)); } else /* do when there is no more left */