Skip to content

Commit

Permalink
fast decimal POC
Browse files Browse the repository at this point in the history
  • Loading branch information
sopel39 committed Feb 12, 2016
1 parent 3f85871 commit 6aa8677
Show file tree
Hide file tree
Showing 15 changed files with 1,115 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,15 @@ public static void main(String... args)

private static void runNumericBenchmarks(LocalQueryRunner localQueryRunner)
{
SimpleLineBenchmarkResultWriter writer = new SimpleLineBenchmarkResultWriter(System.out);
numericBenchmarkAdd(localQueryRunner).runBenchmark(writer);
SimpleLineBenchmarkResultWriter writer = null; //new SimpleLineBenchmarkResultWriter(System.out);
numericBenchmarkAdd(localQueryRunner).runBenchmark(writer);
numericBenchmarkManyAdd(localQueryRunner).runBenchmark(writer);
numericBenchmarkSubtract(localQueryRunner).runBenchmark(writer);
numericBenchmarkMultiply(localQueryRunner).runBenchmark(writer);
numericBenchmarkDivide(localQueryRunner).runBenchmark(writer);
numericBenchmarkSingleColumnCount(localQueryRunner).runBenchmark(writer);
numericBenchmarkTwoColumnsCount(localQueryRunner).runBenchmark(writer);
numericBenchmarkEightColumnsCount(localQueryRunner).runBenchmark(writer);
}

public static SqlBenchmark numericBenchmarkAdd(LocalQueryRunner localQueryRunner)
Expand All @@ -69,6 +70,12 @@ public static SqlBenchmark numericBenchmarkAdd(LocalQueryRunner localQueryRunner
"SELECT count(extendedprice + tax) from lineitem");
}

public static SqlBenchmark numericBenchmarkManyAdd(LocalQueryRunner localQueryRunner)
{
return createSqlBenchmark(localQueryRunner, "numericBenchmarkManyAdd_" + queryNameSuffix(localQueryRunner),
"SELECT count(extendedprice + tax + extendedprice + tax + extendedprice + tax + extendedprice + tax) from lineitem");
}

