Skip to content

Conversation

@rubensantoniorosa2704
Copy link

@rubensantoniorosa2704 rubensantoniorosa2704 commented Nov 7, 2025

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 rubensantoniorosa2704 force-pushed the feat/mysql-expression-type-inference branch from a4b354e to ef78ff2 Compare November 7, 2025 01:49
@rubensantoniorosa2704 rubensantoniorosa2704 force-pushed the feat/mysql-expression-type-inference branch from ef78ff2 to 605cbc5 Compare November 7, 2025 01:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

How can i get correct type

1 participant