Search in sources :

Example 46 with DecimalType

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));
}
Also used : DecimalType(com.facebook.presto.common.type.DecimalType) CharType(com.facebook.presto.common.type.CharType) Type(com.facebook.presto.common.type.Type) VarcharType(com.facebook.presto.common.type.VarcharType) VarcharType(com.facebook.presto.common.type.VarcharType) TypeSignatureParameter(com.facebook.presto.common.type.TypeSignatureParameter) ArrayList(java.util.ArrayList) DecimalType(com.facebook.presto.common.type.DecimalType) CharType(com.facebook.presto.common.type.CharType) NotSupportedException(com.facebook.presto.common.NotSupportedException)

Example 47 with DecimalType

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);
}
Also used : StandardTypes(com.facebook.presto.common.type.StandardTypes) DecimalType(com.facebook.presto.common.type.DecimalType) Slice(io.airlift.slice.Slice) Chars.isCharType(com.facebook.presto.common.type.Chars.isCharType) Decimals.encodeUnscaledValue(com.facebook.presto.common.type.Decimals.encodeUnscaledValue) TINYINT(com.facebook.presto.common.type.TinyintType.TINYINT) Function(java.util.function.Function) REAL(com.facebook.presto.common.type.RealType.REAL) Float.floatToRawIntBits(java.lang.Float.floatToRawIntBits) Chars.truncateToLengthAndTrimSpaces(com.facebook.presto.common.type.Chars.truncateToLengthAndTrimSpaces) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ImmutableList(com.google.common.collect.ImmutableList) HiveBloomFilter(com.facebook.presto.orc.metadata.statistics.HiveBloomFilter) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) BOOLEAN(com.facebook.presto.common.type.BooleanType.BOOLEAN) Varchars.isVarcharType(com.facebook.presto.common.type.Varchars.isVarcharType) Type(com.facebook.presto.common.type.Type) BIGINT(com.facebook.presto.common.type.BigintType.BIGINT) DOUBLE(com.facebook.presto.common.type.DoubleType.DOUBLE) Collection(java.util.Collection) VarcharType(com.facebook.presto.common.type.VarcharType) Domain(com.facebook.presto.common.predicate.Domain) TupleDomain(com.facebook.presto.common.predicate.TupleDomain) Decimals.isLongDecimal(com.facebook.presto.common.type.Decimals.isLongDecimal) Range(com.facebook.presto.common.predicate.Range) ColumnStatistics(com.facebook.presto.orc.metadata.statistics.ColumnStatistics) List(java.util.List) Decimals.isShortDecimal(com.facebook.presto.common.type.Decimals.isShortDecimal) BooleanStatistics(com.facebook.presto.orc.metadata.statistics.BooleanStatistics) SMALLINT(com.facebook.presto.common.type.SmallintType.SMALLINT) Decimals.rescale(com.facebook.presto.common.type.Decimals.rescale) INTEGER(com.facebook.presto.common.type.IntegerType.INTEGER) VarbinaryType(com.facebook.presto.common.type.VarbinaryType) Optional(java.util.Optional) BloomFilter(com.facebook.presto.orc.metadata.statistics.BloomFilter) VisibleForTesting(com.google.common.annotations.VisibleForTesting) ValueSet(com.facebook.presto.common.predicate.ValueSet) RangeStatistics(com.facebook.presto.orc.metadata.statistics.RangeStatistics) MoreObjects.toStringHelper(com.google.common.base.MoreObjects.toStringHelper) BooleanStatistics(com.facebook.presto.orc.metadata.statistics.BooleanStatistics) DecimalType(com.facebook.presto.common.type.DecimalType) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 48 with DecimalType

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());
}
Also used : CharType.createCharType(com.facebook.presto.common.type.CharType.createCharType) VarcharType.createUnboundedVarcharType(com.facebook.presto.common.type.VarcharType.createUnboundedVarcharType) Varchars.isVarcharType(com.facebook.presto.common.type.Varchars.isVarcharType) DecimalType(com.facebook.presto.common.type.DecimalType) Chars.isCharType(com.facebook.presto.common.type.Chars.isCharType) HiveUtil.isStructuralType(com.facebook.presto.hive.HiveUtil.isStructuralType) ArrayType(com.facebook.presto.common.type.ArrayType) CharType(com.facebook.presto.common.type.CharType) RowType(com.facebook.presto.common.type.RowType) TimestampType(com.facebook.presto.common.type.TimestampType) VarcharType.createVarcharType(com.facebook.presto.common.type.VarcharType.createVarcharType) HiveTestUtils.mapType(com.facebook.presto.hive.HiveTestUtils.mapType) Type(com.facebook.presto.common.type.Type) DateType(com.facebook.presto.common.type.DateType) DecimalType(com.facebook.presto.common.type.DecimalType) Block(com.facebook.presto.common.block.Block) SerDeUtils.serializeObject(com.facebook.presto.hive.util.SerDeUtils.serializeObject) BigDecimal(java.math.BigDecimal)

