Search in sources :

Example 31 with ArrayType

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

the class StorageTypeConverter method toStorageType.

public Type toStorageType(Type type) {
    Type storageType;
    if (type.equals(BOOLEAN) || type.equals(BIGINT) || type.equals(INTEGER) || type.equals(SMALLINT) || type.equals(TINYINT) || type.equals(DOUBLE) || type.equals(REAL) || type.equals(DATE) || isCharType(type) || isVarcharType(type) || isLongDecimal(type) || isShortDecimal(type) || type.equals(VARBINARY)) {
        storageType = type;
    } else // Check OrcType::toOrcType() for reference.
    if (type.equals(TIME) || type.equals(TIMESTAMP)) {
        storageType = BIGINT;
    } else if (type instanceof ArrayType) {
        storageType = new ArrayType(toStorageType(((ArrayType) type).getElementType()));
    } else if (type instanceof MapType) {
        storageType = mapType(toStorageType(((MapType) type).getKeyType()), toStorageType(((MapType) type).getValueType()));
    } else {
        throw new PrestoException(NOT_SUPPORTED, "Type not supported: " + type);
    }
    // We cannot write different java types because when Raptor calculates stats, it uses the column types rather than storage types.
    // Need to make sure min/max are compliant with both storage and column types.
    checkState(storageType.getJavaType().equals(type.getJavaType()));
    return storageType;
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) MapType(com.facebook.presto.common.type.MapType) Chars.isCharType(com.facebook.presto.common.type.Chars.isCharType) Varchars.isVarcharType(com.facebook.presto.common.type.Varchars.isVarcharType) ArrayType(com.facebook.presto.common.type.ArrayType) Type(com.facebook.presto.common.type.Type) PrestoException(com.facebook.presto.spi.PrestoException) MapType(com.facebook.presto.common.type.MapType)

Example 32 with ArrayType

use of com.facebook.presto.common.type.ArrayType 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 33 with ArrayType

use of com.facebook.presto.common.type.ArrayType 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)

Example 34 with ArrayType

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

the class TestStructuredColumnMismatchResolver method testResolveMap.

@Test
public void testResolveMap() {
    MapColumnChecksum checksum1 = new MapColumnChecksum(binary(0x1), binary(0xa), binary(0xa), binary(0xc), 1);
    MapColumnChecksum checksum2 = new MapColumnChecksum(binary(0x2), binary(0xa), binary(0xb), binary(0xc), 1);
    MapColumnChecksum checksum3 = new MapColumnChecksum(binary(0x3), binary(0xb), binary(0xa), binary(0xc), 1);
    MapColumnChecksum checksum4 = new MapColumnChecksum(binary(0x4), binary(0xb), binary(0xb), binary(0xc), 1);
    MapColumnChecksum checksum5 = new MapColumnChecksum(binary(0x5), binary(0xb), binary(0xb), binary(0x1c), 1);
    MapColumnChecksum checksum6 = new MapColumnChecksum(binary(0x5), binary(0xb), binary(0xb), binary(0xc), 2);
    // resolved
    assertResolved(createMismatchedColumn(mapType(REAL, DOUBLE), checksum1, checksum4));
    assertResolved(createMismatchedColumn(mapType(new ArrayType(REAL), RowType.anonymous(ImmutableList.of(INTEGER, DOUBLE))), checksum1, checksum4));
    // not resolved, contains no floating point types
    assertNotResolved(createMismatchedColumn(mapType(new ArrayType(VARCHAR), RowType.anonymous(ImmutableList.of(INTEGER, VARCHAR))), checksum1, checksum4));
    // not resolved, cardinality mismatches
    assertNotResolved(createMismatchedColumn(mapType(DOUBLE, DOUBLE), checksum1, checksum5));
    assertNotResolved(createMismatchedColumn(mapType(DOUBLE, DOUBLE), checksum1, checksum6));
    // Key/value checks
    assertResolved(createMismatchedColumn(mapType(DOUBLE, INTEGER), checksum1, checksum3));
    assertNotResolved(createMismatchedColumn(mapType(DOUBLE, INTEGER), checksum1, checksum4));
    assertResolved(createMismatchedColumn(mapType(INTEGER, DOUBLE), checksum1, checksum2));
    assertNotResolved(createMismatchedColumn(mapType(INTEGER, DOUBLE), checksum1, checksum4));
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) MapColumnChecksum(com.facebook.presto.verifier.checksum.MapColumnChecksum) Test(org.testng.annotations.Test)

Example 35 with ArrayType

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

the class TestStructuredColumnMismatchResolver method testMixed.

@Test
public void testMixed() {
    ColumnMatchResult<?> resolvable1 = createMismatchedColumn(new ArrayType(new ArrayType(DOUBLE)), new ArrayColumnChecksum(binary(0xa), binary(0xc), 1), new ArrayColumnChecksum(binary(0xb), binary(0xc), 1));
    ColumnMatchResult<?> resolvable2 = createMismatchedColumn(mapType(REAL, DOUBLE), new MapColumnChecksum(binary(0x1), binary(0xa), binary(0xa), binary(0xc), 1), new MapColumnChecksum(binary(0x4), binary(0xb), binary(0xb), binary(0xc), 1));
    ColumnMatchResult<?> nonResolvable = createMismatchedColumn(VARCHAR, new SimpleColumnChecksum(binary(0xa)), new SimpleColumnChecksum(binary(0xb)));
    assertResolved(resolvable1, resolvable2);
    assertNotResolved(resolvable1, nonResolvable);
    assertNotResolved(nonResolvable, resolvable2);
    assertNotResolved(resolvable1, nonResolvable, resolvable2);
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) SimpleColumnChecksum(com.facebook.presto.verifier.checksum.SimpleColumnChecksum) MapColumnChecksum(com.facebook.presto.verifier.checksum.MapColumnChecksum) ArrayColumnChecksum(com.facebook.presto.verifier.checksum.ArrayColumnChecksum) Test(org.testng.annotations.Test)

Aggregations

ArrayType (com.facebook.presto.common.type.ArrayType)287 Test (org.testng.annotations.Test)219 Type (com.facebook.presto.common.type.Type)99 RowType (com.facebook.presto.common.type.RowType)79 ArrayList (java.util.ArrayList)58 ImmutableList (com.google.common.collect.ImmutableList)54 List (java.util.List)51 MapType (com.facebook.presto.common.type.MapType)39 DecimalType.createDecimalType (com.facebook.presto.common.type.DecimalType.createDecimalType)36 Arrays.asList (java.util.Arrays.asList)34 Collections.singletonList (java.util.Collections.singletonList)33 MessageType (org.apache.parquet.schema.MessageType)30 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)28 VarcharType.createUnboundedVarcharType (com.facebook.presto.common.type.VarcharType.createUnboundedVarcharType)27 MessageTypeParser.parseMessageType (org.apache.parquet.schema.MessageTypeParser.parseMessageType)27 InternalAggregationFunction (com.facebook.presto.operator.aggregation.InternalAggregationFunction)26 PrimitiveType (org.apache.parquet.schema.PrimitiveType)26 StructuralTestUtil.mapType (com.facebook.presto.tests.StructuralTestUtil.mapType)24 Block (com.facebook.presto.common.block.Block)23 DecimalType (com.facebook.presto.common.type.DecimalType)17