Skip to content

Commit 48a9f7a

Browse files
committed
mysql: unsigned families, typmods, enum and set members, and the types MySQL reports for casts
A column's UNSIGNED reaches the core as part of its family, which the seed now lists for every numeric type rather than as an alias of the signed one, so codegen picks uint64 again on the core path. The converter carries the width of tinyint(1) and bit(n), a fractional-seconds precision and the members of an enum or set, and names a cast's type as MySQL types its result: varchar for CAST AS CHAR rather than the parser's var_string. A canonicalizer fills in decimal(10,0), folds float(p) and spells boolean as tinyint(1), as MySQL does. goldeneye reads a table column's whole type from COLUMN_TYPE, in the relations seed and in the analyze check, and compares an expression by family alone, since the wire carries no more. A cast is NULL when its operand is, in every dialect. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Qryf7a1doPFuCz2eGT3bCr
1 parent fedc4a1 commit 48a9f7a

15 files changed

Lines changed: 991 additions & 113 deletions

File tree

‎internal/core/analyzer/expr.go‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -932,9 +932,19 @@ func (a *analyzer) typeTypeCast(c *ast.TypeCast) (exprType, error) {
932932
return exprType{}, fmt.Errorf("cast: missing target type")
933933
}
934934
t := a.lookupType(target)
935-
// A cast is how a query says what an otherwise untyped placeholder holds.
936-
if err := a.typeOperands(c.Arg, t); err != nil {
935+
// A cast is how a query says what an otherwise untyped placeholder
936+
// holds, and a placeholder so typed is not null. Anything else cast
937+
// is NULL exactly when it was NULL before.
938+
if pr, ok := c.Arg.(*ast.ParamRef); ok {
939+
if err := a.typeOperands(pr, t); err != nil {
940+
return exprType{}, err
941+
}
942+
return t, nil
943+
}
944+
arg, err := a.typeExpr(c.Arg)
945+
if err != nil {
937946
return exprType{}, err
938947
}
948+
t.nullable = arg.nullable
939949
return t, nil
940950
}

‎internal/core/seed/seed.go‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,15 +388,22 @@ func Relations(fsys fs.FS, dir, schema string) ([]*catalog.Table, error) {
388388
Columns: make([]*catalog.Column, 0, len(rel.Columns)),
389389
}
390390
for _, col := range rel.Columns {
391+
// A column's type may carry arguments, as MySQL's varchar(64)
392+
// does; the legacy catalog holds the family and the length
393+
// apart.
394+
t := core.ParseTypeExpr(col.Type)
391395
column := &catalog.Column{
392396
Name: col.Name,
393-
Type: ast.TypeName{Name: col.Type},
397+
Type: ast.TypeName{Name: t.Name},
394398
IsNotNull: col.NotNull,
395399
IsArray: col.Array,
396400
}
397401
if col.Length > 0 {
398402
length := col.Length
399403
column.Length = &length
404+
} else if len(t.Args) > 0 && t.Args[0].Int != nil {
405+
length := int(*t.Args[0].Int)
406+
column.Length = &length
400407
}
401408
table.Columns = append(table.Columns, column)
402409
}

‎internal/core/typeexpr.go‎

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,23 @@ func quotedEnd(s string) int {
187187
}
188188

