Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create rum index without "WITH" cause an error when select use “ORDER BY” #118

Closed
Wenbo94 opened this issue Jul 28, 2023 · 1 comment

Comments

@Wenbo94
Copy link

Wenbo94 commented Jul 28, 2023

Use postgres REL_12_STABLE branch

postgres=# CREATE TABLE tsts (id int, t tsvector, d timestamp);
CREATE TABLE
postgres=# \copy tsts from '/rum/data/tsts.data'
COPY 508
postgres=# CREATE INDEX tsts_idx ON tsts USING rum (t rum_tsvector_addon_ops, d);
CREATE INDEX
postgres=# SELECT id, d, d <=> '2016-05-16 14:21:25' FROM tsts WHERE t @@ 'wr&qh' ORDER BY d <=> '2016-05-16 14:21:25' LIMIT 5;
ERROR: cannot order without attribute 2 in WHERE clause
postgres=# DROP INDEX tsts_idx;
DROP INDEX
postgres=# SELECT id, d, d <=> '2016-05-16 14:21:25' FROM tsts WHERE t @@ 'wr&qh' ORDER BY d <=> '2016-05-16 14:21:25' LIMIT 5;
id | d | ?column?
-----+----------------------------+---------------
355 | 2016-05-16 14:21:22.326724 | 2.673276
354 | 2016-05-16 13:21:22.326724 | 3602.673276
371 | 2016-05-17 06:21:22.326724 | 57597.326724
406 | 2016-05-18 17:21:22.326724 | 183597.326724
415 | 2016-05-19 02:21:22.326724 | 215997.326724
(5 rows)

@sokolcati
Copy link
Contributor

sokolcati commented Oct 24, 2024

Hi! This is the expected behavior of the index.

In the case of WITH, we attach one attribute to another, so if you specify one of the columns in ORDER BY, the sort order will be chosen unambiguously.
If the index is created on several fields without attach-to, then you will need to specify the remaining fields in ORDER BY. In the case of your example, you need to add the t-attribute after d <=> '2016-05-16 14:21:25':

SELECT id, d, d <=> '2016-05-16 14:21:25' FROM tsts WHERE t @@ 'wr&qh'
ORDER BY d <=> '2016-05-16 14:21:25', t LIMIT 5;

This query works fine with an index created without WITH.

But I agree that the error message is misleading. The second field is expected not in WHERE , but in ORDER BY . The error text will be changed in the #133.

@Wenbo94 Wenbo94 closed this as completed Oct 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants