11package analyzer
22
33import (
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