Skip to content

Commit 6485753

Browse files
adjust types based on ongoing file tests
1 parent 97b1475 commit 6485753

File tree

1 file changed

+27
-59
lines changed

1 file changed

+27
-59
lines changed

lib/types.ts

Lines changed: 27 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ interface INTERVAL {
2121
milliseconds: number;
2222
}
2323

24+
interface TIME {
25+
value: string | bigint | number;
26+
unit: 'MILLIS' | 'MICROS' | 'NANOS';
27+
isAdjustedToUTC: boolean;
28+
}
29+
2430
export function getParquetTypeDataObject(
2531
type: ParquetType,
2632
field?: ParquetField | Options | FieldDefinition
@@ -46,28 +52,26 @@ export function getParquetTypeDataObject(
4652
toPrimitive: toPrimitive_INT64,
4753
};
4854
}
49-
} else if (field?.logicalType?.TIME && (type === 'INT64' || type === 'INT32')) {
50-
const isAdjustedToUTC = field.logicalType.TIME.isAdjustedToUTC;
55+
} else if (field?.logicalType?.TIME) {
5156
const unit = field.logicalType.TIME.unit;
52-
5357
if (unit.MILLIS) {
5458
return {
5559
originalType: 'TIME_MILLIS',
5660
primitiveType: 'INT32',
57-
toPrimitive: isAdjustedToUTC ? toPrimitive_TIME_MILLIS_UTC : toPrimitive_TIME_MILLIS_LOCAL,
61+
toPrimitive: toPrimitive_TIME,
5862
};
5963
}
6064
if (unit.MICROS) {
6165
return {
6266
originalType: 'TIME_MICROS',
6367
primitiveType: 'INT64',
64-
toPrimitive: isAdjustedToUTC ? toPrimitive_TIME_MICROS_UTC : toPrimitive_TIME_MICROS_LOCAL,
68+
toPrimitive: toPrimitive_TIME,
6569
};
6670
}
6771
if (unit.NANOS) {
6872
return {
6973
primitiveType: 'INT64',
70-
toPrimitive: isAdjustedToUTC ? toPrimitive_TIME_NANOS_UTC : toPrimitive_TIME_NANOS_LOCAL,
74+
toPrimitive: toPrimitive_TIME,
7175
};
7276
}
7377
throw new Error('TIME type must have a valid unit (MILLIS, MICROS, NANOS).');
@@ -585,63 +589,27 @@ function checkValidValue(lowerRange: number | bigint, upperRange: number | bigin
585589
}
586590
}
587591

588-
/**
589-
* Convert a TIME value in MILLIS to its UTC representation.
590-
* @param value The time value.
591-
*/
592-
function toPrimitive_TIME_MILLIS_UTC(value: number | string): number {
593-
return typeof value === 'string' ? Number(value) : value;
594-
}
595-
596-
/**
597-
* Convert a TIME value in MILLIS to its local time representation.
598-
* @param value The time value.
599-
*/
600-
function toPrimitive_TIME_MILLIS_LOCAL(value: number | string): number {
601-
const millis = typeof value === 'string' ? Number(value) : value;
602-
return Number(adjustToLocalTimestamp(BigInt(millis), { MILLIS: true }));
603-
}
604-
605-
/**
606-
* Convert a TIME value in MICROS to its UTC representation.
607-
* @param value The time value.
608-
*/
609-
function toPrimitive_TIME_MICROS_UTC(value: bigint | string): bigint {
610-
return BigInt(value);
611-
}
612-
613-
/**
614-
* Convert a TIME value in MICROS to its local time representation.
615-
* @param value The time value.
616-
*/
617-
function toPrimitive_TIME_MICROS_LOCAL(value: bigint | string): bigint {
618-
const micros = BigInt(value);
619-
return adjustToLocalTimestamp(micros, { MICROS: true });
620-
}
592+
function toPrimitive_TIME(time: TIME): bigint | number {
593+
const { value, unit, isAdjustedToUTC } = time;
621594

622-
/**
623-
* Convert a TIME value in NANOS to its UTC representation.
624-
* @param value The time value.
625-
*/
626-
function toPrimitive_TIME_NANOS_UTC(value: bigint | string): bigint {
627-
return BigInt(value);
628-
}
595+
const timeValue = typeof value === 'string' ? BigInt(value) : BigInt(value);
629596

630-
/**
631-
* Convert a TIME value in NANOS to its local time representation.
632-
* @param value The time value.
633-
*/
634-
function toPrimitive_TIME_NANOS_LOCAL(value: bigint | string): bigint {
635-
const nanos = BigInt(value);
636-
return adjustToLocalTimestamp(nanos, { NANOS: true });
597+
if (isAdjustedToUTC) {
598+
return unit === 'MILLIS' ? Number(timeValue) : timeValue;
599+
} else {
600+
switch (unit) {
601+
case 'MILLIS':
602+
return Number(adjustToLocalTimestamp(timeValue, { MILLIS: true }));
603+
case 'MICROS':
604+
return adjustToLocalTimestamp(timeValue, { MICROS: true });
605+
case 'NANOS':
606+
return adjustToLocalTimestamp(timeValue, { NANOS: true });
607+
default:
608+
throw new Error(`Unsupported time unit: ${unit}`);
609+
}
610+
}
637611
}
638612

639-
/**
640-
* Adjust the timestamp to local time based on the unit (MILLIS, MICROS, NANOS).
641-
* @param timestamp The timestamp to adjust.
642-
* @param unit The unit of the timestamp.
643-
* @returns The adjusted timestamp.
644-
*/
645613
function adjustToLocalTimestamp(
646614
timestamp: bigint,
647615
unit: { MILLIS?: boolean; MICROS?: boolean; NANOS?: boolean }

0 commit comments

Comments
 (0)