Search in sources :

Example 1 with RealType

use of com.facebook.presto.common.type.RealType 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;
    }
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) PrestoException(com.facebook.presto.spi.PrestoException) Page(com.facebook.presto.common.Page) IOException(java.io.IOException) RealType(com.facebook.presto.common.type.RealType) BigintType(com.facebook.presto.common.type.BigintType) DateTime(org.joda.time.DateTime) Type(com.facebook.presto.common.type.Type) BigintType(com.facebook.presto.common.type.BigintType) RealType(com.facebook.presto.common.type.RealType) DoubleType(com.facebook.presto.common.type.DoubleType) TimestampType(com.facebook.presto.common.type.TimestampType) DoubleType(com.facebook.presto.common.type.DoubleType) Slice(io.airlift.slice.Slice) TimestampType(com.facebook.presto.common.type.TimestampType) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) BlockBuilder(com.facebook.presto.common.block.BlockBuilder)

Example 2 with RealType

use of com.facebook.presto.common.type.RealType 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();
}
Also used : IntervalYearMonthType(com.facebook.presto.type.IntervalYearMonthType) VarcharType(com.facebook.presto.common.type.VarcharType) TinyintType(com.facebook.presto.common.type.TinyintType) SqlVarbinary(com.facebook.presto.common.type.SqlVarbinary) SqlTime(com.facebook.presto.common.type.SqlTime) SqlIntervalYearMonth(com.facebook.presto.type.SqlIntervalYearMonth) SqlTimestamp(com.facebook.presto.common.type.SqlTimestamp) PrestoException(com.facebook.presto.spi.PrestoException) RealType(com.facebook.presto.common.type.RealType) IntervalDayTimeType(com.facebook.presto.type.IntervalDayTimeType) TimeType(com.facebook.presto.common.type.TimeType) VarbinaryType(com.facebook.presto.common.type.VarbinaryType) TimestampType(com.facebook.presto.common.type.TimestampType) SmallintType(com.facebook.presto.common.type.SmallintType) DateType(com.facebook.presto.common.type.DateType) SqlFunctionProperties(com.facebook.presto.common.function.SqlFunctionProperties) BooleanType(com.facebook.presto.common.type.BooleanType) BigintType(com.facebook.presto.common.type.BigintType) IntegerType(com.facebook.presto.common.type.IntegerType) IntervalDayTimeType(com.facebook.presto.type.IntervalDayTimeType) TinyintType(com.facebook.presto.common.type.TinyintType) BigintType(com.facebook.presto.common.type.BigintType) IntervalYearMonthType(com.facebook.presto.type.IntervalYearMonthType) VarcharType(com.facebook.presto.common.type.VarcharType) RealType(com.facebook.presto.common.type.RealType) SmallintType(com.facebook.presto.common.type.SmallintType) VarbinaryType(com.facebook.presto.common.type.VarbinaryType) TimestampType(com.facebook.presto.common.type.TimestampType) DecimalType(com.facebook.presto.common.type.DecimalType) BooleanType(com.facebook.presto.common.type.BooleanType) IntegerType(com.facebook.presto.common.type.IntegerType) CharType(com.facebook.presto.common.type.CharType) Type(com.facebook.presto.common.type.Type) TimeType(com.facebook.presto.common.type.TimeType) DateType(com.facebook.presto.common.type.DateType) DoubleType(com.facebook.presto.common.type.DoubleType) DoubleType(com.facebook.presto.common.type.DoubleType) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) Slice(io.airlift.slice.Slice) SqlDate(com.facebook.presto.common.type.SqlDate) SqlIntervalDayTime(com.facebook.presto.type.SqlIntervalDayTime) IntervalDayTimeType(com.facebook.presto.type.IntervalDayTimeType) DecimalType(com.facebook.presto.common.type.DecimalType) CharType(com.facebook.presto.common.type.CharType)

Example 3 with RealType

use of com.facebook.presto.common.type.RealType in project presto by prestodb.

the class ExpressionConverter method getIcebergLiteralValue.

private static Object getIcebergLiteralValue(Type type, Marker marker) {
    if (type instanceof IntegerType) {
        return toIntExact((long) marker.getValue());
    }
    if (type instanceof RealType) {
        return intBitsToFloat(toIntExact((long) marker.getValue()));
    }
    // TODO: Remove this conversion once we move to next iceberg version
    if (type instanceof DateType) {
        return toIntExact(((Long) marker.getValue()));
    }
    if (type instanceof TimestampType || type instanceof TimeType) {
        return MILLISECONDS.toMicros((Long) marker.getValue());
    }
    if (type instanceof VarcharType) {
        return ((Slice) marker.getValue()).toStringUtf8();
    }
    if (type instanceof VarbinaryType) {
        return ByteBuffer.wrap(((Slice) marker.getValue()).getBytes());
    }
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        Object value = requireNonNull(marker.getValue(), "The value of the marker must be non-null");
        if (Decimals.isShortDecimal(decimalType)) {
            checkArgument(value instanceof Long, "A short decimal should be represented by a Long value but was %s", value.getClass().getName());
            return BigDecimal.valueOf((long) value).movePointLeft(decimalType.getScale());
        }
        checkArgument(value instanceof Slice, "A long decimal should be represented by a Slice value but was %s", value.getClass().getName());
        return new BigDecimal(Decimals.decodeUnscaledValue((Slice) value), decimalType.getScale());
    }
    return marker.getValue();
}
Also used : IntegerType(com.facebook.presto.common.type.IntegerType) VarbinaryType(com.facebook.presto.common.type.VarbinaryType) VarcharType(com.facebook.presto.common.type.VarcharType) Slice(io.airlift.slice.Slice) TimestampType(com.facebook.presto.common.type.TimestampType) DecimalType(com.facebook.presto.common.type.DecimalType) RealType(com.facebook.presto.common.type.RealType) DateType(com.facebook.presto.common.type.DateType) BigDecimal(java.math.BigDecimal) TimeType(com.facebook.presto.common.type.TimeType)

Example 4 with RealType

use of com.facebook.presto.common.type.RealType 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;
}
Also used : Varchars.isVarcharType(com.facebook.presto.common.type.Varchars.isVarcharType) VarcharType(com.facebook.presto.common.type.VarcharType) TinyintType(com.facebook.presto.common.type.TinyintType) BooleanType(com.facebook.presto.common.type.BooleanType) PrestoException(com.facebook.presto.spi.PrestoException) ProtectMode.getProtectModeFromString(org.apache.hadoop.hive.metastore.ProtectMode.getProtectModeFromString) RealType(com.facebook.presto.common.type.RealType) BigintType(com.facebook.presto.common.type.BigintType) IntegerType(com.facebook.presto.common.type.IntegerType) Slice(io.airlift.slice.Slice) DoubleType(com.facebook.presto.common.type.DoubleType) DecimalType(com.facebook.presto.common.type.DecimalType) TimestampType(com.facebook.presto.common.type.TimestampType) SmallintType(com.facebook.presto.common.type.SmallintType) Chars.isCharType(com.facebook.presto.common.type.Chars.isCharType) CharType(com.facebook.presto.common.type.CharType) DateType(com.facebook.presto.common.type.DateType) DateTimeFormatter(org.joda.time.format.DateTimeFormatter)

Example 5 with RealType

use of com.facebook.presto.common.type.RealType 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();
}
Also used : DoubleType(com.facebook.presto.common.type.DoubleType) PrestoException(com.facebook.presto.spi.PrestoException) RealType(com.facebook.presto.common.type.RealType) BlockBuilder(com.facebook.presto.common.block.BlockBuilder)

Aggregations

RealType (com.facebook.presto.common.type.RealType)7 DoubleType (com.facebook.presto.common.type.DoubleType)6 TimestampType (com.facebook.presto.common.type.TimestampType)6 Slice (io.airlift.slice.Slice)6 BigintType (com.facebook.presto.common.type.BigintType)5 DecimalType (com.facebook.presto.common.type.DecimalType)5 IntegerType (com.facebook.presto.common.type.IntegerType)5 VarcharType (com.facebook.presto.common.type.VarcharType)5 PrestoException (com.facebook.presto.spi.PrestoException)5 BooleanType (com.facebook.presto.common.type.BooleanType)4 CharType (com.facebook.presto.common.type.CharType)4 DateType (com.facebook.presto.common.type.DateType)4 SmallintType (com.facebook.presto.common.type.SmallintType)4 TinyintType (com.facebook.presto.common.type.TinyintType)4 Type (com.facebook.presto.common.type.Type)4 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)2 TimeType (com.facebook.presto.common.type.TimeType)2 TimestampWithTimeZoneType (com.facebook.presto.common.type.TimestampWithTimeZoneType)2 VarbinaryType (com.facebook.presto.common.type.VarbinaryType)2 DateTimeFormatter (org.joda.time.format.DateTimeFormatter)2