Skip to content

Handling escaped idents which contain periods #1031

Closed

Description

@max-sixty

@brunorpinho suggested we use backticks for the ClickHouse dialect (https://discord.com/channels/936728116712316989/1030229933299077180). "That'll be super easy" I said. But actually the example presents a more general issue, regardless of the the specific dialect issue.

In SQL, "event.type" is treated as a column name. But it's not currently possible to represent an ident containing a period in PRQL. For example, in:

from github_json
derive [event_type_dotted = `event.type`]

`event.type` will be parsed into an event.type ident in the PRQL AST — i.e. a single term with the period. And then later in the compiler, that term will be resolved into a namespace event and a column type. But we want a column name event.type!


I can see two options:

  1. Change Ident into a proper struct — replace https://github.com/prql/prql/blob/10811eb25c0614fe99ff62b55d2a8b389d3bbfe5/prql-compiler/src/ast/mod.rs#L23 with a Struct that can hold a namespace and column name.

    Then:

    • event.type is parsed into {namespace: "event", column: "type"}
    • `event.type` is parsed into {namespace: None, column: "event.type"}

    ...but we use Ident for function names / variables / etc at the moment, and the parser deliberately doesn't have the context to discriminate. So we'll be carrying around a {namespace: None, column: "sum"} for the sum function1

  2. Carry around the backticks where they're supplied in the PRQL query in the Ident struct until we get to the resolver, rather than parse them. So `event.type` stays as `event.type` in the AST, and then when we get to resolving the name, we check for backticks, and produce the column `event.type`.

  3. (I don't think this is viable) Have another type EscapedIdent, which would contain event.type, but knowing that it shouldn't be resolved. But how would `table with space`.column be parsed?

If Ident were always a table name, I would suggest the Struct option, even though it would be quite the mindless refactor. But because it's also used for functions, I'm tempted to go with carrying around the backticks, even though it's some violation of the abstraction (now the name resolver is parsing backticks?!).

Any thoughts?

Footnotes

  1. though we could rename the column field to name or term or something ↩

Activity

  1. changed the title [-]Include namespace in an idents struct?[/-] [+]Handling escaped idents which contain periods[/+] on Oct 15, 2022
  2. aljazerzen commented on Oct 16, 2022

    @aljazerzen
    Member

    As mush as this is a pain in the ass, but it shows that we are not parsing idents correctly.

    Correct way would be to separately parse names in idents that are separated with .. Subsequently, ExprKind::Ident should also contain namespace and varname separately.

  3. max-sixty commented on Oct 16, 2022

    @max-sixty
    MemberAuthor

    Subsequently, ExprKind::Ident should also contain namespace and varname separately.

    OK — to confirm, you're OK having sum be parsed into {namespace: None, varname: "sum"}?

  4. aljazerzen commented on Oct 17, 2022

    @aljazerzen
    Member

    Yeah that's ok. The resolver should figure out that sum varname only appears in namespace std and resolve to that.

  5. max-sixty commented on Oct 28, 2022

    @max-sixty
    MemberAuthor

    Closed by #1079 , tested by #1090

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions