Java Code Examples for io.prestosql.spi.type.Type#getObjectValue()

The following examples show how to use io.prestosql.spi.type.Type#getObjectValue() . 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: TestStructColumnReader.java    From presto with Apache License 2.0 6 votes vote down vote up
/**
 * Reader and writer have the same fields. Checks that fields are read in correctly
 */
@Test
public void testValuesAreReadInCorrectly()
        throws IOException
{
    List<String> readerFields = new ArrayList<>(Arrays.asList("field_a", "field_b", "field_c"));
    List<String> writerFields = new ArrayList<>(Arrays.asList("field_a", "field_b", "field_c"));
    List<String> writerData = new ArrayList<>(Arrays.asList("field_a_value", "field_b_value", "field_c_value"));
    Type readerType = getType(readerFields);
    Type writerType = getType(writerFields);

    write(tempFile, writerType, writerData);
    RowBlock readBlock = read(tempFile, readerType);
    List<?> actual = (List<?>) readerType.getObjectValue(TestingConnectorSession.SESSION, readBlock, 0);

    assertEquals(actual.size(), readerFields.size());
    assertEquals(actual.get(0), "field_a_value");
    assertEquals(actual.get(1), "field_b_value");
    assertEquals(actual.get(2), "field_c_value");
}
 
Example 2
Source File: TestStructColumnReader.java    From presto with Apache License 2.0 6 votes vote down vote up
/**
 * The writer has fields with upper case characters, reader has same names downcased.
 */
@Test
public void testReaderLowerCasesFieldNamesFromStream()
        throws IOException
{
    List<String> readerFields = new ArrayList<>(Arrays.asList("field_a", "field_b", "field_c"));
    List<String> writerFields = new ArrayList<>(Arrays.asList("field_A", "field_B", "field_C"));
    List<String> writerData = new ArrayList<>(Arrays.asList("fieldAValue", "fieldBValue", "fieldCValue"));
    Type readerType = getType(readerFields);
    Type writerType = getType(writerFields);

    write(tempFile, writerType, writerData);
    RowBlock readBlock = read(tempFile, readerType);
    List<?> actual = (List<?>) readerType.getObjectValue(TestingConnectorSession.SESSION, readBlock, 0);

    assertEquals(actual.size(), readerFields.size());
    assertEquals(actual.get(0), "fieldAValue");
    assertEquals(actual.get(1), "fieldBValue");
    assertEquals(actual.get(2), "fieldCValue");
}
 
Example 3
Source File: TestStructColumnReader.java    From presto with Apache License 2.0 6 votes vote down vote up
/**
 * Reader has fields with upper case characters, writer has same names downcased.
 */
@Test
public void testReaderLowerCasesFieldNamesFromType()
        throws IOException
{
    List<String> readerFields = new ArrayList<>(Arrays.asList("field_A", "field_B", "field_C"));
    List<String> writerFields = new ArrayList<>(Arrays.asList("field_a", "field_b", "field_c"));
    List<String> writerData = new ArrayList<>(Arrays.asList("fieldAValue", "fieldBValue", "fieldCValue"));
    Type readerType = getType(readerFields);
    Type writerType = getType(writerFields);

    write(tempFile, writerType, writerData);
    RowBlock readBlock = read(tempFile, readerType);
    List<?> actual = (List<?>) readerType.getObjectValue(TestingConnectorSession.SESSION, readBlock, 0);

    assertEquals(actual.size(), readerFields.size());
    assertEquals(actual.get(0), "fieldAValue");
    assertEquals(actual.get(1), "fieldBValue");
    assertEquals(actual.get(2), "fieldCValue");
}
 
Example 4
Source File: TestStructColumnReader.java    From presto with Apache License 2.0 6 votes vote down vote up
/**
 * The reader has a field that is missing from the ORC file
 */
@Test
public void testExtraFieldsInReader()
        throws IOException
{
    List<String> readerFields = new ArrayList<>(Arrays.asList("field_a", "field_b", "field_c"));

    // field_b is missing
    List<String> writerFields = new ArrayList<>(Arrays.asList("field_a", "field_c"));
    List<String> writerData = new ArrayList<>(Arrays.asList("field_a_value", "field_c_value"));
    Type readerType = getType(readerFields);
    Type writerType = getType(writerFields);

    write(tempFile, writerType, writerData);
    RowBlock readBlock = read(tempFile, readerType);
    List<?> actual = (List<?>) readerType.getObjectValue(TestingConnectorSession.SESSION, readBlock, 0);

    assertEquals(actual.size(), readerFields.size());
    assertEquals(actual.get(0), "field_a_value");
    assertNull(actual.get(1));
    assertEquals(actual.get(2), "field_c_value");
}
 
Example 5
Source File: TestStructColumnReader.java    From presto with Apache License 2.0 6 votes vote down vote up
/**
 * The ORC file has a field that is missing from the reader
 */
