pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Wrong aggregate result when sorting by a NULL value
16+ messages / 5 participants
[nested] [flat]

Commits from this thread:
  2018-11-04 3e0b05a756 6f4e01c7d2  Fix unused-variable warning.
  2018-11-03 793beab37e 6eb31cedb3  Prevent generating EEOP_AGG_STRICT_INPUT_CHECK operations when nargs == 0.
  2018-11-03 4c640f4f38 fd59b29c87  Fix STRICT check for strict aggregates with NULL ORDER BY columns.
* Wrong aggregate result when sorting by a NULL value
@ 2018-11-02 13:29  Ondřej Bouda <obouda@email.cz>
  0 siblings, 1 reply; 16+ messages in thread

From: Ondřej Bouda @ 2018-11-02 13:29 UTC (permalink / raw)
  To: pgsql-bugs

Dear PostgreSQLers,

the following seems as a bug to me on Postgres 11.0:


CREATE FUNCTION first_arg(ANYELEMENT, ANYELEMENT) RETURNS ANYELEMENT
AS $function$
     SELECT $1
$function$ LANGUAGE SQL IMMUTABLE STRICT;

CREATE AGGREGATE first(ANYELEMENT) (
     SFUNC = first_arg,
     STYPE = ANYELEMENT
);

CREATE TABLE t (
     x TEXT,
     y INT,
     z DATE
);
INSERT INTO t (x, y, z) VALUES ('val', 42, NULL);

SELECT first(x ORDER BY y) FROM t; -- returns 'val', as expected
SELECT first(x ORDER BY y, z) FROM t; -- returns NULL, which seems wrong


I would expect both the SELECT statements to return 'val'. Additional 
order by "z" should make no difference as there is just one row in the 
table.

More interestingly, if "z" is not NULL, the result is correct:

UPDATE t SET z = CURRENT_DATE;

SELECT first(x ORDER BY y) FROM t; -- returns 'val'
SELECT first(x ORDER BY y, z) FROM t; -- returns 'val'


