Skip to content

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

Gdgt 1312 adjust range selection to work also with schemas and dbs (#68280)

Jakub Chodorowicz359158e594b8
diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/data-model/components/TablePicker/bulk-selection.utils.ts b/enterprise/frontend/src/metabase-enterprise/data-studio/data-model/components/TablePicker/bulk-selection.utils.ts
index a7c0d5d6016..0fcf3e6c96d 100644
--- a/enterprise/frontend/src/metabase-enterprise/data-studio/data-model/components/TablePicker/bulk-selection.utils.ts
+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/data-model/components/TablePicker/bulk-selection.utils.ts
@@ -1,7 +1,7 @@
import type { SelectionState } from "metabase/ui";
import type { DatabaseId, TableId } from "metabase-types/api";
-import type { DatabaseNode, SchemaNode, TreeNode } from "./types";
+import type { DatabaseNode, SchemaNode, TableNode, TreeNode } from "./types";
import { isSchemaNode, isTableNode } from "./types";
export interface NodeSelection {
@@ -168,3 +168,54 @@ export function toggleSchemaSelection(
databases: selection.databases,
};
}
+
+export function toggleInSet<T>(set: Set<T>, item: T): Set<T> {
+ const newSet = new Set(set);
+ newSet.has(item) ? newSet.delete(item) : newSet.add(item);
+ return newSet;
+}
+
+export const cloneSelection = (selection: NodeSelection): NodeSelection => ({
+ tables: new Set(selection.tables),
+ databases: new Set(selection.databases),
+ schemas: new Set(selection.schemas),
+});
+
+export function addTableToSelection(
+ table: TableNode,
+ selection: NodeSelection,
+): NodeSelection {
+ const nextSelection = cloneSelection(selection);
+ nextSelection.tables.add(table.value.tableId);
+ return nextSelection;
+}
+
+export function addSchemaToSelection(
+ schema: SchemaNode,
+ selection: NodeSelection,
+): NodeSelection {
+ const nextSelection = cloneSelection(selection);
+ if (schema.children.length > 0) {
+ return schema.children.reduce(
+ (memo, tableNode) => addTableToSelection(tableNode, memo),
+ nextSelection,
+ );
+ }
+ nextSelection.schemas.add(getSchemaId(schema));
+ return nextSelection;
+}
+
+export function addDatabaseToSelection(
+ database: DatabaseNode,
+ selection: NodeSelection,
+): NodeSelection {
+ const nextSelection = cloneSelection(selection);
+ if (database.children.length > 0) {
+ return database.children.reduce(
+ (memo, schema) => addSchemaToSelection(schema, memo),
+ nextSelection,
+ );
+ }
+ nextSelection.databases.add(database.value.databaseId);
+ return nextSelection;
+}
diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/data-model/components/TablePicker/components/TablePickerTreeTable.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/data-model/components/TablePicker/components/TablePickerTreeTable.tsx
index 974ba33005c..10eb951ec2e 100644
--- a/enterprise/frontend/src/metabase-enterprise/data-studio/data-model/components/TablePicker/components/TablePickerTreeTable.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/data-model/components/TablePicker/components/TablePickerTreeTable.tsx
@@ -6,6 +6,7 @@ import {
useMemo,
useRef,
} from "react";
+import { match } from "ts-pattern";
import { t } from "ttag";
import { useListUsersQuery } from "metabase/api";
@@ -18,27 +19,34 @@ import {
TreeTable,
useTreeTableInstance,
} from "metabase/ui";
-import type { TableId, UserId } from "metabase-types/api";
+import type { UserId } from "metabase-types/api";
import { useSelection } from "../../../pages/DataModel/contexts/SelectionContext";
import {
type NodeSelection,
+ addDatabaseToSelection,
+ addSchemaToSelection,
+ addTableToSelection,
+ cloneSelection,
getSchemaId,
isItemSelected,
toggleDatabaseSelection,
+ toggleInSet,
toggleSchemaSelection,
} from "../bulk-selection.utils";
import { TYPE_ICONS } from "../constants";
-import type {
- ChangeOptions,
- RootNode,
- TablePickerTreeNode,
- TreePath,
+import {
+ type ChangeOptions,
+ type RootNode,
+ type TablePickerTreeNode,
+ type TreeNode,
+ type TreePath,
+ isDatabaseNode,
+ isSchemaNode,
+ isTableNode,
} from "../types";
import {
- getDatabases,
- getSchemas,
- getTables,
+ getTreeMap,
nodeToTreePath,
transformToTreeTableFormat,
} from "../utils";
@@ -73,7 +81,7 @@ export function TablePickerTreeTable({
} = useSelection();
const formatNumber = useNumberFormatter(NUMBER_FORMAT_OPTIONS);
- const lastSelectedTableIndex = useRef<number | null>(null);
+ const lastSelectedRowIndex = useRef<number | null>(null);
const instanceRef = useRef<ReturnType<
typeof useTreeTableInstance<TablePickerTreeNode>
> | null>(null);
@@ -102,13 +110,7 @@ export function TablePickerTreeTable({
return map;
}, [treeData]);
- const nodeKeyToOriginal = useMemo(() => {
- return new Map(
- [...getDatabases(tree), ...getSchemas(tree), ...getTables(tree)].map(
- (n) => [n.key, n],
- ),
- );
- }, [tree]);
+ const nodeKeyToOriginal = useMemo(() => getTreeMap(tree), [tree]);
const expandedIds = useMemo(() => {
const ids = new Set<string>();
@@ -165,95 +167,23 @@ export function TablePickerTreeTable({
databases: selectedDatabases,
};
- const isTable = row.original.type === "table";
- const lastIndex = lastSelectedTableIndex.current;
-
- if (isShiftPressed && isTable && lastIndex != null) {
- const rows = instanceRef.current?.rows ?? [];
- const start = Math.min(lastIndex, index);
- const end = Math.max(lastIndex, index);
- const rangeRows = rows
- .slice(start, end + 1)
- .filter((r) => !r.original.isDisabled && r.original.type === "table");
-
- const tableIds = rangeRows
- .map((r) => r.original.tableId)
- .filter((id): id is TableId => id != null);
-
- if (tableIds.length > 0) {
- setSelectedTables((prev) => {
- const newSet = new Set(prev);
- tableIds.forEach((id) => newSet.add(id));
- return newSet;
- });
- lastSelectedTableIndex.current = index;
-
- if (row.original.tableId != null) {
- onChange?.(nodeToTreePath(row.original));
- }
- return;
- }
- }
-
- if (row.original.type === "table") {
- const tableId = row.original.tableId;
- if (tableId == null) {
- return;
- }
-
- setSelectedTables((prev) => {
- const newSet = new Set(prev);
- newSet.has(tableId) ? newSet.delete(tableId) : newSet.add(tableId);
- return newSet;
- });
- lastSelectedTableIndex.current = index;
-
+ const result = changeCheckboxSelection({
+ row,
+ index,
+ isShiftPressed,
+ selection,
+ lastSelectedRowIndex: lastSelectedRowIndex.current,
+ rows: instanceRef.current?.rows ?? [],
+ nodeKeyToOriginal,
+ });
+
+ setSelectedTables(result.selection.tables);
+ setSelectedSchemas(result.selection.schemas);
+ setSelectedDatabases(result.selection.databases);
+ lastSelectedRowIndex.current = result.lastSelectedRowIndex;
+
+ if (row.original.type === "table" && row.original.tableId != null) {
onChange?.(nodeToTreePath(row.original));
- } else if (row.original.type === "database") {
- const originalNode = nodeKeyToOriginal.get(row.original.nodeKey);
- if (!originalNode || originalNode.type !== "database") {
- return;
- }
-
- if (originalNode.children.length > 0) {
- const { schemas, tables, databases } = toggleDatabaseSelection(
- originalNode,
- selection,
- );
- setSelectedSchemas(schemas);
- setSelectedTables(tables);
- setSelectedDatabases(databases);
- } else {
- const databaseId = row.original.databaseId;
- if (databaseId) {
- setSelectedDatabases((prev) => {
- const newSet = new Set(prev);
- newSet.has(databaseId)
- ? newSet.delete(databaseId)
- : newSet.add(databaseId);
- return newSet;
- });
- }
- }
- } else if (row.original.type === "schema") {
- const originalNode = nodeKeyToOriginal.get(row.original.nodeKey);
- if (!originalNode || originalNode.type !== "schema") {
- return;
- }
-
- if (originalNode.children.length > 0) {
- const { tables } = toggleSchemaSelection(originalNode, selection);
- setSelectedTables(tables);
- } else {
- const schemaId = getSchemaId(originalNode);
- setSelectedSchemas((prev) => {
- const newSet = new Set(prev);
- newSet.has(schemaId)
- ? newSet.delete(schemaId)
- : newSet.add(schemaId);
- return newSet;
- });
- }
}
},
[
@@ -410,7 +340,7 @@ export function TablePickerTreeTable({
useEffect(() => {
if (selectedItemsCount === 0) {
- lastSelectedTableIndex.current = null;
+ lastSelectedRowIndex.current = null;
}
}, [selectedItemsCount]);
@@ -494,6 +424,178 @@ export function TablePickerTreeTable({
);
}
+export interface CheckboxToggleResult {
+ selection: NodeSelection;
+ lastSelectedRowIndex: number;
+}
+
+export function changeCheckboxSelection(params: {
+ row: Row<TablePickerTreeNode>;
+ index: number;
+ isShiftPressed: boolean;
+ selection: NodeSelection;
+ lastSelectedRowIndex: number | null;
+ rows: Row<TablePickerTreeNode>[];
+ nodeKeyToOriginal: Map<string, TreeNode>;
+}): CheckboxToggleResult {
+ const {
+ row,
+ index,
+ isShiftPressed,
+ selection,
+ lastSelectedRowIndex,
+ rows,
+ nodeKeyToOriginal,
+ } = params;
+
+ if (isShiftPressed && lastSelectedRowIndex != null) {
+ return handleRangeCheckboxSelection({
+ startIndex: Math.min(lastSelectedRowIndex, index),
+ endIndex: Math.max(lastSelectedRowIndex, index),
+ currentIndex: index,
+ rows,
+ nodeKeyToOriginal,
+ baseSelection: selection,
+ });
+ }
+
+ return handleSingleCheckboxSelection({
+ row,
+ index,
+ selection,
+ nodeKeyToOriginal,
+ });
+}
+
+function handleRangeCheckboxSelection(params: {
+ startIndex: number;
+ endIndex: number;
+ currentIndex: number;
+ rows: Row<TablePickerTreeNode>[];
+ nodeKeyToOriginal: Map<string, TreeNode>;
+ baseSelection: NodeSelection;
+}): CheckboxToggleResult {
+ const {
+ startIndex,
+ endIndex,
+ currentIndex,
+ rows,
+ nodeKeyToOriginal,
+ baseSelection,
+ } = params;
+
+ const rangeRows = rows
+ .slice(startIndex, endIndex + 1)
+ .filter((r) => !r.original.isDisabled);
+
+ // Skip a parent if its next item is its child (deeper in the tree).
+ // This allows partial selection within expanded parents.
+ const indexesToSkip = rangeRows.reduce<number[]>((memo, row, i, arr) => {
+ const nextRow = arr[i + 1];
+ if (nextRow?.depth > row.depth) {
+ memo.push(i);
+ }
+ return memo;
+ }, []);
+
+ const nextSelection: NodeSelection = rangeRows.reduce(
+ (memo, row, rowIndex) => {
+ const originalNode = nodeKeyToOriginal.get(row.original.nodeKey);
+ if (indexesToSkip.includes(rowIndex) || !originalNode) {
+ return memo;
+ }
+ return match(originalNode)
+ .when(isTableNode, (treeNode) => addTableToSelection(treeNode, memo))
+ .when(isSchemaNode, (schemaNode) =>
+ addSchemaToSelection(schemaNode, memo),
+ )
+ .when(isDatabaseNode, (databaseNode) =>
+ addDatabaseToSelection(databaseNode, memo),
+ )
+ .otherwise(() => memo);
+ },
+ cloneSelection(baseSelection),
+ );
+
+ return {
+ selection: nextSelection,
+ lastSelectedRowIndex: currentIndex,
+ };
+}
+
+function handleSingleCheckboxSelection(params: {
+ row: Row<TablePickerTreeNode>;
+ index: number;
+ selection: NodeSelection;
+ nodeKeyToOriginal: Map<string, TreeNode>;
+}): CheckboxToggleResult {
+ const { row, index, selection, nodeKeyToOriginal } = params;
+
+ if (row.original.type === "table") {
+ const tableId = row.original.tableId;
+ if (tableId == null) {
+ return { selection, lastSelectedRowIndex: index };
+ }
+ return {
+ selection: {
+ ...selection,
+ tables: toggleInSet(selection.tables, tableId),
+ },
+ lastSelectedRowIndex: index,
+ };
+ }
+
+ if (row.original.type === "database") {
+ const originalNode = nodeKeyToOriginal.get(row.original.nodeKey);
+ if (!originalNode || originalNode.type !== "database") {
+ return { selection, lastSelectedRowIndex: index };
+ }
+
+ if (originalNode.children.length > 0) {
+ return {
+ selection: toggleDatabaseSelection(originalNode, selection),
+ lastSelectedRowIndex: index,
+ };
+ }
+
+ const databaseId = row.original.databaseId;
+ if (databaseId) {
+ return {
+ selection: {
+ ...selection,
+ databases: toggleInSet(selection.databases, databaseId),
+ },
+ lastSelectedRowIndex: index,
+ };
+ }
+ }
+
+ if (row.original.type === "schema") {
+ const originalNode = nodeKeyToOriginal.get(row.original.nodeKey);
+ if (!originalNode || originalNode.type !== "schema") {
+ return { selection, lastSelectedRowIndex: index };
+ }
+
+ if (originalNode.children.length > 0) {
+ return {
+ selection: toggleSchemaSelection(originalNode, selection),
+ lastSelectedRowIndex: index,
+ };
+ }
+
+ const schemaId = getSchemaId(originalNode);
+ return {
+ selection: {
+ ...selection,
+ schemas: toggleInSet(selection.schemas, schemaId),
+ },
+ lastSelectedRowIndex: index,
+ };
+ }
+
+ return { selection, lastSelectedRowIndex: index };
+}
+
function isChildrenLoading(row: Row<TablePickerTreeNode>): boolean {
return (
row.getIsExpanded() &&
diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/data-model/components/TablePicker/components/TablePickerTreeTable.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/data-model/components/TablePicker/components/TablePickerTreeTable.unit.spec.tsx
new file mode 100644
index 00000000000..38fef362602
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/data-model/components/TablePicker/components/TablePickerTreeTable.unit.spec.tsx
@@ -0,0 +1,776 @@
+import type { Row } from "@tanstack/react-table";
+
+import type { DatabaseId, TableId } from "metabase-types/api";
+
+import type { NodeSelection } from "../bulk-selection.utils";
+import type {
+ DatabaseNode,
+ SchemaNode,
+ TableNode,
+ TablePickerTreeNode,
+ TreeNode,
+} from "../types";
+
+import { changeCheckboxSelection } from "./TablePickerTreeTable";
+
+describe("handleCheckboxToggle", () => {
+ interface CheckboxRowParams {
+ type: "database" | "schema" | "table";
+ nodeKey: string;
+ tableId?: TableId;
+ databaseId?: DatabaseId;
+ isDisabled?: boolean;
+ depth?: number;
+ }
+
+ const createCheckboxRow = ({
+ type,
+ nodeKey,
+ tableId,
+ databaseId,
+ isDisabled,
+ depth = 0,
+ }: CheckboxRowParams): Row<TablePickerTreeNode> =>
+ ({
+ depth,
+ original: {
+ type,
+ nodeKey,
+ tableId,
+ databaseId,
+ isDisabled,
+ },
+ }) as Row<TablePickerTreeNode>;
+
+ const createMockTableNode = (
+ databaseId: DatabaseId,
+ schemaName: string,
+ tableId: TableId,
+ ): TableNode => ({
+ type: "table",
+ label: `Table ${tableId}`,
+ key: `db-${databaseId}-schema-${schemaName}-table-${tableId}`,
+ value: { databaseId, schemaName, tableId },
+ children: [],
+ });
+
+ const createMockSchemaNode = (
+ databaseId: DatabaseId,
+ schemaName: string,
+ tables: TableNode[] = [],
+ ): SchemaNode => ({
+ type: "schema",
+ label: schemaName,
+ key: `db-${databaseId}-schema-${schemaName}`,
+ value: { databaseId, schemaName },
+ children: tables,
+ });
+
+ const createMockDatabaseNode = (
+ databaseId: DatabaseId,
+ schemas: SchemaNode[] = [],
+ ): DatabaseNode => ({
+ type: "database",
+ label: `Database ${databaseId}`,
+ key: `db-${databaseId}`,
+ value: { databaseId },
+ children: schemas,
+ });
+
+ describe("single selection", () => {
+ describe("table selection", () => {
+ it("should select a table when it is not selected", () => {
+ const table = createMockTableNode(1, "public", 101);
+ const row = createCheckboxRow({
+ type: "table",
+ nodeKey: table.key,
+ tableId: 101,
+ databaseId: 1,
+ });
+ const selection: NodeSelection = {
+ tables: new Set(),
+ schemas: new Set(),
+ databases: new Set(),
+ };
+ const nodeKeyToOriginal = new Map([[table.key, table]]);
+
+ const result = changeCheckboxSelection({
+ row,
+ index: 0,
+ isShiftPressed: false,
+ selection,
+ lastSelectedRowIndex: null,
+ rows: [row],
+ nodeKeyToOriginal,
+ });
+
+ expect(result.selection.tables).toEqual(new Set([101]));
+ expect(result.selection.schemas).toEqual(new Set());
+ expect(result.selection.databases).toEqual(new Set());
+ expect(result.lastSelectedRowIndex).toBe(0);
+ });
+
+ it("should deselect a table when it is selected", () => {
+ const table = createMockTableNode(1, "public", 101);
+ const row = createCheckboxRow({
+ type: "table",
+ nodeKey: table.key,
+ tableId: table.value.tableId,
+ databaseId: 1,
+ });
+ const selection: NodeSelection = {
+ tables: new Set([table.value.tableId]),
+ schemas: new Set(),
+ databases: new Set(),
+ };
+ const nodeKeyToOriginal = new Map([[table.key, table]]);
+
+ const result = changeCheckboxSelection({
+ row,
+ index: 0,
+ isShiftPressed: false,
+ selection,
+ lastSelectedRowIndex: null,
+ rows: [row],
+ nodeKeyToOriginal,
+ });
+
+ expect(result.selection.tables).toEqual(new Set());
+ expect(result.lastSelectedRowIndex).toBe(0);
+ });
+ });
+
+ describe("schema selection", () => {
+ it("should select all tables in a schema with children", () => {
+ const table1 = createMockTableNode(1, "public", 101);
+ const table2 = createMockTableNode(1, "public", 102);
+ const schema = createMockSchemaNode(1, "public", [table1, table2]);
+ const row = createCheckboxRow({
+ type: "schema",
+ nodeKey: schema.key,
+ databaseId: 1,
+ });
+ const selection: NodeSelection = {
+ tables: new Set(),
+ schemas: new Set(),
+ databases: new Set(),
+ };
+ const nodeKeyToOriginal = new Map([[schema.key, schema]]);
+
+ const result = changeCheckboxSelection({
+ row,
+ index: 0,
+ isShiftPressed: false,
+ selection,
+ lastSelectedRowIndex: null,
+ rows: [row],
+ nodeKeyToOriginal,
+ });
+
+ expect(result.selection.tables).toEqual(new Set([101, 102]));
+ expect(result.selection.schemas).toEqual(new Set());
+ expect(result.lastSelectedRowIndex).toBe(0);
+ });
+
+ it("should select empty schema without children", () => {
+ const schema = createMockSchemaNode(1, "public", []);
+ const row = createCheckboxRow({
+ type: "schema",
+ nodeKey: schema.key,
+ databaseId: 1,
+ });
+ const selection: NodeSelection = {
+ tables: new Set(),
+ schemas: new Set(),
+ databases: new Set(),
+ };
+ const nodeKeyToOriginal = new Map([[schema.key, schema]]);
+
+ const result = changeCheckboxSelection({
+ row,
+ index: 0,
+ isShiftPressed: false,
+ selection,
+ lastSelectedRowIndex: null,
+ rows: [row],
+ nodeKeyToOriginal,
+ });
+
+ expect(result.selection.tables).toEqual(new Set());
+ expect(result.selection.schemas).toEqual(new Set(["1:public"]));
+ expect(result.lastSelectedRowIndex).toBe(0);
+ });
+
+ it("should deselect all tables in a schema when all are selected", () => {
+ const table1 = createMockTableNode(1, "public", 101);
+ const table2 = createMockTableNode(1, "public", 102);
+ const schema = createMockSchemaNode(1, "public", [table1, table2]);
+ const row = createCheckboxRow({
+ type: "schema",
+ nodeKey: schema.key,
+ databaseId: 1,
+ });
+ const selection: NodeSelection = {
+ tables: new Set([table1.value.tableId, table2.value.tableId]),
+ schemas: new Set(),
+ databases: new Set(),
+ };
+ const nodeKeyToOriginal = new Map([[schema.key, schema]]);
+
+ const result = changeCheckboxSelection({
+ row,
+ index: 0,
+ isShiftPressed: false,
+ selection,
+ lastSelectedRowIndex: null,
+ rows: [row],
+ nodeKeyToOriginal,
+ });
+
+ expect(result.selection.tables).toEqual(new Set());
+ expect(result.selection.schemas).toEqual(new Set());
+ expect(result.lastSelectedRowIndex).toBe(0);
+ });
+ });
+
+ describe("database selection", () => {
+ it("should select all schemas and tables in a database with children", () => {
+ const table1 = createMockTableNode(1, "public", 101);
+ const table2 = createMockTableNode(1, "private", 102);
+ const schema1 = createMockSchemaNode(1, "public", [table1]);
+ const schema2 = createMockSchemaNode(1, "private", [table2]);
+ const database = createMockDatabaseNode(1, [schema1, schema2]);
+ const row = createCheckboxRow({
+ type: "database",
+ nodeKey: database.key,
+ databaseId: 1,
+ });
+ const selection: NodeSelection = {
+ tables: new Set(),
+ schemas: new Set(),
+ databases: new Set(),
+ };
+ const nodeKeyToOriginal = new Map([[database.key, database]]);
+
+ const result = changeCheckboxSelection({
+ row,
+ index: 0,
+ isShiftPressed: false,
+ selection,
+ lastSelectedRowIndex: null,
+ rows: [row],
+ nodeKeyToOriginal,
+ });
+
+ expect(result.selection.tables).toEqual(new Set([101, 102]));
+ expect(result.selection.schemas).toEqual(new Set());
+ expect(result.lastSelectedRowIndex).toBe(0);
+ });
+
+ it("should select empty database without children", () => {
+ const database = createMockDatabaseNode(1, []);
+ const row = createCheckboxRow({
+ type: "database",
+ nodeKey: database.key,
+ databaseId: 1,
+ });
+ const selection: NodeSelection = {
+ tables: new Set(),
+ schemas: new Set(),
+ databases: new Set(),
+ };
+ const nodeKeyToOriginal = new Map([[database.key, database]]);
+
+ const result = changeCheckboxSelection({
+ row,
+ index: 0,
+ isShiftPressed: false,
+ selection,
+ lastSelectedRowIndex: null,
+ rows: [row],
+ nodeKeyToOriginal,
+ });
+
+ expect(result.selection.tables).toEqual(new Set());
+ expect(result.selection.schemas).toEqual(new Set());
+ expect(result.selection.databases).toEqual(new Set([1]));
+ expect(result.lastSelectedRowIndex).toBe(0);
+ });
+
+ it("should deselect database when all children are selected", () => {
+ const table1 = createMockTableNode(1, "public", 101);
+ const schema1 = createMockSchemaNode(1, "public", [table1]);
+ const database = createMockDatabaseNode(1, [schema1]);
+ const row = createCheckboxRow({
+ type: "database",
+ nodeKey: database.key,
+ databaseId: 1,
+ });
+ const selection: NodeSelection = {
+ tables: new Set([101]),
+ schemas: new Set(),
+ databases: new Set(),
+ };
+ const nodeKeyToOriginal = new Map([[database.key, database]]);
+
+ const result = changeCheckboxSelection({
+ row,
+ index: 0,
+ isShiftPressed: false,
+ selection,
+ lastSelectedRowIndex: null,
+ rows: [row],
+ nodeKeyToOriginal,
+ });
+
+ expect(result.selection.tables).toEqual(new Set());
+ expect(result.selection.schemas).toEqual(new Set());
+ expect(result.lastSelectedRowIndex).toBe(0);
+ });
+ });
+ });
+
+ describe("shift+click range selection", () => {
+ it("should select all tables in a range", () => {
+ const table1 = createMockTableNode(1, "public", 101);
+ const table2 = createMockTableNode(1, "public", 102);
+ const table3 = createMockTableNode(1, "public", 103);
+ const row1 = createCheckboxRow({
+ type: "table",
+ nodeKey: table1.key,
+ tableId: 101,
+ databaseId: 1,
+ });
+ const row2 = createCheckboxRow({
+ type: "table",
+ nodeKey: table2.key,
+ tableId: 102,
+ databaseId: 1,
+ });
+ const row3 = createCheckboxRow({
+ type: "table",
+ nodeKey: table3.key,
+ tableId: 103,
+ databaseId: 1,
+ });
+ const rows = [row1, row2, row3];
+ const selection: NodeSelection = {
+ tables: new Set(),
+ schemas: new Set(),
+ databases: new Set(),
+ };
+ const nodeKeyToOriginal = new Map([
+ [table1.key, table1],
+ [table2.key, table2],
+ [table3.key, table3],
+ ]);
+
+ const result = changeCheckboxSelection({
+ row: row3,
+ index: 2,
+ isShiftPressed: true,
+ selection,
+ lastSelectedRowIndex: 0,
+ rows,
+ nodeKeyToOriginal,
+ });
+
+ expect(result.selection.tables).toEqual(new Set([101, 102, 103]));
+ expect(result.lastSelectedRowIndex).toBe(2);
+ });
+
+ it("should select range backwards (from higher to lower index)", () => {
+ const table1 = createMockTableNode(1, "public", 101);
+ const table2 = createMockTableNode(1, "public", 102);
+ const table3 = createMockTableNode(1, "public", 103);
+ const row1 = createCheckboxRow({
+ type: "table",
+ nodeKey: table1.key,
+ tableId: 101,
+ databaseId: 1,
+ });
+ const row2 = createCheckboxRow({
+ type: "table",
+ nodeKey: table2.key,
+ tableId: 102,
+ databaseId: 1,
+ });
+ const row3 = createCheckboxRow({
+ type: "table",
+ nodeKey: table3.key,
+ tableId: 103,
+ databaseId: 1,
+ });
+ const rows = [row1, row2, row3];
+ const selection: NodeSelection = {
+ tables: new Set(),
+ schemas: new Set(),
+ databases: new Set(),
+ };
+ const nodeKeyToOriginal = new Map([
+ [table1.key, table1],
+ [table2.key, table2],
+ [table3.key, table3],
+ ]);
+
+ const result = changeCheckboxSelection({
+ row: row1,
+ index: 0,
+ isShiftPressed: true,
+ selection,
+ lastSelectedRowIndex: 2,
+ rows,
+ nodeKeyToOriginal,
+ });
+
+ expect(result.selection.tables).toEqual(new Set([101, 102, 103]));
+ expect(result.lastSelectedRowIndex).toBe(0);
+ });
+
+ it("should skip disabled rows in range selection", () => {
+ const table1 = createMockTableNode(1, "public", 101);
+ const table2 = createMockTableNode(1, "public", 102);
+ const table3 = createMockTableNode(1, "public", 103);
+ const row1 = createCheckboxRow({
+ type: "table",
+ nodeKey: table1.key,
+ tableId: 101,
+ databaseId: 1,
+ });
+ const row2 = createCheckboxRow({
+ type: "table",
+ nodeKey: table2.key,
+ tableId: 102,
+ databaseId: 1,
+ isDisabled: true,
+ });
+ const row3 = createCheckboxRow({
+ type: "table",
+ nodeKey: table3.key,
+ tableId: 103,
+ databaseId: 1,
+ });
+ const rows = [row1, row2, row3];
+ const selection: NodeSelection = {
+ tables: new Set(),
+ schemas: new Set(),
+ databases: new Set(),
+ };
+ const nodeKeyToOriginal = new Map([
+ [table1.key, table1],
+ [table2.key, table2],
+ [table3.key, table3],
+ ]);
+
+ const result = changeCheckboxSelection({
+ row: row3,
+ index: 2,
+ isShiftPressed: true,
+ selection,
+ lastSelectedRowIndex: 0,
+ rows,
+ nodeKeyToOriginal,
+ });
+
+ expect(result.selection.tables).toEqual(new Set([101, 103])); // 102 is skipped
+ expect(result.lastSelectedRowIndex).toBe(2);
+ });
+
+ it("should include schema when it is the last row in range", () => {
+ const table1 = createMockTableNode(1, "public", 101);
+ const table2 = createMockTableNode(1, "public", 102);
+ const schema = createMockSchemaNode(1, "public", [table1, table2]);
+ const table3 = createMockTableNode(1, "public", 103);
+ const row1 = createCheckboxRow({
+ type: "table",
+ nodeKey: table3.key,
+ tableId: 103,
+ databaseId: 1,
+ });
+ const row2 = createCheckboxRow({
+ type: "schema",
+ nodeKey: schema.key,
+ databaseId: 1,
+ });
+ const rows = [row1, row2];
+ const selection: NodeSelection = {
+ tables: new Set(),
+ schemas: new Set(),
+ databases: new Set(),
+ };
+ const nodeKeyToOriginal = new Map<string, TreeNode>([
+ [table3.key, table3],
+ [schema.key, schema],
+ ]);
+
+ const result = changeCheckboxSelection({
+ row: row2,
+ index: 1,
+ isShiftPressed: true,
+ selection,
+ lastSelectedRowIndex: 0,
+ rows,
+ nodeKeyToOriginal,
+ });
+
+ // Schema is the last item, so all it's children should be included
+ expect(result.selection.tables).toEqual(new Set([103, 101, 102]));
+ expect(result.lastSelectedRowIndex).toBe(1);
+ });
+
+ it("should skip last expanded database when it has children after it", () => {
+ const table1 = createMockTableNode(1, "public", 101);
+ const table2 = createMockTableNode(1, "public", 102);
+ const schema1 = createMockSchemaNode(1, "public", [table1]);
+ const schema2 = createMockSchemaNode(1, "public", [table2]);
+ const database = createMockDatabaseNode(1, [schema1, schema2]);
+
+ const row1 = createCheckboxRow({
+ type: "database",
+ nodeKey: database.key,
+ databaseId: 1,
+ depth: 0,
+ });
+ const row2 = createCheckboxRow({
+ type: "schema",
+ nodeKey: schema1.key,
+ databaseId: 1,
+ depth: 1,
+ });
+ const row3 = createCheckboxRow({
+ type: "table",
+ nodeKey: table2.key,
+ tableId: 102,
+ databaseId: 1,
+ depth: 2,
+ });
+ const row4 = createCheckboxRow({
+ type: "schema",
+ nodeKey: schema2.key,
+ databaseId: 1,
+ depth: 1,
+ });
+ const rows = [row1, row2, row3, row4];
+ const selection: NodeSelection = {
+ tables: new Set(),
+ schemas: new Set(),
+ databases: new Set(),
+ };
+ const nodeKeyToOriginal = new Map<string, TreeNode>([
+ [database.key, database],
+ [table2.key, table2],
+ ]);
+
+ const result = changeCheckboxSelection({
+ row: row3,
+ index: 2,
+ isShiftPressed: true,
+ selection,
+ lastSelectedRowIndex: 0,
+ rows,
+ nodeKeyToOriginal,
+ });
+
+ // The database itself should NOT be added because it's the last in range
+ // and has children after it (table2), so only table2 should be selected
+ // schema 2 and it's children should not be added
+ expect(result.selection.tables).toEqual(new Set([102]));
+ expect(result.selection.schemas).toEqual(new Set());
+ expect(result.selection.databases).toEqual(new Set());
+ expect(result.lastSelectedRowIndex).toBe(2);
+ });
+
+ it("should skip last expanded schema when it has children after it", () => {
+ const table1 = createMockTableNode(1, "public", 101);
+ const table2 = createMockTableNode(1, "public", 102);
+ const schema = createMockSchemaNode(1, "public", [table1, table2]);
+
+ const row1 = createCheckboxRow({
+ type: "schema",
+ nodeKey: schema.key,
+ databaseId: 1,
+ depth: 0,
+ });
+ const row2 = createCheckboxRow({
+ type: "table",
+ nodeKey: table1.key,
+ tableId: 101,
+ databaseId: 1,
+ depth: 1,
+ });
+ const row3 = createCheckboxRow({
+ type: "table",
+ nodeKey: table2.key,
+ tableId: 102,
+ databaseId: 1,
+ depth: 1,
+ });
+ const rows = [row1, row2, row3];
+ const selection: NodeSelection = {
+ tables: new Set(),
+ schemas: new Set(),
+ databases: new Set(),
+ };
+ const nodeKeyToOriginal = new Map<string, TreeNode>([
+ [schema.key, schema],
+ [table1.key, table1],
+ [table2.key, table2],
+ ]);
+
+ const result = changeCheckboxSelection({
+ row: row2,
+ index: 1,
+ isShiftPressed: true,
+ selection,
+ lastSelectedRowIndex: 0,
+ rows,
+ nodeKeyToOriginal,
+ });
+
+ // The schema itself should NOT be added because it's the last in range
+ // and has children after it (table2), so only table2 should be selected
+ expect(result.selection.tables).toEqual(new Set([101]));
+ expect(result.selection.schemas).toEqual(new Set());
+ expect(result.lastSelectedRowIndex).toBe(1);
+ });
+
+ it("should include database when it is the last row in range", () => {
+ const table1 = createMockTableNode(1, "public", 101);
+ const schema1 = createMockSchemaNode(1, "public", [table1]);
+ const database = createMockDatabaseNode(1, [schema1]);
+
+ const row1 = createCheckboxRow({
+ type: "table",
+ nodeKey: table1.key,
+ tableId: 101,
+ databaseId: 1,
+ });
+ const row2 = createCheckboxRow({
+ type: "database",
+ nodeKey: database.key,
+ databaseId: 1,
+ });
+ const rows = [row1, row2];
+ const selection: NodeSelection = {
+ tables: new Set(),
+ schemas: new Set(),
+ databases: new Set(),
+ };
+ const nodeKeyToOriginal = new Map<string, TreeNode>([
+ [table1.key, table1],
+ [database.key, database],
+ ]);
+
+ const result = changeCheckboxSelection({
+ row: row2,
+ index: 1,
+ isShiftPressed: true,
+ selection,
+ lastSelectedRowIndex: 0,
+ rows,
+ nodeKeyToOriginal,
+ });
+
+ // Both table1 and the database's children should be selected
+ expect(result.selection.tables).toEqual(new Set([101]));
+ expect(result.lastSelectedRowIndex).toBe(1);
+ });
+
+ it("should select collapsed schema between expanded parents", () => {
+ const eventsRaw = createMockTableNode(1, "raw", 101);
+ const usersEvents = createMockTableNode(1, "raw", 102);
+ const rawSchema = createMockSchemaNode(1, "raw", [
+ eventsRaw,
+ usersEvents,
+ ]);
+
+ // staging is collapsed (no children loaded)
+ const stagingSchema = createMockSchemaNode(1, "staging", []);
+
+ const orders = createMockTableNode(2, "public", 201);
+ const sampleDbSchema = createMockSchemaNode(2, "public", [orders]);
+ const sampleDatabase = createMockDatabaseNode(2, [sampleDbSchema]);
+
+ const rowRaw = createCheckboxRow({
+ type: "schema",
+ nodeKey: rawSchema.key,
+ databaseId: 1,
+ depth: 0,
+ });
+ const rowEventsRaw = createCheckboxRow({
+ type: "table",
+ nodeKey: eventsRaw.key,
+ tableId: 101,
+ databaseId: 1,
+ depth: 1,
+ });
+ const rowUsersEvents = createCheckboxRow({
+ type: "table",
+ nodeKey: usersEvents.key,
+ tableId: 102,
+ databaseId: 1,
+ depth: 1,
+ });
+ const rowStaging = createCheckboxRow({
+ type: "schema",
+ nodeKey: stagingSchema.key,
+ databaseId: 1,
+ depth: 0,
+ });
+ const rowSampleDb = createCheckboxRow({
+ type: "database",
+ nodeKey: sampleDatabase.key,
+ databaseId: 2,
+ depth: 0,
+ });
+ const rowOrders = createCheckboxRow({
+ type: "table",
+ nodeKey: orders.key,
+ tableId: 201,
+ databaseId: 2,
+ depth: 1,
+ });
+
+ const rows = [
+ rowRaw,
+ rowEventsRaw,
+ rowUsersEvents,
+ rowStaging,
+ rowSampleDb,
+ rowOrders,
+ ];
+ const selection: NodeSelection = {
+ tables: new Set(),
+ schemas: new Set(),
+ databases: new Set(),
+ };
+ const nodeKeyToOriginal = new Map<string, TreeNode>([
+ [rawSchema.key, rawSchema],
+ [eventsRaw.key, eventsRaw],
+ [usersEvents.key, usersEvents],
+ [stagingSchema.key, stagingSchema],
+ [sampleDatabase.key, sampleDatabase],
+ [orders.key, orders],
+ ]);
+
+ // Click on raw (index 0), shift+click on Orders (index 5)
+ const result = changeCheckboxSelection({
+ row: rowOrders,
+ index: 5,
+ isShiftPressed: true,
+ selection,
+ lastSelectedRowIndex: 0,
+ rows,
+ nodeKeyToOriginal,
+ });
+
+ // staging should be selected by ID since it's collapsed (no children in range)
+ // raw should be skipped since its children (eventsRaw, usersEvents) are in range
+ // sampleDatabase should be skipped since its child (orders) is in range
+ expect(result.selection.tables).toEqual(new Set([101, 102, 201]));
+ expect(result.selection.schemas).toEqual(new Set(["1:staging"]));
+ expect(result.selection.databases).toEqual(new Set());
+ expect(result.lastSelectedRowIndex).toBe(5);
+ });
+ });
+});
diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/data-model/components/TablePicker/utils.ts b/enterprise/frontend/src/metabase-enterprise/data-studio/data-model/components/TablePicker/utils.ts
index ddec8c661b4..81fde6ce98c 100644
--- a/enterprise/frontend/src/metabase-enterprise/data-studio/data-model/components/TablePicker/utils.ts
+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/data-model/components/TablePicker/utils.ts
@@ -26,6 +26,14 @@ export function getTables(tree: RootNode): TableNode[] {
return getSchemas(tree).flatMap((schema) => schema.children);
}
+export function getTreeMap(tree: RootNode): Map<string, TreeNode> {
+ return new Map(
+ [...getDatabases(tree), ...getSchemas(tree), ...getTables(tree)].map(
+ (n) => [n.key, n],
+ ),
+ );
+}
+
// Returns a new state object with all the nodes along the path expanded.
// Note: This only expands parent containers (database, schema) but NOT tables,
// to prevent unwanted expansion when navigating via checkbox selection.