Java Code Examples for io.prestosql.spi.type.Type#writeDouble()
The following examples show how to use
io.prestosql.spi.type.Type#writeDouble() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractTestType.java From presto with Apache License 2.0 | 6 votes |
private static Block createBlock(Type type, Object value) { BlockBuilder blockBuilder = type.createBlockBuilder(null, 1); Class<?> javaType = type.getJavaType(); if (value == null) { blockBuilder.appendNull(); } else if (javaType == boolean.class) { type.writeBoolean(blockBuilder, (Boolean) value); } else if (javaType == long.class) { type.writeLong(blockBuilder, (Long) value); } else if (javaType == double.class) { type.writeDouble(blockBuilder, (Double) value); } else if (javaType == Slice.class) { Slice slice = (Slice) value; type.writeSlice(blockBuilder, slice, 0, slice.length()); } else { type.writeObject(blockBuilder, value); } return blockBuilder.build(); }
Example 2
Source File: RepeatFunction.java From presto with Apache License 2.0 | 6 votes |
@TypeParameter("T") @SqlType("array(T)") public static Block repeat( @TypeParameter("T") Type type, @SqlNullable @SqlType("T") Double element, @SqlType(StandardTypes.INTEGER) long count) { BlockBuilder blockBuilder = createBlockBuilder(type, count); if (element == null) { return repeatNullValues(blockBuilder, count); } for (int i = 0; i < count; i++) { type.writeDouble(blockBuilder, element); } return blockBuilder.build(); }
Example 3
Source File: BenchmarkArrayAggregation.java From presto with Apache License 2.0 | 6 votes |
private static Block createChannel(int arraySize, Type elementType) { BlockBuilder blockBuilder = elementType.createBlockBuilder(null, arraySize); for (int i = 0; i < arraySize; i++) { if (elementType.getJavaType() == long.class) { elementType.writeLong(blockBuilder, (long) i); } else if (elementType.getJavaType() == double.class) { elementType.writeDouble(blockBuilder, ThreadLocalRandom.current().nextDouble()); } else if (elementType.getJavaType() == boolean.class) { elementType.writeBoolean(blockBuilder, ThreadLocalRandom.current().nextBoolean()); } else if (elementType.equals(VARCHAR)) { // make sure the size of a varchar is rather small; otherwise the aggregated slice may overflow elementType.writeSlice(blockBuilder, Slices.utf8Slice(Long.toString(ThreadLocalRandom.current().nextLong() % 100))); } else { throw new UnsupportedOperationException(); } } return blockBuilder.build(); }
Example 4
Source File: AvroColumnDecoder.java From presto with Apache License 2.0 | 5 votes |
private static void serializePrimitive(BlockBuilder blockBuilder, Object value, Type type, String columnName) { requireNonNull(blockBuilder, "parent blockBuilder is null"); if (value == null) { blockBuilder.appendNull(); return; } if (type instanceof BooleanType) { type.writeBoolean(blockBuilder, (Boolean) value); return; } if ((value instanceof Integer || value instanceof Long) && (type instanceof BigintType || type instanceof IntegerType || type instanceof SmallintType || type instanceof TinyintType)) { type.writeLong(blockBuilder, ((Number) value).longValue()); return; } if (type instanceof DoubleType) { type.writeDouble(blockBuilder, (Double) value); return; } if (type instanceof RealType) { type.writeLong(blockBuilder, floatToIntBits((Float) value)); return; } if (type instanceof VarcharType || type instanceof VarbinaryType) { type.writeSlice(blockBuilder, getSlice(value, type, columnName)); return; } throw new PrestoException(DECODER_CONVERSION_NOT_SUPPORTED, format("cannot decode object of '%s' as '%s' for column '%s'", value.getClass(), type, columnName)); }
Example 5
Source File: PinotSegmentPageSource.java From presto with Apache License 2.0 | 5 votes |
private void writeDoubleBlock(BlockBuilder blockBuilder, Type columnType, int columnIndex) { for (int i = 0; i < currentDataTable.getDataTable().getNumberOfRows(); i++) { columnType.writeDouble(blockBuilder, getDouble(i, columnIndex)); completedBytes += Double.BYTES; } }
Example 6
Source File: DoubleColumnReader.java From presto with Apache License 2.0 | 5 votes |
@Override protected void readValue(BlockBuilder blockBuilder, Type type) { if (definitionLevel == columnDescriptor.getMaxDefinitionLevel()) { type.writeDouble(blockBuilder, valuesReader.readDouble()); } else if (isValueNull()) { blockBuilder.appendNull(); } }
Example 7
Source File: BenchmarkArrayIntersect.java From presto with Apache License 2.0 | 5 votes |
private static Block createChannel(int positionCount, int arraySize, Type elementType) { ArrayType arrayType = new ArrayType(elementType); BlockBuilder blockBuilder = arrayType.createBlockBuilder(null, positionCount); for (int position = 0; position < positionCount; position++) { BlockBuilder entryBuilder = blockBuilder.beginBlockEntry(); for (int i = 0; i < arraySize; i++) { if (elementType.getJavaType() == long.class) { elementType.writeLong(entryBuilder, ThreadLocalRandom.current().nextLong() % arraySize); } else if (elementType.getJavaType() == double.class) { elementType.writeDouble(entryBuilder, ThreadLocalRandom.current().nextDouble() % arraySize); } else if (elementType.getJavaType() == boolean.class) { elementType.writeBoolean(entryBuilder, ThreadLocalRandom.current().nextBoolean()); } else if (elementType.equals(VARCHAR)) { // make sure the size of a varchar is rather small; otherwise the aggregated slice may overflow elementType.writeSlice(entryBuilder, Slices.utf8Slice(Long.toString(ThreadLocalRandom.current().nextLong() % arraySize))); } else { throw new UnsupportedOperationException(); } } blockBuilder.closeEntry(); } return blockBuilder.build(); }
Example 8
Source File: AbstractTestApproximateCountDistinct.java From presto with Apache License 2.0 | 5 votes |
/** * Produce a block with the given values in the last field. */ private static Block createBlock(Type type, List<?> values) { BlockBuilder blockBuilder = type.createBlockBuilder(null, values.size()); for (Object value : values) { Class<?> javaType = type.getJavaType(); if (value == null) { blockBuilder.appendNull(); } else if (javaType == boolean.class) { type.writeBoolean(blockBuilder, (Boolean) value); } else if (javaType == long.class) { type.writeLong(blockBuilder, (Long) value); } else if (javaType == double.class) { type.writeDouble(blockBuilder, (Double) value); } else if (javaType == Slice.class) { Slice slice = (Slice) value; type.writeSlice(blockBuilder, slice, 0, slice.length()); } else { type.writeObject(blockBuilder, value); } } return blockBuilder.build(); }
Example 9
Source File: ArrayConcatUtils.java From presto with Apache License 2.0 | 5 votes |
@UsedByGeneratedCode public static Block prependElement(Type elementType, double value, Block block) { BlockBuilder blockBuilder = elementType.createBlockBuilder(null, block.getPositionCount() + 1); elementType.writeDouble(blockBuilder, value); for (int i = 0; i < block.getPositionCount(); i++) { elementType.appendTo(block, i, blockBuilder); } return blockBuilder.build(); }
Example 10
Source File: ArrayConcatUtils.java From presto with Apache License 2.0 | 5 votes |
@UsedByGeneratedCode public static Block appendElement(Type elementType, Block block, double value) { BlockBuilder blockBuilder = elementType.createBlockBuilder(null, block.getPositionCount() + 1); for (int i = 0; i < block.getPositionCount(); i++) { elementType.appendTo(block, i, blockBuilder); } elementType.writeDouble(blockBuilder, value); return blockBuilder.build(); }
Example 11
Source File: NullableDoubleState.java From presto with Apache License 2.0 | 5 votes |
static void write(Type type, NullableDoubleState state, BlockBuilder out) { if (state.isNull()) { out.appendNull(); } else { type.writeDouble(out, state.getDouble()); } }
Example 12
Source File: PrestoThriftBlock.java From presto with Apache License 2.0 | 5 votes |
private static Block convertColumnToBlock(RecordSet recordSet, int columnIndex, int positions) { Type type = recordSet.getColumnTypes().get(columnIndex); BlockBuilder output = type.createBlockBuilder(null, positions); Class<?> javaType = type.getJavaType(); RecordCursor cursor = recordSet.cursor(); for (int position = 0; position < positions; position++) { checkState(cursor.advanceNextPosition(), "cursor has less values than expected"); if (cursor.isNull(columnIndex)) { output.appendNull(); } else { if (javaType == boolean.class) { type.writeBoolean(output, cursor.getBoolean(columnIndex)); } else if (javaType == long.class) { type.writeLong(output, cursor.getLong(columnIndex)); } else if (javaType == double.class) { type.writeDouble(output, cursor.getDouble(columnIndex)); } else if (javaType == Slice.class) { Slice slice = cursor.getSlice(columnIndex); type.writeSlice(output, slice, 0, slice.length()); } else { type.writeObject(output, cursor.getObject(columnIndex)); } } } checkState(!cursor.advanceNextPosition(), "cursor has more values than expected"); return output.build(); }
Example 13
Source File: MongoPageSource.java From presto with Apache License 2.0 | 4 votes |
private void appendTo(Type type, Object value, BlockBuilder output) { if (value == null) { output.appendNull(); return; } Class<?> javaType = type.getJavaType(); try { if (javaType == boolean.class) { type.writeBoolean(output, (Boolean) value); } else if (javaType == long.class) { if (type.equals(BIGINT)) { type.writeLong(output, ((Number) value).longValue()); } else if (type.equals(INTEGER)) { type.writeLong(output, ((Number) value).intValue()); } else if (type.equals(SMALLINT)) { type.writeLong(output, Shorts.checkedCast(((Number) value).longValue())); } else if (type.equals(TINYINT)) { type.writeLong(output, SignedBytes.checkedCast(((Number) value).longValue())); } else if (type.equals(REAL)) { //noinspection NumericCastThatLosesPrecision type.writeLong(output, floatToIntBits(((float) ((Number) value).doubleValue()))); } else if (type instanceof DecimalType) { type.writeLong(output, encodeShortScaledValue(((Decimal128) value).bigDecimalValue(), ((DecimalType) type).getScale())); } else if (type.equals(DATE)) { long utcMillis = ((Date) value).getTime(); type.writeLong(output, TimeUnit.MILLISECONDS.toDays(utcMillis)); } else if (type.equals(TIME)) { type.writeLong(output, UTC_CHRONOLOGY.millisOfDay().get(((Date) value).getTime())); } else if (type.equals(TIMESTAMP)) { // TODO provide correct TIMESTAMP mapping, and respecting session.isLegacyTimestamp() type.writeLong(output, ((Date) value).getTime()); } else if (type.equals(TIMESTAMP_WITH_TIME_ZONE)) { type.writeLong(output, packDateTimeWithZone(((Date) value).getTime(), UTC_KEY)); } else { throw new PrestoException(GENERIC_INTERNAL_ERROR, "Unhandled type for " + javaType.getSimpleName() + ":" + type.getTypeSignature()); } } else if (javaType == double.class) { type.writeDouble(output, ((Number) value).doubleValue()); } else if (javaType == Slice.class) { writeSlice(output, type, value); } else if (javaType == Block.class) { writeBlock(output, type, value); } else { throw new PrestoException(GENERIC_INTERNAL_ERROR, "Unhandled type for " + javaType.getSimpleName() + ":" + type.getTypeSignature()); } } catch (ClassCastException ignore) { // TODO remove (fail clearly), or hide behind a toggle // returns null instead of raising exception output.appendNull(); } }
Example 14
Source File: LazyRecordPageSource.java From presto with Apache License 2.0 | 4 votes |
@Override public Page getNextPage() { if (!closed) { for (int i = 0; i < ROWS_PER_REQUEST && !pageBuilder.isFull() && pageBuilder.getPositionCount() < maxRowsPerPage; i++) { if (!cursor.advanceNextPosition()) { closed = true; break; } pageBuilder.declarePosition(); for (int column = 0; column < types.size(); column++) { BlockBuilder output = pageBuilder.getBlockBuilder(column); if (cursor.isNull(column)) { output.appendNull(); } else { Type type = types.get(column); Class<?> javaType = type.getJavaType(); if (javaType == boolean.class) { type.writeBoolean(output, cursor.getBoolean(column)); } else if (javaType == long.class) { type.writeLong(output, cursor.getLong(column)); } else if (javaType == double.class) { type.writeDouble(output, cursor.getDouble(column)); } else if (javaType == Slice.class) { Slice slice = cursor.getSlice(column); type.writeSlice(output, slice, 0, slice.length()); } else { type.writeObject(output, cursor.getObject(column)); } } } } } if ((closed && !pageBuilder.isEmpty()) || pageBuilder.isFull() || pageBuilder.getPositionCount() >= maxRowsPerPage) { Page page = pageBuilder.build(); pageBuilder.reset(); return lazyWrapper(page); } return null; }
Example 15
Source File: RecordPageSource.java From presto with Apache License 2.0 | 4 votes |
@Override public Page getNextPage() { if (!closed) { for (int i = 0; i < ROWS_PER_REQUEST && !pageBuilder.isFull(); i++) { if (!cursor.advanceNextPosition()) { closed = true; break; } pageBuilder.declarePosition(); for (int column = 0; column < types.size(); column++) { BlockBuilder output = pageBuilder.getBlockBuilder(column); if (cursor.isNull(column)) { output.appendNull(); } else { Type type = types.get(column); Class<?> javaType = type.getJavaType(); if (javaType == boolean.class) { type.writeBoolean(output, cursor.getBoolean(column)); } else if (javaType == long.class) { type.writeLong(output, cursor.getLong(column)); } else if (javaType == double.class) { type.writeDouble(output, cursor.getDouble(column)); } else if (javaType == Slice.class) { Slice slice = cursor.getSlice(column); type.writeSlice(output, slice, 0, slice.length()); } else { type.writeObject(output, cursor.getObject(column)); } } } } } // only return a page if the buffer is full or we are finishing if ((closed && !pageBuilder.isEmpty()) || pageBuilder.isFull()) { Page page = pageBuilder.build(); pageBuilder.reset(); return page; } return null; }
Example 16
Source File: BigQueryResultPageSource.java From presto with Apache License 2.0 | 4 votes |
private void appendTo(Type type, Object value, BlockBuilder output) { if (value == null) { output.appendNull(); return; } Class<?> javaType = type.getJavaType(); try { if (javaType == boolean.class) { type.writeBoolean(output, (Boolean) value); } else if (javaType == long.class) { if (type.equals(BIGINT)) { type.writeLong(output, ((Number) value).longValue()); } else if (type.equals(INTEGER)) { type.writeLong(output, ((Number) value).intValue()); } else if (type.equals(DATE)) { type.writeLong(output, ((Number) value).intValue()); } else if (type.equals(TIMESTAMP)) { type.writeLong(output, toPrestoTimestamp(((Utf8) value).toString())); } else if (type.equals(TIME_WITH_TIME_ZONE)) { type.writeLong(output, DateTimeEncoding.packDateTimeWithZone(((Long) value).longValue() / 1000, TimeZoneKey.UTC_KEY)); } else if (type.equals(TIMESTAMP_WITH_TIME_ZONE)) { type.writeLong(output, DateTimeEncoding.packDateTimeWithZone(((Long) value).longValue() / 1000, TimeZoneKey.UTC_KEY)); } else { throw new PrestoException(GENERIC_INTERNAL_ERROR, format("Unhandled type for %s: %s", javaType.getSimpleName(), type)); } } else if (javaType == double.class) { type.writeDouble(output, ((Number) value).doubleValue()); } else if (javaType == Slice.class) { writeSlice(output, type, value); } else if (javaType == Block.class) { writeBlock(output, type, value); } else { throw new PrestoException(GENERIC_INTERNAL_ERROR, format("Unhandled type for %s: %s", javaType.getSimpleName(), type)); } } catch (ClassCastException ignore) { // returns null instead of raising exception output.appendNull(); } }
Example 17
Source File: HiveCoercionRecordCursor.java From presto with Apache License 2.0 | 4 votes |
private static void rewriteBlock( Type fromType, Type toType, Block block, int position, BlockBuilder blockBuilder, Coercer coercer, BridgingRecordCursor bridgingRecordCursor) { Class<?> fromJavaType = fromType.getJavaType(); if (fromJavaType == long.class) { bridgingRecordCursor.setValue(fromType.getLong(block, position)); } else if (fromJavaType == double.class) { bridgingRecordCursor.setValue(fromType.getDouble(block, position)); } else if (fromJavaType == boolean.class) { bridgingRecordCursor.setValue(fromType.getBoolean(block, position)); } else if (fromJavaType == Slice.class) { bridgingRecordCursor.setValue(fromType.getSlice(block, position)); } else if (fromJavaType == Block.class) { bridgingRecordCursor.setValue(fromType.getObject(block, position)); } else { bridgingRecordCursor.setValue(null); } coercer.reset(); Class<?> toJaveType = toType.getJavaType(); if (coercer.isNull(bridgingRecordCursor, 0)) { blockBuilder.appendNull(); } else if (toJaveType == long.class) { toType.writeLong(blockBuilder, coercer.getLong(bridgingRecordCursor, 0)); } else if (toJaveType == double.class) { toType.writeDouble(blockBuilder, coercer.getDouble(bridgingRecordCursor, 0)); } else if (toJaveType == boolean.class) { toType.writeBoolean(blockBuilder, coercer.getBoolean(bridgingRecordCursor, 0)); } else if (toJaveType == Slice.class) { toType.writeSlice(blockBuilder, coercer.getSlice(bridgingRecordCursor, 0)); } else if (toJaveType == Block.class) { toType.writeObject(blockBuilder, coercer.getObject(bridgingRecordCursor, 0)); } else { throw new PrestoException(NOT_SUPPORTED, format("Unsupported coercion from %s to %s", fromType.getDisplayName(), toType.getDisplayName())); } coercer.reset(); bridgingRecordCursor.close(); }
Example 18
Source File: BlackHolePageSourceProvider.java From presto with Apache License 2.0 | 4 votes |
private Block createZeroBlock(Type type, int rowsCount, Slice constantSlice) { checkArgument(isSupportedType(type), "Unsupported type [%s]", type); Slice slice; // do not exceed varchar limit if (isVarcharType(type) && !((VarcharType) type).isUnbounded()) { slice = constantSlice.slice(0, Math.min(((VarcharType) type).getBoundedLength(), constantSlice.length())); } else if (isLongDecimal(type)) { slice = encodeScaledValue(ZERO); } else { slice = constantSlice; } BlockBuilder builder; if (type instanceof FixedWidthType) { builder = type.createBlockBuilder(null, rowsCount); } else { builder = type.createBlockBuilder(null, rowsCount, slice.length()); } for (int i = 0; i < rowsCount; i++) { Class<?> javaType = type.getJavaType(); if (javaType == boolean.class) { type.writeBoolean(builder, false); } else if (javaType == long.class) { type.writeLong(builder, 0); } else if (javaType == double.class) { type.writeDouble(builder, 0.0); } else if (javaType == Slice.class) { requireNonNull(slice, "slice is null"); type.writeSlice(builder, slice, 0, slice.length()); } else { throw new UnsupportedOperationException("Unknown javaType: " + javaType.getName()); } } return builder.build(); }