Skip to content

Commit 92e8581

Browse files
committed
fix server errs
1 parent db0f0db commit 92e8581

File tree

6 files changed

+34
-19
lines changed

6 files changed

+34
-19
lines changed

enginetest/queries/script_queries.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ var ScriptTests = []ScriptTest{
129129
},
130130
Assertions: []ScriptTestAssertion{
131131
{
132-
// Test UNION with BIT column and integer constant (related to customer's "48 is beyond maximum value" error)
132+
// Reproduces customer's "48 is beyond maximum value" error when BIT and integer constants are UNIONed
133133
Query: `SELECT archived FROM report_card WHERE id = 1
134134
UNION ALL
135135
SELECT 48 FROM report_card WHERE id = 1`,
@@ -181,8 +181,8 @@ var ScriptTests = []ScriptTest{
181181
WHERE archived = FALSE AND id <> 1
182182
) AS dummy_alias`,
183183
Expected: []sql.Row{
184-
{int64(5), int64(1), "Card1", uint64(0), int64(0), int64(2)},
185-
{int64(7), int64(2), "Collection1", uint64(0), nil, int64(2)},
184+
{int64(5), int64(1), "Card1", int64(0), int64(0), int64(2)},
185+
{int64(7), int64(2), "Collection1", int64(0), nil, int64(2)},
186186
},
187187
},
188188
},

sql/analyzer/resolve_unions.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func finalizeUnions(ctx *sql.Context, a *Analyzer, n sql.Node, scope *plan.Scope
100100
return nil, transform.SameTree, err
101101
}
102102

103-
// UNION operations can return multiple rows, so Max1Row optimization is invalid
103+
// UNION can return multiple rows even when child queries use LIMIT 1, so disable Max1Row optimization
104104
qFlags.Unset(sql.QFlagMax1Row)
105105

106106
return newN, transform.NewTree, nil

sql/iters/rel_iters.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -601,8 +601,9 @@ func (di *distinctIter) Dispose() {
601601
}
602602

603603
type UnionIter struct {
604-
Cur sql.RowIter
605-
NextIter func(ctx *sql.Context) (sql.RowIter, error)
604+
Cur sql.RowIter
605+
NextIter func(ctx *sql.Context) (sql.RowIter, error)
606+
ResultSchema sql.Schema
606607
}
607608

608609
func (ui *UnionIter) Next(ctx *sql.Context) (sql.Row, error) {
@@ -620,8 +621,23 @@ func (ui *UnionIter) Next(ctx *sql.Context) (sql.Row, error) {
620621
if err != nil {
621622
return nil, err
622623
}
623-
return ui.Cur.Next(ctx)
624+
res, err = ui.Cur.Next(ctx)
625+
if err != nil {
626+
return nil, err
627+
}
628+
}
629+
630+
// Convert BIT values to Int64 when schema generalization changed types for server engine compatibility
631+
if ui.ResultSchema != nil {
632+
for i, val := range res {
633+
if i < len(ui.ResultSchema) && val != nil {
634+
if converted, _, err := ui.ResultSchema[i].Type.Convert(ctx, val); err == nil {
635+
res[i] = converted
636+
}
637+
}
638+
}
624639
}
640+
625641
return res, err
626642
}
627643

sql/rowexec/rel.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,7 @@ func (b *BaseBuilder) buildSetOp(ctx *sql.Context, s *plan.SetOp, row sql.Row) (
814814
NextIter: func(ctx *sql.Context) (sql.RowIter, error) {
815815
return b.buildNodeExec(ctx, s.Right(), row)
816816
},
817+
ResultSchema: s.Schema(),
817818
}
818819
case plan.IntersectType:
819820
var iter2 sql.RowIter

sql/types/bit.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,11 @@ func (t BitType_) SQL(ctx *sql.Context, dest []byte, v interface{}) (sqltypes.Va
193193
if v == nil {
194194
return sqltypes.NULL, nil
195195
}
196+
197+
// Delegate int64 values to Int64.SQL to prevent server engine []uint8 serialization errors
198+
if int64Val, ok := v.(int64); ok {
199+
return Int64.SQL(ctx, dest, int64Val)
200+
}
196201
value, _, err := t.Convert(ctx, v)
197202
if err != nil {
198203
return sqltypes.Value{}, err

sql/types/conversion.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,11 @@ func generalizeNumberTypes(a, b sql.Type) sql.Type {
637637
// TODO: Create and handle "Illegal mix of collations" error
638638
// TODO: Handle extended types, like DoltgresType
639639
func GeneralizeTypes(a, b sql.Type) sql.Type {
640+
// BIT types must convert to Int64 in UNION to avoid server engine serialization errors with []uint8
641+
if (IsBit(a) || IsBit(b)) && (IsBit(a) && IsBit(b) || IsNullType(a) || IsNullType(b)) {
642+
return Int64
643+
}
644+
640645
if reflect.DeepEqual(a, b) {
641646
return a
642647
}
@@ -693,18 +698,6 @@ func GeneralizeTypes(a, b sql.Type) sql.Type {
693698
return LongBlob
694699
}
695700

696-
aIsBit := IsBit(a)
697-
bIsBit := IsBit(b)
698-
if aIsBit && bIsBit {
699-
// TODO: match max bits to max of max bits between a and b
700-
return a.Promote()
701-
}
702-
if aIsBit {
703-
a = Int64
704-
}
705-
if bIsBit {
706-
b = Int64
707-
}
708701

709702
aIsYear := IsYear(a)
710703
bIsYear := IsYear(b)

0 commit comments

Comments
 (0)