SQLite Forum

CAST type-name is optional
Login
Poking around in the grammar at `src/parse.y`, I find the entry for `CAST` as follows:

```
expr(A) ::= CAST LP expr(E) AS typetoken(T) RP. {
  A = sqlite3ExprAlloc(pParse->db, TK_CAST, &T, 1);
  sqlite3ExprAttachSubtrees(pParse->db, A, E, 0);
}
```

and looking for `typetoken` in the same file, there is this illuminating comment:

```
// A typetoken is really zero or more tokens that form a type name such
// as can be found after the column name in a CREATE TABLE statement.
// Multiple tokens are concatenated to form the value of the typetoken.
```

To emphasise, **zero or more tokens**. This makes good sense in the context of `CREATE TABLE`, less so in the case of a `CAST` expression.

I conjecture that this inadvertently legalised a `CAST` expression with no type designation. It certainly appears to be undocumented.

Perhaps the grammar should be tightened to disallow this; but if that cannot (or should not) be done for the sake of backward compatibility, I think it ought to remain undocumented, so as not to trick users into relying on it. Or, it could be documented, with a caveat that the resulting behaviour is undefined. (It seems to cast to numeric at the moment.) In either case, I think it had better be kept out of the syntax diagram.

Just my three cents' worth – inflation, y´know.