Skip to content
Snippets Groups Projects
Commit a7bbd70b authored by Nikita Pettik's avatar Nikita Pettik Committed by Kirill Yukhin
Browse files

sql: re-enable ORDER BY field filter optimization

When we replaced SQLite's ephemeral spaces with our ones, one
optimization concerning ORDER BY clause was disabled. It allows to
reduce number of fields in format of ephemeral space or sorter table.
To illustrate how it works, consider example:

CREATE TABLE t (id INT PRIMARY KEY, b INT);
SELECT * FROM t ORDER BY b;

To sort entries from t, ephemeral space with format [b, id, b] is
created. One can see, that such format contains duplicate of b column.
To avoid such situation, SQLite provides optimization which removes
duplicates. Meanwhile, it doesn't change already set format of ephemeral
(or sorter) space (and SQLite tolerates that format difference). That's
why it was turned off. However, such optimization turns out to be not
optional but required: some column values shouldn't be computed twice.
For instance:

SELECT random() AS x FROM t ORDER BY x;

Without filtering fields from ephemeral space format, it would be like:
[random(), random()]. In other words, results would be sorted by first
call to random() function, but resulting set would consist of values
given by second call of random(). So, to enable it, we should reduce
field count in format of ephemeral space by number of matches between
SELECT and ORDER BY column lists.

Also, type of return value for random() function has been fixed.

Closes #3783
parent 5be640a6
No related merge requests found
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment