use of io.trino.spi.type.DecimalType in project trino by trinodb.
the class TestTupleDomainParquetPredicate method testLongDecimalWithNoScale.
@Test
public void testLongDecimalWithNoScale() throws Exception {
ColumnDescriptor columnDescriptor = createColumnDescriptor(FIXED_LEN_BYTE_ARRAY, "LongDecimalColumnWithNoScale");
DecimalType type = createDecimalType(20, 0);
Int128 zero = Int128.ZERO;
Int128 hundred = Int128.valueOf(100L);
assertEquals(getDomain(columnDescriptor, type, 0, null, ID, UTC), all(type));
assertEquals(getDomain(columnDescriptor, type, 10, binaryColumnStats(100L, 100L), ID, UTC), singleValue(type, hundred));
assertEquals(getDomain(columnDescriptor, type, 10, binaryColumnStats(0L, 100L), ID, UTC), create(ValueSet.ofRanges(range(type, zero, true, hundred, true)), false));
// fail on corrupted statistics
assertThatExceptionOfType(ParquetCorruptionException.class).isThrownBy(() -> getDomain(columnDescriptor, type, 10, binaryColumnStats(100L, 10L), ID, UTC)).withMessage("Corrupted statistics for column \"[] required fixed_len_byte_array(0) LongDecimalColumnWithNoScale\" in Parquet file \"testFile\": [min: 0x64, max: 0x0A, num_nulls: 0]");
}
use of io.trino.spi.type.DecimalType in project trino by trinodb.
the class DecimalSumAggregation method specialize.
@Override
public AggregationMetadata specialize(BoundSignature boundSignature) {
Type inputType = getOnlyElement(boundSignature.getArgumentTypes());
checkArgument(inputType instanceof DecimalType, "type must be Decimal");
MethodHandle inputFunction;
Class<LongDecimalWithOverflowState> stateInterface = LongDecimalWithOverflowState.class;
LongDecimalWithOverflowStateSerializer stateSerializer = new LongDecimalWithOverflowStateSerializer();
if (((DecimalType) inputType).isShort()) {
inputFunction = SHORT_DECIMAL_INPUT_FUNCTION;
} else {
inputFunction = LONG_DECIMAL_INPUT_FUNCTION;
}
return new AggregationMetadata(inputFunction, Optional.empty(), Optional.of(COMBINE_FUNCTION), LONG_DECIMAL_OUTPUT_FUNCTION, ImmutableList.of(new AccumulatorStateDescriptor<>(stateInterface, stateSerializer, new LongDecimalWithOverflowStateFactory())));
}
use of io.trino.spi.type.DecimalType in project trino by trinodb.
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_MILLIS)) {
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 io.trino.spi.type.DecimalType in project trino by trinodb.
the class BlockAssertions method createLongDecimalsBlock.
public static Block createLongDecimalsBlock(Iterable<String> values) {
DecimalType longDecimalType = DecimalType.createDecimalType(MAX_SHORT_PRECISION + 1);
BlockBuilder builder = longDecimalType.createBlockBuilder(null, 100);
for (String value : values) {
if (value == null) {
builder.appendNull();
} else {
writeBigDecimal(longDecimalType, builder, new BigDecimal(value));
}
}
return builder.build();
}
use of io.trino.spi.type.DecimalType in project trino by trinodb.
the class QueryPlanner method planFrameOffset.
private FrameOffsetPlanAndSymbol planFrameOffset(PlanBuilder subPlan, Optional<Symbol> frameOffset) {
if (frameOffset.isEmpty()) {
return new FrameOffsetPlanAndSymbol(subPlan, Optional.empty());
}
Symbol offsetSymbol = frameOffset.get();
Type offsetType = symbolAllocator.getTypes().get(offsetSymbol);
// Append filter to validate offset values. They mustn't be negative or null.
Expression zeroOffset = zeroOfType(offsetType);
ResolvedFunction fail = plannerContext.getMetadata().resolveFunction(session, QualifiedName.of("fail"), fromTypes(VARCHAR));
Expression predicate = new IfExpression(new ComparisonExpression(GREATER_THAN_OR_EQUAL, offsetSymbol.toSymbolReference(), zeroOffset), TRUE_LITERAL, new Cast(new FunctionCall(fail.toQualifiedName(), ImmutableList.of(new Cast(new StringLiteral("Window frame offset value must not be negative or null"), toSqlType(VARCHAR)))), toSqlType(BOOLEAN)));
subPlan = subPlan.withNewRoot(new FilterNode(idAllocator.getNextId(), subPlan.getRoot(), predicate));
if (offsetType.equals(BIGINT)) {
return new FrameOffsetPlanAndSymbol(subPlan, Optional.of(offsetSymbol));
}
Expression offsetToBigint;
if (offsetType instanceof DecimalType && !((DecimalType) offsetType).isShort()) {
String maxBigint = Long.toString(Long.MAX_VALUE);
int maxBigintPrecision = maxBigint.length();
int actualPrecision = ((DecimalType) offsetType).getPrecision();
if (actualPrecision < maxBigintPrecision) {
offsetToBigint = new Cast(offsetSymbol.toSymbolReference(), toSqlType(BIGINT));
} else if (actualPrecision > maxBigintPrecision) {
// If the offset value exceeds max bigint, it implies that the frame bound falls beyond the partition bound.
// In such case, the frame bound is set to the partition bound. Passing max bigint as the offset value has
// the same effect. The offset value can be truncated to max bigint for the purpose of cast.
offsetToBigint = new GenericLiteral("BIGINT", maxBigint);
} else {
offsetToBigint = new IfExpression(new ComparisonExpression(LESS_THAN_OR_EQUAL, offsetSymbol.toSymbolReference(), new DecimalLiteral(maxBigint)), new Cast(offsetSymbol.toSymbolReference(), toSqlType(BIGINT)), new GenericLiteral("BIGINT", maxBigint));
}
} else {
offsetToBigint = new Cast(offsetSymbol.toSymbolReference(), toSqlType(BIGINT), false, typeCoercion.isTypeOnlyCoercion(offsetType, BIGINT));
}
Symbol coercedOffsetSymbol = symbolAllocator.newSymbol(offsetToBigint, BIGINT);
subPlan = subPlan.withNewRoot(new ProjectNode(idAllocator.getNextId(), subPlan.getRoot(), Assignments.builder().putIdentities(subPlan.getRoot().getOutputSymbols()).put(coercedOffsetSymbol, offsetToBigint).build()));
return new FrameOffsetPlanAndSymbol(subPlan, Optional.of(coercedOffsetSymbol));
}
Aggregations