导出到JSON时如何保留整数数据类型?

时间:2021-09-27 13:48:46

When I export my bigquery tables in JSON format, the INTEGER fields are converted to strings. Is there any way to maintain the integer data type when exporting?

当我以JSON格式导出我的bigquery表时,INTEGER字段将转换为字符串。导出时有没有办法维护整数数据类型?

Here are the minimum steps to reproduce the integer->string conversion phenomenon:

以下是重现整数 - >字符串转换现象的最小步骤:

  1. Run the query SELECT INTEGER(1) AS myInt and save the result to a table. Note that the output table schema shows the type as INTEGER.
  2. 运行查询SELECT INTEGER(1)AS myInt并将结果保存到表中。请注意,输出表架构将类型显示为INTEGER。

  3. Export the table in JSON format. The output will be: {"myInt":"1"}
  4. 以JSON格式导出表。输出将是:{“myInt”:“1”}

In the JSON format, "1" is a string, not an integer.

在JSON格式中,“1”是字符串,而不是整数。

1 个解决方案

#1


4  

This is not currently possible; the reasoning is because of an unfortunate combination of the Javascript spec, IEEE floating point precision, JSON, and BigQuery integer sizes.

这目前不可能;推理是因为Javascript规范,IEEE浮点精度,JSON和BigQuery整数大小的不幸组合。

In Javascript, all numbers must be representable as IEEE754 double-precision floating point values. Javascript parses JSON numbers into javascript numbers. BigQuery uses 64-bit signed integer values.

在Javascript中,所有数字必须可表示为IEEE754双精度浮点值。 Javascript将JSON数字解析为javascript数字。 BigQuery使用64位有符号整数值。

The problem comes because not all 64-bit integer values can be represented as IEEE 754 double precision floating point values. (it is easy to see why: IEEE 754 double-precision floating point uses 64 bits but can represent lots of things that aren't integers; therefore, there must be 64 bit integers that it can't represent).

问题出现是因为并非所有64位整数值都可以表示为IEEE 754双精度浮点值。 (很容易理解为什么:IEEE 754双精度浮点使用64位,但可以表示许多不是整数的东西;因此,必须有64位整数,它不能代表)。

So in order to make BigQuery JSON responses work in Javascript, integer values are wrapped in quotes, so that no precision is lost.

因此,为了使BigQuery JSON响应在Javascript中工作,整数值用引号括起来,这样就不会丢失精度。

That said ... the decision to represent integers as strings in API request makes sense, since many of the callers of the API will be in javascript. There doesn't seem to be as compelling of an argument to not represent integers as numbers when exporting data. (other than to change it now would be a breaking change).

这就是说...在API请求中将整数表示为字符串的决定是有道理的,因为API的许多调用者都将使用javascript。在导出数据时,似乎没有引人注目的参数表示不将整数表示为数字。 (除了现在改变它将是一个突破性的变化)。

Can you file a bug at the BigQuery issue tracker to fix this? (it would likely involve another flag in the export configuration).

您可以在BigQuery问题跟踪器中提交错误来解决此问题吗? (它可能涉及导出配置中的另一个标志)。

#1


4  

This is not currently possible; the reasoning is because of an unfortunate combination of the Javascript spec, IEEE floating point precision, JSON, and BigQuery integer sizes.

这目前不可能;推理是因为Javascript规范,IEEE浮点精度,JSON和BigQuery整数大小的不幸组合。

In Javascript, all numbers must be representable as IEEE754 double-precision floating point values. Javascript parses JSON numbers into javascript numbers. BigQuery uses 64-bit signed integer values.

在Javascript中,所有数字必须可表示为IEEE754双精度浮点值。 Javascript将JSON数字解析为javascript数字。 BigQuery使用64位有符号整数值。

The problem comes because not all 64-bit integer values can be represented as IEEE 754 double precision floating point values. (it is easy to see why: IEEE 754 double-precision floating point uses 64 bits but can represent lots of things that aren't integers; therefore, there must be 64 bit integers that it can't represent).

问题出现是因为并非所有64位整数值都可以表示为IEEE 754双精度浮点值。 (很容易理解为什么:IEEE 754双精度浮点使用64位,但可以表示许多不是整数的东西;因此,必须有64位整数,它不能代表)。

So in order to make BigQuery JSON responses work in Javascript, integer values are wrapped in quotes, so that no precision is lost.

因此,为了使BigQuery JSON响应在Javascript中工作,整数值用引号括起来,这样就不会丢失精度。

That said ... the decision to represent integers as strings in API request makes sense, since many of the callers of the API will be in javascript. There doesn't seem to be as compelling of an argument to not represent integers as numbers when exporting data. (other than to change it now would be a breaking change).

这就是说...在API请求中将整数表示为字符串的决定是有道理的,因为API的许多调用者都将使用javascript。在导出数据时,似乎没有引人注目的参数表示不将整数表示为数字。 (除了现在改变它将是一个突破性的变化)。

Can you file a bug at the BigQuery issue tracker to fix this? (it would likely involve another flag in the export configuration).

您可以在BigQuery问题跟踪器中提交错误来解决此问题吗? (它可能涉及导出配置中的另一个标志)。