Skip to content

About

Extended MySQL SQL compatible support for KiteSQL

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

33 Commits

Folders and files

Repository files navigation

kite_sql_mysql

MySQL dialect support for KiteSQL1.

Overview

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.

Installation

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" }

Usage

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(())
}

Upstream status

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.

Features

MySQL Type Mappings (default)

  • Text Types: TINYTEXT, MEDIUMTEXT, LONGTEXT → VARCHAR without length limit
  • Bit Types:
    • BIT(1) → BOOLEAN
    • BIT(n) (n > 1) → UBIGINT
  • Serial Type: SERIAL → UBIGINT (identity/AUTO_INCREMENT behaviour needs hook-identity)
  • Integer Display Width: TINYINT(1) and other display widths parse as expected

Comparison coercion and boolean text (default)

  • Boolean vs text/number and temporal vs number comparison casts via SqlDialect::comparison_type
  • CAST(true AS VARCHAR) renders as 1 / 0 via boolean_text
  • BEGIN commits an open session transaction (begin_commits_active_transaction); SET autocommit is accepted through SqlParser::with_set_autocommit

AUTO_INCREMENT and LAST_INSERT_ID() (hook-identity)

Identity columns with AUTO_INCREMENT are supported when the identity hook is available, including:

  • The AUTO_INCREMENT = n table option
  • Explicit 0 values generate new IDs like NULL
  • LAST_INSERT_ID() returns the last generated ID
  • LAST_INSERT_ID(expr) sets and returns a value

Exact-Value Arithmetic (hook-numeric-typing)

  • Exact Decimal Literals: Numbers with a decimal point and no exponent (0.1, .5, 2.50) are treated as exact DECIMAL values, so 0.1 + 0.2 = 0.3 exactly
  • Division: / on exact values produces DECIMAL with scale = dividend scale + 4, so 7 / 2 = 3.5000 and 1.0 / 3 = 0.33333
  • Arithmetic Operations:
    • * adds operand scales (1.10 * 2.5 = 2.750)
    • +, -, % keep the larger scale
    • % and MOD() take the dividend's sign
  • Integer Division: DIV performs integer division, truncating toward zero, with the precedence of * and /
  • Division by Zero: Returns NULL with warning code 1365 (needs hook-diagnostics)
  • Precision Limits: Values hold up to 28 significant digits

MySQL Collation (hook-collation)

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:

  • ESCAPE clause for custom escape characters
  • Standard % and _ wildcards

MySQL Functions

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.

NULL Ordering (hook-nulls-first)

Results order NULLs as MySQL does: NULLs first when ascending, NULLs last when descending.

Warning Codes (hook-diagnostics)

Division by zero and numeric truncation generate MySQL-compatible warning codes (1365).

Limitations

Not yet modeled, or gated on upstream hooks:

  • Full Unicode Collation Algorithm for punctuation/symbol ordering
  • Some collation edge cases in GROUP BY, DISTINCT, UNION on _bin/_cs columns
  • 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

See Also

Trademark Notice

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.

License

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

  1. See the Trademark Notice section below. ↩

About

Extended MySQL SQL compatible support for KiteSQL

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages