Skip to content

Commit

Permalink
allow backticks
Browse files Browse the repository at this point in the history
  • Loading branch information
James Cor committed Jun 13, 2024
1 parent 467efd8 commit 02bfd08
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 36 deletions.
22 changes: 14 additions & 8 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
124 changes: 97 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
17 changes: 16 additions & 1 deletion go/vt/sqlparser/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,10 +428,25 @@ func (tkn *Tokenizer) scanIdentifier(firstByte byte, isDbSystemVariable bool) (i
buffer.WriteByte(byte(tkn.lastChar))
tkn.next()
}
for isLetter(tkn.lastChar) || isDigit(tkn.lastChar) || (isDbSystemVariable && isCarat(tkn.lastChar)) {
for isLetter(tkn.lastChar) || isDigit(tkn.lastChar) || (isDbSystemVariable && isCarat(tkn.lastChar)) /*|| (firstByte == '@' && isCarat(tkn.lastChar))*/ {
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

0 comments on commit 02bfd08

Please sign in to comment.