Skip to content

Commit

Permalink
Math expressional parameters for aggregator (apache#2783)
Browse files Browse the repository at this point in the history
* Supports expression-paramed aggregator (squashed and rebased on master) also includes math post aggregator (was apache#2820)

* Addressed comments

* addressed comments
  • Loading branch information
navis authored and himanshug committed Oct 19, 2016
1 parent b113a34 commit 8b7ff44
Show file tree
Hide file tree
Showing 40 changed files with 1,434 additions and 383 deletions.
7 changes: 6 additions & 1 deletion common/src/main/java/io/druid/common/utils/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/
public class StringUtils extends com.metamx.common.StringUtils
{
public static final String EMPTY = "";
private static final byte[] EMPTY_BYTES = new byte[0];

// should be used only for estimation
// returns the same result with StringUtils.fromUtf8(value).length for valid string values
Expand All @@ -46,4 +46,9 @@ public static int estimatedBinaryLengthAsUTF8(String value)
}
return length;
}

public static byte[] toUtf8WithNullToEmpty(final String string)
{
return string == null ? EMPTY_BYTES : toUtf8(string);
}
}
43 changes: 43 additions & 0 deletions common/src/main/java/io/druid/math/expr/Evals.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Licensed to Metamarkets Group Inc. (Metamarkets) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Metamarkets 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.
*/

package io.druid.math.expr;

import com.google.common.primitives.Longs;

/**
*/
public class Evals
{
public static Number toNumber(Object value)
{
if (value == null) {
return 0L;
}
if (value instanceof Number) {
return (Number) value;
}
String stringValue = String.valueOf(value);
Long longValue = Longs.tryParse(stringValue);
if (longValue == null) {
return Double.valueOf(stringValue);
}
return longValue;
}
}
125 changes: 91 additions & 34 deletions common/src/main/java/io/druid/math/expr/Expr.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,36 @@
import com.google.common.math.LongMath;

import java.util.List;
import java.util.Map;

/**
*/
public interface Expr
{
Number eval(Map<String, Number> bindings);
Number eval(ObjectBinding bindings);

interface ObjectBinding
{
Number get(String name);
}

void visit(Visitor visitor);

interface Visitor
{
void visit(Expr expr);
}
}

abstract class ConstantExpr implements Expr
{
@Override
public void visit(Visitor visitor)
{
visitor.visit(this);
}
}

class LongExpr implements Expr
class LongExpr extends ConstantExpr
{
private final long value;

Expand All @@ -47,13 +67,13 @@ public String toString()
}

@Override
public Number eval(Map<String, Number> bindings)
public Number eval(ObjectBinding bindings)
{
return value;
}
}

class DoubleExpr implements Expr
class DoubleExpr extends ConstantExpr
{
private final double value;

Expand All @@ -69,13 +89,13 @@ public String toString()
}

@Override
public Number eval(Map<String, Number> bindings)
public Number eval(ObjectBinding bindings)
{
return value;
}
}

class IdentifierExpr implements Expr
class IdentifierExpr extends ConstantExpr
{
private final String value;

Expand All @@ -91,7 +111,7 @@ public String toString()
}

