Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat(sql-editor): add table view for Elasticsearch search results (#19421)
h3n4lf198aebfe371
diff --git a/frontend/src/composables/utils.ts b/frontend/src/composables/utils.tsindex 925f695b73..9c2bd24b9c 100644--- a/frontend/src/composables/utils.ts+++ b/frontend/src/composables/utils.ts@@ -8,8 +8,13 @@ import {} from "lossless-json";import { stringify } from "uuid";import type { SQLResultSetV1 } from "@/types";-import type { QueryRow, RowValue } from "@/types/proto-es/v1/sql_service_pb";+import type {+ QueryResult,+ QueryRow,+ RowValue,+} from "@/types/proto-es/v1/sql_service_pb";import {+ QueryResultSchema,QueryRowSchema,RowValueSchema,} from "@/types/proto-es/v1/sql_service_pb";@@ -299,6 +304,128 @@ export const flattenNoSQLResult = (resultSet: SQLResultSetV1) => {}};+// Parses the hits array from an Elasticsearch _search QueryResult's "hits" column.+const parseESHitsArray = (result: QueryResult): unknown[] | undefined => {+ const hitsColIdx = result.columnNames.indexOf("hits");+ if (hitsColIdx === -1 || result.rows.length === 0) return undefined;++ const hitsCell = result.rows[0]?.values[hitsColIdx];+ if (hitsCell?.kind.case !== "stringValue") return undefined;++ let hitsObj: Record<string, unknown>;+ try {+ hitsObj = JSON.parse(hitsCell.kind.value);+ } catch {+ return undefined;+ }++ const hitsArray = hitsObj?.hits;+ if (!Array.isArray(hitsArray) || hitsArray.length === 0) return undefined;+ return hitsArray;+};++// Discovers columns from ES hits: _id, _score, then sorted _source field keys.+const discoverESColumns = (hitsArray: unknown[]) => {+ const metaFields = ["_id", "_score"];+ const sourceKeySet = new Set<string>();+ for (const hit of hitsArray) {+ const source = (hit as Record<string, unknown>)?._source;+ if (source && typeof source === "object") {+ for (const key of Object.keys(source)) {+ sourceKeySet.add(key);+ }+ }+ }+ const allColumns = [+ ...metaFields,+ ...[...sourceKeySet].sort((a, b) => a.localeCompare(b)),+ ];+ const columnIndexMap = new Map<string, number>();+ for (let i = 0; i < allColumns.length; i++) {+ columnIndexMap.set(allColumns[i], i);+ }+ return { metaFields, allColumns, columnIndexMap };+};++// Builds a single QueryRow from an ES hit document.+const buildESHitRow = (+ hit: Record<string, unknown>,+ metaFields: string[],+ columnCount: number,+ columnIndexMap: Map<string, number>,+ columnTypeNames: string[]+): QueryRow => {+ const values: RowValue[] = Array.from({ length: columnCount }).map(() =>+ createProto(RowValueSchema, {+ kind: { case: "nullValue", value: NullValue.NULL_VALUE },+ })+ );++ for (const field of metaFields) {+ const idx = columnIndexMap.get(field);+ if (idx === undefined) continue;+ const val = hit[field];+ if (val != null) {+ const { value: formatted, type } = convertAnyToRowValue(val, false);+ values[idx] = formatted;+ columnTypeNames[idx] = type;+ }+ }++ const source = hit._source;+ if (source && typeof source === "object") {+ for (const [key, val] of Object.entries(source)) {+ const idx = columnIndexMap.get(key);+ if (idx === undefined) continue;+ if (val != null) {+ const { value: formatted, type } = convertAnyToRowValue(val, true);+ values[idx] = formatted;+ columnTypeNames[idx] = type;+ }+ }+ }++ return createProto(QueryRowSchema, { values });+};++/**+ * Transforms an Elasticsearch _search QueryResult into a tabular format.+ * Detects the "hits" column, extracts hits.hits[], and flattens each hit's+ * _source fields into columns. Returns undefined if the result is not a+ * search response.+ */+export const flattenElasticsearchSearchResult = (+ result: QueryResult+): QueryResult | undefined => {+ const hitsArray = parseESHitsArray(result);+ if (!hitsArray) return undefined;++ const { metaFields, allColumns, columnIndexMap } =+ discoverESColumns(hitsArray);+ const columnTypeNames: string[] = Array.from({+ length: allColumns.length,+ }).map(() => "TEXT");++ const rows = hitsArray.map((hit) =>+ buildESHitRow(+ hit as Record<string, unknown>,+ metaFields,+ allColumns.length,+ columnIndexMap,+ columnTypeNames+ )+ );++ return createProto(QueryResultSchema, {+ columnNames: allColumns,+ columnTypeNames,+ rows,+ rowsCount: BigInt(rows.length),+ statement: result.statement,+ latency: result.latency,+ });+};+const getNoSQLColumns = (rows: QueryRow[]) => {const columnSet = new Set<string>();const columnIndexMap = new Map<string, number>();diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.jsonindex 13422a18c2..d737080d87 100644--- a/frontend/src/locales/en-US.json+++ b/frontend/src/locales/en-US.json@@ -2014,6 +2014,7 @@"no-queriable-database": "No queriable database","rows": "row | rows","vertical-display": "Vertical display",+ "table-view": "Table view","no-history-found": "No history found","search-history-by-statement": "Search by statement","binary-format": "Binary format",diff --git a/frontend/src/locales/es-ES.json b/frontend/src/locales/es-ES.jsonindex 2e69e8080c..424c0f0698 100644--- a/frontend/src/locales/es-ES.json+++ b/frontend/src/locales/es-ES.json@@ -2014,6 +2014,7 @@"no-queriable-database": "No hay base de datos consultable","rows": "fila | filas","vertical-display": "Pantalla vertical",+ "table-view": "Table view","no-history-found": "No se encontró historial","search-history-by-statement": "Búsqueda por enunciado","binary-format": "Formato binario",diff --git a/frontend/src/locales/ja-JP.json b/frontend/src/locales/ja-JP.jsonindex d7a6d70a40..d4b208bc43 100644--- a/frontend/src/locales/ja-JP.json+++ b/frontend/src/locales/ja-JP.json@@ -2014,6 +2014,7 @@"no-queriable-database": "クエリ可能なデータベースがありません","rows": "記録","vertical-display": "縦型表示",+ "table-view": "Table view","no-history-found": "まだ履歴がありません","search-history-by-statement": "ステートメントで検索","binary-format": "バイナリ形式",diff --git a/frontend/src/locales/vi-VN.json b/frontend/src/locales/vi-VN.jsonindex e2f30e5c97..de495468be 100644--- a/frontend/src/locales/vi-VN.json+++ b/frontend/src/locales/vi-VN.json@@ -2014,6 +2014,7 @@"no-queriable-database": "Không có cơ sở dữ liệu có thể truy vấn","rows": "dòng | dòng","vertical-display": "Hiển thị dọc",+ "table-view": "Table view","no-history-found": "Không tìm thấy lịch sử","search-history-by-statement": "Tìm kiếm theo câu lệnh","binary-format": "Định dạng nhị phân",diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.jsonindex 9694906dd1..c90f247ad7 100644--- a/frontend/src/locales/zh-CN.json+++ b/frontend/src/locales/zh-CN.json@@ -2014,6 +2014,7 @@"no-queriable-database": "没有可查询的数据库","rows": "条记录","vertical-display": "竖向展示",+ "table-view": "Table view","no-history-found": "暂无历史记录","search-history-by-statement": "搜索 SQL 语句","binary-format": "二进制格式",diff --git a/frontend/src/utils/storage-keys.ts b/frontend/src/utils/storage-keys.tsindex f649def4f6..d18c5c7c97 100644--- a/frontend/src/utils/storage-keys.ts+++ b/frontend/src/utils/storage-keys.ts@@ -26,6 +26,8 @@ export const STORAGE_KEY_SQL_EDITOR_DETAIL_FORMAT ="bb.sql-editor.detail-panel.format";export const STORAGE_KEY_SQL_EDITOR_DETAIL_LINE_WRAP ="bb.sql-editor.detail-panel.line-wrap";+export const STORAGE_KEY_SQL_EDITOR_ES_TABLE_VIEW =+ "bb.sql-editor.es-table-view";export const STORAGE_KEY_MY_ISSUES_TAB = "bb.components.MY_ISSUES.id";// --- User-scoped (cleaned on logout) ---diff --git a/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/TableCell.vue b/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/TableCell.vueindex c5c3e29981..3690bb32a8 100644--- a/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/TableCell.vue+++ b/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/TableCell.vue@@ -134,7 +134,8 @@ useResizeObserver(cellRef, () => {const clickable = computed(() => {if (truncated.value) return true;- if (getInstanceResource(props.database).engine === Engine.MONGODB) {+ const eng = getInstanceResource(props.database).engine;+ if (eng === Engine.MONGODB || eng === Engine.ELASTICSEARCH) {// A cheap way to check JSON string without paying the parsing cost.const maybeJSON = String(props.value).trim();return (diff --git a/frontend/src/views/sql-editor/EditorCommon/ResultView/SingleResultViewV1.vue b/frontend/src/views/sql-editor/EditorCommon/ResultView/SingleResultViewV1.vueindex bf472fb66e..33efc11112 100644--- a/frontend/src/views/sql-editor/EditorCommon/ResultView/SingleResultViewV1.vue+++ b/frontend/src/views/sql-editor/EditorCommon/ResultView/SingleResultViewV1.vue@@ -38,6 +38,12 @@</span></div><div class="flex justify-between items-center shrink-0 gap-x-2">+ <div v-if="isESSearchResult" class="flex items-center">+ <NSwitch v-model:value="esTableView" size="small" />+ <span class="ml-1 whitespace-nowrap text-sm text-gray-500">+ {{ $t("sql-editor.table-view") }}+ </span>+ </div><div class="flex items-center"><NSwitch v-model:value="state.vertical" size="small" /><span class="ml-1 whitespace-nowrap text-sm text-gray-500">@@ -248,6 +254,7 @@<script lang="ts" setup>import { create } from "@bufbuild/protobuf";+import { useLocalStorage } from "@vueuse/core";import { isEmpty } from "lodash-es";import { ArrowDownIcon, ArrowUpIcon, XIcon } from "lucide-vue-next";import { NButton, NFormItem, NSwitch, NTooltip } from "naive-ui";@@ -266,6 +273,7 @@ import DataExportButton from "@/components/DataExportButton.vue";import EllipsisText from "@/components/EllipsisText.vue";import { CopyButton, Drawer, RichDatabaseName } from "@/components/v2";import { useExecuteSQL } from "@/composables/useExecuteSQL";+import { flattenElasticsearchSearchResult } from "@/composables/utils";import { DISMISS_PLACEHOLDER } from "@/plugins/ai/components/state";import { useSQLEditorStore, useSQLEditorTabStore } from "@/store";import type {@@ -288,6 +296,7 @@ import {isNullOrUndefined,type SearchParams,} from "@/utils";+import { STORAGE_KEY_SQL_EDITOR_ES_TABLE_VIEW } from "@/utils/storage-keys";import {provideSQLResultViewContext,type SQLResultViewContext,@@ -382,10 +391,35 @@ const viewMode = computed((): ViewMode => {return "RESULT";});+const engine = computed(() => getInstanceResource(props.database).engine);++const esTableView = useLocalStorage(STORAGE_KEY_SQL_EDITOR_ES_TABLE_VIEW, true);++const flattenedESResult = computed(() => {+ if (engine.value !== Engine.ELASTICSEARCH) return undefined;+ return flattenElasticsearchSearchResult(props.result);+});++const isESSearchResult = computed(() => flattenedESResult.value !== undefined);++const activeResult = computed(() => {+ if (isESSearchResult.value && esTableView.value) {+ return flattenedESResult.value!;+ }+ return props.result;+});++watch(esTableView, () => {+ state.sortState = undefined;+ state.searchParams = { query: "", scopes: [] };+ state.searchCandidateActiveIndex = -1;+ state.searchCandidateRowIndexs = [];+});+const columns = computed((): ResultTableColumn[] => {- return props.result.columnNames.map<ResultTableColumn>(+ return activeResult.value.columnNames.map<ResultTableColumn>((columnName, index) => {- const columnType = props.result.columnTypeNames[index];+ const columnType = activeResult.value.columnTypeNames[index];return {id: columnName,name: columnName,@@ -575,7 +609,7 @@ const rows = computed((): ResultTableRow[] => {const sortState = state.sortState;if (!sortState || !sortState.direction) {- return props.result.rows.map((item, index) => ({+ return activeResult.value.rows.map((item, index) => ({key: index,item,}));@@ -584,7 +618,7 @@ const rows = computed((): ResultTableRow[] => {const { columnIndex, direction } = sortState;const columnType = columns.value[columnIndex]?.columnType ?? "";- return props.result.rows+ return activeResult.value.rows.map((item, index) => ({key: index,item,@@ -606,6 +640,8 @@ const resultRowsText = computed(() => {});const isSensitiveColumn = (columnIndex: number): boolean => {+ // Column indices don't match when showing flattened ES table view.+ if (isESSearchResult.value && esTableView.value) return false;const maskingReason = props.result.masked?.[columnIndex];// Check if maskingReason exists and has actual content (not empty object)return (@@ -617,6 +653,8 @@ const isSensitiveColumn = (columnIndex: number): boolean => {};const getMaskingReason = (columnIndex: number) => {+ // Column indices don't match when showing flattened ES table view.+ if (isESSearchResult.value && esTableView.value) return undefined;if (!props.result.masked || columnIndex >= props.result.masked.length) {return undefined;}@@ -635,8 +673,6 @@ provideSelectionContext({resultViewContext,});-const engine = computed(() => getInstanceResource(props.database).engine);-const showVisualizeButton = computed((): boolean => {return ((engine.value === Engine.POSTGRES ||