Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
amend executable comment logic and add todo
  • Loading branch information
elianddb committed Nov 3, 2025
commit 4f098ac9c7abe014e8226ca436ba836e53c1bd84
5 changes: 4 additions & 1 deletion go/vt/sqlparser/comments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,11 +310,14 @@ func TestExecutableComments(t *testing.T) {
input: "/*! SET max_execution_time=5000*/",
outSQL: "set max_execution_time = 5000",
}, {
input: "/*M!10010 SET @@session.skip_parallel_replication=0*/",
input: "/*M!100101 SET @@session.skip_parallel_replication=0*/",
outSQL: "set session skip_parallel_replication = 0",
}, {
input: "/*M! SET @@session.gtid_domain_id=0*/",
outSQL: "set session gtid_domain_id = 0",
}, {
input: "/*M!100101 SET @@session.skip_parallel_replication=0*/",
outSQL: "set session skip_parallel_replication = 0",
}}
for _, testCase := range testCases {
stmt, err := Parse(testCase.input)
Expand Down
40 changes: 26 additions & 14 deletions go/vt/sqlparser/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -787,31 +787,43 @@ func (tkn *Tokenizer) scanExecutableComment(prefix string) (int, []byte) {
tkn.consumeNext(buffer)
}

// Extract SQL from comment
commentStr := buffer.String()
prefixLen := len(prefix)
suffixLen := 2 // */
suffixLen := 2
sql := commentStr[prefixLen : len(commentStr)-suffixLen]

// Skip optional version number (up to 5 digits) and spaces
// We need to limit to 5 version digits to avoid trimming actual SQL content like /*!401011 from*/
// where 40101 is the version and "1 from" is the SQL
innerSQL := sql
trimmedDigits := 0
digitCount := 0

for i, c := range sql {
if trimmedDigits < 5 && unicode.IsDigit(c) {
trimmedDigits++
} else if unicode.IsSpace(c) {
// Continue skipping spaces after version digits
continue
if unicode.IsDigit(c) {
digitCount++
} else {
// Found first non-space, non-version-digit character
innerSQL = sql[i:]
break
}
if i >= 5 {
break
}
}

var versionDigits int
if prefix == "/*M!" && digitCount >= 6 && len(sql) > 6 && unicode.IsSpace(rune(sql[6])) {
versionDigits = 6
} else if digitCount >= 5 {
versionDigits = 5
} else if digitCount > 0 && digitCount < 5 {
return COMMENT, buffer.Bytes()
} else {
// TODO: MySQL treats comments that begin with fewer than 5 digits as normal comments.
versionDigits = 0
}

skipIndex := versionDigits
for skipIndex < len(sql) && unicode.IsSpace(rune(sql[skipIndex])) {
skipIndex++
}
innerSQL = sql[skipIndex:]

// If comment is empty, treat as regular comment (not executable)
if len(innerSQL) == 0 {
return COMMENT, buffer.Bytes()
}
Expand Down