sqlc
sqlc copied to clipboard
Fix MySQL Mathematical Expression Type Inference
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
Hello, may I ask when this PR will be merged?