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 1rpok8-008myi-4n for pgsql-general@arkaria.postgresql.org; Thu, 28 Mar 2024 12:20:28 +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 1rpok7-00Ct2X-2m for pgsql-general@arkaria.postgresql.org; Thu, 28 Mar 2024 12:20:27 +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 1rpoTi-00CXJ0-CO for pgsql-general@lists.postgresql.org; Thu, 28 Mar 2024 12:03:30 +0000 Received: from mout01.posteo.de ([185.67.36.65]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rpoTe-006zQt-Th for pgsql-general@lists.postgresql.org; Thu, 28 Mar 2024 12:03:29 +0000 Received: from submission (posteo.de [185.67.36.169]) by mout01.posteo.de (Postfix) with ESMTPS id A2FD1240028 for ; Thu, 28 Mar 2024 13:03:25 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.net; s=2017; t=1711627405; bh=qKAHFS4JlbeqVWD0aSfWfCjtmUnikQr/BEvWkA0k0TE=; h=Message-ID:Date:MIME-Version:To:From:Subject:Content-Type: Content-Transfer-Encoding:Autocrypt:OpenPGP:From; b=YiD8kIU8C1KPb/D7zSOEu+WnVQxayumCyr2ZGeKjuyYERDQYIKvSOiwXyPsSlXahn o7u4lSm7c05CSN7MtIlyrYHGAgmoa9aMlaZEB3FOZNKiunLvXDlXRi5V08LNQfjTNY gVvD7yUwRUeRflzFw4mcaRZw/XItl6z8g5tIBefO03TZr2wTC+AMy1uRAI9VKRfEtG VntwrHdvr+lOoceItUly6yPZ+FzJkuLEQlU/2km479TkhAm0l5xu/3BFSpq/7rqUv8 wNv+AHE/pPdVsdz/C1vFLuc/IZ1oIoHSGIXkEhJivIl5pT1VwruGRq143B19YVzb2v Z2IZCeJNeFnUw== Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4V52Hc6C6vz6ty0 for ; Thu, 28 Mar 2024 13:03:24 +0100 (CET) Message-ID: Date: Thu, 28 Mar 2024 12:03:24 +0000 MIME-Version: 1.0 Content-Language: en-US To: pgsql-general@lists.postgresql.org From: rwxrw@posteo.net Subject: Problems caused by type resolution for the unknown type Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Autocrypt: addr=rwxrw@posteo.net; prefer-encrypt=mutual; keydata=xsFNBGNf5UcBEACs/1cjEqbyOrrK+T3HQN9LMYpKtxRR6eeDoboI7mZHhkm+dquel/EHAklK8Gyq 43QykgJA3U6kVIuH1WsD75A3REPMkWYB+fxGtIxTwQnqXdzazYWsEiQ70UWRrElHTFDmpi9O2zbO 5vYgIFbjtZ92tCXiAJJkeU5nOUwyg55xU10q19wBkthKydh7S3lhb6m0DV0MiZX9RLsGeRhaqTz6 +eYNoiblOmkV7OxsWtMdK9cEoHl3EkRTu6NzRDIP3UcDAt7bW1FE5OcUaqmQgkGNY+xHN6HZxB73 nHT5WrjKrQuHcWkZm/Eg6kgAktl2NV7DUKxgev8bQoHqSR1GscB9oK33Mnz/UDbRs+5rmDNyImik s1B/mncXRVkTa/oIKcS5ZOGqliwDn6BxNjvqaDhJL6aqBP7Q+DZzfGLINFXDS7BmUQi7k2zOQhUx 7/zy9aeVfYq+NiznfHU4esqGYSrpTGtOos5C9ohNdP2u6KaMeQxFqCgNaaAIPcmECVEdyzTsD2uQ mX9a894QOPkiCDaVwWLSgnaC+w3U9cOgGYuBq+fUXvYvTyuxvKnUeQyQ/hO0N099rGeeG2xgNvAO 8j5q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dFNafxyq+bDHNI8z+WJulq9UwH/2WkPHq/7PhgP9zWTsnohZjK 9d5LAi/cj4plVgJqIN3JoXkWc4YpCfOijG6lv8sGdMrOP/p6rOsCUnbcBgOVjRDL+U17KNxBuwt2 xV6cw9v8jUAlE5M486rrAfBwX7Xlq5hlOiZSLVHM4qxe4SLhUYnIG4g5cZaSY/u1dLCFv/W/UAl5 DsDWVoJkwiNkzz4WrunVulph/W+b2cF0JA== OpenPGP: url=https://posteo.de/keys/rwxrw@posteo.net.asc; preference=encrypt List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hello, I've identified a number of cases that suffer from problems caused by the type resolution algorithm when the "unknown" type is present. The most common case is that of a UNION query which is even mentioned in the documentation at https://www.postgresql.org/docs/16/typeconv-union-case.html under "Example 10.13. Type Resolution in a Nested Union". I had initially made a false assumption that only UNION, CASE, and Related Constructs (listed in the documentation) are affected by this. Soon, however, I identified some more, which made me consider this a bug or at least a significant inconvenience for PostgreSQL users because it inflicts the burden of inserting explicit type casts. This is especially impactful for SQL-generating code which has to perform a lot of additional analysis to determine the appropriate type cast. Beside the UNION case from the documentation, here are the problematic cases I identified: 1. JOIN on integer = unknown (text) -- SELECT * FROM (SELECT 2 AS a) AS t1 JOIN (SELECT NULL AS b) AS t2 ON t1.a = t2.b; SELECT * FROM (SELECT 2 AS a) AS t1 JOIN (SELECT '2' AS b) AS t2 ON t1.a = t2.b; -- Both queries fail with: ERROR: operator does not exist: integer = text LINE 4: ON t1.a = t2.b; HINT: No operator matches the given name and argument types. You might need to add explicit type casts. 2. WHERE with a comparison between integer and unknown (text) -- CREATE TABLE mytable (a int); SELECT a FROM mytable WHERE a = (SELECT NULL); SELECT a FROM Mytable WHERE a = (SELECT '1'); -- Both fail with: ERROR: operator does not exist: integer = text LINE 3: a = (SELECT NULL); // or LINE 3: a = (SELECT '1'); HINT: No operator matches the given name and argument types. You might need to add explicit type casts. 3. CASE WHEN (which is subject to the same rules as UNION) -- CREATE TABLE test_data(a int, b int); SELECT a FROM test_data WHERE a = (CASE WHEN b > 0 THEN NULL ELSE '1' END) -- Fails with: ERROR: operator does not exist: integer = text LINE 3: a = (CASE WHEN b > 0 THEN NULL ELSE '1' END) HINT: No operator matches the given name and argument types. You might need to add explicit type casts. -- Note that all queries above succeed on Microsoft SQL Server. I wonder if the type resolution algorithm could be changed to delay the final decision until the very last moment instead of resolving "unknown" as "text". Alternatively, maybe implicit type casting could be made use of during query analysis, since the following query succeeds: SELECT ('1'::text)::integer; -- Cheers