Java Code Examples for io.prestosql.spi.connector.RecordCursor#isNull()

The following examples show how to use io.prestosql.spi.connector.RecordCursor#isNull() . 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: PrestoThriftTypeUtils.java    From presto with Apache License 2.0 6 votes vote down vote up
public static PrestoThriftBlock fromLongBasedColumn(RecordSet recordSet, int columnIndex, int positions, BiFunction<boolean[], long[], PrestoThriftBlock> result)
{
    if (positions == 0) {
        return result.apply(null, null);
    }
    boolean[] nulls = null;
    long[] longs = null;
    RecordCursor cursor = recordSet.cursor();
    for (int position = 0; position < positions; position++) {
        checkState(cursor.advanceNextPosition(), "cursor has less values than expected");
        if (cursor.isNull(columnIndex)) {
            if (nulls == null) {
                nulls = new boolean[positions];
            }
            nulls[position] = true;
        }
        else {
            if (longs == null) {
                longs = new long[positions];
            }
            longs[position] = cursor.getLong(columnIndex);
        }
    }
    checkState(!cursor.advanceNextPosition(), "cursor has more values than expected");
    return result.apply(nulls, longs);
}
 
Example 2
Source File: PrestoThriftTypeUtils.java    From presto with Apache License 2.0 6 votes vote down vote up
public static PrestoThriftBlock fromIntBasedColumn(RecordSet recordSet, int columnIndex, int positions, BiFunction<boolean[], int[], PrestoThriftBlock> result)
{
    if (positions == 0) {
        return result.apply(null, null);
    }
    boolean[] nulls = null;
    int[] ints = null;
    RecordCursor cursor = recordSet.cursor();
    for (int position = 0; position < positions; position++) {
        checkState(cursor.advanceNextPosition(), "cursor has less values than expected");
        if (cursor.isNull(columnIndex)) {
            if (nulls == null) {
                nulls = new boolean[positions];
            }
            nulls[position] = true;
        }
        else {
            if (ints == null) {
                ints = new int[positions];
            }
            ints[position] = (int) cursor.getLong(columnIndex);
        }
    }
    checkState(!cursor.advanceNextPosition(), "cursor has more values than expected");
    return result.apply(nulls, ints);
}
 
Example 3
Source File: TableStatisticsRecorder.java    From presto with Apache License 2.0 6 votes vote down vote up
private Comparable<?> getPrestoValue(RecordCursor recordCursor, List<Column> columns, int columnId)
{
    if (recordCursor.isNull(columnId)) {
        return null;
    }

    Column column = columns.get(columnId);
    ColumnType.Base baseType = column.getType().getBase();
    switch (baseType) {
        case IDENTIFIER:
        case INTEGER:
        case DATE:
        case TIME:
        case DECIMAL:
            return recordCursor.getLong(columnId);
        case VARCHAR:
        case CHAR:
            return recordCursor.getSlice(columnId).toStringAscii();
    }
    throw new UnsupportedOperationException(format("Unsupported TPCDS base type [%s]", baseType));
}
 
Example 4
Source File: TpchIndexedData.java    From presto with Apache License 2.0 6 votes vote down vote up
private static Object extractObject(RecordCursor cursor, int field, Type type)
{
    if (cursor.isNull(field)) {
        return null;
    }

    Class<?> javaType = type.getJavaType();
    if (javaType == boolean.class) {
        return cursor.getBoolean(field);
    }
    if (javaType == long.class) {
        return cursor.getLong(field);
    }
    if (javaType == double.class) {
        return cursor.getDouble(field);
    }
    if (javaType == Slice.class) {
        return cursor.getSlice(field).toStringUtf8();
    }
    throw new AssertionError("Unsupported type: " + type);
}
 
Example 5
Source File: PrestoThriftBlock.java    From presto with Apache License 2.0 5 votes vote down vote up
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 6
Source File: FieldSetFilteringRecordSet.java    From presto with Apache License 2.0 5 votes vote down vote up
private static boolean fieldEquals(RecordCursor cursor, Field field1, Field field2)
{
    checkArgument(cursor.getType(field1.getField()).equals(cursor.getType(field2.getField())), "Should only be comparing fields of the same type");

    if (cursor.isNull(field1.getField()) || cursor.isNull(field2.getField())) {
        return false;
    }

    Class<?> javaType = cursor.getType(field1.getField()).getJavaType();
    try {
        if (javaType == long.class) {
            return TRUE.equals((Boolean) field1.getEqualsMethodHandle().invokeExact(cursor.getLong(field1.getField()), cursor.getLong(field2.getField())));
        }
        if (javaType == double.class) {
            return TRUE.equals((Boolean) field1.getEqualsMethodHandle().invokeExact(cursor.getDouble(field1.getField()), cursor.getDouble(field2.getField())));
        }
        if (javaType == boolean.class) {
            return TRUE.equals((Boolean) field1.getEqualsMethodHandle().invokeExact(cursor.getBoolean(field1.getField()), cursor.getBoolean(field2.getField())));
        }
        if (javaType == Slice.class) {
            return TRUE.equals((Boolean) field1.getEqualsMethodHandle().invokeExact(cursor.getSlice(field1.getField()), cursor.getSlice(field2.getField())));
        }
        return TRUE.equals((Boolean) field1.getEqualsMethodHandle().invoke(cursor.getObject(field1.getField()), cursor.getObject(field2.getField())));
    }
    catch (Throwable t) {
        throwIfUnchecked(t);
        throw new RuntimeException(t);
    }
}
 
Example 7
Source File: TestShardMetadataRecordCursor.java    From presto with Apache License 2.0 5 votes vote down vote up
private static List<MaterializedRow> getMaterializedResults(RecordCursor cursor, List<ColumnMetadata> columns)
{
    List<Type> types = columns.stream().map(ColumnMetadata::getType).collect(toList());

    ImmutableList.Builder<MaterializedRow> rowBuilder = ImmutableList.builder();
    for (int i = 0; i < types.size(); i++) {
        assertEquals(cursor.getType(i), types.get(i));
    }

    while (cursor.advanceNextPosition()) {
        List<Object> values = new ArrayList<>();
        for (int i = 0; i < columns.size(); i++) {
            Type type = columns.get(i).getType();
            Class<?> javaType = type.getJavaType();
            if (cursor.isNull(i)) {
                values.add(null);
            }
            else if (javaType == boolean.class) {
                values.add(cursor.getBoolean(i));
            }
            else if (javaType == long.class) {
                values.add(cursor.getLong(i));
            }
            else if (javaType == double.class) {
                values.add(cursor.getDouble(i));
            }
            else if (javaType == Slice.class) {
                values.add(cursor.getSlice(i));
            }
        }
        rowBuilder.add(new MaterializedRow(DEFAULT_PRECISION, values));
    }
    return rowBuilder.build();
}
 
Example 8
Source File: TestCassandraConnector.java    From presto with Apache License 2.0 5 votes vote down vote up
private static void assertReadFields(RecordCursor cursor, List<ColumnMetadata> schema)
{
    for (int columnIndex = 0; columnIndex < schema.size(); columnIndex++) {
        ColumnMetadata column = schema.get(columnIndex);
        if (!cursor.isNull(columnIndex)) {
            Type type = column.getType();
            if (BOOLEAN.equals(type)) {
                cursor.getBoolean(columnIndex);
            }
            else if (INTEGER.equals(type)) {
                cursor.getLong(columnIndex);
            }
            else if (BIGINT.equals(type)) {
                cursor.getLong(columnIndex);
            }
            else if (TIMESTAMP.equals(type)) {
                cursor.getLong(columnIndex);
            }
            else if (DOUBLE.equals(type)) {
                cursor.getDouble(columnIndex);
            }
            else if (REAL.equals(type)) {
                cursor.getLong(columnIndex);
            }
            else if (isVarcharType(type) || VARBINARY.equals(type)) {
                try {
                    cursor.getSlice(columnIndex);
                }
                catch (RuntimeException e) {
                    throw new RuntimeException("column " + column, e);
                }
            }
            else {
                fail("Unknown primitive type " + columnIndex);
            }
        }
    }
}
 
Example 9
Source File: HiveCoercionRecordCursor.java    From presto with Apache License 2.0 5 votes vote down vote up
private void assureLoaded(RecordCursor delegate, int field)
{
    if (!loaded) {
        isNull = delegate.isNull(field);
        if (!isNull) {
            coerce(delegate, field);
        }
        loaded = true;
    }
}
 
