Can I use JS BigInt in a BigQuery UDF?

旧街凉风 提交于 2020-01-03 05:47:27

问题


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

  • https://developers.google.com/web/updates/2018/05/bigint

Can I use these BigInt in BigQuery?


回答1:


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


来源:https://stackoverflow.com/questions/52825129/can-i-use-js-bigint-in-a-bigquery-udf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!