Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

Fix: respect explicit LIMIT in Cypher query_graph

Martin Vogelc4848a89aae2
diff --git a/internal/cypher/cypher_test.go b/internal/cypher/cypher_test.go
index 52e058ba8..f1a2861e6 100644
--- a/internal/cypher/cypher_test.go
+++ b/internal/cypher/cypher_test.go
@@ -968,6 +968,37 @@ func TestEdgePropertyFilterRegex(t *testing.T) {
}
}
+func TestApplyLimitRespectsExplicit(t *testing.T) {
+ rows := make([]map[string]any, 300)
+ for i := range rows {
+ rows[i] = map[string]any{"i": i}
+ }
+
+ // No explicit limit — should use maxRows default (200)
+ got := applyLimit(rows, 0, 200)
+ if len(got) != 200 {
+ t.Errorf("no limit: expected 200 rows, got %d", len(got))
+ }
+
+ // Explicit limit below maxRows
+ got = applyLimit(rows, 50, 200)
+ if len(got) != 50 {
+ t.Errorf("limit=50: expected 50 rows, got %d", len(got))
+ }
+
+ // Explicit limit above maxRows — must be respected (not silently capped)
+ got = applyLimit(rows, 250, 200)
+ if len(got) != 250 {
+ t.Errorf("limit=250: expected 250 rows, got %d", len(got))
+ }
+
+ // Explicit limit above total rows — returns all
+ got = applyLimit(rows, 500, 200)
+ if len(got) != 300 {
+ t.Errorf("limit=500: expected 300 rows (all), got %d", len(got))
+ }
+}
+
func TestEdgeBuiltinPropertyFilter(t *testing.T) {
s := setupTestStoreMultiEdge(t)
defer s.Close()
diff --git a/internal/cypher/executor.go b/internal/cypher/executor.go
index 446f82bcb..632172222 100644
--- a/internal/cypher/executor.go
+++ b/internal/cypher/executor.go
@@ -1294,9 +1294,10 @@ func resolveOrderColumn(orderBy string, items []ReturnItem, cols []string) strin
return orderBy
}
-// applyLimit enforces the LIMIT clause on result rows.
+// applyLimit caps result rows. Explicit LIMIT values from the Cypher query are
+// respected; when no LIMIT is specified (limit <= 0), maxRows is used as default.
func applyLimit(rows []map[string]any, limit, maxRows int) []map[string]any {
- if limit <= 0 || limit > maxRows {
+ if limit <= 0 {
limit = maxRows
}
if len(rows) > limit {