use of com.facebook.presto.common.type.DecimalType in project presto by prestodb.
the class OrcType method toOrcType.
private static List<OrcType> toOrcType(int nextFieldTypeIndex, Type type) {
if (BOOLEAN.equals(type)) {
return ImmutableList.of(new OrcType(OrcTypeKind.BOOLEAN));
}
if (TINYINT.equals(type)) {
return ImmutableList.of(new OrcType(OrcTypeKind.BYTE));
}
if (SMALLINT.equals(type)) {
return ImmutableList.of(new OrcType(OrcTypeKind.SHORT));
}
if (INTEGER.equals(type)) {
return ImmutableList.of(new OrcType(OrcTypeKind.INT));
}
if (BIGINT.equals(type)) {
return ImmutableList.of(new OrcType(OrcTypeKind.LONG));
}
if (DOUBLE.equals(type)) {
return ImmutableList.of(new OrcType(OrcTypeKind.DOUBLE));
}
if (REAL.equals(type)) {
return ImmutableList.of(new OrcType(OrcTypeKind.FLOAT));
}
if (type instanceof VarcharType) {
VarcharType varcharType = (VarcharType) type;
if (varcharType.isUnbounded()) {
return ImmutableList.of(new OrcType(OrcTypeKind.STRING));
}
return ImmutableList.of(new OrcType(OrcTypeKind.VARCHAR, varcharType.getLengthSafe()));
}
if (type instanceof CharType) {
return ImmutableList.of(new OrcType(OrcTypeKind.CHAR, ((CharType) type).getLength()));
}
if (VARBINARY.equals(type)) {
return ImmutableList.of(new OrcType(OrcTypeKind.BINARY));
}
if (DATE.equals(type)) {
return ImmutableList.of(new OrcType(OrcTypeKind.DATE));
}
if (TIMESTAMP.equals(type)) {
return ImmutableList.of(new OrcType(OrcTypeKind.TIMESTAMP));
}
if (type instanceof DecimalType) {
DecimalType decimalType = (DecimalType) type;
return ImmutableList.of(new OrcType(OrcTypeKind.DECIMAL, decimalType.getPrecision(), decimalType.getScale()));
}
if (type.getTypeSignature().getBase().equals(ARRAY)) {
return createOrcArrayType(nextFieldTypeIndex, type.getTypeParameters().get(0));
}
if (type.getTypeSignature().getBase().equals(MAP)) {
return createOrcMapType(nextFieldTypeIndex, type.getTypeParameters().get(0), type.getTypeParameters().get(1));
}
if (type.getTypeSignature().getBase().equals(ROW)) {
List<String> fieldNames = new ArrayList<>();
for (int i = 0; i < type.getTypeSignature().getParameters().size(); i++) {
TypeSignatureParameter parameter = type.getTypeSignature().getParameters().get(i);
fieldNames.add(parameter.getNamedTypeSignature().getName().orElse("field" + i));
}
List<Type> fieldTypes = type.getTypeParameters();
return createOrcRowType(nextFieldTypeIndex, fieldNames, fieldTypes);
}
throw new NotSupportedException(format("Unsupported Hive type: %s", type));
}
use of com.facebook.presto.common.type.DecimalType in project presto by prestodb.
the class TupleDomainOrcPredicate method getDomain.
@VisibleForTesting
public static Domain getDomain(Type type, long rowCount, ColumnStatistics columnStatistics) {
if (rowCount == 0) {
return Domain.none(type);
}
if (columnStatistics == null) {
return Domain.all(type);
}
if (columnStatistics.hasNumberOfValues() && columnStatistics.getNumberOfValues() == 0) {
return Domain.onlyNull(type);
}
boolean hasNullValue = columnStatistics.getNumberOfValues() != rowCount;
if (type.getJavaType() == boolean.class && columnStatistics.getBooleanStatistics() != null) {
BooleanStatistics booleanStatistics = columnStatistics.getBooleanStatistics();
boolean hasTrueValues = (booleanStatistics.getTrueValueCount() != 0);
boolean hasFalseValues = (columnStatistics.getNumberOfValues() != booleanStatistics.getTrueValueCount());
if (hasTrueValues && hasFalseValues) {
return Domain.all(BOOLEAN);
}
if (hasTrueValues) {
return Domain.create(ValueSet.of(BOOLEAN, true), hasNullValue);
}
if (hasFalseValues) {
return Domain.create(ValueSet.of(BOOLEAN, false), hasNullValue);
}
} else if (isShortDecimal(type) && columnStatistics.getDecimalStatistics() != null) {
return createDomain(type, hasNullValue, columnStatistics.getDecimalStatistics(), value -> rescale(value, (DecimalType) type).unscaledValue().longValue());
} else if (isLongDecimal(type) && columnStatistics.getDecimalStatistics() != null) {
return createDomain(type, hasNullValue, columnStatistics.getDecimalStatistics(), value -> encodeUnscaledValue(rescale(value, (DecimalType) type).unscaledValue()));
} else if (isCharType(type) && columnStatistics.getStringStatistics() != null) {
return createDomain(type, hasNullValue, columnStatistics.getStringStatistics(), value -> truncateToLengthAndTrimSpaces(value, type));
} else if (isVarcharType(type) && columnStatistics.getStringStatistics() != null) {
return createDomain(type, hasNullValue, columnStatistics.getStringStatistics());
} else if (type.getTypeSignature().getBase().equals(StandardTypes.DATE) && columnStatistics.getDateStatistics() != null) {
return createDomain(type, hasNullValue, columnStatistics.getDateStatistics(), value -> (long) value);
} else if (type.getJavaType() == long.class && columnStatistics.getIntegerStatistics() != null) {
return createDomain(type, hasNullValue, columnStatistics.getIntegerStatistics());
} else if (type.getJavaType() == double.class && columnStatistics.getDoubleStatistics() != null) {
return createDomain(type, hasNullValue, columnStatistics.getDoubleStatistics());
} else if (REAL.equals(type) && columnStatistics.getDoubleStatistics() != null) {
return createDomain(type, hasNullValue, columnStatistics.getDoubleStatistics(), value -> (long) floatToRawIntBits(value.floatValue()));
}
return Domain.create(ValueSet.all(type), hasNullValue);
}
use of com.facebook.presto.common.type.DecimalType in project presto by prestodb.
the class AbstractTestHiveFileFormats method checkCursor.
protected void checkCursor(RecordCursor cursor, List<TestColumn> testColumns, int rowCount) {
for (int row = 0; row < rowCount; row++) {
assertTrue(cursor.advanceNextPosition());
for (int i = 0, testColumnsSize = testColumns.size(); i < testColumnsSize; i++) {
TestColumn testColumn = testColumns.get(i);
Type type = HiveType.valueOf(testColumn.getObjectInspector().getTypeName()).getType(FUNCTION_AND_TYPE_MANAGER);
Object fieldFromCursor = getFieldFromCursor(cursor, type, i);
if (fieldFromCursor == null) {
assertEquals(null, testColumn.getExpectedValue(), String.format("Expected null for column %s", testColumn.getName()));
} else if (type instanceof DecimalType) {
DecimalType decimalType = (DecimalType) type;
fieldFromCursor = new BigDecimal((BigInteger) fieldFromCursor, decimalType.getScale());
assertEquals(fieldFromCursor, testColumn.getExpectedValue(), String.format("Wrong value for column %s", testColumn.getName()));
} else if (testColumn.getObjectInspector().getTypeName().equals("float")) {
assertEquals((float) fieldFromCursor, (float) testColumn.getExpectedValue(), (float) EPSILON);
} else if (testColumn.getObjectInspector().getTypeName().equals("double")) {
assertEquals((double) fieldFromCursor, (double) testColumn.getExpectedValue(), EPSILON);
} else if (testColumn.getObjectInspector().getTypeName().equals("tinyint")) {
assertEquals(((Number) fieldFromCursor).byteValue(), testColumn.getExpectedValue());
} else if (testColumn.getObjectInspector().getTypeName().equals("smallint")) {
assertEquals(((Number) fieldFromCursor).shortValue(), testColumn.getExpectedValue());
} else if (testColumn.getObjectInspector().getTypeName().equals("int")) {
assertEquals(((Number) fieldFromCursor).intValue(), testColumn.getExpectedValue());
} else if (testColumn.getObjectInspector().getCategory() == Category.PRIMITIVE) {
assertEquals(fieldFromCursor, testColumn.getExpectedValue(), String.format("Wrong value for column %s", testColumn.getName()));
} else {
Block expected = (Block) testColumn.getExpectedValue();
Block actual = (Block) fieldFromCursor;
assertBlockEquals(actual, expected, String.format("Wrong value for column %s", testColumn.getName()));
}
}
}
assertFalse(cursor.advanceNextPosition());
}
use of com.facebook.presto.common.type.DecimalType in project presto by prestodb.
the class SequencePageBuilder method createSequencePage.
public static Page createSequencePage(List<? extends Type> types, int length, int... initialValues) {
Block[] blocks = new Block[initialValues.length];
for (int i = 0; i < blocks.length; i++) {
Type type = types.get(i);
int initialValue = initialValues[i];
if (type.equals(BIGINT)) {
blocks[i] = BlockAssertions.createLongSequenceBlock(initialValue, initialValue + length);
} else if (type.equals(REAL)) {
blocks[i] = BlockAssertions.createSequenceBlockOfReal(initialValue, initialValue + length);
} else if (type.equals(DOUBLE)) {
blocks[i] = BlockAssertions.createDoubleSequenceBlock(initialValue, initialValue + length);
} else if (type instanceof VarcharType) {
blocks[i] = BlockAssertions.createStringSequenceBlock(initialValue, initialValue + length);
} else if (type.equals(BOOLEAN)) {
blocks[i] = BlockAssertions.createBooleanSequenceBlock(initialValue, initialValue + length);
} else if (type.equals(DATE)) {
blocks[i] = BlockAssertions.createDateSequenceBlock(initialValue, initialValue + length);
} else if (type.equals(TIMESTAMP)) {
blocks[i] = BlockAssertions.createTimestampSequenceBlock(initialValue, initialValue + length);
} else if (isShortDecimal(type)) {
blocks[i] = BlockAssertions.createShortDecimalSequenceBlock(initialValue, initialValue + length, (DecimalType) type);
} else if (isLongDecimal(type)) {
blocks[i] = BlockAssertions.createLongDecimalSequenceBlock(initialValue, initialValue + length, (DecimalType) type);
} else {
throw new IllegalStateException("Unsupported type " + type);
}
}
return new Page(blocks);
}
use of com.facebook.presto.common.type.DecimalType in project presto by prestodb.
the class TestChecksumAggregation method testShortDecimal.
@Test
public void testShortDecimal() {
InternalAggregationFunction decimalAgg = getAggregation(createDecimalType(10, 2));
Block block = createShortDecimalsBlock("11.11", "22.22", null, "33.33", "44.44");
DecimalType shortDecimalType = createDecimalType(1);
assertAggregation(decimalAgg, expectedChecksum(shortDecimalType, block), block);
}
Aggregations