Skip to content

Commit 27044d5

Browse files
authored
fix: avoid schema field mutation when passing selectedFields opt (#1437)
1 parent 0f2e334 commit 27044d5

File tree

4 files changed

+85
-150
lines changed

4 files changed

+85
-150
lines changed

src/bigquery.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -588,14 +588,16 @@ export class BigQuery extends Service {
588588
parseJSON?: boolean;
589589
}
590590
) {
591+
// copy schema fields to avoid mutation when filtering selected fields
592+
let schemaFields = schema.fields ? [...schema.fields] : [];
591593
if (options.selectedFields && options.selectedFields!.length > 0) {
592594
const selectedFieldsArray = options.selectedFields!.map(c => {
593595
return c.split('.');
594596
});
595597

596598
const currentFields = selectedFieldsArray.map(c => c.shift());
597599
//filter schema fields based on selected fields.
598-
schema.fields = schema.fields?.filter(
600+
schemaFields = schemaFields.filter(
599601
field =>
600602
currentFields
601603
.map(c => c!.toLowerCase())
@@ -610,7 +612,7 @@ export class BigQuery extends Service {
610612

611613
function mergeSchema(row: TableRow) {
612614
return row.f!.map((field: TableRowField, index: number) => {
613-
const schemaField = schema.fields![index];
615+
const schemaField = schemaFields[index];
614616
let value = field.v;
615617
if (schemaField.mode === 'REPEATED') {
616618
value = (value as TableRowField[]).map(val => {

src/table.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1830,6 +1830,10 @@ class Table extends ServiceObject {
18301830
delete options.wrapIntegers;
18311831
const parseJSON = options.parseJSON ? options.parseJSON : false;
18321832
delete options.parseJSON;
1833+
const selectedFields = options.selectedFields
1834+
? options.selectedFields.split(',')
1835+
: [];
1836+
delete options.selectedFields;
18331837
const onComplete = (
18341838
err: Error | null,
18351839
rows: TableRow[] | null,
@@ -1841,10 +1845,8 @@ class Table extends ServiceObject {
18411845
return;
18421846
}
18431847
rows = BigQuery.mergeSchemaWithRows_(this.metadata.schema, rows || [], {
1844-
wrapIntegers: wrapIntegers,
1845-
selectedFields: options.selectedFields
1846-
? options.selectedFields!.split(',')
1847-
: [],
1848+
wrapIntegers,
1849+
selectedFields,
18481850
parseJSON,
18491851
});
18501852
callback!(null, rows, nextQuery, resp);

0 commit comments

Comments
 (0)