Repository navigation
Description
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
Freax13 commentedon Jun 10, 2020
how about
"id?"to mark it as nullablemeh commentedon Jun 10, 2020
@Freax13 I feel like that would confuse things considering what
?means in Rust. I'm in favor ofid!since that's also what GraphQL does.Or better
id: i32!would beNOT NULLandid: i32would beNULL, andid!when the type is inferred.abonander commentedon Jun 10, 2020
I think that's actually what we've settled on, along with
id: _to disable the compile-time check (only allowed inquery_as!(), in which case it behaves likequery_as_unchecked!()).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?andarg?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.mehcode commentedon Jun 10, 2020
This is what I'm thinking:
field as "field?"- forcesOption<_>field as "field!"- forces removal ofOption<_>field as "field: Type"- explicit type usedfield as "field: _"- uses the declared rust type instead of inferring from the query ( so this only works withquery_as!) which has the effect of removing the compile-time type equivalency check and relying on the runtime type checkabonander commentedon Jun 10, 2020
Seconded.
xpe commentedon Apr 20, 2025
https://docs.rs/sqlx/latest/sqlx/macro.query.html#overrides-cheatsheet is handy to this day!