Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ARROW-18247: [JS] fix: RangeError crash in Vector.toArray() #14587

Merged
merged 5 commits into from
Dec 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions js/src/vector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,9 @@ export class Vector<T extends DataType = any> {
switch (data.length) {
case 0: return new ArrayType();
case 1: return data[0].values.subarray(0, length * stride);
default: return data.reduce((memo, { values }) => {
memo.array.set(values, memo.offset);
memo.offset += values.length;
default: return data.reduce((memo, { values, length: chunk_length }) => {
memo.array.set(values.subarray(0, chunk_length * stride), memo.offset);
memo.offset += chunk_length * stride;
return memo;
}, { array: new ArrayType(length * stride), offset: 0 }).array;
}
Expand Down
30 changes: 29 additions & 1 deletion js/test/unit/vector/vector-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// under the License.

import {
Bool, DateDay, DateMillisecond, Dictionary, Float64, Int32, List, makeVector, Struct, Utf8, util, Vector, vectorFromArray
Bool, DateDay, DateMillisecond, Dictionary, Float64, Int32, List, makeVector, Struct, Timestamp, TimeUnit, Utf8, util, Vector, vectorFromArray
} from 'apache-arrow';

describe(`makeVectorFromArray`, () => {
Expand Down Expand Up @@ -211,6 +211,34 @@ describe(`ListVector`, () => {
});
});

describe(`toArray()`, () => {
test(`when some data blobs have been padded`, () => {
const d1 = vectorFromArray([...new Array(16).keys()]);
const d2 = vectorFromArray([...new Array(10).keys()]);

// Padding has been added
expect(d2.length).toBeLessThan(d2.data[0].buffers[1].length);

const vector = new Vector([d1, d2]);

// This used to crash with "RangeError: offset is out of bounds"
// https://issues.apache.org/jira/browse/ARROW-18247
const array = vector.toArray();
expect(array).toHaveLength(26);
});

test(`when stride is 2`, () => {
let d1 = vectorFromArray([0, 1, 2], new Timestamp(TimeUnit.MILLISECOND)).data[0];
let d2 = vectorFromArray([3, 4, 5], new Timestamp(TimeUnit.MILLISECOND)).data[0];

const vector = new Vector([d1, d2]);

let array = Array.from(vector.toArray());
expect(array).toHaveLength(6 * 2);
expect(Array.from(array)).toMatchObject([0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0]);
});
});

// Creates some basic tests for the given vector.
// Verifies that:
// - `get` and the native iterator return the same data as `values`
Expand Down