* Array selectivity estimation based on most common elements statistics * * This function just deconstructs and sorts the array constant's contents, * and then passes the problem on to mcelem_array_contain_overlap_selec or * mcelem_array_contained_selec depending on the operator. */
| 426 | * mcelem_array_contained_selec depending on the operator. |
| 427 | */ |
| 428 | static Selectivity |
| 429 | mcelem_array_selec(ArrayType *array, TypeCacheEntry *typentry, |
| 430 | Datum *mcelem, int nmcelem, |
| 431 | float4 *numbers, int nnumbers, |
| 432 | float4 *hist, int nhist, |
| 433 | Oid operator) |
| 434 | { |
| 435 | Selectivity selec; |
| 436 | int num_elems; |
| 437 | Datum *elem_values; |
| 438 | bool *elem_nulls; |
| 439 | bool null_present; |
| 440 | int nonnull_nitems; |
| 441 | int i; |
| 442 | |
| 443 | /* |
| 444 | * Prepare constant array data for sorting. Sorting lets us find unique |
| 445 | * elements and efficiently merge with the MCELEM array. |
| 446 | */ |
| 447 | deconstruct_array(array, |
| 448 | typentry->type_id, |
| 449 | typentry->typlen, |
| 450 | typentry->typbyval, |
| 451 | typentry->typalign, |
| 452 | &elem_values, &elem_nulls, &num_elems); |
| 453 | |
| 454 | /* Collapse out any null elements */ |
| 455 | nonnull_nitems = 0; |
| 456 | null_present = false; |
| 457 | for (i = 0; i < num_elems; i++) |
| 458 | { |
| 459 | if (elem_nulls[i]) |
| 460 | null_present = true; |
| 461 | else |
| 462 | elem_values[nonnull_nitems++] = elem_values[i]; |
| 463 | } |
| 464 | |
| 465 | /* |
| 466 | * Query "column @> '{anything, null}'" matches nothing. For the other |
| 467 | * two operators, presence of a null in the constant can be ignored. |
| 468 | */ |
| 469 | if (null_present && operator == OID_ARRAY_CONTAINS_OP) |
| 470 | { |
| 471 | pfree(elem_values); |
| 472 | pfree(elem_nulls); |
| 473 | return (Selectivity) 0.0; |
| 474 | } |
| 475 | |
| 476 | /* Sort extracted elements using their default comparison function. */ |
| 477 | qsort_arg(elem_values, nonnull_nitems, sizeof(Datum), |
| 478 | element_compare, typentry); |
| 479 | |
| 480 | /* Separate cases according to operator */ |
| 481 | if (operator == OID_ARRAY_CONTAINS_OP || operator == OID_ARRAY_OVERLAP_OP) |
| 482 | selec = mcelem_array_contain_overlap_selec(mcelem, nmcelem, |
| 483 | numbers, nnumbers, |
| 484 | elem_values, nonnull_nitems, |
| 485 | operator, typentry); |
no test coverage detected