Skip to content

feat: incremental resolve, IR & type checking - #593

Merged
aljazerzen merged 37 commits into
mainfrom
semantic
Nov 27, 2022
Merged

aljazerzen merged 37 commits into
mainfrom
semantic

Conversation

@aljazerzen

@aljazerzen aljazerzen commented Jun 10, 2022 •

Copy link
Copy Markdown
Member

TODO:

  • figure out how to reference tables
  • move transform casting after type and name resolving

@aljazerzen

aljazerzen commented Jun 19, 2022 •

Copy link
Copy Markdown
Member Author

TODO:

  • name resolving for double references in join (JOIN USING must reference two tables)
  • fix wrap_into_window
  • clear scope at select & aggregate

@aljazerzen aljazerzen changed the title Semantic Type checking Jun 19, 2022
@aljazerzen

Copy link
Copy Markdown
Member Author

This comment will be me thinking out loud on how to internally structure the compiler.

In my previous big commits, I've added declared_at field to the Node struct. It holds an integer that serves as an id of the declaration this variable references. Declarations are stored in context.declarations, which is an array indexed by the id. Declarations can be either expressions (most common), extern_ref (columns from actual tables), table (for table names) or functions.

Because SQL has interesting rules on which variables can contain aliases and which not (i.e. WHERE cannot and ORDER BY must), each variable has to be able to be materialized both as an alias or as an expression, depending on the materialization location.

This is why the name resolver (first stage of semantic analysis) moves all expressions from AST to context.declarations and leaves a simple ident in AST. Even unnamed columns leave Ident("<unnamed>") behind. This way, when materializing, one can pull declarations from one place only.

Now, I've split the resolver into 3 stages: name resolver, type checker and transform constructor.
This way, we can treat (and type check) transforms just as ordinary functions.
The problem is that now they must exactly adhere to rules for ordinary functions.
This becomes problematic for:

  1. join side:left, where left would be resolved as a normal variable (which would reference a column). I've solved this by adding type <unresolved>, which prevents this parameter to be resolved
  2. join employees, where employees should be resolved in namespace of tables. I've solved this by resolving all <table> types in their own namespace
  3. Name resolving depends on transforms - for example select and aggregate clear the scope of the frame columns. But functions are identified as transforms only in stage 3. This cannot be done before, because they may be missing parameters due to currying and such. I currently don't have solution to this.
  4. when moving expressions into context.declarations, they lose information about location of their declaration. This is needed to wrap functions that return a column within select or derive into Windowed. I don't have a solution to this.

One approach that would solve 4. would be to keep all expressions in AST and resolve them as soon as possible - maybe during type checking. This would also simplify some parts of the code base, because there would be no need to traverse both AST and declarations anymore.

All that said, I'll go and sleep on it before going down into another major refactor.

@aljazerzen

Copy link
Copy Markdown
Member Author

@max-sixty, is this a valid query?

derive a = 5

If we want to keep promises of transforms I'd say it is not, because derive only adds a column to a table. And since there is no from, derive is missing its last argument, which means that it has a type of func frame -> frame and thus cannot be converted to SQL query.

But in such case we should provide a syntax of table literals: #286

@max-sixty

Copy link
Copy Markdown
Member

@max-sixty, is this a valid query?

derive a = 5

I could see two options:

  • We start with a null tablespace / namespace, and can add columns like that; e.g. an implicit from null (which gets cleared by from foo? Or maybe derive has an alternative meaning when it doesn't follow from a from?)
  • We use Notation for creating sample data #286 to do this

I do think derive a = 5 is quite a natural, cohesive way of creating a column from nothing. It's also consistent with SQL's SELECT 5 as a. But I'd be fine to give this up if that makes the overall thing more cohesive. We can also try it and iterate — we're still early enough to do that I think.

@aljazerzen

Copy link
Copy Markdown
Member Author

My concern is that derive only adds columns not rows.

from null    # empty table, 0 column, 0 rows?
derive a = 5 # 1 column, 0 rows?

If derive has special behavior without last parameter, we need default values for positional arguments (which breaks other things).

So I'd stick with option 2.

@max-sixty

max-sixty commented Aug 10, 2022 •

Copy link
Copy Markdown
Member

My concern is that derive only adds columns not rows.

Yes good point. Agree with option 2!

Meta point — I think it's good that we're starting to hit these issues and it's good that we're resolving them in a conceptually correct way, even if that has small backward incompatible changes, or deviations from SQL. It's forcing us to confront the SQL warts we didn't even know about!

@aljazerzen

Copy link
Copy Markdown
Member Author

I agree very much on that - one should be able to scan over half of the transforms then be able to read PRQL and expect what is a result of a query.

@aljazerzen
aljazerzen force-pushed the semantic branch 2 times, most recently from 83ec087 to e6fc0bd Compare August 24, 2022 19:31
@aljazerzen
aljazerzen force-pushed the semantic branch 2 times, most recently from 7f28d36 to 3d85402 Compare September 19, 2022 12:19
@aljazerzen aljazerzen changed the title Type checking Incremental resolve, IR & type checking Oct 4, 2022
@aljazerzen

aljazerzen commented Oct 4, 2022 •

Copy link
Copy Markdown
Member Author

70 tests pass, 17 fail.

@max-sixty now, the semantic branch should be quite stable in the sense most of my major changes have been applied. I still have plans to change-up materializer (which will also be renamed into anchorer or something). I also have plans for IR to ditch Declarations, so after that I hope it will be easier to debug.

And I also disabled duckdb integration tests - I got tired of waiting for duckdb to compile. So maybe rather base from commit d3bc7e0 if you don't want that.

@max-sixty

Copy link
Copy Markdown
Member

@max-sixty now, the semantic branch should be quite stable in the sense most of my major changes have been applied. I still have plans to change-up materializer (which will also be renamed into anchorer or something). I also have plans for IR to ditch Declarations, so after that I hope it will be easier to debug.

Awesome, I will switch over to it.

And I also disabled duckdb integration tests - I got tired of waiting for duckdb to compile. So maybe rather base from commit d3bc7e0 if you don't want that.

In my core iteration loop (at the bottom of https://github.com/prql/prql/blob/main/CONTRIBUTING.md), I include -p prql-compiler --lib, which only runs unit tests. Does that help? (ofc no stress about disabling, only suggesting as it may be easier and mean you don't have to diverge from main)

@aljazerzen

Copy link
Copy Markdown
Member Author

I was doing that a lot, but cargo build and rust-analyzer still had long compilation times. For some reason compiling duckdb too way too long and sometimes used >5GB of RAM. Not sure why.

So I just keep rebasing the last commit to the top of my chain so I can remove it before the merge. That's why I gave you the notice, so you will not include it in your commit chain.

@max-sixty

Copy link
Copy Markdown
Member

I was doing that a lot, but cargo build and rust-analyzer still had long compilation times. For some reason compiling duckdb too way too long and sometimes used >5GB of RAM. Not sure why.

FYI you're correct that it does compile all dev-dependencies when running tests. But on my system I can't seem to make it recompile once it's been compiled once. If you can reproduce, I'm very happy to either fix it or move the integration tests back into a separate crate.

@aljazerzen

Copy link
Copy Markdown
Member Author

As discussed, this branch (and this PR) will now turn into a feature branch that we merge into. The plan is to finish the work that I started and to onboard @max-sixty, @priithaamer and anyone who also wants to learn our codebase.

I promise not to rebase this branch anymore. We will also refrain from merging changes from the main branch, so it will be easier to rebase/squash back to the main branch at the end.

Currently, there are many tests are failing: some produce errors/panics and some don't match the snapshots. Make sure not to accept any failing insta snapshots, because they (mostly) fail for a reason and we need to fix the compiler not the test. So we will be merging commits that don't pass CI tests, but please run cargo clippy and cargo fmt before merging.

There are some code blocks commented out, mostly dealing with windowing. Don't delete them, we will still need that code.

To get started, I suggest reading ARCHITECTURE.md (on this branch, I updated it a little) and the two book pages I wrote on the book-semantics branch.

A few quick hints:

  • we have 2 different Expr structs and 2 different ExprKind enums. One in ast and one in ir modules.
  • we have 2 different Context structs. One for semantics context and one for SQL context. There is also AnchorContext within the SQL context. At this point I use the word context for some container that holds additional information along with AST. It's hard to name things.
  • we have 3 types of ids: semantic ids (usually named just id or declared_at), ir::CId (column id) and ir::TId (table id).
  • AST means Abstarct Syntax Tree and we have two of them. One is confusingly named ast because I cannot find a better name.
  • if duckdb is taking too long to compile, cherrypick a commit on branch aljaz that disables it. Just make sure not to merge it to the semantic branch.

I'll be adding onboarding tag to issues that have prepared task for this branch. I suggest setting assignee to avoid multiple people from working on the same thing.

@aljazerzen aljazerzen added compiler good first issue Tasks suited for learning the compiler codebase labels Oct 18, 2022
@max-sixty

Copy link
Copy Markdown
Member

@aljazerzen how do you want to handle merging main in here? I'm guessing merge difficulty is super-linear in size — doing lots of small merges is easier than one big merge.

Doing #1048 would be useful for this, so "all existing tests pass" can be a marker for the merge being successful.

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

Labels

compiler good first issue Tasks suited for learning the compiler codebase

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants