Search in sources :

Example 6 with CharType

use of io.prestosql.spi.type.CharType in project hetu-core by openlookeng.

the class OrcTester method getJavaObjectInspector.

private static ObjectInspector getJavaObjectInspector(Type type) {
    if (type.equals(BOOLEAN)) {
        return javaBooleanObjectInspector;
    }
    if (type.equals(BIGINT)) {
        return javaLongObjectInspector;
    }
    if (type.equals(INTEGER)) {
        return javaIntObjectInspector;
    }
    if (type.equals(SMALLINT)) {
        return javaShortObjectInspector;
    }
    if (type.equals(TINYINT)) {
        return javaByteObjectInspector;
    }
    if (type.equals(REAL)) {
        return javaFloatObjectInspector;
    }
    if (type.equals(DOUBLE)) {
        return javaDoubleObjectInspector;
    }
    if (type instanceof VarcharType) {
        return javaStringObjectInspector;
    }
    if (type instanceof CharType) {
        int charLength = ((CharType) type).getLength();
        return new JavaHiveCharObjectInspector(getCharTypeInfo(charLength));
    }
    if (type instanceof VarbinaryType) {
        return javaByteArrayObjectInspector;
    }
    if (type.equals(DATE)) {
        return javaDateObjectInspector;
    }
    if (type.equals(TIMESTAMP)) {
        return javaTimestampObjectInspector;
    }
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        return getPrimitiveJavaObjectInspector(new DecimalTypeInfo(decimalType.getPrecision(), decimalType.getScale()));
    }
    if (type.getTypeSignature().getBase().equals(StandardTypes.ARRAY)) {
        return getStandardListObjectInspector(getJavaObjectInspector(type.getTypeParameters().get(0)));
    }
    if (type.getTypeSignature().getBase().equals(StandardTypes.MAP)) {
        ObjectInspector keyObjectInspector = getJavaObjectInspector(type.getTypeParameters().get(0));
        ObjectInspector valueObjectInspector = getJavaObjectInspector(type.getTypeParameters().get(1));
        return getStandardMapObjectInspector(keyObjectInspector, valueObjectInspector);
    }
    if (type.getTypeSignature().getBase().equals(StandardTypes.ROW)) {
        return getStandardStructObjectInspector(type.getTypeSignature().getParameters().stream().map(parameter -> parameter.getNamedTypeSignature().getName().get()).collect(toList()), type.getTypeParameters().stream().map(OrcTester::getJavaObjectInspector).collect(toList()));
    }
    throw new IllegalArgumentException("unsupported type: " + type);
}
Also used : DecimalTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo) JavaHiveCharObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.JavaHiveCharObjectInspector) PrimitiveObjectInspectorFactory.javaByteObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaByteObjectInspector) PrimitiveObjectInspectorFactory.javaLongObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaLongObjectInspector) PrimitiveObjectInspectorFactory.javaTimestampObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaTimestampObjectInspector) PrimitiveObjectInspectorFactory.javaDateObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaDateObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) PrimitiveObjectInspectorFactory.javaByteArrayObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaByteArrayObjectInspector) PrimitiveObjectInspectorFactory.javaFloatObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaFloatObjectInspector) PrimitiveObjectInspectorFactory.javaDoubleObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaDoubleObjectInspector) PrimitiveObjectInspectorFactory.javaIntObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaIntObjectInspector) JavaHiveCharObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.JavaHiveCharObjectInspector) PrimitiveObjectInspectorFactory.javaShortObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaShortObjectInspector) ObjectInspectorFactory.getStandardStructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory.getStandardStructObjectInspector) SettableStructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.SettableStructObjectInspector) StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector) PrimitiveObjectInspectorFactory.javaBooleanObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaBooleanObjectInspector) PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector) ObjectInspectorFactory.getStandardMapObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory.getStandardMapObjectInspector) ObjectInspectorFactory.getStandardListObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory.getStandardListObjectInspector) PrimitiveObjectInspectorFactory.javaStringObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaStringObjectInspector) VarbinaryType(io.prestosql.spi.type.VarbinaryType) VarcharType(io.prestosql.spi.type.VarcharType) DecimalType(io.prestosql.spi.type.DecimalType) CharType(io.prestosql.spi.type.CharType)

Example 7 with CharType

use of io.prestosql.spi.type.CharType in project hetu-core by openlookeng.

the class OrcTester method writeValue.

private static void writeValue(Type type, BlockBuilder blockBuilder, Object value) {
    if (value == null) {
        blockBuilder.appendNull();
    } else {
        if (BOOLEAN.equals(type)) {
            type.writeBoolean(blockBuilder, (Boolean) value);
        } else if (TINYINT.equals(type) || SMALLINT.equals(type) || INTEGER.equals(type) || BIGINT.equals(type)) {
            type.writeLong(blockBuilder, ((Number) value).longValue());
        } else if (Decimals.isShortDecimal(type)) {
            type.writeLong(blockBuilder, ((SqlDecimal) value).toBigDecimal().unscaledValue().longValue());
        } else if (Decimals.isLongDecimal(type)) {
            type.writeSlice(blockBuilder, Decimals.encodeUnscaledValue(((SqlDecimal) value).toBigDecimal().unscaledValue()));
        } else if (DOUBLE.equals(type)) {
            type.writeDouble(blockBuilder, ((Number) value).doubleValue());
        } else if (REAL.equals(type)) {
            float floatValue = ((Number) value).floatValue();
            type.writeLong(blockBuilder, Float.floatToIntBits(floatValue));
        } else if (type instanceof VarcharType) {
            Slice slice = truncateToLength(utf8Slice((String) value), type);
            type.writeSlice(blockBuilder, slice);
        } else if (type instanceof CharType) {
            Slice slice = truncateToLengthAndTrimSpaces(utf8Slice((String) value), type);
            type.writeSlice(blockBuilder, slice);
        } else if (VARBINARY.equals(type)) {
            type.writeSlice(blockBuilder, Slices.wrappedBuffer(((SqlVarbinary) value).getBytes()));
        } else if (DATE.equals(type)) {
            long days = ((SqlDate) value).getDays();
            type.writeLong(blockBuilder, days);
        } else if (TIMESTAMP.equals(type)) {
            long millis = ((SqlTimestamp) value).getMillis();
            type.writeLong(blockBuilder, millis);
        } else {
            String baseType = type.getTypeSignature().getBase();
            if (StandardTypes.ARRAY.equals(baseType)) {
                List<?> array = (List<?>) value;
                Type elementType = type.getTypeParameters().get(0);
                BlockBuilder arrayBlockBuilder = blockBuilder.beginBlockEntry();
                for (Object elementValue : array) {
                    writeValue(elementType, arrayBlockBuilder, elementValue);
                }
                blockBuilder.closeEntry();
            } else if (StandardTypes.MAP.equals(baseType)) {
                Map<?, ?> map = (Map<?, ?>) value;
                Type keyType = type.getTypeParameters().get(0);
                Type valueType = type.getTypeParameters().get(1);
                BlockBuilder mapBlockBuilder = blockBuilder.beginBlockEntry();
                for (Entry<?, ?> entry : map.entrySet()) {
                    writeValue(keyType, mapBlockBuilder, entry.getKey());
                    writeValue(valueType, mapBlockBuilder, entry.getValue());
                }
                blockBuilder.closeEntry();
            } else if (StandardTypes.ROW.equals(baseType)) {
                List<?> array = (List<?>) value;
                List<Type> fieldTypes = type.getTypeParameters();
                BlockBuilder rowBlockBuilder = blockBuilder.beginBlockEntry();
                for (int fieldId = 0; fieldId < fieldTypes.size(); fieldId++) {
                    Type fieldType = fieldTypes.get(fieldId);
                    writeValue(fieldType, rowBlockBuilder, array.get(fieldId));
                }
                blockBuilder.closeEntry();
            } else {
                throw new IllegalArgumentException("Unsupported type " + type);
            }
        }
    }
}
Also used : VarcharType(io.prestosql.spi.type.VarcharType) SqlVarbinary(io.prestosql.spi.type.SqlVarbinary) SqlDecimal(io.prestosql.spi.type.SqlDecimal) SqlTimestamp(io.prestosql.spi.type.SqlTimestamp) VarbinaryType(io.prestosql.spi.type.VarbinaryType) CharType(io.prestosql.spi.type.CharType) VarcharType(io.prestosql.spi.type.VarcharType) DecimalType(io.prestosql.spi.type.DecimalType) Type(io.prestosql.spi.type.Type) Entry(java.util.Map.Entry) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) Slice(io.airlift.slice.Slice) Arrays.asList(java.util.Arrays.asList) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Collectors.toList(java.util.stream.Collectors.toList) CharType(io.prestosql.spi.type.CharType) Map(java.util.Map) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) BlockBuilder(io.prestosql.spi.block.BlockBuilder)

