Skip to content

Commit

Permalink
planner: fix the case that the column is the same as the table name #438
Browse files Browse the repository at this point in the history
  • Loading branch information
andyli029 authored and BohuTANG committed Jul 20, 2019
1 parent b6abb8a commit 33d694a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/planner/ddl_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,20 +132,32 @@ func (p *DDLPlan) Build() error {
var query string

segTable := segment.Table
var rawQuery string
var re *regexp.Regexp
if node.Table.Qualifier.IsEmpty() {
segTable = fmt.Sprintf("`%s`.`%s`", database, segTable)
rawQuery := strings.Replace(p.RawQuery, "`", "", 2)
rawQuery = strings.Replace(p.RawQuery, "`", "", 2)
// \b: https://www.regular-expressions.info/wordboundaries.html
re, _ := regexp.Compile(fmt.Sprintf(`\b(%s)\b`, table))
query = re.ReplaceAllString(rawQuery, segTable)
re, _ = regexp.Compile(fmt.Sprintf(`\b(%s)\b`, table))
} else {
segTable = fmt.Sprintf("`%s`.`%s`", database, segTable)
newTable := fmt.Sprintf("%s.%s", database, table)
rawQuery := strings.Replace(p.RawQuery, "`", "", 4)
re, _ := regexp.Compile(fmt.Sprintf(`\b(%s)\b`, newTable))
query = re.ReplaceAllString(rawQuery, segTable)
rawQuery = strings.Replace(p.RawQuery, "`", "", 4)
re, _ = regexp.Compile(fmt.Sprintf(`\b(%s)\b`, newTable))
}

// avoid the name of the column is the same as the table name, eg, issues/438
// just replace the first place.
var count = 0
var occurrence = 1
query = re.ReplaceAllStringFunc(rawQuery, func(m string) string {
count = count + 1;
if (count == occurrence) {
return segTable
}
return m
})

tuple := xcontext.QueryTuple{
Query: query,
Backend: segment.Backend,
Expand Down
43 changes: 43 additions & 0 deletions src/planner/ddl_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,3 +327,46 @@ func TestDDLPlanWithQuote(t *testing.T) {
}
}
}

