/*
 * Licensed to CRATE Technology 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.operation.reference.doc;

import io.crate.exceptions.CrateException;
import io.crate.exceptions.UnsupportedFeatureException;
import io.crate.metadata.ReferenceInfo;
import io.crate.operation.reference.DocLevelReferenceResolver;
import io.crate.planner.RowGranularity;

public class LuceneDocLevelReferenceResolver implements DocLevelReferenceResolver<LuceneCollectorExpression<?>> {

    public static final LuceneDocLevelReferenceResolver INSTANCE = new LuceneDocLevelReferenceResolver();

    private LuceneDocLevelReferenceResolver() {
    }

    @Override
    public LuceneCollectorExpression<?> getImplementation(ReferenceInfo referenceInfo) {
        assert referenceInfo.granularity() == RowGranularity.DOC;

        if (RawCollectorExpression.COLUMN_NAME.equals(referenceInfo.ident().columnIdent().name())
                || DocCollectorExpression.COLUMN_NAME.equals(referenceInfo.ident().columnIdent().name())){
            if (referenceInfo.ident().columnIdent().isColumn()){
                if (RawCollectorExpression.COLUMN_NAME.equals(referenceInfo.ident().columnIdent().name())) {
                    return new RawCollectorExpression();
                } else {
                    return new DocCollectorExpression();
                }
            } else {
                // TODO: implement an Object source expression which may support subscripts
                throw new UnsupportedFeatureException(
                        String.format("_source expression does not support subscripts %s",
                        referenceInfo.ident().columnIdent().fqn()));
            }
        } else if (DocCollectorExpression.COLUMN_NAME.equals(referenceInfo.ident().columnIdent().name())) {
            return DocCollectorExpression.create(referenceInfo);
        }

        String colName = referenceInfo.ident().columnIdent().fqn();

        switch (referenceInfo.type()) {
            case BYTE:
                return new ByteColumnReference(colName);
            case SHORT:
                return new ShortColumnReference(colName);
            case IP:
            case STRING:
                return new BytesRefColumnReference(colName);
            case DOUBLE:
                return new DoubleColumnReference(colName);
            case BOOLEAN:
                return new BooleanColumnReference(colName);
            case OBJECT:
                return new ObjectColumnReference(colName);
            case FLOAT:
                return new FloatColumnReference(colName);
            case LONG:
            case TIMESTAMP:
                return new LongColumnReference(colName);
            case INTEGER:
                return new IntegerColumnReference(colName);
            default:
                throw new CrateException(String.format("unsupported type '%s'", referenceInfo.type().getName()));
        }
    }
}

@@ -1848,446 +1848,128 @@
 e())
-%0A                %7C%7C DocCollectorExpression.COLUMN_NAME.equals(referenceInfo.ident().columnIdent().name()))%7B%0A            if (referenceInfo.ident().columnIdent().isColumn())%7B%0A                if (RawCollectorExpression.COLUMN_NAME.equals(referenceInfo.ident().columnIdent().name())) %7B%0A                    return new RawCollectorExpression();%0A                %7D else %7B%0A                    return new DocCollectorExpression();%0A                %7D
+)%7B%0A            if (referenceInfo.ident().columnIdent().isColumn())%7B%0A                return new RawCollectorExpression();
 %0A   

fix changes in LuceneDocLevelReferenceResolver