Example 8 with CharType

use of io.prestosql.spi.type.CharType in project hetu-core by openlookeng.

the class TestCharType method testGetObjectValue.

@Test
public void testGetObjectValue() {
    CharType charType = createCharType(3);
    for (int codePoint : ImmutableList.of(0, 1, 10, 17, (int) ' ', 127, 1011, 11_000, 65_891, MIN_SUPPLEMENTARY_CODE_POINT, MAX_CODE_POINT)) {
        BlockBuilder blockBuilder = charType.createBlockBuilder(null, 1);
        Slice slice = (codePoint != ' ') ? codePointToUtf8(codePoint) : EMPTY_SLICE;
        blockBuilder.writeBytes(slice, 0, slice.length());
        blockBuilder.closeEntry();
        Block block = blockBuilder.build();
        int codePointLengthInUtf16 = isSupplementaryCodePoint(codePoint) ? 2 : 1;
        String objectValue = (String) charType.getObjectValue(SESSION, block, 0);
        assertNotNull(objectValue);
        assertEquals(objectValue.codePointAt(0), codePoint, "first code point");
        assertEquals(objectValue.length(), codePointLengthInUtf16 + 2, "size");
        for (int i = codePointLengthInUtf16; i < objectValue.length(); i++) {
            assertEquals(objectValue.codePointAt(i), ' ');
        }
    }
}
Also used : Slice(io.airlift.slice.Slice) Block(io.prestosql.spi.block.Block) CharType.createCharType(io.prestosql.spi.type.CharType.createCharType) CharType(io.prestosql.spi.type.CharType) Character.isSupplementaryCodePoint(java.lang.Character.isSupplementaryCodePoint) BlockBuilder(io.prestosql.spi.block.BlockBuilder) Test(org.testng.annotations.Test)

Example 9 with CharType

use of io.prestosql.spi.type.CharType in project hetu-core by openlookeng.

