Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sOvRQ-0020ar-Dm for pgsql-hackers@arkaria.postgresql.org; Wed, 03 Jul 2024 08:34:16 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1sOvRN-006wNK-VZ for pgsql-hackers@arkaria.postgresql.org; Wed, 03 Jul 2024 08:34:14 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sOvRN-006wNC-Ls for pgsql-hackers@lists.postgresql.org; Wed, 03 Jul 2024 08:34:14 +0000 Received: from mail-lf1-x12d.google.com ([2a00:1450:4864:20::12d]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sOvRM-000Bjb-Ao for pgsql-hackers@postgresql.org; Wed, 03 Jul 2024 08:34:13 +0000 Received: by mail-lf1-x12d.google.com with SMTP id 2adb3069b0e04-52e8b27f493so3634262e87.2 for ; Wed, 03 Jul 2024 01:34:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719995651; x=1720600451; darn=postgresql.org; h=content-transfer-encoding:in-reply-to:content-language:references :cc:to:from:subject:user-agent:mime-version:date:message-id:from:to :cc:subject:date:message-id:reply-to; bh=7kgZUeMke+M4/F/Sfy0E3PMhUoRcDzOkm9Mzgs12bZ4=; b=EadYzp+pPa2Fb0m0ULl4iarsudfOlM9qZLCP88QPtE9RAnpipbT5wKn9S6S1kJ1gPc H5tUxaLb36gcMzXfEegZ+fP5WYob7ZHd3+FIs1gcE6GDNV5t7cTVrrZnWJ6q8YhJ2IkO BOO8CjLWv7AJHMp1wjBgWA48SWFdJ4xjH1f9zM0sWXrdjKY93f9EVzNCp/7VKXG9IyWC bhsf57gbmbo8XBZxXr0+KiPAMVkr3SZqvm2RrUv3m6RPMtQjsORPF23PhhERJiBz2HY6 g/t7ow0jaV0oRjdIuHSs4EVk+3nu8cUCi206vLnxeddR8+6+g2vUjsuyxo7auJ9yTdoZ px6w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719995651; x=1720600451; h=content-transfer-encoding:in-reply-to:content-language:references :cc:to:from:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=7kgZUeMke+M4/F/Sfy0E3PMhUoRcDzOkm9Mzgs12bZ4=; b=nWIMp5uYc1x3Hl/wUf77U7unB5N079RKOlmdOSx9sC0S6gb5O94iuaJYZpim+Ufwxq YKq7oFTAekFWa8CpJZh9iurHXApt6siHR0wmWRX2+lC42s5FN68azwgs4X3AVVReRFCE 2vT3koLOYOsuvRE89/TjHVtjoUNzyyD5DpUisWMQU8RaNtH+S/7GWjsvMhcB0AzYZx52 Pht8M8TfCWF1is6qomm6Ff0fbilGanLaGA4jynXeFMThLC9sueDjJkVFwbZK47AkB6a/ DJEBmZ76DFRIftOIl5oINiNjSiIVGC6kqMoZCljnRfMRTiP711H7Xz5XmVL2svqH12D0 tJdg== X-Gm-Message-State: AOJu0YxOiE7601zuVoWevbhJm1cENYc0OGjvG/PiDV/J0FbWznzspmCX IZVzVZtRv585Xe/WnWRj4sMaws4VLKW2I2/vJ54JyGlFGgZzQ6lJ X-Google-Smtp-Source: AGHT+IE0OlS1qn34TmnsEZVCEbnggrH491oj2Vo90Nexz9dLOkdI1cw/m8sQ1WZc26t36Cqoi8hclw== X-Received: by 2002:a05:6512:3baa:b0:52c:d943:300f with SMTP id 2adb3069b0e04-52e8268b961mr6764306e87.38.1719995632129; Wed, 03 Jul 2024 01:33:52 -0700 (PDT) Received: from [10.10.20.97] (node-nj.pool-118-173.dynamic.totinternet.net. [118.173.3.79]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-52e7ab0bb7dsm2052475e87.54.2024.07.03.01.33.48 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 03 Jul 2024 01:33:51 -0700 (PDT) Message-ID: <780533de-c9af-4823-94bf-dbbdb03c1bfa@gmail.com> Date: Wed, 3 Jul 2024 15:33:44 +0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: A new strategy for pull-up correlated ANY_SUBLINK From: Andrei Lepikhov To: Andy Fan , Alena Rybakina Cc: pgsql-hackers , Tom Lane , vignesh C , Richard Guo References: <247851.1668293143@sss.pgh.pa.us> Content-Language: en-AU In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 7/1/24 16:17, Andrei Lepikhov wrote: > On 10/12/23 14:52, Andy Fan wrote: >> Here the sublink can't be pulled up because of its reference to >> the  LHS of left join, the original logic is that no matter the 'b.t >> in ..' >> returns the true or false,  the rows in LHS will be returned.  If we >> pull it up to LHS, some rows in LHS will be filtered out, which >> breaks its original semantics. > Hi, > I spent some time trying to understand your sentence. > I mean the following case: > > SELECT * FROM t1 LEFT JOIN t2 >   ON t2.x IN (SELECT y FROM t3 WHERE t1.x=t3.x); > > I read [1,2,3], but I am still unsure why it is impossible in the case > of OUTER JOIN. By setting the LATERAL clause, we forbid any clauses from > the RTE subquery to bubble up as a top-level clause and filter tuples > from LHS, am I wrong? Does it need more research or you can show some > case to support your opinion - why this type of transformation must be > disallowed? > > [1] https://www.postgresql.org/message-id/6531.1218473967%40sss.pgh.pa.us > [2] > https://www.postgresql.org/message-id/BANLkTikGFtGnAaXVh5%3DntRdN%2B4w%2Br%3DNPuw%40mail.gmail.com > [3] https://www.vldb.org/conf/1992/P091.PDF > I delved into it a bit more. After reading [4,5] I invented query that is analogue of the query above, but with manually pulled-up sublink: EXPLAIN (COSTS OFF) SELECT * FROM t1 LEFT JOIN t2 JOIN LATERAL (SELECT t1.x AS x1, y,x FROM t3) q1 ON (t2.x=q1.y AND q1.x1=q1.x) ON true; And you can see the plan: Nested Loop Left Join -> Seq Scan on t1 -> Hash Join Hash Cond: (t2.x = t3.y) -> Seq Scan on t2 -> Hash -> Seq Scan on t3 Filter: (t1.x = x) Just for fun, I played with MSSQL Server and if I read its explain correctly, it also allows pulls-up sublink which mentions LHS: ------------------------------------- Nested Loops(Left Outer Join, OUTER REFERENCES:(t1.x)) Table Scan(OBJECT:(t1)) Hash Match(Right Semi Join, HASH:(t3.y)=(t2.x), RESIDUAL:(t2.x=t3.y)) Table Scan(OBJECT:(t3), WHERE:(t1.x=t3.x)) Table Scan(OBJECT:(t2)) ------------------------------------- (I cleaned MSSQL explain a little bit for clarity). So, may we allow references to LHS in such sublink? [4] https://www.postgresql.org/message-id/flat/15523.1372190410%40sss.pgh.pa.us [5] https://www.postgresql.org/message-id/20130617235236.GA1636@jeremyevans.local -- regards, Andrei Lepikhov