Type conversion, out-of-range values and strict mode
How Raft DB converts between numbers, text and dates in expressions, comparisons and column stores, and what happens to a value that does not fit.
Raft DB converts types in three places: when two values are compared, when an operator or function needs another type, and when a value is stored in a column. The rules follow MySQL's; this page states what the code does.
In comparisons#
| Left | Right | The comparison is |
|---|---|---|
| text | text | text, ignoring case (also for numbers written as text: '10' < '9' is 1) |
| number | number | numeric (a decimal and a double are compared as doubles) |
| number | text | numeric: the text is read as a number (its leading digits; no digits means 0) |
| any | NULL | unknown (NULL) |
| date text | date text | text, which is right for the form YYYY-MM-DD[ HH:MM:SS] |
In arithmetic and functions#
- A text operand of
+ - * / % DIVand of the numeric functions is read as a number:'10' + 5is 15,'abc' + 1is 1 (the text counts as 0). The text is read up to its first character that is not a digit, a sign, a decimal point;'12abc'is 12. MySQL also raises a warning (1292); Raft DB does not keep it in aSELECT. - A number given to a string function is converted to its text (
CONCAT('a', 1)isa1). - A text given to a date function is read as a date; if it is not one the answer is
NULL. - Bit operators read their operands as unsigned 64-bit numbers; a fraction is rounded first (
7.6 & 3is8 & 3, which is 0). Text is read as a number ('6' & 3is 2). - A division is exact (a decimal); a double operand makes the result a double.
- A hexadecimal literal of up to 8 bytes in arithmetic is its number (
0xAA + 0is 170).
When a value is stored in a column#
The value is converted to the column's type by these rules (the file is types.rs):
| Column | Accepts | Rejects (strict mode) |
|---|---|---|
| integers | a number (a decimal or double is rounded half away from zero), a text of digits | a value outside the range (1264); a text that is not a number (1366) |
DECIMAL | a number or a text of digits; rounded to the scale | too many integer digits (1264); not a number (1366) |
FLOAT, DOUBLE | a number or a text number | NaN, infinity, a negative for UNSIGNED (1264); not a number (1366) |
CHAR, VARCHAR, TEXT, BLOB | any value as text | longer than the column (1406) |
ENUM, SET | a member (any case) or an index | anything else (1265) |
DATE, DATETIME, TIMESTAMP, TIME | the text forms of Date and time types | an impossible date or time (1292) |
YEAR | 1901 to 2155, 0 | anything else (1264) |
BIT(M) | a number that fits M bits | more bits (1406) |
JSON | valid JSON text | invalid text (3140) |
UUID, INET4, INET6 | the forms of UUID, INET4 and INET6 types | anything else (1292) |
any, NOT NULL | NULL (1048) |
Strict and non-strict mode#
In strict mode (STRICT_TRANS_TABLES, STRICT_ALL_TABLES or TRADITIONAL in sql_mode; the default of both dialects) every row above that is rejected is an error and the statement changes nothing. Without it the value is made to fit and the statement goes on with a warning (shown by SHOW WARNINGS): numbers go to the nearest bound or to their leading digits (0 if there are none), text is cut, dates and times become the zero value. ENUM, SET, BIT, JSON and NULL into NOT NULL stay errors. The table of this behaviour is on SQL modes. A statement is all or nothing: if row 3 of a multi-row INSERT is refused, rows 1 and 2 are not stored either.
Explicit conversion#
CAST(x AS type) and CONVERT(x, type); see Cast and conversion functions. Differences from MySQL worth knowing: CAST(-5 AS UNSIGNED) is 0 here (MySQL gives 18446744073709551611); CAST(x AS TIME) keeps the time part of a text without checking it; CAST(x AS CHAR(n)) cuts to n characters.
Differences between the dialects#
- Fractions of a second: MySQL rounds, MariaDB truncates (see Date and time types).
X'..'is a string in MariaDB and a number in arithmetic in MySQL (see Literals).- Invalid JSON text given to a function: error in MySQL,
NULLin MariaDB. - The wording of the errors differs as the servers' does: MariaDB says
The integer value 'a' given for columndb.t.c(row 1) is not valid(SQLSTATE 22007) where MySQL saysThe integer value 'a' given for column 'c' (row 1) is not valid(HY000), and MariaDB saysAn invalid INTEGER value was truncated: ...whenALTER TABLEconverts a column.
Examples#
SELECT '10' < '9', 10 < 9, '10' + 5, 'a' = 'A', NULL IS NULL, 1 <=> NULL, 7 DIV 2;
SELECT CAST('12abc' AS SIGNED), CAST('2024-05-06 07:08:09' AS DATE), CAST(1.5 AS SIGNED);
INSERT INTO types (ti) VALUES (128); -- strict: error 1264
SET SESSION sql_mode = '';
INSERT INTO b VALUES ('', 'abc', 'x'); -- stores 0, 0 and the zero date, with warnings
SHOW WARNINGS;