Skip to content

Commit 043680e

Browse files
EliSauderCopilot
andcommitted
fix(mssql): type a number with a decimal point as decimal
A literal such as 1.5 was typed as a float, and one too large for an int, as 2147483648, as one too. SQL Server types each as the decimal holding its digits, 1.5 as decimal(2,1) and 2147483648 as decimal(10,0), and so now does sqlc. 1e5 is still a float. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 444e97c commit 043680e

5 files changed

Lines changed: 141 additions & 5 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"command": "analyze",
3+
"args": ["--dialect", "mssql", "--schema", "schema.sql", "query.sql"],
4+
"contexts": ["base"]
5+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- name: Literals :many
2+
SELECT 1.5 AS a, 0.5 AS b, 10.00 AS c, 007.10 AS d, 2147483647 AS e, 2147483648 AS f, 12345678901 AS g, -1.5 AS h, 1e5 AS i FROM p;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE TABLE p (a NVARCHAR(10) NOT NULL, b NVARCHAR(20) NOT NULL, c VARCHAR(5) NOT NULL, d DECIMAL(10,2) NOT NULL, e DECIMAL(5,3) NOT NULL, i INT NOT NULL);
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
[
2+
{
3+
"name": "Literals",
4+
"cmd": ":many",
5+
"columns": [
6+
{
7+
"name": "a",
8+
"type": {
9+
"name": "decimal",
10+
"args": [
11+
{
12+
"int": 2
13+
},
14+
{
15+
"int": 1
16+
}
17+
]
18+
}
19+
},
20+
{
21+
"name": "b",
22+
"type": {
23+
"name": "decimal",
24+
"args": [
25+
{
26+
"int": 1
27+
},
28+
{
29+
"int": 1
30+
}
31+
]
32+
}
33+
},
34+
{
35+
"name": "c",
36+
"type": {
37+
"name": "decimal",
38+
"args": [
39+
{
40+
"int": 4
41+
},
42+
{
43+
"int": 2
44+
}
45+
]
46+
}
47+
},
48+
{
49+
"name": "d",
50+
"type": {
51+
"name": "decimal",
52+
"args": [
53+
{
54+
"int": 3
55+
},
56+
{
57+
"int": 2
58+
}
59+
]
60+
}
61+
},
62+
{
63+
"name": "e",
64+
"type": {
65+
"name": "int"
66+
}
67+
},
68+
{
69+
"name": "f",
70+
"type": {
71+
"name": "decimal",
72+
"args": [
73+
{
74+
"int": 10
75+
},
76+
{
77+
"int": 0
78+
}
79+
]
80+
}
81+
},
82+
{
83+
"name": "g",
84+
"type": {
85+
"name": "decimal",
86+
"args": [
87+
{
88+
"int": 11
89+
},
90+
{
91+
"int": 0
92+
}
93+
]
94+
}
95+
},
96+
{
97+
"name": "h",
98+
"type": {
99+
"name": "decimal"
100+
}
101+
},
102+
{
103+
"name": "i",
104+
"type": {
105+
"name": "float"
106+
}
107+
}
108+
],
109+
"params": []
110+
}
111+
]

‎internal/engine/mssql/convert.go‎

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package mssql
22

33
import (
4+
"math"
45
"strconv"
56
"strings"
67
"unicode/utf16"
@@ -780,16 +781,16 @@ func (c *cc) convertScalarExpression(expr tsql.ScalarExpression) ast.Node {
780781
case *tsql.VariableReference:
781782
return c.convertVariableReference(e)
782783
case *tsql.IntegerLiteral:
783-
ival, _ := strconv.ParseInt(e.Value, 10, 64)
784+
ival, err := strconv.ParseInt(e.Value, 10, 64)
785+
if err != nil || ival > math.MaxInt32 {
786+
return c.numericLiteral(e.Value, e)
787+
}
784788
return &ast.A_Const{
785789
Val: &ast.Integer{Ival: ival},
786790
Location: c.loc(e),
787791
}
788792
case *tsql.NumericLiteral:
789-
return &ast.A_Const{
790-
Val: &ast.Float{Str: e.Value},
791-
Location: c.loc(e),
792-
}
793+
return c.numericLiteral(e.Value, e)
793794
case *tsql.RealLiteral:
794795
return &ast.A_Const{
795796
Val: &ast.Float{Str: e.Value},
@@ -1594,6 +1595,22 @@ func (c *cc) convertCreateFunctionStatement(n *tsql.CreateFunctionStatement) ast
15941595
}
15951596
}
15961597

1598+
// numericLiteral is a number written with a decimal point, or too large
1599+
// for an int, which SQL Server types as the decimal holding its digits:
1600+
// 1.50 is a decimal(3,2), 2147483648 a decimal(10,0).
1601+
func (c *cc) numericLiteral(value string, n tsql.Node) ast.Node {
1602+
whole, frac, _ := strings.Cut(value, ".")
1603+
whole = strings.TrimLeft(whole, "0")
1604+
scale := int64(len(frac))
1605+
precision := max(int64(len(whole))+scale, 1)
1606+
return &ast.TypeCast{
1607+
Arg: &ast.A_Const{Val: &ast.Float{Str: value}, Location: c.loc(n)},
1608+
TypeName: &ast.TypeName{Name: "decimal", Typmods: appendTypmod(appendTypmod(nil,
1609+
&ast.A_Const{Val: &ast.Integer{Ival: precision}}), &ast.A_Const{Val: &ast.Integer{Ival: scale}})},
1610+
Location: c.loc(n),
1611+
}
1612+
}
1613+
15971614
// typeSpelling spells a type so that it parses back as the same type, which
15981615
// for a nullable one is the Nullable(T) it was spelled as.
15991616
func typeSpelling(tn *ast.TypeName, t *core.TypeExpr) string {

0 commit comments

Comments
 (0)