TypeBehavior

Trait TypeBehavior 

Source
pub trait TypeBehavior {
    // Required methods
    fn has_decimal_type(&self) -> bool;
    fn uses_dynamic_typing(&self) -> bool;
    fn division_result_type(
        &self,
        left: &SqlValue,
        right: &SqlValue,
    ) -> ValueType;
    fn permissive_type_coercion(&self) -> bool;
}
Expand description

Type system behavior trait for SQL modes

This trait defines how different SQL modes handle type-related operations such as type inference, coercion, and result type determination.

§Examples

use vibesql_types::{MySqlModeFlags, SqlMode, SqlValue, TypeBehavior, ValueType};

let mysql_mode = SqlMode::MySQL { flags: MySqlModeFlags::default() };
let sqlite_mode = SqlMode::SQLite;

// MySQL always returns Numeric for division
assert_eq!(
    mysql_mode.division_result_type(&SqlValue::Integer(5), &SqlValue::Integer(2)),
    ValueType::Numeric
);

// SQLite returns Integer for int/int division
assert_eq!(
    sqlite_mode.division_result_type(&SqlValue::Integer(5), &SqlValue::Integer(2)),
    ValueType::Integer
);

// SQLite returns Float when any operand is real
assert_eq!(
    sqlite_mode.division_result_type(&SqlValue::Float(5.0), &SqlValue::Integer(2)),
    ValueType::Float
);

Required Methods§

Source

fn has_decimal_type(&self) -> bool

Whether this mode has a distinct DECIMAL/NUMERIC type

  • MySQL: true - has separate DECIMAL type for exact arithmetic
  • SQLite: false - only has INTEGER and REAL types
Source

fn uses_dynamic_typing(&self) -> bool

Whether this mode uses dynamic typing (type affinity)

Dynamic typing means values can change types during operations and type checking is lenient.

  • MySQL: false - uses static typing with defined column types
  • SQLite: true - uses type affinity system, types are suggestions
Source

fn division_result_type(&self, left: &SqlValue, right: &SqlValue) -> ValueType

Get the result type for division operation

This determines what type will result from dividing two values, which varies significantly between SQL modes.

§Arguments
  • left - The left operand (dividend)
  • right - The right operand (divisor)
§Returns

The ValueType that should result from this division

§MySQL Behavior

By default, MySQL returns Numeric (exact decimal) for division to preserve precision:

  • INTEGER / INTEGER → Numeric (e.g., 5 / 2 = 2.5000)
  • FLOAT / INTEGER → Numeric
  • Any division → Numeric (unless NULL)

When sqlite_division_semantics flag is set in MySqlModeFlags, MySQL mode will use SQLite’s integer-preserving division instead. This is useful for compatibility with test suites that use MySQL syntax but expect SQLite division results.

§SQLite Behavior

SQLite uses type affinity rules:

  • INTEGER / INTEGER → Integer (truncated, e.g., 5 / 2 = 2)
  • REAL / INTEGER → Float (any real operand makes result float)
  • INTEGER / REAL → Float
  • REAL / REAL → Float
Source

fn permissive_type_coercion(&self) -> bool

Whether implicit type coercion is permissive

Permissive coercion allows implicit conversions between types (e.g., string to number, number to string).

  • MySQL: true (unless STRICT_TRANS_TABLES or similar flags set)
  • SQLite: true (always permissive, uses type affinity)

Implementors§