Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gIaKR-00056m-DG for pgsql-hackers@arkaria.postgresql.org; Fri, 02 Nov 2018 14:21:39 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gIaKP-0005fn-Qr for pgsql-hackers@arkaria.postgresql.org; Fri, 02 Nov 2018 14:21:37 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gIaKP-0005fg-Jo for pgsql-hackers@lists.postgresql.org; Fri, 02 Nov 2018 14:21:37 +0000 Received: from xvm-110-146.dc2.ghst.net ([46.226.110.146] helo=fetter.org) by magus.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gIaKM-0001jw-GJ for pgsql-hackers@lists.postgresql.org; Fri, 02 Nov 2018 14:21:37 +0000 Received: by fetter.org (Postfix, from userid 1001) id E0FAD41D22; Fri, 2 Nov 2018 15:21:32 +0100 (CET) Date: Fri, 2 Nov 2018 15:21:32 +0100 From: David Fetter To: Corey Huinker Cc: nasbyj@amazon.com, surafel3000@gmail.com, cmt@burggraben.net, pgsql-hackers@lists.postgresql.org Subject: Re: COPY FROM WHEN condition Message-ID: <20181102142132.GJ12677@fetter.org> References: <20181011085925.5zopu7jvxzs5c4xq@squirrel.exwg.net> <20181011153505.GL6157@fetter.org> <5E61B32E-61CB-4EBB-BC8E-BE37DC6E79AB@amazon.com> <20181101004155.GI12677@fetter.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.5.24 (2015-08-30) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Thu, Nov 01, 2018 at 10:57:25PM -0400, Corey Huinker wrote: > > > > > Are you thinking something like having a COPY command that provides > > > results in such a way that they could be referenced in a FROM clause > > > (perhaps a COPY that defines a cursor…)? > > > > That would also be nice, but what I was thinking of was that some > > highly restricted subset of cases of SQL in general could lend > > themselves to levels of optimization that would be impractical in > > other contexts. > > If COPY (or a syntactical equivalent) can return a result set, then the > whole of SQL is available to filter and aggregate the results and we don't > have to invent new syntax, or endure confusion whenCOPY-WHEN syntax behaves > subtly different from a similar FROM-WHERE. That's an excellent point. > Also, what would we be saving computationally? The whole file (or program > output) has to be consumed no matter what, the columns have to be parsed no > matter what. At least some of the columns have to be converted to their > assigned datatypes enough to know whether or not to filter the row, but we > might be able push that logic inside a copy. I'm thinking of something like > this: > > SELECT x.a, sum(x.b) > FROM ( COPY INLINE '/path/to/foo.txt' FORMAT CSV ) as x( a integer, b numeric, c text, d date, e json) ) Apologies for bike-shedding, but wouldn't the following be a better fit with the current COPY? COPY t(a integer, b numeric, c text, d date, e json) FROM '/path/to/foo.txt' WITH (FORMAT CSV, INLINE) > WHERE x.d >= '2018-11-01' > > > In this case, there is the *opportunity* to see the following optimizations: > - columns c and e are never referenced, and need never be turned into a > datum (though we might do so just to confirm that they conform to the data > type) That sounds like something that could go inside the WITH extension I'm proposing above. [STRICT_TYPE boolean DEFAULT true]? This might not be something that has to be in version 1. Best, David. -- David Fetter http://fetter.org/ Phone: +1 415 235 3778 Remember to vote! Consider donating to Postgres: http://www.postgresql.org/about/donate