public static SqlBenchmark numericBenchmarkSubtract(LocalQueryRunner localQueryRunner)
{
return createSqlBenchmark(localQueryRunner, "numericBenchmarkSubtract_" + queryNameSuffix(localQueryRunner),
Expand Down Expand Up @@ -99,6 +106,12 @@ public static SqlBenchmark numericBenchmarkTwoColumnsCount(LocalQueryRunner loca
"SELECT count(extendedprice), count(tax) from lineitem");
}

public static SqlBenchmark numericBenchmarkEightColumnsCount(LocalQueryRunner localQueryRunner)
{
return createSqlBenchmark(localQueryRunner, "numericBenchmarkEightColumnsCount_" + queryNameSuffix(localQueryRunner),
"SELECT count(extendedprice), count(tax), count(extendedprice), count(tax), count(extendedprice), count(tax), count(extendedprice), count(tax) from lineitem");
}

private static String queryNameSuffix(LocalQueryRunner localQueryRunner)
{
return localQueryRunner.getDefaultSession().getSchema().get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import static com.facebook.presto.spi.type.StandardTypes.DATE;
import static com.facebook.presto.spi.type.StandardTypes.DECIMAL;
import static com.facebook.presto.spi.type.StandardTypes.DOUBLE;
import static com.facebook.presto.spi.type.StandardTypes.FAST_DECIMAL;
import static com.facebook.presto.spi.type.StandardTypes.INTERVAL_DAY_TO_SECOND;
import static com.facebook.presto.spi.type.StandardTypes.INTERVAL_YEAR_TO_MONTH;
import static com.facebook.presto.spi.type.StandardTypes.JSON;
Expand Down Expand Up @@ -283,6 +284,7 @@ private static Object fixValue(String type, Object value)
case INTERVAL_YEAR_TO_MONTH:
case INTERVAL_DAY_TO_SECOND:
case DECIMAL:
case FAST_DECIMAL:
return String.class.cast(value);
default:
// for now we assume that only the explicit types above are passed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
import com.facebook.presto.type.DateTimeOperators;
import com.facebook.presto.type.DecimalOperators;
import com.facebook.presto.type.DoubleOperators;
import com.facebook.presto.type.FastDecimalOperators;
import com.facebook.presto.type.HyperLogLogOperators;
import com.facebook.presto.type.IntervalDayTimeOperators;
import com.facebook.presto.type.IntervalYearMonthOperators;
Expand Down Expand Up @@ -361,6 +362,7 @@ public WindowFunctionSupplier load(SpecializedFunctionKey key)
.scalar(JsonOperators.class)
.scalar(FailureFunction.class)
.scalar(DecimalOperators.class)
.scalar(FastDecimalOperators.class)
.functions(IDENTITY_CAST, CAST_FROM_UNKNOWN)
.functions(ARRAY_CONTAINS, ARRAY_JOIN, ARRAY_JOIN_WITH_NULL_REPLACEMENT)
.functions(ARRAY_MIN, ARRAY_MAX)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import com.facebook.presto.security.DenyAllAccessControl;
import com.facebook.presto.spi.PrestoException;
import com.facebook.presto.spi.StandardErrorCode;
import com.facebook.presto.spi.type.DecimalType;
import com.facebook.presto.spi.type.FastDecimalType;
import com.facebook.presto.spi.type.Type;
import com.facebook.presto.spi.type.TypeManager;
import com.facebook.presto.spi.type.TypeSignature;
Expand Down Expand Up @@ -98,9 +98,9 @@
import static com.facebook.presto.spi.type.BooleanType.BOOLEAN;
import static com.facebook.presto.spi.type.DateType.DATE;
import static com.facebook.presto.spi.type.DoubleType.DOUBLE;
import static com.facebook.presto.spi.type.FastDecimalType.createDecimalType;
import static com.facebook.presto.spi.type.IntervalDayTimeType.INTERVAL_DAY_TIME;
import static com.facebook.presto.spi.type.IntervalYearMonthType.INTERVAL_YEAR_MONTH;
import static com.facebook.presto.spi.type.ShortDecimalType.createDecimalType;
import static com.facebook.presto.spi.type.TimeType.TIME;
import static com.facebook.presto.spi.type.TimeWithTimeZoneType.TIME_WITH_TIME_ZONE;
import static com.facebook.presto.spi.type.TimestampType.TIMESTAMP;
Expand Down Expand Up @@ -590,7 +590,7 @@ protected Type visitDoubleLiteral(DoubleLiteral node, StackableAstVisitorContext
@Override
protected Type visitDecimalLiteral(DecimalLiteral node, StackableAstVisitorContext<AnalysisContext> context)
{
DecimalType type = createDecimalType(node.getPrecision(), node.getScale());
FastDecimalType type = createDecimalType(node.getPrecision(), node.getScale());
expressionTypes.put(node, type);
return type;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.facebook.presto.operator.scalar.VarbinaryFunctions;
import com.facebook.presto.spi.ConnectorSession;
import com.facebook.presto.spi.block.Block;
import com.facebook.presto.spi.type.DecimalType;
import com.facebook.presto.spi.type.Type;
import com.facebook.presto.spi.type.VarcharType;
import com.facebook.presto.sql.analyzer.SemanticException;
Expand Down Expand Up @@ -57,6 +56,7 @@
import static com.facebook.presto.metadata.FunctionKind.SCALAR;
import static com.facebook.presto.spi.type.BigintType.BIGINT;
import static com.facebook.presto.spi.type.BooleanType.BOOLEAN;
import static com.facebook.presto.spi.type.DecimalArithmetic.decimal;
import static com.facebook.presto.spi.type.DoubleType.DOUBLE;
import static com.facebook.presto.spi.type.TypeSignature.parseTypeSignature;
import static com.facebook.presto.spi.type.VarcharType.VARCHAR;
Expand Down Expand Up @@ -218,9 +218,9 @@ protected Double visitDoubleLiteral(DoubleLiteral node, ConnectorSession session
}

@Override
protected Object visitDecimalLiteral(DecimalLiteral node, ConnectorSession context)
protected Slice visitDecimalLiteral(DecimalLiteral node, ConnectorSession context)
{
return DecimalType.unscaledValueToObject(node.getUnscaledValue(), node.getPrecision());
return decimal(node.getUnscaledValue());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import com.facebook.presto.metadata.FunctionKind;
import com.facebook.presto.metadata.FunctionRegistry;
import com.facebook.presto.metadata.Signature;
import com.facebook.presto.spi.type.DecimalType;
import com.facebook.presto.spi.type.DecimalArithmetic;
import com.facebook.presto.spi.type.TimeZoneKey;
import com.facebook.presto.spi.type.Type;
import com.facebook.presto.spi.type.TypeManager;
Expand Down Expand Up @@ -61,6 +61,7 @@
import com.facebook.presto.type.UnknownType;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import io.airlift.slice.Slice;

import java.util.IdentityHashMap;
import java.util.List;
Expand All @@ -69,7 +70,7 @@
import static com.facebook.presto.spi.type.BigintType.BIGINT;
import static com.facebook.presto.spi.type.BooleanType.BOOLEAN;
import static com.facebook.presto.spi.type.DoubleType.DOUBLE;
import static com.facebook.presto.spi.type.ShortDecimalType.createDecimalType;
import static com.facebook.presto.spi.type.FastDecimalType.createDecimalType;
import static com.facebook.presto.spi.type.TimeWithTimeZoneType.TIME_WITH_TIME_ZONE;
import static com.facebook.presto.spi.type.TimestampWithTimeZoneType.TIMESTAMP_WITH_TIME_ZONE;
import static com.facebook.presto.spi.type.TypeSignature.parseTypeSignature;
Expand Down Expand Up @@ -187,9 +188,7 @@ protected RowExpression visitDoubleLiteral(DoubleLiteral node, Void context)
@Override
protected RowExpression visitDecimalLiteral(DecimalLiteral node, Void context)
{
Object value;
value = DecimalType.unscaledValueToObject(node.getUnscaledValue(), node.getPrecision());

Slice value = DecimalArithmetic.decimal(node.getUnscaledValue());
return constant(value, createDecimalType(node.getPrecision(), node.getScale()));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.facebook.presto.type;

import com.facebook.presto.operator.scalar.ScalarOperator;
import com.facebook.presto.spi.type.DecimalArithmetic;
import io.airlift.slice.Slice;

import static com.facebook.presto.metadata.OperatorType.ADD;
import static com.facebook.presto.metadata.OperatorType.CAST;
import static com.facebook.presto.metadata.OperatorType.MULTIPLY;

public final class FastDecimalOperators
{
private FastDecimalOperators() {}

@ScalarOperator(ADD)
@LiteralParameters({"p", "s"})
@SqlType("fast_decimal(min(38, p + 1), s)")
public static Slice add(@SqlType("fast_decimal(p, s)") Slice a, @SqlType("fast_decimal(p, s)") Slice b)
{
return DecimalArithmetic.add(a, b);
}

@ScalarOperator(MULTIPLY)
@LiteralParameters({"a_precision", "a_scale", "b_precision", "b_scale"})
@SqlType("fast_decimal(min(38, a_precision + b_precision), a_scale + b_scale)")
public static Slice multiply(@SqlType("fast_decimal(a_precision, a_scale)") Slice a, @SqlType("fast_decimal(b_precision, b_scale)") Slice b)
{
return DecimalArithmetic.multiply(a, b);
}

@ScalarOperator(CAST)
@LiteralParameters({"p", "s"})
@SqlType("fast_decimal(p, s)")
public static Slice cast(@SqlType("fast_decimal(p, s)") Slice a)
{
return a;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.facebook.presto.type;

import com.facebook.presto.spi.type.StandardTypes;
import com.facebook.presto.spi.type.Type;
import com.facebook.presto.spi.type.TypeParameter;
import org.jetbrains.annotations.NotNull;

import java.util.List;

import static com.facebook.presto.spi.type.FastDecimalType.createDecimalType;
import static com.facebook.presto.spi.type.FastDecimalType.createUnparametrizedDecimal;
import static com.google.common.base.Preconditions.checkArgument;

public class FastDecimalParametricType
implements ParametricType
{
public static final FastDecimalParametricType FAST_DECIMAL = new FastDecimalParametricType();

@Override
public String getName()
{
return StandardTypes.FAST_DECIMAL;
}

@Override
public Type createType(List<TypeParameter> parameters)
{
if (parameters.size() == 0) {
// we need support for that for support of unparametrized TypeSignatures which are
// used for defining DECIMAL related functions. See e.g. DecimalOperators class.
return createUnparametrizedDecimal();
}

return createParametrizedDecimal(parameters);
}

@NotNull
private Type createParametrizedDecimal(List<TypeParameter> parameters)
{
checkArgument(parameters.size() == 2, "Expected 2 literal parameters for DECIMAL");
TypeParameter precision = parameters.get(0);
TypeParameter scale = parameters.get(1);
checkArgument(precision.isLongLiteral() &&
scale.isLongLiteral(), "Expected both literal parameters for DECIMAL to be numbers");

return createDecimalType(precision.getLongLiteral().intValue(), scale.getLongLiteral().intValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import static com.facebook.presto.spi.type.IntervalYearMonthType.INTERVAL_YEAR_MONTH;
import static com.facebook.presto.spi.type.P4HyperLogLogType.P4_HYPER_LOG_LOG;
import static com.facebook.presto.spi.type.StandardTypes.DECIMAL;
import static com.facebook.presto.spi.type.StandardTypes.FAST_DECIMAL;
import static com.facebook.presto.spi.type.TimeType.TIME;
import static com.facebook.presto.spi.type.TimeWithTimeZoneType.TIME_WITH_TIME_ZONE;
import static com.facebook.presto.spi.type.TimestampType.TIMESTAMP;
Expand Down Expand Up @@ -106,6 +107,7 @@ public TypeRegistry(Set<Type> types)
addType(JSON);
addParametricType(VarcharParametricType.VARCHAR);
addParametricType(DecimalParametricType.DECIMAL);
addParametricType(FastDecimalParametricType.FAST_DECIMAL);
addParametricType(ROW);
addParametricType(ARRAY);
addParametricType(MAP);
Expand Down Expand Up @@ -344,6 +346,19 @@ else if (secondType.getBase().equals(StandardTypes.VARCHAR) && firstType.getBase
ImmutableList.of(TypeSignatureParameter.of(targetPrecision), TypeSignatureParameter.of(targetScale))));
}

if (firstType.getBase().equals(FAST_DECIMAL) && secondType.getBase().equals(FAST_DECIMAL)) {
long firstPrecision = firstType.getParameters().get(0).getLongLiteral();
long secondPrecision = secondType.getParameters().get(0).getLongLiteral();
long firstScale = firstType.getParameters().get(1).getLongLiteral();
long secondScale = secondType.getParameters().get(1).getLongLiteral();
long targetScale = Math.max(firstScale, secondScale);
long targetPrecision = Math.max(firstPrecision - firstScale, secondPrecision - secondScale) + targetScale;
targetPrecision = Math.min(38, targetPrecision); //we allow potential loss of precision here. Overflow checking is done in operators.
return Optional.of(new TypeSignature(
FAST_DECIMAL,
ImmutableList.of(TypeSignatureParameter.of(targetPrecision), TypeSignatureParameter.of(targetScale))));
}

if (firstTypeTypeParameters.size() != secondTypeTypeParameters.size()) {
return Optional.empty();
}
Expand Down Expand Up @@ -444,6 +459,19 @@ else if (actualType.getBase().equals(StandardTypes.VARCHAR) && expectedType.getB
return actualScale == expectedScale && actualPrecision <= expectedPrecision;
}

if (actualType.getBase().equals(FAST_DECIMAL) && expectedType.getBase().equals(FAST_DECIMAL)) {
long actualPrecision = actualType.getParameters().get(0).getLongLiteral();
long expectedPrecision = (long) expectedType.getParameters().get(0).getLongLiteral();
long actualScale = (long) actualType.getParameters().get(1).getLongLiteral();
long expectedScale = (long) expectedType.getParameters().get(1).getLongLiteral();

if (actualPrecision <= DecimalType.MAX_SHORT_PRECISION ^ expectedPrecision <= DecimalType.MAX_SHORT_PRECISION) {
return false;
}

return actualScale == expectedScale && actualPrecision <= expectedPrecision;
}

return false;
}

Expand Down
Loading

0 comments on commit 6aa8677

Please sign in to comment.