Search in sources :

Example 6 with RowType

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

the class OperatorAssertion method toRow.

public static Block toRow(List<Type> parameterTypes, Object... values) {
    checkArgument(parameterTypes.size() == values.length, "parameterTypes.size(" + parameterTypes.size() + ") does not equal to values.length(" + values.length + ")");
    RowType rowType = RowType.anonymous(parameterTypes);
    BlockBuilder blockBuilder = new RowBlockBuilder(parameterTypes, null, 1);
    BlockBuilder singleRowBlockWriter = blockBuilder.beginBlockEntry();
    for (int i = 0; i < values.length; i++) {
        appendToBlockBuilder(parameterTypes.get(i), values[i], singleRowBlockWriter);
    }
    blockBuilder.closeEntry();
    return rowType.getObject(blockBuilder, 0);
}
Also used : RowBlockBuilder(com.facebook.presto.common.block.RowBlockBuilder) RowType(com.facebook.presto.common.type.RowType) RowBlockBuilder(com.facebook.presto.common.block.RowBlockBuilder) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) StructuralTestUtil.appendToBlockBuilder(com.facebook.presto.util.StructuralTestUtil.appendToBlockBuilder)

Example 7 with RowType

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

the class AbstractTestType method getNonNullValueForType.

/**
 * @return a non-null value, represented in native container type
 */
private static Object getNonNullValueForType(Type type) {
    if (type.getJavaType() == boolean.class) {
        return true;
    }
    if (type.getJavaType() == long.class) {
        return 1L;
    }
    if (type.getJavaType() == double.class) {
        return 1.0;
    }
    if (type.getJavaType() == Slice.class) {
        return Slices.utf8Slice("_");
    }
    if (type instanceof ArrayType) {
        ArrayType arrayType = (ArrayType) type;
        Type elementType = arrayType.getElementType();
        Object elementNonNullValue = getNonNullValueForType(elementType);
        return arrayBlockOf(elementType, elementNonNullValue);
    }
    if (type instanceof MapType) {
        MapType mapType = (MapType) type;
        Type keyType = mapType.getKeyType();
        Type valueType = mapType.getValueType();
        Object keyNonNullValue = getNonNullValueForType(keyType);
        Object valueNonNullValue = getNonNullValueForType(valueType);
        Map map = ImmutableMap.of(keyNonNullValue, valueNonNullValue);
        return mapBlockOf(keyType, valueType, map);
    }
    if (type instanceof RowType) {
        RowType rowType = (RowType) type;
        List<Type> elementTypes = rowType.getTypeParameters();
        Object[] elementNonNullValues = elementTypes.stream().map(AbstractTestType::getNonNullValueForType).toArray(Object[]::new);
        return toRow(elementTypes, elementNonNullValues);
    }
    throw new IllegalStateException("Unsupported Java type " + type.getJavaType() + " (for type " + type + ")");
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) MapType(com.facebook.presto.common.type.MapType) ArrayType(com.facebook.presto.common.type.ArrayType) UnknownType(com.facebook.presto.common.type.UnknownType) Type(com.facebook.presto.common.type.Type) RowType(com.facebook.presto.common.type.RowType) RowType(com.facebook.presto.common.type.RowType) Map(java.util.Map) Collections.unmodifiableSortedMap(java.util.Collections.unmodifiableSortedMap) ImmutableMap(com.google.common.collect.ImmutableMap) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap) MapType(com.facebook.presto.common.type.MapType)

Example 8 with RowType

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

the class OrcStorageManager method getColumnInfoFromOrcColumnTypes.

private List<ColumnInfo> getColumnInfoFromOrcColumnTypes(List<String> orcColumnNames, List<OrcType> orcColumnTypes) {
    Type rowType = getType(orcColumnTypes, 0);
    if (orcColumnNames.size() != rowType.getTypeParameters().size()) {
        throw new PrestoException(RAPTOR_ERROR, "Column names and types do not match");
    }
    ImmutableList.Builder<ColumnInfo> list = ImmutableList.builder();
    for (int i = 0; i < orcColumnNames.size(); i++) {
        list.add(new ColumnInfo(Long.parseLong(orcColumnNames.get(i)), rowType.getTypeParameters().get(i)));
    }
    return list.build();
}
Also used : CharType.createCharType(com.facebook.presto.common.type.CharType.createCharType) VarcharType.createUnboundedVarcharType(com.facebook.presto.common.type.VarcharType.createUnboundedVarcharType) DecimalType(com.facebook.presto.common.type.DecimalType) ArrayType(com.facebook.presto.common.type.ArrayType) RowType(com.facebook.presto.common.type.RowType) TimestampType(com.facebook.presto.common.type.TimestampType) MapType(com.facebook.presto.common.type.MapType) VarcharType.createVarcharType(com.facebook.presto.common.type.VarcharType.createVarcharType) OrcType(com.facebook.presto.orc.metadata.OrcType) Type(com.facebook.presto.common.type.Type) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) ColumnInfo(com.facebook.presto.raptor.metadata.ColumnInfo) PrestoException(com.facebook.presto.spi.PrestoException)

