Re: BUG #10972: string_agg function incorrectly concatenating varying delimiter

From: Jeff Fischer <jeff(at)goaldriven(dot)com>
To: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, "pgsql-bugs(at)postgresql(dot)org" <pgsql-bugs(at)postgresql(dot)org>, Dave Yarnall <dave(at)goaldriven(dot)com>
Subject: Re: BUG #10972: string_agg function incorrectly concatenating varying delimiter
Date: 2014-07-22 20:50:43
Message-ID: A9E08E7AD090E449A9A4DE22199AB4D9D432ED51@EXMBX09.netplexity.local
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Thank you very much Pavel.

Thanks,
Jeff

From: Pavel Stehule [mailto:pavel(dot)stehule(at)gmail(dot)com]
Sent: Monday, July 21, 2014 10:42 PM
To: Jeff Fischer
Cc: Tom Lane; pgsql-bugs(at)postgresql(dot)org; Dave Yarnall
Subject: Re: [BUGS] BUG #10972: string_agg function incorrectly concatenating varying delimiter

Hello

2014-07-21 22:34 GMT+02:00 Jeff Fischer <jeff(at)goaldriven(dot)com<mailto:jeff(at)goaldriven(dot)com>>:
Hi Tom,

I just read the Wikipedia article on you, fun. Glad to make your acquaintance. My business partner, Dave Yarnall, went to Carnegie Mellon (CCed).

I suppose principles can be relative, but I'll assume you mean good principles and give it a shot.

Primarily, I'd consider whether another function uses non-deterministic rows for its evaluation. I could be wrong, but I don't think any other function uses two different rows results within a single function evaluation. Even aggregates, such as string_agg, evaluate one row at a time which is a well-known behavior.

A similar paradigm might be in general programming if a compiled program randomly chose values off of the stack to place as a parameter into a method call (function 3's parameters are passed into function2). An odd and unexpected behavior for the SQL language and really any language, I think. Although, it is quite creative.

It sounds like you've quickly isolated the line within the source. In an interest in learning more about the code, would you mind pointing my partner and I to the line for this bug?

https://github.com/postgres/postgres/blob/master/src/backend/utils/adt/varlena.c
search string_agg

Regards
Pavel

Thanks,
Jeff

-----Original Message-----
From: Tom Lane [mailto:tgl(at)sss(dot)pgh(dot)pa(dot)us<mailto:tgl(at)sss(dot)pgh(dot)pa(dot)us>]
Sent: Wednesday, July 16, 2014 7:20 AM
To: Jeff Fischer
Cc: pgsql-bugs(at)postgresql(dot)org<mailto:pgsql-bugs(at)postgresql(dot)org>
Subject: Re: [BUGS] BUG #10972: string_agg function incorrectly concatenating varying delimiter

jeff(at)goaldriven(dot)com<mailto:jeff(at)goaldriven(dot)com> writes:
> Running the query below will show how the delimiter for the current
> row is actually the subsequent rows delimiter.

Hmm, well, the documentation for string_agg doesn't say what happens when the "delimiter" argument varies across rows; but a quick look at the code finds that the first-call delimiter isn't actually used at all, and on subsequent calls the delimiter is appended to the running result before the associated value is. Which seems to me to be at least as reasonable, and certainly a great deal easier to implement, as what you seem to have in mind. Can you offer a principled argument why it should be the other way around?

regards, tom lane

--
Sent via pgsql-bugs mailing list (pgsql-bugs(at)postgresql(dot)org<mailto:pgsql-bugs(at)postgresql(dot)org>)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Bruce Momjian 2014-07-22 22:34:49 Re: pg_upgrade < 9.3 -> >=9.3 misses a step around multixacts
Previous Message Burgess, Freddie 2014-07-22 15:13:38 Re: PostgreSQL 9.3.4 Query Problems