Java Code Examples for io.prestosql.spi.type.Type#writeLong()
The following examples show how to use
io.prestosql.spi.type.Type#writeLong() .
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: TimestampMicrosColumnReader.java From presto with Apache License 2.0 | 6 votes |
@Override protected void readValue(BlockBuilder blockBuilder, Type type) { if (definitionLevel == columnDescriptor.getMaxDefinitionLevel()) { long utcMillis = MICROSECONDS.toMillis(valuesReader.readLong()); // TODO: specialize the class at creation time if (type instanceof TimestampWithTimeZoneType) { type.writeLong(blockBuilder, packDateTimeWithZone(utcMillis, UTC_KEY)); } else { type.writeLong(blockBuilder, utcMillis); } } else if (isValueNull()) { blockBuilder.appendNull(); } }
Example 2
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 3
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") Long 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.writeLong(blockBuilder, element); } return blockBuilder.build(); }
Example 4
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 5
Source File: TimestampColumnReader.java From presto with Apache License 2.0 | 6 votes |
@Override protected void readValue(BlockBuilder blockBuilder, Type type) { if (definitionLevel == columnDescriptor.getMaxDefinitionLevel()) { long utcMillis = getTimestampMillis(valuesReader.readBytes()); if (type instanceof TimestampWithTimeZoneType) { type.writeLong(blockBuilder, packDateTimeWithZone(utcMillis, UTC_KEY)); } else { type.writeLong(blockBuilder, utcMillis); } } else if (isValueNull()) { blockBuilder.appendNull(); } }
Example 6
Source File: PinotSegmentPageSource.java From presto with Apache License 2.0 | 5 votes |
private void writeLongBlock(BlockBuilder blockBuilder, Type columnType, int columnIndex) { for (int i = 0; i < currentDataTable.getDataTable().getNumberOfRows(); i++) { columnType.writeLong(blockBuilder, getLong(i, columnIndex)); completedBytes += Long.BYTES; } }
Example 7
Source File: FloatColumnReader.java From presto with Apache License 2.0 | 5 votes |
@Override protected void readValue(BlockBuilder blockBuilder, Type type) { if (definitionLevel == columnDescriptor.getMaxDefinitionLevel()) { type.writeLong(blockBuilder, floatToRawIntBits(valuesReader.readFloat())); } else if (isValueNull()) { blockBuilder.appendNull(); } }
Example 8
Source File: LongColumnReader.java From presto with Apache License 2.0 | 5 votes |
@Override protected void readValue(BlockBuilder blockBuilder, Type type) { if (definitionLevel == columnDescriptor.getMaxDefinitionLevel()) { type.writeLong(blockBuilder, valuesReader.readLong()); } else if (isValueNull()) { blockBuilder.appendNull(); } }
Example 9
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 10
Source File: LongDecimalColumnReader.java From presto with Apache License 2.0 | 5 votes |
@Override protected void readValue(BlockBuilder blockBuilder, Type prestoType) { if (!(prestoType instanceof DecimalType)) { throw new ParquetDecodingException(format("Unsupported Presto column type (%s) for Parquet column (%s)", prestoType, columnDescriptor)); } DecimalType prestoDecimalType = (DecimalType) prestoType; if (definitionLevel == columnDescriptor.getMaxDefinitionLevel()) { Binary binary = valuesReader.readBytes(); Slice value = Decimals.encodeUnscaledValue(new BigInteger(binary.getBytes())); if (prestoDecimalType.isShort()) { prestoType.writeLong(blockBuilder, longToShortCast( value, parquetDecimalType.getPrecision(), parquetDecimalType.getScale(), prestoDecimalType.getPrecision(), prestoDecimalType.getScale())); } else { prestoType.writeSlice(blockBuilder, longToLongCast( value, parquetDecimalType.getPrecision(), parquetDecimalType.getScale(), prestoDecimalType.getPrecision(), prestoDecimalType.getScale())); } } else if (isValueNull()) { blockBuilder.appendNull(); } }
Example 11
Source File: ArrayConcatUtils.java From presto with Apache License 2.0 | 5 votes |
@UsedByGeneratedCode public static Block prependElement(Type elementType, long value, Block block) { BlockBuilder blockBuilder = elementType.createBlockBuilder(null, block.getPositionCount() + 1); elementType.writeLong(blockBuilder, value); for (int i = 0; i < block.getPositionCount(); i++) { elementType.appendTo(block, i, blockBuilder); } return blockBuilder.build(); }
Example 12
Source File: ArrayConcatUtils.java From presto with Apache License 2.0 | 5 votes |
@UsedByGeneratedCode public static Block appendElement(Type elementType, Block block, long value) { BlockBuilder blockBuilder = elementType.createBlockBuilder(null, block.getPositionCount() + 1); for (int i = 0; i < block.getPositionCount(); i++) { elementType.appendTo(block, i, blockBuilder); } elementType.writeLong(blockBuilder, value); return blockBuilder.build(); }
Example 13
Source File: NullableLongState.java From presto with Apache License 2.0 | 5 votes |
static void write(Type type, NullableLongState state, BlockBuilder out) { if (state.isNull()) { out.appendNull(); } else { type.writeLong(out, state.getLong()); } }
Example 14
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 15
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(); }
Example 16
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 17
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 18
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 19
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 20
Source File: ShortDecimalColumnReader.java From presto with Apache License 2.0 | 4 votes |
@Override protected void readValue(BlockBuilder blockBuilder, Type prestoType) { if (definitionLevel == columnDescriptor.getMaxDefinitionLevel()) { if (!((prestoType instanceof DecimalType) || isIntegerType(prestoType))) { throw new ParquetDecodingException(format("Unsupported Presto column type (%s) for Parquet column (%s)", prestoType, columnDescriptor)); } long value; // When decimals are encoded with primitive types Parquet stores unscaled values if (columnDescriptor.getPrimitiveType().getPrimitiveTypeName() == INT32) { value = valuesReader.readInteger(); } else if (columnDescriptor.getPrimitiveType().getPrimitiveTypeName() == INT64) { value = valuesReader.readLong(); } else { value = getShortDecimalValue(valuesReader.readBytes().getBytes()); } if (prestoType instanceof DecimalType) { DecimalType prestoDecimalType = (DecimalType) prestoType; if (isShortDecimal(prestoDecimalType)) { long rescale = longTenToNth(Math.abs(prestoDecimalType.getScale() - parquetDecimalType.getScale())); long convertedValue = shortToShortCast( value, parquetDecimalType.getPrecision(), parquetDecimalType.getScale(), prestoDecimalType.getPrecision(), prestoDecimalType.getScale(), rescale, rescale / 2); prestoType.writeLong(blockBuilder, convertedValue); } else if (isLongDecimal(prestoDecimalType)) { prestoType.writeSlice(blockBuilder, shortToLongCast( value, parquetDecimalType.getPrecision(), parquetDecimalType.getScale(), prestoDecimalType.getPrecision(), prestoDecimalType.getScale())); } } else { if (parquetDecimalType.getScale() != 0) { throw new PrestoException(NOT_SUPPORTED, format("Unsupported Presto column type (%s) for Parquet column (%s)", prestoType, columnDescriptor)); } if (!isInValidNumberRange(prestoType, value)) { throw new PrestoException(NOT_SUPPORTED, format("Could not coerce from %s to %s: %s", parquetDecimalType, prestoType, value)); } prestoType.writeLong(blockBuilder, value); } } else if (isValueNull()) { blockBuilder.appendNull(); } }