BigQuery-UNION ALL处的类型不兼容? - 使我抓狂

时间:2018-06-25 18:46:50

标签: google-bigquery

以下是我查询的简化版本。 Debug表中的DebugReason类型为INTEGER,而DebugData类型为STRING。而且GPS表中没有这两个字段,因此我用所有NULL进行了伪造。我之所以需要这样做与该问题无关,长话短说,我需要在以下后续过程中使用它

 WITH RawDebug as 
(
  SELECT 
  STRUCT(DebugReason,DebugData) as Debug
  FROM `devicedata.Debug.T*`
  WHERE _TABLE_SUFFIX="20180624"

),
RawGPS AS (        
       SELECT
          STRUCT(null as DebugReason,null as DebugData) as Debug
        FROM
          `devicedata.Gps.T*` AS g
         WHERE _TABLE_SUFFIX="20180624"

)
select Debug
from RawDebug
UNION ALL
select Debug
from RawGPS

BigQuery说:

Error: Column 1 in UNION ALL has incompatible types: STRUCT<DebugReason INT64, DebugData STRING>, STRUCT<DebugReason INT64, DebugData INT64> at [18:1]

我不知道出了什么问题...以及如何使之正确?

2 个答案:

答案 0 :(得分:1)

在联合中的一个查询中,DebugData字段的类型为STRING,而在另一个查询中,字段的类型为INT64。如果要使用一致的类型,则需要强制转换:

WITH RawDebug as 
(
  SELECT 
  STRUCT(DebugReason,DebugData) as Debug
  FROM `devicedata.Debug.T*`

),
RawGPS AS (        
       SELECT
          STRUCT(
            null as DebugReason,
            CAST(DebugData AS STRING) AS DebugData -- note the cast
          ) as Debug
        FROM
          `devicedata.Gps.T*` AS g

)
select Debug
from RawDebug
UNION ALL
select Debug
from RawGPS

之所以会这样,是因为NULL的默认类型是INT64

答案 1 :(得分:1)

另一种选择是显式指定STRUCT,如下所示

serviceQuery