From 7612817ec6778749f2a3b24143843768fef7d59c Mon Sep 17 00:00:00 2001 From: Dominik Moritz Date: Thu, 21 Mar 2024 16:36:59 -0400 Subject: [PATCH] GH-40718: [JS] Fix set visitor in vectors for js dates --- js/src/visitor/set.ts | 14 +++++++------- js/test/unit/vector/date-vector-tests.ts | 11 ++++++++++- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/js/src/visitor/set.ts b/js/src/visitor/set.ts index eb1f280964c8e..5dc42283c36f0 100644 --- a/js/src/visitor/set.ts +++ b/js/src/visitor/set.ts @@ -108,21 +108,21 @@ function wrapSet(fn: (data: Data, _1: any, _2: any) => vo } /** @ignore */ -export const setEpochMsToDays = (data: Int32Array, index: number, epochMs: number) => { data[index] = Math.trunc(epochMs / 86400000); }; +export const setEpochMsToDays = (data: Int32Array, index: number, epochMs: number) => { data[index] = Math.floor(epochMs / 86400000); }; /** @ignore */ export const setEpochMsToMillisecondsLong = (data: Int32Array, index: number, epochMs: number) => { - data[index] = Math.trunc(epochMs % 4294967296); - data[index + 1] = Math.trunc(epochMs / 4294967296); + data[index] = Math.floor(epochMs % 4294967296); + data[index + 1] = Math.floor(epochMs / 4294967296); }; /** @ignore */ export const setEpochMsToMicrosecondsLong = (data: Int32Array, index: number, epochMs: number) => { - data[index] = Math.trunc((epochMs * 1000) % 4294967296); - data[index + 1] = Math.trunc((epochMs * 1000) / 4294967296); + data[index] = Math.floor((epochMs * 1000) % 4294967296); + data[index + 1] = Math.floor((epochMs * 1000) / 4294967296); }; /** @ignore */ export const setEpochMsToNanosecondsLong = (data: Int32Array, index: number, epochMs: number) => { - data[index] = Math.trunc((epochMs * 1000000) % 4294967296); - data[index + 1] = Math.trunc((epochMs * 1000000) / 4294967296); + data[index] = Math.floor((epochMs * 1000000) % 4294967296); + data[index + 1] = Math.floor((epochMs * 1000000) / 4294967296); }; /** @ignore */ diff --git a/js/test/unit/vector/date-vector-tests.ts b/js/test/unit/vector/date-vector-tests.ts index d658ac1d2767d..f8b4c1c7976d2 100644 --- a/js/test/unit/vector/date-vector-tests.ts +++ b/js/test/unit/vector/date-vector-tests.ts @@ -15,7 +15,7 @@ // specific language governing permissions and limitations // under the License. -import { DateDay, DateMillisecond, RecordBatchReader, Table } from 'apache-arrow'; +import { DateDay, DateMillisecond, RecordBatchReader, Table, vectorFromArray } from 'apache-arrow'; describe(`DateVector`, () => { it('returns days since the epoch as correct JS Dates', () => { @@ -27,6 +27,7 @@ describe(`DateVector`, () => { expect(date).toEqual(millis === null ? null : new Date(millis!)); } }); + it('returns millisecond longs since the epoch as correct JS Dates', () => { const table = new Table(RecordBatchReader.from(test_data)); const expectedMillis = expectedMillis64(); @@ -36,6 +37,14 @@ describe(`DateVector`, () => { expect(date).toEqual(millis === null ? null : new Date(millis!)); } }); + + it('returns the same date that was in the vector', () => { + const dates = [new Date(1950, 1, 0)]; + const vec = vectorFromArray(dates); + for (const date of vec) { + expect(date).toEqual(dates.shift()); + } + }); }); const expectedMillis32 = () => [