Skip to content

Commit

Permalink
src: fix column name with no quote when scatter to backends radondb#489
Browse files Browse the repository at this point in the history
[summary]
When scatter sql to backends, the column name should be with quote '`'
e.g.: create table db.t(`key` int primary key, `col` int);
For the ddl with rename table, we remove all quotes '`' to uniform
standars.
e.g.: alter table t1 rename t2.

[test case]
src/planner/ddl_plan_test.go

[patch codecov]
src/planner/ddl_plan.go 95.1%
coverage: 89.8% of statements
  • Loading branch information
hustjieke committed Sep 24, 2019
1 parent 0ec377e commit 3515998
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 28 deletions.
15 changes: 9 additions & 6 deletions src/planner/ddl_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,26 +132,23 @@ func (p *DDLPlan) Build() error {
var query string

orgSegTable := segment.Table
var rawQuery string
var re *regexp.Regexp
var segTable string
if node.Table.Qualifier.IsEmpty() {
segTable = fmt.Sprintf("`%s`.`%s`", database, orgSegTable)
rawQuery = strings.Replace(p.RawQuery, "`", "", -1)
// \b: https://www.regular-expressions.info/wordboundaries.html
re, _ = regexp.Compile(fmt.Sprintf(`\b(%s)\b`, table))
} else {
segTable = fmt.Sprintf("`%s`.`%s`", database, orgSegTable)
newTable := fmt.Sprintf("%s.%s", database, table)
rawQuery = strings.Replace(p.RawQuery, "`", "", -1)
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 {
query = re.ReplaceAllStringFunc(p.RawQuery, func(m string) string {
count = count + 1
if count == occurrence {
return segTable
Expand All @@ -163,6 +160,8 @@ func (p *DDLPlan) Build() error {
var segQuery string
var segToTable string
var re *regexp.Regexp
// Here we default make sql with no quote '`'.
query = strings.Replace(query, "`", "", -1)
pos := strings.Index(query, segTable)
pos += len(segTable)

Expand All @@ -185,12 +184,14 @@ func (p *DDLPlan) Build() error {
}

if node.NewName.Qualifier.IsEmpty() {
segToTable = fmt.Sprintf("`%s`.`%s`", database, segToTable)
// Here we default make sql with no quote '`'.
segToTable = fmt.Sprintf("%s.%s", database, segToTable)
segQuery = strings.Replace(query[pos:], "`", "", -1)
// \b: https://www.regular-expressions.info/wordboundaries.html
re, _ = regexp.Compile(fmt.Sprintf(`\b(%s)\b`, toTable))
} else {
segToTable = fmt.Sprintf("`%s`.`%s`", database, segToTable)
// Here we default make sql with no quote '`'.
segToTable = fmt.Sprintf("%s.%s", database, segToTable)
newToTable := fmt.Sprintf("%s.%s", database, toTable)
segQuery = strings.Replace(query[pos:], "`", "", -1)
re, _ = regexp.Compile(fmt.Sprintf(`\b(%s)\b`, newToTable))
Expand Down Expand Up @@ -241,6 +242,8 @@ func (p *DDLPlan) JSON() string {
RawQuery: p.RawQuery,
Partitions: parts,
}
// If exp include escape, json will add '\' before it.
// e.g.: "\n\t tbl \n" will be "\\n\\t tbl \\n"
bout, err := json.MarshalIndent(exp, "", "\t")
if err != nil {
return err.Error()
Expand Down
Loading

0 comments on commit 3515998

Please sign in to comment.