Skip to content

Commit 48cd864

Browse files
david-rowleytuhaihe
authored andcommitted
Various fixes for aggregates for Postgres-XL
Patch by Pavan Deolasee
1 parent 8aba3d0 commit 48cd864

3 files changed

Lines changed: 47 additions & 18 deletions

File tree

contrib/fixeddecimal/fixeddecimal--xlaggs.sql

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,25 @@
22
-- Aggregate Support
33

44

5-
CREATE FUNCTION fixeddecimalaggstatecombine(FIXEDDECIMALAGGSTATE, INTERNAL)
5+
CREATE FUNCTION fixeddecimalaggstatecombine(FIXEDDECIMALAGGSTATE, FIXEDDECIMALAGGSTATE)
66
RETURNS FIXEDDECIMALAGGSTATE
77
AS 'fixeddecimal', 'fixeddecimalaggstatecombine'
88
LANGUAGE C IMMUTABLE;
99

10-
CREATE FUNCTION fixeddecimal_avg_accum(INTERNAL, FIXEDDECIMAL)
11-
RETURNS INTERNAL
10+
CREATE FUNCTION fixeddecimal_avg_accum(FIXEDDECIMALAGGSTATE, FIXEDDECIMAL)
11+
RETURNS FIXEDDECIMALAGGSTATE
1212
AS 'fixeddecimal', 'fixeddecimal_avg_accum'
1313
LANGUAGE C IMMUTABLE;
1414

15-
CREATE FUNCTION fixeddecimal_sum(INTERNAL)
15+
CREATE FUNCTION fixeddecimal_sum(FIXEDDECIMALAGGSTATE)
1616
RETURNS FIXEDDECIMAL
1717
AS 'fixeddecimal', 'fixeddecimal_sum'
18-
LANGUAGE C IMMUTABLE STRICT;
18+
LANGUAGE C IMMUTABLE;
1919

20-
CREATE FUNCTION fixeddecimal_avg(INTERNAL)
20+
CREATE FUNCTION fixeddecimal_avg(FIXEDDECIMALAGGSTATE)
2121
RETURNS FIXEDDECIMAL
2222
AS 'fixeddecimal', 'fixeddecimal_avg'
23-
LANGUAGE C IMMUTABLE STRICT;
23+
LANGUAGE C IMMUTABLE;
2424

2525
CREATE AGGREGATE min(FIXEDDECIMAL) (
2626
SFUNC = fixeddecimalsmaller,
@@ -43,15 +43,15 @@ CREATE AGGREGATE sum(FIXEDDECIMAL) (
4343
CFUNC = fixeddecimalaggstatecombine,
4444
CTYPE = FIXEDDECIMALAGGSTATE,
4545
FINALFUNC = fixeddecimal_sum,
46-
STYPE = INTERNAL
46+
STYPE = FIXEDDECIMALAGGSTATE
4747
);
4848

4949
CREATE AGGREGATE avg(FIXEDDECIMAL) (
5050
SFUNC = fixeddecimal_avg_accum,
5151
CFUNC = fixeddecimalaggstatecombine,
5252
CTYPE = FIXEDDECIMALAGGSTATE,
5353
FINALFUNC = fixeddecimal_avg,
54-
STYPE = INTERNAL
54+
STYPE = FIXEDDECIMALAGGSTATE
5555
);
5656

5757

contrib/fixeddecimal/fixeddecimal.c

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1746,7 +1746,7 @@ fixeddecimalaggstatein(PG_FUNCTION_ARGS)
17461746
FixedDecimalAggState *state;
17471747
char *token;
17481748

1749-
state = (FixedDecimalAggState *) palloc0(sizeof(FixedDecimalAggState));
1749+
state = (FixedDecimalAggState *) palloc(sizeof(FixedDecimalAggState));
17501750

17511751
token = strtok(str, ":");
17521752
state->sumX = DatumGetInt64(DirectFunctionCall3(fixeddecimalin, CStringGetDatum(token), 0, -1));
@@ -1771,6 +1771,7 @@ fixeddecimalaggstateout(PG_FUNCTION_ARGS)
17711771
p = fixeddecimal2str(state->sumX, buf);
17721772
*p++ = ':';
17731773
p = pg_int64tostr(p, state->N);
1774+
17741775
PG_RETURN_CSTRING(pnstrdup(buf, p - buf));
17751776
}
17761777

@@ -1810,14 +1811,41 @@ fixeddecimalaggstatesend(PG_FUNCTION_ARGS)
18101811
Datum
18111812
fixeddecimalaggstatecombine(PG_FUNCTION_ARGS)
18121813
{
1813-
FixedDecimalAggState *state1 = (FixedDecimalAggState *) PG_GETARG_POINTER(0);
1814-
FixedDecimalAggState *state2 = (FixedDecimalAggState *) PG_GETARG_POINTER(1);
1815-
FixedDecimalAggState *newstate = palloc(sizeof(FixedDecimalAggState));
1814+
FixedDecimalAggState *collectstate;
1815+
FixedDecimalAggState *transstate;
1816+
MemoryContext agg_context;
1817+
MemoryContext old_context;
1818+
1819+
if (!AggCheckCallContext(fcinfo, &agg_context))
1820+
elog(ERROR, "aggregate function called in non-aggregate context");
1821+
1822+
old_context = MemoryContextSwitchTo(agg_context);
18161823

1817-
newstate->sumX = DatumGetInt64(DirectFunctionCall2(fixeddecimalpl, Int64GetDatum(state1->sumX), Int64GetDatum(state2->sumX)));
1818-
newstate->N = DatumGetInt64(DirectFunctionCall2(int8pl, Int64GetDatum(state1->N), Int64GetDatum(state2->N)));
1824+
collectstate = PG_ARGISNULL(0) ? NULL : (FixedDecimalAggState *)
1825+
PG_GETARG_POINTER(0);
18191826

1820-
PG_RETURN_POINTER(newstate);
1827+
if (collectstate == NULL)
1828+
{
1829+
collectstate = (FixedDecimalAggState *) palloc(sizeof
1830+
(FixedDecimalAggState));
1831+
collectstate->sumX = 0;
1832+
collectstate->N = 0;
1833+
}
1834+
1835+
transstate = PG_ARGISNULL(1) ? NULL : (FixedDecimalAggState *)
1836+
PG_GETARG_POINTER(1);
1837+
1838+
if (transstate == NULL)
1839+
PG_RETURN_POINTER(collectstate);
1840+
1841+
collectstate->sumX = DatumGetInt64(DirectFunctionCall2(fixeddecimalpl,
1842+
Int64GetDatum(collectstate->sumX), Int64GetDatum(transstate->sumX)));
1843+
collectstate->N = DatumGetInt64(DirectFunctionCall2(int8pl,
1844+
Int64GetDatum(collectstate->N), Int64GetDatum(transstate->N)));
1845+
1846+
MemoryContextSwitchTo(old_context);
1847+
1848+
PG_RETURN_POINTER(collectstate);
18211849
}
18221850

1823-
#endif /* PGXC */
1851+
#endif /* PGXC */

contrib/fixeddecimal/fixeddecimalaggstate.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ CREATE TYPE FIXEDDECIMALAGGSTATE (
3535
STORAGE = plain,
3636
CATEGORY = 'N',
3737
PREFERRED = false,
38-
COLLATABLE = false
38+
COLLATABLE = false,
39+
PASSEDBYVALUE
3940
);
4041

0 commit comments

Comments
 (0)