SQL reference (MySQL and MariaDB dialects)

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#

LeftRightThe comparison is
texttexttext, ignoring case (also for numbers written as text: '10' < '9' is 1)
numbernumbernumeric (a decimal and a double are compared as doubles)
numbertextnumeric: the text is read as a number (its leading digits; no digits means 0)
anyNULLunknown (NULL)
date textdate texttext, which is right for the form YYYY-MM-DD[ HH:MM:SS]

In arithmetic and functions#

  • A text operand of + - * / % DIV and of the numeric functions is read as a number: '10' + 5 is 15, 'abc' + 1 is 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 a SELECT.
  • A number given to a string function is converted to its text (CONCAT('a', 1) is a1).
  • 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 & 3 is 8 & 3, which is 0). Text is read as a number ('6' & 3 is 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 + 0 is 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):

ColumnAcceptsRejects (strict mode)
integersa number (a decimal or double is rounded half away from zero), a text of digitsa value outside the range (1264); a text that is not a number (1366)
DECIMALa number or a text of digits; rounded to the scaletoo many integer digits (1264); not a number (1366)
FLOAT, DOUBLEa number or a text numberNaN, infinity, a negative for UNSIGNED (1264); not a number (1366)
CHAR, VARCHAR, TEXT, BLOBany value as textlonger than the column (1406)
ENUM, SETa member (any case) or an indexanything else (1265)
DATE, DATETIME, TIMESTAMP, TIMEthe text forms of Date and time typesan impossible date or time (1292)
YEAR1901 to 2155, 0anything else (1264)
BIT(M)a number that fits M bitsmore bits (1406)
JSONvalid JSON textinvalid text (3140)
UUID, INET4, INET6the forms of UUID, INET4 and INET6 typesanything else (1292)
any, NOT NULLNULL (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, NULL in MariaDB.
  • The wording of the errors differs as the servers' does: MariaDB says The integer value 'a' given for column db.t.c (row 1) is not valid (SQLSTATE 22007) where MySQL says The integer value 'a' given for column 'c' (row 1) is not valid (HY000), and MariaDB says An invalid INTEGER value was truncated: ... when ALTER TABLE converts a column.

Examples#

SQL
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;

See also#