Skip to content

Commit

Permalink
Merge pull request #253 from dolthub/zachmu/string-concat
Browse files Browse the repository at this point in the history
Automatically concatenate adjacent string literals
  • Loading branch information
zachmu authored Jul 7, 2023
2 parents d13c7c2 + 4d21c09 commit 241b093
Show file tree
Hide file tree
Showing 5 changed files with 5,881 additions and 5,859 deletions.
2 changes: 1 addition & 1 deletion go/vt/sqlparser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func captureSelectExpressions(sql string, tokenizer *Tokenizer) {
// column names don't need any special handling to capture the input expression
return false, nil
} else {
node.InputExpression = trimQuotes(strings.TrimLeft(sql[node.StartParsePos:node.EndParsePos], " \n\t"))
node.InputExpression = trimQuotes(strings.Trim(sql[node.StartParsePos:node.EndParsePos], " \n\t"))
}
}
return true, nil
Expand Down
48 changes: 29 additions & 19 deletions go/vt/sqlparser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ var (
input: "CREATE table t (pk int primary key, fk int REFERENCES parent(id))",
output: "create table t (\n\tpk int primary key,\n\tfk int references parent [id]\n)",
},
{
input: `Select 'a' "b" 'c'`,
output: "select 'abc'",
},
{
input: `Select concat('a' "b" 'c', "de" 'f')`,
output: "select concat('abc', 'def')",
},
{
input: "SET @foo = 'o' 'ne';",
output: "set @foo = 'one'",
Expand Down Expand Up @@ -412,12 +420,6 @@ var (
output: "select /* table alias */ 1 from t as t1",
}, {
input: "select /* table alias with as */ 1 from t as t1",
}, {
input: "select /* string table alias */ 1 from t as 't1'",
output: "select /* string table alias */ 1 from t as t1",
}, {
input: "select /* string table alias without as */ 1 from t 't1'",
output: "select /* string table alias without as */ 1 from t as t1",
}, {
input: "select /* keyword table alias */ 1 from t as `By`",
}, {
Expand All @@ -435,12 +437,6 @@ var (
output: "select /* table alias */ 1 from t as of '2019-01-01' as t1",
}, {
input: "select /* table alias with as */ 1 from t as of '2019-01-01' as t1",
}, {
input: "select /* string table alias */ 1 from t as of '2019-01-01' as 't1'",
output: "select /* string table alias */ 1 from t as of '2019-01-01' as t1",
}, {
input: "select /* string table alias without as */ 1 from t as of '2019-01-01' 't1'",
output: "select /* string table alias without as */ 1 from t as of '2019-01-01' as t1",
}, {
input: "select /* keyword table alias */ 1 from t as of '2019-01-01' as `By`",
}, {
Expand Down Expand Up @@ -3364,6 +3360,15 @@ func TestValid(t *testing.T) {
}
}

func TestSingle(t *testing.T) {
validSQL = append(validSQL, validMultiStatementSql...)
for _, tcase := range validSQL {
if tcase.input == "select \"'ain't'\", '\"hello\"' from t" {
runParseTestCase(t, tcase)
}
}
}

func TestGeneratedColumns(t *testing.T) {
tests := []parseTest{
{
Expand Down Expand Up @@ -4000,6 +4005,9 @@ func TestInvalid(t *testing.T) {
}{{
input: "SET @foo = `o` `ne`;",
err: "syntax error",
}, {
input: "select '1' '2",
err: "syntax error",
}, {
input: "CHANGE REPLICATION FILTER",
err: "syntax error",
Expand Down Expand Up @@ -4061,13 +4069,15 @@ func TestInvalid(t *testing.T) {
}

for _, tcase := range invalidSQL {
_, err := Parse(tcase.input)
if err == nil {
t.Errorf("Parse invalid query(%q), got: nil, want: %s...", tcase.input, tcase.err)
}
if err != nil && !strings.Contains(err.Error(), tcase.err) {
t.Errorf("Parse invalid query(%q), got: %v, want: %s...", tcase.input, err, tcase.err)
}
t.Run(tcase.input, func(t *testing.T) {
_, err := Parse(tcase.input)
if err == nil {
t.Errorf("Parse invalid query(%q), got: nil, want: %s...", tcase.input, tcase.err)
}
if err != nil && !strings.Contains(err.Error(), tcase.err) {
t.Errorf("Parse invalid query(%q), got: %v, want: %s...", tcase.input, err, tcase.err)
}
})
}

invalidDDL := []struct {
Expand Down
Loading

0 comments on commit 241b093

Please sign in to comment.