Hello,
We’ve got some csv files coming in that have columns of array data like this example column “hobbies”:
"[
""Volunteering"",
""Traveling"",
""Running""
]"
When I try to access the column using array functions I get various errors.
This works:
select array_contains(convert_from('["Swimming","Travel","Fishing"]','json'),'Travel');
But this fails with “‘ARRAY_CONTAINS’ expects an ARRAY, but instead got: ANY”:
select array_contains(convert_from(hobbies,'json'),'Travel') from ArrayTesting;
This works:
select array_slice(convert_from('["Swimming","Travel","Fishing"]','json'),0,3);
This fails with “‘ARRAY_SLICE’ does not accept the supplied operand types: ANY, INTEGER, INTEGER”:
select array_slice(convert_from(hobbies,'json'),0,3) from ArrayTesting;
Visual comparison of the two looks like the arrays match when selected like this:
Comparing the two convert_from values returns true, again implying that they match:
select
case when
convert_from('["Swimming","Travel","Fishing"]','json') = convert_from(hobbies,'json')
then 'true'
else 'false'
end
FROM ArrayTesting;
Is there something under the hood that’s treating these as the same in this instance, but not when used in array functions? Do I need to do some additional conversion to make this compatible?
Below are various tests I’ve run just to see how things respond.
/*
Result: Success
Output:
EXPR$0
["Swimming","Travel","Fishing"]
*/
select convert_from('["Swimming","Travel","Fishing"]','json');
/*
Result: Success
Output:
EXPR$0
["Swimming","Travel","Fishing"]
*/
select convert_from(hobbies,'json')
from ArrayTesting;
/*
Compare the two convert_from values
Result: Values appear to match
Output:
EXPR$0
true
*/
select
case when
convert_from('["Swimming","Travel","Fishing"]','json') = convert_from(hobbies,'json')
then 'true'
else 'false'
end
FROM ArrayTesting;
/*
Result: Success
Output:
EXPR$0
true
*/
select array_contains(convert_from('["Swimming","Travel","Fishing"]','json'),'Travel');
/*
Result: Fail
Output:
'ARRAY_CONTAINS' expects an ARRAY, but instead got: ANY
*/
select array_contains(convert_from(hobbies,'json'),'Travel')
from ArrayTesting;
/*
Result: Success
Output:
EXPR$0
3
*/
select array_length(convert_from('["Swimming","Travel","Fishing"]','json'));
/*
Result: Fail
Output:
'ARRAY_LENGTH' does not accept the supplied operand types: ANY
*/
select array_length(convert_from(hobbies,'json'))
from ArrayTesting;