Skip to content

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

Check that the number was parsed correctly (#67403)

Eric Normand711a7285692e
diff --git a/frontend/src/metabase/lib/formatting/value.tsx b/frontend/src/metabase/lib/formatting/value.tsx
index 664cdd033e9..ee48a0d622c 100644
--- a/frontend/src/metabase/lib/formatting/value.tsx
+++ b/frontend/src/metabase/lib/formatting/value.tsx
@@ -196,7 +196,12 @@ export function formatValueRaw(
) {
return formatDateTimeWithUnit(value as string | number, "minute", options);
} else if (typeof value === "string") {
- if (isNumber(column)) {
+ // Check if we're looking for a number isNumber(column) and
+ // check that the value string is a valid number
+ // 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))) {
const number = parseNumber(value);
if (number != null) {
return formatNumber(number, options);
diff --git a/frontend/src/metabase/lib/formatting/value.unit.spec.tsx b/frontend/src/metabase/lib/formatting/value.unit.spec.tsx
index 8ea1b442045..1d032bb9a6e 100644
--- a/frontend/src/metabase/lib/formatting/value.unit.spec.tsx
+++ b/frontend/src/metabase/lib/formatting/value.unit.spec.tsx
@@ -112,6 +112,46 @@ describe("formatValue", () => {
setup(2, { column, scale: 100 });
expect(screen.getByText("200")).toBeInTheDocument();
});
+
+ it("should correctly parse string with initial digit", () => {
+ const column = createMockColumn({
+ base_type: "type/Float",
+ remapped_to_column: createMockColumn({
+ base_type: "type/Text",
+ }),
+ remapping: new Map([
+ [1, "1j"],
+ [2, "2"],
+ [3, "Three"],
+ ]),
+ } as any);
+ setup(1, { column, scale: 100 });
+ expect(screen.getByText("1j")).toBeInTheDocument();
+
+ setup(2, { column, scale: 100 });
+ expect(screen.getByText("200")).toBeInTheDocument();
+ });
+
+ it("should correctly parse string with big integer", () => {
+ const column = createMockColumn({
+ base_type: "type/Float",
+ remapped_to_column: createMockColumn({
+ base_type: "type/Text",
+ }),
+ remapping: new Map([
+ [1, "4000000000000000000"], // bigger than 9,007,199,254,740,991 to trigger BigInt branch
+ [2, "2"],
+ [3, "Three"],
+ ]),
+ } as any);
+ setup(1, { column, scale: 100 });
+ expect(
+ screen.getByText("400,000,000,000,000,000,000"),
+ ).toBeInTheDocument();
+
+ setup(2, { column, scale: 100 });
+ expect(screen.getByText("200")).toBeInTheDocument();
+ });
});
describe("collapseNewlines", () => {
diff --git a/frontend/src/metabase/lib/number.ts b/frontend/src/metabase/lib/number.ts
index 9970a12f328..fad9ed713d0 100644
--- a/frontend/src/metabase/lib/number.ts
+++ b/frontend/src/metabase/lib/number.ts
@@ -3,6 +3,14 @@ const INTEGER_REGEX = /^[+-]?\d+$/;
export type NumberValue = number | bigint;
export function parseNumber(value: string): NumberValue | null {
+ // TODO(eric, 2025-12-23): we should check if the string is a valid number
+ // if value === "1j", this function returns 1
+ // I tried it and it broke a number of tests
+ // Example:
+ // if (!Number.isFinite(Number(value))) {
+ // return null;
+ // }
+
const number = parseFloat(value);
if (!Number.isFinite(number)) {
return null;