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 1kcb7j-0004NB-Ss for psycopg@arkaria.postgresql.org; Tue, 10 Nov 2020 21:24:20 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kcb7i-0000D0-Ns for psycopg@arkaria.postgresql.org; Tue, 10 Nov 2020 21:24:18 +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 1kcb7i-0000Cs-62 for psycopg@lists.postgresql.org; Tue, 10 Nov 2020 21:24:18 +0000 Received: from campbell-lange.net ([178.79.140.51]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kcb7f-0001my-6y for psycopg@lists.postgresql.org; Tue, 10 Nov 2020 21:24:17 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=campbell-lange.net; s=it; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=v44IwJRyXlqhX6Jp1XlGT/k6Ukfd7GHjEcfP61sL/Zo=; b=TfOk8a6IBTmuXnw0os6ieFqIo Mi3DBWPps01akyOdk5V4f5gcCXGNfNc0JV+0VSLzn/QKUxZs9oizjFxoyBRRnZwYlxIJxSPYL8s++ dGKyP/WEOp5e4CPzLcs1LfiZtmMBMXUyVGJB7letPGGp3i7xGtzZLn+M6a1ckVyseqrq8=; Received: from 180.164.198.146.dyn.plus.net ([146.198.164.180] helo=rory-t470s) by campbell-lange.net with esmtpsa (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kcb7c-0005VM-A0; Tue, 10 Nov 2020 21:24:12 +0000 Received: from rory by rory-t470s with local (Exim 4.92) (envelope-from ) id 1kcb7b-0001mi-Lv; Tue, 10 Nov 2020 21:24:11 +0000 Date: Tue, 10 Nov 2020 21:24:11 +0000 From: Rory Campbell-Lange To: Daniele Varrazzo Cc: Christophe Pettus , Vladimir Ryabtsev , Federico Di Gregorio , psycopg@lists.postgresql.org Subject: Re: psycopg3 and adaptation choices Message-ID: <20201110212411.GA5668@campbell-lange.net> References: <88fb7a7e-a182-a816-c1a7-8a1f54b65215@aklaver.com> <4830fb8d-fa57-e0f8-0e4f-a96ed040dede@dndg.it> <1546497c-ff41-9bd0-b4d7-931bd305caac@dndg.it> <9989BCEF-2CC4-4AA4-9E4E-C83740F21414@thebuild.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 10/11/20, Daniele Varrazzo (daniele.varrazzo@gmail.com) wrote: > On Tue, 10 Nov 2020 at 03:22, Christophe Pettus wrote: > > > > > On Nov 9, 2020, at 19:20, Daniele Varrazzo wrote: > > > > > > Choices to cast Python ``int`` type: > > > > Is it absurd to make the choice at execution time, based on the actual value of the Python int? > > I've been thinking a lot about it. I haven't completely ruled it out, > but there are a few cases in which having different oids for the same > query gets in the way. One that comes to mind is with prepared > statements, either explicit (which I haven't exposed yet, but it's > like the #1 request for a new feature), or implicit (currently using > them to implement 'executemany()'). However I might be overestimating > these issues, yes. > > I guess I should give an overview of the whole adaptation system: I'll > try and write its documentation in the next few days. I have to start > with the documentation somewhere... Apologies for a no-doubt naive suggestion, Daniele, but how about a postgresql type 'shim' of some sort that only accepts python types on input and translates output back to only python types. If such a shim, perhaps a type + C function pair were used, I assume it would not round-trip per-se, but could cycle through int types from most restrictive to most lenient on the basis (I'm guessing) that postgresql will coerce a postgres int4 to and int8 on insertion if necessary, on the principle that the following works ok: test=> create table a (b int8); CREATE TABLE test=> insert into a values (1::int4); INSERT 0 1 test=> create table b (c numeric); CREATE TABLE test=> insert into b values (4::int8); INSERT 0 1 I assume mapping native postgresql column types to output values would pass back through such a 'sieve' quite naturally into native python types. Whether such a filtering layer should work directly in postgresql or as a translation (or 'adaptation') layer in psycopg[3]*is perhaps a similar debate -- although at a lower level -- about Django's ORM. Where should the logic lie? By the way I believe this is this 'layer' for the golang pgx module, which may be of interest: https://github.com/jackc/pgx/blob/93c6b60429e13e0016665214dca2c6382982cf99/values.go#L28 although golang is of course is more strongly typed than python. I thought the type switch test for coercion, as Christophe suggests, might be doable through the 'shim' layer I'm imagining. Regards Rory