7
7
* array.sql and array_1.sql
8
8
* --------------------
9
9
* Test output for 64-bit and 32-bit systems respectively.
10
- *
11
- * --------------------
12
- * array_2.sql and array_3.sql
13
- * --------------------
14
- * Since 6ed83d5fa55c in PostgreSQL 17, the order of rows
15
- * in the output has been changed.
16
10
*/
17
11
set enable_seqscan=off;
18
12
set enable_sort=off;
@@ -859,41 +853,71 @@ EXPLAIN (COSTS OFF) SELECT * FROM test_array WHERE i % '{}';
859
853
DROP INDEX idx_array;
860
854
/*
861
855
* Check ordering using distance operator
856
+ *
857
+ * We want to check that index scan provides us correct ordering by distance
858
+ * operator. File 'data/rum_array.data' contains two arrays that statisfy
859
+ * i @> '{23,20}' and have finite distance i <=> '{51}', and a bunch of arrays
860
+ * that statisfy i @> '{23,20}' and have infinite distance i <=> '{51}'.
861
+ *
862
+ * When ordering by distance the order of this bunch of arrays with infinite
863
+ * distance is not determined and may depend of PostgreSQL version and system.
864
+ * We don't add another sort expression to ORDER BY because that might cause
865
+ * the planner to avoid using the index. Instead, we replace arrays that have
866
+ * infinite distance with {-1} to unambiguously determine the test output.
867
+ *
868
+ * 'Infinity' is printed differently in the output in different PostgreSQL
869
+ * versions, so we replace it with -1.
862
870
*/
863
871
CREATE TABLE test_array_order (
864
872
i int2[]
865
873
);
866
874
\copy test_array_order(i) from 'data/rum_array.data';
867
875
CREATE INDEX idx_array_order ON test_array_order USING rum (i rum_anyarray_ops);
876
+ /*
877
+ * Check that plan of the query uses ordering provided by index scan
878
+ */
868
879
EXPLAIN (COSTS OFF)
869
- SELECT *, i <=> '{51}' from test_array_order WHERE i @> '{23,20}' order by i <=> '{51}';
870
- QUERY PLAN
871
- ------------------------------------------------------
872
- Index Scan using idx_array_order on test_array_order
873
- Index Cond: (i @> '{23,20}'::smallint[])
874
- Order By: (i <=> '{51}'::smallint[])
875
- (3 rows)
880
+ SELECT
881
+ CASE WHEN distance = 'Infinity' THEN '{-1}'
882
+ ELSE i
883
+ END i,
884
+ CASE WHEN distance = 'Infinity' THEN -1
885
+ ELSE distance::numeric(18,14)
886
+ END distance
887
+ FROM
888
+ (SELECT *, (i <=> '{51}') AS distance
889
+ FROM test_array_order WHERE i @> '{23,20}' ORDER BY distance) t;
890
+ QUERY PLAN
891
+ ------------------------------------------------------------
892
+ Subquery Scan on t
893
+ -> Index Scan using idx_array_order on test_array_order
894
+ Index Cond: (i @> '{23,20}'::smallint[])
895
+ Order By: (i <=> '{51}'::smallint[])
896
+ (4 rows)
876
897
877
- SELECT i,
898
+ SELECT
899
+ CASE WHEN distance = 'Infinity' THEN '{-1}'
900
+ ELSE i
901
+ END i,
878
902
CASE WHEN distance = 'Infinity' THEN -1
879
903
ELSE distance::numeric(18,14)
880
904
END distance
881
905
FROM
882
906
(SELECT *, (i <=> '{51}') AS distance
883
- FROM test_array_order WHERE i @> '{23,20}' ORDER BY i <=> '{51}' ) t;
907
+ FROM test_array_order WHERE i @> '{23,20}' ORDER BY distance ) t;
884
908
i | distance
885
909
---------------------+------------------
886
910
{20,23,51} | 1.73205080756888
887
911
{33,51,20,77,23,65} | 2.44948974278318
888
- {23,76,34,23,2,20} | -1
889
- {20,60,45,23,29} | -1
890
- {23,89,38,20,40,95} | -1
891
- {23,20,72} | -1
892
- {73,23,20} | -1
893
- {6,97,20,89,23} | -1
894
- {20,98,30,23,1,66} | -1
895
- {57,23,39,46,50,20} | -1
896
- {81,20,26,22,23} | -1
897
- {18,23,10,90,15,20} | -1
912
+ {-1} | -1
913
+ {-1} | -1
914
+ {-1} | -1
915
+ {-1} | -1
916
+ {-1} | -1
917
+ {-1} | -1
918
+ {-1} | -1
919
+ {-1} | -1
920
+ {-1} | -1
921
+ {-1} | -1
898
922
(12 rows)
899
923
0 commit comments