Felipe Hoffa
Felipe Hoffa

Reputation: 59175

Can I use JS BigInt in a BigQuery UDF?

Chrome V8 - the JavaScript engine - recently added support for BigInt - arbitrary precision large integers:

Can I use these BigInt in BigQuery?

Upvotes: 0

Views: 572

Answers (1)

Felipe Hoffa
Felipe Hoffa

Reputation: 59175

Yes! BigQuery runs one of the latest versions of V8, so it already supports BigInts.

To use them:

CREATE TEMP FUNCTION testBigInt()
RETURNS ARRAY<STRING>
LANGUAGE js AS """

return [
  Number.MAX_SAFE_INTEGER
  , Number.MAX_SAFE_INTEGER+2
  , Number.MAX_SAFE_INTEGER+1
  , Number.MAX_SAFE_INTEGER+100
  , BigInt(Number.MAX_SAFE_INTEGER) + 2n];

""";

SELECT testBigInt()

9007199254740991     
9007199254740992     
9007199254740992     
9007199254741092     
9007199254740993

From the results, note that JavaScript silently produces the wrong answer when not using BigInt - hence the need for BigInts.

To keep the BigInts compatible with BigQuery, you'll need to treat them as String. Stay tuned, as we've requested improvements for this.

With the wrong types you might get these errors:

  • Error: Failed to coerce output value 9007199254740993 to type NUMERIC
  • Error: Failed to coerce output value 9007199254740992 to type INT64

Upvotes: 3

Related Questions