Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow backticks in system and user variables #353

Merged
merged 3 commits into from
Jun 13, 2024
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
29 changes: 20 additions & 9 deletions go/vt/sqlparser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -6842,15 +6842,21 @@ func VarScope(nameParts ...string) (string, SetScope, string, error) {
return VarScope(nameParts[0][:dotIdx], nameParts[0][dotIdx+1:])
}
// Session scope is inferred here, but not explicitly requested
return nameParts[0][2:], SetScope_Session, "", nil
return trimQuotes(nameParts[0][2:]), SetScope_Session, "", nil
} else if strings.HasPrefix(nameParts[0], "@") {
return nameParts[0][1:], SetScope_User, "", nil
varName := nameParts[0][1:]
if len(varName) > 0 {
varName = trimQuotes(varName)
}
return varName, SetScope_User, "", nil
} else {
return nameParts[0], SetScope_None, "", nil
}
case 2:
// `@user.var` is valid, so we check for it here.
if len(nameParts[0]) >= 2 && nameParts[0][0] == '@' && nameParts[0][1] != '@' &&
if len(nameParts[0]) >= 2 &&
nameParts[0][0] == '@' &&
nameParts[0][1] != '@' &&
!strings.HasPrefix(nameParts[1], "@") { // `@user.@var` is invalid though.
return fmt.Sprintf("%s.%s", nameParts[0][1:], nameParts[1]), SetScope_User, "", nil
}
Expand All @@ -6866,27 +6872,27 @@ func VarScope(nameParts ...string) (string, SetScope, string, error) {
if strings.HasPrefix(nameParts[1], `"`) || strings.HasPrefix(nameParts[1], `'`) {
return "", SetScope_None, "", fmt.Errorf("invalid system variable declaration `%s`", nameParts[1])
}
return nameParts[1], SetScope_Global, nameParts[0][2:], nil
return trimQuotes(nameParts[1]), SetScope_Global, nameParts[0][2:], nil
case "@@persist":
if strings.HasPrefix(nameParts[1], `"`) || strings.HasPrefix(nameParts[1], `'`) {
return "", SetScope_None, "", fmt.Errorf("invalid system variable declaration `%s`", nameParts[1])
}
return nameParts[1], SetScope_Persist, nameParts[0][2:], nil
return trimQuotes(nameParts[1]), SetScope_Persist, nameParts[0][2:], nil
case "@@persist_only":
if strings.HasPrefix(nameParts[1], `"`) || strings.HasPrefix(nameParts[1], `'`) {
return "", SetScope_None, "", fmt.Errorf("invalid system variable declaration `%s`", nameParts[1])
}
return nameParts[1], SetScope_PersistOnly, nameParts[0][2:], nil
return trimQuotes(nameParts[1]), SetScope_PersistOnly, nameParts[0][2:], nil
case "@@session":
if strings.HasPrefix(nameParts[1], `"`) || strings.HasPrefix(nameParts[1], `'`) {
return "", SetScope_None, "", fmt.Errorf("invalid system variable declaration `%s`", nameParts[1])
}
return nameParts[1], SetScope_Session, nameParts[0][2:], nil
return trimQuotes(nameParts[1]), SetScope_Session, nameParts[0][2:], nil
case "@@local":
if strings.HasPrefix(nameParts[1], `"`) || strings.HasPrefix(nameParts[1], `'`) {
return "", SetScope_None, "", fmt.Errorf("invalid system variable declaration `%s`", nameParts[1])
}
return nameParts[1], SetScope_Session, nameParts[0][2:], nil
return trimQuotes(nameParts[1]), SetScope_Session, nameParts[0][2:], nil
default:
// This catches `@@@GLOBAL.sys_var`. Due to the earlier check, this does not error on `@user.var`.
if strings.HasPrefix(nameParts[0], "@") {
Expand Down Expand Up @@ -7219,8 +7225,13 @@ func formatID(buf *TrackedBuffer, original, lowered string) {
isDbSystemVariable = true
}

isUserVariable := false
if !isDbSystemVariable && len(original) > 0 && original[:1] == "@" {
isUserVariable = true
}

for i, c := range original {
if !(isLetter(uint16(c)) || c == '@') && (!isDbSystemVariable || !isCarat(uint16(c))) {
if !(isLetter(uint16(c)) || c == '@') && (!isDbSystemVariable || !isCarat(uint16(c))) && !isUserVariable {
if i == 0 || !isDigit(uint16(c)) {
goto mustEscape
}
Expand Down
138 changes: 111 additions & 27 deletions go/vt/sqlparser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1406,76 +1406,146 @@ var (
input: "set #simple\n b = 4",
}, {
input: "set character_set_results = utf8",
}, {
},
{
input: "set @@`version` = true",
output: "set session version = true",
},
{
input: "select @@`version` = true",
output: "select @@`version` = true",
},
{
input: "set @@session.autocommit = true",
output: "set session autocommit = true",
}, {
},
{
input: "set @@session.`autocommit` = true",
output: "set session `autocommit` = true",
}, {
output: "set session autocommit = true",
},
{
input: "select @@session.`autocommit` = true",
output: "select @@session.`autocommit` = true",
},
{
input: "set @@session.autocommit = ON",
output: "set session autocommit = 'ON'",
}, {
},
{
input: "set @@session.autocommit= OFF",
output: "set session autocommit = 'OFF'",
}, {
},
{
input: "set session autocommit = ON",
output: "set session autocommit = 'ON'",
}, {
},
{
input: "set session autocommit := ON",
output: "set session autocommit = 'ON'",
}, {
},
{
input: "set global autocommit = OFF",
output: "set global autocommit = 'OFF'",
}, {
},
{
input: "set @@global.optimizer_prune_level = 1",
output: "set global optimizer_prune_level = 1",
}, {
},
{
input: "set global optimizer_prune_level = 1",
}, {
},
{
input: "set @@persist.optimizer_prune_level = 1",
output: "set persist optimizer_prune_level = 1",
}, {
},
{
input: "set persist optimizer_prune_level = 1",
}, {
},
{
input: "set @@persist_only.optimizer_prune_level = 1",
output: "set persist_only optimizer_prune_level = 1",
}, {
},
{
input: "set persist_only optimizer_prune_level = 1",
}, {
},
{
input: "set @@local.optimizer_prune_level = 1",
output: "set session optimizer_prune_level = 1",
}, {
},
{
input: "set local optimizer_prune_level = 1",
output: "set session optimizer_prune_level = 1",
}, {
},
{
input: "set @@optimizer_prune_level = 1",
output: "set session optimizer_prune_level = 1",
}, {
},
{
input: "set session optimizer_prune_level = 1",
}, {
},
{
input: "set @@optimizer_prune_level = 1, @@global.optimizer_search_depth = 62",
output: "set session optimizer_prune_level = 1, global optimizer_search_depth = 62",
}, {
},
{
input: "set @@GlObAl.optimizer_prune_level = 1",
output: "set global optimizer_prune_level = 1",
}, {
},
{
input: "set @user.var = 1",
}, {
},
{
input: "set @user.var.name = 1",
}, {
},
{
input: "set @user.var.name := 1",
output: "set @user.var.name = 1",
}, {
},
{
input: "set @`user var` = 1",
output: "set @user var = 1",
},
{
input: "select @`user var`",
output: "select @`user var`",
},
{
input: "set @user.`var` = 1",
output: "set @user.var = 1",
},
{
input: "select @user.`var`",
output: "select @user.var",
},
{
input: "set @`user`.`var` = 1",
output: "set @`user`.var = 1",
},
{
input: "select @`user`.`var`",
output: "select @`user`.var",
},
{
input: "set @abc.def.`ghi` = 300",
output: "set @abc.def.ghi = 300",
},
{
input: "select @abc.def.`ghi`",
output: "select @abc.def.ghi",
},
{
input: "set autocommit = on",
output: "set autocommit = 'on'",
}, {
},
{
input: "set autocommit = off",
output: "set autocommit = 'off'",
}, {
},
{
input: "set autocommit = off, foo = 1",
output: "set autocommit = 'off', foo = 1",
}, {
},
{
input: "set names utf8 collate foo",
output: "set names 'utf8'",
}, {
Expand Down Expand Up @@ -4401,6 +4471,20 @@ end`,
}
)

// TestSingleSQL is a helper function to test a single SQL statement.
func TestSingleSQL(t *testing.T) {
t.Skip()
tests := []parseTest{
{
input: "select @`user var`",
output: "select @`user var`",
},
}
for _, tcase := range tests {
runParseTestCase(t, tcase)
}
}

func TestValid(t *testing.T) {
validSQL = append(validSQL, validMultiStatementSql...)
for _, tcase := range validSQL {
Expand Down
15 changes: 15 additions & 0 deletions go/vt/sqlparser/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,21 @@ func (tkn *Tokenizer) scanIdentifier(firstByte byte, isDbSystemVariable bool) (i
buffer.WriteByte(byte(tkn.lastChar))
tkn.next()
}

// special case for user variables with backticks
if firstByte == '@' && tkn.lastChar == '`' {
buffer.WriteByte(byte(tkn.lastChar))
tkn.next()
for isLetter(tkn.lastChar) || isDigit(tkn.lastChar) || isCarat(tkn.lastChar) || unicode.IsSpace(rune(tkn.lastChar)) {
buffer.WriteByte(byte(tkn.lastChar))
if tkn.lastChar == '`' {
tkn.next()
break
}
tkn.next()
}
}

if tkn.lastChar == '@' {
tkn.potentialAccountName = true
}
Expand Down