Skip to content

[Proposal] Type Override Annotation Syntax for Individual Columns #339

Closed

Description

@abonander

This is another thing we talked about a long time ago but was postponed and then forgotten.

Motivation

In some contexts in the query macros, we cannot infer either the type or the nullability of a column that results from an expression:

For Postgres, we infer column nullability (whether to emit T or Option<T> for a column) based on NOT NULL constraints, which we can only look up if the column originated from a table (which includes aggregate columns). While Postgres always tells us the type of a column, for columns that are purely the result of an expression or come from more than one table, e.g. UNION or COALESCSE(), we simply cannot infer whether the column is never null or may be null--the simplest example being select 1.

For SQLite, the situation is even more awkward. Because it is dynamically typed (for the same column across multiple rows of a query, each field may be an entirely different type), the only way we can know what type a column is likely to be is if it originates from a table, in which case we can look up the type affinity of that column. For nullability, it's the same situation as Postgres, we can only determine it based on if the column has a declared NOT NULL constraint.

Proposal

For solving these specific problems, I propose we use SQL's support for arbitrary sigils in identifiers to allow per-column type overrides:

select 1 as "id: i32"

Since we currently require column names to at least be parseable as Rust raw identifiers, this is a backwards-compatible feature.

I think this syntax is decent because it looks like either type ascription or typing syntax for variable/argument bindings. When the macro sees this it ignores the type and nullability of the column and emits the given type instead.

We also discussed a syntax specifically to resolve nullability, which may be less error-prone than fully specifying the type but doesn't help SQLite users:

select 1 as "id:nonnull"

I think since nonnull is most likely to not appear as an actual type, we could special case this. Or maybe we have something completely different like "id!" to mark a not-null column.

Activity

  1. self-assigned this
    on May 25, 2020
  2. Freax13 commented on Jun 10, 2020

    @Freax13
    Contributor

    I think since nonnull is most likely to not appear as an actual type, we could special case this. Or maybe we have something completely different like "id!" to mark a not-null column.

    how about "id?" to mark it as nullable

  3. meh commented on Jun 10, 2020

    @meh
    Contributor

    @Freax13 I feel like that would confuse things considering what ? means in Rust. I'm in favor of id! since that's also what GraphQL does.

    Or better id: i32! would be NOT NULL and id: i32 would be NULL, and id! when the type is inferred.

  4. abonander commented on Jun 10, 2020

    @abonander
    CollaboratorAuthor

    how about "id?" to mark it as nullable

    I think that's actually what we've settled on, along with id: _ to disable the compile-time check (only allowed in query_as!(), in which case it behaves like query_as_unchecked!()).

    Or better id: i32! would be NOT NULL and id: i32 would be NULL, and id! when the type is inferred.

    I think that's the opposite of what Rust users have come to expect; the nullable type should be the one that's called out. Obviously id: Option<i32> would always be allowed as well but I think a shorthand for that would be nice.

    ?Type, Type? and arg? are all pretty common for other langs that support first-class nullable types so I don't think it would come as a complete surprise to most people. It's a completely different context than the ? operator normally appears in anyway.

    I'd suggest making it a prefix instead (i.e. ?id) but that is syntactically ambiguous with named bind parameters in SQL context which we'd like to support at some point.

  5. mehcode commented on Jun 10, 2020

    @mehcode
    Contributor

    This is what I'm thinking:

    • field as "field?" - forces Option<_>
    • field as "field!" - forces removal of Option<_>
    • field as "field: Type" - explicit type used
    • field as "field: _" - uses the declared rust type instead of inferring from the query ( so this only works with query_as! ) which has the effect of removing the compile-time type equivalency check and relying on the runtime type check
  6. abonander commented on Jun 10, 2020

    @abonander
    CollaboratorAuthor

    Seconded.

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

Metadata

Metadata

Assignees

Labels

enhancementmacrosproposal

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions