Skip to content

Commit 5ba6c85

Browse files
EliSauderCopilot
andcommitted
fix(core): type a set operation column of an argument-free type as its family
A column of UNION, INTERSECT or EXCEPT whose combined type has no arguments, as a string literal's varchar or decimal arithmetic's decimal, was looked up as a declared type, which takes the dialect's defaults: varchar(1) and decimal(18,0). It is now its family. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 95835ee commit 5ba6c85

3 files changed

Lines changed: 23 additions & 0 deletions

File tree

‎internal/core/analyzer/setop.go‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ func (a *analyzer) combineTypes(rules *core.SetOperationRules, l, r exprType) ex
6666
case len(out.Args) == 1 && len(loser.Args) == 1 && a.sameCategory(lt.Name, rt.Name):
6767
out.Args[0] = widerArg(out.Args[0], loser.Args[0])
6868
}
69+
// A type with no arguments is its family. Looked up as a declaration
70+
// it would take the dialect's defaults, as varchar is varchar(1).
71+
if len(out.Args) == 0 {
72+
if oid, err := a.cat.TypeOID(out.Name); err == nil {
73+
return exprType{typeOID: oid}
74+
}
75+
}
6976
return a.lookupType(out)
7077
}
7178

‎internal/endtoend/testdata/analyze_set_operations/mssql/query.sql‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,6 @@ UNION ALL
2727
SELECT amount FROM ledger
2828
UNION ALL
2929
SELECT flag FROM ledger;
30+
31+
-- name: LiteralUnion :many
32+
SELECT 'active' AS status UNION ALL SELECT 'inactive';

‎internal/endtoend/testdata/analyze_set_operations/mssql/stdout.json‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,5 +166,18 @@
166166
}
167167
],
168168
"params": []
169+
},
170+
{
171+
"name": "LiteralUnion",
172+
"cmd": ":many",
173+
"columns": [
174+
{
175+
"name": "status",
176+
"type": {
177+
"name": "varchar"
178+
}
179+
}
180+
],
181+
"params": []
169182
}
170183
]

0 commit comments

Comments
 (0)