🐛 Incorrect results for database rollup field filtering https://github.com/siyuan-note/siyuan/issues/15608

This commit is contained in:
Daniel 2025-08-22 16:00:43 +08:00
parent fa7ec5ab95
commit 312be8a019
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
4 changed files with 34 additions and 61 deletions

View file

@ -182,29 +182,21 @@ func (value *Value) Filter(filter *ViewFilter, attrView *AttributeView, rowID st
return false return false
} }
for _, blockID := range relVal.Relation.BlockIDs { value.Rollup.Contents = GetRollupContents(destAv, destKey, relVal.Relation.BlockIDs, nil)
destVal := destAv.GetValue(key.Rollup.KeyID, blockID) value.Rollup.RenderContents(key.Rollup.Calc, destKey)
if nil == destVal { for _, content := range value.Rollup.Contents {
if destAv.ExistItem(blockID) { // 数据库中存在项目但是字段值不存在是数据未初始化,这里补一个默认值
destVal = GetAttributeViewDefaultValue(ast.NewNodeID(), key.Rollup.KeyID, blockID, destKey.Type)
}
if nil == destVal {
continue
}
}
switch filter.Operator { switch filter.Operator {
case FilterOperatorContains: case FilterOperatorContains:
if destVal.filter(filter.Value.Rollup.Contents[0], filter.RelativeDate, filter.RelativeDate2, filter.Operator) { if content.filter(filter.Value.Rollup.Contents[0], filter.RelativeDate, filter.RelativeDate2, filter.Operator) {
return true return true
} }
case FilterOperatorDoesNotContain: case FilterOperatorDoesNotContain:
ret := destVal.filter(filter.Value.Rollup.Contents[0], filter.RelativeDate, filter.RelativeDate2, filter.Operator) ret := content.filter(filter.Value.Rollup.Contents[0], filter.RelativeDate, filter.RelativeDate2, filter.Operator)
if !ret { if !ret {
return false return false
} }
default: default:
if destVal.filter(filter.Value.Rollup.Contents[0], filter.RelativeDate, filter.RelativeDate2, filter.Operator) { if content.filter(filter.Value.Rollup.Contents[0], filter.RelativeDate, filter.RelativeDate2, filter.Operator) {
return true return true
} }
} }

View file

@ -796,6 +796,32 @@ type ValueRollup struct {
Contents []*Value `json:"contents"` Contents []*Value `json:"contents"`
} }
func GetRollupContents(destAv *AttributeView, destKey *Key, relationBlockIDs []string, furtherCollection Collection) (ret []*Value) {
ret = []*Value{}
for _, blockID := range relationBlockIDs {
destVal := destAv.GetValue(destKey.ID, blockID)
if nil != furtherCollection && KeyTypeTemplate == destKey.Type {
destVal = furtherCollection.GetValue(blockID, destKey.ID)
}
if nil == destVal {
if destAv.ExistItem(blockID) { // 数据库中存在项目但是字段值不存在是数据未初始化,这里补一个默认值
destVal = GetAttributeViewDefaultValue(ast.NewNodeID(), destKey.ID, blockID, destKey.Type)
}
if nil == destVal {
continue
}
}
if KeyTypeNumber == destKey.Type {
destVal.Number.Format = destKey.NumberFormat
destVal.Number.FormatNumber()
}
ret = append(ret, destVal.Clone())
}
return
}
func (r *ValueRollup) RenderContents(calc *RollupCalc, destKey *Key) { func (r *ValueRollup) RenderContents(calc *RollupCalc, destKey *Key) {
if nil == calc { if nil == calc {
return return

View file

@ -1534,28 +1534,7 @@ func GetBlockAttributeViewKeys(nodeID string) (ret []*BlockAttributeViewKeys) {
} }
} }
kv.Values[0].Rollup.Contents = nil kv.Values[0].Rollup.Contents = av.GetRollupContents(destAv, destKey, relVal.Relation.BlockIDs, furtherCollection)
for _, bID := range relVal.Relation.BlockIDs {
destVal := destAv.GetValue(kv.Key.Rollup.KeyID, bID)
if nil != furtherCollection && av.KeyTypeTemplate == destKey.Type {
destVal = furtherCollection.GetValue(bID, destKey.ID)
}
if nil == destVal {
if destAv.ExistItem(bID) { // 数据库中存在项目但是字段值不存在是数据未初始化,这里补一个默认值
destVal = av.GetAttributeViewDefaultValue(ast.NewNodeID(), kv.Key.Rollup.KeyID, bID, destKey.Type)
}
if nil == destVal {
continue
}
}
if av.KeyTypeNumber == destKey.Type {
destVal.Number.Format = destKey.NumberFormat
destVal.Number.FormatNumber()
}
kv.Values[0].Rollup.Contents = append(kv.Values[0].Rollup.Contents, destVal.Clone())
}
kv.Values[0].Rollup.RenderContents(kv.Key.Rollup.Calc, destKey) kv.Values[0].Rollup.RenderContents(kv.Key.Rollup.Calc, destKey)
} }
} }

View file

@ -26,7 +26,6 @@ import (
"time" "time"
"github.com/88250/gulu" "github.com/88250/gulu"
"github.com/88250/lute/ast"
"github.com/jinzhu/copier" "github.com/jinzhu/copier"
"github.com/siyuan-note/logging" "github.com/siyuan-note/logging"
"github.com/siyuan-note/siyuan/kernel/av" "github.com/siyuan-note/siyuan/kernel/av"
@ -385,7 +384,6 @@ func fillAttributeViewAutoGeneratedValues(attrView *av.AttributeView, collection
var furtherCollection av.Collection var furtherCollection av.Collection
if av.KeyTypeTemplate == destKey.Type { if av.KeyTypeTemplate == destKey.Type {
// 渲染目标视图,这样才能汇总渲染后的模板字段值 // 渲染目标视图,这样才能汇总渲染后的模板字段值
viewable := renderView(destAv, destAv.Views[0], "", depth, renderedAttrViews) viewable := renderView(destAv, destAv.Views[0], "", depth, renderedAttrViews)
if nil != viewable { if nil != viewable {
@ -396,29 +394,7 @@ func fillAttributeViewAutoGeneratedValues(attrView *av.AttributeView, collection
} }
} }
value.Rollup.Contents = nil value.Rollup.Contents = av.GetRollupContents(destAv, destKey, relVal.Relation.BlockIDs, furtherCollection)
for _, blockID := range relVal.Relation.BlockIDs {
destVal := destAv.GetValue(rollupKey.Rollup.KeyID, blockID)
if nil != furtherCollection && av.KeyTypeTemplate == destKey.Type {
destVal = furtherCollection.GetValue(blockID, destKey.ID)
}
if nil == destVal {
if destAv.ExistItem(blockID) { // 数据库中存在项目但是字段值不存在是数据未初始化,这里补一个默认值
destVal = av.GetAttributeViewDefaultValue(ast.NewNodeID(), rollupKey.Rollup.KeyID, blockID, destKey.Type)
}
if nil == destVal {
continue
}
}
if av.KeyTypeNumber == destKey.Type {
destVal.Number.Format = destKey.NumberFormat
destVal.Number.FormatNumber()
}
value.Rollup.Contents = append(value.Rollup.Contents, destVal.Clone())
}
value.Rollup.RenderContents(rollupKey.Rollup.Calc, destKey) value.Rollup.RenderContents(rollupKey.Rollup.Calc, destKey)
case av.KeyTypeRelation: // 渲染关联 case av.KeyTypeRelation: // 渲染关联
value.Relation.Contents = nil value.Relation.Contents = nil