Skip to content

Commit da991de

Browse files
authored
Merge pull request #3261 from dolthub/angela/string_idx_conversion
Do not convert keys if key type is incompatible with column type
2 parents b4366f3 + 1c49317 commit da991de

File tree

3 files changed

+72
-6
lines changed

3 files changed

+72
-6
lines changed

enginetest/queries/script_queries.go

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,10 +1367,8 @@ FROM task_instance INNER JOIN job ON job.id = task_instance.queued_by_job_id INN
13671367
Expected: []sql.Row{{"11"}},
13681368
ExpectedWarningsCount: 0,
13691369
},
1370-
13711370
{
13721371
// https://github.com/dolthub/dolt/issues/9739
1373-
Skip: true,
13741372
Dialect: "mysql",
13751373
Query: "select * from test01 where pk in (11)",
13761374
Expected: []sql.Row{
@@ -1384,25 +1382,22 @@ FROM task_instance INNER JOIN job ON job.id = task_instance.queued_by_job_id INN
13841382
},
13851383
{
13861384
// https://github.com/dolthub/dolt/issues/9739
1387-
Skip: true,
1385+
Skip: true, // this passes in gms but not dolt
13881386
Dialect: "mysql",
13891387
Query: "select * from test01 where pk=3",
13901388
Expected: []sql.Row{
13911389
{" 3 12 4"},
1392-
{" 3. 12 4"},
13931390
{"3. 12 4"},
13941391
},
13951392
ExpectedWarningsCount: 12,
13961393
ExpectedWarning: mysql.ERTruncatedWrongValue,
13971394
},
13981395
{
13991396
// https://github.com/dolthub/dolt/issues/9739
1400-
Skip: true,
14011397
Dialect: "mysql",
14021398
Query: "select * from test01 where pk>=3 and pk < 4",
14031399
Expected: []sql.Row{
14041400
{" 3 12 4"},
1405-
{" 3. 12 4"},
14061401
{" 3.2 12 4"},
14071402
{"+3.1234"},
14081403
{"3. 12 4"},
@@ -1426,6 +1421,50 @@ FROM task_instance INNER JOIN job ON job.id = task_instance.queued_by_job_id INN
14261421
},
14271422
},
14281423
},
1424+
{
1425+
// https://github.com/dolthub/dolt/issues/9936
1426+
Name: "invisible hash index with different key types",
1427+
Dialect: "mysql",
1428+
SetUpScript: []string{
1429+
"create table t0(c0 varchar(500))",
1430+
"insert into t0(c0) values (77367106)",
1431+
"create index i0 using hash on t0(c0) invisible",
1432+
},
1433+
Assertions: []ScriptTestAssertion{
1434+
{
1435+
Query: "select c0 from t0 where 1630944823 >= t0.c0",
1436+
Expected: []sql.Row{{"77367106"}},
1437+
},
1438+
{
1439+
Query: "select c0 from t0 where 1630944823 <= t0.c0",
1440+
Expected: []sql.Row{},
1441+
},
1442+
{
1443+
Query: "select c0 from t0 where '1630944823' >= t0.c0",
1444+
Expected: []sql.Row{},
1445+
},
1446+
{
1447+
Query: "select c0 from t0 where '1630944823' <= t0.c0",
1448+
Expected: []sql.Row{{"77367106"}},
1449+
},
1450+
{
1451+
Query: "select c0 from t0 where 1630944823.2 >= t0.c0",
1452+
Expected: []sql.Row{{"77367106"}},
1453+
},
1454+
{
1455+
Query: "select c0 from t0 where 1630944823.2 <= t0.c0",
1456+
Expected: []sql.Row{},
1457+
},
1458+
{
1459+
Query: "select c0 from t0 where '1630944823.2' >= t0.c0",
1460+
Expected: []sql.Row{},
1461+
},
1462+
{
1463+
Query: "select c0 from t0 where '1630944823.2' <= t0.c0",
1464+
Expected: []sql.Row{{"77367106"}},
1465+
},
1466+
},
1467+
},
14291468
{
14301469
// https://github.com/dolthub/dolt/issues/9821
14311470
Name: "strings with boolean operators",

sql/index_builder.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,11 +224,23 @@ func (b *MySQLIndexBuilder) GreaterThan(ctx *Context, colExpr string, keyType Ty
224224
return b
225225
}
226226

227+
// isConvertibleKeyType checks if the key can be converted into the column type
228+
func isConvertibleKeyType(colType Type, keyType Type) bool {
229+
if IsStringType(colType) {
230+
return !(IsNumberType(keyType) || IsDecimalType(keyType))
231+
}
232+
// TODO: check other types
233+
return true
234+
}
235+
227236
// convertKey converts the given key from keyType to colType, returning an error if the conversion fails.
228237
func (b *MySQLIndexBuilder) convertKey(ctx *Context, colType Type, keyType Type, key interface{}) (interface{}, error) {
229238
if et, ok := colType.(ExtendedType); ok {
230239
return et.ConvertToType(ctx, keyType.(ExtendedType), key)
231240
} else {
241+
if !isConvertibleKeyType(colType, keyType) {
242+
return nil, ErrInvalidValueType.New(key, colType)
243+
}
232244
k, _, err := colType.Convert(ctx, key)
233245
if err != nil && !ErrTruncatedIncorrect.Is(err) {
234246
return nil, err

sql/type.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@ type NumberType interface {
172172
DisplayWidth() int
173173
}
174174

175+
func IsNumberType(t Type) bool {
176+
_, ok := t.(NumberType)
177+
return ok
178+
}
179+
175180
// RoundingNumberType represents Number Types that implement an additional interface
176181
// that supports rounding when converting rather than the default truncation.
177182
type RoundingNumberType interface {
@@ -197,6 +202,11 @@ type StringType interface {
197202
Length() int64
198203
}
199204

205+
func IsStringType(t Type) bool {
206+
_, ok := t.(StringType)
207+
return ok
208+
}
209+
200210
// DatetimeType represents DATE, DATETIME, and TIMESTAMP.
201211
// https://dev.mysql.com/doc/refman/8.0/en/datetime.html
202212
// The type of the returned value is time.Time.
@@ -277,6 +287,11 @@ type DecimalType interface {
277287
Scale() uint8
278288
}
279289

290+
func IsDecimalType(t Type) bool {
291+
_, ok := t.(DecimalType)
292+
return ok
293+
}
294+
280295
type Type2 interface {
281296
Type
282297

0 commit comments

Comments
 (0)