Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit fbcadc2

Browse files
committed
Replace count(*) with vcount(vattrs) instead of countall(*)
1 parent 3ef7503 commit fbcadc2

File tree

2 files changed

+102
-63
lines changed

2 files changed

+102
-63
lines changed

vops--1.0.sql

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ create type vops_text (
125125
);
126126

127127
create type deltatime (input=vops_deltatime_input, output=vops_deltatime_output, like=int8);
128-
create cast (interval as deltatime) with function vops_time_interval(interval) AS IMPLICIT;
128+
create cast (interval as deltatime) with function vops_time_interval(interval) as implicit;
129129

130130
-- text tile
131131

@@ -229,7 +229,7 @@ create function high(tile vops_text) returns text as 'MODULE_PATHNAME','vops_tex
229229
-- char tile
230230

231231
create function vops_char_const(opd "char") returns vops_char as 'MODULE_PATHNAME' language C parallel safe immutable strict;
232-
create cast ("char" as vops_char) with function vops_char_const("char") AS IMPLICIT;
232+
create cast ("char" as vops_char) with function vops_char_const("char");
233233

234234
create function vops_char_concat(left vops_char, right vops_char) returns vops_int2 as 'MODULE_PATHNAME' language C parallel safe immutable strict;
235235
create operator || (leftarg=vops_char, rightarg=vops_char, procedure=vops_char_concat);
@@ -599,7 +599,7 @@ create function high(tile vops_char) returns "char" as 'MODULE_PATHNAME','vops_c
599599
-- int2 tile
600600

601601
create function vops_int2_const(opd int4) returns vops_int2 as 'MODULE_PATHNAME' language C parallel safe immutable strict;
602-
create cast (int4 as vops_int2) with function vops_int2_const(int4) AS IMPLICIT;
602+
create cast (int4 as vops_int2) with function vops_int2_const(int4);
603603

604604
create function vops_int2_concat(left vops_int2, right vops_int2) returns vops_int4 as 'MODULE_PATHNAME' language C parallel safe immutable strict;
605605
create operator || (leftarg=vops_int2, rightarg=vops_int2, procedure=vops_int2_concat);
@@ -951,7 +951,7 @@ create function high(tile vops_int2) returns int2 as 'MODULE_PATHNAME','vops_int
951951
-- int4 tile
952952

953953
create function vops_int4_const(opd int4) returns vops_int4 as 'MODULE_PATHNAME' language C parallel safe immutable strict;
954-
create cast (int4 as vops_int4) with function vops_int4_const(int4) AS IMPLICIT;
954+
create cast (int4 as vops_int4) with function vops_int4_const(int4);
955955

956956
create function vops_int4_concat(left vops_int4, right vops_int4) returns vops_int8 as 'MODULE_PATHNAME' language C parallel safe immutable strict;
957957
create operator || (leftarg=vops_int4, rightarg=vops_int4, procedure=vops_int4_concat);
@@ -1305,7 +1305,7 @@ create function high(tile vops_int4) returns int4 as 'MODULE_PATHNAME','vops_int
13051305
create function date_bucket(interval, vops_date) returns vops_date as 'MODULE_PATHNAME','vops_date_bucket' language C parallel safe immutable strict;
13061306

13071307
create function vops_date_const(opd date) returns vops_date as 'MODULE_PATHNAME','vops_int4_const' language C parallel safe immutable strict;
1308-
create cast (date as vops_date) with function vops_date_const(date) AS IMPLICIT;
1308+
create cast (date as vops_date) with function vops_date_const(date);
13091309

