You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@hive.apache.org by Lars Francke <la...@gmail.com> on 2011/01/13 19:10:51 UTC

Returning multiple values from an UDF

Hello,

I'm looking for help with UDFs.

I need a UDF to do something like this:
SELECT parseCoordinates(latitude, longitude) AS lat, lng FROM foo;

But I'm not sure how. I've implemented a UDF using GenericUDF
returning a struct with two fields and that works as in me getting
back a struct (e.g. "{"latitude":2.0,"longitude":10.0}") but that's
still one field and I don't know how to split that back in two
columns. I could obviously store that in an intermediate table but
that seems like a hassle and I'd like to avoid it.

Any ideas how to do this properly?

I've tried things like:
SELECT parseCoordinates(latitude, longitude) AS lat, lng FROM foo;
SELECT parseCoordinates(latitude, longitude) AS (lat, lng) FROM foo;
SELECT parseCoordinates(latitude, longitude) AS bar, bar.latitude AS
lat, bar.longitude AS lng FROM foo;

etc. but I always get an error: FAILED: Error in semantic analysis: AS
clause has an invalid number of aliases

Thanks a lot.

Cheers,
Lars

Re: Returning multiple values from an UDF

Posted by Lars Francke <la...@gmail.com>.
Thank you very much!

It worked with one small correction:

SELECT coord.latitude, coord.longitude FROM
(SELECT parseCoordinates(latitude, longitude) as coord FROM foo) bar;

Going to test this some more now.

Cheers,
Lars

PS: Sorry for sending this to the old list

Re: Returning multiple values from an UDF

Posted by John Sichi <js...@fb.com>.
SELECT bar.latitude, bar.longitude FROM
(SELECT parseCoordinates(latitude, longitude) as coord FROM foo) bar;

JVS

> SELECT parseCoordinates(latitude, longitude) AS lat, lng FROM foo;

On Jan 13, 2011, at 10:10 AM, Lars Francke wrote:

> Hello,
> 
> I'm looking for help with UDFs.
> 
> I need a UDF to do something like this:
> SELECT parseCoordinates(latitude, longitude) AS lat, lng FROM foo;
> 
> But I'm not sure how. I've implemented a UDF using GenericUDF
> returning a struct with two fields and that works as in me getting
> back a struct (e.g. "{"latitude":2.0,"longitude":10.0}") but that's
> still one field and I don't know how to split that back in two
> columns. I could obviously store that in an intermediate table but
> that seems like a hassle and I'd like to avoid it.
> 
> Any ideas how to do this properly?
> 
> I've tried things like:
> SELECT parseCoordinates(latitude, longitude) AS lat, lng FROM foo;
> SELECT parseCoordinates(latitude, longitude) AS (lat, lng) FROM foo;
> SELECT parseCoordinates(latitude, longitude) AS bar, bar.latitude AS
> lat, bar.longitude AS lng FROM foo;
> 
> etc. but I always get an error: FAILED: Error in semantic analysis: AS
> clause has an invalid number of aliases
> 
> Thanks a lot.
> 
> Cheers,
> Lars