Example 10
Source File: HiveCoercionRecordCursor.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
public void coerce(RecordCursor delegate, int field)
{
    if (delegate.isNull(field)) {
        setIsNull(true);
        return;
    }
    Block block = (Block) delegate.getObject(field);
    if (pageBuilder.isFull()) {
        pageBuilder.reset();
    }
    BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(0);
    BlockBuilder listBuilder = blockBuilder.beginBlockEntry();
    for (int i = 0; i < block.getPositionCount(); i++) {
        if (elementCoercer == null) {
            toElementType.appendTo(block, i, listBuilder);
        }
        else {
            if (block.isNull(i)) {
                listBuilder.appendNull();
            }
            else {
                rewriteBlock(fromElementType, toElementType, block, i, listBuilder, elementCoercer, bridgingRecordCursor);
            }
        }
    }
    blockBuilder.closeEntry();
    pageBuilder.declarePosition();
    setObject(toType.getObject(blockBuilder, blockBuilder.getPositionCount() - 1));
}
 
Example 11
Source File: HiveCoercionRecordCursor.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
public void coerce(RecordCursor delegate, int field)
{
    if (delegate.isNull(field)) {
        setIsNull(true);
        return;
    }
    Block block = (Block) delegate.getObject(field);
    if (pageBuilder.isFull()) {
        pageBuilder.reset();
    }
    BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(0);
    BlockBuilder mapBuilder = blockBuilder.beginBlockEntry();
    for (int i = 0; i < block.getPositionCount(); i++) {
        int k = i % 2;
        if (coercers[k] == null) {
            toKeyValueTypes.get(k).appendTo(block, i, mapBuilder);
        }
        else {
            if (block.isNull(i)) {
                mapBuilder.appendNull();
            }
            else {
                rewriteBlock(fromKeyValueTypes.get(k), toKeyValueTypes.get(k), block, i, mapBuilder, coercers[k], bridgingRecordCursor);
            }
        }
    }
    blockBuilder.closeEntry();
    pageBuilder.declarePosition();
    setObject(toType.getObject(blockBuilder, blockBuilder.getPositionCount() - 1));
}
 
Example 12
Source File: HiveCoercionRecordCursor.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
public void coerce(RecordCursor delegate, int field)
{
    if (delegate.isNull(field)) {
        setIsNull(true);
        return;
    }
    Block block = (Block) delegate.getObject(field);
    if (pageBuilder.isFull()) {
        pageBuilder.reset();
    }
    BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(0);
    BlockBuilder rowBuilder = blockBuilder.beginBlockEntry();
    for (int i = 0; i < toFieldTypes.size(); i++) {
        if (i >= fromFieldTypes.size() || block.isNull(i)) {
            rowBuilder.appendNull();
        }
        else if (coercers[i] == null) {
            toFieldTypes.get(i).appendTo(block, i, rowBuilder);
        }
        else {
            rewriteBlock(fromFieldTypes.get(i), toFieldTypes.get(i), block, i, rowBuilder, coercers[i], bridgingRecordCursor);
        }
    }
    blockBuilder.closeEntry();
    pageBuilder.declarePosition();
    setObject(toType.getObject(blockBuilder, blockBuilder.getPositionCount() - 1));
}
 
Example 13
Source File: AbstractTestHiveFileFormats.java    From presto with Apache License 2.0 4 votes vote down vote up
public static Object getFieldFromCursor(RecordCursor cursor, Type type, int field)
{
    if (cursor.isNull(field)) {
        return null;
    }
    if (BOOLEAN.equals(type)) {
        return cursor.getBoolean(field);
    }
    if (TINYINT.equals(type)) {
        return cursor.getLong(field);
    }
    if (SMALLINT.equals(type)) {
        return cursor.getLong(field);
    }
    if (INTEGER.equals(type)) {
        return (int) cursor.getLong(field);
    }
    if (BIGINT.equals(type)) {
        return cursor.getLong(field);
    }
    if (REAL.equals(type)) {
        return intBitsToFloat((int) cursor.getLong(field));
    }
    if (DOUBLE.equals(type)) {
        return cursor.getDouble(field);
    }
    if (isVarcharType(type) || isCharType(type) || VARBINARY.equals(type)) {
        return cursor.getSlice(field);
    }
    if (DateType.DATE.equals(type)) {
        return cursor.getLong(field);
    }
    if (TimestampType.TIMESTAMP.equals(type)) {
        return cursor.getLong(field);
    }
    if (isStructuralType(type)) {
        return cursor.getObject(field);
    }
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        if (decimalType.isShort()) {
            return BigInteger.valueOf(cursor.getLong(field));
        }
        else {
            return Decimals.decodeUnscaledValue(cursor.getSlice(field));
        }
    }
    throw new RuntimeException("unknown type");
}