aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils
diff options
context:
space:
mode:
authorAndres Freund <andres@anarazel.de>2018-02-15 21:55:31 -0800
committerAndres Freund <andres@anarazel.de>2018-02-16 14:38:13 -0800
commitbf6c614a2f2c58312b3be34a47e7fb7362e07bcb (patch)
treebbc91aed13afc3ef3b71bada700322197fe40b69 /src/backend/utils
parentad9a274778d2d88c46b90309212b92ee7fdf9afe (diff)
downloadpostgresql-bf6c614a2f2c58312b3be34a47e7fb7362e07bcb.tar.gz
postgresql-bf6c614a2f2c58312b3be34a47e7fb7362e07bcb.zip
Do execGrouping.c via expression eval machinery, take two.
This has a performance benefit on own, although not hugely so. The primary benefit is that it will allow for to JIT tuple deforming and comparator invocations. Large parts of this were previously committed (773aec7aa), but the commit contained an omission around cross-type comparisons and was thus reverted. Author: Andres Freund Discussion: https://postgr.es/m/20171129080934.amqqkke2zjtekd4t@alap3.anarazel.de
Diffstat (limited to 'src/backend/utils')
-rw-r--r--src/backend/utils/adt/orderedsetaggs.c56
1 files changed, 28 insertions, 28 deletions
diff --git a/src/backend/utils/adt/orderedsetaggs.c b/src/backend/utils/adt/orderedsetaggs.c
index 63d9c670274..50b34fcbc68 100644
--- a/src/backend/utils/adt/orderedsetaggs.c
+++ b/src/backend/utils/adt/orderedsetaggs.c
@@ -27,6 +27,7 @@
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
+#include "utils/memutils.h"
#include "utils/timestamp.h"
#include "utils/tuplesort.h"
@@ -54,6 +55,8 @@ typedef struct OSAPerQueryState
Aggref *aggref;
/* Memory context containing this struct and other per-query data: */
MemoryContext qcontext;
+ /* Context for expression evaluation */
+ ExprContext *econtext;
/* Do we expect multiple final-function calls within one group? */
bool rescan_needed;
@@ -71,7 +74,7 @@ typedef struct OSAPerQueryState
Oid *sortCollations;
bool *sortNullsFirsts;
/* Equality operator call info, created only if needed: */
- FmgrInfo *equalfns;
+ ExprState *compareTuple;
/* These fields are used only when accumulating datums: */
@@ -1287,6 +1290,8 @@ hypothetical_cume_dist_final(PG_FUNCTION_ARGS)
Datum
hypothetical_dense_rank_final(PG_FUNCTION_ARGS)
{
+ ExprContext *econtext;
+ ExprState *compareTuple;
int nargs = PG_NARGS() - 1;
int64 rank = 1;
int64 duplicate_count = 0;
@@ -1294,12 +1299,9 @@ hypothetical_dense_rank_final(PG_FUNCTION_ARGS)
int numDistinctCols;
Datum abbrevVal = (Datum) 0;
Datum abbrevOld = (Datum) 0;
- AttrNumber *sortColIdx;
- FmgrInfo *equalfns;
TupleTableSlot *slot;
TupleTableSlot *extraslot;
TupleTableSlot *slot2;
- MemoryContext tmpcontext;
int i;
Assert(AggCheckCallContext(fcinfo, NULL) == AGG_CONTEXT_AGGREGATE);
@@ -1309,6 +1311,9 @@ hypothetical_dense_rank_final(PG_FUNCTION_ARGS)
PG_RETURN_INT64(rank);
osastate = (OSAPerGroupState *) PG_GETARG_POINTER(0);
+ econtext = osastate->qstate->econtext;
+ if (!econtext)
+ osastate->qstate->econtext = econtext = CreateStandaloneExprContext();
/* Adjust nargs to be the number of direct (or aggregated) args */
if (nargs % 2 != 0)
@@ -1323,26 +1328,22 @@ hypothetical_dense_rank_final(PG_FUNCTION_ARGS)
*/
numDistinctCols = osastate->qstate->numSortCols - 1;
- /* Look up the equality function(s), if we didn't already */
- equalfns = osastate->qstate->equalfns;
- if (equalfns == NULL)
+ /* Build tuple comparator, if we didn't already */
+ compareTuple = osastate->qstate->compareTuple;
+ if (compareTuple == NULL)
{
- MemoryContext qcontext = osastate->qstate->qcontext;
-
- equalfns = (FmgrInfo *)
- MemoryContextAlloc(qcontext, numDistinctCols * sizeof(FmgrInfo));
- for (i = 0; i < numDistinctCols; i++)
- {
- fmgr_info_cxt(get_opcode(osastate->qstate->eqOperators[i]),
- &equalfns[i],
- qcontext);
- }
- osastate->qstate->equalfns = equalfns;
+ AttrNumber *sortColIdx = osastate->qstate->sortColIdx;
+ MemoryContext oldContext;
+
+ oldContext = MemoryContextSwitchTo(osastate->qstate->qcontext);
+ compareTuple = execTuplesMatchPrepare(osastate->qstate->tupdesc,
+ numDistinctCols,
+ sortColIdx,
+ osastate->qstate->eqOperators,
+ NULL);
+ MemoryContextSwitchTo(oldContext);
+ osastate->qstate->compareTuple = compareTuple;
}
- sortColIdx = osastate->qstate->sortColIdx;
-
- /* Get short-term context we can use for execTuplesMatch */
- tmpcontext = AggGetTempMemoryContext(fcinfo);
/* because we need a hypothetical row, we can't share transition state */
Assert(!osastate->sort_done);
@@ -1385,19 +1386,18 @@ hypothetical_dense_rank_final(PG_FUNCTION_ARGS)
break;
/* count non-distinct tuples */
+ econtext->ecxt_outertuple = slot;
+ econtext->ecxt_innertuple = slot2;
+
if (!TupIsNull(slot2) &&
abbrevVal == abbrevOld &&
- execTuplesMatch(slot, slot2,
- numDistinctCols,
- sortColIdx,
- equalfns,
- tmpcontext))
+ ExecQualAndReset(compareTuple, econtext))
duplicate_count++;
tmpslot = slot2;
slot2 = slot;
slot = tmpslot;
- /* avoid execTuplesMatch() calls by reusing abbreviated keys */
+ /* avoid ExecQual() calls by reusing abbreviated keys */
abbrevOld = abbrevVal;
rank++;