Review Board 1.7.22


Implemented new math functions : log, sqrt, cbrt, rand, round, exp, e, pi, pow

Review Request #14311 - Created Sept. 24, 2013 and updated

Yash Sharma
Reviewers
drill-git
drill-git
New math functions log, sqrt, cbrt, rand, round, exp, e, pi, pow implemented.

Few points:
rand() and random() both are supported currently.
pow() and power() both are supported currently.
Currently log() is supported, this can be changed to ln() if suggested.
Yes.
$ mvn test -Dtest=RunSimplePlan#newMathPlan


{
  "E" : 2.718281828459045,
  "exp" : 2.9048849665247426E13,
  "pow" : 2.8629151E7,
  "PI" : 3.141592653589793,
  "sqrt" : 5.5677643628300215,
  "round" : 32.0,
  "rand" : 0.7015373314736347,
  "cbrt" : 3.141380652391393,
  "log" : 3.4339872044851463,
  "log10" : 1.4913616938342726
} 
 {
  "E" : 2.718281828459045,
  "exp" : 2.1464357978591606E14,
  "pow" : 3.9135393E7,
  "PI" : 3.141592653589793,
  "sqrt" : 5.744562646538029,
  "round" : 32.0,
  "rand" : 0.6524812400390043,
  "cbrt" : 3.2075343299958265,
  "log" : 3.4965075614664802,
  "log10" : 1.5185139398778875
} 
 {
  "E" : 2.718281828459045,
  "exp" : 5.834617425274549E14,
  "pow" : 4.5435424E7,
  "PI" : 3.141592653589793,
  "sqrt" : 5.830951894845301,
  "round" : 32.0,
  "rand" : 0.2611928590164273,
  "cbrt" : 3.2396118012774835,
  "log" : 3.5263605246161616,
  "log10" : 1.5314789170422551
} 
 {
  "E" : 2.718281828459045,
  "exp" : 1.5860134523134308E15,
  "pow" : 5.2521875E7,
  "PI" : 3.141592653589793,
  "sqrt" : 5.916079783099616,
  "round" : 32.0,
  "rand" : 0.09218448010530156,
  "cbrt" : 3.2710663101885897,
  "log" : 3.5553480614894135,
  "log10" : 1.5440680443502757
} 
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 3.441 sec - in org.apache.drill.exec.ref.RunSimplePlan

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 12.456s
[INFO] Finished at: Tue Sep 24 11:30:42 IST 2013
[INFO] Final Memory: 21M/52M
[INFO] ------------------------------------------------------------------------
common/src/main/java/org/apache/drill/common/expression/fn/MathFunctions.java
Revision ee3a099 New Change
[20] 14 lines
[+20]
15
 * See the License for the specific language governing permissions and
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
16
 * limitations under the License.
17
 */
17
 */
18
package org.apache.drill.common.expression.fn;
18
package org.apache.drill.common.expression.fn;
19

    
   
19

   

    
   
20
import org.apache.drill.common.expression.Arg;
20
import org.apache.drill.common.expression.ArgumentValidators;
21
import org.apache.drill.common.expression.ArgumentValidators;

    
   
22
import org.apache.drill.common.expression.BasicArgumentValidator;
21
import org.apache.drill.common.expression.FunctionDefinition;
23
import org.apache.drill.common.expression.FunctionDefinition;
22
import org.apache.drill.common.expression.CallProvider;
24
import org.apache.drill.common.expression.CallProvider;
23
import org.apache.drill.common.expression.OutputTypeDeterminer;
25
import org.apache.drill.common.expression.OutputTypeDeterminer;

    
   
26
import org.apache.drill.common.types.TypeProtos;

    
   
27
import org.apache.drill.common.types.Types;
24

    
   
28

   
25

    
   
29

   
26

    
   
