Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore(sql-editor): improve the performance of key-ing functions and procedures (#13583)

Liu Ji2a57557ad206
diff --git a/frontend/src/utils/v1/dbSchema.ts b/frontend/src/utils/v1/dbSchema.ts
index 2871c5e878..29d5553d51 100644
--- a/frontend/src/utils/v1/dbSchema.ts
+++ b/frontend/src/utils/v1/dbSchema.ts
@@ -1,55 +1,7 @@
-import {
- DependentColumn,
- FunctionMetadata,
- ProcedureMetadata,
-} from "@/types/proto/v1/database_service";
-
-export const keyForFunction = (func: FunctionMetadata): string => {
- return JSON.stringify(FunctionMetadata.toJSON(func));
-};
-
-export const extractFunction = (key: string) => {
- if (!key) return FunctionMetadata.fromJSON({});
- try {
- const obj = JSON.parse(key);
- const func = FunctionMetadata.fromJSON(obj);
- return func;
- } catch {
- return FunctionMetadata.fromJSON({});
- }
-};
-
-export const keyForProcedure = (procedure: ProcedureMetadata): string => {
- return JSON.stringify(ProcedureMetadata.toJSON(procedure));
-};
-
-export const extractProcedure = (key: string) => {
- if (!key) return ProcedureMetadata.fromJSON({});
- try {
- const obj = JSON.parse(key);
- const procedure = ProcedureMetadata.fromJSON(obj);
- return procedure;
- } catch {
- return ProcedureMetadata.fromJSON({});
- }
-};
+import { DependentColumn } from "@/types/proto/v1/database_service";
export const keyForDependentColumn = (dep: DependentColumn): string => {
return [dep.schema, dep.table, dep.column]
.map((s) => encodeURIComponent(s))
.join("/");
};
-
-export const extractDependentColumn = (key: string) => {
- if (!key) return DependentColumn.fromJSON({});
- try {
- const parts = key.split("/").map((s) => decodeURIComponent(s));
- return DependentColumn.fromJSON({
- schema: parts[0],
- table: parts[1],
- column: parts[2],
- });
- } catch {
- return DependentColumn.fromJSON({});
- }
-};
diff --git a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/actions.tsx b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/actions.tsx
index a05e0917d1..db3c5bdc7a 100644
--- a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/actions.tsx
+++ b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/actions.tsx
@@ -45,11 +45,10 @@ import {
generateSimpleUpdateStatement,
instanceV1HasAlterSchema,
keyForDependentColumn,
- keyForFunction,
- keyForProcedure,
sortByDictionary,
toClipboard,
} from "@/utils";
+import { keyWithPosition } from "../../EditorCommon";
import {
useEditorPanelContext,
type EditorPanelViewState,
@@ -299,12 +298,12 @@ export const useActions = () => {
);
}
if (type === "procedure") {
- detail.procedure = keyForProcedure(
- (target as NodeTarget<"procedure">).procedure
- );
+ const { procedure, position } = target as NodeTarget<"procedure">;
+ detail.procedure = keyWithPosition(procedure.name, position);
}
if (type === "function") {
- detail.func = keyForFunction((target as NodeTarget<"function">).function);
+ const { function: func, position } = target as NodeTarget<"function">;
+ detail.func = keyWithPosition(func.name, position);
}
if (type === "external-table") {
detail.externalTable = (
diff --git a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/common.ts b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/common.ts
index 3fd184c77f..7aabad06e3 100644
--- a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/common.ts
+++ b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/common.ts
@@ -17,11 +17,8 @@ import type {
TablePartitionMetadata,
ViewMetadata,
} from "@/types/proto/v1/database_service";
-import {
- keyForDependentColumn,
- keyForFunction,
- keyForProcedure,
-} from "@/utils";
+import { keyForDependentColumn } from "@/utils";
+import { keyWithPosition } from "../../EditorCommon";
export type NodeType =
| "database"
@@ -77,9 +74,11 @@ export type RichViewMetadata = RichSchemaMetadata & {
};
export type RichProcedureMetadata = RichSchemaMetadata & {
procedure: ProcedureMetadata;
+ position: number;
};
export type RichFunctionMetadata = RichSchemaMetadata & {
function: FunctionMetadata;
+ position: number;
};
export type TextTarget<E extends boolean = any, S extends boolean = any> = {
expandable: E;
@@ -240,19 +239,25 @@ export const keyForNodeTarget = <T extends NodeType>(
return [db.name, `schemas/${schema.name}`, `views/${view.name}`].join("/");
}
if (type === "procedure") {
- const { db, schema, procedure } = target as NodeTarget<"procedure">;
+ const { db, schema, procedure, position } =
+ target as NodeTarget<"procedure">;
return [
db.name,
`schemas/${schema.name}`,
- `procedures/${keyForProcedure(procedure)}`,
+ `procedures/${keyWithPosition(procedure.name, position)}`,
].join("/");
}
if (type === "function") {
- const { db, schema, function: func } = target as NodeTarget<"function">;
+ const {
+ db,
+ schema,
+ function: func,
+ position,
+ } = target as NodeTarget<"function">;
return [
db.name,
`schemas/${schema.name}`,
- `functions/${keyForFunction(func)}`,
+ `functions/${keyWithPosition(func.name, position)}`,
].join("/");
}
if (type === "expandable-text") {
@@ -636,8 +641,8 @@ const mapProcedureNodes = (
parent: TreeNode<"expandable-text">
) => {
const { schema } = target;
- const children = schema.procedures.map((procedure) =>
- mapTreeNodeByType("procedure", { ...target, procedure }, parent)
+ const children = schema.procedures.map((procedure, position) =>
+ mapTreeNodeByType("procedure", { ...target, procedure, position }, parent)
);
if (children.length === 0) {
return [createDummyNode("procedure", parent)];
@@ -649,8 +654,12 @@ const mapFunctionNodes = (
parent: TreeNode<"expandable-text">
) => {
const { schema } = target;
- const children = schema.functions.map((func) =>
- mapTreeNodeByType("function", { ...target, function: func }, parent)
+ const children = schema.functions.map((func, position) =>
+ mapTreeNodeByType(
+ "function",
+ { ...target, function: func, position },
+ parent
+ )
);
if (children.length === 0) {
return [createDummyNode("function", parent)];
diff --git a/frontend/src/views/sql-editor/EditorCommon/index.ts b/frontend/src/views/sql-editor/EditorCommon/index.ts
index 68ef2afc0a..69e9fbcc61 100644
--- a/frontend/src/views/sql-editor/EditorCommon/index.ts
+++ b/frontend/src/views/sql-editor/EditorCommon/index.ts
@@ -21,3 +21,4 @@ export {
};
export * from "./hover-state";
+export * from "./utils";
diff --git a/frontend/src/views/sql-editor/EditorCommon/utils.ts b/frontend/src/views/sql-editor/EditorCommon/utils.ts
new file mode 100644
index 0000000000..d57da47842
--- /dev/null
+++ b/frontend/src/views/sql-editor/EditorCommon/utils.ts
@@ -0,0 +1,11 @@
+const KEY_WITH_POSITION_DELIMITER = "###";
+
+export const keyWithPosition = (key: string, position: number) => {
+ return `${key}${KEY_WITH_POSITION_DELIMITER}${position}`;
+};
+
+export const extractKeyWithPosition = (key: string) => {
+ const [maybeKey, maybePosition] = key.split(KEY_WITH_POSITION_DELIMITER);
+ const position = parseInt(maybePosition, 10);
+ return [maybeKey, Number.isNaN(position) ? -1 : position];
+};
diff --git a/frontend/src/views/sql-editor/EditorPanel/Panels/FunctionsPanel/FunctionsPanel.vue b/frontend/src/views/sql-editor/EditorPanel/Panels/FunctionsPanel/FunctionsPanel.vue
index be426eef92..d64d0217d5 100644
--- a/frontend/src/views/sql-editor/EditorPanel/Panels/FunctionsPanel/FunctionsPanel.vue
+++ b/frontend/src/views/sql-editor/EditorPanel/Panels/FunctionsPanel/FunctionsPanel.vue
@@ -58,7 +58,10 @@ import {
FunctionMetadata,
SchemaMetadata,
} from "@/types/proto/v1/database_service";
-import { extractFunction, keyForFunction } from "@/utils";
+import {
+ extractKeyWithPosition,
+ keyWithPosition,
+} from "@/views/sql-editor/EditorCommon";
import DatabaseChooser from "@/views/sql-editor/EditorCommon/DatabaseChooser.vue";
import { useEditorPanelContext } from "../../context";
import { SchemaSelectToolbar, CodeViewer } from "../common";
@@ -81,9 +84,11 @@ const metadata = computed(() => {
const schema = database.schemas.find(
(s) => s.name === viewState.value?.schema
);
- const target = extractFunction(viewState.value?.detail?.func ?? "");
+ const [name, position] = extractKeyWithPosition(
+ viewState.value?.detail?.func ?? ""
+ );
const func = schema?.functions.find(
- (f) => f.name === target.name && f.definition === target.definition
+ (f, i) => f.name === name && i === position
);
return { database, schema, func };
});
@@ -92,9 +97,10 @@ const select = (selected: {
database: DatabaseMetadata;
schema: SchemaMetadata;
func: FunctionMetadata;
+ position: number;
}) => {
updateViewState({
- detail: { func: keyForFunction(selected.func) },
+ detail: { func: keyWithPosition(selected.func.name, selected.position) },
});
};
diff --git a/frontend/src/views/sql-editor/EditorPanel/Panels/FunctionsPanel/FunctionsTable.vue b/frontend/src/views/sql-editor/EditorPanel/Panels/FunctionsPanel/FunctionsTable.vue
index 59ee5b4fff..98900df559 100644
--- a/frontend/src/views/sql-editor/EditorPanel/Panels/FunctionsPanel/FunctionsTable.vue
+++ b/frontend/src/views/sql-editor/EditorPanel/Panels/FunctionsPanel/FunctionsTable.vue
@@ -4,7 +4,7 @@
v-bind="$attrs"
ref="dataTableRef"
size="small"
- :row-key="(func) => keyForFunction(func)"
+ :row-key="({ func, position }) => keyWithPosition(func.name, position)"
:columns="columns"
:data="layoutReady ? filteredFuncs : []"
:row-props="rowProps"
@@ -27,10 +27,13 @@ import type {
FunctionMetadata,
SchemaMetadata,
} from "@/types/proto/v1/database_service";
-import { getHighlightHTMLByRegExp, keyForFunction } from "@/utils";
+import { getHighlightHTMLByRegExp } from "@/utils";
+import { keyWithPosition } from "@/views/sql-editor/EditorCommon";
import { useAutoHeightDataTable } from "../../common";
import { useEditorPanelContext } from "../../context";
+type FunctionWithPosition = { func: FunctionMetadata; position: number };
+
const props = defineProps<{
db: ComposedDatabase;
database: DatabaseMetadata;
@@ -47,6 +50,7 @@ const emit = defineEmits<{
database: DatabaseMetadata;
schema: SchemaMetadata;
func: FunctionMetadata;
+ position: number;
}
): void;
}>();
@@ -54,14 +58,21 @@ const emit = defineEmits<{
const { t } = useI18n();
const { viewState } = useEditorPanelContext();
+const funcsWithPosition = computed(() => {
+ return props.funcs.map<FunctionWithPosition>((func, position) => ({
+ func,
+ position,
+ }));
+});
+
const filteredFuncs = computed(() => {
const keyword = props.keyword?.trim().toLowerCase();
if (keyword) {
- return props.funcs.filter((func) =>
+ return funcsWithPosition.value.filter(({ func }) =>
func.name.toLowerCase().includes(keyword)
);
}
- return props.funcs;
+ return funcsWithPosition.value;
});
const { dataTableRef, containerElRef, tableBodyHeight, layoutReady } =
@@ -77,13 +88,15 @@ const vlRef = computed(() => {
});
const columns = computed(() => {
- const columns: (DataTableColumn<FunctionMetadata> & { hide?: boolean })[] = [
+ const columns: (DataTableColumn<FunctionWithPosition> & {
+ hide?: boolean;
+ })[] = [
{
key: "name",
title: t("schema-editor.database.name"),
resizable: true,
className: "truncate",
- render: (func) => {
+ render: ({ func }) => {
return h("span", {
innerHTML: getHighlightHTMLByRegExp(func.name, props.keyword ?? ""),
});
@@ -93,13 +106,14 @@ const columns = computed(() => {
return columns;
});
-const rowProps = (func: FunctionMetadata) => {
+const rowProps = ({ func, position }: FunctionWithPosition) => {
return {
onClick: () => {
emit("click", {
database: props.database,
schema: props.schema,
func,
+ position,
});
},
};
diff --git a/frontend/src/views/sql-editor/EditorPanel/Panels/ProceduresPanel/ProceduresPanel.vue b/frontend/src/views/sql-editor/EditorPanel/Panels/ProceduresPanel/ProceduresPanel.vue
index 1cedbf984f..3546320901 100644
--- a/frontend/src/views/sql-editor/EditorPanel/Panels/ProceduresPanel/ProceduresPanel.vue
+++ b/frontend/src/views/sql-editor/EditorPanel/Panels/ProceduresPanel/ProceduresPanel.vue
@@ -58,7 +58,10 @@ import {
ProcedureMetadata,
SchemaMetadata,
} from "@/types/proto/v1/database_service";
-import { extractProcedure, keyForProcedure } from "@/utils";
+import {
+ extractKeyWithPosition,
+ keyWithPosition,
+} from "@/views/sql-editor/EditorCommon";
import DatabaseChooser from "@/views/sql-editor/EditorCommon/DatabaseChooser.vue";
import { useEditorPanelContext } from "../../context";
import { SchemaSelectToolbar, CodeViewer } from "../common";
@@ -81,9 +84,11 @@ const metadata = computed(() => {
const schema = database.schemas.find(
(s) => s.name === viewState.value?.schema
);
- const target = extractProcedure(viewState.value?.detail?.procedure ?? "");
- const procedure = schema?.functions.find(
- (p) => p.name === target.name && p.definition === target.definition
+ const [name, position] = extractKeyWithPosition(
+ viewState.value?.detail?.procedure ?? ""
+ );
+ const procedure = schema?.procedures.find(
+ (p, i) => p.name === name && i === position
);
return { database, schema, procedure };
});
@@ -92,9 +97,12 @@ const select = (selected: {
database: DatabaseMetadata;
schema: SchemaMetadata;
procedure: ProcedureMetadata;
+ position: number;
}) => {
updateViewState({
- detail: { procedure: keyForProcedure(selected.procedure) },
+ detail: {
+ procedure: keyWithPosition(selected.procedure.name, selected.position),
+ },
});
};
diff --git a/frontend/src/views/sql-editor/EditorPanel/Panels/ProceduresPanel/ProceduresTable.vue b/frontend/src/views/sql-editor/EditorPanel/Panels/ProceduresPanel/ProceduresTable.vue
index 77fce3aee0..7d9016bebd 100644
--- a/frontend/src/views/sql-editor/EditorPanel/Panels/ProceduresPanel/ProceduresTable.vue
+++ b/frontend/src/views/sql-editor/EditorPanel/Panels/ProceduresPanel/ProceduresTable.vue
@@ -4,7 +4,9 @@
v-bind="$attrs"
ref="dataTableRef"
size="small"
- :row-key="(procedure) => keyForProcedure(procedure)"
+ :row-key="
+ ({ procedure, position }) => keyWithPosition(procedure.name, position)
+ "
:columns="columns"
:data="layoutReady ? filteredProcedures : []"
:row-props="rowProps"
@@ -27,10 +29,16 @@ import type {
ProcedureMetadata,
SchemaMetadata,
} from "@/types/proto/v1/database_service";
-import { getHighlightHTMLByRegExp, keyForProcedure } from "@/utils";
+import { getHighlightHTMLByRegExp } from "@/utils";
+import { keyWithPosition } from "@/views/sql-editor/EditorCommon";
import { useAutoHeightDataTable } from "../../common";
import { useEditorPanelContext } from "../../context";
+type ProcedureWithPosition = {
+ procedure: ProcedureMetadata;
+ position: number;
+};
+
const props = defineProps<{
db: ComposedDatabase;
database: DatabaseMetadata;
@@ -47,6 +55,7 @@ const emit = defineEmits<{
database: DatabaseMetadata;
schema: SchemaMetadata;
procedure: ProcedureMetadata;
+ position: number;
}
): void;
}>();
@@ -54,14 +63,21 @@ const emit = defineEmits<{
const { t } = useI18n();
const { viewState } = useEditorPanelContext();
+const proceduresWithPosition = computed(() => {
+ return props.procedures.map<ProcedureWithPosition>((procedure, position) => ({
+ procedure,
+ position,
+ }));
+});
+
const filteredProcedures = computed(() => {
const keyword = props.keyword?.trim().toLowerCase();
if (keyword) {
- return props.procedures.filter((procedure) =>
+ return proceduresWithPosition.value.filter(({ procedure }) =>
procedure.name.toLowerCase().includes(keyword)
);
}
- return props.procedures;
+ return proceduresWithPosition.value;
});
const { dataTableRef, containerElRef, tableBodyHeight, layoutReady } =
@@ -77,13 +93,15 @@ const vlRef = computed(() => {
});
const columns = computed(() => {
- const columns: (DataTableColumn<ProcedureMetadata> & { hide?: boolean })[] = [
+ const columns: (DataTableColumn<ProcedureWithPosition> & {
+ hide?: boolean;
+ })[] = [
{
key: "name",
title: t("schema-editor.database.name"),
resizable: true,
className: "truncate",
- render: (procedure) => {
+ render: ({ procedure }) => {
return h("span", {
innerHTML: getHighlightHTMLByRegExp(
procedure.name,
@@ -96,13 +114,14 @@ const columns = computed(() => {
return columns;
});
-const rowProps = (procedure: ProcedureMetadata) => {
+const rowProps = ({ procedure, position }: ProcedureWithPosition) => {
return {
onClick: () => {
emit("click", {
database: props.database,
schema: props.schema,
procedure,
+ position,
});
},
};
diff --git a/frontend/src/views/sql-editor/EditorPanel/Panels/common/CodeViewer.vue b/frontend/src/views/sql-editor/EditorPanel/Panels/common/CodeViewer.vue
index 39f93e108a..c633bcfe75 100644
--- a/frontend/src/views/sql-editor/EditorPanel/Panels/common/CodeViewer.vue
+++ b/frontend/src/views/sql-editor/EditorPanel/Panels/common/CodeViewer.vue
@@ -7,7 +7,7 @@
<span>{{ title }}</span>
</div>
</NButton>
- <NCheckbox v-if="!formatted.error" v-model:checked="format">
+ <NCheckbox v-model:checked="format">
{{ $t("sql-editor.format") }}
</NCheckbox>
</div>
@@ -68,6 +68,8 @@ const formatted = computedAsync(
);
const content = computed(() => {
- return format.value ? formatted.value.data : props.code;
+ return format.value && !formatted.value.error
+ ? formatted.value.data
+ : props.code;
});
</script>