Skip to content

Commit 22c15a5

Browse files
committed
fix safe context assertion in DoltgresType.Compare
1 parent 7d4ba2d commit 22c15a5

2 files changed

Lines changed: 33 additions & 2 deletions

File tree

server/types/type.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,12 @@ func (t *DoltgresType) Compare(ctx context.Context, v1 interface{}, v2 interface
217217

218218
if t.TypType == TypeType_Enum {
219219
// TODO: temporary solution to getting the enum type (which has label info) into the 'enum_cmp' function
220-
qf := globalFunctionRegistry.GetFunction(ctx.(*sql.Context), t.CompareFunc)
220+
// ctx is not guaranteed to be a *sql.Context when called from index comparator goroutines.
221+
sqlCtx, ok := ctx.(*sql.Context)
222+
if !ok {
223+
sqlCtx = sql.NewEmptyContext()
224+
}
225+
qf := globalFunctionRegistry.GetFunction(sqlCtx, t.CompareFunc)
221226
resTypes := qf.ResolvedTypes()
222227
newFunc := qf.WithResolvedTypes([]*DoltgresType{t, t, resTypes[len(resTypes)-1]})
223228
i, err := newFunc.(QuickFunction).CallVariadic(nil, v1, v2)
@@ -226,7 +231,12 @@ func (t *DoltgresType) Compare(ctx context.Context, v1 interface{}, v2 interface
226231
}
227232
return int(i.(int32)), nil
228233
} else if t == Oidvector {
229-
i, err := globalFunctionRegistry.GetFunction(ctx.(*sql.Context), t.CompareFunc).CallVariadic(nil, v1, v2)
234+
// ctx is not guaranteed to be a *sql.Context when called from index comparator goroutines.
235+
sqlCtx, ok := ctx.(*sql.Context)
236+
if !ok {
237+
sqlCtx = sql.NewEmptyContext()
238+
}
239+
i, err := globalFunctionRegistry.GetFunction(sqlCtx, t.CompareFunc).CallVariadic(nil, v1, v2)
230240
if err != nil {
231241
return 0, err
232242
}

testing/go/types_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3789,6 +3789,27 @@ var enumTypeTests = []ScriptTest{
37893789
},
37903790
},
37913791
},
3792+
{
3793+
Name: "btree index scan on enum column returns correct rows",
3794+
SetUpScript: []string{
3795+
`CREATE TYPE rainbow AS ENUM ('red', 'orange', 'yellow', 'green', 'blue', 'purple')`,
3796+
`CREATE TABLE enumtest (col rainbow)`,
3797+
`INSERT INTO enumtest VALUES ('red'), ('orange'), ('yellow'), ('green')`,
3798+
`CREATE UNIQUE INDEX enumtest_btree ON enumtest USING btree (col)`,
3799+
// Force the planner to use the btree index rather than a sequential scan.
3800+
`SET enable_seqscan = off`,
3801+
`SET enable_bitmapscan = off`,
3802+
},
3803+
Assertions: []ScriptTestAssertion{
3804+
{Query: `SELECT * FROM enumtest WHERE col = 'orange'`, Expected: []sql.Row{{"orange"}}},
3805+
{
3806+
Query: `SELECT * FROM enumtest WHERE col > 'orange' ORDER BY col`,
3807+
// Enum values sort by their position in the type definition, not alphabetically.
3808+
Expected: []sql.Row{{"yellow"}, {"green"}},
3809+
},
3810+
{Query: `SELECT * FROM enumtest WHERE col < 'orange' ORDER BY col`, Expected: []sql.Row{{"red"}}},
3811+
},
3812+
},
37923813
{
37933814
Skip: true,
37943815
Name: "create type with existing array type name updates the name of the array type",

0 commit comments

Comments
 (0)