Search in sources :

Example 66 with VarcharType

use of io.trino.spi.type.VarcharType 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 67 with VarcharType

use of io.trino.spi.type.VarcharType in project trino by trinodb.

the class TestValueStore method setUp.

@BeforeMethod(alwaysRun = true)
public void setUp() {
    VarcharType type = VarcharType.createVarcharType(100);
    BlockTypeOperators blockTypeOperators = new BlockTypeOperators(new TypeOperators());
    BlockPositionEqual equalOperator = blockTypeOperators.getEqualOperator(type);
    hashCodeOperator = blockTypeOperators.getHashCodeOperator(type);
    BlockBuilder blockBuilder = type.createBlockBuilder(null, 100, 10);
    valueStore = new ValueStore(type, equalOperator, 100, blockBuilder);
    valueStoreSmall = new ValueStore(type, equalOperator, 1, blockBuilder);
    block = BlockAssertions.createStringsBlock("a", "b", "c", "d");
}
Also used : BlockPositionEqual(io.trino.type.BlockTypeOperators.BlockPositionEqual) BlockTypeOperators(io.trino.type.BlockTypeOperators) VarcharType(io.trino.spi.type.VarcharType) BlockTypeOperators(io.trino.type.BlockTypeOperators) TypeOperators(io.trino.spi.type.TypeOperators) BlockBuilder(io.trino.spi.block.BlockBuilder) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 68 with VarcharType

use of io.trino.spi.type.VarcharType in project trino by trinodb.

the class HiveCoercionRecordCursor method createCoercer.

private static Coercer createCoercer(TypeManager typeManager, HiveType fromHiveType, HiveType toHiveType, BridgingRecordCursor bridgingRecordCursor) {
    Type fromType = typeManager.getType(fromHiveType.getTypeSignature());
    Type toType = typeManager.getType(toHiveType.getTypeSignature());
    if (toType instanceof VarcharType && (fromHiveType.equals(HIVE_BYTE) || fromHiveType.equals(HIVE_SHORT) || fromHiveType.equals(HIVE_INT) || fromHiveType.equals(HIVE_LONG))) {
        return new IntegerNumberToVarcharCoercer();
    }
    if (fromType instanceof VarcharType && (toHiveType.equals(HIVE_BYTE) || toHiveType.equals(HIVE_SHORT) || toHiveType.equals(HIVE_INT) || toHiveType.equals(HIVE_LONG))) {
        return new VarcharToIntegerNumberCoercer(toHiveType);
    }
    if (fromHiveType.equals(HIVE_BYTE) && toHiveType.equals(HIVE_SHORT) || toHiveType.equals(HIVE_INT) || toHiveType.equals(HIVE_LONG)) {
        return new IntegerNumberUpscaleCoercer();
    }
    if (fromHiveType.equals(HIVE_SHORT) && toHiveType.equals(HIVE_INT) || toHiveType.equals(HIVE_LONG)) {
        return new IntegerNumberUpscaleCoercer();
    }
    if (fromHiveType.equals(HIVE_INT) && toHiveType.equals(HIVE_LONG)) {
        return new IntegerNumberUpscaleCoercer();
    }
    if (fromHiveType.equals(HIVE_FLOAT) && toHiveType.equals(HIVE_DOUBLE)) {
        return new FloatToDoubleCoercer();
    }
    if (isArrayType(fromType) && isArrayType(toType)) {
        return new ListCoercer(typeManager, fromHiveType, toHiveType, bridgingRecordCursor);
    }
    if (isMapType(fromType) && isMapType(toType)) {
        return new MapCoercer(typeManager, fromHiveType, toHiveType, bridgingRecordCursor);
    }
    if (isRowType(fromType) && isRowType(toType)) {
        return new StructCoercer(typeManager, fromHiveType, toHiveType, bridgingRecordCursor);
    }
    throw new TrinoException(NOT_SUPPORTED, format("Unsupported coercion from %s to %s", fromHiveType, toHiveType));
}
Also used : HiveUtil.isRowType(io.trino.plugin.hive.util.HiveUtil.isRowType) HiveUtil.isMapType(io.trino.plugin.hive.util.HiveUtil.isMapType) Type(io.trino.spi.type.Type) VarcharType(io.trino.spi.type.VarcharType) HiveUtil.isArrayType(io.trino.plugin.hive.util.HiveUtil.isArrayType) VarcharType(io.trino.spi.type.VarcharType) TrinoException(io.trino.spi.TrinoException)

Example 69 with VarcharType

use of io.trino.spi.type.VarcharType in project trino by trinodb.

the class SetTimeZoneTask method getTimeZoneId.

