Skip to content

Commit f669ca0

Browse files
EliSauderCopilot
andcommitted
refactor(core): inject engine-owned type combiners into analysis
Remove SQL Server dispatch and typed policy settings from shared analysis and seeding. Bind the optional engine factory after catalog creation or restore, inherit it in nested queries, and retain existing fallback inference when no combiner handles a context. Cover nested SQL Server combination end to end and inaccessible policy-contract/configuration failures with focused tests. Preserve single-candidate column provenance and all existing engine goldens. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 002e9f6 commit f669ca0

16 files changed

Lines changed: 415 additions & 194 deletions

File tree

‎internal/compiler/compile.go‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,23 @@ func (c *Compiler) parseCatalogCore(files []schemaFile, merr *multierr.Error) er
143143
// Whatever apply reported is already in merr, which the caller returns.
144144
c.coreCatalog = cat
145145

146+
// Runtime behavior is wired after both seed and restore paths. Installing
147+
// it through a seed option would miss cache hits.
148+
if c.newCoreTypeCombiner != nil {
149+
c.coreTypeCombiner, err = c.newCoreTypeCombiner(cat)
150+
if err != nil {
151+
return err
152+
}
153+
} else {
154+
data, err := cat.TypeCombinationData()
155+
if err != nil {
156+
return err
157+
}
158+
if len(data) > 0 {
159+
return fmt.Errorf("type-combination data requires an engine-supplied combiner")
160+
}
161+
}
162+
146163
// Codegen consumes the catalog through the Result, in the legacy shape.
147164
legacy, err := coreResultCatalog(cat)
148165
if err != nil {

‎internal/compiler/engine.go‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/sqlc-dev/sqlc/internal/engine/duckdb"
1414
"github.com/sqlc-dev/sqlc/internal/engine/googlesql"
1515
"github.com/sqlc-dev/sqlc/internal/engine/mssql"
16+
mssqltypes "github.com/sqlc-dev/sqlc/internal/engine/mssql/typepolicy"
1617
"github.com/sqlc-dev/sqlc/internal/engine/postgresql"
1718
pganalyze "github.com/sqlc-dev/sqlc/internal/engine/postgresql/analyzer"
1819
"github.com/sqlc-dev/sqlc/internal/engine/sqlite"
@@ -37,8 +38,10 @@ type Compiler struct {
3738
// and coreDialect seeds the catalog it does so with. The catalog itself is
3839
// not built until the schema is loaded, because the two are cached
3940
// together.
40-
coreAnalysis bool
41-
coreDialect core.Option
41+
coreAnalysis bool
42+
coreDialect core.Option
43+
coreTypeCombiner core.TypeCombiner
44+
newCoreTypeCombiner func(*core.Catalog) (core.TypeCombiner, error)
4245

4346
// newParser builds a parser for the configured engine, and is set for
4447
// every engine either path supports. The core path analyzes statements
@@ -153,6 +156,7 @@ func (c *Compiler) initCore() error {
153156
c.newParser = func() Parser { return mssql.NewParser() }
154157
c.selector = newDefaultSelector()
155158
dialect = mssql.Dialect()
159+
c.newCoreTypeCombiner = mssqltypes.New
156160
case config.EngineDuckDB:
157161
c.newParser = func() Parser { return duckdb.NewParser() }
158162
c.selector = newDefaultSelector()

‎internal/compiler/parse_core.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func (c *Compiler) parseQueryCore(raw *ast.RawStmt, src string, pre *preprocess.
5757
var params []Parameter
5858
switch raw.Stmt.(type) {
5959
case *ast.SelectStmt, *ast.InsertStmt, *ast.UpdateStmt, *ast.DeleteStmt:
60-
res, err := coreanalyzer.Prepare(c.coreCatalog, raw)
60+
res, err := coreanalyzer.Prepare(c.coreCatalog, raw, coreanalyzer.WithTypeCombiner(c.coreTypeCombiner))
6161
if err != nil {
6262
return nil, err
6363
}

‎internal/core/analyzer/analyzer.go‎

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@ import (
77
"github.com/sqlc-dev/sqlc/internal/sql/ast"
88
)
99

10-
func Prepare(cat *core.Catalog, stmt ast.Node) (core.PrepareResult, error) {
10+
type Option func(*analyzer)
11+
12+
func WithTypeCombiner(combiner core.TypeCombiner) Option {
13+
return func(a *analyzer) { a.combiner = combiner }
14+
}
15+
16+
func Prepare(cat *core.Catalog, stmt ast.Node, options ...Option) (core.PrepareResult, error) {
1117
if rs, ok := stmt.(*ast.RawStmt); ok {
1218
stmt = rs.Stmt
1319
}
@@ -16,6 +22,9 @@ func Prepare(cat *core.Catalog, stmt ast.Node) (core.PrepareResult, error) {
1622
params: map[int]core.Parameter{},
1723
stars: &[]core.StarExpansion{},
1824
}
25+
for _, option := range options {
26+
option(a)
27+
}
1928
switch s := stmt.(type) {
2029
case *ast.SelectStmt:
2130
if err := a.analyzeSelect(s); err != nil {
@@ -44,11 +53,12 @@ func Prepare(cat *core.Catalog, stmt ast.Node) (core.PrepareResult, error) {
4453
}
4554

4655
type analyzer struct {
47-
cat *core.Catalog
48-
scope *scope
49-
columns []core.Column
50-
params map[int]core.Parameter
51-
command core.Command
56+
cat *core.Catalog
57+
combiner core.TypeCombiner
58+
scope *scope
59+
columns []core.Column
60+
params map[int]core.Parameter
61+
command core.Command
5262

5363
// outer is the scope of the query this one is nested in, which a
5464
// correlated subquery refers to.
@@ -83,11 +93,12 @@ func (a *analyzer) recordStar(s core.StarExpansion) {
8393
// query's scope, so a correlated reference resolves.
8494
func (a *analyzer) subquery(s *ast.SelectStmt) (*analyzer, error) {
8595
sub := &analyzer{
86-
cat: a.cat,
87-
params: a.params,
88-
outer: a.scope,
89-
ctes: a.ctes,
90-
stars: a.stars,
96+
cat: a.cat,
97+
combiner: a.combiner,
98+
params: a.params,
99+
outer: a.scope,
100+
ctes: a.ctes,
101+
stars: a.stars,
91102
}
92103
if err := sub.analyzeSelect(s); err != nil {
93104
return nil, err
@@ -283,8 +294,8 @@ func (a *analyzer) analyzeSetOperation(s *ast.SelectStmt) error {
283294
}
284295
a.columns = left.columns
285296
a.scope = left.scope
286-
if rules := a.cat.SetOperations(); rules != nil {
287-
a.columns, err = a.combineColumns(rules, s.Op, left.columns, right.columns)
297+
if a.combiner != nil {
298+
a.columns, err = a.combineColumns(s.Op, left.columns, right.columns)
288299
if err != nil {
289300
return err
290301
}

‎internal/core/analyzer/expr.go‎

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,11 @@ func (a *analyzer) typeExpr(n ast.Node) (exprType, error) {
7474
return a.typeCoalesce(e)
7575

7676
case *ast.MinMaxExpr:
77-
return a.typeFirstOf(listItems(e.Args), false)
77+
context := core.CombineGreatest
78+
if e.Op != 0 {
79+
context = core.CombineLeast
80+
}
81+
return a.typeFirstOf(context, listItems(e.Args), false)
7882

7983
case *ast.A_ArrayExpr:
8084
return a.typeArrayExpr(e)
@@ -481,16 +485,15 @@ func (a *analyzer) typeCase(e *ast.CaseExpr) (exprType, error) {
481485
if e.Defresult != nil {
482486
results = append(results, e.Defresult)
483487
}
484-
t, err := a.typeFirstOf(results, e.Defresult == nil)
488+
t, err := a.typeFirstOf(core.CombineCase, results, e.Defresult == nil)
485489
if err != nil {
486490
return exprType{}, err
487491
}
488492
return t, nil
489493
}
490494

491495
// typeCoalesce types COALESCE, which is null only when every argument is,
492-
// and of the type its arguments combine to, or its first typed argument's
493-
// for SQL Server's ISNULL.
496+
// and uses a common type unless the AST requests the first typed argument.
494497
func (a *analyzer) typeCoalesce(e *ast.CoalesceExpr) (exprType, error) {
495498
types := make([]exprType, 0, len(listItems(e.Args)))
496499
nullable := true
@@ -502,18 +505,14 @@ func (a *analyzer) typeCoalesce(e *ast.CoalesceExpr) (exprType, error) {
502505
types = append(types, t)
503506
nullable = nullable && t.nullable
504507
}
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.
508+
var t exprType
511509
if e.FirstArgType {
512-
for _, first := range types {
513-
if first.typeOID != 0 || first.expr != nil {
514-
t = first
515-
break
516-
}
510+
t = firstTyped(types)
511+
} else {
512+
var err error
513+
t, err = a.commonType(core.CombineCoalesce, types)
514+
if err != nil {
515+
return exprType{}, err
517516
}
518517
}
519518
// A placeholder there is one that may be NULL.
@@ -526,7 +525,7 @@ func (a *analyzer) typeCoalesce(e *ast.CoalesceExpr) (exprType, error) {
526525
// to, which without a dialect's rules is the first typed one's. The result
527526
// is nullable when any alternative is, or when the caller says the set is
528527
// not exhaustive.
529-
func (a *analyzer) typeFirstOf(nodes []ast.Node, nullable bool) (exprType, error) {
528+
func (a *analyzer) typeFirstOf(context core.TypeCombinationContext, nodes []ast.Node, nullable bool) (exprType, error) {
530529
types := make([]exprType, 0, len(nodes))
531530
for _, n := range nodes {
532531
t, err := a.typeExpr(n)
@@ -536,7 +535,7 @@ func (a *analyzer) typeFirstOf(nodes []ast.Node, nullable bool) (exprType, error
536535
types = append(types, t)
537536
nullable = nullable || t.nullable
538537
}
539-
out, err := a.commonType(types)
538+
out, err := a.commonType(context, types)
540539
if err != nil {
541540
return exprType{}, err
542541
}
@@ -560,7 +559,7 @@ func (a *analyzer) inferAlternatives(nodes []ast.Node, t exprType, nullable bool
560559

561560
// typeArrayExpr types ARRAY[...], whose type is an array of its elements'.
562561
func (a *analyzer) typeArrayExpr(e *ast.A_ArrayExpr) (exprType, error) {
563-
elemT, err := a.typeFirstOf(listItems(e.Elements), false)
562+
elemT, err := a.typeFirstOf(core.CombineArray, listItems(e.Elements), false)
564563
if err != nil {
565564
return exprType{}, err
566565
}

‎internal/core/analyzer/setop.go‎

Lines changed: 56 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package analyzer
22

33
import (
4-
"fmt"
5-
64
"github.com/sqlc-dev/sqlc/internal/core"
7-
mssqltypes "github.com/sqlc-dev/sqlc/internal/engine/mssql/typepolicy"
85
"github.com/sqlc-dev/sqlc/internal/sql/ast"
96
)
107

@@ -13,7 +10,14 @@ import (
1310
// and is read from no one table. It may be NULL in a UNION when either
1411
// branch's may, in an INTERSECT when both may, and in an EXCEPT when the
1512
// first branch's may, since every row it returns is one of those.
16-
func (a *analyzer) combineColumns(rules *core.SetOperationRules, op ast.SetOperation, left, right []core.Column) ([]core.Column, error) {
13+
func (a *analyzer) combineColumns(op ast.SetOperation, left, right []core.Column) ([]core.Column, error) {
14+
context := core.CombineUnion
15+
switch op {
16+
case ast.Intersect:
17+
context = core.CombineIntersect
18+
case ast.Except:
19+
context = core.CombineExcept
20+
}
1721
out := make([]core.Column, len(left))
1822
for i, l := range left {
1923
if i >= len(right) {
@@ -28,10 +32,14 @@ func (a *analyzer) combineColumns(rules *core.SetOperationRules, op ast.SetOpera
2832
case ast.Except:
2933
col.NotNull = l.NotNull
3034
}
31-
t, err := a.combineTypes(rules, columnExprType(l), columnExprType(r))
35+
t, handled, err := a.combineTypes(context, []exprType{columnExprType(l), columnExprType(r)})
3236
if err != nil {
3337
return nil, err
3438
}
39+
if !handled {
40+
out[i] = l
41+
continue
42+
}
3543
t.nullable = !col.NotNull
3644
col.TypeOID = t.typeOID
3745
col.DataType, col.IsArray = a.typeNameOf(t)
@@ -41,83 +49,65 @@ func (a *analyzer) combineColumns(rules *core.SetOperationRules, op ast.SetOpera
4149
return out, nil
4250
}
4351

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) {
48-
lt, rt := a.exprOf(l), a.exprOf(r)
49-
switch {
50-
case lt == nil:
51-
return r, nil
52-
case rt == nil:
53-
return l, nil
52+
// combineTypes delegates selection and modifiers to engine-supplied behavior.
53+
// The whole list is passed without discarding untyped candidate positions.
54+
func (a *analyzer) combineTypes(context core.TypeCombinationContext, types []exprType) (exprType, bool, error) {
55+
if a.combiner == nil {
56+
return exprType{}, false, nil
5457
}
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)
58+
candidates := make([]*core.TypeExpr, len(types))
59+
typed := 0
60+
for i, t := range types {
61+
candidates[i] = a.exprOf(t)
62+
if candidates[i] != nil {
63+
typed++
64+
}
65+
}
66+
result, err := a.combiner.Combine(core.TypeCombinationRequest{Context: context, Candidates: candidates})
67+
if err != nil {
68+
return exprType{}, false, err
69+
}
70+
if !result.Handled {
71+
return exprType{}, false, nil
6272
}
63-
if !ok {
64-
return l, nil
73+
out := result.Type
74+
if out == nil {
75+
return exprType{}, true, nil
76+
}
77+
// Preserve the existing column provenance when only the first candidate
78+
// is typed and the combiner leaves its type unchanged.
79+
if typed == 1 && candidates[0] != nil && out.Key() == candidates[0].Key() {
80+
return types[0], true, nil
6581
}
6682
// A type with no arguments is its family. Looked up as a declaration
6783
// it would take the dialect's defaults, as varchar is varchar(1).
6884
if len(out.Args) == 0 {
6985
if oid, err := a.cat.TypeOID(out.Name); err == nil {
70-
return exprType{typeOID: oid}, nil
86+
return exprType{typeOID: oid}, true, nil
7187
}
7288
}
73-
return a.lookupType(out), nil
89+
return a.lookupType(out), true, nil
90+
}
91+
92+
// commonType retains first-typed-value inference when no combiner handles
93+
// the context. This compatibility fallback is not database coercion semantics.
94+
func (a *analyzer) commonType(context core.TypeCombinationContext, types []exprType) (exprType, error) {
95+
out, handled, err := a.combineTypes(context, types)
96+
if err != nil || handled {
97+
return out, err
98+
}
99+
return firstTyped(types), nil
74100
}
75101

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) {
81-
var out exprType
82-
rules := a.cat.SetOperations()
102+
func firstTyped(types []exprType) exprType {
83103
for i, t := range types {
84104
if t.typeOID == 0 && t.expr == nil {
85105
continue
86106
}
87-
switch {
88-
case out.typeOID == 0 && out.expr == nil:
89-
out = t
90-
case rules == nil:
91-
return out, nil
92-
default:
93-
var err error
94-
out, err = a.combineTypes(rules, out, t)
95-
if err != nil {
96-
return exprType{}, err
97-
}
98-
}
99107
if i > 0 {
100-
out.sourceClassOID, out.sourceAttributeOID, out.sourceTableAlias = 0, 0, ""
101-
}
102-
}
103-
return out, nil
104-
}
105-
106-
// sameCategory reports whether two types are of one category, so that the
107-
// length of one says something about the other: the 20 of varchar(20)
108-
// widens an nvarchar(10), but not a datetime2(3).
109-
func (a *analyzer) sameCategory(x, y string) bool {
110-
category := func(name string) string {
111-
oid, err := a.cat.TypeOID(name)
112-
if err != nil {
113-
return ""
114-
}
115-
t, err := a.cat.LookupType(a.familyOID(oid))
116-
if err != nil {
117-
return ""
108+
t.sourceClassOID, t.sourceAttributeOID, t.sourceTableAlias = 0, 0, ""
118109
}
119-
return t.Category
110+
return t
120111
}
121-
cx := category(x)
122-
return cx != "" && cx == category(y)
112+
return exprType{}
123113
}

0 commit comments

Comments
 (0)