Skip to content

Commit 1c04474

Browse files
authored
Infer correct bitWidth for Time type based on unit (#51)
1 parent 63ebe4c commit 1c04474

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

src/data-types.js

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,9 @@ export const dateMillisecond = () => date(DateUnit.MILLISECOND);
308308

309309
/**
310310
* Return a Time data type instance, stored in one of four *unit*s: seconds,
311-
* milliseconds, microseconds or nanoseconds. The integer *bitWidth* depends
312-
* on the *unit* and must be 32 bits for seconds and milliseconds or 64 bits
313-
* for microseconds and nanoseconds. The allowed values are between 0
311+
* milliseconds, microseconds or nanoseconds. The integer *bitWidth* is
312+
* inferred from the *unit* and is 32 bits for seconds and milliseconds or
313+
* 64 bits for microseconds and nanoseconds. The allowed values are between 0
314314
* (inclusive) and 86400 (=24*60*60) seconds (exclusive), adjusted for the
315315
* time unit (for example, up to 86400000 exclusive for the
316316
* `DateUnit.MILLISECOND` unit.
@@ -321,36 +321,38 @@ export const dateMillisecond = () => date(DateUnit.MILLISECOND);
321321
* @param {TimeUnit_} unit The time unit.
322322
* One of `TimeUnit.SECOND`, `TimeUnit.MILLISECOND` (default),
323323
* `TimeUnit.MICROSECOND`, or `TimeUnit.NANOSECOND`.
324-
* @param {32 | 64} bitWidth The time bit width. One of `32` (for seconds
325-
* and milliseconds) or `64` (for microseconds and nanoseconds).
326324
* @returns {TimeType} The time data type.
327325
*/
328-
export const time = (unit = TimeUnit.MILLISECOND, bitWidth = 32) => ({
329-
typeId: Type.Time,
330-
unit: checkOneOf(unit, TimeUnit),
331-
bitWidth: checkOneOf(bitWidth, [32, 64]),
332-
values: bitWidth === 32 ? int32Array : int64Array
333-
});
326+
export const time = (unit = TimeUnit.MILLISECOND) => {
327+
const checked = checkOneOf(unit, TimeUnit);
328+
const bitWidth = checked === TimeUnit.SECOND || checked === TimeUnit.MILLISECOND ? 32 : 64;
329+
return {
330+
typeId: Type.Time,
331+
unit: checked,
332+
bitWidth,
333+
values: bitWidth === 32 ? int32Array : int64Array
334+
};
335+
};
334336
/**
335337
* Return a Time data type instance, represented as seconds.
336338
* @returns {TimeType} The time data type.
337339
*/
338-
export const timeSecond = () => time(TimeUnit.SECOND, 32);
340+
export const timeSecond = () => time(TimeUnit.SECOND);
339341
/**
340342
* Return a Time data type instance, represented as milliseconds.
341343
* @returns {TimeType} The time data type.
342344
*/
343-
export const timeMillisecond = () => time(TimeUnit.MILLISECOND, 32);
345+
export const timeMillisecond = () => time(TimeUnit.MILLISECOND);
344346
/**
345347
* Return a Time data type instance, represented as microseconds.
346348
* @returns {TimeType} The time data type.
347349
*/
348-
export const timeMicrosecond = () => time(TimeUnit.MICROSECOND, 64);
350+
export const timeMicrosecond = () => time(TimeUnit.MICROSECOND);
349351
/**
350352
* Return a Time data type instance, represented as nanoseconds.
351353
* @returns {TimeType} The time data type.
352354
*/
353-
export const timeNanosecond = () => time(TimeUnit.NANOSECOND, 64);
355+
export const timeNanosecond = () => time(TimeUnit.NANOSECOND);
354356

355357
/**
356358
* Return a Timestamp data type instance. Timestamp values are 64-bit signed

test/column-from-array-test.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import assert from 'node:assert';
2-
import { IntervalUnit, TimeUnit, UnionMode, binary, bool, columnFromArray, dateDay, dateMillisecond, decimal, dictionary, duration, field, fixedSizeBinary, fixedSizeList, float16, float32, float64, int16, int32, int64, int8, interval, largeBinary, largeList, largeUtf8, list, map, nullType, runEndEncoded, struct, timeMicrosecond, timeMillisecond, timeNanosecond, timeSecond, timestamp, uint16, uint32, uint64, uint8, union, utf8 } from '../src/index.js';
2+
import { IntervalUnit, TimeUnit, UnionMode, binary, bool, columnFromArray, dateDay, dateMillisecond, decimal, dictionary, duration, field, fixedSizeBinary, fixedSizeList, float16, float32, float64, int16, int32, int64, int8, interval, largeBinary, largeList, largeUtf8, list, map, nullType, runEndEncoded, struct, time, timeMicrosecond, timeMillisecond, timeNanosecond, timeSecond, timestamp, uint16, uint32, uint64, uint8, union, utf8 } from '../src/index.js';
33
import { isTypedArray } from '../src/util/arrays.js';
44

55
function test(values, type, options) {
@@ -153,6 +153,13 @@ describe('columnFromArray', () => {
153153
test(bigints.concat(86400000000000n), timeNanosecond(), opt);
154154
});
155155

156+
it('infers correct bitWidth for time units', () => {
157+
assert.strictEqual(time(TimeUnit.SECOND).bitWidth, 32);
158+
assert.strictEqual(time(TimeUnit.MILLISECOND).bitWidth, 32);
159+
assert.strictEqual(time(TimeUnit.MICROSECOND).bitWidth, 64);
160+
assert.strictEqual(time(TimeUnit.NANOSECOND).bitWidth, 64);
161+
});
162+
156163
it('builds timestamp columns', () => {
157164
const dates = [
158165
new Date(Date.UTC(2000, 0, 1)),

0 commit comments

Comments
 (0)