Skip to content

Commit 5781420

Browse files
committed
Add countany aggregate function
1 parent e91c75d commit 5781420

File tree

2 files changed

+54
-13
lines changed

2 files changed

+54
-13
lines changed

vops--1.0.sql

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2014,7 +2014,7 @@ create function high(tile vops_interval) returns interval as 'MODULE_PATHNAME','
20142014
-- int8 tile
20152015

20162016
create function vops_int8_const(opd int8) returns vops_int8 as 'MODULE_PATHNAME' language C parallel safe immutable strict;
2017-
create cast (int8 as vops_int8) with function vops_int8_const(int8) AS IMPLICIT;
2017+
create cast (int8 as vops_int8) with function vops_int8_const(int8); -- Make it explicit to avoid ambiguity between count(vops_int8) and count(int8)
20182018

20192019
create function vops_int8_group_by(state internal, group_by vops_int8, aggregates cstring, variadic anyarray) returns internal as 'MODULE_PATHNAME' language C parallel safe immutable;
20202020
create aggregate map(group_by vops_int8, aggregates cstring, variadic anyarray) (
@@ -2949,6 +2949,14 @@ CREATE AGGREGATE countall(*) (
29492949
INITCOND = '0',
29502950
PARALLEL = SAFE
29512951
);
2952+
create function vops_count_accumulate_any(state int8, val anyelement) returns int8 as 'MODULE_PATHNAME' language C parallel safe strict;
2953+
CREATE AGGREGATE countany(anyelement) (
2954+
SFUNC = vops_count_accumulate_any,
2955+
STYPE = int8,
2956+
COMBINEFUNC = int8pl,
2957+
INITCOND = '0',
2958+
PARALLEL = SAFE
2959+
);
29522960
create function vops_count_stub(state vops_int8) returns vops_int8 as 'MODULE_PATHNAME','vops_window_accumulate' language C parallel safe strict;
29532961
create function vops_count_extend(state vops_int8) returns vops_int8 as 'MODULE_PATHNAME' language C parallel safe strict;
29542962
create function vops_count_reduce(state vops_int8) returns vops_int8 as 'MODULE_PATHNAME','vops_window_reduce' language C parallel safe strict;

vops.c

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,21 @@ Datum vops_count_accumulate(PG_FUNCTION_ARGS)
10171017
PG_RETURN_INT64(count);
10181018
}
10191019

1020+
PG_FUNCTION_INFO_V1(vops_count_accumulate_any);
1021+
Datum vops_count_accumulate_any(PG_FUNCTION_ARGS)
1022+
{
1023+
vops_tile_hdr* opd = PG_GETARG_VOPS_HDR(0);
1024+
int64 count = PG_GETARG_INT64(1);
1025+
uint64 mask = filter_mask & ~opd->empty_mask;
1026+
int i;
1027+
for (i = 0; i < TILE_SIZE; i++) {
1028+
if (mask & ((uint64)1 << i)) {
1029+
count += 1;
1030+
}
1031+
}
1032+
PG_RETURN_INT64(count);
1033+
}
1034+
10201035
static EState *estate;
10211036
static TupleTableSlot* slot;
10221037
static Relation rel;
@@ -2603,6 +2618,7 @@ static Oid vops_and_oid;
26032618
static Oid vops_or_oid;
26042619
static Oid vops_not_oid;
26052620
static Oid countall_oid;
2621+
static Oid countany_oid;
26062622
static Oid count_oid;
26072623
static Oid is_null_oid;
26082624
static Oid is_not_null_oid;
@@ -2611,9 +2627,31 @@ static Oid coalesce_oids[VOPS_LAST];
26112627
typedef struct
26122628
{
26132629
Aggref* countall;
2630+
Node* vector_col;
26142631
bool has_vector_ops;
26152632
} vops_mutator_context;
26162633

2634+
2635+
static void replace_count(vops_mutator_context* ctx)
2636+
{
2637+
Aggref* count = ctx->countall;
2638+
if (count != NULL)
2639+
{
2640+
if (ctx->vector_col)
2641+
{
2642+
count->aggfnoid = countany_oid;
2643+
count->aggstar = false;
2644+
count->aggargtypes = list_make1_oid(exprType(ctx->vector_col));
2645+
count->args = list_make1(ctx->vector_col);
2646+
}
2647+
else
2648+
{
2649+
count->aggfnoid = countall_oid;
2650+
}
2651+
ctx->countall = NULL;
2652+
}
2653+
}
2654+
26172655
static Node*
26182656
vops_expression_tree_mutator(Node *node, void *context)
26192657
{
@@ -2626,6 +2664,7 @@ vops_expression_tree_mutator(Node *node, void *context)
26262664
{
26272665
vops_mutator_context save_ctx = *ctx;
26282666
ctx->countall = NULL;
2667+
ctx->vector_col = NULL;
26292668
ctx->has_vector_ops = false;
26302669
node = (Node *) query_tree_mutator((Query *) node,
26312670
vops_expression_tree_mutator,
@@ -2702,10 +2741,8 @@ vops_expression_tree_mutator(Node *node, void *context)
27022741
if (!test->argisrow && is_vops_type(exprType((Node*)test->arg)))
27032742
{
27042743
ctx->has_vector_ops = true;
2705-
if (ctx->countall) {
2706-
ctx->countall->aggfnoid = countall_oid;
2707-
ctx->countall = NULL;
2708-
}
2744+
ctx->vector_col = (Node*)test->arg;
2745+
replace_count(ctx);
27092746
return (Node*)makeFuncExpr(filter_oid, BOOLOID,
27102747
list_make1(makeFuncExpr(test->nulltesttype == IS_NULL
27112748
? is_null_oid
@@ -2720,10 +2757,7 @@ vops_expression_tree_mutator(Node *node, void *context)
27202757
else if (IsA(node, FuncExpr) && !ctx->has_vector_ops && ((FuncExpr*)node)->funcid == filter_oid)
27212758
{
27222759
ctx->has_vector_ops = true;
2723-
if (ctx->countall) {
2724-
ctx->countall->aggfnoid = countall_oid;
2725-
ctx->countall = NULL;
2726-
}
2760+
replace_count(ctx);
27272761
}
27282762
else if (IsA(node, Aggref))
27292763
{
@@ -2741,10 +2775,8 @@ vops_expression_tree_mutator(Node *node, void *context)
27412775
if (is_vops_type(linitial_oid(agg->aggargtypes)))
27422776
{
27432777
ctx->has_vector_ops = true;
2744-
if (ctx->countall) {
2745-
ctx->countall->aggfnoid = countall_oid;
2746-
ctx->countall = NULL;
2747-
}
2778+
ctx->vector_col = (Node*)linitial(agg->args);
2779+
replace_count(ctx);
27482780
}
27492781
}
27502782
}
@@ -2786,6 +2818,7 @@ static void vops_post_parse_analysis_hook(ParseState *pstate, Query *query)
27862818
vops_not_oid = LookupFuncName(list_make1(makeString("vops_bool_not")), 1, profile, false);
27872819
count_oid = LookupFuncName(list_make1(makeString("count")), 0, profile, false);
27882820
countall_oid = LookupFuncName(list_make1(makeString("countall")), 0, profile, false);
2821+
countany_oid = LookupFuncName(list_make1(makeString("countany")), 1, &any, false);
27892822
is_null_oid = LookupFuncName(list_make1(makeString("is_null")), 1, &any, false);
27902823

27912824
for (i = VOPS_CHAR; i < VOPS_LAST; i++) {

0 commit comments

Comments
 (0)