View Ticket
Not logged in
Ticket Hash: 56510318e25e7bcb93227fa809ecd84d964b5b1f
Title: Discrepancy with max_items in KNN function
Status: Closed Type: Code_Defect
Severity: Critical Priority: Immediate
Subsystem: Resolution: Fixed
Last Modified: 2017-04-27 11:24:30
Version Found In: 4.5.0-dev
User Comments:
mj10777 added on 2016-09-20 14:13:21:

This is, possibly, the same problem as shown in:

    https://www.gaia-gis.it/fossil/libspatialite/info/000b0fb3fd
However the samples used here cannot be reproduced.

Based on a Database that has
    251692 houses and 20956 streets-segments
a query that should bring 1 result
    a house with a distance 0 from a street segment
brought no results when using max_items=1

A new database was created with 2 tables:
    'area' table with 2 street-segments of 1 street
      the houses of that street
    'main' table with 24 street-segments of 7 street (which include the first street)
      the houses of those streets

A query was worked out that should bring 2 results
    House 33 as ref_geometry should show the distance between the 2 segments it belongs to ('Nollendofstraße')
      0 meters to the first, 75.3553546416035 to the second

If I have understood the logic correctly, the following sql should bring the correct results:
  SELECT
   k.distance,
   name,
   id_admin,
   id_flur
  FROM 
   berlin_alkis_flurstuecke_area admin_11 JOIN knn k
   ON k.fid=admin_11.rowid
  WHERE
  (
   k.f_table_name='berlin_alkis_flurstuecke_area' AND
   k.f_geometry_column='polygon_25833' AND
   k.ref_geometry=(SELECT polygon_25833 FROM berlin_alkis_flurstuecke_area WHERE (berlin_alkis_flurstuecke_area.admin_level IN (12)) AND id_street_nr=1200118099) AND
   k.max_items=2 AND
   admin_11.id_admin=(SELECT id_belongs_to FROM berlin_alkis_flurstuecke_area WHERE (berlin_alkis_flurstuecke_area.admin_level IN (12)) AND id_street_nr=1200118099) AND
   admin_11.admin_level IN (11)
  )
  GROUP BY k.pos
  HAVING k.distance<1000;
It does not.

For the 'area' table
    k.max_items=14 will show the correct 2 results
    k.max_items=3-13 will show only 1 result
    k.max_items=1-2 will show no results

For the 'main' table (replacing only the table-name)
    k.max_items=38 will show the correct 2 results
    k.max_items=8-37 will show only 1 result
    k.max_items=1-7 will show no results

k.max_items=14
-III-> vknn_query_callback max_items[14] curr_items[14] rowid[169171]: SELECT ST_Distance(90.76455, BuildMbr(388181.96000, 5817638.63600, 388234.29900, 5817699.54400));
0.0|Nollendorfstraße|1811003258|167454
75.3553546416035|Nollendorfstraße|1811003258|169171
Here the rowid 169171 turns up, being 75.3553546416035 meters away (searching in all cases for < 1000)
    ctx->max_dist at this point is 90.76455

k.max_items=13
-III-> vknn_query_callback max_items[13] curr_items[13] rowid[169171]: SELECT ST_Distance(73.04606, BuildMbr(388181.96000, 5817638.63600, 388234.29900, 5817699.54400));
0.0|Nollendorfstraße|1811003258|167454
Here the rowid 169171 should turn up, being 75.3553546416035 meters away (searching in all cases for < 1000)
    ctx->max_dist at this point is 73.04606

This leads to:
-E-> vknn_update_items 02a: rowid[169171] distance[75.35535] >= max_dist[73.04606]

This looks as if ctx->max_dist is not receiving the value used in the HAVING statement.
    it is not clear at all (to me at least), where ctx->max_dist gets it's value

It seems strange that the combination of records and k.max_items influences the result.

I have no idea if this is the cause of the original problem
    but only after this is resolved, can we be sure.

I will send the created test-database with sql per mail.

valgrind is also unhappy, see:
    valgrind.select.area.max_item_13.txt
A quick look showed me nothing obvious.


sandro added on 2016-12-10 13:37:44:
same cause as reported on ticket: 000b0fb3fd

expected to be now resolved by the latest KNN implementation

mj10777 (claiming to be mj10777@googlemail.com) added on 2016-12-11 06:56:41:

I cannot confirm that his problem has been resolve with above shown sample.
I still receive only 1 value returned when 2 are expected

    when using max_items=13
When using max_items=14: the expected result comes
valgrind --leak-check=full --show-leak-kinds=all --log-file=valgrind.select.area.max_item_13.txt spatialite berlin_alkis_build_nolle_umgebung.db < select.area.max_item_13.sql

2016-12-11 06:37:24|[1 result, false] max_items=13 [37] searching for specific house on 2 street segemets ; 1st distance=0 ; Not listed: 2nd distance=75.355355
0.0|Nollendorfstraße|1811003258|167454

