From c8a3f5b591f21d32d0f5b9d998c80a3fa427359f Mon Sep 17 00:00:00 2001 From: Ragot Geoffrey Date: Mon, 9 Dec 2024 21:24:25 +0100 Subject: [PATCH] fix: balance aggregated with address and metadata filters (#610) * fix: error handling at storage level * fix: balance aggregation combining partial address filtering and metadata --- internal/storage/ledger/balances.go | 11 +++-------- internal/storage/ledger/balances_test.go | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/internal/storage/ledger/balances.go b/internal/storage/ledger/balances.go index 7bf95e7d8..adbb07950 100644 --- a/internal/storage/ledger/balances.go +++ b/internal/storage/ledger/balances.go @@ -95,6 +95,7 @@ func (s *Store) selectAccountWithAssetAndVolumes(date *time.Time, useInsertionDa ColumnExpr("*"). TableExpr("(?) accounts_volumes", selectAccountsWithVolumes) + needAccount := needAddressSegment if needMetadata { if s.ledger.HasFeature(features.FeatureAccountMetadataHistory, "SYNC") && date != nil && !date.IsZero() { selectAccountsWithVolumes = selectAccountsWithVolumes. @@ -103,17 +104,11 @@ func (s *Store) selectAccountWithAssetAndVolumes(date *time.Time, useInsertionDa s.selectDistinctAccountMetadataHistories(date), ) } else { - selectAccountsWithVolumes = selectAccountsWithVolumes. - Join( - `join (?) accounts on accounts.address = accounts_volumes.accounts_address`, - s.db.NewSelect(). - ModelTableExpr(s.GetPrefixedRelationName("accounts")). - Where("ledger = ?", s.ledger.Name), - ) + needAccount = true } } - if needAddressSegment { + if needAccount { selectAccountsWithVolumes = s.db.NewSelect(). TableExpr( "(?) accounts", diff --git a/internal/storage/ledger/balances_test.go b/internal/storage/ledger/balances_test.go index 5b32c9aaa..3507962df 100644 --- a/internal/storage/ledger/balances_test.go +++ b/internal/storage/ledger/balances_test.go @@ -325,4 +325,20 @@ func TestBalancesAggregates(t *testing.T) { ), }, ret) }) + + t.Run("using a filter on metadata and on address", func(t *testing.T) { + t.Parallel() + ret, err := store.GetAggregatedBalances(ctx, ledgercontroller.NewGetAggregatedBalancesQuery( + ledgercontroller.PITFilter{}, + query.And( + query.Match("address", "users:"), + query.Match("metadata[category]", "premium"), + ), + false, + )) + require.NoError(t, err) + require.Equal(t, ledger.BalancesByAssets{ + "USD": big.NewInt(0).Mul(bigInt, big.NewInt(2)), + }, ret) + }) }