the class H2QueryRunner method rowMapper.

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

        @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++) {
                Type type = types.get(i - 1);
                if (BOOLEAN.equals(type)) {
                    boolean booleanValue = resultSet.getBoolean(i);
                    if (resultSet.wasNull()) {
                        row.add(null);
                    } else {
                        row.add(booleanValue);
                    }
                } else if (TINYINT.equals(type)) {
                    byte byteValue = resultSet.getByte(i);
                    if (resultSet.wasNull()) {
                        row.add(null);
                    } else {
                        row.add(byteValue);
                    }
                } else if (SMALLINT.equals(type)) {
                    short shortValue = resultSet.getShort(i);
                    if (resultSet.wasNull()) {
                        row.add(null);
                    } else {
                        row.add(shortValue);
                    }
                } else if (INTEGER.equals(type)) {
                    int intValue = resultSet.getInt(i);
                    if (resultSet.wasNull()) {
                        row.add(null);
                    } else {
                        row.add(intValue);
                    }
                } else if (BIGINT.equals(type)) {
                    long longValue = resultSet.getLong(i);
                    if (resultSet.wasNull()) {
                        row.add(null);
                    } else {
                        row.add(longValue);
                    }
                } else if (REAL.equals(type)) {
                    float floatValue = resultSet.getFloat(i);
                    if (resultSet.wasNull()) {
                        row.add(null);
                    } else {
                        row.add(floatValue);
                    }
                } else if (DOUBLE.equals(type)) {
                    double doubleValue = resultSet.getDouble(i);
                    if (resultSet.wasNull()) {
                        row.add(null);
                    } else {
                        row.add(doubleValue);
                    }
                } else if (JSON.equals(type)) {
                    String stringValue = resultSet.getString(i);
                    if (resultSet.wasNull()) {
                        row.add(null);
                    } else {
                        row.add(jsonParse(utf8Slice(stringValue)).toStringUtf8());
                    }
                } else if (isVarcharType(type)) {
                    String stringValue = resultSet.getString(i);
                    if (resultSet.wasNull()) {
                        row.add(null);
                    } else {
                        row.add(stringValue);
                    }
                } else if (isCharType(type)) {
                    String stringValue = resultSet.getString(i);
                    if (resultSet.wasNull()) {
                        row.add(null);
                    } else {
                        row.add(padEnd(stringValue, ((CharType) type).getLength(), ' '));
                    }
                } 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(i, LocalDate.class);
                    if (resultSet.wasNull()) {
                        row.add(null);
                    } else {
                        row.add(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(i, LocalTime.class);
                    if (resultSet.wasNull()) {
                        row.add(null);
                    } else {
                        row.add(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(i, 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(i, LocalDate.class)).map(LocalDate::atStartOfDay).orElse(null);
                        } catch (RuntimeException e) {
                            first.addSuppressed(e);
                            throw first;
                        }
                    }
                    if (resultSet.wasNull()) {
                        row.add(null);
                    } else {
                        row.add(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(i);
                    checkState(resultSet.wasNull(), "Expected a null value, but got %s", objectValue);
                    row.add(null);
                } else if (type instanceof DecimalType) {
                    DecimalType decimalType = (DecimalType) type;
                    BigDecimal decimalValue = resultSet.getBigDecimal(i);
                    if (resultSet.wasNull()) {
                        row.add(null);
                    } else {
                        row.add(decimalValue.setScale(decimalType.getScale(), BigDecimal.ROUND_HALF_UP).round(new MathContext(decimalType.getPrecision())));
                    }
                } else if (type instanceof ArrayType) {
                    Array array = resultSet.getArray(i);
                    if (resultSet.wasNull()) {
                        row.add(null);
                    } else {
                        row.add(newArrayList((Object[]) array.getArray()));
                    }
                } else {
                    throw new AssertionError("unhandled type: " + type);
                }
            }
            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) LocalDate(java.time.LocalDate) StatementContext(org.jdbi.v3.core.statement.StatementContext) ArrayType(io.prestosql.spi.type.ArrayType) ResultSet(java.sql.ResultSet) RowMapper(org.jdbi.v3.core.mapper.RowMapper) LocalTime(java.time.LocalTime) BigDecimal(java.math.BigDecimal) MathContext(java.math.MathContext) Array(java.sql.Array) Varchars.isVarcharType(io.prestosql.spi.type.Varchars.isVarcharType) DecimalType(io.prestosql.spi.type.DecimalType) Type(io.prestosql.spi.type.Type) Chars.isCharType(io.prestosql.spi.type.Chars.isCharType) ArrayType(io.prestosql.spi.type.ArrayType) CharType(io.prestosql.spi.type.CharType) VarcharType(io.prestosql.spi.type.VarcharType) DecimalType(io.prestosql.spi.type.DecimalType) MaterializedRow(io.prestosql.testing.MaterializedRow)

Example 10 with CharType

use of io.prestosql.spi.type.CharType in project hetu-core by openlookeng.

the class HiveUtil method charPartitionKey.

public static Slice charPartitionKey(String value, String name, Type columnType) {
    Slice partitionKey = trimTrailingSpaces(Slices.utf8Slice(value));
    CharType charType = (CharType) columnType;
    if (SliceUtf8.countCodePoints(partitionKey) > charType.getLength()) {
        throw new PrestoException(HiveErrorCode.HIVE_INVALID_PARTITION_VALUE, format("Invalid partition value '%s' for %s partition key: %s", value, columnType.toString(), name));
    }
    return partitionKey;
}
Also used : Slice(io.airlift.slice.Slice) PrestoException(io.prestosql.spi.PrestoException) Chars.isCharType(io.prestosql.spi.type.Chars.isCharType) CharType(io.prestosql.spi.type.CharType)

Aggregations

CharType (io.prestosql.spi.type.CharType)48 DecimalType (io.prestosql.spi.type.DecimalType)40 VarcharType (io.prestosql.spi.type.VarcharType)39 PrestoException (io.prestosql.spi.PrestoException)23 Type (io.prestosql.spi.type.Type)23 Slice (io.airlift.slice.Slice)16 TimestampType (io.prestosql.spi.type.TimestampType)13 ArrayType (io.prestosql.spi.type.ArrayType)11 Chars.isCharType (io.prestosql.spi.type.Chars.isCharType)11 DateType (io.prestosql.spi.type.DateType)11 DoubleType (io.prestosql.spi.type.DoubleType)11 RealType (io.prestosql.spi.type.RealType)11 VarbinaryType (io.prestosql.spi.type.VarbinaryType)11 BigDecimal (java.math.BigDecimal)10 ArrayList (java.util.ArrayList)10 BigintType (io.prestosql.spi.type.BigintType)9 BooleanType (io.prestosql.spi.type.BooleanType)9 IntegerType (io.prestosql.spi.type.IntegerType)9 SmallintType (io.prestosql.spi.type.SmallintType)9 TinyintType (io.prestosql.spi.type.TinyintType)8