From 6cae9d2c10e151f741e7bc64a8b70bb2615c367c Mon Sep 17 00:00:00 2001 From: Alexander Korotkov Date: Thu, 19 Sep 2019 21:30:19 +0300 Subject: Improve handling of NULLs in KNN-GiST and KNN-SP-GiST This commit improves subject in two ways: * It removes ugliness of 02f90879e7, which stores distance values and null flags in two separate arrays after GISTSearchItem struct. Instead we pack both distance value and null flag in IndexOrderByDistance struct. Alignment overhead should be negligible, because we typically deal with at most few "col op const" expressions in ORDER BY clause. * It fixes handling of "col op NULL" expression in KNN-SP-GiST. Now, these expression are not passed to support functions, which can't deal with them. Instead, NULL result is implicitly assumed. It future we may decide to teach support functions to deal with NULL arguments, but current solution is bugfix suitable for backpatch. Reported-by: Nikita Glukhov Discussion: https://postgr.es/m/826f57ee-afc7-8977-c44c-6111d18b02ec%40postgrespro.ru Author: Nikita Glukhov Reviewed-by: Alexander Korotkov Backpatch-through: 9.4 --- src/backend/access/index/indexam.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'src/backend/access/index/indexam.c') diff --git a/src/backend/access/index/indexam.c b/src/backend/access/index/indexam.c index 2e8f53a37c8..442f41425d4 100644 --- a/src/backend/access/index/indexam.c +++ b/src/backend/access/index/indexam.c @@ -847,14 +847,14 @@ index_getprocinfo(Relation irel, */ void index_store_float8_orderby_distances(IndexScanDesc scan, Oid *orderByTypes, - double *distanceValues, - bool *distanceNulls, bool recheckOrderBy) + IndexOrderByDistance *distances, + bool recheckOrderBy) { int i; scan->xs_recheckorderby = recheckOrderBy; - if (!distanceValues) + if (!distances) { Assert(!scan->xs_recheckorderby); @@ -869,11 +869,11 @@ index_store_float8_orderby_distances(IndexScanDesc scan, Oid *orderByTypes, for (i = 0; i < scan->numberOfOrderBys; i++) { - if (distanceNulls && distanceNulls[i]) - { + scan->xs_orderbynulls[i] = distances[i].isnull; + + if (scan->xs_orderbynulls[i]) scan->xs_orderbyvals[i] = (Datum) 0; - scan->xs_orderbynulls[i] = true; - } + if (orderByTypes[i] == FLOAT8OID) { #ifndef USE_FLOAT8_BYVAL @@ -881,8 +881,8 @@ index_store_float8_orderby_distances(IndexScanDesc scan, Oid *orderByTypes, if (!scan->xs_orderbynulls[i]) pfree(DatumGetPointer(scan->xs_orderbyvals[i])); #endif - scan->xs_orderbyvals[i] = Float8GetDatum(distanceValues[i]); - scan->xs_orderbynulls[i] = false; + if (!scan->xs_orderbynulls[i]) + scan->xs_orderbyvals[i] = Float8GetDatum(distances[i].value); } else if (orderByTypes[i] == FLOAT4OID) { @@ -892,8 +892,8 @@ index_store_float8_orderby_distances(IndexScanDesc scan, Oid *orderByTypes, if (!scan->xs_orderbynulls[i]) pfree(DatumGetPointer(scan->xs_orderbyvals[i])); #endif - scan->xs_orderbyvals[i] = Float4GetDatum((float4) distanceValues[i]); - scan->xs_orderbynulls[i] = false; + if (!scan->xs_orderbynulls[i]) + scan->xs_orderbyvals[i] = Float4GetDatum((float4) distances[i].value); } else { -- cgit v1.2.3