pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Problem with Lateral ?
6+ messages / 4 participants
[nested] [flat]

* Problem with Lateral ?
@ 2017-08-31 21:15  benj.dev <benj.dev@laposte.net>
  0 siblings, 1 reply; 6+ messages in thread

From: benj.dev @ 2017-08-31 21:15 UTC (permalink / raw)
  To: pgsql-general

I Have a request that produce a bad result. I'm able to rewrite this
request in a form that always produce a good result.
But I don't understand the real reason of the problem.

I have tried to simplify the dataset but the problem doesn't appear with
less data.

The request produce 16 rows (but only 15 are expected)
If I add a filter for the first column like : col1 = 'aaa' I get the 15
expected rows.
But if I put the filter with col1 <> 'aaa' I get 0 rows and it's the
same with col1 IS NULL
col1 is not NULL give 16 rows.

Some other strange elements
- If I try to isolate the rows used in the different tables for this
request in "test" tables and use the test tables the problem doesn't appear.
- If I don't use the regexp_split_to_table function (but a simple
equality), the problem doesn't appear (but in fact with the problematic
dataset, no element contain the split separator).
- If I remove the "sub.evnt IS NULL OR" part from the (sub.evnt IS NULL
OR p.evnt IN (SELECT regexp_split_to_table(sub.evnt,'#'))) the problem
doesn't appear.
- The "cnt" can be different even if the input data are the same.
- If I remove the "ORDER BY x.id" I can obtain more rows.

So the idea is not to find a good way to write the request but just
understand why these problem of "virtual" rows appears.
Is the problem related to the imbrication of the LATERAL clause and
regexp_split_to_table or not ?

So the request was :

The version of Postgres is "PostgreSQL 9.3.6 on x86
64-unknown-linux-gnu, compiled by gcc (Ubuntu 4.8.2-19Ubuntu1) 4.8.2,
64-bit"

===
SELECT DISTINCT col1, date_start, date_end, evt, x.*
FROM (
 SELECT f.*, c.evt
 FROM tmp.debug1 f
 LEFT JOIN tmp.debug2 c ON f.sport=c.sport AND f.compet=c.code
) sub
LEFT JOIN LATERAL
 (SELECT x.id, x.nom,
  (SELECT count(*)
   FROM mother_table_with_some_herited_childs p
   WHERE p.id = x.id
   AND date_evt::date BETWEEN sub.date_start AND sub.date_end
   AND (sub.evt IS NULL OR p.evt IN (SELECT
regexp_split_to_table(sub.evt,'#')))
  ) as cnt
  FROM tmp.debug3 c
  LEFT JOIN LATERAL (SELECT * FROM identify i WHERE i.id = c.id) x ON TRUE
  WHERE c.hash = sub.hash
  ORDER BY x.id
) x ON true
--WHERE col1 = 'aaa' -- <> 'aaa' -- IS NULL -- IS NOT NULL
===

Thanks for your ideas


-- 
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general



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

* Re: Problem with Lateral ?
@ 2017-08-31 22:42  Tom Lane <tgl@sss.pgh.pa.us>
  parent: benj.dev <benj.dev@laposte.net>
  0 siblings, 0 replies; 6+ messages in thread

From: Tom Lane @ 2017-08-31 22:42 UTC (permalink / raw)
  To: benj.dev <benj.dev@laposte.net>; +Cc: pgsql-general

"benj.dev" <benj.dev@laposte.net> writes:
> I Have a request that produce a bad result. I'm able to rewrite this
> request in a form that always produce a good result.

It's not at all clear from the amount of info provided whether you've
hit a Postgres bug or are just misunderstanding the semantics of your
query.  However ...

> The version of Postgres is "PostgreSQL 9.3.6 on x86
> 64-unknown-linux-gnu, compiled by gcc (Ubuntu 4.8.2-19Ubuntu1) 4.8.2,
> 64-bit"

... that's a rather old version of Postgres; we released 9.3.19 yesterday.
And a quick perusal of the 9.3-branch commit log finds quite a few
LATERAL-related bug fixes that went in since 9.3.6.  You might consider
updating to 9.3.something-recent and see if anything changes.

			regards, tom lane


-- 
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general



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

* Problem with LATERAL
@ 2022-10-13 07:05  Eagna <eagna@protonmail.com>
  0 siblings, 1 reply; 6+ messages in thread

From: Eagna @ 2022-10-13 07:05 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>



Good Morning all,

I am having a problem understanding a simple LATERAL join - I'm working on grasping them.

All tables and data are at the bottom of this question and on the fiddles, SQL Server (working) and Postgres (not working).

SQL Server fiddle - https://dbfiddle.uk/hjBBd87B

Postgres fiddle - https://dbfiddle.uk/PihnqTwG

I have the following  - create table scripts and sample data are at end of this question and on the fiddles.

The query in question which works on SQL Server is the following:

SELECT  o.order_id,
  o.total_price - COALESCE(p.paid, 0) AS remaining
FROM _order o
CROSS APPLY (
    SELECT SUM(p.amount) AS paid
    FROM payment p
    WHERE p.order_id = o.order_id
) AS p
WHERE o.total_price > ISNULL(p.paid, 0);

relatively simple one would have thought! I tried to convert this into a Postgres query as follows:

SELECT  o.order_id,
  o.total_price - COALESCE(sub.paid, 0)
FROM _order o
LEFT JOIN LATERAL (
    SELECT SUM(p.amount) AS paid
    FROM payment p
    WHERE p.order_id = o.order_id
) AS sub
WHERE o.total_price > ISNULL(sub.paid, 0);  -- << line 10 - Error occurs!

but I receive the error:

    ERROR:  syntax error at or near "WHERE"
    LINE 10: WHERE o.total_price > ISNULL(sub.paid, 0);


I would be grateful if I could get a working query and also for an explanation as to what it is I'm doing incorrectly?

The payment table has no primary key. Any input on the suitability of creating a surrogate one would also be appreciated.


Please let me know if there's any important information missing.

Rgs and TIA,


E.


CREATE TABLE _order
(
  order_id    INT NOT NULL PRIMARY KEY,
  total_price INT NOT NULL
);

INSERT INTO _order VALUES
(1, 1000), (2, 2000), (3, 3000), (4, 4000);


CREATE TABLE payment
(
  order_id INT NOT NULL,
  amount   INT NOT NULL,
  CONSTRAINT payment_order_id_fk FOREIGN KEY (order_id) REFERENCES _order (order_id)
);

CREATE INDEX pt_order_id_ix ON payment (order_id);   -- normal indexing of foreign key field

INSERT INTO payment VALUES
(1, 500), (2, 2000), (3, 1000), (3, 500), (3, 750);  -- note - no payment for order_id = 4








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

* Re: Problem with LATERAL
@ 2022-10-13 07:30  Julien Rouhaud <rjuju123@gmail.com>
  parent: Eagna <eagna@protonmail.com>
  0 siblings, 1 reply; 6+ messages in thread

From: Julien Rouhaud @ 2022-10-13 07:30 UTC (permalink / raw)
  To: Eagna <eagna@protonmail.com>; +Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

Hi,

On Thu, Oct 13, 2022 at 07:05:48AM +0000, Eagna wrote:
>
> relatively simple one would have thought! I tried to convert this into a Postgres query as follows:
>
> SELECT  o.order_id,
>   o.total_price - COALESCE(sub.paid, 0)
> FROM _order o
> LEFT JOIN LATERAL (
>     SELECT SUM(p.amount) AS paid
>     FROM payment p
>     WHERE p.order_id = o.order_id
> ) AS sub
> WHERE o.total_price > ISNULL(sub.paid, 0);  -- << line 10 - Error occurs!
>
> but I receive the error:
>
>     ERROR:  syntax error at or near "WHERE"
>     LINE 10: WHERE o.total_price > ISNULL(sub.paid, 0);

There error here is because a JOIN clause requires a join condition.  Adding an
"ON true" is probably what you want.  You would also need to change isnull()
with coalesce().

The final query should be:

SELECT  o.order_id,
  o.total_price - COALESCE(sub.paid, 0)
FROM _order o
LEFT JOIN LATERAL (
    SELECT SUM(p.amount) AS paid
    FROM payment p
    WHERE p.order_id = o.order_id
) AS sub ON true
WHERE o.total_price > coalesce(sub.paid, 0);





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

* Re: Problem with LATERAL
@ 2022-10-13 08:04  Eagna <eagna@protonmail.com>
  parent: Julien Rouhaud <rjuju123@gmail.com>
  0 siblings, 1 reply; 6+ messages in thread

From: Eagna @ 2022-10-13 08:04 UTC (permalink / raw)
  To: Julien Rouhaud <rjuju123@gmail.com>; +Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>


> > ERROR: syntax error at or near "WHERE"
> > LINE 10: WHERE o.total_price > ISNULL(sub.paid, 0);

 
> There error here is because a JOIN clause requires a join condition. Adding an
> "ON true" is probably what you want. You would also need to change isnull()
> with coalesce().
 
> The final query should be:

...
... 
... 
> ) AS sub ON true
...
...

OK - I see that it works now - which is great! 

However, it's unclear to me what, exactly, it is that is "TRUE"? 

What am I joining to what?

The syntax is unclear to me - if I'm joining, I should be joining on tab_A.field_x = tab_B.field_y - no?

Why does SQL Server's OUTER APPLY not require this?

Thanks for any input - Merci, À+

Rgs,

E.







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

* Re: Problem with LATERAL
@ 2022-10-13 08:34  Julien Rouhaud <rjuju123@gmail.com>
  parent: Eagna <eagna@protonmail.com>
  0 siblings, 0 replies; 6+ messages in thread

From: Julien Rouhaud @ 2022-10-13 08:34 UTC (permalink / raw)
  To: Eagna <eagna@protonmail.com>; +Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

On Thu, Oct 13, 2022 at 08:04:03AM +0000, Eagna wrote:
>
> > > ERROR: syntax error at or near "WHERE"
> > > LINE 10: WHERE o.total_price > ISNULL(sub.paid, 0);
>
>
> > There error here is because a JOIN clause requires a join condition. Adding an
> > "ON true" is probably what you want. You would also need to change isnull()
> > with coalesce().
>
> > The final query should be:
>
> ...
> ...
> ...
> > ) AS sub ON true
> ...
> ...
>
> OK - I see that it works now - which is great!
>
> However, it's unclear to me what, exactly, it is that is "TRUE"?
>
> What am I joining to what?
>
> The syntax is unclear to me - if I'm joining, I should be joining on tab_A.field_x = tab_B.field_y - no?

Well, yes but the join condition in that case is already in the WHERE clause in
the sub select, so trying to put an actual join clause would be unnecessary and
add extra cost.

But I'm not sure why you want a LATERAL clause in the first place, wouldn't
this query have the same meaning?

SELECT  o.order_id,
  o.total_price - coalesce(sum(p.amount), 0)
FROM _order o
LEFT JOIN payment p ON p.order_id = o.order_id
GROUP BY o.order_id, o.total_price
HAVING o.total_price > coalesce(sum(p.amount), 0);

It should perform better if you have a lot of orders, as it can be executed
with something better than a nested loop.

> Why does SQL Server's OUTER APPLY not require this?

I don't know much about sql server, I'm assuming CROSS APPLY is an alias for
LEFT JOIN LATERAL () ON TRUE.





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


end of thread, other threads:[~2022-10-13 08:34 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2017-08-31 21:15 Problem with Lateral ? benj.dev <benj.dev@laposte.net>
2017-08-31 22:42 ` Tom Lane <tgl@sss.pgh.pa.us>
2022-10-13 07:05 Problem with LATERAL Eagna <eagna@protonmail.com>
2022-10-13 07:30 ` Re: Problem with LATERAL Julien Rouhaud <rjuju123@gmail.com>
2022-10-13 08:04   ` Re: Problem with LATERAL Eagna <eagna@protonmail.com>
2022-10-13 08:34     ` Re: Problem with LATERAL Julien Rouhaud <rjuju123@gmail.com>

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