From efcf8d85c5959bd3d718031bc73589542e1b99b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kr=C3=A6n=20Hansen?= Date: Wed, 2 Apr 2025 11:51:40 +0200 Subject: [PATCH 1/2] Add special handling of vector sub-type --- .../src/components/bson-value.tsx | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/packages/compass-components/src/components/bson-value.tsx b/packages/compass-components/src/components/bson-value.tsx index 0335267f4b2..5660bce7b8b 100644 --- a/packages/compass-components/src/components/bson-value.tsx +++ b/packages/compass-components/src/components/bson-value.tsx @@ -157,6 +157,37 @@ const BinaryValue: React.FunctionComponent> = ({ return { stringifiedValue: `UUID('${uuid}')` }; } + if (value.sub_type === Binary.SUBTYPE_VECTOR) { + const vectorType = value.buffer[0]; + if (vectorType === Binary.VECTOR_TYPE.Int8) { + const truncatedSerializedBuffer = truncate( + value.toInt8Array().slice(0, 100).join(', '), + 100 + ); + return { + stringifiedValue: `Binary.fromInt8Array(new Int8Array([${truncatedSerializedBuffer}]))`, + }; + } else if (vectorType === Binary.VECTOR_TYPE.Float32) { + const truncatedSerializedBuffer = truncate( + [...value.toFloat32Array().slice(0, 100)] + // Using a limited precision and removing trailing zeros for better displaying + .map((num) => num.toPrecision(8).replace(/\.?0+$/, '')) + .join(', '), + 100 + ); + return { + stringifiedValue: `Binary.fromFloat32Array(new Float32Array([${truncatedSerializedBuffer}]))`, + }; + } else if (vectorType === Binary.VECTOR_TYPE.PackedBit) { + const truncatedSerializedBuffer = truncate( + value.toPackedBits().slice(0, 100).join(', '), + 100 + ); + return { + stringifiedValue: `Binary.fromPackedBits(new Uint8Array([${truncatedSerializedBuffer}]))`, + }; + } + } return { stringifiedValue: `Binary.createFromBase64('${truncate( value.toString('base64'), From 6c66e57c46b62fc80fa84f2f64bae5150f5d674d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kr=C3=A6n=20Hansen?= Date: Wed, 2 Apr 2025 11:51:52 +0200 Subject: [PATCH 2/2] Add tests --- .../src/components/bson-value.spec.tsx | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/packages/compass-components/src/components/bson-value.spec.tsx b/packages/compass-components/src/components/bson-value.spec.tsx index 37a83b056b3..38677522158 100644 --- a/packages/compass-components/src/components/bson-value.spec.tsx +++ b/packages/compass-components/src/components/bson-value.spec.tsx @@ -45,6 +45,21 @@ describe('BSONValue', function () { value: Binary.createFromHexString('3132303d', Binary.SUBTYPE_UUID), expected: "UUID('3132303d')", }, + { + type: 'Binary', + value: Binary.fromInt8Array(new Int8Array([1, 2, 3])), + expected: 'Binary.fromInt8Array(new Int8Array([1, 2, 3]))', + }, + { + type: 'Binary', + value: Binary.fromFloat32Array(new Float32Array([1.1, 2.2, 3.3])), + expected: 'Binary.fromFloat32Array(new Float32Array([1.1, 2.2, 3.3]))', + }, + { + type: 'Binary', + value: Binary.fromPackedBits(new Uint8Array([1, 2, 3])), + expected: 'Binary.fromPackedBits(new Uint8Array([1, 2, 3]))', + }, { type: 'Code', value: new Code('var a = 1', { foo: 2 }),