Search in sources :

Example 1 with TimestampType

use of com.facebook.presto.common.type.TimestampType 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 TimestampType

use of com.facebook.presto.common.type.TimestampType 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();
    }
}
Also used : JsonType(com.facebook.presto.common.type.JsonType) VarcharType(com.facebook.presto.common.type.VarcharType) TinyintType(com.facebook.presto.common.type.TinyintType) BooleanType(com.facebook.presto.common.type.BooleanType) BigintType(com.facebook.presto.common.type.BigintType) IntegerType(com.facebook.presto.common.type.IntegerType) DoubleType(com.facebook.presto.common.type.DoubleType) Slice(io.airlift.slice.Slice) DecimalType(com.facebook.presto.common.type.DecimalType) TimestampType(com.facebook.presto.common.type.TimestampType) SmallintType(com.facebook.presto.common.type.SmallintType) DateType(com.facebook.presto.common.type.DateType) FixedWidthType(com.facebook.presto.common.type.FixedWidthType)

Example 3 with TimestampType

use of com.facebook.presto.common.type.TimestampType 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 4 with TimestampType

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

the class TestJmxSplitManager method readTimeStampsFrom.

private List<Long> readTimeStampsFrom(RecordSet recordSet) {
    ImmutableList.Builder<Long> result = ImmutableList.builder();
    try (RecordCursor cursor = recordSet.cursor()) {
        while (cursor.advanceNextPosition()) {
            for (int i = 0; i < recordSet.getColumnTypes().size(); i++) {
                cursor.isNull(i);
            }
            if (cursor.isNull(0)) {
                return result.build();
            }
            assertTrue(recordSet.getColumnTypes().get(0) instanceof TimestampType);
            result.add(cursor.getLong(0));
        }
    }
    return result.build();
}
Also used : RecordCursor(com.facebook.presto.spi.RecordCursor) ImmutableList(com.google.common.collect.ImmutableList) TimestampType(com.facebook.presto.common.type.TimestampType)

Example 5 with TimestampType

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

the class AbstractRowEncoder method appendColumnValue.

@Override
public void appendColumnValue(Block block, int position) {
    checkArgument(currentColumnIndex < columnHandles.size(), format("currentColumnIndex '%d' is greater than number of columns '%d'", currentColumnIndex, columnHandles.size()));
    Type type = columnHandles.get(currentColumnIndex).getType();
    if (block.isNull(position)) {
        appendNullValue();
    } else if (type == BOOLEAN) {
        appendBoolean(type.getBoolean(block, position));
    } else if (type == BIGINT) {
        appendLong(type.getLong(block, position));
    } else if (type == INTEGER) {
        appendInt(toIntExact(type.getLong(block, position)));
    } else if (type == SMALLINT) {
        appendShort(Shorts.checkedCast(type.getLong(block, position)));
    } else if (type == TINYINT) {
        appendByte(SignedBytes.checkedCast(type.getLong(block, position)));
    } else if (type == DOUBLE) {
        appendDouble(type.getDouble(block, position));
    } else if (type == REAL) {
        appendFloat(intBitsToFloat(toIntExact(type.getLong(block, position))));
    } else if (isVarcharType(type)) {
        appendString(type.getSlice(block, position).toStringUtf8());
    } else if (isVarbinaryType(type)) {
        appendByteBuffer(type.getSlice(block, position).toByteBuffer());
    } else if (type == DATE) {
        appendSqlDate((SqlDate) type.getObjectValue(session.getSqlFunctionProperties(), block, position));
    } else if (type == TIME) {
        appendSqlTime((SqlTime) type.getObjectValue(session.getSqlFunctionProperties(), block, position));
    } else if (type == TIME_WITH_TIME_ZONE) {
        appendSqlTimeWithTimeZone((SqlTimeWithTimeZone) type.getObjectValue(session.getSqlFunctionProperties(), block, position));
    } else if (type instanceof TimestampType) {
        appendSqlTimestamp((SqlTimestamp) type.getObjectValue(session.getSqlFunctionProperties(), block, position));
    } else if (type instanceof TimestampWithTimeZoneType) {
        appendSqlTimestampWithTimeZone((SqlTimestampWithTimeZone) type.getObjectValue(session.getSqlFunctionProperties(), block, position));
    } else {
        throw new UnsupportedOperationException(format("Column '%s' does not support 'null' value", columnHandles.get(currentColumnIndex).getName()));
    }
    currentColumnIndex++;
}
Also used : Varchars.isVarcharType(com.facebook.presto.common.type.Varchars.isVarcharType) VarbinaryType.isVarbinaryType(com.facebook.presto.common.type.VarbinaryType.isVarbinaryType) Type(com.facebook.presto.common.type.Type) TimestampWithTimeZoneType(com.facebook.presto.common.type.TimestampWithTimeZoneType) TimestampType(com.facebook.presto.common.type.TimestampType) TimestampWithTimeZoneType(com.facebook.presto.common.type.TimestampWithTimeZoneType) SqlTime(com.facebook.presto.common.type.SqlTime) TimestampType(com.facebook.presto.common.type.TimestampType) SqlTimestamp(com.facebook.presto.common.type.SqlTimestamp)

Aggregations

TimestampType (com.facebook.presto.common.type.TimestampType)10 Slice (io.airlift.slice.Slice)7 BigintType (com.facebook.presto.common.type.BigintType)6 DecimalType (com.facebook.presto.common.type.DecimalType)6 DoubleType (com.facebook.presto.common.type.DoubleType)6 IntegerType (com.facebook.presto.common.type.IntegerType)6 RealType (com.facebook.presto.common.type.RealType)6 Type (com.facebook.presto.common.type.Type)6 VarcharType (com.facebook.presto.common.type.VarcharType)6 BooleanType (com.facebook.presto.common.type.BooleanType)5 DateType (com.facebook.presto.common.type.DateType)5 SmallintType (com.facebook.presto.common.type.SmallintType)5 TinyintType (com.facebook.presto.common.type.TinyintType)5 CharType (com.facebook.presto.common.type.CharType)4 PrestoException (com.facebook.presto.spi.PrestoException)4 TimestampWithTimeZoneType (com.facebook.presto.common.type.TimestampWithTimeZoneType)3 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)2 FixedWidthType (com.facebook.presto.common.type.FixedWidthType)2 SqlTime (com.facebook.presto.common.type.SqlTime)2 SqlTimestamp (com.facebook.presto.common.type.SqlTimestamp)2