129 elog(
ERROR,
"ordered-set aggregate called in non-aggregate context");
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");
171 bool ishypothetical = (aggref->
aggkind == AGGKIND_HYPOTHETICAL);
185 foreach(lc, sortlist)
228 for (
i = 1;
i <= natts;
i++)
252 if (numSortCols != 1 || aggref->
aggkind == AGGKIND_HYPOTHETICAL)
253 elog(
ERROR,
"ordered-set aggregate support function does not support multiple aggregated columns");
284 osastate->
qstate = qstate;
402 for (
i = 0;
i < nargs;
i++)
445 if (percentile < 0 || percentile > 1 || isnan(percentile))
447 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
448 errmsg(
"percentile value %g is not between 0 and 1",
477 Assert(rownum <= osastate->number_of_rows);
482 elog(
ERROR,
"missing row in percentile_disc");
486 elog(
ERROR,
"missing row in percentile_disc");
534 int64 second_row = 0;
549 if (percentile < 0 || percentile > 1 || isnan(percentile))
551 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
552 errmsg(
"percentile value %g is not between 0 and 1",
579 Assert(first_row < osastate->number_of_rows);
582 elog(
ERROR,
"missing row in percentile_cont");
585 elog(
ERROR,
"missing row in percentile_cont");
589 if (first_row == second_row)
596 elog(
ERROR,
"missing row in percentile_cont");
601 proportion = (percentile * (osastate->
number_of_rows - 1)) - first_row;
602 val = lerpfunc(first_val, second_val, proportion);
650 if (
a->first_row !=
b->first_row)
651 return (
a->first_row <
b->first_row) ? -1 : 1;
652 if (
a->second_row !=
b->second_row)
653 return (
a->second_row <
b->second_row) ? -1 : 1;
662 Datum *percentiles_datum,
663 bool *percentiles_null,
672 for (
i = 0;
i < num_percentiles;
i++)
676 if (percentiles_null[
i])
687 if (p < 0 || p > 1 || isnan(p))
689 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
690 errmsg(
"percentile value %g is not between 0 and 1",
707 int64 row = (int64) ceil(p * rowcount);
734 Datum *percentiles_datum;
735 bool *percentiles_null;
769 if (num_percentiles == 0)
779 result_isnull = (
bool *)
palloc(num_percentiles *
sizeof(
bool));
785 for (
i = 0;
i < num_percentiles;
i++)
793 result_isnull[
idx] =
true;
800 if (
i < num_percentiles)
811 for (;
i < num_percentiles;
i++)
817 if (target_row > rownum)
820 elog(
ERROR,
"missing row in percentile_disc");
823 elog(
ERROR,
"missing row in percentile_disc");
829 result_isnull[
idx] = isnull;
850 int16 typLen,
bool typByVal,
char typAlign,
855 Datum *percentiles_datum;
856 bool *percentiles_null;
893 if (num_percentiles == 0)
903 result_isnull = (
bool *)
palloc(num_percentiles *
sizeof(
bool));
909 for (
i = 0;
i < num_percentiles;
i++)
917 result_isnull[
idx] =
true;
924 if (
i < num_percentiles)
935 for (;
i < num_percentiles;
i++)
950 elog(
ERROR,
"missing row in percentile_cont");
953 &isnull, NULL) || isnull)
954 elog(
ERROR,
"missing row in percentile_cont");
958 second_val = first_val;
967 first_val = second_val;
974 &isnull, NULL) || isnull)
975 elog(
ERROR,
"missing row in percentile_cont");
983 result_datum[
idx] = lerpfunc(first_val, second_val,
986 result_datum[
idx] = first_val;
988 result_isnull[
idx] =
false;
1026 16,
false, TYPALIGN_DOUBLE,
1041 int64 mode_freq = 0;
1043 int64 last_val_freq = 0;
1044 bool last_val_is_mode =
false;
1086 if (last_val_freq == 0)
1089 mode_val = last_val =
val;
1090 mode_freq = last_val_freq = 1;
1091 last_val_is_mode =
true;
1092 last_abbrev_val = abbrev_val;
1094 else if (abbrev_val == last_abbrev_val &&
1098 if (last_val_is_mode)
1100 else if (++last_val_freq > mode_freq)
1105 mode_val = last_val;
1106 mode_freq = last_val_freq;
1107 last_val_is_mode =
true;
1115 if (shouldfree && !last_val_is_mode)
1119 last_abbrev_val = abbrev_val;
1121 last_val_is_mode =
false;
1127 if (shouldfree && !last_val_is_mode)
1150 (nargs + 1) != tupdesc->
natts ||
1152 elog(
ERROR,
"type mismatch in hypothetical-set function");
1155 for (
i = 0;
i < nargs;
i++)
1160 elog(
ERROR,
"type mismatch in hypothetical-set function");
1173 int64 *number_of_rows)
1186 *number_of_rows = 0;
1195 elog(
ERROR,
"wrong number of arguments in hypothetical-set function");
1206 for (
i = 0;
i < nargs;
i++)
1270 result_val = (double) (rank - 1) / (double) (rowcount);
1287 result_val = (double) (rank) / (double) (rowcount + 1);
1302 int64 duplicate_count = 0;
1304 int numDistinctCols;
1333 elog(
ERROR,
"wrong number of arguments in hypothetical-set function");
1346 if (compareTuple == NULL)
1368 for (
i = 0;
i < nargs;
i++)
1408 abbrevVal == abbrevOld &&
1416 abbrevOld = abbrevVal;
1428 rank = rank - duplicate_count;
Datum idx(PG_FUNCTION_ARGS)
#define PG_GETARG_ARRAYTYPE_P(n)
ArrayType * construct_empty_array(Oid elmtype)
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
ArrayType * construct_md_array(Datum *elems, bool *nulls, int ndims, int *dims, int *lbs, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Datum interval_mi(PG_FUNCTION_ARGS)
Datum interval_pl(PG_FUNCTION_ARGS)
Datum interval_mul(PG_FUNCTION_ARGS)
#define OidIsValid(objectId)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
ExprState * execTuplesMatchPrepare(TupleDesc desc, int numCols, const AttrNumber *keyColIdx, const Oid *eqOperators, const Oid *collations, PlanState *parent)
TupleTableSlot * ExecStoreVirtualTuple(TupleTableSlot *slot)
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
const TupleTableSlotOps TTSOpsMinimalTuple
TupleDesc ExecTypeFromTL(List *targetList)
TupleTableSlot * MakeSingleTupleTableSlot(TupleDesc tupdesc, const TupleTableSlotOps *tts_ops)
ExprContext * CreateStandaloneExprContext(void)
static bool ExecQualAndReset(ExprState *state, ExprContext *econtext)
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Datum Float8GetDatum(float8 X)
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
#define DirectFunctionCall2(func, arg1, arg2)
#define PG_GETARG_FLOAT8(n)
#define PG_RETURN_FLOAT8(x)
#define PG_GETARG_POINTER(n)
#define PG_RETURN_INT64(x)
#define PG_GETARG_DATUM(n)
#define AGG_CONTEXT_AGGREGATE
#define PG_RETURN_DATUM(x)
#define PG_RETURN_POINTER(x)
#define PG_GET_COLLATION()
Assert(fmt[strlen(fmt) - 1] !='\n')
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
RegProcedure get_opcode(Oid opno)
void pfree(void *pointer)
void * palloc0(Size size)
#define CHECK_FOR_INTERRUPTS()
void AggRegisterCallback(FunctionCallInfo fcinfo, ExprContextCallbackFunction func, Datum arg)
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
bool AggStateIsShared(FunctionCallInfo fcinfo)
Aggref * AggGetAggref(FunctionCallInfo fcinfo)
Oid exprType(const Node *expr)
Oid exprCollation(const Node *expr)
Datum percentile_cont_float8_final(PG_FUNCTION_ARGS)
Datum hypothetical_percent_rank_final(PG_FUNCTION_ARGS)
Datum percentile_disc_final(PG_FUNCTION_ARGS)
Datum hypothetical_rank_final(PG_FUNCTION_ARGS)
static Datum percentile_cont_multi_final_common(FunctionCallInfo fcinfo, Oid expect_type, int16 typLen, bool typByVal, char typAlign, LerpFunc lerpfunc)
struct OSAPerGroupState OSAPerGroupState
static Datum float8_lerp(Datum lo, Datum hi, double pct)
Datum hypothetical_cume_dist_final(PG_FUNCTION_ARGS)
static int64 hypothetical_rank_common(FunctionCallInfo fcinfo, int flag, int64 *number_of_rows)
static Datum interval_lerp(Datum lo, Datum hi, double pct)
static struct pct_info * setup_pct_info(int num_percentiles, Datum *percentiles_datum, bool *percentiles_null, int64 rowcount, bool continuous)
Datum ordered_set_transition(PG_FUNCTION_ARGS)
Datum ordered_set_transition_multi(PG_FUNCTION_ARGS)
Datum percentile_disc_multi_final(PG_FUNCTION_ARGS)
Datum hypothetical_dense_rank_final(PG_FUNCTION_ARGS)
static void ordered_set_shutdown(Datum arg)
static Datum percentile_cont_final_common(FunctionCallInfo fcinfo, Oid expect_type, LerpFunc lerpfunc)
Datum percentile_cont_float8_multi_final(PG_FUNCTION_ARGS)
Datum(* LerpFunc)(Datum lo, Datum hi, double pct)
static OSAPerGroupState * ordered_set_startup(FunctionCallInfo fcinfo, bool use_tuples)
Datum mode_final(PG_FUNCTION_ARGS)
Datum percentile_cont_interval_final(PG_FUNCTION_ARGS)
Datum percentile_cont_interval_multi_final(PG_FUNCTION_ARGS)
struct OSAPerQueryState OSAPerQueryState
static void hypothetical_check_argtypes(FunctionCallInfo fcinfo, int nargs, TupleDesc tupdesc)
static int pct_info_cmp(const void *pa, const void *pb)
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
FormData_pg_attribute * Form_pg_attribute
static int list_length(const List *l)
#define qsort(a, b, c, d)
#define Float8GetDatumFast(X)
#define DatumGetPointer(X)
#define DatumGetFloat8(X)
#define PointerGetDatum(X)
TupleTableSlot * ecxt_innertuple
TupleTableSlot * ecxt_outertuple
OSAPerQueryState * qstate
Tuplesortstate * sortstate
TupleDesc tts_tupleDescriptor
TargetEntry * get_sortgroupclause_tle(SortGroupClause *sgClause, List *targetList)
void FreeTupleDesc(TupleDesc tupdesc)
TupleDesc CreateTemplateTupleDesc(int natts)
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
void TupleDescCopyEntry(TupleDesc dst, AttrNumber dstAttno, TupleDesc src, AttrNumber srcAttno)
#define TupleDescAttr(tupdesc, i)
void tuplesort_rescan(Tuplesortstate *state)
void tuplesort_performsort(Tuplesortstate *state)
void tuplesort_putdatum(Tuplesortstate *state, Datum val, bool isNull)
bool tuplesort_skiptuples(Tuplesortstate *state, int64 ntuples, bool forward)
void tuplesort_puttupleslot(Tuplesortstate *state, TupleTableSlot *slot)
Tuplesortstate * tuplesort_begin_datum(Oid datumType, Oid sortOperator, Oid sortCollation, bool nullsFirstFlag, int workMem, SortCoordinate coordinate, int sortopt)
bool tuplesort_gettupleslot(Tuplesortstate *state, bool forward, bool copy, TupleTableSlot *slot, Datum *abbrev)
void tuplesort_end(Tuplesortstate *state)
bool tuplesort_getdatum(Tuplesortstate *state, bool forward, Datum *val, bool *isNull, Datum *abbrev)
Tuplesortstate * tuplesort_begin_heap(TupleDesc tupDesc, int nkeys, AttrNumber *attNums, Oid *sortOperators, Oid *sortCollations, bool *nullsFirstFlags, int workMem, SortCoordinate coordinate, int sortopt)
#define TUPLESORT_RANDOMACCESS
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
static Datum slot_getattr(TupleTableSlot *slot, int attnum, bool *isnull)