fix(doris): preserve aggregate-state bytes in query results

This commit is contained in:
Guoyu Su authored and GitHub committed 2026-09-28 01:01:09 +08:00
1 parent 4bcab01489
commit c75ce783df
17 files changed
+1066 -42

No files matched your search

@@ -166,8 +166,11 @@ import {
downloadBinaryCellPayload,
formatBinaryCellByteSize,
binaryCellUtf8Text,
hasUnsafeOpaqueAggregateStatePredicate,
isBlobCellColumnType,
isBinaryCellColumnType,
isOpaqueAggregateStateColumnType,
mergeOpaqueReadonlyColumnIndexes,
openBinaryCellFile,
parseBinaryCellBytes,
retainBinaryCellDownloadMenuForHover,
@@ -3878,7 +3881,7 @@ const editor = useDataGridEditor({
tableMeta: computed(() => props.tableMeta),
sourceColumns: computed(() => props.sourceColumns),
joinedWriteTargets: computed(() => props.joinedWriteTargets),
readonlyColumnIndexes: computed(() => (props.readonlyColumnIndexes ? new Set(props.readonlyColumnIndexes) : undefined)),
readonlyColumnIndexes: computed(() => mergeOpaqueReadonlyColumnIndexes(props.readonlyColumnIndexes, allColumnTypes.value)),
canEditExistingRows,
onExecuteSql: computed(() => props.onExecuteSql),
customSaveHandler: computed(() => props.customSaveHandler),
@@ -4088,6 +4091,7 @@ function canEditRowItem(item: RowItem | undefined): boolean {
function canEditCellItem(item: RowItem | undefined, columnIndex: number): boolean {
if (!canEditRowItem(item) || !item || !canEditColumn(columnIndex)) return false;
if (isOpaqueAggregateStateColumnType(allColumnTypes.value[columnIndex])) return false;
if (!item.isNew && !item.isDraft && !canUpdateExistingRows.value) return false;
if (isSavingNewRow(item)) return false;
const column = props.result.columns[columnIndex] ?? "";
@@ -4351,6 +4355,7 @@ function isDecimalColumnType(dataType: string): boolean {
function canDeleteRowItem(item: RowItem | undefined): boolean {
if (!item) return false;
if (!item.isNew && canUseKeylessRowPredicate(props.databaseType, props.tableMeta?.primaryKeys ?? []) && hasUnsafeOpaqueAggregateStatePredicate(allColumnTypes.value, item.data)) return false;
const canDelete = canDeleteGridRowItem({
editable: !!props.editable && canDeleteRows.value,
isDraft: !!item.isDraft,
@@ -113,6 +113,14 @@ import DataGridSearchBar from "@/components/grid/DataGridSearchBar.vue";
const cellDetailPanelSource = readFileSync("apps/desktop/src/components/grid/DataGridCellDetailPanel.vue", "utf8");
const cellDetailDialogSource = readFileSync("apps/desktop/src/components/grid/DataGridCellDetailDialog.vue", "utf8");
const binaryTextPreviewSource = readFileSync("apps/desktop/src/components/grid/DataGridCellDetailTextPreview.vue", "utf8");
const dataGridSource = readFileSync("apps/desktop/src/components/grid/DataGrid.vue", "utf8");
describe("DataGrid opaque aggregate-state safety", () => {
it("blocks cell edits and keyless row deletion through the centralized gates", () => {
expect(dataGridSource).toMatch(/function canEditCellItem[\s\S]*isOpaqueAggregateStateColumnType\(allColumnTypes\.value\[columnIndex\]\)/);
expect(dataGridSource).toMatch(/function canDeleteRowItem[\s\S]*canUseKeylessRowPredicate[\s\S]*hasUnsafeOpaqueAggregateStatePredicate\(allColumnTypes\.value, item\.data\)/);
});
});
function detail(patch: Partial<DataGridCellDetail> = {}): DataGridCellDetail {
return {
@@ -51,11 +51,13 @@ function createEditorWithResult(
mongoCollectionGrid = false,
quickEntry = false,
editable = ref(true),
resultColumnTypes?: string[],
) {
let editor: ReturnType<typeof useDataGridEditor>;
const result = ref<{ columns: string[]; rows: CellValue[][] }>({
const result = ref<{ columns: string[]; rows: CellValue[][]; column_types?: string[] }>({
columns: ["first", "hidden", "last"],
rows: existingRows,
column_types: resultColumnTypes,
});
editor = useDataGridEditor({
@@ -734,12 +736,66 @@ describe("useDataGridEditor appendPastedRowsToNewRow", () => {
]);
});
it("keeps explicitly read-only mapped columns out of editing and paste", () => {
const editor = createEditor(["first", "hidden", "last"], true, undefined, [0]);
it("preserves ordinary read-only values when cloning while still blocking edits and paste", () => {
const editor = createEditor(["first", "hidden", "last"], true, undefined, [0], [["Ada", "hidden", "Lovelace"]]);
expect(editor.canEditColumn(0)).toBe(false);
expect(editor.canEditColumn(2)).toBe(true);
expect(editor.appendPastedRowsToNewRow(-1, [["Ada"]], [0])).toEqual({ ok: false, reason: "readonly-column" });
editor.applyCellValue(0, 0, null);
expect(editor.dirtyRows.value.size).toBe(0);
editor.cloneRow(0);
expect(editor.newRows.value.at(-1)).toEqual(["Ada", "hidden", "Lovelace"]);
});
it("clears opaque aggregate states when cloning and prefers the result type over metadata", () => {
const opaque = createEditor(
["first", "hidden", "last"],
true,
undefined,
[1],
[["Ada", "0x00ff", "Lovelace"]],
undefined,
[
{ name: "first", data_type: "varchar" },
{ name: "hidden", data_type: "varchar" },
{ name: "last", data_type: "varchar" },
],
false,
false,
ref(true),
["varchar", "agg_state<sum(int)>", "varchar"],
);
opaque.cloneRow(0);
expect(opaque.newRows.value.at(-1)).toEqual(["Ada", null, "Lovelace"]);
const metadataFallback = createEditor(["first", "hidden", "last"], true, undefined, [1], [["Ada", "0x00ff", "Lovelace"]], undefined, [
{ name: "first", data_type: "varchar" },
{ name: "hidden", data_type: "agg_state<sum(int)>" },
{ name: "last", data_type: "varchar" },
]);
metadataFallback.cloneRow(0);
expect(metadataFallback.newRows.value.at(-1)).toEqual(["Ada", null, "Lovelace"]);
const resultTypeWins = createEditor(
["first", "hidden", "last"],
true,
undefined,
[1],
[["Ada", "ordinary", "Lovelace"]],
undefined,
[
{ name: "first", data_type: "varchar" },
{ name: "hidden", data_type: "agg_state<sum(int)>" },
{ name: "last", data_type: "varchar" },
],
false,
false,
ref(true),
["varchar", "varchar", "varchar"],
);
resultTypeWins.cloneRow(0);
expect(resultTypeWins.newRows.value.at(-1)).toEqual(["Ada", "ordinary", "Lovelace"]);
});
it("fills following blank new rows before adding more rows", () => {
@@ -6,6 +6,7 @@ import type { CellValue } from "@/lib/dataGrid/cellValue";
import { coerceDataGridCellValue, dataGridCellEditorText } from "@/lib/dataGrid/dataGridCellCoercion";
import { focusDataGridEditorWithoutScrolling, preserveDataGridScrollPosition } from "@/lib/dataGrid/dataGridEditorFocus";
import { normalizeDataGridSaveError } from "@/lib/dataGrid/dataGridSql";
import { isOpaqueAggregateStateColumnType } from "@/lib/dataGrid/binaryCellDownload";
import { rowStatusFilterAfterAddingRow, type RowStatusFilter } from "@/lib/dataGrid/gridRowStatus";
import type { GridNewRowMeta, GridNewRowPlacement } from "@/lib/dataGrid/gridNewRowPlacement";
import { supportsDataGridTransaction } from "@/lib/table/tableEditing";
@@ -107,7 +108,7 @@ export interface DataGridSaveConfirmationRequest {
}
export interface UseDataGridEditorOptions {
result: ComputedRef<{ columns: string[]; rows: CellValue[][] }>;
result: ComputedRef<{ columns: string[]; rows: CellValue[][]; column_types?: string[] }>;
editable: ComputedRef<boolean | undefined>;
databaseType: ComputedRef<DatabaseType | undefined>;
connectionId: ComputedRef<string | undefined>;
@@ -1331,7 +1332,10 @@ export function useDataGridEditor(options: UseDataGridEditorOptions) {
return (columnIndex: number) => {
const columnName = sourceColumns.value?.[columnIndex] ?? result.value.columns[columnIndex];
if (columnName === undefined) return false;
return shouldClearClonedColumn(columnName, columnInfoByName.get(columnName.toLowerCase()));
const columnInfo = columnInfoByName.get(columnName.toLowerCase());
const resultType = result.value.column_types?.[columnIndex]?.trim();
if (isOpaqueAggregateStateColumnType(resultType || columnInfo?.data_type)) return true;
return shouldClearClonedColumn(columnName, columnInfo);
};
}
@@ -1,5 +1,15 @@
import { describe, expect, it } from "vitest";
import { binaryCellBytesToHexValue, canImportBinaryCellFile } from "@/lib/dataGrid/binaryCellDownload";
import {
binaryCellBytesToHexValue,
binaryCellClipboardText,
binaryCellDisplayText,
binaryCellDownloadPayload,
canImportBinaryCellFile,
hasUnsafeOpaqueAggregateStatePredicate,
isBinaryCellColumnType,
isOpaqueAggregateStateColumnType,
mergeOpaqueReadonlyColumnIndexes,
} from "@/lib/dataGrid/binaryCellDownload";
describe("binary cell file import", () => {
it("encodes arbitrary and empty files as prefixed hex cell values", () => {
@@ -14,5 +24,34 @@ describe("binary cell file import", () => {
expect(canImportBinaryCellFile("postgres", "text")).toBe(false);
expect(canImportBinaryCellFile("sqlserver", "varbinary(max)")).toBe(false);
expect(canImportBinaryCellFile("sqlite", "blob")).toBe(false);
expect(canImportBinaryCellFile("mysql", "agg_state<group_concat(text)>")).toBe(false);
});
it("presents aggregate states as opaque byte-preserving binary values", () => {
const type = "agg_state<group_concat(text)>";
const value = "0x0001abff";
expect(isOpaqueAggregateStateColumnType(type)).toBe(true);
expect(isOpaqueAggregateStateColumnType(" AGG_STATE < sum(int) > ")).toBe(true);
expect(isOpaqueAggregateStateColumnType("agg_state<")).toBe(false);
expect(isOpaqueAggregateStateColumnType("agg_state<>")).toBe(false);
expect(isOpaqueAggregateStateColumnType("😺agg_state<sum(int)>")).toBe(false);
expect(isBinaryCellColumnType(type)).toBe(true);
expect(binaryCellDisplayText(value, type, undefined, "mysql")).toBe("AGG_STATE [4 bytes]");
expect(binaryCellClipboardText(value, type, "mysql")).toBeNull();
expect(binaryCellDownloadPayload(value, "binary", type, "mysql").data).toEqual(new Uint8Array([0x00, 0x01, 0xab, 0xff]));
});
it("merges opaque columns into the centralized readonly indexes", () => {
const readonly = mergeOpaqueReadonlyColumnIndexes([0, 3], ["varchar", "agg_state<sum(int)>", "varbinary", "text"]);
expect([...readonly!]).toEqual([0, 3, 1]);
expect(mergeOpaqueReadonlyColumnIndexes(undefined, ["varchar", "varbinary"])).toBeUndefined();
});
it("only blocks keyless predicates for non-null or missing opaque values", () => {
const types = ["varchar", "agg_state<sum(int)>"];
expect(hasUnsafeOpaqueAggregateStatePredicate(types, ["Ada", null])).toBe(false);
expect(hasUnsafeOpaqueAggregateStatePredicate(types, ["Ada", "0x00ff"])).toBe(true);
expect(hasUnsafeOpaqueAggregateStatePredicate(types, ["Ada"])).toBe(true);
expect(hasUnsafeOpaqueAggregateStatePredicate(["varchar", "text"], ["Ada", "ordinary"])).toBe(false);
});
});
@@ -117,6 +117,7 @@ const BINARY_STRING_TYPE_RE = /^(?:binary|varbinary)(?:\b|\()/i;
const VARBINARY_TYPE_RE = /^varbinary(?:\b|\()/i;
const BLOB_TYPE_RE = /^(?:blob|tinyblob|mediumblob|longblob)(?:\b|\()/i;
const MYSQL_FILE_IMPORT_TYPE_RE = /^(?:blob|tinyblob|mediumblob|longblob|binary|varbinary)(?:\b|\()/i;
const OPAQUE_AGG_STATE_TYPE_RE = /^agg_state\s*<\s*[^>\s][^>]*>$/i;
function copyBytesForBlob(bytes: Uint8Array): Uint8Array<ArrayBuffer> {
return new Uint8Array(bytes);
@@ -181,7 +182,23 @@ export function parseBinaryCellBytes(value: unknown, columnType?: string, databa
export function isBinaryCellColumnType(columnType?: string): boolean {
const type = (columnType ?? "").trim();
return !!type && BINARY_TYPE_RE.test(type);
return !!type && (BINARY_TYPE_RE.test(type) || OPAQUE_AGG_STATE_TYPE_RE.test(type));
}
export function isOpaqueAggregateStateColumnType(columnType?: string): boolean {
return OPAQUE_AGG_STATE_TYPE_RE.test((columnType ?? "").trim());
}
export function hasUnsafeOpaqueAggregateStatePredicate(columnTypes: readonly (string | undefined)[], row: readonly CellValue[]): boolean {
return columnTypes.some((columnType, index) => isOpaqueAggregateStateColumnType(columnType) && row[index] !== null);
}
export function mergeOpaqueReadonlyColumnIndexes(readonlyColumnIndexes: readonly number[] | undefined, columnTypes: readonly (string | undefined)[]): ReadonlySet<number> | undefined {
const merged = new Set(readonlyColumnIndexes ?? []);
columnTypes.forEach((columnType, index) => {
if (isOpaqueAggregateStateColumnType(columnType)) merged.add(index);
});
return merged.size ? merged : undefined;
}
export function isBlobCellColumnType(columnType?: string): boolean {
@@ -294,6 +311,7 @@ export function binaryCellClipboardText(value: unknown, columnType?: string, dat
// “显示是文本、编辑器却是十六进制”的不一致,故 blob 文本预览仅在 mysql 连接开启。
// binary/varbinary 的文本预览早于该特性存在(如 TDengine BINARY 文本),保持全库通用。
function isBinaryCellTextPreviewColumn(columnType: string | undefined, databaseType: DatabaseType | undefined): boolean {
if (isOpaqueAggregateStateColumnType(columnType)) return false;
if (BINARY_STRING_TYPE_RE.test((columnType ?? "").trim())) return true;
return isBlobCellColumnType(columnType) && databaseType === "mysql";
}
@@ -367,6 +385,7 @@ function printableText(text: string): boolean {
}
function binaryCellDisplayLabel(columnType?: string): string {
if (isOpaqueAggregateStateColumnType(columnType)) return "AGG_STATE";
const base = (columnType ?? "")
.trim()
.split(/[(:\s]/)[0]