Skip to content

Commit

Permalink
implemented scalar function log(x), log(b,x) and ln(x)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasender authored and msbt committed Jul 29, 2014
1 parent 798267b commit 24425b4
Show file tree
Hide file tree
Showing 5 changed files with 490 additions and 3 deletions.
59 changes: 56 additions & 3 deletions docs/sql/scalar.txt
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,6 @@ If the input value is of type long or double the return value will be of type lo

See below for an example::


cr> select floor(29.9) from sys.cluster;
+-------------+
| floor(29.9) |
Expand All @@ -338,14 +337,13 @@ abs(number)

Returns the absolute value of the given number in the datatype of the given number::

Example::

cr> select abs(214748.0998), abs(0), abs(-214748) from sys.cluster;
+------------------+--------+--------------+
| abs(214748.0998) | abs(0) | abs(-214748) |
+------------------+--------+--------------+
| 214748.0998 | 0 | 214748 |
+------------------+--------+--------------+
SELECT 1 row in set (... sec)

sqrt(number) returns double
---------------------------
Expand All @@ -361,3 +359,58 @@ See below for an example::
| 5.0 |
+------------+
SELECT 1 row in set (... sec)

log(b : number, x : number) returns double
------------------------------------------

Returns the logarithm of given ``x`` to base ``b``.

See below for an example, which essentially is the same as above::

cr> SELECT log(10, 100) FROM sys.cluster;
+--------------+
| log(10, 100) |
+--------------+
| 2.0 |
+--------------+
SELECT 1 row in set (... sec)

The first argument (``b``) is optional. If not present, base 10 is used::

cr> SELECT log(100) FROM sys.cluster;
+----------+
| log(100) |
+----------+
| 2.0 |
+----------+
SELECT 1 row in set (... sec)

.. note::

An error is returned for arguments which lead to undefined or illegal
results. E.g. log(0) results in ``minus infinity``, and therefore, an
error is returned.

The same is true for arguments which lead to a ``division by zero``, as
e.g. log(1, 10) does.

ln(number) returns double
------------------------------------------

Returns the natural logarithm of given ``number``.

See below for an example::

cr> SELECT ln(1) FROM sys.cluster;
+-------+
| ln(1) |
+-------+
| 0.0 |
+-------+
SELECT 1 row in set (... sec)

.. note::

An error is returned for arguments which lead to undefined or illegal
results. E.g. ln(0) results in ``minus infinity``, and therefore, an
error is returned.
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,6 @@ protected void configure() {
AbsFunction.register(this);
FloorFunction.register(this);
SquareRootFunction.register(this);
LogFunction.register(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
/*
* 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.scalar.arithmetic;

import io.crate.metadata.FunctionIdent;
import io.crate.metadata.FunctionInfo;
import io.crate.metadata.Scalar;
import io.crate.operation.Input;
import io.crate.operation.scalar.ScalarFunctionModule;
import io.crate.planner.symbol.Function;
import io.crate.planner.symbol.Literal;
import io.crate.planner.symbol.Symbol;
import io.crate.types.DataType;
import io.crate.types.DataTypes;

import java.util.Arrays;

public abstract class LogFunction implements Scalar<Number,Number> {

public static final String NAME = "log";

protected final FunctionInfo info;

public static void register(ScalarFunctionModule module) {
LogBaseFunction.registerLogBaseFunctions(module);
Log10Function.registerLog10Functions(module);
LnFunction.registerLnFunctions(module);
}

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

/**
* @param result
* @param caller used in the error message for clarification purposes.
* @return
*/
protected Double validateResult(Double result, String caller) {
if (result == null) {
return null;
}
if (Double.isNaN(result) || Double.isInfinite(result)) {
throw new IllegalArgumentException(caller + ": given arguments would result in: '" + result + "'");
}
return result;
}

public LogFunction(FunctionInfo info) {
this.info = info;
}

static class LogBaseFunction extends LogFunction {

protected static void registerLogBaseFunctions(ScalarFunctionModule module) {
// log(baseType, valueType) : double
for (DataType baseType : DataTypes.NUMERIC_PRIMITIVE_TYPES) {
for (DataType valueType : DataTypes.NUMERIC_PRIMITIVE_TYPES) {
FunctionInfo info = new FunctionInfo(
new FunctionIdent(
NAME,
Arrays.asList(baseType, valueType)
),
DataTypes.DOUBLE
);
module.register(new LogBaseFunction(info));
}
}
}

public LogBaseFunction(FunctionInfo info) {
super(info);
}

@Override
public Symbol normalizeSymbol(Function symbol) {
assert (symbol.arguments().size() == 2);
Symbol base = symbol.arguments().get(0);
Symbol value = symbol.arguments().get(1);
if (value.symbolType().isValueSymbol() && base.symbolType().isValueSymbol()) {
return Literal.newLiteral(info.returnType(), evaluate((Input) base, (Input) value));
}
return symbol;
}

@Override
public Number evaluate(Input<Number>... args) {
assert args.length == 2;
if (args[0].value() == null || args[1].value() == null) {
return null;
}
double base = args[0].value().doubleValue();
double value = args[1].value().doubleValue();
double baseResult = Math.log(base);
if (baseResult == 0) {
throw new IllegalArgumentException("log(b, x): given 'base' would result in a division by zero.");
}
return validateResult(Math.log(value) / baseResult, "log(b, x)");
}

}

static class Log10Function extends LogFunction {

protected static void registerLog10Functions(ScalarFunctionModule module) {
// log(dataType) : double
for (DataType dt : DataTypes.NUMERIC_PRIMITIVE_TYPES) {
FunctionInfo info = new FunctionInfo(new FunctionIdent(NAME, Arrays.asList(dt)), DataTypes.DOUBLE);
module.register(new Log10Function(info));
}
}

public Log10Function(FunctionInfo info) {
super(info);
}

@Override
public Symbol normalizeSymbol(Function symbol) {
assert (symbol.arguments().size() == 1);
Symbol value = symbol.arguments().get(0);
if (value.symbolType().isValueSymbol()) {
return Literal.newLiteral(info.returnType(), evaluate((Input)value));
}
return symbol;
}

@Override
public Number evaluate(Input<Number>... args) {
assert args.length == 1;
if (args[0].value() == null) {
return null;
}
double value = args[0].value().doubleValue();
return evaluate(value);
}

protected Double evaluate(double value) {
return validateResult(Math.log10(value), "log(x)");
}

}

public static class LnFunction extends Log10Function {

protected static void registerLnFunctions(ScalarFunctionModule module) {
// ln(dataType) : double
for (DataType dt : DataTypes.NUMERIC_PRIMITIVE_TYPES) {
FunctionInfo info = new FunctionInfo(new FunctionIdent(LnFunction.NAME, Arrays.asList(dt)), DataTypes.DOUBLE);
module.register(new LnFunction(info));
}
}

public static final String NAME = "ln";

public LnFunction(FunctionInfo info) {
super(info);
}

@Override
protected Double evaluate(double value) {
return validateResult(Math.log(value), "ln(x)");
}
}

}
Loading

0 comments on commit 24425b4

Please sign in to comment.