Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: BigQueryTimestamp should keep accepting floats #1339

Merged
merged 5 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Next Next commit
fix: BigQueryTimestamp should keep accepting floats
  • Loading branch information
alvarowolfx committed Mar 11, 2024
commit 9ebcea55d705ae3daebda5931855ec15df5ffb16
29 changes: 27 additions & 2 deletions src/bigquery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,17 +319,17 @@
location?: string;
private _universeDomain: string;

createQueryStream(options?: Query | string): ResourceStream<RowMetadata> {

Check warning on line 322 in src/bigquery.ts

View workflow job for this annotation

GitHub Actions / lint

'options' is defined but never used
// placeholder body, overwritten in constructor
return new ResourceStream<RowMetadata>({}, () => {});
}

getDatasetsStream(options?: GetDatasetsOptions): ResourceStream<Dataset> {

Check warning on line 327 in src/bigquery.ts

View workflow job for this annotation

GitHub Actions / lint

'options' is defined but never used
// placeholder body, overwritten in constructor
return new ResourceStream<Dataset>({}, () => {});
}

getJobsStream(options?: GetJobsOptions): ResourceStream<Job> {

Check warning on line 332 in src/bigquery.ts

View workflow job for this annotation

GitHub Actions / lint

'options' is defined but never used
// placeholder body, overwritten in constructor
return new ResourceStream<Job>({}, () => {});
}
Expand Down Expand Up @@ -2217,10 +2217,10 @@
if (/^\d{4}-\d{1,2}-\d{1,2}/.test(value)) {
pd = new PreciseDate(value);
} else {
pd = new PreciseDate(BigInt(value) * BigInt(1000));
pd = this.fromNumber_(value);
}
} else if (value) {
pd = new PreciseDate(BigInt(value) * BigInt(1000));
pd = this.fromNumber_(value);
} else {
// Nan or 0 - invalid dates
pd = new PreciseDate(value);
Expand All @@ -2233,6 +2233,31 @@
this.value = new Date(pd.getTime()).toJSON();
}
}

fromNumber_(value: number | string): PreciseDate {
let numValue;
if (typeof value === 'string') {
numValue = Number.parseFloat(value);
if (Number.isNaN(numValue)) {
return new PreciseDate(numValue); // invalid date
}
} else {
numValue = value;
}
if (Number.isInteger(numValue)) {
return new PreciseDate(BigInt(numValue) * BigInt(1000));
}
return this.fromFloatValue_(numValue);
}

fromFloatValue_(value: number): PreciseDate {
const secs = Math.trunc(value);
// Timestamps in BigQuery have microsecond precision, so we must
// return a round number of microseconds.
const micros = Math.trunc((value - secs) * 1e6 + 0.5);
const pd = new PreciseDate([secs, micros * 1000]);
return pd;
}
}

/**
Expand Down
10 changes: 10 additions & 0 deletions test/bigquery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,16 @@
assert.strictEqual(timestamp.value, EXPECTED_VALUE);
});

it('should accept a float number', () => {
const d = new Date();
const f = d.valueOf() / 1000; // float seconds
let timestamp = bq.timestamp(f);
assert.strictEqual(timestamp.value, d.toJSON());

Check failure on line 890 in test/bigquery.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `······`

Check failure on line 890 in test/bigquery.ts

View workflow job for this annotation

GitHub Actions / lint

Trailing spaces not allowed

timestamp = bq.timestamp(f.toString());
assert.strictEqual(timestamp.value, d.toJSON());

Check failure on line 893 in test/bigquery.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `······`

Check failure on line 893 in test/bigquery.ts

View workflow job for this annotation

GitHub Actions / lint

Trailing spaces not allowed
});

it('should accept a number in microseconds', () => {
let ms = INPUT_PRECISE_DATE.valueOf(); // milliseconds
let us = ms * 1000 + INPUT_PRECISE_DATE.getMicroseconds(); // microseconds
Expand Down