Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

MUL-7146: lowercase comment content once per search row (#8164)

Multica Eve200dcb44cc15
diff --git a/server/internal/handler/issue.go b/server/internal/handler/issue.go
index efeda7fbea..3b5161162b 100644
--- a/server/internal/handler/issue.go
+++ b/server/internal/handler/issue.go
@@ -702,22 +702,23 @@ func buildSearchQuery(phrase string, terms []string, queryNum int, hasNum bool,
// final page is known. Per-term BOOL_OR flags keep the legacy eligibility rule
// where terms may be spread across comments, while comment_all_terms keeps
// ranking/snippet tied to one comment.
+ const loweredCommentContent = "lowered_comment.lowered"
commentFlagColumns := []string{
"c.issue_id",
- fmt.Sprintf("BOOL_OR(LOWER(c.content) LIKE %s) AS comment_phrase", phraseContainsParam),
+ fmt.Sprintf("BOOL_OR(%s LIKE %s) AS comment_phrase", loweredCommentContent, phraseContainsParam),
}
commentCandidateFlags := []string{"aggregated_comments.comment_phrase"}
commentTerms := make([]string, 0, len(termContainsParams))
for index, termParam := range termContainsParams {
alias := fmt.Sprintf("comment_term_%d", index)
commentFlagColumns = append(commentFlagColumns,
- fmt.Sprintf("BOOL_OR(LOWER(c.content) LIKE %s) AS %s", termParam, alias),
+ fmt.Sprintf("BOOL_OR(%s LIKE %s) AS %s", loweredCommentContent, termParam, alias),
)
commentCandidateFlags = append(commentCandidateFlags, "aggregated_comments."+alias)
- commentTerms = append(commentTerms, fmt.Sprintf("LOWER(c.content) LIKE %s", termParam))
+ commentTerms = append(commentTerms, fmt.Sprintf("%s LIKE %s", loweredCommentContent, termParam))
}
- commentSnippetPredicate := fmt.Sprintf("LOWER(c.content) LIKE %s", phraseContainsParam)
+ commentSnippetPredicate := fmt.Sprintf("%s LIKE %s", loweredCommentContent, phraseContainsParam)
if len(commentTerms) > 1 {
commentAllTerms := "(" + strings.Join(commentTerms, " AND ") + ")"
commentFlagColumns = append(commentFlagColumns,
@@ -735,11 +736,20 @@ func buildSearchQuery(phrase string, terms []string, queryNum int, hasNum bool,
commentSnippetPredicate,
))
+ // PostgreSQL otherwise inlines this scalar LATERAL subquery and recomputes
+ // LOWER(c.content) for every flag. OFFSET 0 is the intentional planner fence
+ // that keeps the long comment body lowercased once per row. Future indexable
+ // text predicates must stay outside this projection-only fence so an index on
+ // LOWER(c.content) can still match them.
commentMatchesCTE := fmt.Sprintf(`comment_matches AS MATERIALIZED (
SELECT *
FROM (
SELECT %s
FROM comment c
+ CROSS JOIN LATERAL (
+ SELECT LOWER(c.content) AS lowered
+ OFFSET 0
+ ) lowered_comment
WHERE c.workspace_id = %s
GROUP BY c.issue_id
) aggregated_comments
diff --git a/server/internal/handler/search_test.go b/server/internal/handler/search_test.go
index 2da26e992b..310b39fe4c 100644
--- a/server/internal/handler/search_test.go
+++ b/server/internal/handler/search_test.go
@@ -23,8 +23,8 @@ func TestBuildSearchQuery_SingleTerm(t *testing.T) {
if !strings.Contains(query, "LOWER(COALESCE(i.description, '')) LIKE") {
t.Error("query should contain LOWER(COALESCE(i.description, '')) LIKE")
}
- if !strings.Contains(query, "LOWER(c.content) LIKE") {
- t.Error("query should contain LOWER(c.content) LIKE")
+ if !strings.Contains(query, "lowered_comment.lowered LIKE") {
+ t.Error("query should match against the pre-lowered comment content")
}
// Exact title rank should not double-LOWER the pattern.
@@ -106,6 +106,29 @@ func TestBuildSearchQuery_MultiTerm(t *testing.T) {
}
}
+func TestBuildSearchQuery_LowersCommentContentOnce(t *testing.T) {
+ query, _ := buildSearchQuery("Foo Bar Baz", []string{"Foo", "Bar", "Baz"}, 0, false, false, []string{"done", "cancelled"})
+
+ if count := strings.Count(query, "LOWER(c.content)"); count != 1 {
+ t.Fatalf("query lowers comment content %d times, want exactly once:\n%s", count, query)
+ }
+ if !strings.Contains(query, "CROSS JOIN LATERAL (") {
+ t.Fatalf("query does not use a lateral join for comment lowercasing:\n%s", query)
+ }
+ if !strings.Contains(query, "SELECT LOWER(c.content) AS lowered") {
+ t.Fatalf("query does not project pre-lowered comment content:\n%s", query)
+ }
+ if !strings.Contains(query, "OFFSET 0") {
+ t.Fatalf("query does not retain the OFFSET 0 planner fence around comment lowercasing:\n%s", query)
+ }
+ if !strings.Contains(query, ") lowered_comment") {
+ t.Fatalf("query does not retain the lateral alias after comment lowercasing:\n%s", query)
+ }
+ if strings.Contains(query, "LOWER(c.content) LIKE") {
+ t.Fatalf("comment predicates bypass the pre-lowered value:\n%s", query)
+ }
+}
+
func TestBuildSearchQuery_WithNumber(t *testing.T) {
query, args := buildSearchQuery("MUL-42", []string{"MUL-42"}, 42, true, false, []string{"done", "cancelled"})
@@ -334,7 +357,7 @@ func TestBuildSearchQuery_CommentSubqueryWorkspaceScope(t *testing.T) {
if fromCountMulti != 1 {
t.Errorf("multi-term query has %d comment scans, want exactly one:\n%s", fromCountMulti, multiQuery)
}
- if !strings.Contains(multiQuery, "BOOL_OR((LOWER(c.content) LIKE") || !strings.Contains(multiQuery, "AS comment_all_terms") {
+ if !strings.Contains(multiQuery, "BOOL_OR((lowered_comment.lowered LIKE") || !strings.Contains(multiQuery, "AS comment_all_terms") {
t.Errorf("multi-term query does not retain the same-comment all-terms flag:\n%s", multiQuery)
}
if !strings.Contains(multiQuery, "ARRAY_AGG(c.id ORDER BY c.created_at DESC, c.id DESC)") {
@@ -357,7 +380,7 @@ func TestBuildSearchQuery_HydratesOnlyTheSelectedPage(t *testing.T) {
if limitAt == -1 || issueHydrationAt < limitAt || commentHydrationAt < limitAt {
t.Fatalf("full issue/comment hydration must happen after LIMIT/OFFSET:\n%s", query)
}
- if lastTextMatch := strings.LastIndex(query, "LOWER(c.content) LIKE"); lastTextMatch > limitAt {
+ if lastTextMatch := strings.LastIndex(query, "lowered_comment.lowered LIKE"); lastTextMatch > limitAt {
t.Errorf("comment hydration repeats a text search after LIMIT/OFFSET:\n%s", query)
}
}