来自 BigQuery Standard SQL 的漂亮打印值 table 在 CLI 上使用 bq 加入了 tables?

Pretty print value table from BigQuery Standard SQL joined tables using bq on CLI?

有没有一种方法可以使用标准 SQL 使用 bq 命令行工具来漂亮地格式化 Big Query "value table" 结果集?这在使用 Standard SQL 的 BigQuery 控制台中按预期工作,但在 bq 中不起作用。我找不到方便的解决方法。它也可以使用 Legacy SQL.

按预期工作

我的用例:我正在探索 BigQuery 中包含大量列的表。 BigQuery 中的匿名查询要求 selected 列的名称是唯一的,否则您会收到消息 "Duplicate column names in the result are not supported. Found duplicate(s):..."。当我只是探索数据时,这是一个非常严格的要求。幸运的是,通过在 select 表达式...

中使用值表,BigQuery Web 控制台中有一种解决方法
with
left_side as
(
select 1 as id, 'hello' as col1 union all 
select 2 as id, 'there' as col1
)
,right_side as
(
select 1 as id, 'hello' as col1 union all 
select 2 as id, 'world' as col1
)
select a, b
  from left_side a
       join
       right_side b
          on a.id = b.id

...这会产生以下表格结果集,其中自动枚举的列非常好...

然而,当在命令行上传递给 bq 时,完全相同的查询如下(为方便起见,通过此处的文档)。通常,我在文件中有查询并将其重定向到 bq,例如 bq query --use_legacy_sql=false < bq_test4.sql

bq query --use_legacy_sql=false <<BQ_TEST
with
left_side as
(
select 1 as id, 'hello' as col1 union all 
select 2 as id, 'there' as col1
)
,right_side as
(
select 1 as id, 'hello' as col1 union all 
select 2 as id, 'world' as col1
)
select a, b
  from left_side a
       join
       right_side b
          on a.id = b.id
BQ_TEST

...无论传递给 bq...

的任何 --format 选项如何,都会产生相同的基本结果集
+---------------------------+---------------------------+
|             a             |             b             |
+---------------------------+---------------------------+
| {"id":"1","col1":"hello"} | {"id":"1","col1":"hello"} |
| {"id":"2","col1":"there"} | {"id":"2","col1":"world"} |
+---------------------------+---------------------------+

这样的结果集对我没有帮助。

好的,我可以将 json 转换为表格结果集...

with
left_side as
(
select 1 as id, 'hello' as col1 union all 
select 2 as id, 'there' as col1
)
,right_side as
(
select 1 as id, 'hello' as col1 union all 
select 2 as id, 'world' as col1
)
select json_extract_scalar(to_json_string(a), "$['id']") as `a_id`
      ,json_extract_scalar(to_json_string(a), "$['col1']") as `a_col1`
      ,json_extract_scalar(to_json_string(b), "$['id']") as `b_id`
      ,json_extract_scalar(to_json_string(b), "$['col1']") as `b_col1`
  from left_side a
       join
       right_side b
          on a.id = b.id

...这会产生什么,而这正是 Legacy SQL 会产生什么...

+------+--------+------+--------+
| a_id | a_col1 | b_id | b_col1 |
+------+--------+------+--------+
| 1    | hello  | 1    | hello  |
| 2    | there  | 2    | world  |
+------+--------+------+--------+

必须枚举列违背了我的用例的意图。

在使用标准 SQL 时,有什么方法可以避免在加入之前或加入之后枚举列?

仍然需要一些输入 - 但版本不那么冗长

#standardSQL
WITH left_side AS (
  SELECT 1 AS id, 'hello' AS col1 UNION ALL 
  SELECT 2 AS id, 'there' AS col1
), right_side AS (
  SELECT 1 AS id, 'hello' AS col1 UNION ALL 
  SELECT 2 AS id, 'world' AS col1
)
SELECT *
FROM (SELECT NULL a_id, NULL a_col1 UNION ALL SELECT * FROM left_side) a
JOIN (SELECT NULL b_id, NULL b_col1 UNION ALL SELECT * FROM right_side) b
ON a_id = b_id

结果

Row a_id    a_col1  b_id    b_col1   
1   1       hello   1       hello    
2   2       there   2       world    

所以在 bq 中将如下所示...

+------+--------+------+--------+
| a_id | a_col1 | b_id | b_col1 |
+------+--------+------+--------+
| 1    | hello  | 1    | hello  |
| 2    | there  | 2    | world  |
+------+--------+------+--------+