use of com.facebook.presto.common.type.VarcharType in project presto by prestodb.
the class PinotBrokerPageSourceBase method setValue.
protected void setValue(Type type, BlockBuilder blockBuilder, String value) {
if (blockBuilder == null) {
return;
}
if (value == null) {
blockBuilder.appendNull();
return;
}
if (!(type instanceof FixedWidthType) && !(type instanceof VarcharType) && !(type instanceof JsonType)) {
throw new PinotException(PINOT_UNSUPPORTED_COLUMN_TYPE, Optional.empty(), "type '" + type + "' not supported");
}
if (type instanceof FixedWidthType) {
completedBytes += ((FixedWidthType) type).getFixedSize();
if (type instanceof BigintType) {
type.writeLong(blockBuilder, parseDouble(value).longValue());
} else if (type instanceof IntegerType) {
blockBuilder.writeInt(parseDouble(value).intValue());
} else if (type instanceof TinyintType) {
blockBuilder.writeByte(parseDouble(value).byteValue());
} else if (type instanceof SmallintType) {
blockBuilder.writeShort(parseDouble(value).shortValue());
} else if (type instanceof BooleanType) {
type.writeBoolean(blockBuilder, parseBoolean(value));
} else if (type instanceof DecimalType || type instanceof DoubleType) {
type.writeDouble(blockBuilder, parseDouble(value));
} else if (type instanceof TimestampType) {
type.writeLong(blockBuilder, parseTimestamp(value));
} else if (type instanceof DateType) {
type.writeLong(blockBuilder, parseLong(value));
} else {
throw new PinotException(PINOT_UNSUPPORTED_COLUMN_TYPE, Optional.empty(), "type '" + type + "' not supported");
}
} else {
Slice slice = Slices.utf8Slice(value);
blockBuilder.writeBytes(slice, 0, slice.length()).closeEntry();
completedBytes += slice.length();
}
}
use of com.facebook.presto.common.type.VarcharType in project presto by prestodb.
the class Row method nativeContainerToOrcValue.
private static Object nativeContainerToOrcValue(Type type, Object nativeValue) {
if (nativeValue == null) {
return null;
}
if (type instanceof DecimalType) {
BigInteger unscaledValue;
DecimalType decimalType = (DecimalType) type;
if (decimalType.isShort()) {
unscaledValue = BigInteger.valueOf((long) nativeValue);
} else {
unscaledValue = Decimals.decodeUnscaledValue((Slice) nativeValue);
}
return HiveDecimal.create(unscaledValue, decimalType.getScale());
}
if (type.getJavaType() == boolean.class) {
return nativeValue;
}
if (type.getJavaType() == long.class) {
return nativeValue;
}
if (type.getJavaType() == double.class) {
return nativeValue;
}
if (type.getJavaType() == Slice.class) {
Slice slice = (Slice) nativeValue;
return type instanceof VarcharType ? slice.toStringUtf8() : slice.getBytes();
}
if (isArrayType(type)) {
Block arrayBlock = (Block) nativeValue;
Type elementType = type.getTypeParameters().get(0);
List<Object> list = new ArrayList<>();
for (int i = 0; i < arrayBlock.getPositionCount(); i++) {
list.add(nativeContainerToOrcValue(elementType, getNativeContainerValue(elementType, arrayBlock, i)));
}
return list;
}
if (isMapType(type)) {
Block mapBlock = (Block) nativeValue;
Type keyType = type.getTypeParameters().get(0);
Type valueType = type.getTypeParameters().get(1);
Map<Object, Object> map = new HashMap<>();
for (int i = 0; i < mapBlock.getPositionCount(); i += 2) {
Object key = nativeContainerToOrcValue(keyType, getNativeContainerValue(keyType, mapBlock, i));
Object value = nativeContainerToOrcValue(valueType, getNativeContainerValue(valueType, mapBlock, i + 1));
map.put(key, value);
}
return map;
}
throw new PrestoException(GENERIC_INTERNAL_ERROR, "Unimplemented type: " + type);
}
use of com.facebook.presto.common.type.VarcharType in project presto by prestodb.
the class ShardStats method doComputeColumnStats.
private static ColumnStats doComputeColumnStats(OrcReader orcReader, long columnId, Type type, TypeManager typeManager) throws IOException {
StorageTypeConverter storageTypeConverter = new StorageTypeConverter(typeManager);
int columnIndex = columnIndex(orcReader.getColumnNames(), columnId);
OrcBatchRecordReader reader = orcReader.createBatchRecordReader(storageTypeConverter.toStorageTypes(ImmutableMap.of(columnIndex, toOrcFileType(type, typeManager))), OrcPredicate.TRUE, UTC, new RaptorOrcAggregatedMemoryContext(), INITIAL_BATCH_SIZE);
if (type.equals(BOOLEAN)) {
return indexBoolean(reader, columnIndex, columnId);
}
if (type.equals(BigintType.BIGINT) || type.equals(DateType.DATE) || type.equals(TimeType.TIME) || type.equals(TimestampType.TIMESTAMP)) {
return indexLong(type, reader, columnIndex, columnId);
}
if (type.equals(DOUBLE)) {
return indexDouble(reader, columnIndex, columnId);
}
if (type instanceof VarcharType) {
return indexString(type, reader, columnIndex, columnId);
}
return null;
}
use of com.facebook.presto.common.type.VarcharType in project presto by prestodb.
the class ParquetSchemaConverter method getPrimitiveType.
private org.apache.parquet.schema.Type getPrimitiveType(Type type, String name, List<String> parent) {
List<String> fullName = ImmutableList.<String>builder().addAll(parent).add(name).build();
primitiveTypes.put(fullName, type);
if (BOOLEAN.equals(type)) {
return Types.primitive(PrimitiveType.PrimitiveTypeName.BOOLEAN, OPTIONAL).named(name);
}
if (INTEGER.equals(type) || SMALLINT.equals(type) || TINYINT.equals(type)) {
return Types.primitive(PrimitiveType.PrimitiveTypeName.INT32, OPTIONAL).named(name);
}
if (type instanceof DecimalType) {
DecimalType decimalType = (DecimalType) type;
if (decimalType.getPrecision() <= 9) {
return Types.optional(PrimitiveType.PrimitiveTypeName.INT32).as(OriginalType.DECIMAL).precision(decimalType.getPrecision()).scale(decimalType.getScale()).named(name);
} else if (decimalType.isShort()) {
return Types.optional(PrimitiveType.PrimitiveTypeName.INT64).as(OriginalType.DECIMAL).precision(decimalType.getPrecision()).scale(decimalType.getScale()).named(name);
} else {
return Types.optional(PrimitiveType.PrimitiveTypeName.FIXED_LEN_BYTE_ARRAY).length(16).as(OriginalType.DECIMAL).precision(decimalType.getPrecision()).scale(decimalType.getScale()).named(name);
}
}
if (DATE.equals(type)) {
return Types.optional(PrimitiveType.PrimitiveTypeName.INT32).as(OriginalType.DATE).named(name);
}
if (BIGINT.equals(type) || TIMESTAMP.equals(type)) {
return Types.primitive(PrimitiveType.PrimitiveTypeName.INT64, OPTIONAL).named(name);
}
if (DOUBLE.equals(type)) {
return Types.primitive(PrimitiveType.PrimitiveTypeName.DOUBLE, OPTIONAL).named(name);
}
if (RealType.REAL.equals(type)) {
return Types.primitive(PrimitiveType.PrimitiveTypeName.FLOAT, OPTIONAL).named(name);
}
if (type instanceof VarcharType || type instanceof CharType || type instanceof VarbinaryType) {
return Types.primitive(PrimitiveType.PrimitiveTypeName.BINARY, OPTIONAL).named(name);
}
throw new PrestoException(NOT_SUPPORTED, format("Unsupported primitive type: %s", type));
}
use of com.facebook.presto.common.type.VarcharType in project presto by prestodb.
the class TestExpressionCompiler method testFunctionCall.
@Test
public void testFunctionCall() throws Exception {
for (Integer left : intLefts) {
for (Integer right : intRights) {
assertExecute(generateExpression("bitwise_and(%s, %s)", left, right), BIGINT, left == null || right == null ? null : BitwiseFunctions.bitwiseAnd(left, right));
}
}
for (Integer left : intLefts) {
for (Double right : doubleRights) {
assertExecute(generateExpression("mod(%s, %s)", left, right), DOUBLE, left == null || right == null ? null : MathFunctions.mod(left, right));
}
}
for (Double left : doubleLefts) {
for (Integer right : intRights) {
assertExecute(generateExpression("mod(%s, %s)", left, right), DOUBLE, left == null || right == null ? null : MathFunctions.mod(left, right));
}
}
for (Double left : doubleLefts) {
for (Double right : doubleRights) {
assertExecute(generateExpression("mod(%s, %s)", left, right), DOUBLE, left == null || right == null ? null : MathFunctions.mod(left, right));
}
}
for (Double left : doubleLefts) {
for (BigDecimal right : decimalRights) {
assertExecute(generateExpression("mod(%s, %s)", left, right), DOUBLE, left == null || right == null ? null : MathFunctions.mod(left, right.doubleValue()));
}
}
for (BigDecimal left : decimalLefts) {
for (Long right : longRights) {
assertExecute(generateExpression("power(%s, %s)", left, right), DOUBLE, left == null || right == null ? null : MathFunctions.power(left.doubleValue(), right));
}
}
for (String value : stringLefts) {
for (Integer start : intLefts) {
for (Integer length : intRights) {
String expected;
if (value == null || start == null || length == null) {
expected = null;
} else {
expected = StringFunctions.substr(utf8Slice(value), start, length).toStringUtf8();
}
VarcharType expectedType = value != null ? createVarcharType(value.length()) : VARCHAR;
assertExecute(generateExpression("substr(%s, %s, %s)", value, start, length), expectedType, expected);
}
}
}
Futures.allAsList(futures).get();
}
Aggregations