[pgsql-jp: 39898] Re: window関数のarray_agg関数(order by指定)の結果がおかしい
Hitoshi Harada
umi.tanuki @ gmail.com
2009年 6月 22日 (月) 23:45:18 JST
原田です。
> stragg2とstragg3が一致するのが正しい結果ではないでしょうか?
> 配列の要素が違います。
おっしゃる通りだと思います。
私もRC1のバージョンで現象を確認しましたが、レポジトリ最新版で試したら正しく動作しました。
Window関数というよりはarray_agg(それも、文字列型など可変長型を引数とする場合)のバグだったようです。恐らく下記のコミットログがバグを解消しています。
http://archives.postgresql.org/pgsql-committers/2009-06/msg00259.php
with w(sortKey,Val) as(
select 1,'a' union all
select 2,'b' union all
select 3,'c' union all
select 4,'d' union all
select 5,'e')
select
array_agg(Val) over() as strAgg1,
array_agg(Val) over(order by sortKey) as strAgg2,
(select array_agg(b.Val)
from w b
where b.sortKey <= a.sortKey) as strAgg3
from w a;
stragg1 | stragg2 | stragg3
-------------+-------------+-------------
{a,b,c,d,e} | {a} | {a}
{a,b,c,d,e} | {a,b} | {a,b}
{a,b,c,d,e} | {a,b,c} | {a,b,c}
{a,b,c,d,e} | {a,b,c,d} | {a,b,c,d}
{a,b,c,d,e} | {a,b,c,d,e} | {a,b,c,d,e}
(5 rows)
2009/06/22 21:44 に <postgresqlmailabcd @ mail.goo.ne.jp> さんは書きました:
> window関数でいろいろ試していたらこんな現象に遭遇しました。
> 原因が分かる方がおられましたら、教えてください。
>
> 環境 8.4beta2 windows版
>
> with w(sortKey,Val) as(
> select 1,'a' union all
> select 2,'b' union all
> select 3,'c' union all
> select 4,'d' union all
> select 5,'e')
> select
> array_agg(Val) over() as strAgg1,
> array_agg(Val) over(order by sortKey) as strAgg2,
> (select array_agg(b.Val)
> from w b
> where b.sortKey <= a.sortKey) as strAgg3
> from w a;
>
> stragg1 | stragg2 | stragg3
> -------------+-------------+-------------
> {a,b,c,d,e} | {a} | {a}
> {a,b,c,d,e} | {b,b} | {a,b}
> {a,b,c,d,e} | {c,c,c} | {a,b,c}
> {a,b,c,d,e} | {d,d,d,d} | {a,b,c,d}
> {a,b,c,d,e} | {e,e,e,e,e} | {a,b,c,d,e}
>
> stragg2とstragg3が一致するのが正しい結果ではないでしょうか?
> 配列の要素が違います。
>
> *******************************************************
> 明智重蔵
> http://oraclesqlpuzzle.hp.infoseek.co.jp/blog.html
>
--
Hitoshi Harada
pgsql-jp メーリングリストの案内