PostgreSQL Source Code  git master
orderedsetaggs.c File Reference
#include "postgres.h"
#include <math.h>
#include "catalog/pg_aggregate.h"
#include "catalog/pg_operator.h"
#include "catalog/pg_type.h"
#include "executor/executor.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/timestamp.h"
#include "utils/tuplesort.h"
Include dependency graph for orderedsetaggs.c:

Go to the source code of this file.

Data Structures

struct  OSAPerQueryState
 
struct  OSAPerGroupState
 
struct  pct_info
 

Typedefs

typedef struct OSAPerQueryState OSAPerQueryState
 
typedef struct OSAPerGroupState OSAPerGroupState
 
typedef Datum(* LerpFunc) (Datum lo, Datum hi, double pct)
 

Functions

static void ordered_set_shutdown (Datum arg)
 
static OSAPerGroupStateordered_set_startup (FunctionCallInfo fcinfo, bool use_tuples)
 
Datum ordered_set_transition (PG_FUNCTION_ARGS)
 
Datum ordered_set_transition_multi (PG_FUNCTION_ARGS)
 
Datum percentile_disc_final (PG_FUNCTION_ARGS)
 
static Datum float8_lerp (Datum lo, Datum hi, double pct)
 
static Datum interval_lerp (Datum lo, Datum hi, double pct)
 
static Datum percentile_cont_final_common (FunctionCallInfo fcinfo, Oid expect_type, LerpFunc lerpfunc)
 
Datum percentile_cont_float8_final (PG_FUNCTION_ARGS)
 
Datum percentile_cont_interval_final (PG_FUNCTION_ARGS)
 
static int pct_info_cmp (const void *pa, const void *pb)
 
static struct pct_infosetup_pct_info (int num_percentiles, Datum *percentiles_datum, bool *percentiles_null, int64 rowcount, bool continuous)
 
Datum percentile_disc_multi_final (PG_FUNCTION_ARGS)
 
static Datum percentile_cont_multi_final_common (FunctionCallInfo fcinfo, Oid expect_type, int16 typLen, bool typByVal, char typAlign, LerpFunc lerpfunc)
 
Datum percentile_cont_float8_multi_final (PG_FUNCTION_ARGS)
 
Datum percentile_cont_interval_multi_final (PG_FUNCTION_ARGS)
 
Datum mode_final (PG_FUNCTION_ARGS)
 
static void hypothetical_check_argtypes (FunctionCallInfo fcinfo, int nargs, TupleDesc tupdesc)
 
static int64 hypothetical_rank_common (FunctionCallInfo fcinfo, int flag, int64 *number_of_rows)
 
Datum hypothetical_rank_final (PG_FUNCTION_ARGS)
 
Datum hypothetical_percent_rank_final (PG_FUNCTION_ARGS)
 
Datum hypothetical_cume_dist_final (PG_FUNCTION_ARGS)
 
Datum hypothetical_dense_rank_final (PG_FUNCTION_ARGS)
 

Typedef Documentation

◆ LerpFunc

typedef Datum(* LerpFunc) (Datum lo, Datum hi, double pct)

Definition at line 502 of file orderedsetaggs.c.

◆ OSAPerGroupState

◆ OSAPerQueryState

Function Documentation

◆ float8_lerp()

static Datum float8_lerp ( Datum  lo,
Datum  hi,
double  pct 
)
static

Definition at line 505 of file orderedsetaggs.c.

506 {
507  double loval = DatumGetFloat8(lo);
508  double hival = DatumGetFloat8(hi);
509 
510  return Float8GetDatum(loval + (pct * (hival - loval)));
511 }
Datum Float8GetDatum(float8 X)
Definition: fmgr.c:1692
static float8 DatumGetFloat8(Datum X)
Definition: postgres.h:842

References DatumGetFloat8(), and Float8GetDatum().

Referenced by percentile_cont_float8_final(), and percentile_cont_float8_multi_final().

◆ hypothetical_check_argtypes()

static void hypothetical_check_argtypes ( FunctionCallInfo  fcinfo,
int  nargs,
TupleDesc  tupdesc 
)
static

Definition at line 1144 of file orderedsetaggs.c.

1146 {
1147  int i;
1148 
1149  /* check that we have an int4 flag column */
1150  if (!tupdesc ||
1151  (nargs + 1) != tupdesc->natts ||
1152  TupleDescAttr(tupdesc, nargs)->atttypid != INT4OID)
1153  elog(ERROR, "type mismatch in hypothetical-set function");
1154 
1155  /* check that direct args match in type with aggregated args */
1156  for (i = 0; i < nargs; i++)
1157  {
1158  Form_pg_attribute attr = TupleDescAttr(tupdesc, i);
1159 
1160  if (get_fn_expr_argtype(fcinfo->flinfo, i + 1) != attr->atttypid)
1161  elog(ERROR, "type mismatch in hypothetical-set function");
1162  }
1163 }
#define ERROR
Definition: elog.h:35
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:1786
int i
Definition: isn.c:73
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:207
FmgrInfo * flinfo
Definition: fmgr.h:87
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92

References elog(), ERROR, FunctionCallInfoBaseData::flinfo, get_fn_expr_argtype(), i, TupleDescData::natts, and TupleDescAttr.

Referenced by hypothetical_dense_rank_final(), and hypothetical_rank_common().

◆ hypothetical_cume_dist_final()

Datum hypothetical_cume_dist_final ( PG_FUNCTION_ARGS  )

Definition at line 1280 of file orderedsetaggs.c.

1281 {
1282  int64 rank;
1283  int64 rowcount;
1284  double result_val;
1285 
1286  rank = hypothetical_rank_common(fcinfo, 1, &rowcount);
1287 
1288  result_val = (double) (rank) / (double) (rowcount + 1);
1289 
1290  PG_RETURN_FLOAT8(result_val);
1291 }
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:367
static int64 hypothetical_rank_common(FunctionCallInfo fcinfo, int flag, int64 *number_of_rows)

References hypothetical_rank_common(), and PG_RETURN_FLOAT8.

◆ hypothetical_dense_rank_final()

Datum hypothetical_dense_rank_final ( PG_FUNCTION_ARGS  )

Definition at line 1297 of file orderedsetaggs.c.

