JSON_ARRAYAGG 的 Oracle Cloud DB 19c 错误

Oracle Cloud DB 19c bug with JSON_ARRAYAGG

我尝试将 JSON_ARRAYAGG 用于 select 并使用相同的数据得到错误结果

SELECT json_object(
         'buy' VALUE JSON_ARRAYAGG(b.buysum),
         'total' VALUE JSON_ARRAYAGG(b.totalsum)
       )
FROM   (
  select *
  from   view_count_sum
  ORDER BY date_rw DESC
  FETCH FIRST 10 ROWS ONLY
) b
ORDER BY b.date_rw;

结果我得到 JSON 2 个数组,第一个数组中的数据顺序减少,第二个数组中的顺序错误

{"buy":[4168,4145,4130,4101,4068,4042,4008,3940,3900,3858],"total":[7778,7258,7333,7442,7546,7607,7642,7683,7718,7745]}

如果我替换 select 中的位置 JSON_ARRAYAGG 我再次看到第一个数组的正确顺序和第二个数组的错误顺序

SELECT json_object(
         'total' VALUE JSON_ARRAYAGG(b.totalsum),
         'buy' VALUE JSON_ARRAYAGG(b.buysum)
       )
FROM   (
  select *
  from   view_count_sum
  ORDER BY date_rw DESC
  FETCH FIRST 10 ROWS ONLY
) b
ORDER BY b.date_rw;

查看结果:

{"total":[7778,7745,7718,7683,7642,7607,7546,7442,7333,7258],"buy":[4168,3858,3900,3940,4008,4042,4068,4101,4130,4145]}

第二个和任何其他数组的顺序都是错误的。第一个元素是对的,其他都是反的

从一些很容易看出预期顺序的数据开始:

CREATE TABLE view_count_sum (date_rw, buysum, totalsum) AS
SELECT 10,  1,  1 FROM DUAL UNION ALL
SELECT  9,  2,  2 FROM DUAL UNION ALL
SELECT  8,  3,  3 FROM DUAL UNION ALL
SELECT  7,  4,  4 FROM DUAL UNION ALL
SELECT  6,  5,  5 FROM DUAL UNION ALL
SELECT  5,  6,  6 FROM DUAL UNION ALL
SELECT  4,  7,  7 FROM DUAL UNION ALL
SELECT  3,  8,  8 FROM DUAL UNION ALL
SELECT  2,  9,  9 FROM DUAL UNION ALL
SELECT  1, 10, 10 FROM DUAL;

那么,如果你这样做:

SELECT json_object(
         'buy'   VALUE JSON_ARRAYAGG(b.buysum),
         'total' VALUE JSON_ARRAYAGG(b.totalsum)
       ) AS json
FROM   (
  select *
  from   view_count_sum
  ORDER BY date_rw DESC
  FETCH FIRST 10 ROWS ONLY
) b
ORDER BY b.date_rw;

则输出为:

JSON
{"buy":[1,2,3,4,5,6,7,8,9,10],"total":[1,10,9,8,7,6,5,4,3,2]}

相反,如果将 ORDER BY 子句添加到聚合函数中:

SELECT json_object(
         'buy'   VALUE JSON_ARRAYAGG(b.buysum   ORDER BY b.date_RW DESC),
         'total' VALUE JSON_ARRAYAGG(b.totalsum ORDER BY b.date_RW DESC)
       ) AS json
FROM   (
  select *
  from   view_count_sum
  ORDER BY date_rw DESC
  FETCH FIRST 10 ROWS ONLY
) b;

则输出为:

JSON
{"buy":[1,2,3,4,5,6,7,8,9,10],"total":[1,2,3,4,5,6,7,8,9,10]}

db<>fiddle for Oracle 18