Search in sources :

Example 61 with DecimalType

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]");
}
Also used : ColumnDescriptor(org.apache.parquet.column.ColumnDescriptor) DecimalType.createDecimalType(io.trino.spi.type.DecimalType.createDecimalType) DecimalType(io.trino.spi.type.DecimalType) Int128(io.trino.spi.type.Int128) Test(org.testng.annotations.Test)

Example 62 with DecimalType

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())));
}
Also used : Type(io.trino.spi.type.Type) DecimalType(io.trino.spi.type.DecimalType) AccumulatorStateDescriptor(io.trino.operator.aggregation.AggregationMetadata.AccumulatorStateDescriptor) LongDecimalWithOverflowStateFactory(io.trino.operator.aggregation.state.LongDecimalWithOverflowStateFactory) DecimalType(io.trino.spi.type.DecimalType) LongDecimalWithOverflowStateSerializer(io.trino.operator.aggregation.state.LongDecimalWithOverflowStateSerializer) MethodHandle(java.lang.invoke.MethodHandle) LongDecimalWithOverflowState(io.trino.operator.aggregation.state.LongDecimalWithOverflowState)

Example 63 with DecimalType

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);
}
Also used : Type(io.trino.spi.type.Type) VarcharType(io.trino.spi.type.VarcharType) DecimalType(io.trino.spi.type.DecimalType) VarcharType(io.trino.spi.type.VarcharType) Block(io.trino.spi.block.Block) DecimalType(io.trino.spi.type.DecimalType) Page(io.trino.spi.Page)

Example 64 with DecimalType

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();
}
Also used : DecimalType(io.trino.spi.type.DecimalType) BigDecimal(java.math.BigDecimal) Decimals.writeBigDecimal(io.trino.spi.type.Decimals.writeBigDecimal) BlockBuilder(io.trino.spi.block.BlockBuilder) RowBlockBuilder(io.trino.spi.block.RowBlockBuilder)

Example 65 with DecimalType

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));
}
Also used : Cast(io.trino.sql.tree.Cast) IfExpression(io.trino.sql.tree.IfExpression) ResolvedFunction(io.trino.metadata.ResolvedFunction) FilterNode(io.trino.sql.planner.plan.FilterNode) GenericLiteral(io.trino.sql.tree.GenericLiteral) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) RelationType(io.trino.sql.analyzer.RelationType) ExpressionAnalyzer.isNumericType(io.trino.sql.analyzer.ExpressionAnalyzer.isNumericType) TypeSignatureTranslator.toSqlType(io.trino.sql.analyzer.TypeSignatureTranslator.toSqlType) DecimalType(io.trino.spi.type.DecimalType) Type(io.trino.spi.type.Type) StringLiteral(io.trino.sql.tree.StringLiteral) SelectExpression(io.trino.sql.analyzer.Analysis.SelectExpression) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) IfExpression(io.trino.sql.tree.IfExpression) Expression(io.trino.sql.tree.Expression) LambdaExpression(io.trino.sql.tree.LambdaExpression) DecimalLiteral(io.trino.sql.tree.DecimalLiteral) DecimalType(io.trino.spi.type.DecimalType) ProjectNode(io.trino.sql.planner.plan.ProjectNode) FunctionCall(io.trino.sql.tree.FunctionCall)

Aggregations

DecimalType (io.trino.spi.type.DecimalType)79 VarcharType (io.trino.spi.type.VarcharType)50 CharType (io.trino.spi.type.CharType)39 TrinoException (io.trino.spi.TrinoException)31 Type (io.trino.spi.type.Type)29 TimestampType (io.trino.spi.type.TimestampType)23 DecimalType.createDecimalType (io.trino.spi.type.DecimalType.createDecimalType)22 ArrayType (io.trino.spi.type.ArrayType)21 BigDecimal (java.math.BigDecimal)19 Int128 (io.trino.spi.type.Int128)16 BigInteger (java.math.BigInteger)15 Block (io.trino.spi.block.Block)14 Slice (io.airlift.slice.Slice)13 TimeType (io.trino.spi.type.TimeType)13 TimestampWithTimeZoneType (io.trino.spi.type.TimestampWithTimeZoneType)13 VarcharType.createUnboundedVarcharType (io.trino.spi.type.VarcharType.createUnboundedVarcharType)13 MapType (io.trino.spi.type.MapType)12 ArrayList (java.util.ArrayList)12 ImmutableList (com.google.common.collect.ImmutableList)11 RowType (io.trino.spi.type.RowType)11