private String getTimeZoneId(Expression expression, SetTimeZone statement, QueryStateMachine stateMachine, List<Expression> parameters, WarningCollector warningCollector) {
    Map<NodeRef<Parameter>, Expression> parameterLookup = parameterExtractor(statement, parameters);
    ExpressionAnalyzer analyzer = createConstantAnalyzer(plannerContext, accessControl, stateMachine.getSession(), parameterLookup, warningCollector);
    Type type = analyzer.analyze(expression, Scope.create());
    if (!(type instanceof VarcharType || type instanceof IntervalDayTimeType)) {
        throw new TrinoException(TYPE_MISMATCH, format("Expected expression of varchar or interval day-time type, but '%s' has %s type", expression, type.getDisplayName()));
    }
    Object timeZoneValue = evaluateConstantExpression(expression, analyzer.getExpressionCoercions(), analyzer.getTypeOnlyCoercions(), plannerContext, stateMachine.getSession(), accessControl, ImmutableSet.of(), parameterLookup);
    TimeZoneKey timeZoneKey;
    if (timeZoneValue instanceof Slice) {
        timeZoneKey = getTimeZoneKey(((Slice) timeZoneValue).toStringUtf8());
    } else if (timeZoneValue instanceof Long) {
        timeZoneKey = getTimeZoneKeyForOffset(getZoneOffsetMinutes((Long) timeZoneValue));
    } else {
        throw new IllegalStateException(format("Time Zone expression '%s' not supported", expression));
    }
    return timeZoneKey.getId();
}
Also used : VarcharType(io.trino.spi.type.VarcharType) ExpressionAnalyzer(io.trino.sql.analyzer.ExpressionAnalyzer) NodeRef(io.trino.sql.tree.NodeRef) Type(io.trino.spi.type.Type) VarcharType(io.trino.spi.type.VarcharType) IntervalDayTimeType(io.trino.type.IntervalDayTimeType) ExpressionInterpreter.evaluateConstantExpression(io.trino.sql.planner.ExpressionInterpreter.evaluateConstantExpression) Expression(io.trino.sql.tree.Expression) Slice(io.airlift.slice.Slice) IntervalDayTimeType(io.trino.type.IntervalDayTimeType) TrinoException(io.trino.spi.TrinoException) TimeZoneKey(io.trino.spi.type.TimeZoneKey) TimeZoneKey.getTimeZoneKey(io.trino.spi.type.TimeZoneKey.getTimeZoneKey)

Example 70 with VarcharType

use of io.trino.spi.type.VarcharType in project trino by trinodb.

the class AvroDecoderTestUtil method checkMapValues.

public static void checkMapValues(Block block, Type type, Object value) {
    assertNotNull(type, "Type is null");
    assertTrue(type instanceof MapType, "Unexpected type");
    assertTrue(((MapType) type).getKeyType() instanceof VarcharType, "Unexpected key type");
    assertNotNull(block, "Block is null");
    assertNotNull(value, "Value is null");
    Map<String, ?> expected = (Map<String, ?>) value;
    assertEquals(block.getPositionCount(), expected.size() * 2);
    Type valueType = ((MapType) type).getValueType();
    if (valueType instanceof ArrayType) {
        for (int index = 0; index < block.getPositionCount(); index += 2) {
            String actualKey = VARCHAR.getSlice(block, index).toStringUtf8();
            assertTrue(expected.containsKey(actualKey));
            if (block.isNull(index + 1)) {
                assertNull(expected.get(actualKey));
                continue;
            }
            Block arrayBlock = block.getObject(index + 1, Block.class);
            checkArrayValues(arrayBlock, valueType, expected.get(actualKey));
        }
    } else if (valueType instanceof MapType) {
        for (int index = 0; index < block.getPositionCount(); index += 2) {
            String actualKey = VARCHAR.getSlice(block, index).toStringUtf8();
            assertTrue(expected.containsKey(actualKey));
            if (block.isNull(index + 1)) {
                assertNull(expected.get(actualKey));
                continue;
            }
            Block mapBlock = block.getObject(index + 1, Block.class);
            checkMapValues(mapBlock, valueType, expected.get(actualKey));
        }
    } else if (valueType instanceof RowType) {
        for (int index = 0; index < block.getPositionCount(); index += 2) {
            String actualKey = VARCHAR.getSlice(block, index).toStringUtf8();
            assertTrue(expected.containsKey(actualKey));
            if (block.isNull(index + 1)) {
                assertNull(expected.get(actualKey));
                continue;
            }
            Block rowBlock = block.getObject(index + 1, Block.class);
            checkRowValues(rowBlock, valueType, expected.get(actualKey));
        }
    } else {
        for (int index = 0; index < block.getPositionCount(); index += 2) {
            String actualKey = VARCHAR.getSlice(block, index).toStringUtf8();
            assertTrue(expected.containsKey(actualKey));
            checkPrimitiveValue(getObjectValue(valueType, block, index + 1), expected.get(actualKey));
        }
    }
}
Also used : ArrayType(io.trino.spi.type.ArrayType) RowType(io.trino.spi.type.RowType) MapType(io.trino.spi.type.MapType) Type(io.trino.spi.type.Type) ArrayType(io.trino.spi.type.ArrayType) VarcharType(io.trino.spi.type.VarcharType) VarcharType(io.trino.spi.type.VarcharType) Block(io.trino.spi.block.Block) RowType(io.trino.spi.type.RowType) Map(java.util.Map) MapType(io.trino.spi.type.MapType)

Aggregations

VarcharType (io.trino.spi.type.VarcharType)83 DecimalType (io.trino.spi.type.DecimalType)50 CharType (io.trino.spi.type.CharType)47 Type (io.trino.spi.type.Type)37 TrinoException (io.trino.spi.TrinoException)35 ArrayType (io.trino.spi.type.ArrayType)29 TimestampType (io.trino.spi.type.TimestampType)23 VarcharType.createUnboundedVarcharType (io.trino.spi.type.VarcharType.createUnboundedVarcharType)23 MapType (io.trino.spi.type.MapType)21 Slice (io.airlift.slice.Slice)20 DecimalType.createDecimalType (io.trino.spi.type.DecimalType.createDecimalType)18 RowType (io.trino.spi.type.RowType)18 ImmutableList (com.google.common.collect.ImmutableList)17 TimeType (io.trino.spi.type.TimeType)15 VarbinaryType (io.trino.spi.type.VarbinaryType)15 Block (io.trino.spi.block.Block)14 TimestampWithTimeZoneType (io.trino.spi.type.TimestampWithTimeZoneType)14 BigDecimal (java.math.BigDecimal)14 List (java.util.List)14 Slices.utf8Slice (io.airlift.slice.Slices.utf8Slice)12