在 Big Query 中计算校验和

Calculating checksum in Big Query

我在 SAP HANA 中有一个查询,如下所示。我正在根据 table

中的某些列计算 check_sum
select Name, address, salary, phone_number, department, HASH_SHA256( TO_BINARY(COALESCE(Name, 'ABC')), TO_BINARY(COALESCE(address, 'ABC')), TO_BINARY(COALESCE(salary, 0)), TO_BINARY(COALESCE(department, 'ABC'))) as check_sum from table1;

现在我在 Big Query 中创建了相同的 table。

我想在 Big Query 中计算 check_sum 并得到与 SAP HANA

相同的结果

我试过如下

select Name, address, salary, phone_number, department, TO_HEX(SHA256((TO_BINARY(COALESCE(Name, 'ABC')), TO_BINARY(COALESCE(address, 'ABC')), TO_BINARY(COALESCE(salary, 0)), TO_BINARY(COALESCE(department, 'ABC')))) as check_sum from table1;

它说 TO_BINARY 功能不可用。

我试图将列转换为 BYTES,但它说 NUMERIC 无法转换为 BYTES

select Name, address, salary, phone_number, department, TO_HEX(SHA256(CAST(COALESCE(Name, 'ABC') AS BYTES), CAST(COALESCE(address, 'ABC') AS BYTES), CAST(COALESCE(salary, 0) AS BYTES), CAST(COALESCE(department, 'ABC') AS BYTES))) as check_sum from table1;

但是还是不行。匹配结果的正确方法是什么。

像下面这样尝试应该会给你正确的值

select Name, address, salary, phone_number, department, UPPER(TO_HEX(SHA256(CONCAT(CAST(COALESCE(Name, 'ABC') AS STRING), CAST(COALESCE(address, 'ABC') AS STRING), CAST(COALESCE(salary, 0) AS STRING), CAST(COALESCE(department, 'ABC') AS STRING))))) as check_sum from table1;