Skip to content

fix: avoid schema field mutation when passing selectedFields opt #1437

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

Merged
merged 7 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
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
6 changes: 4 additions & 2 deletions src/bigquery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,17 +362,17 @@
private _universeDomain: string;
private _enableQueryPreview: boolean;

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

Check warning on line 365 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 370 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 375 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 @@ -588,14 +588,16 @@
parseJSON?: boolean;
}
) {
// copy schema fields to avoid mutation when filtering selected fields
let schemaFields = schema.fields ? [...schema.fields] : [];
if (options.selectedFields && options.selectedFields!.length > 0) {
const selectedFieldsArray = options.selectedFields!.map(c => {
return c.split('.');
});

const currentFields = selectedFieldsArray.map(c => c.shift());
//filter schema fields based on selected fields.
schema.fields = schema.fields?.filter(
schemaFields = schemaFields.filter(
field =>
currentFields
.map(c => c!.toLowerCase())
Expand All @@ -610,7 +612,7 @@

function mergeSchema(row: TableRow) {
return row.f!.map((field: TableRowField, index: number) => {
const schemaField = schema.fields![index];
const schemaField = schemaFields[index];
let value = field.v;
if (schemaField.mode === 'REPEATED') {
value = (value as TableRowField[]).map(val => {
Expand Down Expand Up @@ -1569,7 +1571,7 @@
const parameterMode = is.array(params) ? 'positional' : 'named';
const queryParameters: bigquery.IQueryParameter[] = [];
if (parameterMode === 'named') {
const namedParams = params as {[param: string]: any};

Check warning on line 1574 in src/bigquery.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
for (const namedParameter of Object.getOwnPropertyNames(namedParams)) {
const value = namedParams[namedParameter];
let queryParameter;
Expand Down Expand Up @@ -2212,7 +2214,7 @@

options = extend({job}, queryOpts, options);
if (res && res.jobComplete) {
let rows: any = [];

Check warning on line 2217 in src/bigquery.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
if (res.schema && res.rows) {
rows = BigQuery.mergeSchemaWithRows_(res.schema, res.rows, {
wrapIntegers: options.wrapIntegers || false,
Expand Down
10 changes: 6 additions & 4 deletions src/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1830,6 +1830,10 @@ class Table extends ServiceObject {
delete options.wrapIntegers;
const parseJSON = options.parseJSON ? options.parseJSON : false;
delete options.parseJSON;
const selectedFields = options.selectedFields
? options.selectedFields.split(',')
: [];
delete options.selectedFields;
const onComplete = (
err: Error | null,
rows: TableRow[] | null,
Expand All @@ -1841,10 +1845,8 @@ class Table extends ServiceObject {
return;
}
rows = BigQuery.mergeSchemaWithRows_(this.metadata.schema, rows || [], {
wrapIntegers: wrapIntegers,
selectedFields: options.selectedFields
? options.selectedFields!.split(',')
: [],
wrapIntegers,
selectedFields,
parseJSON,
});
callback!(null, rows, nextQuery, resp);
Expand Down
Loading
Loading