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

feat: switch timestamp representation to int64 usec #1332

Merged
merged 6 commits into from
Feb 21, 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
fix: handle negative unix timestamp values
  • Loading branch information
alvarowolfx committed Feb 20, 2024
commit 5bbc6d96213ad8c2e7aab3640938457330fe613b
4 changes: 2 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 @@ -2219,10 +2219,10 @@
} else {
pd = new PreciseDate(BigInt(value) * BigInt(1000));
}
} else if (value > 0) {
} else if (value) {
pd = new PreciseDate(BigInt(value) * BigInt(1000));
} else {
// Nan or negative values - invalid dates
// Nan or 0 - invalid dates
pd = new PreciseDate(value);
alvarowolfx marked this conversation as resolved.
Show resolved Hide resolved
}
// to keep backward compatibility, only converts with microsecond
Expand Down
17 changes: 14 additions & 3 deletions test/bigquery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -850,8 +850,10 @@ 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 @@ -882,14 +884,23 @@ describe('BigQuery', () => {
});

it('should accept a number in microseconds', () => {
const ms = INPUT_PRECISE_DATE.valueOf(); // milliseconds
const us = ms * 1000 + INPUT_PRECISE_DATE.getMicroseconds(); // 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);

const usStr = `${us}`;
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', () => {
Expand Down