Search in sources :

Example 21 with CharType

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

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).getMillisUtc();
            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(com.facebook.presto.common.type.VarcharType) SqlVarbinary(com.facebook.presto.common.type.SqlVarbinary) SqlDecimal(com.facebook.presto.common.type.SqlDecimal) SqlTimestamp(com.facebook.presto.common.type.SqlTimestamp) DecimalType(com.facebook.presto.common.type.DecimalType) ArrayType(com.facebook.presto.common.type.ArrayType) CharType(com.facebook.presto.common.type.CharType) RowType(com.facebook.presto.common.type.RowType) VarcharType(com.facebook.presto.common.type.VarcharType) VarbinaryType(com.facebook.presto.common.type.VarbinaryType) MapType(com.facebook.presto.common.type.MapType) Type(com.facebook.presto.common.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) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) 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) OrcLazyObject(com.facebook.hive.orc.lazy.OrcLazyObject) CharType(com.facebook.presto.common.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(com.facebook.presto.common.block.BlockBuilder)

Example 22 with CharType

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

the class TestJdbcQueryBuilder method testBuildSqlWithChar.

@Test
public void testBuildSqlWithChar() throws SQLException {
    CharType charType = CharType.createCharType(0);
    TupleDomain<ColumnHandle> tupleDomain = TupleDomain.withColumnDomains(ImmutableMap.of(columns.get(11), Domain.create(SortedRangeSet.copyOf(charType, ImmutableList.of(Range.range(charType, utf8Slice("test_str_700"), true, utf8Slice("test_str_702"), false), Range.equal(charType, utf8Slice("test_str_180")), Range.equal(charType, utf8Slice("test_str_196")))), false)));
    Connection connection = database.getConnection();
    try (PreparedStatement preparedStatement = new QueryBuilder("\"").buildSql(jdbcClient, session, connection, "", "", "test_table", columns, tupleDomain, Optional.empty());
        ResultSet resultSet = preparedStatement.executeQuery()) {
        ImmutableSet.Builder<String> builder = ImmutableSet.builder();
        while (resultSet.next()) {
            builder.add((String) resultSet.getObject("col_11"));
        }
        assertEquals(builder.build(), ImmutableSet.of("test_str_700", "test_str_701", "test_str_180", "test_str_196"));
        assertContains(preparedStatement.toString(), "\"col_11\" >= ?");
        assertContains(preparedStatement.toString(), "\"col_11\" < ?");
        assertContains(preparedStatement.toString(), "\"col_11\" IN (?,?)");
    }
}
Also used : ColumnHandle(com.facebook.presto.spi.ColumnHandle) ImmutableSet(com.google.common.collect.ImmutableSet) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) CharType(com.facebook.presto.common.type.CharType) Test(org.testng.annotations.Test)

Example 23 with CharType

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

the class TupleDomainFilterUtils method isNotIn.

/**
 * Returns true is ranges represent != or NOT IN filter for double, float or string column.
 *
 * The logic is to return true if ranges are next to each other, but don't include the touch value.
 */
private static boolean isNotIn(List<Range> ranges) {
    if (ranges.size() <= 1) {
        return false;
    }
    Range firstRange = ranges.get(0);
    Marker previousHigh = firstRange.getHigh();
    Type type = previousHigh.getType();
    if (type != DOUBLE && type != REAL && !isVarcharType(type) && !(type instanceof CharType)) {
        return false;
    }
    Range lastRange = ranges.get(ranges.size() - 1);
    if (!firstRange.isLowUnbounded() || !lastRange.isHighUnbounded()) {
        return false;
    }
    for (int i = 1; i < ranges.size(); i++) {
        Range current = ranges.get(i);
        if (previousHigh.getBound() != Marker.Bound.BELOW || current.getLow().getBound() != Marker.Bound.ABOVE || type.compareTo(previousHigh.getValueBlock().get(), 0, current.getLow().getValueBlock().get(), 0) != 0) {
            return false;
        }
        previousHigh = current.getHigh();
    }
    return true;
}
Also used : DecimalType(com.facebook.presto.common.type.DecimalType) Varchars.isVarcharType(com.facebook.presto.common.type.Varchars.isVarcharType) CharType(com.facebook.presto.common.type.CharType) Type(com.facebook.presto.common.type.Type) CharType(com.facebook.presto.common.type.CharType) LongDecimalRange(com.facebook.presto.common.predicate.TupleDomainFilter.LongDecimalRange) BigintRange(com.facebook.presto.common.predicate.TupleDomainFilter.BigintRange) BytesRange(com.facebook.presto.common.predicate.TupleDomainFilter.BytesRange) FloatRange(com.facebook.presto.common.predicate.TupleDomainFilter.FloatRange) BigintMultiRange(com.facebook.presto.common.predicate.TupleDomainFilter.BigintMultiRange) MultiRange(com.facebook.presto.common.predicate.TupleDomainFilter.MultiRange) DoubleRange(com.facebook.presto.common.predicate.TupleDomainFilter.DoubleRange)

Example 24 with CharType

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

the class HiveWriteUtils method getRowColumnInspector.

public static ObjectInspector getRowColumnInspector(Type type) {
    if (type.equals(BooleanType.BOOLEAN)) {
        return writableBooleanObjectInspector;
    }
    if (type.equals(BigintType.BIGINT)) {
        return writableLongObjectInspector;
    }
    if (type.equals(IntegerType.INTEGER)) {
        return writableIntObjectInspector;
    }
    if (type.equals(SmallintType.SMALLINT)) {
        return writableShortObjectInspector;
    }
    if (type.equals(TinyintType.TINYINT)) {
        return writableByteObjectInspector;
    }
    if (type.equals(RealType.REAL)) {
        return writableFloatObjectInspector;
    }
    if (type.equals(DoubleType.DOUBLE)) {
        return writableDoubleObjectInspector;
    }
    if (type instanceof VarcharType) {
        VarcharType varcharType = (VarcharType) type;
        int varcharLength = varcharType.getLength();
        // VARCHAR columns with the length less than or equal to 65535 are supported natively by Hive
        if (varcharLength <= HiveVarchar.MAX_VARCHAR_LENGTH) {
            return getPrimitiveWritableObjectInspector(getVarcharTypeInfo(varcharLength));
        } else // Values for such columns must be stored as STRING in Hive
        if (varcharLength == VarcharType.UNBOUNDED_LENGTH) {
            return writableStringObjectInspector;
        }
    }
    if (isCharType(type)) {
        CharType charType = (CharType) type;
        int charLength = charType.getLength();
        return getPrimitiveWritableObjectInspector(getCharTypeInfo(charLength));
    }
    if (type.equals(VarbinaryType.VARBINARY)) {
        return writableBinaryObjectInspector;
    }
    if (type.equals(DateType.DATE)) {
        return writableDateObjectInspector;
    }
    if (type.equals(TimestampType.TIMESTAMP)) {
        return writableTimestampObjectInspector;
    }
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        return getPrimitiveWritableObjectInspector(new DecimalTypeInfo(decimalType.getPrecision(), decimalType.getScale()));
    }
    if (isArrayType(type) || isMapType(type) || isRowType(type)) {
        return getJavaObjectInspector(type);
    }
    throw new IllegalArgumentException("unsupported type: " + type);
}
Also used : DecimalTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo) VarcharType(com.facebook.presto.common.type.VarcharType) DecimalType(com.facebook.presto.common.type.DecimalType) Chars.isCharType(com.facebook.presto.common.type.Chars.isCharType) CharType(com.facebook.presto.common.type.CharType)

Example 25 with CharType

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

the class TypeCoercer method compatibility.

