Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions go/vt/sqlparser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -1544,9 +1544,15 @@ func (node *FuncExpr) Format(buf *TrackedBuffer) {
buf.Myprintf("%v.", node.Qualifier)
}
// Function names should not be back-quoted even
// if they match a reserved word. So, print the
// name as is.
buf.Myprintf("%s(%s%v)", node.Name.String(), distinct, node.Exprs)
// if they match a reserved word, only if they contain illegal characters
funcName := node.Name.String()

if containEscapableChars(funcName) {
writeEscapedString(buf, funcName)
} else {
buf.WriteString(funcName)
}
buf.Myprintf("(%s%v)", distinct, node.Exprs)
}

// Format formats the node
Expand Down
29 changes: 20 additions & 9 deletions go/vt/sqlparser/ast_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -631,26 +631,37 @@ func (node *TableIdent) UnmarshalJSON(b []byte) error {
return nil
}

func formatID(buf *TrackedBuffer, original, lowered string) {
func containEscapableChars(s string) bool {
isDbSystemVariable := false
if len(original) > 1 && original[:2] == "@@" {
if len(s) > 1 && s[:2] == "@@" {
isDbSystemVariable = true
}

for i, c := range original {
for i, c := range s {
if !isLetter(uint16(c)) && (!isDbSystemVariable || !isCarat(uint16(c))) {
if i == 0 || !isDigit(uint16(c)) {
goto mustEscape
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good riddance. goto has its place, but this was not one of them.

return true
}
}
}
if _, ok := keywords[lowered]; ok {
goto mustEscape

return false
}

func isKeyword(s string) bool {
_, isKeyword := keywords[s]
return isKeyword
}

func formatID(buf *TrackedBuffer, original, lowered string) {
if containEscapableChars(original) || isKeyword(lowered) {
writeEscapedString(buf, original)
} else {
buf.Myprintf("%s", original)
}
buf.Myprintf("%s", original)
return
}

mustEscape:
func writeEscapedString(buf *TrackedBuffer, original string) {
buf.WriteByte('`')
for _, c := range original {
buf.WriteRune(c)
Expand Down
6 changes: 6 additions & 0 deletions go/vt/sqlparser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1523,6 +1523,12 @@ var (
}, {
input: "select distinctrow a.* from (select (1) from dual union all select 1 from dual) a",
output: "select distinct a.* from (select (1) from dual union all select 1 from dual) as a",
}, {
input: "select `weird function name`() from t",
}, {
input: "select status() from t", // should not escape function names that are keywords
}, {
input: "select * from `weird table name`",
}}
)

Expand Down