Skip to content
Snippets Groups Projects
Unverified Commit e51f91da authored by Braden Shepherdson's avatar Braden Shepherdson Committed by GitHub
Browse files

[MLv2] Add `isTemporalExtraction` flag to `displayInfo` (#37889)

That applies to both freestanding temporal buckets and to breakouts with
`:temporal-unit` set.

Fixes #36978.
parent 9b10e888
No related branches found
No related tags found
No related merge requests found
import * as Lib from "metabase-lib";
import { createQuery, columnFinder } from "./test-helpers";
import { checkNotNull } from "metabase/lib/types";
import { createQuery, columnFinder, findTemporalBucket } from "./test-helpers";
describe("breakout", () => {
describe("add breakout", () => {
......@@ -49,6 +50,48 @@ describe("breakout", () => {
Lib.displayInfo(roundtripQuery, 0, roundtripTaxColumn).breakoutPosition,
).toBe(0);
});
it("should note whether the temporal unit is for extraction in the displayInfo", () => {
const userBirthDate = findBreakoutableColumn("PEOPLE", "BIRTH_DATE");
// "month-of-year" is a temporal extraction, it returns an integer.
const monthOfYear = findTemporalBucket(
query,
userBirthDate,
"Month of year",
);
expect(monthOfYear).toBeTruthy();
const monthOfYearInfo = Lib.displayInfo(
query,
0,
checkNotNull(monthOfYear),
);
expect(monthOfYearInfo.isTemporalExtraction).toBe(true);
const userBirthDateByMonthOfYear = Lib.withTemporalBucket(
userBirthDate,
monthOfYear,
);
const byMonthOfYear = Lib.breakout(query, 0, userBirthDateByMonthOfYear);
const [breakoutByMonthOfYear] = Lib.breakouts(byMonthOfYear, 0);
expect(
Lib.displayInfo(byMonthOfYear, 0, breakoutByMonthOfYear)
.isTemporalExtraction,
).toBe(true);
// "month" is a regular temporal bucket, which returns rounded datetimes.
const month = findTemporalBucket(query, userBirthDate, "Month");
expect(month).toBeTruthy();
const monthInfo = Lib.displayInfo(query, 0, checkNotNull(month));
expect(monthInfo.isTemporalExtraction).toBe(false);
const userBirthDateByMonth = Lib.withTemporalBucket(userBirthDate, month);
const byMonth = Lib.breakout(query, 0, userBirthDateByMonth);
const [breakoutByMonth] = Lib.breakouts(byMonth, 0);
expect(
Lib.displayInfo(byMonth, 0, breakoutByMonth).isTemporalExtraction,
).toBe(false);
});
});
describe("replace breakout", () => {
......
......@@ -113,6 +113,7 @@ export type BucketDisplayInfo = {
displayName: string;
default?: boolean;
selected?: boolean;
isTemporalExtraction?: boolean;
};
export type TableDisplayInfo = {
......@@ -188,7 +189,9 @@ export type ClauseDisplayInfo = Pick<
export type AggregationClauseDisplayInfo = ClauseDisplayInfo;
export type BreakoutClauseDisplayInfo = ClauseDisplayInfo;
export type BreakoutClauseDisplayInfo = ClauseDisplayInfo & {
isTemporalExtraction?: boolean;
};
export type OrderByClauseDisplayInfo = ClauseDisplayInfo & {
direction: OrderByDirection;
......
......@@ -381,6 +381,8 @@
:is-breakout (= source :source/breakouts)})
(when-some [selected (:selected? x-metadata)]
{:selected selected})
(when-let [temporal-unit ((some-fn :metabase.lib.field/temporal-unit :temporal-unit) x-metadata)]
{:is-temporal-extraction (contains? lib.schema.temporal-bucketing/datetime-extraction-units temporal-unit)})
(select-keys x-metadata [:breakout-position :order-by-position :filter-positions]))))
(defmethod display-info-method :default
......
......@@ -198,7 +198,9 @@
(defmethod lib.metadata.calculation/display-info-method :option/temporal-bucketing
[query stage-number option]
(merge {:display-name (lib.metadata.calculation/display-name query stage-number option)
:short-name (u/qualified-name (raw-temporal-bucket option))}
:short-name (u/qualified-name (raw-temporal-bucket option))
:is-temporal-extraction (contains? lib.schema.temporal-bucketing/datetime-extraction-units
(raw-temporal-bucket option))}
(select-keys option [:default :selected])))
(defmulti available-temporal-buckets-method
......
......@@ -176,21 +176,22 @@
(deftest ^:parallel short-name-display-info-test
(let [query lib.tu/venues-query]
(is (= #{"minute"
"hour"
"day"
"week"
"month"
"quarter"
"year"
"minute-of-hour"
"hour-of-day"
"day-of-week"
"day-of-month"
"day-of-year"
"week-of-year"
"month-of-year"
"quarter-of-year"}
(into #{}
(map #(:short-name (lib/display-info query -1 %)))
(is (= {"minute" false
"hour" false
"day" false
"week" false
"month" false
"quarter" false
"year" true
"minute-of-hour" true
"hour-of-day" true
"day-of-week" true
"day-of-month" true
"day-of-year" true
"week-of-year" true
"month-of-year" true
"quarter-of-year" true}
(into {}
(comp (map #(lib/display-info query -1 %))
(map (juxt :short-name :is-temporal-extraction)))
(lib.temporal-bucket/available-temporal-buckets query (meta/field-metadata :products :created-at)))))))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment