Skip to content

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

:bug: Database rollup relation field entry counts are incorrect

Danielaf4829779c3d
diff --git a/kernel/av/value.go b/kernel/av/value.go
index b146e21c07..451cf97aaa 100644
--- a/kernel/av/value.go
+++ b/kernel/av/value.go
@@ -936,7 +936,16 @@ func (r *ValueRollup) calcContents(calc *RollupCalc, destKey *Key) {
}
r.Contents = newContents
case CalcOperatorCountAll:
- r.Contents = []*Value{{Type: KeyTypeNumber, Number: NewFormattedValueNumber(float64(len(r.Contents)), NumberFormatNone)}}
+ countAll := len(r.Contents)
+ if KeyTypeRelation == destKey.Type {
+ countAll = 0
+ for _, content := range r.Contents {
+ if nil != content.Relation {
+ countAll += len(content.Relation.BlockIDs)
+ }
+ }
+ }
+ r.Contents = []*Value{{Type: KeyTypeNumber, Number: NewFormattedValueNumber(float64(countAll), NumberFormatNone)}}
case CalcOperatorCountValues:
r.Contents = []*Value{{Type: KeyTypeNumber, Number: NewFormattedValueNumber(float64(len(r.Contents)), NumberFormatNone)}}
case CalcOperatorCountUniqueValues:
diff --git a/kernel/av/value_test.go b/kernel/av/value_test.go
index e37e60c6e0..19dc7c7789 100644
--- a/kernel/av/value_test.go
+++ b/kernel/av/value_test.go
@@ -53,3 +53,37 @@ func TestValueRollupCalcUniqueValues(t *testing.T) {
}
})
}
+
+func TestValueRollupCalcCountAll(t *testing.T) {
+ t.Run("Relation", func(t *testing.T) {
+ rollup := &ValueRollup{Contents: []*Value{
+ {Type: KeyTypeRelation, Relation: &ValueRelation{BlockIDs: []string{"order-1", "order-2"}}},
+ {Type: KeyTypeRelation, Relation: &ValueRelation{BlockIDs: []string{"order-3"}}},
+ }}
+
+ rollup.calcContents(&RollupCalc{Operator: CalcOperatorCountAll}, &Key{Type: KeyTypeRelation})
+
+ if 1 != len(rollup.Contents) || nil == rollup.Contents[0].Number {
+ t.Fatalf("unexpected calculation result: %+v", rollup.Contents)
+ }
+ if 3 != rollup.Contents[0].Number.Content {
+ t.Fatalf("expected 3 relation entries, got %v", rollup.Contents[0].Number.Content)
+ }
+ })
+
+ t.Run("Scalar", func(t *testing.T) {
+ rollup := &ValueRollup{Contents: []*Value{
+ {Type: KeyTypeText, Text: &ValueText{Content: "A"}},
+ {Type: KeyTypeText, Text: &ValueText{Content: "B"}},
+ }}
+
+ rollup.calcContents(&RollupCalc{Operator: CalcOperatorCountAll}, &Key{Type: KeyTypeText})
+
+ if 1 != len(rollup.Contents) || nil == rollup.Contents[0].Number {
+ t.Fatalf("unexpected calculation result: %+v", rollup.Contents)
+ }
+ if 2 != rollup.Contents[0].Number.Content {
+ t.Fatalf("expected 2 scalar entries, got %v", rollup.Contents[0].Number.Content)
+ }
+ })
+}