@@ -402,10 +402,7 @@ function generateDate<T extends Date_>(this: TestDataVectorGenerator, type: T, l
402
402
const data = type . unit === DateUnit . DAY
403
403
? createDate32 ( length , nullBitmap , values )
404
404
: createDate64 ( length , nullBitmap , values ) ;
405
- return {
406
- values : ( ) => values . map ( ( x ) => x == null ? null : new Date ( x ) ) ,
407
- vector : new Vector ( [ makeData ( { type, length, nullCount, nullBitmap, data } ) ] )
408
- } ;
405
+ return { values : ( ) => values , vector : new Vector ( [ makeData ( { type, length, nullCount, nullBitmap, data } ) ] ) } ;
409
406
}
410
407
411
408
function generateTimestamp < T extends Timestamp > ( this : TestDataVectorGenerator , type : T , length = 100 , nullCount = Math . trunc ( length * 0.2 ) ) : GeneratedVector < T > {
@@ -649,6 +646,7 @@ type TypedArrayConstructor =
649
646
650
647
651
648
const rand = Math . random . bind ( Math ) ;
649
+ const randSign = ( ) => rand ( ) > 0.5 ? - 1 : 1 ;
652
650
const randomBytes = ( length : number ) => fillRandom ( Uint8Array , length ) ;
653
651
654
652
const memoize = ( fn : ( ) => any ) => ( ( x ?: any ) => ( ) => x || ( x = fn ( ) ) ) ( ) ;
@@ -661,15 +659,15 @@ function fillRandom<T extends TypedArrayConstructor>(ArrayType: T, length: numbe
661
659
const BPE = ArrayType . BYTES_PER_ELEMENT ;
662
660
const array = new ArrayType ( length ) ;
663
661
const max = ( 2 ** ( 8 * BPE ) ) - 1 ;
664
- for ( let i = - 1 ; ++ i < length ; array [ i ] = rand ( ) * max * ( rand ( ) > 0.5 ? - 1 : 1 ) ) ;
662
+ for ( let i = - 1 ; ++ i < length ; array [ i ] = rand ( ) * max * randSign ( ) ) ;
665
663
return array as InstanceType < T > ;
666
664
}
667
665
668
666
function fillRandomBigInt < T extends ( typeof BigInt64Array ) | ( typeof BigUint64Array ) > ( ArrayType : T , length : number ) {
669
667
const BPE = ArrayType . BYTES_PER_ELEMENT ;
670
668
const array = new ArrayType ( length ) ;
671
669
const max = ( 2 ** ( 8 * BPE ) ) - 1 ;
672
- for ( let i = - 1 ; ++ i < length ; array [ i ] = BigInt ( rand ( ) * max * ( rand ( ) > 0.5 ? - 1 : 1 ) ) ) ;
670
+ for ( let i = - 1 ; ++ i < length ; array [ i ] = BigInt ( rand ( ) * max * randSign ( ) ) ) ;
673
671
return array as InstanceType < T > ;
674
672
}
675
673
@@ -735,47 +733,44 @@ function createVariableWidthBytes(length: number, nullBitmap: Uint8Array, offset
735
733
return bytes ;
736
734
}
737
735
736
+ /**
737
+ * Creates timestamps with the accuracy of days (86400000 millisecond).
738
+ */
738
739
function createDate32 ( length : number , nullBitmap : Uint8Array , values : ( number | null ) [ ] = [ ] ) {
739
740
const data = new Int32Array ( length ) . fill ( Math . trunc ( Date . now ( ) / 86400000 ) ) ;
740
741
iterateBitmap ( length , nullBitmap , ( i , valid ) => {
741
742
if ( ! valid ) {
742
743
data [ i ] = 0 ;
743
744
values [ i ] = null ;
744
745
} else {
745
- data [ i ] = Math . trunc ( data [ i ] + ( rand ( ) * 10000 * ( rand ( ) > 0.5 ? - 1 : 1 ) ) ) ;
746
+ data [ i ] = Math . trunc ( data [ i ] + ( rand ( ) * 10000 * randSign ( ) ) ) ;
746
747
values [ i ] = data [ i ] * 86400000 ;
747
748
}
748
749
} ) ;
749
750
return data ;
750
751
}
751
752
752
753
function createDate64 ( length : number , nullBitmap : Uint8Array , values : ( number | null ) [ ] = [ ] ) {
753
- const data = new Int32Array ( length * 2 ) . fill ( 0 ) ;
754
754
const data32 = createDate32 ( length , nullBitmap , values ) ;
755
- iterateBitmap ( length , nullBitmap , ( i , valid ) => {
756
- if ( valid ) {
757
- const value = data32 [ i ] * 86400000 ;
758
- const hi = Math . trunc ( value / 4294967296 ) ;
759
- const lo = Math . trunc ( value - 4294967296 * hi ) ;
760
- values [ i ] = value ;
761
- data [ i * 2 + 0 ] = lo ;
762
- data [ i * 2 + 1 ] = hi ;
763
- }
764
- } ) ;
765
- return data ;
755
+ return BigInt64Array . from ( data32 , x => BigInt ( x * 86400000 ) ) ;
756
+ }
757
+
758
+ function divideBigInts ( number : bigint , divisor : bigint ) : number {
759
+ return Number ( number / divisor ) + Number ( number % divisor ) / Number ( divisor ) ;
766
760
}
767
761
768
762
function createTimestamp ( length : number , nullBitmap : Uint8Array , multiple : number , values : ( number | null ) [ ] = [ ] ) {
769
- const mult = 86400 * multiple ;
770
- const data = new Int32Array ( length * 2 ) . fill ( 0 ) ;
771
- const data32 = createDate32 ( length , nullBitmap , values ) ;
763
+ const data = new BigInt64Array ( length ) . fill ( 0n ) ;
764
+ const tenYears = 10 * 365 * 24 * 60 * 60 * multiple ;
765
+ const now = Math . trunc ( Date . now ( ) / 1000 * multiple ) ;
772
766
iterateBitmap ( length , nullBitmap , ( i , valid ) => {
773
- if ( valid ) {
774
- const value = data32 [ i ] * mult ;
775
- const hi = Math . trunc ( value / 4294967296 ) ;
776
- const lo = Math . trunc ( value - 4294967296 * hi ) ;
777
- data [ i * 2 + 0 ] = lo ;
778
- data [ i * 2 + 1 ] = hi ;
767
+ if ( ! valid ) {
768
+ data [ i ] = 0n ;
769
+ values [ i ] = null ;
770
+ } else {
771
+ const value = BigInt ( now + Math . trunc ( rand ( ) * randSign ( ) * tenYears ) ) ;
772
+ data [ i ] = value ;
773
+ values [ i ] = divideBigInts ( value * 1000n , BigInt ( multiple ) ) ;
779
774
}
780
775
} ) ;
781
776
return data ;
@@ -788,7 +783,7 @@ function createTime32(length: number, nullBitmap: Uint8Array, multiple: number,
788
783
data [ i ] = 0 ;
789
784
values [ i ] = null ;
790
785
} else {
791
- values [ i ] = data [ i ] = ( ( 1000 * rand ( ) ) | 0 * multiple ) * ( rand ( ) > 0.5 ? - 1 : 1 ) ;
786
+ values [ i ] = data [ i ] = ( ( 1000 * rand ( ) ) | 0 * multiple ) * randSign ( ) ;
792
787
}
793
788
} ) ;
794
789
return data ;
0 commit comments