Hive - How to cast array to string? - hive

I'm trying to coerce a column containing a comma separated array to a string in Hive.
SELECT email_address, CAST(explode(GP_array AS STRING)) AS GP
FROM dm.TP
i get the following error
Line: 1 - FAILED: SemanticException [Error 10081]: UDTF's are not supported outside the SELECT clause, nor nested in expressions

explode function Explodes an array to multiple rows. Returns a row-set with a single column (col), one row for each element from the array.
you would need concat_ws function to concatenate comma separated array values to String.
function: concat_ws(string SEP, array)
Refer to: https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF
SELECT email_address, concat_ws(',', GP_array) AS GP FROM dm.TP
concat_ws will return the comma separated String constructed from Array values.

Related

Snowflake; convert strings to an array

Using snowflake, I have a column named 'column_1'. The datatype is TEXT.
An example of a value in this column is here:
["Apple", "Pear","Chicken"]
I say:
select to_array(column_1) from fake_table; and I get:
[ "[\"Apple\",\"Pear\",\"Chicken\"]" ]
So it put my text into it. But I want to convert the datatype. Seems like it should be simple.
I try strtok_to_array(column_1, ',') and get the same situation.
How can snowflake convert strings to an array?
Using PARSE_JSON:
SELECT PARSE_JSON('["Apple", "Pear","Chicken"]')::ARRAY;
DESC RESULT LAST_QUERY_ID();
Output:
Since that's valid JSON, you can use the PARSE_JSON function:
select parse_json('["Apple", "Pear","Chicken"]');
select parse_json('["Apple", "Pear","Chicken"]')[0]; -- Get first one
select parse_json('["Apple", "Pear","Chicken"]')[0]::string; -- Cast to string
I'd say parse_json is the way to go, but if you're concerned some values might not be a valid json, you could get rid of the double quotes and square brackets and split the resulting comma separated string to array
select split(translate(col,$$"[]$$,''),',')
Note : Encapsulating in $$ makes escaping quotes and any other special character easier

Passing Comma Separated List to Function with VARIDIAC parameter as input

I have a user defined function that takes a VARIADIC parameter as an input:
func(name string, VARIADIC values double) returns Blob
Example: select func('abc', 35.2,35.3,35.5)
How can I pass row based float/double values to comma separated double type in TEIID
From:
35.2
35.3
35.5
To:
35.2,35.3,35.5
as a single double/float comma separated variable.
Thank you
See the ARRAY_AGG aggregate function:
select func('abc', select array_agg(double_col) from something)
There's also array constructors ARRAY[value,...] and (value, ...)

Check if string is substring of any element in jsonb array in Postgres

I have a column of type jsonb in a Postgres database. How do I check if a string is a substring of any element in the jsonb array? Ideally, the solution would also ignore capitalization.
Try this :
with cte as (
select jsonb_array_elements('["hello", "world", "earth", "universe"]') as word
from test
)
select cte.word, position('wor' in cte.word::text) > 0 "isSubstr"
from cte;

Hive array specifying multiple delimiter in collection

I have dataset contains two arrays, both arrays separated by different delimiter..
Ex: 14-20-50-60 is 1st array seperated by -
12#2#333#4 is 2nd array seperated by #..
While creating table how do we specify delimiter in
Collection items terminated by '' ?
input
14-20-50-60,12#2#333#4
create table test(first array<string>, second array<string>)
row format delimited
fields terminated by ','
collection items terminated by '-' (How to specify two delimiters in the collection)
You cannot use multiple delimiters for the collection items. You can achieve what you are trying to do as below though. I have used the SPLIT function to create the array using different delimiters.
Data
14-20-50-60,12#2#333#4
SQL - CREATE TABLE
create external table test1(first string, second string)
row format delimited
fields terminated by ','
LOCATION '/user/cloudera/ramesh/test1';
SQL - SELECT
WITH v_test_array AS
(SELECT split(first, "-") AS first_array,
split(second, "#") AS second_array
FROM test1)
SELECT first_array[0], second_array[0]
FROM v_test_array;
OUTPUT
14 12
Hope this helps.

Add single quote to string

Given this data:
col1
----
foo
bar
I want concatenate the rows together, and end up with 'foo','bar'.
Using collect_set gets me an array, concat_ws gets me a comma separated string.
select
concat_ws(',',collect_set(col1))
I cannot figure out how to get the single quotes in there.
concat('''',col1,'''') just returns the value of col1.
What am I doing wrong?
You need to escape the quotes.
concat('\'',col1,'\'')