Skip to content

Resolve partiton, window and sort of ast::TransformCall #1044

Closed

Description

@aljazerzen

ast::TransformCall is just as ast::FuncCall, but after resolving when we have figured out that this function call works on a table.

It has a few properties that start empty:

  • partition,
  • window and
  • sort.

Before lowering, they need to be populated. Here is a few examples of what should they contain:

from employees
sort salary
derive salary_rank = rank  # sort should be set to vec![(salary, ASC)]
from employees
group dept_id (
  derive salary_rank = rank  # partition should be set to vec![dept_id]
)
from employees
window rolling:true (
  derive [cum_salary = sum salary]  # window should be set
)

This can be accomplished by creating a new struct that implements AstFold and keeps current state of the 3 values.

There is some code for deriving window property from the TransformKind::Window commeted out in transforms.rs.

For more requirements of how this should behave, check out this test: https://github.com/prql/prql/blob/f42412a8a4fdef1423b8afdbb1c484aaf02e4756/prql-compiler/src/lib.rs#L381-L504

For debugging this, you will probably need #1043

Activity

  1. added
    good first issue
    on Oct 18, 2022
  2. self-assigned this
    on Oct 23, 2022
  3. max-sixty commented on Oct 23, 2022

    @max-sixty
    Member

    I'll take this

  4. max-sixty commented on Oct 23, 2022

    @max-sixty
    Member

    @aljazerzen can you confirm my understanding is correct?

    Take the first case:

    from employees
    sort salary
    derive salary_rank = rank  # sort should be set to vec![(salary, ASC)]

    This has a debug output from pbpaste | cargo run -p prql-compiler -- debug of:

    - Pipeline:
        TransformCall:
          kind: !Derive
            assigns:
            - Ident: salary_rank
            tbl:
              TransformCall:
                kind: !Sort           # Is this not the right place for the sort described above?
                  by:
                  - direction: Asc
                    column:
                      Ident: salary
                  tbl:
                    TransformCall:
                      kind: !From
                        Ident: employees
                      partition: []
                      window:
                        kind: Rows
                        range:
                          start: null
                          end: null
                      sort: []       # Should it be here? Is that what you mean by "we have figured out that this function call works on a table."?
                partition: []
                window:
                  kind: Rows
                  range:
                    start: null
                    end: null
                sort: []         # Or here?
          partition: []
          window:
            kind: Rows
            range:
              start: null
              end: null
          sort: []          # Or even here?? :)

    ...none of them are on window, where I presume they'll end up in SQL, but I'm guessing that comes later in the translation process.

  5. aljazerzen commented on Oct 24, 2022

    @aljazerzen
    MemberAuthor

    The sort should be set on at your third and fourth comment, because these two sort attributes belong to sort and derive, respectively.

    We need this sort attribute set for all transforms, because some of them will need it afterwards (for example derive if contains window functions and take X, which may be converted derive row_number = rank | filter row_number < X).

    Did I understand your question correctly?

  6. max-sixty commented on Oct 24, 2022

    @max-sixty
    Member

    OK great. Does this mean the diff should be:

    diff --git a/initial.yaml b/proposed.yaml
    index 831eae99..e0a247fe 100644
    --- a/initial.yaml
    +++ b/proposed.yaml
    @@ -27,11 +27,11 @@
                   range:
                     start: null
                     end: null
    -            sort: []
    +            sort: [(salary, ASC)]
           partition: []
           window:
             kind: Rows
             range:
               start: null
               end: null
    -      sort: []
    +      sort: [(salary, ASC)]

    Do we also replace the Sort transform with the From, like:

    diff --git a/initial.yaml b/proposed.yaml
    index 831eae99..e14c33d5 100644
    --- a/initial.yaml
    +++ b/proposed.yaml
    @@ -5,33 +5,19 @@
               - Ident: salary_rank
             tbl:
               TransformCall:
    -            kind: !Sort
    -              by:
    -                - direction: Asc
    -                  column:
    -                    Ident: salary
    -              tbl:
    -                TransformCall:
    -                  kind: !From
    -                    Ident: employees
    -                  partition: []
    -                  window:
    -                    kind: Rows
    -                    range:
    -                      start: null
    -                      end: null
    -                  sort: []
    +            kind: !From
    +              Ident: employees
                 partition: []
                 window:
                   kind: Rows
                   range:
                     start: null
                     end: null
    -            sort: []
    +            sort: [(salary, ASC)]
           partition: []
           window:
             kind: Rows
             range:
               start: null
               end: null
    -      sort: []
    +      sort: [(salary, ASC)]

    Thanks!

  7. aljazerzen commented on Oct 25, 2022

    @aljazerzen
    MemberAuthor

    Yeah that's it exactly.

    We don't need to remove Sort, it will still be needed to for constructing ORDER BY. It's just that it has additional effects on some transforms.

  8. aljazerzen commented on Oct 26, 2022

    @aljazerzen
    MemberAuthor

    I few things: I underestimated difficulty of this task. When I wanted to explain it better, I realized that it's too hard and when I tried to get started on it I found some undocumented bits that would block anyone who hasn't written that code.

    A few things:

    • I've cleaned-up serialization of TransformCall, so default values don't get serialized.
    • This task should also handle flattening of group/window transforms.
    • Having more than one transfrom in inner pipeline of group/window does not work currently, but is out of scope of this task.

    I've written the framework of how it should work on branch semantic-flattener. Group works, it assigns partition to all inner transforms (or rather the only inner transfrom, because of the bug mentioned).

    Sort is prepared to be done, comments on Flattener give more info on that.
    Window is not done yet, but should be similar to group.


    Test case: sort

    from employees
    sort salary
    derive salary_rank = rank

    cargo run debug has been changed as:

      - Pipeline:
          TransformCall:
            kind: !Derive
              assigns:
              - SString:
                - !String RANK()
              tbl:
                TransformCall:
                  kind: !Sort
                    by:
                    - direction: Asc
                      column:
                        Ident:
                          namespace: null
                          name: salary
                    tbl:
                      TransformCall:
                        kind: !From
                          Ident:
                            namespace: null
                            name: employees
    +             sort:
    +             - direction: Asc
    +               column:
    +                 Ident:
    +                   namespace: null
    +                   name: salary
    +       sort:
    +       - direction: Asc
    +         column:
    +           Ident:
    +             namespace: null
    +             name: salary

    Test case: group

    So for:

    from employees
    group [emp_no, gender] (
        aggregate [
            emp_salary = average salary
        ]
    )
    

    ... cargo run debug has been changed from ...

    - Pipeline:
        TransformCall:
          kind: !Group
            by:
            - Ident:
                namespace: null
                name: emp_no
            - Ident:
                namespace: null
                name: gender
            pipeline:
              Closure:
                name: null
                body:
                  TransformCall:
                  kind: !Aggregate
                    assigns:
                    - SString:
                      - !String AVG(
                      - !Expr
                        Ident:
                          namespace: null
                          name: salary
                      - !String )
                    tbl:
                      Ident:
                        namespace: null
                        name: _group_tbl
              args: []
              params:
              - name: _group_tbl
                default_value: null
              named_args: []
              named_params: []
              env: {}
          tbl:
            TransformCall:
              kind: !From
                Ident:
                  namespace: null
                  name: employees

    ... to ...

    - Pipeline:
        TransformCall:
          kind: !Aggregate
            assigns:
            - SString:
              - !String AVG(
              - !Expr
                Ident:
                  namespace: null
                  name: salary
              - !String )
            tbl:
              TransformCall:
                kind: !Join
                  side: Inner
                  with:
                    Ident:
                      namespace: null
                      name: salaries
                  filter:
                    Binary:
                      left:
                        Ident:
                          namespace: _frame
                          name: emp_no
                      op: Eq
                      right:
                        Ident:
                          namespace: _right
                          name: emp_no
                  tbl:
                    TransformCall:
                      kind: !From
                        Ident:
                          namespace: null
                          name: employees
          partition:
          - Ident:
              namespace: null
              name: emp_no
          - Ident:
              namespace: null
              name: gender

    Test case: window

    from employees
    window expanding:true (
        derive [cumsum = sum salary]
    )
    

    Derive should have window set.

  9. max-sixty commented on Nov 9, 2022

    @max-sixty
    Member

    Thanks for completing this @aljazerzen — sorry this was another one you ended up doing yourself. I'm reading through your code now so at least I'm keeping pace.

  10. aljazerzen commented on Nov 10, 2022

    @aljazerzen
    MemberAuthor

    Don't feel bad, because as I said, this turned out to be more complex than I expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

good first issue

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions