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