private TypeCompatibility compatibility(Type fromType, Type toType) {
    Type standardFromType = toStandardType(fromType);
    Type standardToType = toStandardType(toType);
    if (standardFromType.equals(standardToType)) {
        return TypeCompatibility.compatible(toSemanticType(toType, standardToType), true);
    }
    if (standardFromType.equals(UnknownType.UNKNOWN)) {
        return TypeCompatibility.compatible(toSemanticType(toType, standardToType), true);
    }
    if (standardToType.equals(UnknownType.UNKNOWN)) {
        return TypeCompatibility.compatible(toSemanticType(fromType, standardFromType), false);
    }
    String fromTypeBaseName = standardFromType.getTypeSignature().getBase();
    String toTypeBaseName = standardToType.getTypeSignature().getBase();
    if (featuresConfig.isLegacyDateTimestampToVarcharCoercion()) {
        if ((fromTypeBaseName.equals(StandardTypes.DATE) || fromTypeBaseName.equals(StandardTypes.TIMESTAMP)) && toTypeBaseName.equals(StandardTypes.VARCHAR)) {
            return TypeCompatibility.compatible(toSemanticType(toType, standardToType), true);
        }
        if (fromTypeBaseName.equals(StandardTypes.VARCHAR) && (toTypeBaseName.equals(StandardTypes.DATE) || toTypeBaseName.equals(StandardTypes.TIMESTAMP))) {
            return TypeCompatibility.compatible(toSemanticType(fromType, standardFromType), true);
        }
    }
    if (fromTypeBaseName.equals(toTypeBaseName)) {
        if (fromTypeBaseName.equals(StandardTypes.DECIMAL)) {
            Type commonSuperType = getCommonSuperTypeForDecimal((DecimalType) standardFromType, (DecimalType) standardToType);
            return TypeCompatibility.compatible(toSemanticType(toType, commonSuperType), commonSuperType.equals(standardToType));
        }
        if (fromTypeBaseName.equals(StandardTypes.VARCHAR)) {
            Type commonSuperType = getCommonSuperTypeForVarchar((VarcharType) standardFromType, (VarcharType) standardToType);
            return TypeCompatibility.compatible(toSemanticType(toType, commonSuperType), commonSuperType.equals(standardToType));
        }
        if (fromTypeBaseName.equals(StandardTypes.CHAR) && !featuresConfig.isLegacyCharToVarcharCoercion()) {
            Type commonSuperType = getCommonSuperTypeForChar((CharType) standardFromType, (CharType) standardToType);
            return TypeCompatibility.compatible(toSemanticType(toType, commonSuperType), commonSuperType.equals(standardToType));
        }
        if (fromTypeBaseName.equals(StandardTypes.ROW)) {
            return typeCompatibilityForRow((RowType) standardFromType, (RowType) standardToType).toSemanticTypeCompatibility(toType);
        }
        if (isCovariantParametrizedType(standardFromType)) {
            return typeCompatibilityForCovariantParametrizedType(standardFromType, standardToType).toSemanticTypeCompatibility(toType);
        }
        return TypeCompatibility.incompatible();
    }
    Optional<Type> coercedType = coerceTypeBase(standardFromType, standardToType.getTypeSignature().getBase());
    if (coercedType.isPresent()) {
        return compatibility(toSemanticType(toType, coercedType.get()), standardToType);
    }
    coercedType = coerceTypeBase(standardToType, standardFromType.getTypeSignature().getBase());
    if (coercedType.isPresent()) {
        TypeCompatibility typeCompatibility = compatibility(standardFromType, coercedType.get());
        if (!typeCompatibility.isCompatible()) {
            return TypeCompatibility.incompatible();
        }
        return TypeCompatibility.compatible(toSemanticType(toType, typeCompatibility.getCommonSuperType()), false);
    }
    return TypeCompatibility.incompatible();
}
Also used : CharType.createCharType(com.facebook.presto.common.type.CharType.createCharType) MapType(com.facebook.presto.common.type.MapType) DecimalType(com.facebook.presto.common.type.DecimalType) VarcharType.createUnboundedVarcharType(com.facebook.presto.common.type.VarcharType.createUnboundedVarcharType) KHyperLogLogType(com.facebook.presto.type.khyperloglog.KHyperLogLogType) SetDigestType(com.facebook.presto.type.setdigest.SetDigestType) VarcharType.createVarcharType(com.facebook.presto.common.type.VarcharType.createVarcharType) ArrayType(com.facebook.presto.common.type.ArrayType) CharType(com.facebook.presto.common.type.CharType) UnknownType(com.facebook.presto.common.type.UnknownType) Type(com.facebook.presto.common.type.Type) VarcharType(com.facebook.presto.common.type.VarcharType) RowType(com.facebook.presto.common.type.RowType) DecimalType.createDecimalType(com.facebook.presto.common.type.DecimalType.createDecimalType) RowType(com.facebook.presto.common.type.RowType)

Aggregations

CharType (com.facebook.presto.common.type.CharType)29 DecimalType (com.facebook.presto.common.type.DecimalType)23 VarcharType (com.facebook.presto.common.type.VarcharType)22 Type (com.facebook.presto.common.type.Type)16 Slice (io.airlift.slice.Slice)11 ArrayType (com.facebook.presto.common.type.ArrayType)9 RowType (com.facebook.presto.common.type.RowType)9 TimestampType (com.facebook.presto.common.type.TimestampType)9 PrestoException (com.facebook.presto.spi.PrestoException)9 ArrayList (java.util.ArrayList)9 VarbinaryType (com.facebook.presto.common.type.VarbinaryType)8 BigintType (com.facebook.presto.common.type.BigintType)7 BooleanType (com.facebook.presto.common.type.BooleanType)7 Chars.isCharType (com.facebook.presto.common.type.Chars.isCharType)7 DateType (com.facebook.presto.common.type.DateType)7 DoubleType (com.facebook.presto.common.type.DoubleType)7 IntegerType (com.facebook.presto.common.type.IntegerType)7 MapType (com.facebook.presto.common.type.MapType)7 RealType (com.facebook.presto.common.type.RealType)7 SmallintType (com.facebook.presto.common.type.SmallintType)7