13101310
create function vops_date_group_by(state internal, group_by vops_date, aggregates cstring, variadic anyarray) returns internal as 'MODULE_PATHNAME','vops_int4_group_by' language C parallel safe immutable;
13111311
create aggregate map(group_by vops_date, aggregates cstring, variadic anyarray) (
@@ -1655,7 +1655,7 @@ create function high(tile vops_date) returns date as 'MODULE_PATHNAME','vops_int
16551655
create function time_bucket(interval, vops_timestamp) returns vops_timestamp as 'MODULE_PATHNAME','vops_time_bucket' language C parallel safe immutable strict;
16561656

16571657
create function vops_timestamp_const(opd timestamp) returns vops_timestamp as 'MODULE_PATHNAME','vops_int8_const' language C parallel safe immutable strict;
1658-
create cast (timestamp as vops_timestamp) with function vops_timestamp_const(timestamp) AS IMPLICIT;
1658+
create cast (timestamp as vops_timestamp) with function vops_timestamp_const(timestamp);
16591659

16601660
create function vops_timestamp_group_by(state internal, group_by vops_timestamp, aggregates cstring, variadic anyarray) returns internal as 'MODULE_PATHNAME','vops_int8_group_by' language C immutable;
16611661
create aggregate map(group_by vops_timestamp, aggregates cstring, variadic anyarray) (
@@ -1989,7 +1989,7 @@ create function high(tile vops_timestamp) returns timestamp as 'MODULE_PATHNAME'
19891989
-- deltatime tile
19901990

19911991
create function vops_interval_const(opd deltatime) returns vops_interval as 'MODULE_PATHNAME','vops_int8_const' language C parallel safe immutable strict;
1992-
create cast (deltatime as vops_interval) with function vops_interval_const(deltatime) AS IMPLICIT;
1992+
create cast (deltatime as vops_interval) with function vops_interval_const(deltatime);
19931993

19941994
create function vops_interval_group_by(state internal, group_by vops_interval, aggregates cstring, variadic anyarray) returns internal as 'MODULE_PATHNAME','vops_int8_group_by' language C immutable;
19951995
create aggregate map(group_by vops_interval, aggregates cstring, variadic anyarray) (
@@ -2350,7 +2350,7 @@ create function high(tile vops_interval) returns deltatime as 'MODULE_PATHNAME',
23502350
-- int8 tile
23512351

23522352
create function vops_int8_const(opd int8) returns vops_int8 as 'MODULE_PATHNAME' language C parallel safe immutable strict;
2353-
create cast (int8 as vops_int8) with function vops_int8_const(int8) AS IMPLICIT;
2353+
create cast (int8 as vops_int8) with function vops_int8_const(int8);
23542354

23552355
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;
23562356
create aggregate map(group_by vops_int8, aggregates cstring, variadic anyarray) (
@@ -2703,7 +2703,7 @@ create function high(tile vops_int8) returns int8 as 'MODULE_PATHNAME','vops_int
27032703
-- float4 tile
27042704

27052705
create function vops_float4_const(opd float8) returns vops_float4 as 'MODULE_PATHNAME' language C parallel safe immutable strict;
2706-
create cast (float8 as vops_float4) with function vops_float4_const(float8) AS IMPLICIT;
2706+
create cast (float8 as vops_float4) with function vops_float4_const(float8);
27072707

27082708
create function vops_float4_sub(left vops_float4, right vops_float4) returns vops_float4 as 'MODULE_PATHNAME' language C parallel safe immutable strict;
27092709
create function vops_float4_sub_rconst(left vops_float4, right float8) returns vops_float4 as 'MODULE_PATHNAME' language C parallel safe immutable strict;
@@ -3039,7 +3039,7 @@ create function high(tile vops_float4) returns float4 as 'MODULE_PATHNAME','vops
30393039
-- float8 tile
30403040

30413041
create function vops_float8_const(opd float8) returns vops_float8 as 'MODULE_PATHNAME' language C parallel safe immutable strict;
3042-
create cast (float8 as vops_float8) with function vops_float8_const(float8) AS IMPLICIT;
3042+
create cast (float8 as vops_float8) with function vops_float8_const(float8);
30433043

30443044
create function vops_float8_sub(left vops_float8, right vops_float8) returns vops_float8 as 'MODULE_PATHNAME' language C parallel safe immutable strict;
30453045
create function vops_float8_sub_rconst(left vops_float8, right float8) returns vops_float8 as 'MODULE_PATHNAME' language C parallel safe immutable strict;
@@ -3384,8 +3384,16 @@ create function vops_bool_and(left vops_bool, right vops_bool) returns vops_bool
33843384
create operator & (leftarg=vops_bool, rightarg=vops_bool, procedure=vops_bool_and, commutator= &);
33853385

33863386

3387-
create function vops_count_accumulate(state int8) returns int8 as 'MODULE_PATHNAME' language C parallel safe strict;
3387+
create function vops_count_all_accumulate(state int8) returns int8 as 'MODULE_PATHNAME' language C parallel safe strict;
33883388
CREATE AGGREGATE countall(*) (
3389+
SFUNC = vops_count_all_accumulate,
3390+
STYPE = int8,
3391+
COMBINEFUNC = int8pl,
3392+
INITCOND = '0',
3393+
PARALLEL = SAFE
3394+
);
3395+
create function vops_count_accumulate(state int8, val anyelement) returns int8 as 'MODULE_PATHNAME' language C parallel safe strict;
3396+
CREATE AGGREGATE vcount(anyelement) (
33893397
SFUNC = vops_count_accumulate,
33903398
STYPE = int8,
33913399
COMBINEFUNC = int8pl,

vops.c

Lines changed: 83 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,21 @@ LAST_AGG(bool,BOOL,BOOL_PAYLOAD)
968968
FIRST_FUNC(bool,Bool,first,<,BOOL_PAYLOAD)
969969
FIRST_FUNC(bool,Bool,last,>,BOOL_PAYLOAD)
970970

971+
PG_FUNCTION_INFO_V1(vops_count_accumulate);
972+
Datum vops_count_accumulate(PG_FUNCTION_ARGS)
973+
{
974+
vops_tile_hdr* opd = (vops_tile_hdr*)PG_GETARG_POINTER(1);
975+
int64 count = PG_GETARG_INT64(0);
976+
uint64 mask = filter_mask & ~opd->empty_mask;
977+
int i;
978+
for (i = 0; i < TILE_SIZE; i++) {
979+
if (mask & ((uint64)1 << i)) {
980+
count += 1;
981+
}
982+
}
983+
PG_RETURN_INT64(count);
984+
}
985+
971986
PG_FUNCTION_INFO_V1(vops_count_any_accumulate);
972987
Datum vops_count_any_accumulate(PG_FUNCTION_ARGS)
973988
{
@@ -1019,8 +1034,8 @@ Datum vops_count_extend(PG_FUNCTION_ARGS)
10191034
PG_RETURN_POINTER(state);
10201035
}
10211036

1022-
PG_FUNCTION_INFO_V1(vops_count_accumulate);
1023-
Datum vops_count_accumulate(PG_FUNCTION_ARGS)
1037+
PG_FUNCTION_INFO_V1(vops_count_all_accumulate);
1038+
Datum vops_count_all_accumulate(PG_FUNCTION_ARGS)
10241039
{
10251040
int64 count = PG_GETARG_INT64(0);
10261041
int i;
@@ -3747,36 +3762,71 @@ static Oid filter_oid;
37473762
static Oid vops_and_oid;
37483763
static Oid vops_or_oid;
37493764
static Oid vops_not_oid;
3750-
static Oid countall_oid;
3765+
static Oid vcount_oid;
37513766
static Oid count_oid;
37523767
static Oid is_null_oid;
37533768
static Oid is_not_null_oid;
37543769
static Oid coalesce_oids[VOPS_LAST];
37553770

37563771
typedef struct
37573772
{
3758-
Aggref* countall;
3759-
bool has_vector_ops;
3760-
} vops_mutator_context;
3773+
int varno;
3774+
int varattno;
3775+
int vartype;
3776+
} vops_var;
3777+
3778+
static bool
3779+
is_select_from_vops_projection(Query* query, vops_var* var)
3780+
{
3781+
int relno;
3782+
int n_rels = list_length(query->rtable);
3783+
for (relno = 1; relno <= n_rels; relno++)
3784+
{
3785+
RangeTblEntry* rte = list_nth_node(RangeTblEntry, query->rtable, relno-1);
3786+
if (rte->rtekind == RTE_RELATION)
3787+
{
3788+
Relation rel = table_open(rte->relid, NoLock); /* Assume we already have adequate lock */
3789+
int attno;
3790+
for (attno = 1; attno <= rel->rd_att->natts; attno++)
3791+
{
3792+
Oid typid = TupleDescAttr(rel->rd_att, attno-1)->atttypid;
3793+
if (is_vops_type(typid))
3794+
{
3795+
table_close(rel, NoLock);
3796+
var->varno = relno;
3797+
var->varattno = attno;
3798+
var->vartype = typid;
3799+
return true;
3800+
}
3801+
}
3802+
table_close(rel, NoLock);
3803+
}
3804+
}
3805+
return false;
3806+
}
3807+
3808+
37613809

37623810
static Node*
37633811
vops_expression_tree_mutator(Node *node, void *context)
37643812
{
3765-
vops_mutator_context* ctx = (vops_mutator_context*)context;
3813+
vops_var* var = (vops_var*)context;
37663814
if (node == NULL)
37673815
{
37683816
return NULL;
37693817
}
37703818
if (IsA(node, Query))
37713819
{
3772-
vops_mutator_context save_ctx = *ctx;
3773-
ctx->countall = NULL;
3774-
ctx->has_vector_ops = false;
3775-
node = (Node *) query_tree_mutator((Query *) node,
3776-
vops_expression_tree_mutator,
3777-
context,
3778-
QTW_DONT_COPY_QUERY);
3779-
*(vops_mutator_context*)context = save_ctx; /* restore query context */
3820+
vops_var save_var = *var;
3821+
Query* query = (Query*)node;
3822+
if (is_select_from_vops_projection(query, var))
3823+
{
3824+
node = (Node *) query_tree_mutator(query,
3825+
vops_expression_tree_mutator,
3826+
context,
3827+
QTW_DONT_COPY_QUERY);
3828+
*var = save_var; /* restore query context */
3829+
}
37803830
return node;
37813831
}
37823832
/* depth first traversal */
@@ -3851,11 +3901,6 @@ vops_expression_tree_mutator(Node *node, void *context)
38513901
NullTest* test = (NullTest*)node;
38523902
if (!test->argisrow && is_vops_type(exprType((Node*)test->arg)))
38533903
{
3854-
ctx->has_vector_ops = true;
3855-
if (ctx->countall) {
3856-
ctx->countall->aggfnoid = countall_oid;
3857-
ctx->countall = NULL;
3858-
}
38593904
return (Node*)makeFuncExpr(filter_oid, BOOLOID,
38603905
list_make1(makeFuncExpr(test->nulltesttype == IS_NULL
38613906
? is_null_oid
@@ -3867,35 +3912,15 @@ vops_expression_tree_mutator(Node *node, void *context)
38673912
InvalidOid, InvalidOid, COERCE_EXPLICIT_CALL);
38683913
}
38693914
}
3870-
else if (IsA(node, FuncExpr) && !ctx->has_vector_ops && ((FuncExpr*)node)->funcid == filter_oid)
3871-
{
3872-
ctx->has_vector_ops = true;
3873-
if (ctx->countall) {
3874-
ctx->countall->aggfnoid = countall_oid;
3875-
ctx->countall = NULL;
3876-
}
3877-
}
38783915
else if (IsA(node, Aggref))
38793916
{
38803917
Aggref* agg = (Aggref*)node;
38813918
if (agg->aggfnoid == count_oid) {
38823919
Assert(agg->aggstar);
3883-
if (ctx->has_vector_ops) {
3884-
agg->aggfnoid = countall_oid;
3885-
ctx->countall = NULL;
3886-
} else {
3887-
ctx->countall = agg;
3888-
}
3889-
} else if (!agg->aggstar && !ctx->has_vector_ops) {
3890-
Assert(list_length(agg->aggargtypes) >= 1);
3891-
if (is_vops_type(linitial_oid(agg->aggargtypes)))
3892-
{
3893-
ctx->has_vector_ops = true;
3894-
if (ctx->countall) {
3895-
ctx->countall->aggfnoid = countall_oid;
3896-
ctx->countall = NULL;
3897-
}
3898-
}
3920+
agg->aggfnoid = vcount_oid;
3921+
agg->aggstar = false;
3922+
agg->aggargtypes = list_make1_oid(var->vartype);
3923+
agg->args = list_make1(makeTargetEntry((Expr*)makeVar(var->varno, var->varattno, var->vartype, -1, 0, 0), 1, NULL, false));
38993924
}
39003925
}
39013926
else if (IsA(node, CoalesceExpr))
@@ -4490,7 +4515,7 @@ vops_resolve_functions(void)
44904515
vops_or_oid = LookupFuncName(list_make1(makeString("vops_bool_or")), 2, profile, false);
44914516
vops_not_oid = LookupFuncName(list_make1(makeString("vops_bool_not")), 1, profile, false);
44924517
count_oid = LookupFuncName(list_make1(makeString("count")), 0, profile, false);
4493-
countall_oid = LookupFuncName(list_make1(makeString("countall")), 0, profile, false);
4518+
vcount_oid = LookupFuncName(list_make1(makeString("vcount")), 1, &any, false);
44944519
is_null_oid = LookupFuncName(list_make1(makeString("is_null")), 1, &any, false);
44954520

44964521
for (i = VOPS_CHAR; i < VOPS_LAST; i++) {
@@ -4503,21 +4528,25 @@ vops_resolve_functions(void)
45034528

45044529
static void vops_post_parse_analysis_hook(ParseState *pstate, Query *query)
45054530
{
4506-
vops_mutator_context ctx = {NULL,false};
4531+
vops_var var;
45074532
/* Invoke original hook if needed */
45084533
if (post_parse_analyze_hook_next)
45094534
{
45104535
post_parse_analyze_hook_next(pstate, query);
45114536
}
45124537
vops_resolve_functions();
4538+
45134539
filter_mask = ~0;
45144540
if (query->commandType == CMD_SELECT &&
45154541
vops_auto_substitute_projections &&
45164542
pstate->p_paramref_hook == NULL) /* do not support prepared statements yet */
45174543
{
45184544
vops_substitute_tables_with_projections(pstate->p_sourcetext, query);
45194545
}
4520-
(void)query_tree_mutator(query, vops_expression_tree_mutator, &ctx, QTW_DONT_COPY_QUERY);
4546+
if (is_select_from_vops_projection(query, &var))
4547+
{
4548+
(void)query_tree_mutator(query, vops_expression_tree_mutator, &var, QTW_DONT_COPY_QUERY);
4549+
}
45214550
}
45224551

45234552
#if PG_VERSION_NUM>=110000
@@ -4547,14 +4576,14 @@ static void vops_explain_hook(Query *query,
45474576
{
45484577
int cursorOptions = 0;
45494578
#endif
4579+
vops_var var;
45504580
PlannedStmt *plan;
4551-
vops_mutator_context ctx = {NULL,false};
45524581
instr_time planstart, planduration;
45534582

4554-
vops_resolve_functions();
4555-
45564583
INSTR_TIME_SET_CURRENT(planstart);
45574584

4585+
vops_resolve_functions();
4586+
45584587
if (query->commandType == CMD_SELECT &&
45594588
vops_auto_substitute_projections &&
45604589
params == NULL) /* do not support prepared statements yet */
@@ -4570,8 +4599,10 @@ static void vops_explain_hook(Query *query,
45704599
memset(explain, ' ', prefix); /* clear "explain" prefix: we need to preseve node locations */
45714600
vops_substitute_tables_with_projections(explain, query);
45724601
}
4573-
(void)query_tree_mutator(query, vops_expression_tree_mutator, &ctx, QTW_DONT_COPY_QUERY);
4574-
4602+
if (is_select_from_vops_projection(query, &var))
4603+
{
4604+
(void)query_tree_mutator(query, vops_expression_tree_mutator, &var, QTW_DONT_COPY_QUERY);
4605+
}
45754606
plan = pg_plan_query(query, cursorOptions, params);
45764607

45774608
INSTR_TIME_SET_CURRENT(planduration);

0 commit comments

Comments
 (0)