Skip to content

Pipelines, groups, prql fmt and semantic rewrite - #398

Merged
aljazerzen merged 27 commits into
mainfrom
group
Apr 23, 2022
Merged

aljazerzen merged 27 commits into
mainfrom
group

Conversation

@aljazerzen

Copy link
Copy Markdown
Member

I started to work on #300, broke most of the tests and then repeatably found out I needed just one more refactor/feature before tests start working again. Result is this branch with 3441 insertions(+) and 2785 deletions(-).

Work done:

  • Parse group transforms, their pipelines and infer context within these pipelines Orthogonal treatment of groups (nee Window functions) #300,
  • Unify pipeline and inline-pipeline. This includes parsing transforms as function calls, that are determined to be pipeline transforms only during resolving. Before that, they are FunCall nodes in AST.
  • Do resolving in a single pass. This decouples the code a bit and will be beneficial for other uses such as polars backend and language server. It was a major rewrite of ::semantic, but I'm happier with it now. It's not exacly readable yet, but it's a step closer.
  • For debugging reasons, I added Display trait to Item, which means that we can print PRQL queries. Combined with parsing, this is a formatter and a step toward Transpiling from SQL to PRQL #130 . Can be used as prql compile -f fmt (now I see this should be a separate command).
  • Refactor ast and sql modules (including ast::dialect). Just splitting ~1000 lines into ~3x300 lines.

Language changes:

  • aggregate must have two positional args, it won't default to empty list

There is a test case that has not been fixed yet, see below.

max-sixty and others added 23 commits April 15, 2022 17:31
I'm planning to put a `dialect.rs` here
language changes:
- aggregate must have two positional args, it won't default to empty
  list
- transforms cannot use named exprs without braces or parenthesis: `select [a: b + c]`
language changes:
- function calls within expressions must be wrapped by ()
language changes:
- transforms can use named exprs without braces or parenthesis again: `select a: b + c`
@aljazerzen
aljazerzen requested a review from max-sixty April 22, 2022 10:51
table_0.ct,
countries.*,
country_code,
true AS always_true,

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't parse boolean literals yet, so this was parsed as ident. Which now got resolved :D

Comment on lines +34 to +39
AVG(salary),
AVG(salary + payroll_tax),
SUM(salary),
SUM(salary + payroll_tax),
AVG(salary + payroll_tax + benefits_cost),
sum_gross_cost,

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still has to be fixed, but can I do it a separate PR?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's wrong with it now? That there could be a countries.sum_gross_cost?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That and the fact that these aggregation functions should not be here. Instead there should be columns referenced by name.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, sorry, I was myopically looking at just that line. Makes sense now

Comment thread prql-compiler/src/prql.pest Resolved
@max-sixty

Copy link
Copy Markdown
Member

This looks extremely impressive @aljazerzen !!

I will look in more detail later today

Comment thread prql-compiler/src/prql.pest Resolved
inline_pipeline = { "(" ~ expr ~ (pipe ~ func_curry)+ ~ ")" }

ident = @{ !operator ~ (ASCII_ALPHA | "$") ~ (ASCII_ALPHANUMERIC | "." | "_" | "*" )* }
ident = @{ !operator ~ !keyword ~ (ASCII_ALPHA | "$") ~ (ASCII_ALPHANUMERIC | "." | "_" | "*" )* }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ident = @{ !operator ~ !keyword ~ (ASCII_ALPHA | "$") ~ (ASCII_ALPHANUMERIC | "." | "_" | "*" )* }
// `$` is for SQL params
ident = @{ !operator ~ !keyword ~ (ASCII_ALPHA | "$") ~ (ASCII_ALPHANUMERIC | "." | "_" | "*" )* }

operator_unary = ${ "-" }
operator_mul = ${ "*" | "/" | "%" }
operator_add = ${ "+" | "-" }
operator_compare = ${ "==" | "!=" | "=" | ">" | "<" | ">=" | "<=" }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess if we're doing these properly now, we could exclude ==? (or close and we can assess separately if this isn't unambiguous)

Suggested change
operator_compare = ${ "==" | "!=" | "=" | ">" | "<" | ">=" | "<=" }
operator_compare = ${ "!=" | "=" | ">" | "<" | ">=" | "<=" }

}

/// Loads `internal.prql` which contains type definitions of transforms
pub fn init_context() -> Context {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WDYT about making this the impl for Default?


Zooming out: one thing I worry about is that as the code gets more powerful, it becomes more coupled — it's harder to trace through how a simple query gets parsed, resolved & translated.

I'll think more about this. To some extent, the complication is an inevitable cost of progress, but sometimes there are things we can do so that the simple case is more obvious — possible making default impls is one (I even went through a phase a month ago of reorganizing files so the important functions were at the top of files, as an example of another).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had that as a default trait, but default is usually used for a few assignes to fields and that's it.

This function actually parses a prql file with type definition for transforms, so it feelt too big for a drfault trait. Note that you dont have to call it yourself, it will get called if you pass contex = None to resolve function.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that modules should be decupled and should have one major function that you can call, and thats it. Thats the objective, that we currently slightly diviate from.

}

/// Traverses AST and replaces transforms with nested pipelines with the pipeline
struct UnGrouper {}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm trying to think of a better name, so far unsuccessfully...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UnNester? DeNester? Flatten?

@@ -1,164 +0,0 @@
use insta::assert_snapshot;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How come we lost these test files? Is it that they're redundant given our unit tests?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah, I was fixing these tests and noticed that they are duplicates with unit tests and reference tests.

There is no reason to have duplicate tests if we have to maintain them over langugae changes

.comparison {
table-layout: fixed;
display: flex;
justify-content: center;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do these make any practical difference? (I have no strong bond to the existing JS; I'm curious because I'm trying to learn how to make these better)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a container div, so the editor can be wider than the content column.

This is css for centering the table, even when it overflows.

}

fn separate_pipeline(query: &Query) -> Result<(Vec<Table>, Vec<Node>, Vec<Pipeline>)> {
fn into_tables(nodes: Vec<Node>) -> Result<Vec<Table>> {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The convention has been tables_of_nodes, though no strong view on the exact convention; it is nice to have one though

Ok(match item {
Item::Expr(_) | Item::SString(_) | Item::FString(_) | Item::Ident(_) | Item::Raw(_) => {
SelectItem::UnnamedExpr(TryInto::<Expr>::try_into(item)?)
SelectItem::UnnamedExpr(Expr::try_from(item)?)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does seem nicer...

@max-sixty

Copy link
Copy Markdown
Member

Overall — very impressive! Another great step forward.

I've reviewed:

  • The high-level design
    • Unifying the funcs & transforms is really cool, very nice job
    • The group approach seems excellent
  • The tests
  • Some specific parts that I went deeper on as I tried to review how the new flow works
    • I get most of semantic; I need to spend more time with it. Running it as a single pass sounds great
    • Super re adding Display. We'll need to do things like include comments for it to be a full formatter.

I don't have particularly helpful code suggestions at the moment; I'll continue going through the code after we merge, and will engage more with it as I'm adding features.

As I wrote above, I will reflect on keeping the codebase as simple and modular as possible.

I didn't quite get why the test needed fixing, if it's as simple as qualifying a preceding CTE, then that can definitely go into another PR.

Good to go from my end, thank you @aljazerzen !

@max-sixty
max-sixty self-requested a review April 23, 2022 03:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants