Skip to content

Omit table names in idents when there is only one table in SELECT #1081

Closed

Description

@aljazerzen

this issue is part of onboarding, see more info here


When we compile to very basic SELECT statements, table name prefixes in indents are not needed:

SELECT title FROM movies;

As soon as there are multiple tables, prefixes are needed:

SELECT movies.title FROM movies JOIN genres USING(genre_id);

This is because table genres could have column with name title which would cause conflict.

On branch semantic, we always include prefix, just to be sure, but it would be better to omit when possible. To make simple queries simpler.

(this is not critical to get done, but it's easy. @max-sixty I think this is too easy for you, leave it to @MarinPostma or @priithaamer or someone else)


Best way to achieve this is to add a flag omit_ident_prefix: bool on translator::Context. It should start as false.

Then, before starting to build a SELECT query, we must count all tables in From and Join transforms. This can be done by a new struct that implements an IrFold, similar to CidRedirector (https://github.com/prql/prql/blob/22c79b42155621df84ef5fd0429f594ea103abf8/prql-compiler/src/sql/anchor.rs#L263-L272.

Something like:

struct TableCounter {
    count: u16,
}
impl IrFold for TabelCounter {
   fn fold_transform(...) { ... }
}

Folding of the SELECT (represented by Vec<Transform>) can be invoked here:

https://github.com/prql/prql/blob/22c79b42155621df84ef5fd0429f594ea103abf8/prql-compiler/src/sql/translator.rs#L120-L126

and set the flag:

context.omit_ident_prefix = tableCounter.count == 1;

To actually omit prefixes, translate_ident with have to be updated: https://github.com/prql/prql/blob/22c79b42155621df84ef5fd0429f594ea103abf8/prql-compiler/src/sql/codegen.rs#L406-L431

When this is all done, many of currently failing tests should start working. You can check that by running:

cargo test --lib -- --ignored

You can now remove #[ignored] from tests that pass.

Activity

  1. added
    good first issue
    on Oct 25, 2022
  2. MarinPostma commented on Oct 25, 2022

    @MarinPostma
    Collaborator

    Looks interesting if @max-sixty lets me have it :)

  3. max-sixty commented on Oct 25, 2022

    @max-sixty
    Member

    @max-sixty I think this is too easy for you

    I doubt this, but yes @MarinPostma please go ahead!

  4. self-assigned this
    on Oct 26, 2022
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