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
Prev Previous commit
Next Next commit
fix: restore BigQueryTimestamp float parsing and convert int64 timest…
…amp beforehand
  • Loading branch information
alvarowolfx committed Mar 13, 2024
commit 8a0738fb20ea3cc0dafee6144a254407c2e14b33
31 changes: 9 additions & 22 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 = this.fromNumber_(value);
const floatValue = Number.parseFloat(value);
if (!Number.isNaN(floatValue)) {
pd = this.fromFloatValue_(floatValue);
} else {
pd = new PreciseDate(value);
}
}
} else if (value) {
pd = this.fromNumber_(value);
} 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 @@ -2234,22 +2237,6 @@
}
}

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
Expand Down
31 changes: 2 additions & 29 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 @@ -893,31 +891,6 @@ describe('BigQuery', () => {
assert.strictEqual(timestamp.value, d.toJSON());
});

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 Date object', () => {
const timestamp = bq.timestamp(INPUT_DATE);
assert.strictEqual(timestamp.value, EXPECTED_VALUE);
Expand Down