Search in sources :

Example 21 with DecimalType

use of com.facebook.presto.common.type.DecimalType in project presto by prestodb.

the class TestMathFunctions method testSign.

@Test
public void testSign() {
    DecimalType expectedDecimalReturnType = createDecimalType(1, 0);
    // retains type for NULL values
    assertFunction("sign(CAST(NULL as TINYINT))", TINYINT, null);
    assertFunction("sign(CAST(NULL as SMALLINT))", SMALLINT, null);
    assertFunction("sign(CAST(NULL as INTEGER))", INTEGER, null);
    assertFunction("sign(CAST(NULL as BIGINT))", BIGINT, null);
    assertFunction("sign(CAST(NULL as DOUBLE))", DOUBLE, null);
    assertFunction("sign(CAST(NULL as DECIMAL(2,1)))", expectedDecimalReturnType, null);
    assertFunction("sign(CAST(NULL as DECIMAL(38,0)))", expectedDecimalReturnType, null);
    // tinyint
    for (int intValue : intLefts) {
        Float signum = Math.signum(intValue);
        assertFunction("sign(TINYINT '" + intValue + "')", TINYINT, signum.byteValue());
    }
    // smallint
    for (int intValue : intLefts) {
        Float signum = Math.signum(intValue);
        assertFunction("sign(SMALLINT '" + intValue + "')", SMALLINT, signum.shortValue());
    }
    // integer
    for (int intValue : intLefts) {
        Float signum = Math.signum(intValue);
        assertFunction("sign(INTEGER '" + intValue + "')", INTEGER, signum.intValue());
    }
    // bigint
    for (int intValue : intLefts) {
        Float signum = Math.signum(intValue);
        assertFunction("sign(BIGINT '" + intValue + "')", BIGINT, signum.longValue());
    }
    // double and float
    for (double doubleValue : DOUBLE_VALUES) {
        assertFunction("sign(DOUBLE '" + doubleValue + "')", DOUBLE, Math.signum(doubleValue));
        assertFunction("sign(REAL '" + (float) doubleValue + "')", REAL, Math.signum(((float) doubleValue)));
    }
    // returns NaN for NaN input
    assertFunction("sign(DOUBLE 'NaN')", DOUBLE, Double.NaN);
    // returns proper sign for +/-Infinity input
    assertFunction("sign(DOUBLE '+Infinity')", DOUBLE, 1.0);
    assertFunction("sign(DOUBLE '-Infinity')", DOUBLE, -1.0);
    // short decimal
    assertFunction("sign(DECIMAL '0')", expectedDecimalReturnType, SqlDecimal.of("0"));
    assertFunction("sign(DECIMAL '123')", expectedDecimalReturnType, SqlDecimal.of("1"));
    assertFunction("sign(DECIMAL '-123')", expectedDecimalReturnType, SqlDecimal.of("-1"));
    assertFunction("sign(DECIMAL '123.000000000000000')", expectedDecimalReturnType, SqlDecimal.of("1"));
    assertFunction("sign(DECIMAL '-123.000000000000000')", expectedDecimalReturnType, SqlDecimal.of("-1"));
    // long decimal
    assertFunction("sign(DECIMAL '0.000000000000000000')", expectedDecimalReturnType, SqlDecimal.of("0"));
    assertFunction("sign(DECIMAL '1230.000000000000000')", expectedDecimalReturnType, SqlDecimal.of("1"));
    assertFunction("sign(DECIMAL '-1230.000000000000000')", expectedDecimalReturnType, SqlDecimal.of("-1"));
}
Also used : DecimalType(com.facebook.presto.common.type.DecimalType) DecimalType.createDecimalType(com.facebook.presto.common.type.DecimalType.createDecimalType) Test(org.testng.annotations.Test)

Example 22 with DecimalType

use of com.facebook.presto.common.type.DecimalType in project presto by prestodb.

the class BlockAssertions method createRandomBlockForType.

// Note: Make sure positionCount is sufficiently large if nestedNullRate or primitiveNullRate is greater than 0
public static Block createRandomBlockForType(Type type, int positionCount, float primitiveNullRate, float nestedNullRate, boolean createView, List<Encoding> wrappings) {
    verifyNullRate(primitiveNullRate);
    verifyNullRate(nestedNullRate);
    Block block = null;
    if (createView) {
        positionCount *= 2;
    }
    if (type == BOOLEAN) {
        block = createRandomBooleansBlock(positionCount, primitiveNullRate);
    } else if (type == BIGINT) {
        block = createRandomLongsBlock(positionCount, primitiveNullRate);
    } else if (type == INTEGER || type == REAL) {
        block = createRandomIntsBlock(positionCount, primitiveNullRate);
    } else if (type == SMALLINT) {
        block = createRandomSmallintsBlock(positionCount, primitiveNullRate);
    } else if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        if (decimalType.isShort()) {
            block = createRandomLongsBlock(positionCount, primitiveNullRate);
        } else {
            block = createRandomLongDecimalsBlock(positionCount, primitiveNullRate);
        }
    } else if (type == VARCHAR) {
        block = createRandomStringBlock(positionCount, primitiveNullRate, MAX_STRING_SIZE);
    } else {
        // Nested types
        // Build isNull and offsets of size positionCount
        boolean[] isNull = null;
        if (nestedNullRate > 0) {
            isNull = new boolean[positionCount];
        }
        int[] offsets = new int[positionCount + 1];
        for (int position = 0; position < positionCount; position++) {
            if (nestedNullRate > 0 && ThreadLocalRandom.current().nextDouble(1) < nestedNullRate) {
                isNull[position] = true;
                offsets[position + 1] = offsets[position];
            } else {
                offsets[position + 1] = offsets[position] + (type instanceof RowType ? 1 : ThreadLocalRandom.current().nextInt(ENTRY_SIZE) + 1);
            }
        }
        // Build the nested block of size offsets[positionCount].
        if (type instanceof ArrayType) {
            Block valuesBlock = createRandomBlockForType(((ArrayType) type).getElementType(), offsets[positionCount], primitiveNullRate, nestedNullRate, createView, wrappings);
            block = fromElementBlock(positionCount, Optional.ofNullable(isNull), offsets, valuesBlock);
        } else if (type instanceof MapType) {
            MapType mapType = (MapType) type;
            Block keyBlock = createRandomBlockForType(mapType.getKeyType(), offsets[positionCount], 0.0f, 0.0f, createView, wrappings);
            Block valueBlock = createRandomBlockForType(mapType.getValueType(), offsets[positionCount], primitiveNullRate, nestedNullRate, createView, wrappings);
            block = mapType.createBlockFromKeyValue(positionCount, Optional.ofNullable(isNull), offsets, keyBlock, valueBlock);
        } else if (type instanceof RowType) {
            List<Type> fieldTypes = type.getTypeParameters();
            Block[] fieldBlocks = new Block[fieldTypes.size()];
            for (int i = 0; i < fieldBlocks.length; i++) {
                fieldBlocks[i] = createRandomBlockForType(fieldTypes.get(i), positionCount, primitiveNullRate, nestedNullRate, createView, wrappings);
            }
            block = fromFieldBlocks(positionCount, Optional.ofNullable(isNull), fieldBlocks);
        } else {
            throw new IllegalArgumentException(format("type %s is not supported.", type));
        }
    }
    if (createView) {
        positionCount /= 2;
        int offset = positionCount / 2;
        block = block.getRegion(offset, positionCount);
    }
    if (!wrappings.isEmpty()) {
        block = wrapBlock(block, positionCount, wrappings);
    }
    return block;
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) RunLengthEncodedBlock(com.facebook.presto.common.block.RunLengthEncodedBlock) ArrayBlock.fromElementBlock(com.facebook.presto.common.block.ArrayBlock.fromElementBlock) DictionaryBlock(com.facebook.presto.common.block.DictionaryBlock) Block(com.facebook.presto.common.block.Block) DecimalType(com.facebook.presto.common.type.DecimalType) RowType(com.facebook.presto.common.type.RowType) List(java.util.List) ArrayList(java.util.ArrayList) MapType(com.facebook.presto.common.type.MapType)

Example 23 with DecimalType

use of com.facebook.presto.common.type.DecimalType in project presto by prestodb.

the class BlockAssertions method createShortDecimalsBlock.

public static Block createShortDecimalsBlock(Iterable<String> values) {
    DecimalType shortDecimalType = DecimalType.createDecimalType(1);
    BlockBuilder builder = shortDecimalType.createBlockBuilder(null, 100);
    for (String value : values) {
        if (value == null) {
            builder.appendNull();
        } else {
            shortDecimalType.writeLong(builder, new BigDecimal(value).unscaledValue().longValue());
        }
    }
    return builder.build();
}
Also used : DecimalType(com.facebook.presto.common.type.DecimalType) BigDecimal(java.math.BigDecimal) Decimals.writeBigDecimal(com.facebook.presto.common.type.Decimals.writeBigDecimal) RowBlockBuilder(com.facebook.presto.common.block.RowBlockBuilder) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) StructuralTestUtil.appendToBlockBuilder(com.facebook.presto.util.StructuralTestUtil.appendToBlockBuilder)

Example 24 with DecimalType

use of com.facebook.presto.common.type.DecimalType in project presto by prestodb.

the class GenericHiveRecordCursor method parseDecimalColumn.

private void parseDecimalColumn(int column) {
    loaded[column] = true;
    Object fieldData = rowInspector.getStructFieldData(rowData, structFields[column]);
    if (fieldData == null) {
        nulls[column] = true;
    } else {
        Object fieldValue = ((PrimitiveObjectInspector) fieldInspectors[column]).getPrimitiveJavaObject(fieldData);
        checkState(fieldValue != null, "fieldValue should not be null");
        HiveDecimal decimal = (HiveDecimal) fieldValue;
        DecimalType columnType = (DecimalType) types[column];
        BigInteger unscaledDecimal = rescale(decimal.unscaledValue(), decimal.scale(), columnType.getScale());
        if (columnType.isShort()) {
            longs[column] = unscaledDecimal.longValue();
        } else {
            slices[column] = Decimals.encodeUnscaledValue(unscaledDecimal);
        }
        nulls[column] = false;
    }
}
Also used : HiveDecimal(org.apache.hadoop.hive.common.type.HiveDecimal) DecimalType(com.facebook.presto.common.type.DecimalType) BigInteger(java.math.BigInteger) SerDeUtils.getBlockObject(com.facebook.presto.hive.util.SerDeUtils.getBlockObject) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector)

Example 25 with DecimalType

use of com.facebook.presto.common.type.DecimalType in project presto by prestodb.

the class TestBlockEncodingBuffers method buildBlockStatusWithType.

private BlockStatus buildBlockStatusWithType(Type type, int positionCount, boolean isView, float primitiveNullRate, float nestedNullRate, List<Encoding> wrappings) {
    BlockStatus blockStatus = null;
    if (isView) {
        positionCount *= 2;
    }
    if (type == BIGINT) {
        blockStatus = buildBigintBlockStatus(positionCount, primitiveNullRate);
    } else if (type instanceof DecimalType) {
        if (!((DecimalType) type).isShort()) {
            blockStatus = buildLongDecimalBlockStatus(positionCount, primitiveNullRate);
        } else {
            blockStatus = buildShortDecimalBlockStatus(positionCount, primitiveNullRate);
        }
    } else if (type == INTEGER) {
        blockStatus = buildIntegerBlockStatus(positionCount, primitiveNullRate);
    } else if (type == SMALLINT) {
        blockStatus = buildSmallintBlockStatus(positionCount, primitiveNullRate);
    } else if (type == BOOLEAN) {
        blockStatus = buildBooleanBlockStatus(positionCount, primitiveNullRate);
    } else if (type == VARCHAR) {
        blockStatus = buildVarcharBlockStatus(positionCount, primitiveNullRate, 10);
    } else {
        // Nested types
        // Build isNull and offsets of size positionCount
        boolean[] isNull = null;
        if (nestedNullRate > 0) {
            isNull = new boolean[positionCount];
        }
        int[] offsets = new int[positionCount + 1];
        for (int position = 0; position < positionCount; position++) {
            if (nestedNullRate > 0 && ThreadLocalRandom.current().nextDouble(1) < nestedNullRate) {
                isNull[position] = true;
                offsets[position + 1] = offsets[position];
            } else {
                offsets[position + 1] = offsets[position] + (type instanceof RowType ? 1 : ThreadLocalRandom.current().nextInt(10) + 1);
            }
        }
        // Build the nested block of size offsets[positionCount].
        if (type instanceof ArrayType) {
            blockStatus = buildArrayBlockStatus((ArrayType) type, positionCount, isView, Optional.ofNullable(isNull), offsets, primitiveNullRate, nestedNullRate, wrappings);
        } else if (type instanceof MapType) {
            blockStatus = buildMapBlockStatus((MapType) type, positionCount, isView, Optional.ofNullable(isNull), offsets, primitiveNullRate, nestedNullRate, wrappings);
        } else if (type instanceof RowType) {
            blockStatus = buildRowBlockStatus((RowType) type, positionCount, isView, Optional.ofNullable(isNull), offsets, primitiveNullRate, nestedNullRate, wrappings);
        } else {
            throw new UnsupportedOperationException(format("type %s is not supported.", type));
        }
    }
    if (isView) {
        positionCount /= 2;
        int offset = positionCount / 2;
        Block blockView = blockStatus.block.getRegion(offset, positionCount);
        int[] expectedRowSizesView = Arrays.stream(blockStatus.expectedRowSizes, offset, offset + positionCount).toArray();
        blockStatus = new BlockStatus(blockView, expectedRowSizesView);
    }
    blockStatus = buildDictRleBlockStatus(blockStatus, positionCount, wrappings);
    return blockStatus;
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) DecimalType(com.facebook.presto.common.type.DecimalType) DecimalType.createDecimalType(com.facebook.presto.common.type.DecimalType.createDecimalType) RowType(com.facebook.presto.common.type.RowType) MapBlock.fromKeyValueBlock(com.facebook.presto.common.block.MapBlock.fromKeyValueBlock) BlockAssertions.createRandomLongsBlock(com.facebook.presto.block.BlockAssertions.createRandomLongsBlock) OptimizedPartitionedOutputOperator.decodeBlock(com.facebook.presto.operator.repartition.OptimizedPartitionedOutputOperator.decodeBlock) BlockAssertions.createRandomDictionaryBlock(com.facebook.presto.block.BlockAssertions.createRandomDictionaryBlock) BlockAssertions.createRandomSmallintsBlock(com.facebook.presto.block.BlockAssertions.createRandomSmallintsBlock) ArrayBlock.fromElementBlock(com.facebook.presto.common.block.ArrayBlock.fromElementBlock) BlockAssertions.createAllNullsBlock(com.facebook.presto.block.BlockAssertions.createAllNullsBlock) BlockAssertions.createRandomBooleansBlock(com.facebook.presto.block.BlockAssertions.createRandomBooleansBlock) BlockAssertions.createRandomStringBlock(com.facebook.presto.block.BlockAssertions.createRandomStringBlock) BlockAssertions.createRLEBlock(com.facebook.presto.block.BlockAssertions.createRLEBlock) BlockAssertions.createRandomShortDecimalsBlock(com.facebook.presto.block.BlockAssertions.createRandomShortDecimalsBlock) BlockAssertions.createStringsBlock(com.facebook.presto.block.BlockAssertions.createStringsBlock) BlockAssertions.wrapBlock(com.facebook.presto.block.BlockAssertions.wrapBlock) BlockSerdeUtil.readBlock(com.facebook.presto.common.block.BlockSerdeUtil.readBlock) DictionaryBlock(com.facebook.presto.common.block.DictionaryBlock) BlockAssertions.createRandomIntsBlock(com.facebook.presto.block.BlockAssertions.createRandomIntsBlock) Block(com.facebook.presto.common.block.Block) BlockAssertions.createRandomLongDecimalsBlock(com.facebook.presto.block.BlockAssertions.createRandomLongDecimalsBlock) MapType(com.facebook.presto.common.type.MapType) BlockAssertions.createMapType(com.facebook.presto.block.BlockAssertions.createMapType)

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