@Override
public Number eval(Map<String, Number> bindings)
public Number eval(ObjectBinding bindings)
{
Number val = bindings.get(value);
if (val == null) {
Expand All @@ -104,8 +124,8 @@ public Number eval(Map<String, Number> bindings)

class FunctionExpr implements Expr
{
private final String name;
private final List<Expr> args;
final String name;
final List<Expr> args;

public FunctionExpr(String name, List<Expr> args)
{
Expand All @@ -120,23 +140,47 @@ public String toString()
}

@Override
public Number eval(Map<String, Number> bindings)
public Number eval(ObjectBinding bindings)
{
return Parser.func.get(name.toLowerCase()).apply(args, bindings);
}

@Override
public void visit(Visitor visitor)
{
for (Expr child : args) {
child.visit(visitor);
}
visitor.visit(this);
}
}

class UnaryMinusExpr implements Expr
abstract class UnaryExpr implements Expr
{
private final Expr expr;
final Expr expr;

UnaryMinusExpr(Expr expr)
UnaryExpr(Expr expr)
{
this.expr = expr;
}

@Override
public Number eval(Map<String, Number> bindings)
public void visit(Visitor visitor)
{
expr.visit(visitor);
visitor.visit(this);
}
}

class UnaryMinusExpr extends UnaryExpr
{
UnaryMinusExpr(Expr expr)
{
super(expr);
}

@Override
public Number eval(ObjectBinding bindings)
{
Number valObj = expr.eval(bindings);
if (valObj instanceof Long) {
Expand All @@ -146,24 +190,29 @@ public Number eval(Map<String, Number> bindings)
}
}

@Override
public void visit(Visitor visitor)
{
expr.visit(visitor);
visitor.visit(this);
}

@Override
public String toString()
{
return "-" + expr.toString();
}
}

class UnaryNotExpr implements Expr
class UnaryNotExpr extends UnaryExpr
{
private final Expr expr;

UnaryNotExpr(Expr expr)
{
this.expr = expr;
super(expr);
}

@Override
public Number eval(Map<String, Number> bindings)
public Number eval(ObjectBinding bindings)
{
Number valObj = expr.eval(bindings);
return valObj.doubleValue() > 0 ? 0.0d : 1.0d;
Expand Down Expand Up @@ -194,6 +243,14 @@ protected boolean isLong(Number left, Number right)
return left instanceof Long && right instanceof Long;
}

@Override
public void visit(Visitor visitor)
{
left.visit(visitor);
right.visit(visitor);
visitor.visit(this);
}

@Override
public String toString()
{
Expand All @@ -210,7 +267,7 @@ class BinMinusExpr extends BinaryOpExprBase
}

@Override
public Number eval(Map<String, Number> bindings)
public Number eval(ObjectBinding bindings)
{
Number leftVal = left.eval(bindings);
Number rightVal = right.eval(bindings);
Expand All @@ -231,7 +288,7 @@ class BinPowExpr extends BinaryOpExprBase
}

@Override
public Number eval(Map<String, Number> bindings)
public Number eval(ObjectBinding bindings)
{
Number leftVal = left.eval(bindings);
Number rightVal = right.eval(bindings);
Expand All @@ -252,7 +309,7 @@ class BinMulExpr extends BinaryOpExprBase
}

@Override
public Number eval(Map<String, Number> bindings)
public Number eval(ObjectBinding bindings)
{
Number leftVal = left.eval(bindings);
Number rightVal = right.eval(bindings);
Expand All @@ -273,7 +330,7 @@ class BinDivExpr extends BinaryOpExprBase
}

@Override
public Number eval(Map<String, Number> bindings)
public Number eval(ObjectBinding bindings)
{
Number leftVal = left.eval(bindings);
Number rightVal = right.eval(bindings);
Expand All @@ -294,7 +351,7 @@ class BinModuloExpr extends BinaryOpExprBase
}

@Override
public Number eval(Map<String, Number> bindings)
public Number eval(ObjectBinding bindings)
{
Number leftVal = left.eval(bindings);
Number rightVal = right.eval(bindings);
Expand All @@ -315,7 +372,7 @@ class BinPlusExpr extends BinaryOpExprBase
}

@Override
public Number eval(Map<String, Number> bindings)
public Number eval(ObjectBinding bindings)
{
Number leftVal = left.eval(bindings);
Number rightVal = right.eval(bindings);
Expand All @@ -336,7 +393,7 @@ class BinLtExpr extends BinaryOpExprBase
}

@Override
public Number eval(Map<String, Number> bindings)
public Number eval(ObjectBinding bindings)
{
Number leftVal = left.eval(bindings);
Number rightVal = right.eval(bindings);
Expand All @@ -357,7 +414,7 @@ class BinLeqExpr extends BinaryOpExprBase
}

@Override
public Number eval(Map<String, Number> bindings)
public Number eval(ObjectBinding bindings)
{
Number leftVal = left.eval(bindings);
Number rightVal = right.eval(bindings);
Expand All @@ -378,7 +435,7 @@ class BinGtExpr extends BinaryOpExprBase
}

@Override
public Number eval(Map<String, Number> bindings)
public Number eval(ObjectBinding bindings)
{
Number leftVal = left.eval(bindings);
Number rightVal = right.eval(bindings);
Expand All @@ -399,7 +456,7 @@ class BinGeqExpr extends BinaryOpExprBase
}

@Override
public Number eval(Map<String, Number> bindings)
public Number eval(ObjectBinding bindings)
{
Number leftVal = left.eval(bindings);
Number rightVal = right.eval(bindings);
Expand All @@ -420,7 +477,7 @@ class BinEqExpr extends BinaryOpExprBase
}

@Override
public Number eval(Map<String, Number> bindings)
public Number eval(ObjectBinding bindings)
{
Number leftVal = left.eval(bindings);
Number rightVal = right.eval(bindings);
Expand All @@ -441,7 +498,7 @@ class BinNeqExpr extends BinaryOpExprBase
}

@Override
public Number eval(Map<String, Number> bindings)
public Number eval(ObjectBinding bindings)
{
Number leftVal = left.eval(bindings);
Number rightVal = right.eval(bindings);
Expand All @@ -462,7 +519,7 @@ class BinAndExpr extends BinaryOpExprBase
}

@Override
public Number eval(Map<String, Number> bindings)
public Number eval(ObjectBinding bindings)
{
Number leftVal = left.eval(bindings);
Number rightVal = right.eval(bindings);
Expand Down Expand Up @@ -495,7 +552,7 @@ class BinOrExpr extends BinaryOpExprBase
}

@Override
public Number eval(Map<String, Number> bindings)
public Number eval(ObjectBinding bindings)
{
Number leftVal = left.eval(bindings);
Number rightVal = right.eval(bindings);
Expand Down
Loading

0 comments on commit 8b7ff44

Please sign in to comment.