Skip to content

Commit

Permalink
generate timestamps with meaningful accuracy
Browse files Browse the repository at this point in the history
  • Loading branch information
domoritz committed Apr 15, 2024
1 parent aca95f0 commit 8c86cf9
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions js/test/generate-test-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,7 @@ type TypedArrayConstructor =


const rand = Math.random.bind(Math);
const randSign = () => rand() > 0.5 ? -1 : 1;
const randomBytes = (length: number) => fillRandom(Uint8Array, length);

const memoize = (fn: () => any) => ((x?: any) => () => x || (x = fn()))();
Expand All @@ -658,15 +659,15 @@ function fillRandom<T extends TypedArrayConstructor>(ArrayType: T, length: numbe
const BPE = ArrayType.BYTES_PER_ELEMENT;
const array = new ArrayType(length);
const max = (2 ** (8 * BPE)) - 1;
for (let i = -1; ++i < length; array[i] = rand() * max * (rand() > 0.5 ? -1 : 1));
for (let i = -1; ++i < length; array[i] = rand() * max * randSign());
return array as InstanceType<T>;
}

function fillRandomBigInt<T extends (typeof BigInt64Array) | (typeof BigUint64Array)>(ArrayType: T, length: number) {
const BPE = ArrayType.BYTES_PER_ELEMENT;
const array = new ArrayType(length);
const max = (2 ** (8 * BPE)) - 1;
for (let i = -1; ++i < length; array[i] = BigInt(rand() * max * (rand() > 0.5 ? -1 : 1)));
for (let i = -1; ++i < length; array[i] = BigInt(rand() * max * randSign()));
return array as InstanceType<T>;
}

Expand Down Expand Up @@ -732,38 +733,44 @@ function createVariableWidthBytes(length: number, nullBitmap: Uint8Array, offset
return bytes;
}

/**
* Creates timestamps with the accuracy of days (86400000 millisecond).
*/
function createDate32(length: number, nullBitmap: Uint8Array, values: (number | null)[] = []) {
const data = new Int32Array(length).fill(Math.trunc(Date.now() / 86400000));
iterateBitmap(length, nullBitmap, (i, valid) => {
if (!valid) {
data[i] = 0;
values[i] = null;
} else {
data[i] = Math.trunc(data[i] + (rand() * 10000 * (rand() > 0.5 ? -1 : 1)));
data[i] = Math.trunc(data[i] + (rand() * 10000 * randSign()));
values[i] = data[i] * 86400000;
}
});
return data;
}

function createDate64(length: number, nullBitmap: Uint8Array, values: (number | null)[] = []) {
const data = new BigInt64Array(length).fill(0n);
const data32 = createDate32(length, nullBitmap, values);
iterateBitmap(length, nullBitmap, (i, valid) => {
if (valid) {
data[i] = BigInt(data32[i] * 86400000);
}
});
return data;
return BigInt64Array.from(data32, x => BigInt(x * 86400000));
}

function divideBigInts(number: bigint, divisor: bigint): number {
return Number(number / divisor) + Number(number % divisor) / Number(divisor);
}

function createTimestamp(length: number, nullBitmap: Uint8Array, multiple: number, values: (number | null)[] = []) {
const mult = 86400 * multiple;
const data = new BigInt64Array(length).fill(0n);
const data32 = createDate32(length, nullBitmap, values);
const tenYears = 10 * 365 * 24 * 60 * 60 * multiple;
const now = Math.trunc(Date.now() / 1000 * multiple);
iterateBitmap(length, nullBitmap, (i, valid) => {
if (valid) {
data[i] = BigInt(data32[i] * mult);
if (!valid) {
data[i] = 0n;
values[i] = null;
} else {
const value = BigInt(now + Math.trunc(rand() * randSign() * tenYears));
data[i] = value;
values[i] = divideBigInts(value * 1000n, BigInt(multiple));
}
});
return data;
Expand All @@ -776,7 +783,7 @@ function createTime32(length: number, nullBitmap: Uint8Array, multiple: number,
data[i] = 0;
values[i] = null;
} else {
values[i] = data[i] = ((1000 * rand()) | 0 * multiple) * (rand() > 0.5 ? -1 : 1);
values[i] = data[i] = ((1000 * rand()) | 0 * multiple) * randSign();
}
});
return data;
Expand Down

0 comments on commit 8c86cf9

Please sign in to comment.