Skip to content

Commit

Permalink
Add the multiplyByTwo function example
Browse files Browse the repository at this point in the history
  • Loading branch information
stheppi committed Sep 22, 2021
1 parent 04e2634 commit 93b4d92
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/main/java/io/lenses/sql/udf/multiplyByTwo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.lenses.sql.udf;

import io.lenses.sql.udf.datatype.DataType;
import io.lenses.sql.udf.value.*;

import java.math.BigDecimal;
import java.math.BigInteger;


public class multiplyByTwo implements UserDefinedFunction1 {
@Override
public DataType typeMapping(DataType dataType) throws UdfException {
if (dataType.isNumber())
return dataType;
else
throw new UdfException("Invalid input type received. Expecting a Integer but received:" + dataType.name);
}

@Override
public Value evaluate(Value value) throws UdfException {
if (value instanceof BigDecimalValue) {
return new BigDecimalValue(value.toBigDecimalValue().get().multiply(new BigDecimal(2)));
} else if (value instanceof BigIntValue) {
return new BigIntValue(value.toBigIntValue().get().multiply(BigInteger.valueOf(2)));
} else if (value instanceof DoubleValue) {
return new DoubleValue(value.toDoubleValue().get() * 2);
} else if (value instanceof FloatValue) {
return new FloatValue(value.toFloatValue().get() * 2);
} else if (value instanceof IntValue) {
return new IntValue(value.toIntValue().get() * 2);
} else if (value instanceof LongValue) {
return new LongValue(value.toLongValue().get() * 2);
}
throw new UdfRuntimeException("Expecting a number but found " + value.getDataType().name + ".");
}

@Override
public String name() {
return "multiplyByTwo";
}
}

0 comments on commit 93b4d92

Please sign in to comment.