Skip to content

Commit

Permalink
apacheGH-40718: [JS] Fix set visitor in vectors for js dates
Browse files Browse the repository at this point in the history
  • Loading branch information
domoritz committed Mar 21, 2024
1 parent 27cea44 commit a197751
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
14 changes: 7 additions & 7 deletions js/src/visitor/set.ts
Expand Up @@ -108,21 +108,21 @@ function wrapSet<T extends DataType>(fn: (data: Data<T>, _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 */
Expand Down
11 changes: 10 additions & 1 deletion js/test/unit/vector/date-vector-tests.ts
Expand Up @@ -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', () => {
Expand All @@ -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();
Expand All @@ -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 = () => [
Expand Down

0 comments on commit a197751

Please sign in to comment.