Example 9 with RowType

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

the class ArrayColumnValidator method generateArrayChecksum.

public static Expression generateArrayChecksum(Expression column, Type type) {
    checkArgument(type instanceof ArrayType, "Expect ArrayType, found %s", type.getDisplayName());
    Type elementType = ((ArrayType) type).getElementType();
    if (elementType.isOrderable()) {
        FunctionCall arraySort = new FunctionCall(QualifiedName.of("array_sort"), ImmutableList.of(column));
        if (elementType instanceof ArrayType || elementType instanceof RowType) {
            return new CoalesceExpression(new FunctionCall(QualifiedName.of("checksum"), ImmutableList.of(new TryExpression(arraySort))), new FunctionCall(QualifiedName.of("checksum"), ImmutableList.of(column)));
        }
        return new FunctionCall(QualifiedName.of("checksum"), ImmutableList.of(arraySort));
    }
    return new FunctionCall(QualifiedName.of("checksum"), ImmutableList.of(column));
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) ArrayType(com.facebook.presto.common.type.ArrayType) Type(com.facebook.presto.common.type.Type) RowType(com.facebook.presto.common.type.RowType) RowType(com.facebook.presto.common.type.RowType) FunctionCall(com.facebook.presto.sql.tree.FunctionCall) TryExpression(com.facebook.presto.sql.tree.TryExpression) CoalesceExpression(com.facebook.presto.sql.tree.CoalesceExpression)

Example 10 with RowType

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

the class H2QueryRunner method rowMapper.

