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 1qRGhM-00170h-RG for pgsql-bugs@arkaria.postgresql.org; Wed, 02 Aug 2023 18:35:53 +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 1qRGhK-0076Pg-Cr for pgsql-bugs@arkaria.postgresql.org; Wed, 02 Aug 2023 18:35:50 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qRGhJ-0076PY-Ro for pgsql-bugs@lists.postgresql.org; Wed, 02 Aug 2023 18:35:50 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qRGhH-000B51-PZ for pgsql-bugs@lists.postgresql.org; Wed, 02 Aug 2023 18:35:49 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 372IZknf3307253; Wed, 2 Aug 2023 14:35:46 -0400 From: Tom Lane To: "=?utf-8?B?57+B5oCd5oms?=" <52275903002@stu.ecnu.edu.cn> cc: "=?utf-8?B?cGdzcWwtYnVncw==?=" , "=?utf-8?B?cXN3YW5n?=" Subject: Re: [BUG REPORT] Unexpected cast while matching CHAR type In-reply-to: References: Comments: In-reply-to "=?utf-8?B?57+B5oCd5oms?=" <52275903002@stu.ecnu.edu.cn> message dated "Thu, 03 Aug 2023 01:23:16 +0800" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <3307251.1691001346.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Wed, 02 Aug 2023 14:35:46 -0400 Message-ID: <3307252.1691001346@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk "=3D?utf-8?B?57+B5oCd5oms?=3D" <52275903002@stu.ecnu.edu.cn> writes: > We find an unexpected behaviour about query optimizer in PostgreSQL. > More specifically, char type will be transformed into text incorrectly w= hich leads to an unexpected result. This is not a bug. There is no variant of the LIKE operator that accepts char(n) as the pattern argument: =3D# \do ~~ List of operators Schema | Name | Left arg type | Right arg type | Result type | = Description = ------------+------+---------------+----------------+-------------+-------= ------------------ pg_catalog | ~~ | bytea | bytea | boolean | matche= s LIKE expression pg_catalog | ~~ | character | text | boolean | matche= s LIKE expression pg_catalog | ~~ | name | text | boolean | matche= s LIKE expression pg_catalog | ~~ | text | text | boolean | matche= s LIKE expression (4 rows) Therefore, the parser (not the optimizer) has little choice but to insert an implicit cast of the sub-select's output to text; the only other alternative is to throw an error. We could make this case behave as you expect by adding a variant of ~~ that takes char(n) on the RHS, but I'm disinclined to do so because of the high probability that it would cause unwanted behavioral changes in other cases. Moreover, given that char(n)'s trailing spaces are considered insignificant, is preserving them in a LIKE pattern even the right thing to do? I don't think that's exactly an open-and-shut question. (Arguably, the right fix is not to add another ~~ variant but to get rid of the char(n) versus text one, so that both sides get space-stripped in this case.) The short answer is that char(n) is a legacy type that you should not use, precisely because it has such odd behavior around trailing spaces. We support it for pro-forma compliance with the SQL standard, but consider it deprecated. That being the case, there's not a lot of interest in changing edge cases in its behavior. We'd be more likely to get complaints than kudos for doing so. regards, tom lane