I have two tables :
create table a (
`1` array<string>);
create table b (
`1` array<int>);
and I want to put the table a in table b (table b is empty) :
insert into table b
select * from a;
when doing so I get the following error :
FAILED: SemanticException [Error 10044]: Line 1:18 Cannot insert into
target table because column number/types are different 'b': Cannot
convert column 0 from array<string> to array<int>.
whereas I would not get this error if the fields were only of types string
and int
.
Is there a way to do the cast with arrays ?
Re-assemble array using
explode()
andcollect_list()
.Initial String array example:
Convert array:
Result:
And if you want to add more columns in your insert+select query then use
lateral view [outer]
:Not easily. You can manually cast the arrays if you know their size, but if not you might need to use structs. See my answer to this similar question.
Also: I cannot downvote the other answer, but it fails for nested selects with more than one array.
Instead of casting array elements and reconstructing the original arrays, it casts and then combines all elements into a single array. Example: