27 #include "utils/fmgrprotos.h"
127 elog(
ERROR,
"ordered-set aggregate called in non-aggregate context");
144 elog(
ERROR,
"ordered-set aggregate called in non-aggregate context");
145 if (!AGGKIND_IS_ORDERED_SET(aggref->aggkind))
146 elog(
ERROR,
"ordered-set aggregate support function called for non-ordered-set aggregate");
169 bool ishypothetical = (aggref->aggkind == AGGKIND_HYPOTHETICAL);
183 foreach(lc, sortlist)
226 for (
i = 1;
i <= natts;
i++)
250 if (numSortCols != 1 || aggref->aggkind == AGGKIND_HYPOTHETICAL)
251 elog(
ERROR,
"ordered-set aggregate support function does not support multiple aggregated columns");
282 osastate->
qstate = qstate;
400 for (
i = 0;
i < nargs;
i++)
405 if (osastate->
qstate->
aggref->aggkind == AGGKIND_HYPOTHETICAL)
443 if (percentile < 0 || percentile > 1 || isnan(percentile))
445 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
446 errmsg(
"percentile value %g is not between 0 and 1",
475 Assert(rownum <= osastate->number_of_rows);
480 elog(
ERROR,
"missing row in percentile_disc");
485 elog(
ERROR,
"missing row in percentile_disc");
533 int64 second_row = 0;
548 if (percentile < 0 || percentile > 1 || isnan(percentile))
550 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
551 errmsg(
"percentile value %g is not between 0 and 1",
578 Assert(first_row < osastate->number_of_rows);
581 elog(
ERROR,
"missing row in percentile_cont");
585 elog(
ERROR,
"missing row in percentile_cont");
589 if (first_row == second_row)
597 elog(
ERROR,
"missing row in percentile_cont");
602 proportion = (percentile * (osastate->
number_of_rows - 1)) - first_row;
603 val = lerpfunc(first_val, second_val, proportion);
651 if (
a->first_row !=
b->first_row)
652 return (
a->first_row <
b->first_row) ? -1 : 1;
653 if (
a->second_row !=
b->second_row)
654 return (
a->second_row <
b->second_row) ? -1 : 1;
663 Datum *percentiles_datum,
664 bool *percentiles_null,
673 for (
i = 0;
i < num_percentiles;
i++)
677 if (percentiles_null[
i])
688 if (p < 0 || p > 1 || isnan(p))
690 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
691 errmsg(
"percentile value %g is not between 0 and 1",
735 Datum *percentiles_datum;
736 bool *percentiles_null;
768 if (num_percentiles == 0)
778 result_isnull = (
bool *)
palloc(num_percentiles *
sizeof(
bool));
784 for (
i = 0;
i < num_percentiles;
i++)
792 result_isnull[
idx] =
true;
799 if (
i < num_percentiles)
810 for (;
i < num_percentiles;
i++)
816 if (target_row > rownum)
819 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;
891 if (num_percentiles == 0)
901 result_isnull = (
bool *)
palloc(num_percentiles *
sizeof(
bool));
907 for (
i = 0;
i < num_percentiles;
i++)
915 result_isnull[
idx] =
true;
922 if (
i < num_percentiles)
933 for (;
i < num_percentiles;
i++)
948 elog(
ERROR,
"missing row in percentile_cont");
951 &first_val, &isnull, NULL) || isnull)
952 elog(
ERROR,
"missing row in percentile_cont");
956 second_val = first_val;
965 first_val = second_val;
972 &second_val, &isnull, NULL) || isnull)
973 elog(
ERROR,
"missing row in percentile_cont");
981 result_datum[
idx] = lerpfunc(first_val, second_val,
984 result_datum[
idx] = first_val;
986 result_isnull[
idx] =
false;
1024 16,
false, TYPALIGN_DOUBLE,
1039 int64 mode_freq = 0;
1041 int64 last_val_freq = 0;
1042 bool last_val_is_mode =
false;
1085 if (last_val_freq == 0)
1088 mode_val = last_val =
val;
1089 mode_freq = last_val_freq = 1;
1090 last_val_is_mode =
true;
1091 last_abbrev_val = abbrev_val;
1093 else if (abbrev_val == last_abbrev_val &&
1097 if (last_val_is_mode)
1099 else if (++last_val_freq > mode_freq)
1104 mode_val = last_val;
1105 mode_freq = last_val_freq;
1106 last_val_is_mode =
true;
1114 if (shouldfree && !last_val_is_mode)
1118 last_abbrev_val = abbrev_val;
1120 last_val_is_mode =
false;
1126 if (shouldfree && !last_val_is_mode)
1149 (nargs + 1) != tupdesc->
natts ||
1151 elog(
ERROR,
"type mismatch in hypothetical-set function");
1154 for (
i = 0;
i < nargs;
i++)
1159 elog(
ERROR,
"type mismatch in hypothetical-set function");
1172 int64 *number_of_rows)
1185 *number_of_rows = 0;
1194 elog(
ERROR,
"wrong number of arguments in hypothetical-set function");
1205 for (
i = 0;
i < nargs;
i++)
1269 result_val = (double) (rank - 1) / (double) (rowcount);
1286 result_val = (double) (rank) / (double) (rowcount + 1);
1301 int64 duplicate_count = 0;
1303 int numDistinctCols;
1332 elog(
ERROR,
"wrong number of arguments in hypothetical-set function");
1345 if (compareTuple == NULL)
1367 for (
i = 0;
i < nargs;
i++)
1407 abbrevVal == abbrevOld &&
1415 abbrevOld = abbrevVal;
1427 rank = rank - duplicate_count;
Datum idx(PG_FUNCTION_ARGS)
#define PG_GETARG_ARRAYTYPE_P(n)
ArrayType * construct_empty_array(Oid elmtype)
void deconstruct_array_builtin(ArrayType *array, Oid elmtype, 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 Assert(condition)
#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()
if(TABLE==NULL||TABLE_index==NULL)
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)
FormData_pg_attribute * Form_pg_attribute
static int list_length(const List *l)
#define qsort(a, b, c, d)
static bool DatumGetBool(Datum X)
static Datum PointerGetDatum(const void *X)
#define Float8GetDatumFast(X)
static float8 DatumGetFloat8(Datum X)
static Pointer DatumGetPointer(Datum X)
static Datum Int32GetDatum(int32 X)
static int32 DatumGetInt32(Datum X)
MemoryContextSwitchTo(old_ctx)
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)
bool tuplesort_skiptuples(Tuplesortstate *state, int64 ntuples, bool forward)
void tuplesort_end(Tuplesortstate *state)
#define TUPLESORT_RANDOMACCESS
void tuplesort_putdatum(Tuplesortstate *state, Datum val, bool isNull)
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)
bool tuplesort_getdatum(Tuplesortstate *state, bool forward, bool copy, 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)
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
static Datum slot_getattr(TupleTableSlot *slot, int attnum, bool *isnull)