* Calculate selectivity for "arraycolumn @> const", "arraycolumn && const" * or "arraycolumn <@ const" based on the statistics * * This function is mainly responsible for extracting the pg_statistic data * to be used; we then pass the problem on to mcelem_array_selec(). */
| 335 | * to be used; we then pass the problem on to mcelem_array_selec(). |
| 336 | */ |
| 337 | static Selectivity |
| 338 | calc_arraycontsel(VariableStatData *vardata, Datum constval, |
| 339 | Oid elemtype, Oid operator) |
| 340 | { |
| 341 | Selectivity selec; |
| 342 | TypeCacheEntry *typentry; |
| 343 | FmgrInfo *cmpfunc; |
| 344 | ArrayType *array; |
| 345 | |
| 346 | /* Get element type's default comparison function */ |
| 347 | typentry = lookup_type_cache(elemtype, TYPECACHE_CMP_PROC_FINFO); |
| 348 | if (!OidIsValid(typentry->cmp_proc_finfo.fn_oid)) |
| 349 | return DEFAULT_SEL(operator); |
| 350 | cmpfunc = &typentry->cmp_proc_finfo; |
| 351 | |
| 352 | /* |
| 353 | * The caller made sure the const is an array with same element type, so |
| 354 | * get it now |
| 355 | */ |
| 356 | array = DatumGetArrayTypeP(constval); |
| 357 | |
| 358 | if (HeapTupleIsValid(vardata->statsTuple) && |
| 359 | statistic_proc_security_check(vardata, cmpfunc->fn_oid)) |
| 360 | { |
| 361 | Form_pg_statistic stats; |
| 362 | AttStatsSlot sslot; |
| 363 | AttStatsSlot hslot; |
| 364 | |
| 365 | stats = (Form_pg_statistic) GETSTRUCT(vardata->statsTuple); |
| 366 | |
| 367 | /* MCELEM will be an array of same type as column */ |
| 368 | if (get_attstatsslot(&sslot, vardata->statsTuple, |
| 369 | STATISTIC_KIND_MCELEM, InvalidOid, |
| 370 | ATTSTATSSLOT_VALUES | ATTSTATSSLOT_NUMBERS)) |
| 371 | { |
| 372 | /* |
| 373 | * For "array <@ const" case we also need histogram of distinct |
| 374 | * element counts. |
| 375 | */ |
| 376 | if (operator != OID_ARRAY_CONTAINED_OP || |
| 377 | !get_attstatsslot(&hslot, vardata->statsTuple, |
| 378 | STATISTIC_KIND_DECHIST, InvalidOid, |
| 379 | ATTSTATSSLOT_NUMBERS)) |
| 380 | memset(&hslot, 0, sizeof(hslot)); |
| 381 | |
| 382 | /* Use the most-common-elements slot for the array Var. */ |
| 383 | selec = mcelem_array_selec(array, typentry, |
| 384 | sslot.values, sslot.nvalues, |
| 385 | sslot.numbers, sslot.nnumbers, |
| 386 | hslot.numbers, hslot.nnumbers, |
| 387 | operator); |
| 388 | |
| 389 | free_attstatsslot(&hslot); |
| 390 | free_attstatsslot(&sslot); |
| 391 | } |
| 392 | else |
| 393 | { |
| 394 | /* No most-common-elements info, so do without */ |
no test coverage detected