missing is:
75.3553546416035|Nollendorfstraße|1811003258|169171


Also valgrind us still unhappy:
    The first and third (528,531) is with a strlen command.
    The second is on a malloc for the 'real_table'.
    The fourth is on a malloc for the 'real_geom'.
==12284== 
==12284== Invalid read of size 4
==12284==    at 0x4F72C3F: vknn_init_context (virtualknn.c:528)
==12284==    by 0x4F72C3F: vknn_filter (virtualknn.c:1582)
==12284==    by 0x5BDE197: sqlite3VdbeExec (sqlite3.c:73475)
==12284==    by 0x5BE488E: sqlite3Step (sqlite3.c:66192)
==12284==    by 0x5BE488E: sqlite3_step (sqlite3.c:66258)
==12284==    by 0x5BDB659: sqlite3_exec (sqlite3.c:96070)
==12284==    by 0x40B3A5: process_input (shell.c:4836)
==12284==    by 0x403DF3: main (shell.c:5544)
==12284==  Address 0x873faec is 28 bytes inside a block of size 30 alloc'd
==12284==    at 0x4C2ABA0: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==12284==    by 0x4F7279C: vknn_check_rtree (virtualknn.c:963)
==12284==    by 0x4F7279C: vknn_filter (virtualknn.c:1540)
==12284==    by 0x5BDE197: sqlite3VdbeExec (sqlite3.c:73475)
==12284==    by 0x5BE488E: sqlite3Step (sqlite3.c:66192)
==12284==    by 0x5BE488E: sqlite3_step (sqlite3.c:66258)
==12284==    by 0x5BDB659: sqlite3_exec (sqlite3.c:96070)
==12284==    by 0x40B3A5: process_input (shell.c:4836)
==12284==    by 0x403DF3: main (shell.c:5544)
==12284== 
==12284== Invalid read of size 4
==12284==    at 0x4F72C92: vknn_init_context (virtualknn.c:531)
==12284==    by 0x4F72C92: vknn_filter (virtualknn.c:1582)
==12284==    by 0x5BDE197: sqlite3VdbeExec (sqlite3.c:73475)
==12284==    by 0x5BE488E: sqlite3Step (sqlite3.c:66192)
==12284==    by 0x5BE488E: sqlite3_step (sqlite3.c:66258)
==12284==    by 0x5BDB659: sqlite3_exec (sqlite3.c:96070)
==12284==    by 0x40B3A5: process_input (shell.c:4836)
==12284==    by 0x403DF3: main (shell.c:5544)
==12284==  Address 0x873fb3c is 12 bytes inside a block of size 14 alloc'd
==12284==    at 0x4C2ABA0: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==12284==    by 0x4F727EF: vknn_check_rtree (virtualknn.c:969)
==12284==    by 0x4F727EF: vknn_filter (virtualknn.c:1540)
==12284==    by 0x5BDE197: sqlite3VdbeExec (sqlite3.c:73475)
==12284==    by 0x5BE488E: sqlite3Step (sqlite3.c:66192)
==12284==    by 0x5BE488E: sqlite3_step (sqlite3.c:66258)
==12284==    by 0x5BDB659: sqlite3_exec (sqlite3.c:96070)
==12284==    by 0x40B3A5: process_input (shell.c:4836)
==12284==    by 0x403DF3: main (shell.c:5544)
==12284== 


mj10777 added on 2016-12-11 07:00:47:

Also when max_items=2

    no results are returned
Again 2 are expected.


mj10777 added on 2017-04-19 06:17:39:

Based on the ticket

    where the given sample 'JOIN' brings the correct result
I tested this sample again and did not receive the correct results as shown above.


mj10777 added on 2017-04-25 07:15:05:

Based on the commit of 2017-04-24, the above query still does not bring the correct result.

All other samples, reported by users in the last year, bring the expected results.


sandro added on 2017-04-27 11:24:30:
the previous implementation was broken beyond any possible repair, so I've completely refactored the KNN module by adopting a radically different strategy:

step #1
-------
we'll descend the Tree by exploring a Level at each time so to identify the lower-level BBOX containing the reference Geometry. if no BBOX directly contains the reference Geometry then the nearest one will be identified.

step #2
-------
the reference Geometry will now be buffered so to completely cover the BBOX identified in the previous step.

step #3
-------
an ordinary Spatial Index query will be now performed, using the buffered Geometry as the search frame, so to identify all nearest features.

step #4
-------
if the total number of nearest features identified by the previous query is less than requested, then the buffer radius will be repeatedly expanded until all requested nearest features will be found.

the new approach seems to definitively resolve all known issues.

hint: using the new KNN implementation I was able to notice at least one case in which a noticeable improvement was introduced.

https://www.gaia-gis.it/fossil/libspatialite/wiki?name=KNN
paragraph: A third sample of KNN query

now the KNN query correctly identifies that Port Stanley and Mount Pleasant (Falkland Islands) are the nearest airports, which seems encouraging.
(I'll adjust the test sample and figure ASAP)