Skip to content

Commit

Permalink
add numeric functions
Browse files Browse the repository at this point in the history
  • Loading branch information
LiBinfeng-01 committed Sep 12, 2024
1 parent fe8905a commit fa2f58a
Showing 1 changed file with 360 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,13 @@

import org.apache.doris.nereids.trees.expressions.ExecFunction;
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.literal.BigIntLiteral;
import org.apache.doris.nereids.trees.expressions.literal.DecimalLiteral;
import org.apache.doris.nereids.trees.expressions.literal.DecimalV3Literal;
import org.apache.doris.nereids.trees.expressions.literal.DoubleLiteral;
import org.apache.doris.nereids.trees.expressions.literal.IntegerLiteral;
import org.apache.doris.nereids.trees.expressions.literal.LargeIntLiteral;
import org.apache.doris.nereids.trees.expressions.literal.NullLiteral;
import org.apache.doris.nereids.trees.expressions.literal.SmallIntLiteral;
import org.apache.doris.nereids.trees.expressions.literal.TinyIntLiteral;
import org.apache.doris.nereids.trees.expressions.literal.*;
import org.apache.doris.nereids.types.DecimalV3Type;
import org.apache.doris.plsql.Var;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;

/**
* executable functions:
Expand Down Expand Up @@ -596,4 +590,361 @@ public static Expression divideDecimalV3(DecimalV3Literal first, DecimalV3Litera
return new DecimalV3Literal(DecimalV3Type.createDecimalV3TypeLooseCheck(
t1.getPrecision(), t1.getScale() - t2.getScale()), result);
}

/**
* exp
*/
@ExecFunction(name = "exp", argTypes = {"DOUBLE"}, returnType = "DOUBLE")
public static Expression exp(DoubleLiteral first) {
return new DoubleLiteral(Math.exp(first.getValue()));
}

/**
* ln
*/
@ExecFunction(name = "ln", argTypes = {"DOUBLE"}, returnType = "DOUBLE")
public static Expression ln(DoubleLiteral first) {
return new DoubleLiteral(Math.log1p(first.getValue()));
}

/**
* log
*/
@ExecFunction(name = "log", argTypes = {"DOUBLE", "DOUBLE"}, returnType = "DOUBLE")
public static Expression log(DoubleLiteral first, DoubleLiteral second) {
return new DoubleLiteral(Math.log(first.getValue()) / Math.log(second.getValue()));
}

/**
* log2
*/
@ExecFunction(name = "log2", argTypes = {"DOUBLE"}, returnType = "DOUBLE")
public static Expression log2(DoubleLiteral first) {
return new DoubleLiteral(Math.log(first.getValue()) / Math.log(2.0));
}

/**
* log10
*/
@ExecFunction(name = "log10", argTypes = {"DOUBLE"}, returnType = "DOUBLE")
public static Expression log10(DoubleLiteral first) {
return new DoubleLiteral(Math.log10(first.getValue()));
}

/**
* sqrt
*/
@ExecFunction(name = "sqrt", argTypes = {"DOUBLE"}, returnType = "DOUBLE")
public static Expression sqrt(DoubleLiteral first) {
return new DoubleLiteral(Math.sqrt(first.getValue()));
}

/**
* power
*/
@ExecFunction(name = "power", argTypes = {"DOUBLE", "DOUBLE"}, returnType = "DOUBLE")
public static Expression power(DoubleLiteral first, DoubleLiteral second) {
return new DoubleLiteral(Math.pow(first.getValue(), second.getValue()));
}

/**
* sin
*/
@ExecFunction(name = "sin", argTypes = {"DOUBLE"}, returnType = "DOUBLE")
public static Expression sin(DoubleLiteral first) {
return new DoubleLiteral(Math.sin(first.getValue()));
}

/**
* cos
*/
@ExecFunction(name = "cos", argTypes = {"DOUBLE"}, returnType = "DOUBLE")
public static Expression cos(DoubleLiteral first) {
return new DoubleLiteral(Math.cos(first.getValue()));
}

/**
* tan
*/
@ExecFunction(name = "tan", argTypes = {"DOUBLE"}, returnType = "DOUBLE")
public static Expression tan(DoubleLiteral first) {
return new DoubleLiteral(Math.tan(first.getValue()));
}

/**
* asin
*/
@ExecFunction(name = "asin", argTypes = {"DOUBLE"}, returnType = "DOUBLE")
public static Expression asin(DoubleLiteral first) {
return new DoubleLiteral(Math.asin(first.getValue()));
}

/**
* acos
*/
@ExecFunction(name = "acos", argTypes = {"DOUBLE"}, returnType = "DOUBLE")
public static Expression acos(DoubleLiteral first) {
return new DoubleLiteral(Math.acos(first.getValue()));
}