Example 49 with DecimalType

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);
}
Also used : DecimalType(com.facebook.presto.common.type.DecimalType) VarcharType(com.facebook.presto.common.type.VarcharType) Type(com.facebook.presto.common.type.Type) VarcharType(com.facebook.presto.common.type.VarcharType) Block(com.facebook.presto.common.block.Block) DecimalType(com.facebook.presto.common.type.DecimalType) Page(com.facebook.presto.common.Page)

Example 50 with DecimalType

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);
}
Also used : BlockAssertions.createLongsBlock(com.facebook.presto.block.BlockAssertions.createLongsBlock) BlockAssertions.createShortDecimalsBlock(com.facebook.presto.block.BlockAssertions.createShortDecimalsBlock) BlockAssertions.createLongDecimalsBlock(com.facebook.presto.block.BlockAssertions.createLongDecimalsBlock) BlockAssertions.createStringsBlock(com.facebook.presto.block.BlockAssertions.createStringsBlock) BlockAssertions.createArrayBigintBlock(com.facebook.presto.block.BlockAssertions.createArrayBigintBlock) BlockAssertions.createBooleansBlock(com.facebook.presto.block.BlockAssertions.createBooleansBlock) BlockAssertions.createDoublesBlock(com.facebook.presto.block.BlockAssertions.createDoublesBlock) Block(com.facebook.presto.common.block.Block) DecimalType(com.facebook.presto.common.type.DecimalType) DecimalType.createDecimalType(com.facebook.presto.common.type.DecimalType.createDecimalType) Test(org.testng.annotations.Test)

Aggregations

DecimalType (com.facebook.presto.common.type.DecimalType)55 VarcharType (com.facebook.presto.common.type.VarcharType)28 Type (com.facebook.presto.common.type.Type)23 CharType (com.facebook.presto.common.type.CharType)20 Block (com.facebook.presto.common.block.Block)15 ArrayType (com.facebook.presto.common.type.ArrayType)15 RowType (com.facebook.presto.common.type.RowType)14 Slice (io.airlift.slice.Slice)14 ArrayList (java.util.ArrayList)13 PrestoException (com.facebook.presto.spi.PrestoException)12 TimestampType (com.facebook.presto.common.type.TimestampType)11 BigDecimal (java.math.BigDecimal)11 MapType (com.facebook.presto.common.type.MapType)10 Chars.isCharType (com.facebook.presto.common.type.Chars.isCharType)9 DateType (com.facebook.presto.common.type.DateType)9 IntegerType (com.facebook.presto.common.type.IntegerType)9 VarbinaryType (com.facebook.presto.common.type.VarbinaryType)9 ImmutableList (com.google.common.collect.ImmutableList)9 BigintType (com.facebook.presto.common.type.BigintType)8 BooleanType (com.facebook.presto.common.type.BooleanType)8