Skip to content

Commit

Permalink
adds toggle for returning integers
Browse files Browse the repository at this point in the history
adds schema check and type conversion on query response
  • Loading branch information
riascho authored and ShireenMissi committed Sep 25, 2024
1 parent 799006a commit 8813938
Showing 1 changed file with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,14 @@ const properties: INodeProperties[] = [
description:
"Whether to use BigQuery's legacy SQL dialect for this query. If set to false, the query will use BigQuery's standard SQL.",
},
{
displayName: 'Return Integers as Numbers',
name: 'returnAsNumbers',
type: 'boolean',
default: false,
description:
'Whether all integer values will be returned as numbers. If set to false, all integer values will be returned as strings.',
},
],
},
];
Expand Down Expand Up @@ -180,6 +188,7 @@ export async function execute(this: IExecuteFunctions): Promise<INodeExecutionDa
timeoutMs?: number;
rawOutput?: boolean;
useLegacySql?: boolean;
returnAsNumbers?: boolean;
};

const projectId = this.getNodeParameter('projectId', i, undefined, {
Expand Down Expand Up @@ -263,6 +272,29 @@ export async function execute(this: IExecuteFunctions): Promise<INodeExecutionDa
qs,
);

if (body.returnAsNumbers === true) {
const numericDataTypes = ['INTEGER', 'NUMERIC', 'FLOAT', 'BIGNUMERIC']; // https://cloud.google.com/bigquery/docs/schemas#standard_sql_data_types
const schema: IDataObject = queryResponse?.schema as IDataObject;
const schemaFields: IDataObject[] = schema.fields as IDataObject[];
const schemaDataTypes: string[] = schemaFields?.map(
(field: IDataObject) => field.type as string,
);
const rows: IDataObject[] = queryResponse.rows as IDataObject[];

for (const row of rows) {
if (!row?.f || !Array.isArray(row.f)) continue;
row.f.forEach((entry: IDataObject, index: number) => {
if (entry && typeof entry === 'object' && 'v' in entry) {
// Skip this row if it's null or doesn't have 'f' as an array
const value = entry.v;
if (numericDataTypes.includes(schemaDataTypes[index])) {
entry.v = Number(value);
}
}
});
}
}

returnData.push(...prepareOutput.call(this, queryResponse, i, raw, includeSchema));
} else {
jobs.push({ jobId, projectId, i, raw, includeSchema, location });
Expand Down

0 comments on commit 8813938

Please sign in to comment.