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 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 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 @@ -640,7 +640,8 @@
break;
}
case 'TIMESTAMP': {
value = BigQuery.timestamp(value);
const pd = new PreciseDate(BigInt(value) * BigInt(1000));
value = BigQuery.timestamp(pd);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is similar on how it was working before this PR: https://github.com/googleapis/nodejs-bigquery/pull/1192/files

break;
}
case 'GEOGRAPHY': {
Expand Down Expand Up @@ -2217,13 +2218,15 @@
if (/^\d{4}-\d{1,2}-\d{1,2}/.test(value)) {
pd = new PreciseDate(value);
} else {
pd = new PreciseDate(BigInt(value) * BigInt(1000));
const floatValue = Number.parseFloat(value);
if (!Number.isNaN(floatValue)) {
pd = this.fromFloatValue_(floatValue);
} else {
pd = new PreciseDate(value);
}
}
} else if (value) {
pd = new PreciseDate(BigInt(value) * BigInt(1000));
} else {
// Nan or 0 - invalid dates
pd = new PreciseDate(value);
pd = this.fromFloatValue_(value);
}
// to keep backward compatibility, only converts with microsecond
// precision if needed.
Expand All @@ -2233,6 +2236,15 @@
this.value = new Date(pd.getTime()).toJSON();
}
}

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
36 changes: 12 additions & 24 deletions test/bigquery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ describe('BigQuery', () => {
f: [
{v: '3'},
{v: 'Milo'},
{v: now.valueOf() * 1000},
{v: now.valueOf() * 1000}, // int64 microseconds
{v: 'false'},
{v: 'true'},
{v: '5.222330009847'},
Expand Down Expand Up @@ -523,7 +523,7 @@ describe('BigQuery', () => {
id: 3,
name: 'Milo',
dob: {
input: now.valueOf() * 1000,
input: new PreciseDate(BigInt(now.valueOf()) * BigInt(1_000_000)),
type: 'fakeTimestamp',
},
has_claws: false,
Expand Down Expand Up @@ -850,10 +850,8 @@ describe('BigQuery', () => {
describe('timestamp', () => {
const INPUT_STRING = '2016-12-06T12:00:00.000Z';
const INPUT_STRING_MICROS = '2016-12-06T12:00:00.123456Z';
const INPUT_STRING_NEGATIVE = '1969-12-25T00:00:00.000Z';
const INPUT_DATE = new Date(INPUT_STRING);
const INPUT_PRECISE_DATE = new PreciseDate(INPUT_STRING_MICROS);
const INPUT_PRECISE_NEGATIVE_DATE = new PreciseDate(INPUT_STRING_NEGATIVE);
const EXPECTED_VALUE = INPUT_DATE.toJSON();
const EXPECTED_VALUE_MICROS = INPUT_PRECISE_DATE.toISOString();

Expand Down Expand Up @@ -883,31 +881,21 @@ describe('BigQuery', () => {
assert.strictEqual(timestamp.value, EXPECTED_VALUE);
});

it('should accept a number in microseconds', () => {
let ms = INPUT_PRECISE_DATE.valueOf(); // milliseconds
let us = ms * 1000 + INPUT_PRECISE_DATE.getMicroseconds(); // microseconds
let timestamp = bq.timestamp(us);
assert.strictEqual(timestamp.value, EXPECTED_VALUE_MICROS);

let usStr = `${us}`;
timestamp = bq.timestamp(usStr);
assert.strictEqual(timestamp.value, EXPECTED_VALUE_MICROS);

ms = INPUT_PRECISE_NEGATIVE_DATE.valueOf();
us = ms * 1000;
timestamp = bq.timestamp(us);
assert.strictEqual(timestamp.value, INPUT_STRING_NEGATIVE);

usStr = `${us}`;
timestamp = bq.timestamp(usStr);
assert.strictEqual(timestamp.value, INPUT_STRING_NEGATIVE);
});

it('should accept a string with microseconds', () => {
const timestamp = bq.timestamp(INPUT_STRING_MICROS);
assert.strictEqual(timestamp.value, EXPECTED_VALUE_MICROS);
});

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());

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

it('should accept a Date object', () => {
const timestamp = bq.timestamp(INPUT_DATE);
assert.strictEqual(timestamp.value, EXPECTED_VALUE);
Expand Down
Loading