Reply.fill() - 从 Db2 Luw table 获取 JSON_ARRAY() 数据时数据不足

Reply.fill() - insufficient data when fetching JSON_ARRAY() data from Db2 Luw table

我正在使用 docker hub 提供的 Db2 LUW v11.5.5.1 和此架构:

create table t (i int, j int);
insert into t values (1, 2);

当我 运行 来自 DBeaver 的这条语句时:

select json_array(i, j)
from t;

然后我收到这个错误:

SQL Error [08001]: [jcc][t4][2030][11211][4.29.24] A communication error occurred during operations on the connection's underlying socket, socket input stream, or socket output stream. Error location: Reply.fill() - insufficient data (-1). Message: Insufficient data. ERRORCODE=-4499, SQLSTATE=08001

这个有效:

select json_array(1, 2)
from t;

这是服务器上/ JDBC 驱动程序中的已知错误吗(我已经尝试了 11.5.0.0、11.5.4.0、11.5.5.0、11.5.6.0 之间的所有驱动程序版本 maven central)?有解决方法吗?

V11.5.5.1 的 Db2-LUW 服务器可能存在缺陷(即与 jdbc 驱动程序无关)。

似乎已在 Db2-LUW v11.5.6.0 中修复。

单值数组的解决方法

适用于单值数组的解决方法是:

select json_array((select i from sysibm.dual))
from t;

不幸的是,该变通方法不适用于多个这样的值。这两个都失败并出现相同的错误 (as documented, only a single subquery is allowed):

select json_array((select i from sysibm.dual), j)
from t;

select json_array((select i from sysibm.dual), (select j from sysibm.dual))
from t;

SQL Error [42601]: An unexpected token "," was found following "i from sysibm.dual)". Expected tokens may include: "=".. SQLCODE=-104, SQLSTATE=42601, DRIVER=4.29.24

没有连接的多值数组的解决方法

但令人难以置信的是,这有效!

select (select json_array(i, j) from sysibm.dual)
from t;

连接查询的解决方法

但是一旦有连接,上述解决方法就会再次失败:

create table t (i int, j int);
create table u (i int, j int);
insert into t values (1, 2);
insert into u values (1, 2);

-- And then, this fails again:
select (select json_array(t.i, u.j) from sysibm.dual)
from t join u on t.i = u.i

但我不会放弃。可以使用 JSON_QUERY()JSON_OBJECT().

完全重新发明看似微不足道的 JSON_ARRAY() 函数

不是说你应该,但你绝对可以。

select json_query(
  json_object(key 'k' value t.i, key 'k' value u.j), 
  '$.*' with unconditional array wrapper
)
from t join u on t.i = u.i

我遇到了同样的错误,发现 JSON_ARRAY 期望在其中使用 SELECT 语句时返回单个列。

如果您需要从 SELECT 结果构建 json 数组,您需要确保它 returns 多行,每行单列。 如果所需值位于单个结果行的两列中,您可以使用 union all 将它们放在两个单独的结果行中。

例如:

select json_array((select i from sysibm.dual), (select j from sysibm.dual))
from t;

你应该试试

select json_array(select i from sysibm.dual union all select j from sysibm.dual) 
from t;

这也适用于连接表,例如:

select json_array(select t.i from sysibm.dual union all select u.j from sysibm.dual) 
from t join u on t.i = u.i

或通过创建单列 TABLE:

select json_array(select * from TABLE(VALUES(t.i),(u.j)))
from t join u on t.i = u.i