MySQL dialect support for KiteSQL1.
This crate provides MySQL-specific functionality for KiteSQL through its pluggable SqlDialect API. It enables KiteSQL to parse and execute MySQL-specific SQL syntax, type mappings, and (where upstream hooks allow) functions and collation rules.
Add this to your Cargo.toml:
[dependencies]
kite_sql = { git = "https://github.com/KipData/KiteSQL", rev = "2a57f09d279e44c2d510d7aa72b7e24c1efe893b" }
kite_sql_mysql = { git = "https://github.com/alexskinner/KiteSQL-MySQL" }Use with_mysql() to set the dialect, then install_mysql_functions to register MySQL scalar functions through Database::load:
use kite_sql::db::DataBaseBuilder;
use kite_sql_mysql::{install_mysql_functions, MySqlBuilderExt};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut database = DataBaseBuilder::path(".")
.with_mysql()
.build_in_memory()?;
install_mysql_functions(&mut database)?;
// Now you can use MySQL-specific types and syntax
database.ddl("CREATE TABLE users (
id INT PRIMARY KEY,
name VARCHAR(100),
bio MEDIUMTEXT
)")?;
database.run("INSERT INTO users (id, name, bio) VALUES (1, 'Alice', 'Software developer')")?
.done()?;
let mut result = database.run("SELECT * FROM users WHERE id = 1")?;
if let Some(tuple) = result.next_tuple(|_, tuple| tuple.clone())? {
println!("{:?}", tuple);
}
result.done()?;
Ok(())
}Pinned to KipData/KiteSQL main (2a57f09). The default feature set uses only hooks that already exist upstream and is what CI builds and tests.
| Cargo feature | MySQL behaviour | Upstream hook / issue |
|---|---|---|
| (default) | SqlDialect::parse with sqlparser MySQL + rewrite, DataTypeMapper, comparison_type, boolean_text, begin_commits_active_transaction, SqlParser::with_set_autocommit, prefix indexes, fixed-arity scalar functions via Database::load |
#404, #406, #407, #409, #410 |
hook-functions |
Variadic / accepts_any_args functions (GREATEST/LEAST, open-ended CONCAT, …) and richer binder lookup |
Implicit-cast function lookup + ScalarFunctionImpl::{accepts_arity,accepts_any_args,return_type_for} (#412); FunctionRegistry/packs |
hook-collation |
utf8mb4_0900_ai_ci text comparison keys |
Dialect collation hooks (text_collation / binary-collation detection) |
hook-nulls-first |
NULLs first ascending / last descending | Dialect nulls_first hook |
hook-numeric-typing |
Exact DECIMAL literals, / and AVG scale rules, arithmetic result types |
Dialect number_literal / arithmetic_return_type / avg_return_type hooks |
hook-column-display |
Preserve MySQL display types (tinytext, bit(1), serial, …) in catalogs |
ColumnTypeSpec / display-type mapping beyond LogicalType |
hook-identity |
AUTO_INCREMENT / SERIAL identity, LAST_INSERT_ID(), zero-generates |
Identity / identity_zero_generates hooks |
hook-diagnostics |
MySQL warning/error codes (e.g. 1365) and statement policies | Diagnostics / map_diagnostic / StatementPolicy hooks |
hook-upsert |
ON DUPLICATE KEY UPDATE and MySQL affected-row counts |
Upsert dialect hooks (upstream #408 not merged) |
Optional hook-* features keep the MySQL-side code paths ready; enabling them against current upstream main will not compile until the corresponding hooks land. UDAFs and statement undo-log support are likewise waiting on upstream extension points and are not exposed yet.
- Text Types:
TINYTEXT,MEDIUMTEXT,LONGTEXT→VARCHARwithout length limit - Bit Types:
BIT(1)→BOOLEANBIT(n)(n > 1) →UBIGINT
- Serial Type:
SERIAL→UBIGINT(identity/AUTO_INCREMENTbehaviour needshook-identity) - Integer Display Width:
TINYINT(1)and other display widths parse as expected
- Boolean vs text/number and temporal vs number comparison casts via
SqlDialect::comparison_type CAST(true AS VARCHAR)renders as1/0viaboolean_textBEGINcommits an open session transaction (begin_commits_active_transaction);SET autocommitis accepted throughSqlParser::with_set_autocommit
Identity columns with AUTO_INCREMENT are supported when the identity hook is available, including:
- The
AUTO_INCREMENT = ntable option - Explicit 0 values generate new IDs like NULL
LAST_INSERT_ID()returns the last generated IDLAST_INSERT_ID(expr)sets and returns a value
- Exact Decimal Literals: Numbers with a decimal point and no exponent (
0.1,.5,2.50) are treated as exactDECIMALvalues, so0.1 + 0.2 = 0.3exactly - Division:
/on exact values producesDECIMALwith scale = dividend scale + 4, so7 / 2 = 3.5000and1.0 / 3 = 0.33333 - Arithmetic Operations:
*adds operand scales (1.10 * 2.5 = 2.750)+,-,%keep the larger scale%andMOD()take the dividend's sign
- Integer Division:
DIVperforms integer division, truncating toward zero, with the precedence of*and/ - Division by Zero: Returns
NULLwith warning code 1365 (needshook-diagnostics) - Precision Limits: Values hold up to 28 significant digits
Text comparison follows MySQL 8's default collation: case- and accent-insensitive, NO PAD:
'Smith' = 'SMITH','é' = 'e','ß' = 'ss''a' <> 'a '(trailing spaces matter)
LIKE is rewritten case-insensitively by default unless an operand is marked binary, and supports:
ESCAPEclause for custom escape characters- Standard
%and_wildcards
Default build registers fixed-arity implementations through Database::load (for example REPLACE, CONCAT with 1..=8 arguments, ROUND, UUID, ISNULL over common types). Variadic/accepts_any_args forms and reliable use on length-bounded column types wait on hook-functions / #412.
Results order NULLs as MySQL does: NULLs first when ascending, NULLs last when descending.
Division by zero and numeric truncation generate MySQL-compatible warning codes (1365).
Not yet modeled, or gated on upstream hooks:
- Full Unicode Collation Algorithm for punctuation/symbol ordering
- Some collation edge cases in
GROUP BY,DISTINCT,UNIONon_bin/_cscolumns - Accent folding in
LIKE - Per-column collation metadata beyond "byte-wise or not"
- Error 1267 (illegal mix of collations) outside
LIKE ON DUPLICATE KEY UPDATE(hook-upsert)- User-defined aggregates and statement undo log
MySQL is a registered trademark of Oracle Corporation and/or its affiliates. KiteSQL-MySQL is an independent open source project and is not affiliated with, sponsored by, or endorsed by Oracle Corporation. The name "MySQL" is used only to describe the SQL dialect and syntax this crate aims to be compatible with.
Licensed under the Apache License, Version 2.0. See the LICENSE file for details.
This software builds upon KiteSQL, which is also licensed under Apache 2.0 and is Copyright (c) 2024 KipData and contributors.
Footnotes
-
See the Trademark Notice section below. ↩