sqlc icon indicating copy to clipboard operation
sqlc copied to clipboard

Fix MySQL Mathematical Expression Type Inference

Open rubensantoniorosa2704 opened this issue 1 month ago • 1 comments

This implements type inference for MySQL mathematical expressions. Before this change, queries with arithmetic operations would generate interface{} types, which meant no compile-time type safety. Now the compiler properly infers types based on the operands and operators.

Before:

type ListTestRow struct {
    A1Float interface{} `json:"a1_float"`  // was interface{}
}

After:

type ListTestRow struct {
    A1Float string `json:"a1_float"`  // now string (MySQL DECIMAL)
}

What changed

  • Added inferExprType() that recursively analyzes SQL expressions to determine their types
  • Handles column references, constants, binary operations, and type casts
  • MySQL-specific type rules: division returns decimal, float operations return float, nullability propagates correctly
  • Enhanced COALESCE to use type inference instead of always returning interface{}
  • Added "div" and "mod" to the mathematical operators list (MySQL uses symbolic names instead of / and %)

Notes

The implementation is MySQL-specific for now. PostgreSQL and SQLite return nil from combineGenericTypes() to maintain current behavior. There's a TODO comment about refactoring IsMathematicalOperator() to be engine-specific.

Closes #4153

rubensantoniorosa2704 avatar Nov 07 '25 01:11 rubensantoniorosa2704

Hello, may I ask when this PR will be merged?

hts0000 avatar Nov 26 '25 10:11 hts0000