-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the multiplyByTwo function example
- Loading branch information
Showing
1 changed file
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} |