Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Fix inefficiencies in formatValue (#82103)

Oleh K7f4883cc49e0
diff --git a/frontend/src/metabase/utils/formatting/numbers.tsx b/frontend/src/metabase/utils/formatting/numbers.tsx
index 90c7dfa89f9..ec42c9b354e 100644
--- a/frontend/src/metabase/utils/formatting/numbers.tsx
+++ b/frontend/src/metabase/utils/formatting/numbers.tsx
@@ -182,14 +182,15 @@ export function formatChangeWithSign(
export const formatPercent = (percent: number) =>
`${(100 * percent).toFixed(Math.abs(percent) === 1 ? 0 : 2)} %`;
+const NUMBER_FORMATTER_CACHE_SIZE = 100;
+const numberFormatterCache = new Map<string, Intl.NumberFormat>();
+
export function numberFormatterForOptions(options: FormatNumberOptions) {
options = {
...getDefaultNumberOptions(options),
...options,
};
- // always use "en" locale so we have known number separators we can replace depending on number_separators option
- // TODO: if we do that how can we get localized currency names?
- return new Intl.NumberFormat("en", {
+ const formatterOptions: Intl.NumberFormatOptions = {
// Unjustified type cast. FIXME
style: options.number_style as Intl.NumberFormatOptions["style"],
currency: options.currency,
@@ -203,7 +204,23 @@ export function numberFormatterForOptions(options: FormatNumberOptions) {
maximumFractionDigits: options.maximumFractionDigits,
minimumSignificantDigits: options.minimumSignificantDigits,
maximumSignificantDigits: options.maximumSignificantDigits,
- });
+ };
+ const cacheKey = JSON.stringify(formatterOptions);
+ const cached = numberFormatterCache.get(cacheKey);
+ if (cached) {
+ return cached;
+ }
+ // always use "en" locale so we have known number separators we can replace depending on number_separators option
+ // TODO: if we do that how can we get localized currency names?
+ const formatter = new Intl.NumberFormat("en", formatterOptions);
+ if (numberFormatterCache.size >= NUMBER_FORMATTER_CACHE_SIZE) {
+ const oldestKey = numberFormatterCache.keys().next().value;
+ if (oldestKey !== undefined) {
+ numberFormatterCache.delete(oldestKey);
+ }
+ }
+ numberFormatterCache.set(cacheKey, formatter);
+ return formatter;
}
function formatNumberCompact(
diff --git a/frontend/src/metabase/utils/formatting/numbers.unit.spec.ts b/frontend/src/metabase/utils/formatting/numbers.unit.spec.ts
index e1794c89d57..650c894f9dc 100644
--- a/frontend/src/metabase/utils/formatting/numbers.unit.spec.ts
+++ b/frontend/src/metabase/utils/formatting/numbers.unit.spec.ts
@@ -415,3 +415,41 @@ describe("formatPercent", () => {
expect(formatPercent(0)).toBe("0.00 %");
});
});
+
+describe("numberFormatterForOptions caching", () => {
+ it("should reuse the formatter for equivalent options", () => {
+ const options = { number_style: "currency" as const, currency: "EUR" };
+
+ expect(numberFormatterForOptions(options)).toBe(
+ numberFormatterForOptions({ ...options }),
+ );
+ });
+
+ it("should build a separate formatter for different options", () => {
+ const usd = numberFormatterForOptions({
+ number_style: "currency",
+ currency: "USD",
+ });
+ const eur = numberFormatterForOptions({
+ number_style: "currency",
+ currency: "EUR",
+ });
+
+ expect(eur).not.toBe(usd);
+ expect(usd.format(1)).not.toBe(eur.format(1));
+ });
+
+ it("should evict the oldest formatter once the cache is full", async () => {
+ jest.resetModules();
+ const { numberFormatterForOptions: freshCache } = await import("./numbers");
+ const oldest = freshCache({ minimumIntegerDigits: 7 });
+ expect(freshCache({ minimumIntegerDigits: 7 })).toBe(oldest);
+
+ // NUMBER_FORMATTER_CACHE_SIZE is 100, so this pushes the first entry out
+ for (let digits = 0; digits <= 100; digits++) {
+ freshCache({ maximumFractionDigits: digits });
+ }
+
+ expect(freshCache({ minimumIntegerDigits: 7 })).not.toBe(oldest);
+ });
+});
diff --git a/frontend/src/metabase/value-formatting/url.ts b/frontend/src/metabase/value-formatting/url.ts
index 9f1a20f472c..d3a693a1154 100644
--- a/frontend/src/metabase/value-formatting/url.ts
+++ b/frontend/src/metabase/value-formatting/url.ts
@@ -1,5 +1,4 @@
import { removeNewLines } from "metabase/utils/formatting";
-import { isURL } from "metabase-lib/v1/types/utils/isa";
import type { ColumnSettings } from "metabase-types/api";
import { getDataFromClicked } from "./click-data";
@@ -8,6 +7,7 @@ import { getJsxLinkRenderer } from "./registry";
import {
type FormatValueOptions,
formatValue,
+ getColumnTypePredicates,
getRemappedValue,
} from "./value";
@@ -24,6 +24,9 @@ export function isDefaultLinkProtocol(protocol: string) {
}
export function getUrlProtocol(url: string) {
+ if (!url.includes(":")) {
+ return undefined;
+ }
try {
const { protocol } = new URL(url);
return protocol;
@@ -32,7 +35,10 @@ export function getUrlProtocol(url: string) {
}
}
-export function formatUrl(value: string, options: FormatValueOptions = {}) {
+export function formatUrl(
+ value: unknown,
+ options: FormatValueOptions = {},
+): React.ReactElement | string | number | null {
const { jsx, rich, column, collapseNewlines, copyLinkUrl } = options;
const url = getLinkUrl(value, options);
@@ -43,15 +49,18 @@ export function formatUrl(value: string, options: FormatValueOptions = {}) {
return jsxLinkRenderer(url, text);
} else if (url && copyLinkUrl) {
return url;
- } else if (!url && !isURL(column)) {
+ } else if (!url && !getColumnTypePredicates(column).isURL) {
// Even when no URL is found, return a formatted value
return formatValue(value, { ...options, view_as: null });
} else {
- return collapseNewlines ? removeNewLines(value) : value;
+ const text = collapseNewlines ? removeNewLines(value) : value;
+ return typeof text === "string" || typeof text === "number" || text === null
+ ? text
+ : String(text);
}
}
-function getLinkText(value: string, options: ColumnSettings) {
+function getLinkText(value: unknown, options: ColumnSettings) {
const { view_as, link_text, clicked, collapseNewlines } = options;
const isExplicitLink = view_as === "link";
@@ -70,7 +79,7 @@ function getLinkText(value: string, options: ColumnSettings) {
}
function getLinkUrl(
- value: string,
+ value: unknown,
{ view_as, link_url, clicked, column }: ColumnSettings,
) {
const isExplicitLink = view_as === "link";
@@ -80,6 +89,10 @@ function getLinkUrl(
return renderLinkURLForClick(link_url, getDataFromClicked(clicked));
}
+ if (typeof value !== "string") {
+ return null;
+ }
+
const protocol = getUrlProtocol(value);
const isValueSafeLink = protocol && isSafeProtocol(protocol);
@@ -98,7 +111,10 @@ function getLinkUrl(
return value;
}
- if (view_as === undefined && (isURL(column) || isDefaultProtocol)) {
+ if (
+ view_as === undefined &&
+ (getColumnTypePredicates(column).isURL || isDefaultProtocol)
+ ) {
return value;
}
diff --git a/frontend/src/metabase/value-formatting/url.unit.spec.tsx b/frontend/src/metabase/value-formatting/url.unit.spec.tsx
index cd33a43f14a..4423ba8971a 100644
--- a/frontend/src/metabase/value-formatting/url.unit.spec.tsx
+++ b/frontend/src/metabase/value-formatting/url.unit.spec.tsx
@@ -1,3 +1,5 @@
+import { createMockColumn } from "metabase-types/api/mocks";
+
import { formatUrl } from "./url";
// Pure engine behaviour only. The jsx + rich rendering paths (link components,
@@ -31,3 +33,25 @@ describe("formatUrl", () => {
expect(formatted).toEqual("http://metabase.com");
});
});
+
+describe("formatUrl with a non-string value", () => {
+ const column = createMockColumn({
+ base_type: "type/Text",
+ effective_type: "type/Text",
+ semantic_type: "type/URL",
+ });
+
+ it("should pass strings and numbers through untouched", () => {
+ expect(formatUrl("not a url", { column })).toBe("not a url");
+ expect(formatUrl(42, { column })).toBe(42);
+ });
+
+ it("should render a boolean rather than returning it raw", () => {
+ expect(formatUrl(true, { column })).toBe("true");
+ expect(formatUrl(false, { column })).toBe("false");
+ });
+
+ it("should render an object rather than returning it raw", () => {
+ expect(formatUrl({ nested: 1 }, { column })).toBe("[object Object]");
+ });
+});
diff --git a/frontend/src/metabase/value-formatting/value.tsx b/frontend/src/metabase/value-formatting/value.tsx
index 49d258c475f..07c07b3fbc7 100644
--- a/frontend/src/metabase/value-formatting/value.tsx
+++ b/frontend/src/metabase/value-formatting/value.tsx
@@ -31,6 +31,76 @@ export type FormatValueOptions = ColumnSettings & {
copyLinkUrl?: boolean;
};
+type ColumnTypePredicates = {
+ isURL: boolean;
+ isEmail: boolean;
+ isTime: boolean;
+ isDate: boolean;
+ isNumber: boolean;
+ isCoordinate: boolean;
+ isBoolean: boolean;
+};
+
+const NO_COLUMN_PREDICATES: ColumnTypePredicates = {
+ isURL: false,
+ isEmail: false,
+ isTime: false,
+ isDate: false,
+ isNumber: false,
+ isCoordinate: false,
+ isBoolean: false,
+};
+
+type ColumnPredicatesEntry = {
+ base_type: DatasetColumn["base_type"];
+ effective_type: DatasetColumn["effective_type"];
+ semantic_type: DatasetColumn["semantic_type"];
+ predicates: ColumnTypePredicates;
+};
+
+// each predicate fans out into several type-hierarchy lookups, and formatting a
+// table re-checks the same column for every cell, so compute them once per column.
+// Cache entries also snapshot the type fields they were computed from because
+// the custom-viz API hands third-party code mutable column objects,
+// so identity alone can go stale.
+const columnPredicatesCache = new WeakMap<
+ DatasetColumn,
+ ColumnPredicatesEntry
+>();
+
+export function getColumnTypePredicates(
+ column: DatasetColumn | null | undefined,
+): ColumnTypePredicates {
+ if (!column || typeof column !== "object") {
+ return NO_COLUMN_PREDICATES;
+ }
+ const entry = columnPredicatesCache.get(column);
+ if (
+ entry &&
+ entry.base_type === column.base_type &&
+ entry.effective_type === column.effective_type &&
+ entry.semantic_type === column.semantic_type
+ ) {
+ return entry.predicates;
+ }
+ const predicates = {
+ isURL: isURL(column),
+ isEmail: isEmail(column),
+ isTime: isTime(column),
+ isDate: isDate(column),
+ isNumber: isNumber(column),
+ isCoordinate: isCoordinate(column),
+ isBoolean: isBoolean(column),
+ };
+ columnPredicatesCache.set(column, {
+ base_type: column.base_type,
+ effective_type: column.effective_type,
+ semantic_type: column.semantic_type,
+ predicates,
+ });
+ return predicates;
+}
+
export function formatValue(value: unknown, _options: FormatValueOptions = {}) {
let { prefix, suffix, ...options } = _options;
// avoid rendering <ExternalLink> if we have click_behavior set
@@ -45,18 +115,19 @@ export function formatValue(value: unknown, _options: FormatValueOptions = {}) {
};
}
const formatted = formatValueRaw(value, options);
- let maybeJson = {};
- try {
- // Unjustified type cast. FIXME
- maybeJson = JSON.parse(value as string);
- } catch {
- // do nothing
- }
if (options.markdown_template) {
const renderJsxMarkdown = options.jsx
? getJsxMarkdownRenderer()
: undefined;
if (renderJsxMarkdown) {
+ let maybeJson = {};
+ if (typeof value === "string") {
+ try {
+ maybeJson = JSON.parse(value);
+ } catch {
+ // do nothing
+ }
+ }
// inject the formatted value as "value" and the unformatted value as "raw"
return renderJsxMarkdown(options.markdown_template, {
value: formatted,
@@ -88,7 +159,7 @@ export function formatValue(value: unknown, _options: FormatValueOptions = {}) {
}
export function getRemappedValue(
- value: string | number,
+ value: unknown,
{ remap, column }: ColumnSettings = {},
) {
if (remap && column) {
@@ -131,9 +202,9 @@ export function formatValueRaw(
};
const { column } = options;
+ const columnPredicates = getColumnTypePredicates(column);
- // Unjustified type cast. FIXME
- const remapped = getRemappedValue(value as string | number, options);
+ const remapped = getRemappedValue(value, options);
if (remapped !== undefined && options.view_as !== "link") {
value = remapped;
}
@@ -166,15 +237,14 @@ export function formatValueRaw(
getDataFromClicked(options.clicked),
);
} else if (
- (isURL(column) && options.view_as == null) ||
+ (columnPredicates.isURL && options.view_as == null) ||
options.view_as === "link"
) {
- // Unjustified type cast. FIXME
- return formatUrl(value as string, options);
- } else if (isEmail(column)) {
+ return formatUrl(value, options);
+ } else if (columnPredicates.isEmail) {
// Unjustified type cast. FIXME
return formatEmail(value as string, options);
- } else if (isTime(column)) {
+ } else if (columnPredicates.isTime) {
// Unjustified type cast. FIXME
return formatTime(value as Dayjs, column.unit, options);
} else if (column && column.unit != null) {
@@ -185,11 +255,9 @@ export function formatValueRaw(
options,
);
} else if (
- isDate(column) ||
+ columnPredicates.isDate ||
isDateValue(value) ||
- dayjs.isDayjs(value) ||
- // Unjustified type cast. FIXME
- dayjs(value as string, ["YYYY-MM-DD'T'HH:mm:ss.SSSZ"], true).isValid()
+ dayjs.isDayjs(value)
) {
// Unjustified type cast. FIXME
return formatDateTimeWithUnit(value as string | number, "minute", options);
@@ -199,7 +267,7 @@ export function formatValueRaw(
// it could be a remap
// TODO(eric, 2025-12-23): The second check should probably be in parseNumber(),
// but it caused tests to fail so I put it here.
- if (isNumber(column) && Number.isFinite(Number(value))) {
+ if (columnPredicates.isNumber && Number.isFinite(Number(value))) {
const number = parseNumber(value);
if (number != null) {
return formatNumber(number, options);
@@ -212,23 +280,23 @@ export function formatValueRaw(
return options.collapseNewlines ? removeNewLines(value) : value;
}
return formatStringFallback(value, options);
- } else if (typeof value === "number" && isCoordinate(column)) {
+ } else if (typeof value === "number" && columnPredicates.isCoordinate) {
const range = rangeForValue(value, column);
if (range && !options.noRange) {
return formatRange(range, formatCoordinate, options);
} else {
return formatCoordinate(value, options);
}
- } else if (typeof value === "number" && isNumber(column)) {
+ } else if (typeof value === "number" && columnPredicates.isNumber) {
const range = rangeForValue(value, column);
if (range && !options.noRange) {
return formatRange(range, formatNumber, options);
} else {
return formatNumber(value, options);
}
- } else if (typeof value === "bigint" && isNumber(column)) {
+ } else if (typeof value === "bigint" && columnPredicates.isNumber) {
return formatNumber(value, options);
- } else if (typeof value === "boolean" && isBoolean(column)) {
+ } else if (typeof value === "boolean" && columnPredicates.isBoolean) {
return JSON.stringify(value);
} else if (typeof value === "object") {
// no extra whitespace for table cells
diff --git a/frontend/src/metabase/value-formatting/value.unit.spec.tsx b/frontend/src/metabase/value-formatting/value.unit.spec.tsx
index 569a3d91380..bc47fc0ec53 100644
--- a/frontend/src/metabase/value-formatting/value.unit.spec.tsx
+++ b/frontend/src/metabase/value-formatting/value.unit.spec.tsx
@@ -197,4 +197,17 @@ describe("formatValue", () => {
}),
).toEqual("7/7/2019");
});
+
+ it("should re-read column type fields when the same column object is mutated", () => {
+ const column = createMockColumn({
+ base_type: "type/Text",
+ effective_type: "type/Text",
+ semantic_type: null,
+ });
+ expect(formatValue("12345", { column })).toBe("12345");
+
+ column.base_type = "type/Integer";
+ column.effective_type = "type/Integer";
+ expect(formatValue("12345", { column })).toBe("12,345");
+ });
});