Improve LATERAL join case in test memoize.sql

Started by Richard Guoalmost 3 years ago2 messages
#1Richard Guo
guofenglinux@gmail.com
1 attachment(s)

I happened to notice we have the case in memoize.sql that tests for
memoize node with LATERAL joins, which is

-- Try with LATERAL joins
SELECT explain_memoize('
SELECT COUNT(*),AVG(t2.unique1) FROM tenk1 t1,
LATERAL (SELECT t2.unique1 FROM tenk1 t2 WHERE t1.twenty = t2.unique1) t2
WHERE t1.unique1 < 1000;', false);

ISTM this is not the right query for the test. After the subquery being
pulled up into the parent query, there will be no lateral references any
more. I'm thinking maybe we can add an ORDER BY clause in the subquery
to prevent it from being pulled up.

-- Try with LATERAL joins
SELECT explain_memoize('
SELECT COUNT(*),AVG(t2.unique1) FROM tenk1 t1,
LATERAL (SELECT t2.unique1 FROM tenk1 t2 WHERE t1.twenty = t2.unique1 ORDER
BY 1) t2
WHERE t1.unique1 < 1000;', false);

Attach a trivial patch for the change.

Thanks
Richard

Attachments:

v1-0001-Improve-LATERAL-join-case-in-test-memoize.sql.patchapplication/octet-stream; name=v1-0001-Improve-LATERAL-join-case-in-test-memoize.sql.patchDownload
From de7050f3fa20dc9fefe8f66cc9eabbf1da6597fd Mon Sep 17 00:00:00 2001
From: Richard Guo <guofenglinux@gmail.com>
Date: Mon, 16 Jan 2023 17:12:11 +0800
Subject: [PATCH v1] Improve LATERAL join case in test memoize.sql

---
 src/test/regress/expected/memoize.out | 6 +++---
 src/test/regress/sql/memoize.sql      | 4 ++--
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/src/test/regress/expected/memoize.out b/src/test/regress/expected/memoize.out
index de43afa76e..b7fca238c4 100644
--- a/src/test/regress/expected/memoize.out
+++ b/src/test/regress/expected/memoize.out
@@ -63,7 +63,7 @@ WHERE t2.unique1 < 1000;
 -- Try with LATERAL joins
 SELECT explain_memoize('
 SELECT COUNT(*),AVG(t2.unique1) FROM tenk1 t1,
-LATERAL (SELECT t2.unique1 FROM tenk1 t2 WHERE t1.twenty = t2.unique1) t2
+LATERAL (SELECT t2.unique1 FROM tenk1 t2 WHERE t1.twenty = t2.unique1 ORDER BY 1) t2
 WHERE t1.unique1 < 1000;', false);
                                       explain_memoize                                      
 -------------------------------------------------------------------------------------------
@@ -74,7 +74,7 @@ WHERE t1.unique1 < 1000;', false);
                Rows Removed by Filter: 9000
          ->  Memoize (actual rows=1 loops=N)
                Cache Key: t1.twenty
-               Cache Mode: logical
+               Cache Mode: binary
                Hits: 980  Misses: 20  Evictions: Zero  Overflows: 0  Memory Usage: NkB
                ->  Index Only Scan using tenk1_unique1 on tenk1 t2 (actual rows=1 loops=N)
                      Index Cond: (unique1 = t1.twenty)
@@ -83,7 +83,7 @@ WHERE t1.unique1 < 1000;', false);
 
 -- And check we get the expected results.
 SELECT COUNT(*),AVG(t2.unique1) FROM tenk1 t1,
-LATERAL (SELECT t2.unique1 FROM tenk1 t2 WHERE t1.twenty = t2.unique1) t2
+LATERAL (SELECT t2.unique1 FROM tenk1 t2 WHERE t1.twenty = t2.unique1 ORDER BY 1) t2
 WHERE t1.unique1 < 1000;
  count |        avg         
 -------+--------------------
diff --git a/src/test/regress/sql/memoize.sql b/src/test/regress/sql/memoize.sql
index 17c5b4bfab..87af637828 100644
--- a/src/test/regress/sql/memoize.sql
+++ b/src/test/regress/sql/memoize.sql
@@ -47,12 +47,12 @@ WHERE t2.unique1 < 1000;
 -- Try with LATERAL joins
 SELECT explain_memoize('
 SELECT COUNT(*),AVG(t2.unique1) FROM tenk1 t1,
-LATERAL (SELECT t2.unique1 FROM tenk1 t2 WHERE t1.twenty = t2.unique1) t2
+LATERAL (SELECT t2.unique1 FROM tenk1 t2 WHERE t1.twenty = t2.unique1 ORDER BY 1) t2
 WHERE t1.unique1 < 1000;', false);
 
 -- And check we get the expected results.
 SELECT COUNT(*),AVG(t2.unique1) FROM tenk1 t1,
-LATERAL (SELECT t2.unique1 FROM tenk1 t2 WHERE t1.twenty = t2.unique1) t2
+LATERAL (SELECT t2.unique1 FROM tenk1 t2 WHERE t1.twenty = t2.unique1 ORDER BY 1) t2
 WHERE t1.unique1 < 1000;
 
 -- Reduce work_mem and hash_mem_multiplier so that we see some cache evictions
-- 
2.31.0

#2David Rowley
dgrowleyml@gmail.com
In reply to: Richard Guo (#1)
Re: Improve LATERAL join case in test memoize.sql

On Mon, 16 Jan 2023 at 22:27, Richard Guo <guofenglinux@gmail.com> wrote:

I happened to notice we have the case in memoize.sql that tests for
memoize node with LATERAL joins, which is

-- Try with LATERAL joins
SELECT explain_memoize('
SELECT COUNT(*),AVG(t2.unique1) FROM tenk1 t1,
LATERAL (SELECT t2.unique1 FROM tenk1 t2 WHERE t1.twenty = t2.unique1) t2
WHERE t1.unique1 < 1000;', false);

ISTM this is not the right query for the test.

Attach a trivial patch for the change.

Good catch. I've applied this back to v14.

David