func TestDDLPlanWithSameColumn(t *testing.T) {
results := []string{
"{\n\t\"RawQuery\": \"CREATE table A(A int)\",\n\t\"Partitions\": [\n\t\t{\n\t\t\t\"Query\": \"CREATE table `sbtest`.`A0`(A int)\",\n\t\t\t\"Backend\": \"backend0\",\n\t\t\t\"Range\": \"[0-2)\"\n\t\t},\n\t\t{\n\t\t\t\"Query\": \"CREATE table `sbtest`.`A2`(A int)\",\n\t\t\t\"Backend\": \"backend2\",\n\t\t\t\"Range\": \"[2-4)\"\n\t\t},\n\t\t{\n\t\t\t\"Query\": \"CREATE table `sbtest`.`A4`(A int)\",\n\t\t\t\"Backend\": \"backend4\",\n\t\t\t\"Range\": \"[4-8)\"\n\t\t},\n\t\t{\n\t\t\t\"Query\": \"CREATE table `sbtest`.`A8`(A int)\",\n\t\t\t\"Backend\": \"backend8\",\n\t\t\t\"Range\": \"[8-4096)\"\n\t\t}\n\t]\n}",
"{\n\t\"RawQuery\": \"CREATE table `A`(A int)\",\n\t\"Partitions\": [\n\t\t{\n\t\t\t\"Query\": \"CREATE table `sbtest`.`A0`(A int)\",\n\t\t\t\"Backend\": \"backend0\",\n\t\t\t\"Range\": \"[0-2)\"\n\t\t},\n\t\t{\n\t\t\t\"Query\": \"CREATE table `sbtest`.`A2`(A int)\",\n\t\t\t\"Backend\": \"backend2\",\n\t\t\t\"Range\": \"[2-4)\"\n\t\t},\n\t\t{\n\t\t\t\"Query\": \"CREATE table `sbtest`.`A4`(A int)\",\n\t\t\t\"Backend\": \"backend4\",\n\t\t\t\"Range\": \"[4-8)\"\n\t\t},\n\t\t{\n\t\t\t\"Query\": \"CREATE table `sbtest`.`A8`(A int)\",\n\t\t\t\"Backend\": \"backend8\",\n\t\t\t\"Range\": \"[8-4096)\"\n\t\t}\n\t]\n}",
"{\n\t\"RawQuery\": \"create table sbtest.A(A int)\",\n\t\"Partitions\": [\n\t\t{\n\t\t\t\"Query\": \"create table `sbtest`.`A0`(A int)\",\n\t\t\t\"Backend\": \"backend0\",\n\t\t\t\"Range\": \"[0-2)\"\n\t\t},\n\t\t{\n\t\t\t\"Query\": \"create table `sbtest`.`A2`(A int)\",\n\t\t\t\"Backend\": \"backend2\",\n\t\t\t\"Range\": \"[2-4)\"\n\t\t},\n\t\t{\n\t\t\t\"Query\": \"create table `sbtest`.`A4`(A int)\",\n\t\t\t\"Backend\": \"backend4\",\n\t\t\t\"Range\": \"[4-8)\"\n\t\t},\n\t\t{\n\t\t\t\"Query\": \"create table `sbtest`.`A8`(A int)\",\n\t\t\t\"Backend\": \"backend8\",\n\t\t\t\"Range\": \"[8-4096)\"\n\t\t}\n\t]\n}",
"{\n\t\"RawQuery\": \"alter table A add column(A int)\",\n\t\"Partitions\": [\n\t\t{\n\t\t\t\"Query\": \"alter table `sbtest`.`A0` add column(A int)\",\n\t\t\t\"Backend\": \"backend0\",\n\t\t\t\"Range\": \"[0-2)\"\n\t\t},\n\t\t{\n\t\t\t\"Query\": \"alter table `sbtest`.`A2` add column(A int)\",\n\t\t\t\"Backend\": \"backend2\",\n\t\t\t\"Range\": \"[2-4)\"\n\t\t},\n\t\t{\n\t\t\t\"Query\": \"alter table `sbtest`.`A4` add column(A int)\",\n\t\t\t\"Backend\": \"backend4\",\n\t\t\t\"Range\": \"[4-8)\"\n\t\t},\n\t\t{\n\t\t\t\"Query\": \"alter table `sbtest`.`A8` add column(A int)\",\n\t\t\t\"Backend\": \"backend8\",\n\t\t\t\"Range\": \"[8-4096)\"\n\t\t}\n\t]\n}",
"{\n\t\"RawQuery\": \"alter table sbtest.A add column(A int)\",\n\t\"Partitions\": [\n\t\t{\n\t\t\t\"Query\": \"alter table `sbtest`.`A0` add column(A int)\",\n\t\t\t\"Backend\": \"backend0\",\n\t\t\t\"Range\": \"[0-2)\"\n\t\t},\n\t\t{\n\t\t\t\"Query\": \"alter table `sbtest`.`A2` add column(A int)\",\n\t\t\t\"Backend\": \"backend2\",\n\t\t\t\"Range\": \"[2-4)\"\n\t\t},\n\t\t{\n\t\t\t\"Query\": \"alter table `sbtest`.`A4` add column(A int)\",\n\t\t\t\"Backend\": \"backend4\",\n\t\t\t\"Range\": \"[4-8)\"\n\t\t},\n\t\t{\n\t\t\t\"Query\": \"alter table `sbtest`.`A8` add column(A int)\",\n\t\t\t\"Backend\": \"backend8\",\n\t\t\t\"Range\": \"[8-4096)\"\n\t\t}\n\t]\n}",
}

querys := []string{
"CREATE table A(A int)",
"CREATE table `A`(A int)",
"create table sbtest.A(A int)",
"alter table A add column(A int)",
"alter table sbtest.A add column(A int)",
}

log := xlog.NewStdLog(xlog.Level(xlog.PANIC))
database := "sbtest"

route, cleanup := router.MockNewRouter(log)
defer cleanup()

err := route.AddForTest(database, router.MockTableAConfig())
assert.Nil(t, err)
for i, query := range querys {
log.Debug("%v", query)
node, err := sqlparser.Parse(query)
assert.Nil(t, err)
plan := NewDDLPlan(log, database, query, node.(*sqlparser.DDL), route)

// plan build
{
err := plan.Build()
assert.Nil(t, err)
want := results[i]
got := plan.JSON()
assert.Equal(t, want, got)
assert.True(t, nil == plan.Children())
}
}
}

0 comments on commit 33d694a

Please sign in to comment.