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