'Given a column or nested type, return the corresponding SELECT and FROM clauses in Impala\'s nested-type syntax'

@classmethod
def get_nested_select(cls, database, table, column, nested=None):

    select_tokens = [column]
    from_tokens = [database, table]
    if nested:
        nested_tokens = nested.strip('/').split('/')
        while nested_tokens:
            token = nested_tokens.pop(0)
            if (token not in ['key', 'value', 'item']):
                select_tokens.append(token)
            else:
                from_tokens.extend(select_tokens)
                select_tokens = []
                if (not nested_tokens):
                    select_tokens = [token]
    select_clause = '.'.join(select_tokens)
    from_clause = '.'.join((('`%s`' % token.strip('`')) for token in from_tokens))
    return (select_clause, from_clause)