@Test
public void testExtraFieldsInWriter()
        throws IOException
{
    // field_b is missing
    List<String> readerFields = new ArrayList<>(Arrays.asList("field_a", "field_c"));
    List<String> writerFields = new ArrayList<>(Arrays.asList("field_a", "field_b", "field_c"));
    List<String> writerData = new ArrayList<>(Arrays.asList("field_a_value", "field_b_value", "field_c_value"));
    Type readerType = getType(readerFields);
    Type writerType = getType(writerFields);

    write(tempFile, writerType, writerData);
    RowBlock readBlock = read(tempFile, readerType);
    List<?> actual = (List<?>) readerType.getObjectValue(TestingConnectorSession.SESSION, readBlock, 0);

    assertEquals(actual.size(), readerFields.size());
    assertEquals(actual.get(0), "field_a_value");
    assertEquals(actual.get(1), "field_c_value");
}
 
Example 6
Source File: AbstractPropertyManager.java    From presto with Apache License 2.0 6 votes vote down vote up
private Object evaluatePropertyValue(
        Expression expression,
        Type expectedType,
        Session session,
        Metadata metadata,
        AccessControl accessControl,
        Map<NodeRef<Parameter>, Expression> parameters)
{
    Expression rewritten = ExpressionTreeRewriter.rewriteWith(new ParameterRewriter(parameters), expression);
    Object value = evaluateConstantExpression(rewritten, expectedType, metadata, session, accessControl, parameters);

    // convert to object value type of SQL type
    BlockBuilder blockBuilder = expectedType.createBlockBuilder(null, 1);
    writeNativeValue(expectedType, blockBuilder, value);
    Object objectValue = expectedType.getObjectValue(session.toConnectorSession(), blockBuilder, 0);

    if (objectValue == null) {
        throw new PrestoException(propertyError, format("Invalid null value for %s property", propertyType));
    }
    return objectValue;
}
 
Example 7
Source File: PostgreSqlClient.java    From presto with Apache License 2.0 5 votes vote down vote up
private static SliceReadFunction arrayAsJsonReadFunction(ConnectorSession session, ColumnMapping baseElementMapping)
{
    return (resultSet, columnIndex) -> {
        // resolve array type
        Object jdbcArray = resultSet.getArray(columnIndex).getArray();
        int arrayDimensions = arrayDepth(jdbcArray);

        ReadFunction readFunction = baseElementMapping.getReadFunction();
        Type type = baseElementMapping.getType();
        for (int i = 0; i < arrayDimensions; i++) {
            readFunction = arrayReadFunction(type, readFunction);
            type = new ArrayType(type);
        }

        // read array into a block
        Block block = (Block) ((ObjectReadFunction) readFunction).readObject(resultSet, columnIndex);

        // convert block to JSON slice
        BlockBuilder builder = type.createBlockBuilder(null, 1);
        type.writeObject(builder, block);
        Object value = type.getObjectValue(session, builder.build(), 0);

        try {
            return Slices.wrappedBuffer(SORTED_MAPPER.writeValueAsBytes(value));
        }
        catch (JsonProcessingException e) {
            throw new PrestoException(JDBC_ERROR, "Conversion to JSON failed for  " + type.getDisplayName(), e);
        }
    };
}
 
Example 8
Source File: JsonFunctions.java    From presto with Apache License 2.0 5 votes vote down vote up
public static Object getJsonObjectValue(Type valueType, ConnectorSession session, Block block, int position)
{
    Object objectValue = valueType.getObjectValue(session, block, position);
    if (objectValue instanceof SqlDecimal) {
        objectValue = ((SqlDecimal) objectValue).toBigDecimal();
    }
    return objectValue;
}
 
Example 9
Source File: SessionPropertyManager.java    From presto with Apache License 2.0 5 votes vote down vote up
public static Object evaluatePropertyValue(Expression expression, Type expectedType, Session session, Metadata metadata, AccessControl accessControl, Map<NodeRef<Parameter>, Expression> parameters)
{
    Expression rewritten = ExpressionTreeRewriter.rewriteWith(new ParameterRewriter(parameters), expression);
    Object value = evaluateConstantExpression(rewritten, expectedType, metadata, session, accessControl, parameters);

    // convert to object value type of SQL type
    BlockBuilder blockBuilder = expectedType.createBlockBuilder(null, 1);
    writeNativeValue(expectedType, blockBuilder, value);
    Object objectValue = expectedType.getObjectValue(session.toConnectorSession(), blockBuilder, 0);

    if (objectValue == null) {
        throw new PrestoException(INVALID_SESSION_PROPERTY, "Session property value must not be null");
    }
    return objectValue;
}
 
