BUG #5123: bug in window function "last_value"

Started by Andreyover 16 years ago7 messagesbugs
Jump to latest
#1Andrey
andrey@ulab.ru

The following bug has been logged online:

Bug reference: 5123
Logged by: Andrey
Email address: andrey@ulab.ru
PostgreSQL version: 8.4.1-x86_64
Operating system: RHEL5-x86_64
Description: bug in window function "last_value"
Details:

EXAMPLE:

CREATE TABLE t
(
id serial NOT NULL,
CONSTRAINT pkey_t PRIMARY KEY (id)
)
WITH (OIDS=FALSE);

INSERT INTO t VALUES(default);
INSERT INTO t VALUES(default);
INSERT INTO t VALUES(default);

select id, first_value(id) over(order by id), last_value(id) over(order by
id) from t;

RESULT:
id | first_value | last_value
----+-------------+------------
1 | 1 | 1
2 | 1 | 2
3 | 1 | 3
(3 rows)

fist_value - good, last_value - bad

#2Heikki Linnakangas
heikki.linnakangas@enterprisedb.com
In reply to: Andrey (#1)
Re: BUG #5123: bug in window function "last_value"

Andrey wrote:

select id, first_value(id) over(order by id), last_value(id) over(order by
id) from t;

RESULT:
id | first_value | last_value
----+-------------+------------
1 | 1 | 1
2 | 1 | 2
3 | 1 | 3
(3 rows)

fist_value - good, last_value - bad

Looks ok to me. What did you expect?

--
Heikki Linnakangas
EnterpriseDB http://www.enterprisedb.com

#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: Heikki Linnakangas (#2)
Re: BUG #5123: bug in window function "last_value"

Heikki Linnakangas <heikki.linnakangas@enterprisedb.com> writes:

Andrey wrote:

select id, first_value(id) over(order by id), last_value(id) over(order by
id) from t;

RESULT:
id | first_value | last_value
----+-------------+------------
1 | 1 | 1
2 | 1 | 2
3 | 1 | 3
(3 rows)

fist_value - good, last_value - bad

Looks ok to me. What did you expect?

These *are* the correct answers, since the default window frame runs
from first row to current row. If you don't like them, you may need
to specify a different window frame.

regards, tom lane

#4Hitoshi Harada
umi.tanuki@gmail.com
In reply to: Tom Lane (#3)
Re: BUG #5123: bug in window function "last_value"

2009/10/16 Tom Lane <tgl@sss.pgh.pa.us>:

Heikki Linnakangas <heikki.linnakangas@enterprisedb.com> writes:

Andrey wrote:

select id, first_value(id) over(order by id), last_value(id) over(order by
id) from t;

RESULT:
id | first_value | last_value
----+-------------+------------
1 |           1 |          1
2 |           1 |          2
3 |           1 |          3
(3 rows)

fist_value - good, last_value - bad

Looks ok to me. What did you expect?

These *are* the correct answers, since the default window frame runs
from first row to current row.  If you don't like them, you may need
to specify a different window frame.

                       regards, tom lane

And it's well-documented. See
http://www.postgresql.org/docs/8.4/static/functions-window.html

--
Hitoshi Harada

#5David Fetter
david@fetter.org
In reply to: Heikki Linnakangas (#2)
Re: BUG #5123: bug in window function "last_value"

On Fri, Oct 16, 2009 at 04:45:55PM +0300, Heikki Linnakangas wrote:

Andrey wrote:

select id, first_value(id) over(order by id), last_value(id) over(order by
id) from t;

RESULT:
id | first_value | last_value
----+-------------+------------
1 | 1 | 1
2 | 1 | 2
3 | 1 | 3
(3 rows)

fist_value - good, last_value - bad

Looks ok to me. What did you expect?

I think what the OP was expecting was to have the last value be 3
instead of changing. This is at least a POLA violation.

Cheers,
David.
--
David Fetter <david@fetter.org> http://fetter.org/
Phone: +1 415 235 3778 AIM: dfetter666 Yahoo!: dfetter
Skype: davidfetter XMPP: david.fetter@gmail.com

Remember to vote!
Consider donating to Postgres: http://www.postgresql.org/about/donate

#6Tom Lane
tgl@sss.pgh.pa.us
In reply to: David Fetter (#5)
Re: BUG #5123: bug in window function "last_value"

David Fetter <david@fetter.org> writes:

I think what the OP was expecting was to have the last value be 3
instead of changing. This is at least a POLA violation.

[ shrug... ] It's what the spec requires, as far as anybody here
can tell. As Hitoshi-san already noted, we do point out in our
docs that last_value is not too useful unless you use a nondefault
window frame selection. I'm not sure what else we could do.

regards, tom lane

#7David Fetter
david@fetter.org
In reply to: Tom Lane (#6)
Re: BUG #5123: bug in window function "last_value"

On Fri, Oct 16, 2009 at 01:28:57PM -0400, Tom Lane wrote:

David Fetter <david@fetter.org> writes:

I think what the OP was expecting was to have the last value be 3
instead of changing. This is at least a POLA violation.

[ shrug... ] It's what the spec requires, as far as anybody here
can tell. As Hitoshi-san already noted, we do point out in our docs
that last_value is not too useful unless you use a nondefault window
frame selection. I'm not sure what else we could do.

Perhaps an illustration of the issue and a workaround in the docs?

Cheers,
David.
--
David Fetter <david@fetter.org> http://fetter.org/
Phone: +1 415 235 3778 AIM: dfetter666 Yahoo!: dfetter
Skype: davidfetter XMPP: david.fetter@gmail.com

Remember to vote!
Consider donating to Postgres: http://www.postgresql.org/about/donate