Skip to content

Enums / named unions #2930

Open
Open

Description

@vanillajonathan

What's up?

There could be a way to declare enums which could be used in queries.

Example with implicit assignment:

enum InvoiceKind
{
    Paid,
    Unpaid
}

from invoices
filter invoice_status == InvoiceKind.Paid

Here the variant Paid is implicitly assigned the value 0, and Unpaid is implicitly assigned the value 1.

Example with explicit assignment

enum InvoiceKind
{
    Paid = 10,
    Unpaid
}

Here the variant Paid is explicitly assigned the value 10, and Unpaid is implicitly assigned the value 11.

Example with string enums

Here the variant is assigned a string.

enum InvoiceKind
{
    Paid = "PAID",
    Unpaid = "UNPAID"
}

Activity

  1. added
    language-design
    on Jul 2, 2023
  2. changed the title [-]Add enums[/-] [+]Enums[/+] on Jul 2, 2023
  3. changed the title [-]Enums[/-] [+]Enums / named unions[/+] on Dec 18, 2023
  4. aljazerzen commented on Dec 18, 2023

    @aljazerzen
    Member

    This is something anticipated by the type system and something that will be useful for expanding our std module (for example for #2125).

    At the moment this is not easily implementable, as our backend does not have capabilities for checking a type at runtime. I also don't have a plan for how will this be implemented.

    I do know that enums should not be just enumerations of values, but a named union - a proper sum-type:

    type InvoiceStatus = (
        Paid = (),
        Unpaid = { amount = float },
        Cancelled = { reason = str },
    )
    

    (I'm using made-up syntax that is most consistent with the rules for other type-system constructs.)

    Related resource: https://www.parsonsmatt.org/2019/03/19/sum_types_in_sql.html

  5. vanillajonathan commented on Dec 18, 2023

    @vanillajonathan
    CollaboratorAuthor

    But can this still be used together with filter to filter integers or strings?
    Maybe the column InvoiceKind contains integers such as 1, 2, 3, or a string such as "PAID" or "UNPAID".

    I can't remember what those integers mean so I want to look them via enum.

  6. aljazerzen commented on Dec 19, 2023

    @aljazerzen
    Member

    Yes it could be used in filter. I'm thinking about pattern matching very similar to Rust's:

    from invoices
    filter (match this.status {
      InvoiceStatus.Paid => true,
      InvoiceStatus.Unpaid { amount } => amount < 10.0,
      _ => false,
    })
    

    I didn't talk about it above, but the "discriminator" between the enum values (i.e. the thing that is actually written into the table) should be configurable, as you propose.

    This is because there are already databases that contain enums based on strings, ints and actual enum types and we want to be able to write idiomatic queries for all of them.

  7. vanillajonathan commented on Dec 19, 2023

    @vanillajonathan
    CollaboratorAuthor

    Looks powerful, but also quite verbose. I would still like to be able to do simple stuff such as:

    from invoices
    filter invoice_status == InvoiceStatus.Paid
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

    language-designmajor-featureneeds-discussion

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions