use of com.facebook.presto.common.type.DoubleType in project presto by prestodb.
the class DruidBrokerPageSource method getNextPage.
@Override
public Page getNextPage() {
if (finished) {
return null;
}
long start = System.nanoTime();
boolean columnHandlesHasErrorMessageField = columnHandles.stream().anyMatch(handle -> ((DruidColumnHandle) handle).getColumnName().equals("errorMessage"));
try {
String readLine;
while ((readLine = responseStream.readLine()) != null) {
// if read a blank line,it means read finish
if (readLine.isEmpty()) {
finished = true;
break;
} else {
JsonNode rootNode = OBJECT_MAPPER.readTree(readLine);
if (rootNode.has("errorMessage") && !columnHandlesHasErrorMessageField) {
throw new PrestoException(DRUID_BROKER_RESULT_ERROR, rootNode.findValue("errorMessage").asText());
}
for (int i = 0; i < columnHandles.size(); i++) {
Type type = columnTypes.get(i);
BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(i);
JsonNode value = rootNode.get(((DruidColumnHandle) columnHandles.get(i)).getColumnName());
if (value == null) {
blockBuilder.appendNull();
continue;
}
if (type instanceof BigintType) {
type.writeLong(blockBuilder, value.longValue());
} else if (type instanceof DoubleType) {
type.writeDouble(blockBuilder, value.doubleValue());
} else if (type instanceof RealType) {
type.writeLong(blockBuilder, floatToRawIntBits(value.floatValue()));
} else if (type instanceof TimestampType) {
DateTimeFormatter formatter = ISODateTimeFormat.dateTimeParser().withChronology(ISOChronology.getInstanceUTC()).withOffsetParsed();
DateTime dateTime = formatter.parseDateTime(value.textValue());
type.writeLong(blockBuilder, dateTime.getMillis());
} else {
Slice slice = Slices.utf8Slice(value.textValue());
type.writeSlice(blockBuilder, slice);
}
}
}
pageBuilder.declarePosition();
if (pageBuilder.isFull()) {
break;
}
}
// if responseStream.readLine() is null, it means read finish
if (readLine == null) {
finished = true;
return null;
}
// only return a page if the buffer is full or we are finishing
if (pageBuilder.isEmpty() || (!finished && !pageBuilder.isFull())) {
return null;
}
Page page = pageBuilder.build();
completedPositions += page.getPositionCount();
completedBytes += page.getSizeInBytes();
pageBuilder.reset();
return page;
} catch (IOException e) {
finished = true;
throw new PrestoException(DRUID_BROKER_RESULT_ERROR, "Parse druid client response error", e);
} finally {
readTimeNanos += System.nanoTime() - start;
}
}
use of com.facebook.presto.common.type.DoubleType 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.DoubleType in project presto by prestodb.
the class LiteralInterpreter method evaluate.
public static Object evaluate(ConnectorSession session, ConstantExpression node) {
Type type = node.getType();
SqlFunctionProperties properties = session.getSqlFunctionProperties();
if (node.getValue() == null) {
return null;
}
if (type instanceof BooleanType) {
return node.getValue();
}
if (type instanceof BigintType || type instanceof TinyintType || type instanceof SmallintType || type instanceof IntegerType) {
return node.getValue();
}
if (type instanceof DoubleType) {
return node.getValue();
}
if (type instanceof RealType) {
Long number = (Long) node.getValue();
return intBitsToFloat(number.intValue());
}
if (type instanceof DecimalType) {
DecimalType decimalType = (DecimalType) type;
if (decimalType.isShort()) {
checkState(node.getValue() instanceof Long);
return decodeDecimal(BigInteger.valueOf((long) node.getValue()), decimalType);
}
checkState(node.getValue() instanceof Slice);
Slice value = (Slice) node.getValue();
return decodeDecimal(decodeUnscaledValue(value), decimalType);
}
if (type instanceof VarcharType || type instanceof CharType) {
return ((Slice) node.getValue()).toStringUtf8();
}
if (type instanceof VarbinaryType) {
return new SqlVarbinary(((Slice) node.getValue()).getBytes());
}
if (type instanceof DateType) {
return new SqlDate(((Long) node.getValue()).intValue());
}
if (type instanceof TimeType) {
if (properties.isLegacyTimestamp()) {
return new SqlTime((long) node.getValue(), properties.getTimeZoneKey());
}
return new SqlTime((long) node.getValue());
}
if (type instanceof TimestampType) {
try {
if (properties.isLegacyTimestamp()) {
return new SqlTimestamp((long) node.getValue(), properties.getTimeZoneKey());
}
return new SqlTimestamp((long) node.getValue());
} catch (RuntimeException e) {
throw new PrestoException(GENERIC_USER_ERROR, format("'%s' is not a valid timestamp literal", (String) node.getValue()));
}
}
if (type instanceof IntervalDayTimeType) {
return new SqlIntervalDayTime((long) node.getValue());
}
if (type instanceof IntervalYearMonthType) {
return new SqlIntervalYearMonth(((Long) node.getValue()).intValue());
}
if (type.getJavaType().equals(Slice.class)) {
// DO NOT ever remove toBase64. Calling toString directly on Slice whose base is not byte[] will cause JVM to crash.
return "'" + VarbinaryFunctions.toBase64((Slice) node.getValue()).toStringUtf8() + "'";
}
// We should not fail at the moment; just return the raw value (block, regex, etc) to the user
return node.getValue();
}
use of com.facebook.presto.common.type.DoubleType in project presto by prestodb.
the class MetastoreUtil method convertRawValueToString.
public static String convertRawValueToString(Object value, Type type) {
String val;
if (value == null) {
val = HIVE_DEFAULT_DYNAMIC_PARTITION;
} else if (type instanceof CharType) {
Slice slice = (Slice) value;
val = padSpaces(slice, type).toStringUtf8();
} else if (type instanceof VarcharType) {
Slice slice = (Slice) value;
val = slice.toStringUtf8();
} else if (type instanceof DecimalType && !((DecimalType) type).isShort()) {
Slice slice = (Slice) value;
val = Decimals.toString(slice, ((DecimalType) type).getScale());
} else if (type instanceof DecimalType && ((DecimalType) type).isShort()) {
val = Decimals.toString((long) value, ((DecimalType) type).getScale());
} else if (type instanceof DateType) {
DateTimeFormatter dateTimeFormatter = ISODateTimeFormat.date().withZoneUTC();
val = dateTimeFormatter.print(TimeUnit.DAYS.toMillis((long) value));
} else if (type instanceof TimestampType) {
// we don't have time zone info, so just add a wildcard
val = PARTITION_VALUE_WILDCARD;
} else if (type instanceof TinyintType || type instanceof SmallintType || type instanceof IntegerType || type instanceof BigintType || type instanceof DoubleType || type instanceof RealType || type instanceof BooleanType) {
val = value.toString();
} else {
throw new PrestoException(NOT_SUPPORTED, format("Unsupported partition key type: %s", type.getDisplayName()));
}
return val;
}
use of com.facebook.presto.common.type.DoubleType in project presto by prestodb.
the class ArrayNormalizeFunction method normalizeArray.
private static Block normalizeArray(Type elementType, Block block, double p, ValueAccessor valueAccessor) {
if (!(elementType instanceof RealType) && !(elementType instanceof DoubleType)) {
throw new PrestoException(FUNCTION_IMPLEMENTATION_MISSING, format("Unsupported array element type for array_normalize function: %s", elementType.getDisplayName()));
}
checkCondition(p >= 0, INVALID_FUNCTION_ARGUMENT, "array_normalize only supports non-negative p: %s", p);
if (p == 0) {
return block;
}
int elementCount = block.getPositionCount();
double pNorm = 0;
for (int i = 0; i < elementCount; i++) {
if (block.isNull(i)) {
return null;
}
pNorm += Math.pow(Math.abs(valueAccessor.getValue(elementType, block, i)), p);
}
if (pNorm == 0) {
return block;
}
pNorm = Math.pow(pNorm, 1.0 / p);
BlockBuilder blockBuilder = elementType.createBlockBuilder(null, elementCount);
for (int i = 0; i < elementCount; i++) {
valueAccessor.writeValue(elementType, blockBuilder, valueAccessor.getValue(elementType, block, i) / pNorm);
}
return blockBuilder.build();
}
Aggregations