mysql外部查询聚合返回日期时间的case表达式的bigquery出现内部错误

rsaldnfx  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(319)

我下面有一个mre,它在大查询中生成一个内部错误。当运行mysql语句作为bq external\u query函数的参数时,会出现这个问题。sql在mysql中运行良好。这是导致问题的表达式,即返回datetime的case表达式的max。 max(case when t.code = 'BALANCE' then t.date else null end) 将case表达式的空端转换为datetime没有帮助。在mysql中,case表达式的结果是int或bigint时没有问题。
错误是:“发生内部错误,请求无法完成。错误:3144498“
根据bq日志:

severity: "INFO"   
textPayload: "... [Note] Aborted connection 737652 to db:... (Got an error reading communication packets)"

这个mre不需要模式,只需要到mysql云示例(v5.7)(#standardsql)的bq连接。3例;前2个生成内部错误,最后一个是解决方法。根据需要切换注解并运行。同样,引用的mysql在mysql中也能正常工作。

select *
  FROM EXTERNAL_QUERY("your_mysql_connection_string",
'''
select t.id as transactionID
       -- Aggregation 1: Generates internal error
      ,max(case when t.code = 'BALANCE' then t.date else null end) as maxDate
--       -- Aggregation 2: Generates internal error
--       ,max(case when t.code_id = 999 then t.date else null end) as maxDate
--       -- Aggregation 3: Workaround the internal error
--      ,from_unixtime(max(case when t.code = 'BALANCE' then unix_timestamp(t.date) else unix_timestamp(cast(null as datetime)) end)) as maxDate
  from (
          select 1 as id, 'BALANCE' as code, 999 as code_id, current_timestamp() as date union all 
          select 2, 'BALANCE' as code, 999 as code_id, current_timestamp() as date
       ) t
 group by t.id
;''')
;

在我看来,这就像bigquery/mysql接口中的某种bug。这些查询在mysql中工作正常,是基本的聚合语句。

k4aesqcs

k4aesqcs1#

同意,听起来像个bug-所以你可能想在这里提交它
同时,下面是更简单的解决方法-只需使用 select * from (...) t 如下例所示

SELECT * FROM EXTERNAL_QUERY("your_mysql_connection_string", '''
select * from (
    select t.id as transactionID
        -- Aggregation 1: Generates internal error
        ,max(case when t.code = 'BALANCE' then t.date else null end) as maxDate
    from (
        select 1 as id, 'BALANCE' as code, 999 as code_id, current_timestamp() as date union all 
        select 2, 'BALANCE' as code, 999 as code_id, current_timestamp() as date
  ) t
  group by t.id
) t
''');

相关问题