Skip to content
Merged
Changes from all 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
155 changes: 155 additions & 0 deletions handwritten/bigquery/system-test/bigquery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1950,6 +1950,161 @@ describe('BigQuery', () => {
},
);
});
describe('High Precision Query System Tests', () => {
let bigquery: BigQuery;
const expectedTsValueNanoseconds = '2023-01-01T12:00:00.123456000Z';
const expectedTsValuePicoseconds =
'2023-01-01T12:00:00.123456789123Z';
const expectedErrorMessage =
'Cannot specify both timestamp_as_int and timestamp_output_format.';

before(() => {
bigquery = new BigQuery();
});

const testCases = [
{
name: 'TOF: FLOAT64, UI64: true (error)',
timestampOutputFormat: 'FLOAT64',
useInt64Timestamp: true,
expectedTsValue: undefined,
expectedError: expectedErrorMessage,
},
{
name: 'TOF: omitted, UI64: omitted (default INT64)',
timestampOutputFormat: undefined,
useInt64Timestamp: undefined,
expectedTsValue: expectedTsValuePicoseconds,
},
{
name: 'TOF: omitted, UI64: true',
timestampOutputFormat: undefined,
useInt64Timestamp: true,
expectedTsValue: expectedTsValueNanoseconds,
},
];

testCases.forEach(testCase => {
it(`should handle ${testCase.name}`, async () => {
/*
The users use the new TIMESTAMP(12) type to indicate they want to
opt in to using timestampPrecision=12. The reason is that some queries
like `SELECT CAST(? as TIMESTAMP(12))` will fail if we set
timestampPrecision=12 and we don't want this code change to affect
existing users. Queries using TIMESTAMP_ADD are another example.
*/
const query = {
query: 'SELECT @r AS ts',
params: {
r: bigquery.timestamp('2023-01-01T12:00:00.123456789123Z'),
},
types: {
r: 'TIMESTAMP(12)',
},
};

const options: any = {};
if (testCase.timestampOutputFormat !== undefined) {
options['formatOptions.timestampOutputFormat'] =
testCase.timestampOutputFormat;
}
if (testCase.useInt64Timestamp !== undefined) {
options['formatOptions.useInt64Timestamp'] =
testCase.useInt64Timestamp;
}

try {
const [rows] = await bigquery.query(query, options);
if (testCase.expectedError) {
assert.fail(
`Query should have failed for ${testCase.name}, but succeeded`,
);
}
assert.ok(rows.length > 0);
assert.ok(rows[0].ts.value !== undefined);
assert.strictEqual(
rows[0].ts.value,
testCase.expectedTsValue,
);
} catch (err: any) {
if (!testCase.expectedError) {
throw err;
}

const message = err.message;
assert.strictEqual(
message,
testCase.expectedError,
`Expected ${testCase.expectedError} error for ${testCase.name}, got ${message} (${err.message})`,
);
}
});
it(`should handle nested ${testCase.name}`, async () => {
/*
The users use the new TIMESTAMP(12) type to indicate they want to
opt in to using timestampPrecision=12. The reason is that some queries
like `SELECT CAST(? as TIMESTAMP(12))` will fail if we set
timestampPrecision=12 and we don't want this code change to affect
existing users.
*/
const query = {
query: 'SELECT @r obj',
params: {
r: {
nested: {
a: bigquery.timestamp(
'2023-01-01T12:00:00.123456789123Z',
),
},
},
},
types: {
r: {
nested: {
a: 'TIMESTAMP(12)',
},
},
},
};

const options: any = {};
if (testCase.timestampOutputFormat !== undefined) {
options['formatOptions.timestampOutputFormat'] =
testCase.timestampOutputFormat;
}
if (testCase.useInt64Timestamp !== undefined) {
options['formatOptions.useInt64Timestamp'] =
testCase.useInt64Timestamp;
}

try {
const [rows] = await bigquery.query(query, options);
if (testCase.expectedError) {
assert.fail(
`Query should have failed for ${testCase.name}, but succeeded`,
);
}
assert.ok(rows.length > 0);
assert.ok(rows[0].obj.nested.a.value !== undefined);
assert.strictEqual(
rows[0].obj.nested.a.value,
testCase.expectedTsValue,
);
} catch (err: any) {
if (!testCase.expectedError) {
throw err;
}

const message = err.message;
assert.strictEqual(
message,
testCase.expectedError,
`Expected ${testCase.expectedError} error for ${testCase.name}, got ${message} (${err.message})`,
);
}
});
});
});
});
});

Expand Down