Skip to content

Instantly share code, notes, and snippets.

@losipiuk
Last active August 29, 2015 14:25
Show Gist options
  • Save losipiuk/c05bdebfcfdfd5be0236 to your computer and use it in GitHub Desktop.
Save losipiuk/c05bdebfcfdfd5be0236 to your computer and use it in GitHub Desktop.
package com.facebook.presto.type;
import com.facebook.presto.operator.scalar.ScalarFunction;
import io.airlift.slice.Slice;
import java.math.BigInteger;
import static java.math.BigInteger.TEN;
import static java.util.Arrays.asList;
public class FluentDecimalOperators
{
@ScalarFunction
public static final ParametrizedScalar ADD = ParametrizedScalar.builder(FluentDecimalOperators.class)
.signature("decimal(min(38, 1+max(x,v) + max(x-y, v-w))), max(y,w) as rs", "decimal(x,y)", "decimal(v,w)")
.methods("addShortShortShort")
.extraParameters((literals, types) ->
asList(Math.pow(10, literals.get("rs") - literals.get("y")),
Math.pow(10, literals.get("rs") - literals.get("w"))))
.methods("addShortShortLong", "addLongLongLong", "addShortLongLong", "addLongShortLong")
.extraParameters((literals, types) ->
asList(TEN.pow(literals.get("rs") - literals.get("y")),
TEN.pow(literals.get("rs") - literals.get("w"))))
.build();
public static long addShortShortShort(long a, long b, long aRescale, long bRescale)
{
return a * aRescale + b * bRescale;
}
public static Slice addShortShortLong(long a, long b, BigInteger aRescale, BigInteger bRescale) {...}
public static Slice addLongLongLong(Slice a, Slice b, BigInteger aRescale, BigInteger bRescale) {...}
public static Slice addShortLongLong(long a, Slice b, BigInteger aRescale, BigInteger bRescale) {...}
public static Slice addLongShortLong(Slice a, long b, BigInteger aRescale, BigInteger bRescale) {...}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment