Skip to content

Commit a4b354e

Browse files
fix: Add MySQL-specific operators (div, mod) to IsMathematicalOperator
The MySQL parser uses symbolic names like 'div' and 'mod' instead of '/' and '%'. Added these to operator.go with TODO comments indicating they should be moved to engine-specific logic during refactoring. This fixes the division case in type inference where (a1 / 1024) was generating interface{} instead of the correct decimal/string type. All 3 test cases now work correctly: - ListTest: Simple division generates string/sql.NullString - ListTest4: Arithmetic with nullability generates sql.NullInt32 - ListTest5: COALESCE generates string
1 parent a107216 commit a4b354e

File tree

6 files changed

+248
-10
lines changed

6 files changed

+248
-10
lines changed

examples/type-inference-test/mysql/db.go

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/type-inference-test/mysql/models.go

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/type-inference-test/mysql/query.sql.go

Lines changed: 195 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/compiler/infer_type.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -142,24 +142,18 @@ func combineMySQLTypes(left, right *Column, operator string, notNull bool) *Colu
142142
return left
143143
}
144144

145-
// Normalize MySQL-specific operators
146145
switch operator {
147-
case "div":
148-
operator = "/" // DIV is MySQL's integer division, but for type inference treat as division
149-
case "mod":
150-
operator = "%" // MOD is alias for %
151-
}
152-
153-
switch operator {
154-
case "/":
146+
case "/", "div":
155147
// Division: int/int = decimal, float/anything = float
148+
// Note: "div" is MySQL-specific operator recognized by IsMathematicalOperator()
156149
if isFloatType(left.DataType) || isFloatType(right.DataType) {
157150
return &Column{DataType: "float", NotNull: notNull}
158151
}
159152
return &Column{DataType: "decimal", NotNull: notNull}
160153

161-
case "%":
154+
case "%", "mod":
162155
// Modulo: returns integer if both are integers, otherwise decimal
156+
// Note: "mod" is MySQL-specific operator recognized by IsMathematicalOperator()
163157
if isIntegerType(left.DataType) && isIntegerType(right.DataType) {
164158
return &Column{DataType: "int", NotNull: notNull}
165159
}

internal/sql/lang/operator.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ func IsMathematicalOperator(s string) bool {
2323
case "-":
2424
case "*":
2525
case "/":
26+
case "div": // TODO: MySQL-specific operator - should be moved to engine-specific logic
27+
case "mod": // TODO: MySQL-specific operator - should be moved to engine-specific logic
2628
case "%":
2729
case "^":
2830
case "|/":

sqlc.exe

72.1 MB
Binary file not shown.

0 commit comments

Comments
 (0)