/*
 * 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.operator.operator;

import io.crate.metadata.FunctionInfo;
import io.crate.operator.Input;
import io.crate.planner.symbol.BooleanLiteral;
import io.crate.planner.symbol.Function;
import io.crate.planner.symbol.Symbol;
import org.cratedb.DataType;

public class AndOperator extends Operator {

    public static final String NAME = "op_and";
    public static final FunctionInfo INFO = generateInfo(NAME, DataType.BOOLEAN);

    @Override
    public FunctionInfo info() {
        return INFO;
    }

    public static void register(OperatorModule module) {
        module.registerOperatorFunction(new AndOperator());
    }

    @Override
    public Symbol normalizeSymbol(Function function) {
        Boolean result = true;
        for (Symbol symbol : function.arguments()) {
            if (symbol instanceof BooleanLiteral) {
                result = result && ((BooleanLiteral) symbol).value();
            } else {
                return function; // can't optimize -> return unmodified symbol
            }
        }

        return new BooleanLiteral(result);
    }

    @Override
    public Boolean evaluate(Input<?>... args) {
        if (args == null) {
            return false;
        }
        for (Input<?> input : args) {
            if (!(Boolean)input.value()) {
                return false;
            }
        }

        return true;
    }
}

@@ -1249,16 +1249,59 @@
 Symbol;%0A
+import io.crate.planner.symbol.SymbolType;%0A
 import o
@@ -1778,30 +1778,35 @@
     
-Boolean result = true;
+assert (function != null);%0A
 %0A   
@@ -1874,239 +1874,300 @@
  if 
+(
 (symbol
- instanceof BooleanLiteral) %7B%0A                result = result && ((BooleanLiteral) symbol).value();%0A            %7D else %7B%0A                return function; // can't optimize -%3E return unmodified symbol%0A            %7D%0A        %7D%0A
+.symbolType() != SymbolType.BOOLEAN_LITERAL)) %7B%0A                return function; // can't optimize -%3E return unmodified symbol%0A            %7D%0A        %7D%0A%0A        BooleanLiteral%5B%5D literals = new BooleanLiteral%5Bfunction.arguments().size()%5D;%0A        function.arguments().toArray(literals);
 %0A   
@@ -2201,14 +2201,26 @@
 ral(
-result
+evaluate(literals)
 );%0A 

call 'evaluate' in AndOperator#normalizeSymbol.