The documentation [https://www.postgresql.org/docs/11/static/xaggr.html] 
says that if the state function is STRICT, the first non-NULL value is 
automatically used as the initial state. The ORDER BY option is not 
documented to have any effect on this - the documentation just says that 
"[DISTINCT and ORDER BY] options are implemented behind the scenes and 
are not the concern of the aggregate's support functions."

Do I miss something, or is it really a bug?

Best regards,
Ondrej Bouda




^ permalink  raw  reply  [nested|flat] 16+ messages in thread

* Re: Wrong aggregate result when sorting by a NULL value
@ 2018-11-02 15:07  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Ondřej Bouda <obouda@email.cz>
  0 siblings, 1 reply; 16+ messages in thread

From: Tom Lane @ 2018-11-02 15:07 UTC (permalink / raw)
  To: Ondřej Bouda <obouda@email.cz>; +Cc: pgsql-bugs

=?UTF-8?Q?Ond=c5=99ej_Bouda?= <obouda@email.cz> writes:
> the following seems as a bug to me on Postgres 11.0:

Yeah, somebody broke this between 10.x and 11.0.  You don't need the
custom aggregate, even plain min() fails:

regression=# SELECT min(x ORDER BY z) FROM t;
 min 
-----
 
(1 row)

while 10.5 delivers the expected result:

regression=# SELECT min(x ORDER BY z) FROM t;
 min 
-----
 val
(1 row)

I've not looked at the code yet, but it's acting like somebody changed the
STRICT logic from "are any of the aggregate's arguments null" to "is any
part of the whole row (including ordering values) null".  Wrong ...

			regards, tom lane




^ permalink  raw  reply  [nested|flat] 16+ messages in thread

* Re: Wrong aggregate result when sorting by a NULL value
@ 2018-11-02 15:34  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 16+ messages in thread

From: Tom Lane @ 2018-11-02 15:34 UTC (permalink / raw)
  To: Ondřej Bouda <obouda@email.cz>; +Cc: Andres Freund <andres@anarazel.de>; pgsql-bugs

I wrote:
> =?UTF-8?Q?Ond=c5=99ej_Bouda?= <obouda@email.cz> writes:
>> the following seems as a bug to me on Postgres 11.0:

> Yeah, somebody broke this between 10.x and 11.0. ...
> I've not looked at the code yet, but it's acting like somebody changed the
> STRICT logic from "are any of the aggregate's arguments null" to "is any
> part of the whole row (including ordering values) null".  Wrong ...

git bisect fingers this:

commit 69c3936a1499b772a749ae629fc59b2d72722332
Author: Andres Freund <andres@anarazel.de>
Date:   Tue Jan 9 13:25:38 2018 -0800

    Expression evaluation based aggregate transition invocation.

Andres, do you have time to look at this right now?

			regards, tom lane




^ permalink  raw  reply  [nested|flat] 16+ messages in thread

* Re: Wrong aggregate result when sorting by a NULL value
@ 2018-11-02 15:40  Andres Freund <andres@anarazel.de>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 16+ messages in thread

From: Andres Freund @ 2018-11-02 15:40 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Ondřej Bouda <obouda@email.cz>; pgsql-bugs

Hi,

On 2018-11-02 11:34:46 -0400, Tom Lane wrote:
> I wrote:
> > =?UTF-8?Q?Ond=c5=99ej_Bouda?= <obouda@email.cz> writes:
> >> the following seems as a bug to me on Postgres 11.0:
> 
> > Yeah, somebody broke this between 10.x and 11.0. ...
> > I've not looked at the code yet, but it's acting like somebody changed the
> > STRICT logic from "are any of the aggregate's arguments null" to "is any
> > part of the whole row (including ordering values) null".  Wrong ...
> 
> git bisect fingers this:
> 
> commit 69c3936a1499b772a749ae629fc59b2d72722332
> Author: Andres Freund <andres@anarazel.de>
> Date:   Tue Jan 9 13:25:38 2018 -0800
> 
>     Expression evaluation based aggregate transition invocation.
> 
> Andres, do you have time to look at this right now?

Thanks for bisecting.  I'll take a look later today.

Greetings,

Andres Freund




^ permalink  raw  reply  [nested|flat] 16+ messages in thread

* Re: Wrong aggregate result when sorting by a NULL value
@ 2018-11-02 17:34  Andrew Gierth <andrew@tao11.riddles.org.uk>
  parent: Andres Freund <andres@anarazel.de>
  0 siblings, 1 reply; 16+ messages in thread

From: Andrew Gierth @ 2018-11-02 17:34 UTC (permalink / raw)
  To: Andres Freund <andres@anarazel.de>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Ondřej Bouda <obouda@email.cz>; pgsql-bugs

>>>>> "Andres" == Andres Freund <andres@anarazel.de> writes:

 >> Andres, do you have time to look at this right now?

 Andres> Thanks for bisecting.  I'll take a look later today.

Looks like this:

+           scratch.d.agg_strict_input_check.nargs = numInputs;

should have been pertrans->numTransInputs instead?

-- 
Andrew (irc:RhodiumToad)




^ permalink  raw  reply  [nested|flat] 16+ messages in thread

* Re: Wrong aggregate result when sorting by a NULL value
@ 2018-11-03 05:57  Andres Freund <andres@anarazel.de>
  parent: Andrew Gierth <andrew@tao11.riddles.org.uk>
  0 siblings, 1 reply; 16+ messages in thread

From: Andres Freund @ 2018-11-03 05:57 UTC (permalink / raw)
  To: Andrew Gierth <andrew@tao11.riddles.org.uk>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Ondřej Bouda <obouda@email.cz>; pgsql-bugs

Hi,

On 2018-11-02 17:34:43 +0000, Andrew Gierth wrote:
> >>>>> "Andres" == Andres Freund <andres@anarazel.de> writes:
> 
>  >> Andres, do you have time to look at this right now?
> 
>  Andres> Thanks for bisecting.  I'll take a look later today.
> 
> Looks like this:
> 
> +           scratch.d.agg_strict_input_check.nargs = numInputs;
> 
> should have been pertrans->numTransInputs instead?

That looks like it's precisely the reason. I'll push something tomorrow
PST morning, crediting both you (diagnosis) and Tom (bisecting)
obviously, unless you prefer to do so yourself.

It's a bit sad that we don't have any tests that test this :/. I'll add
something for the specific case, but that'll obviously not be
exhaustive.

Greetings,

Andres Freund




^ permalink  raw  reply  [nested|flat] 16+ messages in thread

* Re: Wrong aggregate result when sorting by a NULL value
@ 2018-11-03 21:50  Andres Freund <andres@anarazel.de>
  parent: Andres Freund <andres@anarazel.de>
  0 siblings, 1 reply; 16+ messages in thread

From: Andres Freund @ 2018-11-03 21:50 UTC (permalink / raw)
  To: Andrew Gierth <andrew@tao11.riddles.org.uk>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Ondřej Bouda <obouda@email.cz>; pgsql-bugs

Hi,

On 2018-11-02 22:57:00 -0700, Andres Freund wrote:
> On 2018-11-02 17:34:43 +0000, Andrew Gierth wrote:
> > >>>>> "Andres" == Andres Freund <andres@anarazel.de> writes:
> > 
> >  >> Andres, do you have time to look at this right now?
> > 
> >  Andres> Thanks for bisecting.  I'll take a look later today.
> > 
> > Looks like this:
> > 
> > +           scratch.d.agg_strict_input_check.nargs = numInputs;
> > 
> > should have been pertrans->numTransInputs instead?
> 
> That looks like it's precisely the reason. I'll push something tomorrow
> PST morning, crediting both you (diagnosis) and Tom (bisecting)
> obviously, unless you prefer to do so yourself.
> 
> It's a bit sad that we don't have any tests that test this :/. I'll add
> something for the specific case, but that'll obviously not be
> exhaustive.

And pushed. Thanks Ondřej for the report, thanks Tom & Andrew for
identifying the issue.

Greetings,

Andres Freund




^ permalink  raw  reply  [nested|flat] 16+ messages in thread

* Re: Wrong aggregate result when sorting by a NULL value
@ 2018-11-03 22:18  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Andres Freund <andres@anarazel.de>
  0 siblings, 1 reply; 16+ messages in thread

From: Tom Lane @ 2018-11-03 22:18 UTC (permalink / raw)
  To: Andres Freund <andres@anarazel.de>; +Cc: Andrew Gierth <andrew@tao11.riddles.org.uk>; Ondřej Bouda <obouda@email.cz>; pgsql-bugs

Andres Freund <andres@anarazel.de> writes:
> And pushed. Thanks Ondřej for the report, thanks Tom & Andrew for
> identifying the issue.

Hm, buildfarm seems less than pleased.  Did you miss making a
corresponding change in the JIT code?

			regards, tom lane




^ permalink  raw  reply  [nested|flat] 16+ messages in thread

* Re: Wrong aggregate result when sorting by a NULL value
@ 2018-11-03 22:27  Andres Freund <andres@anarazel.de>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 16+ messages in thread

From: Andres Freund @ 2018-11-03 22:27 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Andrew Gierth <andrew@tao11.riddles.org.uk>; Ondřej Bouda <obouda@email.cz>; pgsql-bugs

On 2018-11-03 18:18:40 -0400, Tom Lane wrote:
> Andres Freund <andres@anarazel.de> writes:
> > And pushed. Thanks Ondřej for the report, thanks Tom & Andrew for
> > identifying the issue.
> 
> Hm, buildfarm seems less than pleased.  Did you miss making a
> corresponding change in the JIT code?

Hm, I'm somewhat confused, let me look into that. The JIT code shouldn't
really need to be changed here - it's the *generation* of expression
steps that's going wrong - which then later get turned into JITed code,
but that part worked previously for other expressions.

Greetings,

Andres Freund




^ permalink  raw  reply  [nested|flat] 16+ messages in thread

* Re: Wrong aggregate result when sorting by a NULL value
@ 2018-11-03 23:08  Andres Freund <andres@anarazel.de>
  parent: Andres Freund <andres@anarazel.de>
  0 siblings, 1 reply; 16+ messages in thread

From: Andres Freund @ 2018-11-03 23:08 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Andrew Gierth <andrew@tao11.riddles.org.uk>; Ondřej Bouda <obouda@email.cz>; pgsql-bugs

Hi,

On 2018-11-03 15:27:09 -0700, Andres Freund wrote:
> On 2018-11-03 18:18:40 -0400, Tom Lane wrote:
> > Andres Freund <andres@anarazel.de> writes:
> > > And pushed. Thanks Ondřej for the report, thanks Tom & Andrew for
> > > identifying the issue.
> > 
> > Hm, buildfarm seems less than pleased.  Did you miss making a
> > corresponding change in the JIT code?
> 
> Hm, I'm somewhat confused, let me look into that. The JIT code shouldn't
> really need to be changed here - it's the *generation* of expression
> steps that's going wrong - which then later get turned into JITed code,
> but that part worked previously for other expressions.

Turns out it's not a great idea to generate EEOP_AGG_STRICT_INPUT_CHECK
expressions with nargs = 0. Head -> Desk.  Pushed a fix (+ new
assertion).

Thanks!

Andres Freund




^ permalink  raw  reply  [nested|flat] 16+ messages in thread

* Re: Wrong aggregate result when sorting by a NULL value
@ 2018-11-04 14:11  Jeff Janes <jeff.janes@gmail.com>
  parent: Andres Freund <andres@anarazel.de>
  0 siblings, 1 reply; 16+ messages in thread

From: Jeff Janes @ 2018-11-04 14:11 UTC (permalink / raw)
  To: Andres Freund <andres@anarazel.de>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Andrew Gierth <andrew@tao11.riddles.org.uk>; pgsql-bugs

On Sat, Nov 3, 2018 at 7:08 PM Andres Freund <andres@anarazel.de> wrote:

> >
> > Hm, I'm somewhat confused, let me look into that. The JIT code shouldn't
> > really need to be changed here - it's the *generation* of expression
> > steps that's going wrong - which then later get turned into JITed code,
> > but that part worked previously for other expressions.
>
> Turns out it's not a great idea to generate EEOP_AGG_STRICT_INPUT_CHECK
> expressions with nargs = 0. Head -> Desk.  Pushed a fix (+ new
> assertion).
>
>
I'm now getting a compiler warning:

execExpr.c: In function 'ExecBuildAggTrans':
execExpr.c:2864:7: warning: unused variable 'numInputs' [-Wunused-variable]
   int   numInputs = pertrans->numInputs;

Cheers,

Jeff

^ permalink  raw  reply  [nested|flat] 16+ messages in thread

* Re: Wrong aggregate result when sorting by a NULL value
@ 2018-11-04 16:19  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Jeff Janes <jeff.janes@gmail.com>
  0 siblings, 1 reply; 16+ messages in thread

From: Tom Lane @ 2018-11-04 16:19 UTC (permalink / raw)
  To: Jeff Janes <jeff.janes@gmail.com>; +Cc: Andres Freund <andres@anarazel.de>; Andrew Gierth <andrew@tao11.riddles.org.uk>; pgsql-bugs

Jeff Janes <jeff.janes@gmail.com> writes:
> I'm now getting a compiler warning:

> execExpr.c: In function 'ExecBuildAggTrans':
> execExpr.c:2864:7: warning: unused variable 'numInputs' [-Wunused-variable]
>    int   numInputs = pertrans->numInputs;

Used-for-asserts-only problem.  Will fix.

			regards, tom lane




^ permalink  raw  reply  [nested|flat] 16+ messages in thread

* Re: Wrong aggregate result when sorting by a NULL value
@ 2018-11-04 23:44  Andres Freund <andres@anarazel.de>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 16+ messages in thread

From: Andres Freund @ 2018-11-04 23:44 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Jeff Janes <jeff.janes@gmail.com>; Andrew Gierth <andrew@tao11.riddles.org.uk>; pgsql-bugs

On 2018-11-04 11:19:59 -0500, Tom Lane wrote:
> Jeff Janes <jeff.janes@gmail.com> writes:
> > I'm now getting a compiler warning:
> 
> > execExpr.c: In function 'ExecBuildAggTrans':
> > execExpr.c:2864:7: warning: unused variable 'numInputs' [-Wunused-variable]
> >    int   numInputs = pertrans->numInputs;
> 
> Used-for-asserts-only problem.  Will fix.

Thanks.  I wonder if we shouldn't turn Asserts() into something roughly
akin to if (0) {expr};. That way we'd not deal with errors about unused
variables anymore - we're not safe against unreachable code warnings
anyway.

Greetings,

Andres Freund




^ permalink  raw  reply  [nested|flat] 16+ messages in thread

* Re: Wrong aggregate result when sorting by a NULL value
@ 2018-11-04 23:49  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Andres Freund <andres@anarazel.de>
  0 siblings, 1 reply; 16+ messages in thread

From: Tom Lane @ 2018-11-04 23:49 UTC (permalink / raw)
  To: Andres Freund <andres@anarazel.de>; +Cc: Jeff Janes <jeff.janes@gmail.com>; Andrew Gierth <andrew@tao11.riddles.org.uk>; pgsql-bugs

Andres Freund <andres@anarazel.de> writes:
> On 2018-11-04 11:19:59 -0500, Tom Lane wrote:
>> Used-for-asserts-only problem.  Will fix.

> Thanks.  I wonder if we shouldn't turn Asserts() into something roughly
> akin to if (0) {expr};. That way we'd not deal with errors about unused
> variables anymore - we're not safe against unreachable code warnings
> anyway.

Meh.  I'm unexcited about getting rid of one type of compiler warning by
introducing another one.

			regards, tom lane




^ permalink  raw  reply  [nested|flat] 16+ messages in thread

* Re: Wrong aggregate result when sorting by a NULL value
@ 2018-11-05 00:09  Andres Freund <andres@anarazel.de>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 16+ messages in thread

From: Andres Freund @ 2018-11-05 00:09 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Jeff Janes <jeff.janes@gmail.com>; Andrew Gierth <andrew@tao11.riddles.org.uk>; pgsql-bugs

On 2018-11-04 18:49:40 -0500, Tom Lane wrote:
> Andres Freund <andres@anarazel.de> writes:
> > On 2018-11-04 11:19:59 -0500, Tom Lane wrote:
> >> Used-for-asserts-only problem.  Will fix.
> 
> > Thanks.  I wonder if we shouldn't turn Asserts() into something roughly
> > akin to if (0) {expr};. That way we'd not deal with errors about unused
> > variables anymore - we're not safe against unreachable code warnings
> > anyway.
> 
> Meh.  I'm unexcited about getting rid of one type of compiler warning by
> introducing another one.

It's impracticable to enable dead code warnings in postgres
anyway. There's way way too many of them (all the returns after
elog(ERROR) etc are enough to make it infeasible to change that).

