X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 040F49DCA14 for ; Fri, 10 Mar 2006 08:19:40 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 53544-05-2 for ; Fri, 10 Mar 2006 08:19:35 -0400 (AST) Received: from svr4.postgresql.org (svr4.postgresql.org [66.98.251.159]) by postgresql.org (Postfix) with ESMTP id 2589B9DC9DC for ; Fri, 10 Mar 2006 08:19:29 -0400 (AST) Received: from mail.logix-tt.com (unknown [212.211.145.186]) by svr4.postgresql.org (Postfix) with ESMTP id 2F2ED5AF667 for ; Fri, 10 Mar 2006 12:19:29 +0000 (GMT) Received: from kingfisher.intern.logix-tt.com (Ta907.t.pppool.de [89.55.169.7]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.logix-tt.com (Postfix) with ESMTP id 6F1C465456; Fri, 10 Mar 2006 13:19:44 +0100 (CET) Received: from [127.0.0.1] (localhost [127.0.0.1]) by kingfisher.intern.logix-tt.com (Postfix) with ESMTP id 406D1181ABFE3; Fri, 10 Mar 2006 13:19:31 +0100 (CET) Message-ID: <44116ED2.9060901@logix-tt.com> Date: Fri, 10 Mar 2006 13:19:30 +0100 From: Markus Schaber Organization: Logical Tracking and Tracing International AG, Switzerland User-Agent: Debian Thunderbird 1.0.7 (X11/20051017) X-Accept-Language: en-us, en MIME-Version: 1.0 To: PostgreSQL SQL List Subject: Set generating functions and subqueries X-Enigmail-Version: 0.93.0.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.144 required=5 tests=[AWL=0.144] X-Spam-Score: 0.144 X-Spam-Level: X-Archive-Number: 200603/143 X-Sequence-Number: 24489 Hello, Today, I stumbled across the following: postgres=# select generate_series(1,2),'othercol'; generate_series | ?column? -----------------+---------- 1 | othercol 2 | othercol (2 rows) postgres=# select (select generate_series(1,2)),'othercol'; ERROR: more than one row returned by a subquery used as an expression So it seems that set-returning functions "blow up" the resultset by duplicating rows - so why is this not allowed for subqueries? It is easy to refactor a subquery into a set-returning function, so I think this violates the principle of orthogonality. But there may be subtle reasons of ambiguity here I don't see right now. (I know that usually, a JOIN would be the right thing to do here, but I'm just curious why multi-row subqueries are not allowed.) Btw, having several set-returning functions with equal or different set lengths produce interesting results: postgres=# select generate_series(1,2),generate_series(3,4),'othercol'; generate_series | generate_series | ?column? -----------------+-----------------+---------- 1 | 3 | othercol 2 | 4 | othercol (2 rows) postgres=# select generate_series(1,2),generate_series(3,5),'othercol'; generate_series | generate_series | ?column? -----------------+-----------------+---------- 1 | 3 | othercol 2 | 4 | othercol 1 | 5 | othercol 2 | 3 | othercol 1 | 4 | othercol 2 | 5 | othercol (6 rows) Is there any way to indicate that I want the cross-product if both set-returning functions have the same length? This could lead to strange effects if the sets have varying lengths otherwhise. (One workaround would be to join two selects, each one having one set-returning function.) Btw, it is not possible to trick PostgreSQL into accepting multi-row selects this way: postgres=# select (select generate_series(1,2)),generate_series(3,4),''; ERROR: more than one row returned by a subquery used as an expression Have fun, Markus -- Markus Schaber | Logical Tracking&Tracing International AG Dipl. Inf. | Software Development GIS Fight against software patents in EU! www.ffii.org www.nosoftwarepatents.org