Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nVZMq-0006vj-5o for psycopg@arkaria.postgresql.org; Sat, 19 Mar 2022 13:43:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nVZMn-0006rI-Pa for psycopg@arkaria.postgresql.org; Sat, 19 Mar 2022 13:43:37 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nVZMn-0006r9-Go for psycopg@lists.postgresql.org; Sat, 19 Mar 2022 13:43:37 +0000 Received: from mail-vs1-xe2e.google.com ([2607:f8b0:4864:20::e2e]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1nVZMj-0000Ak-Au for psycopg@postgresql.org; Sat, 19 Mar 2022 13:43:35 +0000 Received: by mail-vs1-xe2e.google.com with SMTP id i63so6873291vsi.5 for ; Sat, 19 Mar 2022 06:43:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=nQ7L8af2zOvfebSVF68i2eJHcfW2CCDUG6bPXkBXx4w=; b=DnZBx/sv4GZqQ3xFsQ0JJOXJxeznAiWdZq4EVLZl7ruxaAaXFdAcCJk6uvcJUFTX6H sDzi/QrR1msk+PMMQ1+SOo2KpDLRn0I8wkuvpSIbA9nLDf67d3B1N2K1r3NVjxHy1tHc haOx5645o14oSSYLMDw2uck7f77t3LgV2u9EGz+Xzhe7oieZGul7Qrn7xRiaS7H0E8LJ 5RstCahYKOIp1ofiIzG/KohuKhSRSulokO/gxXXxZq6JidHiBq8UTDWlJbRWng9UG7Ul YmTkEdhfSi+F1t+alYvKUrLfYE0nCWgH6nyv+SYVYj4yiwICsG8jHYpbdSJ+JCbqNSFM sXsw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=nQ7L8af2zOvfebSVF68i2eJHcfW2CCDUG6bPXkBXx4w=; b=eKnNMEALJDIV0E18UVOF5oHnez12JaSCJlj6Uz3flQRHlZyLYpJxC+zhgWTz59TN/y tr0RKGCMdf7egs2VkJtJ254HDbGWg1XobNU7no3HX2LAaljdBCzw4fNX+De/KZTRc6M+ MErZe576kSbCUaJIHQBRvgRIVByOEp7RY1frtuyHgK14wstxWXQeKD/d61+gAbhTvIcS KMqLTQpfyjhJ+/L7evprYlgG0kMTMNfkwWtseEbKS+K1XNdWef2sHIsUHXsgiJQ7dozX Vj0wcGI22kC9S0ZaBqHjeufA2qvvnttF/ckZlCfNv4sIzJ7MIGu2BWpOx2bkoXWsPJhA 1NWw== X-Gm-Message-State: AOAM5311Sx4H9BJAlBiXGCVI/g2y75QGBa5MoofhakeNyWBF4sGGObaw F7b65InC3tAkKt1OE1AmXTVFZHPr8iHZNwKoUas= X-Google-Smtp-Source: ABdhPJwSI7HQ2zJUcqfTjJEIMgiiPh15yAOZex6JGsX2gl9fh2ZFSqNYlgggoAzhOr3rH+JmI8tZy+Pz0lh+CL+l790= X-Received: by 2002:a67:846:0:b0:320:c699:efdf with SMTP id 67-20020a670846000000b00320c699efdfmr5067439vsi.60.1647697410834; Sat, 19 Mar 2022 06:43:30 -0700 (PDT) MIME-Version: 1.0 References: <8df15d1263aaa1a9d36eacb0e5b9a28e@paolodestefani.it> <74FC637F-2D25-4CD0-A075-9CB5293FCA5B@thebuild.com> In-Reply-To: From: Daniele Varrazzo Date: Sat, 19 Mar 2022 14:43:19 +0100 Message-ID: Subject: Re: Query with boolean parameter To: Paolo De Stefani Cc: Christophe Pettus , Psycopg Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > Il 19/03/2022 01:00 Christophe Pettus ha scritto:> >> On Mar 18, 2022, at 16:56, Paolo De Stefani > >> wrote: > >> Is there any reason why the second query results in a syntax error? > > > > There's not IS operator in PostgreSQL (or in SQL). IS NULL, IS NOT > > NULL, IS TRUE, and IS FALSE are in effect unary postfix operators, so > > you can't construct them that way via parameter substitution. That's unexpected. Thank you for the insight, Christophe. On Sat, 19 Mar 2022 at 13:11, Paolo De Stefani wrote: > The problem is (for me) that with psycopg2 this works: > cur.execute('SELECT * FROM system.app_user WHERE can_edit_views IS > TRUE') > cur.execute('SELECT * FROM system.app_user WHERE can_edit_views IS %s', > (True,)) > cur.execute('SELECT * FROM system.app_user WHERE can_edit_views IS %s', > (None,)) > > Switching to psycopg 3 i have to consider many more differences than i > expected Maybe you can play around with IS NOT DISTINCT FROM? https://www.postgresql.org/docs/current/functions-comparison.html In [1]: import psycopg In [2]: cnn = psycopg.connect(autocommit=True) In [5]: cnn.execute("create table dist (id int primary key, cond bool)") In [7]: cnn.cursor().executemany("insert into dist values (%s, %s)", [(1, True), (2, False), (3, None)]) In [9]: cnn.execute("select * from dist where cond is not distinct from %s", [True]).fetchone() Out[9]: (1, True) In [10]: cnn.execute("select * from dist where cond is not distinct from %s", [False]).fetchone() Out[10]: (2, False) In [11]: cnn.execute("select * from dist where cond is not distinct from %s", [None]).fetchone() Out[11]: (3, None) -- Daniele