Skip to content

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

:bug: Correct relation and rollup value counts https://github.com/siyuan-note/siyuan/issues/19422

Daniel3ed15fa1b6fd
diff --git a/kernel/av/calc.go b/kernel/av/calc.go
index e7c0f1af13..d5808597cc 100644
--- a/kernel/av/calc.go
+++ b/kernel/av/calc.go
@@ -1688,7 +1688,7 @@ func calcFieldRelation(collection Collection, field Field, fieldIndex int) {
for _, item := range collection.GetItems() {
values := item.GetValues()
if nil != values[fieldIndex] && nil != values[fieldIndex].Relation {
- countValues++
+ countValues += len(values[fieldIndex].Relation.BlockIDs)
}
}
calc.Result = &Value{Number: NewFormattedValueNumber(float64(countValues), NumberFormatNone)}
@@ -1779,7 +1779,7 @@ func calcFieldRollup(collection Collection, field Field, fieldIndex int) {
for _, item := range collection.GetItems() {
values := item.GetValues()
if nil != values[fieldIndex] && nil != values[fieldIndex].Rollup {
- countValues++
+ countValues += len(values[fieldIndex].Rollup.Contents)
}
}
calc.Result = &Value{Number: NewFormattedValueNumber(float64(countValues), NumberFormatNone)}
diff --git a/kernel/av/calc_test.go b/kernel/av/calc_test.go
new file mode 100644
index 0000000000..c84879307e
--- /dev/null
+++ b/kernel/av/calc_test.go
@@ -0,0 +1,63 @@
+package av
+
+import "testing"
+
+func TestCalcRelationAndRollupCounts(t *testing.T) {
+ for _, keyType := range []KeyType{KeyTypeRelation, KeyTypeRollup} {
+ t.Run(string(keyType), func(t *testing.T) {
+ makeValue := func(contents ...string) *Value {
+ value := &Value{Type: keyType}
+ if keyType == KeyTypeRelation {
+ value.Relation = &ValueRelation{BlockIDs: contents}
+ } else {
+ value.Rollup = &ValueRollup{}
+ for _, content := range contents {
+ value.Rollup.Contents = append(value.Rollup.Contents, &Value{
+ Type: KeyTypeText, Text: &ValueText{Content: content},
+ })
+ }
+ }
+ return value
+ }
+ for _, test := range []struct {
+ name string
+ values []*Value
+ count float64
+ empty float64
+ notEmpty float64
+ }{
+ {name: "no rows"},
+ {name: "empty containers", values: []*Value{makeValue(), makeValue(), makeValue()}, empty: 3},
+ {name: "missing containers", values: []*Value{{Type: keyType}}, empty: 1},
+ {name: "multiple and repeated values", values: []*Value{makeValue(), makeValue("a", "b"), makeValue("a")}, count: 3, empty: 1, notEmpty: 2},
+ } {
+ t.Run(test.name, func(t *testing.T) {
+ column := &TableColumn{BaseInstanceField: &BaseInstanceField{Type: keyType}}
+ table := &Table{BaseInstance: &BaseInstance{}, Columns: []*TableColumn{column}}
+ for _, value := range test.values {
+ table.Rows = append(table.Rows, &TableRow{Cells: []*TableCell{
+ {BaseValue: &BaseValue{Value: value, ValueType: keyType}},
+ }})
+ }
+ for _, check := range []struct {
+ operator CalcOperator
+ want float64
+ }{
+ {CalcOperatorCountValues, test.count},
+ {CalcOperatorCountAll, float64(len(test.values))},
+ {CalcOperatorCountEmpty, test.empty},
+ {CalcOperatorCountNotEmpty, test.notEmpty},
+ } {
+ column.Calc = &FieldCalc{Operator: check.operator}
+ Calc(table, &AttributeView{})
+ if result := column.Calc.Result; result == nil || result.Number == nil {
+ t.Fatalf("%s: missing numeric result", check.operator)
+ } else if result.Number.Content != check.want {
+ t.Errorf("%s: got %v, want %v", check.operator, result.Number.Content, check.want)
+ }
+ }
+ })
+ }
+ })
+ }
+}