如何使用BigQuery在INT64和二进制STRING之间转换?

时间:2018-07-30 18:31:52

标签: google-bigquery standard-sql

BigQuery问题跟踪器上有一个feature request,用于转换功能,例如在INT64和二进制文件STRING之间。除了使用JavaScript UDF之外,还有其他解决方法吗?

1 个答案:

答案 0 :(得分:3)

是的,您可以使用SQL在INT64和二进制STRING之间进行转换,而无需使用JavaScript。这是一个示例:

CREATE TEMP FUNCTION ToBinary(x INT64) AS (
  (SELECT STRING_AGG(CAST(x >> bit & 0x1 AS STRING), '' ORDER BY bit DESC)
   FROM UNNEST(GENERATE_ARRAY(0, 63)) AS bit)
);

CREATE TEMP FUNCTION FromBinary(s STRING) AS (
  (SELECT SUM(CAST(c AS INT64) << (LENGTH(s) - 1 - bit))
   FROM UNNEST(SPLIT(s, '')) AS c WITH OFFSET bit)
);

SELECT x, ToBinary(x) AS binary, FromBinary(ToBinary(x)) AS roundtrip
FROM UNNEST([1, 123456, 9876543210, -1001]) AS x;

这将返回:

+------------+------------------------------------------------------------------+------------+
|     x      |                              binary                              | roundtrip  |
+------------+------------------------------------------------------------------+------------+
|          1 | 0000000000000000000000000000000000000000000000000000000000000001 |          1 |
|     123456 | 0000000000000000000000000000000000000000000000011110001001000000 |     123456 |
| 9876543210 | 0000000000000000000000000000001001001100101100000001011011101010 | 9876543210 |
|      -1001 | 1111111111111111111111111111111111111111111111111111110000010111 |      -1001 |
+------------+------------------------------------------------------------------+------------+

作为奖励,这是一种在INT64和十六进制STRING之间进行转换的方法:

CREATE TEMP FUNCTION ToHex(x INT64) AS (
  (SELECT STRING_AGG(FORMAT('%02x', x >> (byte * 8) & 0xff), '' ORDER BY byte DESC)
   FROM UNNEST(GENERATE_ARRAY(0, 7)) AS byte)
);

CREATE TEMP FUNCTION FromHex(s STRING) AS (
  (SELECT SUM(CAST(CONCAT('0x', SUBSTR(s, byte * 2 + 1, 2)) AS INT64) << ((LENGTH(s) - (byte + 1) * 2) * 4))
   FROM UNNEST(GENERATE_ARRAY(1, LENGTH(s) / 2)) WITH OFFSET byte)
);

SELECT x, ToHex(x) AS hex, FromHex(ToHex(x)) AS roundtrip
FROM UNNEST([1, 123456, 9876543210, -1001]) AS x;

这将返回:

+------------+------------------+------------+
|     x      |       hex        | roundtrip  |
+------------+------------------+------------+
|          1 | 0000000000000001 |          1 |
|     123456 | 000000000001e240 |     123456 |
| 9876543210 | 000000024cb016ea | 9876543210 |
|      -1001 | fffffffffffffc17 |      -1001 |
+------------+------------------+------------+

(最终转换是留给读者的练习。)