/**
* atan
*/
@ExecFunction(name = "atan", argTypes = {"DOUBLE"}, returnType = "DOUBLE")
public static Expression atan(DoubleLiteral first) {
return new DoubleLiteral(Math.atan(first.getValue()));
}

/**
* atan2
*/
@ExecFunction(name = "atan2", argTypes = {"DOUBLE", "DOUBLE"}, returnType = "DOUBLE")
public static Expression atan2(DoubleLiteral first, DoubleLiteral second) {
return new DoubleLiteral(Math.atan2(first.getValue(), second.getValue()));
}

/**
* sign
*/
@ExecFunction(name = "sign", argTypes = {"DOUBLE"}, returnType = "TINYINT")
public static Expression sign(DoubleLiteral first) {
if (first.getValue() < 0) {
return new TinyIntLiteral((byte) -1);
} else if (first.getValue() == 0) {
return new TinyIntLiteral((byte) 0);
} else {
return new TinyIntLiteral((byte) 1);
}
}

/**
* bit_count
*/
@ExecFunction(name = "bit_count", argTypes = {"TINYINT"}, returnType = "TINYINT")
public static Expression bitCount(TinyIntLiteral first) {
return new TinyIntLiteral((byte) Integer.bitCount(first.getValue() & 0xFF));
}

/**
* bit_count
*/
@ExecFunction(name = "bit_count", argTypes = {"SMALLINT"}, returnType = "TINYINT")
public static Expression bitCount(SmallIntLiteral first) {
return new TinyIntLiteral((byte) Integer.bitCount(first.getValue() & 0xFFFF));
}

/**
* bit_count
*/
@ExecFunction(name = "bit_count", argTypes = {"INT"}, returnType = "TINYINT")
public static Expression bitCount(IntegerLiteral first) {
return new TinyIntLiteral((byte) Integer.bitCount(first.getValue()));
}

/**
* bit_count
*/
@ExecFunction(name = "bit_count", argTypes = {"BIGINT"}, returnType = "TINYINT")
public static Expression bitCount(BigIntLiteral first) {
return new TinyIntLiteral((byte) Long.bitCount(first.getValue()));
}

/**
* bit_count
*/
@ExecFunction(name = "bit_count", argTypes = {"LARGEINT"}, returnType = "SMALLINT")
public static Expression bitCount(LargeIntLiteral first) {
if (first.getValue().compareTo(BigInteger.ZERO) < 0) {
return new SmallIntLiteral((short) (128 - first.getValue().bitCount()));
} else {
return new SmallIntLiteral((short) first.getValue().bitCount());
}
}

/**
* bit_length
*/
@ExecFunction(name = "bit_length", argTypes = {"VARCHAR"}, returnType = "INT")
public static Expression bitLength(VarcharLiteral first) {
byte[] byteArray = first.getValue().getBytes(StandardCharsets.UTF_8); // Convert to bytes in UTF-8
int byteLength = byteArray.length;
return new IntegerLiteral(byteLength * Byte.SIZE);
}

/**
* bit_length
*/
@ExecFunction(name = "bit_length", argTypes = {"STRING"}, returnType = "INT")
public static Expression bitLength(StringLiteral first) {
byte[] byteArray = first.getValue().getBytes(StandardCharsets.UTF_8); // Convert to bytes in UTF-8
int byteLength = byteArray.length;
return new IntegerLiteral(byteLength * Byte.SIZE);
}

/**
* cbrt
*/
@ExecFunction(name = "cbrt", argTypes = {"DOUBLE"}, returnType = "DOUBLE")
public static Expression cbrt(DoubleLiteral first) {
return new DoubleLiteral(Math.cbrt(first.getValue()));
}

/**
* cosh
*/
@ExecFunction(name = "cosh", argTypes = {"DOUBLE"}, returnType = "DOUBLE")
public static Expression cosh(DoubleLiteral first) {
return new DoubleLiteral(Math.cosh(first.getValue()));
}

/**
* tanh
*/
@ExecFunction(name = "cosh", argTypes = {"DOUBLE"}, returnType = "DOUBLE")
public static Expression tanh(DoubleLiteral first) {
return new DoubleLiteral(Math.tanh(first.getValue()));
}

/**
* dexp
*/
@ExecFunction(name = "dexp", argTypes = {"DOUBLE"}, returnType = "DOUBLE")
public static Expression dexp(DoubleLiteral first) {
return new DoubleLiteral(Math.exp(first.getValue()));
}

