/*
 * 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.expression.scalar.arithmetic;

import com.google.common.collect.ImmutableList;
import io.crate.data.Input;
import io.crate.expression.scalar.ScalarFunctionModule;
import io.crate.expression.symbol.FuncArg;
import io.crate.metadata.FunctionIdent;
import io.crate.metadata.FunctionImplementation;
import io.crate.metadata.FunctionInfo;
import io.crate.metadata.FunctionResolver;
import io.crate.metadata.TransactionContext;
import io.crate.metadata.Scalar;
import io.crate.metadata.functions.params.FuncParams;
import io.crate.metadata.functions.params.Param;
import io.crate.types.DataType;

import javax.annotation.Nullable;
import java.util.List;

public class AbsFunction extends Scalar<Number, Number> {

    public static final String NAME = "abs";

    public static void register(ScalarFunctionModule module) {
        module.register(NAME, new Resolver());
    }

    private final FunctionInfo info;

    public AbsFunction(DataType dataType) {
        this.info = new FunctionInfo(
            new FunctionIdent(NAME, ImmutableList.of(dataType)), dataType);
    }

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

    @Override
    public Number evaluate(TransactionContext txnCtx, Input<Number>... args) {
        assert args.length == 1 : "number of args must be 1";
        Number value = args[0].value();
        if (value != null) {
            return (Number) info.returnType().value(Math.abs(value.doubleValue()));
        }
        return null;
    }

    private static class Resolver implements FunctionResolver {

        private final FuncParams funcParams = FuncParams.builder(Param.NUMERIC).build();

        @Override
        public FunctionImplementation getForTypes(List<DataType> symbols) throws IllegalArgumentException {
            return new AbsFunction(symbols.get(0));
        }

        @Nullable
        @Override
        public List<DataType> getSignature(List<? extends FuncArg> symbols) {
            return funcParams.match(symbols);
        }
    }
}

@@ -1075,84 +1075,8 @@
 ort 
-com.google.common.collect.ImmutableList;%0Aimport io.crate.data.Input;%0Aimport 
 io.c
@@ -1118,16 +1118,63 @@
 onModule
+;%0Aimport io.crate.expression.scalar.UnaryScalar
 ;%0Aimport
@@ -1214,48 +1214,8 @@
 rg;%0A
-import io.crate.metadata.FunctionIdent;%0A
 impo
@@ -1263,47 +1263,8 @@
 on;%0A
-import io.crate.metadata.FunctionInfo;%0A
 impo
@@ -1306,86 +1306,8 @@
 er;%0A
-import io.crate.metadata.TransactionContext;%0Aimport io.crate.metadata.Scalar;%0A
 impo
@@ -1360,57 +1360,8 @@
 ms;%0A
-import io.crate.metadata.functions.params.Param;%0A
 impo
@@ -1454,16 +1454,22 @@
 %0Apublic 
+final 
 class Ab
@@ -1481,39 +1481,8 @@
 tion
- extends Scalar%3CNumber, Number%3E
  %7B%0A%0A
@@ -1660,199 +1660,65 @@
 ate 
-final FunctionInfo info;%0A%0A    public AbsFunction(DataType dataType) %7B%0A        this.info = new FunctionInfo(%0A            new FunctionIdent(NAME, ImmutableList.of(dataType)), dataType);
+static class Resolver implements FunctionResolver %7B%0A
 %0A    
-%7D%0A%0A
     
@@ -1719,32 +1719,36 @@
       @Override%0A
+    
     public Funct
@@ -1755,356 +1755,178 @@
 ionI
-nfo info() %7B%0A        return info;%0A    %7D%0A%0A    @Override%0A    public Number evaluate(TransactionContext txnCtx, Input%3CNumber%3E... args) %7B%0A        assert args.length == 1 : %22number of args must be 1%22;%0A        Number value = args%5B0%5D.value();%0A        if (value != null) %7B%0A            return (Number) info.returnType().value(Math.abs(value.doubleValue()));
+mplementation getForTypes(List%3CDataType%3E argTypes) throws IllegalArgumentException %7B%0A            DataType argType = argTypes.get(0);%0A            return new UnaryScalar%3C%3E(
 %0A   
@@ -1934,18 +1934,16 @@
     
-%7D%0A
         
 retu
@@ -1942,358 +1942,149 @@
     
-return null;%0A
+NAME,%0A      
     
-%7D%0A%0A
     
-private static class Resolver implements FunctionResolver %7B%0A%0A        private final FuncParams funcParams = FuncParams.builder(Param.NUMERIC).build();%0A%0A        @Override%0A        public FunctionImplementation getForTypes(List%3CDataType%3E symbols) throws IllegalArgumentException %7B%0A            return new AbsFunction(symbols.get(0)
+  argType,%0A                argType,%0A                x -%3E argType.value(Math.abs(((Number) x).doubleValue()))%0A            
 );%0A 
@@ -2226,17 +2226,17 @@
  return 
-f
+F
 uncParam
@@ -2237,16 +2237,31 @@
 cParams.
+SINGLE_NUMERIC.
 match(sy

Use UnaryScalar in abs implementation