trxcllnt commented on code in PR #385:
URL: https://github.com/apache/arrow-js/pull/385#discussion_r2887569391


##########
src/factories.ts:
##########
@@ -80,9 +80,35 @@ export function vectorFromArray<T extends 
dtypes.DataType>(data: DataProps<T>):
 export function vectorFromArray<T extends TypedArray | BigIntArray | readonly 
unknown[]>(data: T): Vector<ArrayDataType<T>>;
 
 export function vectorFromArray(init: any, type?: dtypes.DataType) {
-    if (init instanceof Data || init instanceof Vector || init.type instanceof 
dtypes.DataType || ArrayBuffer.isView(init)) {
+    if (init instanceof Data || init instanceof Vector || init.type instanceof 
dtypes.DataType) {
         return makeVector(init as any);
     }
+    if (ArrayBuffer.isView(init) && !type) {
+        return makeVector(init as any);
+    }
+    if (ArrayBuffer.isView(init) && type) {
+        // Validate BigInt/number boundary
+        const isBigIntInput = init instanceof BigInt64Array || init instanceof 
BigUint64Array;
+        const isBigIntTarget = type.ArrayType === BigInt64Array || 
type.ArrayType === BigUint64Array;
+        if (isBigIntInput && !isBigIntTarget) {
+            throw new TypeError(
+                `Cannot convert BigInt input to ${type}. BigInt arrays can 
only target BigInt-based types (e.g. Int64, Uint64).`
+            );
+        }
+        if (!isBigIntInput && isBigIntTarget) {
+            throw new TypeError(
+                `Cannot convert non-BigInt input to ${type}. ${type} requires 
BigInt values.`
+            );
+        }
+
+        // Fast path: direct TypedArray conversion for Int and Float types
+        if (dtypes.DataType.isInt(type) || dtypes.DataType.isFloat(type)) {
+            const data = init.constructor === type.ArrayType
+                ? init                                  // zero-copy, same 
TypedArray type
+                : new (type.ArrayType as any)(init);    // standard JS 
TypedArray conversion

Review Comment:
   I believe this is redundant, as this will happen in `makeVector` via 
`makeData` calling `toArrayBufferView` on the incoming typed array 
[here](https://github.com/apache/arrow-js/blob/ed42d662c221f3748d75566bfa1ce3f13ee026a6/src/data.ts#L355)
 and 
[here](https://github.com/apache/arrow-js/blob/ed42d662c221f3748d75566bfa1ce3f13ee026a6/src/data.ts#L362).
   
   `toArrayBufferView` returns a zero-copy ArrayBufferView of the desired type 
([here](https://github.com/apache/arrow-js/blob/ed42d662c221f3748d75566bfa1ce3f13ee026a6/src/util/buffer.ts#L111-L112))
 when given an ArrayBuffer (or any ArrayBufferView).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to