8000 Add `current` keyword, representing the current row in a query · Issue #265 · hydromatic/morel · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Add current keyword, representing the current row in a query #265
Closed
@julianhyde

Description

@julianhyde

Add current keyword, representing the current row in a query.

The current row of a query is either a record or an atomic value. (It is a record if the current step is downstream of from, join, through, group, or yield with a record constructor; an atomic value if downstream of yield; other steps, such as order, distinct, where, propagate the type.)

At any step in the query, there are a number of current fields. If the type is a record, these are the fields of the record. If the type is atomic, there one field, which may or may not have a name. (A field called f, if the upstream yield is something like yield f or yield r.f, otherwise an anonymous field for cases like yield i + j.)

(*) Atom, one field 'e'
from e in emps yield e;

(*) Atom, one field 'deptno'
from e in emps yield e.deptno;

(*) Atom, one anonymous field
from e in emps yield e.deptno + 10;

(*) Record, two fields 'deptno' and 'sal'
from e in emps yield {e.deptno, e.sal};

(*) Record, no fields
from e in emps yield {};

(*) Record, one field 'x'
from e in emps yield {x = e.deptno + 10};

(*) Record, eight fields, 'empno', 'deptno', 'sal' etc.
from e in emps yield {e with sal = sal * 2.0};

(*) Record, one field 'e'
from e in emps yield {e = {e with sal = sal * 2.0}};

(*) Record, one field 'i'
from i in [1, 2, 3];

(*) Record, two fields 'i' and 'j'
from i in [1, 2, 3],
    j in [2, 3, 5];

(*) Atom, one anonymous field
from i in [1, 2, 3]
    j in [2, 3, 5]
  yield i + j;

Suppose we want to sort the rows of the last query above in descending order. How should we refer to the current value in an expression? We propose current as the syntax to do this:

(* Atom, one anonymous field, but you can still refer
   to the row using `current`. *)
from i in [1, 2, 3],
    j in [2, 3, 5]
  yield i + j
  order current desc;

You can also use current in an expression:

from i in [1, 2, 3],
    j in [2, 3, 5]
  yield i + j
  order current mod 2, current;

If the current row is an atom with a name, current has the same meaning as the name. For example, the following two queries are equivalent:

(*) Atom, one field 'deptno'
from e in emps
  yield e.deptno
  order deptno desc;

(*) Atom, one field 'deptno'
from e in emps
  yield e.deptno
  order current desc;

If the current row is a record (even a record with zero or one fields), current has a record type:

(*) Record, eight fields, 'empno', 'deptno', 'sal' etc.
from e in emps
  yield {e with sal = sal * 2.0}
  order current.deptno;

In the implementation, we are now assured that atomic rows always have a field name. In the previous "Atom, one anonymous field" cases, the field now has a name, and that name is "current". In all other cases, current is expanded to an expression: either the real field name of an atom, or a record constructor like {empno, deptno, sal} for a record.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0