Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add decimal example #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions src/main/java/com/canner/udf/UdfPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.canner.udf.scalar.EncryptDecryptWithKey;
import com.canner.udf.scalar.ExtendedHashFunction;
import com.canner.udf.scalar.MathOperation;
import com.canner.udf.scalar.MathOperation.MathOP;
import com.canner.udf.scalar.TimestampSeqNo;
import com.canner.udf.scalar.TimestampWithTZSeqNo;
import io.trino.spi.Plugin;
Expand Down Expand Up @@ -47,6 +48,7 @@ public Set<Class<?>> getFunctions()
functions.add(TimestampWithTZSeqNo.class);
// load udfs in MathOperation class
functions.add(MathOperation.class);
functions.add(MathOP.class);

return Collections.unmodifiableSet(functions);
}
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/com/canner/udf/scalar/MathOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@
package com.canner.udf.scalar;

import io.trino.spi.function.Description;
import io.trino.spi.function.LiteralParameters;
import io.trino.spi.function.ScalarFunction;
import io.trino.spi.function.SqlType;
import io.trino.spi.type.Int128;
import io.trino.spi.type.StandardTypes;

import static io.trino.spi.type.Int128Math.add;

public class MathOperation
{
private MathOperation() {}
Expand All @@ -31,4 +35,32 @@ public static long mathOpAddBigInt(@SqlType(StandardTypes.BIGINT) long left, @Sq
{
return left + right;
}

@ScalarFunction("math_op")
@Description("math op")
public static final class MathOP
{
private MathOP() {}

// this is needed, otherwise mathOp(Int128, Int128) won't work
@LiteralParameters({"p", "s"})
@SqlType("decimal(p, s)")
public static long mathOp(@SqlType("decimal(p, s)") long left, @SqlType("decimal(p, s)") long right)
{
return left + right;
}

@LiteralParameters({"p", "s"})
@SqlType("decimal(p, s)")
public static Int128 mathOp(@SqlType("decimal(p, s)") Int128 left, @SqlType("decimal(p, s)") Int128 right)
{
long[] resultArray = new long[2];
add(
left.getHigh(), left.getLow(),
right.getHigh(), right.getLow(),
resultArray,
0);
return Int128.valueOf(resultArray);
}
}
}