Skip to content
Snippets Groups Projects
Unverified Commit a43fe5a7 authored by metabase-bot[bot]'s avatar metabase-bot[bot] Committed by GitHub
Browse files

Cannot use filter relative interval on breakout fields (#28799) (#28930)


Fixes #25378

When using a relative filter with a starting from n units ago on an
aggregation with the same units, we were incorrectly assuming that we
would be working with a field ref at the top level. However, in this
scenario the field is nested such as `[:+ [:field $created_at
{:temporal-unit :month}] [:interval 1 :month]]`. This change makes sure
that we are altering the field-ref.

Co-authored-by: default avatarCase Nelson <case@metabase.com>
parent b5abb60a
No related branches found
No related tags found
No related merge requests found
......@@ -24,7 +24,7 @@ const questionDetails = {
display: "line",
};
describe.skip("issue 25378", () => {
describe("issue 25378", () => {
beforeEach(() => {
restore();
cy.signInAsAdmin();
......
......@@ -69,7 +69,9 @@
(:end (u.date/range t unit)))
(defn- change-temporal-unit-to-default [field]
(mbql.u/update-field-options field assoc :temporal-unit :default))
(mbql.u/replace field
[:field _ (_ :guard (comp optimizable-units :temporal-unit))]
(mbql.u/update-field-options &match assoc :temporal-unit :default)))
(defmulti ^:private temporal-value-lower-bound
"Get a clause representing the *lower* bound that should be used when converting a `temporal-value-clause` (e.g.
......
......@@ -353,3 +353,27 @@
{:source-query
{:source-table $$checkins
:filter [:= [:field %date {:temporal-unit :month}] [:relative-datetime -1 :month]]}})))))))
(deftest optimize-filter-with-nested-compatible-field
(testing "Should optimize fields when starting n :temporal-unit ago (#25378)"
(mt/$ids users
(is (= (mt/mbql-query users
{:filter [:and
[:>=
[:+ [:field %last_login {:temporal-unit :default}] [:interval 3 :month]]
[:relative-datetime -12 :month]]
[:<
[:+ [:field %last_login {:temporal-unit :default}] [:interval 3 :month]]
[:relative-datetime 1 :month]]]
:source-query {:source-table $$users
:aggregation [[:count]]
:breakout [[:field %last_login {:temporal-unit :month}]]}})
(optimize-temporal-filters/optimize-temporal-filters
(mt/mbql-query users
{:filter [:between
[:+ [:field %last_login {:temporal-unit :month}] [:interval 3 :month]]
[:relative-datetime -12 :month]
[:relative-datetime 0 :month]]
:source-query {:source-table $$users
:aggregation [[:count]]
:breakout [[:field %last_login {:temporal-unit :month}]]}})))))))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment