/*
 * Licensed to Crate.io GmbH ("Crate") under one or more contributor
 * license agreements.  See the NOTICE file distributed with this work for
 * additional information regarding copyright ownership.  Crate licenses
 * this file to you under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.  You may
 * obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations
 * under the License.
 *
 * However, if you have executed another commercial license agreement
 * with Crate these terms will supersede the license and you may use the
 * software solely pursuant to the terms of the relevant commercial agreement.
 */

package io.crate.metadata;

import java.io.IOException;

import javax.annotation.Nullable;

import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;

import io.crate.expression.symbol.Symbol;
import io.crate.expression.symbol.SymbolType;
import io.crate.sql.tree.ColumnPolicy;

public interface Reference extends Symbol {

    ReferenceIdent ident();

    ColumnIdent column();

    IndexType indexType();

    ColumnPolicy columnPolicy();

    boolean isNullable();

    RowGranularity granularity();

    int position();

    boolean hasDocValues();

    @Nullable
    Symbol defaultExpression();

    Reference getRelocated(ReferenceIdent referenceIdent);

    static void toStream(Reference ref, StreamOutput out) throws IOException {
        out.writeVInt(ref.symbolType().ordinal());
        ref.writeTo(out);
    }

    @SuppressWarnings("unchecked")
    static <T extends Reference> T fromStream(StreamInput in) throws IOException {
        return (T) SymbolType.VALUES.get(in.readVInt()).newInstance(in);
    }
}

@@ -1061,16 +1061,93 @@
 ception;
+%0Aimport java.util.ArrayList;%0Aimport java.util.HashMap;%0Aimport java.util.List;
 %0A%0Aimport
@@ -2156,11 +2156,1446 @@
 );%0A    %7D
+%0A%0A    /**%0A     * Builds a hierarchy for an object column(s) out of the flat structure.%0A     *%0A     * @param references must contain all path members of each leaf node%0A     * to make sure that leaf references are reachable from the root.%0A     *%0A     * @return tree represented by sort of %22adjacency list%22.%0A     * We identify references by FQN and store tree as a map(ident -%3E list(reference)).%0A     * NULL node is a root which is an entry point for any traversing method utilizing the tree.%0A     */%0A     static HashMap%3CColumnIdent, List%3CReference%3E%3E buildTree(List%3CReference%3E references) %7B%0A        HashMap%3CColumnIdent, List%3CReference%3E%3E tree = new HashMap%3C%3E();%0A        for (Reference treeNode: references) %7B%0A            // To build an %22adjacency list%22 we add each edge only once, thus we add only direct neighbor node (parent).%0A            // I.e if a leaf node C has path A-B we add only (B,C) edge when handling node C.%0A            // Edge (A,B) will be added later when processing node B:%0A            // we have a requirement to contain all path nodes in the flat list, so it's guaranteed that we will process B at some point and add (B,A).%0A            List%3CReference%3E siblings = tree.computeIfAbsent(treeNode.column().getParent(), k -%3E new ArrayList%3C%3E()); // When parent is null we are adding a root.%0A            siblings.add(treeNode); // Every node is added only once, no duplicates in the list.%0A        %7D%0A        return tree;%0A    %7D
 %0A%7D%0A

Create common method to create Reference hierarchy out of flat structure