30

   
27
public class MathFunctions implements CallProvider{
31
public class MathFunctions implements CallProvider{
28
	
32
	
[+20] [20] 4 lines
[+20] [+] public FunctionDefinition[] getFunctionDefintions() {
33
        FunctionDefinition.operator("add", new ArgumentValidators.NumericTypeAllowed(1, Integer.MAX_VALUE, true), new OutputTypeDeterminer.SameAsAnySoft(), "+"),
37
        FunctionDefinition.operator("add", new ArgumentValidators.NumericTypeAllowed(1, Integer.MAX_VALUE, true), new OutputTypeDeterminer.SameAsAnySoft(), "+"),
34
        FunctionDefinition.operator("subtract", new ArgumentValidators.NumericTypeAllowed(1, Integer.MAX_VALUE, true), new OutputTypeDeterminer.SameAsAnySoft(), "-"),
38
        FunctionDefinition.operator("subtract", new ArgumentValidators.NumericTypeAllowed(1, Integer.MAX_VALUE, true), new OutputTypeDeterminer.SameAsAnySoft(), "-"),
35
        FunctionDefinition.operator("divide", new ArgumentValidators.NumericTypeAllowed(1, Integer.MAX_VALUE, true), new OutputTypeDeterminer.SameAsAnySoft(), "/"),
39
        FunctionDefinition.operator("divide", new ArgumentValidators.NumericTypeAllowed(1, Integer.MAX_VALUE, true), new OutputTypeDeterminer.SameAsAnySoft(), "/"),
36
        FunctionDefinition.operator("multiply", new ArgumentValidators.NumericTypeAllowed(1, Integer.MAX_VALUE, true), new OutputTypeDeterminer.SameAsAnySoft(), "*"),
40
        FunctionDefinition.operator("multiply", new ArgumentValidators.NumericTypeAllowed(1, Integer.MAX_VALUE, true), new OutputTypeDeterminer.SameAsAnySoft(), "*"),
37
        FunctionDefinition.operator("modulo", new ArgumentValidators.NumericTypeAllowed(1, Integer.MAX_VALUE, true), new OutputTypeDeterminer.SameAsAnySoft(), "%"),
41
        FunctionDefinition.operator("modulo", new ArgumentValidators.NumericTypeAllowed(1, Integer.MAX_VALUE, true), new OutputTypeDeterminer.SameAsAnySoft(), "%"),

    
   
42
        FunctionDefinition.simple("floatlog", new BasicArgumentValidator(new Arg(Types.required(TypeProtos.MinorType.FLOAT8))), new OutputTypeDeterminer.SameAsAnySoft(), "log"),

    
   
43
        FunctionDefinition.simple("floatlog10", new BasicArgumentValidator(new Arg(Types.required(TypeProtos.MinorType.FLOAT8))), new OutputTypeDeterminer.SameAsAnySoft(), "log10"),

    
   
44
        FunctionDefinition.simple("floatsqrt", new BasicArgumentValidator(new Arg(Types.required(TypeProtos.MinorType.FLOAT8))), new OutputTypeDeterminer.SameAsAnySoft(), "sqrt"),

    
   
45
        FunctionDefinition.simple("floatcbrt", new BasicArgumentValidator(new Arg(Types.required(TypeProtos.MinorType.FLOAT8))), new OutputTypeDeterminer.SameAsAnySoft(), "cbrt"),

    
   
46
        FunctionDefinition.simple("floatrand", new BasicArgumentValidator(new Arg(Types.required(TypeProtos.MinorType.FLOAT8))), new OutputTypeDeterminer.SameAsAnySoft(), "rand", "random"),

    
   
47
        FunctionDefinition.simple("floatround", new BasicArgumentValidator(new Arg(Types.required(TypeProtos.MinorType.FLOAT8))), new OutputTypeDeterminer.SameAsAnySoft(), "round"),

    
   
48
        FunctionDefinition.simple("floatexp", new BasicArgumentValidator(new Arg(Types.required(TypeProtos.MinorType.FLOAT8))), new OutputTypeDeterminer.SameAsAnySoft(), "exp"),

    
   
49
        FunctionDefinition.simple("floate", new BasicArgumentValidator(new Arg(Types.required(TypeProtos.MinorType.FLOAT8))), new OutputTypeDeterminer.SameAsAnySoft(), "e"),

    
   
50
        FunctionDefinition.simple("floatpi", new BasicArgumentValidator(new Arg(Types.required(TypeProtos.MinorType.FLOAT8))), new OutputTypeDeterminer.SameAsAnySoft(), "pi"),

    
   
51
        FunctionDefinition.simple("floatpow", new BasicArgumentValidator(new Arg(Types.required(TypeProtos.MinorType.FLOAT8))), new OutputTypeDeterminer.SameAsAnySoft(), "pow", "power"),
38
        XOR
52
        XOR
39
    };
53
    };
40

    
   
54

   
41
  }
55
  }
42
  
56
  
43
	public static final FunctionDefinition XOR = FunctionDefinition.operator("xor", new ArgumentValidators.NumericTypeAllowed(1, Integer.MAX_VALUE, true), new OutputTypeDeterminer.SameAsAnySoft(), "^");
57
	public static final FunctionDefinition XOR = FunctionDefinition.operator("xor", new ArgumentValidators.NumericTypeAllowed(1, Integer.MAX_VALUE, true), new OutputTypeDeterminer.SameAsAnySoft(), "^");
44
}
58
}
exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/MathFunctions.java
Revision 288760b New Change
 
exec/ref/src/main/java/org/apache/drill/exec/ref/eval/fn/MathEvaluators.java
Revision 347a4f3 New Change
 
exec/ref/src/test/java/org/apache/drill/exec/ref/RunSimplePlan.java
Revision f4992e0 New Change
 
exec/ref/src/test/resources/new_math.json
New File
 
  1. common/src/main/java/org/apache/drill/common/expression/fn/MathFunctions.java: Loading...
  2. exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/MathFunctions.java: Loading...
  3. exec/ref/src/main/java/org/apache/drill/exec/ref/eval/fn/MathEvaluators.java: Loading...
  4. exec/ref/src/test/java/org/apache/drill/exec/ref/RunSimplePlan.java: Loading...
  5. exec/ref/src/test/resources/new_math.json: Loading...