private static RowMapper<MaterializedRow> rowMapper(List<? extends Type> types) {
    return new RowMapper<MaterializedRow>() {

        private Object getValue(Type type, ResultSet resultSet, int position) throws SQLException {
            if (BOOLEAN.equals(type)) {
                boolean booleanValue = resultSet.getBoolean(position);
                return resultSet.wasNull() ? null : booleanValue;
            } else if (TINYINT.equals(type)) {
                byte byteValue = resultSet.getByte(position);
                return resultSet.wasNull() ? null : byteValue;
            } else if (SMALLINT.equals(type)) {
                short shortValue = resultSet.getShort(position);
                return resultSet.wasNull() ? null : shortValue;
            } else if (INTEGER.equals(type)) {
                int intValue = resultSet.getInt(position);
                return resultSet.wasNull() ? null : intValue;
            } else if (BIGINT.equals(type)) {
                long longValue = resultSet.getLong(position);
                return resultSet.wasNull() ? null : longValue;
            } else if (REAL.equals(type)) {
                float floatValue = resultSet.getFloat(position);
                return resultSet.wasNull() ? null : floatValue;
            } else if (DOUBLE.equals(type)) {
                double doubleValue = resultSet.getDouble(position);
                return resultSet.wasNull() ? null : doubleValue;
            } else if (isVarcharType(type)) {
                String stringValue = resultSet.getString(position);
                return resultSet.wasNull() ? null : stringValue;
            } else if (isCharType(type)) {
                String stringValue = resultSet.getString(position);
                return resultSet.wasNull() ? null : padEnd(stringValue, ((CharType) type).getLength(), ' ');
            } else if (VARBINARY.equals(type)) {
                byte[] binary = resultSet.getBytes(position);
                return resultSet.wasNull() ? null : binary;
            } else if (DATE.equals(type)) {
                // resultSet.getDate(i) doesn't work if JVM's zone skipped day being retrieved (e.g. 2011-12-30 and Pacific/Apia zone)
                LocalDate dateValue = resultSet.getObject(position, LocalDate.class);
                return resultSet.wasNull() ? null : dateValue;
            } else if (TIME.equals(type)) {
                // resultSet.getTime(i) doesn't work if JVM's zone had forward offset change during 1970-01-01 (e.g. America/Hermosillo zone)
                LocalTime timeValue = resultSet.getObject(position, LocalTime.class);
                return resultSet.wasNull() ? null : timeValue;
            } else if (TIME_WITH_TIME_ZONE.equals(type)) {
                throw new UnsupportedOperationException("H2 does not support TIME WITH TIME ZONE");
            } else if (TIMESTAMP.equals(type)) {
                // resultSet.getTimestamp(i) doesn't work if JVM's zone had forward offset at the date/time being retrieved
                LocalDateTime timestampValue;
                try {
                    timestampValue = resultSet.getObject(position, LocalDateTime.class);
                } catch (SQLException first) {
                    // H2 cannot convert DATE to LocalDateTime in their JDBC driver (even though it can convert to java.sql.Timestamp), we need to do this manually
                    try {
                        timestampValue = Optional.ofNullable(resultSet.getObject(position, LocalDate.class)).map(LocalDate::atStartOfDay).orElse(null);
                    } catch (RuntimeException e) {
                        first.addSuppressed(e);
                        throw first;
                    }
                }
                return resultSet.wasNull() ? null : timestampValue;
            } else if (TIMESTAMP_WITH_TIME_ZONE.equals(type)) {
                // This means H2 is unsuitable for testing TIMESTAMP WITH TIME ZONE-bearing queries. Those need to be tested manually.
                throw new UnsupportedOperationException();
            } else if (UNKNOWN.equals(type)) {
                Object objectValue = resultSet.getObject(position);
                checkState(resultSet.wasNull(), "Expected a null value, but got %s", objectValue);
                return null;
            } else if (type instanceof DecimalType) {
                DecimalType decimalType = (DecimalType) type;
                BigDecimal decimalValue = resultSet.getBigDecimal(position);
                return resultSet.wasNull() ? null : decimalValue.setScale(decimalType.getScale(), BigDecimal.ROUND_HALF_UP).round(new MathContext(decimalType.getPrecision()));
            } else if (type instanceof ArrayType) {
                Array array = resultSet.getArray(position);
                return resultSet.wasNull() ? null : newArrayList(mapArrayValues(((ArrayType) type), (Object[]) array.getArray()));
            } else if (type instanceof RowType) {
                Array array = resultSet.getArray(position);
                return resultSet.wasNull() ? null : newArrayList(mapRowValues((RowType) type, (Object[]) array.getArray()));
            } else if (type instanceof TypeWithName) {
                return getValue(((TypeWithName) type).getType(), resultSet, position);
            } else {
                throw new AssertionError("unhandled type: " + type);
            }
        }

        @Override
        public MaterializedRow map(ResultSet resultSet, StatementContext context) throws SQLException {
            int count = resultSet.getMetaData().getColumnCount();
            checkArgument(types.size() == count, "expected types count (%s) does not match actual column count (%s)", types.size(), count);
            List<Object> row = new ArrayList<>(count);
            for (int i = 1; i <= count; i++) {
                row.add(getValue(types.get(i - 1), resultSet, i));
            }
            return new MaterializedRow(MaterializedResult.DEFAULT_PRECISION, row);
        }
    };
}
Also used : LocalDateTime(java.time.LocalDateTime) SQLException(java.sql.SQLException) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ArrayList(java.util.ArrayList) RowType(com.facebook.presto.common.type.RowType) LocalDate(java.time.LocalDate) StatementContext(org.jdbi.v3.core.statement.StatementContext) ArrayType(com.facebook.presto.common.type.ArrayType) ResultSet(java.sql.ResultSet) RowMapper(org.jdbi.v3.core.mapper.RowMapper) TypeWithName(com.facebook.presto.common.type.TypeWithName) LocalTime(java.time.LocalTime) BigDecimal(java.math.BigDecimal) MathContext(java.math.MathContext) Array(java.sql.Array) Varchars.isVarcharType(com.facebook.presto.common.type.Varchars.isVarcharType) VarcharType(com.facebook.presto.common.type.VarcharType) TimestampType(com.facebook.presto.common.type.TimestampType) DecimalType(com.facebook.presto.common.type.DecimalType) Chars.isCharType(com.facebook.presto.common.type.Chars.isCharType) ArrayType(com.facebook.presto.common.type.ArrayType) CharType(com.facebook.presto.common.type.CharType) Type(com.facebook.presto.common.type.Type) RowType(com.facebook.presto.common.type.RowType) DecimalType(com.facebook.presto.common.type.DecimalType) Chars.isCharType(com.facebook.presto.common.type.Chars.isCharType) CharType(com.facebook.presto.common.type.CharType) MaterializedRow(com.facebook.presto.testing.MaterializedRow)

Aggregations

RowType (com.facebook.presto.common.type.RowType)61 ArrayType (com.facebook.presto.common.type.ArrayType)37 Type (com.facebook.presto.common.type.Type)32 MapType (com.facebook.presto.common.type.MapType)28 ImmutableList (com.google.common.collect.ImmutableList)19 ArrayList (java.util.ArrayList)18 DecimalType (com.facebook.presto.common.type.DecimalType)16 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)15 Test (org.testng.annotations.Test)15 List (java.util.List)14 VarcharType (com.facebook.presto.common.type.VarcharType)12 Block (com.facebook.presto.common.block.Block)11 CharType (com.facebook.presto.common.type.CharType)9 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)9 PrestoException (com.facebook.presto.spi.PrestoException)8 Map (java.util.Map)8 ImmutableMap (com.google.common.collect.ImmutableMap)7 VarcharType.createUnboundedVarcharType (com.facebook.presto.common.type.VarcharType.createUnboundedVarcharType)6 ColumnMetadata (com.facebook.presto.spi.ColumnMetadata)6 TimestampType (com.facebook.presto.common.type.TimestampType)5