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