Received: from makus.postgresql.org ([98.129.198.125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TarkQ-0004rm-LE for pgsql-bugs@postgresql.org; Tue, 20 Nov 2012 17:36:34 +0000 Received: from nm39-vm1.bullet.mail.bf1.yahoo.com ([72.30.239.145]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TarkP-0008HW-A3 for pgsql-bugs@postgresql.org; Tue, 20 Nov 2012 17:36:34 +0000 Received: from [98.139.215.143] by nm39.bullet.mail.bf1.yahoo.com with NNFMP; 20 Nov 2012 17:36:32 -0000 Received: from [76.13.13.40] by tm14.bullet.mail.bf1.yahoo.com with NNFMP; 20 Nov 2012 17:36:32 -0000 Received: from [127.0.0.1] by smtp101.prem.mail.ac4.yahoo.com with NNFMP; 20 Nov 2012 17:36:32 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1353432992; bh=OycF0Nq6cEw+SwWY8hnitn+MxxVB1gaYyniV7dj+UWs=; h=X-Yahoo-Newman-Id:X-Yahoo-Newman-Property:X-YMail-OSG:X-Yahoo-SMTP:Received:From:To:Subject:Date:Message-ID:MIME-Version:Content-Type:Content-Transfer-Encoding:X-Mailer:Thread-Index:Content-Language; b=3f7QKKmMtApuKEgyWHPiOj0CEvpBM1ULcUHyjNtAyIHRut3hYB5cF0NZp1q+QFx3PVA6PILk2j5l/6WH3IP5DGbnrWWYSSQOrrQRXMZiizEAT7crWaTNe+vBkpZtEQUmp/679lZyUDlbGhco3wR6byIhjJ9BTIAMCK4/Mk1ZNGI= X-Yahoo-Newman-Id: 56819.32521.bm@smtp101.prem.mail.ac4.yahoo.com X-Yahoo-Newman-Property: ymail-3 X-YMail-OSG: aGVlwXoVM1l2uikP0tgeYlJKyKCX9P8mqauHVAH63dHb2hu zrafp.TbzM9M5lLYSX4JmfX6kLS1ukHVLc2GnTf57eF8ihvFgMFI.Vhhglyk Q7.XgOds5o9wYUuYmEsp8w0Umy5CduQT2UYT3ZUzjYxmElIXrJ8MO_hGTAiS zJFOcedJfMcpo0xUR4tx1PWUK0KVh1HF1noB4CmH03Myu_t.oVh_DcmDeQgo rTbl77RmhUFcClcGQJdbY2G8UokvIHK435Y6fFMH6myniGLrFPZPSqh3t4cJ 74Peq39peWwozMWVbsZ5LXfQTn8mAK2v6IKebq7w6mDv7wJGRmaQQjPIeHPi mciQFDCeYPLhXDewkmluEHPx8LIKwFD3E8V0sLuTI_y_lnQoxLDqMuzIuaHE y.pZ5rZRqRutQpitYykVFLkILo7BNvpaKlXyY8cJym6HfZ2v5Io3Zwg9mQvZ xXry_ X-Yahoo-SMTP: mpGJl6eswBD2IBufoVEg0Pa8gg-- Received: from WolfDog (polobo@24.93.23.188 with login) by smtp101.prem.mail.ac4.yahoo.com with SMTP; 20 Nov 2012 09:36:31 -0800 PST From: "David Johnston" To: , Subject: {Not A Bug} RE: BUG #7685: last_value() not consistent throughout window partition Date: Tue, 20 Nov 2012 12:36:15 -0500 Message-ID: <010501cdc745$8ac660e0$a05322a0$@yahoo.com> MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Mailer: Microsoft Outlook 14.0 Thread-Index: Ac3HRYpUn7RgIGF0RnqYE4BiJFJ6Tg== Content-Language: en-us X-Pg-Spam-Score: -2.3 (--) X-Archive-Number: 201211/166 X-Sequence-Number: 34724 > -----Original Message----- > From: pgsql-bugs-owner@postgresql.org [mailto:pgsql-bugs- > owner@postgresql.org] On Behalf Of wes@devauld.ca > Sent: Tuesday, November 20, 2012 10:28 AM > To: pgsql-bugs@postgresql.org > Subject: [BUGS] BUG #7685: last_value() not consistent throughout = window > partition >=20 > The following bug has been logged on the website: >=20 > Bug reference: 7685 > Logged by: Wes Devauld > Email address: wes@devauld.ca > PostgreSQL version: 9.2.1 > Operating system: Windows 7 Enterprise > Description: >=20 > -- The last_value(0 window function appears to be changing values > -- mid window. An example is detailed below. >=20 > ----SELECT version(); > -- version > ------------------------------------------------------------- > -- PostgreSQL 9.1.6, compiled by Visual C++ build 1500, 32-bit >=20 > ------------------------- >=20 > -- **** DB DUMP **** > -- > -- PostgreSQL database dump > -- >=20 > -- Dumped from database version 9.1.4 > -- Dumped by pg_dump version 9.1.4 > -- Started on 2012-11-19 15:01:41 >=20 > SET statement_timeout =3D 0; > SET client_encoding =3D 'UTF8'; > SET standard_conforming_strings =3D on; > SET check_function_bodies =3D false; > SET client_min_messages =3D warning; >=20 > SET search_path =3D public, pg_catalog; >=20 > SET default_tablespace =3D ''; >=20 > SET default_with_oids =3D false; >=20 > -- > -- TOC entry 170 (class 1259 OID 25186) > -- Dependencies: 5 > -- Name: bug_report; Type: TABLE; Schema: public; Owner: postgres; > Tablespace: > -- >=20 > CREATE TABLE bug_report ( > d1 date NOT NULL, > d2 date NOT NULL, > v real NOT NULL > ); >=20 >=20 > ALTER TABLE public.bug_report OWNER TO postgres; >=20 > -- > -- TOC entry 1880 (class 0 OID 25186) > -- Dependencies: 170 > -- Data for Name: bug_report; Type: TABLE DATA; Schema: public; Owner: > postgres > -- >=20 > COPY bug_report (d1, d2, v) FROM stdin; > 2012-11-19 2012-11-07 358363 > 2012-11-18 2012-11-07 358363 > 2012-11-17 2012-11-07 358363 > 2012-11-16 2012-11-07 257572 > 2012-11-15 2012-11-07 257572 > 2012-11-14 2012-11-07 257572 > 2012-11-13 2012-11-07 257572 > 2012-11-12 2012-11-07 257572 > 2012-11-11 2012-11-07 257572 > 2012-11-10 2012-11-07 257572 > \. >=20 >=20 > -- Completed on 2012-11-19 15:01:42 >=20 > --**** Description **** >=20 > select d2, last_value(v) over (partition by d2 order by d1), > first_value(v) over (partition by d2 order by d1 desc) from = bug_report >=20 > --Returns: > --"2012-11-07";358363;358363 > --"2012-11-07";358363;358363 > --"2012-11-07";358363;358363 > --"2012-11-07";257572;358363 > --"2012-11-07";257572;358363 > --"2012-11-07";257572;358363 > --"2012-11-07";257572;358363 > --"2012-11-07";257572;358363 > --"2012-11-07";257572;358363 > --"2012-11-07";257572;358363 >=20 > --You can see that the last_value() > --switches in the middle of the > --partition for the window function > --when both columns should be > --identical since the first/last > --and asc/desc changes should > --cancel each other out. >=20 >=20 > --Thanks, and good work with the product. >=20 Not A Bug. When you add an "Order By" the resultant window FRAME is from the first = record (of the partition) up-to-and-including the current record (of the = partition); no "future" records (in the partition) are part of the = window FRAME. Thus for "last_value" you are always implicitly referring = to the current row (of the FRAME). This is defined and documented = behavior. In order to use "last_value" you need to override the FRAME = that the window operates on to include all rows. http://www.postgresql.org/docs/9.2/interactive/functions-window.html - = read the second paragraph following the table and explore from there David J.