1 /*-------------------------------------------------------------------------
4 * Functions for selectivity estimation of array operators
6 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/backend/utils/adt/array_selfuncs.c
13 *-------------------------------------------------------------------------
19 #include "access/htup_details.h"
20 #include "catalog/pg_operator.h"
21 #include "catalog/pg_statistic.h"
22 #include "utils/array.h"
23 #include "utils/fmgrprotos.h"
24 #include "utils/lsyscache.h"
25 #include "utils/selfuncs.h"
26 #include "utils/typcache.h"
29 /* Default selectivity constant for "@>" and "<@" operators */
30 #define DEFAULT_CONTAIN_SEL 0.005
32 /* Default selectivity constant for "&&" operator */
33 #define DEFAULT_OVERLAP_SEL 0.01
35 /* Default selectivity for given operator */
36 #define DEFAULT_SEL(operator) \
37 ((operator) == OID_ARRAY_OVERLAP_OP ? \
38 DEFAULT_OVERLAP_SEL : DEFAULT_CONTAIN_SEL)
40 static Selectivity
calc_arraycontsel(VariableStatData
*vardata
, Datum constval
,
41 Oid elemtype
, Oid
operator);
42 static Selectivity
mcelem_array_selec(ArrayType
*array
,
43 TypeCacheEntry
*typentry
,
44 Datum
*mcelem
, int nmcelem
,
45 float4
*numbers
, int nnumbers
,
46 float4
*hist
, int nhist
,
48 static Selectivity
mcelem_array_contain_overlap_selec(Datum
*mcelem
, int nmcelem
,
49 float4
*numbers
, int nnumbers
,
50 Datum
*array_data
, int nitems
,
51 Oid
operator, TypeCacheEntry
*typentry
);
52 static Selectivity
mcelem_array_contained_selec(Datum
*mcelem
, int nmcelem
,
53 float4
*numbers
, int nnumbers
,
54 Datum
*array_data
, int nitems
,
55 float4
*hist
, int nhist
,
56 Oid
operator, TypeCacheEntry
*typentry
);
57 static float *calc_hist(const float4
*hist
, int nhist
, int n
);
58 static float *calc_distr(const float *p
, int n
, int m
, float rest
);
59 static int floor_log2(uint32 n
);
60 static bool find_next_mcelem(Datum
*mcelem
, int nmcelem
, Datum value
,
61 int *index
, TypeCacheEntry
*typentry
);
62 static int element_compare(const void *key1
, const void *key2
, void *arg
);
63 static int float_compare_desc(const void *key1
, const void *key2
);
67 * scalararraysel_containment
68 * Estimate selectivity of ScalarArrayOpExpr via array containment.
70 * If we have const =/<> ANY/ALL (array_var) then we can estimate the
71 * selectivity as though this were an array containment operator,
72 * array_var op ARRAY[const].
74 * scalararraysel() has already verified that the ScalarArrayOpExpr's operator
75 * is the array element type's default equality or inequality operator, and
76 * has aggressively simplified both inputs to constants.
78 * Returns selectivity (0..1), or -1 if we fail to estimate selectivity.
81 scalararraysel_containment(PlannerInfo
*root
,
82 Node
*leftop
, Node
*rightop
,
83 Oid elemtype
, bool isEquality
, bool useOr
,
87 VariableStatData vardata
;
89 TypeCacheEntry
*typentry
;
93 * rightop must be a variable, else punt.
95 examine_variable(root
, rightop
, varRelid
, &vardata
);
98 ReleaseVariableStats(vardata
);
103 * leftop must be a constant, else punt.
105 if (!IsA(leftop
, Const
))
107 ReleaseVariableStats(vardata
);
110 if (((Const
*) leftop
)->constisnull
)
112 /* qual can't succeed if null on left */
113 ReleaseVariableStats(vardata
);
114 return (Selectivity
) 0.0;
116 constval
= ((Const
*) leftop
)->constvalue
;
118 /* Get element type's default comparison function */
119 typentry
= lookup_type_cache(elemtype
, TYPECACHE_CMP_PROC_FINFO
);
120 if (!OidIsValid(typentry
->cmp_proc_finfo
.fn_oid
))
122 ReleaseVariableStats(vardata
);
125 cmpfunc
= &typentry
->cmp_proc_finfo
;
128 * If the operator is <>, swap ANY/ALL, then invert the result later.
133 /* Get array element stats for var, if available */
134 if (HeapTupleIsValid(vardata
.statsTuple
) &&
135 statistic_proc_security_check(&vardata
, cmpfunc
->fn_oid
))
137 Form_pg_statistic stats
;
141 stats
= (Form_pg_statistic
) GETSTRUCT(vardata
.statsTuple
);
143 /* MCELEM will be an array of same type as element */
144 if (get_attstatsslot(&sslot
, vardata
.statsTuple
,
145 STATISTIC_KIND_MCELEM
, InvalidOid
,
146 ATTSTATSSLOT_VALUES
| ATTSTATSSLOT_NUMBERS
))
148 /* For ALL case, also get histogram of distinct-element counts */
150 !get_attstatsslot(&hslot
, vardata
.statsTuple
,
151 STATISTIC_KIND_DECHIST
, InvalidOid
,
152 ATTSTATSSLOT_NUMBERS
))
153 memset(&hslot
, 0, sizeof(hslot
));
156 * For = ANY, estimate as var @> ARRAY[const].
158 * For = ALL, estimate as var <@ ARRAY[const].
161 selec
= mcelem_array_contain_overlap_selec(sslot
.values
,
166 OID_ARRAY_CONTAINS_OP
,
169 selec
= mcelem_array_contained_selec(sslot
.values
,
176 OID_ARRAY_CONTAINED_OP
,
179 free_attstatsslot(&hslot
);
180 free_attstatsslot(&sslot
);
184 /* No most-common-elements info, so do without */
186 selec
= mcelem_array_contain_overlap_selec(NULL
, 0,
189 OID_ARRAY_CONTAINS_OP
,
192 selec
= mcelem_array_contained_selec(NULL
, 0,
196 OID_ARRAY_CONTAINED_OP
,
201 * MCE stats count only non-null rows, so adjust for null rows.
203 selec
*= (1.0 - stats
->stanullfrac
);
207 /* No stats at all, so do without */
209 selec
= mcelem_array_contain_overlap_selec(NULL
, 0,
212 OID_ARRAY_CONTAINS_OP
,
215 selec
= mcelem_array_contained_selec(NULL
, 0,
219 OID_ARRAY_CONTAINED_OP
,
221 /* we assume no nulls here, so no stanullfrac correction */
224 ReleaseVariableStats(vardata
);
227 * If the operator is <>, invert the results.
232 CLAMP_PROBABILITY(selec
);
238 * arraycontsel -- restriction selectivity for array @>, &&, <@ operators
241 arraycontsel(PG_FUNCTION_ARGS
)
243 PlannerInfo
*root
= (PlannerInfo
*) PG_GETARG_POINTER(0);
244 Oid
operator = PG_GETARG_OID(1);
245 List
*args
= (List
*) PG_GETARG_POINTER(2);
246 int varRelid
= PG_GETARG_INT32(3);
247 VariableStatData vardata
;
254 * If expression is not (variable op something) or (something op
255 * variable), then punt and return a default estimate.
257 if (!get_restriction_variable(root
, args
, varRelid
,
258 &vardata
, &other
, &varonleft
))
259 PG_RETURN_FLOAT8(DEFAULT_SEL(operator));
262 * Can't do anything useful if the something is not a constant, either.
264 if (!IsA(other
, Const
))
266 ReleaseVariableStats(vardata
);
267 PG_RETURN_FLOAT8(DEFAULT_SEL(operator));
271 * The "&&", "@>" and "<@" operators are strict, so we can cope with a
272 * NULL constant right away.
274 if (((Const
*) other
)->constisnull
)
276 ReleaseVariableStats(vardata
);
277 PG_RETURN_FLOAT8(0.0);
281 * If var is on the right, commute the operator, so that we can assume the
282 * var is on the left in what follows.
286 if (operator == OID_ARRAY_CONTAINS_OP
)
287 operator = OID_ARRAY_CONTAINED_OP
;
288 else if (operator == OID_ARRAY_CONTAINED_OP
)
289 operator = OID_ARRAY_CONTAINS_OP
;
293 * OK, there's a Var and a Const we're dealing with here. We need the
294 * Const to be an array with same element type as column, else we can't do
295 * anything useful. (Such cases will likely fail at runtime, but here
296 * we'd rather just return a default estimate.)
298 element_typeid
= get_base_element_type(((Const
*) other
)->consttype
);
299 if (element_typeid
!= InvalidOid
&&
300 element_typeid
== get_base_element_type(vardata
.vartype
))
302 selec
= calc_arraycontsel(&vardata
, ((Const
*) other
)->constvalue
,
303 element_typeid
, operator);
307 selec
= DEFAULT_SEL(operator);
310 ReleaseVariableStats(vardata
);
312 CLAMP_PROBABILITY(selec
);
314 PG_RETURN_FLOAT8((float8
) selec
);
318 * arraycontjoinsel -- join selectivity for array @>, &&, <@ operators
321 arraycontjoinsel(PG_FUNCTION_ARGS
)
323 /* For the moment this is just a stub */
324 Oid
operator = PG_GETARG_OID(1);
326 PG_RETURN_FLOAT8(DEFAULT_SEL(operator));
330 * Calculate selectivity for "arraycolumn @> const", "arraycolumn && const"
331 * or "arraycolumn <@ const" based on the statistics
333 * This function is mainly responsible for extracting the pg_statistic data
334 * to be used; we then pass the problem on to mcelem_array_selec().
337 calc_arraycontsel(VariableStatData
*vardata
, Datum constval
,
338 Oid elemtype
, Oid
operator)
341 TypeCacheEntry
*typentry
;
345 /* Get element type's default comparison function */
346 typentry
= lookup_type_cache(elemtype
, TYPECACHE_CMP_PROC_FINFO
);
347 if (!OidIsValid(typentry
->cmp_proc_finfo
.fn_oid
))
348 return DEFAULT_SEL(operator);
349 cmpfunc
= &typentry
->cmp_proc_finfo
;
352 * The caller made sure the const is an array with same element type, so
355 array
= DatumGetArrayTypeP(constval
);
357 if (HeapTupleIsValid(vardata
->statsTuple
) &&
358 statistic_proc_security_check(vardata
, cmpfunc
->fn_oid
))
360 Form_pg_statistic stats
;
364 stats
= (Form_pg_statistic
) GETSTRUCT(vardata
->statsTuple
);
366 /* MCELEM will be an array of same type as column */
367 if (get_attstatsslot(&sslot
, vardata
->statsTuple
,
368 STATISTIC_KIND_MCELEM
, InvalidOid
,
369 ATTSTATSSLOT_VALUES
| ATTSTATSSLOT_NUMBERS
))
372 * For "array <@ const" case we also need histogram of distinct
375 if (operator != OID_ARRAY_CONTAINED_OP
||
376 !get_attstatsslot(&hslot
, vardata
->statsTuple
,
377 STATISTIC_KIND_DECHIST
, InvalidOid
,
378 ATTSTATSSLOT_NUMBERS
))
379 memset(&hslot
, 0, sizeof(hslot
));
381 /* Use the most-common-elements slot for the array Var. */
382 selec
= mcelem_array_selec(array
, typentry
,
383 sslot
.values
, sslot
.nvalues
,
384 sslot
.numbers
, sslot
.nnumbers
,
385 hslot
.numbers
, hslot
.nnumbers
,
388 free_attstatsslot(&hslot
);
389 free_attstatsslot(&sslot
);
393 /* No most-common-elements info, so do without */
394 selec
= mcelem_array_selec(array
, typentry
,
395 NULL
, 0, NULL
, 0, NULL
, 0,
400 * MCE stats count only non-null rows, so adjust for null rows.
402 selec
*= (1.0 - stats
->stanullfrac
);
406 /* No stats at all, so do without */
407 selec
= mcelem_array_selec(array
, typentry
,
408 NULL
, 0, NULL
, 0, NULL
, 0,
410 /* we assume no nulls here, so no stanullfrac correction */
413 /* If constant was toasted, release the copy we made */
414 if (PointerGetDatum(array
) != constval
)
421 * Array selectivity estimation based on most common elements statistics
423 * This function just deconstructs and sorts the array constant's contents,
424 * and then passes the problem on to mcelem_array_contain_overlap_selec or
425 * mcelem_array_contained_selec depending on the operator.
428 mcelem_array_selec(ArrayType
*array
, TypeCacheEntry
*typentry
,
429 Datum
*mcelem
, int nmcelem
,
430 float4
*numbers
, int nnumbers
,
431 float4
*hist
, int nhist
,
443 * Prepare constant array data for sorting. Sorting lets us find unique
444 * elements and efficiently merge with the MCELEM array.
446 deconstruct_array(array
,
451 &elem_values
, &elem_nulls
, &num_elems
);
453 /* Collapse out any null elements */
455 null_present
= false;
456 for (i
= 0; i
< num_elems
; i
++)
461 elem_values
[nonnull_nitems
++] = elem_values
[i
];
465 * Query "column @> '{anything, null}'" matches nothing. For the other
466 * two operators, presence of a null in the constant can be ignored.
468 if (null_present
&& operator == OID_ARRAY_CONTAINS_OP
)
472 return (Selectivity
) 0.0;
475 /* Sort extracted elements using their default comparison function. */
476 qsort_arg(elem_values
, nonnull_nitems
, sizeof(Datum
),
477 element_compare
, typentry
);
479 /* Separate cases according to operator */
480 if (operator == OID_ARRAY_CONTAINS_OP
|| operator == OID_ARRAY_OVERLAP_OP
)
481 selec
= mcelem_array_contain_overlap_selec(mcelem
, nmcelem
,
483 elem_values
, nonnull_nitems
,
485 else if (operator == OID_ARRAY_CONTAINED_OP
)
486 selec
= mcelem_array_contained_selec(mcelem
, nmcelem
,
488 elem_values
, nonnull_nitems
,
493 elog(ERROR
, "arraycontsel called for unrecognized operator %u",
495 selec
= 0.0; /* keep compiler quiet */
504 * Estimate selectivity of "column @> const" and "column && const" based on
505 * most common element statistics. This estimation assumes element
506 * occurrences are independent.
508 * mcelem (of length nmcelem) and numbers (of length nnumbers) are from
509 * the array column's MCELEM statistics slot, or are NULL/0 if stats are
510 * not available. array_data (of length nitems) is the constant's elements.
512 * Both the mcelem and array_data arrays are assumed presorted according
513 * to the element type's cmpfunc. Null elements are not present.
515 * TODO: this estimate probably could be improved by using the distinct
516 * elements count histogram. For example, excepting the special case of
517 * "column @> '{}'", we can multiply the calculated selectivity by the
518 * fraction of nonempty arrays in the column.
521 mcelem_array_contain_overlap_selec(Datum
*mcelem
, int nmcelem
,
522 float4
*numbers
, int nnumbers
,
523 Datum
*array_data
, int nitems
,
524 Oid
operator, TypeCacheEntry
*typentry
)
534 * There should be three more Numbers than Values, because the last three
535 * cells should hold minimal and maximal frequency among the non-null
536 * elements, and then the frequency of null elements. Ignore the Numbers
539 if (nnumbers
!= nmcelem
+ 3)
547 /* Grab the lowest observed frequency */
548 minfreq
= numbers
[nmcelem
];
552 /* Without statistics make some default assumptions */
553 minfreq
= 2 * (float4
) DEFAULT_CONTAIN_SEL
;
556 /* Decide whether it is faster to use binary search or not. */
557 if (nitems
* floor_log2((uint32
) nmcelem
) < nmcelem
+ nitems
)
562 if (operator == OID_ARRAY_CONTAINS_OP
)
565 * Initial selectivity for "column @> const" query is 1.0, and it will
566 * be decreased with each element of constant array.
573 * Initial selectivity for "column && const" query is 0.0, and it will
574 * be increased with each element of constant array.
579 /* Scan mcelem and array in parallel. */
581 for (i
= 0; i
< nitems
; i
++)
585 /* Ignore any duplicates in the array data. */
587 element_compare(&array_data
[i
- 1], &array_data
[i
], typentry
) == 0)
590 /* Find the smallest MCELEM >= this array item. */
593 match
= find_next_mcelem(mcelem
, nmcelem
, array_data
[i
],
594 &mcelem_index
, typentry
);
598 while (mcelem_index
< nmcelem
)
600 int cmp
= element_compare(&mcelem
[mcelem_index
],
609 match
= true; /* mcelem is found */
615 if (match
&& numbers
)
617 /* MCELEM matches the array item; use its frequency. */
618 elem_selec
= numbers
[mcelem_index
];
624 * The element is not in MCELEM. Punt, but assume that the
625 * selectivity cannot be more than minfreq / 2.
627 elem_selec
= Min(DEFAULT_CONTAIN_SEL
, minfreq
/ 2);
631 * Update overall selectivity using the current element's selectivity
632 * and an assumption of element occurrence independence.
634 if (operator == OID_ARRAY_CONTAINS_OP
)
637 selec
= selec
+ elem_selec
- selec
* elem_selec
;
639 /* Clamp intermediate results to stay sane despite roundoff error */
640 CLAMP_PROBABILITY(selec
);
647 * Estimate selectivity of "column <@ const" based on most common element
650 * mcelem (of length nmcelem) and numbers (of length nnumbers) are from
651 * the array column's MCELEM statistics slot, or are NULL/0 if stats are
652 * not available. array_data (of length nitems) is the constant's elements.
653 * hist (of length nhist) is from the array column's DECHIST statistics slot,
654 * or is NULL/0 if those stats are not available.
656 * Both the mcelem and array_data arrays are assumed presorted according
657 * to the element type's cmpfunc. Null elements are not present.
659 * Independent element occurrence would imply a particular distribution of
660 * distinct element counts among matching rows. Real data usually falsifies
661 * that assumption. For example, in a set of 11-element integer arrays having
662 * elements in the range [0..10], element occurrences are typically not
663 * independent. If they were, a sufficiently-large set would include all
664 * distinct element counts 0 through 11. We correct for this using the
665 * histogram of distinct element counts.
667 * In the "column @> const" and "column && const" cases, we usually have a
668 * "const" with low number of elements (otherwise we have selectivity close
669 * to 0 or 1 respectively). That's why the effect of dependence related
670 * to distinct element count distribution is negligible there. In the
671 * "column <@ const" case, number of elements is usually high (otherwise we
672 * have selectivity close to 0). That's why we should do a correction with
673 * the array distinct element count distribution here.
675 * Using the histogram of distinct element counts produces a different
676 * distribution law than independent occurrences of elements. This
677 * distribution law can be described as follows:
679 * P(o1, o2, ..., on) = f1^o1 * (1 - f1)^(1 - o1) * f2^o2 *
680 * (1 - f2)^(1 - o2) * ... * fn^on * (1 - fn)^(1 - on) * hist[m] / ind[m]
683 * o1, o2, ..., on - occurrences of elements 1, 2, ..., n
684 * (1 - occurrence, 0 - no occurrence) in row
685 * f1, f2, ..., fn - frequencies of elements 1, 2, ..., n
686 * (scalar values in [0..1]) according to collected statistics
687 * m = o1 + o2 + ... + on = total number of distinct elements in row
688 * hist[m] - histogram data for occurrence of m elements.
689 * ind[m] - probability of m occurrences from n events assuming their
690 * probabilities to be equal to frequencies of array elements.
692 * ind[m] = sum(f1^o1 * (1 - f1)^(1 - o1) * f2^o2 * (1 - f2)^(1 - o2) *
693 * ... * fn^on * (1 - fn)^(1 - on), o1, o2, ..., on) | o1 + o2 + .. on = m
696 mcelem_array_contained_selec(Datum
*mcelem
, int nmcelem
,
697 float4
*numbers
, int nnumbers
,
698 Datum
*array_data
, int nitems
,
699 float4
*hist
, int nhist
,
700 Oid
operator, TypeCacheEntry
*typentry
)
717 * There should be three more Numbers than Values in the MCELEM slot,
718 * because the last three cells should hold minimal and maximal frequency
719 * among the non-null elements, and then the frequency of null elements.
720 * Punt if not right, because we can't do much without the element freqs.
722 if (numbers
== NULL
|| nnumbers
!= nmcelem
+ 3)
723 return DEFAULT_CONTAIN_SEL
;
725 /* Can't do much without a count histogram, either */
726 if (hist
== NULL
|| nhist
< 3)
727 return DEFAULT_CONTAIN_SEL
;
730 * Grab some of the summary statistics that compute_array_stats() stores:
731 * lowest frequency, frequency of null elements, and average distinct
734 minfreq
= numbers
[nmcelem
];
735 nullelem_freq
= numbers
[nmcelem
+ 2];
736 avg_count
= hist
[nhist
- 1];
739 * "rest" will be the sum of the frequencies of all elements not
740 * represented in MCELEM. The average distinct element count is the sum
741 * of the frequencies of *all* elements. Begin with that; we will proceed
742 * to subtract the MCELEM frequencies.
747 * mult is a multiplier representing estimate of probability that each
748 * mcelem that is not present in constant doesn't occur.
753 * elem_selec is array of estimated frequencies for elements in the
756 elem_selec
= (float *) palloc(sizeof(float) * nitems
);
758 /* Scan mcelem and array in parallel. */
760 for (i
= 0; i
< nitems
; i
++)
764 /* Ignore any duplicates in the array data. */
766 element_compare(&array_data
[i
- 1], &array_data
[i
], typentry
) == 0)
770 * Iterate over MCELEM until we find an entry greater than or equal to
771 * this element of the constant. Update "rest" and "mult" for mcelem
772 * entries skipped over.
774 while (mcelem_index
< nmcelem
)
776 int cmp
= element_compare(&mcelem
[mcelem_index
],
782 mult
*= (1.0f
- numbers
[mcelem_index
]);
783 rest
-= numbers
[mcelem_index
];
789 match
= true; /* mcelem is found */
796 /* MCELEM matches the array item. */
797 elem_selec
[unique_nitems
] = numbers
[mcelem_index
];
798 /* "rest" is decremented for all mcelems, matched or not */
799 rest
-= numbers
[mcelem_index
];
805 * The element is not in MCELEM. Punt, but assume that the
806 * selectivity cannot be more than minfreq / 2.
808 elem_selec
[unique_nitems
] = Min(DEFAULT_CONTAIN_SEL
,
816 * If we handled all constant elements without exhausting the MCELEM
817 * array, finish walking it to complete calculation of "rest" and "mult".
819 while (mcelem_index
< nmcelem
)
821 mult
*= (1.0f
- numbers
[mcelem_index
]);
822 rest
-= numbers
[mcelem_index
];
827 * The presence of many distinct rare elements materially decreases
828 * selectivity. Use the Poisson distribution to estimate the probability
829 * of a column value having zero occurrences of such elements. See above
830 * for the definition of "rest".
835 * Using the distinct element count histogram requires
836 * O(unique_nitems * (nmcelem + unique_nitems))
837 * operations. Beyond a certain computational cost threshold, it's
838 * reasonable to sacrifice accuracy for decreased planning time. We limit
839 * the number of operations to EFFORT * nmcelem; since nmcelem is limited
840 * by the column's statistics target, the work done is user-controllable.
842 * If the number of operations would be too large, we can reduce it
843 * without losing all accuracy by reducing unique_nitems and considering
844 * only the most-common elements of the constant array. To make the
845 * results exactly match what we would have gotten with only those
846 * elements to start with, we'd have to remove any discarded elements'
847 * frequencies from "mult", but since this is only an approximation
848 * anyway, we don't bother with that. Therefore it's sufficient to qsort
849 * elem_selec[] and take the largest elements. (They will no longer match
850 * up with the elements of array_data[], but we don't care.)
855 if ((nmcelem
+ unique_nitems
) > 0 &&
856 unique_nitems
> EFFORT
* nmcelem
/ (nmcelem
+ unique_nitems
))
859 * Use the quadratic formula to solve for largest allowable N. We
860 * have A = 1, B = nmcelem, C = - EFFORT * nmcelem.
862 double b
= (double) nmcelem
;
865 n
= (int) ((sqrt(b
* b
+ 4 * EFFORT
* b
) - b
) / 2);
867 /* Sort, then take just the first n elements */
868 qsort(elem_selec
, unique_nitems
, sizeof(float),
874 * Calculate probabilities of each distinct element count for both mcelems
875 * and constant elements. At this point, assume independent element
878 dist
= calc_distr(elem_selec
, unique_nitems
, unique_nitems
, 0.0f
);
879 mcelem_dist
= calc_distr(numbers
, nmcelem
, unique_nitems
, rest
);
881 /* ignore hist[nhist-1], which is the average not a histogram member */
882 hist_part
= calc_hist(hist
, nhist
- 1, unique_nitems
);
885 for (i
= 0; i
<= unique_nitems
; i
++)
888 * mult * dist[i] / mcelem_dist[i] gives us probability of qual
889 * matching from assumption of independent element occurrence with the
890 * condition that distinct element count = i.
892 if (mcelem_dist
[i
] > 0)
893 selec
+= hist_part
[i
] * mult
* dist
[i
] / mcelem_dist
[i
];
901 /* Take into account occurrence of NULL element. */
902 selec
*= (1.0f
- nullelem_freq
);
904 CLAMP_PROBABILITY(selec
);
910 * Calculate the first n distinct element count probabilities from a
911 * histogram of distinct element counts.
913 * Returns a palloc'd array of n+1 entries, with array[k] being the
914 * probability of element count k, k in [0..n].
916 * We assume that a histogram box with bounds a and b gives 1 / ((b - a + 1) *
917 * (nhist - 1)) probability to each value in (a,b) and an additional half of
918 * that to a and b themselves.
921 calc_hist(const float4
*hist
, int nhist
, int n
)
926 float prev_interval
= 0,
930 hist_part
= (float *) palloc((n
+ 1) * sizeof(float));
933 * frac is a probability contribution for each interval between histogram
934 * values. We have nhist - 1 intervals, so contribution of each one will
935 * be 1 / (nhist - 1).
937 frac
= 1.0f
/ ((float) (nhist
- 1));
939 for (k
= 0; k
<= n
; k
++)
944 * Count the histogram boundaries equal to k. (Although the histogram
945 * should theoretically contain only exact integers, entries are
946 * floats so there could be roundoff error in large values. Treat any
947 * fractional value as equal to the next larger k.)
949 while (i
< nhist
&& hist
[i
] <= k
)
957 /* k is an exact bound for at least one histogram box. */
960 /* Find length between current histogram value and the next one */
962 next_interval
= hist
[i
] - hist
[i
- 1];
967 * count - 1 histogram boxes contain k exclusively. They
968 * contribute a total of (count - 1) * frac probability. Also
969 * factor in the partial histogram boxes on either side.
971 val
= (float) (count
- 1);
972 if (next_interval
> 0)
973 val
+= 0.5f
/ next_interval
;
974 if (prev_interval
> 0)
975 val
+= 0.5f
/ prev_interval
;
976 hist_part
[k
] = frac
* val
;
978 prev_interval
= next_interval
;
982 /* k does not appear as an exact histogram bound. */
983 if (prev_interval
> 0)
984 hist_part
[k
] = frac
/ prev_interval
;
994 * Consider n independent events with probabilities p[]. This function
995 * calculates probabilities of exact k of events occurrence for k in [0..m].
996 * Returns a palloc'd array of size m+1.
998 * "rest" is the sum of the probabilities of all low-probability events not
1001 * Imagine matrix M of size (n + 1) x (m + 1). Element M[i,j] denotes the
1002 * probability that exactly j of first i events occur. Obviously M[0,0] = 1.
1003 * For any constant j, each increment of i increases the probability iff the
1004 * event occurs. So, by the law of total probability:
1005 * M[i,j] = M[i - 1, j] * (1 - p[i]) + M[i - 1, j - 1] * p[i]
1007 * M[i,0] = M[i - 1, 0] * (1 - p[i]) for i > 0.
1010 calc_distr(const float *p
, int n
, int m
, float rest
)
1019 * Since we return only the last row of the matrix and need only the
1020 * current and previous row for calculations, allocate two rows.
1022 row
= (float *) palloc((m
+ 1) * sizeof(float));
1023 prev_row
= (float *) palloc((m
+ 1) * sizeof(float));
1027 for (i
= 1; i
<= n
; i
++)
1036 /* Calculate next row */
1037 for (j
= 0; j
<= i
&& j
<= m
; j
++)
1042 val
+= prev_row
[j
] * (1.0f
- t
);
1044 val
+= prev_row
[j
- 1] * t
;
1050 * The presence of many distinct rare (not in "p") elements materially
1051 * decreases selectivity. Model their collective occurrence with the
1052 * Poisson distribution.
1054 if (rest
> DEFAULT_CONTAIN_SEL
)
1063 for (i
= 0; i
<= m
; i
++)
1066 /* Value of Poisson distribution for 0 occurrences */
1070 * Calculate convolution of previously computed distribution and the
1071 * Poisson distribution.
1073 for (i
= 0; i
<= m
; i
++)
1075 for (j
= 0; j
<= m
- i
; j
++)
1076 row
[j
+ i
] += prev_row
[j
] * t
;
1078 /* Get Poisson distribution value for (i + 1) occurrences */
1079 t
*= rest
/ (float) (i
+ 1);
1087 /* Fast function for floor value of 2 based logarithm calculation. */
1089 floor_log2(uint32 n
)
1123 * find_next_mcelem binary-searches a most common elements array, starting
1124 * from *index, for the first member >= value. It saves the position of the
1125 * match into *index and returns true if it's an exact match. (Note: we
1126 * assume the mcelem elements are distinct so there can't be more than one
1130 find_next_mcelem(Datum
*mcelem
, int nmcelem
, Datum value
, int *index
,
1131 TypeCacheEntry
*typentry
)
1141 res
= element_compare(&mcelem
[i
], &value
, typentry
);
1157 * Comparison function for elements.
1159 * We use the element type's default btree opclass, and its default collation
1160 * if the type is collation-sensitive.
1162 * XXX consider using SortSupport infrastructure
1165 element_compare(const void *key1
, const void *key2
, void *arg
)
1167 Datum d1
= *((const Datum
*) key1
);
1168 Datum d2
= *((const Datum
*) key2
);
1169 TypeCacheEntry
*typentry
= (TypeCacheEntry
*) arg
;
1170 FmgrInfo
*cmpfunc
= &typentry
->cmp_proc_finfo
;
1173 c
= FunctionCall2Coll(cmpfunc
, typentry
->typcollation
, d1
, d2
);
1174 return DatumGetInt32(c
);
1178 * Comparison function for sorting floats into descending order.
1181 float_compare_desc(const void *key1
, const void *key2
)
1183 float d1
= *((const float *) key1
);
1184 float d2
= *((const float *) key2
);