333 points · by massysett · 5y ago · 111 comments
What about TIMESTAMP? DATETIME? You can't use that as a type name anymore. Either pick INT, REAL or TEXT for that. What about JSON? You can't use it as a type name anymore. Pick TEXT. Basically type names, even if SQLite didn't have explicit support for that type, were helpful documentation hints in what the column contained. Now everything devolves to either INT or TEXT. I have UUID fields, I can't even call them UUID or BINARY(16), just BLOB.
CREATE DOMAIN UUID AS TEXT;
Or if you prefer:
CREATE DOMAIN UUID AS BINARY;
Without CREATE DOMAIN, for STRICT tables they don't know which basic type to use for an unknown type name. They could use something like "unknown types are equivalent to ANY" but that would mean they couldn't implement CREATE DOMAIN in the future without breaking backward compatibility. If they did implement CREATE DOMAIN I expect it would be supported for STRICT tables only.
What makes it really powerful is you can also define a CHECK constraint, NOT NULL constraint, and DEFAULTs to be automatically applied to table columns of this custom type. Examples:
CREATE DOMAIN UUID AS TEXT CHECK (value REGEXP '[a-f0-9]{8}(-[a-f0-9]{4}){4}[a-f0-9]{8}');
CREATE DOMAIN UUID AS BINARY CHECK (LENGTH(value) = 16);
I think Postgres is the only major relational database to implement CREATE DOMAIN, although there are some more obscure RDBMSes which support it too (such as Firebird, Interbase, Sybase/SAP SQL Anywhere).
The SQLite developers haven't said they plan to do this, but I imagine they are thinking about it and may do it at some point. I think it is the kind of feature likely to appeal to them, it should be relatively simple to implement (especially if they don't implement ALTER DOMAIN, only CREATE and DROP), but provide a lot of power in exchange.
It's like the modern C convention of avoiding "int" in favor of u8/u16/u32/u64. Rather than pretending "int" is a separate thing, you have to explicitly choose and acknowledge the concrete primitive type you're really getting, and explicitly opt into the semantics that use of that concrete primitive type implies.
Also, many APIs of the C standard library are defined to take or give char/int/etc., and it takes effort to translate between those types and u8/u32/etc. in a safe way that works on all platforms.
> What about TIMESTAMP? DATETIME?
The same thing as we've always done: proper column names and strong datatypes in the application layer.I usually use TEXT and store ISO 8601.
Datetime and timestamp is fine if you need date functions ("- interval '7' day", etc.) but most of the time you can just convert the text to datetime when running the query without much performance penalty in other databases.
What is the usecase for TIMESTAMP and DATETIME that cannot be solved today?
It's horrible for performance. A iso8601 timestamp takes at least double the amount of bytes (without a timezone), but more importantly you need to parse and validate the timestamp each time you use it. Filtering and sorting suffer a lot. Iso8601 without timezones can in theory be sorted trivially without parsing, but sorting strings is still a lot more expensive than sorting arrays of int64. And you also have to make sure to never write a different timezone. Why not just use an int?
You then have to parse the string into the native datetime type again in application code.
You also need to add a custom check constraint to prevent writing garbage to the column, which is easy to forget.
I wonder if SQLite didn't store the schema for a table as a "CREATE TABLE..." string, but instead something a bit more... structured, then such things could be added in a more backwards compatible way?
And yes, it'd be very nice to have a relational metaschema that doesn't suck for SQL schema. So far all such metaschemas I've seen leave a lot to be desired.
What would be nice is if SQLite had separate “compatible with” and “read-only compatible with” fields, since the read-only behavior should not have changed at all (although I suppose describing the table would fail depending on how the presence of the STRICT modifier is codified).
Something like an object-relational mapper, e.g. SQLAlchemy?
I would expect the additional checks on writes to be negligible in most cases (dwarfed by other bookkeeping and I/O). I imagine that strict mode could enable some optimizations for read queries, but I can't see those being huge wins.
I agree from an algorithmic perspective but in the real world, it really depends. The size of a non-text/binary cell within a column is now fixed, meaning guaranteed to never need to resize/reallocate when streaming results. That could translate to non-negligible improvements (multiple percentage point speed up) if it’s implemented separately from the existing code path.
Some people seem to object to the Any type, but it's incredibly useful since a column may naturally contain a different type per row and it avoids ugly hacks to accommodate them.
> The behavior of ANY is slightly different in a STRICT table versus an ordinary non-strict table. In a STRICT table, a column of type ANY always preserves the data exactly as it is received. For an ordinary non-strict table, a column of type ANY will attempt to convert strings that look like numbers into a numeric value, and if successful will store the numeric value rather than the original string.
So it has some utility in a strict table that you can't get otherwise.
[1] https://www.sqlite.org/stricttables.html#the_any_datatype
Of course, migrating current databases is another story. They have plenty of CHECK constraints and app-level validation already so the value in migrating those is small (although I'd still do it because the ANY data type columns will not coerce / convert anything when the table is STRICT which to me is still a win).
I tried out DuckDB which is more my style but the .NET libs are poor and I can't shave that yak right now.
> You still can't store a float in the database and get it back out unmodified. SQLite will expand it to a double,
When would a double not be an acceptable replacement for float? Seriously interested. If the application layer works with floats, then a double-precision float should contain all possible values. I'd really love to hear about the use-case, I'm sure they exist even if I can't think of one. > and it can't store NaNs.
Well, neither can other small-time databases such as MySQL. Postgres does, though, which I did not know until googling it now.> In a STRICT table, a column of type ANY always preserves the data exactly as it is received. For an ordinary non-strict table, a column of type ANY will attempt to convert strings that look like numbers into a numeric value, and if successful will store the numeric value rather than the original string
There's still much less data types than in most SQL databases though, e.g. float4, boolean, timestamp etc are missing.
Is that part a real problem? (The NaN thing certainly is.)
Doubles are a strict superset of floats. You don't risk losing any precision on the float to double conversion, and if you then convert the result back to float, you won't lose any precision either.
Is it hard to work around? No. But every time I use SQLite it requires half a dozen little hacks to work around it's strangeness and I would prefer to not have to do that.
Anyway, I've never touched a serialization library that doesn't mess with the data types it gets. There is always some conversion needed, and this one is on the easy end of the scale. The fact that composed types (records and arrays) aren't on the list is a much larger troublemaker.
(Regardless, I'm curious about the applications of 13M inserts per minute on a synchronous single-computer database. Pushing a machine to that extreme looks very interesting. Is that application something you can share?)
I would like an even stricter mode with no automatic type coercion.
I wonder if this will provide more adoption of this client server database.
EDIT: for this not aware, Expensify is a paid sponsor of SQLITE and there’s an official branch (Bedrock) of SQLite that has client / server capabilities.
I love this level of attention to detail and kindness in the SQLite documentation. Very few projects do this.
At last.
That's fair and allowed by the standard, but it will break some things.
If it's what I'm thinking of, this concern was carefully considered:
If a subquery in the FROM clause has an ORDER BY, that ORDER BY is omitted if all of the following conditions are also true:
1. There is no LIMIT clause in the subquery
2. The subquery was not one of the virtual subqueries added internally by SQLite for window-function processing
3. The subquery is not part of the FROM clause in an UPDATE-FROM statement
4. The outer query does not use any aggregate functions other than the built-in count(), min(), and/or max() functions.
5. Either the outer query has its own ORDER BY clause or else the subquery is one term of a join.
from https://sqlite.org/forum/forumpost/878ca7a9be0862af?t=hAn almost Clintonian phrase! :-)
Personally i'm more helped by good examples than these syntax diagrams.
However, it does make handling more "advanced" types less standard. For example, there isn't a standard way to store a date, especially if we want to preserve high-precision (nanoseconds).
This also is an interesting tidbit:
"The query planner now omits ORDER BY clauses on subqueries and views if removing those clauses does not change the semantics of the query."
Curious how it might improve queries -- I wonder how many folks were doing potentially redundant order by's in subqueries
1: Start out lean and simple. Often replacing an old complex one
2: Become more complex over time
3: Be replaced by a new lean and simple one
If you can take a developer using the old version that was lean and simple, give them the latest version full of extra features, and they can use that version without any need to change anything, then I think it's fine.
And, so far, I think that SQLite has done a good job of keeping their core usage lean and simple.