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"));
}
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;
}
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();
}
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;
}
}
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;
}
Aggregations