189189
// splitTypeArgs splits `Base(arg, arg)` into its base name and top-level
190-
// arguments, leaving nested parentheses and quoted strings intact.
190+
// arguments, leaving nested parentheses and quoted strings intact. Words
191+
// after the closing parenthesis belong to the name, since MySQL writes
192+
// decimal(10,2) unsigned and PostgreSQL timestamp(3) with time zone.
191193
func splitTypeArgs(t string) (string, []string) {
192194
open := strings.IndexByte(t, '(')
193-
if open < 0 || !strings.HasSuffix(t, ")") {
195+
if open < 0 {
196+
return t, nil
197+
}
198+
close := matchingParen(t, open)
199+
if close < 0 {
194200
return t, nil
195201
}
196202
base := strings.TrimSpace(t[:open])
197-
inner := t[open+1 : len(t)-1]
203+
if rest := strings.TrimSpace(t[close+1:]); rest != "" {
204+
base += " " + rest
205+
}
206+
inner := t[open+1 : close]
198207
var (
199208
args []string
200209
depth int
@@ -227,6 +236,34 @@ func splitTypeArgs(t string) (string, []string) {
227236
return base, args
228237
}
229238

239+
// matchingParen finds the parenthesis closing the one at open, skipping
240+
// nested parentheses and quoted strings, or -1 when it is not closed.
241+
func matchingParen(t string, open int) int {
242+
depth := 0
243+
var quote byte
244+
for i := open; i < len(t); i++ {
245+
c := t[i]
246+
switch {
247+
case quote != 0:
248+
if c == '\\' {
249+
i++
250+
} else if c == quote {
251+
quote = 0
252+
}
253+
case c == '\'' || c == '"' || c == '`':
254+
quote = c
255+
case c == '(':
256+
depth++
257+
case c == ')':
258+
depth--
259+
if depth == 0 {
260+
return i
261+
}
262+
}
263+
}
264+
return -1
265+
}
266+
230267
// HasNullable reports whether the expression marks nullability anywhere,
231268
// which tells whether the spelling it came from said so itself.
232269
func (t *TypeExpr) HasNullable() bool {

‎internal/core/typename.go‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,19 @@ func ColumnTypeExpr(col *ast.ColumnDef) *TypeExpr {
6161
if t == nil {
6262
return nil
6363
}
64+
// MySQL reports an unsigned column on the definition, and the
65+
// members of an enum or set apart from the type's name.
66+
if col.IsUnsigned && !strings.Contains(t.Name, " unsigned") {
67+
t.Name += " unsigned"
68+
}
69+
if vals := listItems(col.Vals); len(vals) > 0 && len(t.Args) == 0 {
70+
for _, item := range vals {
71+
if s, ok := item.(*ast.String); ok {
72+
v := s.Str
73+
t.Args = append(t.Args, TypeArg{String: &v})
74+
}
75+
}
76+
}
6477
if col.TypeName.Spelling != "" || listItems(col.TypeName.ArrayBounds) != nil {
6578
return t
6679
}

‎internal/endtoend/testdata/analyze_system_catalog/mysql/stdout.json‎

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,25 @@
66
{
77
"name": "table_name",
88
"type": {
9-
"name": "varchar"
9+
"name": "varchar",
10+
"args": [
11+
{
12+
"int": 64
13+
}
14+
]
1015
},
1116
"table": "columns"
1217
},
1318
{
1419
"name": "column_name",
1520
"type": {
1621
"name": "varchar",
17-
"nullable": true
22+
"nullable": true,
23+
"args": [
24+
{
25+
"int": 64
26+
}
27+
]
1828
},
1929
"table": "columns"
2030
},
@@ -33,7 +43,12 @@
3343
"column": {
3444
"name": "table_schema",
3545
"type": {
36-
"name": "varchar"
46+
"name": "varchar",
47+
"args": [
48+
{
49+
"int": 64
50+
}
51+
]
3752
},
3853
"table": "columns"
3954
}
@@ -57,7 +72,18 @@
5772
"column": {
5873
"name": "table_type",
5974
"type": {
60-
"name": "enum"
75+
"name": "enum",
76+
"args": [
77+
{
78+
"string": "base table"
79+
},
80+
{
81+
"string": "view"
82+
},
83+
{
84+
"string": "system view"
85+
}
86+
]
6187
},
6288
"table": "tables"
6389
}
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", "mysql", "--schema", "schema.sql", "query.sql"],
4+
"contexts": ["base"]
5+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
-- name: AllTypes :many
2+
SELECT * FROM things;
3+
4+
-- name: Casts :one
5+
SELECT
6+
CAST(count AS UNSIGNED) AS a,
7+
CAST(price AS DECIMAL(5,2)) AS b,
8+
CAST(title AS CHAR(10)) AS c,
9+
CAST(count AS SIGNED) AS d,
10+
CAST(doc AS JSON) AS e,
11+
CAST(created AS DATETIME(3)) AS f,
12+
CAST(price AS DECIMAL) AS g,
13+
CAST(key16 AS BINARY(8)) AS h
14+
FROM things;
15+
16+
-- name: Params :one
17+
SELECT id FROM things
18+
WHERE count = ? AND price = ? AND kind = ? AND flags = ? AND title = ? AND uprice = ? AND flag = ? AND created = ?;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
CREATE TABLE things (
2+
id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
3+
flag TINYINT(1),
4+
count INT UNSIGNED NOT NULL,
5+
price DECIMAL(10,2) NOT NULL,
6+
uprice DECIMAL(10,2) UNSIGNED,
7+
plain DECIMAL,
8+
ratio FLOAT(7,4),
9+
f FLOAT,
10+
title VARCHAR(255),
11+
code CHAR(3),
12+
kind ENUM('a','b'),
13+
flags SET('x','y'),
14+
doc JSON,
15+
key16 BINARY(16),
16+
blob255 VARBINARY(255),
17+
created DATETIME(6),
18+
updated TIMESTAMP(3),
19+
y YEAR,
20+
bits BIT(8),
21+
body TEXT,
22+
bin VARCHAR(10) CHARACTER SET binary,
23+
geo GEOMETRY,
24+
ok BOOLEAN,
25+
d DOUBLE,
26+
small MEDIUMINT UNSIGNED,
27+
i INT
28+
);

0 commit comments

Comments
 (0)