Skip to content

Commit 9d229f3

Browse files
committed
Provide moving-aggregate support for a bunch of numerical aggregates.
First installment of the promised moving-aggregate support in built-in aggregates: count(), sum(), avg(), stddev() and variance() for assorted datatypes, though not for float4/float8. In passing, remove a 2001-vintage kluge in interval_accum(): interval array elements have been properly aligned since around 2003, but nobody remembered to take out this workaround. Also, fix a thinko in the opr_sanity tests for moving-aggregate catalog entries. David Rowley and Florian Pflug, reviewed by Dean Rasheed
1 parent a9d9acb commit 9d229f3

File tree

13 files changed

+1230
-222
lines changed

13 files changed

+1230
-222
lines changed

src/backend/utils/adt/int8.c

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -717,13 +717,58 @@ int8inc(PG_FUNCTION_ARGS)
717717
}
718718
}
719719

720+
Datum
721+
int8dec(PG_FUNCTION_ARGS)
722+
{
723+
/*
724+
* When int8 is pass-by-reference, we provide this special case to avoid
725+
* palloc overhead for COUNT(): when called as an aggregate, we know that
726+
* the argument is modifiable local storage, so just update it in-place.
727+
* (If int8 is pass-by-value, then of course this is useless as well as
728+
* incorrect, so just ifdef it out.)
729+
*/
730+
#ifndef USE_FLOAT8_BYVAL /* controls int8 too */
731+
if (AggCheckCallContext(fcinfo, NULL))
732+
{
733+
int64 *arg = (int64 *) PG_GETARG_POINTER(0);
734+
int64 result;
735+
736+
result = *arg - 1;
737+
/* Overflow check */
738+
if (result > 0 && *arg < 0)
739+
ereport(ERROR,
740+
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
741+
errmsg("bigint out of range")));
742+
743+
*arg = result;
744+
PG_RETURN_POINTER(arg);
745+
}
746+
else
747+
#endif
748+
{
749+
/* Not called as an aggregate, so just do it the dumb way */
750+
int64 arg = PG_GETARG_INT64(0);
751+
int64 result;
752+
753+
result = arg - 1;
754+
/* Overflow check */
755+
if (result > 0 && arg < 0)
756+
ereport(ERROR,
757+
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
758+
errmsg("bigint out of range")));
759+
760+
PG_RETURN_INT64(result);
761+
}
762+
}
763+
764+
720765
/*
721-
* These functions are exactly like int8inc but are used for aggregates that
722-
* count only non-null values. Since the functions are declared strict,
723-
* the null checks happen before we ever get here, and all we need do is
724-
* increment the state value. We could actually make these pg_proc entries
725-
* point right at int8inc, but then the opr_sanity regression test would
726-
* complain about mismatched entries for a built-in function.
766+
* These functions are exactly like int8inc/int8dec but are used for
767+
* aggregates that count only non-null values. Since the functions are
768+
* declared strict, the null checks happen before we ever get here, and all we
769+
* need do is increment the state value. We could actually make these pg_proc
770+
* entries point right at int8inc/int8dec, but then the opr_sanity regression
771+
* test would complain about mismatched entries for a built-in function.
727772
*/
728773

729774
Datum
@@ -738,6 +783,12 @@ int8inc_float8_float8(PG_FUNCTION_ARGS)
738783
return int8inc(fcinfo);
739784
}
740785

786+
Datum
787+
int8dec_any(PG_FUNCTION_ARGS)
788+
{
789+
return int8dec(fcinfo);
790+
}
791+
741792

742793
Datum
743794
int8larger(PG_FUNCTION_ARGS)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy