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 1tGfc8-0000EX-Um for pgsql-general@arkaria.postgresql.org; Thu, 28 Nov 2024 14:35:29 +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 1tGfc5-005TpP-P8 for pgsql-general@arkaria.postgresql.org; Thu, 28 Nov 2024 14:35:25 +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 1tGfc5-005TpG-9I for pgsql-general@lists.postgresql.org; Thu, 28 Nov 2024 14:35:25 +0000 Received: from mail-lj1-x234.google.com ([2a00:1450:4864:20::234]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1tGfc1-004VuW-SW for pgsql-general@postgresql.org; Thu, 28 Nov 2024 14:35:24 +0000 Received: by mail-lj1-x234.google.com with SMTP id 38308e7fff4ca-2ffd711274dso16085041fa.0 for ; Thu, 28 Nov 2024 06:35:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1732804520; x=1733409320; darn=postgresql.org; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=dsFYUortWe3SqoAjD9zx5c1ZPXWqqFCpy6K6p7G2q2k=; b=Q3Il3ubWKfOwmebeEgKBAo5RKctsw6Mnrf6kLn4Is9JZXk+ImaxS7hZKrnjBwMPTox O+/F3Fn6AlcD6IR8RWiZETtNik5Ot4R1s2Q0ymSwtgsuJURbZFsJzDufbXsGOvCdM1fh 3YNAaiHn8/kvbu6fbmEHa/qHLRvNuDbox2Yd0jgC5ikBNczOt0IidQjHH+rATd5joR8s oXoXy5XwsV2jalKxuRSAj9kvf0+QMSi0w7vLSvMHxfknIq9jhfIY5FqxP1zkUb2Hm3UO YCXhO5kWHhb9KjnfgGfXdqELmnBlKeWQoc0cUT5TITf2F8VWNXcn1GzvtxuR8/tbKI3i p9nQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732804520; x=1733409320; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=dsFYUortWe3SqoAjD9zx5c1ZPXWqqFCpy6K6p7G2q2k=; b=W+j99GPXTJeOKZ/fUkgitkPtoEtj7Tova44hvWBVtw1ARXxltyFQ1u9TOxk6WCVXwn G1fn25z8i6/qtC3sg0rOd1lL6dHKMCZP+mjBz6bX2w0ozUNEjb3rcyit4lp4dI02cqt7 cOybrpaRlbp287uFVenezaT5DYRJwTl7vHzp/NumoC0lYoeuhVno3LeNmWf9Yj50BaR4 5XWQQU+xmRpLShR4pGFYqUu55JIPHLkBbs20MBkWIvJdzw19JnqD5g/fwJlVXv9E6XPI J2aR8n6GEjNPEtwHe1pYi8cCyqCfbnqj4bSd0gkDwqw73iup0ZWcVoX3xOI7azTypiP8 pwUA== X-Gm-Message-State: AOJu0Yxl8y66GBWRpRGbKzbyfPseXkN0/kVHyW4VRJPITYkBxGsk5ibu WVcc90x+TXoFPNKvqszigf/yT74V8EKnCb0vGREtR/01U0JpnR8jWfroHji9AGSxnORzDapGsX0 O1zOtzDMAeIyeUmaYkqLZGUKCvOek5Sam X-Gm-Gg: ASbGnctPLa4MSWtyge2RASilOHv8j+dNw3HSIp4LhOgyymThv14D2fEToyw+NdrqlnF bQ0+raDETIWQq8R9SQoz/RfeTvqyT8GA2 X-Google-Smtp-Source: AGHT+IEATCuN5mrN9kCzoHQdISb/SX5xOU4ouVwMzuGO1qnzHiqKADUKxpNXFfhwVY7o2NuLR+v4ShRtfox3DCTruOI= X-Received: by 2002:a05:651c:2127:b0:2ff:b8f5:5a17 with SMTP id 38308e7fff4ca-2ffde1eea49mr12701711fa.5.1732804519897; Thu, 28 Nov 2024 06:35:19 -0800 (PST) MIME-Version: 1.0 From: Bertrand Mamasam Date: Thu, 28 Nov 2024 15:35:08 +0100 Message-ID: Subject: ERROR wrong varnullingrels (b 3) (expected (b)) with LEFT JOIN LATERAL and CTE To: pgsql-general@postgresql.org Content-Type: multipart/alternative; boundary="000000000000dfb64a0627f9fddb" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000dfb64a0627f9fddb Content-Type: text/plain; charset="UTF-8" Hello, My query has been working fine in 16.4 and before, but not anymore in 16.5, 16.6 and 17. It is a query with multiple CTE, some of which are using values of the previous ones, and the end of the query sort of make a mix of found values with aggregation from a LATERAL JOIN. Something like this : WITH taxrules AS (...) , defaultprices AS (...) , baseprices AS (...) , currentprices AS (...) , discountedprices AS (...) SELECT discountedprices.variants_id, discountedprices.products_id, sum(COALESCE(taxes.tax_price, 0)) FROM discountedprices LEFT JOIN LATERAL ( SELECT products_taxrules.products_id, round(discountedprices.price * taxrules.rate_percent, 4) - discountedprices.price AS tax_price FROM taxrules INNER JOIN products_taxrules ON taxrules.id = products_taxrules.taxrules_id ) AS taxes ON taxes.products_id = discountedprices.products_id WHERE discountedprices.variants_id = ANY(ARRAY[12345]) GROUP BY discountedprices.variants_id, discountedprices.products_id, discountedprices.price ; I get this error in PG16.5, 16.6 and 17 : wrong varnullingrels (b 3) (expected (b)) for Var 1/19 The query works again if I add a COALESCE on the line in the LATERAL JOIN query like this : round(discountedprices.price * COALESCE(taxrules.rate_percent, 0), 4) - discountedprices.price AS tax_price The query also works if I use an INNER JOIN LATERAL instead of a LEFT JOIN LATERAL. But the taxrules.rate_percent cannot be null anyway. It comes from the result of this calculation : (1 + t.rate_percent / 100) AS rate_percent in the taxrules CTE. So now I wonder if my sql was wrong and should be fixed or if since 16.5 Postgresql has a bug in the way it deals with values in a LEFT JOIN LATERAL ? Thanks for your help, Bertrand Mansion Mamasam --000000000000dfb64a0627f9fddb Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hello,

My query has been wor= king fine in 16.4 and before, but not anymore in 16.5, 16.6 and 17. It is a= query with multiple CTE, some of which are using values of the previous on= es, and the end of the query sort of make a mix of found values with aggreg= ation from a LATERAL JOIN. Something like this :

<= div>WITH
taxrules AS (...)
, defaultprices AS (...)
, baseprices A= S (...)
, currentprices AS (...)
, discountedprices AS (...)

S= ELECT
discountedprices.variants_id,
discountedprices.products_id, sum(COALESCE(taxes.tax_price, 0))

FROM
discountedprices
= LEFT JOIN LATERAL (
=C2=A0=C2=A0=C2=A0 SELECT
=C2=A0=C2=A0=C2=A0 pro= ducts_taxrules.products_id,
=C2=A0=C2=A0=C2=A0 round(discountedprices.pr= ice * taxrules.rate_percent, 4) - discountedprices.price AS tax_price
= =C2=A0=C2=A0=C2=A0 FROM taxrules
=C2=A0=C2=A0=C2=A0 INNER JOIN products_= taxrules ON taxrules.id =3D products_tax= rules.taxrules_id
) AS taxes ON taxes.products_id =3D discountedprices.= products_id
WHERE
discountedprices.variants_id =3D ANY(ARRAY[12345])=
GROUP BY
discountedprices.variants_id,
discountedprices.product= s_id,
discountedprices.price
;

I get this e= rror in PG16.5, 16.6 and 17 :
wrong varnullingrels (b 3) (expected (b))= for Var 1/19

The query works again if I add a COA= LESCE on the line in the LATERAL JOIN query like this :
round(discounte= dprices.price * COALESCE(taxrules.rate_percent, 0), 4) - discountedprices.p= rice AS tax_price

The query also works if I use an INNER = JOIN LATERAL instead of a LEFT JOIN LATERAL.

B= ut the taxrules.rate_percent cannot be null anyway. It comes from the resul= t of this calculation : (1 + t.rate_percent / 100) AS rate_percent in the t= axrules CTE.

So now I wonder if my sql was wro= ng and should be fixed or if since 16.5 Postgresql has a bug in the way it = deals with values in a LEFT JOIN LATERAL ?

Thanks for you= r help,

Bertrand Mansion
Mamasam
<= div>
--000000000000dfb64a0627f9fddb--