Skip to content

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

:bug: Database rollup fields display unique values incorrectly

Daniel92f1e4a3a497
diff --git a/kernel/av/value.go b/kernel/av/value.go
index 501067353b..b146e21c07 100644
--- a/kernel/av/value.go
+++ b/kernel/av/value.go
@@ -885,6 +885,7 @@ func (r *ValueRollup) calcContents(calc *RollupCalc, destKey *Key) {
switch calc.Operator {
case CalcOperatorNone:
case CalcOperatorUniqueValues:
+ var newContents []*Value
uniqueValues := map[string]bool{}
for _, content := range r.Contents {
switch content.Type {
@@ -898,6 +899,9 @@ func (r *ValueRollup) calcContents(calc *RollupCalc, destKey *Key) {
}
}
content.Relation.Contents = newRelationContents
+ if 0 < len(newRelationContents) {
+ newContents = append(newContents, content)
+ }
case KeyTypeMSelect:
var newMSelect []*ValueSelect
for _, mSelect := range content.MSelect {
@@ -907,6 +911,9 @@ func (r *ValueRollup) calcContents(calc *RollupCalc, destKey *Key) {
}
}
content.MSelect = newMSelect
+ if 0 < len(newMSelect) {
+ newContents = append(newContents, content)
+ }
case KeyTypeMAsset:
var newMAsset []*ValueAsset
for _, mAsset := range content.MAsset {
@@ -916,8 +923,18 @@ func (r *ValueRollup) calcContents(calc *RollupCalc, destKey *Key) {
}
}
content.MAsset = newMAsset
+ if 0 < len(newMAsset) {
+ newContents = append(newContents, content)
+ }
+ default:
+ key := content.String(true)
+ if !uniqueValues[key] {
+ uniqueValues[key] = true
+ newContents = append(newContents, content)
+ }
}
}
+ r.Contents = newContents
case CalcOperatorCountAll:
r.Contents = []*Value{{Type: KeyTypeNumber, Number: NewFormattedValueNumber(float64(len(r.Contents)), NumberFormatNone)}}
case CalcOperatorCountValues:
diff --git a/kernel/av/value_test.go b/kernel/av/value_test.go
new file mode 100644
index 0000000000..e37e60c6e0
--- /dev/null
+++ b/kernel/av/value_test.go
@@ -0,0 +1,55 @@
+// SiYuan - Refactor your thinking
+// Copyright (c) 2020-present, b3log.org
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+package av
+
+import "testing"
+
+func TestValueRollupCalcUniqueValues(t *testing.T) {
+ t.Run("Text", func(t *testing.T) {
+ rollup := &ValueRollup{Contents: []*Value{
+ {Type: KeyTypeText, Text: &ValueText{Content: "11"}},
+ {Type: KeyTypeText, Text: &ValueText{Content: "Carry Bag"}},
+ {Type: KeyTypeText, Text: &ValueText{Content: "Carry Bag"}},
+ }}
+
+ rollup.calcContents(&RollupCalc{Operator: CalcOperatorUniqueValues}, &Key{Type: KeyTypeText})
+
+ if 2 != len(rollup.Contents) {
+ t.Fatalf("expected 2 unique values, got %d", len(rollup.Contents))
+ }
+ if "11" != rollup.Contents[0].String(true) || "Carry Bag" != rollup.Contents[1].String(true) {
+ t.Fatalf("unexpected unique values: %q, %q", rollup.Contents[0].String(true), rollup.Contents[1].String(true))
+ }
+ })
+
+ t.Run("Multiple Select", func(t *testing.T) {
+ rollup := &ValueRollup{Contents: []*Value{
+ {Type: KeyTypeMSelect, MSelect: []*ValueSelect{{Content: "A"}, {Content: "B"}}},
+ {Type: KeyTypeMSelect, MSelect: []*ValueSelect{{Content: "B"}, {Content: "C"}}},
+ {Type: KeyTypeMSelect, MSelect: []*ValueSelect{{Content: "A"}}},
+ }}
+
+ rollup.calcContents(&RollupCalc{Operator: CalcOperatorUniqueValues}, &Key{Type: KeyTypeMSelect})
+
+ if 2 != len(rollup.Contents) {
+ t.Fatalf("expected 2 non-empty values, got %d", len(rollup.Contents))
+ }
+ if "A B" != rollup.Contents[0].String(true) || "C" != rollup.Contents[1].String(true) {
+ t.Fatalf("unexpected unique values: %q, %q", rollup.Contents[0].String(true), rollup.Contents[1].String(true))
+ }
+ })
+}