1298 {
1299  ExprContext *econtext;
1300  ExprState *compareTuple;
1301  int nargs = PG_NARGS() - 1;
1302  int64 rank = 1;
1303  int64 duplicate_count = 0;
1304  OSAPerGroupState *osastate;
1305  int numDistinctCols;
1306  Datum abbrevVal = (Datum) 0;
1307  Datum abbrevOld = (Datum) 0;
1308  TupleTableSlot *slot;
1309  TupleTableSlot *extraslot;
1310  TupleTableSlot *slot2;
1311  int i;
1312 
1314 
1315  /* If there were no regular rows, the rank is always 1 */
1316  if (PG_ARGISNULL(0))
1317  PG_RETURN_INT64(rank);
1318 
1319  osastate = (OSAPerGroupState *) PG_GETARG_POINTER(0);
1320  econtext = osastate->qstate->econtext;
1321  if (!econtext)
1322  {
1323  MemoryContext oldcontext;
1324 
1325  /* Make sure to we create econtext under correct parent context. */
1326  oldcontext = MemoryContextSwitchTo(osastate->qstate->qcontext);
1328  econtext = osastate->qstate->econtext;
1329  MemoryContextSwitchTo(oldcontext);
1330  }
1331 
1332  /* Adjust nargs to be the number of direct (or aggregated) args */
1333  if (nargs % 2 != 0)
1334  elog(ERROR, "wrong number of arguments in hypothetical-set function");
1335  nargs /= 2;
1336 
1337  hypothetical_check_argtypes(fcinfo, nargs, osastate->qstate->tupdesc);
1338 
1339  /*
1340  * When comparing tuples, we can omit the flag column since we will only
1341  * compare rows with flag == 0.
1342  */
1343  numDistinctCols = osastate->qstate->numSortCols - 1;
1344 
1345  /* Build tuple comparator, if we didn't already */
1346  compareTuple = osastate->qstate->compareTuple;
1347  if (compareTuple == NULL)
1348  {
1349  AttrNumber *sortColIdx = osastate->qstate->sortColIdx;
1350  MemoryContext oldContext;
1351 
1352  oldContext = MemoryContextSwitchTo(osastate->qstate->qcontext);
1353  compareTuple = execTuplesMatchPrepare(osastate->qstate->tupdesc,
1354  numDistinctCols,
1355  sortColIdx,
1356  osastate->qstate->eqOperators,
1357  osastate->qstate->sortCollations,
1358  NULL);
1359  MemoryContextSwitchTo(oldContext);
1360  osastate->qstate->compareTuple = compareTuple;
1361  }
1362 
1363  /* because we need a hypothetical row, we can't share transition state */
1364  Assert(!osastate->sort_done);
1365 
1366  /* insert the hypothetical row into the sort */
1367  slot = osastate->qstate->tupslot;
1368  ExecClearTuple(slot);
1369  for (i = 0; i < nargs; i++)
1370  {
1371  slot->tts_values[i] = PG_GETARG_DATUM(i + 1);
1372  slot->tts_isnull[i] = PG_ARGISNULL(i + 1);
1373  }
1374  slot->tts_values[i] = Int32GetDatum(-1);
1375  slot->tts_isnull[i] = false;
1376  ExecStoreVirtualTuple(slot);
1377 
1378  tuplesort_puttupleslot(osastate->sortstate, slot);
1379 
1380  /* finish the sort */
1381  tuplesort_performsort(osastate->sortstate);
1382  osastate->sort_done = true;
1383 
1384  /*
1385  * We alternate fetching into tupslot and extraslot so that we have the
1386  * previous row available for comparisons. This is accomplished by
1387  * swapping the slot pointer variables after each row.
1388  */
1389  extraslot = MakeSingleTupleTableSlot(osastate->qstate->tupdesc,
1391  slot2 = extraslot;
1392 
1393  /* iterate till we find the hypothetical row */
1394  while (tuplesort_gettupleslot(osastate->sortstate, true, true, slot,
1395  &abbrevVal))
1396  {
1397  bool isnull;
1398  Datum d = slot_getattr(slot, nargs + 1, &isnull);
1399  TupleTableSlot *tmpslot;
1400 
1401  if (!isnull && DatumGetInt32(d) != 0)
1402  break;
1403 
1404  /* count non-distinct tuples */
1405  econtext->ecxt_outertuple = slot;
1406  econtext->ecxt_innertuple = slot2;
1407 
1408  if (!TupIsNull(slot2) &&
1409  abbrevVal == abbrevOld &&
1410  ExecQualAndReset(compareTuple, econtext))
1411  duplicate_count++;
1412 
1413  tmpslot = slot2;
1414  slot2 = slot;
1415  slot = tmpslot;
1416  /* avoid ExecQual() calls by reusing abbreviated keys */
1417  abbrevOld = abbrevVal;
1418 
1419  rank++;
1420 
1422  }
1423 
1424  ExecClearTuple(slot);
1425  ExecClearTuple(slot2);
1426 
1427  ExecDropSingleTupleTableSlot(extraslot);
1428 
1429  rank = rank - duplicate_count;
1430 
1431  PG_RETURN_INT64(rank);
1432 }
int16 AttrNumber
Definition: attnum.h:21
ExprState * execTuplesMatchPrepare(TupleDesc desc, int numCols, const AttrNumber *keyColIdx, const Oid *eqOperators, const Oid *collations, PlanState *parent)
Definition: execGrouping.c:59
TupleTableSlot * ExecStoreVirtualTuple(TupleTableSlot *slot)
Definition: execTuples.c:1552
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
Definition: execTuples.c:1254
const TupleTableSlotOps TTSOpsMinimalTuple
Definition: execTuples.c:85
TupleTableSlot * MakeSingleTupleTableSlot(TupleDesc tupdesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1238
ExprContext * CreateStandaloneExprContext(void)
Definition: execUtils.c:353
static bool ExecQualAndReset(ExprState *state, ExprContext *econtext)
Definition: executor.h:425
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define PG_NARGS()
Definition: fmgr.h:203
#define AGG_CONTEXT_AGGREGATE
Definition: fmgr.h:753
Assert(fmt[strlen(fmt) - 1] !='\n')
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:121
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:4509
static void hypothetical_check_argtypes(FunctionCallInfo fcinfo, int nargs, TupleDesc tupdesc)
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:135
uintptr_t Datum
Definition: postgres.h:412
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:560
static int32 DatumGetInt32(Datum X)
Definition: postgres.h:550
TupleTableSlot * ecxt_innertuple
Definition: execnodes.h:249
TupleTableSlot * ecxt_outertuple
Definition: execnodes.h:251
OSAPerQueryState * qstate
Tuplesortstate * sortstate
ExprContext * econtext
AttrNumber * sortColIdx
TupleTableSlot * tupslot
MemoryContext qcontext
ExprState * compareTuple
bool * tts_isnull
Definition: tuptable.h:128
Datum * tts_values
Definition: tuptable.h:126
void tuplesort_performsort(Tuplesortstate *state)
Definition: tuplesort.c:1385
void tuplesort_puttupleslot(Tuplesortstate *state, TupleTableSlot *slot)
bool tuplesort_gettupleslot(Tuplesortstate *state, bool forward, bool copy, TupleTableSlot *slot, Datum *abbrev)
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:433
static Datum slot_getattr(TupleTableSlot *slot, int attnum, bool *isnull)
Definition: tuptable.h:389
#define TupIsNull(slot)
Definition: tuptable.h:300

References AGG_CONTEXT_AGGREGATE, AggCheckCallContext(), Assert(), CHECK_FOR_INTERRUPTS, OSAPerQueryState::compareTuple, CreateStandaloneExprContext(), DatumGetInt32(), OSAPerQueryState::econtext, ExprContext::ecxt_innertuple, ExprContext::ecxt_outertuple, elog(), OSAPerQueryState::eqOperators, ERROR, ExecClearTuple(), ExecDropSingleTupleTableSlot(), ExecQualAndReset(), ExecStoreVirtualTuple(), execTuplesMatchPrepare(), hypothetical_check_argtypes(), i, Int32GetDatum(), MakeSingleTupleTableSlot(), MemoryContextSwitchTo(), OSAPerQueryState::numSortCols, PG_ARGISNULL, PG_GETARG_DATUM, PG_GETARG_POINTER, PG_NARGS, PG_RETURN_INT64, OSAPerQueryState::qcontext, OSAPerGroupState::qstate, slot_getattr(), OSAPerGroupState::sort_done, OSAPerQueryState::sortColIdx, OSAPerQueryState::sortCollations, OSAPerGroupState::sortstate, TupleTableSlot::tts_isnull, TupleTableSlot::tts_values, TTSOpsMinimalTuple, OSAPerQueryState::tupdesc, TupIsNull, tuplesort_gettupleslot(), tuplesort_performsort(), tuplesort_puttupleslot(), and OSAPerQueryState::tupslot.

◆ hypothetical_percent_rank_final()

Datum hypothetical_percent_rank_final ( PG_FUNCTION_ARGS  )

Definition at line 1260 of file orderedsetaggs.c.

1261 {
1262  int64 rank;
1263  int64 rowcount;
1264  double result_val;
1265 
1266  rank = hypothetical_rank_common(fcinfo, -1, &rowcount);
1267 
1268  if (rowcount == 0)
1269  PG_RETURN_FLOAT8(0);
1270 
1271  result_val = (double) (rank - 1) / (double) (rowcount);
1272 
1273  PG_RETURN_FLOAT8(result_val);
1274 }

References hypothetical_rank_common(), and PG_RETURN_FLOAT8.

◆ hypothetical_rank_common()

static int64 hypothetical_rank_common ( FunctionCallInfo  fcinfo,
int  flag,
int64 *  number_of_rows 
)
static

Definition at line 1173 of file orderedsetaggs.c.

1175 {
1176  int nargs = PG_NARGS() - 1;
1177  int64 rank = 1;
1178  OSAPerGroupState *osastate;
1179  TupleTableSlot *slot;
1180  int i;
1181 
1183 
1184  /* If there were no regular rows, the rank is always 1 */
1185  if (PG_ARGISNULL(0))
1186  {
1187  *number_of_rows = 0;
1188  return 1;
1189  }
1190 
1191  osastate = (OSAPerGroupState *) PG_GETARG_POINTER(0);
1192  *number_of_rows = osastate->number_of_rows;
1193 
1194  /* Adjust nargs to be the number of direct (or aggregated) args */
1195  if (nargs % 2 != 0)
1196  elog(ERROR, "wrong number of arguments in hypothetical-set function");
1197  nargs /= 2;
1198 
1199  hypothetical_check_argtypes(fcinfo, nargs, osastate->qstate->tupdesc);
1200 
1201  /* because we need a hypothetical row, we can't share transition state */
1202  Assert(!osastate->sort_done);
1203 
1204  /* insert the hypothetical row into the sort */
1205  slot = osastate->qstate->tupslot;
1206  ExecClearTuple(slot);
1207  for (i = 0; i < nargs; i++)
1208  {
1209  slot->tts_values[i] = PG_GETARG_DATUM(i + 1);
1210  slot->tts_isnull[i] = PG_ARGISNULL(i + 1);
1211  }
1212  slot->tts_values[i] = Int32GetDatum(flag);
1213  slot->tts_isnull[i] = false;
1214  ExecStoreVirtualTuple(slot);
1215 
1216  tuplesort_puttupleslot(osastate->sortstate, slot);
1217 
1218  /* finish the sort */
1219  tuplesort_performsort(osastate->sortstate);
1220  osastate->sort_done = true;
1221 
1222  /* iterate till we find the hypothetical row */
1223  while (tuplesort_gettupleslot(osastate->sortstate, true, true, slot, NULL))
1224  {
1225  bool isnull;
1226  Datum d = slot_getattr(slot, nargs + 1, &isnull);
1227 
1228  if (!isnull && DatumGetInt32(d) != 0)
1229  break;
1230 
1231  rank++;
1232 
1234  }
1235 
1236  ExecClearTuple(slot);
1237 
1238  return rank;
1239 }
char * flag(int b)
Definition: test-ctype.c:33

References AGG_CONTEXT_AGGREGATE, AggCheckCallContext(), Assert(), CHECK_FOR_INTERRUPTS, DatumGetInt32(), elog(), ERROR, ExecClearTuple(), ExecStoreVirtualTuple(), flag(), hypothetical_check_argtypes(), i, Int32GetDatum(), OSAPerGroupState::number_of_rows, PG_ARGISNULL, PG_GETARG_DATUM, PG_GETARG_POINTER, PG_NARGS, OSAPerGroupState::qstate, slot_getattr(), OSAPerGroupState::sort_done, OSAPerGroupState::sortstate, TupleTableSlot::tts_isnull, TupleTableSlot::tts_values, OSAPerQueryState::tupdesc, tuplesort_gettupleslot(), tuplesort_performsort(), tuplesort_puttupleslot(), and OSAPerQueryState::tupslot.

Referenced by hypothetical_cume_dist_final(), hypothetical_percent_rank_final(), and hypothetical_rank_final().

◆ hypothetical_rank_final()

Datum hypothetical_rank_final ( PG_FUNCTION_ARGS  )

Definition at line 1246 of file orderedsetaggs.c.

1247 {
1248  int64 rank;
1249  int64 rowcount;
1250 
1251  rank = hypothetical_rank_common(fcinfo, -1, &rowcount);
1252 
1253  PG_RETURN_INT64(rank);
1254 }

References hypothetical_rank_common(), and PG_RETURN_INT64.

◆ interval_lerp()

static Datum interval_lerp ( Datum  lo,
Datum  hi,
double  pct 
)
static

Definition at line 514 of file orderedsetaggs.c.

515 {
516  Datum diff_result = DirectFunctionCall2(interval_mi, hi, lo);
518  diff_result,
519  Float8GetDatumFast(pct));
520 
521  return DirectFunctionCall2(interval_pl, mul_result, lo);
522 }
Datum interval_mi(PG_FUNCTION_ARGS)
Definition: timestamp.c:3209
Datum interval_pl(PG_FUNCTION_ARGS)
Definition: timestamp.c:3175
Datum interval_mul(PG_FUNCTION_ARGS)
Definition: timestamp.c:3249
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:644
#define Float8GetDatumFast(X)
Definition: postgres.h:904

References DirectFunctionCall2, Float8GetDatumFast, interval_mi(), interval_mul(), and interval_pl().

Referenced by percentile_cont_interval_final(), and percentile_cont_interval_multi_final().

◆ mode_final()

Datum mode_final ( PG_FUNCTION_ARGS  )

Definition at line 1035 of file orderedsetaggs.c.

1036 {
1037  OSAPerGroupState *osastate;
1038  Datum val;
1039  bool isnull;
1040  Datum mode_val = (Datum) 0;
1041  int64 mode_freq = 0;
1042  Datum last_val = (Datum) 0;
1043  int64 last_val_freq = 0;
1044  bool last_val_is_mode = false;
1045  FmgrInfo *equalfn;
1046  Datum abbrev_val = (Datum) 0;
1047  Datum last_abbrev_val = (Datum) 0;
1048  bool shouldfree;
1049 
1051 
1052  /* If there were no regular rows, the result is NULL */
1053  if (PG_ARGISNULL(0))
1054  PG_RETURN_NULL();
1055 
1056  osastate = (OSAPerGroupState *) PG_GETARG_POINTER(0);
1057 
1058  /* number_of_rows could be zero if we only saw NULL input values */
1059  if (osastate->number_of_rows == 0)
1060  PG_RETURN_NULL();
1061 
1062  /* Look up the equality function for the datatype, if we didn't already */
1063  equalfn = &(osastate->qstate->equalfn);
1064  if (!OidIsValid(equalfn->fn_oid))
1065  fmgr_info_cxt(get_opcode(osastate->qstate->eqOperator), equalfn,
1066  osastate->qstate->qcontext);
1067 
1068  shouldfree = !(osastate->qstate->typByVal);
1069 
1070  /* Finish the sort, or rescan if we already did */
1071  if (!osastate->sort_done)
1072  {
1073  tuplesort_performsort(osastate->sortstate);
1074  osastate->sort_done = true;
1075  }
1076  else
1077  tuplesort_rescan(osastate->sortstate);
1078 
1079  /* Scan tuples and count frequencies */
1080  while (tuplesort_getdatum(osastate->sortstate, true, true, &val, &isnull,
1081  &abbrev_val))
1082  {
1083  /* we don't expect any nulls, but ignore them if found */
1084  if (isnull)
1085  continue;
1086 
1087  if (last_val_freq == 0)
1088  {
1089  /* first nonnull value - it's the mode for now */
1090  mode_val = last_val = val;
1091  mode_freq = last_val_freq = 1;
1092  last_val_is_mode = true;
1093  last_abbrev_val = abbrev_val;
1094  }
1095  else if (abbrev_val == last_abbrev_val &&
1096  DatumGetBool(FunctionCall2Coll(equalfn, PG_GET_COLLATION(), val, last_val)))
1097  {
1098  /* value equal to previous value, count it */
1099  if (last_val_is_mode)
1100  mode_freq++; /* needn't maintain last_val_freq */
1101  else if (++last_val_freq > mode_freq)
1102  {
1103  /* last_val becomes new mode */
1104  if (shouldfree)
1105  pfree(DatumGetPointer(mode_val));
1106  mode_val = last_val;
1107  mode_freq = last_val_freq;
1108  last_val_is_mode = true;
1109  }
1110  if (shouldfree)
1112  }
1113  else
1114  {
1115  /* val should replace last_val */
1116  if (shouldfree && !last_val_is_mode)
1117  pfree(DatumGetPointer(last_val));
1118  last_val = val;
1119  /* avoid equality function calls by reusing abbreviated keys */
1120  last_abbrev_val = abbrev_val;
1121  last_val_freq = 1;
1122  last_val_is_mode = false;
1123  }
1124 
1126  }
1127 
1128  if (shouldfree && !last_val_is_mode)
1129  pfree(DatumGetPointer(last_val));
1130 
1131  if (mode_freq)
1132  PG_RETURN_DATUM(mode_val);
1133  else
1134  PG_RETURN_NULL();
1135 }
#define OidIsValid(objectId)
Definition: c.h:711
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1134
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition: fmgr.c:136
#define PG_RETURN_NULL()
Definition: fmgr.h:345
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
#define PG_GET_COLLATION()
Definition: fmgr.h:198
long val
Definition: informix.c:664
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1267
void pfree(void *pointer)
Definition: mcxt.c:1306
static bool DatumGetBool(Datum X)
Definition: postgres.h:438
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:660
Definition: fmgr.h:57
Oid fn_oid
Definition: fmgr.h:59
void tuplesort_rescan(Tuplesortstate *state)
Definition: tuplesort.c:2440
bool tuplesort_getdatum(Tuplesortstate *state, bool forward, bool copy, Datum *val, bool *isNull, Datum *abbrev)

References AGG_CONTEXT_AGGREGATE, AggCheckCallContext(), Assert(), CHECK_FOR_INTERRUPTS, DatumGetBool(), DatumGetPointer(), OSAPerQueryState::eqOperator, OSAPerQueryState::equalfn, fmgr_info_cxt(), FmgrInfo::fn_oid, FunctionCall2Coll(), get_opcode(), OSAPerGroupState::number_of_rows, OidIsValid, pfree(), PG_ARGISNULL, PG_GET_COLLATION, PG_GETARG_POINTER, PG_RETURN_DATUM, PG_RETURN_NULL, OSAPerQueryState::qcontext, OSAPerGroupState::qstate, OSAPerGroupState::sort_done, OSAPerGroupState::sortstate, tuplesort_getdatum(), tuplesort_performsort(), tuplesort_rescan(), OSAPerQueryState::typByVal, and val.

◆ ordered_set_shutdown()

static void ordered_set_shutdown ( Datum  arg)
static

Definition at line 341 of file orderedsetaggs.c.

342 {
344 
345  /* Tuplesort object might have temp files. */
346  if (osastate->sortstate)
347  tuplesort_end(osastate->sortstate);
348  osastate->sortstate = NULL;
349  /* The tupleslot probably can't be holding a pin, but let's be safe. */
350  if (osastate->qstate->tupslot)
351  ExecClearTuple(osastate->qstate->tupslot);
352 }
void * arg
void tuplesort_end(Tuplesortstate *state)
Definition: tuplesort.c:972

References arg, DatumGetPointer(), ExecClearTuple(), OSAPerGroupState::qstate, OSAPerGroupState::sortstate, tuplesort_end(), and OSAPerQueryState::tupslot.

Referenced by ordered_set_startup().

◆ ordered_set_startup()

static OSAPerGroupState* ordered_set_startup ( FunctionCallInfo  fcinfo,
bool  use_tuples 
)
static

Definition at line 115 of file orderedsetaggs.c.

116 {
117  OSAPerGroupState *osastate;
118  OSAPerQueryState *qstate;
119  MemoryContext gcontext;
120  MemoryContext oldcontext;
121  int tuplesortopt;
122 
123  /*
124  * Check we're called as aggregate (and not a window function), and get
125  * the Agg node's group-lifespan context (which might change from group to
126  * group, so we shouldn't cache it in the per-query state).
127  */
128  if (AggCheckCallContext(fcinfo, &gcontext) != AGG_CONTEXT_AGGREGATE)
129  elog(ERROR, "ordered-set aggregate called in non-aggregate context");
130 
131  /*
132  * We keep a link to the per-query state in fn_extra; if it's not there,
133  * create it, and do the per-query setup we need.
134  */
135  qstate = (OSAPerQueryState *) fcinfo->flinfo->fn_extra;
136  if (qstate == NULL)
137  {
138  Aggref *aggref;
139  MemoryContext qcontext;
140  List *sortlist;
141  int numSortCols;
142 
143  /* Get the Aggref so we can examine aggregate's arguments */
144  aggref = AggGetAggref(fcinfo);
145  if (!aggref)
146  elog(ERROR, "ordered-set aggregate called in non-aggregate context");
147  if (!AGGKIND_IS_ORDERED_SET(aggref->aggkind))
148  elog(ERROR, "ordered-set aggregate support function called for non-ordered-set aggregate");
149 
150  /*
151  * Prepare per-query structures in the fn_mcxt, which we assume is the
152  * executor's per-query context; in any case it's the right place to
153  * keep anything found via fn_extra.
154  */
155  qcontext = fcinfo->flinfo->fn_mcxt;
156  oldcontext = MemoryContextSwitchTo(qcontext);
157 
158  qstate = (OSAPerQueryState *) palloc0(sizeof(OSAPerQueryState));
159  qstate->aggref = aggref;
160  qstate->qcontext = qcontext;
161 
162  /* We need to support rescans if the trans state is shared */
163  qstate->rescan_needed = AggStateIsShared(fcinfo);
164 
165  /* Extract the sort information */
166  sortlist = aggref->aggorder;
167  numSortCols = list_length(sortlist);
168 
169  if (use_tuples)
170  {
171  bool ishypothetical = (aggref->aggkind == AGGKIND_HYPOTHETICAL);
172  ListCell *lc;
173  int i;
174 
175  if (ishypothetical)
176  numSortCols++; /* make space for flag column */
177  qstate->numSortCols = numSortCols;
178  qstate->sortColIdx = (AttrNumber *) palloc(numSortCols * sizeof(AttrNumber));
179  qstate->sortOperators = (Oid *) palloc(numSortCols * sizeof(Oid));
180  qstate->eqOperators = (Oid *) palloc(numSortCols * sizeof(Oid));
181  qstate->sortCollations = (Oid *) palloc(numSortCols * sizeof(Oid));
182  qstate->sortNullsFirsts = (bool *) palloc(numSortCols * sizeof(bool));
183 
184  i = 0;
185  foreach(lc, sortlist)
186  {
187  SortGroupClause *sortcl = (SortGroupClause *) lfirst(lc);
188  TargetEntry *tle = get_sortgroupclause_tle(sortcl,
189  aggref->args);
190 
191  /* the parser should have made sure of this */
192  Assert(OidIsValid(sortcl->sortop));
193 
194  qstate->sortColIdx[i] = tle->resno;
195  qstate->sortOperators[i] = sortcl->sortop;
196  qstate->eqOperators[i] = sortcl->eqop;
197  qstate->sortCollations[i] = exprCollation((Node *) tle->expr);
198  qstate->sortNullsFirsts[i] = sortcl->nulls_first;
199  i++;
200  }
201 
202  if (ishypothetical)
203  {
204  /* Add an integer flag column as the last sort column */
205  qstate->sortColIdx[i] = list_length(aggref->args) + 1;
206  qstate->sortOperators[i] = Int4LessOperator;
207  qstate->eqOperators[i] = Int4EqualOperator;
208  qstate->sortCollations[i] = InvalidOid;
209  qstate->sortNullsFirsts[i] = false;
210  i++;
211  }
212 
213  Assert(i == numSortCols);
214 
215  /*
216  * Get a tupledesc corresponding to the aggregated inputs
217  * (including sort expressions) of the agg.
218  */
219  qstate->tupdesc = ExecTypeFromTL(aggref->args);
220 
221  /* If we need a flag column, hack the tupledesc to include that */
222  if (ishypothetical)
223  {
224  TupleDesc newdesc;
225  int natts = qstate->tupdesc->natts;
226 
227  newdesc = CreateTemplateTupleDesc(natts + 1);
228  for (i = 1; i <= natts; i++)
229  TupleDescCopyEntry(newdesc, i, qstate->tupdesc, i);
230 
231  TupleDescInitEntry(newdesc,
232  (AttrNumber) ++natts,
233  "flag",
234  INT4OID,
235  -1,
236  0);
237 
238  FreeTupleDesc(qstate->tupdesc);
239  qstate->tupdesc = newdesc;
240  }
241 
242  /* Create slot we'll use to store/retrieve rows */
243  qstate->tupslot = MakeSingleTupleTableSlot(qstate->tupdesc,
245  }
246  else
247  {
248  /* Sort single datums */
249  SortGroupClause *sortcl;
250  TargetEntry *tle;
251 
252  if (numSortCols != 1 || aggref->aggkind == AGGKIND_HYPOTHETICAL)
253  elog(ERROR, "ordered-set aggregate support function does not support multiple aggregated columns");
254 
255  sortcl = (SortGroupClause *) linitial(sortlist);
256  tle = get_sortgroupclause_tle(sortcl, aggref->args);
257 
258  /* the parser should have made sure of this */
259  Assert(OidIsValid(sortcl->sortop));
260 
261  /* Save sort ordering info */
262  qstate->sortColType = exprType((Node *) tle->expr);
263  qstate->sortOperator = sortcl->sortop;
264  qstate->eqOperator = sortcl->eqop;
265  qstate->sortCollation = exprCollation((Node *) tle->expr);
266  qstate->sortNullsFirst = sortcl->nulls_first;
267 
268  /* Save datatype info */
270  &qstate->typLen,
271  &qstate->typByVal,
272  &qstate->typAlign);
273  }
274 
275  fcinfo->flinfo->fn_extra = (void *) qstate;
276 
277  MemoryContextSwitchTo(oldcontext);
278  }
279 
280  /* Now build the stuff we need in group-lifespan context */
281  oldcontext = MemoryContextSwitchTo(gcontext);
282 
283  osastate = (OSAPerGroupState *) palloc(sizeof(OSAPerGroupState));
284  osastate->qstate = qstate;
285  osastate->gcontext = gcontext;
286 
287  tuplesortopt = TUPLESORT_NONE;
288 
289  if (qstate->rescan_needed)
290  tuplesortopt |= TUPLESORT_RANDOMACCESS;
291 
292  /*
293  * Initialize tuplesort object.
294  */
295  if (use_tuples)
296  osastate->sortstate = tuplesort_begin_heap(qstate->tupdesc,
297  qstate->numSortCols,
298  qstate->sortColIdx,
299  qstate->sortOperators,
300  qstate->sortCollations,
301  qstate->sortNullsFirsts,
302  work_mem,
303  NULL,
304  tuplesortopt);
305  else
306  osastate->sortstate = tuplesort_begin_datum(qstate->sortColType,
307  qstate->sortOperator,
308  qstate->sortCollation,
309  qstate->sortNullsFirst,
310  work_mem,
311  NULL,
312  tuplesortopt);
313 
314  osastate->number_of_rows = 0;
315  osastate->sort_done = false;
316 
317  /* Now register a shutdown callback to clean things up at end of group */
318  AggRegisterCallback(fcinfo,
320  PointerGetDatum(osastate));
321 
322  MemoryContextSwitchTo(oldcontext);
323 
324  return osastate;
325 }
TupleDesc ExecTypeFromTL(List *targetList)
Definition: execTuples.c:1938
int work_mem
Definition: globals.c:125
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
Definition: lsyscache.c:2229
void * palloc0(Size size)
Definition: mcxt.c:1230
void * palloc(Size size)
Definition: mcxt.c:1199
void AggRegisterCallback(FunctionCallInfo fcinfo, ExprContextCallbackFunction func, Datum arg)
Definition: nodeAgg.c:4652
bool AggStateIsShared(FunctionCallInfo fcinfo)
Definition: nodeAgg.c:4613
Aggref * AggGetAggref(FunctionCallInfo fcinfo)
Definition: nodeAgg.c:4553
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:43
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:764
static void ordered_set_shutdown(Datum arg)
#define lfirst(lc)
Definition: pg_list.h:170
static int list_length(const List *l)
Definition: pg_list.h:150
#define linitial(l)
Definition: pg_list.h:176
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:670
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31
char aggkind
Definition: primnodes.h:418
List * args
Definition: primnodes.h:397
List * aggorder
Definition: primnodes.h:400
void * fn_extra
Definition: fmgr.h:64
MemoryContext fn_mcxt
Definition: fmgr.h:65
Definition: pg_list.h:52
Definition: nodes.h:112
MemoryContext gcontext
Expr * expr
Definition: primnodes.h:1555
AttrNumber resno
Definition: primnodes.h:1556
TargetEntry * get_sortgroupclause_tle(SortGroupClause *sgClause, List *targetList)
Definition: tlist.c:367
void FreeTupleDesc(TupleDesc tupdesc)
Definition: tupdesc.c:309
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:45
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:583
void TupleDescCopyEntry(TupleDesc dst, AttrNumber dstAttno, TupleDesc src, AttrNumber srcAttno)
Definition: tupdesc.c:267
#define TUPLESORT_NONE
Definition: tuplesort.h:92
#define TUPLESORT_RANDOMACCESS
Definition: tuplesort.h:95
Tuplesortstate * tuplesort_begin_datum(Oid datumType, Oid sortOperator, Oid sortCollation, bool nullsFirstFlag, int workMem, SortCoordinate coordinate, int sortopt)
Tuplesortstate * tuplesort_begin_heap(TupleDesc tupDesc, int nkeys, AttrNumber *attNums, Oid *sortOperators, Oid *sortCollations, bool *nullsFirstFlags, int workMem, SortCoordinate coordinate, int sortopt)

References AGG_CONTEXT_AGGREGATE, AggCheckCallContext(), AggGetAggref(), Aggref::aggkind, Aggref::aggorder, OSAPerQueryState::aggref, AggRegisterCallback(), AggStateIsShared(), Aggref::args, Assert(), CreateTemplateTupleDesc(), elog(), SortGroupClause::eqop, OSAPerQueryState::eqOperator, OSAPerQueryState::eqOperators, ERROR, ExecTypeFromTL(), TargetEntry::expr, exprCollation(), exprType(), FunctionCallInfoBaseData::flinfo, FmgrInfo::fn_extra, FmgrInfo::fn_mcxt, FreeTupleDesc(), OSAPerGroupState::gcontext, get_sortgroupclause_tle(), get_typlenbyvalalign(), i, if(), InvalidOid, lfirst, linitial, list_length(), MakeSingleTupleTableSlot(), MemoryContextSwitchTo(), TupleDescData::natts, SortGroupClause::nulls_first, OSAPerGroupState::number_of_rows, OSAPerQueryState::numSortCols, OidIsValid, ordered_set_shutdown(), palloc(), palloc0(), PointerGetDatum(), OSAPerQueryState::qcontext, OSAPerGroupState::qstate, OSAPerQueryState::rescan_needed, TargetEntry::resno, OSAPerGroupState::sort_done, OSAPerQueryState::sortColIdx, OSAPerQueryState::sortCollation, OSAPerQueryState::sortCollations, OSAPerQueryState::sortColType, OSAPerQueryState::sortNullsFirst, OSAPerQueryState::sortNullsFirsts, SortGroupClause::sortop, OSAPerQueryState::sortOperator, OSAPerQueryState::sortOperators, OSAPerGroupState::sortstate, TTSOpsMinimalTuple, OSAPerQueryState::tupdesc, TupleDescCopyEntry(), TupleDescInitEntry(), tuplesort_begin_datum(), tuplesort_begin_heap(), TUPLESORT_NONE, TUPLESORT_RANDOMACCESS, OSAPerQueryState::tupslot, OSAPerQueryState::typAlign, OSAPerQueryState::typByVal, OSAPerQueryState::typLen, and work_mem.

Referenced by ordered_set_transition(), and ordered_set_transition_multi().

◆ ordered_set_transition()

Datum ordered_set_transition ( PG_FUNCTION_ARGS  )

Definition at line 360 of file orderedsetaggs.c.

361 {
362  OSAPerGroupState *osastate;
363 
364  /* If first call, create the transition state workspace */
365  if (PG_ARGISNULL(0))
366  osastate = ordered_set_startup(fcinfo, false);
367  else
368  osastate = (OSAPerGroupState *) PG_GETARG_POINTER(0);
369 
370  /* Load the datum into the tuplesort object, but only if it's not null */
371  if (!PG_ARGISNULL(1))
372  {
373  tuplesort_putdatum(osastate->sortstate, PG_GETARG_DATUM(1), false);
374  osastate->number_of_rows++;
375  }
376 
377  PG_RETURN_POINTER(osastate);
378 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
static OSAPerGroupState * ordered_set_startup(FunctionCallInfo fcinfo, bool use_tuples)
void tuplesort_putdatum(Tuplesortstate *state, Datum val, bool isNull)

References OSAPerGroupState::number_of_rows, ordered_set_startup(), PG_ARGISNULL, PG_GETARG_DATUM, PG_GETARG_POINTER, PG_RETURN_POINTER, OSAPerGroupState::sortstate, and tuplesort_putdatum().

◆ ordered_set_transition_multi()

Datum ordered_set_transition_multi ( PG_FUNCTION_ARGS  )

Definition at line 385 of file orderedsetaggs.c.

386 {
387  OSAPerGroupState *osastate;
388  TupleTableSlot *slot;
389  int nargs;
390  int i;
391 
392  /* If first call, create the transition state workspace */
393  if (PG_ARGISNULL(0))
394  osastate = ordered_set_startup(fcinfo, true);
395  else
396  osastate = (OSAPerGroupState *) PG_GETARG_POINTER(0);
397 
398  /* Form a tuple from all the other inputs besides the transition value */
399  slot = osastate->qstate->tupslot;
400  ExecClearTuple(slot);
401  nargs = PG_NARGS() - 1;
402  for (i = 0; i < nargs; i++)
403  {
404  slot->tts_values[i] = PG_GETARG_DATUM(i + 1);
405  slot->tts_isnull[i] = PG_ARGISNULL(i + 1);
406  }
407  if (osastate->qstate->aggref->aggkind == AGGKIND_HYPOTHETICAL)
408  {
409  /* Add a zero flag value to mark this row as a normal input row */
410  slot->tts_values[i] = Int32GetDatum(0);
411  slot->tts_isnull[i] = false;
412  i++;
413  }
414  Assert(i == slot->tts_tupleDescriptor->natts);
415  ExecStoreVirtualTuple(slot);
416 
417  /* Load the row into the tuplesort object */
418  tuplesort_puttupleslot(osastate->sortstate, slot);
419  osastate->number_of_rows++;
420 
421  PG_RETURN_POINTER(osastate);
422 }
TupleDesc tts_tupleDescriptor
Definition: tuptable.h:124

References Aggref::aggkind, OSAPerQueryState::aggref, Assert(), ExecClearTuple(), ExecStoreVirtualTuple(), i, Int32GetDatum(), TupleDescData::natts, OSAPerGroupState::number_of_rows, ordered_set_startup(), PG_ARGISNULL, PG_GETARG_DATUM, PG_GETARG_POINTER, PG_NARGS, PG_RETURN_POINTER, OSAPerGroupState::qstate, OSAPerGroupState::sortstate, TupleTableSlot::tts_isnull, TupleTableSlot::tts_tupleDescriptor, TupleTableSlot::tts_values, tuplesort_puttupleslot(), and OSAPerQueryState::tupslot.

◆ pct_info_cmp()

static int pct_info_cmp ( const void *  pa,
const void *  pb 
)
static

Definition at line 648 of file orderedsetaggs.c.

649 {
650  const struct pct_info *a = (const struct pct_info *) pa;
651  const struct pct_info *b = (const struct pct_info *) pb;
652 
653  if (a->first_row != b->first_row)
654  return (a->first_row < b->first_row) ? -1 : 1;
655  if (a->second_row != b->second_row)
656  return (a->second_row < b->second_row) ? -1 : 1;
657  return 0;
658 }
int b
Definition: isn.c:70
int a
Definition: isn.c:69

References a, and b.

Referenced by setup_pct_info().

◆ percentile_cont_final_common()

static Datum percentile_cont_final_common ( FunctionCallInfo  fcinfo,
Oid  expect_type,
LerpFunc  lerpfunc 
)
static

Definition at line 528 of file orderedsetaggs.c.

531 {
532  OSAPerGroupState *osastate;
533  double percentile;
534  int64 first_row = 0;
535  int64 second_row = 0;
536  Datum val;
537  Datum first_val;
538  Datum second_val;
539  double proportion;
540  bool isnull;
541 
543 
544  /* Get and check the percentile argument */
545  if (PG_ARGISNULL(1))
546  PG_RETURN_NULL();
547 
548  percentile = PG_GETARG_FLOAT8(1);
549 
550  if (percentile < 0 || percentile > 1 || isnan(percentile))
551  ereport(ERROR,
552  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
553  errmsg("percentile value %g is not between 0 and 1",
554  percentile)));
555 
556  /* If there were no regular rows, the result is NULL */
557  if (PG_ARGISNULL(0))
558  PG_RETURN_NULL();
559 
560  osastate = (OSAPerGroupState *) PG_GETARG_POINTER(0);
561 
562  /* number_of_rows could be zero if we only saw NULL input values */
563  if (osastate->number_of_rows == 0)
564  PG_RETURN_NULL();
565 
566  Assert(expect_type == osastate->qstate->sortColType);
567 
568  /* Finish the sort, or rescan if we already did */
569  if (!osastate->sort_done)
570  {
571  tuplesort_performsort(osastate->sortstate);
572  osastate->sort_done = true;
573  }
574  else
575  tuplesort_rescan(osastate->sortstate);
576 
577  first_row = floor(percentile * (osastate->number_of_rows - 1));
578  second_row = ceil(percentile * (osastate->number_of_rows - 1));
579 
580  Assert(first_row < osastate->number_of_rows);
581 
582  if (!tuplesort_skiptuples(osastate->sortstate, first_row, true))
583  elog(ERROR, "missing row in percentile_cont");
584 
585  if (!tuplesort_getdatum(osastate->sortstate, true, true, &first_val,
586  &isnull, NULL))
587  elog(ERROR, "missing row in percentile_cont");
588  if (isnull)
589  PG_RETURN_NULL();
590 
591  if (first_row == second_row)
592  {
593  val = first_val;
594  }
595  else
596  {
597  if (!tuplesort_getdatum(osastate->sortstate, true, true, &second_val,
598  &isnull, NULL))
599  elog(ERROR, "missing row in percentile_cont");
600 
601  if (isnull)
602  PG_RETURN_NULL();
603 
604  proportion = (percentile * (osastate->number_of_rows - 1)) - first_row;
605  val = lerpfunc(first_val, second_val, proportion);
606  }
607 
609 }
int errcode(int sqlerrcode)
Definition: elog.c:695
int errmsg(const char *fmt,...)
Definition: elog.c:906
#define ereport(elevel,...)
Definition: elog.h:145
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:282
bool tuplesort_skiptuples(Tuplesortstate *state, int64 ntuples, bool forward)
Definition: tuplesort.c:1736

References AGG_CONTEXT_AGGREGATE, AggCheckCallContext(), Assert(), elog(), ereport, errcode(), errmsg(), ERROR, OSAPerGroupState::number_of_rows, PG_ARGISNULL, PG_GETARG_FLOAT8, PG_GETARG_POINTER, PG_RETURN_DATUM, PG_RETURN_NULL, OSAPerGroupState::qstate, OSAPerGroupState::sort_done, OSAPerQueryState::sortColType, OSAPerGroupState::sortstate, tuplesort_getdatum(), tuplesort_performsort(), tuplesort_rescan(), tuplesort_skiptuples(), and val.

Referenced by percentile_cont_float8_final(), and percentile_cont_interval_final().

◆ percentile_cont_float8_final()

Datum percentile_cont_float8_final ( PG_FUNCTION_ARGS  )

Definition at line 615 of file orderedsetaggs.c.

616 {
617  return percentile_cont_final_common(fcinfo, FLOAT8OID, float8_lerp);
618 }
static Datum float8_lerp(Datum lo, Datum hi, double pct)
static Datum percentile_cont_final_common(FunctionCallInfo fcinfo, Oid expect_type, LerpFunc lerpfunc)

References float8_lerp(), and percentile_cont_final_common().

◆ percentile_cont_float8_multi_final()

Datum percentile_cont_float8_multi_final ( PG_FUNCTION_ARGS  )

Definition at line 1006 of file orderedsetaggs.c.

1007 {
1008  return percentile_cont_multi_final_common(fcinfo,
1009  FLOAT8OID,
1010  /* hard-wired info on type float8 */
1011  sizeof(float8),
1013  TYPALIGN_DOUBLE,
1014  float8_lerp);
1015 }
double float8
Definition: c.h:566
#define FLOAT8PASSBYVAL
Definition: c.h:571
static Datum percentile_cont_multi_final_common(FunctionCallInfo fcinfo, Oid expect_type, int16 typLen, bool typByVal, char typAlign, LerpFunc lerpfunc)

References float8_lerp(), FLOAT8PASSBYVAL, and percentile_cont_multi_final_common().

◆ percentile_cont_interval_final()

Datum percentile_cont_interval_final ( PG_FUNCTION_ARGS  )

Definition at line 624 of file orderedsetaggs.c.

625 {
626  return percentile_cont_final_common(fcinfo, INTERVALOID, interval_lerp);
627 }
static Datum interval_lerp(Datum lo, Datum hi, double pct)

References interval_lerp(), and percentile_cont_final_common().

◆ percentile_cont_interval_multi_final()

Datum percentile_cont_interval_multi_final ( PG_FUNCTION_ARGS  )

Definition at line 1021 of file orderedsetaggs.c.

1022 {
1023  return percentile_cont_multi_final_common(fcinfo,
1024  INTERVALOID,
1025  /* hard-wired info on type interval */
1026  16, false, TYPALIGN_DOUBLE,
1027  interval_lerp);
1028 }

References interval_lerp(), and percentile_cont_multi_final_common().

◆ percentile_cont_multi_final_common()

static Datum percentile_cont_multi_final_common ( FunctionCallInfo  fcinfo,
Oid  expect_type,
int16  typLen,
bool  typByVal,
char  typAlign,
LerpFunc  lerpfunc 
)
static

Definition at line 850 of file orderedsetaggs.c.

854 {
855  OSAPerGroupState *osastate;
856  ArrayType *param;
857  Datum *percentiles_datum;
858  bool *percentiles_null;
859  int num_percentiles;
860  struct pct_info *pct_info;
861  Datum *result_datum;
862  bool *result_isnull;
863  int64 rownum = 0;
864  Datum first_val = (Datum) 0;
865  Datum second_val = (Datum) 0;
866  bool isnull;
867  int i;
868 
870 
871  /* If there were no regular rows, the result is NULL */
872  if (PG_ARGISNULL(0))
873  PG_RETURN_NULL();
874 
875  osastate = (OSAPerGroupState *) PG_GETARG_POINTER(0);
876 
877  /* number_of_rows could be zero if we only saw NULL input values */
878  if (osastate->number_of_rows == 0)
879  PG_RETURN_NULL();
880 
881  Assert(expect_type == osastate->qstate->sortColType);
882 
883  /* Deconstruct the percentile-array input */
884  if (PG_ARGISNULL(1))
885  PG_RETURN_NULL();
886  param = PG_GETARG_ARRAYTYPE_P(1);
887 
888  deconstruct_array_builtin(param, FLOAT8OID,
889  &percentiles_datum,
890  &percentiles_null,
891  &num_percentiles);
892 
893  if (num_percentiles == 0)
895 
896  pct_info = setup_pct_info(num_percentiles,
897  percentiles_datum,
898  percentiles_null,
899  osastate->number_of_rows,
900  true);
901 
902  result_datum = (Datum *) palloc(num_percentiles * sizeof(Datum));
903  result_isnull = (bool *) palloc(num_percentiles * sizeof(bool));
904 
905  /*
906  * Start by dealing with any nulls in the param array - those are sorted
907  * to the front on row=0, so set the corresponding result indexes to null
908  */
909  for (i = 0; i < num_percentiles; i++)
910  {
911  int idx = pct_info[i].idx;
912 
913  if (pct_info[i].first_row > 0)
914  break;
915 
916  result_datum[idx] = (Datum) 0;
917  result_isnull[idx] = true;
918  }
919 
920  /*
921  * If there's anything left after doing the nulls, then grind the input
922  * and extract the needed values
923  */
924  if (i < num_percentiles)
925  {
926  /* Finish the sort, or rescan if we already did */
927  if (!osastate->sort_done)
928  {
929  tuplesort_performsort(osastate->sortstate);
930  osastate->sort_done = true;
931  }
932  else
933  tuplesort_rescan(osastate->sortstate);
934 
935  for (; i < num_percentiles; i++)
936  {
937  int64 first_row = pct_info[i].first_row;
938  int64 second_row = pct_info[i].second_row;
939  int idx = pct_info[i].idx;
940 
941  /*
942  * Advance to first_row, if not already there. Note that we might
943  * already have rownum beyond first_row, in which case first_val
944  * is already correct. (This occurs when interpolating between
945  * the same two input rows as for the previous percentile.)
946  */
947  if (first_row > rownum)
948  {
949  if (!tuplesort_skiptuples(osastate->sortstate, first_row - rownum - 1, true))
950  elog(ERROR, "missing row in percentile_cont");
951 
952  if (!tuplesort_getdatum(osastate->sortstate, true, true,
953  &first_val, &isnull, NULL) || isnull)
954  elog(ERROR, "missing row in percentile_cont");
955 
956  rownum = first_row;
957  /* Always advance second_val to be latest input value */
958  second_val = first_val;
959  }
960  else if (first_row == rownum)
961  {
962  /*
963  * We are already at the desired row, so we must previously
964  * have read its value into second_val (and perhaps first_val
965  * as well, but this assignment is harmless in that case).
966  */
967  first_val = second_val;
968  }
969 
970  /* Fetch second_row if needed */
971  if (second_row > rownum)
972  {
973  if (!tuplesort_getdatum(osastate->sortstate, true, true,
974  &second_val, &isnull, NULL) || isnull)
975  elog(ERROR, "missing row in percentile_cont");
976  rownum++;
977  }
978  /* We should now certainly be on second_row exactly */
979  Assert(second_row == rownum);
980 
981  /* Compute appropriate result */
982  if (second_row > first_row)
983  result_datum[idx] = lerpfunc(first_val, second_val,
985  else
986  result_datum[idx] = first_val;
987 
988  result_isnull[idx] = false;
989  }
990  }
991 
992  /* We make the output array the same shape as the input */
993  PG_RETURN_POINTER(construct_md_array(result_datum, result_isnull,
994  ARR_NDIM(param),
995  ARR_DIMS(param), ARR_LBOUND(param),
996  expect_type,
997  typLen,
998  typByVal,
999  typAlign));
1000 }
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:259
#define ARR_NDIM(a)
Definition: array.h:283
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:256
#define ARR_DIMS(a)
Definition: array.h:287
#define ARR_LBOUND(a)
Definition: array.h:289
ArrayType * construct_empty_array(Oid elmtype)
Definition: arrayfuncs.c:3525
void deconstruct_array_builtin(ArrayType *array, Oid elmtype, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3642
ArrayType * construct_md_array(Datum *elems, bool *nulls, int ndims, int *dims, int *lbs, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3439
static struct pct_info * setup_pct_info(int num_percentiles, Datum *percentiles_datum, bool *percentiles_null, int64 rowcount, bool continuous)
int64 first_row
int64 second_row
double proportion

References AGG_CONTEXT_AGGREGATE, AggCheckCallContext(), ARR_DIMS, ARR_LBOUND, ARR_NDIM, Assert(), construct_empty_array(), construct_md_array(), deconstruct_array_builtin(), elog(), ERROR, pct_info::first_row, i, idx(), pct_info::idx, OSAPerGroupState::number_of_rows, palloc(), PG_ARGISNULL, PG_GETARG_ARRAYTYPE_P, PG_GETARG_POINTER, PG_RETURN_NULL, PG_RETURN_POINTER, pct_info::proportion, OSAPerGroupState::qstate, pct_info::second_row, setup_pct_info(), OSAPerGroupState::sort_done, OSAPerQueryState::sortColType, OSAPerGroupState::sortstate, tuplesort_getdatum(), tuplesort_performsort(), tuplesort_rescan(), and tuplesort_skiptuples().

Referenced by percentile_cont_float8_multi_final(), and percentile_cont_interval_multi_final().

◆ percentile_disc_final()

Datum percentile_disc_final ( PG_FUNCTION_ARGS  )

Definition at line 429 of file orderedsetaggs.c.

430 {
431  OSAPerGroupState *osastate;
432  double percentile;
433  Datum val;
434  bool isnull;
435  int64 rownum;
436 
438 
439  /* Get and check the percentile argument */
440  if (PG_ARGISNULL(1))
441  PG_RETURN_NULL();
442 
443  percentile = PG_GETARG_FLOAT8(1);
444 
445  if (percentile < 0 || percentile > 1 || isnan(percentile))
446  ereport(ERROR,
447  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
448  errmsg("percentile value %g is not between 0 and 1",
449  percentile)));
450 
451  /* If there were no regular rows, the result is NULL */
452  if (PG_ARGISNULL(0))
453  PG_RETURN_NULL();
454 
455  osastate = (OSAPerGroupState *) PG_GETARG_POINTER(0);
456 
457  /* number_of_rows could be zero if we only saw NULL input values */
458  if (osastate->number_of_rows == 0)
459  PG_RETURN_NULL();
460 
461  /* Finish the sort, or rescan if we already did */
462  if (!osastate->sort_done)
463  {
464  tuplesort_performsort(osastate->sortstate);
465  osastate->sort_done = true;
466  }
467  else
468  tuplesort_rescan(osastate->sortstate);
469 
470  /*----------
471  * We need the smallest K such that (K/N) >= percentile.
472  * N>0, therefore K >= N*percentile, therefore K = ceil(N*percentile).
473  * So we skip K-1 rows (if K>0) and return the next row fetched.
474  *----------
475  */
476  rownum = (int64) ceil(percentile * osastate->number_of_rows);
477  Assert(rownum <= osastate->number_of_rows);
478 
479  if (rownum > 1)
480  {
481  if (!tuplesort_skiptuples(osastate->sortstate, rownum - 1, true))
482  elog(ERROR, "missing row in percentile_disc");
483  }
484 
485  if (!tuplesort_getdatum(osastate->sortstate, true, true, &val, &isnull,
486  NULL))
487  elog(ERROR, "missing row in percentile_disc");
488 
489  /* We shouldn't have stored any nulls, but do the right thing anyway */
490  if (isnull)
491  PG_RETURN_NULL();
492  else
494 }

References AGG_CONTEXT_AGGREGATE, AggCheckCallContext(), Assert(), elog(), ereport, errcode(), errmsg(), ERROR, OSAPerGroupState::number_of_rows, PG_ARGISNULL, PG_GETARG_FLOAT8, PG_GETARG_POINTER, PG_RETURN_DATUM, PG_RETURN_NULL, OSAPerGroupState::sort_done, OSAPerGroupState::sortstate, tuplesort_getdatum(), tuplesort_performsort(), tuplesort_rescan(), tuplesort_skiptuples(), and val.

◆ percentile_disc_multi_final()

Datum percentile_disc_multi_final ( PG_FUNCTION_ARGS  )

Definition at line 733 of file orderedsetaggs.c.

734 {
735  OSAPerGroupState *osastate;
736  ArrayType *param;
737  Datum *percentiles_datum;
738  bool *percentiles_null;
739  int num_percentiles;
740  struct pct_info *pct_info;
741  Datum *result_datum;
742  bool *result_isnull;
743  int64 rownum = 0;
744  Datum val = (Datum) 0;
745  bool isnull = true;
746  int i;
747 
749 
750  /* If there were no regular rows, the result is NULL */
751  if (PG_ARGISNULL(0))
752  PG_RETURN_NULL();
753 
754  osastate = (OSAPerGroupState *) PG_GETARG_POINTER(0);
755 
756  /* number_of_rows could be zero if we only saw NULL input values */
757  if (osastate->number_of_rows == 0)
758  PG_RETURN_NULL();
759 
760  /* Deconstruct the percentile-array input */
761  if (PG_ARGISNULL(1))
762  PG_RETURN_NULL();
763  param = PG_GETARG_ARRAYTYPE_P(1);
764 
765  deconstruct_array_builtin(param, FLOAT8OID,
766  &percentiles_datum,
767  &percentiles_null,
768  &num_percentiles);
769 
770  if (num_percentiles == 0)
772 
773  pct_info = setup_pct_info(num_percentiles,
774  percentiles_datum,
775  percentiles_null,
776  osastate->number_of_rows,
777  false);
778 
779  result_datum = (Datum *) palloc(num_percentiles * sizeof(Datum));
780  result_isnull = (bool *) palloc(num_percentiles * sizeof(bool));
781 
782  /*
783  * Start by dealing with any nulls in the param array - those are sorted
784  * to the front on row=0, so set the corresponding result indexes to null
785  */
786  for (i = 0; i < num_percentiles; i++)
787  {
788  int idx = pct_info[i].idx;
789 
790  if (pct_info[i].first_row > 0)
791  break;
792 
793  result_datum[idx] = (Datum) 0;
794  result_isnull[idx] = true;
795  }
796 
797  /*
798  * If there's anything left after doing the nulls, then grind the input
799  * and extract the needed values
800  */
801  if (i < num_percentiles)
802  {
803  /* Finish the sort, or rescan if we already did */
804  if (!osastate->sort_done)
805  {
806  tuplesort_performsort(osastate->sortstate);
807  osastate->sort_done = true;
808  }
809  else
810  tuplesort_rescan(osastate->sortstate);
811 
812  for (; i < num_percentiles; i++)
813  {
814  int64 target_row = pct_info[i].first_row;
815  int idx = pct_info[i].idx;
816 
817  /* Advance to target row, if not already there */
818  if (target_row > rownum)
819  {
820  if (!tuplesort_skiptuples(osastate->sortstate, target_row - rownum - 1, true))
821  elog(ERROR, "missing row in percentile_disc");
822 
823  if (!tuplesort_getdatum(osastate->sortstate, true, true, &val,
824  &isnull, NULL))
825  elog(ERROR, "missing row in percentile_disc");
826 
827  rownum = target_row;
828  }
829 
830  result_datum[idx] = val;
831  result_isnull[idx] = isnull;
832  }
833  }
834 
835  /* We make the output array the same shape as the input */
836  PG_RETURN_POINTER(construct_md_array(result_datum, result_isnull,
837  ARR_NDIM(param),
838  ARR_DIMS(param),
839  ARR_LBOUND(param),
840  osastate->qstate->sortColType,
841  osastate->qstate->typLen,
842  osastate->qstate->typByVal,
843  osastate->qstate->typAlign));
844 }

References AGG_CONTEXT_AGGREGATE, AggCheckCallContext(), ARR_DIMS, ARR_LBOUND, ARR_NDIM, Assert(), construct_empty_array(), construct_md_array(), deconstruct_array_builtin(), elog(), ERROR, pct_info::first_row, i, idx(), pct_info::idx, OSAPerGroupState::number_of_rows, palloc(), PG_ARGISNULL, PG_GETARG_ARRAYTYPE_P, PG_GETARG_POINTER, PG_RETURN_NULL, PG_RETURN_POINTER, OSAPerGroupState::qstate, setup_pct_info(), OSAPerGroupState::sort_done, OSAPerQueryState::sortColType, OSAPerGroupState::sortstate, tuplesort_getdatum(), tuplesort_performsort(), tuplesort_rescan(), tuplesort_skiptuples(), OSAPerQueryState::typAlign, OSAPerQueryState::typByVal, OSAPerQueryState::typLen, and val.

◆ setup_pct_info()

static struct pct_info* setup_pct_info ( int  num_percentiles,
Datum percentiles_datum,
bool percentiles_null,
int64  rowcount,
bool  continuous 
)
static

Definition at line 664 of file orderedsetaggs.c.

669 {
670  struct pct_info *pct_info;
671  int i;
672 
673  pct_info = (struct pct_info *) palloc(num_percentiles * sizeof(struct pct_info));
674 
675  for (i = 0; i < num_percentiles; i++)
676  {
677  pct_info[i].idx = i;
678 
679  if (percentiles_null[i])
680  {
681  /* dummy entry for any NULL in array */
682  pct_info[i].first_row = 0;
683  pct_info[i].second_row = 0;
684  pct_info[i].proportion = 0;
685  }
686  else
687  {
688  double p = DatumGetFloat8(percentiles_datum[i]);
689 
690  if (p < 0 || p > 1 || isnan(p))
691  ereport(ERROR,
692  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
693  errmsg("percentile value %g is not between 0 and 1",
694  p)));
695 
696  if (continuous)
697  {
698  pct_info[i].first_row = 1 + floor(p * (rowcount - 1));
699  pct_info[i].second_row = 1 + ceil(p * (rowcount - 1));
700  pct_info[i].proportion = (p * (rowcount - 1)) - floor(p * (rowcount - 1));
701  }
702  else
703  {
704  /*----------
705  * We need the smallest K such that (K/N) >= percentile.
706  * N>0, therefore K >= N*percentile, therefore
707  * K = ceil(N*percentile); but not less than 1.
708  *----------
709  */
710  int64 row = (int64) ceil(p * rowcount);
711 
712  row = Max(1, row);
713  pct_info[i].first_row = row;
714  pct_info[i].second_row = row;
715  pct_info[i].proportion = 0;
716  }
717  }
718  }
719 
720  /*
721  * The parameter array wasn't necessarily in sorted order, but we need to
722  * visit the rows in order, so sort by first_row/second_row.
723  */
724  qsort(pct_info, num_percentiles, sizeof(struct pct_info), pct_info_cmp);
725 
726  return pct_info;
727 }
#define Max(x, y)
Definition: c.h:931
static int pct_info_cmp(const void *pa, const void *pb)
#define qsort(a, b, c, d)
Definition: port.h:445

References DatumGetFloat8(), ereport, errcode(), errmsg(), ERROR, pct_info::first_row, i, pct_info::idx, Max, palloc(), pct_info_cmp(), pct_info::proportion, qsort, and pct_info::second_row.

Referenced by percentile_cont_multi_final_common(), and percentile_disc_multi_final().