Internals

Expressions and types in the front-end

How MySQL's value rules, column types and expression result types are reproduced, how generated columns, checks and functional indexes use them, and the known gaps.

Values and evaluation#

expr.rs evaluates expressions with MySQL's rules where they differ from the obvious (D52): text comparison ignores case (the default collation), division is exact (7 / 2 is 3.5, 1 / 0 is NULL), logic is three-valued, and numbers inside text are read as numbers. Decimal and whole-number arithmetic is exact up to 128 bits for + - * / % DIV; a division gives the scale of the dividend plus 4, rounded half away from zero (D80). A whole number beyond 64 bits keeps all its digits.

Strict typing with MySQL's own errors and messages applies on write (1048, 1364, 1366, 1264, 1406, 1292, 1062, 1054 and others). Types in the connector: the integer family with signed and unsigned ranges, FLOAT and DOUBLE, DECIMAL(p,s) up to 65 digits and 30 decimals (kept exactly as text; comparison, ORDER BY, SUM and AVG beyond 15 digits go through a double and are approximate, D58), CHAR, VARCHAR and the TEXT family, ENUM and SET, BLOB and BINARY families, BIT(1..64), DATE, DATETIME(n), TIMESTAMP(n), TIME(n) (n up to 6), YEAR, JSON (validated), BOOLEAN, and for MariaDB UUID, INET4, INET6 (D82).

Column metadata is what drivers decide types from, so it follows MySQL and MariaDB: a TINYINT(1) has display length 1, the TEXT family are BLOB types with a text character set, a DECIMAL's length is precision plus 2 with a scale, and numeric, date and time columns use the binary character set 63 (D52, level 1c).

Expression result types (exprtype.rs, D78)#

exprtype::expr_ty(expr, lookup) gives a Ty (type, arguments, unsigned flag, may-be-null) or None, without evaluating the expression. It is used for the select list of CREATE TABLE ... AS SELECT (a table with no rows has no values to look at) and for the hidden column of a functional index. The rules were found by trying expressions on a MySQL server:

  • A whole number has a display width (int 11, bigint 20, a literal its digits); + - * gives int while the width is at most 11 and bigint above it, unsigned if both sides are unsigned or literal.
  • / gives decimal(p+4, s+4); + - * % on fixed-point numbers compute precision and scale; DIV gives an int; a float, double or text in arithmetic gives double.
  • String functions give a varchar as long as what they can make (CONCAT the sum, LEFT(s, n) the smaller of the two).
  • Comparisons and logic give int; COALESCE, IFNULL, IF, CASE, GREATEST and LEAST merge the types of their results; SUM is decimal(p+22, s), AVG is decimal(p+4, s+4), COUNT is bigint; CAST gives what it names.
  • Nullability follows MySQL (for example /, % and DIV may give NULL).

A test script (about 120 expressions, each stored with CREATE TABLE ... AS SELECT and shown with DESCRIBE) is identical to MySQL 9.7. Known gaps stated in D78: DATE_ADD and INTERVAL arithmetic, a SELECT over a derived table, a view or a natural join, a UNION, and non-ASCII text literals are not typed this way and the values are looked at as before; an integer literal beyond bigint is a double in this module and a decimal in MySQL.

Generated columns, checks and functional indexes#

  • Generated columns (D74): GENERATED ALWAYS AS (expr) [VIRTUAL | STORED] is stored in the column's document and parsed again on load. Session::regenerate fills them whenever a row is built, updated or rewritten; writing to one is error 3105.
  • CHECK constraints (D73): stored in the table document, parsed again when read, evaluated by Session::check_row on every new or replaced row; ALTER TABLE ADD CHECK also checks the existing rows.
  • Functional indexes (D76): a key part that is an expression is lifted into a hidden generated column with an index on it (functional::lift); the hidden column is left out of DESCRIBE, SHOW CREATE TABLE and information_schema. pushdown::column_of lets a query that contains the same expression use that index (D77).
  • Rules (D77): functional::first_bad finds what a generated column, functional index or check may not contain; alter.rs::column_dependency refuses to drop or rename a column that one of them uses.
  • Invisible columns and indexes (D75): stored as invisible; * skips invisible columns and INSERT without a column list takes only visible ones.
  • Expression defaults (D81): DEFAULT (expr), DEFAULT NEXTVAL(s), DEFAULT UUID() and so on, in both dialects.

The function library#

funcs.rs and funcs2.rs hold the scalar functions (string, numeric, date and time, and, from D82, about 60 more such as CONV, CRC32, INET_ATON, TIME_FORMAT); jsonfn.rs holds the JSON functions with paths .key, [n], [last-n], [a to b], .*, [*], **; regex.rs is the regular-expression matcher behind REGEXP, REGEXP_REPLACE, REGEXP_SUBSTR and REGEXP_INSTR (groups, back-references \1..\9, look-ahead and look-behind; flags such as (?i), named groups and the match_type argument are not supported yet, D83). window.rs runs window functions in two parts: an inner query for the plain columns and aggregates, then the window columns worked out over those rows, then the outer query over the result.

See Pushdown and the executor for how conditions reach Raft DB, and Dialects for dialect differences in these rules.