Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions core/src/main/antlr4/org/apache/druid/math/expr/antlr/Expr.g4
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,12 @@ numericElement : (LONG | DOUBLE | NULL);
literalElement : (STRING | LONG | DOUBLE | NULL);

NULL : 'null';
LONG : [0-9]+;
EXP: [eE] [-]? LONG;
// DOUBLE provides partial support for java double format
// see: https://docs.oracle.com/javase/8/docs/api/java/lang/Double.html#valueOf-java.lang.String-
DOUBLE : 'NaN' | 'Infinity' | (LONG '.' LONG?) | (LONG EXP) | (LONG '.' LONG? EXP);
IDENTIFIER : [_$a-zA-Z][_$a-zA-Z0-9]* | '"' (ESC | ~ [\"\\])* '"';
LONG : [0-9]+ ;
DOUBLE : [0-9]+ '.' [0-9]* ;
WS : [ \t\r\n]+ -> skip ;

STRING : '\'' (ESC | ~ [\'\\])* '\'';
Expand Down
231 changes: 231 additions & 0 deletions core/src/main/java/org/apache/druid/math/expr/Function.java
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,40 @@ public ExprType getOutputType(Expr.InputBindingInspector inspector, List<Expr> a
}
}

abstract class BivariateBitwiseMathFunction extends BivariateFunction
{
@Override
protected final ExprEval eval(ExprEval x, ExprEval y)
{
// this is a copy of the logic of BivariateMathFunction for string handling, which itself is a
// remix of BinaryEvalOpExprBase.eval modified so that string inputs are always null outputs
if (NullHandling.sqlCompatible() && (x.value() == null || y.value() == null)) {
return ExprEval.of(null);
}

ExprType type = ExprTypeConversion.autoDetect(x, y);
if (type == ExprType.STRING) {
return ExprEval.of(null);
}
return eval(x.asLong(), y.asLong());
}

protected abstract ExprEval eval(long x, long y);

@Nullable
@Override
public ExprType getOutputType(Expr.InputBindingInspector inspector, List<Expr> args)
{
return ExprType.LONG;
}

@Override
public boolean canVectorize(Expr.InputBindingInspector inspector, List<Expr> args)
{
return inspector.areNumeric(args) && inspector.canVectorize(args);
}
}

/**
* Base class for a 2 variable input {@link Function} whose first argument is a {@link ExprType#STRING} and second
* argument is {@link ExprType#LONG}
Expand Down Expand Up @@ -724,6 +758,203 @@ public <T> ExprVectorProcessor<T> asVectorProcessor(Expr.VectorInputBindingInspe
}
}

class BitwiseComplement extends UnivariateMathFunction
{
@Override
public String name()
{
return "bitwiseComplement";
}

@Nullable
@Override
public ExprType getOutputType(Expr.InputBindingInspector inspector, List<Expr> args)
{
return ExprType.LONG;
}

@Override
protected ExprEval eval(long param)
{
return ExprEval.of(~param);
}

@Override
public <T> ExprVectorProcessor<T> asVectorProcessor(Expr.VectorInputBindingInspector inspector, List<Expr> args)
{
return VectorMathProcessors.bitwiseComplement(inspector, args.get(0));
}
}

class BitwiseConvertLongBitsToDouble extends UnivariateMathFunction
{
@Override
public String name()
{
return "bitwiseConvertLongBitsToDouble";
}

@Nullable
@Override
public ExprType getOutputType(Expr.InputBindingInspector inspector, List<Expr> args)
{
ExprType type = args.get(0).getOutputType(inspector);
if (type == null) {
return null;
}
return ExprType.DOUBLE;
}

@Override
protected ExprEval eval(long param)
{
return ExprEval.of(Double.longBitsToDouble(param));
}

@Override
public <T> ExprVectorProcessor<T> asVectorProcessor(Expr.VectorInputBindingInspector inspector, List<Expr> args)
{
return VectorMathProcessors.bitwiseConvertLongBitsToDouble(inspector, args.get(0));
}
}

class BitwiseConvertDoubleToLongBits extends UnivariateMathFunction
{
@Override
public String name()
{
return "bitwiseConvertDoubleToLongBits";
}

@Nullable
@Override
public ExprType getOutputType(Expr.InputBindingInspector inspector, List<Expr> args)
{
ExprType type = args.get(0).getOutputType(inspector);
if (type == null) {
return null;
}
return ExprType.LONG;
}

@Override
protected ExprEval eval(double param)
{
return ExprEval.of(Double.doubleToLongBits(param));
}

@Override
public <T> ExprVectorProcessor<T> asVectorProcessor(Expr.VectorInputBindingInspector inspector, List<Expr> args)
{
return VectorMathProcessors.bitwiseConvertDoubleToLongBits(inspector, args.get(0));
}
}

class BitwiseAnd extends BivariateBitwiseMathFunction
{
@Override
public String name()
{
return "bitwiseAnd";
}

@Override
protected ExprEval eval(long x, long y)
{
return ExprEval.of(x & y);
}

@Override
public <T> ExprVectorProcessor<T> asVectorProcessor(Expr.VectorInputBindingInspector inspector, List<Expr> args)
{
return VectorMathProcessors.bitwiseAnd(inspector, args.get(0), args.get(1));
}
}

class BitwiseOr extends BivariateBitwiseMathFunction
{
@Override
public String name()
{
return "bitwiseOr";
}

@Override
protected ExprEval eval(long x, long y)
{
return ExprEval.of(x | y);
}

@Override
public <T> ExprVectorProcessor<T> asVectorProcessor(Expr.VectorInputBindingInspector inspector, List<Expr> args)
{
return VectorMathProcessors.bitwiseOr(inspector, args.get(0), args.get(1));
}
}

class BitwiseShiftLeft extends BivariateBitwiseMathFunction
{
@Override
public String name()
{
return "bitwiseShiftLeft";
}

@Override
protected ExprEval eval(long x, long y)
{
return ExprEval.of(x << y);
}

@Override
public <T> ExprVectorProcessor<T> asVectorProcessor(Expr.VectorInputBindingInspector inspector, List<Expr> args)
{
return VectorMathProcessors.bitwiseShiftLeft(inspector, args.get(0), args.get(1));
}
}

class BitwiseShiftRight extends BivariateBitwiseMathFunction
{
@Override
public String name()
{
return "bitwiseShiftRight";
}

@Override
protected ExprEval eval(long x, long y)
{
return ExprEval.of(x >> y);
}

@Override
public <T> ExprVectorProcessor<T> asVectorProcessor(Expr.VectorInputBindingInspector inspector, List<Expr> args)
{
return VectorMathProcessors.bitwiseShiftRight(inspector, args.get(0), args.get(1));
}
}

class BitwiseXor extends BivariateBitwiseMathFunction
{
@Override
public String name()
{
return "bitwiseXor";
}

@Override
protected ExprEval eval(long x, long y)
{
return ExprEval.of(x ^ y);
}

@Override
public <T> ExprVectorProcessor<T> asVectorProcessor(Expr.VectorInputBindingInspector inspector, List<Expr> args)
{
return VectorMathProcessors.bitwiseXor(inspector, args.get(0), args.get(1));
}
}

class Cbrt extends DoubleUnivariateMathFunction
{
@Override
Expand Down
Loading