/**
* dlog1
*/
@ExecFunction(name = "dlog1", argTypes = {"DOUBLE"}, returnType = "DOUBLE")
public static Expression dlog1(DoubleLiteral first) {
return new DoubleLiteral(Math.log1p(first.getValue()));
}

/**
* dlog10
*/
@ExecFunction(name = "dlog10", argTypes = {"DOUBLE"}, returnType = "DOUBLE")
public static Expression dlog10(DoubleLiteral first) {
return new DoubleLiteral(Math.log10(first.getValue()));
}

/**
* dsqrt
*/
@ExecFunction(name = "dsqrt", argTypes = {"DOUBLE"}, returnType = "DOUBLE")
public static Expression dsqrt(DoubleLiteral first) {
return new DoubleLiteral(Math.sqrt(first.getValue()));
}

/**
* dpower
*/
@ExecFunction(name = "dpow", argTypes = {"DOUBLE", "DOUBLE"}, returnType = "DOUBLE")
public static Expression dpow(DoubleLiteral first, DoubleLiteral second) {
return new DoubleLiteral(Math.pow(first.getValue(), second.getValue()));
}

/**
* fmod
*/
@ExecFunction(name = "fmod", argTypes = {"DOUBLE", "DOUBLE"}, returnType = "DOUBLE")
public static Expression fmod(DoubleLiteral first, DoubleLiteral second) {
return new DoubleLiteral(first.getValue()/second.getValue());
}

/**
* fmod
*/
@ExecFunction(name = "fmod", argTypes = {"FLOAT", "FLOAT"}, returnType = "FLOAT")
public static Expression fmod(FloatLiteral first, FloatLiteral second) {
return new FloatLiteral(first.getValue()/second.getValue());
}

/**
* fpow
*/
@ExecFunction(name = "fpow", argTypes = {"DOUBLE", "DOUBLE"}, returnType = "DOUBLE")
public static Expression fpow(DoubleLiteral first, DoubleLiteral second) {
return new DoubleLiteral(Math.pow(first.getValue(), second.getValue()));
}

/**
* radians
*/
@ExecFunction(name = "radians", argTypes = {"DOUBLE"}, returnType = "DOUBLE")
public static Expression radians(DoubleLiteral first) {
return new DoubleLiteral(Math.toRadians(first.getValue()));
}

/**
* degrees
*/
@ExecFunction(name = "degrees", argTypes = {"DOUBLE"}, returnType = "DOUBLE")
public static Expression degrees(DoubleLiteral first) {
return new DoubleLiteral(Math.toDegrees(first.getValue()));
}

/**
* xor
*/
@ExecFunction(name = "xor", argTypes = {"BOOLEAN", "BOOLEAN"}, returnType = "BOOLEAN")
public static Expression xor(BooleanLiteral first, BooleanLiteral second) {
return BooleanLiteral.of(Boolean.logicalXor(first.getValue(), second.getValue()));
}

/**
* pi
*/
@ExecFunction(name = "pi", argTypes = {}, returnType = "DOUBLE")
public static Expression pi() {
return DoubleLiteral.of(Math.PI);
}

/**
* E
*/
@ExecFunction(name = "e", argTypes = {}, returnType = "DOUBLE")
public static Expression e() {
return DoubleLiteral.of(Math.E);
}

/**
* truncate
*/
@ExecFunction(name = "truncate", argTypes = {"DECIMALV3", "INT"}, returnType = "DECIMALV3")
public static Expression truncate(DecimalV3Literal first, IntegerLiteral second) {
if (first.getValue().compareTo(BigDecimal.ZERO) == 0) {
return first;
} else {
if (first.getValue().scale() < second.getValue()) {
return first;
}
if (second.getValue() < 0) {
return new DecimalV3Literal(BigDecimal.ZERO);
}
if (first.getValue().compareTo(BigDecimal.ZERO) == -1) {
return first.roundCeiling(second.getValue());
} else {
return first.roundFloor(second.getValue());
}
}
}

/**
* pmod
*/
@ExecFunction(name = "pmod", argTypes = {"BIGINT", "BIGINT"}, returnType = "BIGINT")
public static Expression pmod(BigIntLiteral first, BigIntLiteral second) {
return new BigIntLiteral(Math.floorMod(first.getValue(), second.getValue()));
}

/**
* pmod
*/
@ExecFunction(name = "pmod", argTypes = {"DOUBLE", "DOUBLE"}, returnType = "DOUBLE")
public static Expression pmod(DoubleLiteral first, DoubleLiteral second) {
return new DoubleLiteral(Math.floorMod(first.getValue().longValue(), second.getValue().longValue()));
}

}

0 comments on commit fa2f58a

Please sign in to comment.