Skip to content

Commit 3f41c61

Browse files
kyleconroyclaude
andauthored
Preserve MySQL optimizer hints in generated query text (#4382)
* Preserve MySQL optimizer hints in stripped query text Fixes #4353. Hint comments (/*+ ... */) on their own line were being treated as regular block comments and removed from the query string embedded in generated code. Inline hints already survived because the surrounding line did not match the block-comment pattern. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * Drop unit test; end-to-end coverage is sufficient Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent bba5fc9 commit 3f41c61

File tree

7 files changed

+109
-0
lines changed

7 files changed

+109
-0
lines changed

internal/endtoend/testdata/mysql_optimizer_hints/mysql/go/db.go

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/mysql_optimizer_hints/mysql/go/models.go

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/mysql_optimizer_hints/mysql/go/query.sql.go

Lines changed: 37 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- name: InlineHint :one
2+
SELECT /*+ MAX_EXECUTION_TIME(1000) */ bar FROM foo LIMIT 1;
3+
4+
-- name: MultilineHint :one
5+
SELECT
6+
/*+ MAX_EXECUTION_TIME(1000) */
7+
bar
8+
FROM foo
9+
LIMIT 1;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE TABLE foo (bar text);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"path": "go",
6+
"engine": "mysql",
7+
"name": "querytest",
8+
"schema": "schema.sql",
9+
"queries": "query.sql"
10+
}
11+
]
12+
}

internal/source/code.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ func StripComments(sql string) (string, []string, error) {
115115
continue
116116
}
117117
if strings.HasPrefix(t, "/*") && strings.HasSuffix(t, "*/") {
118+
// Preserve MySQL optimizer hints, which share block-comment
119+
// syntax but are semantically part of the query.
120+
if strings.HasPrefix(t, "/*+") {
121+
lines = append(lines, t)
122+
continue
123+
}
118124
t = strings.TrimPrefix(t, "/*")
119125
t = strings.TrimSuffix(t, "*/")
120126
comments = append(comments, t)

0 commit comments

Comments
 (0)