diff --git a/Makefile b/Makefile index 8fc69c3..c556e86 100644 --- a/Makefile +++ b/Makefile @@ -24,7 +24,7 @@ REGRESS = init tables points euler circle line ellipse poly path box index \ contains_ops contains_ops_compat bounding_box_gist gnomo healpix \ moc -REGRESS_9_5 = index_9.5 # experimental for spoint3 +REGRESS_9_5 = index_9.5 knn # experimental for spoint3 TESTS = init_test tables points euler circle line ellipse poly path box index \ contains_ops contains_ops_compat bounding_box_gist gnomo healpix \ @@ -81,11 +81,11 @@ has_explain_summary = $(if $(filter-out 9.%,$(pg_version)),y,n) # ## the use of spoint 3 is too experimental and preliminary: -#ifeq ($(pg_version_9_5_plus),y) -# REGRESS += $(REGRESS_9_5) -# TESTS += $(REGRESS_9_5) -# PGS_SQL += $(PGS_SQL_9_5) -#endif +ifeq ($(pg_version_9_5_plus),y) + REGRESS += $(REGRESS_9_5) + TESTS += $(REGRESS_9_5) + PGS_SQL += $(PGS_SQL_9_5) +endif crushtest: REGRESS += $(CRUSH_TESTS) crushtest: installcheck diff --git a/expected/knn.out b/expected/knn.out new file mode 100644 index 0000000..3cb22af --- /dev/null +++ b/expected/knn.out @@ -0,0 +1,40 @@ +CREATE TABLE points (id int, p spoint, pos int); +INSERT INTO points (id, p) SELECT x, spoint(random()*6.28, (2*random()-1)*1.57) FROM generate_series(1,314159) x; +CREATE INDEX i ON points USING gist (p spoint3); +SET enable_indexscan = true; +EXPLAIN (costs off) SELECT p <-> spoint (0.2, 0.3) FROM points ORDER BY 1 LIMIT 10; + QUERY PLAN +------------------------------------------------- + Limit + -> Index Only Scan using i on points + Order By: (p <-> '(0.2 , 0.3)'::spoint) +(3 rows) + +UPDATE points SET pos = n FROM (SELECT id, row_number() OVER (ORDER BY p <-> spoint (0.2, 0.3)) n FROM points ORDER BY p <-> spoint (0.2, 0.3) LIMIT 10) sel WHERE points.id = sel.id; +SET enable_indexscan = false; +SET enable_indexonlyscan = false; +EXPLAIN (costs off) SELECT p <-> spoint (0.2, 0.3) FROM points ORDER BY 1 LIMIT 10; + QUERY PLAN +--------------------------------------------------- + Limit + -> Sort + Sort Key: ((p <-> '(0.2 , 0.3)'::spoint)) + -> Seq Scan on points +(4 rows) + +SELECT pos, row_number() OVER (ORDER BY p <-> spoint (0.2, 0.3)) n FROM points ORDER BY p <-> spoint (0.2, 0.3) LIMIT 10; + pos | n +-----+---- + 1 | 1 + 2 | 2 + 3 | 3 + 4 | 4 + 5 | 5 + 6 | 6 + 7 | 7 + 8 | 8 + 9 | 9 + 10 | 10 +(10 rows) + +DROP TABLE points; diff --git a/sql/knn.sql b/sql/knn.sql new file mode 100644 index 0000000..e49ddad --- /dev/null +++ b/sql/knn.sql @@ -0,0 +1,12 @@ +CREATE TABLE points (id int, p spoint, pos int); +INSERT INTO points (id, p) SELECT x, spoint(random()*6.28, (2*random()-1)*1.57) FROM generate_series(1,314159) x; +CREATE INDEX i ON points USING gist (p spoint3); +SET enable_indexscan = true; +EXPLAIN (costs off) SELECT p <-> spoint (0.2, 0.3) FROM points ORDER BY 1 LIMIT 10; +UPDATE points SET pos = n FROM (SELECT id, row_number() OVER (ORDER BY p <-> spoint (0.2, 0.3)) n FROM points ORDER BY p <-> spoint (0.2, 0.3) LIMIT 10) sel WHERE points.id = sel.id; +SET enable_indexscan = false; +SET enable_indexonlyscan = false; +EXPLAIN (costs off) SELECT p <-> spoint (0.2, 0.3) FROM points ORDER BY 1 LIMIT 10; +SELECT pos, row_number() OVER (ORDER BY p <-> spoint (0.2, 0.3)) n FROM points ORDER BY p <-> spoint (0.2, 0.3) LIMIT 10; +DROP TABLE points; +