Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TPwsS-00077r-Ej for pgsql-jdbc@postgresql.org; Sun, 21 Oct 2012 14:51:44 +0000 Received: from nm22.bullet.mail.ac4.yahoo.com ([98.139.52.219]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TPwsN-0006Wf-3r for pgsql-jdbc@postgresql.org; Sun, 21 Oct 2012 14:51:43 +0000 Received: from [98.139.52.188] by nm22.bullet.mail.ac4.yahoo.com with NNFMP; 21 Oct 2012 14:51:37 -0000 Received: from [76.13.13.42] by tm1.bullet.mail.ac4.yahoo.com with NNFMP; 21 Oct 2012 14:51:37 -0000 Received: from [127.0.0.1] by smtp103.prem.mail.ac4.yahoo.com with NNFMP; 21 Oct 2012 14:51:37 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1350831097; bh=HTf7eo+kHPhttjEOxdcdsAZ7JlpM2VR7ztPg0foQdQs=; h=X-Yahoo-Newman-Id:X-Yahoo-Newman-Property:X-YMail-OSG:X-Yahoo-SMTP:Received:From:To:Cc:References:In-Reply-To:Subject:Date:Message-ID:MIME-Version:Content-Type:Content-Transfer-Encoding:X-Mailer:Thread-Index:Content-Language; b=YK+pQJC7uCbiA6GKrm+Q9sSMAXCx6iC/LdUoGUlvc/3tTgGKy/JeND9/Y5F4hh+0ida9i0VvJ8fzB3iQzjTVAikh1u0GOmByfynbPIjkbI259NCR814uBCxEyjQhMHQnavqkoqR0DRih/2pXhx9bfjGycHRBd3utWIRl1FIMlP4= X-Yahoo-Newman-Id: 212238.2521.bm@smtp103.prem.mail.ac4.yahoo.com X-Yahoo-Newman-Property: ymail-3 X-YMail-OSG: dJxpU0MVM1l2ak2KG1_bHQ3I6PMdwok19PWJ2Zqf9P51NuT WIuaRSvYifQVv0EX.cmlce9OTQR2YlQiwYv3.3urFFQUaAv9W8v8cWEQ0ays HvvCgYVLPJ5PcQYaOn7ulAK229AAW8EOOhYGrAq_u_lQReiggdHnOOiK3p0g GAJQrWTInQQpkAbkPi2mCr3R_G5Njlgdt2HFW_Nkn0uIEJNyne5hQOPsH6dF De3i7H9mptPGJconAvOEA2g_Zzos9RjOV8rBP8hoAf2qtuWD4eDMMNHwoLFr RxctEmVFRhcvbZyCHrF_g37CGpODiEoL7mYWHOnmHB2epDqQz.GRHJxLZaEw wpVT5Qbbyn2aCyxJ8IlfskhHdd5OaoYycpddFRazJpxlAocHLrvIa9gSzzn0 qH9bWN3Vy1V03dVkXMBUQcmpkLxCUOmClS.nwCruKfE8_e2k0zJVF.eSpW1X KBtVb X-Yahoo-SMTP: mpGJl6eswBD2IBufoVEg0Pa8gg-- Received: from WolfDog (polobo@24.93.23.188 with login) by smtp103.prem.mail.ac4.yahoo.com with SMTP; 21 Oct 2012 07:51:37 -0700 PDT From: "David Johnston" To: "'Dave Cramer'" , "'Muhammad Altaf'" Cc: References: <1350791042.11899.YahooMailNeo@web161806.mail.bf1.yahoo.com> In-Reply-To: Subject: Re: Struct Support Date: Sun, 21 Oct 2012 10:51:30 -0400 Message-ID: <013a01cdaf9b$8e8c3f40$aba4bdc0$@yahoo.com> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Outlook 14.0 Thread-Index: AQLLW15M3sEI3G8g5CXMsxcQhDJd+AH6UcTglbinYhA= Content-Language: en-us X-Pg-Spam-Score: -2.7 (--) X-Archive-Number: 201210/94 X-Sequence-Number: 23418 > -----Original Message----- > From: pgsql-jdbc-owner@postgresql.org [mailto:pgsql-jdbc- > owner@postgresql.org] On Behalf Of Dave Cramer > Sent: Sunday, October 21, 2012 6:42 AM > To: Muhammad Altaf > Cc: pgsql-jdbc@postgresql.org > Subject: Re: [JDBC] Struct Support > > On Sat, Oct 20, 2012 at 11:44 PM, Muhammad Altaf > wrote: > > Hi Dave, > > I am in process of implementing java.sql.Struct to support composite > > types as OUT/IN parameters in postgres JDBC Driver. I am almost done > > with that, but while writing test cases, I came across an issue with > > return values. The Struct parameters are successfully being passed and > > returned as OUT parameters (I have tested that with multiple out and > > in parameters of type Struct as well). But when it comes to returning > > a struct value, server returns two fields in RowDescription which > > makes driver believe that these are actually two parameters. Initially > > I thought I am doing something wrong at the Driver level, but > > pgAdminIII showed the same behavior. I'll show both behaviors in > pgAdminIII. > > > > SQL Script: > > create type city as (city_id int4, city_name text); create type > > address as (street_address text, city city); create or replace > > function udt_f1(a in varchar, b out city, c out address) returns > > RECORD AS $$ DECLARE > > c_city city; > > BEGIN > > select 1,a into b; > > select 2, 'City Name' into c_city; > > select 'Street address',c_city into c; > > END;$$ language 'plpgsql'; > > > > create or replace function udt_f2(a in int4, b in city) returns city > > AS $$ declare > > c_city city; > > BEGIN > > select a,b.city_name into c_city; > > return c_city; > > END;$$ language 'plpgsql'; > > > > Now, in pgAdmin, call udt_f1 > > select * from udt_f1('Test City'); This will display two columns > > with appropriate city and address details. > > > > Now call udt_f2 > > select * from udt_f2(100, '(4, "Washington")'); Instead of > > displaying a single city column, it displays two columns. The first as > > integer (city_id) and the second being text (city_name). > > > > Can you please confirm I am not correct and something needs to be done > > while calling this function to get it working? > > > > Thanks, > > Altaf Malik > > > > Seems consistent with the way postgresql would do things. What were you > expecting it to return ? > Specifically, if the only output from a function call is a single output type, and that function call is performed in the "FROM" clause of a query, then the result is treated just like a view or table reference and each top-level component of the composite type is treated as an individual column. This applies to the set-returning form ...RETURNS TABLE ( c city )... as well. It also applies to using an explicit OUT parameter in the function parameter declaration. The cleanest way to get the behavior you want is to call the function directly from the SELECT list and not via the FROM clause: SELECT udt_f2(100, '(4, "Washington")') AS city_composite When done this way the entire result of the function is interpreted as a single column. You could also write: SELCET (udt_f2.*)::city AS city_composite FROM udt_f2(...) Adding additional output columns will also cause the composite type to revert to being a single output column. I do see where there could be value in declaring whether the function should return a single composite column or an expanded "table" but the expanded form is very useful as well and at this point would remain the default no matter what changes are considered. Something like: CREATE FUNCTION name(...) RETURNS composite_type SINGLE COLUMN ONLY AS $$ .... $$ LANGUAGE ... ; David J.