Skip to content

Commit 1c7f94c

Browse files
committed
merge scope column types for setop subqueries
1 parent 635d803 commit 1c7f94c

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

sql/planbuilder/set_op.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package planbuilder
1616

1717
import (
1818
"fmt"
19+
"github.com/dolthub/go-mysql-server/sql/types"
1920
"reflect"
2021

2122
ast "github.com/dolthub/vitess/go/vt/sqlparser"
@@ -144,10 +145,28 @@ func (b *Builder) buildSetOp(inScope *scope, u *ast.SetOp) (outScope *scope) {
144145
tabId := b.tabId
145146
ret := plan.NewSetOp(setOpType, leftScope.node, rightScope.node, distinct, limit, offset, sortFields).WithId(tabId).WithColumns(cols)
146147
outScope = leftScope
148+
outScope.cols = b.mergeSetOpScopeColumns(leftScope.cols, rightScope.cols, tabId)
147149
outScope.node = b.mergeSetOpSchemas(ret.(*plan.SetOp))
148150
return
149151
}
150152

153+
func (b *Builder) mergeSetOpScopeColumns(left, right []scopeColumn, tabId sql.TableId) []scopeColumn {
154+
merged := make([]scopeColumn, len(left))
155+
for i := range left {
156+
merged[i] = scopeColumn{
157+
tableId: tabId,
158+
db: left[i].db,
159+
table: left[i].table,
160+
col: left[i].col,
161+
originalCol: left[i].originalCol,
162+
id: left[i].id,
163+
typ: types.GeneralizeTypes(left[i].typ, right[i].typ),
164+
nullable: left[i].nullable || right[i].nullable,
165+
}
166+
}
167+
return merged
168+
}
169+
151170
func (b *Builder) mergeSetOpSchemas(u *plan.SetOp) sql.Node {
152171
ls, rs := u.Left().Schema(), u.Right().Schema()
153172
if len(ls) != len(rs) {

0 commit comments

Comments
 (0)