Greetings,

Andres Freund




^ permalink  raw  reply  [nested|flat] 16+ messages in thread

* Re: Wrong aggregate result when sorting by a NULL value
@ 2018-11-05 00:15  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Andres Freund <andres@anarazel.de>
  0 siblings, 0 replies; 16+ messages in thread

From: Tom Lane @ 2018-11-05 00:15 UTC (permalink / raw)
  To: Andres Freund <andres@anarazel.de>; +Cc: Jeff Janes <jeff.janes@gmail.com>; Andrew Gierth <andrew@tao11.riddles.org.uk>; pgsql-bugs

Andres Freund <andres@anarazel.de> writes:
> On 2018-11-04 18:49:40 -0500, Tom Lane wrote:
>> Meh.  I'm unexcited about getting rid of one type of compiler warning by
>> introducing another one.

> It's impracticable to enable dead code warnings in postgres
> anyway. There's way way too many of them (all the returns after
> elog(ERROR) etc are enough to make it infeasible to change that).

I think you have a very limited conception of what sorts of warnings
code like that would trigger.  In particular, even given the
unsubstantiated assumption that nobody's compiler would complain,
I think static analyzers like Coverity might.

			regards, tom lane




^ permalink  raw  reply  [nested|flat] 16+ messages in thread


end of thread, other threads:[~2018-11-05 00:15 UTC | newest]

Thread overview: 16+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-11-02 13:29 Wrong aggregate result when sorting by a NULL value Ondřej Bouda <obouda@email.cz>
2018-11-02 15:07 ` Tom Lane <tgl@sss.pgh.pa.us>
2018-11-02 15:34   ` Tom Lane <tgl@sss.pgh.pa.us>
2018-11-02 15:40     ` Andres Freund <andres@anarazel.de>
2018-11-02 17:34       ` Andrew Gierth <andrew@tao11.riddles.org.uk>
2018-11-03 05:57         ` Andres Freund <andres@anarazel.de>
2018-11-03 21:50           ` Andres Freund <andres@anarazel.de>
2018-11-03 22:18             ` Tom Lane <tgl@sss.pgh.pa.us>
2018-11-03 22:27               ` Andres Freund <andres@anarazel.de>
2018-11-03 23:08                 ` Andres Freund <andres@anarazel.de>
2018-11-04 14:11                   ` Jeff Janes <jeff.janes@gmail.com>
2018-11-04 16:19                     ` Tom Lane <tgl@sss.pgh.pa.us>
2018-11-04 23:44                       ` Andres Freund <andres@anarazel.de>
2018-11-04 23:49                         ` Tom Lane <tgl@sss.pgh.pa.us>
2018-11-05 00:09                           ` Andres Freund <andres@anarazel.de>
2018-11-05 00:15                             ` Tom Lane <tgl@sss.pgh.pa.us>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox