Skip to content

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

Fix error for pivot tables with no dimensions (#74521)

Nick Fitzpatrick7520bff5a048
diff --git a/frontend/src/metabase/visualizations/visualizations/PivotTable/settings.ts b/frontend/src/metabase/visualizations/visualizations/PivotTable/settings.ts
index ddcd46f8d05..199445343b8 100644
--- a/frontend/src/metabase/visualizations/visualizations/PivotTable/settings.ts
+++ b/frontend/src/metabase/visualizations/visualizations/PivotTable/settings.ts
@@ -125,7 +125,8 @@ export const settings = {
if (dimensions.length < 2) {
columns = [];
- rows = [first];
+ // use `dimensions` so `rows` is `[]` when there are zero dimensions, not `[undefined]` (metabase#56235)
+ rows = dimensions;
} else if (dimensions.length <= 3) {
columns = [first];
rows = [second, ...rest];
diff --git a/frontend/src/metabase/visualizations/visualizations/PivotTable/settings.unit.spec.ts b/frontend/src/metabase/visualizations/visualizations/PivotTable/settings.unit.spec.ts
new file mode 100644
index 00000000000..4a4eaa38da5
--- /dev/null
+++ b/frontend/src/metabase/visualizations/visualizations/PivotTable/settings.unit.spec.ts
@@ -0,0 +1,36 @@
+import {
+ createMockCard,
+ createMockColumn,
+ createMockDatasetData,
+} from "metabase-types/api/mocks";
+
+import { settings } from "./settings";
+
+const COLUMN_SPLIT_SETTING = "pivot_table.column_split";
+
+describe("PivotTable settings", () => {
+ describe(`${COLUMN_SPLIT_SETTING} getValue`, () => {
+ it("should not throw when the query has no dimension columns (metabase#56235)", () => {
+ // a Count-only query has zero dimensions, which previously left an
+ // `undefined` slot in `rows` and threw when we mapped to `col.name`
+ const data = createMockDatasetData({
+ rows: [[0]],
+ cols: [
+ createMockColumn({
+ name: "count",
+ display_name: "Count",
+ source: "aggregation",
+ base_type: "type/Integer",
+ effective_type: "type/Integer",
+ }),
+ ],
+ });
+
+ const card = createMockCard({ display: "pivot" });
+
+ expect(() => {
+ settings[COLUMN_SPLIT_SETTING].getValue([{ data, card }], {});
+ }).not.toThrow();
+ });
+ });
+});