metabase.models.field-usage/aggregation->field-usage wants to find all the field refs inside an aggregation.
lib.aggregation/aggregation-column naively assumes the 2nd element of an aggregation clause must be a direct ref. That's not true - it can be a subexpression that contains field refs for eg. a :sum-where.
That's straight-up a bug in aggregation-column and should be fixed. (By picking the inner column correctly for each kind of aggregation.)
But it's also a problem for aggregation->field-usage which actually wants both the field being eg. summed and any fields referenced in the :sum-where condition!
So some deeper change is needed to support aggregation->field-usage; probably a new lib.aggregation/aggregation-filter that returns the filtering expression for :count-where and :sum-where. (models.field-usage has logic for pulling apart expressions already.)
To Reproduce
Write a :sum-where aggregation.
Run the query
Observe the "Unknown type of ref" error on the client where find-match-column was called with the filter expression as the ref, rather than the column getting summed.
Describe the bug
metabase.models.field-usage/aggregation->field-usage
wants to find all the field refs inside an aggregation.lib.aggregation/aggregation-column
naively assumes the 2nd element of an aggregation clause must be a direct ref. That's not true - it can be a subexpression that contains field refs for eg. a:sum-where
.aggregation-column
and should be fixed. (By picking the inner column correctly for each kind of aggregation.)aggregation->field-usage
which actually wants both the field being eg. summed and any fields referenced in the:sum-where
condition!So some deeper change is needed to support
aggregation->field-usage
; probably a newlib.aggregation/aggregation-filter
that returns the filtering expression for:count-where
and:sum-where
. (models.field-usage
has logic for pulling apart expressions already.)To Reproduce
:sum-where
aggregation."Unknown type of ref"
error on the client wherefind-match-column
was called with the filter expression as the ref, rather than the column getting summed.Expected behavior
Correctly finding field usage in all cases!
Logs
No response
Information about your Metabase installation
any version; eg. master
Severity
Log spam, also a gap in field usage stats
Additional context
No response