Skip to content

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

chore(sql-editor): improved data sorting (#11175)

Liu Jiad1a6529c187
diff --git a/frontend/src/utils/v1/sql.ts b/frontend/src/utils/v1/sql.ts
index f8046356a2..3c6f5c9f5b 100644
--- a/frontend/src/utils/v1/sql.ts
+++ b/frontend/src/utils/v1/sql.ts
@@ -1,8 +1,12 @@
+import dayjs from "dayjs";
import { NullValue } from "@/types/proto/google/protobuf/struct";
import { Engine } from "@/types/proto/v1/common";
import { RowValue } from "@/types/proto/v1/sql_service";
-export const extractSQLRowValue = (value: RowValue) => {
+export const extractSQLRowValue = (value: RowValue | undefined) => {
+ if (typeof value === "undefined") {
+ return null;
+ }
if (value.nullValue === NullValue.NULL_VALUE) {
return null;
}
@@ -85,3 +89,46 @@ export const generateSimpleSelectAllStatement = (
return `SELECT * FROM ${schemaAndTable} LIMIT ${limit};`;
};
+
+export const compareQueryRowValues = (
+ type: string,
+ a: RowValue,
+ b: RowValue
+): number => {
+ const valueA = extractSQLRowValue(a);
+ const valueB = extractSQLRowValue(b);
+ if (type === "INT" || type === "INTEGER") {
+ const intA = toInt(valueA);
+ const intB = toInt(valueB);
+ return intA - intB;
+ }
+ if (type === "FLOAT") {
+ const floatA = toFloat(valueA);
+ const floatB = toFloat(valueB);
+ return floatA - floatB;
+ }
+ if (type === "DATE" || type === "DATETIME") {
+ const dateA = dayjs(valueA);
+ const dateB = dayjs(valueB);
+ return dateA.isBefore(dateB) ? -1 : dateA.isAfter(dateB) ? 1 : 0;
+ }
+ const stringA = String(valueA);
+ const stringB = String(valueB);
+ return stringA < stringB ? -1 : stringA > stringB ? 1 : 0;
+};
+
+const toInt = (a: any) => {
+ return typeof a === "number"
+ ? a
+ : typeof a === "string"
+ ? parseInt(a, 10)
+ : Number(a);
+};
+
+const toFloat = (a: any) => {
+ return typeof a === "number"
+ ? a
+ : typeof a === "string"
+ ? parseFloat(a)
+ : Number(a);
+};
diff --git a/frontend/src/views/sql-editor/EditorCommon/ResultView/DataBlock.vue b/frontend/src/views/sql-editor/EditorCommon/ResultView/DataBlock.vue
index bcaad2baae..18f07f8c97 100644
--- a/frontend/src/views/sql-editor/EditorCommon/ResultView/DataBlock.vue
+++ b/frontend/src/views/sql-editor/EditorCommon/ResultView/DataBlock.vue
@@ -41,7 +41,7 @@
<div class="flex-1">
<TableCell
:table="table"
- :value="row.getVisibleCells()[header.index].getValue()"
+ :value="row.getVisibleCells()[header.index].getValue() as RowValue"
:keyword="keyword"
:set-index="setIndex"
:row-index="offset + rowIndex"
@@ -61,12 +61,13 @@
import { Table } from "@tanstack/vue-table";
import { computed } from "vue";
import { useSubscriptionV1Store } from "@/store";
+import { QueryRow, RowValue } from "@/types/proto/v1/sql_service";
import SensitiveDataIcon from "./DataTable/SensitiveDataIcon.vue";
import TableCell from "./DataTable/TableCell.vue";
import { useSQLResultViewContext } from "./context";
const props = defineProps<{
- table: Table<string[]>;
+ table: Table<QueryRow>;
setIndex: number;
offset: number;
isSensitiveColumn: (index: number) => boolean;
diff --git a/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/ColumnSortedIcon.vue b/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/ColumnSortedIcon.vue
index ae2be3ef9d..390d0a5e92 100644
--- a/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/ColumnSortedIcon.vue
+++ b/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/ColumnSortedIcon.vue
@@ -1,17 +1,11 @@
<template>
<span class="w-4 h-auto opacity-80 ml-1">
- <ArrowUpWideNarrow
- v-if="isSorted === 'desc'"
- class="w-full h-auto text-accent"
- />
<ArrowDownWideNarrow
- v-else
- :class="[
- 'w-full h-auto',
- !isSorted && ' text-gray-300',
- !!isSorted && 'text-accent',
- ]"
+ v-if="!isSorted || isSorted === 'desc'"
+ class="w-full h-auto"
+ :class="[isSorted ? 'text-accent' : 'text-gray-300']"
/>
+ <ArrowUpWideNarrow v-else class="w-full h-auto text-accent" />
</span>
</template>
diff --git a/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/DataTable.vue b/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/DataTable.vue
index 1888ae242c..c415e9c16b 100644
--- a/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/DataTable.vue
+++ b/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/DataTable.vue
@@ -108,6 +108,7 @@
import { Table } from "@tanstack/vue-table";
import { computed, nextTick, ref, watch } from "vue";
import { useSubscriptionV1Store } from "@/store";
+import { QueryRow } from "@/types/proto/v1/sql_service";
import { useSQLResultViewContext } from "../context";
import ColumnSortedIcon from "./ColumnSortedIcon.vue";
import SensitiveDataIcon from "./SensitiveDataIcon.vue";
@@ -120,7 +121,7 @@ export type DataTableColumn = {
};
const props = defineProps<{
- table: Table<string[]>;
+ table: Table<QueryRow>;
setIndex: number;
offset: number;
isSensitiveColumn: (index: number) => boolean;
diff --git a/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/TableCell.vue b/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/TableCell.vue
index b5c373649d..07bdf60e3e 100644
--- a/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/TableCell.vue
+++ b/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/TableCell.vue
@@ -29,12 +29,13 @@ import { NButton } from "naive-ui";
import { computed, ref } from "vue";
import { useConnectionOfCurrentSQLEditorTab } from "@/store";
import { Engine } from "@/types/proto/v1/common";
-import { getHighlightHTMLByRegExp } from "@/utils";
+import { QueryRow, RowValue } from "@/types/proto/v1/sql_service";
+import { extractSQLRowValue, getHighlightHTMLByRegExp } from "@/utils";
import { useSQLResultViewContext } from "../context";
const props = defineProps<{
- table: Table<string[]>;
- value: unknown;
+ table: Table<QueryRow>;
+ value: RowValue;
setIndex: number;
rowIndex: number;
colIndex: number;
@@ -83,10 +84,11 @@ const classes = computed(() => {
});
const html = computed(() => {
- if (props.value === null) {
+ const value = extractSQLRowValue(props.value);
+ if (value === null) {
return `<span class="text-gray-400 italic">NULL</span>`;
}
- const str = String(props.value);
+ const str = String(value);
if (str.length === 0) {
return `<br style="min-width: 1rem; display: inline-flex;" />`;
}
diff --git a/frontend/src/views/sql-editor/EditorCommon/ResultView/DetailPanel.vue b/frontend/src/views/sql-editor/EditorCommon/ResultView/DetailPanel.vue
index 5b322f4fde..089e129e43 100644
--- a/frontend/src/views/sql-editor/EditorCommon/ResultView/DetailPanel.vue
+++ b/frontend/src/views/sql-editor/EditorCommon/ResultView/DetailPanel.vue
@@ -83,20 +83,23 @@ import { computed } from "vue";
import { useI18n } from "vue-i18n";
import { DrawerContent } from "@/components/v2";
import { pushNotification } from "@/store";
+import { RowValue } from "@/types/proto/v1/sql_service";
+import { extractSQLRowValue } from "@/utils";
import { useSQLResultViewContext } from "./context";
const { t } = useI18n();
const { dark, detail, disallowCopyingData } = useSQLResultViewContext();
-const detailValue = computed(() => {
+const detailValueString = computed(() => {
const { row, col, table } = detail.value;
if (!table) return undefined;
const value = table
.getPrePaginationRowModel()
.rows[row]?.getVisibleCells()
- [col]?.getValue<string>();
- return value;
+ [col]?.getValue<RowValue>();
+
+ return String(extractSQLRowValue(value));
});
const totalCount = computed(() => {
@@ -106,7 +109,7 @@ const totalCount = computed(() => {
});
const html = computed(() => {
- const str = detailValue.value;
+ const str = detailValueString.value;
if (!str || str.length === 0) {
return `<br style="min-width: 1rem; display: inline-flex;" />`;
}
@@ -116,7 +119,7 @@ const html = computed(() => {
const { copy, copied } = useClipboard({
source: computed(() => {
- return detailValue.value ?? "";
+ return detailValueString.value ?? "";
}),
legacy: true,
});
diff --git a/frontend/src/views/sql-editor/EditorCommon/ResultView/SingleResultViewV1.vue b/frontend/src/views/sql-editor/EditorCommon/ResultView/SingleResultViewV1.vue
index f8ee230563..2a8713753b 100644
--- a/frontend/src/views/sql-editor/EditorCommon/ResultView/SingleResultViewV1.vue
+++ b/frontend/src/views/sql-editor/EditorCommon/ResultView/SingleResultViewV1.vue
@@ -185,8 +185,9 @@ import {
} from "@/types";
import { ExportFormat } from "@/types/proto/v1/common";
import { Engine } from "@/types/proto/v1/common";
-import { QueryResult } from "@/types/proto/v1/sql_service";
+import { QueryResult, QueryRow, RowValue } from "@/types/proto/v1/sql_service";
import {
+ compareQueryRowValues,
createExplainToken,
extractSQLRowValue,
hasPermissionToCreateRequestGrantIssue,
@@ -296,28 +297,34 @@ const updateKeyword = (value: string) => {
};
const columns = computed(() => {
- const columns = props.result.columnNames;
- return columns.map<ColumnDef<string[]>>((col, index) => ({
- id: `${col}@${index}`,
- accessorFn: (item) => item[index],
- header: col,
- }));
-});
-
-const convertedData = computed(() => {
- const rows = props.result.rows;
- return rows.map((row) => {
- return row.values.map((value) => extractSQLRowValue(value));
- });
+ return props.result.columnNames.map<ColumnDef<QueryRow, RowValue>>(
+ (columnName, index) => {
+ const columnType = props.result.columnTypeNames[index] as string;
+ return {
+ id: `${columnName}@${index}`,
+ accessorFn: (item) => item.values[index],
+ header: columnName,
+ sortingFn: (rowA, rowB) => {
+ return compareQueryRowValues(
+ columnType,
+ rowA.original.values[index],
+ rowB.original.values[index]
+ );
+ },
+ };
+ }
+ );
});
const data = computed(() => {
- const data = convertedData.value;
+ const data = props.result.rows;
const search = keyword.value.trim().toLowerCase();
let temp = data;
if (search) {
temp = data.filter((item) => {
- return item.some((col) => String(col).toLowerCase().includes(search));
+ return item.values.some((col) =>
+ String(extractSQLRowValue(col)).toLowerCase().includes(search)
+ );
});
}
return temp;
@@ -334,7 +341,7 @@ const isColumnMissingSensitive = (columnIndex: number): boolean => {
);
};
-const table = useVueTable<string[]>({
+const table = useVueTable<QueryRow>({
get data() {
return data.value;
},
diff --git a/frontend/src/views/sql-editor/EditorCommon/ResultView/context.ts b/frontend/src/views/sql-editor/EditorCommon/ResultView/context.ts
index a543a5a43f..66619b0d76 100644
--- a/frontend/src/views/sql-editor/EditorCommon/ResultView/context.ts
+++ b/frontend/src/views/sql-editor/EditorCommon/ResultView/context.ts
@@ -1,5 +1,6 @@
import { type Table } from "@tanstack/vue-table";
import { type Ref, type InjectionKey, provide, inject } from "vue";
+import { QueryRow } from "@/types/proto/v1/sql_service";
export type SQLResultViewContext = {
dark: Ref<boolean>;
@@ -10,7 +11,7 @@ export type SQLResultViewContext = {
set: number; // The index of selected result set.
row: number; // The row index of selected record.
col: number; // The column index of selected cell.
- table: Table<string[]> | undefined;
+ table: Table<QueryRow> | undefined;
}>;
};