Skip to content

Commit 64e0b37

Browse files
EliSauderCopilot
andcommitted
refactor(core): select engine-owned type-combination policies explicitly
Keep SQL Server precedence and modifier rules in its typepolicy package, validate the serialized selector, and preserve the existing fallback for all other dialects. Cover invalid embedded seed metadata separately because it cannot be supplied through the CLI. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 5ba6c85 commit 64e0b37

8 files changed

Lines changed: 178 additions & 92 deletions

File tree

‎internal/core/analyzer/analyzer.go‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,10 @@ func (a *analyzer) analyzeSetOperation(s *ast.SelectStmt) error {
284284
a.columns = left.columns
285285
a.scope = left.scope
286286
if rules := a.cat.SetOperations(); rules != nil {
287-
a.columns = a.combineColumns(rules, s.Op, left.columns, right.columns)
287+
a.columns, err = a.combineColumns(rules, s.Op, left.columns, right.columns)
288+
if err != nil {
289+
return err
290+
}
288291
}
289292
return nil
290293
}

‎internal/core/analyzer/expr.go‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,12 @@ func (a *analyzer) typeCoalesce(e *ast.CoalesceExpr) (exprType, error) {
502502
types = append(types, t)
503503
nullable = nullable && t.nullable
504504
}
505-
t := a.commonType(types)
505+
t, err := a.commonType(types)
506+
if err != nil {
507+
return exprType{}, err
508+
}
509+
// Only ISNULL's converter sets FirstArgType. Ordinary COALESCE keeps
510+
// the common type selected above, rather than its first argument's.
506511
if e.FirstArgType {
507512
for _, first := range types {
508513
if first.typeOID != 0 || first.expr != nil {
@@ -531,7 +536,10 @@ func (a *analyzer) typeFirstOf(nodes []ast.Node, nullable bool) (exprType, error
531536
types = append(types, t)
532537
nullable = nullable || t.nullable
533538
}
534-
out := a.commonType(types)
539+
out, err := a.commonType(types)
540+
if err != nil {
541+
return exprType{}, err
542+
}
535543
a.inferAlternatives(nodes, out, false)
536544
out.nullable = nullable
537545
return out, nil

‎internal/core/analyzer/setop.go‎

Lines changed: 37 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package analyzer
22

33
import (
4-
"slices"
4+
"fmt"
55

66
"github.com/sqlc-dev/sqlc/internal/core"
7+
mssqltypes "github.com/sqlc-dev/sqlc/internal/engine/mssql/typepolicy"
78
"github.com/sqlc-dev/sqlc/internal/sql/ast"
89
)
910

@@ -12,7 +13,7 @@ import (
1213
// and is read from no one table. It may be NULL in a UNION when either
1314
// branch's may, in an INTERSECT when both may, and in an EXCEPT when the
1415
// first branch's may, since every row it returns is one of those.
15-
func (a *analyzer) combineColumns(rules *core.SetOperationRules, op ast.SetOperation, left, right []core.Column) []core.Column {
16+
func (a *analyzer) combineColumns(rules *core.SetOperationRules, op ast.SetOperation, left, right []core.Column) ([]core.Column, error) {
1617
out := make([]core.Column, len(left))
1718
for i, l := range left {
1819
if i >= len(right) {
@@ -27,60 +28,56 @@ func (a *analyzer) combineColumns(rules *core.SetOperationRules, op ast.SetOpera
2728
case ast.Except:
2829
col.NotNull = l.NotNull
2930
}
30-
t := a.combineTypes(rules, columnExprType(l), columnExprType(r))
31+
t, err := a.combineTypes(rules, columnExprType(l), columnExprType(r))
32+
if err != nil {
33+
return nil, err
34+
}
3135
t.nullable = !col.NotNull
3236
col.TypeOID = t.typeOID
3337
col.DataType, col.IsArray = a.typeNameOf(t)
3438
col.Type = a.typeExprOf(t)
3539
out[i] = col
3640
}
37-
return out
41+
return out, nil
3842
}
3943

40-
// combineTypes is the type a column takes from two branches: the one of
41-
// higher precedence, varying when either branch varies, with the length or
42-
// precision that holds both.
43-
func (a *analyzer) combineTypes(rules *core.SetOperationRules, l, r exprType) exprType {
44+
// combineTypes delegates type selection and modifiers to the selected policy,
45+
// then resolves its result against the catalog. No policy is enabled merely
46+
// by listing a dialect's types.
47+
func (a *analyzer) combineTypes(rules *core.SetOperationRules, l, r exprType) (exprType, error) {
4448
lt, rt := a.exprOf(l), a.exprOf(r)
4549
switch {
4650
case lt == nil:
47-
return r
51+
return r, nil
4852
case rt == nil:
49-
return l
50-
}
51-
li, ri := slices.Index(rules.Precedence, lt.Name), slices.Index(rules.Precedence, rt.Name)
52-
if li < 0 || ri < 0 {
53-
return l
53+
return l, nil
5454
}
55-
winner, loser := lt, rt
56-
if ri < li {
57-
winner, loser = rt, lt
55+
var out *core.TypeExpr
56+
var ok bool
57+
switch rules.Strategy {
58+
case "mssql":
59+
out, ok = mssqltypes.Combine(rules, lt, rt, a.sameCategory(lt.Name, rt.Name))
60+
default:
61+
return exprType{}, fmt.Errorf("unsupported type-combination strategy %q", rules.Strategy)
5862
}
59-
out := winner.WithNullable(false)
60-
if varying, ok := rules.Varying[out.Name]; ok && slices.Contains(mapValues(rules.Varying), loser.Name) {
61-
out.Name = varying
62-
}
63-
switch {
64-
case out.Name == rules.Decimal && rules.Decimal != "":
65-
combineDecimal(rules, out, loser)
66-
case len(out.Args) == 1 && len(loser.Args) == 1 && a.sameCategory(lt.Name, rt.Name):
67-
out.Args[0] = widerArg(out.Args[0], loser.Args[0])
63+
if !ok {
64+
return l, nil
6865
}
6966
// A type with no arguments is its family. Looked up as a declaration
7067
// it would take the dialect's defaults, as varchar is varchar(1).
7168
if len(out.Args) == 0 {
7269
if oid, err := a.cat.TypeOID(out.Name); err == nil {
73-
return exprType{typeOID: oid}
70+
return exprType{typeOID: oid}, nil
7471
}
7572
}
76-
return a.lookupType(out)
73+
return a.lookupType(out), nil
7774
}
7875

79-
// commonType is the type a dialect gives an expression that yields one of
80-
// several values, as CASE, COALESCE and GREATEST do, by the rules it
81-
// combines a set operation's branches by; a dialect without them takes the
82-
// first typed value's. The result is no one column's.
83-
func (a *analyzer) commonType(types []exprType) exprType {
76+
// commonType uses the enabled policy for alternative-valued expressions.
77+
// Currently only MSSQL combines these with its set-operation rules. Other
78+
// dialects retain the first typed value; this is a compatibility fallback,
79+
// not a claim that their databases use the same rules.
80+
func (a *analyzer) commonType(types []exprType) (exprType, error) {
8481
var out exprType
8582
rules := a.cat.SetOperations()
8683
for i, t := range types {
@@ -91,65 +88,19 @@ func (a *analyzer) commonType(types []exprType) exprType {
9188
case out.typeOID == 0 && out.expr == nil:
9289
out = t
9390
case rules == nil:
94-
return out
91+
return out, nil
9592
default:
96-
out = a.combineTypes(rules, out, t)
93+
var err error
94+
out, err = a.combineTypes(rules, out, t)
95+
if err != nil {
96+
return exprType{}, err
97+
}
9798
}
9899
if i > 0 {
99100
out.sourceClassOID, out.sourceAttributeOID, out.sourceTableAlias = 0, 0, ""
100101
}
101102
}
102-
return out
103-
}
104-
105-
func mapValues(m map[string]string) []string {
106-
out := make([]string, 0, len(m))
107-
for _, v := range m {
108-
out = append(out, v)
109-
}
110-
return out
111-
}
112-
113-
// widerArg is the larger of two lengths or precisions, MAX above any.
114-
func widerArg(a, b core.TypeArg) core.TypeArg {
115-
switch {
116-
case a.Ident != nil:
117-
return a
118-
case b.Ident != nil:
119-
return b
120-
case a.Int != nil && b.Int != nil && *b.Int > *a.Int:
121-
return b
122-
}
123-
return a
124-
}
125-
126-
// combineDecimal gives the exact numeric out the precision and scale that
127-
// hold both it and the other branch's type: the larger scale, and the
128-
// larger count of integer digits, with scale giving way past the maximum
129-
// precision. A family with no decimal form leaves out as it is.
130-
func combineDecimal(rules *core.SetOperationRules, out, other *core.TypeExpr) {
131-
p1, s1, ok1 := decimalForm(rules, out)
132-
p2, s2, ok2 := decimalForm(rules, other)
133-
if !ok1 || !ok2 {
134-
return
135-
}
136-
scale := max(s1, s2)
137-
precision := max(p1-s1, p2-s2) + scale
138-
if limit := rules.DecimalMaxPrecision; limit > 0 && precision > limit {
139-
scale = max(scale-(precision-limit), 0)
140-
precision = limit
141-
}
142-
out.Args = []core.TypeArg{{Int: &precision}, {Int: &scale}}
143-
}
144-
145-
func decimalForm(rules *core.SetOperationRules, t *core.TypeExpr) (int64, int64, bool) {
146-
if t.Name == rules.Decimal && len(t.Args) == 2 && t.Args[0].Int != nil && t.Args[1].Int != nil {
147-
return *t.Args[0].Int, *t.Args[1].Int, true
148-
}
149-
if form, ok := rules.AsDecimal[t.Name]; ok {
150-
return form[0], form[1], true
151-
}
152-
return 0, 0, false
103+
return out, nil
153104
}
154105

155106
// sameCategory reports whether two types are of one category, so that the

‎internal/core/dialect.go‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,15 @@ func (c *Catalog) QualifiesDuplicateColumns() bool {
201201
return v == "true"
202202
}
203203

204-
// SetOperationRules say how a dialect types a column of UNION, INTERSECT or
205-
// EXCEPT from its branches, as SQL Server does by data type precedence. A
206-
// dialect without them types the column from the first branch.
204+
// SetOperationRules select a dialect's type-combination policy. A dialect
205+
// without one keeps the analyzer's first-branch/first-typed-value behavior;
206+
// that fallback is not a model of the database's coercion rules.
207207
type SetOperationRules struct {
208+
// Strategy selects an implemented policy, currently "mssql". Other
209+
// engines need their own policy, not just another precedence list.
210+
Strategy string `json:"strategy"`
211+
212+
// The remaining settings parameterize the MSSQL policy.
208213
// Precedence lists type families, highest first: the column takes the
209214
// highest of its branches'.
210215
Precedence []string `json:"precedence"`

‎internal/core/seed/seed.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,9 @@ func loadSettings(fsys fs.FS) (Settings, error) {
370370
if settings.Dialect == "" {
371371
return Settings{}, fmt.Errorf("seed: %s: dialect has no name", SettingsFile)
372372
}
373+
if rules := settings.SetOperations; rules != nil && rules.Strategy != "mssql" {
374+
return Settings{}, fmt.Errorf("seed: %s: unsupported type-combination strategy %q", SettingsFile, rules.Strategy)
375+
}
373376
settings.fsys = fsys
374377
return settings, nil
375378
}

‎internal/core/seed/seed_test.go‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package seed
2+
3+
import (
4+
"strings"
5+
"testing"
6+
"testing/fstest"
7+
)
8+
9+
// Dialect seeds are embedded, so the CLI cannot supply invalid policy metadata.
10+
func TestTypeCombinationStrategy(t *testing.T) {
11+
tests := []struct {
12+
name string
13+
settings string
14+
wantErr string
15+
}{
16+
{"disabled", `{"dialect":"postgresql"}`, ""},
17+
{"mssql", `{"dialect":"mssql","set_operations":{"strategy":"mssql"}}`, ""},
18+
{"missing", `{"dialect":"mssql","set_operations":{}}`, `unsupported type-combination strategy ""`},
19+
{"unknown", `{"dialect":"mssql","set_operations":{"strategy":"unknown"}}`, `unsupported type-combination strategy "unknown"`},
20+
}
21+
for _, tt := range tests {
22+
t.Run(tt.name, func(t *testing.T) {
23+
_, err := loadSettings(fstest.MapFS{
24+
SettingsFile: &fstest.MapFile{Data: []byte(tt.settings)},
25+
})
26+
if tt.wantErr == "" {
27+
if err != nil {
28+
t.Fatal(err)
29+
}
30+
} else if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
31+
t.Fatalf("got %v, want error containing %q", err, tt.wantErr)
32+
}
33+
})
34+
}
35+
}

‎internal/engine/mssql/dialect/dialect.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
},
1010
"bool": "bit",
1111
"set_operations": {
12+
"strategy": "mssql",
1213
"precedence": [
1314
"sql_variant", "xml", "datetimeoffset", "datetime2", "datetime",
1415
"smalldatetime", "date", "time", "float", "real", "decimal", "money",
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Package typepolicy implements SQL Server's type-combination rules without
2+
// depending on its parser or the shared analyzer.
3+
package typepolicy
4+
5+
import (
6+
"slices"
7+
8+
"github.com/sqlc-dev/sqlc/internal/core"
9+
)
10+
11+
// Combine applies SQL Server's precedence and set-operation type modifiers.
12+
// sameCategory says whether lengths/precisions of the two families are
13+
// comparable. An unlisted family leaves type selection to the caller.
14+
func Combine(rules *core.SetOperationRules, left, right *core.TypeExpr, sameCategory bool) (*core.TypeExpr, bool) {
15+
li, ri := slices.Index(rules.Precedence, left.Name), slices.Index(rules.Precedence, right.Name)
16+
if li < 0 || ri < 0 {
17+
return nil, false
18+
}
19+
winner, loser := left, right
20+
if ri < li {
21+
winner, loser = right, left
22+
}
23+
out := winner.WithNullable(false)
24+
if varying, ok := rules.Varying[out.Name]; ok {
25+
for _, name := range rules.Varying {
26+
if loser.Name == name {
27+
out.Name = varying
28+
break
29+
}
30+
}
31+
}
32+
switch {
33+
case out.Name == rules.Decimal && rules.Decimal != "":
34+
combineDecimal(rules, out, loser)
35+
case len(out.Args) == 1 && len(loser.Args) == 1 && sameCategory:
36+
out.Args[0] = widerArg(out.Args[0], loser.Args[0])
37+
}
38+
return out, true
39+
}
40+
41+
// widerArg is the larger of two lengths or precisions, MAX above any.
42+
func widerArg(a, b core.TypeArg) core.TypeArg {
43+
switch {
44+
case a.Ident != nil:
45+
return a
46+
case b.Ident != nil:
47+
return b
48+
case a.Int != nil && b.Int != nil && *b.Int > *a.Int:
49+
return b
50+
}
51+
return a
52+
}
53+
54+
// combineDecimal applies SQL Server's UNION/INTERSECT/EXCEPT formula:
55+
// retain the larger scale and integer-digit count, reducing scale at the
56+
// precision limit. This is not a cross-engine decimal-coercion rule.
57+
func combineDecimal(rules *core.SetOperationRules, out, other *core.TypeExpr) {
58+
p1, s1, ok1 := decimalForm(rules, out)
59+
p2, s2, ok2 := decimalForm(rules, other)
60+
if !ok1 || !ok2 {
61+
return
62+
}
63+
scale := max(s1, s2)
64+
precision := max(p1-s1, p2-s2) + scale
65+
if limit := rules.DecimalMaxPrecision; limit > 0 && precision > limit {
66+
scale = max(scale-(precision-limit), 0)
67+
precision = limit
68+
}
69+
out.Args = []core.TypeArg{{Int: &precision}, {Int: &scale}}
70+
}
71+
72+
func decimalForm(rules *core.SetOperationRules, t *core.TypeExpr) (int64, int64, bool) {
73+
if t.Name == rules.Decimal && len(t.Args) == 2 && t.Args[0].Int != nil && t.Args[1].Int != nil {
74+
return *t.Args[0].Int, *t.Args[1].Int, true
75+
}
76+
if form, ok := rules.AsDecimal[t.Name]; ok {
77+
return form[0], form[1], true
78+
}
79+
return 0, 0, false
80+
}

0 commit comments

Comments
 (0)