1

I've been trying to add a column in an object column inside another object column but not able to do so (for nested objects).

Adding columns

Adding a column inside an object is straight forward. How to add column inside nested object which is one level deep (or N levels deep) ?

create table my_table (name string, age integer, book object as (isbn string));

example row: {"age": 34, "book": {"isbn": "1"}, "name": "my_name"}

I have tried to add an object column 'author' inside book object column but the following alter statements fail

alter table my_table add column person['book['author']'] object as (authorId as integer)
alter table my_table add column person['book']['author'] object as (authorId as integer)
alter table my_table add column person['book[author]'] object as (authorId as integer)
alter table my_table add column person[book['author']] object as (authorId as integer)

What is the correct syntax for adding column(s) inside nested objects ?

Kind Regards.

Hasnain
  • 1,879
  • 2
  • 13
  • 12

1 Answers1

1

Nested objects are accessed in this form:

select obj['level1']['level2']['level3']

and so forth.

So the alter table statement would look like your second example, but without the "as integer":

alter table my_table add column book['person']['author'] object as ("authorId" integer);

(note that the authorId is in double quotes to preserve the case, without it it would became all lowercase)

Or the same could also be accomplished with a slightly different syntax:

alter table my_table add column book['person']['author']['authorId'] integer;
mfussenegger
  • 3,931
  • 23
  • 18
  • Thanks for the correct syntax. The second alter statement above was almost correct. I was able to add an object column but was not able to alter again to add another column "authorName" inside author column and it gave the following error "SQLActionException[The table "my_table" already has a column named "book['person']"]" `alter table my_table add column book['person']['author']['authorName'] string;` – Hasnain Nov 03 '14 at 10:22
  • @Hasnain you're right, that seems to be a bug. We'll look into it – mfussenegger Nov 04 '14 at 08:13
  • thanks for the confirmation. Kindly let us know when the bug is fixed. – Hasnain Nov 05 '14 at 06:06
  • @Hasnain it has been fixed in master already and the fix will be included in our next bugfix-release – mfussenegger Nov 05 '14 at 12:47