GlareDB / glaredb

GlareDB: An analytics DBMS for distributed data
https://glaredb.com
MIT License
722 stars 41 forks source link

`LIMIT` results not equivalent to `FETCH` #967

Open greyscaled opened 1 year ago

greyscaled commented 1 year ago

Context

In the SQL standard, FETCH is accepted (not LIMIT). Postgres supports both and they're both equivalent.

GlareDB parses/understands FETCH, but doesn't actually apply the limits.

So for example, if I make a syntax error:

select distinct name from pg_db.public.users order by name fetch;

I get:

sql parser error: Expected one of FIRST or NEXT, found: ;

Expected

select distinct name
from pg_db.public.users
order by name asc
fetch first row only;

and

select distinct name
from pg_db.public.users
order by name asc
limit 1;

both return the same result:

name
------
Ashley McClelland

Actual

select distinct name
from pg_db.public.users
order by name asc
limit 1;

select distinct name
from pg_db.public.users
order by name asc
fetch first row only;
name
------
Ashley McClelland

name
------
Ashley McClelland
Clint Kadera
Garrett Delfosse
Grey Barkans
Sean Smith
Vaibhav Rabber

Impact

Because that FETCH is understood by postgres, and is more sql standard, it's likely a customer or user will have a prebaked query/script somewhere that uses it.

What's worse is that we don't throw any errors (but do throw errors if the syntax isn't parsed correctly), but just simply don't apply the limit.

greyscaled commented 1 year ago

For further reference, see LIMIT Clause in https://www.postgresql.org/docs/current/sql-select.html

greyscaled commented 1 year ago

Would need to contrib to DF