hydromatic / morel

Standard ML interpreter, with relational extensions, implemented in Java
Apache License 2.0
291 stars 15 forks source link

Add `take` and `skip` relational operators #204

Closed julianhyde closed 6 months ago

julianhyde commented 7 months ago

Add take and skip relational operators:

For example,

from e in emps
  order e.hiredate
  skip 2 take 3

returns the 3rd, 4th and 5th employees hired.

The argument to take and skip are int expressions that are evaluated in the same environment as the from. They can, of course, be literals. But they may not reference variables from the pipeline. For example:

(*) Valid
fun topNEmployeesBySalary n =
  from e in emps
    order by sal desc take n;

(*) Invalid
from e in emps
  take e.deptno

take and skip are equivalent to LIMIT and OFFSET keywords in SQL. It is equivalent to the Postgres SQL

SELECT *
FROM Emp AS e
ORDER BY e.hiredate LIMIT 3 OFFSET 2;

or standard SQL

SELECT *
FROM Emp AS e
ORDER BY e.hiredate OFFSET 2 ROWS FETCH NEXT 3 ROWS ONLY;
julianhyde commented 7 months ago

Dev branch: https://github.com/julianhyde/morel/tree/204-skip-take

julianhyde commented 6 months ago

Note that tag https://github.com/julianhyde/morel/tree/204-skip-take.0 has some code to work with suchthat that is not present in the current dev branch. We may need to revive it before merging #202.