Skip to content

Commit 18900d9

Browse files
authored
fix text storage for left and instr function (#3018)
1 parent af9d166 commit 18900d9

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

enginetest/memory_engine_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ func TestSingleScript(t *testing.T) {
226226

227227
for _, test := range scripts {
228228
harness := enginetest.NewMemoryHarness("", 1, testNumPartitions, true, nil)
229-
harness.UseServer()
229+
//harness.UseServer()
230230
engine, err := harness.NewEngine(t)
231231
if err != nil {
232232
panic(err)

enginetest/queries/script_queries.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8684,6 +8684,34 @@ where
86848684
},
86858685
},
86868686
},
8687+
{
8688+
Name: "substring function tests with wrappers",
8689+
Dialect: "mysql",
8690+
SetUpScript: []string{
8691+
"create table tbl (t text);",
8692+
"insert into tbl values ('abcdef');",
8693+
},
8694+
Assertions: []ScriptTestAssertion{
8695+
{
8696+
Query: "select left(t, 3) from tbl;",
8697+
Expected: []sql.Row{
8698+
{"abc"},
8699+
},
8700+
},
8701+
{
8702+
Query: "select right(t, 3) from tbl;",
8703+
Expected: []sql.Row{
8704+
{"def"},
8705+
},
8706+
},
8707+
{
8708+
Query: "select instr(t, 'bcd') from tbl;",
8709+
Expected: []sql.Row{
8710+
{2},
8711+
},
8712+
},
8713+
},
8714+
},
86878715
}
86888716

86898717
var SpatialScriptTests = []ScriptTest{

sql/expression/function/substring.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,20 @@ func (l Left) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
349349
switch str := str.(type) {
350350
case string:
351351
text = []rune(str)
352+
case sql.StringWrapper:
353+
s, err := str.Unwrap(ctx)
354+
if err != nil {
355+
return nil, err
356+
}
357+
text = []rune(s)
352358
case []byte:
353359
text = []rune(string(str))
360+
case sql.BytesWrapper:
361+
b, err := str.Unwrap(ctx)
362+
if err != nil {
363+
return nil, err
364+
}
365+
text = []rune(string(b))
354366
case nil:
355367
return nil, nil
356368
default:
@@ -583,8 +595,20 @@ func (i Instr) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
583595
switch str := str.(type) {
584596
case string:
585597
text = []rune(str)
598+
case sql.StringWrapper:
599+
s, err := str.Unwrap(ctx)
600+
if err != nil {
601+
return nil, err
602+
}
603+
text = []rune(s)
586604
case []byte:
587605
text = []rune(string(str))
606+
case sql.BytesWrapper:
607+
s, err := str.Unwrap(ctx)
608+
if err != nil {
609+
return nil, err
610+
}
611+
text = []rune(string(s))
588612
case nil:
589613
return nil, nil
590614
default:
@@ -600,8 +624,20 @@ func (i Instr) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
600624
switch substr := substr.(type) {
601625
case string:
602626
subtext = []rune(substr)
627+
case sql.StringWrapper:
628+
s, err := substr.Unwrap(ctx)
629+
if err != nil {
630+
return nil, err
631+
}
632+
text = []rune(s)
603633
case []byte:
604-
subtext = []rune(string(subtext))
634+
subtext = []rune(string(substr))
635+
case sql.BytesWrapper:
636+
s, err := substr.Unwrap(ctx)
637+
if err != nil {
638+
return nil, err
639+
}
640+
subtext = []rune(string(s))
605641
case nil:
606642
return nil, nil
607643
default:

0 commit comments

Comments
 (0)