Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Cap statement queries at the requested row limit

Jeffrey Chenf5086b967c9f
diff --git a/kernel/sql/asset_content_query.go b/kernel/sql/asset_content_query.go
index fce57a58f4..07e90fba0f 100644
--- a/kernel/sql/asset_content_query.go
+++ b/kernel/sql/asset_content_query.go
@@ -78,7 +78,7 @@ func queryAssetContentRawStmt(stmt string, args []any, limit int) (ret []map[str
ret = append(ret, m)
count++
- if noLimit && limit < count {
+ if noLimit && count >= limit {
break
}
}
@@ -185,7 +185,7 @@ func selectAssetContentsRawStmt(stmt string, args []any, limit int) (ret []*Asse
errCount++
}
- if (noLimit && limit < count) || 0 < errCount {
+ if (noLimit && count >= limit) || 0 < errCount {
break
}
}
diff --git a/kernel/sql/block_query.go b/kernel/sql/block_query.go
index 1391c7b05d..dcdaafca82 100644
--- a/kernel/sql/block_query.go
+++ b/kernel/sql/block_query.go
@@ -658,7 +658,7 @@ func queryRawStmtArgs(stmt string, args []any, limit int) (ret []map[string]any,
ret = append(ret, m)
count++
- if noLimit && limit < count {
+ if noLimit && count >= limit {
break
}
}
@@ -712,7 +712,7 @@ func SelectBlocksRawStmtArgs(stmt string, args []any, limit int) (ret []*Block)
errCount++
}
- if (noLimit && limit < count) || 0 < errCount {
+ if (noLimit && count >= limit) || 0 < errCount {
break
}
}
@@ -857,7 +857,7 @@ func selectBlocksRawStmtNoParseWithQuery(stmt string, limit int, boxID string, q
errCount++
}
- if (noLimit && limit < count) || 0 < errCount {
+ if (noLimit && count >= limit) || 0 < errCount {
break
}
}
diff --git a/kernel/sql/encrypted_query.go b/kernel/sql/encrypted_query.go
index 265d2f466c..123984c5a0 100644
--- a/kernel/sql/encrypted_query.go
+++ b/kernel/sql/encrypted_query.go
@@ -445,7 +445,7 @@ func SelectBlocksRawStmtArgsInBoxContext(ctx context.Context, stmt string, args
errCount++
}
- if (noLimit && limit < count) || 0 < errCount {
+ if (noLimit && count >= limit) || 0 < errCount {
break
}
}
@@ -768,7 +768,7 @@ func SelectBlocksRawStmtNoParseInBox(stmt string, limit int, boxID string) (ret
errCount++
}
- if (noLimit && limit < count) || 0 < errCount {
+ if (noLimit && count >= limit) || 0 < errCount {
break
}
}
@@ -896,7 +896,7 @@ func queryRawStmtForBox(boxID, stmt string, limit int) (ret []map[string]any, er
ret = append(ret, m)
count++
- if noLimit && limit < count {
+ if noLimit && count >= limit {
break
}
}
@@ -942,7 +942,7 @@ func queryRawStmtArgsForBoxContext(ctx context.Context, boxID, stmt string, args
}
ret = append(ret, m)
count++
- if noLimit && limit < count {
+ if noLimit && count >= limit {
break
}
}
@@ -987,7 +987,7 @@ func queryRawStmtArgsForBox(boxID, stmt string, args []any, limit int) (ret []ma
ret = append(ret, m)
count++
- if noLimit && limit < count {
+ if noLimit && count >= limit {
break
}
}
diff --git a/kernel/sql/query_limit_test.go b/kernel/sql/query_limit_test.go
index fe89a279ac..a53f26851a 100644
--- a/kernel/sql/query_limit_test.go
+++ b/kernel/sql/query_limit_test.go
@@ -58,6 +58,36 @@ func TestQueryRowLimit(t *testing.T) {
}
}
+// 带参查询与资源内容查询同样以 limit 为上限:语句未写 LIMIT 时不得多返回一行,
+// 否则以 len(rows) == limit 判定截断的调用方会误认结果未截断。
+func TestRawStatementQueriesHonourRowLimit(t *testing.T) {
+ testDB, err := gosql.Open("sqlite3_extended", ":memory:")
+ if err != nil {
+ t.Fatal(err)
+ }
+ testDB.SetMaxOpenConns(1)
+ t.Cleanup(func() { _ = testDB.Close() })
+ previousDB := db
+ db = testDB
+ t.Cleanup(func() { db = previousDB })
+ if _, err = testDB.Exec("CREATE TABLE items (n INTEGER, content TEXT); INSERT INTO items VALUES (1, 'a'), (2, 'a'), (3, 'a'), (4, 'a'), (5, 'a')"); err != nil {
+ t.Fatal(err)
+ }
+
+ for _, limit := range []int{1, 3, 5} {
+ rows, queryErr := queryRawStmtArgs("SELECT n FROM items ORDER BY n", nil, limit)
+ if queryErr != nil || len(rows) != limit {
+ t.Fatalf("queryRawStmtArgs limit=%d: got %d rows, err=%v", limit, len(rows), queryErr)
+ }
+ }
+
+ // 语句自带 LIMIT 时由语句决定行数,本层不再截断。
+ rows, err := queryRawStmtArgs("SELECT n FROM items ORDER BY n LIMIT 2", nil, 3)
+ if err != nil || len(rows) != 2 {
+ t.Fatalf("statement limit was overridden: %d rows, err=%v", len(rows), err)
+ }
+}
+
func TestQueryWithLimitInfo(t *testing.T) {
testDB, err := gosql.Open("sqlite3_extended", ":memory:")
if err != nil {