Skip to content

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

Keep query column order for new table columns (#82476) (#82972)

Denis Berezin16ad7b75f29f
diff --git a/frontend/src/metabase/querying/viz-settings/utils/sync-viz-settings.ts b/frontend/src/metabase/querying/viz-settings/utils/sync-viz-settings.ts
index f4e3a863e7a..03a0cde7803 100644
--- a/frontend/src/metabase/querying/viz-settings/utils/sync-viz-settings.ts
+++ b/frontend/src/metabase/querying/viz-settings/utils/sync-viz-settings.ts
@@ -1,4 +1,7 @@
-import { getDeduplicatedTableColumnSettings } from "metabase/viz-core";
+import {
+ getDeduplicatedTableColumnSettings,
+ insertNewColumnSettings,
+} from "metabase/viz-core";
import * as Lib from "metabase-lib";
import {
getColumnKey,
@@ -53,16 +56,28 @@ export function syncVizSettingsWithSeries(
const newColumns = getSeriesColumns(newSingleSeries);
const oldColumns = getSeriesColumns(oldSingleSeries);
- return syncVizSettings(settings, newColumns, oldColumns);
+ return syncVizSettings(settings, newColumns, oldColumns, {
+ placeNewColumnsInQueryOrder: true,
+ });
}
+export type SyncVizSettingsOpts = {
+ placeNewColumnsInQueryOrder?: boolean;
+};
+
export function syncVizSettings(
settings: VisualizationSettings,
newColumns: ColumnInfo[],
oldColumns: ColumnInfo[],
+ options: SyncVizSettingsOpts = {},
): VisualizationSettings {
let nextSettings = settings;
- nextSettings = syncTableColumns(nextSettings, newColumns, oldColumns);
+ nextSettings = syncTableColumns(
+ nextSettings,
+ newColumns,
+ oldColumns,
+ options,
+ );
nextSettings = syncColumnSettings(nextSettings, newColumns, oldColumns);
nextSettings = syncGraphMetrics(nextSettings, newColumns, oldColumns);
nextSettings = syncPivotColumnSplit(nextSettings, newColumns, oldColumns);
@@ -107,6 +122,7 @@ type SyncColumnsOpts<T> = {
setColumnName: (setting: T, newName: string) => T;
createSetting: (column: ColumnInfo) => T;
shouldCreateSetting?: (column: ColumnInfo) => boolean | undefined;
+ placeNewColumnsInQueryOrder?: boolean;
};
function syncColumns<T>({
@@ -117,6 +133,7 @@ function syncColumns<T>({
setColumnName,
createSetting,
shouldCreateSetting = () => false,
+ placeNewColumnsInQueryOrder = false,
}: SyncColumnsOpts<T>): T[] {
const newNameByKey = Object.fromEntries(
newColumns.map((column) => [column.key, column.name]),
@@ -139,16 +156,23 @@ function syncColumns<T>({
return settings;
}, []);
const remappedNames = new Set(remappedSettings.map(getColumnName));
- const addedSettings = newColumns
- .filter(
- (column) =>
- !oldNameByKey[column.key] &&
- !remappedNames.has(column.name) &&
- shouldCreateSetting(column),
- )
- .map(createSetting);
+ const isNewColumn = (column: ColumnInfo) =>
+ !oldNameByKey[column.key] &&
+ !remappedNames.has(column.name) &&
+ shouldCreateSetting(column);
- return [...remappedSettings, ...addedSettings];
+ if (!placeNewColumnsInQueryOrder) {
+ return [
+ ...remappedSettings,
+ ...newColumns.filter(isNewColumn).map(createSetting),
+ ];
+ }
+
+ return insertNewColumnSettings(remappedSettings, newColumns, {
+ getColumnName,
+ isNewColumn,
+ createSetting,
+ });
}
type SyncColumnNamesOpts = {
@@ -179,6 +203,7 @@ function syncTableColumns(
settings: VisualizationSettings,
newColumns: ColumnInfo[],
oldColumns: ColumnInfo[],
+ { placeNewColumnsInQueryOrder }: SyncVizSettingsOpts,
): VisualizationSettings {
const columnSettings = settings["table.columns"];
if (!columnSettings) {
@@ -195,6 +220,7 @@ function syncTableColumns(
setColumnName: (setting, newName) => ({ ...setting, name: newName }),
createSetting: (column) => ({ name: column.name, enabled: true }),
shouldCreateSetting: () => true,
+ placeNewColumnsInQueryOrder,
}),
};
}
diff --git a/frontend/src/metabase/querying/viz-settings/utils/sync-viz-settings.unit.spec.tsx b/frontend/src/metabase/querying/viz-settings/utils/sync-viz-settings.unit.spec.tsx
index f41e402b2a7..457e7b46e9a 100644
--- a/frontend/src/metabase/querying/viz-settings/utils/sync-viz-settings.unit.spec.tsx
+++ b/frontend/src/metabase/querying/viz-settings/utils/sync-viz-settings.unit.spec.tsx
@@ -76,6 +76,126 @@ describe("syncVizSettings", () => {
});
});
+ it("should place a new column after its preceding column instead of appending it (metabase#82476)", () => {
+ const oldColumns: ColumnInfo[] = ["a", "b", "c", "d", "e"].map(
+ (name) => ({ name, key: name }),
+ );
+ const newColumns: ColumnInfo[] = ["a", "b", "z", "c", "d", "e"].map(
+ (name) => ({ name, key: name }),
+ );
+ const oldSettings = createMockVisualizationSettings({
+ "table.columns": [
+ createMockTableColumnOrderSetting({ name: "a", enabled: true }),
+ createMockTableColumnOrderSetting({ name: "b", enabled: true }),
+ createMockTableColumnOrderSetting({ name: "c", enabled: false }),
+ createMockTableColumnOrderSetting({ name: "d", enabled: true }),
+ createMockTableColumnOrderSetting({ name: "e", enabled: true }),
+ ],
+ });
+
+ const newSettings = syncVizSettings(oldSettings, newColumns, oldColumns, {
+ placeNewColumnsInQueryOrder: true,
+ });
+ expect(newSettings).toEqual({
+ "table.columns": [
+ { name: "a", enabled: true },
+ { name: "b", enabled: true },
+ { name: "z", enabled: true },
+ { name: "c", enabled: false },
+ { name: "d", enabled: true },
+ { name: "e", enabled: true },
+ ],
+ });
+ });
+
+ it("should keep the user's column order when placing a new column", () => {
+ const oldColumns: ColumnInfo[] = ["a", "b", "c", "d"].map((name) => ({
+ name,
+ key: name,
+ }));
+ const newColumns: ColumnInfo[] = ["a", "b", "z", "c", "d"].map(
+ (name) => ({ name, key: name }),
+ );
+ const oldSettings = createMockVisualizationSettings({
+ "table.columns": [
+ createMockTableColumnOrderSetting({ name: "d", enabled: true }),
+ createMockTableColumnOrderSetting({ name: "a", enabled: true }),
+ createMockTableColumnOrderSetting({ name: "b", enabled: true }),
+ createMockTableColumnOrderSetting({ name: "c", enabled: true }),
+ ],
+ });
+
+ const newSettings = syncVizSettings(oldSettings, newColumns, oldColumns, {
+ placeNewColumnsInQueryOrder: true,
+ });
+ expect(newSettings).toEqual({
+ "table.columns": [
+ { name: "d", enabled: true },
+ { name: "a", enabled: true },
+ { name: "b", enabled: true },
+ { name: "z", enabled: true },
+ { name: "c", enabled: true },
+ ],
+ });
+ });
+
+ it("should place a new column first when no column precedes it", () => {
+ const oldColumns: ColumnInfo[] = ["a", "b"].map((name) => ({
+ name,
+ key: name,
+ }));
+ const newColumns: ColumnInfo[] = ["z", "a", "b"].map((name) => ({
+ name,
+ key: name,
+ }));
+ const oldSettings = createMockVisualizationSettings({
+ "table.columns": [
+ createMockTableColumnOrderSetting({ name: "b", enabled: true }),
+ createMockTableColumnOrderSetting({ name: "a", enabled: true }),
+ ],
+ });
+
+ const newSettings = syncVizSettings(oldSettings, newColumns, oldColumns, {
+ placeNewColumnsInQueryOrder: true,
+ });
+ expect(newSettings).toEqual({
+ "table.columns": [
+ { name: "z", enabled: true },
+ { name: "b", enabled: true },
+ { name: "a", enabled: true },
+ ],
+ });
+ });
+
+ it("should keep the query order of adjacent new columns", () => {
+ const oldColumns: ColumnInfo[] = ["a", "b"].map((name) => ({
+ name,
+ key: name,
+ }));
+ const newColumns: ColumnInfo[] = ["a", "y", "z", "b"].map((name) => ({
+ name,
+ key: name,
+ }));
+ const oldSettings = createMockVisualizationSettings({
+ "table.columns": [
+ createMockTableColumnOrderSetting({ name: "a", enabled: true }),
+ createMockTableColumnOrderSetting({ name: "b", enabled: false }),
+ ],
+ });
+
+ const newSettings = syncVizSettings(oldSettings, newColumns, oldColumns, {
+ placeNewColumnsInQueryOrder: true,
+ });
+ expect(newSettings).toEqual({
+ "table.columns": [
+ { name: "a", enabled: true },
+ { name: "y", enabled: true },
+ { name: "z", enabled: true },
+ { name: "b", enabled: false },
+ ],
+ });
+ });
+
it("should handle removing columns with column.name changes", () => {
const oldColumns: ColumnInfo[] = [
{ name: "ID", key: "ID" },
@@ -601,6 +721,35 @@ describe("syncVizSettingsWithSeries", () => {
});
});
+ it("should place a new column at its query position (metabase#82476)", () => {
+ const seriesWithNewMiddleColumn: Series = [
+ {
+ card: createMockCard(),
+ data: createMockDatasetData({
+ cols: [
+ createMockColumn({ name: "ID", source: "native" }),
+ createMockColumn({ name: "NEW", source: "native" }),
+ createMockColumn({ name: "ID_2", source: "native" }),
+ ],
+ }),
+ },
+ ];
+
+ const newSettings = syncVizSettingsWithSeries(
+ oldSettings,
+ query,
+ seriesWithNewMiddleColumn,
+ oldSeries,
+ );
+ expect(newSettings).toEqual({
+ "table.columns": [
+ { name: "ID", enabled: true },
+ { name: "NEW", enabled: true },
+ { name: "ID_2", enabled: false },
+ ],
+ });
+ });
+
it("should ignore updates if there are errors in new query results", () => {
const newSettings = syncVizSettingsWithSeries(
oldSettings,
diff --git a/frontend/src/metabase/viz-core/index.ts b/frontend/src/metabase/viz-core/index.ts
index cf6c6a8dd61..452a5afe990 100644
--- a/frontend/src/metabase/viz-core/index.ts
+++ b/frontend/src/metabase/viz-core/index.ts
@@ -354,6 +354,7 @@ export {
fieldSetting,
getDeduplicatedTableColumnSettings,
getOptionFromColumn,
+ insertNewColumnSettings,
metricSetting,
} from "./lib/settings/utils";
export {
diff --git a/frontend/src/metabase/viz-core/lib/settings/column.ts b/frontend/src/metabase/viz-core/lib/settings/column.ts
index cf97ce39eed..deea1988248 100644
--- a/frontend/src/metabase/viz-core/lib/settings/column.ts
+++ b/frontend/src/metabase/viz-core/lib/settings/column.ts
@@ -57,7 +57,10 @@ import type {
import { getVisualization, getVisualizationRaw } from "../registry";
import { nestedSettings } from "./nested";
-import { getDeduplicatedTableColumnSettings } from "./utils";
+import {
+ getDeduplicatedTableColumnSettings,
+ insertNewColumnSettings,
+} from "./utils";
type GetColumnsFn = (
series: Series,
@@ -574,19 +577,18 @@ export function tableColumnSettings({
uniqColumnSettings,
);
- return [
+ return insertNewColumnSettings(
// retain settings with matching columns only
- ...uniqColumnSettings.filter(
+ uniqColumnSettings.filter(
(_, settingIndex) => columnIndexes[settingIndex] >= 0,
),
- // add columns that do not have matching settings to the end
- ...cols
- .filter((_, columnIndex) => settingIndexes[columnIndex] < 0)
- .map((column) => ({
- name: column.name,
- enabled: true,
- })),
- ];
+ cols,
+ {
+ getColumnName: (setting) => setting.name,
+ isNewColumn: (_, columnIndex) => settingIndexes[columnIndex] < 0,
+ createSetting: (column) => ({ name: column.name, enabled: true }),
+ },
+ );
},
getProps: (series, settings) => {
const [
diff --git a/frontend/src/metabase/viz-core/lib/settings/column.unit.spec.ts b/frontend/src/metabase/viz-core/lib/settings/column.unit.spec.ts
index a2a298ef22e..f682911de37 100644
--- a/frontend/src/metabase/viz-core/lib/settings/column.unit.spec.ts
+++ b/frontend/src/metabase/viz-core/lib/settings/column.unit.spec.ts
@@ -181,5 +181,137 @@ describe("column settings", () => {
{ name: "QUANTITY_RENAMED", enabled: true },
]);
});
+
+ it("should place new result columns in their query position instead of appending them (metabase#82476)", () => {
+ const series: Series = [
+ createMockSingleSeries(
+ {},
+ {
+ data: {
+ cols: [
+ createMockColumn({ name: "a" }),
+ createMockColumn({ name: "b" }),
+ createMockColumn({ name: "z" }),
+ createMockColumn({ name: "c" }),
+ createMockColumn({ name: "d" }),
+ createMockColumn({ name: "e" }),
+ ],
+ },
+ },
+ ),
+ ];
+
+ const computed = getComputedSettings(tableColumnSettings(), series, {
+ "table.columns": [
+ { name: "a", enabled: true },
+ { name: "b", enabled: true },
+ { name: "c", enabled: false },
+ { name: "d", enabled: true },
+ { name: "e", enabled: true },
+ ],
+ });
+
+ expect(computed["table.columns"]).toEqual([
+ { name: "a", enabled: true },
+ { name: "b", enabled: true },
+ { name: "z", enabled: true },
+ { name: "c", enabled: false },
+ { name: "d", enabled: true },
+ { name: "e", enabled: true },
+ ]);
+ });
+
+ it("should keep the user's column order and insert a new column after its preceding result column", () => {
+ const series: Series = [
+ createMockSingleSeries(
+ {},
+ {
+ data: {
+ cols: [
+ createMockColumn({ name: "a" }),
+ createMockColumn({ name: "b" }),
+ createMockColumn({ name: "z" }),
+ createMockColumn({ name: "c" }),
+ createMockColumn({ name: "d" }),
+ ],
+ },
+ },
+ ),
+ ];
+
+ const computed = getComputedSettings(tableColumnSettings(), series, {
+ "table.columns": [
+ { name: "d", enabled: true },
+ { name: "a", enabled: true },
+ { name: "b", enabled: true },
+ { name: "c", enabled: true },
+ ],
+ });
+
+ expect(computed["table.columns"]).toEqual([
+ { name: "d", enabled: true },
+ { name: "a", enabled: true },
+ { name: "b", enabled: true },
+ { name: "z", enabled: true },
+ { name: "c", enabled: true },
+ ]);
+ });
+
+ it("should place a new result column first when no column precedes it and keep adjacent new columns in query order", () => {
+ const series: Series = [
+ createMockSingleSeries(
+ {},
+ {
+ data: {
+ cols: [
+ createMockColumn({ name: "z" }),
+ createMockColumn({ name: "a" }),
+ createMockColumn({ name: "x" }),
+ createMockColumn({ name: "y" }),
+ createMockColumn({ name: "b" }),
+ ],
+ },
+ },
+ ),
+ ];
+
+ const computed = getComputedSettings(tableColumnSettings(), series, {
+ "table.columns": [
+ { name: "b", enabled: true },
+ { name: "a", enabled: false },
+ ],
+ });
+
+ expect(computed["table.columns"]).toEqual([
+ { name: "z", enabled: true },
+ { name: "b", enabled: true },
+ { name: "a", enabled: false },
+ { name: "x", enabled: true },
+ { name: "y", enabled: true },
+ ]);
+ });
+
+ it("should use the result column order when there are no saved column settings", () => {
+ const series: Series = [
+ createMockSingleSeries(
+ {},
+ {
+ data: {
+ cols: [
+ createMockColumn({ name: "b" }),
+ createMockColumn({ name: "a" }),
+ ],
+ },
+ },
+ ),
+ ];
+
+ const computed = getComputedSettings(tableColumnSettings(), series, {});
+
+ expect(computed["table.columns"]).toEqual([
+ { name: "b", enabled: true },
+ { name: "a", enabled: true },
+ ]);
+ });
});
});
diff --git a/frontend/src/metabase/viz-core/lib/settings/utils.ts b/frontend/src/metabase/viz-core/lib/settings/utils.ts
index 250c222c475..b6ac692b815 100644
--- a/frontend/src/metabase/viz-core/lib/settings/utils.ts
+++ b/frontend/src/metabase/viz-core/lib/settings/utils.ts
@@ -112,3 +112,62 @@ export function getDeduplicatedTableColumnSettings(
): TableColumnOrderSetting[] {
return _.uniq(tableColumnsSettings, false, (item) => item.name);
}
+
+type InsertNewColumnSettingsOpts<TSetting, TColumn extends { name: string }> = {
+ getColumnName: (setting: TSetting) => string | undefined;
+ isNewColumn: (column: TColumn, columnIndex: number) => boolean | undefined;
+ createSetting: (column: TColumn) => TSetting;
+};
+
+/**
+ * Keeps the user's order of existing settings and places each new column right
+ * after the nearest preceding column that has a setting, so new columns follow
+ * the query's column order.
+ */
+export function insertNewColumnSettings<
+ TSetting,
+ TColumn extends { name: string },
+>(
+ settings: TSetting[],
+ columns: TColumn[],
+ {
+ getColumnName,
+ isNewColumn,
+ createSetting,
+ }: InsertNewColumnSettingsOpts<TSetting, TColumn>,
+): TSetting[] {
+ if (settings.length === 0) {
+ return columns.filter(isNewColumn).map(createSetting);
+ }
+
+ const nextSettings = [...settings];
+ const settingNames = new Set(settings.map(getColumnName));
+ columns.forEach((column, columnIndex) => {
+ if (!isNewColumn(column, columnIndex)) {
+ return;
+ }
+ const anchorName = findAnchorName(columns, columnIndex, settingNames);
+ const insertIndex =
+ anchorName == null
+ ? 0
+ : nextSettings.findIndex(
+ (setting) => getColumnName(setting) === anchorName,
+ ) + 1;
+ nextSettings.splice(insertIndex, 0, createSetting(column));
+ settingNames.add(column.name);
+ });
+ return nextSettings;
+}
+
+function findAnchorName(
+ columns: { name: string }[],
+ columnIndex: number,
+ settingNames: Set<string | undefined>,
+) {
+ for (let index = columnIndex - 1; index >= 0; index--) {
+ if (settingNames.has(columns[index].name)) {
+ return columns[index].name;
+ }
+ }
+ return null;
+}