Skip to content

Commit 4ae8110

Browse files
authored
Merge pull request #2964 from dolthub/nicktobey/unwrap
Unwrap inputs to REGEXP_LIKE, REPLACE, and RPAD/LPAD functions.
2 parents a36eec4 + 1067759 commit 4ae8110

File tree

9 files changed

+73
-15
lines changed

9 files changed

+73
-15
lines changed

enginetest/engine_only_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,14 @@ func TestCollationCoercion(t *testing.T) {
717717

718718
func TestRegex(t *testing.T) {
719719
harness := enginetest.NewDefaultMemoryHarness()
720-
harness.Setup(setup.SimpleSetup...)
720+
regexSetup := []setup.SetupScript{
721+
{
722+
"CREATE TABLE tests(pk int primary key, str text, pattern text, flags text);",
723+
"INSERT INTO tests VALUES (1, 'testing', 'TESTING', 'ci');",
724+
},
725+
}
726+
setupsScripts := append(setup.SimpleSetup, regexSetup)
727+
harness.Setup(setupsScripts...)
721728
engine, err := harness.NewEngine(t)
722729
require.NoError(t, err)
723730
defer engine.Close()

enginetest/enginetests.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3046,7 +3046,7 @@ func TestRenameColumn(t *testing.T, harness Harness) {
30463046
TestQueryWithContext(t, ctx, e, harness, "ALTER TABLE mydb.tabletest RENAME COLUMN s TO i1", []sql.Row{{types.NewOkResult(0)}}, nil, nil, nil)
30473047
TestQueryWithContext(t, ctx, e, harness, "SHOW FULL COLUMNS FROM mydb.tabletest", []sql.Row{
30483048
{"i", "int", nil, "NO", "PRI", nil, "", "", ""},
3049-
{"i1", "varchar(20)", "utf8mb4_0900_bin", "NO", "", nil, "", "", ""},
3049+
{"i1", "text", "utf8mb4_0900_bin", "NO", "", nil, "", "", ""},
30503050
}, nil, nil, nil)
30513051
})
30523052
}

enginetest/queries/queries.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6772,6 +6772,10 @@ SELECT * FROM cte WHERE d = 2;`,
67726772
Query: "select replace(s, 'row', '') from mytable order by i",
67736773
Expected: []sql.Row{{"first "}, {"second "}, {"third "}},
67746774
},
6775+
{
6776+
Query: "select replace(s, 'row', '') from tabletest order by i",
6777+
Expected: []sql.Row{{"first "}, {"second "}, {"third "}},
6778+
},
67756779
{
67766780
Query: "select rpad(s, 13, ' ') from mytable order by i",
67776781
Expected: []sql.Row{{"first row "}, {"second row "}, {"third row "}},
@@ -6780,6 +6784,10 @@ SELECT * FROM cte WHERE d = 2;`,
67806784
Query: "select lpad(s, 13, ' ') from mytable order by i",
67816785
Expected: []sql.Row{{" first row"}, {" second row"}, {" third row"}},
67826786
},
6787+
{
6788+
Query: "select lpad(s, 13, ' ') from tabletest order by i",
6789+
Expected: []sql.Row{{" first row"}, {" second row"}, {" third row"}},
6790+
},
67836791
{
67846792
Query: "select sqrt(i) from mytable order by i",
67856793
Expected: []sql.Row{{1.0}, {1.4142135623730951}, {1.7320508075688772}},

enginetest/queries/regex_queries.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@
2121
package queries
2222

2323
import (
24-
"gopkg.in/src-d/go-errors.v1"
25-
2624
regex "github.com/dolthub/go-icu-regex"
25+
"gopkg.in/src-d/go-errors.v1"
2726

2827
"github.com/dolthub/go-mysql-server/sql"
2928
)
@@ -55,6 +54,10 @@ var RegexTests = []RegexTest{
5554
Query: "SELECT REGEXP_LIKE('testing', 'TESTING', 'ic');",
5655
Expected: []sql.Row{{0}},
5756
},
57+
{
58+
Query: "SELECT REGEXP_LIKE(str, pattern, flags) from tests;",
59+
Expected: []sql.Row{{1}},
60+
},
5861
{
5962
Query: "SELECT REGEXP_LIKE('testing', 'TESTING' COLLATE utf8mb4_0900_ai_ci);",
6063
Expected: []sql.Row{{1}},

enginetest/scriptgen/setup/scripts/tabletest

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
exec
22
create table tabletest (
33
i int primary key,
4-
s varchar(20) not null
4+
s text not null
55
)
66
----
77

enginetest/scriptgen/setup/setup_data.sg.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sql/expression/function/regexp_like.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,12 @@ func (r *RegexpLike) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
179179
if err != nil {
180180
return nil, err
181181
}
182+
textStr, _, err := sql.Unwrap[string](ctx, text)
183+
if err != nil {
184+
return nil, err
185+
}
182186

183-
err = r.re.SetMatchString(ctx, text.(string))
187+
err = r.re.SetMatchString(ctx, textStr)
184188
if err != nil {
185189
return nil, err
186190
}
@@ -220,9 +224,13 @@ func compileRegex(ctx *sql.Context, pattern, text, flags sql.Expression, funcNam
220224
if err != nil {
221225
return nil, err
222226
}
227+
patternValStr, _, err := sql.Unwrap[string](ctx, patternVal)
228+
if err != nil {
229+
return nil, err
230+
}
223231

224232
// Empty regex, throw illegal argument
225-
if len(patternVal.(string)) == 0 {
233+
if len(patternValStr) == 0 {
226234
return nil, errors.NewKind("Illegal argument to regular expression.").New()
227235
}
228236

@@ -250,7 +258,10 @@ func compileRegex(ctx *sql.Context, pattern, text, flags sql.Expression, funcNam
250258
return nil, err
251259
}
252260

253-
flagsStr = f.(string)
261+
flagsStr, _, err = sql.Unwrap[string](ctx, f)
262+
if err != nil {
263+
return nil, err
264+
}
254265
flagsStr, err = consolidateRegexpFlags(flagsStr, funcName)
255266
if err != nil {
256267
return nil, err
@@ -279,7 +290,7 @@ func compileRegex(ctx *sql.Context, pattern, text, flags sql.Expression, funcNam
279290
ctx.Warn(1193, `System variable for regular expressions "regexp_buffer_size" is missing`)
280291
}
281292
re := regex.CreateRegex(bufferSize)
282-
if err = re.SetRegexString(ctx, patternVal.(string), regexFlags); err != nil {
293+
if err = re.SetRegexString(ctx, patternValStr, regexFlags); err != nil {
283294
_ = re.Close()
284295
return nil, err
285296
}

sql/expression/function/reverse_repeat_replace.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,9 +280,26 @@ func (r *Replace) Eval(
280280
return nil, err
281281
}
282282

283-
if fromStr.(string) == "" {
284-
return str, nil
283+
{
284+
str, _, err := sql.Unwrap[string](ctx, str)
285+
if err != nil {
286+
return nil, err
287+
}
288+
289+
fromStr, _, err := sql.Unwrap[string](ctx, fromStr)
290+
if err != nil {
291+
return nil, err
292+
}
293+
294+
toStr, _, err := sql.Unwrap[string](ctx, toStr)
295+
if err != nil {
296+
return nil, err
297+
}
298+
299+
if fromStr == "" {
300+
return str, nil
301+
}
302+
303+
return strings.Replace(str, fromStr, toStr, -1), nil
285304
}
286-
287-
return strings.Replace(str.(string), fromStr.(string), toStr.(string), -1), nil
288305
}

sql/expression/function/rpad_lpad.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,19 @@ func (p *Pad) Eval(
169169
return nil, err
170170
}
171171

172-
return padString(str.(string), length.(int64), padStr.(string), p.padType)
172+
{
173+
str, _, err := sql.Unwrap[string](ctx, str)
174+
if err != nil {
175+
return nil, err
176+
}
177+
178+
padStr, _, err := sql.Unwrap[string](ctx, padStr)
179+
if err != nil {
180+
return nil, err
181+
}
182+
183+
return padString(str, length.(int64), padStr, p.padType)
184+
}
173185
}
174186

175187
func padString(str string, length int64, padStr string, padType padType) (string, error) {

0 commit comments

Comments
 (0)