Skip to content

Commit

Permalink
fix: avoid schema field mutation when passing selectedFields opt (#1437)
Browse files Browse the repository at this point in the history
  • Loading branch information
alvarowolfx authored Feb 12, 2025
1 parent 0f2e334 commit 27044d5
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 150 deletions.
6 changes: 4 additions & 2 deletions src/bigquery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -588,14 +588,16 @@ export class BigQuery extends Service {
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 @@ export class BigQuery extends Service {

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
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

0 comments on commit 27044d5

Please sign in to comment.