1

Given that numeric type in BigQuery supports limits way beyond the number type of Javascript, how do I store a numeric type using the node js client Table.insert(rows, options, callback) method? For handling big numbers using string type, I was considering using https://github.com/MikeMcl/big.js. If I send a string value (containing a valid number beyond javascript's number limit) for a numeric field in my BigQuery table, will it work?

Nikhil Agarwal
  • 529
  • 1
  • 4
  • 16

1 Answers1

3

You need to treat the numbers that are too big for JavaScript as strings.

For example, JS doesn't know how to handle -99999999999999999999999999999.999999999:

enter image description here

Meanwhile this code works in NodeJS to insert that huge number into BigQuery:

BigQuery = require('@google-cloud/bigquery');
bigquery = BigQuery({projectId: 'my-project'});
dataset = bigquery.dataset('tt');
table = dataset.table('testfun_numeric');

rows = [{
  name:"whatever",
  numi: "-99999999999999999999999999999.999999999"
}];
table.insert(rows);

And in BigQuery:

SELECT name, numi 
  , numi=numi+1000 better_comparison
FROM `fh-bigquery.tt.testfun_numeric` 

enter image description here

I previously created the table in BigQuery with

CREATE TABLE `tt.testfun_numeric`
AS
SELECT '' name, CAST(0 AS NUMERIC) numi 
LIMIT 0

FWIW, I also tried BigInt(Number.MAX_SAFE_INTEGER) in NodeJS, but the library wouldn't send it correctly to BigQuery - unless I converted it into a string first.

Felipe Hoffa
  • 54,922
  • 16
  • 151
  • 325