Example 10
Source File: QueryResultRows.java    From presto with Apache License 2.0 5 votes vote down vote up
private Optional<List<Object>> getRowValues()
{
    // types are present if data is present
    List<ColumnAndType> columns = this.columns.orElseThrow();
    List<Object> row = new ArrayList<>(columns.size());

    for (int channel = 0; channel < currentPage.getChannelCount(); channel++) {
        ColumnAndType column = columns.get(channel);
        Type type = column.getType();
        Block block = currentPage.getBlock(channel);

        try {
            Object value = type.getObjectValue(session, block, inPageIndex);
            if (value != null && !supportsParametricDateTime) {
                if (type instanceof TimestampType) {
                    value = ((SqlTimestamp) value).roundTo(3);
                }
                else if (type instanceof TimestampWithTimeZoneType) {
                    value = ((SqlTimestampWithTimeZone) value).roundTo(3);
                }
            }
            row.add(channel, value);
        }
        catch (Throwable throwable) {
            propagateException(rowPosition, column, throwable);
            // skip row as it contains non-serializable value
            return Optional.empty();
        }
    }

    return Optional.of(unmodifiableList(row));
}
 
Example 11
Source File: FunctionAssertions.java    From presto with Apache License 2.0 5 votes vote down vote up
private Object selectSingleValue(Operator operator, Type type)
{
    Page output = getAtMostOnePage(operator, SOURCE_PAGE);

    assertNotNull(output);
    assertEquals(output.getPositionCount(), 1);
    assertEquals(output.getChannelCount(), 1);

    Block block = output.getBlock(0);
    assertEquals(block.getPositionCount(), 1);

    return type.getObjectValue(session.toConnectorSession(), block, 0);
}
 
Example 12
Source File: ParquetTester.java    From presto with Apache License 2.0 5 votes vote down vote up
private static Object decodeObject(Type type, Block block, int position)
{
    if (block.isNull(position)) {
        return null;
    }

    return type.getObjectValue(SESSION, block, position);
}
 
Example 13
Source File: AvroDecoderTestUtil.java    From presto with Apache License 2.0 5 votes vote down vote up
public static Object getObjectValue(Type type, Block block, int position)
{
    if (block.isNull(position)) {
        return null;
    }
    return type.getObjectValue(SESSION, block, position);
}
 
Example 14
Source File: CallTask.java    From presto with Apache License 2.0 4 votes vote down vote up
private static Object toTypeObjectValue(Session session, Type type, Object value)
{
    BlockBuilder blockBuilder = type.createBlockBuilder(null, 1);
    writeNativeValue(type, blockBuilder, value);
    return type.getObjectValue(session.toConnectorSession(), blockBuilder, 0);
}
 
Example 15
Source File: BlockAssertions.java    From presto with Apache License 2.0 4 votes vote down vote up
public static Object getOnlyValue(Type type, Block block)
{
    assertEquals(block.getPositionCount(), 1, "Block positions");
    return type.getObjectValue(SESSION, block, 0);
}
 
Example 16
Source File: KuduPageSink.java    From presto with Apache License 2.0 4 votes vote down vote up
private void appendColumn(PartialRow row, Page page, int position, int channel, int destChannel)
{
    Block block = page.getBlock(channel);
    Type type = columnTypes.get(destChannel);
    if (block.isNull(position)) {
        row.setNull(destChannel);
    }
    else if (TIMESTAMP.equals(type)) {
        row.addLong(destChannel, type.getLong(block, position) * 1000);
    }
    else if (REAL.equals(type)) {
        row.addFloat(destChannel, intBitsToFloat(toIntExact(type.getLong(block, position))));
    }
    else if (BIGINT.equals(type)) {
        row.addLong(destChannel, type.getLong(block, position));
    }
    else if (INTEGER.equals(type)) {
        row.addInt(destChannel, toIntExact(type.getLong(block, position)));
    }
    else if (SMALLINT.equals(type)) {
        row.addShort(destChannel, Shorts.checkedCast(type.getLong(block, position)));
    }
    else if (TINYINT.equals(type)) {
        row.addByte(destChannel, SignedBytes.checkedCast(type.getLong(block, position)));
    }
    else if (BOOLEAN.equals(type)) {
        row.addBoolean(destChannel, type.getBoolean(block, position));
    }
    else if (DOUBLE.equals(type)) {
        row.addDouble(destChannel, type.getDouble(block, position));
    }
    else if (isVarcharType(type)) {
        Type originalType = originalColumnTypes.get(destChannel);
        if (DATE.equals(originalType)) {
            SqlDate date = (SqlDate) originalType.getObjectValue(connectorSession, block, position);
            LocalDateTime ldt = LocalDateTime.ofEpochSecond(TimeUnit.DAYS.toSeconds(date.getDays()), 0, ZoneOffset.UTC);
            byte[] bytes = ldt.format(DateTimeFormatter.ISO_LOCAL_DATE).getBytes(StandardCharsets.UTF_8);
            row.addStringUtf8(destChannel, bytes);
        }
        else {
            row.addString(destChannel, type.getSlice(block, position).toStringUtf8());
        }
    }
    else if (VARBINARY.equals(type)) {
        row.addBinary(destChannel, type.getSlice(block, position).toByteBuffer());
    }
    else if (type instanceof DecimalType) {
        SqlDecimal sqlDecimal = (SqlDecimal) type.getObjectValue(connectorSession, block, position);
        row.addDecimal(destChannel, sqlDecimal.toBigDecimal());
    }
    else {
        throw new UnsupportedOperationException("Type is not supported: " + type);
    }
}