Skip to content

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

Handle legacy viz settings where `base-type` might not be present (#63468)

Alexander Polyankinc5b5970b77c5
diff --git a/e2e/test/scenarios/visualizations-tabular/visualizations-tabular-reproductions.cy.spec.js b/e2e/test/scenarios/visualizations-tabular/visualizations-tabular-reproductions.cy.spec.js
index eef9ae8e294..785a1ab3cf3 100644
--- a/e2e/test/scenarios/visualizations-tabular/visualizations-tabular-reproductions.cy.spec.js
+++ b/e2e/test/scenarios/visualizations-tabular/visualizations-tabular-reproductions.cy.spec.js
@@ -1089,12 +1089,9 @@ describe("issue 50346", () => {
display: "pivot",
visualization_settings: {
"pivot_table.column_split": {
+ // mix field refs with and without `base-type` to make sure we support both cases
rows: [
- [
- "field",
- PRODUCTS.CATEGORY,
- { "base-type": "type/Text", "source-field": ORDERS.PRODUCT_ID },
- ],
+ ["field", PRODUCTS.CATEGORY, { "source-field": ORDERS.PRODUCT_ID }],
[
"field",
PRODUCTS.VENDOR,
diff --git a/frontend/src/metabase-lib/v1/queries/utils/column-key.ts b/frontend/src/metabase-lib/v1/queries/utils/column-key.ts
index 6fbe550f364..1bdc1bc355a 100644
--- a/frontend/src/metabase-lib/v1/queries/utils/column-key.ts
+++ b/frontend/src/metabase-lib/v1/queries/utils/column-key.ts
@@ -1,10 +1,11 @@
import {
createFieldReference,
- getBaseDimensionReference,
+ getDimensionReferenceWithoutBaseType,
+ getDimensionReferenceWithoutTemporalUnitAndBinning,
getNormalizedDimensionReference,
hasStringFieldName,
isAggregationReference,
- isExpressionReference,
+ isDimensionReferenceWithOptions,
isFieldReference,
isValidDimensionReference,
} from "metabase-lib/v1/references";
@@ -20,7 +21,14 @@ export const getColumnKey = (column: DatasetColumnReference) => {
return JSON.stringify(["name", column.name]);
};
-export const getLegacyColumnKey = (column: DatasetColumnReference) => {
+export type LegacyColumnKeyOptions = {
+ excludeBaseType?: boolean;
+};
+
+export const getLegacyColumnKey = (
+ column: DatasetColumnReference,
+ { excludeBaseType }: LegacyColumnKeyOptions = {},
+) => {
let fieldRef = column.field_ref;
if (!fieldRef) {
@@ -34,12 +42,11 @@ export const getLegacyColumnKey = (column: DatasetColumnReference) => {
fieldRef = getNormalizedDimensionReference(fieldRef);
- if (
- isFieldReference(fieldRef) ||
- isExpressionReference(fieldRef) ||
- isAggregationReference(fieldRef)
- ) {
- fieldRef = getBaseDimensionReference(fieldRef);
+ if (isDimensionReferenceWithOptions(fieldRef)) {
+ fieldRef = getDimensionReferenceWithoutTemporalUnitAndBinning(fieldRef);
+ if (excludeBaseType) {
+ fieldRef = getDimensionReferenceWithoutBaseType(fieldRef);
+ }
}
const isLegacyRef =
@@ -67,15 +74,23 @@ export const getColumnSettings = (
return getObjectColumnSettings(settings?.column_settings, column);
};
-// Gets the corresponding viz settings for the column. We check for both
-// legacy and modern keys because not all viz settings have been migrated.
-// To be extra safe and maintain backward compatibility, we check for the
-// legacy key first.
+/**
+ * Gets the corresponding viz settings for the column. We check for both
+ * legacy and modern keys because not all viz settings have been migrated.
+ * To be extra safe and maintain backward compatibility, we check for the
+ * legacy key first.
+ *
+ * Sometimes the `field_ref` has the `base-type`, sometimes it doesn't. To not
+ * break legacy viz settings, try to get the column settings with and without
+ * `base-type`.
+ */
export const getObjectColumnSettings = (
settings: Record<string, ColumnSettings> | null | undefined,
column: DatasetColumnReference,
) => {
return (
- settings?.[getLegacyColumnKey(column)] ?? settings?.[getColumnKey(column)]
+ settings?.[getLegacyColumnKey(column)] ??
+ settings?.[getLegacyColumnKey(column, { excludeBaseType: true })] ??
+ settings?.[getColumnKey(column)]
);
};
diff --git a/frontend/src/metabase-lib/v1/queries/utils/column-key.unit.spec.ts b/frontend/src/metabase-lib/v1/queries/utils/column-key.unit.spec.ts
index ff4594ef782..5e28377a69d 100644
--- a/frontend/src/metabase-lib/v1/queries/utils/column-key.unit.spec.ts
+++ b/frontend/src/metabase-lib/v1/queries/utils/column-key.unit.spec.ts
@@ -1,4 +1,5 @@
import {
+ type LegacyColumnKeyOptions,
getColumnKey,
getColumnNameFromKey,
getColumnSettings,
@@ -14,6 +15,7 @@ import {
type TestCase = {
title: string;
column: DatasetColumn;
+ options?: LegacyColumnKeyOptions;
expectedKey: string;
};
@@ -94,9 +96,49 @@ describe("getLegacyColumnKey", () => {
}),
expectedKey: JSON.stringify(["ref", ["expression", "foo"]]),
},
- ])("should create a ref-based key: $title", ({ column, expectedKey }) => {
- expect(getLegacyColumnKey(column)).toEqual(expectedKey);
- });
+ {
+ title: "should include `base-type` by default",
+ column: createMockColumn({
+ name: "foo",
+ field_ref: ["field", 1, { "base-type": "type/DateTime" }],
+ }),
+ expectedKey: JSON.stringify([
+ "ref",
+ ["field", 1, { "base-type": "type/DateTime" }],
+ ]),
+ },
+ {
+ title: "should allow to exclude `base-type`",
+ column: createMockColumn({
+ name: "foo",
+ field_ref: ["field", 1, { "base-type": "type/DateTime" }],
+ }),
+ options: {
+ excludeBaseType: true,
+ },
+ expectedKey: JSON.stringify(["ref", ["field", 1, null]]),
+ },
+ {
+ title: "should allow to exclude `base-type` when there are other options",
+ column: createMockColumn({
+ name: "foo",
+ field_ref: [
+ "field",
+ 1,
+ { "base-type": "type/DateTime", "source-field": 2 },
+ ],
+ }),
+ options: {
+ excludeBaseType: true,
+ },
+ expectedKey: JSON.stringify(["ref", ["field", 1, { "source-field": 2 }]]),
+ },
+ ])(
+ "should create a ref-based key: $title",
+ ({ column, options, expectedKey }) => {
+ expect(getLegacyColumnKey(column, options)).toEqual(expectedKey);
+ },
+ );
it.each<TestCase>([
{
@@ -197,6 +239,17 @@ describe("getObjectColumnSettings", () => {
});
});
+ it("should be able to find the setting when `base-type` is not present", () => {
+ const settings = {
+ [getLegacyColumnKey(column, { excludeBaseType: true })]: {
+ column_title: "A",
+ },
+ };
+ expect(getObjectColumnSettings(settings, column)).toEqual({
+ column_title: "A",
+ });
+ });
+
it("should use new keys if old keys are not available", () => {
const settings = {
[getColumnKey(column)]: { column_title: "B" },
diff --git a/frontend/src/metabase-lib/v1/references.ts b/frontend/src/metabase-lib/v1/references.ts
index 8e6112a71ee..9a032aab2ca 100644
--- a/frontend/src/metabase-lib/v1/references.ts
+++ b/frontend/src/metabase-lib/v1/references.ts
@@ -49,6 +49,16 @@ export const isValidDimensionReference = (
].some((predicate) => predicate(mbql));
};
+export const isDimensionReferenceWithOptions = (
+ mbql: unknown,
+): mbql is DimensionReferenceWithOptions => {
+ return (
+ isFieldReference(mbql) ||
+ isExpressionReference(mbql) ||
+ isAggregationReference(mbql)
+ );
+};
+
export const normalizeReferenceOptions = (
options?: ReferenceOptions | null,
): ReferenceOptions | null => {
@@ -109,7 +119,7 @@ export const BASE_DIMENSION_REFERENCE_OMIT_OPTIONS = [
"binning",
];
-export const getBaseDimensionReference = (
+export const getDimensionReferenceWithoutTemporalUnitAndBinning = (
mbql: DimensionReferenceWithOptions,
) =>
getDimensionReferenceWithoutOptions(
@@ -117,6 +127,12 @@ export const getBaseDimensionReference = (
BASE_DIMENSION_REFERENCE_OMIT_OPTIONS,
);
+const BASE_TYPE_OPTIONS = ["base-type"];
+
+export const getDimensionReferenceWithoutBaseType = (
+ mbql: DimensionReferenceWithOptions,
+) => getDimensionReferenceWithoutOptions(mbql, BASE_TYPE_OPTIONS);
+
/**
* Whether this Field clause has a string Field name (as opposed to an integer Field ID). This generally means the
* Field comes from a native query.
diff --git a/frontend/src/metabase/visualizations/visualizations/PivotTable/settings.ts b/frontend/src/metabase/visualizations/visualizations/PivotTable/settings.ts
index db9300ce9f3..8961d21c951 100644
--- a/frontend/src/metabase/visualizations/visualizations/PivotTable/settings.ts
+++ b/frontend/src/metabase/visualizations/visualizations/PivotTable/settings.ts
@@ -18,11 +18,16 @@ import { ChartSettingsTableFormatting } from "metabase/visualizations/components
import { columnSettings } from "metabase/visualizations/lib/settings/column";
import type { ComputedVisualizationSettings } from "metabase/visualizations/types";
import { migratePivotColumnSplitSetting } from "metabase-lib/v1/queries/utils/pivot";
+import {
+ getDimensionReferenceWithoutBaseType,
+ isDimensionReferenceWithOptions,
+} from "metabase-lib/v1/references";
import { isDimension } from "metabase-lib/v1/types/utils/isa";
import type {
Card,
DatasetColumn,
DatasetData,
+ DimensionReference,
PivotTableColumnSplitSetting,
RawSeries,
Series,
@@ -303,7 +308,13 @@ export const _columnSettings = {
.slice(0, -1)
.some(
(row) =>
- _.isEqual(row, column.name) || _.isEqual(row, column.field_ref),
+ _.isEqual(row, column.name) ||
+ (Array.isArray(row) &&
+ column.field_ref != null &&
+ _.isEqual(
+ getFieldRefForComparison(row),
+ getFieldRefForComparison(column.field_ref),
+ )),
);
},
getHidden: (
@@ -326,3 +337,14 @@ export const _columnSettings = {
getDefault: displayNameForColumn,
},
};
+
+/*
+ When comparing field refs for pivot viz settings, ignore `base-type`.
+ Sometimes it's present, sometimes it's not. New pivot settings use column
+ names only and do not depend on field refs.
+ */
+function getFieldRefForComparison(fieldRef: DimensionReference) {
+ return isDimensionReferenceWithOptions(fieldRef)
+ ? getDimensionReferenceWithoutBaseType(fieldRef)
+ : fieldRef;
+}